Skip to content

Commit

Permalink
rustc: Perform some AST surgery to separate out class fields from met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
pcwalton committed Aug 15, 2012
1 parent 353c632 commit 3038968
Show file tree
Hide file tree
Showing 18 changed files with 301 additions and 308 deletions.
30 changes: 19 additions & 11 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,27 @@ type trait_ref = {path: @path, ref_id: node_id, impl_id: node_id};
#[auto_serialize]
enum visibility { public, private, inherited }

#[auto_serialize]
type struct_field_ = {
kind: struct_field_kind,
id: node_id,
ty: @ty
};

#[auto_serialize]
type struct_field = spanned<struct_field_>;

#[auto_serialize]
enum struct_field_kind {
named_field(ident, class_mutability, visibility),
unnamed_field // element of a tuple-like struct
}

#[auto_serialize]
type struct_def = {
traits: ~[@trait_ref], /* traits this class implements */
members: ~[@class_member], /* methods, etc. */
traits: ~[@trait_ref], /* traits this struct implements */
fields: ~[@struct_field], /* fields */
methods: ~[@method], /* methods */
/* (not including ctor or dtor) */
/* ctor is optional, and will soon go away */
ctor: option<class_ctor>,
Expand Down Expand Up @@ -750,15 +767,6 @@ enum item_ {
item_mac(mac),
}

#[auto_serialize]
type class_member = spanned<class_member_>;

#[auto_serialize]
enum class_member_ {
instance_var(ident, @ty, class_mutability, node_id, visibility),
class_method(@method)
}

#[auto_serialize]
enum class_mutability { class_mutable, class_immutable }

Expand Down
3 changes: 1 addition & 2 deletions src/libsyntax/ast_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ fn map_item(i: @item, cx: ctx, v: vt) {

fn map_struct_def(struct_def: @ast::struct_def, parent_node: ast_node,
ident: ast::ident, id: ast::node_id, cx: ctx, _v: vt) {
let (_, ms) = ast_util::split_class_items(struct_def.members);
// Map trait refs to their parent classes. This is
// so we can find the self_ty
for struct_def.traits.each |p| {
Expand All @@ -260,7 +259,7 @@ fn map_struct_def(struct_def: @ast::struct_def, parent_node: ast_node,
let d_id = ast_util::local_def(id);
let p = extend(cx, ident);
// only need to handle methods
do vec::iter(ms) |m| { map_method(d_id, p, m, cx); }
do vec::iter(struct_def.methods) |m| { map_method(d_id, p, m, cx); }
}

fn map_view_item(vi: @view_item, cx: ctx, _v: vt) {
Expand Down
47 changes: 10 additions & 37 deletions src/libsyntax/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,6 @@ pure fn unguarded_pat(a: arm) -> option<~[@pat]> {
if is_unguarded(a) { some(/* FIXME (#2543) */ copy a.pats) } else { none }
}

pure fn class_item_ident(ci: @class_member) -> ident {
match ci.node {
instance_var(i,_,_,_,_) => /* FIXME (#2543) */ copy i,
class_method(it) => /* FIXME (#2543) */ copy it.ident
}
}

type ivar = {ident: ident, ty: @ty, cm: class_mutability,
id: node_id, vis: visibility};

fn public_methods(ms: ~[@method]) -> ~[@method] {
vec::filter(ms,
|m| match m.vis {
Expand All @@ -313,23 +303,6 @@ fn public_methods(ms: ~[@method]) -> ~[@method] {
})
}

fn split_class_items(cs: ~[@class_member]) -> (~[ivar], ~[@method]) {
let mut vs = ~[], ms = ~[];
for cs.each |c| {
match c.node {
instance_var(i, t, cm, id, vis) => {
vec::push(vs, {ident: /* FIXME (#2543) */ copy i,
ty: t,
cm: cm,
id: id,
vis: vis});
}
class_method(m) => vec::push(ms, m)
}
};
(vs, ms)
}

// extract a ty_method from a trait_method. if the trait_method is
// a default, pull out the useful fields to make a ty_method
fn trait_method_to_ty_method(method: trait_method) -> ty_method {
Expand All @@ -355,11 +328,11 @@ fn split_trait_methods(trait_methods: ~[trait_method])
(reqd, provd)
}

pure fn class_member_visibility(ci: @class_member) -> visibility {
match ci.node {
instance_var(_, _, _, _, vis) => vis,
class_method(m) => m.vis
}
pure fn struct_field_visibility(field: ast::struct_field) -> visibility {
match field.node.kind {
ast::named_field(_, _, visibility) => visibility,
ast::unnamed_field => ast::public
}
}

trait inlined_item_utils {
Expand Down Expand Up @@ -570,11 +543,11 @@ fn id_visitor(vfn: fn@(node_id)) -> visit::vt<()> {
_id: node_id) {
},

visit_class_item: fn@(c: @class_member) {
match c.node {
instance_var(_, _, _, id,_) => vfn(id),
class_method(_) => ()
}
visit_struct_field: fn@(f: @struct_field) {
vfn(f.node.id);
},

visit_struct_method: fn@(_m: @method) {
}
})
}
Expand Down
52 changes: 26 additions & 26 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait ast_fold {
fn fold_view_item(&&@view_item) -> @view_item;
fn fold_foreign_item(&&@foreign_item) -> @foreign_item;
fn fold_item(&&@item) -> option<@item>;
fn fold_class_item(&&@class_member) -> @class_member;
fn fold_struct_field(&&@struct_field) -> @struct_field;
fn fold_item_underscore(item_) -> item_;
fn fold_method(&&@method) -> @method;
fn fold_block(blk) -> blk;
Expand Down Expand Up @@ -55,7 +55,7 @@ type ast_fold_precursor = @{
fold_view_item: fn@(view_item_, ast_fold) -> view_item_,
fold_foreign_item: fn@(&&@foreign_item, ast_fold) -> @foreign_item,
fold_item: fn@(&&@item, ast_fold) -> option<@item>,
fold_class_item: fn@(&&@class_member, ast_fold) -> @class_member,
fold_struct_field: fn@(&&@struct_field, ast_fold) -> @struct_field,
fold_item_underscore: fn@(item_, ast_fold) -> item_,
fold_method: fn@(&&@method, ast_fold) -> @method,
fold_block: fn@(blk_, span, ast_fold) -> (blk_, span),
Expand Down Expand Up @@ -214,16 +214,12 @@ fn noop_fold_item(&&i: @item, fld: ast_fold) -> option<@item> {
span: fld.new_span(i.span)});
}

fn noop_fold_class_item(&&ci: @class_member, fld: ast_fold)
-> @class_member {
@{node: match ci.node {
instance_var(ident, t, cm, id, p) => {
instance_var(/* FIXME (#2543) */ copy ident,
fld.fold_ty(t), cm, id, p)
}
class_method(m) => class_method(fld.fold_method(m))
},
span: ci.span}
fn noop_fold_struct_field(&&sf: @struct_field, fld: ast_fold)
-> @struct_field {
@{node: {kind: copy sf.node.kind,
id: sf.node.id,
ty: fld.fold_ty(sf.node.ty)},
span: sf.span}
}

fn noop_fold_item_underscore(i: item_, fld: ast_fold) -> item_ {
Expand Down Expand Up @@ -295,7 +291,8 @@ fn fold_struct_def(struct_def: @ast::struct_def, fld: ast_fold)
with dtor}};
return @{
traits: vec::map(struct_def.traits, |p| fold_trait_ref(p, fld)),
members: vec::map(struct_def.members, |x| fld.fold_class_item(x)),
fields: vec::map(struct_def.fields, |f| fold_struct_field(f, fld)),
methods: vec::map(struct_def.methods, |m| fld.fold_method(m)),
ctor: resulting_optional_constructor,
dtor: dtor
};
Expand All @@ -306,6 +303,13 @@ fn fold_trait_ref(&&p: @trait_ref, fld: ast_fold) -> @trait_ref {
impl_id: fld.new_id(p.impl_id)}
}

fn fold_struct_field(&&f: @struct_field, fld: ast_fold) -> @struct_field {
@{node: {kind: copy f.node.kind,
id: fld.new_id(f.node.id),
ty: fld.fold_ty(f.node.ty)},
span: fld.new_span(f.span)}
}

fn noop_fold_method(&&m: @method, fld: ast_fold) -> @method {
return @{ident: fld.fold_ident(m.ident),
attrs: /* FIXME (#2543) */ copy m.attrs,
Expand Down Expand Up @@ -570,8 +574,9 @@ fn noop_fold_variant(v: variant_, fld: ast_fold) -> variant_ {
with dtor}};
kind = struct_variant_kind(@{
traits: ~[],
members: vec::map(struct_def.members,
|x| fld.fold_class_item(x)),
fields: vec::map(struct_def.fields,
|f| fld.fold_struct_field(f)),
methods: vec::map(struct_def.methods, |m| fld.fold_method(m)),
ctor: none,
dtor: dtor
})
Expand Down Expand Up @@ -644,7 +649,7 @@ fn default_ast_fold() -> ast_fold_precursor {
fold_view_item: noop_fold_view_item,
fold_foreign_item: noop_fold_foreign_item,
fold_item: noop_fold_item,
fold_class_item: noop_fold_class_item,
fold_struct_field: noop_fold_struct_field,
fold_item_underscore: noop_fold_item_underscore,
fold_method: noop_fold_method,
fold_block: wrap(noop_fold_block),
Expand Down Expand Up @@ -692,16 +697,11 @@ impl ast_fold_precursor: ast_fold {
fn fold_item(&&i: @item) -> option<@item> {
return self.fold_item(i, self as ast_fold);
}
fn fold_class_item(&&ci: @class_member) -> @class_member {
@{node: match ci.node {
instance_var(nm, t, mt, id, p) => {
instance_var(/* FIXME (#2543) */ copy nm,
(self as ast_fold).fold_ty(t), mt, id, p)
}
class_method(m) => {
class_method(self.fold_method(m, self as ast_fold))
}
}, span: self.new_span(ci.span)}
fn fold_struct_field(&&sf: @struct_field) -> @struct_field {
@{node: {kind: copy sf.node.kind,
id: sf.node.id,
ty: (self as ast_fold).fold_ty(sf.node.ty)},
span: self.new_span(sf.span)}
}
fn fold_item_underscore(i: item_) ->
item_ {
Expand Down
Loading

0 comments on commit 3038968

Please sign in to comment.