xref: /llvm-project/flang/docs/ModFiles.md (revision 038b42ba5b47b1aa2d47ef5706a713f6bfbbc37c)
1932aae77SSourabh Singh Tomar<!--===- docs/ModFiles.md
2932aae77SSourabh Singh Tomar
3932aae77SSourabh Singh Tomar   Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4932aae77SSourabh Singh Tomar   See https://llvm.org/LICENSE.txt for license information.
5932aae77SSourabh Singh Tomar   SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6932aae77SSourabh Singh Tomar
7932aae77SSourabh Singh Tomar-->
8932aae77SSourabh Singh Tomar
9eaff2004Ssameeran joshi# Module Files
10eaff2004Ssameeran joshi
11b7ff0320Scor3ntin```{contents}
12b7ff0320Scor3ntin---
13b7ff0320Scor3ntinlocal:
14b7ff0320Scor3ntin---
15271a7bb1SRichard Barton```
16271a7bb1SRichard Barton
17eaff2004Ssameeran joshiModule files hold information from a module that is necessary to compile
18eaff2004Ssameeran joshiprogram units that depend on the module.
19eaff2004Ssameeran joshi
20eaff2004Ssameeran joshi## Name
21eaff2004Ssameeran joshi
22eaff2004Ssameeran joshiModule files must be searchable by module name. They are typically named
23eaff2004Ssameeran joshi`<modulename>.mod`. The advantage of using `.mod` is that it is consistent with
24eaff2004Ssameeran joshiother compilers so users will know what they are. Also, makefiles and scripts
25eaff2004Ssameeran joshioften use `rm *.mod` to clean up.
26eaff2004Ssameeran joshi
27eaff2004Ssameeran joshiThe disadvantage of using the same name as other compilers is that it is not
28eaff2004Ssameeran joshiclear which compiler created a `.mod` file and files from multiple compilers
29eaff2004Ssameeran joshicannot be in the same directory. This could be solved by adding something
30a1cd5e69SMichael Klemmbetween the module name and extension, e.g. `<modulename>-f18.mod`.  If this
31a1cd5e69SMichael Klemmis needed, Flang's fc1 accepts the option `-module-suffix` to alter the suffix
32a1cd5e69SMichael Klemmused for the module file.
33eaff2004Ssameeran joshi
34eaff2004Ssameeran joshi## Format
35eaff2004Ssameeran joshi
36eaff2004Ssameeran joshiModule files will be Fortran source.
37eaff2004Ssameeran joshiDeclarations of all visible entities will be included, along with private
38eaff2004Ssameeran joshientities that they depend on.
39eaff2004Ssameeran joshiEntity declarations that span multiple statements will be collapsed into
40eaff2004Ssameeran joshia single *type-declaration-statement*.
41eaff2004Ssameeran joshiExecutable statements will be omitted.
42eaff2004Ssameeran joshi
43eaff2004Ssameeran joshi### Header
44eaff2004Ssameeran joshi
45eaff2004Ssameeran joshiThere will be a header containing extra information that cannot be expressed
46eaff2004Ssameeran joshiin Fortran. This will take the form of a comment or directive
47eaff2004Ssameeran joshiat the beginning of the file.
48eaff2004Ssameeran joshi
49eaff2004Ssameeran joshiIf it's a comment, the module file reader would have to strip it out and
50eaff2004Ssameeran joshiperform *ad hoc* parsing on it. If it's a directive the compiler could
51eaff2004Ssameeran joshiparse it like other directives as part of the grammar.
52eaff2004Ssameeran joshiProcessing the header before parsing might result in better error messages
53eaff2004Ssameeran joshiwhen the `.mod` file is invalid.
54eaff2004Ssameeran joshi
55eaff2004Ssameeran joshiRegardless of whether the header is a comment or directive we can use the
56eaff2004Ssameeran joshisame string to introduce it: `!mod$`.
57eaff2004Ssameeran joshi
58eaff2004Ssameeran joshiInformation in the header:
59eaff2004Ssameeran joshi- Magic string to confirm it is an f18 `.mod` file
60eaff2004Ssameeran joshi- Version information: to indicate the version of the file format, in case it changes,
61eaff2004Ssameeran joshi  and the version of the compiler that wrote the file, for diagnostics.
62eaff2004Ssameeran joshi- Checksum of the body of the current file
63eaff2004Ssameeran joshi- Modules we depend on and the checksum of their module file when the current
64eaff2004Ssameeran joshi  module file is created
65eaff2004Ssameeran joshi- The source file that produced the `.mod` file? This could be used in error messages.
66eaff2004Ssameeran joshi
67eaff2004Ssameeran joshi### Body
68eaff2004Ssameeran joshi
69eaff2004Ssameeran joshiThe body will consist of minimal Fortran source for the required declarations.
70eaff2004Ssameeran joshiThe order will match the order they first appeared in the source.
71eaff2004Ssameeran joshi
72eaff2004Ssameeran joshiSome normalization will take place:
73eaff2004Ssameeran joshi- extraneous spaces will be removed
74eaff2004Ssameeran joshi- implicit types will be made explicit
75eaff2004Ssameeran joshi- attributes will be written in a consistent order
76eaff2004Ssameeran joshi- entity declarations will be combined into a single declaration
77eaff2004Ssameeran joshi- function return types specified in a *prefix-spec* will be replaced by
78eaff2004Ssameeran joshi  an entity declaration
79eaff2004Ssameeran joshi- etc.
80eaff2004Ssameeran joshi
81eaff2004Ssameeran joshi#### Symbols included
82eaff2004Ssameeran joshi
83eaff2004Ssameeran joshiAll public symbols from the module need to be included.
84eaff2004Ssameeran joshi
85eaff2004Ssameeran joshiIn addition, some private symbols are needed:
86eaff2004Ssameeran joshi- private types that appear in the public API
87eaff2004Ssameeran joshi- private components of non-private derived types
88eaff2004Ssameeran joshi- private parameters used in non-private declarations (initial values, kind parameters)
89eaff2004Ssameeran joshi- others?
90eaff2004Ssameeran joshi
91eaff2004Ssameeran joshiIt might be possible to anonymize private names if users don't want them exposed
92eaff2004Ssameeran joshiin the `.mod` file. (Currently they are readable in PGI `.mod` files.)
93eaff2004Ssameeran joshi
94eaff2004Ssameeran joshi#### USE association
95eaff2004Ssameeran joshi
96eaff2004Ssameeran joshiA module that contains `USE` statements needs them represented in the
97eaff2004Ssameeran joshi`.mod` file.
98eaff2004Ssameeran joshiEach use-associated symbol will be written as a separate *use-only* statement,
99eaff2004Ssameeran joshipossibly with renaming.
100eaff2004Ssameeran joshi
101eaff2004Ssameeran joshiAlternatives:
102eaff2004Ssameeran joshi- Emit a single `USE` for each module, listing all of the symbols that were
103eaff2004Ssameeran joshi  use-associated in the *only-list*.
104eaff2004Ssameeran joshi- Detect when all of the symbols from a module are imported (either by a *use-stmt*
105eaff2004Ssameeran joshi  without an *only-list* or because all of the public symbols of the module
106eaff2004Ssameeran joshi  have been listed in *only-list*s). In that case collapse them into a single *use-stmt*.
107eaff2004Ssameeran joshi- Emit the *use-stmt*s that appeared in the original source.
108eaff2004Ssameeran joshi
109eaff2004Ssameeran joshi## Reading and writing module files
110eaff2004Ssameeran joshi
111eaff2004Ssameeran joshi### Options
112eaff2004Ssameeran joshi
113eaff2004Ssameeran joshiThe compiler will have command-line options to specify where to search
114eaff2004Ssameeran joshifor module files and where to write them. By default it will be the current
115eaff2004Ssameeran joshidirectory for both.
116eaff2004Ssameeran joshi
117eaff2004Ssameeran joshiFor PGI, `-I` specifies directories to search for include files and module
118eaff2004Ssameeran joshifiles. `-module` specifics a directory to write module files in as well as to
119eaff2004Ssameeran joshisearch for them. gfortran is similar except it uses `-J` instead of `-module`.
120eaff2004Ssameeran joshi
121eaff2004Ssameeran joshiThe search order for module files is:
122eaff2004Ssameeran joshi1. The `-module` directory (Note: for gfortran the `-J` directory is not searched).
123eaff2004Ssameeran joshi2. The current directory
124eaff2004Ssameeran joshi3. The `-I` directories in the order they appear on the command line
125eaff2004Ssameeran joshi
126eaff2004Ssameeran joshi### Writing module files
127eaff2004Ssameeran joshi
128eaff2004Ssameeran joshiWhen writing a module file, if the existing one matches what would be written,
129eaff2004Ssameeran joshithe timestamp is not updated.
130eaff2004Ssameeran joshi
131eaff2004Ssameeran joshiModule files will be written after semantics, i.e. after the compiler has
132eaff2004Ssameeran joshidetermined the module is valid Fortran.<br>
133eaff2004Ssameeran joshi**NOTE:** PGI does create `.mod` files sometimes even when the module has a
134eaff2004Ssameeran joshicompilation error.
135eaff2004Ssameeran joshi
136eaff2004Ssameeran joshiQuestion: If the compiler can get far enough to determine it is compiling a module
137eaff2004Ssameeran joshibut then encounters an error, should it delete the existing `.mod` file?
138eaff2004Ssameeran joshiPGI does not, gfortran does.
139eaff2004Ssameeran joshi
140eaff2004Ssameeran joshi### Reading module files
141eaff2004Ssameeran joshi
142eaff2004Ssameeran joshiWhen the compiler finds a `.mod` file it needs to read, it firsts checks the first
143eaff2004Ssameeran joshiline and verifies it is a valid module file. It can also verify checksums of
144eaff2004Ssameeran joshimodules it depends on and report if they are out of date.
145eaff2004Ssameeran joshi
146eaff2004Ssameeran joshiIf the header is valid, the module file will be run through the parser and name
147eaff2004Ssameeran joshiresolution to recreate the symbols from the module. Once the symbol table is
148eaff2004Ssameeran joshipopulated the parse tree can be discarded.
149eaff2004Ssameeran joshi
150eaff2004Ssameeran joshiWhen processing `.mod` files we know they are valid Fortran with these properties:
151eaff2004Ssameeran joshi1. The input (without the header) is already in the "cooked input" format.
152eaff2004Ssameeran joshi2. No preprocessing is necessary.
153eaff2004Ssameeran joshi3. No errors can occur.
154eaff2004Ssameeran joshi
155eaff2004Ssameeran joshi## Error messages referring to modules
156eaff2004Ssameeran joshi
157eaff2004Ssameeran joshiWith this design, diagnostics can refer to names in modules and can emit a
158eaff2004Ssameeran joshinormalized declaration of an entity but not point to its location in the
159eaff2004Ssameeran joshisource.
160eaff2004Ssameeran joshi
161eaff2004Ssameeran joshiIf the header includes the source file it came from, that could be included in
162eaff2004Ssameeran joshia diagnostic but we still wouldn't have line numbers.
163eaff2004Ssameeran joshi
164eaff2004Ssameeran joshiTo provide line numbers and character positions or source lines as the user
165eaff2004Ssameeran joshiwrote them we would have to save some amount of provenance information in the
166eaff2004Ssameeran joshimodule file as well.
167*038b42baSPeter Klausler
168*038b42baSPeter Klausler## Hermetic modules files
169*038b42baSPeter Klausler
170*038b42baSPeter KlauslerTop-level module files for libraries can be build with `-fhermetic-module-files`.
171*038b42baSPeter KlauslerThis option causes these module files to contain copies of all of the non-intrinsic
172*038b42baSPeter Klauslermodules on which they depend, so that non-top-level local modules and the
173*038b42baSPeter Klauslermodules of dependent libraries need not also be packaged with the library.
174*038b42baSPeter KlauslerWhen the compiler reads a hermetic module file, the copies of the dependent
175*038b42baSPeter Klauslermodules are read into their own scope, and will not conflict with other modules
176*038b42baSPeter Klauslerof the same name that client code might `USE`.
177