--- local: ---
Module files hold information from a module that is necessary to compile program units that depend on the module.
Module files must be searchable by module name. They are typically named
<modulename>.mod. The advantage of using
.mod is that it is consistent with other compilers so users will know what they are. Also, makefiles and scripts often use
rm *.mod to clean up.
The disadvantage of using the same name as other compilers is that it is not clear which compiler created a
.mod file and files from multiple compilers cannot be in the same directory. This could be solved by adding something between the module name and extension, e.g.
Module files will be Fortran source. Declarations of all visible entities will be included, along with private entities that they depend on. Entity declarations that span multiple statements will be collapsed into a single type-declaration-statement. Executable statements will be omitted.
There will be a header containing extra information that cannot be expressed in Fortran. This will take the form of a comment or directive at the beginning of the file.
If it‘s a comment, the module file reader would have to strip it out and perform ad hoc parsing on it. If it’s a directive the compiler could parse it like other directives as part of the grammar. Processing the header before parsing might result in better error messages when the
.mod file is invalid.
Regardless of whether the header is a comment or directive we can use the same string to introduce it:
Information in the header:
.modfile? This could be used in error messages.
The body will consist of minimal Fortran source for the required declarations. The order will match the order they first appeared in the source.
Some normalization will take place:
All public symbols from the module need to be included.
In addition, some private symbols are needed:
It might be possible to anonymize private names if users don't want them exposed in the
.mod file. (Currently they are readable in PGI
A module that contains
USE statements needs them represented in the
.mod file. Each use-associated symbol will be written as a separate use-only statement, possibly with renaming.
USEfor each module, listing all of the symbols that were use-associated in the only-list.
The compiler will have command-line options to specify where to search for module files and where to write them. By default it will be the current directory for both.
-I specifies directories to search for include files and module files.
-module specifics a directory to write module files in as well as to search for them. gfortran is similar except it uses
-J instead of
The search order for module files is:
-moduledirectory (Note: for gfortran the
-Jdirectory is not searched).
-Idirectories in the order they appear on the command line
When writing a module file, if the existing one matches what would be written, the timestamp is not updated.
Module files will be written after semantics, i.e. after the compiler has determined the module is valid Fortran.
NOTE: PGI does create
.mod files sometimes even when the module has a compilation error.
Question: If the compiler can get far enough to determine it is compiling a module but then encounters an error, should it delete the existing
.mod file? PGI does not, gfortran does.
When the compiler finds a
.mod file it needs to read, it firsts checks the first line and verifies it is a valid module file. It can also verify checksums of modules it depends on and report if they are out of date.
If the header is valid, the module file will be run through the parser and name resolution to recreate the symbols from the module. Once the symbol table is populated the parse tree can be discarded.
.mod files we know they are valid Fortran with these properties:
With this design, diagnostics can refer to names in modules and can emit a normalized declaration of an entity but not point to its location in the source.
If the header includes the source file it came from, that could be included in a diagnostic but we still wouldn't have line numbers.
To provide line numbers and character positions or source lines as the user wrote them we would have to save some amount of provenance information in the module file as well.