libsyntax: make enum variants take refs

This commit is contained in:
Erick Tryzelaar 2013-02-17 10:59:09 -08:00
parent 59ba4fc104
commit bc62bd3782
9 changed files with 149 additions and 103 deletions

View File

@ -193,7 +193,7 @@ pub fn map_fn(fk: visit::fn_kind, decl: fn_decl, body: blk,
cx.local_id += 1u;
}
match fk {
visit::fk_dtor(tps, ref attrs, self_id, parent_id) => {
visit::fk_dtor(ref tps, ref attrs, self_id, parent_id) => {
let dt = @spanned {
node: ast::struct_dtor_ {
id: id,
@ -203,7 +203,7 @@ pub fn map_fn(fk: visit::fn_kind, decl: fn_decl, body: blk,
},
span: sp,
};
cx.map.insert(id, node_dtor(/* FIXME (#2543) */ copy tps, dt,
cx.map.insert(id, node_dtor(/* FIXME (#2543) */ copy *tps, dt,
parent_id,
@/* FIXME (#2543) */ copy cx.path));
}
@ -286,7 +286,7 @@ pub fn map_item(i: @item, &&cx: @mut Ctx, v: vt) {
map_struct_def(struct_def, node_item(i, item_path), i.ident, cx,
v);
}
item_trait(_, traits, ref methods) => {
item_trait(_, ref traits, ref methods) => {
for traits.each |p| {
cx.map.insert(p.ref_id, node_item(i, item_path));
}

View File

@ -327,8 +327,8 @@ pub impl inlined_item_utils for inlined_item {
ii_item(i) => (v.visit_item)(i, e, v),
ii_foreign(i) => (v.visit_foreign_item)(i, e, v),
ii_method(_, m) => visit::visit_method_helper(m, e, v),
ii_dtor(ref dtor, _, tps, parent_id) => {
visit::visit_struct_dtor_helper((*dtor), tps, parent_id, e, v);
ii_dtor(/*bad*/ copy dtor, _, /*bad*/ copy tps, parent_id) => {
visit::visit_struct_dtor_helper(dtor, tps, parent_id, e, v);
}
}
}

View File

@ -127,24 +127,24 @@ pub fn expand_auto_encode(
do vec::flat_map(in_items) |item| {
if item.attrs.any(is_auto_encode) {
match item.node {
ast::item_struct(@ast::struct_def { fields, _}, tps) => {
ast::item_struct(ref struct_def, ref tps) => {
let ser_impl = mk_struct_ser_impl(
cx,
item.span,
item.ident,
fields,
tps
struct_def.fields,
*tps
);
~[filter_attrs(*item), ser_impl]
},
ast::item_enum(ref enum_def, tps) => {
ast::item_enum(ref enum_def, ref tps) => {
let ser_impl = mk_enum_ser_impl(
cx,
item.span,
item.ident,
(*enum_def),
tps
*enum_def,
*tps
);
~[filter_attrs(*item), ser_impl]
@ -182,24 +182,24 @@ pub fn expand_auto_decode(
do vec::flat_map(in_items) |item| {
if item.attrs.any(is_auto_decode) {
match item.node {
ast::item_struct(@ast::struct_def { fields, _}, tps) => {
ast::item_struct(ref struct_def, ref tps) => {
let deser_impl = mk_struct_deser_impl(
cx,
item.span,
item.ident,
fields,
tps
struct_def.fields,
*tps
);
~[filter_attrs(*item), deser_impl]
},
ast::item_enum(ref enum_def, tps) => {
ast::item_enum(ref enum_def, ref tps) => {
let deser_impl = mk_enum_deser_impl(
cx,
item.span,
item.ident,
(*enum_def),
tps
*enum_def,
*tps
);
~[filter_attrs(*item), deser_impl]
@ -410,7 +410,7 @@ fn mk_impl(
ident: ast::ident,
ty_param: ast::ty_param,
path: @ast::path,
tps: ~[ast::ty_param],
tps: &[ast::ty_param],
f: fn(@ast::Ty) -> @ast::method
) -> @ast::item {
// All the type parameters need to bound to the trait.
@ -458,7 +458,7 @@ fn mk_ser_impl(
cx: ext_ctxt,
span: span,
ident: ast::ident,
tps: ~[ast::ty_param],
tps: &[ast::ty_param],
body: @ast::expr
) -> @ast::item {
// Make a path to the std::serialize::Encodable typaram.
@ -666,8 +666,8 @@ fn mk_struct_ser_impl(
cx: ext_ctxt,
span: span,
ident: ast::ident,
fields: ~[@ast::struct_field],
tps: ~[ast::ty_param]
fields: &[@ast::struct_field],
tps: &[ast::ty_param]
) -> @ast::item {
let fields = do mk_struct_fields(fields).mapi |idx, field| {
// ast for `|| self.$(name).encode(__s)`
@ -808,7 +808,7 @@ struct field {
mutbl: ast::mutability,
}
fn mk_struct_fields(fields: ~[@ast::struct_field]) -> ~[field] {
fn mk_struct_fields(fields: &[@ast::struct_field]) -> ~[field] {
do fields.map |field| {
let (ident, mutbl) = match field.node.kind {
ast::named_field(ident, mutbl, _) => (ident, mutbl),

View File

@ -54,27 +54,27 @@ pub impl proto::visitor<(), (), ()> for ext_ctxt {
fn visit_message(&self, name: ~str, _span: span, _tys: &[@ast::Ty],
this: state, next: Option<next_state>) {
match next {
Some(next_state { state: ref next, tys: next_tys }) => {
Some(ref next_state) => {
let proto = this.proto;
if !proto.has_state((*next)) {
if !proto.has_state(next_state.state) {
// This should be a span fatal, but then we need to
// track span information.
self.span_err(
proto.get_state((*next)).span,
proto.get_state(next_state.state).span,
fmt!("message %s steps to undefined state, %s",
name, (*next)));
name, next_state.state));
}
else {
let next = proto.get_state((*next));
let next = proto.get_state(next_state.state);
if next.ty_params.len() != next_tys.len() {
if next.ty_params.len() != next_state.tys.len() {
self.span_err(
next.span, // use a real span
fmt!("message %s target (%s) \
needs %u type parameters, but got %u",
name, next.name,
next.ty_params.len(),
next_tys.len()));
next_state.tys.len()));
}
}
}

View File

@ -50,16 +50,13 @@ pub impl gen_send for message {
fn gen_send(&self, cx: ext_ctxt, try: bool) -> @ast::item {
debug!("pipec: gen_send");
match *self {
message(ref _id, span, tys, this,
Some(next_state {state: ref next, tys: next_tys})) => {
message(ref _id, span, ref tys, this, Some(ref next_state)) => {
debug!("pipec: next state exists");
let next = this.proto.get_state((*next));
assert next_tys.len() == next.ty_params.len();
let next = this.proto.get_state(next_state.state);
assert next_state.tys.len() == next.ty_params.len();
let arg_names = tys.mapi(|i, _ty| cx.ident_of(~"x_"+i.to_str()));
let args_ast = (arg_names, tys).map(
|n, t| cx.arg(*n, *t)
);
let args_ast = (arg_names, *tys).map(|n, t| cx.arg(*n, *t));
let pipe_ty = cx.ty_path_ast_builder(
path(~[this.data_name()], span)
@ -119,7 +116,7 @@ pub impl gen_send for message {
let mut rty = cx.ty_path_ast_builder(path(~[next.data_name()],
span)
.add_tys(next_tys));
.add_tys(next_state.tys));
if try {
rty = cx.ty_option(rty);
}
@ -134,13 +131,13 @@ pub impl gen_send for message {
cx.expr_block(body))
}
message(ref _id, span, tys, this, None) => {
message(ref _id, span, ref tys, this, None) => {
debug!("pipec: no next state");
let arg_names = tys.mapi(|i, _ty| (~"x_" + i.to_str()));
let args_ast = (arg_names, tys).map(
|n, t| cx.arg(cx.ident_of(*n), *t)
);
let args_ast = do (arg_names, *tys).map |n, t| {
cx.arg(cx.ident_of(*n), *t)
};
let args_ast = vec::append(
~[cx.arg(cx.ident_of(~"pipe"),
@ -219,8 +216,8 @@ pub impl to_type_decls for state {
let message(name, span, tys, this, next) = *m;
let tys = match next {
Some(next_state { state: ref next, tys: next_tys }) => {
let next = this.proto.get_state((*next));
Some(ref next_state) => {
let next = this.proto.get_state((next_state.state));
let next_name = cx.str_of(next.data_name());
let dir = match this.dir {
@ -232,7 +229,7 @@ pub impl to_type_decls for state {
cx.ty_path_ast_builder(
path(~[cx.ident_of(dir),
cx.ident_of(next_name)], span)
.add_tys(next_tys)))
.add_tys(next_state.tys)))
}
None => tys
};

View File

@ -59,12 +59,13 @@ pub fn add_new_extension(cx: ext_ctxt, sp: span, name: ident,
arg_reader as reader, argument_gram);
// Extract the arguments:
let lhses:~[@named_match] = match argument_map.get(&lhs_nm) {
@matched_seq(s, _) => s,
_ => cx.span_bug(sp, ~"wrong-structured lhs")
let lhses = match argument_map.get(&lhs_nm) {
@matched_seq(ref s, _) => /* FIXME (#2543) */ copy *s,
_ => cx.span_bug(sp, ~"wrong-structured lhs")
};
let rhses:~[@named_match] = match argument_map.get(&rhs_nm) {
@matched_seq(s, _) => s,
let rhses = match argument_map.get(&rhs_nm) {
@matched_seq(ref s, _) => /* FIXME (#2543) */ copy *s,
_ => cx.span_bug(sp, ~"wrong-structured rhs")
};

View File

@ -82,10 +82,10 @@ fn fold_meta_item_(&&mi: @meta_item, fld: ast_fold) -> @meta_item {
node:
match mi.node {
meta_word(ref id) => meta_word((*id)),
meta_list(ref id, mis) => {
let fold_meta_item = |x|fold_meta_item_(x, fld);
meta_list(/* FIXME: (#2543) */ copy (*id),
vec::map(mis, |e| fold_meta_item(*e)))
meta_list(ref id, ref mis) => {
let fold_meta_item = |x| fold_meta_item_(x, fld);
meta_list(/* FIXME: (#2543) */ copy *id,
mis.map(|e| fold_meta_item(*e)))
}
meta_name_value(ref id, s) => {
meta_name_value((*id), /* FIXME (#2543) */ copy s)
@ -215,42 +215,44 @@ fn noop_fold_struct_field(&&sf: @struct_field, fld: ast_fold)
pub fn noop_fold_item_underscore(i: item_, fld: ast_fold) -> item_ {
match i {
item_const(t, e) => item_const(fld.fold_ty(t), fld.fold_expr(e)),
item_fn(decl, purity, typms, ref body) => {
item_fn(fold_fn_decl(decl, fld),
item_fn(ref decl, purity, ref typms, ref body) => {
item_fn(fold_fn_decl(/* FIXME (#2543) */ copy *decl, fld),
purity,
fold_ty_params(typms, fld),
fld.fold_block((*body)))
fold_ty_params(/* FIXME (#2543) */ copy *typms, fld),
fld.fold_block(*body))
}
item_mod(m) => item_mod(fld.fold_mod(m)),
item_foreign_mod(nm) => item_foreign_mod(fld.fold_foreign_mod(nm)),
item_ty(t, typms) => item_ty(fld.fold_ty(t),
fold_ty_params(typms, fld)),
item_enum(ref enum_definition, typms) => {
item_enum(ref enum_definition, ref typms) => {
item_enum(ast::enum_def(ast::enum_def_ {
variants: enum_definition.variants.map(
|x| fld.fold_variant(*x)),
common: enum_definition.common.map(
|x| fold_struct_def(*x, fld)),
}), fold_ty_params(typms, fld))
}), fold_ty_params(/* FIXME (#2543) */ copy *typms, fld))
}
item_struct(struct_def, typms) => {
let struct_def = fold_struct_def(struct_def, fld);
item_struct(struct_def, /* FIXME (#2543) */ copy typms)
item_struct(ref struct_def, ref typms) => {
let struct_def = fold_struct_def(
/* FIXME (#2543) */ copy *struct_def,
fld);
item_struct(struct_def, /* FIXME (#2543) */ copy *typms)
}
item_impl(tps, ifce, ty, ref methods) => {
item_impl(fold_ty_params(tps, fld),
item_impl(ref tps, ifce, ty, ref methods) => {
item_impl(fold_ty_params(/* FIXME (#2543) */ copy *tps, fld),
ifce.map(|p| fold_trait_ref(*p, fld)),
fld.fold_ty(ty),
methods.map(|x| fld.fold_method(*x)))
}
item_trait(tps, traits, ref methods) => {
item_trait(ref tps, ref traits, ref methods) => {
let methods = do methods.map |method| {
match *method {
required(*) => copy *method,
provided(method) => provided(fld.fold_method(method))
}
};
item_trait(fold_ty_params(tps, fld),
item_trait(fold_ty_params(/* FIXME (#2543) */ copy *tps, fld),
traits.map(|p| fold_trait_ref(*p, fld)),
methods)
}
@ -466,14 +468,14 @@ pub fn noop_fold_expr(e: expr_, fld: ast_fold) -> expr_ {
expr_match(fld.fold_expr(expr),
vec::map((*arms), |x| fld.fold_arm(*x)))
}
expr_fn(proto, decl, ref body, _) => {
expr_fn(proto, ref decl, ref body, _) => {
expr_fn(proto,
fold_fn_decl(decl, fld),
fold_fn_decl(/* FIXME (#2543) */ copy *decl, fld),
fld.fold_block(*body),
@())
}
expr_fn_block(decl, ref body) => {
expr_fn_block(fold_fn_decl(decl, fld),
expr_fn_block(ref decl, ref body) => {
expr_fn_block(fold_fn_decl(/* FIXME (#2543) */ copy *decl, fld),
fld.fold_block(*body))
}
expr_block(ref blk) => expr_block(fld.fold_block((*blk))),

View File

@ -495,9 +495,16 @@ pub fn print_item(s: @ps, &&item: @ast::item) {
end(s); // end the outer cbox
}
ast::item_fn(decl, purity, typarams, ref body) => {
print_fn(s, decl, Some(purity), item.ident, typarams, None,
item.vis);
ast::item_fn(ref decl, purity, ref typarams, ref body) => {
print_fn(
s,
/* FIXME (#2543) */ copy *decl,
Some(purity),
item.ident,
/* FIXME (#2543) */ copy *typarams,
None,
item.vis
);
word(s.s, ~" ");
print_block_with_attrs(s, (*body), item.attrs);
}
@ -539,9 +546,15 @@ pub fn print_item(s: @ps, &&item: @ast::item) {
word(s.s, ~";");
end(s); // end the outer ibox
}
ast::item_enum(ref enum_definition, params) => {
print_enum_def(s, (*enum_definition), params, item.ident,
item.span, item.vis);
ast::item_enum(ref enum_definition, ref params) => {
print_enum_def(
s,
*enum_definition,
/* FIXME (#2543) */ copy *params,
item.ident,
item.span,
item.vis
);
}
ast::item_struct(struct_def, tps) => {
head(s, visibility_qualified(item.vis, ~"struct"));
@ -577,13 +590,13 @@ pub fn print_item(s: @ps, &&item: @ast::item) {
bclose(s, item.span);
}
}
ast::item_trait(tps, traits, ref methods) => {
ast::item_trait(ref tps, ref traits, ref methods) => {
head(s, visibility_qualified(item.vis, ~"trait"));
print_ident(s, item.ident);
print_type_params(s, tps);
if vec::len(traits) != 0u {
print_type_params(s, /* FIXME (#2543) */ copy *tps);
if traits.len() != 0u {
word(s.s, ~":");
for vec::each(traits) |trait_| {
for traits.each |trait_| {
nbsp(s);
print_path(s, trait_.path, false);
}
@ -619,7 +632,7 @@ pub fn print_enum_def(s: @ps, enum_definition: ast::enum_def,
ident == enum_definition.variants[0].node.name;
if newtype {
match enum_definition.variants[0].node.kind {
ast::tuple_variant_kind(args) if args.len() == 1 => {}
ast::tuple_variant_kind(ref args) if args.len() == 1 => {}
_ => newtype = false
}
}
@ -1325,24 +1338,24 @@ pub fn print_expr(s: @ps, &&expr: @ast::expr) {
}
bclose_(s, expr.span, match_indent_unit);
}
ast::expr_fn(sigil, decl, ref body, _) => {
ast::expr_fn(sigil, ref decl, ref body, _) => {
// containing cbox, will be closed by print-block at }
cbox(s, indent_unit);
// head-box, will be closed by print-block at start
ibox(s, 0u);
print_fn_header_info(s, None, None, ast::Many,
Some(sigil), ast::inherited);
print_fn_args_and_ret(s, decl, None);
print_fn_args_and_ret(s, /* FIXME (#2543) */ copy *decl, None);
space(s.s);
print_block(s, (*body));
}
ast::expr_fn_block(decl, ref body) => {
ast::expr_fn_block(ref decl, ref body) => {
// in do/for blocks we don't want to show an empty
// argument list, but at this point we don't know which
// we are inside.
//
// if !decl.inputs.is_empty() {
print_fn_block_args(s, decl);
print_fn_block_args(s, /* FIXME (#2543) */ copy *decl);
space(s.s);
// }
assert (*body).node.stmts.is_empty();
@ -1809,10 +1822,15 @@ pub fn print_meta_item(s: @ps, &&item: @ast::meta_item) {
word_space(s, ~"=");
print_literal(s, @value);
}
ast::meta_list(ref name, items) => {
ast::meta_list(ref name, ref items) => {
word(s.s, (*name));
popen(s);
commasep(s, consistent, items, print_meta_item);
commasep(
s,
consistent,
/* FIXME (#2543) */ copy *items,
print_meta_item
);
pclose(s);
}
}

View File

@ -137,11 +137,20 @@ pub fn visit_item<E>(i: @item, e: E, v: vt<E>) {
(v.visit_ty)(t, e, v);
(v.visit_expr)(ex, e, v);
}
item_fn(decl, purity, tp, ref body) => {
(v.visit_fn)(fk_item_fn(/* FIXME (#2543) */ copy i.ident,
/* FIXME (#2543) */ copy tp,
purity), decl, (*body),
i.span, i.id, e, v);
item_fn(ref decl, purity, ref tp, ref body) => {
(v.visit_fn)(
fk_item_fn(
/* FIXME (#2543) */ copy i.ident,
/* FIXME (#2543) */ copy *tp,
purity
),
/* FIXME (#2543) */ copy *decl,
(*body),
i.span,
i.id,
e,
v
);
}
item_mod(m) => (v.visit_mod)(m, i.span, i.id, e, v),
item_foreign_mod(nm) => {
@ -152,9 +161,14 @@ pub fn visit_item<E>(i: @item, e: E, v: vt<E>) {
(v.visit_ty)(t, e, v);
(v.visit_ty_params)(tps, e, v);
}
item_enum(ref enum_definition, tps) => {
(v.visit_ty_params)(tps, e, v);
visit_enum_def((*enum_definition), tps, e, v);
item_enum(ref enum_definition, ref tps) => {
(v.visit_ty_params)(/* FIXME (#2543) */ copy *tps, e, v);
visit_enum_def(
*enum_definition,
/* FIXME (#2543) */ copy *tps,
e,
v
);
}
item_impl(tps, traits, ty, methods) => {
(v.visit_ty_params)(tps, e, v);
@ -170,8 +184,8 @@ pub fn visit_item<E>(i: @item, e: E, v: vt<E>) {
(v.visit_ty_params)(tps, e, v);
(v.visit_struct_def)(struct_def, i.ident, tps, i.id, e, v);
}
item_trait(tps, traits, ref methods) => {
(v.visit_ty_params)(tps, e, v);
item_trait(ref tps, ref traits, ref methods) => {
(v.visit_ty_params)(/* FIXME (#2543) */ copy *tps, e, v);
for traits.each |p| { visit_path(p.path, e, v); }
for (*methods).each |m| {
(v.visit_trait_method)(*m, e, v);
@ -460,13 +474,27 @@ pub fn visit_expr<E>(ex: @expr, e: E, v: vt<E>) {
(v.visit_expr)(x, e, v);
for (*arms).each |a| { (v.visit_arm)(*a, e, v); }
}
expr_fn(proto, decl, ref body, _) => {
(v.visit_fn)(fk_anon(proto), decl, (*body),
ex.span, ex.id, e, v);
expr_fn(proto, ref decl, ref body, _) => {
(v.visit_fn)(
fk_anon(proto),
/* FIXME (#2543) */ copy *decl,
*body,
ex.span,
ex.id,
e,
v
);
}
expr_fn_block(decl, ref body) => {
(v.visit_fn)(fk_fn_block, decl, (*body),
ex.span, ex.id, e, v);
expr_fn_block(ref decl, ref body) => {
(v.visit_fn)(
fk_fn_block,
/* FIXME (#2543) */ copy *decl,
*body,
ex.span,
ex.id,
e,
v
);
}
expr_block(ref b) => (v.visit_block)((*b), e, v),
expr_assign(a, b) => {