Skip to content

Commit c07ff8d

Browse files
committed
Add module ext::placeholders with placeholder() and PlaceholderExpander.
1 parent 7a3ae57 commit c07ff8d

File tree

3 files changed

+182
-39
lines changed

3 files changed

+182
-39
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use ast::{Block, Crate, Ident, Mac_, PatKind};
1212
use ast::{MacStmtStyle, StmtKind, ItemKind};
1313
use ast;
1414
use ext::hygiene::Mark;
15+
use ext::placeholders;
1516
use attr::{self, HasAttrs};
16-
use codemap::{dummy_spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
17+
use codemap::{ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
1718
use syntax_pos::{self, Span, ExpnId};
1819
use config::StripUnconfigured;
1920
use ext::base::*;
@@ -35,8 +36,8 @@ macro_rules! expansions {
3536
$(.$fold:ident)* $(lift .$fold_elt:ident)*,
3637
$(.$visit:ident)* $(lift .$visit_elt:ident)*;)*) => {
3738
#[derive(Copy, Clone)]
38-
enum ExpansionKind { OptExpr, $( $kind, )* }
39-
enum Expansion { OptExpr(Option<P<ast::Expr>>), $( $kind($ty), )* }
39+
pub enum ExpansionKind { OptExpr, $( $kind, )* }
40+
pub enum Expansion { OptExpr(Option<P<ast::Expr>>), $( $kind($ty), )* }
4041

4142
impl ExpansionKind {
4243
fn name(self) -> &'static str {
@@ -55,20 +56,20 @@ macro_rules! expansions {
5556
}
5657

5758
impl Expansion {
58-
fn make_opt_expr(self) -> Option<P<ast::Expr>> {
59+
pub fn make_opt_expr(self) -> Option<P<ast::Expr>> {
5960
match self {
6061
Expansion::OptExpr(expr) => expr,
6162
_ => panic!("Expansion::make_* called on the wrong kind of expansion"),
6263
}
6364
}
64-
$( fn $make(self) -> $ty {
65+
$( pub fn $make(self) -> $ty {
6566
match self {
6667
Expansion::$kind(ast) => ast,
6768
_ => panic!("Expansion::make_* called on the wrong kind of expansion"),
6869
}
6970
} )*
7071

71-
fn fold_with<F: Folder>(self, folder: &mut F) -> Self {
72+
pub fn fold_with<F: Folder>(self, folder: &mut F) -> Self {
7273
use self::Expansion::*;
7374
match self {
7475
OptExpr(expr) => OptExpr(expr.and_then(|expr| folder.fold_opt_expr(expr))),
@@ -434,9 +435,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
434435

435436
// If keep_macs is true, expands to a MacEager::items instead.
436437
if self.keep_macs {
437-
Some(reconstruct_macro_rules(&def, &path))
438+
Some(placeholders::reconstructed_macro_rules(&def, &path))
438439
} else {
439-
Some(macro_scope_placeholder())
440+
Some(placeholders::macro_scope_placeholder())
440441
}
441442
}
442443

@@ -650,37 +651,6 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
650651
}
651652
}
652653

653-
fn macro_scope_placeholder() -> Expansion {
654-
Expansion::Items(SmallVector::one(P(ast::Item {
655-
ident: keywords::Invalid.ident(),
656-
attrs: Vec::new(),
657-
id: ast::DUMMY_NODE_ID,
658-
node: ast::ItemKind::Mac(dummy_spanned(ast::Mac_ {
659-
path: ast::Path { span: syntax_pos::DUMMY_SP, global: false, segments: Vec::new() },
660-
tts: Vec::new(),
661-
})),
662-
vis: ast::Visibility::Inherited,
663-
span: syntax_pos::DUMMY_SP,
664-
})))
665-
}
666-
667-
fn reconstruct_macro_rules(def: &ast::MacroDef, path: &ast::Path) -> Expansion {
668-
Expansion::Items(SmallVector::one(P(ast::Item {
669-
ident: def.ident,
670-
attrs: def.attrs.clone(),
671-
id: ast::DUMMY_NODE_ID,
672-
node: ast::ItemKind::Mac(ast::Mac {
673-
span: def.span,
674-
node: ast::Mac_ {
675-
path: path.clone(),
676-
tts: def.body.clone(),
677-
}
678-
}),
679-
vis: ast::Visibility::Inherited,
680-
span: def.span,
681-
})))
682-
}
683-
684654
pub struct ExpansionConfig<'feat> {
685655
pub crate_name: String,
686656
pub features: Option<&'feat Features>,

src/libsyntax/ext/placeholders.rs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use ast;
12+
use codemap::{DUMMY_SP, dummy_spanned};
13+
use ext::expand::{Expansion, ExpansionKind};
14+
use fold::*;
15+
use parse::token::keywords;
16+
use ptr::P;
17+
use util::small_vector::SmallVector;
18+
19+
use std::collections::HashMap;
20+
21+
pub fn placeholder(kind: ExpansionKind, id: ast::NodeId) -> Expansion {
22+
fn mac_placeholder() -> ast::Mac {
23+
dummy_spanned(ast::Mac_ {
24+
path: ast::Path { span: DUMMY_SP, global: false, segments: Vec::new() },
25+
tts: Vec::new(),
26+
})
27+
}
28+
29+
let ident = keywords::Invalid.ident();
30+
let attrs = Vec::new();
31+
let vis = ast::Visibility::Inherited;
32+
let span = DUMMY_SP;
33+
let expr_placeholder = || P(ast::Expr {
34+
id: id, span: span,
35+
attrs: ast::ThinVec::new(),
36+
node: ast::ExprKind::Mac(mac_placeholder()),
37+
});
38+
39+
match kind {
40+
ExpansionKind::Expr => Expansion::Expr(expr_placeholder()),
41+
ExpansionKind::OptExpr => Expansion::OptExpr(Some(expr_placeholder())),
42+
ExpansionKind::Items => Expansion::Items(SmallVector::one(P(ast::Item {
43+
id: id, span: span, ident: ident, vis: vis, attrs: attrs,
44+
node: ast::ItemKind::Mac(mac_placeholder()),
45+
}))),
46+
ExpansionKind::TraitItems => Expansion::TraitItems(SmallVector::one(ast::TraitItem {
47+
id: id, span: span, ident: ident, attrs: attrs,
48+
node: ast::TraitItemKind::Macro(mac_placeholder()),
49+
})),
50+
ExpansionKind::ImplItems => Expansion::ImplItems(SmallVector::one(ast::ImplItem {
51+
id: id, span: span, ident: ident, vis: vis, attrs: attrs,
52+
node: ast::ImplItemKind::Macro(mac_placeholder()),
53+
defaultness: ast::Defaultness::Final,
54+
})),
55+
ExpansionKind::Pat => Expansion::Pat(P(ast::Pat {
56+
id: id, span: span, node: ast::PatKind::Mac(mac_placeholder()),
57+
})),
58+
ExpansionKind::Ty => Expansion::Ty(P(ast::Ty {
59+
id: id, span: span, node: ast::TyKind::Mac(mac_placeholder()),
60+
})),
61+
ExpansionKind::Stmts => Expansion::Stmts(SmallVector::one({
62+
let mac = P((mac_placeholder(), ast::MacStmtStyle::Braces, ast::ThinVec::new()));
63+
ast::Stmt { id: id, span: span, node: ast::StmtKind::Mac(mac) }
64+
})),
65+
}
66+
}
67+
68+
pub fn macro_scope_placeholder() -> Expansion {
69+
placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
70+
}
71+
72+
pub struct PlaceholderExpander {
73+
expansions: HashMap<ast::NodeId, Expansion>,
74+
}
75+
76+
impl PlaceholderExpander {
77+
pub fn new(expansions: HashMap<ast::NodeId, Expansion>) -> Self {
78+
PlaceholderExpander {
79+
expansions: expansions,
80+
}
81+
}
82+
83+
pub fn remove(&mut self, id: ast::NodeId) -> Expansion {
84+
let expansion = self.expansions.remove(&id).unwrap();
85+
expansion.fold_with(self)
86+
}
87+
}
88+
89+
impl Folder for PlaceholderExpander {
90+
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
91+
match item.node {
92+
// Scope placeholder
93+
ast::ItemKind::Mac(_) if item.id == ast::DUMMY_NODE_ID => SmallVector::one(item),
94+
ast::ItemKind::Mac(_) => self.remove(item.id).make_items(),
95+
_ => noop_fold_item(item, self),
96+
}
97+
}
98+
99+
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
100+
match item.node {
101+
ast::TraitItemKind::Macro(_) => self.remove(item.id).make_trait_items(),
102+
_ => noop_fold_trait_item(item, self),
103+
}
104+
}
105+
106+
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
107+
match item.node {
108+
ast::ImplItemKind::Macro(_) => self.remove(item.id).make_impl_items(),
109+
_ => noop_fold_impl_item(item, self),
110+
}
111+
}
112+
113+
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
114+
match expr.node {
115+
ast::ExprKind::Mac(_) => self.remove(expr.id).make_expr(),
116+
_ => expr.map(|expr| noop_fold_expr(expr, self)),
117+
}
118+
}
119+
120+
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
121+
match expr.node {
122+
ast::ExprKind::Mac(_) => self.remove(expr.id).make_opt_expr(),
123+
_ => noop_fold_opt_expr(expr, self),
124+
}
125+
}
126+
127+
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
128+
let (style, mut expansion) = match stmt.node {
129+
ast::StmtKind::Mac(mac) => (mac.1, self.remove(stmt.id).make_stmts()),
130+
_ => return noop_fold_stmt(stmt, self),
131+
};
132+
133+
if style == ast::MacStmtStyle::Semicolon {
134+
if let Some(stmt) = expansion.pop() {
135+
expansion.push(stmt.add_trailing_semicolon());
136+
}
137+
}
138+
139+
expansion
140+
}
141+
142+
fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
143+
match pat.node {
144+
ast::PatKind::Mac(_) => self.remove(pat.id).make_pat(),
145+
_ => noop_fold_pat(pat, self),
146+
}
147+
}
148+
149+
fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> {
150+
match ty.node {
151+
ast::TyKind::Mac(_) => self.remove(ty.id).make_ty(),
152+
_ => noop_fold_ty(ty, self),
153+
}
154+
}
155+
}
156+
157+
pub fn reconstructed_macro_rules(def: &ast::MacroDef, path: &ast::Path) -> Expansion {
158+
Expansion::Items(SmallVector::one(P(ast::Item {
159+
ident: def.ident,
160+
attrs: def.attrs.clone(),
161+
id: ast::DUMMY_NODE_ID,
162+
node: ast::ItemKind::Mac(ast::Mac {
163+
span: def.span,
164+
node: ast::Mac_ {
165+
path: path.clone(),
166+
tts: def.body.clone(),
167+
}
168+
}),
169+
vis: ast::Visibility::Inherited,
170+
span: def.span,
171+
})))
172+
}

src/libsyntax/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ pub mod ext {
126126
pub mod base;
127127
pub mod build;
128128
pub mod expand;
129+
pub mod placeholders;
129130
pub mod hygiene;
130131
pub mod proc_macro_shim;
131132
pub mod quote;

0 commit comments

Comments
 (0)