1
Fork 0

Convert many libsyntax records into structs

Specifically:

ast_map::ctx
ast_util::id_range
diagnostic::{handler_t,codemap_t}
auto_encode::field
ext::base::{macro_def,syntax_expander_tt,syntax_expander_tt_item}
ext::pipes::proto::next_state
This commit is contained in:
Erick Tryzelaar 2013-01-17 08:55:28 -08:00 committed by Tim Chevalier
parent 28da4ecdaa
commit 7f2c399f3a
12 changed files with 83 additions and 53 deletions

View file

@ -164,7 +164,7 @@ fn reserve_id_range(sess: Session,
let to_id_min = sess.parse_sess.next_id; let to_id_min = sess.parse_sess.next_id;
let to_id_max = sess.parse_sess.next_id + cnt; let to_id_max = sess.parse_sess.next_id + cnt;
sess.parse_sess.next_id = to_id_max; sess.parse_sess.next_id = to_id_max;
return {min: to_id_min, max: to_id_min}; ast_util::id_range { min: to_id_min, max: to_id_min }
} }
impl extended_decode_ctxt { impl extended_decode_ctxt {

View file

@ -108,8 +108,12 @@ enum ast_node {
} }
type map = std::map::HashMap<node_id, ast_node>; type map = std::map::HashMap<node_id, ast_node>;
type ctx = {map: map, mut path: path, struct ctx {
mut local_id: uint, diag: span_handler}; map: map,
mut path: path,
mut local_id: uint,
diag: span_handler,
}
type vt = visit::vt<ctx>; type vt = visit::vt<ctx>;
fn extend(cx: ctx, +elt: ident) -> @path { fn extend(cx: ctx, +elt: ident) -> @path {
@ -131,12 +135,14 @@ fn mk_ast_map_visitor() -> vt {
} }
fn map_crate(diag: span_handler, c: crate) -> map { fn map_crate(diag: span_handler, c: crate) -> map {
let cx = {map: std::map::HashMap(), let cx = ctx {
map: std::map::HashMap(),
mut path: ~[], mut path: ~[],
mut local_id: 0u, mut local_id: 0u,
diag: diag}; diag: diag,
};
visit::visit_crate(c, cx, mk_ast_map_visitor()); visit::visit_crate(c, cx, mk_ast_map_visitor());
return cx.map; cx.map
} }
// Used for items loaded from external crate that are being inlined into this // Used for items loaded from external crate that are being inlined into this
@ -150,10 +156,12 @@ fn map_decoded_item(diag: span_handler,
// alias analysis, which we will not be running on the inlined items, and // alias analysis, which we will not be running on the inlined items, and
// even if we did I think it only needs an ordering between local // even if we did I think it only needs an ordering between local
// variables that are simultaneously in scope). // variables that are simultaneously in scope).
let cx = {map: map, let cx = ctx {
map: map,
mut path: /* FIXME (#2543) */ copy path, mut path: /* FIXME (#2543) */ copy path,
mut local_id: 0u, mut local_id: 0u,
diag: diag}; diag: diag,
};
let v = mk_ast_map_visitor(); let v = mk_ast_map_visitor();
// methods get added to the AST map when their impl is visited. Since we // methods get added to the AST map when their impl is visited. Since we

View file

@ -455,7 +455,10 @@ fn dtor_dec() -> fn_decl {
#[auto_encode] #[auto_encode]
#[auto_decode] #[auto_decode]
type id_range = {min: node_id, max: node_id}; struct id_range {
min: node_id,
max: node_id,
}
fn empty(range: id_range) -> bool { fn empty(range: id_range) -> bool {
range.min >= range.max range.min >= range.max
@ -596,7 +599,7 @@ fn compute_id_range(visit_ids_fn: fn(fn@(node_id))) -> id_range {
*min = int::min(*min, id); *min = int::min(*min, id);
*max = int::max(*max, id + 1); *max = int::max(*max, id + 1);
} }
return {min:*min, max:*max}; id_range { min: *min, max: *max }
} }
fn compute_id_range_for_inlined_item(item: inlined_item) -> id_range { fn compute_id_range_for_inlined_item(item: inlined_item) -> id_range {

View file

@ -57,15 +57,15 @@ trait handler {
fn emit(cmsp: Option<(@codemap::CodeMap, span)>, msg: &str, lvl: level); fn emit(cmsp: Option<(@codemap::CodeMap, span)>, msg: &str, lvl: level);
} }
type handler_t = @{ struct handler_t {
mut err_count: uint, mut err_count: uint,
emit: emitter emit: emitter,
}; }
type codemap_t = @{ struct codemap_t {
handler: handler, handler: handler,
cm: @codemap::CodeMap cm: @codemap::CodeMap,
}; }
impl codemap_t: span_handler { impl codemap_t: span_handler {
fn span_fatal(sp: span, msg: &str) -> ! { fn span_fatal(sp: span, msg: &str) -> ! {
@ -138,7 +138,7 @@ fn ice_msg(msg: &str) -> ~str {
} }
fn mk_span_handler(handler: handler, cm: @codemap::CodeMap) -> span_handler { fn mk_span_handler(handler: handler, cm: @codemap::CodeMap) -> span_handler {
@{ handler: handler, cm: cm } as span_handler @codemap_t { handler: handler, cm: cm } as span_handler
} }
fn mk_handler(emitter: Option<emitter>) -> handler { fn mk_handler(emitter: Option<emitter>) -> handler {
@ -154,12 +154,7 @@ fn mk_handler(emitter: Option<emitter>) -> handler {
} }
}; };
let x: handler_t = @{ @handler_t { mut err_count: 0, emit: emit } as handler
mut err_count: 0,
emit: emit
};
x as handler
} }
enum level { enum level {

View file

@ -46,7 +46,7 @@ references other non-built-in types. A type definition like:
#[auto_encode] #[auto_encode]
#[auto_decode] #[auto_decode]
type spanned<T> = {node: T, span: span}; struct spanned<T> {node: T, span: span}
would yield functions like: would yield functions like:
@ -810,11 +810,15 @@ fn mk_struct_deser_impl(
// Records and structs don't have the same fields types, but they share enough // Records and structs don't have the same fields types, but they share enough
// that if we extract the right subfields out we can share the code // that if we extract the right subfields out we can share the code
// generator code. // generator code.
type field = { span: span, ident: ast::ident, mutbl: ast::mutability }; struct field {
span: span,
ident: ast::ident,
mutbl: ast::mutability,
}
fn mk_rec_fields(fields: ~[ast::ty_field]) -> ~[field] { fn mk_rec_fields(fields: ~[ast::ty_field]) -> ~[field] {
do fields.map |field| { do fields.map |field| {
{ field {
span: field.span, span: field.span,
ident: field.node.ident, ident: field.node.ident,
mutbl: field.node.mt.mutbl, mutbl: field.node.mt.mutbl,
@ -830,7 +834,7 @@ fn mk_struct_fields(fields: ~[@ast::struct_field]) -> ~[field] {
unnamed fields", unnamed fields",
}; };
{ field {
span: field.span, span: field.span,
ident: ident, ident: ident,
mutbl: match mutbl { mutbl: match mutbl {
@ -886,7 +890,7 @@ fn mk_ser_fields(
fn mk_deser_fields( fn mk_deser_fields(
cx: ext_ctxt, cx: ext_ctxt,
span: span, span: span,
fields: ~[{ span: span, ident: ast::ident, mutbl: ast::mutability }] fields: ~[field]
) -> ~[ast::field] { ) -> ~[ast::field] {
do fields.mapi |idx, field| { do fields.mapi |idx, field| {
// ast for `|| std::serialize::decode(__d)` // ast for `|| std::serialize::decode(__d)`

View file

@ -32,17 +32,27 @@ use std::map::HashMap;
// is now probably a redundant AST node, can be merged with // is now probably a redundant AST node, can be merged with
// ast::mac_invoc_tt. // ast::mac_invoc_tt.
type macro_def = {name: ~str, ext: syntax_extension}; struct macro_def {
name: ~str,
ext: syntax_extension,
}
type item_decorator = type item_decorator =
fn@(ext_ctxt, span, ast::meta_item, ~[@ast::item]) -> ~[@ast::item]; fn@(ext_ctxt, span, ast::meta_item, ~[@ast::item]) -> ~[@ast::item];
type syntax_expander_tt = {expander: syntax_expander_tt_, span: Option<span>}; struct syntax_expander_tt {
expander: syntax_expander_tt_,
span: Option<span>,
}
type syntax_expander_tt_ = fn@(ext_ctxt, span, ~[ast::token_tree]) type syntax_expander_tt_ = fn@(ext_ctxt, span, ~[ast::token_tree])
-> mac_result; -> mac_result;
type syntax_expander_tt_item struct syntax_expander_tt_item {
= {expander: syntax_expander_tt_item_, span: Option<span>}; expander: syntax_expander_tt_item_,
span: Option<span>,
}
type syntax_expander_tt_item_ type syntax_expander_tt_item_
= fn@(ext_ctxt, span, ast::ident, ~[ast::token_tree]) -> mac_result; = fn@(ext_ctxt, span, ast::ident, ~[ast::token_tree]) -> mac_result;
@ -70,10 +80,10 @@ enum syntax_extension {
// AST nodes into full ASTs // AST nodes into full ASTs
fn syntax_expander_table() -> HashMap<~str, syntax_extension> { fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
fn builtin_normal_tt(f: syntax_expander_tt_) -> syntax_extension { fn builtin_normal_tt(f: syntax_expander_tt_) -> syntax_extension {
normal_tt({expander: f, span: None}) normal_tt(syntax_expander_tt {expander: f, span: None})
} }
fn builtin_item_tt(f: syntax_expander_tt_item_) -> syntax_extension { fn builtin_item_tt(f: syntax_expander_tt_item_) -> syntax_extension {
item_tt({expander: f, span: None}) item_tt(syntax_expander_tt_item {expander: f, span: None})
} }
let syntax_expanders = HashMap(); let syntax_expanders = HashMap();
syntax_expanders.insert(~"macro_rules", syntax_expanders.insert(~"macro_rules",

View file

@ -46,7 +46,9 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt,
cx.span_fatal(pth.span, cx.span_fatal(pth.span,
fmt!("macro undefined: '%s'", *extname)) fmt!("macro undefined: '%s'", *extname))
} }
Some(normal_tt({expander: exp, span: exp_sp})) => { Some(normal_tt(
syntax_expander_tt { expander: exp, span: exp_sp }
)) => {
cx.bt_push(ExpandedFrom({call_site: s, cx.bt_push(ExpandedFrom({call_site: s,
callie: {name: *extname, span: exp_sp}})); callie: {name: *extname, span: exp_sp}}));
@ -231,7 +233,9 @@ fn expand_stmt(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt,
None => None =>
cx.span_fatal(pth.span, fmt!("macro undefined: '%s'", *extname)), cx.span_fatal(pth.span, fmt!("macro undefined: '%s'", *extname)),
Some(normal_tt({expander: exp, span: exp_sp})) => { Some(normal_tt(
syntax_expander_tt { expander: exp, span: exp_sp }
)) => {
cx.bt_push(ExpandedFrom( cx.bt_push(ExpandedFrom(
{call_site: sp, callie: {name: *extname, span: exp_sp}})); {call_site: sp, callie: {name: *extname, span: exp_sp}}));
let expanded = match exp(cx, mac.span, tts) { let expanded = match exp(cx, mac.span, tts) {

View file

@ -52,9 +52,9 @@ impl ext_ctxt: proto::visitor<(), (), ()> {
} }
fn visit_message(name: ~str, _span: span, _tys: &[@ast::Ty], fn visit_message(name: ~str, _span: span, _tys: &[@ast::Ty],
this: state, next: next_state) { this: state, next: Option<next_state>) {
match next { match next {
Some({state: ref next, tys: next_tys}) => { Some(next_state { state: ref next, tys: next_tys }) => {
let proto = this.proto; let proto = this.proto;
if !proto.has_state((*next)) { if !proto.has_state((*next)) {
// This should be a span fatal, but then we need to // This should be a span fatal, but then we need to

View file

@ -88,7 +88,7 @@ impl parser::Parser: proto_parser {
|p| p.parse_ty(false)) |p| p.parse_ty(false))
} }
else { ~[] }; else { ~[] };
Some({state: name, tys: ntys}) Some(next_state {state: name, tys: ntys})
} }
token::NOT => { token::NOT => {
// -> ! // -> !

View file

@ -49,7 +49,7 @@ impl message: gen_send {
debug!("pipec: gen_send"); debug!("pipec: gen_send");
match self { match self {
message(ref _id, span, tys, this, message(ref _id, span, tys, this,
Some({state: ref next, tys: next_tys})) => { Some(next_state {state: ref next, tys: next_tys})) => {
debug!("pipec: next state exists"); debug!("pipec: next state exists");
let next = this.proto.get_state((*next)); let next = this.proto.get_state((*next));
assert next_tys.len() == next.ty_params.len(); assert next_tys.len() == next.ty_params.len();
@ -217,7 +217,7 @@ impl state: to_type_decls {
let message(name, span, tys, this, next) = *m; let message(name, span, tys, this, next) = *m;
let tys = match next { let tys = match next {
Some({state: ref next, tys: next_tys}) => { Some(next_state { state: ref next, tys: next_tys }) => {
let next = this.proto.get_state((*next)); let next = this.proto.get_state((*next));
let next_name = cx.str_of(next.data_name()); let next_name = cx.str_of(next.data_name());

View file

@ -51,11 +51,14 @@ impl direction {
} }
} }
type next_state = Option<{state: ~str, tys: ~[@ast::Ty]}>; struct next_state {
state: ~str,
tys: ~[@ast::Ty],
}
enum message { enum message {
// name, span, data, current state, next state // name, span, data, current state, next state
message(~str, span, ~[@ast::Ty], state, next_state) message(~str, span, ~[@ast::Ty], state, Option<next_state>)
} }
impl message { impl message {
@ -94,7 +97,7 @@ enum state {
impl state { impl state {
fn add_message(name: ~str, span: span, fn add_message(name: ~str, span: span,
+data: ~[@ast::Ty], next: next_state) { +data: ~[@ast::Ty], next: Option<next_state>) {
self.messages.push(message(name, span, data, self, self.messages.push(message(name, span, data, self,
next)); next));
} }
@ -119,7 +122,7 @@ impl state {
fn reachable(f: fn(state) -> bool) { fn reachable(f: fn(state) -> bool) {
for self.messages.each |m| { for self.messages.each |m| {
match *m { match *m {
message(_, _, _, _, Some({state: ref id, _})) => { message(_, _, _, _, Some(next_state { state: ref id, _ })) => {
let state = self.proto.get_state((*id)); let state = self.proto.get_state((*id));
if !f(state) { break } if !f(state) { break }
} }
@ -217,7 +220,7 @@ trait visitor<Tproto, Tstate, Tmessage> {
fn visit_proto(proto: protocol, st: &[Tstate]) -> Tproto; fn visit_proto(proto: protocol, st: &[Tstate]) -> Tproto;
fn visit_state(state: state, m: &[Tmessage]) -> Tstate; fn visit_state(state: state, m: &[Tmessage]) -> Tstate;
fn visit_message(name: ~str, spane: span, tys: &[@ast::Ty], fn visit_message(name: ~str, spane: span, tys: &[@ast::Ty],
this: state, next: next_state) -> Tmessage; this: state, next: Option<next_state>) -> Tmessage;
} }
fn visit<Tproto, Tstate, Tmessage, V: visitor<Tproto, Tstate, Tmessage>>( fn visit<Tproto, Tstate, Tmessage, V: visitor<Tproto, Tstate, Tmessage>>(

View file

@ -139,8 +139,11 @@ fn add_new_extension(cx: ext_ctxt, sp: span, name: ident,
let exp: @fn(ext_ctxt, span, ~[ast::token_tree]) -> mac_result = let exp: @fn(ext_ctxt, span, ~[ast::token_tree]) -> mac_result =
|cx, sp, arg| generic_extension(cx, sp, name, arg, lhses, rhses); |cx, sp, arg| generic_extension(cx, sp, name, arg, lhses, rhses);
return mr_def({ mr_def(base::macro_def {
name: *cx.parse_sess().interner.get(name), name: *cx.parse_sess().interner.get(name),
ext: normal_tt({expander: exp, span: Some(sp)}) ext: normal_tt(base::syntax_expander_tt {
}); expander: exp,
span: Some(sp),
})
})
} }