Skip to content

Commit dba9eae

Browse files
authored
transpile: use core::ffi::c_* instead of std::ffi::c_* (#1381)
* Fixes #1356. This leaves alone `std` items that could be `alloc`, like `std::vec::from_elem` or `std::ffi::CString::new` (needs a newer `rustc` 1.63), but it should address most cases.
2 parents 88a4af2 + 76f39c1 commit dba9eae

30 files changed

+541
-536
lines changed

c2rust-transpile/src/convert_type.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl TypeConverter {
284284
// in the case of pointers.
285285
CTypeKind::Void => Ok(mk()
286286
.set_mutbl(mutbl)
287-
.ptr_ty(mk().path_ty(vec!["std", "ffi", "c_void"]))),
287+
.ptr_ty(mk().path_ty(vec!["core", "ffi", "c_void"]))),
288288

289289
CTypeKind::VariableArray(mut elt, _len) => {
290290
while let CTypeKind::VariableArray(elt_, _) = ctxt.resolve_type(elt).kind {
@@ -325,23 +325,23 @@ impl TypeConverter {
325325
match ctxt.index(ctype).kind {
326326
CTypeKind::Void => Ok(mk().tuple_ty(vec![])),
327327
CTypeKind::Bool => Ok(mk().path_ty(mk().path(vec!["bool"]))),
328-
CTypeKind::Short => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_short"]))),
329-
CTypeKind::Int => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_int"]))),
330-
CTypeKind::Long => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_long"]))),
331-
CTypeKind::LongLong => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_longlong"]))),
332-
CTypeKind::UShort => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_ushort"]))),
333-
CTypeKind::UInt => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_uint"]))),
334-
CTypeKind::ULong => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_ulong"]))),
335-
CTypeKind::ULongLong => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_ulonglong"]))),
336-
CTypeKind::SChar => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_schar"]))),
337-
CTypeKind::UChar => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_uchar"]))),
338-
CTypeKind::Char => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_char"]))),
339-
CTypeKind::Double => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_double"]))),
328+
CTypeKind::Short => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_short"]))),
329+
CTypeKind::Int => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_int"]))),
330+
CTypeKind::Long => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_long"]))),
331+
CTypeKind::LongLong => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_longlong"]))),
332+
CTypeKind::UShort => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_ushort"]))),
333+
CTypeKind::UInt => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_uint"]))),
334+
CTypeKind::ULong => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_ulong"]))),
335+
CTypeKind::ULongLong => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_ulonglong"]))),
336+
CTypeKind::SChar => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_schar"]))),
337+
CTypeKind::UChar => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_uchar"]))),
338+
CTypeKind::Char => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_char"]))),
339+
CTypeKind::Double => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_double"]))),
340340
CTypeKind::LongDouble | CTypeKind::Float128 => {
341341
self.use_crate(ExternCrate::F128);
342342
Ok(mk().path_ty(mk().path(vec!["f128", "f128"])))
343343
}
344-
CTypeKind::Float => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_float"]))),
344+
CTypeKind::Float => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_float"]))),
345345
CTypeKind::Int128 => Ok(mk().path_ty(mk().path(vec!["i128"]))),
346346
CTypeKind::UInt128 => Ok(mk().path_ty(mk().path(vec!["u128"]))),
347347
CTypeKind::BFloat16 => Ok(mk().path_ty(mk().path(vec!["bf16"]))),

c2rust-transpile/src/translator/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'c> Translation<'c> {
106106
Ok(val.map(|v| {
107107
let val = mk().method_call_expr(v, "is_sign_negative", vec![]);
108108

109-
mk().cast_expr(val, mk().path_ty(vec!["std", "ffi", "c_int"]))
109+
mk().cast_expr(val, mk().path_ty(vec!["core", "ffi", "c_int"]))
110110
}))
111111
}
112112
"__builtin_ffs" | "__builtin_ffsl" | "__builtin_ffsll" => {

c2rust-transpile/src/translator/main_function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'c> Translation<'c> {
5858
Some(mk().path_ty(vec![mk().path_segment_with_args(
5959
"Vec",
6060
mk().angle_bracketed_args(vec![
61-
mk().mutbl().ptr_ty(mk().path_ty(vec!["std", "ffi", "c_char"])),
61+
mk().mutbl().ptr_ty(mk().path_ty(vec!["core", "ffi", "c_char"])),
6262
]),
6363
)])),
6464
Some(mk().call_expr(mk().path_expr(vec!["Vec", "new"]), vec![])),
@@ -126,7 +126,7 @@ impl<'c> Translation<'c> {
126126
Some(mk().path_ty(vec![mk().path_segment_with_args(
127127
"Vec",
128128
mk().angle_bracketed_args(vec![
129-
mk().mutbl().ptr_ty(mk().path_ty(vec!["std", "ffi", "c_char"])),
129+
mk().mutbl().ptr_ty(mk().path_ty(vec!["core", "ffi", "c_char"])),
130130
]),
131131
)])),
132132
Some(mk().call_expr(mk().path_expr(vec!["Vec", "new"]), vec![])),

c2rust-transpile/src/translator/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ fn arrange_header(t: &Translation, is_binary: bool) -> (Vec<syn::Attribute>, Vec
10741074

10751075
/// Convert a boolean expression to a c_int
10761076
fn bool_to_int(val: Box<Expr>) -> Box<Expr> {
1077-
mk().cast_expr(val, mk().path_ty(vec!["std", "ffi", "c_int"]))
1077+
mk().cast_expr(val, mk().path_ty(vec!["core", "ffi", "c_int"]))
10781078
}
10791079

10801080
/// Add a src_loc = "line:col" attribute to an item/foreign_item

c2rust-transpile/src/translator/operators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ impl<'c> Translation<'c> {
10361036

10371037
c_ast::UnOp::Not => {
10381038
let val = self.convert_condition(ctx, false, arg)?;
1039-
Ok(val.map(|x| mk().cast_expr(x, mk().path_ty(vec!["std", "ffi", "c_int"]))))
1039+
Ok(val.map(|x| mk().cast_expr(x, mk().path_ty(vec!["core", "ffi", "c_int"]))))
10401040
}
10411041
c_ast::UnOp::Extension => {
10421042
let arg = self.convert_expr(ctx, arg, Some(cqual_type))?;

c2rust-transpile/src/translator/variadic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'c> Translation<'c> {
195195
real_arg_ty = Some(arg_ty.clone());
196196
arg_ty = mk()
197197
.mutbl()
198-
.ptr_ty(mk().path_ty(vec!["std", "ffi", "c_void"]));
198+
.ptr_ty(mk().path_ty(vec!["core", "ffi", "c_void"]));
199199
}
200200

201201
val.and_then(|val| {

c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,56 @@ input_file: c2rust-transpile/tests/snapshots/arch-specific/vm_x86.c
1414
#[derive(Copy, Clone)]
1515
#[repr(C)]
1616
pub struct vm_t {
17-
pub programStack: std::ffi::c_int,
18-
pub entryOfs: std::ffi::c_int,
19-
pub dataBase: *mut std::ffi::c_void,
20-
pub codeBase: *mut std::ffi::c_void,
21-
pub instructionPointers: *mut std::ffi::c_ulong,
17+
pub programStack: core::ffi::c_int,
18+
pub entryOfs: core::ffi::c_int,
19+
pub dataBase: *mut core::ffi::c_void,
20+
pub codeBase: *mut core::ffi::c_void,
21+
pub instructionPointers: *mut core::ffi::c_ulong,
2222
}
23-
pub type byte = std::ffi::c_uchar;
24-
pub const MAX_VMMAIN_ARGS: std::ffi::c_int = 50 as std::ffi::c_int;
23+
pub type byte = core::ffi::c_uchar;
24+
pub const MAX_VMMAIN_ARGS: core::ffi::c_int = 50 as core::ffi::c_int;
2525
#[no_mangle]
2626
pub unsafe extern "C" fn VM_CallCompiled(
2727
mut vm: *mut vm_t,
28-
mut args: *mut std::ffi::c_int,
29-
) -> std::ffi::c_int {
28+
mut args: *mut core::ffi::c_int,
29+
) -> core::ffi::c_int {
3030
let mut stack: [byte; 271] = [0; 271];
31-
let mut entryPoint: *mut std::ffi::c_void = 0 as *mut std::ffi::c_void;
32-
let mut programStack: std::ffi::c_int = 0;
33-
let mut stackOnEntry: std::ffi::c_int = 0;
31+
let mut entryPoint: *mut core::ffi::c_void = 0 as *mut core::ffi::c_void;
32+
let mut programStack: core::ffi::c_int = 0;
33+
let mut stackOnEntry: core::ffi::c_int = 0;
3434
let mut image: *mut byte = 0 as *mut byte;
35-
let mut opStack: *mut std::ffi::c_int = 0 as *mut std::ffi::c_int;
36-
let mut opStackOfs: std::ffi::c_int = 0;
37-
let mut arg: std::ffi::c_int = 0;
35+
let mut opStack: *mut core::ffi::c_int = 0 as *mut core::ffi::c_int;
36+
let mut opStackOfs: core::ffi::c_int = 0;
37+
let mut arg: core::ffi::c_int = 0;
3838
let mut currentVM: *mut vm_t = vm;
3939
stackOnEntry = (*vm).programStack;
4040
programStack = stackOnEntry;
4141
image = (*vm).dataBase as *mut byte;
42-
programStack -= 8 as std::ffi::c_int + 4 as std::ffi::c_int * MAX_VMMAIN_ARGS;
43-
arg = 0 as std::ffi::c_int;
42+
programStack -= 8 as core::ffi::c_int + 4 as core::ffi::c_int * MAX_VMMAIN_ARGS;
43+
arg = 0 as core::ffi::c_int;
4444
while arg < MAX_VMMAIN_ARGS {
4545
*(&mut *image
46-
.offset((programStack + 8 as std::ffi::c_int + arg * 4 as std::ffi::c_int) as isize)
47-
as *mut byte as *mut std::ffi::c_int) = *args.offset(arg as isize);
46+
.offset((programStack + 8 as core::ffi::c_int + arg * 4 as core::ffi::c_int) as isize)
47+
as *mut byte as *mut core::ffi::c_int) = *args.offset(arg as isize);
4848
arg += 1;
4949
}
50-
*(&mut *image.offset((programStack + 4 as std::ffi::c_int) as isize) as *mut byte
51-
as *mut std::ffi::c_int) = 0 as std::ffi::c_int;
52-
*(&mut *image.offset(programStack as isize) as *mut byte as *mut std::ffi::c_int) =
53-
-(1 as std::ffi::c_int);
50+
*(&mut *image.offset((programStack + 4 as core::ffi::c_int) as isize) as *mut byte
51+
as *mut core::ffi::c_int) = 0 as core::ffi::c_int;
52+
*(&mut *image.offset(programStack as isize) as *mut byte as *mut core::ffi::c_int) =
53+
-(1 as core::ffi::c_int);
5454
entryPoint = ((*vm).codeBase).offset((*vm).entryOfs as isize);
55-
opStack = (stack.as_mut_ptr() as *mut std::ffi::c_int).offset(16 as std::ffi::c_int as isize);
56-
*opStack = 0 as std::ffi::c_int;
57-
opStackOfs = 0 as std::ffi::c_int;
58-
if opStackOfs != 1 as std::ffi::c_int
59-
|| *opStack as std::ffi::c_uint != 0xdeadbeef as std::ffi::c_uint
55+
opStack = (stack.as_mut_ptr() as *mut core::ffi::c_int).offset(16 as core::ffi::c_int as isize);
56+
*opStack = 0 as core::ffi::c_int;
57+
opStackOfs = 0 as core::ffi::c_int;
58+
if opStackOfs != 1 as core::ffi::c_int
59+
|| *opStack as core::ffi::c_uint != 0xdeadbeef as core::ffi::c_uint
6060
{
61-
return 0 as std::ffi::c_int;
61+
return 0 as core::ffi::c_int;
6262
}
6363
if programStack
64-
!= stackOnEntry - (8 as std::ffi::c_int + 4 as std::ffi::c_int * MAX_VMMAIN_ARGS)
64+
!= stackOnEntry - (8 as core::ffi::c_int + 4 as core::ffi::c_int * MAX_VMMAIN_ARGS)
6565
{
66-
return 0 as std::ffi::c_int;
66+
return 0 as core::ffi::c_int;
6767
}
6868
(*vm).programStack = stackOnEntry;
6969
return *opStack.offset(opStackOfs as isize);

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@ input_file: c2rust-transpile/tests/snapshots/os-specific/macros.c
1212
unused_mut
1313
)]
1414
extern "C" {
15-
fn __errno_location() -> *mut std::ffi::c_int;
15+
fn __errno_location() -> *mut core::ffi::c_int;
1616
fn memcpy(
17-
__dest: *mut std::ffi::c_void,
18-
__src: *const std::ffi::c_void,
17+
__dest: *mut core::ffi::c_void,
18+
__src: *const core::ffi::c_void,
1919
__n: size_t,
20-
) -> *mut std::ffi::c_void;
20+
) -> *mut core::ffi::c_void;
2121
}
2222
pub type size_t = usize;
2323
#[no_mangle]
2424
pub unsafe extern "C" fn errno_is_error() -> bool {
25-
return *__errno_location() != 0 as std::ffi::c_int;
25+
return *__errno_location() != 0 as core::ffi::c_int;
2626
}
2727
#[no_mangle]
28-
pub unsafe extern "C" fn size_of_const() -> std::ffi::c_int {
29-
let mut a: [std::ffi::c_int; 10] = [0; 10];
30-
return SIZE as std::ffi::c_int;
28+
pub unsafe extern "C" fn size_of_const() -> core::ffi::c_int {
29+
let mut a: [core::ffi::c_int; 10] = [0; 10];
30+
return SIZE as core::ffi::c_int;
3131
}
32-
pub const SIZE: usize = ::core::mem::size_of::<[std::ffi::c_int; 10]>();
33-
pub const POS: [std::ffi::c_char; 3] =
34-
unsafe { ::core::mem::transmute::<[u8; 3], [std::ffi::c_char; 3]>(*b"\"]\0") };
32+
pub const SIZE: usize = ::core::mem::size_of::<[core::ffi::c_int; 10]>();
33+
pub const POS: [core::ffi::c_char; 3] =
34+
unsafe { ::core::mem::transmute::<[u8; 3], [core::ffi::c_char; 3]>(*b"\"]\0") };
3535
#[no_mangle]
36-
pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut std::ffi::c_char) {
36+
pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut core::ffi::c_char) {
3737
memcpy(
38-
out as *mut std::ffi::c_void,
39-
POS.as_ptr() as *const std::ffi::c_void,
40-
(::core::mem::size_of::<[std::ffi::c_char; 3]>() as size_t)
41-
.wrapping_div(::core::mem::size_of::<std::ffi::c_char>() as size_t)
38+
out as *mut core::ffi::c_void,
39+
POS.as_ptr() as *const core::ffi::c_void,
40+
(::core::mem::size_of::<[core::ffi::c_char; 3]>() as size_t)
41+
.wrapping_div(::core::mem::size_of::<core::ffi::c_char>() as size_t)
4242
.wrapping_sub(1 as size_t)
4343
.wrapping_add(1 as size_t)
44-
.wrapping_mul(::core::mem::size_of::<std::ffi::c_char>() as size_t),
44+
.wrapping_mul(::core::mem::size_of::<core::ffi::c_char>() as size_t),
4545
);
4646
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ input_file: c2rust-transpile/tests/snapshots/os-specific/rnd.c
1212
unused_mut
1313
)]
1414
extern "C" {
15-
fn abs(__x: std::ffi::c_int) -> std::ffi::c_int;
15+
fn abs(__x: core::ffi::c_int) -> core::ffi::c_int;
1616
}
1717
pub type __int32_t = i32;
1818
pub type __uint32_t = u32;
@@ -31,6 +31,6 @@ pub unsafe extern "C" fn get_rand_seed() -> uint32_t {
3131
cur_rand_seed = MULTIPLIER
3232
.wrapping_mul(cur_rand_seed)
3333
.wrapping_add(INCREMENT);
34-
let mut ret: uint32_t = abs(cur_rand_seed as std::ffi::c_int) as uint32_t;
34+
let mut ret: uint32_t = abs(cur_rand_seed as core::ffi::c_int) as uint32_t;
3535
return ret;
3636
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ input_file: c2rust-transpile/tests/snapshots/os-specific/rotate.c
1212
unused_mut
1313
)]
1414
#[no_mangle]
15-
pub unsafe extern "C" fn rotate_left_64(mut x: std::ffi::c_ulonglong) -> std::ffi::c_ulonglong {
16-
return (x as std::ffi::c_ulong).rotate_left(4 as std::ffi::c_int as std::ffi::c_ulong as u32)
17-
as std::ffi::c_ulonglong;
15+
pub unsafe extern "C" fn rotate_left_64(mut x: core::ffi::c_ulonglong) -> core::ffi::c_ulonglong {
16+
return (x as core::ffi::c_ulong).rotate_left(4 as core::ffi::c_int as core::ffi::c_ulong as u32)
17+
as core::ffi::c_ulonglong;
1818
}
1919
#[no_mangle]
20-
pub unsafe extern "C" fn rotate_right_64(mut x: std::ffi::c_ulonglong) -> std::ffi::c_ulonglong {
21-
return (x as std::ffi::c_ulong).rotate_right(4 as std::ffi::c_int as std::ffi::c_ulong as u32)
22-
as std::ffi::c_ulonglong;
20+
pub unsafe extern "C" fn rotate_right_64(mut x: core::ffi::c_ulonglong) -> core::ffi::c_ulonglong {
21+
return (x as core::ffi::c_ulong)
22+
.rotate_right(4 as core::ffi::c_int as core::ffi::c_ulong as u32)
23+
as core::ffi::c_ulonglong;
2324
}

0 commit comments

Comments
 (0)