Skip to content

Commit 7caecf8

Browse files
committed
transpile: add support for auto types
1 parent 50f5cb3 commit 7caecf8

5 files changed

Lines changed: 22 additions & 0 deletions

File tree

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,16 @@ impl ConversionContext {
947947
self.processed_nodes.insert(new_id, OTHER_TYPE);
948948
}
949949

950+
TypeTag::TagAutoType if expected_ty & TYPE != 0 => {
951+
let auto_id =
952+
from_value(ty_node.extras[0].clone()).expect("Auto type child not found");
953+
let auto = self.visit_type(auto_id);
954+
955+
let auto_ty = CTypeKind::Auto(auto);
956+
self.add_type(new_id, not_located(auto_ty));
957+
self.processed_nodes.insert(new_id, TYPE);
958+
}
959+
950960
t => panic!(
951961
"Type conversion not implemented for {:?} expecting {:?}",
952962
t, expected_ty

c2rust-transpile/src/c_ast/iterators.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ fn immediate_type_children(kind: &CTypeKind) -> Vec<SomeId> {
294294
Decayed(ctype)
295295
| Paren(ctype)
296296
| TypeOf(ctype)
297+
| Auto(ctype)
297298
| Complex(ctype)
298299
| ConstantArray(ctype, _)
299300
| IncompleteArray(ctype) => intos![ctype],

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ impl TypedAstContext {
760760
CDeclKind::Typedef { typ: ty, .. } => ty.ctype,
761761
_ => panic!("Typedef decl did not point to a typedef"),
762762
},
763+
Auto(ty) => ty,
763764
_ => return typ,
764765
};
765766
self.resolve_type_id(ty)
@@ -778,6 +779,7 @@ impl TypedAstContext {
778779
Decayed(ty) => ty,
779780
TypeOf(ty) => ty,
780781
Paren(ty) => ty,
782+
Auto(ty) => ty,
781783
_ => return typ,
782784
};
783785
self.resolve_type_id_no_typedef(ty)
@@ -2549,6 +2551,9 @@ pub enum CTypeKind {
25492551
SSize,
25502552
PtrDiff,
25512553
WChar,
2554+
2555+
// `__auto_type` with its deduced actual type.
2556+
Auto(CTypeId),
25522557
}
25532558

25542559
impl CTypeKind {
@@ -2678,6 +2683,8 @@ impl CTypeKind {
26782683
Float128 => false,
26792684

26802685
// Non-scalars.
2686+
// TODO: we should investigate if all of these are dead code,
2687+
// and replace them with panics in that case.
26812688
Complex(_) => false,
26822689
Pointer(_) => false,
26832690
Reference(_) => false,
@@ -2700,6 +2707,7 @@ impl CTypeKind {
27002707
Vector(_, _) => false,
27012708
UnhandledSveType => false,
27022709
Atomic(_) => false,
2710+
Auto(_) => false,
27032711
}
27042712
}
27052713

@@ -2787,6 +2795,7 @@ impl CTypeKind {
27872795
SSize => false,
27882796
PtrDiff => false,
27892797
WChar => false,
2798+
Auto(_) => false,
27902799
}
27912800
}
27922801
}

c2rust-transpile/src/convert_type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ impl TypeConverter {
329329
}
330330

331331
CTypeKind::TypeOf(ty) => self.convert(ctxt, ty),
332+
CTypeKind::Auto(ty) => self.convert(ctxt, ty),
332333

333334
ref t => Err(format_err!("Unsupported type {:?}", t).into()),
334335
}

c2rust-transpile/src/translator/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4243,6 +4243,7 @@ impl<'c> Translation<'c> {
42434243
| Reference(CQualTypeId { ctype, .. })
42444244
| BlockPointer(CQualTypeId { ctype, .. })
42454245
| TypeOf(ctype)
4246+
| Auto(ctype)
42464247
| Complex(ctype) => imports.extend(self.imports_for_type(*ctype)),
42474248
Enum(decl_id) | Typedef(decl_id) | Union(decl_id) | Struct(decl_id) => {
42484249
let mut decl_id = *decl_id;

0 commit comments

Comments
 (0)