diff options
| author | Brian Anderson <banderson@mozilla.com> | 2011-08-21 21:44:41 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2011-08-22 10:44:04 -0700 |
| commit | 152cbaade7bc2ec36546554eb2d2090a9833bfff (patch) | |
| tree | d6bb39fcdcce9f67cc97f97a6259e85cbf315650 /src | |
| parent | a3affaa20fcd718080951d5be2da3f578c960664 (diff) | |
| download | rust-152cbaade7bc2ec36546554eb2d2090a9833bfff.tar.gz rust-152cbaade7bc2ec36546554eb2d2090a9833bfff.zip | |
Move functions from syntax::ast to syntax::ast_util
This leaves syntax::ast just defining the AST, which strikes me as somewhat nicer
Diffstat (limited to 'src')
33 files changed, 358 insertions, 314 deletions
diff --git a/src/comp/front/attr.rs b/src/comp/front/attr.rs index 0f3eb354d9e..e199d10fbd1 100644 --- a/src/comp/front/attr.rs +++ b/src/comp/front/attr.rs @@ -5,6 +5,7 @@ import std::str; import std::map; import std::option; import syntax::ast; +import syntax::ast_util; import util::common; import driver::session; @@ -189,7 +190,7 @@ fn require_unique_names(sess: &session::session, metas: &[@ast::meta_item]) { } fn span<T>(item: &T) -> ast::spanned<T> { - ret {node: item, span: ast::mk_sp(0u, 0u)}; + ret {node: item, span: ast_util::dummy_sp()}; } fn mk_name_value_item_str(name: ast::ident, value: str) -> @ast::meta_item { diff --git a/src/comp/front/test.rs b/src/comp/front/test.rs index 93ddaa54559..d9cda6e601a 100644 --- a/src/comp/front/test.rs +++ b/src/comp/front/test.rs @@ -3,6 +3,8 @@ import std::option; import std::vec; import syntax::ast; +import syntax::ast_util; +import syntax::ast_util::dummy_sp; import syntax::fold; import syntax::print::pprust; import front::attr; @@ -88,7 +90,7 @@ fn fold_item(cx: &test_ctxt, i: &@ast::item, fld: fold::ast_fold) -> @ast::item { cx.path += [i.ident]; - log #fmt["current path: %s", ast::path_name_i(cx.path)]; + log #fmt["current path: %s", ast_util::path_name_i(cx.path)]; if is_test_fn(i) { log "this is a test function"; @@ -161,7 +163,7 @@ fn mk_test_module(cx: &test_ctxt) -> @ast::item { attrs: [], id: cx.next_node_id(), node: item_, - span: ast::dummy_sp()}; + span: dummy_sp()}; log #fmt["Synthetic test module:\n%s\n", pprust::item_to_str(@item)]; @@ -169,7 +171,7 @@ fn mk_test_module(cx: &test_ctxt) -> @ast::item { } fn nospan<T>(t: &T) -> ast::spanned<T> { - ret {node: t, span: ast::dummy_sp()}; + ret {node: t, span: dummy_sp()}; } fn mk_tests(cx: &test_ctxt) -> @ast::item { @@ -199,7 +201,7 @@ fn mk_tests(cx: &test_ctxt) -> @ast::item { attrs: [], id: cx.next_node_id(), node: item_, - span: ast::dummy_sp()}; + span: dummy_sp()}; ret @item; } @@ -237,20 +239,20 @@ fn mk_test_desc_vec(cx: &test_ctxt) -> @ast::expr { ret @{id: cx.next_node_id(), node: ast::expr_vec(descs, ast::imm), - span: ast::dummy_sp()}; + span: dummy_sp()}; } fn mk_test_desc_rec(cx: &test_ctxt, test: test) -> @ast::expr { let path = test.path; - log #fmt["encoding %s", ast::path_name_i(path)]; + log #fmt["encoding %s", ast_util::path_name_i(path)]; let name_lit: ast::lit = - nospan(ast::lit_str(ast::path_name_i(path), ast::sk_rc)); + nospan(ast::lit_str(ast_util::path_name_i(path), ast::sk_rc)); let name_expr: ast::expr = {id: cx.next_node_id(), node: ast::expr_lit(@name_lit), - span: ast::dummy_sp()}; + span: dummy_sp()}; let name_field: ast::field = nospan({mut: ast::imm, ident: "name", expr: @name_expr}); @@ -260,7 +262,7 @@ fn mk_test_desc_rec(cx: &test_ctxt, test: test) -> @ast::expr { let fn_expr: ast::expr = {id: cx.next_node_id(), node: ast::expr_path(fn_path), - span: ast::dummy_sp()}; + span: dummy_sp()}; let fn_field: ast::field = nospan({mut: ast::imm, ident: "fn", expr: @fn_expr}); @@ -270,7 +272,7 @@ fn mk_test_desc_rec(cx: &test_ctxt, test: test) -> @ast::expr { let ignore_expr: ast::expr = {id: cx.next_node_id(), node: ast::expr_lit(@ignore_lit), - span: ast::dummy_sp()}; + span: dummy_sp()}; let ignore_field: ast::field = nospan({mut: ast::imm, ident: "ignore", expr: @ignore_expr}); @@ -278,7 +280,7 @@ fn mk_test_desc_rec(cx: &test_ctxt, test: test) -> @ast::expr { let desc_rec_: ast::expr_ = ast::expr_rec([name_field, fn_field, ignore_field], option::none); let desc_rec: ast::expr = - {id: cx.next_node_id(), node: desc_rec_, span: ast::dummy_sp()}; + {id: cx.next_node_id(), node: desc_rec_, span: dummy_sp()}; ret @desc_rec; } @@ -307,7 +309,7 @@ fn mk_main(cx: &test_ctxt) -> @ast::item { {stmts: [], expr: option::some(test_main_call_expr), id: cx.next_node_id()}; - let body = {node: body_, span: ast::dummy_sp()}; + let body = {node: body_, span: dummy_sp()}; let fn_ = {decl: decl, proto: proto, body: body}; @@ -317,7 +319,7 @@ fn mk_main(cx: &test_ctxt) -> @ast::item { attrs: [], id: cx.next_node_id(), node: item_, - span: ast::dummy_sp()}; + span: dummy_sp()}; ret @item; } @@ -330,7 +332,7 @@ fn mk_test_main_call(cx: &test_ctxt) -> @ast::expr { let args_path_expr_: ast::expr_ = ast::expr_path(args_path); let args_path_expr: ast::expr = - {id: cx.next_node_id(), node: args_path_expr_, span: ast::dummy_sp()}; + {id: cx.next_node_id(), node: args_path_expr_, span: dummy_sp()}; // Call __test::test to generate the vector of test_descs let test_path: ast::path = @@ -339,12 +341,12 @@ fn mk_test_main_call(cx: &test_ctxt) -> @ast::expr { let test_path_expr_: ast::expr_ = ast::expr_path(test_path); let test_path_expr: ast::expr = - {id: cx.next_node_id(), node: test_path_expr_, span: ast::dummy_sp()}; + {id: cx.next_node_id(), node: test_path_expr_, span: dummy_sp()}; let test_call_expr_: ast::expr_ = ast::expr_call(@test_path_expr, []); let test_call_expr: ast::expr = - {id: cx.next_node_id(), node: test_call_expr_, span: ast::dummy_sp()}; + {id: cx.next_node_id(), node: test_call_expr_, span: dummy_sp()}; // Call std::test::test_main let test_main_path: ast::path = @@ -357,7 +359,7 @@ fn mk_test_main_call(cx: &test_ctxt) -> @ast::expr { let test_main_path_expr: ast::expr = {id: cx.next_node_id(), node: test_main_path_expr_, - span: ast::dummy_sp()}; + span: dummy_sp()}; let test_main_call_expr_: ast::expr_ = ast::expr_call(@test_main_path_expr, @@ -366,7 +368,7 @@ fn mk_test_main_call(cx: &test_ctxt) -> @ast::expr { let test_main_call_expr: ast::expr = {id: cx.next_node_id(), node: test_main_call_expr_, - span: ast::dummy_sp()}; + span: dummy_sp()}; ret @test_main_call_expr; } diff --git a/src/comp/metadata/creader.rs b/src/comp/metadata/creader.rs index fd233ad4443..b2570e41148 100644 --- a/src/comp/metadata/creader.rs +++ b/src/comp/metadata/creader.rs @@ -2,6 +2,7 @@ import driver::session; import syntax::ast; +import syntax::ast_util; import lib::llvm::False; import lib::llvm::llvm; import lib::llvm::mk_object_file; @@ -262,7 +263,7 @@ fn resolve_crate_deps(e: env, cdata: &@[u8]) -> cstore::cnum_map { log "need to load it"; // This is a new one so we've got to load it // FIXME: Need better error reporting than just a bogus span - let fake_span = ast::dummy_sp(); + let fake_span = ast_util::dummy_sp(); let local_cnum = resolve_crate(e, cname, [], fake_span); cnum_map.insert(extrn_cnum, local_cnum); } diff --git a/src/comp/metadata/decoder.rs b/src/comp/metadata/decoder.rs index 972334ddfc7..fc2f3a00a9b 100644 --- a/src/comp/metadata/decoder.rs +++ b/src/comp/metadata/decoder.rs @@ -7,6 +7,7 @@ import std::str; import std::io; import std::map::hashmap; import syntax::ast; +import syntax::ast_util; import front::attr; import middle::ty; import common::*; @@ -335,7 +336,7 @@ fn get_attributes(md: &ebml::doc) -> [ast::attribute] { let meta_item = meta_items[0]; attrs += [{node: {style: ast::attr_outer, value: *meta_item}, - span: ast::dummy_sp()}]; + span: ast_util::dummy_sp()}]; } } option::none. { } diff --git a/src/comp/metadata/encoder.rs b/src/comp/metadata/encoder.rs index 02427fa0321..6ecd8c21f7a 100644 --- a/src/comp/metadata/encoder.rs +++ b/src/comp/metadata/encoder.rs @@ -10,6 +10,8 @@ import std::option::none; import std::ebml; import std::map; import syntax::ast::*; +import syntax::ast_util; +import syntax::ast_util::local_def; import common::*; import middle::trans_common::crate_ctxt; import middle::ty; @@ -71,7 +73,7 @@ fn encode_native_module_item_paths(ebml_w: &ebml::writer, nmod: &native_mod, fn encode_module_item_paths(ebml_w: &ebml::writer, module: &_mod, path: &[str], index: &mutable [entry<str>]) { for it: @item in module.items { - if !is_exported(it.ident, module) { cont; } + if !ast_util::is_exported(it.ident, module) { cont; } alt it.node { item_const(_, _) { add_to_index(ebml_w, path, index, it.ident); diff --git a/src/comp/metadata/tydecode.rs b/src/comp/metadata/tydecode.rs index 31034b7a6ae..f632e5ed28b 100644 --- a/src/comp/metadata/tydecode.rs +++ b/src/comp/metadata/tydecode.rs @@ -8,7 +8,8 @@ import std::option::none; import std::option::some; import syntax::ast; import syntax::ast::*; -import ast::respan; +import syntax::ast_util; +import syntax::ast_util::respan; import middle::ty; export parse_def_id; @@ -106,7 +107,7 @@ fn parse_path(st: @pstate, sd: str_def) -> ast::path { ':' { next(st); next(st); } c { if c == '(' { - ret respan(ast::dummy_sp(), + ret respan(ast_util::dummy_sp(), {global: false, idents: idents, types: []}); } else { idents += [parse_ident_(st, sd, is_last)]; } } @@ -152,7 +153,7 @@ fn parse_ty_constr_arg(st: @pstate, sd: str_def) -> fn parse_constr<@T>(st: @pstate, sd: str_def, pser: arg_parser<T>) -> @ty::constr_general<T> { - let sp = ast::dummy_sp(); // FIXME: use a real span + let sp = ast_util::dummy_sp(); // FIXME: use a real span let args: [@sp_constr_arg<T>] = []; let pth: path = parse_path(st, sd); let ignore: char = next(st) as char; diff --git a/src/comp/middle/alias.rs b/src/comp/middle/alias.rs index e29c06c9239..03fcb283621 100644 --- a/src/comp/middle/alias.rs +++ b/src/comp/middle/alias.rs @@ -1,5 +1,6 @@ import syntax::ast; +import syntax::ast_util; import ast::ident; import ast::fn_ident; import ast::node_id; @@ -195,7 +196,7 @@ fn check_call(cx: &ctx, f: &@ast::expr, args: &[@ast::expr], sc: &scope) -> if arg_t.mode == ty::mo_alias(true) { alt path_def(cx, arg) { some(def) { - let dnum = ast::def_id_of_def(def).node; + let dnum = ast_util::def_id_of_def(def).node; if def_is_local(def, true) { if is_immutable_alias(cx, sc, dnum) { cx.tcx.sess.span_err( @@ -302,7 +303,7 @@ fn check_tail_call(cx: &ctx, call: &@ast::expr) { alt args[i].node { ast::expr_path(_) { let def = cx.tcx.def_map.get(args[i].id); - let dnum = ast::def_id_of_def(def).node; + let dnum = ast_util::def_id_of_def(def).node; alt cx.local_map.find(dnum) { some(arg(ast::alias(mut))) { if mut_a && !mut { @@ -352,7 +353,7 @@ fn check_alt(cx: &ctx, input: &@ast::expr, arms: &[ast::arm], sc: &scope, } fn arm_defnums(arm: &ast::arm) -> [node_id] { - ret ast::pat_binding_ids(arm.pats[0]); + ret ast_util::pat_binding_ids(arm.pats[0]); } fn check_for_each(cx: &ctx, local: &@ast::local, call: &@ast::expr, @@ -361,7 +362,7 @@ fn check_for_each(cx: &ctx, local: &@ast::local, call: &@ast::expr, alt call.node { ast::expr_call(f, args) { let data = check_call(cx, f, args, sc); - let bindings = ast::pat_binding_ids(local.node.pat); + let bindings = ast_util::pat_binding_ids(local.node.pat); let new_sc = @{root_vars: data.root_vars, block_defnum: bindings[vec::len(bindings) - 1u], @@ -393,7 +394,7 @@ fn check_for(cx: &ctx, local: &@ast::local, seq: &@ast::expr, blk: &ast::blk, util::ppaux::ty_to_str(cx.tcx, seq_t)); } } - let bindings = ast::pat_binding_ids(local.node.pat); + let bindings = ast_util::pat_binding_ids(local.node.pat); let new_sc = @{root_vars: root_def, block_defnum: bindings[vec::len(bindings) - 1u], @@ -408,7 +409,7 @@ fn check_var(cx: &ctx, ex: &@ast::expr, p: &ast::path, id: ast::node_id, assign: bool, sc: &scope) { let def = cx.tcx.def_map.get(id); if !def_is_local(def, true) { ret; } - let my_defnum = ast::def_id_of_def(def).node; + let my_defnum = ast_util::def_id_of_def(def).node; let var_t = ty::expr_ty(cx.tcx, ex); for r: restrict in *sc { @@ -429,7 +430,7 @@ fn check_var(cx: &ctx, ex: &@ast::expr, p: &ast::path, id: ast::node_id, fn check_lval(cx: &@ctx, dest: &@ast::expr, sc: &scope, v: &vt<scope>) { alt dest.node { ast::expr_path(p) { - let dnum = ast::def_id_of_def(cx.tcx.def_map.get(dest.id)).node; + let dnum = ast_util::def_id_of_def(cx.tcx.def_map.get(dest.id)).node; cx.mut_map.insert(dnum, ()); if is_immutable_alias(*cx, sc, dnum) { cx.tcx.sess.span_err(dest.span, "assigning to immutable alias"); @@ -515,15 +516,17 @@ fn test_scope(cx: &ctx, sc: &scope, r: &restrict, p: &ast::path) { let msg = alt prob { overwritten(sp, wpt) { - {span: sp, msg: "overwriting " + ast::path_name(wpt)} + {span: sp, msg: "overwriting " + ast_util::path_name(wpt)} } val_taken(sp, vpt) { - {span: sp, msg: "taking the value of " + ast::path_name(vpt)} + {span: sp, + msg: "taking the value of " + ast_util::path_name(vpt)} } }; cx.tcx.sess.span_err(msg.span, msg.msg + " will invalidate alias " + - ast::path_name(p) + ", which is still used"); + ast_util::path_name(p) + + ", which is still used"); } } @@ -660,7 +663,7 @@ fn path_def(cx: &ctx, ex: &@ast::expr) -> option::t<ast::def> { fn path_def_id(cx: &ctx, ex: &@ast::expr) -> option::t<ast::def_id> { alt ex.node { ast::expr_path(_) { - ret some(ast::def_id_of_def(cx.tcx.def_map.get(ex.id))); + ret some(ast_util::def_id_of_def(cx.tcx.def_map.get(ex.id))); } _ { ret none; } } diff --git a/src/comp/middle/ast_map.rs b/src/comp/middle/ast_map.rs index afba7e20ced..356d2b46b8c 100644 --- a/src/comp/middle/ast_map.rs +++ b/src/comp/middle/ast_map.rs @@ -125,9 +125,11 @@ fn node_span(node: &ast_node) -> codemap::span { #[cfg(test)] mod test { + import syntax::ast_util; + #[test] fn test_node_span_item() { - let expected: codemap::span = mk_sp(20u, 30u); + let expected: codemap::span = ast_util::mk_sp(20u, 30u); let node = node_item(@{ident: "test", attrs: [], @@ -139,7 +141,7 @@ mod test { #[test] fn test_node_span_obj_ctor() { - let expected: codemap::span = mk_sp(20u, 30u); + let expected: codemap::span = ast_util::mk_sp(20u, 30u); let node = node_obj_ctor(@{ident: "test", attrs: [], @@ -151,7 +153,7 @@ mod test { #[test] fn test_node_span_native_item() { - let expected: codemap::span = mk_sp(20u, 30u); + let expected: codemap::span = ast_util::mk_sp(20u, 30u); let node = node_native_item(@{ident: "test", attrs: [], @@ -163,7 +165,7 @@ mod test { #[test] fn test_node_span_expr() { - let expected: codemap::span = mk_sp(20u, 30u); + let expected: codemap::span = ast_util::mk_sp(20u, 30u); let node = node_expr(@{id: 0, node: expr_break, span: expected}); assert (node_span(node) == expected); } diff --git a/src/comp/middle/check_alt.rs b/src/comp/middle/check_alt.rs index 9d99464c2b9..ce5e937cf69 100644 --- a/src/comp/middle/check_alt.rs +++ b/src/comp/middle/check_alt.rs @@ -1,4 +1,6 @@ import syntax::ast::*; +import syntax::ast_util::variant_def_ids; +import syntax::ast_util::dummy_sp; import syntax::visit; fn check_crate(tcx: &ty::ctxt, crate: &@crate) { diff --git a/src/comp/middle/freevars.rs b/src/comp/middle/freevars.rs index 8bef22f8ad8..e8c146c5a85 100644 --- a/src/comp/middle/freevars.rs +++ b/src/comp/middle/freevars.rs @@ -7,6 +7,7 @@ import std::option; import std::int; import std::option::*; import syntax::ast; +import syntax::ast_util; import syntax::visit; import driver::session; import middle::resolve; @@ -69,7 +70,7 @@ fn collect_freevars(def_map: &resolve::def_map, sess: &session::session, }; let walk_local = lambda (local: &@ast::local) { - for each b: @ast::pat in ast::pat_bindings(local.node.pat) { + for each b: @ast::pat in ast_util::pat_bindings(local.node.pat) { set_add(decls, b.id); } }; @@ -91,7 +92,7 @@ fn collect_freevars(def_map: &resolve::def_map, sess: &session::session, let defs = new_int_hash(); for ref_id_: ast::node_id in *refs { let ref_id = ref_id_; - let def_id = ast::def_id_of_def(def_map.get(ref_id)).node; + let def_id = ast_util::def_id_of_def(def_map.get(ref_id)).node; if !decls.contains_key(def_id) && !defs.contains_key(def_id) { canonical_refs += [ref_id]; set_add(defs, def_id); @@ -127,7 +128,7 @@ fn annotate_freevars(sess: &session::session, def_map: &resolve::def_map, lambda (v: &visit::vt<()>) { v.visit_block(body, (), v); }; - let bound = ast::pat_binding_ids(local.node.pat); + let bound = ast_util::pat_binding_ids(local.node.pat); let vars = collect_freevars(def_map, sess, start_walk, bound); freevars.insert(body.node.id, vars); } @@ -166,7 +167,7 @@ fn def_lookup(tcx: &ty::ctxt, f: ast::node_id, id: ast::node_id) -> alt tcx.def_map.find(id) { none. { ret none; } some(d) { - let did = ast::def_id_of_def(d); + let did = ast_util::def_id_of_def(d); if f != -1 && is_freevar_of(tcx, did.node, f) { ret some(ast::def_upvar(did, @d)); } else { ret some(d); } diff --git a/src/comp/middle/kind.rs b/src/comp/middle/kind.rs index f5e833a86d2..4ffdcc95ff3 100644 --- a/src/comp/middle/kind.rs +++ b/src/comp/middle/kind.rs @@ -72,6 +72,7 @@ import syntax::ast; +import syntax::ast_util; import syntax::visit; import std::vec; @@ -142,7 +143,8 @@ fn check_expr(tcx: &ty::ctxt, e: &@ast::expr) { // that all the types we're supplying as typarams conform to the // typaram kind constraints on that item. if vec::len(tpt.params) != 0u { - let callee_def = ast::def_id_of_def(tcx.def_map.get(callee.id)); + let callee_def = ast_util::def_id_of_def( + tcx.def_map.get(callee.id)); let item_tk = ty::lookup_item_type(tcx, callee_def); let i = 0; assert (vec::len(item_tk.kinds) == vec::len(tpt.params)); diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index 0785be75823..d3712f90da2 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -1,13 +1,14 @@ import syntax::ast; import syntax::ast::*; +import syntax::ast_util; import syntax::codemap; import ast::ident; import ast::fn_ident; import ast::def; import ast::def_id; import ast::node_id; -import ast::local_def; +import syntax::ast_util::local_def; import metadata::csearch; import metadata::cstore; @@ -16,7 +17,7 @@ import util::common::*; import std::map::new_int_hash; import std::map::new_str_hash; import syntax::codemap::span; -import syntax::ast::respan; +import syntax::ast_util::respan; import middle::ty::constr_table; import syntax::visit; import visit::vt; @@ -321,7 +322,8 @@ fn resolve_names(e: &@env, c: &@ast::crate) { } _ { e.sess.span_err(p.span, - "not a tag variant: " + ast::path_name(p)); + "not a tag variant: " + + ast_util::path_name(p)); } } } @@ -739,7 +741,7 @@ fn lookup_in_ty_params(name: &ident, ty_params: &[ast::ty_param]) -> fn lookup_in_pat(name: &ident, pat: &@ast::pat) -> option::t<def_id> { let found = none; - for each bound in ast::pat_bindings(pat) { + for each bound in ast_util::pat_bindings(pat) { let p_name = alt bound.node { ast::pat_bind(n) { n } }; if str::eq(p_name, name) { found = some(local_def(bound.id)); } } @@ -891,7 +893,7 @@ fn lookup_in_mod_strict(e: &env, sc: &scopes, m: def, sp: &span, name: &ident, fn lookup_in_mod(e: &env, m: &def, sp: &span, name: &ident, ns: namespace, dr: dir) -> option::t<def> { - let defid = ast::def_id_of_def(m); + let defid = ast_util::def_id_of_def(m); if defid.crate != ast::local_crate { // examining a module in an external crate @@ -946,7 +948,7 @@ fn lookup_in_local_native_mod(e: &env, node_id: node_id, sp: &span, fn lookup_in_local_mod(e: &env, node_id: node_id, sp: &span, id: &ident, ns: namespace, dr: dir) -> option::t<def> { let info = e.mod_map.get(node_id); - if dr == outside && !ast::is_exported(id, option::get(info.m)) { + if dr == outside && !ast_util::is_exported(id, option::get(info.m)) { // if we're in a native mod, then dr==inside, so info.m is some _mod ret none::<def>; // name is not visible @@ -1173,7 +1175,7 @@ fn ns_for_def(d: def) -> namespace { fn lookup_external(e: &env, cnum: int, ids: &[ident], ns: namespace) -> option::t<def> { for d: def in csearch::lookup_defs(e.sess.get_cstore(), cnum, ids) { - e.ext_map.insert(ast::def_id_of_def(d), ids); + e.ext_map.insert(ast_util::def_id_of_def(d), ids); if ns == ns_for_def(d) { ret some(d); } } ret none::<def>; @@ -1273,7 +1275,7 @@ fn check_item(e: &@env, i: &@ast::item, x: &(), v: &vt<()>) { } fn check_pat(ch: checker, p: &@ast::pat) { - for each p in ast::pat_bindings(p) { + for each p in ast_util::pat_bindings(p) { let ident = alt p.node { pat_bind(n) { n } }; add_name(ch, p.span, ident); } @@ -1321,7 +1323,7 @@ fn check_block(e: &@env, b: &ast::blk, x: &(), v: &vt<()>) { ast::decl_local(locs) { let local_values = checker(*e, "value"); for loc in locs { - for each p in ast::pat_bindings(loc.node.pat) { + for each p in ast_util::pat_bindings(loc.node.pat) { let ident = alt p.node { pat_bind(n) { n } }; add_name(local_values, p.span, ident); check_name(values, p.span, ident); diff --git a/src/comp/middle/shape.rs b/src/comp/middle/shape.rs index 6c10ee4f357..f3ada9b7ca4 100644 --- a/src/comp/middle/shape.rs +++ b/src/comp/middle/shape.rs @@ -21,7 +21,7 @@ import middle::ty; import middle::ty::field; import middle::ty::mt; import syntax::ast; -import syntax::ast::dummy_sp; +import syntax::ast_util::dummy_sp; import syntax::codemap::span; import syntax::util::interner; import util::common; diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 58edce732a2..8fd45fff56b 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -25,6 +25,7 @@ import std::fs; import std::time; import std::vec; import syntax::ast; +import syntax::ast_util; import driver::session; import middle::ty; import middle::freevars::*; @@ -3478,7 +3479,7 @@ fn trans_if(cx: &@block_ctxt, cond: &@ast::expr, thn: &ast::blk, some(elexpr) { alt elexpr.node { ast::expr_if(_, _, _) { - let elseif_blk = ast::block_from_expr(elexpr); + let elseif_blk = ast_util::block_from_expr(elexpr); trans_block(else_cx, elseif_blk, output) } ast::expr_block(blk) { trans_block(else_cx, blk, output) } @@ -3722,7 +3723,8 @@ fn load_environment(enclosing_cx: &@block_ctxt, fcx: &@fn_ctxt, envty: ty::t, bcx = upvarptr.bcx; let llupvarptr = upvarptr.val; if !copying { llupvarptr = bcx.build.Load(llupvarptr); } - let def_id = ast::def_id_of_def(bcx_tcx(bcx).def_map.get(upvar_id)); + let def_id = ast_util::def_id_of_def(bcx_tcx(bcx). + def_map.get(upvar_id)); fcx.llupvars.insert(def_id.node, llupvarptr); i += 1u; } @@ -4980,7 +4982,7 @@ fn trans_expr_out(cx: &@block_ctxt, e: &@ast::expr, output: out_method) -> output); } ast::expr_ternary(_, _, _) { - ret trans_expr_out(cx, ast::ternary_to_if(e), output); + ret trans_expr_out(cx, ast_util::ternary_to_if(e), output); } ast::expr_for(decl, seq, body) { ret trans_for(cx, decl, seq, body); } ast::expr_for_each(decl, seq, body) { @@ -5463,7 +5465,7 @@ fn build_return(bcx: &@block_ctxt) { bcx.build.Br(bcx_fcx(bcx).llreturn); } fn trans_be(cx: &@block_ctxt, e: &@ast::expr) -> result { // FIXME: This should be a typestate precondition - assert (ast::is_call_expr(e)); + assert (ast_util::is_call_expr(e)); // FIXME: Turn this into a real tail call once // calling convention issues are settled @@ -6208,8 +6210,8 @@ fn trans_tag_variant(cx: @local_ctxt, tag_id: ast::node_id, i = 0u; for va: ast::variant_arg in variant.node.args { let rslt = - GEP_tag(bcx, llblobptr, ast::local_def(tag_id), - ast::local_def(variant.node.id), ty_param_substs, + GEP_tag(bcx, llblobptr, ast_util::local_def(tag_id), + ast_util::local_def(variant.node.id), ty_param_substs, i as int); bcx = rslt.bcx; let lldestptr = rslt.val; diff --git a/src/comp/middle/trans_alt.rs b/src/comp/middle/trans_alt.rs index 5d4cc64c079..56948101f86 100644 --- a/src/comp/middle/trans_alt.rs +++ b/src/comp/middle/trans_alt.rs @@ -14,7 +14,8 @@ import trans::new_scope_block_ctxt; import trans::load_if_immediate; import ty::pat_ty; import syntax::ast; -import syntax::ast::dummy_sp; +import syntax::ast_util; +import syntax::ast_util::dummy_sp; import syntax::ast::def_id; import syntax::codemap::span; import util::common::lit_eq; @@ -44,7 +45,7 @@ fn trans_opt(bcx: &@block_ctxt, o: &opt) -> result { } fn variant_opt(ccx: &@crate_ctxt, pat_id: ast::node_id) -> opt { - let vdef = ast::variant_def_ids(ccx.tcx.def_map.get(pat_id)); + let vdef = ast_util::variant_def_ids(ccx.tcx.def_map.get(pat_id)); let variants = ty::tag_variants(ccx.tcx, vdef.tg); let i = 0u; for v: ty::variant_info in variants { @@ -500,7 +501,7 @@ fn trans_alt(cx: &@block_ctxt, expr: &@ast::expr, arms: &[ast::arm], for a: ast::arm in arms { let body = new_scope_block_ctxt(cx, "case_body"); - let id_map = ast::pat_id_map(a.pats[0]); + let id_map = ast_util::pat_id_map(a.pats[0]); bodies += [body]; for p: @ast::pat in a.pats { match += [@{pats: [p], @@ -532,7 +533,8 @@ fn trans_alt(cx: &@block_ctxt, expr: &@ast::expr, arms: &[ast::arm], let arm_results = []; for a: ast::arm in arms { let body_cx = bodies[i]; - if make_phi_bindings(body_cx, exit_map, ast::pat_id_map(a.pats[0])) { + if make_phi_bindings(body_cx, exit_map, + ast_util::pat_id_map(a.pats[0])) { let block_res = trans::trans_block(body_cx, a.body, output); arm_results += [block_res]; } else { // Unreachable @@ -562,7 +564,7 @@ fn bind_irrefutable_pat(bcx: @block_ctxt, pat: &@ast::pat, val: ValueRef, } ast::pat_tag(_, sub) { if vec::len(sub) == 0u { ret bcx; } - let vdefs = ast::variant_def_ids(ccx.tcx.def_map.get(pat.id)); + let vdefs = ast_util::variant_def_ids(ccx.tcx.def_map.get(pat.id)); let args = extract_variant_args(bcx, pat.id, vdefs, val); let i = 0; for argval: ValueRef in args.vals { diff --git a/src/comp/middle/trans_objects.rs b/src/comp/middle/trans_objects.rs index 643cba56d7e..c5a23309f75 100644 --- a/src/comp/middle/trans_objects.rs +++ b/src/comp/middle/trans_objects.rs @@ -15,6 +15,7 @@ import back::abi; import back::link::mangle_internal_name_by_path; import back::link::mangle_internal_name_by_path_and_seq; import syntax::ast; +import syntax::ast_util; import syntax::codemap::span; import trans_common::*; @@ -231,7 +232,7 @@ fn trans_anon_obj(bcx: @block_ctxt, sp: &span, anon_obj: &ast::anon_obj, // methods, not inner ones. let wrapper_obj: ast::_obj = {fields: - std::vec::map(ast::obj_field_from_anon_obj_field, + std::vec::map(ast_util::obj_field_from_anon_obj_field, additional_fields), methods: anon_obj.methods}; diff --git a/src/comp/middle/tstate/annotate.rs b/src/comp/middle/tstate/annotate.rs index 6aa35018f2f..bb288cb18c7 100644 --- a/src/comp/middle/tstate/annotate.rs +++ b/src/comp/middle/tstate/annotate.rs @@ -5,6 +5,7 @@ import std::option::none; import std::int; import std::uint; import syntax::ast::*; +import syntax::ast_util::pat_binding_ids; import syntax::visit; import syntax::codemap::span; import std::map::new_str_hash; diff --git a/src/comp/middle/tstate/auxiliary.rs b/src/comp/middle/tstate/auxiliary.rs index 8f9a56b5e20..6aab9e5aed4 100644 --- a/src/comp/middle/tstate/auxiliary.rs +++ b/src/comp/middle/tstate/auxiliary.rs @@ -6,6 +6,7 @@ import std::option::*; import std::int; import std::uint; import syntax::ast::*; +import syntax::ast_util::*; import syntax::codemap::span; import syntax::visit; import util::common; diff --git a/src/comp/middle/tstate/ck.rs b/src/comp/middle/tstate/ck.rs index ca28e06c9f8..070417e6401 100644 --- a/src/comp/middle/tstate/ck.rs +++ b/src/comp/middle/tstate/ck.rs @@ -11,7 +11,7 @@ import ast::ident; import ast::fn_ident; import ast::node_id; import ast::def_id; -import ast::local_def; +import syntax::ast_util::local_def; import ast::ty_param; import ast::crate; import ast::return; diff --git a/src/comp/middle/tstate/collect_locals.rs b/src/comp/middle/tstate/collect_locals.rs index 277704998ce..0065fcaf41d 100644 --- a/src/comp/middle/tstate/collect_locals.rs +++ b/src/comp/middle/tstate/collect_locals.rs @@ -2,6 +2,7 @@ import std::uint; import std::int; import std::vec; import syntax::ast::*; +import syntax::ast_util::*; import util::ppaux::fn_ident_to_string; import std::option::*; import syntax::visit; @@ -9,7 +10,7 @@ import aux::*; import std::map::new_int_hash; import util::common::new_def_hash; import syntax::codemap::span; -import syntax::ast::respan; +import syntax::ast_util::respan; type ctxt = {cs: @mutable [sp_constr], tcx: ty::ctxt}; diff --git a/src/comp/middle/tstate/pre_post_conditions.rs b/src/comp/middle/tstate/pre_post_conditions.rs index 0d04d5656e7..c1d81d4d277 100644 --- a/src/comp/middle/tstate/pre_post_conditions.rs +++ b/src/comp/middle/tstate/pre_post_conditions.rs @@ -17,6 +17,7 @@ import bitvectors::relax_precond_block; import bitvectors::gen; import tritv::*; import syntax::ast::*; +import syntax::ast_util::*; import syntax::visit; import std::map::new_int_hash; import util::common::new_def_hash; diff --git a/src/comp/middle/tstate/states.rs b/src/comp/middle/tstate/states.rs index 3d8d8767ceb..75f6172711c 100644 --- a/src/comp/middle/tstate/states.rs +++ b/src/comp/middle/tstate/states.rs @@ -15,6 +15,7 @@ import tritv::ttrue; import bitvectors::*; import syntax::ast::*; +import syntax::ast_util::*; import middle::ty::expr_ty; import middle::ty::type_is_nil; import middle::ty::type_is_bot; diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs index 7a98b5a8cab..5d448825b89 100644 --- a/src/comp/middle/ty.rs +++ b/src/comp/middle/ty.rs @@ -13,6 +13,7 @@ import std::smallintmap; import driver::session; import syntax::ast; import syntax::ast::*; +import syntax::ast_util; import syntax::codemap::span; import metadata::csearch; import util::common::*; @@ -416,7 +417,8 @@ fn mk_ctxt(s: session::session, dm: resolve::def_map, has_pointer_cache: map::mk_hashmap(ty::hash_ty, ty::eq_ty), kind_cache: map::mk_hashmap(ty::hash_ty, ty::eq_ty), owns_heap_mem_cache: map::mk_hashmap(ty::hash_ty, ty::eq_ty), - ast_ty_to_ty_cache: map::mk_hashmap(ast::hash_ty, ast::eq_ty)}; + ast_ty_to_ty_cache: map::mk_hashmap(ast_util::hash_ty, + ast_util::eq_ty)}; populate_type_store(cx); ret cx; } @@ -2716,7 +2718,7 @@ fn tag_variants(cx: &ctxt, id: &ast::def_id) -> [variant_info] { result += [{args: arg_tys, ctor_ty: ctor_ty, - id: ast::local_def(did)}]; + id: ast_util::local_def(did)}]; } ret result; } @@ -2873,8 +2875,10 @@ fn ast_constr_to_constr<T>(tcx: ty::ctxt, c: &@ast::constr_general<T>) -> @ty::constr_general<T> { alt tcx.def_map.find(c.node.id) { some(ast::def_fn(pred_id, ast::pure_fn.)) { - ret @respan(c.span, - {path: c.node.path, args: c.node.args, id: pred_id}); + ret @ast_util::respan(c.span, + {path: c.node.path, + args: c.node.args, + id: pred_id}); } _ { tcx.sess.span_fatal(c.span, diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index 9f03a31fbb8..859e169b44f 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -1,7 +1,8 @@ import syntax::ast; +import syntax::ast_util; import ast::mutability; -import ast::local_def; -import ast::respan; +import syntax::ast_util::local_def; +import syntax::ast_util::respan; import ast::spanned; import syntax::visit; import metadata::csearch; @@ -708,14 +709,14 @@ mod collect { let convert = bind ast_ty_to_ty(cx.tcx, get, _); let f = bind ty_of_arg(cx, _); ret ty_of_native_fn_decl(cx, convert, f, fn_decl, abi, params, - ast::local_def(it.id)); + ast_util::local_def(it.id)); } ast::native_item_ty. { alt cx.tcx.tcache.find(local_def(it.id)) { some(tpt) { ret tpt; } none. { } } - let t = ty::mk_native(cx.tcx, ast::local_def(it.id)); + let t = ty::mk_native(cx.tcx, ast_util::local_def(it.id)); let tpt = {kinds: no_kinds, ty: t}; cx.tcx.tcache.insert(local_def(it.id), tpt); ret tpt; @@ -1329,7 +1330,7 @@ fn check_pat(fcx: &@fn_ctxt, map: &ast::pat_id_map, pat: &@ast::pat, ast::pat_tag(path, subpats) { // Typecheck the path. let v_def = lookup_def(fcx, path.span, pat.id); - let v_def_ids = ast::variant_def_ids(v_def); + let v_def_ids = ast_util::variant_def_ids(v_def); let tag_tpt = ty::lookup_item_type(fcx.ccx.tcx, v_def_ids.tg); let path_tpot = instantiate_path(fcx, path, tag_tpt, pat.span); @@ -1727,7 +1728,7 @@ fn check_expr_with_unifier(fcx: &@fn_ctxt, expr: &@ast::expr, unify: &unifier, } } for operand: @ast::expr in operands { - if !ast::is_constraint_arg(operand) { + if !ast_util::is_constraint_arg(operand) { let s = "Constraint args must be \ slot variables or literals"; @@ -1776,7 +1777,7 @@ fn check_expr_with_unifier(fcx: &@fn_ctxt, expr: &@ast::expr, unify: &unifier, binop: ast::binop) { let resolved_t = resolve_type_vars_if_possible(fcx, ty); if !ty::is_binopable(fcx.ccx.tcx, resolved_t, binop) { - let binopstr = ast::binop_to_str(binop); + let binopstr = ast_util::binop_to_str(binop); let t_str = ty_to_str(fcx.ccx.tcx, resolved_t); let errmsg = "binary operation " + binopstr + @@ -1798,7 +1799,7 @@ fn check_expr_with_unifier(fcx: &@fn_ctxt, expr: &@ast::expr, unify: &unifier, bot = check_expr_with(fcx, lhs, lhs_t); let rhs_bot = check_expr_with(fcx, rhs, lhs_t); - if !ast::lazy_binop(binop) { bot |= rhs_bot; } + if !ast_util::lazy_binop(binop) { bot |= rhs_bot; } check_binop_type_compat(fcx, expr.span, lhs_t, binop); @@ -1928,7 +1929,7 @@ fn check_expr_with_unifier(fcx: &@fn_ctxt, expr: &@ast::expr, unify: &unifier, } ast::expr_be(e) { // FIXME: prove instead of assert - assert (ast::is_call_expr(e)); + assert (ast_util::is_call_expr(e)); check_expr_with(fcx, e, fcx.ret_ty); bot = true; write::nil_ty(tcx, id); @@ -1947,7 +1948,7 @@ fn check_expr_with_unifier(fcx: &@fn_ctxt, expr: &@ast::expr, unify: &unifier, check_then_else(fcx, thn, elsopt, id, expr.span); } ast::expr_ternary(_, _, _) { - bot = check_expr(fcx, ast::ternary_to_if(expr)); + bot = check_expr(fcx, ast_util::ternary_to_if(expr)); } ast::expr_assert(e) { bot = check_expr_with(fcx, e, ty::mk_bool(tcx)); @@ -2029,7 +2030,7 @@ fn check_expr_with_unifier(fcx: &@fn_ctxt, expr: &@ast::expr, unify: &unifier, // bindings. let pattern_ty = ty::expr_ty(tcx, expr); for arm: ast::arm in arms { - let id_map = ast::pat_id_map(arm.pats[0]); + let id_map = ast_util::pat_id_map(arm.pats[0]); for p: @ast::pat in arm.pats { check_pat(fcx, id_map, p, pattern_ty); } @@ -2392,7 +2393,7 @@ fn check_expr_with_unifier(fcx: &@fn_ctxt, expr: &@ast::expr, unify: &unifier, } fcx.ccx.obj_infos += - [anon_obj(vec::map(ast::obj_field_from_anon_obj_field, + [anon_obj(vec::map(ast_util::obj_field_from_anon_obj_field, fields), inner_obj_sty)]; // Whenever an outer method overrides an inner, we need to remove @@ -2499,7 +2500,7 @@ fn check_decl_local(fcx: &@fn_ctxt, local: &@ast::local) -> bool { } _ {/* fall through */ } } - let id_map = ast::pat_id_map(local.node.pat); + let id_map = ast_util::pat_id_map(local.node.pat); check_pat(fcx, id_map, local.node.pat, t); } } diff --git a/src/comp/rustc.rc b/src/comp/rustc.rc index 555f8d45655..1cba8c469a8 100644 --- a/src/comp/rustc.rc +++ b/src/comp/rustc.rc @@ -45,6 +45,7 @@ mod middle { mod syntax { mod ast; + mod ast_util; mod fold; mod visit; diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs index 5c3ec015e5a..ec38c349ce3 100644 --- a/src/comp/syntax/ast.rs +++ b/src/comp/syntax/ast.rs @@ -6,15 +6,6 @@ import codemap::span; import codemap::filename; type spanned<T> = {node: T, span: span}; -fn respan<T>(sp: &span, t: &T) -> spanned<T> { ret {node: t, span: sp}; } - -/* assuming that we're not in macro expansion */ -fn mk_sp(lo: uint, hi: uint) -> span { - ret {lo: lo, hi: hi, expanded_from: codemap::os_none}; -} - -// make this a const, once the compiler supports it -fn dummy_sp() -> span { ret mk_sp(0u, 0u); } type ident = str; // Functions may or may not have names. @@ -27,16 +18,11 @@ type path_ = {global: bool, idents: [ident], types: [@ty]}; type path = spanned<path_>; -fn path_name(p: &path) -> str { path_name_i(p.node.idents) } - -fn path_name_i(idents: &[ident]) -> str { str::connect(idents, "::") } - type crate_num = int; type node_id = int; type def_id = {crate: crate_num, node: node_id}; const local_crate: crate_num = 0; -fn local_def(id: node_id) -> def_id { ret {crate: local_crate, node: id}; } type ty_param = {ident: ident, kind: kind}; @@ -64,30 +50,6 @@ tag def { def_upvar(def_id, @def); } -fn variant_def_ids(d: &def) -> {tg: def_id, var: def_id} { - alt d { def_variant(tag_id, var_id) { ret {tg: tag_id, var: var_id}; } } -} - -fn def_id_of_def(d: def) -> def_id { - alt d { - def_fn(id, _) { ret id; } - def_obj_field(id) { ret id; } - def_mod(id) { ret id; } - def_native_mod(id) { ret id; } - def_const(id) { ret id; } - def_arg(id) { ret id; } - def_local(id) { ret id; } - def_variant(_, id) { ret id; } - def_ty(id) { ret id; } - def_ty_arg(_, _) { fail; } - def_binding(id) { ret id; } - def_use(id) { ret id; } - def_native_ty(id) { ret id; } - def_native_fn(id) { ret id; } - def_upvar(id, _) { ret id; } - } -} - // The set of meta_items that define the compilation environment of the crate, // used to drive conditional compilation type crate_cfg = [@meta_item]; @@ -138,41 +100,6 @@ tag pat_ { type pat_id_map = std::map::hashmap<str, ast::node_id>; -// This is used because same-named variables in alternative patterns need to -// use the node_id of their namesake in the first pattern. -fn pat_id_map(pat: &@pat) -> pat_id_map { - let map = std::map::new_str_hash::<node_id>(); - for each bound in pat_bindings(pat) { - let name = alt bound.node { pat_bind(n) { n } }; - map.insert(name, bound.id); - } - ret map; -} - -// FIXME: could return a constrained type -iter pat_bindings(pat: &@pat) -> @pat { - alt pat.node { - pat_bind(_) { put pat; } - pat_tag(_, sub) { - for p in sub { for each b in pat_bindings(p) { put b; } } - } - pat_rec(fields, _) { - for f in fields { for each b in pat_bindings(f.pat) { put b; } } - } - pat_tup(elts) { - for elt in elts { for each b in pat_bindings(elt) { put b; } } - } - pat_box(sub) { for each b in pat_bindings(sub) { put b; } } - pat_wild. | pat_lit(_) { } - } -} - -fn pat_binding_ids(pat: &@pat) -> [node_id] { - let found = []; - for each b in pat_bindings(pat) { found += [b.id]; } - ret found; -} - tag mutability { mut; imm; maybe_mut; } tag kind { kind_pinned; kind_shared; kind_unique; } @@ -203,45 +130,8 @@ tag binop { gt; } -fn binop_to_str(op: binop) -> str { - alt op { - add. { ret "+"; } - sub. { ret "-"; } - mul. { ret "*"; } - div. { ret "/"; } - rem. { ret "%"; } - and. { ret "&&"; } - or. { ret "||"; } - bitxor. { ret "^"; } - bitand. { ret "&"; } - bitor. { ret "|"; } - lsl. { ret "<<"; } - lsr. { ret ">>"; } - asr. { ret ">>>"; } - eq. { ret "=="; } - lt. { ret "<"; } - le. { ret "<="; } - ne. { ret "!="; } - ge. { ret ">="; } - gt. { ret ">"; } - } -} - -pred lazy_binop(b: binop) -> bool { - alt b { and. { true } or. { true } _ { false } } -} - tag unop { box(mutability); deref; not; neg; } -fn unop_to_str(op: unop) -> str { - alt op { - box(mt) { if mt == mut { ret "@mutable "; } ret "@"; } - deref. { ret "*"; } - not. { ret "!"; } - neg. { ret "-"; } - } -} - tag mode { val; alias(bool); move; } type stmt = spanned<stmt_>; @@ -358,11 +248,6 @@ tag lit_ { lit_bool(bool); } -fn is_path(e: &@expr) -> bool { - ret alt e.node { expr_path(_) { true } _ { false } }; -} - - // NB: If you change this, you'll probably want to change the corresponding // type structure in middle/ty.rs as well. type mt = {ty: @ty, mut: mutability}; @@ -398,21 +283,6 @@ tag ty_mach { ty_f64; } -fn ty_mach_to_str(tm: ty_mach) -> str { - alt tm { - ty_u8. { ret "u8"; } - ty_u16. { ret "u16"; } - ty_u32. { ret "u32"; } - ty_u64. { ret "u64"; } - ty_i8. { ret "i8"; } - ty_i16. { ret "i16"; } - ty_i32. { ret "i32"; } - ty_i64. { ret "i64"; } - ty_f32. { ret "f32"; } - ty_f64. { ret "f64"; } - } -} - type ty = spanned<ty_>; tag ty_ { @@ -618,79 +488,6 @@ tag native_item_ { native_item_fn(option::t<str>, fn_decl, [ty_param]); } -fn is_exported(i: ident, m: _mod) -> bool { - let nonlocal = true; - for it: @ast::item in m.items { - if it.ident == i { nonlocal = false; } - alt it.node { - item_tag(variants, _) { - for v: variant in variants { - if v.node.name == i { nonlocal = false; } - } - } - _ { } - } - if !nonlocal { break; } - } - let count = 0u; - for vi: @ast::view_item in m.view_items { - alt vi.node { - ast::view_item_export(ids, _) { - for id in ids { if str::eq(i, id) { ret true; } } - count += 1u; - } - _ {/* fall through */ } - } - } - // If there are no declared exports then - // everything not imported is exported - // even if it's nonlocal (since it's explicit) - ret count == 0u && !nonlocal; -} - -fn is_call_expr(e: @expr) -> bool { - alt e.node { expr_call(_, _) { ret true; } _ { ret false; } } -} - -fn is_constraint_arg(e: @expr) -> bool { - alt e.node { - expr_lit(_) { ret true; } - expr_path(_) { ret true; } - _ { ret false; } - } -} - -fn eq_ty(a: &@ty, b: &@ty) -> bool { ret std::box::ptr_eq(a, b); } - -fn hash_ty(t: &@ty) -> uint { ret t.span.lo << 16u + t.span.hi; } - -fn block_from_expr(e: @expr) -> blk { - let blk_ = {stmts: [], expr: option::some::<@expr>(e), id: e.id}; - ret {node: blk_, span: e.span}; -} - - -fn obj_field_from_anon_obj_field(f: &anon_obj_field) -> obj_field { - ret {mut: f.mut, ty: f.ty, ident: f.ident, id: f.id}; -} - -// This is a convenience function to transfor ternary expressions to if -// expressions so that they can be treated the same -fn ternary_to_if(e: &@expr) -> @ast::expr { - alt e.node { - expr_ternary(cond, then, els) { - let then_blk = block_from_expr(then); - let els_blk = block_from_expr(els); - let els_expr = - @{id: els.id, node: expr_block(els_blk), span: els.span}; - ret @{id: e.id, - node: expr_if(cond, then_blk, option::some(els_expr)), - span: e.span}; - } - _ { fail; } - } -} - // // Local Variables: // mode: rust diff --git a/src/comp/syntax/ast_util.rs b/src/comp/syntax/ast_util.rs new file mode 100644 index 00000000000..e3b16702b76 --- /dev/null +++ b/src/comp/syntax/ast_util.rs @@ -0,0 +1,209 @@ +import std::str; +import std::option; +import codemap::span; +import ast::*; + +fn respan<T>(sp: &span, t: &T) -> spanned<T> { ret {node: t, span: sp}; } + +/* assuming that we're not in macro expansion */ +fn mk_sp(lo: uint, hi: uint) -> span { + ret {lo: lo, hi: hi, expanded_from: codemap::os_none}; +} + +// make this a const, once the compiler supports it +fn dummy_sp() -> span { ret mk_sp(0u, 0u); } + +fn path_name(p: &path) -> str { path_name_i(p.node.idents) } + +fn path_name_i(idents: &[ident]) -> str { str::connect(idents, "::") } + +fn local_def(id: node_id) -> def_id { ret {crate: local_crate, node: id}; } + +fn variant_def_ids(d: &def) -> {tg: def_id, var: def_id} { + alt d { def_variant(tag_id, var_id) { ret {tg: tag_id, var: var_id}; } } +} + +fn def_id_of_def(d: def) -> def_id { + alt d { + def_fn(id, _) { ret id; } + def_obj_field(id) { ret id; } + def_mod(id) { ret id; } + def_native_mod(id) { ret id; } + def_const(id) { ret id; } + def_arg(id) { ret id; } + def_local(id) { ret id; } + def_variant(_, id) { ret id; } + def_ty(id) { ret id; } + def_ty_arg(_, _) { fail; } + def_binding(id) { ret id; } + def_use(id) { ret id; } + def_native_ty(id) { ret id; } + def_native_fn(id) { ret id; } + def_upvar(id, _) { ret id; } + } +} + +// This is used because same-named variables in alternative patterns need to +// use the node_id of their namesake in the first pattern. +fn pat_id_map(pat: &@pat) -> pat_id_map { + let map = std::map::new_str_hash::<node_id>(); + for each bound in pat_bindings(pat) { + let name = alt bound.node { pat_bind(n) { n } }; + map.insert(name, bound.id); + } + ret map; +} + +// FIXME: could return a constrained type +iter pat_bindings(pat: &@pat) -> @pat { + alt pat.node { + pat_bind(_) { put pat; } + pat_tag(_, sub) { + for p in sub { for each b in pat_bindings(p) { put b; } } + } + pat_rec(fields, _) { + for f in fields { for each b in pat_bindings(f.pat) { put b; } } + } + pat_tup(elts) { + for elt in elts { for each b in pat_bindings(elt) { put b; } } + } + pat_box(sub) { for each b in pat_bindings(sub) { put b; } } + pat_wild. | pat_lit(_) { } + } +} + +fn pat_binding_ids(pat: &@pat) -> [node_id] { + let found = []; + for each b in pat_bindings(pat) { found += [b.id]; } + ret found; +} + +fn binop_to_str(op: binop) -> str { + alt op { + add. { ret "+"; } + sub. { ret "-"; } + mul. { ret "*"; } + div. { ret "/"; } + rem. { ret "%"; } + and. { ret "&&"; } + or. { ret "||"; } + bitxor. { ret "^"; } + bitand. { ret "&"; } + bitor. { ret "|"; } + lsl. { ret "<<"; } + lsr. { ret ">>"; } + asr. { ret ">>>"; } + eq. { ret "=="; } + lt. { ret "<"; } + le. { ret "<="; } + ne. { ret "!="; } + ge. { ret ">="; } + gt. { ret ">"; } + } +} + +pred lazy_binop(b: binop) -> bool { + alt b { and. { true } or. { true } _ { false } } +} + +fn unop_to_str(op: unop) -> str { + alt op { + box(mt) { if mt == mut { ret "@mutable "; } ret "@"; } + deref. { ret "*"; } + not. { ret "!"; } + neg. { ret "-"; } + } +} + +fn is_path(e: &@expr) -> bool { + ret alt e.node { expr_path(_) { true } _ { false } }; +} + +fn ty_mach_to_str(tm: ty_mach) -> str { + alt tm { + ty_u8. { ret "u8"; } + ty_u16. { ret "u16"; } + ty_u32. { ret "u32"; } + ty_u64. { ret "u64"; } + ty_i8. { ret "i8"; } + ty_i16. { ret "i16"; } + ty_i32. { ret "i32"; } + ty_i64. { ret "i64"; } + ty_f32. { ret "f32"; } + ty_f64. { ret "f64"; } + } +} + + +fn is_exported(i: ident, m: _mod) -> bool { + let nonlocal = true; + for it: @ast::item in m.items { + if it.ident == i { nonlocal = false; } + alt it.node { + item_tag(variants, _) { + for v: variant in variants { + if v.node.name == i { nonlocal = false; } + } + } + _ { } + } + if !nonlocal { break; } + } + let count = 0u; + for vi: @ast::view_item in m.view_items { + alt vi.node { + ast::view_item_export(ids, _) { + for id in ids { if str::eq(i, id) { ret true; } } + count += 1u; + } + _ {/* fall through */ } + } + } + // If there are no declared exports then + // everything not imported is exported + // even if it's nonlocal (since it's explicit) + ret count == 0u && !nonlocal; +} + +fn is_call_expr(e: @expr) -> bool { + alt e.node { expr_call(_, _) { ret true; } _ { ret false; } } +} + +fn is_constraint_arg(e: @expr) -> bool { + alt e.node { + expr_lit(_) { ret true; } + expr_path(_) { ret true; } + _ { ret false; } + } +} + +fn eq_ty(a: &@ty, b: &@ty) -> bool { ret std::box::ptr_eq(a, b); } + +fn hash_ty(t: &@ty) -> uint { ret t.span.lo << 16u + t.span.hi; } + +fn block_from_expr(e: @expr) -> blk { + let blk_ = {stmts: [], expr: option::some::<@expr>(e), id: e.id}; + ret {node: blk_, span: e.span}; +} + + +fn obj_field_from_anon_obj_field(f: &anon_obj_field) -> obj_field { + ret {mut: f.mut, ty: f.ty, ident: f.ident, id: f.id}; +} + +// This is a convenience function to transfor ternary expressions to if +// expressions so that they can be treated the same +fn ternary_to_if(e: &@expr) -> @ast::expr { + alt e.node { + expr_ternary(cond, then, els) { + let then_blk = block_from_expr(then); + let els_blk = block_from_expr(els); + let els_expr = + @{id: els.id, node: expr_block(els_blk), span: els.span}; + ret @{id: e.id, + node: expr_if(cond, then_blk, option::some(els_expr)), + span: e.span}; + } + _ { fail; } + } +} diff --git a/src/comp/syntax/ext/simplext.rs b/src/comp/syntax/ext/simplext.rs index e5dd343203a..fb4402732ae 100644 --- a/src/comp/syntax/ext/simplext.rs +++ b/src/comp/syntax/ext/simplext.rs @@ -16,7 +16,7 @@ import base::expr_to_ident; import fold::*; import ast::node_id; -import ast::respan; +import ast_util::respan; import ast::ident; import ast::path; import ast::ty; diff --git a/src/comp/syntax/parse/lexer.rs b/src/comp/syntax/parse/lexer.rs index 6e4cc10abb7..c39cd75db3b 100644 --- a/src/comp/syntax/parse/lexer.rs +++ b/src/comp/syntax/parse/lexer.rs @@ -78,7 +78,7 @@ fn new_reader(cm: &codemap::codemap, src: str, filemap: codemap::filemap, fn get_col() -> uint { ret col; } fn get_filemap() -> codemap::filemap { ret fm; } fn err(m: str) { - codemap::emit_error(some(ast::mk_sp(chpos, chpos)), m, cm); + codemap::emit_error(some(ast_util::mk_sp(chpos, chpos)), m, cm); } } let strs: [str] = []; diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index b20c7905013..527f6927010 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -90,7 +90,7 @@ fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: lexer::reader, if vec::len(buffer) == 0u { let next = lexer::next_token(rdr); tok = next.tok; - tok_span = ast::mk_sp(next.chpos, rdr.get_chpos()); + tok_span = ast_util::mk_sp(next.chpos, rdr.get_chpos()); } else { let next = vec::pop(buffer); tok = next.tok; @@ -99,12 +99,12 @@ fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: lexer::reader, } fn swap(next: token::token, lo: uint, hi: uint) { tok = next; - tok_span = ast::mk_sp(lo, hi); + tok_span = ast_util::mk_sp(lo, hi); } fn look_ahead(distance: uint) -> token::token { while vec::len(buffer) < distance { let next = lexer::next_token(rdr); - let sp = ast::mk_sp(next.chpos, rdr.get_chpos()); + let sp = ast_util::mk_sp(next.chpos, rdr.get_chpos()); buffer = [{tok: next.tok, span: sp}] + buffer; } ret buffer[distance - 1u].tok; @@ -139,7 +139,7 @@ fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: lexer::reader, } let tok0 = lexer::next_token(rdr); - let span0 = ast::mk_sp(tok0.chpos, rdr.get_chpos()); + let span0 = ast_util::mk_sp(tok0.chpos, rdr.get_chpos()); ret stdio_parser(sess, cfg, ftype, tok0.tok, span0, span0, [], UNRESTRICTED, rdr, prec_table(), bad_expr_word_table()); } @@ -223,7 +223,7 @@ fn expect_gt(p: &parser) { } fn spanned<T>(lo: uint, hi: uint, node: &T) -> spanned<T> { - ret {node: node, span: ast::mk_sp(lo, hi)}; + ret {node: node, span: ast_util::mk_sp(lo, hi)}; } fn parse_ident(p: &parser) -> ast::ident { @@ -792,13 +792,13 @@ fn parse_field(p: &parser, sep: &token::token) -> ast::field { } fn mk_expr(p: &parser, lo: uint, hi: uint, node: &ast::expr_) -> @ast::expr { - ret @{id: p.get_id(), node: node, span: ast::mk_sp(lo, hi)}; + ret @{id: p.get_id(), node: node, span: ast_util::mk_sp(lo, hi)}; } fn mk_mac_expr(p: &parser, lo: uint, hi: uint, m: &ast::mac_) -> @ast::expr { ret @{id: p.get_id(), - node: ast::expr_mac({node: m, span: ast::mk_sp(lo, hi)}), - span: ast::mk_sp(lo, hi)}; + node: ast::expr_mac({node: m, span: ast_util::mk_sp(lo, hi)}), + span: ast_util::mk_sp(lo, hi)}; } fn parse_bottom_expr(p: &parser) -> @ast::expr { @@ -999,7 +999,7 @@ fn parse_bottom_expr(p: &parser) -> @ast::expr { let e = parse_expr(p); // FIXME: Is this the right place for this check? - if /*check*/ast::is_call_expr(e) { + if /*check*/ast_util::is_call_expr(e) { hi = e.span.hi; ex = ast::expr_be(e); } else { p.fatal("Non-call expression in tail call"); } @@ -1460,7 +1460,7 @@ fn parse_pat(p: &parser) -> @ast::pat { subpat = @{id: p.get_id(), node: ast::pat_bind(fieldname), - span: ast::mk_sp(lo, hi)}; + span: ast_util::mk_sp(lo, hi)}; } fields += [{ident: fieldname, pat: subpat}]; } @@ -1474,7 +1474,8 @@ fn parse_pat(p: &parser) -> @ast::pat { hi = p.get_hi_pos(); p.bump(); pat = - ast::pat_lit(@{node: ast::lit_nil, span: ast::mk_sp(lo, hi)}); + ast::pat_lit(@{node: ast::lit_nil, + span: ast_util::mk_sp(lo, hi)}); } else { let fields = [parse_pat(p)]; while p.peek() == token::COMMA { @@ -1520,7 +1521,7 @@ fn parse_pat(p: &parser) -> @ast::pat { } } } - ret @{id: p.get_id(), node: pat, span: ast::mk_sp(lo, hi)}; + ret @{id: p.get_id(), node: pat, span: ast_util::mk_sp(lo, hi)}; } fn parse_local(p: &parser, allow_init: bool) -> @ast::local { @@ -1808,7 +1809,7 @@ fn mk_item(p: &parser, lo: uint, hi: uint, ident: &ast::ident, attrs: attrs, id: p.get_id(), node: node, - span: ast::mk_sp(lo, hi)}; + span: ast_util::mk_sp(lo, hi)}; } fn parse_item_fn_or_iter(p: &parser, purity: ast::purity, proto: ast::proto, @@ -1945,7 +1946,7 @@ fn parse_item_native_type(p: &parser, attrs: &[ast::attribute]) -> attrs: attrs, node: ast::native_item_ty, id: p.get_id(), - span: ast::mk_sp(t.lo, hi)}; + span: ast_util::mk_sp(t.lo, hi)}; } fn parse_item_native_fn(p: &parser, attrs: &[ast::attribute]) -> @@ -1961,7 +1962,7 @@ fn parse_item_native_fn(p: &parser, attrs: &[ast::attribute]) -> attrs: attrs, node: ast::native_item_fn(link_name, decl, t.tps), id: p.get_id(), - span: ast::mk_sp(lo, hi)}; + span: ast_util::mk_sp(lo, hi)}; } fn parse_native_item(p: &parser, attrs: &[ast::attribute]) -> diff --git a/src/comp/syntax/parse/token.rs b/src/comp/syntax/parse/token.rs index c974dc915c4..cf20e0a328e 100644 --- a/src/comp/syntax/parse/token.rs +++ b/src/comp/syntax/parse/token.rs @@ -1,6 +1,6 @@ import ast::ty_mach; -import ast::ty_mach_to_str; +import ast_util::ty_mach_to_str; import std::map::new_str_hash; import util::interner; import std::int; diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index 8ecbfd58405..f0848fbdce5 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -9,6 +9,7 @@ import parse::lexer; import syntax::codemap::codemap; import syntax::visit; import ast; +import ast_util; import option::some; import option::none; import pp::printer; @@ -271,7 +272,7 @@ fn print_type(s: &ps, ty: &@ast::ty) { ast::ty_int. { word(s.s, "int"); } ast::ty_uint. { word(s.s, "uint"); } ast::ty_float. { word(s.s, "float"); } - ast::ty_machine(tm) { word(s.s, ast::ty_mach_to_str(tm)); } + ast::ty_machine(tm) { word(s.s, ast_util::ty_mach_to_str(tm)); } ast::ty_char. { word(s.s, "char"); } ast::ty_str. { word(s.s, "str"); } ast::ty_istr. { word(s.s, "istr"); } @@ -831,11 +832,11 @@ fn print_expr(s: &ps, expr: &@ast::expr) { let prec = operator_prec(op); print_maybe_parens(s, lhs, prec); space(s.s); - word_space(s, ast::binop_to_str(op)); + word_space(s, ast_util::binop_to_str(op)); print_maybe_parens(s, rhs, prec + 1); } ast::expr_unary(op, expr) { - word(s.s, ast::unop_to_str(op)); + word(s.s, ast_util::unop_to_str(op)); print_maybe_parens(s, expr, parse::parser::unop_prec); } ast::expr_lit(lit) { print_literal(s, lit); } @@ -967,7 +968,7 @@ fn print_expr(s: &ps, expr: &@ast::expr) { ast::expr_assign_op(op, lhs, rhs) { print_expr(s, lhs); space(s.s); - word(s.s, ast::binop_to_str(op)); + word(s.s, ast_util::binop_to_str(op)); word_space(s, "="); print_expr(s, rhs); } @@ -1486,12 +1487,12 @@ fn print_literal(s: &ps, lit: &@ast::lit) { ast::lit_float(fstr) { word(s.s, fstr); } ast::lit_mach_int(mach, val) { word(s.s, int::str(val as int)); - word(s.s, ast::ty_mach_to_str(mach)); + word(s.s, ast_util::ty_mach_to_str(mach)); } ast::lit_mach_float(mach, val) { // val is already a str word(s.s, val); - word(s.s, ast::ty_mach_to_str(mach)); + word(s.s, ast_util::ty_mach_to_str(mach)); } ast::lit_nil. { word(s.s, "()"); } ast::lit_bool(val) { diff --git a/src/comp/util/ppaux.rs b/src/comp/util/ppaux.rs index 5a71005acc2..a37e64cfb0a 100644 --- a/src/comp/util/ppaux.rs +++ b/src/comp/util/ppaux.rs @@ -16,7 +16,7 @@ import pp::word; import pp::eof; import pp::zerobreak; import pp::hardbreak; -import ast::ty_mach_to_str; +import syntax::ast_util::ty_mach_to_str; import syntax::ast; import middle::ast_map; import metadata::csearch; |
