The parser: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: The parser
| Old | New | Line | |
|---|---|---|---|
| 1 | + | back to contents [[FPC internals]] | |
| 2 | + | ||
| 3 | + | # The parser | |
| 4 | + | ||
| 5 | + | *(last updated for fpc version 1.0.x)* | |
| 6 | + | ||
| 7 | + | The task of the parser is to read the token fed by the scanner, and make sure that the pascal syntax is respected. It also populates the symbol table, and creates the intermediate nodes (the tree) which will be used by the code generator. | |
| 8 | + | ||
| 9 | + | An overview of the parsing process, as well as its relationship with the tree the type checker and the code generator is shown in the following diagram: | |
| 10 | + | ||
| 11 | + | <http://www.pjh2.de/fpc/CompilerInternalsFigure08.png> | |
| 12 | + | ||
| 13 | + | ## Module information | |
| 14 | + | ||
| 15 | + | *(last updated for fpc version 1.0.x)* | |
| 16 | + | ||
| 17 | + | Each module being compiled, be it a library , unit or main program has some information which is required. This is stored in the tmodule object in memory. To avoid recompilation of already compiled module, the dependencies of the modules is stored in a PPU file, which makes it easier to determine which modules to recompile. | |
| 18 | + | ||
| 19 | + | ``` pascal | |
| 20 | + | type | |
| 21 | + | PModule = ^TModule; | |
| 22 | + | TModule = object(TLinkedList_Item) | |
| 23 | + | PPUFile: PPPUFile; // Pointer to PPU file object (unit file) | |
| 24 | + | Crc: Longint; // CRC-32 bit of the whole PPU file | |
| 25 | + | Interface_CRC: Longint; // CRC-32 bit of the interface part of | |
| 26 | + | // the PPU file | |
| 27 | + | Flags: Longint; // Unit file flags | |
| 28 | + | Compiled: Boolean; // TRUE if module is already compiled | |
| 29 | + | Do_Reload: Boolean; // TRUE if the PPU file must be reloaded | |
| 30 | + | Do_Assemble: Boolean; // Only assemble, don’t recompile unit | |
| 31 | + | Sources_Avail: Boolean; // TRUE if all sources of module are available | |
| 32 | + | Sources_Checked: Boolean; // TRUE if the sources has already been checked | |
| 33 | + | Is_Unit: Boolean; // TRUE if this is a unit (otherwise a library | |
| 34 | + | // or a main program) | |
| 35 | + | In_Compile: Boolean; // module is currently being recompiled | |
| 36 | + | In_Second_Compile: Boolean; // module is being compiled for second time | |
| 37 | + | In_Second_Load: Boolean; // module is being reloaded a second time | |
| 38 | + | In_Implementation: Boolean; // currently compiling implementation part | |
| 39 | + | // (units only) | |
| 40 | + | In_Global: Boolean; // currently compiling implementation part | |
| 41 | + | // (units only) | |
| 42 | + | Recompile_Reason: TRecompile_Reason; // Reason why module should be recompiled | |
| 43 | + | Islibrary: Boolean; // TRUE if this module is a shared library | |
| 44 | + | Map: PUnitMap; // Map of all used units for this unit | |
| 45 | + | Unitcount: Word; // Internal identifier of unit (for GDB support) | |
| 46 | + | Unit_index: Word; | |
| 47 | + | Globalsymtable: Pointer; // Symbol table for this module of externally | |
| 48 | + | // visible symbols | |
| 49 | + | Localsymtable: Pointer; // Symbol table for this module of locally | |
| 50 | + | // visible symbols | |
| 51 | + | Scanner: Pointer; // Scanner object pointer | |
| 52 | + | Loaded_From: PModule; // Module which referred to this module | |
| 53 | + | Uses_Imports: Boolean; // TRUE if this module imports symbols | |
| 54 | + | // from a shared library | |
| 55 | + | Imports: PLinkedList; // Linked list of imported symbols | |
| 56 | + | _Exports: PLinkedList; // Linked list of exported symbols (libraries only) | |
| 57 | + | SourceFiles: PFileManager; // List of all source files for this module | |
| 58 | + | ResourceFiles: TStringContainer; // List of all resource files for this module | |
| 59 | + | Used_Units: TLinkedList; // Information on units used by this module | |
| 60 | + | // (pused_unit) | |
| 61 | + | Dependent_Units: TLinkedList; | |
| 62 | + | LocalUnitSearchPath, // Search path for obtaining module source code | |
| 63 | + | LocalObjectSearchPath, | |
| 64 | + | LocalIncludeSearchPath, // Search path for includes for this module | |
| 65 | + | LocalLibrarySearchPath: TSearchPathList; | |
| 66 | + | Path: PString; // Path were module is located or created | |
| 67 | + | OutputPath: PString; // Path where object files (unit), | |
| 68 | + | // executable (program) or | |
| 69 | + | // shared library (library) is created | |
| 70 | + | ModuleName: PString; // Name of the module in uppercase | |
| 71 | + | ObjFileName: PString; // Full name of object file or executable file | |
| 72 | + | AsmFileName: PString; // Full name of the assembler file | |
| 73 | + | PPUFileName: PString; // Full name of the PPU file | |
| 74 | + | StaticLibFilename: PString; // Full name of the static library name | |
| 75 | + | // (used when smart linking is used) | |
| 76 | + | SharedLibFilename: PString; // Filename of the output shared library | |
| 77 | + | // (in the case of a library) | |
| 78 | + | ExeFileName: PString; // Filename of the output executable | |
| 79 | + | // (in the case of a program) | |
| 80 | + | AsmPrefix: PString; // Filename prefix of output assembler | |
| 81 | + | // files when using smartlinking | |
| 82 | + | MainSource: PString; // Name of the main source file | |
| 83 | + | end; | |
| 84 | + | ``` | |
| 85 | + | ||
| 86 | + | ## Parse types | |
| 87 | + | ||
| 88 | + | *(last updated for fpc version 1.0.x)* | |
| 89 | + | ||
| 90 | + | ### Entry | |
| 91 | + | ||
| 92 | + | ### program or library parsing | |
| 93 | + | ||
| 94 | + | ### unit parsing | |
| 95 | + | ||
| 96 | + | ### routine parsing | |
| 97 | + | ||
| 98 | + | ### label declarations | |
| 99 | + | ||
| 100 | + | ### constant declarations | |
| 101 | + | ||
| 102 | + | ### type declarations | |
| 103 | + | ||
| 104 | + | ### variable declarations | |
| 105 | + | ||
| 106 | + | ### thread variable declarations | |
| 107 | + | ||
| 108 | + | ### resource string declarations | |
| 109 | + | ||
| 110 | + | ### exports declaration | |
| 111 | + | ||
| 112 | + | ### expression parsing | |
| 113 | + | ||
| 114 | + | ### typed constant declarations | |
| 115 | + | ||
| 116 | + | ## Parser interface | |
| 117 | + | ||
| 118 | + | *(last updated for fpc version 1.0.x)* | |
| 119 | + | ||
| 120 | + | ### variables | |
| 121 | + | ||
| 122 | + | #### AktProcSym | |
| 123 | + | ||
| 124 | + | | | | | |
| 125 | + | |----|----| | |
| 126 | + | | Declaration: | `var`` AktProcSym: PProcSym;` | | |
| 127 | + | | Description: | Pointer to the symbol information for the routine currently being parsed. | | |
| 128 | + | ||
| 129 | + | #### LexLevel | |
| 130 | + | ||
| 131 | + | <tbody> | |
| 132 | + | ||
| 133 | + | 0 = for main program<br /> | |
| 134 | + | 1 = for subroutine<br /> | |
| 135 | + | 2 = for local / nested subroutines.</td> | |
| 136 | + | ||
| 137 | + | </tbody> | |
| 138 | + | ||
| 139 | + | #### Current_Module | |
| 140 | + | ||
| 141 | + | | | | | |
| 142 | + | |----|----| | |
| 143 | + | | Declaration: | `var`` Current_Module: PModule;` | | |
| 144 | + | | Description: | Information on the current module (program, library or unit) being compiled. | | |
| 145 | + | ||
| 146 | + | The following variables are default type definitions which are created each time compilation begins (default system-unit definitions), these definitions should always be valid: | |
| 147 | + | ||
| 148 | + | #### VoidDef | |
| 149 | + | ||
| 150 | + | | | | | |
| 151 | + | |--------------|-------------------------------------------------------------| | |
| 152 | + | | Declaration: | `var`` VoidDef: POrdDef;` | | |
| 153 | + | | Description: | Pointer to nothing type | | |
| 154 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 155 | + | ||
| 156 | + | #### cCharDef | |
| 157 | + | ||
| 158 | + | | | | | |
| 159 | + | |--------------|-------------------------------------------------------------| | |
| 160 | + | | Declaration: | `var`` cCharDef: POrdDef;` | | |
| 161 | + | | Description: | Type definition for a character (char) | | |
| 162 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 163 | + | ||
| 164 | + | #### cWideCharDef | |
| 165 | + | ||
| 166 | + | | | | | |
| 167 | + | |--------------|-------------------------------------------------------------| | |
| 168 | + | | Declaration: | `var`` cWideCharDef: POrdDef;` | | |
| 169 | + | | Description: | Type definition for a unicode character (WideChar) | | |
| 170 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 171 | + | ||
| 172 | + | #### BoolDef | |
| 173 | + | ||
| 174 | + | | | | | |
| 175 | + | |--------------|-------------------------------------------------------------| | |
| 176 | + | | Declaration: | `var`` BoolDef: POrdDef;` | | |
| 177 | + | | Description: | Type definition for a boolean value (boolean) | | |
| 178 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 179 | + | ||
| 180 | + | #### u8BitDef | |
| 181 | + | ||
| 182 | + | | | | | |
| 183 | + | |--------------|-------------------------------------------------------------| | |
| 184 | + | | Declaration: | `var`` u8BitDef: POrdDef;` | | |
| 185 | + | | Description: | Type definition for an 8-nit unsigned value (byte) | | |
| 186 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 187 | + | ||
| 188 | + | #### u16BitDef | |
| 189 | + | ||
| 190 | + | | | | | |
| 191 | + | |--------------|-------------------------------------------------------------| | |
| 192 | + | | Declaration: | `var`` u16BitDef: POrdDef;` | | |
| 193 | + | | Description: | Type definition for an unsigned 16-bit value (word) | | |
| 194 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 195 | + | ||
| 196 | + | #### u32BitDef | |
| 197 | + | ||
| 198 | + | | | | | |
| 199 | + | |--------------|-------------------------------------------------------------| | |
| 200 | + | | Declaration: | `var`` u32BitDef: POrdDef;` | | |
| 201 | + | | Description: | Type definition for an unsigned 32-bit value (cardinal) | | |
| 202 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 203 | + | ||
| 204 | + | #### s32BitDef | |
| 205 | + | ||
| 206 | + | | | | | |
| 207 | + | |--------------|-------------------------------------------------------------| | |
| 208 | + | | Declaration: | `var`` s32BitDef: POrdDef;` | | |
| 209 | + | | Description: | Type definition for a signed 32-bit value (Longint) | | |
| 210 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 211 | + | ||
| 212 | + | #### cu64BitDef | |
| 213 | + | ||
| 214 | + | | | | | |
| 215 | + | |--------------|-------------------------------------------------------------| | |
| 216 | + | | Declaration: | `var`` cu64BitDef: POrdDef;` | | |
| 217 | + | | Description: | Type definition for an unsigned 64-bit value (QWord) | | |
| 218 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 219 | + | ||
| 220 | + | #### cs64BitDef | |
| 221 | + | ||
| 222 | + | | | | | |
| 223 | + | |--------------|-------------------------------------------------------------| | |
| 224 | + | | Declaration: | `var`` cs64BitDef: POrdDef;` | | |
| 225 | + | | Description: | Type definition for a signed 64-bit value (Int64) | | |
| 226 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 227 | + | ||
| 228 | + | The following variables are default type definitions which are created each time compilation begins (default system-unit definitions), these definitions should always be valid: | |
| 229 | + | ||
| 230 | + | #### s64FloatDef | |
| 231 | + | ||
| 232 | + | | | | | |
| 233 | + | |----|----| | |
| 234 | + | | Declaration: | `var`` s64FloatDef: PFloatDef;` | | |
| 235 | + | | Description: | Type definition for a 64-bit IEEE floating point type (double) | | |
| 236 | + | | Notes: | This is loaded as a default supported type for the compiler. This might not actually really point to the double type if the cpu does not support it. | | |
| 237 | + | ||
| 238 | + | #### s32FloatDef | |
| 239 | + | ||
| 240 | + | | | | | |
| 241 | + | |----|----| | |
| 242 | + | | Declaration: | `var`` s32FloatDef: PFloatDef;` | | |
| 243 | + | | Description: | Type definition for a 32-bit IEEE floating point type (single) | | |
| 244 | + | | Notes: | This is loaded as a default supported type for the compiler. This might not actually really point to the single type if the cpu does not support it. | | |
| 245 | + | ||
| 246 | + | #### s80FloatDef | |
| 247 | + | ||
| 248 | + | | | | | |
| 249 | + | |----|----| | |
| 250 | + | | Declaration: | `var`` s80FloatDef : PFloatDef;` | | |
| 251 | + | | Description: | Type definition for an extended floating point type (extended) | | |
| 252 | + | | Notes: | This is loaded as a default supported type for the compiler. This might not actually really point to the extended type if the cpu does not support it. | | |
| 253 | + | ||
| 254 | + | #### s32FixedDef | |
| 255 | + | ||
| 256 | + | | | | | |
| 257 | + | |----|----| | |
| 258 | + | | Declaration: | `var`` s32FixedDef: PFloatDef;` | | |
| 259 | + | | Description: | Type definition for a fixed point 32-bit value (fixed) | | |
| 260 | + | | Notes: | This is loaded as a default supported type for the compiler. This is not supported officially in FPC 1.0 | | |
| 261 | + | ||
| 262 | + | The following variables are default type definitions which are created each time compilation begins (default system-unit definitions), these definitions should always be valid: | |
| 263 | + | ||
| 264 | + | #### cShortStringDef | |
| 265 | + | ||
| 266 | + | | | | | |
| 267 | + | |--------------|--------------------------------------------------------------| | |
| 268 | + | | Declaration: | `var`` cShortStringDef: PStringDef;` | | |
| 269 | + | | Description: | Type definition for a short string type (ShortString) | | |
| 270 | + | | Notes: | This is loaded as a default supported type for the compiler. | | |
| 271 | + | ||
| 272 | + | #### cLongStringDef | |
| 273 | + | ||
| 274 | + | | | | | |
| 275 | + | |--------------|--------------------------------------------------------------| | |
| 276 | + | | Declaration: | `var`` cLongStringDef: PStringDef;` | | |
| 277 | + | | Description: | Type definition for a long string type (LongString) | | |
| 278 | + | | Notes: | This is loaded as a default supported type for the compiler. | | |
| 279 | + | ||
| 280 | + | #### cAnsiStringDef | |
| 281 | + | ||
| 282 | + | | | | | |
| 283 | + | |--------------|--------------------------------------------------------------| | |
| 284 | + | | Declaration: | `var`` cAnsiStringDef: PStringDef;` | | |
| 285 | + | | Description: | Type definition for an ansistring type (AnsiString) | | |
| 286 | + | | Notes: | This is loaded as a default supported type for the compiler. | | |
| 287 | + | ||
| 288 | + | #### cWideStringDef | |
| 289 | + | ||
| 290 | + | | | | | |
| 291 | + | |--------------|--------------------------------------------------------------| | |
| 292 | + | | Declaration: | `var`` cWideStringDef: PStringDef;` | | |
| 293 | + | | Description: | Type definition for an wide string type (WideString) | | |
| 294 | + | | Notes: | This is loaded as a default supported type for the compiler. | | |
| 295 | + | ||
| 296 | + | #### OpenShortStringDef | |
| 297 | + | ||
| 298 | + | | | | | |
| 299 | + | |--------------|--------------------------------------------------------------| | |
| 300 | + | | Declaration: | `var`` OpenShortStringDef: PStringDef;` | | |
| 301 | + | | Description: | Type definition for an open string type (OpenString) | | |
| 302 | + | | Notes: | This is loaded as a default supported type for the compiler. | | |
| 303 | + | ||
| 304 | + | #### OpenCharArrayDef | |
| 305 | + | ||
| 306 | + | | | | | |
| 307 | + | |--------------|--------------------------------------------------------------| | |
| 308 | + | | Declaration: | `var`` OpenCharArrayDef: PArrayDef;` | | |
| 309 | + | | Description: | Type definition for an open char array type(OpenCharArray) | | |
| 310 | + | | Notes: | This is loaded as a default supported type for the compiler. | | |
| 311 | + | ||
| 312 | + | The following variables are default type definitions which are created each time compilation begins (default system-unit definitions), these definitions should always be valid: | |
| 313 | + | ||
| 314 | + | #### VoidPointerDef | |
| 315 | + | ||
| 316 | + | | | | | |
| 317 | + | |----|----| | |
| 318 | + | | Declaration: | `var`` VoidPointerDef: PPointerDef;` | | |
| 319 | + | | Description: | Type definition for a pointer which can point to anything (Pointer) | | |
| 320 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 321 | + | ||
| 322 | + | #### CharPointerDef | |
| 323 | + | ||
| 324 | + | | | | | |
| 325 | + | |----|----| | |
| 326 | + | | Declaration: | `var`` CharPointerDef: PPointerDef;` | | |
| 327 | + | | Description: | Type definition for a pointer which can point to characters (PChar) | | |
| 328 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 329 | + | ||
| 330 | + | #### VoidFarPointerDef | |
| 331 | + | ||
| 332 | + | | | | | |
| 333 | + | |----|----| | |
| 334 | + | | Declaration: | `var`` VoidFarPointerDef: PPointerDef;` | | |
| 335 | + | | Description: | Type definition for a pointer which can point to anything (intra-segment) (far Pointer) | | |
| 336 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 337 | + | ||
| 338 | + | #### cFormalDef | |
| 339 | + | ||
| 340 | + | | | | | |
| 341 | + | |--------------|-------------------------------------------------------------| | |
| 342 | + | | Declaration: | `var`` cFormalDef: PFormalDef;` | | |
| 343 | + | | Description: | | | |
| 344 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 345 | + | ||
| 346 | + | #### cfFileDef | |
| 347 | + | ||
| 348 | + | | | | | |
| 349 | + | |--------------|-------------------------------------------------------------| | |
| 350 | + | | Declaration: | `var`` cfFileDef: PFileDef;` | | |
| 351 | + | | Description: | This is the default file type (file) | | |
| 352 | + | | Notes: | This is loaded as a default supported type for the compiler | | |
| 353 | + | ||
| 354 | + | Next chapter: [[The inline assembler parser]] | |
| 355 | + | ||
| 356 | + | --- | |
| 357 | + | *From the Free Pascal wiki, page "The parser", as it stood on 2020-12-31 after a change by E-ric, 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).* | |
| 358 | + | Category: FPC internals |
