r/ProgrammingLanguages • • Jul 26 '26

Adding cyclic modules to the C programming language

https://youtu.be/p8NpyBIRbEQ

The idea is to create a module system, within C, that you can use as a drop-in replacement for header files and forward declarations.

To my knowledge, all module implementations within the C family (eg. C++20 modules, Objective C modules, Clang modules) do not allow cyclic imports. Cyclic imports are necessary if you want to remove forward declarations from C (otherwise mutually recursive data structures would need to exist in the same module).

When looking closely at the C grammar, I noticed something extraordinary and borderline miraculous - C without expressions is context-free you can extract the names of symbol definitions without prior access to a symbol table! With this knowledge, it becomes possible to implement cyclic modules within the C language.

EDIT: Added a strikethrough. C without expressions still has some ambiguities in the parameter list, and my use of "context-free" is incorrect here. https://www.reddit.com/r/C_Programming/comments/1v7l174/is_c_without_expressions_contextfree/

16 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/TheChief275 Aug 06 '26

I think it would be a better effort to support constexpr than it would be to have export static. It is fairly nonsensical, and the important part wasn't export static, but that the combination of "static const" means true const to some compilers. In C this is rather inconsistent (Clang treats static const variables as compile-time constants, while GCC doesn't), while C++ does mostly treat them as compile-time constants, except in certain situations (which ultimately makes it unreliable as well). Since the intention is likely for this to be used with modern C/C++ and they both have constexpr, that would be the right decision.

In the same way the foo(void) signature could be dropped as well, as I think it has been deprecated in C++ for a while, and in C since C23 (which added constexpr).

Regarding the header modules, maybe it could be done through a .hmod file to mirror the .cmod file? This could only include declarations instead of definitions. Though being able to import header files would be the most convenient, it would necessitate additional syntax and you're likely to deal with files that weren't created to be imported by your module system (having the .hmod would give a guarantee in a way).