1 use crate::hir::map::definitions::*;
2 use crate::hir::def_id::DefIndex;
5 use syntax::ext::hygiene::ExpnId;
7 use syntax::symbol::{kw, sym};
8 use syntax::parse::token::{self, Token};
11 /// Creates `DefId`s for nodes in the AST.
12 pub struct DefCollector<'a> {
13 definitions: &'a mut Definitions,
18 impl<'a> DefCollector<'a> {
19 pub fn new(definitions: &'a mut Definitions, expansion: ExpnId) -> Self {
20 let parent_def = definitions.invocation_parent(expansion);
21 DefCollector { definitions, parent_def, expansion }
24 fn create_def(&mut self,
29 let parent_def = self.parent_def;
30 debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
31 self.definitions.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
34 pub fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
35 let orig_parent_def = std::mem::replace(&mut self.parent_def, parent_def);
37 self.parent_def = orig_parent_def;
46 generics: &'a Generics,
50 let (closure_id, return_impl_trait_id) = match header.asyncness.node {
54 } => (closure_id, return_impl_trait_id),
58 // For async functions, we need to create their inner defs inside of a
59 // closure to match their desugared representation.
60 let fn_def_data = DefPathData::ValueNs(name.as_interned_str());
61 let fn_def = self.create_def(id, fn_def_data, span);
62 return self.with_parent(fn_def, |this| {
63 this.create_def(return_impl_trait_id, DefPathData::ImplTrait, span);
65 visit::walk_generics(this, generics);
66 visit::walk_fn_decl(this, decl);
68 let closure_def = this.create_def(
69 closure_id, DefPathData::ClosureExpr, span,
71 this.with_parent(closure_def, |this| {
72 visit::walk_block(this, body);
77 fn visit_macro_invoc(&mut self, id: NodeId) {
78 self.definitions.set_invocation_parent(id.placeholder_to_expn_id(), self.parent_def);
82 impl<'a> visit::Visitor<'a> for DefCollector<'a> {
83 fn visit_item(&mut self, i: &'a Item) {
84 debug!("visit_item: {:?}", i);
86 // Pick the def data. This need not be unique, but the more
87 // information we encapsulate into, the better
88 let def_data = match i.node {
89 ItemKind::Impl(..) => DefPathData::Impl,
90 ItemKind::Mod(..) if i.ident.name == kw::Invalid => {
91 return visit::walk_item(self, i);
93 ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) |
94 ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
95 ItemKind::OpaqueTy(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
96 ItemKind::Ty(..) => DefPathData::TypeNs(i.ident.as_interned_str()),
102 ) if header.asyncness.node.is_async() => {
103 return self.visit_async_fn(
113 ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
114 DefPathData::ValueNs(i.ident.as_interned_str()),
115 ItemKind::MacroDef(..) => DefPathData::MacroNs(i.ident.as_interned_str()),
116 ItemKind::Mac(..) => return self.visit_macro_invoc(i.id),
117 ItemKind::GlobalAsm(..) => DefPathData::Misc,
118 ItemKind::Use(..) => {
119 return visit::walk_item(self, i);
122 let def = self.create_def(i.id, def_data, i.span);
124 self.with_parent(def, |this| {
126 ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
127 // If this is a unit or tuple-like struct, register the constructor.
128 if let Some(ctor_hir_id) = struct_def.ctor_id() {
129 this.create_def(ctor_hir_id, DefPathData::Ctor, i.span);
134 visit::walk_item(this, i);
138 fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
139 self.create_def(id, DefPathData::Misc, use_tree.span);
140 visit::walk_use_tree(self, use_tree, id);
143 fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
144 if let ForeignItemKind::Macro(_) = foreign_item.node {
145 return self.visit_macro_invoc(foreign_item.id);
148 let def = self.create_def(foreign_item.id,
149 DefPathData::ValueNs(foreign_item.ident.as_interned_str()),
152 self.with_parent(def, |this| {
153 visit::walk_foreign_item(this, foreign_item);
157 fn visit_variant(&mut self, v: &'a Variant, g: &'a Generics, item_id: NodeId) {
158 let def = self.create_def(v.node.id,
159 DefPathData::TypeNs(v.node.ident.as_interned_str()),
161 self.with_parent(def, |this| {
162 if let Some(ctor_hir_id) = v.node.data.ctor_id() {
163 this.create_def(ctor_hir_id, DefPathData::Ctor, v.span);
165 visit::walk_variant(this, v, g, item_id)
169 fn visit_variant_data(&mut self, data: &'a VariantData, _: Ident,
170 _: &'a Generics, _: NodeId, _: Span) {
171 for (index, field) in data.fields().iter().enumerate() {
172 let name = field.ident.map(|ident| ident.name)
173 .unwrap_or_else(|| sym::integer(index));
174 let def = self.create_def(field.id,
175 DefPathData::ValueNs(name.as_interned_str()),
177 self.with_parent(def, |this| this.visit_struct_field(field));
181 fn visit_generic_param(&mut self, param: &'a GenericParam) {
182 let name = param.ident.as_interned_str();
183 let def_path_data = match param.kind {
184 GenericParamKind::Lifetime { .. } => DefPathData::LifetimeNs(name),
185 GenericParamKind::Type { .. } => DefPathData::TypeNs(name),
186 GenericParamKind::Const { .. } => DefPathData::ValueNs(name),
188 self.create_def(param.id, def_path_data, param.ident.span);
190 visit::walk_generic_param(self, param);
193 fn visit_trait_item(&mut self, ti: &'a TraitItem) {
194 let def_data = match ti.node {
195 TraitItemKind::Method(..) | TraitItemKind::Const(..) =>
196 DefPathData::ValueNs(ti.ident.as_interned_str()),
197 TraitItemKind::Type(..) => {
198 DefPathData::TypeNs(ti.ident.as_interned_str())
200 TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id),
203 let def = self.create_def(ti.id, def_data, ti.span);
204 self.with_parent(def, |this| visit::walk_trait_item(this, ti));
207 fn visit_impl_item(&mut self, ii: &'a ImplItem) {
208 let def_data = match ii.node {
209 ImplItemKind::Method(MethodSig {
212 }, ref body) if header.asyncness.node.is_async() => {
213 return self.visit_async_fn(
223 ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
224 DefPathData::ValueNs(ii.ident.as_interned_str()),
225 ImplItemKind::Type(..) |
226 ImplItemKind::OpaqueTy(..) => {
227 DefPathData::TypeNs(ii.ident.as_interned_str())
229 ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
232 let def = self.create_def(ii.id, def_data, ii.span);
233 self.with_parent(def, |this| visit::walk_impl_item(this, ii));
236 fn visit_pat(&mut self, pat: &'a Pat) {
238 PatKind::Mac(..) => return self.visit_macro_invoc(pat.id),
239 _ => visit::walk_pat(self, pat),
243 fn visit_anon_const(&mut self, constant: &'a AnonConst) {
244 let def = self.create_def(constant.id,
245 DefPathData::AnonConst,
246 constant.value.span);
247 self.with_parent(def, |this| visit::walk_anon_const(this, constant));
250 fn visit_expr(&mut self, expr: &'a Expr) {
251 let parent_def = match expr.node {
252 ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id),
253 ExprKind::Closure(_, asyncness, ..) => {
254 // Async closures desugar to closures inside of closures, so
255 // we must create two defs.
256 let closure_def = self.create_def(expr.id, DefPathData::ClosureExpr, expr.span);
258 IsAsync::Async { closure_id, .. } =>
259 self.create_def(closure_id, DefPathData::ClosureExpr, expr.span),
260 IsAsync::NotAsync => closure_def,
263 ExprKind::Async(_, async_id, _) =>
264 self.create_def(async_id, DefPathData::ClosureExpr, expr.span),
265 _ => self.parent_def,
268 self.with_parent(parent_def, |this| visit::walk_expr(this, expr));
271 fn visit_ty(&mut self, ty: &'a Ty) {
273 TyKind::Mac(..) => return self.visit_macro_invoc(ty.id),
274 TyKind::ImplTrait(node_id, _) => {
275 self.create_def(node_id, DefPathData::ImplTrait, ty.span);
279 visit::walk_ty(self, ty);
282 fn visit_stmt(&mut self, stmt: &'a Stmt) {
284 StmtKind::Mac(..) => self.visit_macro_invoc(stmt.id),
285 _ => visit::walk_stmt(self, stmt),
289 fn visit_token(&mut self, t: Token) {
290 if let token::Interpolated(nt) = t.kind {
291 if let token::NtExpr(ref expr) = *nt {
292 if let ExprKind::Mac(..) = expr.node {
293 self.visit_macro_invoc(expr.id);