Skip to content

Commit 4f6cce7

Browse files
committed
transpile: sort top-level decls for converting w/ macros coming last
1 parent 98e803b commit 4f6cce7

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
@@ -973,6 +973,21 @@ impl TypedAstContext {
973973
}
974974
}
975975

976+
/// Sort the top-level declarations with macros coming last.
977+
/// This is needed during conversion so that when recreating macros,
978+
/// we can look up the [`Translation::expr_override_tys`],
979+
/// which are set when the macro expansion expressions are converted.
980+
pub fn sort_top_decls_for_converting(&mut self) {
981+
self.c_decls_top.sort_unstable_by_key(|decl_id| {
982+
let reverse_order = match self.c_decls.get(decl_id).unwrap().kind {
983+
CDeclKind::MacroFunction { .. } => 0,
984+
CDeclKind::MacroObject { .. } => 1,
985+
_ => 2,
986+
};
987+
-reverse_order
988+
});
989+
}
990+
976991
/// Sort the top-level declarations by file and source location
977992
/// so that we preserve the ordering of all declarations in each file.
978993
/// 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
@@ -681,6 +681,8 @@ pub fn translate(
681681
}
682682
}
683683

684+
t.ast_context.sort_top_decls_for_converting();
685+
684686
// Export top-level value declarations.
685687
// We do this in a conversion pass and then an insertion pass
686688
// so that the conversion order can differ from the order they're emitted in.

0 commit comments

Comments
 (0)