2015-09-10 14:53:08 -05:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use super::MapEntry::*;
|
|
|
|
|
|
|
|
use rustc_front::hir::*;
|
|
|
|
use rustc_front::util;
|
2015-11-17 16:38:23 -06:00
|
|
|
use rustc_front::intravisit::{self, Visitor};
|
2015-09-17 13:29:59 -05:00
|
|
|
use middle::def_id::{CRATE_DEF_INDEX, DefIndex};
|
2015-09-10 14:53:08 -05:00
|
|
|
use std::iter::repeat;
|
|
|
|
use syntax::ast::{NodeId, CRATE_NODE_ID, DUMMY_NODE_ID};
|
|
|
|
use syntax::codemap::Span;
|
|
|
|
|
|
|
|
/// A Visitor that walks over an AST and collects Node's into an AST
|
|
|
|
/// Map.
|
|
|
|
pub struct NodeCollector<'ast> {
|
2015-11-17 16:38:23 -06:00
|
|
|
pub krate: &'ast Crate,
|
2015-09-10 14:53:08 -05:00
|
|
|
pub map: Vec<MapEntry<'ast>>,
|
2015-09-17 13:29:59 -05:00
|
|
|
pub definitions: Definitions,
|
2015-09-10 14:53:08 -05:00
|
|
|
pub parent_node: NodeId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ast> NodeCollector<'ast> {
|
2015-11-17 16:38:23 -06:00
|
|
|
pub fn root(krate: &'ast Crate) -> NodeCollector<'ast> {
|
2015-09-10 14:53:08 -05:00
|
|
|
let mut collector = NodeCollector {
|
2015-11-17 16:38:23 -06:00
|
|
|
krate: krate,
|
2015-09-10 14:53:08 -05:00
|
|
|
map: vec![],
|
2015-09-17 13:29:59 -05:00
|
|
|
definitions: Definitions::new(),
|
2015-09-10 14:53:08 -05:00
|
|
|
parent_node: CRATE_NODE_ID,
|
|
|
|
};
|
|
|
|
collector.insert_entry(CRATE_NODE_ID, RootCrate);
|
2015-09-17 13:29:59 -05:00
|
|
|
|
|
|
|
let result = collector.create_def_with_parent(None, CRATE_NODE_ID, DefPathData::CrateRoot);
|
|
|
|
assert_eq!(result, CRATE_DEF_INDEX);
|
|
|
|
|
|
|
|
collector.create_def_with_parent(Some(CRATE_DEF_INDEX), DUMMY_NODE_ID, DefPathData::Misc);
|
|
|
|
|
2015-09-10 14:53:08 -05:00
|
|
|
collector
|
|
|
|
}
|
|
|
|
|
2015-11-17 16:38:23 -06:00
|
|
|
pub fn extend(krate: &'ast Crate,
|
|
|
|
parent: &'ast InlinedParent,
|
2015-09-10 14:53:08 -05:00
|
|
|
parent_node: NodeId,
|
2015-09-17 13:29:59 -05:00
|
|
|
parent_def_path: DefPath,
|
2015-09-10 14:53:08 -05:00
|
|
|
map: Vec<MapEntry<'ast>>,
|
2015-09-17 13:29:59 -05:00
|
|
|
definitions: Definitions)
|
2015-09-10 14:53:08 -05:00
|
|
|
-> NodeCollector<'ast> {
|
|
|
|
let mut collector = NodeCollector {
|
2015-11-17 16:38:23 -06:00
|
|
|
krate: krate,
|
2015-09-10 14:53:08 -05:00
|
|
|
map: map,
|
2015-09-17 13:29:59 -05:00
|
|
|
parent_node: parent_node,
|
|
|
|
definitions: definitions,
|
2015-09-10 14:53:08 -05:00
|
|
|
};
|
2015-09-17 13:29:59 -05:00
|
|
|
|
2015-09-10 14:53:08 -05:00
|
|
|
collector.insert_entry(parent_node, RootInlinedParent(parent));
|
2015-09-17 13:29:59 -05:00
|
|
|
collector.create_def(parent_node, DefPathData::InlinedRoot(parent_def_path));
|
2015-09-10 14:53:08 -05:00
|
|
|
|
|
|
|
collector
|
|
|
|
}
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
fn parent_def(&self) -> Option<DefIndex> {
|
|
|
|
let mut parent_node = Some(self.parent_node);
|
|
|
|
while let Some(p) = parent_node {
|
|
|
|
if let Some(q) = self.definitions.opt_def_index(p) {
|
|
|
|
return Some(q);
|
|
|
|
}
|
|
|
|
parent_node = self.map[p as usize].parent_node();
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_def(&mut self, node_id: NodeId, data: DefPathData) -> DefIndex {
|
|
|
|
let parent_def = self.parent_def();
|
|
|
|
self.definitions.create_def_with_parent(parent_def, node_id, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_def_with_parent(&mut self,
|
|
|
|
parent: Option<DefIndex>,
|
|
|
|
node_id: NodeId,
|
|
|
|
data: DefPathData)
|
|
|
|
-> DefIndex {
|
|
|
|
self.definitions.create_def_with_parent(parent, node_id, data)
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_entry(&mut self, id: NodeId, entry: MapEntry<'ast>) {
|
|
|
|
debug!("ast_map: {:?} => {:?}", id, entry);
|
|
|
|
let len = self.map.len();
|
|
|
|
if id as usize >= len {
|
|
|
|
self.map.extend(repeat(NotPresent).take(id as usize - len + 1));
|
|
|
|
}
|
|
|
|
self.map[id as usize] = entry;
|
|
|
|
}
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
fn insert_def(&mut self, id: NodeId, node: Node<'ast>, data: DefPathData) -> DefIndex {
|
|
|
|
self.insert(id, node);
|
|
|
|
self.create_def(id, data)
|
|
|
|
}
|
|
|
|
|
2015-09-10 14:53:08 -05:00
|
|
|
fn insert(&mut self, id: NodeId, node: Node<'ast>) {
|
|
|
|
let entry = MapEntry::from_node(self.parent_node, node);
|
|
|
|
self.insert_entry(id, entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
|
2015-11-17 16:38:23 -06:00
|
|
|
/// Because we want to track parent items and so forth, enable
|
|
|
|
/// deep walking so that we walk nested items in the context of
|
|
|
|
/// their outer items.
|
|
|
|
fn visit_nested_item(&mut self, item: ItemId) {
|
|
|
|
self.visit_item(self.krate.item(item.id))
|
|
|
|
}
|
|
|
|
|
2015-09-10 14:53:08 -05:00
|
|
|
fn visit_item(&mut self, i: &'ast Item) {
|
2015-09-17 13:29:59 -05:00
|
|
|
// Pick the def data. This need not be unique, but the more
|
|
|
|
// information we encapsulate into
|
|
|
|
let def_data = match i.node {
|
2015-12-11 09:01:08 -06:00
|
|
|
ItemDefaultImpl(..) | ItemImpl(..) => DefPathData::Impl(i.name),
|
2015-09-17 13:29:59 -05:00
|
|
|
ItemEnum(..) | ItemStruct(..) | ItemTrait(..) => DefPathData::Type(i.name),
|
|
|
|
ItemExternCrate(..) | ItemMod(..) => DefPathData::Mod(i.name),
|
|
|
|
ItemStatic(..) | ItemConst(..) | ItemFn(..) => DefPathData::Value(i.name),
|
|
|
|
_ => DefPathData::Misc,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.insert_def(i.id, NodeItem(i), def_data);
|
2015-09-10 14:53:08 -05:00
|
|
|
|
|
|
|
let parent_node = self.parent_node;
|
|
|
|
self.parent_node = i.id;
|
|
|
|
|
|
|
|
match i.node {
|
2015-09-17 13:29:59 -05:00
|
|
|
ItemImpl(..) => {}
|
2015-09-10 14:53:08 -05:00
|
|
|
ItemEnum(ref enum_definition, _) => {
|
|
|
|
for v in &enum_definition.variants {
|
2015-09-17 13:29:59 -05:00
|
|
|
let variant_def_index =
|
2015-10-09 19:28:40 -05:00
|
|
|
self.insert_def(v.node.data.id(),
|
2015-12-07 08:17:41 -06:00
|
|
|
NodeVariant(v),
|
2015-09-17 13:29:59 -05:00
|
|
|
DefPathData::EnumVariant(v.node.name));
|
2015-09-10 14:53:08 -05:00
|
|
|
|
2015-10-08 15:45:46 -05:00
|
|
|
for field in v.node.data.fields() {
|
2015-10-01 16:03:22 -05:00
|
|
|
self.create_def_with_parent(
|
|
|
|
Some(variant_def_index),
|
|
|
|
field.node.id,
|
|
|
|
DefPathData::Field(field.node.kind));
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-17 13:29:59 -05:00
|
|
|
ItemForeignMod(..) => {
|
|
|
|
}
|
2015-09-10 14:53:08 -05:00
|
|
|
ItemStruct(ref struct_def, _) => {
|
|
|
|
// If this is a tuple-like struct, register the constructor.
|
2015-10-08 15:45:46 -05:00
|
|
|
if !struct_def.is_struct() {
|
2015-10-09 19:28:40 -05:00
|
|
|
self.insert_def(struct_def.id(),
|
2015-10-25 10:33:51 -05:00
|
|
|
NodeStructCtor(struct_def),
|
2015-09-17 13:29:59 -05:00
|
|
|
DefPathData::StructCtor);
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
2015-10-08 15:45:46 -05:00
|
|
|
for field in struct_def.fields() {
|
2015-09-17 13:29:59 -05:00
|
|
|
self.create_def(field.node.id, DefPathData::Field(field.node.kind));
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ItemTrait(_, _, ref bounds, _) => {
|
|
|
|
for b in bounds.iter() {
|
|
|
|
if let TraitTyParamBound(ref t, TraitBoundModifier::None) = *b {
|
|
|
|
self.insert(t.trait_ref.ref_id, NodeItem(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ItemUse(ref view_path) => {
|
|
|
|
match view_path.node {
|
|
|
|
ViewPathList(_, ref paths) => {
|
|
|
|
for path in paths {
|
|
|
|
self.insert(path.node.id(), NodeItem(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2015-11-17 16:38:23 -06:00
|
|
|
intravisit::walk_item(self, i);
|
2015-09-10 14:53:08 -05:00
|
|
|
self.parent_node = parent_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &'ast ForeignItem) {
|
2015-09-17 13:29:59 -05:00
|
|
|
self.insert_def(foreign_item.id,
|
|
|
|
NodeForeignItem(foreign_item),
|
|
|
|
DefPathData::Value(foreign_item.name));
|
2015-09-10 14:53:08 -05:00
|
|
|
|
|
|
|
let parent_node = self.parent_node;
|
|
|
|
self.parent_node = foreign_item.id;
|
2015-11-17 16:38:23 -06:00
|
|
|
intravisit::walk_foreign_item(self, foreign_item);
|
2015-09-10 14:53:08 -05:00
|
|
|
self.parent_node = parent_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_generics(&mut self, generics: &'ast Generics) {
|
|
|
|
for ty_param in generics.ty_params.iter() {
|
2015-09-17 13:29:59 -05:00
|
|
|
self.insert_def(ty_param.id,
|
|
|
|
NodeTyParam(ty_param),
|
|
|
|
DefPathData::TypeParam(ty_param.name));
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
2015-11-17 16:38:23 -06:00
|
|
|
intravisit::walk_generics(self, generics);
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, ti: &'ast TraitItem) {
|
2015-09-17 13:29:59 -05:00
|
|
|
let def_data = match ti.node {
|
|
|
|
MethodTraitItem(..) | ConstTraitItem(..) => DefPathData::Value(ti.name),
|
|
|
|
TypeTraitItem(..) => DefPathData::Type(ti.name),
|
|
|
|
};
|
|
|
|
|
2015-09-10 14:53:08 -05:00
|
|
|
self.insert(ti.id, NodeTraitItem(ti));
|
2015-09-17 13:29:59 -05:00
|
|
|
self.create_def(ti.id, def_data);
|
|
|
|
|
|
|
|
let parent_node = self.parent_node;
|
|
|
|
self.parent_node = ti.id;
|
2015-09-10 14:53:08 -05:00
|
|
|
|
|
|
|
match ti.node {
|
|
|
|
ConstTraitItem(_, Some(ref expr)) => {
|
2015-09-17 13:29:59 -05:00
|
|
|
self.create_def(expr.id, DefPathData::Initializer);
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
_ => { }
|
|
|
|
}
|
|
|
|
|
2015-11-17 16:38:23 -06:00
|
|
|
intravisit::walk_trait_item(self, ti);
|
2015-09-17 13:29:59 -05:00
|
|
|
|
2015-09-10 14:53:08 -05:00
|
|
|
self.parent_node = parent_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, ii: &'ast ImplItem) {
|
2015-09-17 13:29:59 -05:00
|
|
|
let def_data = match ii.node {
|
2015-11-12 08:57:51 -06:00
|
|
|
ImplItemKind::Method(..) | ImplItemKind::Const(..) => DefPathData::Value(ii.name),
|
|
|
|
ImplItemKind::Type(..) => DefPathData::Type(ii.name),
|
2015-09-17 13:29:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
self.insert_def(ii.id, NodeImplItem(ii), def_data);
|
|
|
|
|
|
|
|
let parent_node = self.parent_node;
|
|
|
|
self.parent_node = ii.id;
|
2015-09-10 14:53:08 -05:00
|
|
|
|
|
|
|
match ii.node {
|
2015-11-12 08:57:51 -06:00
|
|
|
ImplItemKind::Const(_, ref expr) => {
|
2015-09-17 13:29:59 -05:00
|
|
|
self.create_def(expr.id, DefPathData::Initializer);
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
_ => { }
|
|
|
|
}
|
|
|
|
|
2015-11-17 16:38:23 -06:00
|
|
|
intravisit::walk_impl_item(self, ii);
|
2015-09-17 13:29:59 -05:00
|
|
|
|
2015-09-10 14:53:08 -05:00
|
|
|
self.parent_node = parent_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_pat(&mut self, pat: &'ast Pat) {
|
|
|
|
let maybe_binding = match pat.node {
|
2015-09-17 13:29:59 -05:00
|
|
|
PatIdent(_, id, _) => Some(id.node),
|
|
|
|
_ => None
|
2015-09-10 14:53:08 -05:00
|
|
|
};
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
if let Some(id) = maybe_binding {
|
|
|
|
self.insert_def(pat.id, NodeLocal(pat), DefPathData::Binding(id.name));
|
|
|
|
} else {
|
|
|
|
self.insert(pat.id, NodePat(pat));
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let parent_node = self.parent_node;
|
|
|
|
self.parent_node = pat.id;
|
2015-11-17 16:38:23 -06:00
|
|
|
intravisit::walk_pat(self, pat);
|
2015-09-10 14:53:08 -05:00
|
|
|
self.parent_node = parent_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'ast Expr) {
|
|
|
|
self.insert(expr.id, NodeExpr(expr));
|
|
|
|
|
|
|
|
match expr.node {
|
2015-09-17 13:29:59 -05:00
|
|
|
ExprClosure(..) => { self.create_def(expr.id, DefPathData::ClosureExpr); }
|
|
|
|
_ => { }
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let parent_node = self.parent_node;
|
|
|
|
self.parent_node = expr.id;
|
2015-11-17 16:38:23 -06:00
|
|
|
intravisit::walk_expr(self, expr);
|
2015-09-10 14:53:08 -05:00
|
|
|
self.parent_node = parent_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_stmt(&mut self, stmt: &'ast Stmt) {
|
|
|
|
let id = util::stmt_id(stmt);
|
|
|
|
self.insert(id, NodeStmt(stmt));
|
|
|
|
let parent_node = self.parent_node;
|
|
|
|
self.parent_node = id;
|
2015-11-17 16:38:23 -06:00
|
|
|
intravisit::walk_stmt(self, stmt);
|
2015-09-10 14:53:08 -05:00
|
|
|
self.parent_node = parent_node;
|
|
|
|
}
|
|
|
|
|
2015-11-17 16:38:23 -06:00
|
|
|
fn visit_fn(&mut self, fk: intravisit::FnKind<'ast>, fd: &'ast FnDecl,
|
2015-09-10 14:53:08 -05:00
|
|
|
b: &'ast Block, s: Span, id: NodeId) {
|
2015-09-29 05:47:33 -05:00
|
|
|
assert_eq!(self.parent_node, id);
|
2015-11-17 16:38:23 -06:00
|
|
|
intravisit::walk_fn(self, fk, fd, b, s);
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_block(&mut self, block: &'ast Block) {
|
|
|
|
self.insert(block.id, NodeBlock(block));
|
|
|
|
let parent_node = self.parent_node;
|
|
|
|
self.parent_node = block.id;
|
2015-11-17 16:38:23 -06:00
|
|
|
intravisit::walk_block(self, block);
|
2015-09-10 14:53:08 -05:00
|
|
|
self.parent_node = parent_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
|
|
|
|
self.insert(lifetime.id, NodeLifetime(lifetime));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_lifetime_def(&mut self, def: &'ast LifetimeDef) {
|
2015-09-17 13:29:59 -05:00
|
|
|
self.create_def(def.lifetime.id, DefPathData::LifetimeDef(def.lifetime.name));
|
2015-09-10 14:53:08 -05:00
|
|
|
self.visit_lifetime(&def.lifetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_macro_def(&mut self, macro_def: &'ast MacroDef) {
|
2015-09-17 13:29:59 -05:00
|
|
|
self.create_def(macro_def.id, DefPathData::MacroDef(macro_def.name));
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
}
|