Skip to content

Commit e861f34

Browse files
committed
transpile: Add MacroInvocationInfo
1 parent 9397b5b commit e861f34

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,10 @@ impl ConversionContext {
543543

544544
// Invert the macro invocations to get a list of macro expansion expressions
545545
for (expr_id, macro_ids) in &self.typed_context.macro_invocations {
546-
for mac_id in macro_ids {
546+
for info in macro_ids {
547547
self.typed_context
548548
.macro_expansions
549-
.entry(*mac_id)
549+
.entry(info.macro_id)
550550
.or_default()
551551
.push(*expr_id);
552552
}
@@ -986,12 +986,15 @@ impl ConversionContext {
986986

987987
if expected_ty & EXPR != 0 {
988988
for info in &node.macro_invocations {
989-
let mac = CDeclId(self.visit_node_type(info.macro_id, MACRO_DECL));
989+
let info = MacroInvocationInfo {
990+
macro_id: CDeclId(self.visit_node_type(info.macro_id, MACRO_DECL)),
991+
parameter: info.parameter.clone(),
992+
};
990993
self.typed_context
991994
.macro_invocations
992995
.entry(CExprId(new_id))
993996
.or_default()
994-
.push(mac);
997+
.push(info);
995998
}
996999
}
9971000

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub struct TypedAstContext {
103103
pub label_names: IndexMap<CLabelId, Rc<str>>,
104104

105105
/// map expressions to the stack of macros they were expanded from
106-
pub macro_invocations: IndexMap<CExprId, Vec<CDeclId>>,
106+
pub macro_invocations: IndexMap<CExprId, Vec<MacroInvocationInfo>>,
107107

108108
/// map macro decls to the expressions they expand to
109109
pub macro_expansions: IndexMap<CDeclId, Vec<CExprId>>,
@@ -122,6 +122,12 @@ pub struct TypedAstContext {
122122
pub target: String,
123123
}
124124

125+
#[derive(Debug, Clone)]
126+
pub struct MacroInvocationInfo {
127+
pub macro_id: CDeclId,
128+
pub parameter: Option<String>,
129+
}
130+
125131
/// Comments associated with a typed AST context
126132
#[derive(Debug, Clone)]
127133
pub struct CommentContext {
@@ -1165,9 +1171,9 @@ impl TypedAstContext {
11651171
Expr(expr_id) => {
11661172
let expr = self.index(expr_id);
11671173
if let Some(macs) = self.macro_invocations.get(&expr_id) {
1168-
for mac_id in macs {
1169-
if wanted.insert(*mac_id) {
1170-
to_walk.push(*mac_id);
1174+
for info in macs {
1175+
if wanted.insert(info.macro_id) {
1176+
to_walk.push(info.macro_id);
11711177
}
11721178
}
11731179
}

c2rust-transpile/src/translator/macros.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,18 @@ impl<'c> Translation<'c> {
157157

158158
// Find the first macro after the macro we're currently expanding, if any.
159159
let first_macro = macros
160-
.splitn(2, |macro_id| ctx.expanding_macro(macro_id))
160+
.splitn(2, |info| ctx.expanding_macro(&info.macro_id))
161161
.last()
162162
.unwrap()
163163
.first();
164164
let macro_id = match first_macro {
165-
Some(macro_id) => macro_id,
165+
Some(info) => info.macro_id,
166166
None => return Ok(None),
167167
};
168168

169169
trace!(" found macro expansion: {macro_id:?}");
170170
// Ensure that we've converted this macro and that it has a valid definition.
171-
let expansion = self.macro_expansions.borrow().get(macro_id).cloned();
171+
let expansion = self.macro_expansions.borrow().get(&macro_id).cloned();
172172
let macro_ty = match expansion {
173173
// Expansion exists.
174174
Some(Some(expansion)) => expansion.ty,
@@ -178,8 +178,8 @@ impl<'c> Translation<'c> {
178178

179179
// We haven't tried to expand it yet.
180180
None => {
181-
self.convert_decl(ctx, *macro_id)?;
182-
if let Some(Some(expansion)) = self.macro_expansions.borrow().get(macro_id) {
181+
self.convert_decl(ctx, macro_id)?;
182+
if let Some(Some(expansion)) = self.macro_expansions.borrow().get(&macro_id) {
183183
expansion.ty
184184
} else {
185185
return Ok(None);
@@ -189,10 +189,10 @@ impl<'c> Translation<'c> {
189189
let rust_name = self
190190
.renamer
191191
.borrow_mut()
192-
.get(macro_id)
192+
.get(&macro_id)
193193
.ok_or_else(|| format_err!("Macro name not declared"))?;
194194

195-
self.add_import(*macro_id, &rust_name);
195+
self.add_import(macro_id, &rust_name);
196196

197197
let val = WithStmts::new_val(mk().path_expr(vec![rust_name]));
198198

0 commit comments

Comments
 (0)