Ignore:
Timestamp:
Jun 6, 2024, 5:50:33 PM (2 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
1967109, 405dbb3
Parents:
83b2fb5e
Message:

Update to the module system, folding in feedback and some PAB content.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/modules.md

    r83b2fb5e rbf64de3  
    55======================
    66
    7 Modules are a term for the base in separate compilation. Different languages have different ways to implement it, for C/C++ the module is the code/source file and usually header file.
     7In this proposal we will be descussing modules. Although their exact nature changes between programming languages, modules are the smallest unit of code reuse between programs, or the base unit in separate compilation. Modules, and the extended module system, will be tied up in various stages of compilation and execution, with a particular focus on visibility between different parts of the program.
     8
     9Note that terminology is not fixed across languages. For instance, some languages use the word package or library instead. Module was chosen as the generic term because it seems to have the least amount of other uses (for example, a package is sometimes a group of modules).
     10
     11In C there is no formal definition of module, but informally modules are a pair of files, the body file (.c) and the header (.h). The header provides the interface and the body file gives the implementation. (A translation unit is a source file, usually a .c file, and all the recursively included files.) Some modules, like the main module,
    812
    913Uses of Modules
    1014---------------
    11 The most straight forward purpose of modules is to enable separate compilation.
    12 This in turn reduces recompilation, by isolating changes, and parallel compilation, but making modules independent.
     15This section covers the features module system to allow for the separatation of code across modules and why
    1316
    14 An related feature is sharing information between modules. Information needed by other modules must be shared. However, avoiding sharing extra information can further isolating changes, and can also reduces the work of compiling a single module.
     17Modules are often, but not always, the means by which a language views source files. There is almost always some kind of parity between modules and source files, with modules being mapped onto one (or a few) source files. Sometimes the use of modules is used to find the approprate source files, requring this parity to be enforced in the language. Other times the parity is just a convention or is enforced for other reasons.
    1518
    16 Modules are also used as a base for other organizational features. Such as namespacing on module names, using the module as a space for visibility modifiers.
     19[]
     20
     21If there is a universal feature of modules, it is information visibility. Modules decide what information within them is visible to other modules. Here visibility is the course grained sense of "visible in another module for any purpose".
     22
     23[]
     24
     25Accessablity is the more fine grained partner to visibility, allowing for information to be visible, but only usable for certain purposes. This includes privacy and friendship - only usable in certain parts of the program - or inlining information - only usable by the optimizer.
     26
     27In languages that have namespacing/qualified-names, modules will often interact with namespaces. Such as each module placing its declarations in a namespace of the same name.
    1728
    1829C Comparisons
     
    8091Second, this does nothing to solve the oversized header issue. It does not reduce any requirements on what includes need to be use.
    8192
     93Alternate Solutions
     94-------------------
     95There are other ways C's modules could be improved in Cforall.
     96
     97Explicit Module Blocks
     98......................
     99Instead of trying map files to modules, they could instead be declared explicitly. Marking out the beginning and the end of a section of code as a module. If built on top of the body/header and include system might look like this.
     100
     101>   extern module NAME {
     102>       BODY
     103>   }
     104>
     105>   module NAME {
     106>       BODY
     107>   }
     108
     109The extern module goes in the header, the other module goes in the body. The basic usage is the forward declarations in the header module and the body contains the definitions. It can be used to check that the two sets match, but on its own it is only replicates the current header/body divide with a bit more explicit syntax. However, it can be used as the base for a lot of features of the module linkage system. It does solve the "knowning two declarations came from the same other module" problem (and could work with namespaces) but is otherwise very similar for a heavier syntax.
     110
     111Compiled Headers
     112................
     113Most programming languages do not share source code between modules. Instead each module is compiled without looking at the source code in other modules. The result of compilation includes all the information required for later stages of compilation and information for compiling other modules.
     114
     115This is a more popular pattern more recent programming languages. It does have some advantages, such as reducing the amount of times that a file will need to be processed and can cut out unneeded transitive information. It is downsides include adding dependences between modules and it prevents any circlar dependences between modules.
     116
     117There is one other notable downside, and that is retrofitting this pattern on top of C. The problems with GCC precompiled headers and C++ modules give some indication of how tricky the situation is. The problem is the C pre-processor, not only is this the tool by which modules are implemented, but they contain information for the preprocessor itself, such as macros. Macro definitions must also be applied to the text of source files and so must be preserved. This might be possible in cases with strict dependences from the included file, but there are more unusual uses where macros depend on their context (previous includes or a define before the include) in their definition and these would almost imposible to translate over.
    82118
    83119##########################################################################################
Note: See TracChangeset for help on using the changeset viewer.