rustc_metadata: Remove rmeta::MacroDef

Use `ast::MacroDef` instead.

Also remove `Session::imported_macro_spans`, external macros have spans now.
This commit is contained in:
Vadim Petrochenkov 2020-03-14 00:00:35 +03:00
parent d6072319a9
commit 01a0c6d441
12 changed files with 21 additions and 60 deletions

View File

@ -1446,7 +1446,7 @@ impl MacDelimiter {
} }
/// Represents a macro definition. /// Represents a macro definition.
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] #[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
pub struct MacroDef { pub struct MacroDef {
pub body: P<MacArgs>, pub body: P<MacArgs>,
/// `true` if macro was defined with `macro_rules`. /// `true` if macro was defined with `macro_rules`.

View File

@ -6,6 +6,7 @@ use rustc::bug;
use rustc_ast::ast::*; use rustc_ast::ast::*;
use rustc_ast::attr; use rustc_ast::attr;
use rustc_ast::node_id::NodeMap; use rustc_ast::node_id::NodeMap;
use rustc_ast::ptr::P;
use rustc_ast::visit::{self, AssocCtxt, Visitor}; use rustc_ast::visit::{self, AssocCtxt, Visitor};
use rustc_errors::struct_span_err; use rustc_errors::struct_span_err;
use rustc_hir as hir; use rustc_hir as hir;
@ -219,18 +220,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
let mut vis = self.lower_visibility(&i.vis, None); let mut vis = self.lower_visibility(&i.vis, None);
let attrs = self.lower_attrs(&i.attrs); let attrs = self.lower_attrs(&i.attrs);
if let ItemKind::MacroDef(ref def) = i.kind { if let ItemKind::MacroDef(MacroDef { ref body, legacy }) = i.kind {
if !def.legacy || attr::contains_name(&i.attrs, sym::macro_export) { if !legacy || attr::contains_name(&i.attrs, sym::macro_export) {
let body = self.lower_token_stream(def.body.inner_tokens());
let hir_id = self.lower_node_id(i.id); let hir_id = self.lower_node_id(i.id);
let body = P(self.lower_mac_args(body));
self.exported_macros.push(hir::MacroDef { self.exported_macros.push(hir::MacroDef {
name: ident.name, ident,
vis, vis,
attrs, attrs,
hir_id, hir_id,
span: i.span, span: i.span,
body, ast: MacroDef { body, legacy },
legacy: def.legacy,
}); });
} else { } else {
self.non_exported_macro_attrs.extend(attrs.iter().cloned()); self.non_exported_macro_attrs.extend(attrs.iter().cloned());

View File

@ -13,7 +13,6 @@ use rustc_ast::ast::{AttrVec, Attribute, FloatTy, IntTy, Label, LitKind, StrStyl
pub use rustc_ast::ast::{BorrowKind, ImplPolarity, IsAuto}; pub use rustc_ast::ast::{BorrowKind, ImplPolarity, IsAuto};
pub use rustc_ast::ast::{CaptureBy, Movability, Mutability}; pub use rustc_ast::ast::{CaptureBy, Movability, Mutability};
use rustc_ast::node_id::NodeMap; use rustc_ast::node_id::NodeMap;
use rustc_ast::tokenstream::TokenStream;
use rustc_ast::util::parser::ExprPrecedence; use rustc_ast::util::parser::ExprPrecedence;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::{par_for_each_in, Send, Sync}; use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
@ -722,13 +721,12 @@ impl Crate<'_> {
/// Not parsed directly, but created on macro import or `macro_rules!` expansion. /// Not parsed directly, but created on macro import or `macro_rules!` expansion.
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] #[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
pub struct MacroDef<'hir> { pub struct MacroDef<'hir> {
pub name: Name, pub ident: Ident,
pub vis: Visibility<'hir>, pub vis: Visibility<'hir>,
pub attrs: &'hir [Attribute], pub attrs: &'hir [Attribute],
pub hir_id: HirId, pub hir_id: HirId,
pub span: Span, pub span: Span,
pub body: TokenStream, pub ast: ast::MacroDef,
pub legacy: bool,
} }
/// A block of statements `{ .. }`, which may have a label (in this case the /// A block of statements `{ .. }`, which may have a label (in this case the

View File

@ -445,7 +445,7 @@ pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef<'v>) { pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef<'v>) {
visitor.visit_id(macro_def.hir_id); visitor.visit_id(macro_def.hir_id);
visitor.visit_name(macro_def.span, macro_def.name); visitor.visit_ident(macro_def.ident);
walk_list!(visitor, visit_attribute, macro_def.attrs); walk_list!(visitor, visit_attribute, macro_def.attrs);
} }

View File

@ -17,8 +17,6 @@ use rustc::ty::{self, TyCtxt};
use rustc_ast::ast; use rustc_ast::ast;
use rustc_ast::attr; use rustc_ast::attr;
use rustc_ast::expand::allocator::AllocatorKind; use rustc_ast::expand::allocator::AllocatorKind;
use rustc_ast::ptr::P;
use rustc_ast::tokenstream::DelimSpan;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
@ -415,8 +413,6 @@ impl CStore {
} }
let span = data.get_span(id.index, sess); let span = data.get_span(id.index, sess);
let dspan = DelimSpan::from_single(span);
let rmeta::MacroDef { body, legacy } = data.get_macro(id.index, sess);
// Mark the attrs as used // Mark the attrs as used
let attrs = data.get_item_attrs(id.index, sess); let attrs = data.get_item_attrs(id.index, sess);
@ -424,25 +420,21 @@ impl CStore {
attr::mark_used(attr); attr::mark_used(attr);
} }
let name = data let ident = data
.def_key(id.index) .def_key(id.index)
.disambiguated_data .disambiguated_data
.data .data
.get_opt_name() .get_opt_name()
.map(ast::Ident::with_dummy_span) // FIXME: cross-crate hygiene
.expect("no name in load_macro"); .expect("no name in load_macro");
sess.imported_macro_spans.borrow_mut().insert(span, (name.to_string(), span));
LoadedMacro::MacroDef( LoadedMacro::MacroDef(
ast::Item { ast::Item {
// FIXME: cross-crate hygiene ident,
ident: ast::Ident::with_dummy_span(name),
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
span, span,
attrs: attrs.iter().cloned().collect(), attrs: attrs.iter().cloned().collect(),
kind: ast::ItemKind::MacroDef(ast::MacroDef { kind: ast::ItemKind::MacroDef(data.get_macro(id.index, sess)),
body: P(ast::MacArgs::Delimited(dspan, ast::MacDelimiter::Brace, body)),
legacy,
}),
vis: source_map::respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited), vis: source_map::respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
tokens: None, tokens: None,
}, },

View File

@ -1236,10 +1236,7 @@ impl EncodeContext<'tcx> {
/// Serialize the text of exported macros /// Serialize the text of exported macros
fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) { fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) {
let def_id = self.tcx.hir().local_def_id(macro_def.hir_id); let def_id = self.tcx.hir().local_def_id(macro_def.hir_id);
record!(self.per_def.kind[def_id] <- EntryKind::MacroDef(self.lazy(MacroDef { record!(self.per_def.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone())));
body: macro_def.body.clone(),
legacy: macro_def.legacy,
})));
record!(self.per_def.visibility[def_id] <- ty::Visibility::Public); record!(self.per_def.visibility[def_id] <- ty::Visibility::Public);
record!(self.per_def.span[def_id] <- macro_def.span); record!(self.per_def.span[def_id] <- macro_def.span);
record!(self.per_def.attributes[def_id] <- macro_def.attrs); record!(self.per_def.attributes[def_id] <- macro_def.attrs);

View File

@ -10,8 +10,7 @@ use rustc::mir;
use rustc::session::config::SymbolManglingVersion; use rustc::session::config::SymbolManglingVersion;
use rustc::session::CrateDisambiguator; use rustc::session::CrateDisambiguator;
use rustc::ty::{self, ReprOptions, Ty}; use rustc::ty::{self, ReprOptions, Ty};
use rustc_ast::ast; use rustc_ast::ast::{self, MacroDef};
use rustc_ast::tokenstream::TokenStream;
use rustc_attr as attr; use rustc_attr as attr;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MetadataRef; use rustc_data_structures::sync::MetadataRef;
@ -323,12 +322,6 @@ struct ModData {
reexports: Lazy<[Export<hir::HirId>]>, reexports: Lazy<[Export<hir::HirId>]>,
} }
#[derive(RustcEncodable, RustcDecodable)]
struct MacroDef {
body: TokenStream,
legacy: bool,
}
#[derive(RustcEncodable, RustcDecodable)] #[derive(RustcEncodable, RustcDecodable)]
struct FnData { struct FnData {
asyncness: hir::IsAsync, asyncness: hir::IsAsync,

View File

@ -920,7 +920,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
} }
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) { fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
if attr::find_transparency(&md.attrs, md.legacy).0 != Transparency::Opaque { if attr::find_transparency(&md.attrs, md.ast.legacy).0 != Transparency::Opaque {
self.update(md.hir_id, Some(AccessLevel::Public)); self.update(md.hir_id, Some(AccessLevel::Public));
return; return;
} }

View File

@ -794,19 +794,6 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
ExpnKind::Root | ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => return None, ExpnKind::Root | ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => return None,
}; };
// If the callee is an imported macro from an external crate, need to get
// the source span and name from the session, as their spans are localized
// when read in, and no longer correspond to the source.
if let Some(mac) = self.tcx.sess.imported_macro_spans.borrow().get(&callee.def_site) {
let &(ref mac_name, mac_span) = mac;
let mac_span = self.span_from_span(mac_span);
return Some(MacroRef {
span: callsite_span,
qualname: mac_name.clone(), // FIXME: generate the real qualname
callee_span: mac_span,
});
}
let callee_span = self.span_from_span(callee.def_site); let callee_span = self.span_from_span(callee.def_site);
Some(MacroRef { Some(MacroRef {
span: callsite_span, span: callsite_span,

View File

@ -103,11 +103,6 @@ pub struct Session {
/// The maximum blocks a const expression can evaluate. /// The maximum blocks a const expression can evaluate.
pub const_eval_limit: Once<usize>, pub const_eval_limit: Once<usize>,
/// Map from imported macro spans (which consist of
/// the localized span for the macro body) to the
/// macro name and definition span in the source crate.
pub imported_macro_spans: OneThread<RefCell<FxHashMap<Span, (String, Span)>>>,
incr_comp_session: OneThread<RefCell<IncrCompSession>>, incr_comp_session: OneThread<RefCell<IncrCompSession>>,
/// Used for incremental compilation tests. Will only be populated if /// Used for incremental compilation tests. Will only be populated if
/// `-Zquery-dep-graph` is specified. /// `-Zquery-dep-graph` is specified.
@ -1080,7 +1075,6 @@ fn build_session_(
recursion_limit: Once::new(), recursion_limit: Once::new(),
type_length_limit: Once::new(), type_length_limit: Once::new(),
const_eval_limit: Once::new(), const_eval_limit: Once::new(),
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)), incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
cgu_reuse_tracker, cgu_reuse_tracker,
prof, prof,

View File

@ -955,7 +955,7 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirCollector<'a, 'hir> {
} }
fn visit_macro_def(&mut self, macro_def: &'hir hir::MacroDef) { fn visit_macro_def(&mut self, macro_def: &'hir hir::MacroDef) {
self.visit_testable(macro_def.name.to_string(), &macro_def.attrs, |_| ()); self.visit_testable(macro_def.ident.to_string(), &macro_def.attrs, |_| ());
} }
} }

View File

@ -620,8 +620,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
def: &'tcx hir::MacroDef, def: &'tcx hir::MacroDef,
renamed: Option<ast::Name>, renamed: Option<ast::Name>,
) -> Macro<'tcx> { ) -> Macro<'tcx> {
debug!("visit_local_macro: {}", def.name); debug!("visit_local_macro: {}", def.ident);
let tts = def.body.trees().collect::<Vec<_>>(); let tts = def.ast.body.inner_tokens().trees().collect::<Vec<_>>();
// Extract the spans of all matchers. They represent the "interface" of the macro. // Extract the spans of all matchers. They represent the "interface" of the macro.
let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect(); let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect();
@ -629,7 +629,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
hid: def.hir_id, hid: def.hir_id,
def_id: self.cx.tcx.hir().local_def_id(def.hir_id), def_id: self.cx.tcx.hir().local_def_id(def.hir_id),
attrs: &def.attrs, attrs: &def.attrs,
name: renamed.unwrap_or(def.name), name: renamed.unwrap_or(def.ident.name),
whence: def.span, whence: def.span,
matchers, matchers,
imported_from: None, imported_from: None,