1*f4a2713aSLionel Sambuc======= 2*f4a2713aSLionel SambucModules 3*f4a2713aSLionel Sambuc======= 4*f4a2713aSLionel Sambuc 5*f4a2713aSLionel Sambuc.. warning:: 6*f4a2713aSLionel Sambuc The functionality described on this page is supported for C and 7*f4a2713aSLionel Sambuc Objective-C. C++ support is experimental. 8*f4a2713aSLionel Sambuc 9*f4a2713aSLionel Sambuc.. contents:: 10*f4a2713aSLionel Sambuc :local: 11*f4a2713aSLionel Sambuc 12*f4a2713aSLionel SambucIntroduction 13*f4a2713aSLionel Sambuc============ 14*f4a2713aSLionel 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): 15*f4a2713aSLionel Sambuc 16*f4a2713aSLionel Sambuc.. code-block:: c 17*f4a2713aSLionel Sambuc 18*f4a2713aSLionel Sambuc #include <SomeLib.h> 19*f4a2713aSLionel Sambuc 20*f4a2713aSLionel SambucThe implementation is handled separately by linking against the appropriate library. For example, by passing ``-lSomeLib`` to the linker. 21*f4a2713aSLionel Sambuc 22*f4a2713aSLionel 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. 23*f4a2713aSLionel Sambuc 24*f4a2713aSLionel SambucProblems with the current model 25*f4a2713aSLionel Sambuc------------------------------- 26*f4a2713aSLionel 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: 27*f4a2713aSLionel Sambuc 28*f4a2713aSLionel Sambuc* **Compile-time scalability**: Each time a header is included, the 29*f4a2713aSLionel Sambuc compiler must preprocess and parse the text in that header and every 30*f4a2713aSLionel Sambuc header it includes, transitively. This process must be repeated for 31*f4a2713aSLionel Sambuc every translation unit in the application, which involves a huge 32*f4a2713aSLionel Sambuc amount of redundant work. In a project with *N* translation units 33*f4a2713aSLionel Sambuc and *M* headers included in each translation unit, the compiler is 34*f4a2713aSLionel Sambuc performing *M x N* work even though most of the *M* headers are 35*f4a2713aSLionel Sambuc shared among multiple translation units. C++ is particularly bad, 36*f4a2713aSLionel Sambuc because the compilation model for templates forces a huge amount of 37*f4a2713aSLionel Sambuc code into headers. 38*f4a2713aSLionel Sambuc 39*f4a2713aSLionel Sambuc* **Fragility**: ``#include`` directives are treated as textual 40*f4a2713aSLionel Sambuc inclusion by the preprocessor, and are therefore subject to any 41*f4a2713aSLionel Sambuc active macro definitions at the time of inclusion. If any of the 42*f4a2713aSLionel Sambuc active macro definitions happens to collide with a name in the 43*f4a2713aSLionel Sambuc library, it can break the library API or cause compilation failures 44*f4a2713aSLionel Sambuc in the library header itself. For an extreme example, 45*f4a2713aSLionel Sambuc ``#define std "The C++ Standard"`` and then include a standard 46*f4a2713aSLionel Sambuc library header: the result is a horrific cascade of failures in the 47*f4a2713aSLionel Sambuc C++ Standard Library's implementation. More subtle real-world 48*f4a2713aSLionel Sambuc problems occur when the headers for two different libraries interact 49*f4a2713aSLionel Sambuc due to macro collisions, and users are forced to reorder 50*f4a2713aSLionel Sambuc ``#include`` directives or introduce ``#undef`` directives to break 51*f4a2713aSLionel Sambuc the (unintended) dependency. 52*f4a2713aSLionel Sambuc 53*f4a2713aSLionel Sambuc* **Conventional workarounds**: C programmers have 54*f4a2713aSLionel Sambuc adopted a number of conventions to work around the fragility of the 55*f4a2713aSLionel Sambuc C preprocessor model. Include guards, for example, are required for 56*f4a2713aSLionel Sambuc the vast majority of headers to ensure that multiple inclusion 57*f4a2713aSLionel Sambuc doesn't break the compile. Macro names are written with 58*f4a2713aSLionel Sambuc ``LONG_PREFIXED_UPPERCASE_IDENTIFIERS`` to avoid collisions, and some 59*f4a2713aSLionel Sambuc library/framework developers even use ``__underscored`` names 60*f4a2713aSLionel Sambuc in headers to avoid collisions with "normal" names that (by 61*f4a2713aSLionel Sambuc convention) shouldn't even be macros. These conventions are a 62*f4a2713aSLionel Sambuc barrier to entry for developers coming from non-C languages, are 63*f4a2713aSLionel Sambuc boilerplate for more experienced developers, and make our headers 64*f4a2713aSLionel Sambuc far uglier than they should be. 65*f4a2713aSLionel Sambuc 66*f4a2713aSLionel Sambuc* **Tool confusion**: In a C-based language, it is hard to build tools 67*f4a2713aSLionel Sambuc that work well with software libraries, because the boundaries of 68*f4a2713aSLionel Sambuc the libraries are not clear. Which headers belong to a particular 69*f4a2713aSLionel Sambuc library, and in what order should those headers be included to 70*f4a2713aSLionel Sambuc guarantee that they compile correctly? Are the headers C, C++, 71*f4a2713aSLionel Sambuc Objective-C++, or one of the variants of these languages? What 72*f4a2713aSLionel Sambuc declarations in those headers are actually meant to be part of the 73*f4a2713aSLionel Sambuc API, and what declarations are present only because they had to be 74*f4a2713aSLionel Sambuc written as part of the header file? 75*f4a2713aSLionel Sambuc 76*f4a2713aSLionel SambucSemantic import 77*f4a2713aSLionel Sambuc--------------- 78*f4a2713aSLionel 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: 79*f4a2713aSLionel Sambuc 80*f4a2713aSLionel Sambuc.. code-block:: c 81*f4a2713aSLionel Sambuc 82*f4a2713aSLionel Sambuc import std.io; // pseudo-code; see below for syntax discussion 83*f4a2713aSLionel Sambuc 84*f4a2713aSLionel 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 [#]_ 85*f4a2713aSLionel SambucThis semantic import model addresses many of the problems of the preprocessor inclusion model: 86*f4a2713aSLionel Sambuc 87*f4a2713aSLionel 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. 88*f4a2713aSLionel Sambuc 89*f4a2713aSLionel 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. 90*f4a2713aSLionel Sambuc 91*f4a2713aSLionel 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. 92*f4a2713aSLionel Sambuc 93*f4a2713aSLionel SambucProblems modules do not solve 94*f4a2713aSLionel Sambuc----------------------------- 95*f4a2713aSLionel 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: 96*f4a2713aSLionel Sambuc 97*f4a2713aSLionel 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. 98*f4a2713aSLionel Sambuc 99*f4a2713aSLionel 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. 100*f4a2713aSLionel Sambuc 101*f4a2713aSLionel 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. 102*f4a2713aSLionel Sambuc 103*f4a2713aSLionel 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. 104*f4a2713aSLionel Sambuc 105*f4a2713aSLionel SambucUsing Modules 106*f4a2713aSLionel Sambuc============= 107*f4a2713aSLionel 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. 108*f4a2713aSLionel Sambuc 109*f4a2713aSLionel SambucObjective-C Import declaration 110*f4a2713aSLionel Sambuc------------------------------ 111*f4a2713aSLionel SambucObjective-C provides syntax for importing a module via an *@import declaration*, which imports the named module: 112*f4a2713aSLionel Sambuc 113*f4a2713aSLionel Sambuc.. parsed-literal:: 114*f4a2713aSLionel Sambuc 115*f4a2713aSLionel Sambuc @import std; 116*f4a2713aSLionel Sambuc 117*f4a2713aSLionel 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., 118*f4a2713aSLionel Sambuc 119*f4a2713aSLionel Sambuc.. parsed-literal:: 120*f4a2713aSLionel Sambuc 121*f4a2713aSLionel Sambuc @import std.io; 122*f4a2713aSLionel Sambuc 123*f4a2713aSLionel 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. 124*f4a2713aSLionel Sambuc 125*f4a2713aSLionel SambucAt present, there is no C or C++ syntax for import declarations. Clang 126*f4a2713aSLionel Sambucwill track the modules proposal in the C++ committee. See the section 127*f4a2713aSLionel Sambuc`Includes as imports`_ to see how modules get imported today. 128*f4a2713aSLionel Sambuc 129*f4a2713aSLionel SambucIncludes as imports 130*f4a2713aSLionel Sambuc------------------- 131*f4a2713aSLionel 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 132*f4a2713aSLionel Sambuc 133*f4a2713aSLionel Sambuc.. code-block:: c 134*f4a2713aSLionel Sambuc 135*f4a2713aSLionel Sambuc #include <stdio.h> 136*f4a2713aSLionel Sambuc 137*f4a2713aSLionel 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. 138*f4a2713aSLionel Sambuc 139*f4a2713aSLionel Sambuc.. note:: 140*f4a2713aSLionel Sambuc 141*f4a2713aSLionel 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. 142*f4a2713aSLionel Sambuc 143*f4a2713aSLionel SambucModule maps 144*f4a2713aSLionel Sambuc----------- 145*f4a2713aSLionel 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. 146*f4a2713aSLionel Sambuc 147*f4a2713aSLionel SambucModule maps are specified as separate files (each named ``module.map``) 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. 148*f4a2713aSLionel Sambuc 149*f4a2713aSLionel Sambuc.. note:: 150*f4a2713aSLionel Sambuc 151*f4a2713aSLionel 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. 152*f4a2713aSLionel Sambuc 153*f4a2713aSLionel 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. 154*f4a2713aSLionel Sambuc 155*f4a2713aSLionel SambucCompilation model 156*f4a2713aSLionel Sambuc----------------- 157*f4a2713aSLionel 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. 158*f4a2713aSLionel Sambuc 159*f4a2713aSLionel 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. 160*f4a2713aSLionel Sambuc 161*f4a2713aSLionel 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. 162*f4a2713aSLionel Sambuc 163*f4a2713aSLionel SambucCommand-line parameters 164*f4a2713aSLionel Sambuc----------------------- 165*f4a2713aSLionel Sambuc``-fmodules`` 166*f4a2713aSLionel Sambuc Enable the modules feature (EXPERIMENTAL). 167*f4a2713aSLionel Sambuc 168*f4a2713aSLionel Sambuc``-fcxx-modules`` 169*f4a2713aSLionel Sambuc Enable the modules feature for C++ (EXPERIMENTAL and VERY BROKEN). 170*f4a2713aSLionel Sambuc 171*f4a2713aSLionel Sambuc``-fmodule-maps`` 172*f4a2713aSLionel Sambuc Enable interpretation of module maps (EXPERIMENTAL). This option is implied by ``-fmodules``. 173*f4a2713aSLionel Sambuc 174*f4a2713aSLionel Sambuc``-fmodules-cache-path=<directory>`` 175*f4a2713aSLionel Sambuc Specify the path to the modules cache. If not provided, Clang will select a system-appropriate default. 176*f4a2713aSLionel Sambuc 177*f4a2713aSLionel Sambuc``-fno-autolink`` 178*f4a2713aSLionel Sambuc Disable automatic linking against the libraries associated with imported modules. 179*f4a2713aSLionel Sambuc 180*f4a2713aSLionel Sambuc``-fmodules-ignore-macro=macroname`` 181*f4a2713aSLionel 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. 182*f4a2713aSLionel Sambuc 183*f4a2713aSLionel Sambuc``-fmodules-prune-interval=seconds`` 184*f4a2713aSLionel 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. 185*f4a2713aSLionel Sambuc 186*f4a2713aSLionel Sambuc``-fmodules-prune-after=seconds`` 187*f4a2713aSLionel 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. 188*f4a2713aSLionel Sambuc 189*f4a2713aSLionel Sambuc``-module-file-info <module file name>`` 190*f4a2713aSLionel 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. 191*f4a2713aSLionel Sambuc 192*f4a2713aSLionel Sambuc``-fmodules-decluse`` 193*f4a2713aSLionel Sambuc Enable checking of module ``use`` declarations. 194*f4a2713aSLionel Sambuc 195*f4a2713aSLionel Sambuc``-fmodule-name=module-id`` 196*f4a2713aSLionel Sambuc Consider a source file as a part of the given module. 197*f4a2713aSLionel Sambuc 198*f4a2713aSLionel Sambuc``-fmodule-map-file=<file>`` 199*f4a2713aSLionel Sambuc Load the given module map file if a header from its directory or one of its subdirectories is loaded. 200*f4a2713aSLionel Sambuc 201*f4a2713aSLionel SambucModule Map Language 202*f4a2713aSLionel Sambuc=================== 203*f4a2713aSLionel Sambuc 204*f4a2713aSLionel SambucThe module map language describes the mapping from header files to the 205*f4a2713aSLionel Sambuclogical structure of modules. To enable support for using a library as 206*f4a2713aSLionel Sambuca module, one must write a ``module.map`` file for that library. The 207*f4a2713aSLionel Sambuc``module.map`` file is placed alongside the header files themselves, 208*f4a2713aSLionel Sambucand is written in the module map language described below. 209*f4a2713aSLionel Sambuc 210*f4a2713aSLionel SambucAs an example, the module map file for the C standard library might look a bit like this: 211*f4a2713aSLionel Sambuc 212*f4a2713aSLionel Sambuc.. parsed-literal:: 213*f4a2713aSLionel Sambuc 214*f4a2713aSLionel Sambuc module std [system] { 215*f4a2713aSLionel Sambuc module complex { 216*f4a2713aSLionel Sambuc header "complex.h" 217*f4a2713aSLionel Sambuc export * 218*f4a2713aSLionel Sambuc } 219*f4a2713aSLionel Sambuc 220*f4a2713aSLionel Sambuc module ctype { 221*f4a2713aSLionel Sambuc header "ctype.h" 222*f4a2713aSLionel Sambuc export * 223*f4a2713aSLionel Sambuc } 224*f4a2713aSLionel Sambuc 225*f4a2713aSLionel Sambuc module errno { 226*f4a2713aSLionel Sambuc header "errno.h" 227*f4a2713aSLionel Sambuc header "sys/errno.h" 228*f4a2713aSLionel Sambuc export * 229*f4a2713aSLionel Sambuc } 230*f4a2713aSLionel Sambuc 231*f4a2713aSLionel Sambuc module fenv { 232*f4a2713aSLionel Sambuc header "fenv.h" 233*f4a2713aSLionel Sambuc export * 234*f4a2713aSLionel Sambuc } 235*f4a2713aSLionel Sambuc 236*f4a2713aSLionel Sambuc // ...more headers follow... 237*f4a2713aSLionel Sambuc } 238*f4a2713aSLionel Sambuc 239*f4a2713aSLionel 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. 240*f4a2713aSLionel Sambuc 241*f4a2713aSLionel SambucLexical structure 242*f4a2713aSLionel Sambuc----------------- 243*f4a2713aSLionel 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. 244*f4a2713aSLionel Sambuc 245*f4a2713aSLionel Sambuc.. parsed-literal:: 246*f4a2713aSLionel Sambuc 247*f4a2713aSLionel Sambuc ``config_macros`` ``export`` ``module`` 248*f4a2713aSLionel Sambuc ``conflict`` ``framework`` ``requires`` 249*f4a2713aSLionel Sambuc ``exclude`` ``header`` ``private`` 250*f4a2713aSLionel Sambuc ``explicit`` ``link`` ``umbrella`` 251*f4a2713aSLionel Sambuc ``extern`` ``use`` 252*f4a2713aSLionel Sambuc 253*f4a2713aSLionel SambucModule map file 254*f4a2713aSLionel Sambuc--------------- 255*f4a2713aSLionel SambucA module map file consists of a series of module declarations: 256*f4a2713aSLionel Sambuc 257*f4a2713aSLionel Sambuc.. parsed-literal:: 258*f4a2713aSLionel Sambuc 259*f4a2713aSLionel Sambuc *module-map-file*: 260*f4a2713aSLionel Sambuc *module-declaration** 261*f4a2713aSLionel Sambuc 262*f4a2713aSLionel 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: 263*f4a2713aSLionel Sambuc 264*f4a2713aSLionel Sambuc.. parsed-literal:: 265*f4a2713aSLionel Sambuc 266*f4a2713aSLionel Sambuc *module-id*: 267*f4a2713aSLionel Sambuc *identifier* ('.' *identifier*)* 268*f4a2713aSLionel Sambuc 269*f4a2713aSLionel SambucModule declaration 270*f4a2713aSLionel Sambuc------------------ 271*f4a2713aSLionel SambucA module declaration describes a module, including the headers that contribute to that module, its submodules, and other aspects of the module. 272*f4a2713aSLionel Sambuc 273*f4a2713aSLionel Sambuc.. parsed-literal:: 274*f4a2713aSLionel Sambuc 275*f4a2713aSLionel Sambuc *module-declaration*: 276*f4a2713aSLionel Sambuc ``explicit``:sub:`opt` ``framework``:sub:`opt` ``module`` *module-id* *attributes*:sub:`opt` '{' *module-member** '}' 277*f4a2713aSLionel Sambuc ``extern`` ``module`` *module-id* *string-literal* 278*f4a2713aSLionel Sambuc 279*f4a2713aSLionel 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. 280*f4a2713aSLionel Sambuc 281*f4a2713aSLionel 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. 282*f4a2713aSLionel Sambuc 283*f4a2713aSLionel 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: 284*f4a2713aSLionel Sambuc 285*f4a2713aSLionel Sambuc.. parsed-literal:: 286*f4a2713aSLionel Sambuc 287*f4a2713aSLionel Sambuc Name.framework/ 288*f4a2713aSLionel Sambuc module.map Module map for the framework 289*f4a2713aSLionel Sambuc Headers/ Subdirectory containing framework headers 290*f4a2713aSLionel Sambuc Frameworks/ Subdirectory containing embedded frameworks 291*f4a2713aSLionel Sambuc Resources/ Subdirectory containing additional resources 292*f4a2713aSLionel Sambuc Name Symbolic link to the shared library for the framework 293*f4a2713aSLionel Sambuc 294*f4a2713aSLionel SambucThe ``system`` attribute specifies that the module is a system module. When a system module is rebuilt, all of the module's header 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. 295*f4a2713aSLionel Sambuc 296*f4a2713aSLionel SambucModules can have a number of different kinds of members, each of which is described below: 297*f4a2713aSLionel Sambuc 298*f4a2713aSLionel Sambuc.. parsed-literal:: 299*f4a2713aSLionel Sambuc 300*f4a2713aSLionel Sambuc *module-member*: 301*f4a2713aSLionel Sambuc *requires-declaration* 302*f4a2713aSLionel Sambuc *header-declaration* 303*f4a2713aSLionel Sambuc *umbrella-dir-declaration* 304*f4a2713aSLionel Sambuc *submodule-declaration* 305*f4a2713aSLionel Sambuc *export-declaration* 306*f4a2713aSLionel Sambuc *use-declaration* 307*f4a2713aSLionel Sambuc *link-declaration* 308*f4a2713aSLionel Sambuc *config-macros-declaration* 309*f4a2713aSLionel Sambuc *conflict-declaration* 310*f4a2713aSLionel Sambuc 311*f4a2713aSLionel 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. 312*f4a2713aSLionel Sambuc 313*f4a2713aSLionel SambucRequires declaration 314*f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~~~ 315*f4a2713aSLionel SambucA *requires-declaration* specifies the requirements that an importing translation unit must satisfy to use the module. 316*f4a2713aSLionel Sambuc 317*f4a2713aSLionel Sambuc.. parsed-literal:: 318*f4a2713aSLionel Sambuc 319*f4a2713aSLionel Sambuc *requires-declaration*: 320*f4a2713aSLionel Sambuc ``requires`` *feature-list* 321*f4a2713aSLionel Sambuc 322*f4a2713aSLionel Sambuc *feature-list*: 323*f4a2713aSLionel Sambuc *feature* (',' *feature*)* 324*f4a2713aSLionel Sambuc 325*f4a2713aSLionel Sambuc *feature*: 326*f4a2713aSLionel Sambuc ``!``:sub:`opt` *identifier* 327*f4a2713aSLionel Sambuc 328*f4a2713aSLionel 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. 329*f4a2713aSLionel Sambuc 330*f4a2713aSLionel SambucThe following features are defined: 331*f4a2713aSLionel Sambuc 332*f4a2713aSLionel Sambucaltivec 333*f4a2713aSLionel Sambuc The target supports AltiVec. 334*f4a2713aSLionel Sambuc 335*f4a2713aSLionel Sambucblocks 336*f4a2713aSLionel Sambuc The "blocks" language feature is available. 337*f4a2713aSLionel Sambuc 338*f4a2713aSLionel Sambuccplusplus 339*f4a2713aSLionel Sambuc C++ support is available. 340*f4a2713aSLionel Sambuc 341*f4a2713aSLionel Sambuccplusplus11 342*f4a2713aSLionel Sambuc C++11 support is available. 343*f4a2713aSLionel Sambuc 344*f4a2713aSLionel Sambucobjc 345*f4a2713aSLionel Sambuc Objective-C support is available. 346*f4a2713aSLionel Sambuc 347*f4a2713aSLionel Sambucobjc_arc 348*f4a2713aSLionel Sambuc Objective-C Automatic Reference Counting (ARC) is available 349*f4a2713aSLionel Sambuc 350*f4a2713aSLionel Sambucopencl 351*f4a2713aSLionel Sambuc OpenCL is available 352*f4a2713aSLionel Sambuc 353*f4a2713aSLionel Sambuctls 354*f4a2713aSLionel Sambuc Thread local storage is available. 355*f4a2713aSLionel Sambuc 356*f4a2713aSLionel Sambuc*target feature* 357*f4a2713aSLionel Sambuc A specific target feature (e.g., ``sse4``, ``avx``, ``neon``) is available. 358*f4a2713aSLionel Sambuc 359*f4a2713aSLionel Sambuc 360*f4a2713aSLionel Sambuc**Example**: The ``std`` module can be extended to also include C++ and C++11 headers using a *requires-declaration*: 361*f4a2713aSLionel Sambuc 362*f4a2713aSLionel Sambuc.. parsed-literal:: 363*f4a2713aSLionel Sambuc 364*f4a2713aSLionel Sambuc module std { 365*f4a2713aSLionel Sambuc // C standard library... 366*f4a2713aSLionel Sambuc 367*f4a2713aSLionel Sambuc module vector { 368*f4a2713aSLionel Sambuc requires cplusplus 369*f4a2713aSLionel Sambuc header "vector" 370*f4a2713aSLionel Sambuc } 371*f4a2713aSLionel Sambuc 372*f4a2713aSLionel Sambuc module type_traits { 373*f4a2713aSLionel Sambuc requires cplusplus11 374*f4a2713aSLionel Sambuc header "type_traits" 375*f4a2713aSLionel Sambuc } 376*f4a2713aSLionel Sambuc } 377*f4a2713aSLionel Sambuc 378*f4a2713aSLionel SambucHeader declaration 379*f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~ 380*f4a2713aSLionel SambucA header declaration specifies that a particular header is associated with the enclosing module. 381*f4a2713aSLionel Sambuc 382*f4a2713aSLionel Sambuc.. parsed-literal:: 383*f4a2713aSLionel Sambuc 384*f4a2713aSLionel Sambuc *header-declaration*: 385*f4a2713aSLionel Sambuc ``umbrella``:sub:`opt` ``header`` *string-literal* 386*f4a2713aSLionel Sambuc ``private`` ``header`` *string-literal* 387*f4a2713aSLionel Sambuc ``exclude`` ``header`` *string-literal* 388*f4a2713aSLionel Sambuc 389*f4a2713aSLionel SambucA header declaration that does not contain ``exclude`` 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. 390*f4a2713aSLionel Sambuc 391*f4a2713aSLionel 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. 392*f4a2713aSLionel Sambuc 393*f4a2713aSLionel Sambuc.. note:: 394*f4a2713aSLionel Sambuc Any headers not included by the umbrella header should have 395*f4a2713aSLionel Sambuc explicit ``header`` declarations. Use the 396*f4a2713aSLionel Sambuc ``-Wincomplete-umbrella`` warning option to ask Clang to complain 397*f4a2713aSLionel Sambuc about headers not covered by the umbrella header or the module map. 398*f4a2713aSLionel Sambuc 399*f4a2713aSLionel SambucA header with the ``private`` specifier may not be included from outside the module itself. 400*f4a2713aSLionel Sambuc 401*f4a2713aSLionel 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. 402*f4a2713aSLionel Sambuc 403*f4a2713aSLionel Sambuc**Example**: The C header ``assert.h`` is an excellent candidate for an excluded header, because it is meant to be included multiple times (possibly with different ``NDEBUG`` settings). 404*f4a2713aSLionel Sambuc 405*f4a2713aSLionel Sambuc.. parsed-literal:: 406*f4a2713aSLionel Sambuc 407*f4a2713aSLionel Sambuc module std [system] { 408*f4a2713aSLionel Sambuc exclude header "assert.h" 409*f4a2713aSLionel Sambuc } 410*f4a2713aSLionel Sambuc 411*f4a2713aSLionel SambucA given header shall not be referenced by more than one *header-declaration*. 412*f4a2713aSLionel Sambuc 413*f4a2713aSLionel SambucUmbrella directory declaration 414*f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 415*f4a2713aSLionel SambucAn umbrella directory declaration specifies that all of the headers in the specified directory should be included within the module. 416*f4a2713aSLionel Sambuc 417*f4a2713aSLionel Sambuc.. parsed-literal:: 418*f4a2713aSLionel Sambuc 419*f4a2713aSLionel Sambuc *umbrella-dir-declaration*: 420*f4a2713aSLionel Sambuc ``umbrella`` *string-literal* 421*f4a2713aSLionel Sambuc 422*f4a2713aSLionel 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. 423*f4a2713aSLionel Sambuc 424*f4a2713aSLionel 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. 425*f4a2713aSLionel Sambuc 426*f4a2713aSLionel Sambuc.. note:: 427*f4a2713aSLionel Sambuc 428*f4a2713aSLionel Sambuc Umbrella directories are useful for libraries that have a large number of headers but do not have an umbrella header. 429*f4a2713aSLionel Sambuc 430*f4a2713aSLionel Sambuc 431*f4a2713aSLionel SambucSubmodule declaration 432*f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~~~~ 433*f4a2713aSLionel SambucSubmodule declarations describe modules that are nested within their enclosing module. 434*f4a2713aSLionel Sambuc 435*f4a2713aSLionel Sambuc.. parsed-literal:: 436*f4a2713aSLionel Sambuc 437*f4a2713aSLionel Sambuc *submodule-declaration*: 438*f4a2713aSLionel Sambuc *module-declaration* 439*f4a2713aSLionel Sambuc *inferred-submodule-declaration* 440*f4a2713aSLionel Sambuc 441*f4a2713aSLionel 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. 442*f4a2713aSLionel Sambuc 443*f4a2713aSLionel 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*. 444*f4a2713aSLionel Sambuc 445*f4a2713aSLionel Sambuc.. parsed-literal:: 446*f4a2713aSLionel Sambuc 447*f4a2713aSLionel Sambuc *inferred-submodule-declaration*: 448*f4a2713aSLionel Sambuc ``explicit``:sub:`opt` ``framework``:sub:`opt` ``module`` '*' *attributes*:sub:`opt` '{' *inferred-submodule-member** '}' 449*f4a2713aSLionel Sambuc 450*f4a2713aSLionel Sambuc *inferred-submodule-member*: 451*f4a2713aSLionel Sambuc ``export`` '*' 452*f4a2713aSLionel Sambuc 453*f4a2713aSLionel 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). 454*f4a2713aSLionel Sambuc 455*f4a2713aSLionel 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: 456*f4a2713aSLionel Sambuc 457*f4a2713aSLionel Sambuc* Have the same name as the header (without the file extension) 458*f4a2713aSLionel Sambuc* Have the ``explicit`` specifier, if the *inferred-submodule-declaration* has the ``explicit`` specifier 459*f4a2713aSLionel Sambuc* Have the ``framework`` specifier, if the 460*f4a2713aSLionel Sambuc *inferred-submodule-declaration* has the ``framework`` specifier 461*f4a2713aSLionel Sambuc* Have the attributes specified by the \ *inferred-submodule-declaration* 462*f4a2713aSLionel Sambuc* Contain a single *header-declaration* naming that header 463*f4a2713aSLionel Sambuc* Contain a single *export-declaration* ``export *``, if the \ *inferred-submodule-declaration* contains the \ *inferred-submodule-member* ``export *`` 464*f4a2713aSLionel Sambuc 465*f4a2713aSLionel Sambuc**Example**: If the subdirectory "MyLib" contains the headers ``A.h`` and ``B.h``, then the following module map: 466*f4a2713aSLionel Sambuc 467*f4a2713aSLionel Sambuc.. parsed-literal:: 468*f4a2713aSLionel Sambuc 469*f4a2713aSLionel Sambuc module MyLib { 470*f4a2713aSLionel Sambuc umbrella "MyLib" 471*f4a2713aSLionel Sambuc explicit module * { 472*f4a2713aSLionel Sambuc export * 473*f4a2713aSLionel Sambuc } 474*f4a2713aSLionel Sambuc } 475*f4a2713aSLionel Sambuc 476*f4a2713aSLionel Sambucis equivalent to the (more verbose) module map: 477*f4a2713aSLionel Sambuc 478*f4a2713aSLionel Sambuc.. parsed-literal:: 479*f4a2713aSLionel Sambuc 480*f4a2713aSLionel Sambuc module MyLib { 481*f4a2713aSLionel Sambuc explicit module A { 482*f4a2713aSLionel Sambuc header "A.h" 483*f4a2713aSLionel Sambuc export * 484*f4a2713aSLionel Sambuc } 485*f4a2713aSLionel Sambuc 486*f4a2713aSLionel Sambuc explicit module B { 487*f4a2713aSLionel Sambuc header "B.h" 488*f4a2713aSLionel Sambuc export * 489*f4a2713aSLionel Sambuc } 490*f4a2713aSLionel Sambuc } 491*f4a2713aSLionel Sambuc 492*f4a2713aSLionel SambucExport declaration 493*f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~ 494*f4a2713aSLionel SambucAn *export-declaration* specifies which imported modules will automatically be re-exported as part of a given module's API. 495*f4a2713aSLionel Sambuc 496*f4a2713aSLionel Sambuc.. parsed-literal:: 497*f4a2713aSLionel Sambuc 498*f4a2713aSLionel Sambuc *export-declaration*: 499*f4a2713aSLionel Sambuc ``export`` *wildcard-module-id* 500*f4a2713aSLionel Sambuc 501*f4a2713aSLionel Sambuc *wildcard-module-id*: 502*f4a2713aSLionel Sambuc *identifier* 503*f4a2713aSLionel Sambuc '*' 504*f4a2713aSLionel Sambuc *identifier* '.' *wildcard-module-id* 505*f4a2713aSLionel Sambuc 506*f4a2713aSLionel 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. 507*f4a2713aSLionel Sambuc 508*f4a2713aSLionel Sambuc**Example**:: In the following example, importing ``MyLib.Derived`` also provides the API for ``MyLib.Base``: 509*f4a2713aSLionel Sambuc 510*f4a2713aSLionel Sambuc.. parsed-literal:: 511*f4a2713aSLionel Sambuc 512*f4a2713aSLionel Sambuc module MyLib { 513*f4a2713aSLionel Sambuc module Base { 514*f4a2713aSLionel Sambuc header "Base.h" 515*f4a2713aSLionel Sambuc } 516*f4a2713aSLionel Sambuc 517*f4a2713aSLionel Sambuc module Derived { 518*f4a2713aSLionel Sambuc header "Derived.h" 519*f4a2713aSLionel Sambuc export Base 520*f4a2713aSLionel Sambuc } 521*f4a2713aSLionel Sambuc } 522*f4a2713aSLionel Sambuc 523*f4a2713aSLionel SambucNote that, if ``Derived.h`` includes ``Base.h``, one can simply use a wildcard export to re-export everything ``Derived.h`` includes: 524*f4a2713aSLionel Sambuc 525*f4a2713aSLionel Sambuc.. parsed-literal:: 526*f4a2713aSLionel Sambuc 527*f4a2713aSLionel Sambuc module MyLib { 528*f4a2713aSLionel Sambuc module Base { 529*f4a2713aSLionel Sambuc header "Base.h" 530*f4a2713aSLionel Sambuc } 531*f4a2713aSLionel Sambuc 532*f4a2713aSLionel Sambuc module Derived { 533*f4a2713aSLionel Sambuc header "Derived.h" 534*f4a2713aSLionel Sambuc export * 535*f4a2713aSLionel Sambuc } 536*f4a2713aSLionel Sambuc } 537*f4a2713aSLionel Sambuc 538*f4a2713aSLionel Sambuc.. note:: 539*f4a2713aSLionel Sambuc 540*f4a2713aSLionel Sambuc The wildcard export syntax ``export *`` re-exports all of the 541*f4a2713aSLionel Sambuc modules that were imported in the actual header file. Because 542*f4a2713aSLionel Sambuc ``#include`` directives are automatically mapped to module imports, 543*f4a2713aSLionel Sambuc ``export *`` provides the same transitive-inclusion behavior 544*f4a2713aSLionel Sambuc provided by the C preprocessor, e.g., importing a given module 545*f4a2713aSLionel Sambuc implicitly imports all of the modules on which it depends. 546*f4a2713aSLionel Sambuc Therefore, liberal use of ``export *`` provides excellent backward 547*f4a2713aSLionel Sambuc compatibility for programs that rely on transitive inclusion (i.e., 548*f4a2713aSLionel Sambuc all of them). 549*f4a2713aSLionel Sambuc 550*f4a2713aSLionel SambucUse declaration 551*f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~ 552*f4a2713aSLionel 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*. 553*f4a2713aSLionel Sambuc 554*f4a2713aSLionel Sambuc.. parsed-literal:: 555*f4a2713aSLionel Sambuc 556*f4a2713aSLionel Sambuc *use-declaration*: 557*f4a2713aSLionel Sambuc ``use`` *module-id* 558*f4a2713aSLionel Sambuc 559*f4a2713aSLionel Sambuc**Example**:: In the following example, use of A from C is not declared, so will trigger a warning. 560*f4a2713aSLionel Sambuc 561*f4a2713aSLionel Sambuc.. parsed-literal:: 562*f4a2713aSLionel Sambuc 563*f4a2713aSLionel Sambuc module A { 564*f4a2713aSLionel Sambuc header "a.h" 565*f4a2713aSLionel Sambuc } 566*f4a2713aSLionel Sambuc 567*f4a2713aSLionel Sambuc module B { 568*f4a2713aSLionel Sambuc header "b.h" 569*f4a2713aSLionel Sambuc } 570*f4a2713aSLionel Sambuc 571*f4a2713aSLionel Sambuc module C { 572*f4a2713aSLionel Sambuc header "c.h" 573*f4a2713aSLionel Sambuc use B 574*f4a2713aSLionel Sambuc } 575*f4a2713aSLionel Sambuc 576*f4a2713aSLionel 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. 577*f4a2713aSLionel Sambuc 578*f4a2713aSLionel SambucThe compiler at present only applies restrictions to the module directly being built. 579*f4a2713aSLionel Sambuc 580*f4a2713aSLionel SambucLink declaration 581*f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~ 582*f4a2713aSLionel 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. 583*f4a2713aSLionel Sambuc 584*f4a2713aSLionel Sambuc.. parsed-literal:: 585*f4a2713aSLionel Sambuc 586*f4a2713aSLionel Sambuc *link-declaration*: 587*f4a2713aSLionel Sambuc ``link`` ``framework``:sub:`opt` *string-literal* 588*f4a2713aSLionel Sambuc 589*f4a2713aSLionel 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. 590*f4a2713aSLionel Sambuc 591*f4a2713aSLionel SambucA *link-declaration* with the ``framework`` specifies that the linker should link against the named framework, e.g., with ``-framework MyFramework``. 592*f4a2713aSLionel Sambuc 593*f4a2713aSLionel Sambuc.. note:: 594*f4a2713aSLionel Sambuc 595*f4a2713aSLionel Sambuc Automatic linking with the ``link`` directive is not yet widely 596*f4a2713aSLionel Sambuc implemented, because it requires support from both the object file 597*f4a2713aSLionel Sambuc format and the linker. The notion is similar to Microsoft Visual 598*f4a2713aSLionel Sambuc Studio's ``#pragma comment(lib...)``. 599*f4a2713aSLionel Sambuc 600*f4a2713aSLionel SambucConfiguration macros declaration 601*f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 602*f4a2713aSLionel SambucThe *config-macros-declaration* specifies the set of configuration macros that have an effect on the the API of the enclosing module. 603*f4a2713aSLionel Sambuc 604*f4a2713aSLionel Sambuc.. parsed-literal:: 605*f4a2713aSLionel Sambuc 606*f4a2713aSLionel Sambuc *config-macros-declaration*: 607*f4a2713aSLionel Sambuc ``config_macros`` *attributes*:sub:`opt` *config-macro-list*:sub:`opt` 608*f4a2713aSLionel Sambuc 609*f4a2713aSLionel Sambuc *config-macro-list*: 610*f4a2713aSLionel Sambuc *identifier* (',' *identifier*)* 611*f4a2713aSLionel Sambuc 612*f4a2713aSLionel 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. 613*f4a2713aSLionel Sambuc 614*f4a2713aSLionel 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. 615*f4a2713aSLionel Sambuc 616*f4a2713aSLionel 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. 617*f4a2713aSLionel Sambuc 618*f4a2713aSLionel Sambuc.. note:: 619*f4a2713aSLionel Sambuc 620*f4a2713aSLionel Sambuc The ``exhaustive`` attribute implies that any macro definitions 621*f4a2713aSLionel Sambuc for macros not listed as configuration macros should be ignored 622*f4a2713aSLionel Sambuc completely when building the module. As an optimization, the 623*f4a2713aSLionel Sambuc compiler could reduce the number of unique module variants by not 624*f4a2713aSLionel Sambuc considering these non-configuration macros. This optimization is not 625*f4a2713aSLionel Sambuc yet implemented in Clang. 626*f4a2713aSLionel Sambuc 627*f4a2713aSLionel SambucA translation unit shall not import the same module under different definitions of the configuration macros. 628*f4a2713aSLionel Sambuc 629*f4a2713aSLionel Sambuc.. note:: 630*f4a2713aSLionel Sambuc 631*f4a2713aSLionel Sambuc Clang implements a weak form of this requirement: the definitions 632*f4a2713aSLionel Sambuc used for configuration macros are fixed based on the definitions 633*f4a2713aSLionel Sambuc provided by the command line. If an import occurs and the definition 634*f4a2713aSLionel Sambuc of any configuration macro has changed, the compiler will produce a 635*f4a2713aSLionel Sambuc warning (under the control of ``-Wconfig-macros``). 636*f4a2713aSLionel Sambuc 637*f4a2713aSLionel 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: 638*f4a2713aSLionel Sambuc 639*f4a2713aSLionel Sambuc.. parsed-literal:: 640*f4a2713aSLionel Sambuc 641*f4a2713aSLionel Sambuc module MyLogger { 642*f4a2713aSLionel Sambuc umbrella header "MyLogger.h" 643*f4a2713aSLionel Sambuc config_macros [exhaustive] NDEBUG 644*f4a2713aSLionel Sambuc } 645*f4a2713aSLionel Sambuc 646*f4a2713aSLionel SambucConflict declarations 647*f4a2713aSLionel Sambuc~~~~~~~~~~~~~~~~~~~~~ 648*f4a2713aSLionel 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. 649*f4a2713aSLionel Sambuc 650*f4a2713aSLionel Sambuc.. parsed-literal:: 651*f4a2713aSLionel Sambuc 652*f4a2713aSLionel Sambuc *conflict-declaration*: 653*f4a2713aSLionel Sambuc ``conflict`` *module-id* ',' *string-literal* 654*f4a2713aSLionel Sambuc 655*f4a2713aSLionel 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. 656*f4a2713aSLionel Sambuc 657*f4a2713aSLionel SambucThe *string-literal* provides a message to be provided as part of the compiler diagnostic when two modules conflict. 658*f4a2713aSLionel Sambuc 659*f4a2713aSLionel Sambuc.. note:: 660*f4a2713aSLionel Sambuc 661*f4a2713aSLionel Sambuc Clang emits a warning (under the control of ``-Wmodule-conflict``) 662*f4a2713aSLionel Sambuc when a module conflict is discovered. 663*f4a2713aSLionel Sambuc 664*f4a2713aSLionel Sambuc**Example:** 665*f4a2713aSLionel Sambuc 666*f4a2713aSLionel Sambuc.. parsed-literal:: 667*f4a2713aSLionel Sambuc 668*f4a2713aSLionel Sambuc module Conflicts { 669*f4a2713aSLionel Sambuc explicit module A { 670*f4a2713aSLionel Sambuc header "conflict_a.h" 671*f4a2713aSLionel Sambuc conflict B, "we just don't like B" 672*f4a2713aSLionel Sambuc } 673*f4a2713aSLionel Sambuc 674*f4a2713aSLionel Sambuc module B { 675*f4a2713aSLionel Sambuc header "conflict_b.h" 676*f4a2713aSLionel Sambuc } 677*f4a2713aSLionel Sambuc } 678*f4a2713aSLionel Sambuc 679*f4a2713aSLionel Sambuc 680*f4a2713aSLionel SambucAttributes 681*f4a2713aSLionel Sambuc---------- 682*f4a2713aSLionel 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. 683*f4a2713aSLionel Sambuc 684*f4a2713aSLionel Sambuc.. parsed-literal:: 685*f4a2713aSLionel Sambuc 686*f4a2713aSLionel Sambuc *attributes*: 687*f4a2713aSLionel Sambuc *attribute* *attributes*:sub:`opt` 688*f4a2713aSLionel Sambuc 689*f4a2713aSLionel Sambuc *attribute*: 690*f4a2713aSLionel Sambuc '[' *identifier* ']' 691*f4a2713aSLionel Sambuc 692*f4a2713aSLionel SambucAny *identifier* can be used as an attribute, and each declaration specifies what attributes can be applied to it. 693*f4a2713aSLionel Sambuc 694*f4a2713aSLionel SambucModularizing a Platform 695*f4a2713aSLionel Sambuc======================= 696*f4a2713aSLionel 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). 697*f4a2713aSLionel Sambuc 698*f4a2713aSLionel 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. 699*f4a2713aSLionel Sambuc 700*f4a2713aSLionel Sambuc**Macro-guarded copy-and-pasted definitions** 701*f4a2713aSLionel 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: 702*f4a2713aSLionel Sambuc 703*f4a2713aSLionel Sambuc .. parsed-literal:: 704*f4a2713aSLionel Sambuc 705*f4a2713aSLionel Sambuc #ifndef _SIZE_T 706*f4a2713aSLionel Sambuc #define _SIZE_T 707*f4a2713aSLionel Sambuc typedef __SIZE_TYPE__ size_t; 708*f4a2713aSLionel Sambuc #endif 709*f4a2713aSLionel Sambuc 710*f4a2713aSLionel 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. 711*f4a2713aSLionel Sambuc 712*f4a2713aSLionel Sambuc**Conflicting definitions** 713*f4a2713aSLionel 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). 714*f4a2713aSLionel Sambuc 715*f4a2713aSLionel Sambuc**Missing includes** 716*f4a2713aSLionel 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. 717*f4a2713aSLionel Sambuc 718*f4a2713aSLionel Sambuc**Headers that vend multiple APIs at different times** 719*f4a2713aSLionel 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. 720*f4a2713aSLionel Sambuc 721*f4a2713aSLionel 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. 722*f4a2713aSLionel Sambuc 723*f4a2713aSLionel SambucFuture Directions 724*f4a2713aSLionel Sambuc================= 725*f4a2713aSLionel SambucModules is an experimental feature, and there is much work left to do to make it both real and useful. Here are a few ideas: 726*f4a2713aSLionel Sambuc 727*f4a2713aSLionel Sambuc**Detect unused module imports** 728*f4a2713aSLionel 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. 729*f4a2713aSLionel Sambuc 730*f4a2713aSLionel Sambuc**Fix-Its for missing imports** 731*f4a2713aSLionel 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 should detect such cases and auto-import the required module (with a Fix-It!). 732*f4a2713aSLionel Sambuc 733*f4a2713aSLionel Sambuc**Improve modularize** 734*f4a2713aSLionel 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. 735*f4a2713aSLionel Sambuc 736*f4a2713aSLionel Sambuc**C++ Support** 737*f4a2713aSLionel Sambuc Modules clearly has to work for C++, or we'll never get to use it for the Clang code base. 738*f4a2713aSLionel Sambuc 739*f4a2713aSLionel SambucWhere To Learn More About Modules 740*f4a2713aSLionel Sambuc================================= 741*f4a2713aSLionel SambucThe Clang source code provides additional information about modules: 742*f4a2713aSLionel Sambuc 743*f4a2713aSLionel Sambuc``clang/lib/Headers/module.map`` 744*f4a2713aSLionel Sambuc Module map for Clang's compiler-specific header files. 745*f4a2713aSLionel Sambuc 746*f4a2713aSLionel Sambuc``clang/test/Modules/`` 747*f4a2713aSLionel Sambuc Tests specifically related to modules functionality. 748*f4a2713aSLionel Sambuc 749*f4a2713aSLionel Sambuc``clang/include/clang/Basic/Module.h`` 750*f4a2713aSLionel Sambuc The ``Module`` class in this header describes a module, and is used throughout the compiler to implement modules. 751*f4a2713aSLionel Sambuc 752*f4a2713aSLionel Sambuc``clang/include/clang/Lex/ModuleMap.h`` 753*f4a2713aSLionel 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). 754*f4a2713aSLionel Sambuc 755*f4a2713aSLionel SambucPCHInternals_ 756*f4a2713aSLionel Sambuc Information about the serialized AST format used for precompiled headers and modules. The actual implementation is in the ``clangSerialization`` library. 757*f4a2713aSLionel Sambuc 758*f4a2713aSLionel Sambuc.. [#] Automatic linking against the libraries of modules requires specific linker support, which is not widely available. 759*f4a2713aSLionel Sambuc 760*f4a2713aSLionel Sambuc.. [#] Modules are only available in C and Objective-C; a separate flag ``-fcxx-modules`` enables modules support for C++, which is even more experimental and broken. 761*f4a2713aSLionel Sambuc 762*f4a2713aSLionel 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. 763*f4a2713aSLionel Sambuc 764*f4a2713aSLionel 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. 765*f4a2713aSLionel Sambuc 766*f4a2713aSLionel 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. 767*f4a2713aSLionel Sambuc 768*f4a2713aSLionel Sambuc.. _PCHInternals: PCHInternals.html 769*f4a2713aSLionel Sambuc 770