resolve/expand: Rename some things for clarity

This commit is contained in:
Vadim Petrochenkov 2019-08-17 20:49:00 +03:00
parent d479ff2ffe
commit 1064d41c96
7 changed files with 43 additions and 41 deletions

View file

@ -1,4 +1,4 @@
use crate::ast::{self, Attribute, Name, PatKind};
use crate::ast::{self, NodeId, Attribute, Name, PatKind};
use crate::attr::{HasAttrs, Stability, Deprecation};
use crate::source_map::SourceMap;
use crate::edition::Edition;
@ -671,13 +671,13 @@ bitflags::bitflags! {
}
pub trait Resolver {
fn next_node_id(&mut self) -> ast::NodeId;
fn next_node_id(&mut self) -> NodeId;
fn get_module_scope(&mut self, id: ast::NodeId) -> ExpnId;
fn get_module_scope(&mut self, id: NodeId) -> ExpnId;
fn resolve_dollar_crates(&mut self);
fn visit_ast_fragment_with_placeholders(&mut self, expn_id: ExpnId, fragment: &AstFragment,
derives: &[ExpnId]);
extra_placeholders: &[NodeId]);
fn register_builtin_macro(&mut self, ident: ast::Ident, ext: SyntaxExtension);
fn resolve_imports(&mut self);

View file

@ -291,7 +291,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
// Unresolved macros produce dummy outputs as a recovery measure.
invocations.reverse();
let mut expanded_fragments = Vec::new();
let mut derives: FxHashMap<ExpnId, Vec<_>> = FxHashMap::default();
let mut all_derive_placeholders: FxHashMap<ExpnId, Vec<_>> = FxHashMap::default();
let mut undetermined_invocations = Vec::new();
let (mut progress, mut force) = (false, !self.monotonic);
loop {
@ -347,13 +347,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
let mut item = self.fully_configure(item);
item.visit_attrs(|attrs| attrs.retain(|a| a.path != sym::derive));
let derives = derives.entry(invoc.expansion_data.id).or_default();
let derive_placeholders =
all_derive_placeholders.entry(invoc.expansion_data.id).or_default();
derives.reserve(traits.len());
derive_placeholders.reserve(traits.len());
invocations.reserve(traits.len());
for path in traits {
let expn_id = ExpnId::fresh(None);
derives.push(expn_id);
derive_placeholders.push(NodeId::placeholder_from_expn_id(expn_id));
invocations.push(Invocation {
kind: InvocationKind::Derive { path, item: item.clone() },
fragment_kind: invoc.fragment_kind,
@ -365,7 +366,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}
let fragment = invoc.fragment_kind
.expect_from_annotatables(::std::iter::once(item));
self.collect_invocations(fragment, derives)
self.collect_invocations(fragment, derive_placeholders)
} else {
unreachable!()
};
@ -384,10 +385,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
// Finally incorporate all the expanded macros into the input AST fragment.
let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
while let Some(expanded_fragments) = expanded_fragments.pop() {
for (mark, expanded_fragment) in expanded_fragments.into_iter().rev() {
let derives = derives.remove(&mark).unwrap_or_else(Vec::new);
placeholder_expander.add(NodeId::placeholder_from_expn_id(mark),
expanded_fragment, derives);
for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() {
let derive_placeholders =
all_derive_placeholders.remove(&expn_id).unwrap_or_else(Vec::new);
placeholder_expander.add(NodeId::placeholder_from_expn_id(expn_id),
expanded_fragment, derive_placeholders);
}
}
fragment_with_placeholders.mut_visit_with(&mut placeholder_expander);
@ -404,7 +406,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
/// them with "placeholders" - dummy macro invocations with specially crafted `NodeId`s.
/// Then call into resolver that builds a skeleton ("reduced graph") of the fragment and
/// prepares data for resolving paths of macro invocations.
fn collect_invocations(&mut self, mut fragment: AstFragment, derives: &[ExpnId])
fn collect_invocations(&mut self, mut fragment: AstFragment, extra_placeholders: &[NodeId])
-> (AstFragment, Vec<Invocation>) {
// Resolve `$crate`s in the fragment for pretty-printing.
self.cx.resolver.resolve_dollar_crates();
@ -423,9 +425,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
collector.invocations
};
// FIXME: Merge `extra_placeholders` into the `fragment` as regular placeholders.
if self.monotonic {
self.cx.resolver.visit_ast_fragment_with_placeholders(
self.cx.current_expansion.id, &fragment, derives);
self.cx.current_expansion.id, &fragment, extra_placeholders);
}
(fragment, invocations)

View file

@ -2,7 +2,6 @@ use crate::ast::{self, NodeId};
use crate::source_map::{DUMMY_SP, dummy_spanned};
use crate::ext::base::ExtCtxt;
use crate::ext::expand::{AstFragment, AstFragmentKind};
use crate::ext::hygiene::ExpnId;
use crate::tokenstream::TokenStream;
use crate::mut_visit::*;
use crate::ptr::P;
@ -86,11 +85,11 @@ impl<'a, 'b> PlaceholderExpander<'a, 'b> {
}
}
pub fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment, derives: Vec<ExpnId>) {
pub fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment, placeholders: Vec<NodeId>) {
fragment.mut_visit_with(self);
if let AstFragment::Items(mut items) = fragment {
for derive in derives {
match self.remove(NodeId::placeholder_from_expn_id(derive)) {
for placeholder in placeholders {
match self.remove(placeholder) {
AstFragment::Items(derived_items) => items.extend(derived_items),
_ => unreachable!(),
}