Skip to content

Commit 588740f

Browse files
committed
transpile: add synthetic CTypeKinds for sized types
these CTypeKinds do not correspond to Clang type kinds, but it is useful to create them when we process the C AST so we can recognize the type aliases that define them and reify them into the appropriate types on the Rust side, which do exist at a language level there (such as u8)
1 parent dfc0d3d commit 588740f

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

c2rust-transpile/src/c_ast/iterators.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ fn immediate_type_children(kind: &CTypeKind) -> Vec<SomeId> {
285285
TypeOfExpr(e) => intos![e],
286286
Void | Bool | Short | Int | Long | LongLong | UShort | UInt | ULong | ULongLong | SChar
287287
| UChar | Char | Double | LongDouble | Float | Int128 | UInt128 | BuiltinFn | Half
288-
| BFloat16 | UnhandledSveType | Float128 => {
288+
| BFloat16 | UnhandledSveType | Float128 | Int8 | Int16 | Int32 | Int64 | IntPtr
289+
| UInt8 | UInt16 | UInt32 | UInt64 | UIntPtr | IntMax | UIntMax | Size | SSize
290+
| PtrDiff | WChar => {
289291
vec![]
290292
}
291293

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,9 +1711,35 @@ pub enum CTypeKind {
17111711
Float128,
17121712
// Atomic types (6.7.2.4)
17131713
Atomic(CQualTypeId),
1714+
1715+
// Rust sized types, pullback'd into C so that we can treat uint16_t, etc. as real types.
1716+
Int8,
1717+
Int16,
1718+
Int32,
1719+
Int64,
1720+
IntPtr,
1721+
UInt8,
1722+
UInt16,
1723+
UInt32,
1724+
UInt64,
1725+
UIntPtr,
1726+
IntMax,
1727+
UIntMax,
1728+
Size,
1729+
SSize,
1730+
PtrDiff,
1731+
WChar,
17141732
}
17151733

17161734
impl CTypeKind {
1735+
pub const PULLBACK_KINDS: [CTypeKind; 16] = {
1736+
use CTypeKind::*;
1737+
[
1738+
Int8, Int16, Int32, Int64, IntPtr, UInt8, UInt16, UInt32, UInt64, UIntPtr, IntMax,
1739+
UIntMax, Size, SSize, PtrDiff, WChar,
1740+
]
1741+
};
1742+
17171743
pub fn as_str(&self) -> &'static str {
17181744
use CTypeKind::*;
17191745
match self {
@@ -1738,6 +1764,24 @@ impl CTypeKind {
17381764
Half => "half",
17391765
BFloat16 => "bfloat16",
17401766
Float128 => "__float128",
1767+
1768+
Int8 => "int8_t",
1769+
Int16 => "int16_t",
1770+
Int32 => "int32_t",
1771+
Int64 => "int64_t",
1772+
IntPtr => "intptr_t",
1773+
UInt8 => "uint8_t",
1774+
UInt16 => "uint16_t",
1775+
UInt32 => "uint32_t",
1776+
UInt64 => "uint64_t",
1777+
UIntPtr => "uintptr_t",
1778+
IntMax => "intmax_t",
1779+
UIntMax => "uintmax_t",
1780+
Size => "size_t",
1781+
SSize => "ssize_t",
1782+
PtrDiff => "ptrdiff_t",
1783+
WChar => "wchar_t",
1784+
17411785
_ => unimplemented!("Printer::print_type({:?})", self),
17421786
}
17431787
}
@@ -1803,14 +1847,43 @@ impl CTypeKind {
18031847
use CTypeKind::*;
18041848
matches!(
18051849
self,
1806-
Bool | UChar | UInt | UShort | ULong | ULongLong | UInt128
1850+
Bool | UChar
1851+
| UInt
1852+
| UShort
1853+
| ULong
1854+
| ULongLong
1855+
| UInt128
1856+
| UInt8
1857+
| UInt16
1858+
| UInt32
1859+
| UInt64
1860+
| UIntPtr
1861+
| UIntMax
1862+
| Size
1863+
| WChar
18071864
)
18081865
}
18091866

18101867
pub fn is_signed_integral_type(&self) -> bool {
18111868
use CTypeKind::*;
18121869
// `Char` is true on the platforms we handle
1813-
matches!(self, Char | SChar | Int | Short | Long | LongLong | Int128)
1870+
matches!(
1871+
self,
1872+
Char | SChar
1873+
| Int
1874+
| Short
1875+
| Long
1876+
| LongLong
1877+
| Int128
1878+
| Int8
1879+
| Int16
1880+
| Int32
1881+
| Int64
1882+
| IntPtr
1883+
| IntMax
1884+
| SSize
1885+
| PtrDiff
1886+
)
18141887
}
18151888

18161889
pub fn is_floating_type(&self) -> bool {

c2rust-transpile/src/convert_type.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,23 @@ impl TypeConverter {
333333
CTypeKind::UInt128 => Ok(mk().path_ty(mk().path(vec!["u128"]))),
334334
CTypeKind::BFloat16 => Ok(mk().path_ty(mk().path(vec!["bf16"]))),
335335

336+
CTypeKind::Int8 => Ok(mk().path_ty(mk().path(vec!["i8"]))),
337+
CTypeKind::Int16 => Ok(mk().path_ty(mk().path(vec!["i16"]))),
338+
CTypeKind::Int32 => Ok(mk().path_ty(mk().path(vec!["i32"]))),
339+
CTypeKind::Int64 => Ok(mk().path_ty(mk().path(vec!["i64"]))),
340+
CTypeKind::IntPtr => Ok(mk().path_ty(mk().path(vec!["isize"]))),
341+
CTypeKind::UInt8 => Ok(mk().path_ty(mk().path(vec!["u8"]))),
342+
CTypeKind::UInt16 => Ok(mk().path_ty(mk().path(vec!["u16"]))),
343+
CTypeKind::UInt32 => Ok(mk().path_ty(mk().path(vec!["u32"]))),
344+
CTypeKind::UInt64 => Ok(mk().path_ty(mk().path(vec!["u64"]))),
345+
CTypeKind::UIntPtr => Ok(mk().path_ty(mk().path(vec!["usize"]))),
346+
CTypeKind::IntMax => Ok(mk().path_ty(mk().path(vec!["libc", "intmax_t"]))),
347+
CTypeKind::UIntMax => Ok(mk().path_ty(mk().path(vec!["libc", "uintmax_t"]))),
348+
CTypeKind::Size => Ok(mk().path_ty(mk().path(vec!["usize"]))),
349+
CTypeKind::SSize => Ok(mk().path_ty(mk().path(vec!["isize"]))),
350+
CTypeKind::PtrDiff => Ok(mk().path_ty(mk().path(vec!["isize"]))),
351+
CTypeKind::WChar => Ok(mk().path_ty(mk().path(vec!["libc", "wchar_t"]))),
352+
336353
CTypeKind::Pointer(qtype) => self.convert_pointer(ctxt, qtype),
337354

338355
CTypeKind::Elaborated(ref ctype) => self.convert(ctxt, *ctype),

c2rust-transpile/src/translator/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4988,6 +4988,11 @@ impl<'c> Translation<'c> {
49884988
Void | Char | SChar | UChar | Short | UShort | Int | UInt | Long | ULong | LongLong
49894989
| ULongLong | Int128 | UInt128 | Half | BFloat16 | Float | Double | LongDouble
49904990
| Float128 => {}
4991+
// other libc types
4992+
WChar | IntMax | UIntMax => {}
4993+
// Built-in Rust sized types
4994+
Int8 | Int16 | Int32 | Int64 | IntPtr | UInt8 | UInt16 | UInt32 | UInt64 | UIntPtr
4995+
| Size | SSize | PtrDiff => {}
49914996
// Bool uses the bool type, so no dependency on libc
49924997
Bool => {}
49934998
Paren(ctype)

0 commit comments

Comments
 (0)