Skip to content

Commit 522ca27

Browse files
committed
Fix macro hygiene issues with hir
Using the re-exporting trick from rust-lang/rust#52234 (comment) to reference the macro recursively at a known path. This way any external callers don't have to import the macro or its dependencies.
1 parent efef17e commit 522ca27

File tree

5 files changed

+66
-35
lines changed

5 files changed

+66
-35
lines changed

src/data.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
pub mod asm;
22
pub mod ast;
33
pub mod hir;
4-
#[macro_use]
54
pub mod ir;
65
pub mod lir;
76
pub mod operand;

src/data/hir.rs

+53-28
Original file line numberDiff line numberDiff line change
@@ -160,28 +160,42 @@ impl fmt::Display for Statement {
160160
#[macro_export]
161161
macro_rules! hir {
162162
((CONST $($integer:tt)+)) => {
163-
$crate::data::hir::Expression::from(hir!($($integer)+))
163+
$crate::data::hir::Expression::from(
164+
$crate::data::hir::hir!($($integer)+)
165+
)
164166
};
165167
((NAME $($label:tt)+)) => {
166-
$crate::data::hir::Expression::from(hir!($($label)+))
168+
$crate::data::hir::Expression::from(
169+
$crate::data::hir::hir!($($label)+)
170+
)
167171
};
168172
((TEMP $($temporary:tt)+)) => {
169-
$crate::data::hir::Expression::from(hir!($($temporary)+))
173+
$crate::data::hir::Expression::from(
174+
$crate::data::hir::hir!($($temporary)+)
175+
)
170176
};
171177
((MEM $expression:tt)) => {
172-
$crate::data::hir::Expression::Memory(Box::new(hir!($expression)))
178+
$crate::data::hir::Expression::Memory(Box::new(
179+
$crate::data::hir::hir!($expression)
180+
))
173181
};
174182
((CALL $function:tt $returns:tt $($argument:tt)*)) => {
175183
$crate::data::hir::Expression::Call(
176-
Box::new(hir!($function)),
177-
vec![$(hir!($argument),)*],
184+
Box::new(
185+
$crate::data::hir::hir!($function)
186+
),
187+
vec![
188+
$(
189+
$crate::data::hir::hir!($argument),
190+
)*
191+
],
178192
$returns,
179193
)
180194
};
181195
((ESEQ $statement:tt $expression:tt)) => {
182196
$crate::data::hir::Expression::Sequence(
183-
Box::new(hir!($statement)),
184-
Box::new(hir!($expression)),
197+
Box::new($crate::data::hir::hir!($statement)),
198+
Box::new($crate::data::hir::hir!($expression)),
185199
)
186200
};
187201

@@ -190,9 +204,9 @@ macro_rules! hir {
190204
};
191205
((CJUMP ($condition:ident $left:tt $right:tt) $r#true:ident $r#false:ident)) => {
192206
$crate::data::hir::Statement::CJump {
193-
condition: ir!($condition),
194-
left: hir!($left),
195-
right: hir!($right),
207+
condition: $crate::data::ir::ir!($condition),
208+
left: $crate::data::hir::hir!($left),
209+
right: $crate::data::hir::hir!($right),
196210
r#true: $r#true,
197211
r#false: $r#false,
198212
}
@@ -201,36 +215,47 @@ macro_rules! hir {
201215
$crate::data::hir::Statement::Label($label)
202216
};
203217
((EXP $expression:tt)) => {
204-
$crate::data::hir::Statement::Expression(hir!($expression))
218+
$crate::data::hir::Statement::Expression(
219+
$crate::data::hir::hir!($expression)
220+
)
205221
};
206222
((MOVE $into:tt $from:tt)) => {
207223
$crate::data::hir::Statement::Move {
208-
destination: hir!($into),
209-
source: hir!($from),
224+
destination: $crate::data::hir::hir!($into),
225+
source: $crate::data::hir::hir!($from),
210226
}
211227
};
212-
((RETURN $returns:expr)) => {
213-
$crate::data::hir::Statement::Return($returns)
214-
};
215-
((SEQ $statement:tt $($statements:tt)+)) => {
216-
$crate::data::hir::Statement::Sequence(vec![
217-
hir!($statement),
218-
$(hir!($statements),)*
219-
])
228+
((RETURN $($expression:tt)*)) => {
229+
$crate::data::hir::Statement::Return(
230+
vec![
231+
$(
232+
$crate::data::hir::hir!($expression),
233+
)*
234+
]
235+
)
220236
};
221-
((SEQ $statements:expr)) => {
222-
$crate::data::hir::Statement::Sequence($statements)
237+
((SEQ $($statement:tt)*)) => {
238+
$crate::data::hir::Statement::Sequence(
239+
vec![
240+
$(
241+
$crate::data::hir::hir!($statement),
242+
)*
243+
],
244+
)
223245
};
224-
225246
(($binary:ident $left:tt $right:tt)) => {
226247
$crate::data::hir::Expression::Binary(
227-
ir!($binary),
228-
Box::new(hir!($left)),
229-
Box::new(hir!($right)),
248+
$crate::data::ir::ir!($binary),
249+
Box::new($crate::data::hir::hir!($left)),
250+
Box::new($crate::data::hir::hir!($right)),
230251
)
231252
};
232253

233254
($expression:expr) => {
234255
$expression
235256
}
236257
}
258+
259+
// https://github.com/rust-lang/rust/pull/52234#issuecomment-976702997
260+
#[doc(hidden)]
261+
pub use hir;

src/data/ir.rs

+4
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,7 @@ macro_rules! ir {
144144

145145
($ident:ident) => { $ident };
146146
}
147+
148+
// https://github.com/rust-lang/rust/pull/52234#issuecomment-976702997
149+
#[doc(hidden)]
150+
pub use ir;

src/emit/emit.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl<'env> Emitter<'env> {
7171

7272
hir::Function {
7373
name,
74-
statement: hir!((SEQ statements)),
74+
statement: hir::Statement::Sequence(statements),
7575
arguments: function.parameters.len(),
7676
returns: function.returns.len(),
7777
}
@@ -125,7 +125,7 @@ impl<'env> Emitter<'env> {
125125

126126
hir!(
127127
(ESEQ
128-
(SEQ statements)
128+
(hir::Statement::Sequence(statements))
129129
(ADD (TEMP array) (CONST abi::WORD))))
130130
.into()
131131
}
@@ -281,7 +281,7 @@ impl<'env> Emitter<'env> {
281281
//
282282
// The number of returns must match the rest of the function, so return values
283283
// are defined along all paths to the exit.
284-
(RETURN vec![hir!((CONST 0)); self.returns])
284+
(hir::Statement::Return(vec![hir!((CONST 0)); self.returns]))
285285
(LABEL r#in))
286286
(MEM (ADD (TEMP base) (MUL (TEMP index) (CONST abi::WORD)))))
287287
).into()
@@ -329,7 +329,7 @@ impl<'env> Emitter<'env> {
329329
lengths.push(hir!((MOVE (TEMP fresh) (declaration))));
330330

331331
hir::Expression::Sequence(
332-
Box::new(hir!((SEQ lengths))),
332+
Box::new(hir::Statement::Sequence(lengths)),
333333
Box::new(hir!((TEMP fresh))),
334334
)
335335
}
@@ -382,7 +382,11 @@ impl<'env> Emitter<'env> {
382382
}
383383
}
384384

385-
hir!((ESEQ (SEQ statements) (ADD (TEMP array) (CONST abi::WORD))))
385+
hir!(
386+
(ESEQ
387+
(hir::Statement::Sequence(statements))
388+
(ADD (TEMP array) (CONST abi::WORD)))
389+
)
386390
}
387391

388392
fn emit_statement(

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#[macro_use]
21
pub mod data;
32

43
mod abi;

0 commit comments

Comments
 (0)