Skip to content

Commit 9c6430b

Browse files
committed
Refactor out ast::MacroDef.
1 parent cf747fc commit 9c6430b

File tree

10 files changed

+36
-14
lines changed

10 files changed

+36
-14
lines changed

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ impl<'a> LoweringContext<'a> {
15051505
if let ItemKind::MacroDef(ref tts) = i.node {
15061506
if i.attrs.iter().any(|attr| attr.path == "macro_export") {
15071507
self.exported_macros.push(hir::MacroDef {
1508-
name: name, attrs: attrs, id: i.id, span: i.span, body: tts.clone().into(),
1508+
name: name, attrs: attrs, id: i.id, span: i.span, body: tts.stream(),
15091509
});
15101510
}
15111511
return None;

src/librustc/lint/context.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use hir;
4949
use hir::def_id::LOCAL_CRATE;
5050
use hir::intravisit as hir_visit;
5151
use syntax::visit as ast_visit;
52-
use syntax::tokenstream::ThinTokenStream;
5352

5453
/// Information about the registered lints.
5554
///
@@ -1127,7 +1126,7 @@ impl<'a> ast_visit::Visitor<'a> for EarlyContext<'a> {
11271126
run_lints!(self, check_attribute, early_passes, attr);
11281127
}
11291128

1130-
fn visit_mac_def(&mut self, _mac: &'a ThinTokenStream, id: ast::NodeId) {
1129+
fn visit_mac_def(&mut self, _mac: &'a ast::MacroDef, id: ast::NodeId) {
11311130
let lints = self.sess.lints.borrow_mut().take(id);
11321131
for early_lint in lints {
11331132
self.early_lint(&early_lint);

src/librustc_metadata/cstore_impl.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ impl CrateStore for cstore::CStore {
386386
id: ast::DUMMY_NODE_ID,
387387
span: local_span,
388388
attrs: attrs.iter().cloned().collect(),
389-
node: ast::ItemKind::MacroDef(body.into()),
389+
node: ast::ItemKind::MacroDef(ast::MacroDef {
390+
tokens: body.into(),
391+
}),
390392
vis: ast::Visibility::Inherited,
391393
})
392394
}

src/librustdoc/visit_ast.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use std::mem;
1616
use syntax::abi;
1717
use syntax::ast;
1818
use syntax::attr;
19-
use syntax::tokenstream::TokenStream;
2019
use syntax_pos::Span;
2120

2221
use rustc::hir::map as hir_map;
@@ -214,8 +213,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
214213
LoadedMacro::ProcMacro(..) => continue,
215214
};
216215

217-
let matchers = if let ast::ItemKind::MacroDef(ref tokens) = def.node {
218-
let tts: Vec<_> = TokenStream::from(tokens.clone()).into_trees().collect();
216+
let matchers = if let ast::ItemKind::MacroDef(ref def) = def.node {
217+
let tts: Vec<_> = def.stream().into_trees().collect();
219218
tts.chunks(4).map(|arm| arm[0].span()).collect()
220219
} else {
221220
unreachable!()

src/libsyntax/ast.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,17 @@ impl Mac_ {
10191019
}
10201020
}
10211021

1022+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1023+
pub struct MacroDef {
1024+
pub tokens: ThinTokenStream,
1025+
}
1026+
1027+
impl MacroDef {
1028+
pub fn stream(&self) -> TokenStream {
1029+
self.tokens.clone().into()
1030+
}
1031+
}
1032+
10221033
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
10231034
pub enum StrStyle {
10241035
/// A regular string, like `"foo"`
@@ -1863,7 +1874,7 @@ pub enum ItemKind {
18631874
Mac(Mac),
18641875

18651876
/// A macro definition.
1866-
MacroDef(ThinTokenStream),
1877+
MacroDef(MacroDef),
18671878
}
18681879

18691880
impl ItemKind {

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ pub fn compile(sess: &ParseSess, features: &RefCell<Features>, def: &ast::Item)
189189

190190
// Parse the macro_rules! invocation
191191
let body = match def.node {
192-
ast::ItemKind::MacroDef(ref body) => body.clone().into(),
192+
ast::ItemKind::MacroDef(ref body) => body.stream(),
193193
_ => unreachable!(),
194194
};
195195
let argument_map = match parse(sess, body, &argument_gram, None, true) {

src/libsyntax/fold.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ pub trait Folder : Sized {
189189
// fold::noop_fold_mac(_mac, self)
190190
}
191191

192+
fn fold_macro_def(&mut self, def: MacroDef) -> MacroDef {
193+
noop_fold_macro_def(def, self)
194+
}
195+
192196
fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime {
193197
noop_fold_lifetime(l, self)
194198
}
@@ -515,6 +519,12 @@ pub fn noop_fold_mac<T: Folder>(Spanned {node, span}: Mac, fld: &mut T) -> Mac {
515519
}
516520
}
517521

522+
pub fn noop_fold_macro_def<T: Folder>(def: MacroDef, fld: &mut T) -> MacroDef {
523+
MacroDef {
524+
tokens: fld.fold_tts(def.tokens.into()).into(),
525+
}
526+
}
527+
518528
pub fn noop_fold_meta_list_item<T: Folder>(li: NestedMetaItem, fld: &mut T)
519529
-> NestedMetaItem {
520530
Spanned {
@@ -919,7 +929,7 @@ pub fn noop_fold_item_kind<T: Folder>(i: ItemKind, folder: &mut T) -> ItemKind {
919929
items.move_flat_map(|item| folder.fold_trait_item(item)),
920930
),
921931
ItemKind::Mac(m) => ItemKind::Mac(folder.fold_mac(m)),
922-
ItemKind::MacroDef(tts) => ItemKind::MacroDef(folder.fold_tts(tts.into()).into()),
932+
ItemKind::MacroDef(def) => ItemKind::MacroDef(folder.fold_macro_def(def)),
923933
}
924934
}
925935

src/libsyntax/parse/parser.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3781,7 +3781,9 @@ impl<'a> Parser<'a> {
37813781
}
37823782

37833783
let span = lo.to(self.prev_span);
3784-
let kind = ItemKind::MacroDef(tts);
3784+
let kind = ItemKind::MacroDef(ast::MacroDef {
3785+
tokens: tts,
3786+
});
37853787
Ok(Some(self.mk_item(span, id, kind, Visibility::Inherited, attrs.to_owned())))
37863788
}
37873789

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ impl<'a> State<'a> {
13921392
self.print_ident(item.ident)?;
13931393
self.cbox(INDENT_UNIT)?;
13941394
self.popen()?;
1395-
self.print_tts(tts.clone().into())?;
1395+
self.print_tts(tts.stream())?;
13961396
self.pclose()?;
13971397
word(&mut self.s, ";")?;
13981398
self.end()?;

src/libsyntax/visit.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use abi::Abi;
2727
use ast::*;
2828
use syntax_pos::Span;
2929
use codemap::Spanned;
30-
use tokenstream::ThinTokenStream;
3130

3231
#[derive(Copy, Clone, PartialEq, Eq)]
3332
pub enum FnKind<'a> {
@@ -113,7 +112,7 @@ pub trait Visitor<'ast>: Sized {
113112
// definition in your trait impl:
114113
// visit::walk_mac(self, _mac)
115114
}
116-
fn visit_mac_def(&mut self, _mac: &'ast ThinTokenStream, _id: NodeId) {
115+
fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
117116
// Nothing to do
118117
}
119118
fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {

0 commit comments

Comments
 (0)