#188
translator: promote enum underlying types to avoid Go integer overflows
Fixes #133
Problem
CEnumSpec.PromoteType previously hardcoded every C enum's underlying type to int (int32). When a C enum contains unsigned 32-bit constants above math.MaxInt32 (such as 0xFFFFFFFF), mixed signed/large values, or 64-bit constants, Go constant declarations overflow at compile time across eval, expand, and cgo rules.
Changes
- Range-based enum type promotion (
translator/model_enum.go): Track the min/max enumerator values across an enum and promoteCEnumSpec.Typeto the smallest matching Go integer type:[math.MinInt32, math.MaxInt32]→int32(preserves existing behavior)[0, math.MaxUint32]with max> math.MaxInt32→uint32- Values outside
int32/uint32withinint64(including mixed negative +0xFFFFFFFF,< MinInt32, and(MaxUint32, MaxInt64]) →int64 - Values
> math.MaxInt64→uint64 - Uses
long long/unsigned long longspecs for 64-bit enums so Go types remainint64/uint64regardless ofLongIs64Bit, while preservingTypeTips.enum(unsigned) support.
- Untyped numeric literals & expression validation for
ConstRules.enum: expand(translator/helpers.go,translator/ast_walker.go):- Strip C integer suffixes (
U/L/UL/LL/ULL) without wrapping inuint32(...)/int64(...)/uint64(...)for enum expressions so expanded constants remain untyped and assignable to defined enum types, and fix uppercase0X/0Bnumeric parsing. - Validate expanded Go expressions with
go/parserandgo/constantagainst C's evaluated value, falling back to the C-evaluated value when Go would fail to parse (e.g. C-style casts) or evaluate differently due to C wrap/overflow semantics (e.g.~0U,1 << 31).
- Strip C integer suffixes (
ConstRules.enum: cgofallback for values> math.MaxInt64(translator/ast_walker.go):cmd/cgodecodes DWARF enum constants as signedint64, turning values> math.MaxInt64(e.g.1ULL << 63,0xFFFFFFFFFFFFFFFFULL) into negative untyped constants that overflowuint64. Fall back to the evaluated unsigned value for those enumerators so generatedcgobindings compile with the true unsigned value.
- Tests (
translator/enum_test.go,generator/gen_const_test.go):- Added boundary/promotion unit tests,
expandandcgotranslation tests,TypeTipsunsignedtests, and end-to-end generator +go build(including realcgo) compilation tests.
- Added boundary/promotion unit tests,