Skip to content

Commit b13d905

Browse files
committed
transpile: Add MacroInvocationInfo
1 parent b31a214 commit b13d905

3 files changed

Lines changed: 29 additions & 16 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: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,22 @@ 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| {
161+
info.parameter.is_none() && ctx.expanding_macro(&info.macro_id)
162+
})
161163
.last()
162164
.unwrap()
163-
.first();
165+
.iter()
166+
.filter(|info| info.parameter.is_none())
167+
.next();
164168
let macro_id = match first_macro {
165-
Some(macro_id) => macro_id,
169+
Some(info) => info.macro_id,
166170
None => return Ok(None),
167171
};
168172

169173
trace!(" found macro expansion: {macro_id:?}");
170174
// 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();
175+
let expansion = self.macro_expansions.borrow().get(&macro_id).cloned();
172176
let macro_ty = match expansion {
173177
// Expansion exists.
174178
Some(Some(expansion)) => expansion.ty,
@@ -178,8 +182,8 @@ impl<'c> Translation<'c> {
178182

179183
// We haven't tried to expand it yet.
180184
None => {
181-
self.convert_decl(ctx, *macro_id)?;
182-
if let Some(Some(expansion)) = self.macro_expansions.borrow().get(macro_id) {
185+
self.convert_decl(ctx, macro_id)?;
186+
if let Some(Some(expansion)) = self.macro_expansions.borrow().get(&macro_id) {
183187
expansion.ty
184188
} else {
185189
return Ok(None);
@@ -189,10 +193,10 @@ impl<'c> Translation<'c> {
189193
let rust_name = self
190194
.renamer
191195
.borrow_mut()
192-
.get(macro_id)
196+
.get(&macro_id)
193197
.ok_or_else(|| format_err!("Macro name not declared"))?;
194198

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

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

0 commit comments

Comments
 (0)