2019-11-23 15:19:57 +00:00
|
|
|
use log::debug;
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc::hir::map::definitions::*;
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::ast::*;
|
|
|
|
use rustc_ast::token::{self, Token};
|
2020-03-14 19:22:29 +00:00
|
|
|
use rustc_ast::visit::{self, FnKind};
|
2019-12-29 14:23:55 +00:00
|
|
|
use rustc_expand::expand::AstFragment;
|
2019-11-03 12:36:59 +00:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::hygiene::ExpnId;
|
2020-01-01 18:30:57 +00:00
|
|
|
use rustc_span::symbol::{kw, sym};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2016-04-14 00:14:03 +00:00
|
|
|
|
2019-11-23 15:19:57 +00:00
|
|
|
crate fn collect_definitions(
|
|
|
|
definitions: &mut Definitions,
|
|
|
|
fragment: &AstFragment,
|
|
|
|
expansion: ExpnId,
|
|
|
|
) {
|
|
|
|
let parent_def = definitions.invocation_parent(expansion);
|
|
|
|
fragment.visit_with(&mut DefCollector { definitions, parent_def, expansion });
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Creates `DefId`s for nodes in the AST.
|
2019-11-23 15:19:57 +00:00
|
|
|
struct DefCollector<'a> {
|
2016-09-14 09:55:20 +00:00
|
|
|
definitions: &'a mut Definitions,
|
2019-11-03 12:36:59 +00:00
|
|
|
parent_def: LocalDefId,
|
2019-07-15 22:04:05 +00:00
|
|
|
expansion: ExpnId,
|
2016-04-14 00:14:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 09:55:20 +00:00
|
|
|
impl<'a> DefCollector<'a> {
|
2019-11-03 12:36:59 +00:00
|
|
|
fn create_def(&mut self, node_id: NodeId, data: DefPathData, span: Span) -> LocalDefId {
|
2019-07-02 20:42:00 +00:00
|
|
|
let parent_def = self.parent_def;
|
2016-04-14 00:14:03 +00:00
|
|
|
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
|
2019-07-02 20:42:00 +00:00
|
|
|
self.definitions.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
|
2016-04-14 00:14:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 12:36:59 +00:00
|
|
|
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {
|
2019-07-02 20:42:00 +00:00
|
|
|
let orig_parent_def = std::mem::replace(&mut self.parent_def, parent_def);
|
2016-04-14 05:24:30 +00:00
|
|
|
f(self);
|
2019-07-02 20:42:00 +00:00
|
|
|
self.parent_def = orig_parent_def;
|
2016-04-14 05:24:30 +00:00
|
|
|
}
|
2016-05-02 20:11:19 +00:00
|
|
|
|
2019-09-14 14:36:27 +00:00
|
|
|
fn collect_field(&mut self, field: &'a StructField, index: Option<usize>) {
|
2019-12-22 22:42:04 +00:00
|
|
|
let index = |this: &Self| {
|
|
|
|
index.unwrap_or_else(|| {
|
|
|
|
let node_id = NodeId::placeholder_from_expn_id(this.expansion);
|
|
|
|
this.definitions.placeholder_field_index(node_id)
|
|
|
|
})
|
|
|
|
};
|
2019-11-23 11:16:38 +00:00
|
|
|
|
2019-09-14 14:36:27 +00:00
|
|
|
if field.is_placeholder {
|
2019-11-23 11:16:38 +00:00
|
|
|
self.definitions.set_placeholder_field_index(field.id, index(self));
|
2019-09-14 14:36:27 +00:00
|
|
|
self.visit_macro_invoc(field.id);
|
|
|
|
} else {
|
2019-11-23 11:16:38 +00:00
|
|
|
let name = field.ident.map_or_else(|| sym::integer(index(self)), |ident| ident.name);
|
2019-09-14 14:36:27 +00:00
|
|
|
let def = self.create_def(field.id, DefPathData::ValueNs(name), field.span);
|
|
|
|
self.with_parent(def, |this| visit::walk_struct_field(this, field));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 22:41:47 +00:00
|
|
|
fn visit_macro_invoc(&mut self, id: NodeId) {
|
2019-07-15 22:42:58 +00:00
|
|
|
self.definitions.set_invocation_parent(id.placeholder_to_expn_id(), self.parent_def);
|
2016-09-14 09:55:20 +00:00
|
|
|
}
|
2016-04-14 00:14:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
impl<'a> visit::Visitor<'a> for DefCollector<'a> {
|
|
|
|
fn visit_item(&mut self, i: &'a Item) {
|
2016-04-14 00:14:03 +00:00
|
|
|
debug!("visit_item: {:?}", i);
|
|
|
|
|
|
|
|
// Pick the def data. This need not be unique, but the more
|
2018-05-22 12:31:56 +00:00
|
|
|
// information we encapsulate into, the better
|
2019-11-07 12:33:37 +00:00
|
|
|
let def_data = match &i.kind {
|
2020-01-14 04:30:20 +00:00
|
|
|
ItemKind::Impl { .. } => DefPathData::Impl,
|
2019-05-11 16:08:09 +00:00
|
|
|
ItemKind::Mod(..) if i.ident.name == kw::Invalid => {
|
2016-09-14 09:55:20 +00:00
|
|
|
return visit::walk_item(self, i);
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
ItemKind::Mod(..)
|
|
|
|
| ItemKind::Trait(..)
|
|
|
|
| ItemKind::TraitAlias(..)
|
|
|
|
| ItemKind::Enum(..)
|
|
|
|
| ItemKind::Struct(..)
|
|
|
|
| ItemKind::Union(..)
|
|
|
|
| ItemKind::ExternCrate(..)
|
|
|
|
| ItemKind::ForeignMod(..)
|
|
|
|
| ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
|
|
|
|
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) => {
|
|
|
|
DefPathData::ValueNs(i.ident.name)
|
2018-06-06 22:50:59 +00:00
|
|
|
}
|
2019-10-21 03:25:08 +00:00
|
|
|
ItemKind::MacroDef(..) => DefPathData::MacroNs(i.ident.name),
|
2020-02-29 16:32:20 +00:00
|
|
|
ItemKind::MacCall(..) => return self.visit_macro_invoc(i.id),
|
2017-03-16 02:27:40 +00:00
|
|
|
ItemKind::GlobalAsm(..) => DefPathData::Misc,
|
2017-09-26 21:04:00 +00:00
|
|
|
ItemKind::Use(..) => {
|
|
|
|
return visit::walk_item(self, i);
|
2016-11-24 04:11:31 +00:00
|
|
|
}
|
2016-04-14 00:14:03 +00:00
|
|
|
};
|
2019-05-08 19:07:12 +00:00
|
|
|
let def = self.create_def(i.id, def_data, i.span);
|
2016-04-14 00:14:03 +00:00
|
|
|
|
2016-04-14 05:24:30 +00:00
|
|
|
self.with_parent(def, |this| {
|
2019-09-26 16:51:36 +00:00
|
|
|
match i.kind {
|
2016-08-29 05:04:31 +00:00
|
|
|
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
|
2019-03-21 22:38:50 +00:00
|
|
|
// If this is a unit or tuple-like struct, register the constructor.
|
|
|
|
if let Some(ctor_hir_id) = struct_def.ctor_id() {
|
2019-05-08 19:07:12 +00:00
|
|
|
this.create_def(ctor_hir_id, DefPathData::Ctor, i.span);
|
2016-04-14 05:24:30 +00:00
|
|
|
}
|
2016-04-14 00:14:03 +00:00
|
|
|
}
|
2016-04-14 05:24:30 +00:00
|
|
|
_ => {}
|
2016-04-14 00:14:03 +00:00
|
|
|
}
|
2016-04-14 05:24:30 +00:00
|
|
|
visit::walk_item(this, i);
|
|
|
|
});
|
2016-04-14 00:14:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 19:22:29 +00:00
|
|
|
fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
|
|
|
|
if let FnKind::Fn(_, _, sig, _, body) = fn_kind {
|
|
|
|
if let Async::Yes { closure_id, return_impl_trait_id, .. } = sig.header.asyncness {
|
|
|
|
self.create_def(return_impl_trait_id, DefPathData::ImplTrait, span);
|
|
|
|
|
|
|
|
// For async functions, we need to create their inner defs inside of a
|
|
|
|
// closure to match their desugared representation. Besides that,
|
|
|
|
// we must mirror everything that `visit::walk_fn` below does.
|
|
|
|
self.visit_fn_header(&sig.header);
|
|
|
|
visit::walk_fn_decl(self, &sig.decl);
|
|
|
|
if let Some(body) = body {
|
|
|
|
let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span);
|
|
|
|
self.with_parent(closure_def, |this| this.visit_block(body));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_fn(self, fn_kind, span);
|
|
|
|
}
|
|
|
|
|
2017-09-26 21:04:00 +00:00
|
|
|
fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
|
2019-05-08 19:07:12 +00:00
|
|
|
self.create_def(id, DefPathData::Misc, use_tree.span);
|
2017-09-26 21:04:00 +00:00
|
|
|
visit::walk_use_tree(self, use_tree, id);
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
|
2020-02-29 16:32:20 +00:00
|
|
|
if let ForeignItemKind::MacCall(_) = foreign_item.kind {
|
2018-05-17 18:28:50 +00:00
|
|
|
return self.visit_macro_invoc(foreign_item.id);
|
2018-03-11 02:16:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
let def = self.create_def(
|
|
|
|
foreign_item.id,
|
|
|
|
DefPathData::ValueNs(foreign_item.ident.name),
|
|
|
|
foreign_item.span,
|
|
|
|
);
|
2016-04-14 00:14:03 +00:00
|
|
|
|
2016-04-14 05:24:30 +00:00
|
|
|
self.with_parent(def, |this| {
|
|
|
|
visit::walk_foreign_item(this, foreign_item);
|
|
|
|
});
|
2016-04-14 00:14:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 16:54:40 +00:00
|
|
|
fn visit_variant(&mut self, v: &'a Variant) {
|
2019-09-09 12:26:25 +00:00
|
|
|
if v.is_placeholder {
|
|
|
|
return self.visit_macro_invoc(v.id);
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
let def = self.create_def(v.id, DefPathData::TypeNs(v.ident.name), v.span);
|
2019-03-21 22:38:50 +00:00
|
|
|
self.with_parent(def, |this| {
|
2019-08-14 00:40:21 +00:00
|
|
|
if let Some(ctor_hir_id) = v.data.ctor_id() {
|
2019-05-08 19:07:12 +00:00
|
|
|
this.create_def(ctor_hir_id, DefPathData::Ctor, v.span);
|
2019-03-21 22:38:50 +00:00
|
|
|
}
|
2019-08-24 16:54:40 +00:00
|
|
|
visit::walk_variant(this, v)
|
2019-03-21 22:38:50 +00:00
|
|
|
});
|
2018-05-17 18:28:50 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 16:54:40 +00:00
|
|
|
fn visit_variant_data(&mut self, data: &'a VariantData) {
|
2019-09-14 10:56:44 +00:00
|
|
|
// The assumption here is that non-`cfg` macro expansion cannot change field indices.
|
|
|
|
// It currently holds because only inert attributes are accepted on fields,
|
|
|
|
// and every such attribute expands into a single field after it's resolved.
|
2018-05-17 18:28:50 +00:00
|
|
|
for (index, field) in data.fields().iter().enumerate() {
|
2019-09-14 14:36:27 +00:00
|
|
|
self.collect_field(field, Some(index));
|
2018-05-17 18:28:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 19:07:09 +00:00
|
|
|
fn visit_generic_param(&mut self, param: &'a GenericParam) {
|
2019-09-09 12:26:25 +00:00
|
|
|
if param.is_placeholder {
|
|
|
|
self.visit_macro_invoc(param.id);
|
|
|
|
return;
|
|
|
|
}
|
2019-10-21 03:25:08 +00:00
|
|
|
let name = param.ident.name;
|
2018-05-26 18:16:21 +00:00
|
|
|
let def_path_data = match param.kind {
|
2019-05-03 19:45:36 +00:00
|
|
|
GenericParamKind::Lifetime { .. } => DefPathData::LifetimeNs(name),
|
2019-05-03 18:51:24 +00:00
|
|
|
GenericParamKind::Type { .. } => DefPathData::TypeNs(name),
|
2019-05-03 19:28:29 +00:00
|
|
|
GenericParamKind::Const { .. } => DefPathData::ValueNs(name),
|
2018-05-26 18:16:21 +00:00
|
|
|
};
|
2019-05-08 19:07:12 +00:00
|
|
|
self.create_def(param.id, def_path_data, param.ident.span);
|
2016-04-14 00:14:03 +00:00
|
|
|
|
2017-10-16 19:07:26 +00:00
|
|
|
visit::walk_generic_param(self, param);
|
2016-04-14 00:14:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) {
|
|
|
|
let def_data = match &i.kind {
|
2020-02-29 12:51:08 +00:00
|
|
|
AssocItemKind::Fn(..) | AssocItemKind::Const(..) => DefPathData::ValueNs(i.ident.name),
|
2020-01-29 23:18:54 +00:00
|
|
|
AssocItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
|
2020-02-29 16:32:20 +00:00
|
|
|
AssocItemKind::MacCall(..) => return self.visit_macro_invoc(i.id),
|
2016-04-14 00:14:03 +00:00
|
|
|
};
|
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
let def = self.create_def(i.id, def_data, i.span);
|
|
|
|
self.with_parent(def, |this| visit::walk_assoc_item(this, i, ctxt));
|
2016-04-14 00:14:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
fn visit_pat(&mut self, pat: &'a Pat) {
|
2019-09-26 15:18:31 +00:00
|
|
|
match pat.kind {
|
2020-02-29 16:32:20 +00:00
|
|
|
PatKind::MacCall(..) => return self.visit_macro_invoc(pat.id),
|
2017-04-29 11:39:47 +00:00
|
|
|
_ => visit::walk_pat(self, pat),
|
2016-04-14 00:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 18:28:50 +00:00
|
|
|
fn visit_anon_const(&mut self, constant: &'a AnonConst) {
|
2019-12-22 22:42:04 +00:00
|
|
|
let def = self.create_def(constant.id, DefPathData::AnonConst, constant.value.span);
|
2018-05-17 18:28:50 +00:00
|
|
|
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'a Expr) {
|
2019-09-26 13:39:48 +00:00
|
|
|
let parent_def = match expr.kind {
|
2020-02-29 16:32:20 +00:00
|
|
|
ExprKind::MacCall(..) => return self.visit_macro_invoc(expr.id),
|
2019-05-29 01:10:49 +00:00
|
|
|
ExprKind::Closure(_, asyncness, ..) => {
|
2018-06-06 22:50:59 +00:00
|
|
|
// Async closures desugar to closures inside of closures, so
|
|
|
|
// we must create two defs.
|
2019-07-02 20:42:00 +00:00
|
|
|
let closure_def = self.create_def(expr.id, DefPathData::ClosureExpr, expr.span);
|
|
|
|
match asyncness {
|
2020-01-30 04:31:04 +00:00
|
|
|
Async::Yes { closure_id, .. } => {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.create_def(closure_id, DefPathData::ClosureExpr, expr.span)
|
|
|
|
}
|
2020-01-30 04:31:04 +00:00
|
|
|
Async::No => closure_def,
|
2018-06-06 22:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
ExprKind::Async(_, async_id, _) => {
|
|
|
|
self.create_def(async_id, DefPathData::ClosureExpr, expr.span)
|
|
|
|
}
|
2019-07-02 20:42:00 +00:00
|
|
|
_ => self.parent_def,
|
2018-06-06 22:50:59 +00:00
|
|
|
};
|
2016-04-14 00:14:03 +00:00
|
|
|
|
2019-07-02 20:42:00 +00:00
|
|
|
self.with_parent(parent_def, |this| visit::walk_expr(this, expr));
|
2016-04-14 00:14:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
fn visit_ty(&mut self, ty: &'a Ty) {
|
2019-09-26 16:25:31 +00:00
|
|
|
match ty.kind {
|
2020-02-29 16:32:20 +00:00
|
|
|
TyKind::MacCall(..) => return self.visit_macro_invoc(ty.id),
|
2018-06-20 08:59:24 +00:00
|
|
|
TyKind::ImplTrait(node_id, _) => {
|
2019-05-08 19:07:12 +00:00
|
|
|
self.create_def(node_id, DefPathData::ImplTrait, ty.span);
|
2018-06-20 08:59:24 +00:00
|
|
|
}
|
2016-09-14 09:55:20 +00:00
|
|
|
_ => {}
|
2016-07-22 15:56:22 +00:00
|
|
|
}
|
2016-05-02 20:11:19 +00:00
|
|
|
visit::walk_ty(self, ty);
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
2019-09-26 16:34:50 +00:00
|
|
|
match stmt.kind {
|
2020-02-29 16:32:20 +00:00
|
|
|
StmtKind::MacCall(..) => self.visit_macro_invoc(stmt.id),
|
2016-09-14 09:55:20 +00:00
|
|
|
_ => visit::walk_stmt(self, stmt),
|
|
|
|
}
|
|
|
|
}
|
2017-10-23 08:22:28 +00:00
|
|
|
|
2019-06-05 10:54:54 +00:00
|
|
|
fn visit_token(&mut self, t: Token) {
|
|
|
|
if let token::Interpolated(nt) = t.kind {
|
2019-02-14 22:10:02 +00:00
|
|
|
if let token::NtExpr(ref expr) = *nt {
|
2020-02-29 16:32:20 +00:00
|
|
|
if let ExprKind::MacCall(..) = expr.kind {
|
2018-09-12 10:31:11 +00:00
|
|
|
self.visit_macro_invoc(expr.id);
|
2017-10-23 08:22:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-09 12:26:25 +00:00
|
|
|
|
|
|
|
fn visit_arm(&mut self, arm: &'a Arm) {
|
2019-12-22 22:42:04 +00:00
|
|
|
if arm.is_placeholder { self.visit_macro_invoc(arm.id) } else { visit::walk_arm(self, arm) }
|
2019-09-09 12:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_field(&mut self, f: &'a Field) {
|
2019-12-22 22:42:04 +00:00
|
|
|
if f.is_placeholder { self.visit_macro_invoc(f.id) } else { visit::walk_field(self, f) }
|
2019-09-09 12:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_field_pattern(&mut self, fp: &'a FieldPat) {
|
|
|
|
if fp.is_placeholder {
|
|
|
|
self.visit_macro_invoc(fp.id)
|
|
|
|
} else {
|
|
|
|
visit::walk_field_pattern(self, fp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_param(&mut self, p: &'a Param) {
|
2019-12-22 22:42:04 +00:00
|
|
|
if p.is_placeholder { self.visit_macro_invoc(p.id) } else { visit::walk_param(self, p) }
|
2019-09-09 12:26:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 10:56:44 +00:00
|
|
|
// This method is called only when we are visiting an individual field
|
|
|
|
// after expanding an attribute on it.
|
2019-09-14 14:36:27 +00:00
|
|
|
fn visit_struct_field(&mut self, field: &'a StructField) {
|
|
|
|
self.collect_field(field, None);
|
2019-09-09 12:26:25 +00:00
|
|
|
}
|
2018-09-06 21:18:39 +00:00
|
|
|
}
|