Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions c2rust-transpile/src/translator/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,16 +452,16 @@ impl<'c> Translation<'c> {

let args = self.convert_call_args(ctx.used(), args, arg_tys.as_deref(), is_variadic)?;

let mut call_expr = args.map(|args| mk().call_expr(func, args));
if let Some(expected_ty) = override_ty {
if call_expr_ty != expected_ty {
let ret_ty = self.convert_type(expected_ty.ctype)?;
call_expr = call_expr.map(|call| mk().cast_expr(call, ret_ty));
}
}

let res: TranslationResult<_> = Ok(call_expr);
res
let call_expr = args.map(|args| mk().call_expr(func, args));
self.convert_cast(
ctx,
call_expr_ty,
override_ty.unwrap_or(call_expr_ty),
call_expr,
None,
None,
None,
)
})?;

self.convert_side_effects_expr(
Expand Down
101 changes: 69 additions & 32 deletions c2rust-transpile/src/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2872,33 +2872,34 @@ impl<'c> Translation<'c> {
pub fn compute_size_of_type(
&self,
ctx: ExprContext,
expected_type_id: Option<CQualTypeId>,
result_type_id: CQualTypeId,
type_id: CTypeId,
override_ty: Option<CQualTypeId>,
) -> TranslationResult<WithStmts<Box<Expr>>> {
if let CTypeKind::VariableArray(elts, len) = self.ast_context.resolve_type(type_id).kind {
let len = len.expect("Sizeof a VLA type with count expression omitted");

let elts = self.compute_size_of_type(ctx, elts, override_ty)?;
let elts = self.compute_size_of_type(ctx, expected_type_id, result_type_id, elts)?;
return elts.and_then_try(|lhs| {
let len = self.convert_expr(ctx.used().not_static(), len, override_ty)?;
let len = self.convert_expr(ctx.used().not_static(), len, expected_type_id)?;
Ok(len.map(|len| {
let rhs = cast_int(len, "usize", true);
mk().binary_expr(BinOp::Mul(Default::default()), lhs, rhs)
}))
});
}
let ty = self.convert_type(type_id)?;
let mut result = self.mk_size_of_ty_expr(ty);
// cast to expected ty if one is known
if let Some(expected_ty) = override_ty {
trace!(
"Converting result of sizeof to {:?}",
self.ast_context.resolve_type(expected_ty.ctype)
);
let result_ty = self.convert_type(expected_ty.ctype)?;
result = result.map(|x| x.map(|x| mk().cast_expr(x, result_ty)));
}
result
let result = self.mk_size_of_ty_expr(ty)?;

self.convert_cast(
ctx,
result_type_id,
expected_type_id.unwrap_or(result_type_id),
result,
None,
None,
None,
)
}

fn mk_size_of_ty_expr(&self, ty: Box<Type>) -> TranslationResult<WithStmts<Box<Expr>>> {
Expand All @@ -2916,6 +2917,9 @@ impl<'c> Translation<'c> {

pub fn compute_align_of_type(
&self,
ctx: ExprContext,
expected_type_id: Option<CQualTypeId>,
result_type_id: CQualTypeId,
mut type_id: CTypeId,
preferred: bool,
src_loc: &Option<SrcSpan>,
Expand Down Expand Up @@ -2952,7 +2956,15 @@ impl<'c> Translation<'c> {
path.push(mk().path_segment_with_args("align_of", mk().angle_bracketed_args(tys)));
}
let call = mk().call_expr(mk().abs_path_expr(path), vec![]);
Ok(WithStmts::new_val(call))
self.convert_cast(
ctx,
result_type_id,
expected_type_id.unwrap_or(result_type_id),
WithStmts::new_val(call),
None,
None,
None,
)
}

/// Convert multiple expressions (while collecting a context of statements) given either all or
Expand Down Expand Up @@ -3033,13 +3045,19 @@ impl<'c> Translation<'c> {
}),
ConvertVector(..) => Err(TranslationError::generic("convert vector not supported")),

UnaryType(_ty, kind, opt_expr, arg_ty) => {
UnaryType(result_type_id, kind, opt_expr, arg_ty) => {
let result = match kind {
CUnTypeOp::SizeOf => match opt_expr {
None => self.compute_size_of_type(ctx, arg_ty.ctype, override_ty)?,
None => self.compute_size_of_type(
ctx,
override_ty,
result_type_id,
arg_ty.ctype,
)?,
Some(_) => {
let inner = self.variable_array_base_type(arg_ty.ctype);
let inner_size = self.compute_size_of_type(ctx, inner, override_ty)?;
let inner_size =
self.compute_size_of_type(ctx, override_ty, result_type_id, inner)?;

if let Some(sz) = self.compute_size_of_expr(arg_ty.ctype) {
inner_size.map(|x| {
Expand All @@ -3051,12 +3069,22 @@ impl<'c> Translation<'c> {
}
}
},
CUnTypeOp::AlignOf => {
self.compute_align_of_type(arg_ty.ctype, false, src_loc)?
}
CUnTypeOp::PreferredAlignOf => {
self.compute_align_of_type(arg_ty.ctype, true, src_loc)?
}
CUnTypeOp::AlignOf => self.compute_align_of_type(
ctx,
override_ty,
result_type_id,
arg_ty.ctype,
false,
src_loc,
)?,
CUnTypeOp::PreferredAlignOf => self.compute_align_of_type(
ctx,
override_ty,
result_type_id,
arg_ty.ctype,
true,
src_loc,
)?,
};

Ok(result)
Expand All @@ -3066,7 +3094,7 @@ impl<'c> Translation<'c> {
if let Some(constant) = value {
self.convert_constant(constant).map(WithStmts::new_val)
} else {
self.convert_expr(ctx, child, Some(ty))
self.convert_expr(ctx, child, Some(override_ty.unwrap_or(ty)))
}
}

Expand Down Expand Up @@ -3190,14 +3218,21 @@ impl<'c> Translation<'c> {
val = mk().method_call_expr(val, "as_mut_ptr", vec![]);
}

// if the context wants a different type, add a cast
if let Some(expected_ty) = override_ty {
if lrvalue.is_rvalue() && expected_ty != qual_ty {
val = mk().cast_expr(val, self.convert_type(expected_ty.ctype)?);
}
let mut val = WithStmts::new_val(val).merge_unsafe(set_unsafe);

if lrvalue.is_rvalue() {
val = self.convert_cast(
ctx,
qual_ty,
override_ty.unwrap_or(qual_ty),
val,
None,
None,
None,
)?;
}

Ok(WithStmts::new_val(val).merge_unsafe(set_unsafe))
Ok(val)
}

OffsetOf(ty, ref kind) => match kind {
Expand Down Expand Up @@ -3480,7 +3515,9 @@ impl<'c> Translation<'c> {
self.convert_init_list(ctx, ty, ids, opt_union_field_id)
}

ImplicitValueInit(ty) => self.implicit_default_expr(ctx, ty.ctype),
ImplicitValueInit(ty) => {
self.implicit_default_expr(ctx, override_ty.unwrap_or(ty).ctype)
}

Predefined(_, val_id) => self.convert_expr(ctx, val_id, override_ty),

Expand Down
18 changes: 12 additions & 6 deletions c2rust-transpile/src/translator/pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,19 @@ impl<'c> Translation<'c> {

let mut val =
self.convert_pointer_offset(lhs, rhs, pointee_type_id.ctype, false, deref);
// if the context wants a different type, add a cast
if let Some(expected_ty) = override_ty {
if lrvalue.is_rvalue() && expected_ty != pointee_type_id {
let ty = self.convert_type(expected_ty.ctype)?;
val = val.map(|val| mk().cast_expr(val, ty));
}

if lrvalue.is_rvalue() {
val = self.convert_cast(
ctx,
pointee_type_id,
override_ty.unwrap_or(pointee_type_id),
val,
None,
None,
None,
)?;
}

Ok(val)
})
}
Expand Down
16 changes: 10 additions & 6 deletions c2rust-transpile/src/translator/structs_unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,12 +1043,16 @@ impl<'a> Translation<'a> {
val = val.map(|v| mk().field_expr(v, field_name));
};

// if the context wants a different type, add a cast
if let Some(expected_ty) = override_ty {
if lrvalue.is_rvalue() && expected_ty != qual_ty {
let ty = self.convert_type(expected_ty.ctype)?;
val = val.map(|v| mk().cast_expr(v, ty));
}
if lrvalue.is_rvalue() {
val = self.convert_cast(
ctx,
qual_ty,
override_ty.unwrap_or(qual_ty),
val,
None,
None,
None,
)?;
}

Ok(val)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct bar {
pub unsafe extern "C" fn foo() -> ::core::ffi::c_int {
let mut x: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
let mut px: *mut ::core::ffi::c_int = &raw mut x;
let mut sx: usize = ::core::mem::size_of::<::core::ffi::c_int>() as usize;
let mut sx: usize = ::core::mem::size_of::<::core::ffi::c_int>();
let mut y: bar = bar {
x: x as ::core::ffi::c_int,
} as bar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct bar {
pub unsafe extern "C" fn foo() -> ::core::ffi::c_int {
let mut x: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
let mut px: *mut ::core::ffi::c_int = &raw mut x;
let mut sx: usize = ::core::mem::size_of::<::core::ffi::c_int>() as usize;
let mut sx: usize = ::core::mem::size_of::<::core::ffi::c_int>();
let mut y: bar = bar {
x: x as ::core::ffi::c_int,
} as bar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ pub unsafe extern "C" fn long_double_test() {
let mut f: ::core::ffi::c_float = one.to_f32().unwrap();
let mut cast_from_int: ::f128::f128 = ::f128::f128::new(i);
let mut cast_from_float: ::f128::f128 = ::f128::f128::new(f);
let mut is_inf: bool = if ::core::mem::size_of::<::f128::f128>() as usize
== ::core::mem::size_of::<::core::ffi::c_float>() as usize
let mut is_inf: bool = if ::core::mem::size_of::<::f128::f128>()
== ::core::mem::size_of::<::core::ffi::c_float>()
{
__inline_isinff(huge.to_f32().unwrap())
} else if ::core::mem::size_of::<::f128::f128>() as usize
== ::core::mem::size_of::<::core::ffi::c_double>() as usize
} else if ::core::mem::size_of::<::f128::f128>()
== ::core::mem::size_of::<::core::ffi::c_double>()
{
__inline_isinfd(huge.to_f64().unwrap())
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ pub unsafe extern "C" fn long_double_test() {
let mut f: ::core::ffi::c_float = one.to_f32().unwrap();
let mut cast_from_int: ::f128::f128 = ::f128::f128::new(i);
let mut cast_from_float: ::f128::f128 = ::f128::f128::new(f);
let mut is_inf: bool = if ::core::mem::size_of::<::f128::f128>() as usize
== ::core::mem::size_of::<::core::ffi::c_float>() as usize
let mut is_inf: bool = if ::core::mem::size_of::<::f128::f128>()
== ::core::mem::size_of::<::core::ffi::c_float>()
{
__inline_isinff(huge.to_f32().unwrap())
} else if ::core::mem::size_of::<::f128::f128>() as usize
== ::core::mem::size_of::<::core::ffi::c_double>() as usize
} else if ::core::mem::size_of::<::f128::f128>()
== ::core::mem::size_of::<::core::ffi::c_double>()
{
__inline_isinfd(huge.to_f64().unwrap())
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ pub static mut p: *const fn_ptrs = unsafe { &raw const fns };
pub const ZSTD_WINDOWLOG_MAX_32: ::core::ffi::c_int = 30 as ::core::ffi::c_int;
pub const ZSTD_WINDOWLOG_MAX_64: ::core::ffi::c_int = 31 as ::core::ffi::c_int;
pub const ZSTD_WINDOWLOG_MAX: ::core::ffi::c_int =
if ::core::mem::size_of::<zstd_platform_dependent_type>() as usize == 4 as usize {
if ::core::mem::size_of::<zstd_platform_dependent_type>() == 4 as usize {
ZSTD_WINDOWLOG_MAX_32
} else {
ZSTD_WINDOWLOG_MAX_64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut ::core::ffi::c_char) {
memcpy(
out as *mut ::core::ffi::c_void,
POS.as_ptr() as *const ::core::ffi::c_void,
(::core::mem::size_of::<[::core::ffi::c_char; 3]>() as size_t)
.wrapping_div(::core::mem::size_of::<::core::ffi::c_char>() as size_t)
::core::mem::size_of::<[::core::ffi::c_char; 3]>()
.wrapping_div(::core::mem::size_of::<::core::ffi::c_char>())
.wrapping_sub(1 as size_t)
.wrapping_add(1 as size_t)
.wrapping_mul(::core::mem::size_of::<::core::ffi::c_char>() as size_t),
.wrapping_mul(::core::mem::size_of::<::core::ffi::c_char>()),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut ::core::ffi::c_char) {
memcpy(
out as *mut ::core::ffi::c_void,
POS.as_ptr() as *const ::core::ffi::c_void,
(::core::mem::size_of::<[::core::ffi::c_char; 3]>() as size_t)
.wrapping_div(::core::mem::size_of::<::core::ffi::c_char>() as size_t)
::core::mem::size_of::<[::core::ffi::c_char; 3]>()
.wrapping_div(::core::mem::size_of::<::core::ffi::c_char>())
.wrapping_sub(1 as size_t)
.wrapping_add(1 as size_t)
.wrapping_mul(::core::mem::size_of::<::core::ffi::c_char>() as size_t),
.wrapping_mul(::core::mem::size_of::<::core::ffi::c_char>()),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ pub static mut p: *const fn_ptrs = &raw const fns;
pub const ZSTD_WINDOWLOG_MAX_32: ::core::ffi::c_int = 30 as ::core::ffi::c_int;
pub const ZSTD_WINDOWLOG_MAX_64: ::core::ffi::c_int = 31 as ::core::ffi::c_int;
pub const ZSTD_WINDOWLOG_MAX: ::core::ffi::c_int =
if ::core::mem::size_of::<zstd_platform_dependent_type>() as usize == 4 as usize {
if ::core::mem::size_of::<zstd_platform_dependent_type>() == 4 as usize {
ZSTD_WINDOWLOG_MAX_32
} else {
ZSTD_WINDOWLOG_MAX_64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut ::core::ffi::c_char) {
memcpy(
out as *mut ::core::ffi::c_void,
POS.as_ptr() as *const ::core::ffi::c_void,
(::core::mem::size_of::<[::core::ffi::c_char; 3]>() as size_t)
.wrapping_div(::core::mem::size_of::<::core::ffi::c_char>() as size_t)
::core::mem::size_of::<[::core::ffi::c_char; 3]>()
.wrapping_div(::core::mem::size_of::<::core::ffi::c_char>())
.wrapping_sub(1 as size_t)
.wrapping_add(1 as size_t)
.wrapping_mul(::core::mem::size_of::<::core::ffi::c_char>() as size_t),
.wrapping_mul(::core::mem::size_of::<::core::ffi::c_char>()),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut ::core::ffi::c_char) {
memcpy(
out as *mut ::core::ffi::c_void,
POS.as_ptr() as *const ::core::ffi::c_void,
(::core::mem::size_of::<[::core::ffi::c_char; 3]>() as size_t)
.wrapping_div(::core::mem::size_of::<::core::ffi::c_char>() as size_t)
::core::mem::size_of::<[::core::ffi::c_char; 3]>()
.wrapping_div(::core::mem::size_of::<::core::ffi::c_char>())
.wrapping_sub(1 as size_t)
.wrapping_add(1 as size_t)
.wrapping_mul(::core::mem::size_of::<::core::ffi::c_char>() as size_t),
.wrapping_mul(::core::mem::size_of::<::core::ffi::c_char>()),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ pub unsafe extern "C" fn func() {
let mut ptr: *const wchar_t =
const { ::core::mem::transmute::<[u8; 8], [::core::ffi::c_int; 2]>(*b"x\0\0\0\0\0\0\0") }
.as_ptr() as *const wchar_t;
let mut len: size_t = wcslen(&raw mut array as *mut wchar_t) as size_t;
let mut len: size_t = wcslen(&raw mut array as *mut wchar_t);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ pub unsafe extern "C" fn func() {
let mut ptr: *const wchar_t =
const { ::core::mem::transmute::<[u8; 8], [::core::ffi::c_int; 2]>(*b"x\0\0\0\0\0\0\0") }
.as_ptr() as *const wchar_t;
let mut len: size_t = wcslen(&raw mut array as *mut wchar_t) as size_t;
let mut len: size_t = wcslen(&raw mut array as *mut wchar_t);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ pub unsafe extern "C" fn func() {
let mut ptr: *const wchar_t =
const { ::core::mem::transmute::<[u8; 8], [::core::ffi::c_int; 2]>(*b"x\0\0\0\0\0\0\0") }
.as_ptr() as *const wchar_t;
let mut len: size_t = wcslen(&raw mut array as *mut wchar_t) as size_t;
let mut len: size_t = wcslen(&raw mut array as *mut wchar_t);
}
Loading
Loading