1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use hir::map::definitions::*;
12 use hir::def_id::{CRATE_DEF_INDEX, DefIndex, DefIndexAddressSpace};
13 use session::CrateDisambiguator;
16 use syntax::ext::hygiene::Mark;
18 use syntax::symbol::keywords;
19 use syntax::symbol::Symbol;
20 use syntax::parse::token::{self, Token};
23 use hir::map::{ITEM_LIKE_SPACE, REGULAR_SPACE};
25 /// Creates def ids for nodes in the AST.
26 pub struct DefCollector<'a> {
27 definitions: &'a mut Definitions,
28 parent_def: Option<DefIndex>,
30 pub visit_macro_invoc: Option<&'a mut dyn FnMut(MacroInvocationData)>,
33 pub struct MacroInvocationData {
35 pub def_index: DefIndex,
39 impl<'a> DefCollector<'a> {
40 pub fn new(definitions: &'a mut Definitions, expansion: Mark) -> Self {
45 visit_macro_invoc: None,
49 pub fn collect_root(&mut self,
51 crate_disambiguator: CrateDisambiguator) {
52 let root = self.definitions.create_root_def(crate_name,
54 assert_eq!(root, CRATE_DEF_INDEX);
55 self.parent_def = Some(root);
58 fn create_def(&mut self,
61 address_space: DefIndexAddressSpace,
64 let parent_def = self.parent_def.unwrap();
65 debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
67 .create_def_with_parent(parent_def, node_id, data, address_space, self.expansion, span)
70 pub fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
71 let parent = self.parent_def;
72 self.parent_def = Some(parent_def);
74 self.parent_def = parent;
77 pub fn visit_const_expr(&mut self, expr: &Expr) {
79 // Find the node which will be used after lowering.
80 ExprKind::Paren(ref inner) => return self.visit_const_expr(inner),
81 ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id, true),
82 // FIXME(eddyb) Closures should have separate
83 // function definition IDs and expression IDs.
84 ExprKind::Closure(..) => return,
88 self.create_def(expr.id, DefPathData::Initializer, REGULAR_SPACE, expr.span);
91 fn visit_macro_invoc(&mut self, id: NodeId, const_expr: bool) {
92 if let Some(ref mut visit) = self.visit_macro_invoc {
93 visit(MacroInvocationData {
94 mark: id.placeholder_to_mark(),
96 def_index: self.parent_def.unwrap(),
102 impl<'a> visit::Visitor<'a> for DefCollector<'a> {
103 fn visit_item(&mut self, i: &'a Item) {
104 debug!("visit_item: {:?}", i);
106 // Pick the def data. This need not be unique, but the more
107 // information we encapsulate into
108 let def_data = match i.node {
109 ItemKind::Impl(..) => DefPathData::Impl,
110 ItemKind::Trait(..) => DefPathData::Trait(i.ident.name.as_str()),
111 ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
112 ItemKind::TraitAlias(..) |
113 ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::Ty(..) =>
114 DefPathData::TypeNs(i.ident.name.as_str()),
115 ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
116 return visit::walk_item(self, i);
118 ItemKind::Mod(..) => DefPathData::Module(i.ident.name.as_str()),
119 ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
120 DefPathData::ValueNs(i.ident.name.as_str()),
121 ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),
122 ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
123 ItemKind::GlobalAsm(..) => DefPathData::Misc,
124 ItemKind::Use(..) => {
125 return visit::walk_item(self, i);
128 let def = self.create_def(i.id, def_data, ITEM_LIKE_SPACE, i.span);
130 self.with_parent(def, |this| {
132 ItemKind::Enum(ref enum_definition, _) => {
133 for v in &enum_definition.variants {
134 let variant_def_index =
135 this.create_def(v.node.data.id(),
136 DefPathData::EnumVariant(v.node.ident.name.as_str()),
139 this.with_parent(variant_def_index, |this| {
140 for (index, field) in v.node.data.fields().iter().enumerate() {
141 let name = field.ident.map(|ident| ident.name)
142 .unwrap_or_else(|| Symbol::intern(&index.to_string()));
143 this.create_def(field.id,
144 DefPathData::Field(name.as_str()),
149 if let Some(ref expr) = v.node.disr_expr {
150 this.visit_const_expr(expr);
155 ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
156 // If this is a tuple-like struct, register the constructor.
157 if !struct_def.is_struct() {
158 this.create_def(struct_def.id(),
159 DefPathData::StructCtor,
164 for (index, field) in struct_def.fields().iter().enumerate() {
165 let name = field.ident.map(|ident| ident.name)
166 .unwrap_or_else(|| Symbol::intern(&index.to_string()));
167 this.create_def(field.id,
168 DefPathData::Field(name.as_str()),
175 visit::walk_item(this, i);
179 fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
180 self.create_def(id, DefPathData::Misc, ITEM_LIKE_SPACE, use_tree.span);
181 visit::walk_use_tree(self, use_tree, id);
184 fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
185 if let ForeignItemKind::Macro(_) = foreign_item.node {
186 return self.visit_macro_invoc(foreign_item.id, false);
189 let def = self.create_def(foreign_item.id,
190 DefPathData::ValueNs(foreign_item.ident.name.as_str()),
194 self.with_parent(def, |this| {
195 visit::walk_foreign_item(this, foreign_item);
199 fn visit_generic_param(&mut self, param: &'a GenericParam) {
201 GenericParam::Lifetime(ref lifetime_def) => {
203 lifetime_def.lifetime.id,
204 DefPathData::LifetimeDef(lifetime_def.lifetime.ident.name.as_str()),
206 lifetime_def.lifetime.ident.span
209 GenericParam::Type(ref ty_param) => {
212 DefPathData::TypeParam(ty_param.ident.name.as_str()),
219 visit::walk_generic_param(self, param);
222 fn visit_trait_item(&mut self, ti: &'a TraitItem) {
223 let def_data = match ti.node {
224 TraitItemKind::Method(..) | TraitItemKind::Const(..) =>
225 DefPathData::ValueNs(ti.ident.name.as_str()),
226 TraitItemKind::Type(..) => DefPathData::AssocTypeInTrait(ti.ident.name.as_str()),
227 TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id, false),
230 let def = self.create_def(ti.id, def_data, ITEM_LIKE_SPACE, ti.span);
231 self.with_parent(def, |this| {
232 if let TraitItemKind::Const(_, Some(ref expr)) = ti.node {
233 this.visit_const_expr(expr);
236 visit::walk_trait_item(this, ti);
240 fn visit_impl_item(&mut self, ii: &'a ImplItem) {
241 let def_data = match ii.node {
242 ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
243 DefPathData::ValueNs(ii.ident.name.as_str()),
244 ImplItemKind::Type(..) => DefPathData::AssocTypeInImpl(ii.ident.name.as_str()),
245 ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id, false),
248 let def = self.create_def(ii.id, def_data, ITEM_LIKE_SPACE, ii.span);
249 self.with_parent(def, |this| {
250 if let ImplItemKind::Const(_, ref expr) = ii.node {
251 this.visit_const_expr(expr);
254 visit::walk_impl_item(this, ii);
258 fn visit_pat(&mut self, pat: &'a Pat) {
260 PatKind::Mac(..) => return self.visit_macro_invoc(pat.id, false),
261 _ => visit::walk_pat(self, pat),
265 fn visit_expr(&mut self, expr: &'a Expr) {
266 let parent_def = self.parent_def;
269 ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id, false),
270 ExprKind::Repeat(_, ref count) => self.visit_const_expr(count),
271 ExprKind::Closure(..) => {
272 let def = self.create_def(expr.id,
273 DefPathData::ClosureExpr,
276 self.parent_def = Some(def);
281 visit::walk_expr(self, expr);
282 self.parent_def = parent_def;
285 fn visit_ty(&mut self, ty: &'a Ty) {
287 TyKind::Mac(..) => return self.visit_macro_invoc(ty.id, false),
288 TyKind::Array(_, ref length) => self.visit_const_expr(length),
289 TyKind::ImplTrait(..) => {
290 self.create_def(ty.id, DefPathData::ImplTrait, REGULAR_SPACE, ty.span);
292 TyKind::Typeof(ref expr) => self.visit_const_expr(expr),
295 visit::walk_ty(self, ty);
298 fn visit_stmt(&mut self, stmt: &'a Stmt) {
300 StmtKind::Mac(..) => self.visit_macro_invoc(stmt.id, false),
301 _ => visit::walk_stmt(self, stmt),
305 fn visit_token(&mut self, t: Token) {
306 if let Token::Interpolated(nt) = t {
308 token::NtExpr(ref expr) => {
309 if let ExprKind::Mac(..) = expr.node {
310 self.visit_macro_invoc(expr.id, false);