Reserve node_id 0 for the crate top-level module

And define a const to refer to it.
This commit is contained in:
Marijn Haverbeke 2011-12-16 16:55:22 +01:00
parent dbfa1b5689
commit dd9693f211
2 changed files with 12 additions and 9 deletions

View File

@ -27,8 +27,6 @@
// locates all names (in expressions, types, and alt patterns) and resolves
// them, storing the resulting def in the AST nodes.
const crate_mod: int = -1;
tag scope {
scope_crate;
scope_item(@ast::item);
@ -176,7 +174,7 @@ fn map_crate(e: @env, c: @ast::crate) {
visit::visit_crate(*c, cons(scope_crate, @nil), visit::mk_vt(v_map_mod));
// Register the top-level mod
e.mod_map.insert(crate_mod,
e.mod_map.insert(ast::crate_node_id,
@{m: some(c.node.module),
index: index_mod(c.node.module),
mutable glob_imports: [],
@ -260,7 +258,7 @@ fn link_glob(e: @env, vi: @ast::view_item, sc: scopes, _v: vt<scopes>) {
e.block_map.insert(b.node.id, globs);
}
scope_crate. {
e.mod_map.get(crate_mod).glob_imports += [glob];
e.mod_map.get(ast::crate_node_id).glob_imports += [glob];
}
}
}
@ -534,7 +532,8 @@ fn lst(my_id: node_id, vis: [@view_item]) -> [node_id] {
lst(id, b.node.view_items)
}
cons(scope_crate., _) {
lst(id, option::get(e.mod_map.get(crate_mod).m).view_items)
lst(id,
option::get(e.mod_map.get(ast::crate_node_id).m).view_items)
}
}
}
@ -639,7 +638,7 @@ fn find_fn_or_mod_scope(sc: scopes) -> option::t<scope> {
let did = def_id_of_def(def);
if did.crate == ast::local_crate {
path = e.mod_map.get(did.node).path + path;
} else if did.node != -1 {
} else if did.node != ast::crate_node_id {
let paths = e.ext_map.get(did);
if vec::len(paths) > 0u {
path = str::connect(paths, "::") + "::" + path;
@ -744,7 +743,8 @@ fn in_scope(e: env, sp: span, name: ident, s: scope, ns: namespace) ->
option::t<def> {
alt s {
scope_crate. {
ret lookup_in_local_mod(e, crate_mod, sp, name, ns, inside);
ret lookup_in_local_mod(e, ast::crate_node_id, sp,
name, ns, inside);
}
scope_item(it) {
alt it.node {
@ -1057,7 +1057,9 @@ fn lookup_in_mod(e: env, m: def, sp: span, name: ident, ns: namespace,
let cached = e.ext_cache.find({did: defid, ident: name, ns: ns});
if !is_none(cached) { ret cached; }
let path = [name];
if defid.node != crate_mod { path = e.ext_map.get(defid) + path; }
if defid.node != ast::crate_node_id {
path = e.ext_map.get(defid) + path;
}
let fnd = lookup_external(e, defid.crate, path, ns);
if !is_none(fnd) {
e.ext_cache.insert({did: defid, ident: name, ns: ns},
@ -1079,7 +1081,7 @@ fn found_view_item(e: env, vi: @ast::view_item) -> option::t<def> {
alt vi.node {
ast::view_item_use(_, _, id) {
let cnum = cstore::get_use_stmt_cnum(e.cstore, id);
ret some(ast::def_mod({crate: cnum, node: crate_mod}));
ret some(ast::def_mod({crate: cnum, node: ast::crate_node_id}));
}
}
}

View File

@ -22,6 +22,7 @@
type def_id = {crate: crate_num, node: node_id};
const local_crate: crate_num = 0;
const crate_node_id: node_id = 0;
type ty_param = {ident: ident, kind: kind};