Skip to content

Commit 8cb4d97

Browse files
committed
transpile: Support absolute paths in PathedMultiImports
1 parent 9506e09 commit 8cb4d97

File tree

5 files changed

+52
-30
lines changed

5 files changed

+52
-30
lines changed

c2rust-transpile/src/rust_ast/item_store.rs

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,47 @@ impl MultiImport {
3636
}
3737

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

4141
impl PathedMultiImports {
4242
pub fn new() -> Self {
4343
Self::default()
4444
}
4545

46-
pub fn get_mut(&mut self, path: Vec<String>) -> &mut MultiImport {
47-
self.0.entry(path).or_insert(MultiImport::new())
46+
pub fn get_mut(&mut self, is_absolute: bool, path: Vec<String>) -> &mut MultiImport {
47+
self.0
48+
.entry((is_absolute, path))
49+
.or_insert(MultiImport::new())
4850
}
4951

5052
pub fn into_items(self) -> Vec<Box<Item>> {
51-
fn build_items((mut path, imports): (Vec<String>, MultiImport)) -> Box<Item> {
52-
let mut leaves = imports.leaves;
53-
let attrs = imports.attrs.unwrap_or_else(mk);
54-
55-
if leaves.len() == 1 {
56-
path.push(leaves.pop().unwrap());
57-
58-
let path = mk().abs_path(path);
59-
attrs.use_simple_item(path, None as Option<Ident>)
60-
} else {
61-
let path = mk().abs_path(path);
62-
attrs.use_multiple_item(path, leaves.into_iter())
63-
}
64-
}
65-
66-
self.0.into_iter().map(build_items).collect()
53+
self.0
54+
.into_iter()
55+
.map(|((is_absolute, mut path), imports)| {
56+
let mut leaves = imports.leaves;
57+
let attrs = imports.attrs.unwrap_or_else(mk);
58+
59+
if leaves.len() == 1 {
60+
path.push(leaves.pop().unwrap());
61+
62+
let path = if is_absolute {
63+
mk().abs_path(path)
64+
} else {
65+
mk().path(path)
66+
};
67+
68+
attrs.use_simple_item(path, None as Option<Ident>)
69+
} else {
70+
let path = if is_absolute {
71+
mk().abs_path(path)
72+
} else {
73+
mk().path(path)
74+
};
75+
76+
attrs.use_multiple_item(path, leaves.into_iter())
77+
}
78+
})
79+
.collect()
6780
}
6881
}
6982

@@ -89,12 +102,20 @@ impl ItemStore {
89102
self.foreign_items.push(item);
90103
}
91104

92-
pub fn add_use(&mut self, path: Vec<String>, ident: &str) {
93-
self.uses.get_mut(path).insert(ident)
105+
pub fn add_use(&mut self, is_absolute: bool, path: Vec<String>, ident: &str) {
106+
self.uses.get_mut(is_absolute, path).insert(ident)
94107
}
95108

96-
pub fn add_use_with_attr(&mut self, path: Vec<String>, ident: &str, attrs: Builder) {
97-
self.uses.get_mut(path).insert_with_attr(ident, attrs)
109+
pub fn add_use_with_attr(
110+
&mut self,
111+
is_absolute: bool,
112+
path: Vec<String>,
113+
ident: &str,
114+
attrs: Builder,
115+
) {
116+
self.uses
117+
.get_mut(is_absolute, path)
118+
.insert_with_attr(ident, attrs)
98119
}
99120

100121
pub fn drain(&mut self) -> (Vec<Box<Item>>, Vec<ForeignItem>, PathedMultiImports) {

c2rust-transpile/src/translator/assembly.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ impl<'c> Translation<'c> {
931931

932932
// Import the trait into scope
933933
self.with_cur_file_item_store(|item_store| {
934-
item_store.add_use(vec!["c2rust_asm_casts".into()], "AsmCastTrait");
934+
item_store.add_use(true, vec!["c2rust_asm_casts".into()], "AsmCastTrait");
935935
});
936936

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

10611061
self.with_cur_file_item_store(|item_store| {
1062-
item_store.add_use(vec!["core".into(), "arch".into()], "asm");
1062+
item_store.add_use(true, vec!["core".into(), "arch".into()], "asm");
10631063
});
10641064

10651065
let mac = mk().mac(

c2rust-transpile/src/translator/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<'c> Translation<'c> {
9797
// Long doubles require the Float trait from num_traits to call this method
9898
if builtin_name == "__builtin_signbitl" {
9999
self.with_cur_file_item_store(|item_store| {
100-
item_store.add_use(vec!["num_traits".into()], "Float");
100+
item_store.add_use(true, vec!["num_traits".into()], "Float");
101101
});
102102
}
103103

c2rust-transpile/src/translator/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ fn make_submodule(
10141014
None => continue,
10151015
};
10161016

1017-
use_item_store.add_use_with_attr(use_path, &ident_name, vis);
1017+
use_item_store.add_use_with_attr(false, use_path, &ident_name, vis);
10181018
}
10191019

10201020
for foreign_item in foreign_items.iter() {
@@ -1024,7 +1024,7 @@ fn make_submodule(
10241024
};
10251025
let use_path = vec!["self".into(), mod_name.clone()];
10261026

1027-
use_item_store.add_use(use_path, &ident_name);
1027+
use_item_store.add_use(false, use_path, &ident_name);
10281028
}
10291029

10301030
for item in uses.into_items() {
@@ -4711,7 +4711,7 @@ impl<'c> Translation<'c> {
47114711
self.use_crate(ExternCrate::NumTraits);
47124712

47134713
self.with_cur_file_item_store(|item_store| {
4714-
item_store.add_use(vec!["num_traits".into()], "ToPrimitive");
4714+
item_store.add_use(true, vec!["num_traits".into()], "ToPrimitive");
47154715
});
47164716
let to_method_name = match target_ty_ctype {
47174717
CTypeKind::Float => "to_f32",
@@ -5167,7 +5167,7 @@ impl<'c> Translation<'c> {
51675167
.borrow_mut()
51685168
.entry(decl_file_id)
51695169
.or_default()
5170-
.add_use(module_path, ident_name);
5170+
.add_use(false, module_path, ident_name);
51715171
}
51725172

51735173
fn import_type(&self, ctype: CTypeId, decl_file_id: FileId) {

c2rust-transpile/src/translator/simd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static SIMD_X86_64_ONLY: &[&str] = &[
6969

7070
fn add_arch_use(store: &mut ItemStore, arch_name: &str, item_name: &str) {
7171
store.add_use_with_attr(
72+
true,
7273
vec!["core".into(), "arch".into(), arch_name.into()],
7374
item_name,
7475
mk().call_attr("cfg", vec![mk().meta_namevalue("target_arch", arch_name)])

0 commit comments

Comments
 (0)