Skip to content

Commit a13c7e1

Browse files
kkysenfw-immunant
authored andcommitted
transpile: sort top-level decls for converting w/ macros coming last
1 parent f046e52 commit a13c7e1

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,21 @@ impl TypedAstContext {
10131013
}
10141014
}
10151015

1016+
/// Sort the top-level declarations with macros coming last.
1017+
/// This is needed during conversion so that when recreating macros,
1018+
/// we can look up the [`Translation::expr_override_tys`],
1019+
/// which are set when the macro expansion expressions are converted.
1020+
pub fn sort_top_decls_for_converting(&mut self) {
1021+
self.c_decls_top.sort_unstable_by_key(|decl_id| {
1022+
let reverse_order = match self.c_decls.get(decl_id).unwrap().kind {
1023+
CDeclKind::MacroFunction { .. } => 0,
1024+
CDeclKind::MacroObject { .. } => 1,
1025+
_ => 2,
1026+
};
1027+
-reverse_order
1028+
});
1029+
}
1030+
10161031
/// Sort the top-level declarations by file and source location
10171032
/// so that we preserve the ordering of all declarations in each file.
10181033
/// This preserves the order when we emit the converted declarations.

c2rust-transpile/src/translator/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,8 @@ pub fn translate(
689689
}
690690
}
691691

692+
t.ast_context.sort_top_decls_for_converting();
693+
692694
// Export top-level value declarations.
693695
// We do this in a conversion pass and then an insertion pass
694696
// so that the conversion order can differ from the order they're emitted in.

0 commit comments

Comments
 (0)