Improved C macro support

Hello, I feel like there's a noticeable issue with C interoperability, specifically regarding macros.

In this case I found myself with a curious issue when moving from SDL2 to SDL3:

#define SDL_WINDOW_HIGH_PIXEL_DENSITY   SDL_UINT64_C(0x0000000000002000)    /**< window uses high pixel density back buffer if possible */

The constants are now defined with a special macro that chooses the correct type for the literal based on platform to match the size, and Swift can no longer import these.

I will never understand C developers, I don't think this has any need to be a define, but in the end it's not really in issue with the library but Swift.


I'm not sure how this could best be solved, but maybe Swift could try to evaluate more complex macros as long as they can be evaluated to a simple constant?

4 Likes

I’ve had the same problem with the same kind of macros before. (probably with SDL actually!)

In modern C you can use appropriately sized enums to declare constants but a lot of projects want to keep supporting C99 where enums are only ever int. const variables don’t count as constants for the language, I think this might change with the constexpr proposal but I think that’s C2y.

2 Likes

Oh that makes more sense

I knew that C constants are very limited, but I did not know that at one point enums couldn’t use other types.

Still, supporting C99 was not something that crossed my mind. Surely no one is making graphical applications in the year 2024 in C99 :sweat_smile:

1 Like

There’s a fun thread about using at least C99 in SDL instead of C89 in the SDL3 codebase and they’ve actually landed on C89 for public headers and C90-ish for internal codebase.

They discuss the reason for using such an old standard and link to other projects’ decisions on the matter!

1 Like

Well, good for the N-Gage :slightly_smiling_face:

I suppose I shouldn't judge, ancient hardware is cool, personally I would like to try compiling Swift for my old PSP — which SDL even supports. I wonder if Embedded Swift can target it yet

1 Like

It used to be even more finicky, refusing to register C constants years ago just because they had an empty attribute (which is probably fixed now), but still can't register even simple preprocessor expressions. It would be great if the ClangImporter were updated to at least evaluate all constants, as you say.

3 Likes