2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-03-13 21:25:28 -05:00
|
|
|
use abi::AbiSet;
|
2012-09-04 13:37:29 -05:00
|
|
|
use ast::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
2013-03-26 15:38:07 -05:00
|
|
|
use ast_util::{inlined_item_utils, stmt_id};
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast_util;
|
2013-08-31 11:13:04 -05:00
|
|
|
use codemap::Span;
|
2012-12-23 16:41:37 -06:00
|
|
|
use codemap;
|
2012-09-04 13:37:29 -05:00
|
|
|
use diagnostic::span_handler;
|
|
|
|
use parse::token::ident_interner;
|
2013-07-19 20:42:11 -05:00
|
|
|
use parse::token::special_idents;
|
2012-12-23 16:41:37 -06:00
|
|
|
use print::pprust;
|
2013-07-19 20:42:11 -05:00
|
|
|
use visit::{Visitor, fn_kind};
|
2012-12-23 16:41:37 -06:00
|
|
|
use visit;
|
|
|
|
|
2013-06-24 19:40:33 -05:00
|
|
|
use std::hashmap::HashMap;
|
|
|
|
use std::vec;
|
2011-06-19 15:41:21 -05:00
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2013-01-29 15:54:06 -06:00
|
|
|
pub enum path_elt {
|
2013-09-01 19:50:59 -05:00
|
|
|
path_mod(Ident),
|
|
|
|
path_name(Ident)
|
2012-08-27 18:26:35 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub type path = ~[path_elt];
|
2012-02-03 02:53:37 -06:00
|
|
|
|
2013-06-05 06:57:24 -05:00
|
|
|
pub fn path_to_str_with_sep(p: &[path_elt], sep: &str, itr: @ident_interner)
|
2013-01-29 15:54:06 -06:00
|
|
|
-> ~str {
|
2013-01-22 13:57:39 -06:00
|
|
|
let strs = do p.map |e| {
|
2012-09-21 20:43:30 -05:00
|
|
|
match *e {
|
2013-06-12 12:02:55 -05:00
|
|
|
path_mod(s) => itr.get(s.name),
|
|
|
|
path_name(s) => itr.get(s.name)
|
2012-02-10 08:01:32 -06:00
|
|
|
}
|
|
|
|
};
|
2013-06-10 08:25:25 -05:00
|
|
|
strs.connect(sep)
|
2012-02-10 08:01:32 -06:00
|
|
|
}
|
|
|
|
|
2013-09-01 19:50:59 -05:00
|
|
|
pub fn path_ident_to_str(p: &path, i: Ident, itr: @ident_interner) -> ~str {
|
2013-06-08 20:38:47 -05:00
|
|
|
if p.is_empty() {
|
2013-06-12 12:02:55 -05:00
|
|
|
itr.get(i.name).to_owned()
|
2012-03-28 14:54:06 -05:00
|
|
|
} else {
|
2013-06-12 12:02:55 -05:00
|
|
|
fmt!("%s::%s", path_to_str(*p, itr), itr.get(i.name))
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn path_to_str(p: &[path_elt], itr: @ident_interner) -> ~str {
|
2013-06-05 06:57:24 -05:00
|
|
|
path_to_str_with_sep(p, "::", itr)
|
2012-02-10 08:01:32 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn path_elt_to_str(pe: path_elt, itr: @ident_interner) -> ~str {
|
2013-01-10 01:17:57 -06:00
|
|
|
match pe {
|
2013-06-12 12:02:55 -05:00
|
|
|
path_mod(s) => itr.get(s.name).to_owned(),
|
|
|
|
path_name(s) => itr.get(s.name).to_owned()
|
2013-01-10 01:17:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone)]
|
2013-01-29 15:54:06 -06:00
|
|
|
pub enum ast_node {
|
2012-02-03 02:53:37 -06:00
|
|
|
node_item(@item, @path),
|
2013-03-13 21:25:28 -05:00
|
|
|
node_foreign_item(@foreign_item, AbiSet, visibility, @path),
|
2012-08-02 17:52:25 -05:00
|
|
|
node_trait_method(@trait_method, def_id /* trait did */,
|
|
|
|
@path /* path to the trait */),
|
2012-03-02 15:14:10 -06:00
|
|
|
node_method(@method, def_id /* impl did */, @path /* path to the impl */),
|
2012-03-07 05:21:08 -06:00
|
|
|
node_variant(variant, @item, @path),
|
2012-01-19 19:56:05 -06:00
|
|
|
node_expr(@expr),
|
2012-08-17 16:09:20 -05:00
|
|
|
node_stmt(@stmt),
|
2013-03-15 14:24:24 -05:00
|
|
|
node_arg,
|
2013-09-01 19:50:59 -05:00
|
|
|
node_local(Ident),
|
2013-07-19 00:38:55 -05:00
|
|
|
node_block(Block),
|
2012-10-26 20:23:45 -05:00
|
|
|
node_struct_ctor(@struct_def, @item, @path),
|
2013-03-15 14:24:24 -05:00
|
|
|
node_callee_scope(@expr)
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub type map = @mut HashMap<NodeId, ast_node>;
|
2013-02-04 16:02:01 -06:00
|
|
|
|
|
|
|
pub struct Ctx {
|
2013-03-23 20:45:27 -05:00
|
|
|
map: map,
|
2013-02-04 16:02:01 -06:00
|
|
|
path: path,
|
2013-08-11 12:23:40 -05:00
|
|
|
diag: @mut span_handler,
|
2013-01-17 10:55:28 -06:00
|
|
|
}
|
2011-06-19 15:41:21 -05:00
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
impl Ctx {
|
2013-09-01 19:50:59 -05:00
|
|
|
fn extend(&self, elt: Ident) -> @path {
|
2013-07-19 20:42:11 -05:00
|
|
|
@vec::append(self.path.clone(), [path_name(elt)])
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn map_method(&mut self,
|
2013-07-19 20:42:11 -05:00
|
|
|
impl_did: def_id,
|
|
|
|
impl_path: @path,
|
|
|
|
m: @method,
|
|
|
|
is_provided: bool) {
|
|
|
|
let entry = if is_provided {
|
|
|
|
node_trait_method(@provided(m), impl_did, impl_path)
|
|
|
|
} else {
|
|
|
|
node_method(m, impl_did, impl_path)
|
|
|
|
};
|
|
|
|
self.map.insert(m.id, entry);
|
|
|
|
self.map.insert(m.self_id, node_local(special_idents::self_));
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn map_struct_def(&mut self,
|
2013-07-19 20:42:11 -05:00
|
|
|
struct_def: @ast::struct_def,
|
|
|
|
parent_node: ast_node,
|
2013-09-01 19:50:59 -05:00
|
|
|
ident: ast::Ident) {
|
2013-07-19 20:42:11 -05:00
|
|
|
let p = self.extend(ident);
|
|
|
|
|
|
|
|
// If this is a tuple-like struct, register the constructor.
|
|
|
|
match struct_def.ctor_id {
|
|
|
|
None => {}
|
|
|
|
Some(ctor_id) => {
|
|
|
|
match parent_node {
|
|
|
|
node_item(item, _) => {
|
|
|
|
self.map.insert(ctor_id,
|
|
|
|
node_struct_ctor(struct_def,
|
|
|
|
item,
|
|
|
|
p));
|
|
|
|
}
|
|
|
|
_ => fail!("struct def parent wasn't an item")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn map_expr(&mut self, ex: @expr) {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.map.insert(ex.id, node_expr(ex));
|
|
|
|
|
|
|
|
// Expressions which are or might be calls:
|
|
|
|
{
|
|
|
|
let r = ex.get_callee_id();
|
2013-08-03 11:45:23 -05:00
|
|
|
for callee_id in r.iter() {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.map.insert(*callee_id, node_callee_scope(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_expr(self, ex, ());
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
2013-02-04 16:02:01 -06:00
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn map_fn(&mut self,
|
2013-07-19 20:42:11 -05:00
|
|
|
fk: &visit::fn_kind,
|
|
|
|
decl: &fn_decl,
|
|
|
|
body: &Block,
|
2013-08-31 11:13:04 -05:00
|
|
|
sp: codemap::Span,
|
2013-07-19 20:42:11 -05:00
|
|
|
id: NodeId) {
|
2013-08-03 11:45:23 -05:00
|
|
|
for a in decl.inputs.iter() {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.map.insert(a.id, node_arg);
|
|
|
|
}
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_fn(self, fk, decl, body, sp, id, ());
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn map_stmt(&mut self, stmt: @stmt) {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.map.insert(stmt_id(stmt), node_stmt(stmt));
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_stmt(self, stmt, ());
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn map_block(&mut self, b: &Block) {
|
2013-07-19 20:42:11 -05:00
|
|
|
// clone is FIXME #2543
|
|
|
|
self.map.insert(b.id, node_block((*b).clone()));
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_block(self, b, ());
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn map_pat(&mut self, pat: @pat) {
|
2013-07-19 20:42:11 -05:00
|
|
|
match pat.node {
|
|
|
|
pat_ident(_, ref path, _) => {
|
|
|
|
// Note: this is at least *potentially* a pattern...
|
|
|
|
self.map.insert(pat.id,
|
|
|
|
node_local(ast_util::path_to_ident(path)));
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_pat(self, pat, ());
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
2012-03-06 06:57:07 -06:00
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
impl Visitor<()> for Ctx {
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_item(&mut self, i: @item, _: ()) {
|
2013-07-19 20:42:11 -05:00
|
|
|
// clone is FIXME #2543
|
|
|
|
let item_path = @self.path.clone();
|
|
|
|
self.map.insert(i.id, node_item(i, item_path));
|
|
|
|
match i.node {
|
|
|
|
item_impl(_, _, _, ref ms) => {
|
|
|
|
let impl_did = ast_util::local_def(i.id);
|
2013-08-03 11:45:23 -05:00
|
|
|
for m in ms.iter() {
|
2013-08-08 07:23:25 -05:00
|
|
|
let extended = { self.extend(i.ident) };
|
|
|
|
self.map_method(impl_did, extended, *m, false)
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
item_enum(ref enum_definition, _) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for v in (*enum_definition).variants.iter() {
|
2013-07-19 20:42:11 -05:00
|
|
|
// FIXME #2543: bad clone
|
|
|
|
self.map.insert(v.node.id,
|
|
|
|
node_variant((*v).clone(),
|
|
|
|
i,
|
|
|
|
self.extend(i.ident)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
item_foreign_mod(ref nm) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for nitem in nm.items.iter() {
|
2013-07-19 20:42:11 -05:00
|
|
|
// Compute the visibility for this native item.
|
|
|
|
let visibility = match nitem.vis {
|
|
|
|
public => public,
|
|
|
|
private => private,
|
|
|
|
inherited => i.vis
|
|
|
|
};
|
|
|
|
|
|
|
|
self.map.insert(nitem.id,
|
|
|
|
node_foreign_item(*nitem,
|
|
|
|
nm.abis,
|
|
|
|
visibility,
|
|
|
|
// FIXME (#2543)
|
|
|
|
if nm.sort ==
|
|
|
|
ast::named {
|
|
|
|
self.extend(i.ident)
|
|
|
|
} else {
|
|
|
|
// Anonymous extern
|
|
|
|
// mods go in the
|
|
|
|
// parent scope.
|
|
|
|
@self.path.clone()
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
item_struct(struct_def, _) => {
|
|
|
|
self.map_struct_def(struct_def,
|
|
|
|
node_item(i, item_path),
|
|
|
|
i.ident)
|
|
|
|
}
|
|
|
|
item_trait(_, ref traits, ref methods) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for p in traits.iter() {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.map.insert(p.ref_id, node_item(i, item_path));
|
|
|
|
}
|
2013-08-03 11:45:23 -05:00
|
|
|
for tm in methods.iter() {
|
2013-08-20 12:15:36 -05:00
|
|
|
let ext = { self.extend(i.ident) };
|
2013-07-19 20:42:11 -05:00
|
|
|
let d_id = ast_util::local_def(i.id);
|
2013-08-20 12:15:36 -05:00
|
|
|
match *tm {
|
|
|
|
required(ref m) => {
|
|
|
|
let entry =
|
|
|
|
node_trait_method(@(*tm).clone(), d_id, ext);
|
|
|
|
self.map.insert(m.id, entry);
|
|
|
|
}
|
|
|
|
provided(m) => {
|
|
|
|
self.map_method(d_id, ext, m, true);
|
|
|
|
}
|
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
match i.node {
|
|
|
|
item_mod(_) | item_foreign_mod(_) => {
|
|
|
|
self.path.push(path_mod(i.ident));
|
|
|
|
}
|
|
|
|
_ => self.path.push(path_name(i.ident))
|
|
|
|
}
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_item(self, i, ());
|
2013-07-19 20:42:11 -05:00
|
|
|
self.path.pop();
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_pat(&mut self, pat: @pat, _: ()) {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.map_pat(pat);
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_pat(self, pat, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_expr(&mut self, expr: @expr, _: ()) {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.map_expr(expr)
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_stmt(&mut self, stmt: @stmt, _: ()) {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.map_stmt(stmt)
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_fn(&mut self,
|
2013-07-19 20:42:11 -05:00
|
|
|
function_kind: &fn_kind,
|
|
|
|
function_declaration: &fn_decl,
|
|
|
|
block: &Block,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-07-19 20:42:11 -05:00
|
|
|
node_id: NodeId,
|
|
|
|
_: ()) {
|
|
|
|
self.map_fn(function_kind, function_declaration, block, span, node_id)
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_block(&mut self, block: &Block, _: ()) {
|
2013-07-19 20:42:11 -05:00
|
|
|
self.map_block(block)
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Methods below can become default methods.
|
|
|
|
|
2013-08-31 11:13:04 -05:00
|
|
|
fn visit_mod(&mut self, module: &_mod, _: Span, _: NodeId, _: ()) {
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_mod(self, module, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_view_item(&mut self, view_item: &view_item, _: ()) {
|
|
|
|
visit::walk_view_item(self, view_item, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_foreign_item(&mut self, foreign_item: @foreign_item, _: ()) {
|
|
|
|
visit::walk_foreign_item(self, foreign_item, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_local(&mut self, local: @Local, _: ()) {
|
|
|
|
visit::walk_local(self, local, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_arm(&mut self, arm: &arm, _: ()) {
|
|
|
|
visit::walk_arm(self, arm, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_decl(&mut self, decl: @decl, _: ()) {
|
|
|
|
visit::walk_decl(self, decl, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_expr_post(&mut self, _: @expr, _: ()) {
|
2013-07-19 20:42:11 -05:00
|
|
|
// Empty!
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_ty(&mut self, typ: &Ty, _: ()) {
|
|
|
|
visit::walk_ty(self, typ, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_generics(&mut self, generics: &Generics, _: ()) {
|
|
|
|
visit::walk_generics(self, generics, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_fn(&mut self,
|
2013-07-19 20:42:11 -05:00
|
|
|
function_kind: &fn_kind,
|
|
|
|
function_declaration: &fn_decl,
|
|
|
|
block: &Block,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-07-19 20:42:11 -05:00
|
|
|
node_id: NodeId,
|
|
|
|
_: ()) {
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_fn(self,
|
2013-07-19 20:42:11 -05:00
|
|
|
function_kind,
|
|
|
|
function_declaration,
|
|
|
|
block,
|
|
|
|
span,
|
|
|
|
node_id,
|
|
|
|
())
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_ty_method(&mut self, ty_method: &TypeMethod, _: ()) {
|
|
|
|
visit::walk_ty_method(self, ty_method, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_trait_method(&mut self, trait_method: &trait_method, _: ()) {
|
|
|
|
visit::walk_trait_method(self, trait_method, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_struct_def(&mut self,
|
2013-07-19 20:42:11 -05:00
|
|
|
struct_def: @struct_def,
|
2013-09-01 19:50:59 -05:00
|
|
|
ident: Ident,
|
2013-07-19 20:42:11 -05:00
|
|
|
generics: &Generics,
|
|
|
|
node_id: NodeId,
|
|
|
|
_: ()) {
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_struct_def(self,
|
2013-07-19 20:42:11 -05:00
|
|
|
struct_def,
|
|
|
|
ident,
|
|
|
|
generics,
|
|
|
|
node_id,
|
|
|
|
())
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:23:25 -05:00
|
|
|
fn visit_struct_field(&mut self, struct_field: @struct_field, _: ()) {
|
|
|
|
visit::walk_struct_field(self, struct_field, ())
|
2013-07-19 20:42:11 -05:00
|
|
|
}
|
2012-02-14 17:21:53 -06:00
|
|
|
}
|
|
|
|
|
2013-08-11 12:23:40 -05:00
|
|
|
pub fn map_crate(diag: @mut span_handler, c: &Crate) -> map {
|
2013-02-04 16:02:01 -06:00
|
|
|
let cx = @mut Ctx {
|
2013-04-03 08:28:36 -05:00
|
|
|
map: @mut HashMap::new(),
|
2013-02-04 16:02:01 -06:00
|
|
|
path: ~[],
|
2013-01-17 10:55:28 -06:00
|
|
|
diag: diag,
|
|
|
|
};
|
2013-08-08 07:23:25 -05:00
|
|
|
visit::walk_crate(cx, c, ());
|
2013-03-23 20:45:27 -05:00
|
|
|
cx.map
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
// Used for items loaded from external crate that are being inlined into this
|
2012-03-02 15:14:10 -06:00
|
|
|
// crate. The `path` should be the path to the item but should not include
|
|
|
|
// the item itself.
|
2013-08-11 12:23:40 -05:00
|
|
|
pub fn map_decoded_item(diag: @mut span_handler,
|
2013-02-04 16:02:01 -06:00
|
|
|
map: map,
|
2013-04-17 11:15:08 -05:00
|
|
|
path: path,
|
|
|
|
ii: &inlined_item) {
|
2012-02-14 17:21:53 -06:00
|
|
|
// I believe it is ok for the local IDs of inlined items from other crates
|
|
|
|
// to overlap with the local ids from this crate, so just generate the ids
|
2013-07-19 20:42:11 -05:00
|
|
|
// starting from 0.
|
2013-02-04 16:02:01 -06:00
|
|
|
let cx = @mut Ctx {
|
2013-03-23 20:45:27 -05:00
|
|
|
map: map,
|
2013-07-02 14:47:32 -05:00
|
|
|
path: path.clone(),
|
2013-01-17 10:55:28 -06:00
|
|
|
diag: diag,
|
|
|
|
};
|
2012-03-02 15:14:10 -06:00
|
|
|
|
|
|
|
// methods get added to the AST map when their impl is visited. Since we
|
|
|
|
// don't decode and instantiate the impl, but just the method, we have to
|
|
|
|
// add it to the table now:
|
2013-04-17 11:15:08 -05:00
|
|
|
match *ii {
|
2013-07-19 20:42:11 -05:00
|
|
|
ii_item(*) => {} // fallthrough
|
|
|
|
ii_foreign(i) => {
|
|
|
|
cx.map.insert(i.id, node_foreign_item(i,
|
|
|
|
AbiSet::Intrinsic(),
|
|
|
|
i.vis, // Wrong but OK
|
|
|
|
@path));
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
ii_method(impl_did, is_provided, m) => {
|
|
|
|
cx.map_method(impl_did, @path, m, is_provided);
|
2012-08-02 17:52:25 -05:00
|
|
|
}
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
// visit the item / method contents and add those to the map:
|
2013-08-08 07:23:25 -05:00
|
|
|
ii.accept((), cx);
|
2012-08-17 16:09:20 -05:00
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn node_id_to_str(map: map, id: NodeId, itr: @ident_interner) -> ~str {
|
2013-02-05 21:41:45 -06:00
|
|
|
match map.find(&id) {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("unknown node (id=%d)", id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&node_item(item, path)) => {
|
2013-05-11 23:25:31 -05:00
|
|
|
let path_str = path_ident_to_str(path, item.ident, itr);
|
2012-08-09 11:59:50 -05:00
|
|
|
let item_str = match item.node {
|
2013-06-21 20:46:34 -05:00
|
|
|
item_static(*) => ~"static",
|
2012-08-09 11:59:50 -05:00
|
|
|
item_fn(*) => ~"fn",
|
|
|
|
item_mod(*) => ~"mod",
|
|
|
|
item_foreign_mod(*) => ~"foreign mod",
|
|
|
|
item_ty(*) => ~"ty",
|
|
|
|
item_enum(*) => ~"enum",
|
2012-12-10 15:47:54 -06:00
|
|
|
item_struct(*) => ~"struct",
|
2012-08-09 11:59:50 -05:00
|
|
|
item_trait(*) => ~"trait",
|
|
|
|
item_impl(*) => ~"impl",
|
|
|
|
item_mac(*) => ~"macro"
|
|
|
|
};
|
|
|
|
fmt!("%s %s (id=%?)", item_str, path_str, id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&node_foreign_item(item, abi, _, path)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("foreign item %s with abi %? (id=%?)",
|
2013-05-11 23:25:31 -05:00
|
|
|
path_ident_to_str(path, item.ident, itr), abi, id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&node_method(m, _, path)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("method %s in %s (id=%?)",
|
2013-06-12 12:02:55 -05:00
|
|
|
itr.get(m.ident.name), path_to_str(*path, itr), id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&node_trait_method(ref tm, _, path)) => {
|
2013-02-18 00:20:36 -06:00
|
|
|
let m = ast_util::trait_method_to_ty_method(&**tm);
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("method %s in %s (id=%?)",
|
2013-06-12 12:02:55 -05:00
|
|
|
itr.get(m.ident.name), path_to_str(*path, itr), id)
|
2012-08-02 17:52:25 -05:00
|
|
|
}
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&node_variant(ref variant, _, path)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("variant %s in %s (id=%?)",
|
2013-06-12 12:02:55 -05:00
|
|
|
itr.get(variant.node.name.name), path_to_str(*path, itr), id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&node_expr(expr)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("expr %s (id=%?)", pprust::expr_to_str(expr, itr), id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
Some(&node_callee_scope(expr)) => {
|
|
|
|
fmt!("callee_scope %s (id=%?)", pprust::expr_to_str(expr, itr), id)
|
|
|
|
}
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&node_stmt(stmt)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("stmt %s (id=%?)",
|
2013-04-17 11:15:08 -05:00
|
|
|
pprust::stmt_to_str(stmt, itr), id)
|
2012-08-17 16:09:20 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
Some(&node_arg) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("arg (id=%?)", id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
Some(&node_local(ident)) => {
|
2013-06-12 12:02:55 -05:00
|
|
|
fmt!("local (id=%?, name=%s)", id, itr.get(ident.name))
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2013-07-29 15:44:53 -05:00
|
|
|
Some(&node_block(ref block)) => {
|
|
|
|
fmt!("block %s (id=%?)", pprust::block_to_str(block, itr), id)
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
2013-07-29 15:44:53 -05:00
|
|
|
Some(&node_struct_ctor(_, _, path)) => {
|
|
|
|
fmt!("struct_ctor %s (id=%?)", path_to_str(*path, itr), id)
|
2012-10-24 16:36:00 -05:00
|
|
|
}
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
|
|
|
}
|
2012-11-30 13:24:16 -06:00
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn node_item_query<Result>(items: map, id: NodeId,
|
2013-03-07 16:38:38 -06:00
|
|
|
query: &fn(@item) -> Result,
|
2013-04-17 11:15:08 -05:00
|
|
|
error_msg: ~str) -> Result {
|
2013-02-05 21:41:45 -06:00
|
|
|
match items.find(&id) {
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&node_item(it, _)) => query(it),
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(error_msg)
|
2012-11-30 13:24:16 -06:00
|
|
|
}
|
|
|
|
}
|