From 358a1aeec99ba6bf3c3f6bc5886e4dddf647a75a Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Thu, 12 May 2011 13:25:18 +0200 Subject: Keep resolve data in external hash table, rather than embedded defs One step closer to removing fold and having a single, immutable AST. Resolve still uses fold, because it has to detect and transform expr_field expressions. If we go through on our plan of moving to a different syntax for module dereferencing, the parser can spit out expr_field expressions, and resolve can move to walk. (I am truly sorry for the things I did in typestate_check.rs. I expect we'll want to change that to walk as well in the near future, at which point it should probably pass around a context record, which could hold the def_map.) --- src/comp/front/ast.rs | 18 ++++++++++++------ src/comp/front/eval.rs | 9 ++++++--- src/comp/front/extfmt.rs | 2 +- src/comp/front/parser.rs | 24 ++++++++++++++---------- 4 files changed, 33 insertions(+), 20 deletions(-) (limited to 'src/comp/front') diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs index af3ded19..84dae306 100644 --- a/src/comp/front/ast.rs +++ b/src/comp/front/ast.rs @@ -56,6 +56,14 @@ tag def { def_native_fn(def_id); } +fn variant_def_ids(&def d) -> tup(def_id, def_id) { + alt (d) { + case (def_variant(?tag_id, ?var_id)) { + ret tup(tag_id, var_id); + } + } +} + fn def_id_of_def(def d) -> def_id { alt (d) { case (def_fn(?id)) { ret id; } @@ -106,14 +114,12 @@ type block_ = rec(vec[@stmt] stmts, Option.t[@expr] expr, ann a); /* ann is only meaningful for the ts_ann field */ -type variant_def = tup(def_id /* tag */, def_id /* variant */); - type pat = spanned[pat_]; tag pat_ { pat_wild(ann); pat_bind(ident, def_id, ann); pat_lit(@lit, ann); - pat_tag(path, vec[@pat], Option.t[variant_def], ann); + pat_tag(path, vec[@pat], ann); } tag mutability { @@ -277,7 +283,7 @@ tag expr_ { expr_recv(@expr /* TODO: @expr|is_lval */, @expr, ann); expr_field(@expr, ident, ann); expr_index(@expr, @expr, ann); - expr_path(path, Option.t[def], ann); + expr_path(path, ann); expr_ext(path, vec[@expr], Option.t[str], @expr, ann); expr_fail(ann); expr_break(ann); @@ -333,7 +339,7 @@ tag ty_ { ty_rec(vec[ty_field]); ty_fn(proto, vec[ty_arg], @ty); ty_obj(vec[ty_method]); - ty_path(path, Option.t[def]); + ty_path(path, ann); ty_type; ty_constr(@ty, vec[@constr]); } @@ -463,7 +469,7 @@ fn is_constraint_arg(@expr e) -> bool { case (expr_lit(_,_)) { ret true; } - case (expr_path(_, Option.some[def](def_local(_)), _)) { + case (expr_path(_, _)) { ret true; } case (_) { diff --git a/src/comp/front/eval.rs b/src/comp/front/eval.rs index 1d708f17..c03e313d 100644 --- a/src/comp/front/eval.rs +++ b/src/comp/front/eval.rs @@ -34,7 +34,8 @@ type ctx = @rec(parser p, eval_mode mode, mutable vec[str] deps, session.session sess, - mutable uint chpos); + mutable uint chpos, + mutable uint next_ann); fn mk_env() -> env { let env e = vec(); @@ -113,7 +114,7 @@ fn eval_lit(ctx cx, span sp, @ast.lit lit) -> val { fn eval_expr(ctx cx, env e, @ast.expr x) -> val { alt (x.node) { - case (ast.expr_path(?pth, _, _)) { + case (ast.expr_path(?pth, _)) { if (Vec.len[ident](pth.node.idents) == 1u && Vec.len[@ast.ty](pth.node.types) == 0u) { ret lookup(cx.sess, e, x.span, pth.node.idents.(0)); @@ -383,12 +384,14 @@ fn eval_crate_directive(ctx cx, } auto start_id = cx.p.next_def_id(); - auto p0 = new_parser(cx.sess, e, start_id, full_path, cx.chpos); + auto p0 = new_parser(cx.sess, e, start_id, full_path, cx.chpos, + cx.next_ann); auto m0 = parse_mod_items(p0, token.EOF); auto next_id = p0.next_def_id(); // Thread defids and chpos through the parsers cx.p.set_def(next_id._1); cx.chpos = p0.get_chpos(); + cx.next_ann = p0.next_ann_num(); auto im = ast.item_mod(id, m0, next_id); auto i = @spanned(cdir.span.lo, cdir.span.hi, im); Vec.push[@ast.item](items, i); diff --git a/src/comp/front/extfmt.rs b/src/comp/front/extfmt.rs index 49c29a13..ab080024 100644 --- a/src/comp/front/extfmt.rs +++ b/src/comp/front/extfmt.rs @@ -121,7 +121,7 @@ fn pieces_to_expr(parser p, vec[piece] pieces, vec[@ast.expr] args) let vec[@ast.ty] types = vec(); auto path = rec(idents=idents, types=types); auto sp_path = rec(node=path, span=sp); - auto pathexpr = ast.expr_path(sp_path, none[ast.def], p.get_ann()); + auto pathexpr = ast.expr_path(sp_path, p.get_ann()); auto sp_pathexpr = @rec(node=pathexpr, span=sp); ret sp_pathexpr; } diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index e65532a8..fb43188a 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -43,12 +43,13 @@ state type parser = fn get_filemap() -> codemap.filemap; fn get_chpos() -> uint; fn get_ann() -> ast.ann; + fn next_ann_num() -> uint; }; fn new_parser(session.session sess, eval.env env, ast.def_id initial_def, - str path, uint pos) -> parser { + str path, uint pos, uint next_ann) -> parser { state obj stdio_parser(session.session sess, eval.env env, file_type ftype, @@ -134,6 +135,9 @@ fn new_parser(session.session sess, next_ann_var += 1u; ret rv; } + fn next_ann_num() -> uint { + ret next_ann_var; + } } auto ftype = SOURCE_FILE; if (Str.ends_with(path, ".rc")) { @@ -148,7 +152,7 @@ fn new_parser(session.session sess, auto npos = rdr.get_chpos(); ret stdio_parser(sess, env, ftype, lexer.next_token(rdr), npos, npos, initial_def._1, UNRESTRICTED, initial_def._0, - rdr, prec_table(), 0u); + rdr, prec_table(), next_ann); } fn unexpected(parser p, token.token t) { @@ -474,7 +478,7 @@ fn parse_ty(parser p) -> @ast.ty { case (token.IDENT(_)) { auto path = parse_path(p, GREEDY); - t = ast.ty_path(path, none[ast.def]); + t = ast.ty_path(path, p.get_ann()); hi = path.span.hi; } @@ -693,7 +697,7 @@ fn parse_bottom_expr(parser p) -> @ast.expr { case (token.IDENT(_)) { auto pth = parse_path(p, MINIMAL); hi = pth.span.hi; - ex = ast.expr_path(pth, none[ast.def], p.get_ann()); + ex = ast.expr_path(pth, p.get_ann()); } case (token.LPAREN) { @@ -985,7 +989,7 @@ fn extend_expr_by_ident(parser p, uint lo, uint hi, @ast.expr e, ast.ident i) -> @ast.expr { auto e_ = e.node; alt (e.node) { - case (ast.expr_path(?pth, ?def, ?ann)) { + case (ast.expr_path(?pth, ?ann)) { if (Vec.len[@ast.ty](pth.node.types) == 0u) { auto idents_ = pth.node.idents; idents_ += vec(i); @@ -993,7 +997,7 @@ fn extend_expr_by_ident(parser p, uint lo, uint hi, auto pth_ = spanned(pth.span.lo, tys.span.hi, rec(idents=idents_, types=tys.node)); - e_ = ast.expr_path(pth_, def, ann); + e_ = ast.expr_path(pth_, ann); ret @spanned(pth_.span.lo, pth_.span.hi, e_); } else { e_ = ast.expr_field(e, i, ann); @@ -1525,8 +1529,7 @@ fn parse_pat(parser p) -> @ast.pat { case (_) { args = vec(); } } - pat = ast.pat_tag(tag_path, args, none[ast.variant_def], - p.get_ann()); + pat = ast.pat_tag(tag_path, args, p.get_ann()); } case (_) { auto lit = parse_lit(p); @@ -1666,7 +1669,7 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool { case (ast.expr_recv(_,_,_)) { ret true; } case (ast.expr_field(_,_,_)) { ret true; } case (ast.expr_index(_,_,_)) { ret true; } - case (ast.expr_path(_,_,_)) { ret true; } + case (ast.expr_path(_,_)) { ret true; } case (ast.expr_fail(_)) { ret true; } case (ast.expr_break(_)) { ret true; } case (ast.expr_cont(_)) { ret true; } @@ -2496,7 +2499,8 @@ fn parse_crate_from_crate_file(parser p) -> @ast.crate { mode=eval.mode_parse, mutable deps = deps, sess=p.get_session(), - mutable chpos=p.get_chpos()); + mutable chpos=p.get_chpos(), + mutable next_ann=p.next_ann_num()); auto m = eval.eval_crate_directives_to_mod(cx, p.get_env(), cdirs, prefix); auto hi = p.get_hi_pos(); -- cgit v1.2.3