Skip to content

Commit

Permalink
Fix u16 enum repr.
Browse files Browse the repository at this point in the history
A previous change assumed we could always treat u16 as char16_t, since
it appeared only to be used to represent char16_t. It turns out it's
also used for the representation of 16-bit-wide enums, so we need to
keep track of the distinction between these two types as they travel
through autocxx-bindgen.

Relates to google/autocxx#1214
  • Loading branch information
adetaylor committed Jan 31, 2023
1 parent a7aabe9 commit 4461d93
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 1 addition & 3 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3941,9 +3941,7 @@ impl TryToRustTy for Type {
IntKind::I8 => Ok(quote! { i8 }.into()),
IntKind::U8 => Ok(quote! { u8 }.into()),
IntKind::I16 => Ok(quote! { i16 }.into()),
IntKind::U16 if ctx.options().use_distinct_char16_t => {
Ok(quote! { c_char16_t }.into())
}
IntKind::Char16 => Ok(quote! { c_char16_t }.into()),
IntKind::U16 => Ok(quote! { u16 }.into()),
IntKind::I32 => Ok(quote! { i32 }.into()),
IntKind::U32 => Ok(quote! { u32 }.into()),
Expand Down
1 change: 1 addition & 0 deletions bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
CXType_Short => TypeKind::Int(IntKind::Short),
CXType_UShort => TypeKind::Int(IntKind::UShort),
CXType_WChar => TypeKind::Int(IntKind::WChar),
CXType_Char16 if self.options().use_distinct_char16_t => TypeKind::Int(IntKind::Char16),
CXType_Char16 => TypeKind::Int(IntKind::U16),
CXType_Char32 => TypeKind::Int(IntKind::U32),
CXType_Long => TypeKind::Int(IntKind::Long),
Expand Down
9 changes: 6 additions & 3 deletions bindgen/ir/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ pub enum IntKind {
/// A 16-bit signed integer.
I16,

/// Either a `char16_t` or a `wchar_t`.
/// A 16-bit integer, used only for enum size representation.
U16,

/// Either a `char16_t` or a `wchar_t`.
Char16,

/// A 32-bit signed integer.
I32,

Expand Down Expand Up @@ -93,7 +96,7 @@ impl IntKind {
// TODO(emilio): wchar_t can in theory be signed, but we have no way
// to know whether it is or not right now (unlike char, there's no
// WChar_S / WChar_U).
Bool | UChar | UShort | UInt | ULong | ULongLong | U8 | U16 |
Bool | UChar | UShort | UInt | ULong | ULongLong | U8 | U16 | Char16 |
WChar | U32 | U64 | U128 => false,

SChar | Short | Int | Long | LongLong | I8 | I16 | I32 | I64 |
Expand All @@ -112,7 +115,7 @@ impl IntKind {
use self::IntKind::*;
Some(match *self {
Bool | UChar | SChar | U8 | I8 | Char { .. } => 1,
U16 | I16 => 2,
U16 | I16 | Char16 => 2,
U32 | I32 => 4,
U64 | I64 => 8,
I128 | U128 => 16,
Expand Down

0 comments on commit 4461d93

Please sign in to comment.