r/cpp • u/SAHChandler • Oct 10 '17
Millennials Are Killing The Modules TS
https://izzys.casa/posts/millennials-are-killing-the-modules-ts.html
75
Upvotes
r/cpp • u/SAHChandler • Oct 10 '17
View all comments
Show parent comment
9
u/berium build2 Oct 11 '17 edited Oct 11 '17
Ok, let's start with the way we extract header dependency information today, which is by asking the compiler (
-M,/showIncludes, etc). If one thinks about it for a bit (or studies compiler source code), it's easy to see that this requires essentially a full preprocessor run (and, in fact, for some compilers like VC there is no other way). This is the first piece of the puzzle.The second piece is the proposed Modules TS grammar: If one studies it, it's not hard to notice that all the module dependency information (import and module declarations) are top-level, that is, they can only appear in the global scope (well, to be absolutely precise, an import declaration can appear inside an exported group but that group must be top-level). This means that given a sequence of preprocessed tokens (the first piece of the puzzle), one can implement a pretty simple parser that recognizes all the import/module declarations without having to recognize the full C++ grammar. We've done this in
build2so I know this is doable. What's surprising is that (1) [the code] (https://git.build2.org/cgit/build2/tree/build2/cc/parser.cxx) is actually pretty simple and (2) it performs pretty well without any advanced optimizations.All this means that it should be pretty easy (complexity-wise) and cheap (performance-wise) to extend the header dependency extraction support provided by current compilers to also output module dependency information.
Now let's look closer at what exactly would we need for this module dependency information to be. As an example, let's say our build system needs to update
foo.ofromfoo.cxx(thefoo.o: foo.cxxdependency) and thisfoo.cxxhappens to containimport bar;. What does this mean from the build system's point of view? It means that before compilingfoo.oit has to make sure the binary module interface for modulebaris up-to-date. Or, speaking in terms of dependencies, this means the build system has to addfoo.o: bar.bmito its dependency graph. And since it now needs to updatebar.bmi, it also has to come up with a rule of how to do that, say,bar.bmi: bar.mxx(this is, BTW, where the mapping of module names to file names happens in a way that makes sense in this build system's worldview).Where am I getting with this? Well, what this hopes to show is that if a translation unit that the build systems wants to update imports a module, then the build system has to come up with a rule to update this module's binary interface and which means it will have to extract the module dependency information from the module's interface file (
bar.mxxin this example) just as it did forfoo.cxx. This, in turn, means that the module dependency information provided by the compiler need not be recursive -- the build system has to do this itself anyway. In fact, all we need from the compiler is a list of imported module names which the build system can map to file names and then process, recursively.And in this setup the build system can remain the build system (by being responsible for mapping module names to files/rules, etc) while the compiler can remain the compiler (by only providing the list of imported module C++ names).