Skip to content
Merged
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
8 changes: 8 additions & 0 deletions c2rust-ast-builder/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,14 @@ impl Builder {
}))
}

pub fn abs_path_ty<Pa>(self, path: Pa) -> Box<Type>
where
Pa: Make<Path>,
{
let path = mk().abs_path(path);
self.path_ty(path)
}

pub fn path_ty<Pa>(self, path: Pa) -> Box<Type>
where
Pa: Make<Path>,
Expand Down
83 changes: 41 additions & 42 deletions c2rust-transpile/src/convert_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl TypeConverter {
// in the case of pointers.
CTypeKind::Void => Ok(mk()
.set_mutbl(mutbl)
.ptr_ty(mk().path_ty(vec!["core", "ffi", "c_void"]))),
.ptr_ty(mk().abs_path_ty(vec!["core", "ffi", "c_void"]))),

CTypeKind::VariableArray(mut elt, _len) => {
while let CTypeKind::VariableArray(elt_, _) = ctxt.resolve_type(elt).kind {
Expand Down Expand Up @@ -317,59 +317,58 @@ impl TypeConverter {
ctype: CTypeId,
) -> TranslationResult<Box<Type>> {
if self.translate_valist && ctxt.is_va_list(ctype) {
let path = vec!["core", "ffi", "VaList"];
let ty = mk().path_ty(mk().abs_path(path));
let ty = mk().abs_path_ty(vec!["core", "ffi", "VaList"]);
return Ok(ty);
}

match ctxt.index(ctype).kind {
CTypeKind::Void => Ok(mk().tuple_ty(vec![])),
CTypeKind::Bool => Ok(mk().path_ty(mk().path(vec!["bool"]))),
CTypeKind::Short => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_short"]))),
CTypeKind::Int => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_int"]))),
CTypeKind::Long => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_long"]))),
CTypeKind::LongLong => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_longlong"]))),
CTypeKind::UShort => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_ushort"]))),
CTypeKind::UInt => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_uint"]))),
CTypeKind::ULong => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_ulong"]))),
CTypeKind::ULongLong => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_ulonglong"]))),
CTypeKind::SChar => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_schar"]))),
CTypeKind::UChar => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_uchar"]))),
CTypeKind::Char => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_char"]))),
CTypeKind::Double => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_double"]))),
CTypeKind::Bool => Ok(mk().path_ty(vec!["bool"])),
CTypeKind::Short => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_short"])),
CTypeKind::Int => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_int"])),
CTypeKind::Long => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_long"])),
CTypeKind::LongLong => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_longlong"])),
CTypeKind::UShort => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_ushort"])),
CTypeKind::UInt => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_uint"])),
CTypeKind::ULong => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_ulong"])),
CTypeKind::ULongLong => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_ulonglong"])),
CTypeKind::SChar => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_schar"])),
CTypeKind::UChar => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_uchar"])),
CTypeKind::Char => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_char"])),
CTypeKind::Double => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_double"])),
CTypeKind::LongDouble | CTypeKind::Float128 => {
self.use_crate(ExternCrate::F128);
Ok(mk().path_ty(mk().path(vec!["f128", "f128"])))
Ok(mk().abs_path_ty(vec!["f128", "f128"]))
}
CTypeKind::Float => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_float"]))),
CTypeKind::Int128 => Ok(mk().path_ty(mk().path(vec!["i128"]))),
CTypeKind::UInt128 => Ok(mk().path_ty(mk().path(vec!["u128"]))),
CTypeKind::BFloat16 => Ok(mk().path_ty(mk().path(vec!["bf16"]))),

CTypeKind::Int8 => Ok(mk().path_ty(mk().path(vec!["i8"]))),
CTypeKind::Int16 => Ok(mk().path_ty(mk().path(vec!["i16"]))),
CTypeKind::Int32 => Ok(mk().path_ty(mk().path(vec!["i32"]))),
CTypeKind::Int64 => Ok(mk().path_ty(mk().path(vec!["i64"]))),
CTypeKind::IntPtr => Ok(mk().path_ty(mk().path(vec!["isize"]))),
CTypeKind::UInt8 => Ok(mk().path_ty(mk().path(vec!["u8"]))),
CTypeKind::UInt16 => Ok(mk().path_ty(mk().path(vec!["u16"]))),
CTypeKind::UInt32 => Ok(mk().path_ty(mk().path(vec!["u32"]))),
CTypeKind::UInt64 => Ok(mk().path_ty(mk().path(vec!["u64"]))),
CTypeKind::UIntPtr => Ok(mk().path_ty(mk().path(vec!["usize"]))),
CTypeKind::Float => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_float"])),
CTypeKind::Int128 => Ok(mk().path_ty(vec!["i128"])),
CTypeKind::UInt128 => Ok(mk().path_ty(vec!["u128"])),
CTypeKind::BFloat16 => Ok(mk().path_ty(vec!["bf16"])),

CTypeKind::Int8 => Ok(mk().path_ty(vec!["i8"])),
CTypeKind::Int16 => Ok(mk().path_ty(vec!["i16"])),
CTypeKind::Int32 => Ok(mk().path_ty(vec!["i32"])),
CTypeKind::Int64 => Ok(mk().path_ty(vec!["i64"])),
CTypeKind::IntPtr => Ok(mk().path_ty(vec!["isize"])),
CTypeKind::UInt8 => Ok(mk().path_ty(vec!["u8"])),
CTypeKind::UInt16 => Ok(mk().path_ty(vec!["u16"])),
CTypeKind::UInt32 => Ok(mk().path_ty(vec!["u32"])),
CTypeKind::UInt64 => Ok(mk().path_ty(vec!["u64"])),
CTypeKind::UIntPtr => Ok(mk().path_ty(vec!["usize"])),
CTypeKind::IntMax => {
self.use_crate(ExternCrate::Libc);
Ok(mk().path_ty(mk().path(vec!["libc", "intmax_t"])))
Ok(mk().abs_path_ty(vec!["libc", "intmax_t"]))
}
CTypeKind::UIntMax => {
self.use_crate(ExternCrate::Libc);
Ok(mk().path_ty(mk().path(vec!["libc", "uintmax_t"])))
Ok(mk().abs_path_ty(vec!["libc", "uintmax_t"]))
}
CTypeKind::Size => Ok(mk().path_ty(mk().path(vec!["usize"]))),
CTypeKind::SSize => Ok(mk().path_ty(mk().path(vec!["isize"]))),
CTypeKind::PtrDiff => Ok(mk().path_ty(mk().path(vec!["isize"]))),
CTypeKind::Size => Ok(mk().path_ty(vec!["usize"])),
CTypeKind::SSize => Ok(mk().path_ty(vec!["isize"])),
CTypeKind::PtrDiff => Ok(mk().path_ty(vec!["isize"])),
CTypeKind::WChar => {
self.use_crate(ExternCrate::Libc);
Ok(mk().path_ty(mk().path(vec!["libc", "wchar_t"])))
Ok(mk().abs_path_ty(vec!["libc", "wchar_t"]))
}

CTypeKind::Pointer(qtype) => self.convert_pointer(ctxt, qtype),
Expand All @@ -382,22 +381,22 @@ impl TypeConverter {
let new_name = self
.resolve_decl_name(decl_id)
.ok_or_else(|| format_err!("Unknown decl id {:?}", decl_id))?;
Ok(mk().path_ty(mk().path(vec![new_name])))
Ok(mk().path_ty(vec![new_name]))
}

CTypeKind::Union(decl_id) => {
let new_name = self.resolve_decl_name(decl_id).unwrap();
Ok(mk().path_ty(mk().path(vec![new_name])))
Ok(mk().path_ty(vec![new_name]))
}

CTypeKind::Enum(decl_id) => {
let new_name = self.resolve_decl_name(decl_id).unwrap();
Ok(mk().path_ty(mk().path(vec![new_name])))
Ok(mk().path_ty(vec![new_name]))
}

CTypeKind::Typedef(decl_id) => {
let new_name = self.resolve_decl_name(decl_id).unwrap();
Ok(mk().path_ty(mk().path(vec![new_name])))
Ok(mk().path_ty(vec![new_name]))
}

CTypeKind::ConstantArray(element, count) => {
Expand Down
65 changes: 44 additions & 21 deletions c2rust-transpile/src/rust_ast/item_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,47 @@ impl MultiImport {
}

#[derive(Debug, Default)]
pub struct PathedMultiImports(IndexMap<Vec<String>, MultiImport>);
pub struct PathedMultiImports(IndexMap<(bool, Vec<String>), MultiImport>);

impl PathedMultiImports {
pub fn new() -> Self {
Self::default()
}

pub fn get_mut(&mut self, path: Vec<String>) -> &mut MultiImport {
self.0.entry(path).or_insert(MultiImport::new())
pub fn get_mut(&mut self, is_absolute: bool, path: Vec<String>) -> &mut MultiImport {
self.0
.entry((is_absolute, path))
.or_insert(MultiImport::new())
}

pub fn into_items(self) -> Vec<Box<Item>> {
fn build_items((mut path, imports): (Vec<String>, MultiImport)) -> Box<Item> {
let mut leaves = imports.leaves;
let attrs = imports.attrs.unwrap_or_else(mk);

if leaves.len() == 1 {
path.push(leaves.pop().unwrap());

attrs.use_simple_item(path, None as Option<Ident>)
} else {
attrs.use_multiple_item(path, leaves.into_iter())
}
}

self.0.into_iter().map(build_items).collect()
self.0
.into_iter()
.map(|((is_absolute, mut path), imports)| {
let mut leaves = imports.leaves;
let attrs = imports.attrs.unwrap_or_else(mk);

if leaves.len() == 1 {
path.push(leaves.pop().unwrap());

let path = if is_absolute {
mk().abs_path(path)
} else {
mk().path(path)
};

attrs.use_simple_item(path, None as Option<Ident>)
} else {
let path = if is_absolute {
mk().abs_path(path)
} else {
mk().path(path)
};

attrs.use_multiple_item(path, leaves.into_iter())
}
})
.collect()
}
}

Expand All @@ -87,12 +102,20 @@ impl ItemStore {
self.foreign_items.push(item);
}

pub fn add_use(&mut self, path: Vec<String>, ident: &str) {
self.uses.get_mut(path).insert(ident)
pub fn add_use(&mut self, is_absolute: bool, path: Vec<String>, ident: &str) {
self.uses.get_mut(is_absolute, path).insert(ident)
}

pub fn add_use_with_attr(&mut self, path: Vec<String>, ident: &str, attrs: Builder) {
self.uses.get_mut(path).insert_with_attr(ident, attrs)
pub fn add_use_with_attr(
&mut self,
is_absolute: bool,
path: Vec<String>,
ident: &str,
attrs: Builder,
) {
self.uses
.get_mut(is_absolute, path)
.insert_with_attr(ident, attrs)
}

pub fn drain(&mut self) -> (Vec<Box<Item>>, Vec<ForeignItem>, PathedMultiImports) {
Expand Down
4 changes: 2 additions & 2 deletions c2rust-transpile/src/translator/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ impl<'c> Translation<'c> {

// Import the trait into scope
self.with_cur_file_item_store(|item_store| {
item_store.add_use(vec!["c2rust_asm_casts".into()], "AsmCastTrait");
item_store.add_use(true, vec!["c2rust_asm_casts".into()], "AsmCastTrait");
});

let (output_name, inner_name) = operand_renames.get(tied_operand).unwrap();
Expand Down Expand Up @@ -1059,7 +1059,7 @@ impl<'c> Translation<'c> {
}

self.with_cur_file_item_store(|item_store| {
item_store.add_use(vec!["core".into(), "arch".into()], "asm");
item_store.add_use(true, vec!["core".into(), "arch".into()], "asm");
});

let mac = mk().mac(
Expand Down
12 changes: 6 additions & 6 deletions c2rust-transpile/src/translator/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ impl<'c> Translation<'c> {
self.use_crate(ExternCrate::F128);

Ok(WithStmts::new_val(
mk().path_expr(vec!["f128", "f128", "NAN"]),
mk().abs_path_expr(vec!["f128", "f128", "NAN"]),
))
}
"__builtin_signbit" | "__builtin_signbitf" | "__builtin_signbitl" => {
// Long doubles require the Float trait from num_traits to call this method
if builtin_name == "__builtin_signbitl" {
self.with_cur_file_item_store(|item_store| {
item_store.add_use(vec!["num_traits".into()], "Float");
item_store.add_use(true, vec!["num_traits".into()], "Float");
});
}

Expand All @@ -106,7 +106,7 @@ impl<'c> Translation<'c> {
Ok(val.map(|v| {
let val = mk().method_call_expr(v, "is_sign_negative", vec![]);

mk().cast_expr(val, mk().path_ty(vec!["core", "ffi", "c_int"]))
mk().cast_expr(val, mk().abs_path_ty(vec!["core", "ffi", "c_int"]))
}))
}
"__builtin_ffs" | "__builtin_ffsl" | "__builtin_ffsll" => {
Expand Down Expand Up @@ -309,7 +309,7 @@ impl<'c> Translation<'c> {
Some(mk().lit_expr(mk().int_lit(0, "isize"))),
);
self.use_crate(ExternCrate::Libc);
let size_t = mk().path_ty(vec!["libc", "size_t"]);
let size_t = mk().abs_path_ty(vec!["libc", "size_t"]);
mk().cast_expr(if_expr, size_t)
}))
})
Expand Down Expand Up @@ -772,7 +772,7 @@ impl<'c> Translation<'c> {
) -> TranslationResult<WithStmts<Box<Expr>>> {
let name = &builtin_name[10..];
self.use_crate(ExternCrate::Libc);
let mem = mk().path_expr(vec!["libc", name]);
let mem = mk().abs_path_expr(vec!["libc", name]);
let args = self.convert_exprs(ctx.used(), args, None)?;
args.and_then(|args| {
if args.len() != arg_types.len() {
Expand All @@ -791,7 +791,7 @@ impl<'c> Translation<'c> {
.map(|(arg, &ty)| {
if ty == LibcFnArgType::Size {
self.use_crate(ExternCrate::Libc);
mk().cast_expr(arg, mk().path_ty(vec!["libc", "size_t"]))
mk().cast_expr(arg, mk().abs_path_ty(vec!["libc", "size_t"]))
} else {
arg
}
Expand Down
2 changes: 1 addition & 1 deletion c2rust-transpile/src/translator/literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<'c> Translation<'c> {

self.use_crate(ExternCrate::F128);

let fn_path = mk().path_expr(vec!["f128", "f128", "new"]);
let fn_path = mk().abs_path_expr(vec!["f128", "f128", "new"]);
let args = vec![mk().lit_expr(mk().float_unsuffixed_lit(&str))];

mk().call_expr(fn_path, args)
Expand Down
4 changes: 2 additions & 2 deletions c2rust-transpile/src/translator/main_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'c> Translation<'c> {
Some(mk().path_ty(vec![mk().path_segment_with_args(
"Vec",
mk().angle_bracketed_args(vec![
mk().mutbl().ptr_ty(mk().path_ty(vec!["core", "ffi", "c_char"])),
mk().mutbl().ptr_ty(mk().abs_path_ty(vec!["core", "ffi", "c_char"])),
]),
)])),
Some(mk().call_expr(mk().path_expr(vec!["Vec", "new"]), vec![])),
Expand Down Expand Up @@ -126,7 +126,7 @@ impl<'c> Translation<'c> {
Some(mk().path_ty(vec![mk().path_segment_with_args(
"Vec",
mk().angle_bracketed_args(vec![
mk().mutbl().ptr_ty(mk().path_ty(vec!["core", "ffi", "c_char"])),
mk().mutbl().ptr_ty(mk().abs_path_ty(vec!["core", "ffi", "c_char"])),
]),
)])),
Some(mk().call_expr(mk().path_expr(vec!["Vec", "new"]), vec![])),
Expand Down
Loading