2 use rustc::hir::map::definitions::*;
3 use rustc_expand::expand::AstFragment;
4 use rustc_hir::def_id::DefIndex;
5 use rustc_span::hygiene::ExpnId;
6 use rustc_span::symbol::{kw, sym};
9 use syntax::token::{self, Token};
12 crate fn collect_definitions(
13 definitions: &mut Definitions,
14 fragment: &AstFragment,
17 let parent_def = definitions.invocation_parent(expansion);
18 fragment.visit_with(&mut DefCollector { definitions, parent_def, expansion });
21 /// Creates `DefId`s for nodes in the AST.
22 struct DefCollector<'a> {
23 definitions: &'a mut Definitions,
28 impl<'a> DefCollector<'a> {
29 fn create_def(&mut self, node_id: NodeId, data: DefPathData, span: Span) -> DefIndex {
30 let parent_def = self.parent_def;
31 debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
32 self.definitions.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
35 fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
36 let orig_parent_def = std::mem::replace(&mut self.parent_def, parent_def);
38 self.parent_def = orig_parent_def;
47 generics: &'a Generics,
49 body: Option<&'a Block>,
51 let (closure_id, return_impl_trait_id) = match header.asyncness {
52 Async::Yes { span: _, closure_id, return_impl_trait_id } => {
53 (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);
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(closure_id, DefPathData::ClosureExpr, span);
69 this.with_parent(closure_def, |this| {
70 if let Some(body) = body {
71 visit::walk_block(this, body);
77 fn collect_field(&mut self, field: &'a StructField, index: Option<usize>) {
78 let index = |this: &Self| {
79 index.unwrap_or_else(|| {
80 let node_id = NodeId::placeholder_from_expn_id(this.expansion);
81 this.definitions.placeholder_field_index(node_id)
85 if field.is_placeholder {
86 self.definitions.set_placeholder_field_index(field.id, index(self));
87 self.visit_macro_invoc(field.id);
89 let name = field.ident.map_or_else(|| sym::integer(index(self)), |ident| ident.name);
90 let def = self.create_def(field.id, DefPathData::ValueNs(name), field.span);
91 self.with_parent(def, |this| visit::walk_struct_field(this, field));
95 fn visit_macro_invoc(&mut self, id: NodeId) {
96 self.definitions.set_invocation_parent(id.placeholder_to_expn_id(), self.parent_def);
100 impl<'a> visit::Visitor<'a> for DefCollector<'a> {
101 fn visit_item(&mut self, i: &'a Item) {
102 debug!("visit_item: {:?}", i);
104 // Pick the def data. This need not be unique, but the more
105 // information we encapsulate into, the better
106 let def_data = match &i.kind {
107 ItemKind::Impl { .. } => DefPathData::Impl,
108 ItemKind::Mod(..) if i.ident.name == kw::Invalid => {
109 return visit::walk_item(self, i);
112 | ItemKind::Trait(..)
113 | ItemKind::TraitAlias(..)
115 | ItemKind::Struct(..)
116 | ItemKind::Union(..)
117 | ItemKind::ExternCrate(..)
118 | ItemKind::ForeignMod(..)
119 | ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
120 ItemKind::Fn(_, sig, generics, body) if sig.header.asyncness.is_async() => {
121 return self.visit_async_fn(
131 ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) => {
132 DefPathData::ValueNs(i.ident.name)
134 ItemKind::MacroDef(..) => DefPathData::MacroNs(i.ident.name),
135 ItemKind::Mac(..) => return self.visit_macro_invoc(i.id),
136 ItemKind::GlobalAsm(..) => DefPathData::Misc,
137 ItemKind::Use(..) => {
138 return visit::walk_item(self, i);
141 let def = self.create_def(i.id, def_data, i.span);
143 self.with_parent(def, |this| {
145 ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
146 // If this is a unit or tuple-like struct, register the constructor.
147 if let Some(ctor_hir_id) = struct_def.ctor_id() {
148 this.create_def(ctor_hir_id, DefPathData::Ctor, i.span);
153 visit::walk_item(this, i);
157 fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
158 self.create_def(id, DefPathData::Misc, use_tree.span);
159 visit::walk_use_tree(self, use_tree, id);
162 fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
163 if let ForeignItemKind::Macro(_) = foreign_item.kind {
164 return self.visit_macro_invoc(foreign_item.id);
167 let def = self.create_def(
169 DefPathData::ValueNs(foreign_item.ident.name),
173 self.with_parent(def, |this| {
174 visit::walk_foreign_item(this, foreign_item);
178 fn visit_variant(&mut self, v: &'a Variant) {
179 if v.is_placeholder {
180 return self.visit_macro_invoc(v.id);
182 let def = self.create_def(v.id, DefPathData::TypeNs(v.ident.name), v.span);
183 self.with_parent(def, |this| {
184 if let Some(ctor_hir_id) = v.data.ctor_id() {
185 this.create_def(ctor_hir_id, DefPathData::Ctor, v.span);
187 visit::walk_variant(this, v)
191 fn visit_variant_data(&mut self, data: &'a VariantData) {
192 // The assumption here is that non-`cfg` macro expansion cannot change field indices.
193 // It currently holds because only inert attributes are accepted on fields,
194 // and every such attribute expands into a single field after it's resolved.
195 for (index, field) in data.fields().iter().enumerate() {
196 self.collect_field(field, Some(index));
200 fn visit_generic_param(&mut self, param: &'a GenericParam) {
201 if param.is_placeholder {
202 self.visit_macro_invoc(param.id);
205 let name = param.ident.name;
206 let def_path_data = match param.kind {
207 GenericParamKind::Lifetime { .. } => DefPathData::LifetimeNs(name),
208 GenericParamKind::Type { .. } => DefPathData::TypeNs(name),
209 GenericParamKind::Const { .. } => DefPathData::ValueNs(name),
211 self.create_def(param.id, def_path_data, param.ident.span);
213 visit::walk_generic_param(self, param);
216 fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) {
217 let def_data = match &i.kind {
218 AssocItemKind::Fn(_, FnSig { header, decl }, generics, body)
219 if header.asyncness.is_async() =>
221 return self.visit_async_fn(
231 AssocItemKind::Fn(..) | AssocItemKind::Const(..) | AssocItemKind::Static(..) => {
232 DefPathData::ValueNs(i.ident.name)
234 AssocItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
235 AssocItemKind::Macro(..) => return self.visit_macro_invoc(i.id),
238 let def = self.create_def(i.id, def_data, i.span);
239 self.with_parent(def, |this| visit::walk_assoc_item(this, i, ctxt));
242 fn visit_pat(&mut self, pat: &'a Pat) {
244 PatKind::Mac(..) => return self.visit_macro_invoc(pat.id),
245 _ => visit::walk_pat(self, pat),
249 fn visit_anon_const(&mut self, constant: &'a AnonConst) {
250 let def = self.create_def(constant.id, DefPathData::AnonConst, constant.value.span);
251 self.with_parent(def, |this| visit::walk_anon_const(this, constant));
254 fn visit_expr(&mut self, expr: &'a Expr) {
255 let parent_def = match expr.kind {
256 ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id),
257 ExprKind::Closure(_, asyncness, ..) => {
258 // Async closures desugar to closures inside of closures, so
259 // we must create two defs.
260 let closure_def = self.create_def(expr.id, DefPathData::ClosureExpr, expr.span);
262 Async::Yes { closure_id, .. } => {
263 self.create_def(closure_id, DefPathData::ClosureExpr, expr.span)
265 Async::No => closure_def,
268 ExprKind::Async(_, async_id, _) => {
269 self.create_def(async_id, DefPathData::ClosureExpr, expr.span)
271 _ => self.parent_def,
274 self.with_parent(parent_def, |this| visit::walk_expr(this, expr));
277 fn visit_ty(&mut self, ty: &'a Ty) {
279 TyKind::Mac(..) => return self.visit_macro_invoc(ty.id),
280 TyKind::ImplTrait(node_id, _) => {
281 self.create_def(node_id, DefPathData::ImplTrait, ty.span);
285 visit::walk_ty(self, ty);
288 fn visit_stmt(&mut self, stmt: &'a Stmt) {
290 StmtKind::Mac(..) => self.visit_macro_invoc(stmt.id),
291 _ => visit::walk_stmt(self, stmt),
295 fn visit_token(&mut self, t: Token) {
296 if let token::Interpolated(nt) = t.kind {
297 if let token::NtExpr(ref expr) = *nt {
298 if let ExprKind::Mac(..) = expr.kind {
299 self.visit_macro_invoc(expr.id);
305 fn visit_arm(&mut self, arm: &'a Arm) {
306 if arm.is_placeholder { self.visit_macro_invoc(arm.id) } else { visit::walk_arm(self, arm) }
309 fn visit_field(&mut self, f: &'a Field) {
310 if f.is_placeholder { self.visit_macro_invoc(f.id) } else { visit::walk_field(self, f) }
313 fn visit_field_pattern(&mut self, fp: &'a FieldPat) {
314 if fp.is_placeholder {
315 self.visit_macro_invoc(fp.id)
317 visit::walk_field_pattern(self, fp)
321 fn visit_param(&mut self, p: &'a Param) {
322 if p.is_placeholder { self.visit_macro_invoc(p.id) } else { visit::walk_param(self, p) }
325 // This method is called only when we are visiting an individual field
326 // after expanding an attribute on it.
327 fn visit_struct_field(&mut self, field: &'a StructField) {
328 self.collect_field(field, None);