1f4a2713aSLionel Sambuc======= 2f4a2713aSLionel SambucModules 3f4a2713aSLionel Sambuc======= 4f4a2713aSLionel Sambuc 5f4a2713aSLionel Sambuc.. contents:: 6f4a2713aSLionel Sambuc :local: 7f4a2713aSLionel Sambuc 8f4a2713aSLionel SambucIntroduction 9f4a2713aSLionel Sambuc============ 10f4a2713aSLionel SambucMost software is built using a number of software libraries, including libraries supplied by the platform, internal libraries built as part of the software itself to provide structure, and third-party libraries. For each library, one needs to access both its interface (API) and its implementation. In the C family of languages, the interface to a library is accessed by including the appropriate header files(s): 11f4a2713aSLionel Sambuc 12f4a2713aSLionel Sambuc.. code-block:: c 13f4a2713aSLionel Sambuc 14f4a2713aSLionel Sambuc #include <SomeLib.h> 15f4a2713aSLionel Sambuc 16f4a2713aSLionel SambucThe implementation is handled separately by linking against the appropriate library. For example, by passing ``-lSomeLib`` to the linker. 17f4a2713aSLionel Sambuc 18f4a2713aSLionel SambucModules provide an alternative, simpler way to use software libraries that provides better compile-time scalability and eliminates many of the problems inherent to using the C preprocessor to access the API of a library. 19f4a2713aSLionel Sambuc 20f4a2713aSLionel SambucProblems with the current model 21f4a2713aSLionel Sambuc------------------------------- 22f4a2713aSLionel SambucThe ``#include`` mechanism provided by the C preprocessor is a very poor way to access the API of a library, for a number of reasons: 23f4a2713aSLionel Sambuc 24f4a2713aSLionel Sambuc* **Compile-time scalability**: Each time a header is included, the 25f4a2713aSLionel Sambuc compiler must preprocess and parse the text in that header and every 26f4a2713aSLionel Sambuc header it includes, transitively. This process must be repeated for 27f4a2713aSLionel Sambuc every translation unit in the application, which involves a huge 28f4a2713aSLionel Sambuc amount of redundant work. In a project with *N* translation units 29f4a2713aSLionel Sambuc and *M* headers included in each translation unit, the compiler is 30f4a2713aSLionel Sambuc performing *M x N* work even though most of the *M* headers are 31f4a2713aSLionel Sambuc shared among multiple translation units. C++ is particularly bad, 32f4a2713aSLionel Sambuc because the compilation model for templates forces a huge amount of 33f4a2713aSLionel Sambuc code into headers. 34f4a2713aSLionel Sambuc 35f4a2713aSLionel Sambuc* **Fragility**: ``#include`` directives are treated as textual 36f4a2713aSLionel Sambuc inclusion by the preprocessor, and are therefore subject to any 37f4a2713aSLionel Sambuc active macro definitions at the time of inclusion. If any of the 38f4a2713aSLionel Sambuc active macro definitions happens to collide with a name in the 39f4a2713aSLionel Sambuc library, it can break the library API or cause compilation failures 40f4a2713aSLionel Sambuc in the library header itself. For an extreme example, 41f4a2713aSLionel Sambuc ``#define std "The C++ Standard"`` and then include a standard 42f4a2713aSLionel Sambuc library header: the result is a horrific cascade of failures in the 43f4a2713aSLionel Sambuc C++ Standard Library's implementation. More subtle real-world 44f4a2713aSLionel Sambuc problems occur when the headers for two different libraries interact 45f4a2713aSLionel Sambuc due to macro collisions, and users are forced to reorder 46f4a2713aSLionel Sambuc ``#include`` directives or introduce ``#undef`` directives to break 47f4a2713aSLionel Sambuc the (unintended) dependency. 48f4a2713aSLionel Sambuc 49f4a2713aSLionel Sambuc* **Conventional workarounds**: C programmers have 50f4a2713aSLionel Sambuc adopted a number of conventions to work around the fragility of the 51f4a2713aSLionel Sambuc C preprocessor model. Include guards, for example, are required for 52f4a2713aSLionel Sambuc the vast majority of headers to ensure that multiple inclusion 53f4a2713aSLionel Sambuc doesn't break the compile. Macro names are written with 54f4a2713aSLionel Sambuc ``LONG_PREFIXED_UPPERCASE_IDENTIFIERS`` to avoid collisions, and some 55f4a2713aSLionel Sambuc library/framework developers even use ``__underscored`` names 56f4a2713aSLionel Sambuc in headers to avoid collisions with "normal" names that (by 57f4a2713aSLionel Sambuc convention) shouldn't even be macros. These conventions are a 58f4a2713aSLionel Sambuc barrier to entry for developers coming from non-C languages, are 59f4a2713aSLionel Sambuc boilerplate for more experienced developers, and make our headers 60f4a2713aSLionel Sambuc far uglier than they should be. 61f4a2713aSLionel Sambuc 62f4a2713aSLionel Sambuc* **Tool confusion**: In a C-based language, it is hard to build tools 63f4a2713aSLionel Sambuc that work well with software libraries, because the boundaries of 64f4a2713aSLionel Sambuc the libraries are not clear. Which headers belong to a particular 65f4a2713aSLionel Sambuc library, and in what order should those headers be included to 66f4a2713aSLionel Sambuc guarantee that they compile correctly? Are the headers C, C++, 67f4a2713aSLionel Sambuc Objective-C++, or one of the variants of these languages? What 68f4a2713aSLionel Sambuc declarations in those headers are actually meant to be part of the 69f4a2713aSLionel Sambuc API, and what declarations are present only because they had to be 70f4a2713aSLionel Sambuc written as part of the header file? 71f4a2713aSLionel Sambuc 72f4a2713aSLionel SambucSemantic import 73f4a2713aSLionel Sambuc--------------- 74f4a2713aSLionel SambucModules improve access to the API of software libraries by replacing the textual preprocessor inclusion model with a more robust, more efficient semantic model. From the user's perspective, the code looks only slightly different, because one uses an ``import`` declaration rather than a ``#include`` preprocessor directive: 75f4a2713aSLionel Sambuc 76f4a2713aSLionel Sambuc.. code-block:: c 77f4a2713aSLionel Sambuc 78f4a2713aSLionel Sambuc import std.io; // pseudo-code; see below for syntax discussion 79f4a2713aSLionel Sambuc 80f4a2713aSLionel SambucHowever, this module import behaves quite differently from the corresponding ``#include <stdio.h>``: when the compiler sees the module import above, it loads a binary representation of the ``std.io`` module and makes its API available to the application directly. Preprocessor definitions that precede the import declaration have no impact on the API provided by ``std.io``, because the module itself was compiled as a separate, standalone module. Additionally, any linker flags required to use the ``std.io`` module will automatically be provided when the module is imported [#]_ 81f4a2713aSLionel SambucThis semantic import model addresses many of the problems of the preprocessor inclusion model: 82f4a2713aSLionel Sambuc 83f4a2713aSLionel Sambuc* **Compile-time scalability**: The ``std.io`` module is only compiled once, and importing the module into a translation unit is a constant-time operation (independent of module system). Thus, the API of each software library is only parsed once, reducing the *M x N* compilation problem to an *M + N* problem. 84f4a2713aSLionel Sambuc 85f4a2713aSLionel Sambuc* **Fragility**: Each module is parsed as a standalone entity, so it has a consistent preprocessor environment. This completely eliminates the need for ``__underscored`` names and similarly defensive tricks. Moreover, the current preprocessor definitions when an import declaration is encountered are ignored, so one software library can not affect how another software library is compiled, eliminating include-order dependencies. 86f4a2713aSLionel Sambuc 87f4a2713aSLionel Sambuc* **Tool confusion**: Modules describe the API of software libraries, and tools can reason about and present a module as a representation of that API. Because modules can only be built standalone, tools can rely on the module definition to ensure that they get the complete API for the library. Moreover, modules can specify which languages they work with, so, e.g., one can not accidentally attempt to load a C++ module into a C program. 88f4a2713aSLionel Sambuc 89f4a2713aSLionel SambucProblems modules do not solve 90f4a2713aSLionel Sambuc----------------------------- 91f4a2713aSLionel SambucMany programming languages have a module or package system, and because of the variety of features provided by these languages it is important to define what modules do *not* do. In particular, all of the following are considered out-of-scope for modules: 92f4a2713aSLionel Sambuc 93f4a2713aSLionel Sambuc* **Rewrite the world's code**: It is not realistic to require applications or software libraries to make drastic or non-backward-compatible changes, nor is it feasible to completely eliminate headers. Modules must interoperate with existing software libraries and allow a gradual transition. 94f4a2713aSLionel Sambuc 95f4a2713aSLionel Sambuc* **Versioning**: Modules have no notion of version information. Programmers must still rely on the existing versioning mechanisms of the underlying language (if any exist) to version software libraries. 96f4a2713aSLionel Sambuc 97f4a2713aSLionel Sambuc* **Namespaces**: Unlike in some languages, modules do not imply any notion of namespaces. Thus, a struct declared in one module will still conflict with a struct of the same name declared in a different module, just as they would if declared in two different headers. This aspect is important for backward compatibility, because (for example) the mangled names of entities in software libraries must not change when introducing modules. 98f4a2713aSLionel Sambuc 99f4a2713aSLionel Sambuc* **Binary distribution of modules**: Headers (particularly C++ headers) expose the full complexity of the language. Maintaining a stable binary module format across architectures, compiler versions, and compiler vendors is technically infeasible. 100f4a2713aSLionel Sambuc 101f4a2713aSLionel SambucUsing Modules 102f4a2713aSLionel Sambuc============= 103*0a6a1f1dSLionel SambucTo enable modules, pass the command-line flag ``-fmodules``. This will make any modules-enabled software libraries available as modules as well as introducing any modules-specific syntax. Additional `command-line parameters`_ are described in a separate section later. 104f4a2713aSLionel Sambuc 105f4a2713aSLionel SambucObjective-C Import declaration 106f4a2713aSLionel Sambuc------------------------------ 107f4a2713aSLionel SambucObjective-C provides syntax for importing a module via an *@import declaration*, which imports the named module: 108f4a2713aSLionel Sambuc 109f4a2713aSLionel Sambuc.. parsed-literal:: 110f4a2713aSLionel Sambuc 111f4a2713aSLionel Sambuc @import std; 112f4a2713aSLionel Sambuc 113*0a6a1f1dSLionel SambucThe ``@import`` declaration above imports the entire contents of the ``std`` module (which would contain, e.g., the entire C or C++ standard library) and make its API available within the current translation unit. To import only part of a module, one may use dot syntax to specific a particular submodule, e.g., 114f4a2713aSLionel Sambuc 115f4a2713aSLionel Sambuc.. parsed-literal:: 116f4a2713aSLionel Sambuc 117f4a2713aSLionel Sambuc @import std.io; 118f4a2713aSLionel Sambuc 119f4a2713aSLionel SambucRedundant import declarations are ignored, and one is free to import modules at any point within the translation unit, so long as the import declaration is at global scope. 120f4a2713aSLionel Sambuc 121f4a2713aSLionel SambucAt present, there is no C or C++ syntax for import declarations. Clang 122f4a2713aSLionel Sambucwill track the modules proposal in the C++ committee. See the section 123f4a2713aSLionel Sambuc`Includes as imports`_ to see how modules get imported today. 124f4a2713aSLionel Sambuc 125f4a2713aSLionel SambucIncludes as imports 126f4a2713aSLionel Sambuc------------------- 127f4a2713aSLionel SambucThe primary user-level feature of modules is the import operation, which provides access to the API of software libraries. However, today's programs make extensive use of ``#include``, and it is unrealistic to assume that all of this code will change overnight. Instead, modules automatically translate ``#include`` directives into the corresponding module import. For example, the include directive 128f4a2713aSLionel Sambuc 129f4a2713aSLionel Sambuc.. code-block:: c 130f4a2713aSLionel Sambuc 131f4a2713aSLionel Sambuc #include <stdio.h> 132f4a2713aSLionel Sambuc 133f4a2713aSLionel Sambucwill be automatically mapped to an import of the module ``std.io``. Even with specific ``import`` syntax in the language, this particular feature is important for both adoption and backward compatibility: automatic translation of ``#include`` to ``import`` allows an application to get the benefits of modules (for all modules-enabled libraries) without any changes to the application itself. Thus, users can easily use modules with one compiler while falling back to the preprocessor-inclusion mechanism with other compilers. 134f4a2713aSLionel Sambuc 135f4a2713aSLionel Sambuc.. note:: 136f4a2713aSLionel Sambuc 137f4a2713aSLionel Sambuc The automatic mapping of ``#include`` to ``import`` also solves an implementation problem: importing a module with a definition of some entity (say, a ``struct Point``) and then parsing a header containing another definition of ``struct Point`` would cause a redefinition error, even if it is the same ``struct Point``. By mapping ``#include`` to ``import``, the compiler can guarantee that it always sees just the already-parsed definition from the module. 138f4a2713aSLionel Sambuc 139*0a6a1f1dSLionel SambucWhile building a module, ``#include_next`` is also supported, with one caveat. 140*0a6a1f1dSLionel SambucThe usual behavior of ``#include_next`` is to search for the specified filename 141*0a6a1f1dSLionel Sambucin the list of include paths, starting from the path *after* the one 142*0a6a1f1dSLionel Sambucin which the current file was found. 143*0a6a1f1dSLionel SambucBecause files listed in module maps are not found through include paths, a 144*0a6a1f1dSLionel Sambucdifferent strategy is used for ``#include_next`` directives in such files: the 145*0a6a1f1dSLionel Sambuclist of include paths is searched for the specified header name, to find the 146*0a6a1f1dSLionel Sambucfirst include path that would refer to the current file. ``#include_next`` is 147*0a6a1f1dSLionel Sambucinterpreted as if the current file had been found in that path. 148*0a6a1f1dSLionel SambucIf this search finds a file named by a module map, the ``#include_next`` 149*0a6a1f1dSLionel Sambucdirective is translated into an import, just like for a ``#include`` 150*0a6a1f1dSLionel Sambucdirective.`` 151*0a6a1f1dSLionel Sambuc 152f4a2713aSLionel SambucModule maps 153f4a2713aSLionel Sambuc----------- 154f4a2713aSLionel SambucThe crucial link between modules and headers is described by a *module map*, which describes how a collection of existing headers maps on to the (logical) structure of a module. For example, one could imagine a module ``std`` covering the C standard library. Each of the C standard library headers (``<stdio.h>``, ``<stdlib.h>``, ``<math.h>``, etc.) would contribute to the ``std`` module, by placing their respective APIs into the corresponding submodule (``std.io``, ``std.lib``, ``std.math``, etc.). Having a list of the headers that are part of the ``std`` module allows the compiler to build the ``std`` module as a standalone entity, and having the mapping from header names to (sub)modules allows the automatic translation of ``#include`` directives to module imports. 155f4a2713aSLionel Sambuc 156*0a6a1f1dSLionel SambucModule maps are specified as separate files (each named ``module.modulemap``) alongside the headers they describe, which allows them to be added to existing software libraries without having to change the library headers themselves (in most cases [#]_). The actual `Module map language`_ is described in a later section. 157f4a2713aSLionel Sambuc 158f4a2713aSLionel Sambuc.. note:: 159f4a2713aSLionel Sambuc 160f4a2713aSLionel Sambuc To actually see any benefits from modules, one first has to introduce module maps for the underlying C standard library and the libraries and headers on which it depends. The section `Modularizing a Platform`_ describes the steps one must take to write these module maps. 161f4a2713aSLionel Sambuc 162f4a2713aSLionel SambucOne can use module maps without modules to check the integrity of the use of header files. To do this, use the ``-fmodule-maps`` option instead of the ``-fmodules`` option. 163f4a2713aSLionel Sambuc 164f4a2713aSLionel SambucCompilation model 165f4a2713aSLionel Sambuc----------------- 166f4a2713aSLionel SambucThe binary representation of modules is automatically generated by the compiler on an as-needed basis. When a module is imported (e.g., by an ``#include`` of one of the module's headers), the compiler will spawn a second instance of itself [#]_, with a fresh preprocessing context [#]_, to parse just the headers in that module. The resulting Abstract Syntax Tree (AST) is then persisted into the binary representation of the module that is then loaded into translation unit where the module import was encountered. 167f4a2713aSLionel Sambuc 168f4a2713aSLionel SambucThe binary representation of modules is persisted in the *module cache*. Imports of a module will first query the module cache and, if a binary representation of the required module is already available, will load that representation directly. Thus, a module's headers will only be parsed once per language configuration, rather than once per translation unit that uses the module. 169f4a2713aSLionel Sambuc 170f4a2713aSLionel SambucModules maintain references to each of the headers that were part of the module build. If any of those headers changes, or if any of the modules on which a module depends change, then the module will be (automatically) recompiled. The process should never require any user intervention. 171f4a2713aSLionel Sambuc 172f4a2713aSLionel SambucCommand-line parameters 173f4a2713aSLionel Sambuc----------------------- 174f4a2713aSLionel Sambuc``-fmodules`` 175*0a6a1f1dSLionel Sambuc Enable the modules feature. 176f4a2713aSLionel Sambuc 177f4a2713aSLionel Sambuc``-fmodule-maps`` 178*0a6a1f1dSLionel Sambuc Enable interpretation of module maps. This option is implied by ``-fmodules``. 179f4a2713aSLionel Sambuc 180f4a2713aSLionel Sambuc``-fmodules-cache-path=<directory>`` 181f4a2713aSLionel Sambuc Specify the path to the modules cache. If not provided, Clang will select a system-appropriate default. 182f4a2713aSLionel Sambuc 183f4a2713aSLionel Sambuc``-fno-autolink`` 184f4a2713aSLionel Sambuc Disable automatic linking against the libraries associated with imported modules. 185f4a2713aSLionel Sambuc 186f4a2713aSLionel Sambuc``-fmodules-ignore-macro=macroname`` 187f4a2713aSLionel Sambuc Instruct modules to ignore the named macro when selecting an appropriate module variant. Use this for macros defined on the command line that don't affect how modules are built, to improve sharing of compiled module files. 188f4a2713aSLionel Sambuc 189f4a2713aSLionel Sambuc``-fmodules-prune-interval=seconds`` 190f4a2713aSLionel Sambuc Specify the minimum delay (in seconds) between attempts to prune the module cache. Module cache pruning attempts to clear out old, unused module files so that the module cache itself does not grow without bound. The default delay is large (604,800 seconds, or 7 days) because this is an expensive operation. Set this value to 0 to turn off pruning. 191f4a2713aSLionel Sambuc 192f4a2713aSLionel Sambuc``-fmodules-prune-after=seconds`` 193f4a2713aSLionel Sambuc Specify the minimum time (in seconds) for which a file in the module cache must be unused (according to access time) before module pruning will remove it. The default delay is large (2,678,400 seconds, or 31 days) to avoid excessive module rebuilding. 194f4a2713aSLionel Sambuc 195f4a2713aSLionel Sambuc``-module-file-info <module file name>`` 196f4a2713aSLionel Sambuc Debugging aid that prints information about a given module file (with a ``.pcm`` extension), including the language and preprocessor options that particular module variant was built with. 197f4a2713aSLionel Sambuc 198f4a2713aSLionel Sambuc``-fmodules-decluse`` 199f4a2713aSLionel Sambuc Enable checking of module ``use`` declarations. 200f4a2713aSLionel Sambuc 201f4a2713aSLionel Sambuc``-fmodule-name=module-id`` 202f4a2713aSLionel Sambuc Consider a source file as a part of the given module. 203f4a2713aSLionel Sambuc 204f4a2713aSLionel Sambuc``-fmodule-map-file=<file>`` 205f4a2713aSLionel Sambuc Load the given module map file if a header from its directory or one of its subdirectories is loaded. 206f4a2713aSLionel Sambuc 207*0a6a1f1dSLionel Sambuc``-fmodules-search-all`` 208*0a6a1f1dSLionel Sambuc If a symbol is not found, search modules referenced in the current module maps but not imported for symbols, so the error message can reference the module by name. Note that if the global module index has not been built before, this might take some time as it needs to build all the modules. Note that this option doesn't apply in module builds, to avoid the recursion. 209*0a6a1f1dSLionel Sambuc 210*0a6a1f1dSLionel Sambuc``-fno-modules-implicit-maps`` 211*0a6a1f1dSLionel Sambuc Suppresses the implicit search for files called ``module.modulemap`` and similar. Instead, module files need to be explicitly specified via ``-fmodule-map-file`` or transitively used. 212*0a6a1f1dSLionel Sambuc 213*0a6a1f1dSLionel SambucModule Semantics 214*0a6a1f1dSLionel Sambuc================ 215*0a6a1f1dSLionel Sambuc 216*0a6a1f1dSLionel SambucModules are modeled as if each submodule were a separate translation unit, and a module import makes names from the other translation unit visible. Each submodule starts with a new preprocessor state and an empty translation unit. 217*0a6a1f1dSLionel Sambuc 218*0a6a1f1dSLionel Sambuc.. note:: 219*0a6a1f1dSLionel Sambuc 220*0a6a1f1dSLionel Sambuc This behavior is currently only approximated when building a module with submodules. Entities within a submodule that has already been built are visible when building later submodules in that module. This can lead to fragile modules that depend on the build order used for the submodules of the module, and should not be relied upon. This behavior is subject to change. 221*0a6a1f1dSLionel Sambuc 222*0a6a1f1dSLionel SambucAs an example, in C, this implies that if two structs are defined in different submodules with the same name, those two types are distinct types (but may be *compatible* types if their definitions match. In C++, two structs defined with the same name in different submodules are the *same* type, and must be equivalent under C++'s One Definition Rule. 223*0a6a1f1dSLionel Sambuc 224*0a6a1f1dSLionel Sambuc.. note:: 225*0a6a1f1dSLionel Sambuc 226*0a6a1f1dSLionel Sambuc Clang currently only performs minimal checking for violations of the One Definition Rule. 227*0a6a1f1dSLionel Sambuc 228*0a6a1f1dSLionel SambucIf any submodule of a module is imported into any part of a program, the entire top-level module is considered to be part of the program. As a consequence of this, Clang may diagnose conflicts between an entity declared in an unimported submodule and an entity declared in the current translation unit, and Clang may inline or devirtualize based on knowledge from unimported submodules. 229*0a6a1f1dSLionel Sambuc 230*0a6a1f1dSLionel SambucMacros 231*0a6a1f1dSLionel Sambuc------ 232*0a6a1f1dSLionel Sambuc 233*0a6a1f1dSLionel SambucThe C and C++ preprocessor assumes that the input text is a single linear buffer, but with modules this is not the case. It is possible to import two modules that have conflicting definitions for a macro (or where one ``#define``\s a macro and the other ``#undef``\ines it). The rules for handling macro definitions in the presence of modules are as follows: 234*0a6a1f1dSLionel Sambuc 235*0a6a1f1dSLionel Sambuc* Each definition and undefinition of a macro is considered to be a distinct entity. 236*0a6a1f1dSLionel Sambuc* Such entities are *visible* if they are from the current submodule or translation unit, or if they were exported from a submodule that has been imported. 237*0a6a1f1dSLionel Sambuc* A ``#define X`` or ``#undef X`` directive *overrides* all definitions of ``X`` that are visible at the point of the directive. 238*0a6a1f1dSLionel Sambuc* A ``#define`` or ``#undef`` directive is *active* if it is visible and no visible directive overrides it. 239*0a6a1f1dSLionel Sambuc* A set of macro directives is *consistent* if it consists of only ``#undef`` directives, or if all ``#define`` directives in the set define the macro name to the same sequence of tokens (following the usual rules for macro redefinitions). 240*0a6a1f1dSLionel Sambuc* If a macro name is used and the set of active directives is not consistent, the program is ill-formed. Otherwise, the (unique) meaning of the macro name is used. 241*0a6a1f1dSLionel Sambuc 242*0a6a1f1dSLionel SambucFor example, suppose: 243*0a6a1f1dSLionel Sambuc 244*0a6a1f1dSLionel Sambuc* ``<stdio.h>`` defines a macro ``getc`` (and exports its ``#define``) 245*0a6a1f1dSLionel Sambuc* ``<cstdio>`` imports the ``<stdio.h>`` module and undefines the macro (and exports its ``#undef``) 246*0a6a1f1dSLionel Sambuc 247*0a6a1f1dSLionel SambucThe ``#undef`` overrides the ``#define``, and a source file that imports both modules *in any order* will not see ``getc`` defined as a macro. 248*0a6a1f1dSLionel Sambuc 249f4a2713aSLionel SambucModule Map Language 250f4a2713aSLionel Sambuc=================== 251f4a2713aSLionel Sambuc 252*0a6a1f1dSLionel Sambuc.. warning:: 253*0a6a1f1dSLionel Sambuc 254*0a6a1f1dSLionel Sambuc The module map language is not currently guaranteed to be stable between major revisions of Clang. 255*0a6a1f1dSLionel Sambuc 256f4a2713aSLionel SambucThe module map language describes the mapping from header files to the 257f4a2713aSLionel Sambuclogical structure of modules. To enable support for using a library as 258*0a6a1f1dSLionel Sambuca module, one must write a ``module.modulemap`` file for that library. The 259*0a6a1f1dSLionel Sambuc``module.modulemap`` file is placed alongside the header files themselves, 260f4a2713aSLionel Sambucand is written in the module map language described below. 261f4a2713aSLionel Sambuc 262*0a6a1f1dSLionel Sambuc.. note:: 263*0a6a1f1dSLionel Sambuc For compatibility with previous releases, if a module map file named 264*0a6a1f1dSLionel Sambuc ``module.modulemap`` is not found, Clang will also search for a file named 265*0a6a1f1dSLionel Sambuc ``module.map``. This behavior is deprecated and we plan to eventually 266*0a6a1f1dSLionel Sambuc remove it. 267*0a6a1f1dSLionel Sambuc 268f4a2713aSLionel SambucAs an example, the module map file for the C standard library might look a bit like this: 269f4a2713aSLionel Sambuc 270f4a2713aSLionel Sambuc.. parsed-literal:: 271f4a2713aSLionel Sambuc 272*0a6a1f1dSLionel Sambuc module std [system] [extern_c] { 273*0a6a1f1dSLionel Sambuc module assert { 274*0a6a1f1dSLionel Sambuc textual header "assert.h" 275*0a6a1f1dSLionel Sambuc header "bits/assert-decls.h" 276*0a6a1f1dSLionel Sambuc export * 277*0a6a1f1dSLionel Sambuc } 278*0a6a1f1dSLionel Sambuc 279f4a2713aSLionel Sambuc module complex { 280f4a2713aSLionel Sambuc header "complex.h" 281f4a2713aSLionel Sambuc export * 282f4a2713aSLionel Sambuc } 283f4a2713aSLionel Sambuc 284f4a2713aSLionel Sambuc module ctype { 285f4a2713aSLionel Sambuc header "ctype.h" 286f4a2713aSLionel Sambuc export * 287f4a2713aSLionel Sambuc } 288f4a2713aSLionel Sambuc 289f4a2713aSLionel Sambuc module errno { 290f4a2713aSLionel Sambuc header "errno.h" 291f4a2713aSLionel Sambuc header "sys/errno.h" 292f4a2713aSLionel Sambuc export * 293f4a2713aSLionel Sambuc } 294f4a2713aSLionel Sambuc 295f4a2713aSLionel Sambuc module fenv { 296f4a2713aSLionel Sambuc header "fenv.h" 297f4a2713aSLionel Sambuc export * 298f4a2713aSLionel Sambuc } 299f4a2713aSLionel Sambuc 300f4a2713aSLionel Sambuc // ...more headers follow... 301f4a2713aSLionel Sambuc } 302f4a2713aSLionel Sambuc 303f4a2713aSLionel SambucHere, the top-level module ``std`` encompasses the whole C standard library. It has a number of submodules containing different parts of the standard library: ``complex`` for complex numbers, ``ctype`` for character types, etc. Each submodule lists one of more headers that provide the contents for that submodule. Finally, the ``export *`` command specifies that anything included by that submodule will be automatically re-exported. 304f4a2713aSLionel Sambuc 305f4a2713aSLionel SambucLexical structure 306f4a2713aSLionel Sambuc----------------- 307f4a2713aSLionel SambucModule map files use a simplified form of the C99 lexer, with the same rules for identifiers, tokens, string literals, ``/* */`` and ``//`` comments. The module map language has the following reserved words; all other C identifiers are valid identifiers. 308f4a2713aSLionel Sambuc 309f4a2713aSLionel Sambuc.. parsed-literal:: 310f4a2713aSLionel Sambuc 311*0a6a1f1dSLionel Sambuc ``config_macros`` ``export`` ``private`` 312f4a2713aSLionel Sambuc ``conflict`` ``framework`` ``requires`` 313*0a6a1f1dSLionel Sambuc ``exclude`` ``header`` ``textual`` 314f4a2713aSLionel Sambuc ``explicit`` ``link`` ``umbrella`` 315*0a6a1f1dSLionel Sambuc ``extern`` ``module`` ``use`` 316f4a2713aSLionel Sambuc 317f4a2713aSLionel SambucModule map file 318f4a2713aSLionel Sambuc--------------- 319f4a2713aSLionel SambucA module map file consists of a series of module declarations: 320f4a2713aSLionel Sambuc 321f4a2713aSLionel Sambuc.. parsed-literal:: 322f4a2713aSLionel Sambuc 323f4a2713aSLionel Sambuc *module-map-file*: 324f4a2713aSLionel Sambuc *module-declaration** 325f4a2713aSLionel Sambuc 326f4a2713aSLionel SambucWithin a module map file, modules are referred to by a *module-id*, which uses periods to separate each part of a module's name: 327f4a2713aSLionel Sambuc 328f4a2713aSLionel Sambuc.. parsed-literal:: 329f4a2713aSLionel Sambuc 330f4a2713aSLionel Sambuc *module-id*: 331f4a2713aSLionel Sambuc *identifier* ('.' *identifier*)* 332f4a2713aSLionel Sambuc 333f4a2713aSLionel SambucModule declaration 334f4a2713aSLionel Sambuc------------------ 335f4a2713aSLionel SambucA module declaration describes a module, including the headers that contribute to that module, its submodules, and other aspects of the module. 336f4a2713aSLionel Sambuc 337f4a2713aSLionel Sambuc.. parsed-literal:: 338f4a2713aSLionel Sambuc 339f4a2713aSLionel Sambuc *module-declaration*: 340f4a2713aSLionel Sambuc ``explicit``:sub:`opt` ``framework``:sub:`opt` ``module`` *module-id* *attributes*:sub:`opt` '{' *module-member** '}' 341f4a2713aSLionel Sambuc ``extern`` ``module`` *module-id* *string-literal* 342f4a2713aSLionel Sambuc 343f4a2713aSLionel SambucThe *module-id* should consist of only a single *identifier*, which provides the name of the module being defined. Each module shall have a single definition. 344f4a2713aSLionel Sambuc 345f4a2713aSLionel SambucThe ``explicit`` qualifier can only be applied to a submodule, i.e., a module that is nested within another module. The contents of explicit submodules are only made available when the submodule itself was explicitly named in an import declaration or was re-exported from an imported module. 346f4a2713aSLionel Sambuc 347f4a2713aSLionel SambucThe ``framework`` qualifier specifies that this module corresponds to a Darwin-style framework. A Darwin-style framework (used primarily on Mac OS X and iOS) is contained entirely in directory ``Name.framework``, where ``Name`` is the name of the framework (and, therefore, the name of the module). That directory has the following layout: 348f4a2713aSLionel Sambuc 349f4a2713aSLionel Sambuc.. parsed-literal:: 350f4a2713aSLionel Sambuc 351f4a2713aSLionel Sambuc Name.framework/ 352*0a6a1f1dSLionel Sambuc Modules/module.modulemap Module map for the framework 353f4a2713aSLionel Sambuc Headers/ Subdirectory containing framework headers 354f4a2713aSLionel Sambuc Frameworks/ Subdirectory containing embedded frameworks 355f4a2713aSLionel Sambuc Resources/ Subdirectory containing additional resources 356f4a2713aSLionel Sambuc Name Symbolic link to the shared library for the framework 357f4a2713aSLionel Sambuc 358*0a6a1f1dSLionel SambucThe ``system`` attribute specifies that the module is a system module. When a system module is rebuilt, all of the module's headers will be considered system headers, which suppresses warnings. This is equivalent to placing ``#pragma GCC system_header`` in each of the module's headers. The form of attributes is described in the section Attributes_, below. 359*0a6a1f1dSLionel Sambuc 360*0a6a1f1dSLionel SambucThe ``extern_c`` attribute specifies that the module contains C code that can be used from within C++. When such a module is built for use in C++ code, all of the module's headers will be treated as if they were contained within an implicit ``extern "C"`` block. An import for a module with this attribute can appear within an ``extern "C"`` block. No other restrictions are lifted, however: the module currently cannot be imported within an ``extern "C"`` block in a namespace. 361f4a2713aSLionel Sambuc 362f4a2713aSLionel SambucModules can have a number of different kinds of members, each of which is described below: 363f4a2713aSLionel Sambuc 364f4a2713aSLionel Sambuc.. parsed-literal:: 365f4a2713aSLionel Sambuc 366f4a2713aSLionel Sambuc *module-member*: 367f4a2713aSLionel Sambuc *requires-declaration* 368f4a2713aSLionel Sambuc *header-declaration* 369f4a2713aSLionel Sambuc *umbrella-dir-declaration* 370f4a2713aSLionel Sambuc *submodule-declaration* 371f4a2713aSLionel Sambuc *export-declaration* 372f4a2713aSLionel Sambuc *use-declaration* 373f4a2713aSLionel Sambuc *link-declaration* 374f4a2713aSLionel Sambuc *config-macros-declaration* 375f4a2713aSLionel Sambuc *conflict-declaration* 376f4a2713aSLionel Sambuc 377f4a2713aSLionel SambucAn extern module references a module defined by the *module-id* in a file given by the *string-literal*. The file can be referenced either by an absolute path or by a path relative to the current map file. 378f4a2713aSLionel Sambuc 379f4a2713aSLionel SambucRequires declaration 380f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~~~ 381f4a2713aSLionel SambucA *requires-declaration* specifies the requirements that an importing translation unit must satisfy to use the module. 382f4a2713aSLionel Sambuc 383f4a2713aSLionel Sambuc.. parsed-literal:: 384f4a2713aSLionel Sambuc 385f4a2713aSLionel Sambuc *requires-declaration*: 386f4a2713aSLionel Sambuc ``requires`` *feature-list* 387f4a2713aSLionel Sambuc 388f4a2713aSLionel Sambuc *feature-list*: 389f4a2713aSLionel Sambuc *feature* (',' *feature*)* 390f4a2713aSLionel Sambuc 391f4a2713aSLionel Sambuc *feature*: 392f4a2713aSLionel Sambuc ``!``:sub:`opt` *identifier* 393f4a2713aSLionel Sambuc 394f4a2713aSLionel SambucThe requirements clause allows specific modules or submodules to specify that they are only accessible with certain language dialects or on certain platforms. The feature list is a set of identifiers, defined below. If any of the features is not available in a given translation unit, that translation unit shall not import the module. The optional ``!`` indicates that a feature is incompatible with the module. 395f4a2713aSLionel Sambuc 396f4a2713aSLionel SambucThe following features are defined: 397f4a2713aSLionel Sambuc 398f4a2713aSLionel Sambucaltivec 399f4a2713aSLionel Sambuc The target supports AltiVec. 400f4a2713aSLionel Sambuc 401f4a2713aSLionel Sambucblocks 402f4a2713aSLionel Sambuc The "blocks" language feature is available. 403f4a2713aSLionel Sambuc 404f4a2713aSLionel Sambuccplusplus 405f4a2713aSLionel Sambuc C++ support is available. 406f4a2713aSLionel Sambuc 407f4a2713aSLionel Sambuccplusplus11 408f4a2713aSLionel Sambuc C++11 support is available. 409f4a2713aSLionel Sambuc 410f4a2713aSLionel Sambucobjc 411f4a2713aSLionel Sambuc Objective-C support is available. 412f4a2713aSLionel Sambuc 413f4a2713aSLionel Sambucobjc_arc 414f4a2713aSLionel Sambuc Objective-C Automatic Reference Counting (ARC) is available 415f4a2713aSLionel Sambuc 416f4a2713aSLionel Sambucopencl 417f4a2713aSLionel Sambuc OpenCL is available 418f4a2713aSLionel Sambuc 419f4a2713aSLionel Sambuctls 420f4a2713aSLionel Sambuc Thread local storage is available. 421f4a2713aSLionel Sambuc 422f4a2713aSLionel Sambuc*target feature* 423f4a2713aSLionel Sambuc A specific target feature (e.g., ``sse4``, ``avx``, ``neon``) is available. 424f4a2713aSLionel Sambuc 425f4a2713aSLionel Sambuc 426f4a2713aSLionel Sambuc**Example**: The ``std`` module can be extended to also include C++ and C++11 headers using a *requires-declaration*: 427f4a2713aSLionel Sambuc 428f4a2713aSLionel Sambuc.. parsed-literal:: 429f4a2713aSLionel Sambuc 430f4a2713aSLionel Sambuc module std { 431f4a2713aSLionel Sambuc // C standard library... 432f4a2713aSLionel Sambuc 433f4a2713aSLionel Sambuc module vector { 434f4a2713aSLionel Sambuc requires cplusplus 435f4a2713aSLionel Sambuc header "vector" 436f4a2713aSLionel Sambuc } 437f4a2713aSLionel Sambuc 438f4a2713aSLionel Sambuc module type_traits { 439f4a2713aSLionel Sambuc requires cplusplus11 440f4a2713aSLionel Sambuc header "type_traits" 441f4a2713aSLionel Sambuc } 442f4a2713aSLionel Sambuc } 443f4a2713aSLionel Sambuc 444f4a2713aSLionel SambucHeader declaration 445f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~ 446f4a2713aSLionel SambucA header declaration specifies that a particular header is associated with the enclosing module. 447f4a2713aSLionel Sambuc 448f4a2713aSLionel Sambuc.. parsed-literal:: 449f4a2713aSLionel Sambuc 450f4a2713aSLionel Sambuc *header-declaration*: 451*0a6a1f1dSLionel Sambuc ``private``:sub:`opt` ``textual``:sub:`opt` ``header`` *string-literal* 452*0a6a1f1dSLionel Sambuc ``umbrella`` ``header`` *string-literal* 453f4a2713aSLionel Sambuc ``exclude`` ``header`` *string-literal* 454f4a2713aSLionel Sambuc 455*0a6a1f1dSLionel SambucA header declaration that does not contain ``exclude`` nor ``textual`` specifies a header that contributes to the enclosing module. Specifically, when the module is built, the named header will be parsed and its declarations will be (logically) placed into the enclosing submodule. 456f4a2713aSLionel Sambuc 457f4a2713aSLionel SambucA header with the ``umbrella`` specifier is called an umbrella header. An umbrella header includes all of the headers within its directory (and any subdirectories), and is typically used (in the ``#include`` world) to easily access the full API provided by a particular library. With modules, an umbrella header is a convenient shortcut that eliminates the need to write out ``header`` declarations for every library header. A given directory can only contain a single umbrella header. 458f4a2713aSLionel Sambuc 459f4a2713aSLionel Sambuc.. note:: 460f4a2713aSLionel Sambuc Any headers not included by the umbrella header should have 461f4a2713aSLionel Sambuc explicit ``header`` declarations. Use the 462f4a2713aSLionel Sambuc ``-Wincomplete-umbrella`` warning option to ask Clang to complain 463f4a2713aSLionel Sambuc about headers not covered by the umbrella header or the module map. 464f4a2713aSLionel Sambuc 465f4a2713aSLionel SambucA header with the ``private`` specifier may not be included from outside the module itself. 466f4a2713aSLionel Sambuc 467*0a6a1f1dSLionel SambucA header with the ``textual`` specifier will not be included when the module is built, and will be textually included if it is named by a ``#include`` directive. However, it is considered to be part of the module for the purpose of checking *use-declaration*\s. 468f4a2713aSLionel Sambuc 469*0a6a1f1dSLionel SambucA header with the ``exclude`` specifier is excluded from the module. It will not be included when the module is built, nor will it be considered to be part of the module, even if an ``umbrella`` header or directory would otherwise make it part of the module. 470*0a6a1f1dSLionel Sambuc 471*0a6a1f1dSLionel Sambuc**Example**: The C header ``assert.h`` is an excellent candidate for a textual header, because it is meant to be included multiple times (possibly with different ``NDEBUG`` settings). However, declarations within it should typically be split into a separate modular header. 472f4a2713aSLionel Sambuc 473f4a2713aSLionel Sambuc.. parsed-literal:: 474f4a2713aSLionel Sambuc 475f4a2713aSLionel Sambuc module std [system] { 476*0a6a1f1dSLionel Sambuc textual header "assert.h" 477f4a2713aSLionel Sambuc } 478f4a2713aSLionel Sambuc 479f4a2713aSLionel SambucA given header shall not be referenced by more than one *header-declaration*. 480f4a2713aSLionel Sambuc 481f4a2713aSLionel SambucUmbrella directory declaration 482f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 483f4a2713aSLionel SambucAn umbrella directory declaration specifies that all of the headers in the specified directory should be included within the module. 484f4a2713aSLionel Sambuc 485f4a2713aSLionel Sambuc.. parsed-literal:: 486f4a2713aSLionel Sambuc 487f4a2713aSLionel Sambuc *umbrella-dir-declaration*: 488f4a2713aSLionel Sambuc ``umbrella`` *string-literal* 489f4a2713aSLionel Sambuc 490f4a2713aSLionel SambucThe *string-literal* refers to a directory. When the module is built, all of the header files in that directory (and its subdirectories) are included in the module. 491f4a2713aSLionel Sambuc 492f4a2713aSLionel SambucAn *umbrella-dir-declaration* shall not refer to the same directory as the location of an umbrella *header-declaration*. In other words, only a single kind of umbrella can be specified for a given directory. 493f4a2713aSLionel Sambuc 494f4a2713aSLionel Sambuc.. note:: 495f4a2713aSLionel Sambuc 496f4a2713aSLionel Sambuc Umbrella directories are useful for libraries that have a large number of headers but do not have an umbrella header. 497f4a2713aSLionel Sambuc 498f4a2713aSLionel Sambuc 499f4a2713aSLionel SambucSubmodule declaration 500f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~~~~ 501f4a2713aSLionel SambucSubmodule declarations describe modules that are nested within their enclosing module. 502f4a2713aSLionel Sambuc 503f4a2713aSLionel Sambuc.. parsed-literal:: 504f4a2713aSLionel Sambuc 505f4a2713aSLionel Sambuc *submodule-declaration*: 506f4a2713aSLionel Sambuc *module-declaration* 507f4a2713aSLionel Sambuc *inferred-submodule-declaration* 508f4a2713aSLionel Sambuc 509f4a2713aSLionel SambucA *submodule-declaration* that is a *module-declaration* is a nested module. If the *module-declaration* has a ``framework`` specifier, the enclosing module shall have a ``framework`` specifier; the submodule's contents shall be contained within the subdirectory ``Frameworks/SubName.framework``, where ``SubName`` is the name of the submodule. 510f4a2713aSLionel Sambuc 511f4a2713aSLionel SambucA *submodule-declaration* that is an *inferred-submodule-declaration* describes a set of submodules that correspond to any headers that are part of the module but are not explicitly described by a *header-declaration*. 512f4a2713aSLionel Sambuc 513f4a2713aSLionel Sambuc.. parsed-literal:: 514f4a2713aSLionel Sambuc 515f4a2713aSLionel Sambuc *inferred-submodule-declaration*: 516f4a2713aSLionel Sambuc ``explicit``:sub:`opt` ``framework``:sub:`opt` ``module`` '*' *attributes*:sub:`opt` '{' *inferred-submodule-member** '}' 517f4a2713aSLionel Sambuc 518f4a2713aSLionel Sambuc *inferred-submodule-member*: 519f4a2713aSLionel Sambuc ``export`` '*' 520f4a2713aSLionel Sambuc 521f4a2713aSLionel SambucA module containing an *inferred-submodule-declaration* shall have either an umbrella header or an umbrella directory. The headers to which the *inferred-submodule-declaration* applies are exactly those headers included by the umbrella header (transitively) or included in the module because they reside within the umbrella directory (or its subdirectories). 522f4a2713aSLionel Sambuc 523f4a2713aSLionel SambucFor each header included by the umbrella header or in the umbrella directory that is not named by a *header-declaration*, a module declaration is implicitly generated from the *inferred-submodule-declaration*. The module will: 524f4a2713aSLionel Sambuc 525f4a2713aSLionel Sambuc* Have the same name as the header (without the file extension) 526f4a2713aSLionel Sambuc* Have the ``explicit`` specifier, if the *inferred-submodule-declaration* has the ``explicit`` specifier 527f4a2713aSLionel Sambuc* Have the ``framework`` specifier, if the 528f4a2713aSLionel Sambuc *inferred-submodule-declaration* has the ``framework`` specifier 529f4a2713aSLionel Sambuc* Have the attributes specified by the \ *inferred-submodule-declaration* 530f4a2713aSLionel Sambuc* Contain a single *header-declaration* naming that header 531f4a2713aSLionel Sambuc* Contain a single *export-declaration* ``export *``, if the \ *inferred-submodule-declaration* contains the \ *inferred-submodule-member* ``export *`` 532f4a2713aSLionel Sambuc 533f4a2713aSLionel Sambuc**Example**: If the subdirectory "MyLib" contains the headers ``A.h`` and ``B.h``, then the following module map: 534f4a2713aSLionel Sambuc 535f4a2713aSLionel Sambuc.. parsed-literal:: 536f4a2713aSLionel Sambuc 537f4a2713aSLionel Sambuc module MyLib { 538f4a2713aSLionel Sambuc umbrella "MyLib" 539f4a2713aSLionel Sambuc explicit module * { 540f4a2713aSLionel Sambuc export * 541f4a2713aSLionel Sambuc } 542f4a2713aSLionel Sambuc } 543f4a2713aSLionel Sambuc 544f4a2713aSLionel Sambucis equivalent to the (more verbose) module map: 545f4a2713aSLionel Sambuc 546f4a2713aSLionel Sambuc.. parsed-literal:: 547f4a2713aSLionel Sambuc 548f4a2713aSLionel Sambuc module MyLib { 549f4a2713aSLionel Sambuc explicit module A { 550f4a2713aSLionel Sambuc header "A.h" 551f4a2713aSLionel Sambuc export * 552f4a2713aSLionel Sambuc } 553f4a2713aSLionel Sambuc 554f4a2713aSLionel Sambuc explicit module B { 555f4a2713aSLionel Sambuc header "B.h" 556f4a2713aSLionel Sambuc export * 557f4a2713aSLionel Sambuc } 558f4a2713aSLionel Sambuc } 559f4a2713aSLionel Sambuc 560f4a2713aSLionel SambucExport declaration 561f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~ 562f4a2713aSLionel SambucAn *export-declaration* specifies which imported modules will automatically be re-exported as part of a given module's API. 563f4a2713aSLionel Sambuc 564f4a2713aSLionel Sambuc.. parsed-literal:: 565f4a2713aSLionel Sambuc 566f4a2713aSLionel Sambuc *export-declaration*: 567f4a2713aSLionel Sambuc ``export`` *wildcard-module-id* 568f4a2713aSLionel Sambuc 569f4a2713aSLionel Sambuc *wildcard-module-id*: 570f4a2713aSLionel Sambuc *identifier* 571f4a2713aSLionel Sambuc '*' 572f4a2713aSLionel Sambuc *identifier* '.' *wildcard-module-id* 573f4a2713aSLionel Sambuc 574f4a2713aSLionel SambucThe *export-declaration* names a module or a set of modules that will be re-exported to any translation unit that imports the enclosing module. Each imported module that matches the *wildcard-module-id* up to, but not including, the first ``*`` will be re-exported. 575f4a2713aSLionel Sambuc 576f4a2713aSLionel Sambuc**Example**:: In the following example, importing ``MyLib.Derived`` also provides the API for ``MyLib.Base``: 577f4a2713aSLionel Sambuc 578f4a2713aSLionel Sambuc.. parsed-literal:: 579f4a2713aSLionel Sambuc 580f4a2713aSLionel Sambuc module MyLib { 581f4a2713aSLionel Sambuc module Base { 582f4a2713aSLionel Sambuc header "Base.h" 583f4a2713aSLionel Sambuc } 584f4a2713aSLionel Sambuc 585f4a2713aSLionel Sambuc module Derived { 586f4a2713aSLionel Sambuc header "Derived.h" 587f4a2713aSLionel Sambuc export Base 588f4a2713aSLionel Sambuc } 589f4a2713aSLionel Sambuc } 590f4a2713aSLionel Sambuc 591f4a2713aSLionel SambucNote that, if ``Derived.h`` includes ``Base.h``, one can simply use a wildcard export to re-export everything ``Derived.h`` includes: 592f4a2713aSLionel Sambuc 593f4a2713aSLionel Sambuc.. parsed-literal:: 594f4a2713aSLionel Sambuc 595f4a2713aSLionel Sambuc module MyLib { 596f4a2713aSLionel Sambuc module Base { 597f4a2713aSLionel Sambuc header "Base.h" 598f4a2713aSLionel Sambuc } 599f4a2713aSLionel Sambuc 600f4a2713aSLionel Sambuc module Derived { 601f4a2713aSLionel Sambuc header "Derived.h" 602f4a2713aSLionel Sambuc export * 603f4a2713aSLionel Sambuc } 604f4a2713aSLionel Sambuc } 605f4a2713aSLionel Sambuc 606f4a2713aSLionel Sambuc.. note:: 607f4a2713aSLionel Sambuc 608f4a2713aSLionel Sambuc The wildcard export syntax ``export *`` re-exports all of the 609f4a2713aSLionel Sambuc modules that were imported in the actual header file. Because 610f4a2713aSLionel Sambuc ``#include`` directives are automatically mapped to module imports, 611f4a2713aSLionel Sambuc ``export *`` provides the same transitive-inclusion behavior 612f4a2713aSLionel Sambuc provided by the C preprocessor, e.g., importing a given module 613f4a2713aSLionel Sambuc implicitly imports all of the modules on which it depends. 614f4a2713aSLionel Sambuc Therefore, liberal use of ``export *`` provides excellent backward 615f4a2713aSLionel Sambuc compatibility for programs that rely on transitive inclusion (i.e., 616f4a2713aSLionel Sambuc all of them). 617f4a2713aSLionel Sambuc 618f4a2713aSLionel SambucUse declaration 619f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~ 620f4a2713aSLionel SambucA *use-declaration* specifies one of the other modules that the module is allowed to use. An import or include not matching one of these is rejected when the option *-fmodules-decluse*. 621f4a2713aSLionel Sambuc 622f4a2713aSLionel Sambuc.. parsed-literal:: 623f4a2713aSLionel Sambuc 624f4a2713aSLionel Sambuc *use-declaration*: 625f4a2713aSLionel Sambuc ``use`` *module-id* 626f4a2713aSLionel Sambuc 627f4a2713aSLionel Sambuc**Example**:: In the following example, use of A from C is not declared, so will trigger a warning. 628f4a2713aSLionel Sambuc 629f4a2713aSLionel Sambuc.. parsed-literal:: 630f4a2713aSLionel Sambuc 631f4a2713aSLionel Sambuc module A { 632f4a2713aSLionel Sambuc header "a.h" 633f4a2713aSLionel Sambuc } 634f4a2713aSLionel Sambuc 635f4a2713aSLionel Sambuc module B { 636f4a2713aSLionel Sambuc header "b.h" 637f4a2713aSLionel Sambuc } 638f4a2713aSLionel Sambuc 639f4a2713aSLionel Sambuc module C { 640f4a2713aSLionel Sambuc header "c.h" 641f4a2713aSLionel Sambuc use B 642f4a2713aSLionel Sambuc } 643f4a2713aSLionel Sambuc 644*0a6a1f1dSLionel SambucWhen compiling a source file that implements a module, use the option ``-fmodule-name=module-id`` to indicate that the source file is logically part of that module. 645f4a2713aSLionel Sambuc 646f4a2713aSLionel SambucThe compiler at present only applies restrictions to the module directly being built. 647f4a2713aSLionel Sambuc 648f4a2713aSLionel SambucLink declaration 649f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~ 650f4a2713aSLionel SambucA *link-declaration* specifies a library or framework against which a program should be linked if the enclosing module is imported in any translation unit in that program. 651f4a2713aSLionel Sambuc 652f4a2713aSLionel Sambuc.. parsed-literal:: 653f4a2713aSLionel Sambuc 654f4a2713aSLionel Sambuc *link-declaration*: 655f4a2713aSLionel Sambuc ``link`` ``framework``:sub:`opt` *string-literal* 656f4a2713aSLionel Sambuc 657f4a2713aSLionel SambucThe *string-literal* specifies the name of the library or framework against which the program should be linked. For example, specifying "clangBasic" would instruct the linker to link with ``-lclangBasic`` for a Unix-style linker. 658f4a2713aSLionel Sambuc 659f4a2713aSLionel SambucA *link-declaration* with the ``framework`` specifies that the linker should link against the named framework, e.g., with ``-framework MyFramework``. 660f4a2713aSLionel Sambuc 661f4a2713aSLionel Sambuc.. note:: 662f4a2713aSLionel Sambuc 663f4a2713aSLionel Sambuc Automatic linking with the ``link`` directive is not yet widely 664f4a2713aSLionel Sambuc implemented, because it requires support from both the object file 665f4a2713aSLionel Sambuc format and the linker. The notion is similar to Microsoft Visual 666f4a2713aSLionel Sambuc Studio's ``#pragma comment(lib...)``. 667f4a2713aSLionel Sambuc 668f4a2713aSLionel SambucConfiguration macros declaration 669f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 670f4a2713aSLionel SambucThe *config-macros-declaration* specifies the set of configuration macros that have an effect on the the API of the enclosing module. 671f4a2713aSLionel Sambuc 672f4a2713aSLionel Sambuc.. parsed-literal:: 673f4a2713aSLionel Sambuc 674f4a2713aSLionel Sambuc *config-macros-declaration*: 675f4a2713aSLionel Sambuc ``config_macros`` *attributes*:sub:`opt` *config-macro-list*:sub:`opt` 676f4a2713aSLionel Sambuc 677f4a2713aSLionel Sambuc *config-macro-list*: 678f4a2713aSLionel Sambuc *identifier* (',' *identifier*)* 679f4a2713aSLionel Sambuc 680f4a2713aSLionel SambucEach *identifier* in the *config-macro-list* specifies the name of a macro. The compiler is required to maintain different variants of the given module for differing definitions of any of the named macros. 681f4a2713aSLionel Sambuc 682f4a2713aSLionel SambucA *config-macros-declaration* shall only be present on a top-level module, i.e., a module that is not nested within an enclosing module. 683f4a2713aSLionel Sambuc 684f4a2713aSLionel SambucThe ``exhaustive`` attribute specifies that the list of macros in the *config-macros-declaration* is exhaustive, meaning that no other macro definition is intended to have an effect on the API of that module. 685f4a2713aSLionel Sambuc 686f4a2713aSLionel Sambuc.. note:: 687f4a2713aSLionel Sambuc 688f4a2713aSLionel Sambuc The ``exhaustive`` attribute implies that any macro definitions 689f4a2713aSLionel Sambuc for macros not listed as configuration macros should be ignored 690f4a2713aSLionel Sambuc completely when building the module. As an optimization, the 691f4a2713aSLionel Sambuc compiler could reduce the number of unique module variants by not 692f4a2713aSLionel Sambuc considering these non-configuration macros. This optimization is not 693f4a2713aSLionel Sambuc yet implemented in Clang. 694f4a2713aSLionel Sambuc 695f4a2713aSLionel SambucA translation unit shall not import the same module under different definitions of the configuration macros. 696f4a2713aSLionel Sambuc 697f4a2713aSLionel Sambuc.. note:: 698f4a2713aSLionel Sambuc 699f4a2713aSLionel Sambuc Clang implements a weak form of this requirement: the definitions 700f4a2713aSLionel Sambuc used for configuration macros are fixed based on the definitions 701f4a2713aSLionel Sambuc provided by the command line. If an import occurs and the definition 702f4a2713aSLionel Sambuc of any configuration macro has changed, the compiler will produce a 703f4a2713aSLionel Sambuc warning (under the control of ``-Wconfig-macros``). 704f4a2713aSLionel Sambuc 705f4a2713aSLionel Sambuc**Example:** A logging library might provide different API (e.g., in the form of different definitions for a logging macro) based on the ``NDEBUG`` macro setting: 706f4a2713aSLionel Sambuc 707f4a2713aSLionel Sambuc.. parsed-literal:: 708f4a2713aSLionel Sambuc 709f4a2713aSLionel Sambuc module MyLogger { 710f4a2713aSLionel Sambuc umbrella header "MyLogger.h" 711f4a2713aSLionel Sambuc config_macros [exhaustive] NDEBUG 712f4a2713aSLionel Sambuc } 713f4a2713aSLionel Sambuc 714f4a2713aSLionel SambucConflict declarations 715f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~~~~ 716f4a2713aSLionel SambucA *conflict-declaration* describes a case where the presence of two different modules in the same translation unit is likely to cause a problem. For example, two modules may provide similar-but-incompatible functionality. 717f4a2713aSLionel Sambuc 718f4a2713aSLionel Sambuc.. parsed-literal:: 719f4a2713aSLionel Sambuc 720f4a2713aSLionel Sambuc *conflict-declaration*: 721f4a2713aSLionel Sambuc ``conflict`` *module-id* ',' *string-literal* 722f4a2713aSLionel Sambuc 723f4a2713aSLionel SambucThe *module-id* of the *conflict-declaration* specifies the module with which the enclosing module conflicts. The specified module shall not have been imported in the translation unit when the enclosing module is imported. 724f4a2713aSLionel Sambuc 725f4a2713aSLionel SambucThe *string-literal* provides a message to be provided as part of the compiler diagnostic when two modules conflict. 726f4a2713aSLionel Sambuc 727f4a2713aSLionel Sambuc.. note:: 728f4a2713aSLionel Sambuc 729f4a2713aSLionel Sambuc Clang emits a warning (under the control of ``-Wmodule-conflict``) 730f4a2713aSLionel Sambuc when a module conflict is discovered. 731f4a2713aSLionel Sambuc 732f4a2713aSLionel Sambuc**Example:** 733f4a2713aSLionel Sambuc 734f4a2713aSLionel Sambuc.. parsed-literal:: 735f4a2713aSLionel Sambuc 736f4a2713aSLionel Sambuc module Conflicts { 737f4a2713aSLionel Sambuc explicit module A { 738f4a2713aSLionel Sambuc header "conflict_a.h" 739f4a2713aSLionel Sambuc conflict B, "we just don't like B" 740f4a2713aSLionel Sambuc } 741f4a2713aSLionel Sambuc 742f4a2713aSLionel Sambuc module B { 743f4a2713aSLionel Sambuc header "conflict_b.h" 744f4a2713aSLionel Sambuc } 745f4a2713aSLionel Sambuc } 746f4a2713aSLionel Sambuc 747f4a2713aSLionel Sambuc 748f4a2713aSLionel SambucAttributes 749f4a2713aSLionel Sambuc---------- 750f4a2713aSLionel SambucAttributes are used in a number of places in the grammar to describe specific behavior of other declarations. The format of attributes is fairly simple. 751f4a2713aSLionel Sambuc 752f4a2713aSLionel Sambuc.. parsed-literal:: 753f4a2713aSLionel Sambuc 754f4a2713aSLionel Sambuc *attributes*: 755f4a2713aSLionel Sambuc *attribute* *attributes*:sub:`opt` 756f4a2713aSLionel Sambuc 757f4a2713aSLionel Sambuc *attribute*: 758f4a2713aSLionel Sambuc '[' *identifier* ']' 759f4a2713aSLionel Sambuc 760f4a2713aSLionel SambucAny *identifier* can be used as an attribute, and each declaration specifies what attributes can be applied to it. 761f4a2713aSLionel Sambuc 762*0a6a1f1dSLionel SambucPrivate Module Map Files 763*0a6a1f1dSLionel Sambuc------------------------ 764*0a6a1f1dSLionel SambucModule map files are typically named ``module.modulemap`` and live 765*0a6a1f1dSLionel Sambuceither alongside the headers they describe or in a parent directory of 766*0a6a1f1dSLionel Sambucthe headers they describe. These module maps typically describe all of 767*0a6a1f1dSLionel Sambucthe API for the library. 768*0a6a1f1dSLionel Sambuc 769*0a6a1f1dSLionel SambucHowever, in some cases, the presence or absence of particular headers 770*0a6a1f1dSLionel Sambucis used to distinguish between the "public" and "private" APIs of a 771*0a6a1f1dSLionel Sambucparticular library. For example, a library may contain the headers 772*0a6a1f1dSLionel Sambuc``Foo.h`` and ``Foo_Private.h``, providing public and private APIs, 773*0a6a1f1dSLionel Sambucrespectively. Additionally, ``Foo_Private.h`` may only be available on 774*0a6a1f1dSLionel Sambucsome versions of library, and absent in others. One cannot easily 775*0a6a1f1dSLionel Sambucexpress this with a single module map file in the library: 776*0a6a1f1dSLionel Sambuc 777*0a6a1f1dSLionel Sambuc.. parsed-literal:: 778*0a6a1f1dSLionel Sambuc 779*0a6a1f1dSLionel Sambuc module Foo { 780*0a6a1f1dSLionel Sambuc header "Foo.h" 781*0a6a1f1dSLionel Sambuc 782*0a6a1f1dSLionel Sambuc explicit module Private { 783*0a6a1f1dSLionel Sambuc header "Foo_Private.h" 784*0a6a1f1dSLionel Sambuc } 785*0a6a1f1dSLionel Sambuc } 786*0a6a1f1dSLionel Sambuc 787*0a6a1f1dSLionel Sambuc 788*0a6a1f1dSLionel Sambucbecause the header ``Foo_Private.h`` won't always be available. The 789*0a6a1f1dSLionel Sambucmodule map file could be customized based on whether 790*0a6a1f1dSLionel Sambuc``Foo_Private.h`` is available or not, but doing so requires custom 791*0a6a1f1dSLionel Sambucbuild machinery. 792*0a6a1f1dSLionel Sambuc 793*0a6a1f1dSLionel SambucPrivate module map files, which are named ``module.private.modulemap`` 794*0a6a1f1dSLionel Sambuc(or, for backward compatibility, ``module_private.map``), allow one to 795*0a6a1f1dSLionel Sambucaugment the primary module map file with an additional submodule. For 796*0a6a1f1dSLionel Sambucexample, we would split the module map file above into two module map 797*0a6a1f1dSLionel Sambucfiles: 798*0a6a1f1dSLionel Sambuc 799*0a6a1f1dSLionel Sambuc.. code-block:: c 800*0a6a1f1dSLionel Sambuc 801*0a6a1f1dSLionel Sambuc /* module.modulemap */ 802*0a6a1f1dSLionel Sambuc module Foo { 803*0a6a1f1dSLionel Sambuc header "Foo.h" 804*0a6a1f1dSLionel Sambuc } 805*0a6a1f1dSLionel Sambuc 806*0a6a1f1dSLionel Sambuc /* module.private.modulemap */ 807*0a6a1f1dSLionel Sambuc explicit module Foo.Private { 808*0a6a1f1dSLionel Sambuc header "Foo_Private.h" 809*0a6a1f1dSLionel Sambuc } 810*0a6a1f1dSLionel Sambuc 811*0a6a1f1dSLionel Sambuc 812*0a6a1f1dSLionel SambucWhen a ``module.private.modulemap`` file is found alongside a 813*0a6a1f1dSLionel Sambuc``module.modulemap`` file, it is loaded after the ``module.modulemap`` 814*0a6a1f1dSLionel Sambucfile. In our example library, the ``module.private.modulemap`` file 815*0a6a1f1dSLionel Sambucwould be available when ``Foo_Private.h`` is available, making it 816*0a6a1f1dSLionel Sambuceasier to split a library's public and private APIs along header 817*0a6a1f1dSLionel Sambucboundaries. 818*0a6a1f1dSLionel Sambuc 819f4a2713aSLionel SambucModularizing a Platform 820f4a2713aSLionel Sambuc======================= 821f4a2713aSLionel SambucTo get any benefit out of modules, one needs to introduce module maps for software libraries starting at the bottom of the stack. This typically means introducing a module map covering the operating system's headers and the C standard library headers (in ``/usr/include``, for a Unix system). 822f4a2713aSLionel Sambuc 823f4a2713aSLionel SambucThe module maps will be written using the `module map language`_, which provides the tools necessary to describe the mapping between headers and modules. Because the set of headers differs from one system to the next, the module map will likely have to be somewhat customized for, e.g., a particular distribution and version of the operating system. Moreover, the system headers themselves may require some modification, if they exhibit any anti-patterns that break modules. Such common patterns are described below. 824f4a2713aSLionel Sambuc 825f4a2713aSLionel Sambuc**Macro-guarded copy-and-pasted definitions** 826f4a2713aSLionel Sambuc System headers vend core types such as ``size_t`` for users. These types are often needed in a number of system headers, and are almost trivial to write. Hence, it is fairly common to see a definition such as the following copy-and-pasted throughout the headers: 827f4a2713aSLionel Sambuc 828f4a2713aSLionel Sambuc .. parsed-literal:: 829f4a2713aSLionel Sambuc 830f4a2713aSLionel Sambuc #ifndef _SIZE_T 831f4a2713aSLionel Sambuc #define _SIZE_T 832f4a2713aSLionel Sambuc typedef __SIZE_TYPE__ size_t; 833f4a2713aSLionel Sambuc #endif 834f4a2713aSLionel Sambuc 835f4a2713aSLionel Sambuc Unfortunately, when modules compiles all of the C library headers together into a single module, only the first actual type definition of ``size_t`` will be visible, and then only in the submodule corresponding to the lucky first header. Any other headers that have copy-and-pasted versions of this pattern will *not* have a definition of ``size_t``. Importing the submodule corresponding to one of those headers will therefore not yield ``size_t`` as part of the API, because it wasn't there when the header was parsed. The fix for this problem is either to pull the copied declarations into a common header that gets included everywhere ``size_t`` is part of the API, or to eliminate the ``#ifndef`` and redefine the ``size_t`` type. The latter works for C++ headers and C11, but will cause an error for non-modules C90/C99, where redefinition of ``typedefs`` is not permitted. 836f4a2713aSLionel Sambuc 837f4a2713aSLionel Sambuc**Conflicting definitions** 838f4a2713aSLionel Sambuc Different system headers may provide conflicting definitions for various macros, functions, or types. These conflicting definitions don't tend to cause problems in a pre-modules world unless someone happens to include both headers in one translation unit. Since the fix is often simply "don't do that", such problems persist. Modules requires that the conflicting definitions be eliminated or that they be placed in separate modules (the former is generally the better answer). 839f4a2713aSLionel Sambuc 840f4a2713aSLionel Sambuc**Missing includes** 841f4a2713aSLionel Sambuc Headers are often missing ``#include`` directives for headers that they actually depend on. As with the problem of conflicting definitions, this only affects unlucky users who don't happen to include headers in the right order. With modules, the headers of a particular module will be parsed in isolation, so the module may fail to build if there are missing includes. 842f4a2713aSLionel Sambuc 843f4a2713aSLionel Sambuc**Headers that vend multiple APIs at different times** 844f4a2713aSLionel Sambuc Some systems have headers that contain a number of different kinds of API definitions, only some of which are made available with a given include. For example, the header may vend ``size_t`` only when the macro ``__need_size_t`` is defined before that header is included, and also vend ``wchar_t`` only when the macro ``__need_wchar_t`` is defined. Such headers are often included many times in a single translation unit, and will have no include guards. There is no sane way to map this header to a submodule. One can either eliminate the header (e.g., by splitting it into separate headers, one per actual API) or simply ``exclude`` it in the module map. 845f4a2713aSLionel Sambuc 846f4a2713aSLionel SambucTo detect and help address some of these problems, the ``clang-tools-extra`` repository contains a ``modularize`` tool that parses a set of given headers and attempts to detect these problems and produce a report. See the tool's in-source documentation for information on how to check your system or library headers. 847f4a2713aSLionel Sambuc 848f4a2713aSLionel SambucFuture Directions 849f4a2713aSLionel Sambuc================= 850*0a6a1f1dSLionel SambucModules support is under active development, and there are many opportunities remaining to improve it. Here are a few ideas: 851f4a2713aSLionel Sambuc 852f4a2713aSLionel Sambuc**Detect unused module imports** 853f4a2713aSLionel Sambuc Unlike with ``#include`` directives, it should be fairly simple to track whether a directly-imported module has ever been used. By doing so, Clang can emit ``unused import`` or ``unused #include`` diagnostics, including Fix-Its to remove the useless imports/includes. 854f4a2713aSLionel Sambuc 855f4a2713aSLionel Sambuc**Fix-Its for missing imports** 856*0a6a1f1dSLionel Sambuc It's fairly common for one to make use of some API while writing code, only to get a compiler error about "unknown type" or "no function named" because the corresponding header has not been included. Clang can detect such cases and auto-import the required module, but should provide a Fix-It to add the import. 857f4a2713aSLionel Sambuc 858f4a2713aSLionel Sambuc**Improve modularize** 859f4a2713aSLionel Sambuc The modularize tool is both extremely important (for deployment) and extremely crude. It needs better UI, better detection of problems (especially for C++), and perhaps an assistant mode to help write module maps for you. 860f4a2713aSLionel Sambuc 861f4a2713aSLionel SambucWhere To Learn More About Modules 862f4a2713aSLionel Sambuc================================= 863f4a2713aSLionel SambucThe Clang source code provides additional information about modules: 864f4a2713aSLionel Sambuc 865*0a6a1f1dSLionel Sambuc``clang/lib/Headers/module.modulemap`` 866f4a2713aSLionel Sambuc Module map for Clang's compiler-specific header files. 867f4a2713aSLionel Sambuc 868f4a2713aSLionel Sambuc``clang/test/Modules/`` 869f4a2713aSLionel Sambuc Tests specifically related to modules functionality. 870f4a2713aSLionel Sambuc 871f4a2713aSLionel Sambuc``clang/include/clang/Basic/Module.h`` 872f4a2713aSLionel Sambuc The ``Module`` class in this header describes a module, and is used throughout the compiler to implement modules. 873f4a2713aSLionel Sambuc 874f4a2713aSLionel Sambuc``clang/include/clang/Lex/ModuleMap.h`` 875f4a2713aSLionel Sambuc The ``ModuleMap`` class in this header describes the full module map, consisting of all of the module map files that have been parsed, and providing facilities for looking up module maps and mapping between modules and headers (in both directions). 876f4a2713aSLionel Sambuc 877f4a2713aSLionel SambucPCHInternals_ 878f4a2713aSLionel Sambuc Information about the serialized AST format used for precompiled headers and modules. The actual implementation is in the ``clangSerialization`` library. 879f4a2713aSLionel Sambuc 880f4a2713aSLionel Sambuc.. [#] Automatic linking against the libraries of modules requires specific linker support, which is not widely available. 881f4a2713aSLionel Sambuc 882f4a2713aSLionel Sambuc.. [#] There are certain anti-patterns that occur in headers, particularly system headers, that cause problems for modules. The section `Modularizing a Platform`_ describes some of them. 883f4a2713aSLionel Sambuc 884f4a2713aSLionel Sambuc.. [#] The second instance is actually a new thread within the current process, not a separate process. However, the original compiler instance is blocked on the execution of this thread. 885f4a2713aSLionel Sambuc 886f4a2713aSLionel Sambuc.. [#] The preprocessing context in which the modules are parsed is actually dependent on the command-line options provided to the compiler, including the language dialect and any ``-D`` options. However, the compiled modules for different command-line options are kept distinct, and any preprocessor directives that occur within the translation unit are ignored. See the section on the `Configuration macros declaration`_ for more information. 887f4a2713aSLionel Sambuc 888f4a2713aSLionel Sambuc.. _PCHInternals: PCHInternals.html 889f4a2713aSLionel Sambuc 890