Skip to content

Commit 9b2f762

Browse files
committed
syntax: Add tokens: Option<TokenStream> to Item
This commit adds a new field to the `Item` AST node in libsyntax to optionally contain the original token stream that the item itself was parsed from. This is currently `None` everywhere but is intended for use later with procedural macros.
1 parent 6f815ca commit 9b2f762

File tree

11 files changed

+35
-9
lines changed

11 files changed

+35
-9
lines changed

src/librustc_metadata/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ impl CrateStore for cstore::CStore {
389389
legacy: def.legacy,
390390
}),
391391
vis: ast::Visibility::Inherited,
392+
tokens: None,
392393
})
393394
}
394395

src/libsyntax/ast.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,6 +1812,12 @@ pub struct Item {
18121812
pub node: ItemKind,
18131813
pub vis: Visibility,
18141814
pub span: Span,
1815+
1816+
/// Original tokens this item was parsed from. This isn't necessarily
1817+
/// available for all items, although over time more and more items should
1818+
/// have this be `Some`. Right now this is primarily used for procedural
1819+
/// macros, notably custom attributes.
1820+
pub tokens: Option<TokenStream>,
18151821
}
18161822

18171823
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]

src/libsyntax/diagnostics/plugin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
236236
),
237237
vis: ast::Visibility::Public,
238238
span: span,
239+
tokens: None,
239240
})
240241
]))
241242
}

src/libsyntax/ext/build.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
979979
id: ast::DUMMY_NODE_ID,
980980
node: node,
981981
vis: ast::Visibility::Inherited,
982-
span: span
982+
span: span,
983+
tokens: None,
983984
})
984985
}
985986

@@ -1147,7 +1148,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
11471148
attrs: vec![],
11481149
node: ast::ItemKind::Use(vp),
11491150
vis: vis,
1150-
span: sp
1151+
span: sp,
1152+
tokens: None,
11511153
})
11521154
}
11531155

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
214214
ident: keywords::Invalid.ident(),
215215
id: ast::DUMMY_NODE_ID,
216216
vis: ast::Visibility::Public,
217+
tokens: None,
217218
})));
218219

219220
match self.expand(krate_item).make_items().pop().map(P::unwrap) {

src/libsyntax/ext/placeholders.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub fn placeholder(kind: ExpansionKind, id: ast::NodeId) -> Expansion {
4646
ExpansionKind::Items => Expansion::Items(SmallVector::one(P(ast::Item {
4747
id: id, span: span, ident: ident, vis: vis, attrs: attrs,
4848
node: ast::ItemKind::Mac(mac_placeholder()),
49+
tokens: None,
4950
}))),
5051
ExpansionKind::TraitItems => Expansion::TraitItems(SmallVector::one(ast::TraitItem {
5152
id: id, span: span, ident: ident, attrs: attrs,

src/libsyntax/fold.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, span}: Crate,
10001000
vis: ast::Visibility::Public,
10011001
span: span,
10021002
node: ast::ItemKind::Mod(module),
1003+
tokens: None,
10031004
})).into_iter();
10041005

10051006
let (module, attrs, span) = match items.next() {
@@ -1032,15 +1033,18 @@ pub fn noop_fold_item<T: Folder>(i: P<Item>, folder: &mut T) -> SmallVector<P<It
10321033
}
10331034

10341035
// fold one item into exactly one item
1035-
pub fn noop_fold_item_simple<T: Folder>(Item {id, ident, attrs, node, vis, span}: Item,
1036+
pub fn noop_fold_item_simple<T: Folder>(Item {id, ident, attrs, node, vis, span, tokens}: Item,
10361037
folder: &mut T) -> Item {
10371038
Item {
10381039
id: folder.new_id(id),
10391040
vis: folder.fold_vis(vis),
10401041
ident: folder.fold_ident(ident),
10411042
attrs: fold_attrs(attrs, folder),
10421043
node: folder.fold_item_kind(node),
1043-
span: folder.new_span(span)
1044+
span: folder.new_span(span),
1045+
tokens: tokens.map(|tokens| {
1046+
folder.fold_tts(tokens.into()).into()
1047+
}),
10441048
}
10451049
}
10461050

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4653,6 +4653,7 @@ impl<'a> Parser<'a> {
46534653
node: node,
46544654
vis: vis,
46554655
span: span,
4656+
tokens: None, // TODO: fill this in
46564657
})
46574658
}
46584659

src/libsyntax/std_inject.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<Strin
6060
ident: ast::Ident::from_str(name),
6161
id: ast::DUMMY_NODE_ID,
6262
span: DUMMY_SP,
63+
tokens: None,
6364
}));
6465

6566
let span = ignored_span(DUMMY_SP);
@@ -82,6 +83,7 @@ pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<Strin
8283
id: ast::DUMMY_NODE_ID,
8384
ident: keywords::Invalid.ident(),
8485
span: span,
86+
tokens: None,
8587
}));
8688

8789
krate

src/libsyntax/test.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl fold::Folder for EntryPointCleaner {
192192
EntryPointType::MainNamed |
193193
EntryPointType::MainAttr |
194194
EntryPointType::Start =>
195-
folded.map(|ast::Item {id, ident, attrs, node, vis, span}| {
195+
folded.map(|ast::Item {id, ident, attrs, node, vis, span, tokens}| {
196196
let allow_str = Symbol::intern("allow");
197197
let dead_code_str = Symbol::intern("dead_code");
198198
let word_vec = vec![attr::mk_list_word_item(dead_code_str)];
@@ -212,7 +212,8 @@ impl fold::Folder for EntryPointCleaner {
212212
.collect(),
213213
node: node,
214214
vis: vis,
215-
span: span
215+
span: span,
216+
tokens: tokens,
216217
}
217218
}),
218219
EntryPointType::None |
@@ -255,6 +256,7 @@ fn mk_reexport_mod(cx: &mut TestCtxt,
255256
node: ast::ItemKind::Mod(reexport_mod),
256257
vis: ast::Visibility::Public,
257258
span: DUMMY_SP,
259+
tokens: None,
258260
})).pop().unwrap();
259261

260262
(it, sym)
@@ -465,7 +467,8 @@ fn mk_std(cx: &TestCtxt) -> P<ast::Item> {
465467
node: vi,
466468
attrs: vec![],
467469
vis: vis,
468-
span: sp
470+
span: sp,
471+
tokens: None,
469472
})
470473
}
471474

@@ -506,7 +509,8 @@ fn mk_main(cx: &mut TestCtxt) -> P<ast::Item> {
506509
id: ast::DUMMY_NODE_ID,
507510
node: main,
508511
vis: ast::Visibility::Public,
509-
span: sp
512+
span: sp,
513+
tokens: None,
510514
})
511515
}
512516

@@ -536,6 +540,7 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<P<ast::Item>>) {
536540
node: item_,
537541
vis: ast::Visibility::Public,
538542
span: DUMMY_SP,
543+
tokens: None,
539544
})).pop().unwrap();
540545
let reexport = cx.reexport_test_harness_main.map(|s| {
541546
// building `use <ident> = __test::main`
@@ -551,7 +556,8 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<P<ast::Item>>) {
551556
attrs: vec![],
552557
node: ast::ItemKind::Use(P(use_path)),
553558
vis: ast::Visibility::Inherited,
554-
span: DUMMY_SP
559+
span: DUMMY_SP,
560+
tokens: None,
555561
})).pop().unwrap()
556562
});
557563

src/libsyntax_ext/global_asm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@ pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt,
6161
})),
6262
vis: ast::Visibility::Inherited,
6363
span: sp,
64+
tokens: None,
6465
})))
6566
}

0 commit comments

Comments
 (0)