1
Fork 0

Convert ast::{pat,field_pat,local_,arm} into structs

This commit is contained in:
Erick Tryzelaar 2013-01-14 20:52:28 -08:00
parent 3ea3136e84
commit 4bcd19f6be
13 changed files with 185 additions and 130 deletions

View file

@ -133,14 +133,19 @@ fn filter_stmt(cx: ctxt, &&stmt: @ast::stmt) ->
}
}
fn fold_block(cx: ctxt, b: ast::blk_, fld: fold::ast_fold) ->
ast::blk_ {
fn fold_block(
cx: ctxt,
b: ast::blk_,
fld: fold::ast_fold
) -> ast::blk_ {
let filtered_stmts = vec::filter_map(b.stmts, |a| filter_stmt(cx, *a));
return {view_items: /*bad*/copy b.view_items,
stmts: vec::map(filtered_stmts, |x| fld.fold_stmt(*x)),
expr: option::map(&b.expr, |x| fld.fold_expr(*x)),
id: b.id,
rules: b.rules};
ast::blk_ {
view_items: /*bad*/copy b.view_items,
stmts: vec::map(filtered_stmts, |x| fld.fold_stmt(*x)),
expr: option::map(&b.expr, |x| fld.fold_expr(*x)),
id: b.id,
rules: b.rules,
}
}
fn item_in_cfg(cx: ctxt, item: @ast::item) -> bool {

View file

@ -469,7 +469,7 @@ fn mk_test_wrapper(cx: test_ctxt,
cf: ast::return_val
};
let wrapper_body: ast::blk = nospan({
let wrapper_body = nospan(ast::blk_ {
view_items: ~[],
stmts: ~[@call_stmt],
expr: option::None,

View file

@ -267,7 +267,10 @@ fn simplify_ast(ii: ast::inlined_item) -> ast::inlined_item {
}
};
// XXX: Bad copy.
let blk_sans_items = { stmts: stmts_sans_items,.. copy blk };
let blk_sans_items = ast::blk_ {
stmts: stmts_sans_items,
.. copy blk
};
fold::noop_fold_block(blk_sans_items, fld)
}

View file

@ -478,7 +478,7 @@ fn ctor_arity(cx: @MatchCheckCtxt, ctor: ctor, ty: ty::t) -> uint {
}
fn wild() -> @pat {
@{id: 0, node: pat_wild, span: ast_util::dummy_sp()}
@pat {id: 0, node: pat_wild, span: ast_util::dummy_sp()}
}
fn specialize(cx: @MatchCheckCtxt, r: ~[@pat], ctor_id: ctor, arity: uint,

View file

@ -499,7 +499,7 @@ fn enter_opt(bcx: block, m: &[@Match/&r], opt: &Opt, col: uint,
let _indenter = indenter();
let tcx = bcx.tcx();
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
do enter_match(bcx, tcx.def_map, m, col, val) |p| {
match /*bad*/copy p.node {
ast::pat_enum(_, subpats) => {
@ -600,7 +600,7 @@ fn enter_rec_or_struct(bcx: block, dm: DefMap, m: &[@Match/&r], col: uint,
bcx.val_str(val));
let _indenter = indenter();
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
do enter_match(bcx, dm, m, col, val) |p| {
match /*bad*/copy p.node {
ast::pat_rec(fpats, _) | ast::pat_struct(_, fpats, _) => {
@ -632,7 +632,7 @@ fn enter_tup(bcx: block, dm: DefMap, m: &[@Match/&r],
bcx.val_str(val));
let _indenter = indenter();
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
do enter_match(bcx, dm, m, col, val) |p| {
match /*bad*/copy p.node {
ast::pat_tup(elts) => {
@ -657,7 +657,7 @@ fn enter_tuple_struct(bcx: block, dm: DefMap, m: &[@Match/&r], col: uint,
bcx.val_str(val));
let _indenter = indenter();
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
do enter_match(bcx, dm, m, col, val) |p| {
match /*bad*/copy p.node {
ast::pat_enum(_, Some(elts)) => Some(elts),
@ -680,7 +680,7 @@ fn enter_box(bcx: block, dm: DefMap, m: &[@Match/&r],
bcx.val_str(val));
let _indenter = indenter();
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
do enter_match(bcx, dm, m, col, val) |p| {
match p.node {
ast::pat_box(sub) => {
@ -705,7 +705,7 @@ fn enter_uniq(bcx: block, dm: DefMap, m: &[@Match/&r],
bcx.val_str(val));
let _indenter = indenter();
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
let dummy = @ast::pat {id: 0, node: ast::pat_wild, span: dummy_sp()};
do enter_match(bcx, dm, m, col, val) |p| {
match p.node {
ast::pat_uniq(sub) => {
@ -730,7 +730,7 @@ fn enter_region(bcx: block, dm: DefMap, m: &[@Match/&r],
bcx.val_str(val));
let _indenter = indenter();
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
let dummy = @ast::pat { id: 0, node: ast::pat_wild, span: dummy_sp() };
do enter_match(bcx, dm, m, col, val) |p| {
match p.node {
ast::pat_region(sub) => {

View file

@ -323,11 +323,18 @@ struct blk_ {
#[auto_encode]
#[auto_decode]
type pat = {id: node_id, node: pat_, span: span};
struct pat {
id: node_id,
node: pat_,
span: span,
}
#[auto_encode]
#[auto_decode]
type field_pat = {ident: ident, pat: @pat};
struct field_pat {
ident: ident,
pat: @pat,
}
#[auto_encode]
#[auto_decode]
@ -637,8 +644,13 @@ enum stmt_ {
// a refinement on pat.
#[auto_encode]
#[auto_decode]
type local_ = {is_mutbl: bool, ty: @Ty, pat: @pat,
init: Option<@expr>, id: node_id};
struct local_ {
is_mutbl: bool,
ty: @Ty,
pat: @pat,
init: Option<@expr>,
id: node_id,
}
type local = spanned<local_>;
@ -650,7 +662,11 @@ enum decl_ { decl_local(~[@local]), decl_item(@item), }
#[auto_encode]
#[auto_decode]
type arm = {pats: ~[@pat], guard: Option<@expr>, body: blk};
struct arm {
pats: ~[@pat],
guard: Option<@expr>,
body: blk,
}
#[auto_encode]
#[auto_decode]

View file

@ -312,9 +312,9 @@ fn ident_to_path(s: span, +i: ident) -> @path {
}
fn ident_to_pat(id: node_id, s: span, +i: ident) -> @pat {
@{id: id,
node: pat_ident(bind_by_value, ident_to_path(s, i), None),
span: s}
@ast::pat { id: id,
node: pat_ident(bind_by_value, ident_to_path(s, i), None),
span: s }
}
pure fn is_unguarded(a: &arm) -> bool {

View file

@ -317,11 +317,14 @@ priv impl ext_ctxt {
}
fn binder_pat(span: span, nm: ast::ident) -> @ast::pat {
@{id: self.next_id(),
node: ast::pat_ident(ast::bind_by_ref(ast::m_imm),
self.path(span, ~[nm]),
None),
span: span}
@ast::pat {
id: self.next_id(),
node: ast::pat_ident(
ast::bind_by_ref(ast::m_imm),
self.path(span, ~[nm]),
None),
span: span,
}
}
fn stmt(expr: @ast::expr) -> @ast::stmt {
@ -579,12 +582,14 @@ fn mk_ser_method(
let ser_inputs = ~[{
mode: ast::infer(cx.next_id()),
ty: ty_s,
pat: @{id: cx.next_id(),
node: ast::pat_ident(
ast::bind_by_value,
ast_util::ident_to_path(span, cx.ident_of(~"__s")),
None),
span: span},
pat: @ast::pat {
id: cx.next_id(),
node: ast::pat_ident(
ast::bind_by_value,
ast_util::ident_to_path(span, cx.ident_of(~"__s")),
None),
span: span,
},
id: cx.next_id(),
}];
@ -640,12 +645,14 @@ fn mk_deser_method(
let deser_inputs = ~[{
mode: ast::infer(cx.next_id()),
ty: ty_d,
pat: @{id: cx.next_id(),
node: ast::pat_ident(
ast::bind_by_value,
ast_util::ident_to_path(span, cx.ident_of(~"__d")),
None),
span: span},
pat: @ast::pat {
id: cx.next_id(),
node: ast::pat_ident(
ast::bind_by_value,
ast_util::ident_to_path(span, cx.ident_of(~"__d")),
None),
span: span,
},
id: cx.next_id(),
}];
@ -967,7 +974,7 @@ fn ser_variant(
)
};
let pat = @{
let pat = @ast::pat {
id: cx.next_id(),
node: pat_node,
span: span,
@ -1020,7 +1027,7 @@ fn ser_variant(
]
);
{ pats: ~[pat], guard: None, body: cx.expr_blk(body) }
ast::arm { pats: ~[pat], guard: None, body: cx.expr_blk(body) }
}
fn mk_enum_ser_body(
@ -1132,21 +1139,25 @@ fn mk_enum_deser_body(
fail ~"enum variants unimplemented",
};
let pat = @{
let pat = @ast::pat {
id: cx.next_id(),
node: ast::pat_lit(cx.lit_uint(span, v_idx)),
span: span,
};
{
ast::arm {
pats: ~[pat],
guard: None,
body: cx.expr_blk(body),
}
};
let impossible_case = {
pats: ~[@{ id: cx.next_id(), node: ast::pat_wild, span: span}],
let impossible_case = ast::arm {
pats: ~[@ast::pat {
id: cx.next_id(),
node: ast::pat_wild,
span: span,
}],
guard: None,
// FIXME(#3198): proper error message
@ -1167,13 +1178,14 @@ fn mk_enum_deser_body(
node: ast::ty_infer,
span: span
},
pat: @{id: cx.next_id(),
node: ast::pat_ident(
ast::bind_by_value,
ast_util::ident_to_path(span,
cx.ident_of(~"i")),
None),
span: span},
pat: @ast::pat {
id: cx.next_id(),
node: ast::pat_ident(
ast::bind_by_value,
ast_util::ident_to_path(span, cx.ident_of(~"i")),
None),
span: span,
},
id: cx.next_id(),
}],
output: @{

View file

@ -181,18 +181,25 @@ fn mk_glob_use(cx: ext_ctxt, sp: span,
fn mk_local(cx: ext_ctxt, sp: span, mutbl: bool,
ident: ast::ident, ex: @ast::expr) -> @ast::stmt {
let pat : @ast::pat = @{id: cx.next_id(),
node: ast::pat_ident(ast::bind_by_value,
mk_raw_path(sp, ~[ident]),
None),
span: sp};
let pat = @ast::pat {
id: cx.next_id(),
node: ast::pat_ident(
ast::bind_by_value,
mk_raw_path(sp, ~[ident]),
None),
span: sp,
};
let ty : @ast::Ty = @{ id: cx.next_id(), node: ast::ty_infer, span: sp };
let local : @ast::local = @ast::spanned { node: { is_mutbl: mutbl,
ty: ty,
pat: pat,
init: Some(ex),
id: cx.next_id()},
span: sp};
let local = @ast::spanned {
node: ast::local_ {
is_mutbl: mutbl,
ty: ty,
pat: pat,
init: Some(ex),
id: cx.next_id(),
},
span: sp,
};
let decl = ast::spanned {node: ast::decl_local(~[local]), span: sp};
@ast::spanned { node: ast::stmt_decl(@decl, cx.next_id()), span: sp }
}
@ -243,7 +250,7 @@ fn mk_managed(cx: ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
mk_expr(cx, sp, ast::expr_unary(ast::box(ast::m_imm), e))
}
fn mk_pat(cx: ext_ctxt, span: span, +pat: ast::pat_) -> @ast::pat {
@{ id: cx.next_id(), node: move pat, span: span }
@ast::pat { id: cx.next_id(), node: pat, span: span }
}
fn mk_pat_ident(cx: ext_ctxt, span: span, ident: ast::ident) -> @ast::pat {
let path = mk_raw_path(span, ~[ ident ]);

View file

@ -374,22 +374,17 @@ fn create_enum_variant_pattern(cx: ext_ctxt,
prefix,
struct_def.fields.len());
let field_pats = dvec::DVec();
for struct_def.fields.eachi |i, struct_field| {
let field_pats = do struct_def.fields.mapi |i, struct_field| {
let ident = match struct_field.node.kind {
named_field(ident, _, _) => ident,
unnamed_field => {
cx.span_bug(span, ~"unexpected unnamed field");
}
};
field_pats.push({ ident: ident, pat: subpats[i] });
}
let field_pats = dvec::unwrap(move field_pats);
ast::field_pat { ident: ident, pat: subpats[i] }
};
return build::mk_pat_struct(cx,
span,
matching_path,
move field_pats);
build::mk_pat_struct(cx, span, matching_path, field_pats)
}
enum_variant_kind(*) => {
cx.span_unimpl(span, ~"enum variants for `deriving`");
@ -732,7 +727,7 @@ fn expand_deriving_eq_enum_method(cx: ext_ctxt,
matching_body_expr);
// Create the matching arm.
let matching_arm = {
let matching_arm = ast::arm {
pats: ~[ matching_pat ],
guard: None,
body: move matching_body_block
@ -743,7 +738,7 @@ fn expand_deriving_eq_enum_method(cx: ext_ctxt,
// variant then there will always be a match.
if enum_definition.variants.len() > 1 {
// Create the nonmatching pattern.
let nonmatching_pat = @{
let nonmatching_pat = @ast::pat {
id: cx.next_id(),
node: pat_wild,
span: span
@ -757,12 +752,12 @@ fn expand_deriving_eq_enum_method(cx: ext_ctxt,
nonmatching_expr);
// Create the nonmatching arm.
let nonmatching_arm = {
let nonmatching_arm = ast::arm {
pats: ~[ nonmatching_pat ],
guard: None,
body: move nonmatching_body_block
body: nonmatching_body_block,
};
other_arms.push(move nonmatching_arm);
other_arms.push(nonmatching_arm);
}
// Create the self pattern.
@ -784,10 +779,10 @@ fn expand_deriving_eq_enum_method(cx: ext_ctxt,
other_match_expr);
// Create the self arm.
let self_arm = {
let self_arm = ast::arm {
pats: ~[ self_pat ],
guard: None,
body: move other_match_body_block
body: other_match_body_block,
};
self_arms.push(move self_arm);
}
@ -813,8 +808,7 @@ fn expand_deriving_iter_bytes_enum_method(cx: ext_ctxt,
enum_definition: &enum_def)
-> @method {
// Create the arms of the match in the method body.
let arms = dvec::DVec();
for enum_definition.variants.eachi |i, variant| {
let arms = do enum_definition.variants.mapi |i, variant| {
// Create the matching pattern.
let pat = create_enum_variant_pattern(cx, span, variant, ~"__self");
@ -850,24 +844,22 @@ fn expand_deriving_iter_bytes_enum_method(cx: ext_ctxt,
let match_body_block = build::mk_block_(cx, span, move stmts);
// Create the arm.
let arm = {
ast::arm {
pats: ~[ pat ],
guard: None,
body: move match_body_block
};
arms.push(move arm);
}
body: match_body_block,
}
};
// Create the method body.
let self_ident = cx.ident_of(~"self");
let self_expr = build::mk_path(cx, span, ~[ self_ident ]);
let self_expr = build::mk_unary(cx, span, deref, self_expr);
let arms = dvec::unwrap(move arms);
let self_match_expr = expr_match(self_expr, move arms);
let self_match_expr = build::mk_expr(cx, span, move self_match_expr);
let self_match_expr = expr_match(self_expr, arms);
let self_match_expr = build::mk_expr(cx, span, self_match_expr);
let self_match_stmt = build::mk_stmt(cx, span, self_match_expr);
// Create the method.
return create_iter_bytes_method(cx, span, ~[ self_match_stmt ]);
create_iter_bytes_method(cx, span, ~[ self_match_stmt ])
}

View file

@ -181,15 +181,19 @@ impl ext_ctxt: ext_ctxt_ast_builder {
}
fn arg(name: ident, ty: @ast::Ty) -> ast::arg {
{mode: ast::infer(self.next_id()),
ty: ty,
pat: @{id: self.next_id(),
{
mode: ast::infer(self.next_id()),
ty: ty,
pat: @ast::pat {
id: self.next_id(),
node: ast::pat_ident(
ast::bind_by_value,
ast_util::ident_to_path(dummy_sp(), name),
None),
span: dummy_sp()},
id: self.next_id()}
span: dummy_sp(),
},
id: self.next_id(),
}
}
fn block(+stmts: ~[@ast::stmt], e: @ast::expr) -> ast::blk {

View file

@ -339,9 +339,11 @@ fn noop_fold_stmt(s: stmt_, fld: ast_fold) -> stmt_ {
}
fn noop_fold_arm(a: arm, fld: ast_fold) -> arm {
return {pats: vec::map(a.pats, |x| fld.fold_pat(*x)),
guard: option::map(&a.guard, |x| fld.fold_expr(*x)),
body: fld.fold_block(a.body)};
arm {
pats: vec::map(a.pats, |x| fld.fold_pat(*x)),
guard: option::map(&a.guard, |x| fld.fold_expr(*x)),
body: fld.fold_block(a.body),
}
}
fn noop_fold_pat(p: pat_, fld: ast_fold) -> pat_ {
@ -358,20 +360,22 @@ fn noop_fold_pat(p: pat_, fld: ast_fold) -> pat_ {
|pats| vec::map(*pats, |x| fld.fold_pat(*x))))
}
pat_rec(fields, etc) => {
let mut fs = ~[];
for fields.each |f| {
fs.push({ident: /* FIXME (#2543) */ copy f.ident,
pat: fld.fold_pat(f.pat)});
}
let fs = do fields.map |f| {
ast::field_pat {
ident: /* FIXME (#2543) */ copy f.ident,
pat: fld.fold_pat(f.pat),
}
};
pat_rec(fs, etc)
}
pat_struct(pth, fields, etc) => {
let pth_ = fld.fold_path(pth);
let mut fs = ~[];
for fields.each |f| {
fs.push({ident: /* FIXME (#2543) */ copy f.ident,
pat: fld.fold_pat(f.pat)});
}
let fs = do fields.map |f| {
ast::field_pat {
ident: /* FIXME (#2543) */ copy f.ident,
pat: fld.fold_pat(f.pat)
}
};
pat_struct(pth_, fs, etc)
}
pat_tup(elts) => pat_tup(vec::map(elts, |x| fld.fold_pat(*x))),
@ -634,11 +638,13 @@ fn noop_fold_path(&&p: path, fld: ast_fold) -> path {
}
fn noop_fold_local(l: local_, fld: ast_fold) -> local_ {
return {is_mutbl: l.is_mutbl,
ty: fld.fold_ty(l.ty),
pat: fld.fold_pat(l.pat),
init: l.init.map(|e| fld.fold_expr(*e)),
id: fld.new_id(l.id)};
local_ {
is_mutbl: l.is_mutbl,
ty: fld.fold_ty(l.ty),
pat: fld.fold_pat(l.pat),
init: l.init.map(|e| fld.fold_expr(*e)),
id: fld.new_id(l.id),
}
}
/* temporarily eta-expand because of a compiler bug with using `fn<T>` as a
@ -731,9 +737,11 @@ impl ast_fold_fns: ast_fold {
}
fn fold_pat(&&x: @pat) -> @pat {
let (n, s) = (self.fold_pat)(x.node, x.span, self as ast_fold);
return @{id: (self.new_id)(x.id),
node: n,
span: (self.new_span)(s)};
@pat {
id: (self.new_id)(x.id),
node: n,
span: (self.new_span)(s),
}
}
fn fold_decl(&&x: @decl) -> @decl {
let (n, s) = (self.fold_decl)(x.node, x.span, self as ast_fold);

View file

@ -1769,7 +1769,7 @@ impl Parser {
span: expr.span,
};
arms.push({pats: pats, guard: guard, body: blk});
arms.push(ast::arm { pats: pats, guard: guard, body: blk });
}
let mut hi = self.span.hi;
self.bump();
@ -1833,9 +1833,9 @@ impl Parser {
let subpat = self.parse_pat(refutable);
if is_tail {
match subpat {
@{ node: pat_wild, _ } => (),
@{ node: pat_ident(_, _, _), _ } => (),
@{ span, _ } => self.span_fatal(
@ast::pat { node: pat_wild, _ } => (),
@ast::pat { node: pat_ident(_, _, _), _ } => (),
@ast::pat { span, _ } => self.span_fatal(
span, ~"expected an identifier or `_`"
)
}
@ -1881,13 +1881,13 @@ impl Parser {
self.bump();
subpat = self.parse_pat(refutable);
} else {
subpat = @{
subpat = @ast::pat {
id: self.get_id(),
node: pat_ident(bind_infer, fieldpath, None),
span: self.last_span
};
}
fields.push({ident: fieldname, pat: subpat});
fields.push(ast::field_pat { ident: fieldname, pat: subpat });
}
return (fields, etc);
}
@ -2092,7 +2092,7 @@ impl Parser {
hi = self.span.hi;
}
}
return @{id: self.get_id(), node: pat, span: mk_sp(lo, hi)};
@ast::pat { id: self.get_id(), node: pat, span: mk_sp(lo, hi) }
}
fn parse_pat_ident(refutable: bool,
@ -2131,9 +2131,17 @@ impl Parser {
span: mk_sp(lo, lo)};
if self.eat(token::COLON) { ty = self.parse_ty(false); }
let init = if allow_init { self.parse_initializer() } else { None };
return @spanned(lo, self.last_span.hi,
{is_mutbl: is_mutbl, ty: ty, pat: pat,
init: init, id: self.get_id()});
@spanned(
lo,
self.last_span.hi,
ast::local_ {
is_mutbl: is_mutbl,
ty: ty,
pat: pat,
init: init,
id: self.get_id(),
}
)
}
fn parse_let() -> @decl {