Skip to content

Commit 795f70f

Browse files
committed
transpile: Add MacroInvocationInfo
1 parent 9b9a115 commit 795f70f

3 files changed

Lines changed: 35 additions & 20 deletions

File tree

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,12 +543,14 @@ 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 {
547-
self.typed_context
548-
.macro_expansions
549-
.entry(*mac_id)
550-
.or_default()
551-
.push(*expr_id);
546+
for info in macro_ids {
547+
if info.parameter.is_none() {
548+
self.typed_context
549+
.macro_expansions
550+
.entry(info.macro_id)
551+
.or_default()
552+
.push(*expr_id);
553+
}
552554
}
553555
}
554556

@@ -1025,12 +1027,15 @@ impl ConversionContext {
10251027

10261028
if expected_ty & EXPR != 0 {
10271029
for info in &node.macro_invocations {
1028-
let mac = CDeclId(self.visit_node_type(info.macro_id, MACRO_DECL));
1030+
let info = MacroInvocationInfo {
1031+
macro_id: CDeclId(self.visit_node_type(info.macro_id, MACRO_DECL)),
1032+
parameter: info.parameter.clone(),
1033+
};
10291034
self.typed_context
10301035
.macro_invocations
10311036
.entry(CExprId(new_id))
10321037
.or_default()
1033-
.push(mac);
1038+
.push(info);
10341039
}
10351040
}
10361041

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 {
@@ -1176,9 +1182,9 @@ impl TypedAstContext {
11761182
Expr(expr_id) => {
11771183
let expr = self.index(expr_id);
11781184
if let Some(macs) = self.macro_invocations.get(&expr_id) {
1179-
for mac_id in macs {
1180-
if wanted.insert(*mac_id) {
1181-
to_walk.push(*mac_id);
1185+
for info in macs {
1186+
if wanted.insert(info.macro_id) {
1187+
to_walk.push(info.macro_id);
11821188
}
11831189
}
11841190
}

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)