Sign in Join
Wiki copytemporary

Macro internals: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Implementation of macros in FPC
22
33Compile time symbols (a k a defines), substitution macros and compile time variables, are all internally refered to as macros.
44
55Each macro is stored in an object of type TMacro. Macro objects belonging to the same scope are collected in a macro symbol table. When a macro is defined, a macro object is created. If it later on is undefined, it is not deleted, instead its *defined* flag is set to false. If again defined, its *defined* flag is set to true.
66
77Since several scopes of macro symbols might be in effect, their corresponding macro symbol tables are arranged in a macrosymbolstack, in a fashion in parallell with ordinary symbol tables.
88
99When a macro is used, the stack is searched top-down for the macro. If an existing macro is changed in any way, and the macro is not found on the top of the stack, a new macro object is created and put in the macrosymtable on the top of the stack. This is the case even if the macro is undefined, in which case the defined flag of the newly created macro object is set to false.
1010
1111Each module always has a localmacrosymtable. If its macros should be exported (happends in MODE MACPAS), it will also have a globalmacrosymtable. There is also the internalmacrosymtable, which is immutable, and is used by every module. It contains all predefined macros, both by the compiler and given on the command line.
1212
1313For a unit which do not export and import macros, the macrosymbolstack look like this:
1414
15-` <top>`\
16-` localmacrosymtable`\
17-` internalmacrosymtable`
15+```text
16+ <top>
17+ localmacrosymtable
18+ internalmacrosymtable
19+```
1820
1921And for a unit which do export and import macros, it look like this (at the end of the parsing):
2022
21-` <top>`\
22-` localmacrosymtable`\
23-` globalmacrosymtable`\
24-` globalmacrosymtables for used unit 1`\
25-` globalmacrosymtables for used unit 2`\
26-` ...`\
27-` internalmacrosymtable`
23+``` pascal
24+ <top>
25+ localmacrosymtable
26+ globalmacrosymtable
27+ globalmacrosymtables for used unit 1
28+ globalmacrosymtables for used unit 2
29+ ...
30+ internalmacrosymtable
31+```
2832
2933### System macros
3034
3135A system macro is a macro which is defined by the compiler, it often has FPC_ prepended to it. Although it is not a good programming practice, system macros can be overridden by the user.
3236
3337Most system macros are in initialsymboltable, but those which are specific to a unit are put in the localmacrosymtable of the unit instead. Examples are FPC_\<mode\> and FPC_THREADING.
3438
3539System macros are not exported, even if they are available in the interface part of a unit, although if they are redefined there, they *will* be exported.
3640
3741### Compilation of a module with regard to macros
3842
3943When a unit is about to be compiled, its localmacrosymtable is created, and it is pushed on the stack (which initially contains initialmacrosymtable). At this moment we do not know if macros should be exported.
4044
4145Macros should be exported if it is a) a unit and b) it is MODE MACPAS.
4246
4347Only one \$MODE \<xxx\> declaration is allowed when a module is parsed. This ensures that when a \$MODE \<xxx\> is encountered, we know for sure what the mode of the file should be. So, if a \$MODE MACPAS is encountered, the globalmacrosymtable is immediatelly created.
4448
4549If -Mmacpas is given on the command line, and no \$MODE has been encountered when UNIT is encountered, the globalmacrosymtable is created, and further \$MODE switch is forbidden.
4650
4751For other modes given on the command line, or if no mode is given, \$MODE is forbidden at the point where the first pascal construct is encountered after the UNIT or PROGRAM clause. In this case, or in all cases if it is a PROGRAM, the module has only a localmacrosymtable, and none of what is described below will happen. Thus the following apply to MODE MACPAS units only.
4852
4953Between the beginning of the file and until the globalmacrosymtable is created, the localmacrossymtable is available for macros which are declared by the user or for local system macros. They will of course not be exported.
5054
5155When there is a globalmacrosymtable, user macros are declared there, but system macros are declared in localmacrosymtable (so they are not exported). If some of the macros declared in the localmacrosymtable part is overriden, this is ok, and the override is then put in globalmacrosymtable.
5256
5357At IMPLEMENTATION the stack positions of globalmacrosymtable and localmacrotable is swapped. Macros existing in the localmacrosymtable, which has been overriden in globalmacrosymtable is deleted from localmacrosymtable (otherwise it would appear for the user as the macro sudenly has changed).
5458
5559*Caveat:* If \$MODE MACPAS is given before UNIT, the macros defined between this point and UNIT is exported, if the unit is compiled as part of a program or other unit, but is not exported if the unit is compiled from the commandline (at top level). Remedy: Do not put macros before UNIT.
5660
5761*NOTE:* In macpas, globalmacrosymtable is saved only if it contains anything.
5862
5963---
6064*From the Free Pascal wiki, page "Macro internals", as it stood on 2016-01-18 after a change by Skalogryz, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
6165Category: FPC internals

Back to history