resolve: Rename some fields related to legacy macro scopes

This commit is contained in:
Vadim Petrochenkov 2018-08-29 04:48:02 +03:00
parent c057d579ab
commit d26ae20507
2 changed files with 60 additions and 51 deletions

View File

@ -950,7 +950,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
pub struct BuildReducedGraphVisitor<'a, 'b: 'a, 'c: 'b> {
pub resolver: &'a mut Resolver<'b, 'c>,
pub legacy_scope: LegacyScope<'b>,
pub current_legacy_scope: LegacyScope<'b>,
pub expansion: Mark,
}
@ -960,7 +960,7 @@ impl<'a, 'b, 'cl> BuildReducedGraphVisitor<'a, 'b, 'cl> {
self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
let invocation = self.resolver.invocations[&mark];
invocation.module.set(self.resolver.current_module);
invocation.legacy_scope.set(self.legacy_scope);
invocation.parent_legacy_scope.set(self.current_legacy_scope);
invocation
}
}
@ -986,29 +986,30 @@ impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {
fn visit_item(&mut self, item: &'a Item) {
let macro_use = match item.node {
ItemKind::MacroDef(..) => {
self.resolver.define_macro(item, self.expansion, &mut self.legacy_scope);
self.resolver.define_macro(item, self.expansion, &mut self.current_legacy_scope);
return
}
ItemKind::Mac(..) => {
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
self.current_legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
return
}
ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),
_ => false,
};
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
let orig_current_module = self.resolver.current_module;
let orig_current_legacy_scope = self.current_legacy_scope;
self.resolver.build_reduced_graph_for_item(item, self.expansion);
visit::walk_item(self, item);
self.resolver.current_module = parent;
self.resolver.current_module = orig_current_module;
if !macro_use {
self.legacy_scope = legacy_scope;
self.current_legacy_scope = orig_current_legacy_scope;
}
}
fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
if let ast::StmtKind::Mac(..) = stmt.node {
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(stmt.id));
self.current_legacy_scope = LegacyScope::Expansion(self.visit_invoc(stmt.id));
} else {
visit::walk_stmt(self, stmt);
}
@ -1025,11 +1026,12 @@ impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {
}
fn visit_block(&mut self, block: &'a Block) {
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
let orig_current_module = self.resolver.current_module;
let orig_current_legacy_scope = self.current_legacy_scope;
self.resolver.build_reduced_graph_for_block(block, self.expansion);
visit::walk_block(self, block);
self.resolver.current_module = parent;
self.legacy_scope = legacy_scope;
self.resolver.current_module = orig_current_module;
self.current_legacy_scope = orig_current_legacy_scope;
}
fn visit_trait_item(&mut self, item: &'a TraitItem) {

View File

@ -50,13 +50,15 @@ crate struct FromPrelude(bool);
#[derive(Clone)]
pub struct InvocationData<'a> {
pub module: Cell<Module<'a>>,
pub def_index: DefIndex,
// The scope in which the invocation path is resolved.
pub legacy_scope: Cell<LegacyScope<'a>>,
// The smallest scope that includes this invocation's expansion,
// or `Empty` if this invocation has not been expanded yet.
pub expansion: Cell<LegacyScope<'a>>,
crate module: Cell<Module<'a>>,
def_index: DefIndex,
// Legacy scope in which the macro was invoked.
// The invocation path is resolved in this scope.
crate parent_legacy_scope: Cell<LegacyScope<'a>>,
// Legacy scope *produced* by expanding this macro invocation,
// includes all the macro_rules items, other invocations, etc generated by it.
// `Empty` is used if for invocations that has not been expanded yet.
output_legacy_scope: Cell<LegacyScope<'a>>,
}
impl<'a> InvocationData<'a> {
@ -64,12 +66,21 @@ impl<'a> InvocationData<'a> {
InvocationData {
module: Cell::new(graph_root),
def_index: CRATE_DEF_INDEX,
legacy_scope: Cell::new(LegacyScope::Empty),
expansion: Cell::new(LegacyScope::Empty),
parent_legacy_scope: Cell::new(LegacyScope::Empty),
output_legacy_scope: Cell::new(LegacyScope::Empty),
}
}
}
// Binding produced by a `macro_rules` item.
// Not modularized, can shadow previous legacy bindings, etc.
pub struct LegacyBinding<'a> {
binding: &'a NameBinding<'a>,
// Legacy scope into which the `macro_rules` item was planted.
parent_legacy_scope: Cell<LegacyScope<'a>>,
ident: Ident,
}
#[derive(Copy, Clone)]
pub enum LegacyScope<'a> {
Empty,
@ -78,14 +89,6 @@ pub enum LegacyScope<'a> {
Binding(&'a LegacyBinding<'a>),
}
// Binding produced by a `macro_rules` item.
// Not modularized, can shadow previous legacy bindings, etc.
pub struct LegacyBinding<'a> {
binding: &'a NameBinding<'a>,
parent: Cell<LegacyScope<'a>>,
ident: Ident,
}
pub struct ProcMacError {
crate_name: Symbol,
name: Symbol,
@ -105,8 +108,8 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
module: Cell::new(module),
def_index: module.def_id().unwrap().index,
legacy_scope: Cell::new(LegacyScope::Empty),
expansion: Cell::new(LegacyScope::Empty),
parent_legacy_scope: Cell::new(LegacyScope::Empty),
output_legacy_scope: Cell::new(LegacyScope::Empty),
}));
mark
}
@ -178,11 +181,11 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
}
let mut visitor = BuildReducedGraphVisitor {
resolver: self,
legacy_scope: LegacyScope::Invocation(invocation),
current_legacy_scope: LegacyScope::Invocation(invocation),
expansion: mark,
};
fragment.visit_with(&mut visitor);
invocation.expansion.set(visitor.legacy_scope);
invocation.output_legacy_scope.set(visitor.current_legacy_scope);
}
fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
@ -481,7 +484,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
}
let legacy_resolution =
self.resolve_legacy_scope(path[0], invoc_id, &invocation.legacy_scope, false);
self.resolve_legacy_scope(path[0], invoc_id, &invocation.parent_legacy_scope, false);
let result = if let Some(legacy_binding) = legacy_resolution {
Ok(legacy_binding.def())
} else {
@ -814,18 +817,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
macro_rules! continue_search { () => {
where_to_resolve = match where_to_resolve.get() {
LegacyScope::Binding(binding) => &binding.parent,
LegacyScope::Invocation(invocation) => &invocation.legacy_scope,
LegacyScope::Expansion(invocation) => match invocation.expansion.get() {
LegacyScope::Empty => &invocation.legacy_scope,
LegacyScope::Empty => break, // nowhere else to search
LegacyScope::Binding(binding) => &binding.parent_legacy_scope,
LegacyScope::Invocation(invocation) => &invocation.parent_legacy_scope,
LegacyScope::Expansion(invocation) => {
match invocation.output_legacy_scope.get() {
LegacyScope::Empty => &invocation.parent_legacy_scope,
LegacyScope::Binding(..) |
LegacyScope::Expansion(..) => &invocation.expansion,
LegacyScope::Expansion(..) => &invocation.output_legacy_scope,
LegacyScope::Invocation(..) => {
where_to_resolve.set(invocation.legacy_scope.get());
where_to_resolve.set(invocation.parent_legacy_scope.get());
where_to_resolve
}
}
LegacyScope::Empty => break, // nowhere else to search
}
};
continue;
@ -880,8 +885,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
for &(invoc_id, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
let span = ident.span;
let legacy_scope = &self.invocations[&invoc_id].legacy_scope;
let legacy_resolution = self.resolve_legacy_scope(ident, invoc_id, legacy_scope, true);
let invoc_parent_legacy_scope = &self.invocations[&invoc_id].parent_legacy_scope;
let legacy_resolution =
self.resolve_legacy_scope(ident, invoc_id, invoc_parent_legacy_scope, true);
let resolution = self.resolve_lexical_macro_path_segment(
ident, MacroNS, invoc_id, true, true, kind == MacroKind::Attr, span
);
@ -1007,8 +1013,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
arenas.alloc_invocation_data(InvocationData {
def_index: invoc.def_index,
module: Cell::new(graph_root),
expansion: Cell::new(LegacyScope::Empty),
legacy_scope: Cell::new(LegacyScope::Empty),
parent_legacy_scope: Cell::new(LegacyScope::Empty),
output_legacy_scope: Cell::new(LegacyScope::Empty),
})
});
};
@ -1023,7 +1029,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
pub fn define_macro(&mut self,
item: &ast::Item,
expansion: Mark,
legacy_scope: &mut LegacyScope<'a>) {
current_legacy_scope: &mut LegacyScope<'a>) {
self.local_macro_def_scopes.insert(item.id, self.current_module);
let ident = item.ident;
if ident.name == "macro_rules" {
@ -1043,9 +1049,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
let def = Def::Macro(def_id, MacroKind::Bang);
let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(
LegacyBinding { parent: Cell::new(*legacy_scope), binding, ident }
));
let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
parent_legacy_scope: Cell::new(*current_legacy_scope), binding, ident
});
*current_legacy_scope = LegacyScope::Binding(legacy_binding);
self.all_macros.insert(ident.name, def);
if attr::contains_name(&item.attrs, "macro_export") {
let module = self.graph_root;