Move the ids of pat AST nodes into their struct

Just like it was done with items and exprs. Simplifies some code.
This commit is contained in:
Marijn Haverbeke 2011-07-04 21:53:33 +02:00
parent b669430f72
commit b1423be1d6
11 changed files with 68 additions and 79 deletions

View file

@ -110,13 +110,15 @@ type block = spanned[block_];
type block_ = rec(vec[@stmt] stmts, option::t[@expr] expr, node_id id); type block_ = rec(vec[@stmt] stmts, option::t[@expr] expr, node_id id);
type pat = spanned[pat_]; type pat = rec(node_id id,
pat_ node,
span span);
tag pat_ { tag pat_ {
pat_wild(node_id); pat_wild;
pat_bind(ident, node_id); pat_bind(ident);
pat_lit(@lit, node_id); pat_lit(@lit);
pat_tag(path, vec[@pat], node_id); pat_tag(path, vec[@pat]);
} }
tag mutability { mut; imm; maybe_mut; } tag mutability { mut; imm; maybe_mut; }

View file

@ -252,12 +252,12 @@ fn noop_fold_arm(&arm a, ast_fold fld) -> arm {
fn noop_fold_pat(&pat_ p, ast_fold fld) -> pat_ { fn noop_fold_pat(&pat_ p, ast_fold fld) -> pat_ {
ret alt (p) { ret alt (p) {
case (pat_wild(_)) { p } case (pat_wild) { p }
case (pat_bind(?id, ?d)) { pat_bind(fld.fold_ident(id), d)} case (pat_bind(?ident)) { pat_bind(fld.fold_ident(ident))}
case (pat_lit(_, _)) { p } case (pat_lit(_)) { p }
case (pat_tag(?pth, ?pats, ?nid)) { case (pat_tag(?pth, ?pats)) {
pat_tag(fld.fold_path(pth), map(fld.fold_pat, pats), nid) pat_tag(fld.fold_path(pth), map(fld.fold_pat, pats))
} }
}; };
} }
@ -616,7 +616,7 @@ fn make_fold(&ast_fold_precursor afp) -> ast_fold {
ret afp.fold_arm(x, f); ret afp.fold_arm(x, f);
} }
fn f_pat(&ast_fold_precursor afp, ast_fold f, &@pat x) -> @pat { fn f_pat(&ast_fold_precursor afp, ast_fold f, &@pat x) -> @pat {
ret @rec(node=afp.fold_pat(x.node, f), span=x.span); ret @rec(id=x.id, node=afp.fold_pat(x.node, f), span=x.span);
} }
fn f_decl(&ast_fold_precursor afp, ast_fold f, &@decl x) -> @decl { fn f_decl(&ast_fold_precursor afp, ast_fold f, &@decl x) -> @decl {
ret @rec(node=afp.fold_decl(x.node, f), span=x.span); ret @rec(node=afp.fold_decl(x.node, f), span=x.span);

View file

@ -1396,7 +1396,7 @@ fn parse_pat(&parser p) -> @ast::pat {
alt (p.peek()) { alt (p.peek()) {
case (token::UNDERSCORE) { case (token::UNDERSCORE) {
p.bump(); p.bump();
pat = ast::pat_wild(p.get_id()); pat = ast::pat_wild;
} }
case (token::QUES) { case (token::QUES) {
p.bump(); p.bump();
@ -1404,8 +1404,7 @@ fn parse_pat(&parser p) -> @ast::pat {
case (token::IDENT(?id, _)) { case (token::IDENT(?id, _)) {
hi = p.get_hi_pos(); hi = p.get_hi_pos();
p.bump(); p.bump();
pat = pat = ast::pat_bind(p.get_str(id));
ast::pat_bind(p.get_str(id), p.get_id());
} }
case (?tok) { case (?tok) {
p.fatal("expected identifier after '?' in pattern but " + p.fatal("expected identifier after '?' in pattern but " +
@ -1418,7 +1417,7 @@ fn parse_pat(&parser p) -> @ast::pat {
if (!is_ident(tok) || is_word(p, "true") || is_word(p, "false")) { if (!is_ident(tok) || is_word(p, "true") || is_word(p, "false")) {
auto lit = parse_lit(p); auto lit = parse_lit(p);
hi = lit.span.hi; hi = lit.span.hi;
pat = ast::pat_lit(@lit, p.get_id()); pat = ast::pat_lit(@lit);
} else { } else {
auto tag_path = parse_path_and_ty_param_substs(p); auto tag_path = parse_path_and_ty_param_substs(p);
hi = tag_path.span.hi; hi = tag_path.span.hi;
@ -1434,11 +1433,11 @@ fn parse_pat(&parser p) -> @ast::pat {
} }
case (_) { args = []; } case (_) { args = []; }
} }
pat = ast::pat_tag(tag_path, args, p.get_id()); pat = ast::pat_tag(tag_path, args);
} }
} }
} }
ret @spanned(lo, hi, pat); ret @rec(id=p.get_id(), node=pat, span=rec(lo=lo, hi=hi));
} }
fn parse_local_full(&option::t[@ast::ty] tyopt, &parser p) fn parse_local_full(&option::t[@ast::ty] tyopt, &parser p)

View file

@ -291,8 +291,8 @@ fn arm_defnums(&ast::arm arm) -> vec[node_id] {
auto dnums = []; auto dnums = [];
fn walk_pat(&mutable vec[node_id] found, &@ast::pat p) { fn walk_pat(&mutable vec[node_id] found, &@ast::pat p) {
alt (p.node) { alt (p.node) {
case (ast::pat_bind(_, ?id)) { vec::push(found, id); } case (ast::pat_bind(_)) { vec::push(found, p.id); }
case (ast::pat_tag(_, ?children, _)) { case (ast::pat_tag(_, ?children)) {
for (@ast::pat child in children) { walk_pat(found, child); } for (@ast::pat child in children) { walk_pat(found, child); }
} }
case (_) { } case (_) { }

View file

@ -301,14 +301,14 @@ fn resolve_names(&@env e, &@ast::crate c) {
} }
fn walk_pat(&env e, &scopes sc, &@ast::pat pat) { fn walk_pat(&env e, &scopes sc, &@ast::pat pat) {
alt (pat.node) { alt (pat.node) {
case (ast::pat_tag(?p, ?children, ?id)) { case (ast::pat_tag(?p, ?children)) {
auto fnd = auto fnd =
lookup_path_strict(e, sc, p.span, p.node.idents, lookup_path_strict(e, sc, p.span, p.node.idents,
ns_value); ns_value);
if (option::is_some(fnd)) { if (option::is_some(fnd)) {
alt (option::get(fnd)) { alt (option::get(fnd)) {
case (ast::def_variant(?did, ?vid)) { case (ast::def_variant(?did, ?vid)) {
e.def_map.insert(id, option::get(fnd)); e.def_map.insert(pat.id, option::get(fnd));
for (@ast::pat child in children) { for (@ast::pat child in children) {
walk_pat(e, sc, child); walk_pat(e, sc, child);
} }
@ -694,14 +694,14 @@ fn lookup_in_ty_params(&ident name, &vec[ast::ty_param] ty_params) ->
fn lookup_in_pat(&ident name, &ast::pat pat) -> option::t[def] { fn lookup_in_pat(&ident name, &ast::pat pat) -> option::t[def] {
alt (pat.node) { alt (pat.node) {
case (ast::pat_bind(?p_name, ?id)) { case (ast::pat_bind(?p_name)) {
if (str::eq(p_name, name)) { if (str::eq(p_name, name)) {
ret some(ast::def_binding(local_def(id))); ret some(ast::def_binding(local_def(pat.id)));
} }
} }
case (ast::pat_wild(_)) { } case (ast::pat_wild) { }
case (ast::pat_lit(_, _)) { } case (ast::pat_lit(_)) { }
case (ast::pat_tag(_, ?pats, _)) { case (ast::pat_tag(_, ?pats)) {
for (@ast::pat p in pats) { for (@ast::pat p in pats) {
auto found = lookup_in_pat(name, *p); auto found = lookup_in_pat(name, *p);
if (!option::is_none(found)) { ret found; } if (!option::is_none(found)) { ret found; }
@ -1248,8 +1248,8 @@ fn check_arm(@env e, &ast::arm a, &() x, &vt[()] v) {
visit::visit_arm(a, x, v); visit::visit_arm(a, x, v);
fn walk_pat(checker ch, &@ast::pat p) { fn walk_pat(checker ch, &@ast::pat p) {
alt (p.node) { alt (p.node) {
case (ast::pat_bind(?name, _)) { add_name(ch, p.span, name); } case (ast::pat_bind(?name)) { add_name(ch, p.span, name); }
case (ast::pat_tag(_, ?children, _)) { case (ast::pat_tag(_, ?children)) {
for (@ast::pat child in children) { walk_pat(ch, child); } for (@ast::pat child in children) { walk_pat(ch, child); }
} }
case (_) { } case (_) { }

View file

@ -4629,19 +4629,19 @@ fn trans_do_while(&@block_ctxt cx, &ast::block body, &@ast::expr cond) ->
fn trans_pat_match(&@block_ctxt cx, &@ast::pat pat, ValueRef llval, fn trans_pat_match(&@block_ctxt cx, &@ast::pat pat, ValueRef llval,
&@block_ctxt next_cx) -> result { &@block_ctxt next_cx) -> result {
alt (pat.node) { alt (pat.node) {
case (ast::pat_wild(_)) { ret rslt(cx, llval); } case (ast::pat_wild) { ret rslt(cx, llval); }
case (ast::pat_bind(_, _)) { ret rslt(cx, llval); } case (ast::pat_bind(_)) { ret rslt(cx, llval); }
case (ast::pat_lit(?lt, ?id)) { case (ast::pat_lit(?lt)) {
auto lllit = trans_lit(cx.fcx.lcx.ccx, *lt, id); auto lllit = trans_lit(cx.fcx.lcx.ccx, *lt, pat.id);
auto lltype = ty::node_id_to_type(cx.fcx.lcx.ccx.tcx, id); auto lltype = ty::node_id_to_type(cx.fcx.lcx.ccx.tcx, pat.id);
auto lleq = trans_compare(cx, ast::eq, lltype, llval, lllit); auto lleq = trans_compare(cx, ast::eq, lltype, llval, lllit);
auto matched_cx = new_sub_block_ctxt(lleq.bcx, "matched_cx"); auto matched_cx = new_sub_block_ctxt(lleq.bcx, "matched_cx");
lleq.bcx.build.CondBr(lleq.val, matched_cx.llbb, next_cx.llbb); lleq.bcx.build.CondBr(lleq.val, matched_cx.llbb, next_cx.llbb);
ret rslt(matched_cx, llval); ret rslt(matched_cx, llval);
} }
case (ast::pat_tag(?ident, ?subpats, ?id)) { case (ast::pat_tag(?ident, ?subpats)) {
auto vdef = auto vdef =
ast::variant_def_ids(cx.fcx.lcx.ccx.tcx.def_map.get(id)); ast::variant_def_ids(cx.fcx.lcx.ccx.tcx.def_map.get(pat.id));
auto variants = ty::tag_variants(cx.fcx.lcx.ccx.tcx, vdef._0); auto variants = ty::tag_variants(cx.fcx.lcx.ccx.tcx, vdef._0);
auto matched_cx = new_sub_block_ctxt(cx, "matched_cx"); auto matched_cx = new_sub_block_ctxt(cx, "matched_cx");
auto llblobptr = llval; auto llblobptr = llval;
@ -4675,7 +4675,7 @@ fn trans_pat_match(&@block_ctxt cx, &@ast::pat pat, ValueRef llval,
} }
} }
auto ty_params = ty::node_id_to_type_params auto ty_params = ty::node_id_to_type_params
(cx.fcx.lcx.ccx.tcx, id); (cx.fcx.lcx.ccx.tcx, pat.id);
if (vec::len(subpats) > 0u) { if (vec::len(subpats) > 0u) {
auto i = 0; auto i = 0;
for (@ast::pat subpat in subpats) { for (@ast::pat subpat in subpats) {
@ -4702,29 +4702,29 @@ fn trans_pat_match(&@block_ctxt cx, &@ast::pat pat, ValueRef llval,
fn trans_pat_binding(&@block_ctxt cx, &@ast::pat pat, ValueRef llval, fn trans_pat_binding(&@block_ctxt cx, &@ast::pat pat, ValueRef llval,
bool bind_alias) -> result { bool bind_alias) -> result {
alt (pat.node) { alt (pat.node) {
case (ast::pat_wild(_)) { ret rslt(cx, llval); } case (ast::pat_wild) { ret rslt(cx, llval); }
case (ast::pat_lit(_, _)) { ret rslt(cx, llval); } case (ast::pat_lit(_)) { ret rslt(cx, llval); }
case (ast::pat_bind(?name, ?id)) { case (ast::pat_bind(?name)) {
if (bind_alias) { if (bind_alias) {
cx.fcx.lllocals.insert(id, llval); cx.fcx.lllocals.insert(pat.id, llval);
ret rslt(cx, llval); ret rslt(cx, llval);
} else { } else {
auto t = node_id_type(cx.fcx.lcx.ccx, id); auto t = node_id_type(cx.fcx.lcx.ccx, pat.id);
auto rslt = alloc_ty(cx, t); auto rslt = alloc_ty(cx, t);
auto dst = rslt.val; auto dst = rslt.val;
auto bcx = rslt.bcx; auto bcx = rslt.bcx;
maybe_name_value(cx.fcx.lcx.ccx, dst, name); maybe_name_value(cx.fcx.lcx.ccx, dst, name);
bcx.fcx.lllocals.insert(id, dst); bcx.fcx.lllocals.insert(pat.id, dst);
bcx.cleanups += [clean(bind drop_slot(_, dst, t))]; bcx.cleanups += [clean(bind drop_slot(_, dst, t))];
ret copy_val(bcx, INIT, dst, llval, t); ret copy_val(bcx, INIT, dst, llval, t);
} }
} }
case (ast::pat_tag(_, ?subpats, ?id)) { case (ast::pat_tag(_, ?subpats)) {
if (vec::len[@ast::pat](subpats) == 0u) { ret rslt(cx, llval); } if (vec::len[@ast::pat](subpats) == 0u) { ret rslt(cx, llval); }
// Get the appropriate variant for this tag. // Get the appropriate variant for this tag.
auto vdef = auto vdef =
ast::variant_def_ids(cx.fcx.lcx.ccx.tcx.def_map.get(id)); ast::variant_def_ids(cx.fcx.lcx.ccx.tcx.def_map.get(pat.id));
auto llblobptr = llval; auto llblobptr = llval;
if (vec::len(ty::tag_variants(cx.fcx.lcx.ccx.tcx, vdef._0))!=1u) { if (vec::len(ty::tag_variants(cx.fcx.lcx.ccx.tcx, vdef._0))!=1u) {
auto lltagptr = cx.build.PointerCast auto lltagptr = cx.build.PointerCast
@ -4732,7 +4732,7 @@ fn trans_pat_binding(&@block_ctxt cx, &@ast::pat pat, ValueRef llval,
llblobptr = cx.build.GEP(lltagptr, [C_int(0), C_int(1)]); llblobptr = cx.build.GEP(lltagptr, [C_int(0), C_int(1)]);
} }
auto ty_param_substs = auto ty_param_substs =
ty::node_id_to_type_params(cx.fcx.lcx.ccx.tcx, id); ty::node_id_to_type_params(cx.fcx.lcx.ccx.tcx, pat.id);
auto this_cx = cx; auto this_cx = cx;
auto i = 0; auto i = 0;
for (@ast::pat subpat in subpats) { for (@ast::pat subpat in subpats) {

View file

@ -99,7 +99,6 @@ export mo_val;
export mo_alias; export mo_alias;
export mt; export mt;
export node_type_table; export node_type_table;
export pat_node_id;
export pat_ty; export pat_ty;
export cname; export cname;
export rename; export rename;
@ -1850,7 +1849,7 @@ fn block_ty(&ctxt cx, &ast::block b) -> t {
// Returns the type of a pattern as a monotype. Like @expr_ty, this function // Returns the type of a pattern as a monotype. Like @expr_ty, this function
// doesn't provide type parameter substitutions. // doesn't provide type parameter substitutions.
fn pat_ty(&ctxt cx, &@ast::pat pat) -> t { fn pat_ty(&ctxt cx, &@ast::pat pat) -> t {
ret node_id_to_monotype(cx, pat_node_id(pat)); ret node_id_to_monotype(cx, pat.id);
} }
@ -1888,16 +1887,6 @@ fn stmt_node_id(&@ast::stmt s) -> ast::node_id {
} }
} }
fn pat_node_id(&@ast::pat p) -> ast::node_id {
alt (p.node) {
case (ast::pat_wild(?id)) { ret id; }
case (ast::pat_bind(_, ?id)) { ret id; }
case (ast::pat_lit(_, ?id)) { ret id; }
case (ast::pat_tag(_, _, ?id)) { ret id; }
}
}
// Expression utilities // Expression utilities
fn field_num(&session::session sess, &span sp, &ast::ident id) -> uint { fn field_num(&session::session sess, &span sp, &ast::ident id) -> uint {
let uint accum = 0u; let uint accum = 0u;

View file

@ -1056,7 +1056,7 @@ mod writeback {
resolve_type_vars_for_node(fcx, b.span, b.node.id); resolve_type_vars_for_node(fcx, b.span, b.node.id);
} }
fn visit_pat_pre(@fn_ctxt fcx, &@ast::pat p) { fn visit_pat_pre(@fn_ctxt fcx, &@ast::pat p) {
resolve_type_vars_for_node(fcx, p.span, ty::pat_node_id(p)); resolve_type_vars_for_node(fcx, p.span, p.id);
} }
fn visit_local_pre(@fn_ctxt fcx, &@ast::local l) { fn visit_local_pre(@fn_ctxt fcx, &@ast::local l) {
auto var_id = lookup_local(fcx, l.span, l.node.id); auto var_id = lookup_local(fcx, l.span, l.node.id);
@ -1196,9 +1196,9 @@ fn gather_locals(&@crate_ctxt ccx, &ast::fn_decl decl, &ast::block body,
hashmap[ast::node_id, ast::ident] local_names, hashmap[ast::node_id, ast::ident] local_names,
@mutable int nvi, &@ast::pat p) { @mutable int nvi, &@ast::pat p) {
alt (p.node) { alt (p.node) {
case (ast::pat_bind(?ident, ?id)) { case (ast::pat_bind(?ident)) {
assign(ccx.tcx, vb, locals, local_names, nvi, assign(ccx.tcx, vb, locals, local_names, nvi,
id, ident, none[ty::t]); p.id, ident, none[ty::t]);
} }
case (_) {/* no-op */ } case (_) {/* no-op */ }
} }
@ -1249,24 +1249,23 @@ fn check_lit(@crate_ctxt ccx, &@ast::lit lit) -> ty::t {
// their types immediately. // their types immediately.
fn check_pat(&@fn_ctxt fcx, &@ast::pat pat, ty::t expected) { fn check_pat(&@fn_ctxt fcx, &@ast::pat pat, ty::t expected) {
alt (pat.node) { alt (pat.node) {
case (ast::pat_wild(?id)) { case (ast::pat_wild) {
write::ty_only_fixup(fcx, id, expected); write::ty_only_fixup(fcx, pat.id, expected);
} }
case (ast::pat_lit(?lt, ?id)) { case (ast::pat_lit(?lt)) {
auto typ = check_lit(fcx.ccx, lt); auto typ = check_lit(fcx.ccx, lt);
typ = demand::simple(fcx, pat.span, expected, typ); typ = demand::simple(fcx, pat.span, expected, typ);
write::ty_only_fixup(fcx, id, typ); write::ty_only_fixup(fcx, pat.id, typ);
} }
case (ast::pat_bind(?name, ?id)) { case (ast::pat_bind(?name)) {
auto vid = lookup_local(fcx, pat.span, id); auto vid = lookup_local(fcx, pat.span, pat.id);
auto typ = ty::mk_var(fcx.ccx.tcx, vid); auto typ = ty::mk_var(fcx.ccx.tcx, vid);
typ = demand::simple(fcx, pat.span, expected, typ); typ = demand::simple(fcx, pat.span, expected, typ);
write::ty_only_fixup(fcx, id, typ); write::ty_only_fixup(fcx, pat.id, typ);
} }
case (ast::pat_tag(?path, ?subpats, ?id)) { case (ast::pat_tag(?path, ?subpats)) {
// Typecheck the path. // Typecheck the path.
auto v_def = lookup_def(fcx, path.span, pat.id);
auto v_def = lookup_def(fcx, path.span, id);
auto v_def_ids = ast::variant_def_ids(v_def); auto v_def_ids = ast::variant_def_ids(v_def);
auto tag_tpt = ty::lookup_item_type(fcx.ccx.tcx, v_def_ids._0); auto tag_tpt = ty::lookup_item_type(fcx.ccx.tcx, v_def_ids._0);
auto path_tpot = instantiate_path(fcx, path, tag_tpt, pat.span); auto path_tpot = instantiate_path(fcx, path, tag_tpt, pat.span);
@ -1341,7 +1340,7 @@ fn check_pat(&@fn_ctxt fcx, &@ast::pat pat, ty::t expected) {
"" ""
} else { "s" })); } else { "s" }));
} }
write::ty_fixup(fcx, id, path_tpot); write::ty_fixup(fcx, pat.id, path_tpot);
} }
} }
} }

View file

@ -190,7 +190,7 @@ fn visit_constr[E](&@constr c, &E e, &vt[E] v) {
fn visit_pat[E](&@pat p, &E e, &vt[E] v) { fn visit_pat[E](&@pat p, &E e, &vt[E] v) {
alt (p.node) { alt (p.node) {
case (pat_tag(?path, ?children, _)) { case (pat_tag(?path, ?children)) {
for (@ty tp in path.node.types) { vt(v).visit_ty(tp, e, v); } for (@ty tp in path.node.types) { vt(v).visit_ty(tp, e, v); }
for (@pat child in children) { vt(v).visit_pat(child, e, v); } for (@pat child in children) { vt(v).visit_pat(child, e, v); }
} }

View file

@ -185,7 +185,7 @@ fn walk_ty(&ast_visitor v, @ast::ty t) {
fn walk_pat(&ast_visitor v, &@ast::pat p) { fn walk_pat(&ast_visitor v, &@ast::pat p) {
v.visit_pat_pre(p); v.visit_pat_pre(p);
alt (p.node) { alt (p.node) {
case (ast::pat_tag(?path, ?children, _)) { case (ast::pat_tag(?path, ?children)) {
for (@ast::ty tp in path.node.types) { walk_ty(v, tp); } for (@ast::ty tp in path.node.types) { walk_ty(v, tp); }
for (@ast::pat child in children) { walk_pat(v, child); } for (@ast::pat child in children) { walk_pat(v, child); }
} }

View file

@ -1016,10 +1016,10 @@ fn print_path(&ps s, &ast::path path) {
fn print_pat(&ps s, &@ast::pat pat) { fn print_pat(&ps s, &@ast::pat pat) {
maybe_print_comment(s, pat.span.lo); maybe_print_comment(s, pat.span.lo);
alt (pat.node) { alt (pat.node) {
case (ast::pat_wild(_)) { word(s.s, "_"); } case (ast::pat_wild) { word(s.s, "_"); }
case (ast::pat_bind(?id, _)) { word(s.s, "?" + id); } case (ast::pat_bind(?id)) { word(s.s, "?" + id); }
case (ast::pat_lit(?lit, _)) { print_literal(s, lit); } case (ast::pat_lit(?lit)) { print_literal(s, lit); }
case (ast::pat_tag(?path, ?args, _)) { case (ast::pat_tag(?path, ?args)) {
print_path(s, path); print_path(s, path);
if (vec::len(args) > 0u) { if (vec::len(args) > 0u) {
popen(s); popen(s);
@ -1033,7 +1033,7 @@ fn print_pat(&ps s, &@ast::pat pat) {
alt (s.mode) { alt (s.mode) {
case (mo_identified) { case (mo_identified) {
space(s.s); space(s.s);
synth_comment(s, int::to_str(ty::pat_node_id(pat), 10u)); synth_comment(s, int::to_str(pat.id, 10u));
} }
case (_) {/* no-op */ } case (_) {/* no-op */ }
} }