Skip to content

Commit da6f493

Browse files
authored
[flang][NFC] Update module file documentation (#135107)
The current module file documentation antedates the current implementation of module files and contains many aspirational and conditional statements, all of which can now be resolved with descriptions of how things actually work.
1 parent fcf0f81 commit da6f493

File tree

1 file changed

+85
-95
lines changed

1 file changed

+85
-95
lines changed

flang/docs/ModFiles.md

Lines changed: 85 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -14,138 +14,136 @@ local:
1414
---
1515
```
1616

17-
Module files hold information from a module that is necessary to compile
18-
program units that depend on the module.
17+
Module files hold information from a module (or submodule) that is
18+
necessary to compile program units in other source files that depend on that module.
19+
Program units in the same source file as the module do not read
20+
module files, as this compiler parses entire source files and processes
21+
the program units it contains in dependency order.
1922

2023
## Name
2124

22-
Module files must be searchable by module name. They are typically named
23-
`<modulename>.mod`. The advantage of using `.mod` is that it is consistent with
24-
other compilers so users will know what they are. Also, makefiles and scripts
25-
often use `rm *.mod` to clean up.
25+
Module files are named according to the module's name, suffixed with `.mod`.
26+
This is consistent with other compilers and expected by makefiles and
27+
other build systems.
28+
29+
Module files for submodules are named with their ancestor module's name
30+
as a prefix, separated by a hyphen.
31+
E.g., `module-submod.mod` is generated for submodule `submod' of module
32+
`module`.
33+
Some other compilers use a distinct filename suffix for submodules,
34+
but this one doesn't.
2635

2736
The disadvantage of using the same name as other compilers is that it is not
2837
clear which compiler created a `.mod` file and files from multiple compilers
29-
cannot be in the same directory. This could be solved by adding something
30-
between the module name and extension, e.g. `<modulename>-f18.mod`. If this
31-
is needed, Flang's fc1 accepts the option `-module-suffix` to alter the suffix
32-
used for the module file.
38+
cannot be in the same directory. This can be solved by adding something
39+
between the module name and extension, e.g. `<modulename>-f18.mod`. When
40+
this is needed, Flang accepts the option `-module-suffix` to alter the suffix.
3341

3442
## Format
3543

36-
Module files will be Fortran source.
37-
Declarations of all visible entities will be included, along with private
38-
entities that they depend on.
39-
Entity declarations that span multiple statements will be collapsed into
40-
a single *type-declaration-statement*.
41-
Executable statements will be omitted.
44+
Module files are Fortran free form source code.
45+
(One can, in principle, copy `foo.mod` into `tmp.f90`, recompile it,
46+
and obtain a matching `foo.mod` file.)
47+
They include the declarations of all visible locally defined entities along
48+
with the private entities on which they depend.
4249

4350
### Header
4451

45-
There will be a header containing extra information that cannot be expressed
46-
in Fortran. This will take the form of a comment or directive
47-
at the beginning of the file.
48-
49-
If it's a comment, the module file reader would have to strip it out and
50-
perform *ad hoc* parsing on it. If it's a directive the compiler could
51-
parse it like other directives as part of the grammar.
52-
Processing the header before parsing might result in better error messages
53-
when the `.mod` file is invalid.
54-
55-
Regardless of whether the header is a comment or directive we can use the
56-
same string to introduce it: `!mod$`.
52+
Module files begin with a UTF-8 byte order mark and a few lines of
53+
Fortran comments.
54+
(Pro tip: use `dd if=foo.mod bs=1 skip=3 2>/dev/null` to skip the byte order
55+
mark and dump the rest of the module.)
56+
The first comment begins `!mod$` and contains a version number
57+
and hash code.
58+
Further `!need$` comments contain the names and hash codes of other modules
59+
on which this module depends, and whether those modules are intrinsic
60+
or not to Fortran.
5761

58-
Information in the header:
59-
- Magic string to confirm it is an f18 `.mod` file
60-
- Version information: to indicate the version of the file format, in case it changes,
61-
and the version of the compiler that wrote the file, for diagnostics.
62-
- Checksum of the body of the current file
63-
- Modules we depend on and the checksum of their module file when the current
64-
module file is created
65-
- The source file that produced the `.mod` file? This could be used in error messages.
62+
The header comments do not contain timestamps or original source file paths.
6663

6764
### Body
6865

69-
The body will consist of minimal Fortran source for the required declarations.
70-
The order will match the order they first appeared in the source.
71-
72-
Some normalization will take place:
73-
- extraneous spaces will be removed
74-
- implicit types will be made explicit
75-
- attributes will be written in a consistent order
76-
- entity declarations will be combined into a single declaration
77-
- function return types specified in a *prefix-spec* will be replaced by
78-
an entity declaration
79-
- etc.
66+
The body comprises minimal Fortran source for the required declarations.
67+
Their order generally matches the order they appeared in the original
68+
source code for the module.
69+
All types are explicit, and all non-character literal constants are
70+
marked with explicit kind values.
71+
72+
Declarations of objects, interfaces, types, and other entities are
73+
regenerated from the compiler's symbol table.
74+
So entity declarations that spanned multiple statements in the source
75+
program are effectively collapsed into a single *type-declaration-statement*.
76+
Constant expressions that appear in initializers, bounds, and other sites
77+
appear in the module file as their folded values.
78+
Any compiler directives (`!omp$`, `!acc$`, &c.) relevant to the declarations
79+
of names are also included in the module file.
80+
81+
Executable statements are omitted.
82+
If we ever want to do Fortran-level inline expansion of procedures
83+
in the future,
84+
we will have to "unparse" the executable parts of their definitions.
8085

8186
#### Symbols included
8287

83-
All public symbols from the module need to be included.
88+
All public symbols from the module are included.
8489

8590
In addition, some private symbols are needed:
8691
- private types that appear in the public API
8792
- private components of non-private derived types
8893
- private parameters used in non-private declarations (initial values, kind parameters)
89-
- others?
90-
91-
It might be possible to anonymize private names if users don't want them exposed
92-
in the `.mod` file. (Currently they are readable in PGI `.mod` files.)
9394

9495
#### USE association
9596

96-
A module that contains `USE` statements needs them represented in the
97-
`.mod` file.
98-
Each use-associated symbol will be written as a separate *use-only* statement,
99-
possibly with renaming.
100-
101-
Alternatives:
102-
- Emit a single `USE` for each module, listing all of the symbols that were
103-
use-associated in the *only-list*.
104-
- Detect when all of the symbols from a module are imported (either by a *use-stmt*
105-
without an *only-list* or because all of the public symbols of the module
106-
have been listed in *only-list*s). In that case collapse them into a single *use-stmt*.
107-
- Emit the *use-stmt*s that appeared in the original source.
97+
Entities that have been included in a module by means of USE association
98+
are represented in the module file with `USE` statements.
99+
Name aliases are sometimes necessary when an entity from another
100+
module is needed for a declaration and conflicts with another
101+
entity of the same name.
108102

109103
## Reading and writing module files
110104

111105
### Options
112106

113-
The compiler will have command-line options to specify where to search
107+
The compiler has command-line options to specify where to search
114108
for module files and where to write them. By default it will be the current
115109
directory for both.
116110

117-
For PGI, `-I` specifies directories to search for include files and module
118-
files. `-module` specifics a directory to write module files in as well as to
119-
search for them. gfortran is similar except it uses `-J` instead of `-module`.
111+
`-I` specifies directories to search for include files and module
112+
files.
113+
`-J`, and its alias `-module-dir`, specify a directory into which module files are written
114+
as well as to search for them.
120115

121-
The search order for module files is:
122-
1. The `-module` directory (Note: for gfortran the `-J` directory is not searched).
123-
2. The current directory
124-
3. The `-I` directories in the order they appear on the command line
116+
`-fintrinsic-modules-path` is available to specify an alternative location
117+
for Fortran's intrinsic modules.
125118

126119
### Writing module files
127120

128121
When writing a module file, if the existing one matches what would be written,
129122
the timestamp is not updated.
130123

131-
Module files will be written after semantics, i.e. after the compiler has
132-
determined the module is valid Fortran.<br>
133-
**NOTE:** PGI does create `.mod` files sometimes even when the module has a
134-
compilation error.
135-
136-
Question: If the compiler can get far enough to determine it is compiling a module
137-
but then encounters an error, should it delete the existing `.mod` file?
138-
PGI does not, gfortran does.
124+
Module files are written only after semantic analysis completes without
125+
a fatal error message.
139126

140127
### Reading module files
141128

142129
When the compiler finds a `.mod` file it needs to read, it firsts checks the first
143-
line and verifies it is a valid module file. It can also verify checksums of
144-
modules it depends on and report if they are out of date.
145-
146-
If the header is valid, the module file will be run through the parser and name
147-
resolution to recreate the symbols from the module. Once the symbol table is
148-
populated the parse tree can be discarded.
130+
line and verifies it is a valid module file.
131+
The header checksum must match the file's contents.
132+
(Pro tip: if a developer needs to hack the contents of a module file, they can
133+
recompile it afterwards as Fortran source to regenerate it with its new hash.)
134+
135+
The known hashes of dependent modules are used to disambiguate modules whose
136+
names match module files in multiple search directories, as well as to
137+
detect dependent modules whose recompilation has rendered a module file
138+
obsolete.
139+
140+
The hash codes used in module files also serve as a means of protection from
141+
updates to code in other packages.
142+
If a project A uses module files from package B, and package B is updated in
143+
a way that causes its module files to be updated, then the modules in A that
144+
depend on those modules in B will no longer be accepted for use until they
145+
have also been regenerated.
146+
This feature can catch errors that other compilers cannot.
149147

150148
When processing `.mod` files we know they are valid Fortran with these properties:
151149
1. The input (without the header) is already in the "cooked input" format.
@@ -155,15 +153,7 @@ When processing `.mod` files we know they are valid Fortran with these propertie
155153
## Error messages referring to modules
156154

157155
With this design, diagnostics can refer to names in modules and can emit a
158-
normalized declaration of an entity but not point to its location in the
159-
source.
160-
161-
If the header includes the source file it came from, that could be included in
162-
a diagnostic but we still wouldn't have line numbers.
163-
164-
To provide line numbers and character positions or source lines as the user
165-
wrote them we would have to save some amount of provenance information in the
166-
module file as well.
156+
normalized declaration of an entity.
167157

168158
## Hermetic modules files
169159

0 commit comments

Comments
 (0)