Skip to content

Fixes C99 Enumeration Integer Promotions - #416

Merged
drmortalwombat merged 6 commits into
drmortalwombat:mainfrom
jadziamd:c99-enum-integeger-promotion
Oct 10, 2026
Merged

drmortalwombat merged 6 commits into
drmortalwombat:mainfrom
jadziamd:c99-enum-integeger-promotion

Conversation

@jadziamd

@jadziamd jadziamd commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

This fixes the enumerations not conforming to the C99 specification It specifies that enumeration constants have type int and that their values must be representable as int (6.7.2.2 paragraphs 2 and 3). An enum object has an implementation-defined compatible integer type (paragraph 4), which determines its integer promotions. Integer promotions and usual arithmetic conversions are specified in 6.3.1.1 and 6.3.1.8. Right shift of a negative signed value is implementation-defined (6.5.7 paragraph 5). See the C99 committee draft N1256.

  • Fixes unsigned arithmetic involving C enumerators by assigning constants signed int types independently of enum storage.
  • Preserves enum variables’ width and signedness by reusing the existing declaration when resolving enum Tag.
  • Rejects C enumerator values outside the target’s int range by validating explicit and implicit values before narrowing.

…nts signed int types independently of enum storage.

Preserves enum variables’ width and signedness by reusing the existing declaration when resolving enum Tag.
Rejects C enumerator values outside the target’s int range by validating explicit and implicit values before narrowing.
@drmortalwombat

Copy link
Copy Markdown
Owner

C23 now also supports the explicit underlying type for enums, so this should be taken over from the C++ to allow enum values of a specified type in C.

@drmortalwombat

Copy link
Copy Markdown
Owner

Providing a constant out of the valid range would be a warning, not an error

@jadziamd

Copy link
Copy Markdown
Contributor Author

Added the addition and the diagnostic fix.

@drmortalwombat

Copy link
Copy Markdown
Owner

The enumeration value range is limited to the provided constants by design to give the compiler more information on the value range to expeced. So it should not be initialized with the base type value range.

@jadziamd

Copy link
Copy Markdown
Contributor Author

Reverted the initialisation range.

@drmortalwombat

Copy link
Copy Markdown
Owner

Might make sense to use the "mBase" member of the "enum" type declaration to point to the base instead of the DTF_FIXED_ENUM. This way one could check for IsSame() and avoid the additional flag

@drmortalwombat

Copy link
Copy Markdown
Owner

Some of the size promotions should also be done for ALL integer types, not just enums:

char k = 1; printf("%d, %d\n", sizeof(k), sizeof(+k));

should print 1, 2 and not 1, 1

John Schneiderman added 2 commits October 10, 2026 17:55
…them with IsSame instead of using a fixed-enum flag.
…g, constant folding and code generation.

Adds integer promotion tests.
@jadziamd

Copy link
Copy Markdown
Contributor Author

Made the change and added the promotion tests. They found a few promotion issues which were also fixed.

@drmortalwombat
drmortalwombat merged commit 5945ee1 into drmortalwombat:main Oct 10, 2026
4 checks passed
@jadziamd
jadziamd deleted the c99-enum-integeger-promotion branch October 10, 2026 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants