Skip to content

Commit 340d1f2

Browse files
committed
Merge branch 'ast_macro_def' of https://github.com/jseyfried/rust into rollup
2 parents 35680bd + 9057c8d commit 340d1f2

File tree

25 files changed

+304
-315
lines changed

25 files changed

+304
-315
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>,
@@ -123,6 +124,7 @@ pub fn lower_crate(sess: &Session,
123124
trait_impls: BTreeMap::new(),
124125
trait_default_impl: BTreeMap::new(),
125126
catch_scopes: Vec::new(),
127+
exported_macros: Vec::new(),
126128
loop_scopes: Vec::new(),
127129
is_in_loop_condition: false,
128130
type_def_lifetime_params: DefIdMap(),
@@ -172,9 +174,10 @@ impl<'a> LoweringContext<'a> {
172174

173175
impl<'lcx, 'interner> Visitor<'lcx> for ItemLowerer<'lcx, 'interner> {
174176
fn visit_item(&mut self, item: &'lcx Item) {
175-
let hir_item = self.lctx.lower_item(item);
176-
self.lctx.items.insert(item.id, hir_item);
177-
visit::walk_item(self, item);
177+
if let Some(hir_item) = self.lctx.lower_item(item) {
178+
self.lctx.items.insert(item.id, hir_item);
179+
visit::walk_item(self, item);
180+
}
178181
}
179182

180183
fn visit_trait_item(&mut self, item: &'lcx TraitItem) {
@@ -197,14 +200,13 @@ impl<'a> LoweringContext<'a> {
197200

198201
let module = self.lower_mod(&c.module);
199202
let attrs = self.lower_attrs(&c.attrs);
200-
let exported_macros = c.exported_macros.iter().map(|m| self.lower_macro_def(m)).collect();
201203
let body_ids = body_ids(&self.bodies);
202204

203205
hir::Crate {
204206
module: module,
205207
attrs: attrs,
206208
span: c.span,
207-
exported_macros: exported_macros,
209+
exported_macros: hir::HirVec::from(self.exported_macros),
208210
items: self.items,
209211
trait_items: self.trait_items,
210212
impl_items: self.impl_items,
@@ -1153,7 +1155,7 @@ impl<'a> LoweringContext<'a> {
11531155
bounds,
11541156
items)
11551157
}
1156-
ItemKind::Mac(_) => panic!("Shouldn't still be around"),
1158+
ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
11571159
}
11581160
}
11591161

@@ -1275,42 +1277,45 @@ impl<'a> LoweringContext<'a> {
12751277
}
12761278
}
12771279

1278-
fn lower_macro_def(&mut self, m: &MacroDef) -> hir::MacroDef {
1279-
hir::MacroDef {
1280-
name: m.ident.name,
1281-
attrs: self.lower_attrs(&m.attrs),
1282-
id: m.id,
1283-
span: m.span,
1284-
body: m.body.clone().into(),
1285-
}
1286-
}
1287-
12881280
fn lower_item_id(&mut self, i: &Item) -> SmallVector<hir::ItemId> {
1289-
if let ItemKind::Use(ref view_path) = i.node {
1290-
if let ViewPathList(_, ref imports) = view_path.node {
1291-
return iter::once(i.id).chain(imports.iter().map(|import| import.node.id))
1292-
.map(|id| hir::ItemId { id: id }).collect();
1281+
match i.node {
1282+
ItemKind::Use(ref view_path) => {
1283+
if let ViewPathList(_, ref imports) = view_path.node {
1284+
return iter::once(i.id).chain(imports.iter().map(|import| import.node.id))
1285+
.map(|id| hir::ItemId { id: id }).collect();
1286+
}
12931287
}
1288+
ItemKind::MacroDef(..) => return SmallVector::new(),
1289+
_ => {}
12941290
}
12951291
SmallVector::one(hir::ItemId { id: i.id })
12961292
}
12971293

1298-
pub fn lower_item(&mut self, i: &Item) -> hir::Item {
1294+
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
12991295
let mut name = i.ident.name;
13001296
let attrs = self.lower_attrs(&i.attrs);
13011297
let mut vis = self.lower_visibility(&i.vis);
1298+
if let ItemKind::MacroDef(ref tts) = i.node {
1299+
if i.attrs.iter().any(|attr| attr.name() == "macro_export") {
1300+
self.exported_macros.push(hir::MacroDef {
1301+
name: name, attrs: attrs, id: i.id, span: i.span, body: tts.clone().into(),
1302+
});
1303+
}
1304+
return None;
1305+
}
1306+
13021307
let node = self.with_parent_def(i.id, |this| {
13031308
this.lower_item_kind(i.id, &mut name, &attrs, &mut vis, &i.node)
13041309
});
13051310

1306-
hir::Item {
1311+
Some(hir::Item {
13071312
id: i.id,
13081313
name: name,
13091314
attrs: attrs,
13101315
node: node,
13111316
vis: vis,
13121317
span: i.span,
1313-
}
1318+
})
13141319
}
13151320

13161321
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};
@@ -708,8 +707,6 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
708707
krate
709708
});
710709

711-
krate.exported_macros = mem::replace(&mut resolver.exported_macros, Vec::new());
712-
713710
krate = time(time_passes, "maybe building test harness", || {
714711
syntax::test::modify_for_testing(&sess.parse_sess,
715712
&mut resolver,

src/librustc_metadata/cstore_impl.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,13 @@ impl CrateStore for cstore::CStore {
414414
sess.imported_macro_spans.borrow_mut()
415415
.insert(local_span, (name.to_string(), data.get_span(id.index, sess)));
416416

417-
LoadedMacro::MacroRules(ast::MacroDef {
417+
LoadedMacro::MacroDef(ast::Item {
418418
ident: ast::Ident::with_empty_ctxt(name),
419419
id: ast::DUMMY_NODE_ID,
420420
span: local_span,
421421
attrs: attrs,
422-
body: body.into(),
422+
node: ast::ItemKind::MacroDef(body.into()),
423+
vis: ast::Visibility::Inherited,
423424
})
424425
}
425426

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

+20-21
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

@@ -493,6 +492,16 @@ impl<'a> Resolver<'a> {
493492
})
494493
}
495494

495+
pub fn macro_def_scope(&mut self, expansion: Mark) -> Module<'a> {
496+
let def_id = self.macro_defs[&expansion];
497+
if let Some(id) = self.definitions.as_local_node_id(def_id) {
498+
self.local_macro_def_scopes[&id]
499+
} else {
500+
let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();
501+
self.get_extern_crate_root(module_def_id.krate)
502+
}
503+
}
504+
496505
pub fn get_macro(&mut self, def: Def) -> Rc<SyntaxExtension> {
497506
let def_id = match def {
498507
Def::Macro(def_id, ..) => def_id,
@@ -502,22 +511,12 @@ impl<'a> Resolver<'a> {
502511
return ext.clone();
503512
}
504513

505-
let mut macro_rules = match self.session.cstore.load_macro(def_id, &self.session) {
506-
LoadedMacro::MacroRules(macro_rules) => macro_rules,
514+
let macro_def = match self.session.cstore.load_macro(def_id, &self.session) {
515+
LoadedMacro::MacroDef(macro_def) => macro_def,
507516
LoadedMacro::ProcMacro(ext) => return ext,
508517
};
509518

510-
let mark = Mark::fresh();
511-
let invocation = self.arenas.alloc_invocation_data(InvocationData {
512-
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),
517-
});
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));
519+
let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, &macro_def));
521520
self.macro_map.insert(def_id, ext.clone());
522521
ext
523522
}
@@ -707,12 +706,12 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
707706

708707
fn visit_item(&mut self, item: &'a Item) {
709708
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-
}
709+
ItemKind::MacroDef(..) => {
710+
self.resolver.define_macro(item, &mut self.legacy_scope);
711+
return
712+
}
713+
ItemKind::Mac(..) => {
714+
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
716715
return
717716
}
718717
ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),

0 commit comments

Comments
 (0)