1 // Copyright 2012-2014 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 //! Reduced graph building
13 //! Here we build the "reduced graph": the graph of the module tree without
14 //! any imports resolved.
16 use macros::{InvocationData, LegacyScope};
17 use resolve_imports::ImportDirective;
18 use resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
19 use {Module, ModuleData, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
20 use {Resolver, ResolverArenas};
21 use Namespace::{self, TypeNS, ValueNS, MacroNS};
22 use {resolve_error, resolve_struct_error, ResolutionError};
24 use rustc::middle::cstore::LoadedMacro;
25 use rustc::hir::def::*;
26 use rustc::hir::def_id::{BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
32 use syntax::ast::{Name, Ident};
35 use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind};
36 use syntax::ast::{Mutability, StmtKind, TraitItem, TraitItemKind};
37 use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
38 use syntax::ext::base::SyntaxExtension;
39 use syntax::ext::base::Determinacy::Undetermined;
40 use syntax::ext::hygiene::Mark;
41 use syntax::ext::tt::macro_rules;
42 use syntax::parse::token;
43 use syntax::symbol::keywords;
44 use syntax::visit::{self, Visitor};
46 use syntax_pos::{Span, DUMMY_SP};
48 impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
49 fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
50 arenas.alloc_name_binding(NameBinding {
51 kind: NameBindingKind::Module(self.0),
59 impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark) {
60 fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
61 arenas.alloc_name_binding(NameBinding {
62 kind: NameBindingKind::Def(self.0),
70 #[derive(Default, PartialEq, Eq)]
71 struct LegacyMacroImports {
72 import_all: Option<Span>,
73 imports: Vec<(Name, Span)>,
74 reexports: Vec<(Name, Span)>,
77 impl<'a> Resolver<'a> {
78 /// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
79 /// otherwise, reports an error.
80 pub fn define<T>(&mut self, parent: Module<'a>, ident: Ident, ns: Namespace, def: T)
81 where T: ToNameBinding<'a>,
83 let binding = def.to_name_binding(self.arenas);
84 if let Err(old_binding) = self.try_define(parent, ident, ns, binding) {
85 self.report_conflict(parent, ident, ns, old_binding, &binding);
89 fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
90 // If any statements are items, we need to create an anonymous module
91 block.stmts.iter().any(|statement| match statement.node {
92 StmtKind::Item(_) | StmtKind::Mac(_) => true,
97 fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Name>) {
98 if !field_names.is_empty() {
99 self.field_names.insert(def_id, field_names);
103 /// Constructs the reduced graph for one item.
104 fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
105 let parent = self.current_module;
106 let ident = item.ident;
108 let vis = self.resolve_visibility(&item.vis);
111 ItemKind::Use(ref view_path) => {
112 // Extract and intern the module part of the path. For
113 // globs and lists, the path is found directly in the AST;
114 // for simple paths we have to munge the path a little.
115 let mut module_path: Vec<_> = match view_path.node {
116 ViewPathSimple(_, ref full_path) => {
122 .map(|seg| seg.identifier)
126 ViewPathGlob(ref module_ident_path) |
127 ViewPathList(ref module_ident_path, _) => {
128 module_ident_path.segments
130 .map(|seg| seg.identifier)
135 // This can be removed once warning cycle #36888 is complete.
136 if module_path.len() >= 2 && module_path[0].name == keywords::CrateRoot.name() &&
137 token::Ident(module_path[1]).is_path_segment_keyword() {
138 module_path.remove(0);
141 // Build up the import directives.
142 let is_prelude = attr::contains_name(&item.attrs, "prelude_import");
144 match view_path.node {
145 ViewPathSimple(mut binding, ref full_path) => {
146 let mut source = full_path.segments.last().unwrap().identifier;
147 let source_name = source.name;
148 if source_name == "mod" || source_name == "self" {
151 ResolutionError::SelfImportsOnlyAllowedWithin);
152 } else if source_name == "$crate" && full_path.segments.len() == 1 {
153 let crate_root = self.resolve_crate_root(source.ctxt);
154 let crate_name = match crate_root.kind {
155 ModuleKind::Def(_, name) => name,
156 ModuleKind::Block(..) => unreachable!(),
158 source.name = crate_name;
159 if binding.name == "$crate" {
160 binding.name = crate_name;
163 self.session.struct_span_warn(item.span, "`$crate` may not be imported")
164 .note("`use $crate;` was erroneously allowed and \
165 will become a hard error in a future release")
169 let subclass = SingleImport {
172 result: self.per_ns(|_, _| Cell::new(Err(Undetermined))),
175 self.add_import_directive(
176 module_path, subclass, view_path.span, item.id, vis, expansion,
179 ViewPathList(_, ref source_items) => {
180 // Make sure there's at most one `mod` import in the list.
181 let mod_spans = source_items.iter().filter_map(|item| {
182 if item.node.name.name == keywords::SelfValue.name() {
187 }).collect::<Vec<Span>>();
189 if mod_spans.len() > 1 {
190 let mut e = resolve_struct_error(self,
192 ResolutionError::SelfImportCanOnlyAppearOnceInTheList);
193 for other_span in mod_spans.iter().skip(1) {
194 e.span_note(*other_span, "another `self` import appears here");
199 for source_item in source_items {
200 let node = source_item.node;
201 let (module_path, ident, rename, type_ns_only) = {
202 if node.name.name != keywords::SelfValue.name() {
203 let rename = node.rename.unwrap_or(node.name);
204 (module_path.clone(), node.name, rename, false)
206 let ident = *module_path.last().unwrap();
207 if ident.name == keywords::CrateRoot.name() {
212 SelfImportOnlyInImportListWithNonEmptyPrefix
216 let module_path = module_path.split_last().unwrap().1;
217 let rename = node.rename.unwrap_or(ident);
218 (module_path.to_vec(), ident, rename, true)
221 let subclass = SingleImport {
224 result: self.per_ns(|_, _| Cell::new(Err(Undetermined))),
225 type_ns_only: type_ns_only,
227 let id = source_item.node.id;
228 self.add_import_directive(
229 module_path, subclass, source_item.span, id, vis, expansion,
234 let subclass = GlobImport {
235 is_prelude: is_prelude,
236 max_vis: Cell::new(ty::Visibility::Invisible),
238 self.add_import_directive(
239 module_path, subclass, view_path.span, item.id, vis, expansion,
245 ItemKind::ExternCrate(_) => {
246 self.crate_loader.process_item(item, &self.definitions);
248 // n.b. we don't need to look at the path option here, because cstore already did
249 let crate_id = self.session.cstore.extern_mod_stmt_cnum(item.id).unwrap();
251 self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
252 self.populate_module_if_necessary(module);
253 let used = self.process_legacy_macro_imports(item, module, expansion);
255 (module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
256 let directive = self.arenas.alloc_import_directive(ImportDirective {
259 imported_module: Cell::new(Some(module)),
260 subclass: ImportDirectiveSubclass::ExternCrate,
262 module_path: Vec::new(),
264 expansion: expansion,
265 used: Cell::new(used),
267 self.potentially_unused_imports.push(directive);
268 let imported_binding = self.import(binding, directive);
269 self.define(parent, ident, TypeNS, imported_binding);
272 ItemKind::GlobalAsm(..) => {}
274 ItemKind::Mod(..) if item.ident == keywords::Invalid.ident() => {} // Crate root
276 ItemKind::Mod(..) => {
277 let def_id = self.definitions.local_def_id(item.id);
278 let module_kind = ModuleKind::Def(Def::Mod(def_id), ident.name);
279 let module = self.arenas.alloc_module(ModuleData {
280 no_implicit_prelude: parent.no_implicit_prelude || {
281 attr::contains_name(&item.attrs, "no_implicit_prelude")
283 ..ModuleData::new(Some(parent), module_kind, def_id, expansion, item.span)
285 self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
286 self.module_map.insert(def_id, module);
288 // Descend into the module.
289 self.current_module = module;
292 ItemKind::ForeignMod(..) => self.crate_loader.process_item(item, &self.definitions),
294 // These items live in the value namespace.
295 ItemKind::Static(_, m, _) => {
296 let mutbl = m == Mutability::Mutable;
297 let def = Def::Static(self.definitions.local_def_id(item.id), mutbl);
298 self.define(parent, ident, ValueNS, (def, vis, sp, expansion));
300 ItemKind::Const(..) => {
301 let def = Def::Const(self.definitions.local_def_id(item.id));
302 self.define(parent, ident, ValueNS, (def, vis, sp, expansion));
304 ItemKind::Fn(..) => {
305 let def = Def::Fn(self.definitions.local_def_id(item.id));
306 self.define(parent, ident, ValueNS, (def, vis, sp, expansion));
309 // These items live in the type namespace.
310 ItemKind::Ty(..) => {
311 let def = Def::TyAlias(self.definitions.local_def_id(item.id));
312 self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
315 ItemKind::Enum(ref enum_definition, _) => {
316 let def = Def::Enum(self.definitions.local_def_id(item.id));
317 let module_kind = ModuleKind::Def(def, ident.name);
318 let module = self.new_module(parent,
320 parent.normal_ancestor_id,
323 self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
325 for variant in &(*enum_definition).variants {
326 self.build_reduced_graph_for_variant(variant, module, vis, expansion);
330 // These items live in both the type and value namespaces.
331 ItemKind::Struct(ref struct_def, _) => {
332 // Define a name in the type namespace.
333 let def = Def::Struct(self.definitions.local_def_id(item.id));
334 self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
336 // Record field names for error reporting.
337 let mut ctor_vis = vis;
338 let field_names = struct_def.fields().iter().filter_map(|field| {
339 let field_vis = self.resolve_visibility(&field.vis);
340 if ctor_vis.is_at_least(field_vis, &*self) {
341 ctor_vis = field_vis;
343 field.ident.map(|ident| ident.name)
345 let item_def_id = self.definitions.local_def_id(item.id);
346 self.insert_field_names(item_def_id, field_names);
348 // If this is a tuple or unit struct, define a name
349 // in the value namespace as well.
350 if !struct_def.is_struct() {
351 let ctor_def = Def::StructCtor(self.definitions.local_def_id(struct_def.id()),
352 CtorKind::from_ast(struct_def));
353 self.define(parent, ident, ValueNS, (ctor_def, ctor_vis, sp, expansion));
354 self.struct_constructors.insert(def.def_id(), (ctor_def, ctor_vis));
358 ItemKind::Union(ref vdata, _) => {
359 let def = Def::Union(self.definitions.local_def_id(item.id));
360 self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
362 // Record field names for error reporting.
363 let field_names = vdata.fields().iter().filter_map(|field| {
364 self.resolve_visibility(&field.vis);
365 field.ident.map(|ident| ident.name)
367 let item_def_id = self.definitions.local_def_id(item.id);
368 self.insert_field_names(item_def_id, field_names);
371 ItemKind::DefaultImpl(..) | ItemKind::Impl(..) => {}
373 ItemKind::Trait(..) => {
374 let def_id = self.definitions.local_def_id(item.id);
376 // Add all the items within to a new module.
377 let module_kind = ModuleKind::Def(Def::Trait(def_id), ident.name);
378 let module = self.new_module(parent,
380 parent.normal_ancestor_id,
383 self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
384 self.current_module = module;
386 ItemKind::MacroDef(..) | ItemKind::Mac(_) => unreachable!(),
390 // Constructs the reduced graph for one variant. Variants exist in the
391 // type and value namespaces.
392 fn build_reduced_graph_for_variant(&mut self,
397 let ident = variant.node.name;
398 let def_id = self.definitions.local_def_id(variant.node.data.id());
400 // Define a name in the type namespace.
401 let def = Def::Variant(def_id);
402 self.define(parent, ident, TypeNS, (def, vis, variant.span, expansion));
404 // Define a constructor name in the value namespace.
405 // Braced variants, unlike structs, generate unusable names in
406 // value namespace, they are reserved for possible future use.
407 let ctor_kind = CtorKind::from_ast(&variant.node.data);
408 let ctor_def = Def::VariantCtor(def_id, ctor_kind);
409 self.define(parent, ident, ValueNS, (ctor_def, vis, variant.span, expansion));
412 /// Constructs the reduced graph for one foreign item.
413 fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem, expansion: Mark) {
414 let def = match item.node {
415 ForeignItemKind::Fn(..) => {
416 Def::Fn(self.definitions.local_def_id(item.id))
418 ForeignItemKind::Static(_, m) => {
419 Def::Static(self.definitions.local_def_id(item.id), m)
422 let parent = self.current_module;
423 let vis = self.resolve_visibility(&item.vis);
424 self.define(parent, item.ident, ValueNS, (def, vis, item.span, expansion));
427 fn build_reduced_graph_for_block(&mut self, block: &Block, expansion: Mark) {
428 let parent = self.current_module;
429 if self.block_needs_anonymous_module(block) {
430 let module = self.new_module(parent,
431 ModuleKind::Block(block.id),
432 parent.normal_ancestor_id,
435 self.block_map.insert(block.id, module);
436 self.current_module = module; // Descend into the block.
440 /// Builds the reduced graph for a single item in an external crate.
441 fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'a>, child: Export) {
442 let ident = child.ident;
444 let def_id = def.def_id();
445 let vis = self.session.cstore.visibility(def_id);
446 let span = child.span;
447 let expansion = Mark::root(); // FIXME(jseyfried) intercrate hygiene
449 Def::Mod(..) | Def::Enum(..) => {
450 let module = self.new_module(parent,
451 ModuleKind::Def(def, ident.name),
455 self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
457 Def::Variant(..) | Def::TyAlias(..) => {
458 self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
460 Def::Fn(..) | Def::Static(..) | Def::Const(..) | Def::VariantCtor(..) => {
461 self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
463 Def::StructCtor(..) => {
464 self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
466 if let Some(struct_def_id) =
467 self.session.cstore.def_key(def_id).parent
468 .map(|index| DefId { krate: def_id.krate, index: index }) {
469 self.struct_constructors.insert(struct_def_id, (def, vis));
473 let module_kind = ModuleKind::Def(def, ident.name);
474 let module = self.new_module(parent,
476 parent.normal_ancestor_id,
479 self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
481 for child in self.session.cstore.item_children(def_id, self.session) {
482 let ns = if let Def::AssociatedTy(..) = child.def { TypeNS } else { ValueNS };
483 self.define(module, child.ident, ns,
484 (child.def, ty::Visibility::Public, DUMMY_SP, expansion));
486 if self.session.cstore.associated_item_cloned(child.def.def_id())
487 .method_has_self_argument {
488 self.has_self.insert(child.def.def_id());
491 module.populated.set(true);
493 Def::Struct(..) | Def::Union(..) => {
494 self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
496 // Record field names for error reporting.
497 let field_names = self.session.cstore.struct_field_names(def_id);
498 self.insert_field_names(def_id, field_names);
501 self.define(parent, ident, MacroNS, (def, vis, DUMMY_SP, expansion));
503 _ => bug!("unexpected definition: {:?}", def)
507 pub fn get_module(&mut self, def_id: DefId) -> Module<'a> {
508 if def_id.krate == LOCAL_CRATE {
509 return self.module_map[&def_id]
512 let macros_only = self.session.cstore.dep_kind(def_id.krate).macros_only();
513 if let Some(&module) = self.extern_module_map.get(&(def_id, macros_only)) {
517 let (name, parent) = if def_id.index == CRATE_DEF_INDEX {
518 (self.session.cstore.crate_name(def_id.krate), None)
520 let def_key = self.session.cstore.def_key(def_id);
521 (def_key.disambiguated_data.data.get_opt_name().unwrap(),
522 Some(self.get_module(DefId { index: def_key.parent.unwrap(), ..def_id })))
525 let kind = ModuleKind::Def(Def::Mod(def_id), name);
527 self.arenas.alloc_module(ModuleData::new(parent, kind, def_id, Mark::root(), DUMMY_SP));
528 self.extern_module_map.insert((def_id, macros_only), module);
532 pub fn macro_def_scope(&mut self, expansion: Mark) -> Module<'a> {
533 let def_id = self.macro_defs[&expansion];
534 if let Some(id) = self.definitions.as_local_node_id(def_id) {
535 self.local_macro_def_scopes[&id]
536 } else if def_id.krate == BUILTIN_MACROS_CRATE {
537 // FIXME(jseyfried): This happens when `include!()`ing a `$crate::` path, c.f, #40469.
540 let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();
541 self.get_module(module_def_id)
545 pub fn get_macro(&mut self, def: Def) -> Rc<SyntaxExtension> {
546 let def_id = match def {
547 Def::Macro(def_id, ..) => def_id,
548 _ => panic!("Expected Def::Macro(..)"),
550 if let Some(ext) = self.macro_map.get(&def_id) {
554 let macro_def = match self.session.cstore.load_macro(def_id, &self.session) {
555 LoadedMacro::MacroDef(macro_def) => macro_def,
556 LoadedMacro::ProcMacro(ext) => return ext,
559 let ext = Rc::new(macro_rules::compile(&self.session.parse_sess,
560 &self.session.features,
562 self.macro_map.insert(def_id, ext.clone());
566 /// Ensures that the reduced graph rooted at the given external module
567 /// is built, building it if it is not.
568 pub fn populate_module_if_necessary(&mut self, module: Module<'a>) {
569 if module.populated.get() { return }
570 for child in self.session.cstore.item_children(module.def_id().unwrap(), self.session) {
571 self.build_reduced_graph_for_external_crate_def(module, child);
573 module.populated.set(true)
576 fn legacy_import_macro(&mut self,
578 binding: &'a NameBinding<'a>,
580 allow_shadowing: bool) {
581 if self.global_macros.insert(name, binding).is_some() && !allow_shadowing {
582 let msg = format!("`{}` is already in scope", name);
584 "macro-expanded `#[macro_use]`s may not shadow existing macros (see RFC 1560)";
585 self.session.struct_span_err(span, &msg).note(note).emit();
589 // This returns true if we should consider the underlying `extern crate` to be used.
590 fn process_legacy_macro_imports(&mut self, item: &Item, module: Module<'a>, expansion: Mark)
592 let allow_shadowing = expansion == Mark::root();
593 let legacy_imports = self.legacy_macro_imports(&item.attrs);
594 let mut used = legacy_imports != LegacyMacroImports::default();
596 // `#[macro_use]` and `#[macro_reexport]` are only allowed at the crate root.
597 if self.current_module.parent.is_some() && used {
598 span_err!(self.session, item.span, E0468,
599 "an `extern crate` loading macros must be at the crate root");
600 } else if !self.use_extern_macros && !used &&
601 self.session.cstore.dep_kind(module.def_id().unwrap().krate).macros_only() {
602 let msg = "proc macro crates and `#[no_link]` crates have no effect without \
604 self.session.span_warn(item.span, msg);
605 used = true; // Avoid the normal unused extern crate warning
608 let (graph_root, arenas) = (self.graph_root, self.arenas);
609 let macro_use_directive = |span| arenas.alloc_import_directive(ImportDirective {
612 imported_module: Cell::new(Some(module)),
613 subclass: ImportDirectiveSubclass::MacroUse,
615 module_path: Vec::new(),
616 vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),
617 expansion: expansion,
618 used: Cell::new(false),
621 if let Some(span) = legacy_imports.import_all {
622 let directive = macro_use_directive(span);
623 self.potentially_unused_imports.push(directive);
624 module.for_each_child(|ident, ns, binding| if ns == MacroNS {
625 let imported_binding = self.import(binding, directive);
626 self.legacy_import_macro(ident.name, imported_binding, span, allow_shadowing);
629 for (name, span) in legacy_imports.imports {
630 let ident = Ident::with_empty_ctxt(name);
631 let result = self.resolve_ident_in_module(module, ident, MacroNS,
633 if let Ok(binding) = result {
634 let directive = macro_use_directive(span);
635 self.potentially_unused_imports.push(directive);
636 let imported_binding = self.import(binding, directive);
637 self.legacy_import_macro(name, imported_binding, span, allow_shadowing);
639 span_err!(self.session, span, E0469, "imported macro not found");
643 for (name, span) in legacy_imports.reexports {
644 self.session.cstore.export_macros(module.def_id().unwrap().krate);
645 let ident = Ident::with_empty_ctxt(name);
646 let result = self.resolve_ident_in_module(module, ident, MacroNS, false, false, span);
647 if let Ok(binding) = result {
648 self.macro_exports.push(Export { ident: ident, def: binding.def(), span: span });
650 span_err!(self.session, span, E0470, "reexported macro not found");
656 // does this attribute list contain "macro_use"?
657 fn contains_macro_use(&mut self, attrs: &[ast::Attribute]) -> bool {
659 if attr.check_name("macro_escape") {
660 let msg = "macro_escape is a deprecated synonym for macro_use";
661 let mut err = self.session.struct_span_warn(attr.span, msg);
662 if let ast::AttrStyle::Inner = attr.style {
663 err.help("consider an outer attribute, #[macro_use] mod ...").emit();
667 } else if !attr.check_name("macro_use") {
672 self.session.span_err(attr.span, "arguments to macro_use are not allowed here");
680 fn legacy_macro_imports(&mut self, attrs: &[ast::Attribute]) -> LegacyMacroImports {
681 let mut imports = LegacyMacroImports::default();
683 if attr.check_name("macro_use") {
684 match attr.meta_item_list() {
685 Some(names) => for attr in names {
686 if let Some(word) = attr.word() {
687 imports.imports.push((word.name(), attr.span()));
689 span_err!(self.session, attr.span(), E0466, "bad macro import");
692 None => imports.import_all = Some(attr.span),
694 } else if attr.check_name("macro_reexport") {
695 let bad_macro_reexport = |this: &mut Self, span| {
696 span_err!(this.session, span, E0467, "bad macro reexport");
698 if let Some(names) = attr.meta_item_list() {
700 if let Some(word) = attr.word() {
701 imports.reexports.push((word.name(), attr.span()));
703 bad_macro_reexport(self, attr.span());
707 bad_macro_reexport(self, attr.span());
715 pub struct BuildReducedGraphVisitor<'a, 'b: 'a> {
716 pub resolver: &'a mut Resolver<'b>,
717 pub legacy_scope: LegacyScope<'b>,
721 impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
722 fn visit_invoc(&mut self, id: ast::NodeId) -> &'b InvocationData<'b> {
723 let mark = id.placeholder_to_mark();
724 self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
725 let invocation = self.resolver.invocations[&mark];
726 invocation.module.set(self.resolver.current_module);
727 invocation.legacy_scope.set(self.legacy_scope);
732 macro_rules! method {
733 ($visit:ident: $ty:ty, $invoc:path, $walk:ident) => {
734 fn $visit(&mut self, node: &'a $ty) {
735 if let $invoc(..) = node.node {
736 self.visit_invoc(node.id);
738 visit::$walk(self, node);
744 impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
745 method!(visit_impl_item: ast::ImplItem, ast::ImplItemKind::Macro, walk_impl_item);
746 method!(visit_expr: ast::Expr, ast::ExprKind::Mac, walk_expr);
747 method!(visit_pat: ast::Pat, ast::PatKind::Mac, walk_pat);
748 method!(visit_ty: ast::Ty, ast::TyKind::Mac, walk_ty);
750 fn visit_item(&mut self, item: &'a Item) {
751 let macro_use = match item.node {
752 ItemKind::MacroDef(..) => {
753 self.resolver.define_macro(item, self.expansion, &mut self.legacy_scope);
756 ItemKind::Mac(..) => {
757 self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
760 ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),
764 let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
765 self.resolver.build_reduced_graph_for_item(item, self.expansion);
766 visit::walk_item(self, item);
767 self.resolver.current_module = parent;
769 self.legacy_scope = legacy_scope;
773 fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
774 if let ast::StmtKind::Mac(..) = stmt.node {
775 self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(stmt.id));
777 visit::walk_stmt(self, stmt);
781 fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
782 self.resolver.build_reduced_graph_for_foreign_item(foreign_item, self.expansion);
783 visit::walk_foreign_item(self, foreign_item);
786 fn visit_block(&mut self, block: &'a Block) {
787 let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
788 self.resolver.build_reduced_graph_for_block(block, self.expansion);
789 visit::walk_block(self, block);
790 self.resolver.current_module = parent;
791 self.legacy_scope = legacy_scope;
794 fn visit_trait_item(&mut self, item: &'a TraitItem) {
795 let parent = self.resolver.current_module;
797 if let TraitItemKind::Macro(_) = item.node {
798 self.visit_invoc(item.id);
802 // Add the item to the trait info.
803 let item_def_id = self.resolver.definitions.local_def_id(item.id);
804 let (def, ns) = match item.node {
805 TraitItemKind::Const(..) => (Def::AssociatedConst(item_def_id), ValueNS),
806 TraitItemKind::Method(ref sig, _) => {
807 if sig.decl.has_self() {
808 self.resolver.has_self.insert(item_def_id);
810 (Def::Method(item_def_id), ValueNS)
812 TraitItemKind::Type(..) => (Def::AssociatedTy(item_def_id), TypeNS),
813 TraitItemKind::Macro(_) => bug!(), // handled above
816 let vis = ty::Visibility::Public;
817 self.resolver.define(parent, item.ident, ns, (def, vis, item.span, self.expansion));
819 self.resolver.current_module = parent.parent.unwrap(); // nearest normal ancestor
820 visit::walk_trait_item(self, item);
821 self.resolver.current_module = parent;