Skip to content

Commit 16fa167

Browse files
committed
transpile: save override_tys in fn convert_expr in a map and use them for const macros
1 parent 4f6cce7 commit 16fa167

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

c2rust-transpile/src/translator/mod.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::path::{self, PathBuf};
77
use std::result::Result; // To override syn::Result from glob import
88

99
use dtoa;
10-
1110
use failure::{err_msg, format_err, Fail};
1211
use indexmap::indexmap;
1312
use indexmap::{IndexMap, IndexSet};
@@ -258,6 +257,15 @@ pub struct Translation<'c> {
258257
potential_flexible_array_members: RefCell<IndexSet<CDeclId>>,
259258
macro_expansions: RefCell<IndexMap<CDeclId, Option<MacroExpansion>>>,
260259

260+
/// For every call to [`Self::convert_expr`],
261+
/// we record the `override_ty` that is used for that expression.
262+
///
263+
/// This is needed [`Self::recreate_const_macro_from_expansions`]
264+
/// when we call [`Self::convert_expr`] for each of the macro expansions,
265+
/// as unlike normal [`Self::convert_expr`]s, there is no surrounding context
266+
/// higher in the call stack calculating the `override_ty`.
267+
expr_override_tys: RefCell<HashMap<CExprId, CQualTypeId>>,
268+
261269
// Comment support
262270
pub comment_context: CommentContext, // Incoming comments
263271
pub comment_store: RefCell<CommentStore>, // Outgoing comments
@@ -1209,6 +1217,7 @@ impl<'c> Translation<'c> {
12091217
function_context: RefCell::new(FuncContext::new()),
12101218
potential_flexible_array_members: RefCell::new(IndexSet::new()),
12111219
macro_expansions: RefCell::new(IndexMap::new()),
1220+
expr_override_tys: Default::default(),
12121221
comment_context,
12131222
comment_store: RefCell::new(CommentStore::new()),
12141223
spans: HashMap::new(),
@@ -2195,7 +2204,11 @@ impl<'c> Translation<'c> {
21952204
.ast_context
21962205
.resolve_expr_type_id(id)
21972206
.unwrap_or((id, ty));
2198-
let expr = self.convert_expr(ctx, expr_id, None)?;
2207+
2208+
let override_ty = self.expr_override_tys.borrow().get(&expr_id).copied();
2209+
let expr = self.convert_expr(ctx, expr_id, override_ty)?;
2210+
2211+
let ty = override_ty.map(|ty| ty.ctype).unwrap_or(ty);
21992212

22002213
// Join ty and cur_ty to the smaller of the two types. If the
22012214
// types are not cast-compatible, abort the fold.
@@ -3247,6 +3260,12 @@ impl<'c> Translation<'c> {
32473260
expr_id: CExprId,
32483261
override_ty: Option<CQualTypeId>,
32493262
) -> TranslationResult<WithStmts<Box<Expr>>> {
3263+
if let Some(override_ty) = override_ty {
3264+
self.expr_override_tys
3265+
.borrow_mut()
3266+
.insert(expr_id, override_ty);
3267+
}
3268+
32503269
let Located {
32513270
loc: src_loc,
32523271
kind: expr_kind,

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct fn_ptrs {
3030
pub fn2: Option<unsafe extern "C" fn(std::ffi::c_int) -> std::ffi::c_int>,
3131
}
3232
pub type zstd_platform_dependent_type = std::ffi::c_long;
33-
pub const UINTPTR_MAX: std::ffi::c_ulong = unsafe { 18446744073709551615 as std::ffi::c_ulong };
33+
pub const UINTPTR_MAX: uintptr_t = unsafe { 18446744073709551615 as uintptr_t };
3434
pub const true_0: std::ffi::c_int = unsafe { 1 as std::ffi::c_int };
3535
pub const LITERAL_INT: std::ffi::c_int = unsafe { 0xffff as std::ffi::c_int };
3636
pub const LITERAL_BOOL: std::ffi::c_int = unsafe { true_0 };
@@ -416,7 +416,7 @@ pub unsafe extern "C" fn use_local_value() -> std::ffi::c_int {
416416
}
417417
#[no_mangle]
418418
pub unsafe extern "C" fn use_portable_type(mut len: uintptr_t) -> bool {
419-
return len <= (UINTPTR_MAX as uintptr_t).wrapping_div(2 as uintptr_t);
419+
return len <= UINTPTR_MAX.wrapping_div(2 as uintptr_t);
420420
}
421421
unsafe extern "C" fn run_static_initializers() {
422422
global_static_const_ptr_arithmetic = PTR_ARITHMETIC;

0 commit comments

Comments
 (0)