Skip to content

Commit 212b6c2

Browse files
jseyfriedalexcrichton
authored andcommitted
Refactor out ast::ItemKind::MacroDef.
1 parent f573db4 commit 212b6c2

File tree

25 files changed

+172
-196
lines changed

25 files changed

+172
-196
lines changed

src/librustc/hir/lowering.rs

+28-23
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub struct LoweringContext<'a> {
7979
trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem>,
8080
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem>,
8181
bodies: BTreeMap<hir::BodyId, hir::Body>,
82+
exported_macros: Vec<hir::MacroDef>,
8283

8384
trait_impls: BTreeMap<DefId, Vec<NodeId>>,
8485
trait_default_impl: BTreeMap<DefId, NodeId>,
@@ -121,6 +122,7 @@ pub fn lower_crate(sess: &Session,
121122
bodies: BTreeMap::new(),
122123
trait_impls: BTreeMap::new(),
123124
trait_default_impl: BTreeMap::new(),
125+
exported_macros: Vec::new(),
124126
loop_scopes: Vec::new(),
125127
is_in_loop_condition: false,
126128
type_def_lifetime_params: DefIdMap(),
@@ -170,9 +172,10 @@ impl<'a> LoweringContext<'a> {
170172

171173
impl<'lcx, 'interner> Visitor<'lcx> for ItemLowerer<'lcx, 'interner> {
172174
fn visit_item(&mut self, item: &'lcx Item) {
173-
let hir_item = self.lctx.lower_item(item);
174-
self.lctx.items.insert(item.id, hir_item);
175-
visit::walk_item(self, item);
175+
if let Some(hir_item) = self.lctx.lower_item(item) {
176+
self.lctx.items.insert(item.id, hir_item);
177+
visit::walk_item(self, item);
178+
}
176179
}
177180

178181
fn visit_trait_item(&mut self, item: &'lcx TraitItem) {
@@ -195,14 +198,13 @@ impl<'a> LoweringContext<'a> {
195198

196199
let module = self.lower_mod(&c.module);
197200
let attrs = self.lower_attrs(&c.attrs);
198-
let exported_macros = c.exported_macros.iter().map(|m| self.lower_macro_def(m)).collect();
199201
let body_ids = body_ids(&self.bodies);
200202

201203
hir::Crate {
202204
module: module,
203205
attrs: attrs,
204206
span: c.span,
205-
exported_macros: exported_macros,
207+
exported_macros: hir::HirVec::from(self.exported_macros),
206208
items: self.items,
207209
trait_items: self.trait_items,
208210
impl_items: self.impl_items,
@@ -1134,7 +1136,7 @@ impl<'a> LoweringContext<'a> {
11341136
bounds,
11351137
items)
11361138
}
1137-
ItemKind::Mac(_) => panic!("Shouldn't still be around"),
1139+
ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
11381140
}
11391141
}
11401142

@@ -1256,42 +1258,45 @@ impl<'a> LoweringContext<'a> {
12561258
}
12571259
}
12581260

1259-
fn lower_macro_def(&mut self, m: &MacroDef) -> hir::MacroDef {
1260-
hir::MacroDef {
1261-
name: m.ident.name,
1262-
attrs: self.lower_attrs(&m.attrs),
1263-
id: m.id,
1264-
span: m.span,
1265-
body: m.body.clone().into(),
1266-
}
1267-
}
1268-
12691261
fn lower_item_id(&mut self, i: &Item) -> SmallVector<hir::ItemId> {
1270-
if let ItemKind::Use(ref view_path) = i.node {
1271-
if let ViewPathList(_, ref imports) = view_path.node {
1272-
return iter::once(i.id).chain(imports.iter().map(|import| import.node.id))
1273-
.map(|id| hir::ItemId { id: id }).collect();
1262+
match i.node {
1263+
ItemKind::Use(ref view_path) => {
1264+
if let ViewPathList(_, ref imports) = view_path.node {
1265+
return iter::once(i.id).chain(imports.iter().map(|import| import.node.id))
1266+
.map(|id| hir::ItemId { id: id }).collect();
1267+
}
12741268
}
1269+
ItemKind::MacroDef(..) => return SmallVector::new(),
1270+
_ => {}
12751271
}
12761272
SmallVector::one(hir::ItemId { id: i.id })
12771273
}
12781274

1279-
pub fn lower_item(&mut self, i: &Item) -> hir::Item {
1275+
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
12801276
let mut name = i.ident.name;
12811277
let attrs = self.lower_attrs(&i.attrs);
12821278
let mut vis = self.lower_visibility(&i.vis);
1279+
if let ItemKind::MacroDef(ref tts, _) = i.node {
1280+
if i.attrs.iter().any(|attr| attr.name() == "macro_export") {
1281+
self.exported_macros.push(hir::MacroDef {
1282+
name: name, attrs: attrs, id: i.id, span: i.span, body: tts.clone().into(),
1283+
});
1284+
}
1285+
return None;
1286+
}
1287+
12831288
let node = self.with_parent_def(i.id, |this| {
12841289
this.lower_item_kind(i.id, &mut name, &attrs, &mut vis, &i.node)
12851290
});
12861291

1287-
hir::Item {
1292+
Some(hir::Item {
12881293
id: i.id,
12891294
name: name,
12901295
attrs: attrs,
12911296
node: node,
12921297
vis: vis,
12931298
span: i.span,
1294-
}
1299+
})
12951300
}
12961301

12971302
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem {

src/librustc/hir/map/def_collector.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
108108
ItemKind::Mod(..) => DefPathData::Module(i.ident.name.as_str()),
109109
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
110110
DefPathData::ValueNs(i.ident.name.as_str()),
111-
ItemKind::Mac(..) if i.id == DUMMY_NODE_ID => return, // Scope placeholder
111+
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),
112112
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
113113
ItemKind::Use(ref view_path) => {
114114
match view_path.node {
@@ -269,10 +269,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
269269
self.create_def(def.lifetime.id, DefPathData::LifetimeDef(def.lifetime.name.as_str()));
270270
}
271271

272-
fn visit_macro_def(&mut self, macro_def: &'a MacroDef) {
273-
self.create_def(macro_def.id, DefPathData::MacroDef(macro_def.ident.name.as_str()));
274-
}
275-
276272
fn visit_stmt(&mut self, stmt: &'a Stmt) {
277273
match stmt.node {
278274
StmtKind::Mac(..) => self.visit_macro_invoc(stmt.id, false),

src/librustc/middle/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub struct NativeLibrary {
136136
}
137137

138138
pub enum LoadedMacro {
139-
MacroRules(ast::MacroDef),
139+
MacroDef(ast::Item),
140140
ProcMacro(Rc<SyntaxExtension>),
141141
}
142142

src/librustc_driver/driver.rs

-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use super::Compilation;
4343
use serialize::json;
4444

4545
use std::env;
46-
use std::mem;
4746
use std::ffi::{OsString, OsStr};
4847
use std::fs;
4948
use std::io::{self, Write};
@@ -705,8 +704,6 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
705704
krate
706705
});
707706

708-
krate.exported_macros = mem::replace(&mut resolver.exported_macros, Vec::new());
709-
710707
krate = time(time_passes, "maybe building test harness", || {
711708
syntax::test::modify_for_testing(&sess.parse_sess,
712709
&mut resolver,

src/librustc_metadata/cstore_impl.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use std::rc::Rc;
3434

3535
use syntax::ast;
3636
use syntax::attr;
37+
use syntax::ext::hygiene::Mark;
3738
use syntax::parse::filemap_to_stream;
3839
use syntax::symbol::Symbol;
3940
use syntax_pos::{mk_sp, Span};
@@ -414,12 +415,13 @@ impl CrateStore for cstore::CStore {
414415
sess.imported_macro_spans.borrow_mut()
415416
.insert(local_span, (name.to_string(), data.get_span(id.index, sess)));
416417

417-
LoadedMacro::MacroRules(ast::MacroDef {
418+
LoadedMacro::MacroDef(ast::Item {
418419
ident: ast::Ident::with_empty_ctxt(name),
419420
id: ast::DUMMY_NODE_ID,
420421
span: local_span,
421422
attrs: attrs,
422-
body: body.into(),
423+
node: ast::ItemKind::MacroDef(body.into(), Mark::fresh()),
424+
vis: ast::Visibility::Inherited,
423425
})
424426
}
425427

src/librustc_passes/hir_stats.rs

-5
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,4 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
375375
fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
376376
self.record("Attribute", Id::None, attr);
377377
}
378-
379-
fn visit_macro_def(&mut self, macro_def: &'v ast::MacroDef) {
380-
self.record("MacroDef", Id::None, macro_def);
381-
ast_visit::walk_macro_def(self, macro_def)
382-
}
383378
}

src/librustc_resolve/build_reduced_graph.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use syntax::ast::{Mutability, StmtKind, TraitItem, TraitItemKind};
3737
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
3838
use syntax::ext::base::SyntaxExtension;
3939
use syntax::ext::base::Determinacy::Undetermined;
40-
use syntax::ext::expand::mark_tts;
4140
use syntax::ext::hygiene::Mark;
4241
use syntax::ext::tt::macro_rules;
4342
use syntax::parse::token;
@@ -373,7 +372,7 @@ impl<'a> Resolver<'a> {
373372
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
374373
self.current_module = module;
375374
}
376-
ItemKind::Mac(_) => panic!("unexpanded macro in resolve!"),
375+
ItemKind::MacroDef(..) | ItemKind::Mac(_) => unreachable!(),
377376
}
378377
}
379378

@@ -502,22 +501,21 @@ impl<'a> Resolver<'a> {
502501
return ext.clone();
503502
}
504503

505-
let mut macro_rules = match self.session.cstore.load_macro(def_id, &self.session) {
506-
LoadedMacro::MacroRules(macro_rules) => macro_rules,
504+
let macro_def = match self.session.cstore.load_macro(def_id, &self.session) {
505+
LoadedMacro::MacroDef(macro_def) => macro_def,
507506
LoadedMacro::ProcMacro(ext) => return ext,
508507
};
509508

510-
let mark = Mark::fresh();
511509
let invocation = self.arenas.alloc_invocation_data(InvocationData {
512510
module: Cell::new(self.get_extern_crate_root(def_id.krate)),
513-
def_index: CRATE_DEF_INDEX,
514-
const_expr: false,
515-
legacy_scope: Cell::new(LegacyScope::Empty),
516-
expansion: Cell::new(LegacyScope::Empty),
511+
// FIXME(jseyfried) the following are irrelevant
512+
def_index: CRATE_DEF_INDEX, const_expr: false,
513+
legacy_scope: Cell::new(LegacyScope::Empty), expansion: Cell::new(LegacyScope::Empty),
517514
});
518-
self.invocations.insert(mark, invocation);
519-
macro_rules.body = mark_tts(macro_rules.stream(), mark).into();
520-
let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, &macro_rules));
515+
if let ast::ItemKind::MacroDef(_, mark) = macro_def.node {
516+
self.invocations.insert(mark, invocation);
517+
}
518+
let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, &macro_def));
521519
self.macro_map.insert(def_id, ext.clone());
522520
ext
523521
}
@@ -707,12 +705,12 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
707705

708706
fn visit_item(&mut self, item: &'a Item) {
709707
let macro_use = match item.node {
710-
ItemKind::Mac(ref mac) => {
711-
if mac.node.path.segments.is_empty() {
712-
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
713-
} else {
714-
self.resolver.define_macro(item, &mut self.legacy_scope);
715-
}
708+
ItemKind::MacroDef(..) => {
709+
self.resolver.define_macro(item, &mut self.legacy_scope);
710+
return
711+
}
712+
ItemKind::Mac(..) => {
713+
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
716714
return
717715
}
718716
ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),

src/librustc_resolve/lib.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -1092,10 +1092,6 @@ pub struct Resolver<'a> {
10921092

10931093
pub definitions: Definitions,
10941094

1095-
// Maps the node id of a statement to the expansions of the `macro_rules!`s
1096-
// immediately above the statement (if appropriate).
1097-
macros_at_scope: FxHashMap<NodeId, Vec<Mark>>,
1098-
10991095
graph_root: Module<'a>,
11001096

11011097
prelude: Option<Module<'a>>,
@@ -1171,7 +1167,6 @@ pub struct Resolver<'a> {
11711167
dummy_binding: &'a NameBinding<'a>,
11721168
use_extern_macros: bool, // true if `#![feature(use_extern_macros)]`
11731169

1174-
pub exported_macros: Vec<ast::MacroDef>,
11751170
crate_loader: &'a mut CrateLoader,
11761171
macro_names: FxHashSet<Name>,
11771172
builtin_macros: FxHashMap<Name, &'a NameBinding<'a>>,
@@ -1309,7 +1304,6 @@ impl<'a> Resolver<'a> {
13091304
session: session,
13101305

13111306
definitions: definitions,
1312-
macros_at_scope: FxHashMap(),
13131307

13141308
// The outermost module has def ID 0; this is not reflected in the
13151309
// AST.
@@ -1365,7 +1359,6 @@ impl<'a> Resolver<'a> {
13651359
// `#![feature(proc_macro)]` implies `#[feature(extern_macros)]`
13661360
use_extern_macros: features.use_extern_macros || features.proc_macro,
13671361

1368-
exported_macros: Vec::new(),
13691362
crate_loader: crate_loader,
13701363
macro_names: FxHashSet(),
13711364
builtin_macros: FxHashMap(),
@@ -1696,7 +1689,7 @@ impl<'a> Resolver<'a> {
16961689
}
16971690
}
16981691

1699-
ItemKind::ExternCrate(_) => {
1692+
ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) => {
17001693
// do nothing, these are just around to be encoded
17011694
}
17021695

@@ -2031,9 +2024,9 @@ impl<'a> Resolver<'a> {
20312024

20322025
// Descend into the block.
20332026
for stmt in &block.stmts {
2034-
if let Some(marks) = self.macros_at_scope.remove(&stmt.id) {
2035-
num_macro_definition_ribs += marks.len() as u32;
2036-
for mark in marks {
2027+
if let ast::StmtKind::Item(ref item) = stmt.node {
2028+
if let ast::ItemKind::MacroDef(_, mark) = item.node {
2029+
num_macro_definition_ribs += 1;
20372030
self.ribs[ValueNS].push(Rib::new(MacroDefinition(mark)));
20382031
self.label_ribs.push(Rib::new(MacroDefinition(mark)));
20392032
}

0 commit comments

Comments
 (0)