Sign in Join
Wiki copytemporary

The parser: what changed

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

OldNewLine
11back to contents [[FPC internals]]
22
33# The parser
44
55*(last updated for fpc version 1.0.x)*
66
77The 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.
88
99An 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:
1010
1111<http://www.pjh2.de/fpc/CompilerInternalsFigure08.png>
1212
1313## Module information
1414
1515*(last updated for fpc version 1.0.x)*
1616
1717Each 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.
1818
1919``` pascal
2020type
2121 PModule = ^TModule;
2222 TModule = object(TLinkedList_Item)
2323 PPUFile: PPPUFile; // Pointer to PPU file object (unit file)
2424 Crc: Longint; // CRC-32 bit of the whole PPU file
2525 Interface_CRC: Longint; // CRC-32 bit of the interface part of
2626 // the PPU file
2727 Flags: Longint; // Unit file flags
2828 Compiled: Boolean; // TRUE if module is already compiled
2929 Do_Reload: Boolean; // TRUE if the PPU file must be reloaded
3030 Do_Assemble: Boolean; // Only assemble, don’t recompile unit
3131 Sources_Avail: Boolean; // TRUE if all sources of module are available
3232 Sources_Checked: Boolean; // TRUE if the sources has already been checked
3333 Is_Unit: Boolean; // TRUE if this is a unit (otherwise a library
3434 // or a main program)
3535 In_Compile: Boolean; // module is currently being recompiled
3636 In_Second_Compile: Boolean; // module is being compiled for second time
3737 In_Second_Load: Boolean; // module is being reloaded a second time
3838 In_Implementation: Boolean; // currently compiling implementation part
3939 // (units only)
4040 In_Global: Boolean; // currently compiling implementation part
4141 // (units only)
4242 Recompile_Reason: TRecompile_Reason; // Reason why module should be recompiled
4343 Islibrary: Boolean; // TRUE if this module is a shared library
4444 Map: PUnitMap; // Map of all used units for this unit
4545 Unitcount: Word; // Internal identifier of unit (for GDB support)
4646 Unit_index: Word;
4747 Globalsymtable: Pointer; // Symbol table for this module of externally
4848 // visible symbols
4949 Localsymtable: Pointer; // Symbol table for this module of locally
5050 // visible symbols
5151 Scanner: Pointer; // Scanner object pointer
5252 Loaded_From: PModule; // Module which referred to this module
5353 Uses_Imports: Boolean; // TRUE if this module imports symbols
5454 // from a shared library
5555 Imports: PLinkedList; // Linked list of imported symbols
5656 _Exports: PLinkedList; // Linked list of exported symbols (libraries only)
5757 SourceFiles: PFileManager; // List of all source files for this module
5858 ResourceFiles: TStringContainer; // List of all resource files for this module
5959 Used_Units: TLinkedList; // Information on units used by this module
6060 // (pused_unit)
6161 Dependent_Units: TLinkedList;
6262 LocalUnitSearchPath, // Search path for obtaining module source code
6363 LocalObjectSearchPath,
6464 LocalIncludeSearchPath, // Search path for includes for this module
6565 LocalLibrarySearchPath: TSearchPathList;
6666 Path: PString; // Path were module is located or created
6767 OutputPath: PString; // Path where object files (unit),
6868 // executable (program) or
6969 // shared library (library) is created
7070 ModuleName: PString; // Name of the module in uppercase
7171 ObjFileName: PString; // Full name of object file or executable file
7272 AsmFileName: PString; // Full name of the assembler file
7373 PPUFileName: PString; // Full name of the PPU file
7474 StaticLibFilename: PString; // Full name of the static library name
7575 // (used when smart linking is used)
7676 SharedLibFilename: PString; // Filename of the output shared library
7777 // (in the case of a library)
7878 ExeFileName: PString; // Filename of the output executable
7979 // (in the case of a program)
8080 AsmPrefix: PString; // Filename prefix of output assembler
8181 // files when using smartlinking
8282 MainSource: PString; // Name of the main source file
8383 end;
8484```
8585
8686## Parse types
8787
8888*(last updated for fpc version 1.0.x)*
8989
9090### Entry
9191
9292### program or library parsing
9393
9494### unit parsing
9595
9696### routine parsing
9797
9898### label declarations
9999
100100### constant declarations
101101
102102### type declarations
103103
104104### variable declarations
105105
106106### thread variable declarations
107107
108108### resource string declarations
109109
110110### exports declaration
111111
112112### expression parsing
113113
114114### typed constant declarations
115115
116116## Parser interface
117117
118118*(last updated for fpc version 1.0.x)*
119119
120120### variables
121121
122122#### AktProcSym
123123
124124| | |
125125|----|----|
126126| Declaration: | `var`` AktProcSym: PProcSym;` |
127127| Description: | Pointer to the symbol information for the routine currently being parsed. |
128128
129129#### LexLevel
130130
131-<tbody>
132-
133-0 = for main program<br />
134-1 = for subroutine<br />
135-2 = for local / nested subroutines.</td>
136-
137-</tbody>
131+| Declaration: | `var`` LexLevel: Longint;` |
132+|---|---|
133+| Description: | Level of code currently being parsed and compiled · 0 = for main program · 1 = for subroutine · 2 = for local / nested subroutines. |
138134
139135#### Current_Module
140136
141137| | |
142138|----|----|
143139| Declaration: | `var`` Current_Module: PModule;` |
144140| Description: | Information on the current module (program, library or unit) being compiled. |
145141
146142The following variables are default type definitions which are created each time compilation begins (default system-unit definitions), these definitions should always be valid:
147143
148144#### VoidDef
149145
150146| | |
151147|--------------|-------------------------------------------------------------|
152148| Declaration: | `var`` VoidDef: POrdDef;` |
153149| Description: | Pointer to nothing type |
154150| Notes: | This is loaded as a default supported type for the compiler |
155151
156152#### cCharDef
157153
158154| | |
159155|--------------|-------------------------------------------------------------|
160156| Declaration: | `var`` cCharDef: POrdDef;` |
161157| Description: | Type definition for a character (char) |
162158| Notes: | This is loaded as a default supported type for the compiler |
163159
164160#### cWideCharDef
165161
166162| | |
167163|--------------|-------------------------------------------------------------|
168164| Declaration: | `var`` cWideCharDef: POrdDef;` |
169165| Description: | Type definition for a unicode character (WideChar) |
170166| Notes: | This is loaded as a default supported type for the compiler |
171167
172168#### BoolDef
173169
174170| | |
175171|--------------|-------------------------------------------------------------|
176172| Declaration: | `var`` BoolDef: POrdDef;` |
177173| Description: | Type definition for a boolean value (boolean) |
178174| Notes: | This is loaded as a default supported type for the compiler |
179175
180176#### u8BitDef
181177
182178| | |
183179|--------------|-------------------------------------------------------------|
184180| Declaration: | `var`` u8BitDef: POrdDef;` |
185181| Description: | Type definition for an 8-nit unsigned value (byte) |
186182| Notes: | This is loaded as a default supported type for the compiler |
187183
188184#### u16BitDef
189185
190186| | |
191187|--------------|-------------------------------------------------------------|
192188| Declaration: | `var`` u16BitDef: POrdDef;` |
193189| Description: | Type definition for an unsigned 16-bit value (word) |
194190| Notes: | This is loaded as a default supported type for the compiler |
195191
196192#### u32BitDef
197193
198194| | |
199195|--------------|-------------------------------------------------------------|
200196| Declaration: | `var`` u32BitDef: POrdDef;` |
201197| Description: | Type definition for an unsigned 32-bit value (cardinal) |
202198| Notes: | This is loaded as a default supported type for the compiler |
203199
204200#### s32BitDef
205201
206202| | |
207203|--------------|-------------------------------------------------------------|
208204| Declaration: | `var`` s32BitDef: POrdDef;` |
209205| Description: | Type definition for a signed 32-bit value (Longint) |
210206| Notes: | This is loaded as a default supported type for the compiler |
211207
212208#### cu64BitDef
213209
214210| | |
215211|--------------|-------------------------------------------------------------|
216212| Declaration: | `var`` cu64BitDef: POrdDef;` |
217213| Description: | Type definition for an unsigned 64-bit value (QWord) |
218214| Notes: | This is loaded as a default supported type for the compiler |
219215
220216#### cs64BitDef
221217
222218| | |
223219|--------------|-------------------------------------------------------------|
224220| Declaration: | `var`` cs64BitDef: POrdDef;` |
225221| Description: | Type definition for a signed 64-bit value (Int64) |
226222| Notes: | This is loaded as a default supported type for the compiler |
227223
228224The following variables are default type definitions which are created each time compilation begins (default system-unit definitions), these definitions should always be valid:
229225
230226#### s64FloatDef
231227
232228| | |
233229|----|----|
234230| Declaration: | `var`` s64FloatDef: PFloatDef;` |
235231| Description: | Type definition for a 64-bit IEEE floating point type (double) |
236232| 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. |
237233
238234#### s32FloatDef
239235
240236| | |
241237|----|----|
242238| Declaration: | `var`` s32FloatDef: PFloatDef;` |
243239| Description: | Type definition for a 32-bit IEEE floating point type (single) |
244240| 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. |
245241
246242#### s80FloatDef
247243
248244| | |
249245|----|----|
250246| Declaration: | `var`` s80FloatDef : PFloatDef;` |
251247| Description: | Type definition for an extended floating point type (extended) |
252248| 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. |
253249
254250#### s32FixedDef
255251
256252| | |
257253|----|----|
258254| Declaration: | `var`` s32FixedDef: PFloatDef;` |
259255| Description: | Type definition for a fixed point 32-bit value (fixed) |
260256| Notes: | This is loaded as a default supported type for the compiler. This is not supported officially in FPC 1.0 |
261257
262258The following variables are default type definitions which are created each time compilation begins (default system-unit definitions), these definitions should always be valid:
263259
264260#### cShortStringDef
265261
266262| | |
267263|--------------|--------------------------------------------------------------|
268264| Declaration: | `var`` cShortStringDef: PStringDef;` |
269265| Description: | Type definition for a short string type (ShortString) |
270266| Notes: | This is loaded as a default supported type for the compiler. |
271267
272268#### cLongStringDef
273269
274270| | |
275271|--------------|--------------------------------------------------------------|
276272| Declaration: | `var`` cLongStringDef: PStringDef;` |
277273| Description: | Type definition for a long string type (LongString) |
278274| Notes: | This is loaded as a default supported type for the compiler. |
279275
280276#### cAnsiStringDef
281277
282278| | |
283279|--------------|--------------------------------------------------------------|
284280| Declaration: | `var`` cAnsiStringDef: PStringDef;` |
285281| Description: | Type definition for an ansistring type (AnsiString) |
286282| Notes: | This is loaded as a default supported type for the compiler. |
287283
288284#### cWideStringDef
289285
290286| | |
291287|--------------|--------------------------------------------------------------|
292288| Declaration: | `var`` cWideStringDef: PStringDef;` |
293289| Description: | Type definition for an wide string type (WideString) |
294290| Notes: | This is loaded as a default supported type for the compiler. |
295291
296292#### OpenShortStringDef
297293
298294| | |
299295|--------------|--------------------------------------------------------------|
300296| Declaration: | `var`` OpenShortStringDef: PStringDef;` |
301297| Description: | Type definition for an open string type (OpenString) |
302298| Notes: | This is loaded as a default supported type for the compiler. |
303299
304300#### OpenCharArrayDef
305301
306302| | |
307303|--------------|--------------------------------------------------------------|
308304| Declaration: | `var`` OpenCharArrayDef: PArrayDef;` |
309305| Description: | Type definition for an open char array type(OpenCharArray) |
310306| Notes: | This is loaded as a default supported type for the compiler. |
311307
312308The following variables are default type definitions which are created each time compilation begins (default system-unit definitions), these definitions should always be valid:
313309
314310#### VoidPointerDef
315311
316312| | |
317313|----|----|
318314| Declaration: | `var`` VoidPointerDef: PPointerDef;` |
319315| Description: | Type definition for a pointer which can point to anything (Pointer) |
320316| Notes: | This is loaded as a default supported type for the compiler |
321317
322318#### CharPointerDef
323319
324320| | |
325321|----|----|
326322| Declaration: | `var`` CharPointerDef: PPointerDef;` |
327323| Description: | Type definition for a pointer which can point to characters (PChar) |
328324| Notes: | This is loaded as a default supported type for the compiler |
329325
330326#### VoidFarPointerDef
331327
332328| | |
333329|----|----|
334330| Declaration: | `var`` VoidFarPointerDef: PPointerDef;` |
335331| Description: | Type definition for a pointer which can point to anything (intra-segment) (far Pointer) |
336332| Notes: | This is loaded as a default supported type for the compiler |
337333
338334#### cFormalDef
339335
340336| | |
341337|--------------|-------------------------------------------------------------|
342338| Declaration: | `var`` cFormalDef: PFormalDef;` |
343339| Description: |   |
344340| Notes: | This is loaded as a default supported type for the compiler |
345341
346342#### cfFileDef
347343
348344| | |
349345|--------------|-------------------------------------------------------------|
350346| Declaration: | `var`` cfFileDef: PFileDef;` |
351347| Description: | This is the default file type (file) |
352348| Notes: | This is loaded as a default supported type for the compiler |
353349
354350Next chapter: [[The inline assembler parser]]
355351
356352---
357353*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).*
358354Category: FPC internals

Back to history