Expand description
C++20 modules: what a file declares itself to be, and what it imports.
§Why this is a layer of its own
A module is not an #include, and the difference is not a detail of style — it changes what a file’s
analysis can be based on:
#includeis textual. The includer’s macros are in force inside the header, so a header’s#ifconditions depend on who included it. That is the whole reasoncrate::graphcarries a macro environment along its edges and why one header can be analysed under two different configurations.importis not. A module unit is compiled on its own, with its own macro environment, and what it exports is a set of declarations. The importer’s macros are irrelevant to the module and the module’s macros are irrelevant to the importer — except for the one thing that does cross, the declarations.
So a module graph must not reuse the include walk’s macro chaining. Doing so would be worse than
useless: it would inherit an environment that no compiler ever used and then decide the module’s own
conditions against it, producing confident answers that are simply wrong. This module reads the shape a
file declares, and crate::modules resolves the imports between files without pretending they are
textual.
§The unit kinds
The standard names the shapes ([module.unit]), and each is a different answer to “what is this file”:
module; <- global module fragment: #includes that belong to no module
#include <vector>
export module m; <- primary module interface unit
export module m:part; <- module partition *interface* unit
module m; <- module implementation unit
module m:part; <- module partition implementation unit
module : private; <- private module fragment: importers cannot see past itA partition is not a module of its own: m:part belongs to m, is imported as import :part from
inside m, and cannot be imported from outside it. Keeping the partition separate from the module name
is therefore not bookkeeping — it is the difference between resolving an import to the right file and
resolving it to a file that does not exist.
§Extracting module unit kinds
ModuleUnit::InterfaceUnit is the one unit that answers an import m; from outside, which is why
ModuleUnit::exports_to_importers is the predicate a resolver needs and is_interface is not enough
on its own: a partition interface unit is an interface and not importable by name.
Structs§
- Import
Declaration - One
importdeclaration. - Module
Info - What a file declares about modules.
Enums§
- Import
Target - What an
importdeclaration names. - Module
Unit - What a file declares itself to be.