Skip to content

Commit ed4b312

Browse files
bors[bot]matklad
andauthored
Merge #10514
10514: internal: clean up code duplication r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 4d80fd1 + d4d6740 commit ed4b312

File tree

5 files changed

+45
-77
lines changed

5 files changed

+45
-77
lines changed

crates/hir_expand/src/db.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ static TOKEN_LIMIT: Limit = Limit::new(524_288);
2828

2929
#[derive(Debug, Clone, Eq, PartialEq)]
3030
pub enum TokenExpander {
31-
/// Old-style `macro_rules`.
32-
MacroRules { mac: mbe::MacroRules, def_site_token_map: mbe::TokenMap },
33-
/// AKA macros 2.0.
34-
MacroDef { mac: mbe::MacroDef, def_site_token_map: mbe::TokenMap },
31+
/// Old-style `macro_rules` or the new macros 2.0
32+
DeclarativeMacro { mac: mbe::DeclarativeMacro, def_site_token_map: mbe::TokenMap },
3533
/// Stuff like `line!` and `file!`.
3634
Builtin(BuiltinFnLikeExpander),
3735
/// `global_allocator` and such.
@@ -50,8 +48,7 @@ impl TokenExpander {
5048
tt: &tt::Subtree,
5149
) -> mbe::ExpandResult<tt::Subtree> {
5250
match self {
53-
TokenExpander::MacroRules { mac, .. } => mac.expand(tt),
54-
TokenExpander::MacroDef { mac, .. } => mac.expand(tt),
51+
TokenExpander::DeclarativeMacro { mac, .. } => mac.expand(tt),
5552
TokenExpander::Builtin(it) => it.expand(db, id, tt),
5653
TokenExpander::BuiltinAttr(it) => it.expand(db, id, tt),
5754
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt),
@@ -66,8 +63,7 @@ impl TokenExpander {
6663

6764
pub(crate) fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
6865
match self {
69-
TokenExpander::MacroRules { mac, .. } => mac.map_id_down(id),
70-
TokenExpander::MacroDef { mac, .. } => mac.map_id_down(id),
66+
TokenExpander::DeclarativeMacro { mac, .. } => mac.map_id_down(id),
7167
TokenExpander::Builtin(..)
7268
| TokenExpander::BuiltinAttr(..)
7369
| TokenExpander::BuiltinDerive(..)
@@ -77,8 +73,7 @@ impl TokenExpander {
7773

7874
pub(crate) fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) {
7975
match self {
80-
TokenExpander::MacroRules { mac, .. } => mac.map_id_up(id),
81-
TokenExpander::MacroDef { mac, .. } => mac.map_id_up(id),
76+
TokenExpander::DeclarativeMacro { mac, .. } => mac.map_id_up(id),
8277
TokenExpander::Builtin(..)
8378
| TokenExpander::BuiltinAttr(..)
8479
| TokenExpander::BuiltinDerive(..)
@@ -368,24 +363,27 @@ fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
368363

369364
fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Result<Arc<TokenExpander>, mbe::ParseError> {
370365
match id.kind {
371-
MacroDefKind::Declarative(ast_id) => match ast_id.to_node(db) {
372-
ast::Macro::MacroRules(macro_rules) => {
373-
let arg = macro_rules
374-
.token_tree()
375-
.ok_or_else(|| mbe::ParseError::Expected("expected a token tree".into()))?;
376-
let (tt, def_site_token_map) = mbe::syntax_node_to_token_tree(arg.syntax());
377-
let mac = mbe::MacroRules::parse(&tt)?;
378-
Ok(Arc::new(TokenExpander::MacroRules { mac, def_site_token_map }))
379-
}
380-
ast::Macro::MacroDef(macro_def) => {
381-
let arg = macro_def
382-
.body()
383-
.ok_or_else(|| mbe::ParseError::Expected("expected a token tree".into()))?;
384-
let (tt, def_site_token_map) = mbe::syntax_node_to_token_tree(arg.syntax());
385-
let mac = mbe::MacroDef::parse(&tt)?;
386-
Ok(Arc::new(TokenExpander::MacroDef { mac, def_site_token_map }))
387-
}
388-
},
366+
MacroDefKind::Declarative(ast_id) => {
367+
let (mac, def_site_token_map) = match ast_id.to_node(db) {
368+
ast::Macro::MacroRules(macro_rules) => {
369+
let arg = macro_rules
370+
.token_tree()
371+
.ok_or_else(|| mbe::ParseError::Expected("expected a token tree".into()))?;
372+
let (tt, def_site_token_map) = mbe::syntax_node_to_token_tree(arg.syntax());
373+
let mac = mbe::DeclarativeMacro::parse_macro_rules(&tt)?;
374+
(mac, def_site_token_map)
375+
}
376+
ast::Macro::MacroDef(macro_def) => {
377+
let arg = macro_def
378+
.body()
379+
.ok_or_else(|| mbe::ParseError::Expected("expected a token tree".into()))?;
380+
let (tt, def_site_token_map) = mbe::syntax_node_to_token_tree(arg.syntax());
381+
let mac = mbe::DeclarativeMacro::parse_macro2(&tt)?;
382+
(mac, def_site_token_map)
383+
}
384+
};
385+
Ok(Arc::new(TokenExpander::DeclarativeMacro { mac, def_site_token_map }))
386+
}
389387
MacroDefKind::BuiltIn(expander, _) => Ok(Arc::new(TokenExpander::Builtin(expander))),
390388
MacroDefKind::BuiltInAttr(expander, _) => {
391389
Ok(Arc::new(TokenExpander::BuiltinAttr(expander)))

crates/hir_expand/src/hygiene.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,9 @@ impl HygieneInfo {
160160
InFile::new(loc.kind.file_id(), loc.kind.arg(db)?.text_range().start()),
161161
),
162162
mbe::Origin::Def => match (&*self.macro_def, &self.attr_input_or_mac_def_start) {
163-
(
164-
TokenExpander::MacroDef { def_site_token_map, .. }
165-
| TokenExpander::MacroRules { def_site_token_map, .. },
166-
Some(tt),
167-
) => (def_site_token_map, *tt),
163+
(TokenExpander::DeclarativeMacro { def_site_token_map, .. }, Some(tt)) => {
164+
(def_site_token_map, *tt)
165+
}
168166
_ => panic!("`Origin::Def` used with non-`macro_rules!` macro"),
169167
},
170168
},

crates/hir_expand/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,9 @@ impl ExpansionInfo {
465465
_ => match origin {
466466
mbe::Origin::Call => (&self.macro_arg.1, self.arg.clone()),
467467
mbe::Origin::Def => match (&*self.macro_def, &self.attr_input_or_mac_def) {
468-
(
469-
TokenExpander::MacroRules { def_site_token_map, .. }
470-
| TokenExpander::MacroDef { def_site_token_map, .. },
471-
Some(tt),
472-
) => (def_site_token_map, tt.syntax().cloned()),
468+
(TokenExpander::DeclarativeMacro { def_site_token_map, .. }, Some(tt)) => {
469+
(def_site_token_map, tt.syntax().cloned())
470+
}
473471
_ => panic!("`Origin::Def` used with non-`macro_rules!` macro"),
474472
},
475473
},

crates/mbe/src/benchmark.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use test_utils::{bench, bench_fixture, skip_slow_tests};
99

1010
use crate::{
1111
parser::{Op, RepeatKind, Separator},
12-
syntax_node_to_token_tree, MacroRules,
12+
syntax_node_to_token_tree, DeclarativeMacro,
1313
};
1414

1515
#[test]
@@ -20,7 +20,7 @@ fn benchmark_parse_macro_rules() {
2020
let rules = macro_rules_fixtures_tt();
2121
let hash: usize = {
2222
let _pt = bench("mbe parse macro rules");
23-
rules.values().map(|it| MacroRules::parse(it).unwrap().rules.len()).sum()
23+
rules.values().map(|it| DeclarativeMacro::parse_macro_rules(it).unwrap().rules.len()).sum()
2424
};
2525
assert_eq!(hash, 1144);
2626
}
@@ -47,10 +47,10 @@ fn benchmark_expand_macro_rules() {
4747
assert_eq!(hash, 69413);
4848
}
4949

50-
fn macro_rules_fixtures() -> FxHashMap<String, MacroRules> {
50+
fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro> {
5151
macro_rules_fixtures_tt()
5252
.into_iter()
53-
.map(|(id, tt)| (id, MacroRules::parse(&tt).unwrap()))
53+
.map(|(id, tt)| (id, DeclarativeMacro::parse_macro_rules(&tt).unwrap()))
5454
.collect()
5555
}
5656

@@ -71,7 +71,7 @@ fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree> {
7171
}
7272

7373
/// Generate random invocation fixtures from rules
74-
fn invocation_fixtures(rules: &FxHashMap<String, MacroRules>) -> Vec<(String, tt::Subtree)> {
74+
fn invocation_fixtures(rules: &FxHashMap<String, DeclarativeMacro>) -> Vec<(String, tt::Subtree)> {
7575
let mut seed = 123456789;
7676
let mut res = Vec::new();
7777

crates/mbe/src/lib.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,7 @@ pub use crate::{
8383
/// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident`
8484
/// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
8585
#[derive(Clone, Debug, PartialEq, Eq)]
86-
pub struct MacroRules {
87-
rules: Vec<Rule>,
88-
/// Highest id of the token we have in TokenMap
89-
shift: Shift,
90-
}
91-
92-
/// For Macro 2.0
93-
#[derive(Clone, Debug, PartialEq, Eq)]
94-
pub struct MacroDef {
86+
pub struct DeclarativeMacro {
9587
rules: Vec<Rule>,
9688
/// Highest id of the token we have in TokenMap
9789
shift: Shift,
@@ -176,8 +168,9 @@ pub enum Origin {
176168
Call,
177169
}
178170

179-
impl MacroRules {
180-
pub fn parse(tt: &tt::Subtree) -> Result<MacroRules, ParseError> {
171+
impl DeclarativeMacro {
172+
/// The old, `macro_rules! m {}` flavor.
173+
pub fn parse_macro_rules(tt: &tt::Subtree) -> Result<DeclarativeMacro, ParseError> {
181174
// Note: this parsing can be implemented using mbe machinery itself, by
182175
// matching against `$($lhs:tt => $rhs:tt);*` pattern, but implementing
183176
// manually seems easier.
@@ -198,30 +191,11 @@ impl MacroRules {
198191
validate(&rule.lhs)?;
199192
}
200193

201-
Ok(MacroRules { rules, shift: Shift::new(tt) })
202-
}
203-
204-
pub fn expand(&self, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
205-
// apply shift
206-
let mut tt = tt.clone();
207-
self.shift.shift_all(&mut tt);
208-
expander::expand_rules(&self.rules, &tt)
194+
Ok(DeclarativeMacro { rules, shift: Shift::new(tt) })
209195
}
210196

211-
pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
212-
self.shift.shift(id)
213-
}
214-
215-
pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin) {
216-
match self.shift.unshift(id) {
217-
Some(id) => (id, Origin::Call),
218-
None => (id, Origin::Def),
219-
}
220-
}
221-
}
222-
223-
impl MacroDef {
224-
pub fn parse(tt: &tt::Subtree) -> Result<MacroDef, ParseError> {
197+
/// The new, unstable `macro m {}` flavor.
198+
pub fn parse_macro2(tt: &tt::Subtree) -> Result<DeclarativeMacro, ParseError> {
225199
let mut src = TtIter::new(tt);
226200
let mut rules = Vec::new();
227201

@@ -251,7 +225,7 @@ impl MacroDef {
251225
validate(&rule.lhs)?;
252226
}
253227

254-
Ok(MacroDef { rules, shift: Shift::new(tt) })
228+
Ok(DeclarativeMacro { rules, shift: Shift::new(tt) })
255229
}
256230

257231
pub fn expand(&self, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {

0 commit comments

Comments
 (0)