Sign in Join
Wiki copytemporary

Type information: what changed

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

Title: Type information

OldNewLine
1+Back to contents [[FPC internals]]
2+
3+# Type information
4+
5+## Architecture
6+
7+*(last updated for fpc version 1.0.x)*
8+
9+A type declaration , which is the basis for the symbol table, since inherently everything comes down to a type after parsing is a special structure with two principal fields, which point to a symbol table entry which is the type name, and the actual definition which gives the information on other symbols in the type, the size of the type and other such information.
10+
11+``` pascal
12+type
13+ TType = object
14+ Sym: PSym; // Points to the symbol table of this type
15+ Def: PDef; // Points to the actual definition of this type
16+ end;
17+```
18+
19+## Definition types
20+
21+*(last updated for fpc version 1.0.x)*
22+
23+Definitions represent the type information for all possible symbols which can be encountered by the parser. The definition types are associated with symbols in the symbol table, and are used by the parsing process (among other things) to perform type checking.
24+
25+The current possible definition types are enumerated in `TDefType` and can have one of the following symbolic values:
26+
27+<http://www.pjh2.de/fpc/CompilerInternalsFigure07.png>
28+
29+| deftype of TDef object | Description |
30+|------------------------|---------------------------------------------------|
31+| AbstractDef |   |
32+| ArrayDef | array type definition |
33+| RecordDef | record type definition |
34+| PointerDef | pointer type definition |
35+| OrdDef | ordinal (numeric value) type definition |
36+| StringDef | string type definition |
37+| EnumDef | enumeration type definition |
38+| ProcDef | procedure type definition |
39+| ObjectDef | object or class type definition |
40+| ErrorDef | error definition (empty, used for error recovery) |
41+| FileDef | file type definition |
42+| FormalDef |   |
43+| SetDef | set type definition |
44+| ProcVarDef | procedure variable type definition |
45+| FloatDef | floating point type definition |
46+| ClassrefDef |   |
47+| ForwardDef |   |
48+
49+### base definition (TDef)
50+
51+All type definitions are based on this object. Therefore all derived object all posess the fields in this object in addition to their own private fields.
52+
53+``` pascal
54+type
55+ PDef = ^TDef;
56+ TDef = object(TSymTableEntry)
57+ TypeSym: PTypeSym; // Pointer to symbol table entry for this type
58+ // definition
59+ InitTable_Label: PAsmLabel; // Label to initialization information (required
60+ // for some complex types)
61+ Rtti_Label: PAsmLabel; // Label to the runtime type information.
62+ NextGlobal: PDef;
63+ PreviousGlobal: PDef;
64+ SaveSize: Longint; // Size in bytes of the data definition
65+ DefType: TDefType; // Indicates the definition type (see table ??).
66+ Has_InitTable: Boolean;
67+ Has_Rtti: Boolean;
68+ Is_Def_Stab_Written: TDefStabStatus; // Can be one of the following states :
69+ // (Not_Written, written, Being_Written)
70+ // which indicates if the debug information
71+ // for this type has been defined or not.
72+ GlobalNb: Longint; // Internal stabs debug information type signature
73+ // (each type definition has a numeric
74+ // signature).
75+ end;
76+```
77+
78+### file definition (TFileDef)
79+
80+The file definition can occur in only some rare instances, when a `file of type` is parsed, a file definition of that type will be created. Furthermore, internally, a definition for a Text file type and untyped File type are created when the system unit is loaded. These types are always defined when compiling any unit or program.
81+
82+``` pascal
83+type
84+ PFileDef = ^TFileDef;
85+ TFileDef = object(TDef)
86+ FileTyp: TFileTyp; // Indicates what type of file definition it is
87+ // (text, untyped or typed).
88+ TypedFileType: TType; // In the case of a typed file definition,
89+ // definition of the type of the file
90+ end;
91+```
92+
93+### formal definition (TFormalDef)
94+
95+### forward definition (TForwardDef)
96+
97+The forward definition is created, when a type is declared before an actual definition exists. This is the case, when, for example `type PMyObject = TMyObject`, while `TMyObject` has yet to be defined.
98+
99+``` pascal
100+type
101+ PForwardDef = ^TForwardDef;
102+ TForwardDef = object(TDef)
103+ toSymName: String; // The symbol name for this forward declaration
104+ // (the actual real definition does not exist yet)
105+ ForwardPos: TFilePosInfo; // Indicates file position where this forward
106+ // definition was declared.
107+ end;
108+```
109+
110+### error definition (TErrorDef)
111+
112+This definition is actually an empty definition entry. When the parser encounters an error when parsing a definition instead of putting nothing in the type for a symbol, it puts this entry. This avoids illegal memory accesses later in parsing.
113+
114+### pointer definition (TPointerDef)
115+
116+The pointer definition is used for distinguishing between different types of pointers in the compiler, and are created at each `typename` parsing construct found.
117+
118+``` pascal
119+type
120+ PPointerDef = ^TPointerDef;
121+ TPointerDef = object(TDef)
122+ Is_Far: Boolean; // Used to indicate if this is a far pointer or not
123+ // (this flag is cpu-specific)
124+ PointerType: TType; // This indicates to what type definition this pointer
125+ // points to.
126+ end;
127+```
128+
129+### object definition (TObjectDef)
130+
131+The object definition is created each time an object declaration is found in the type declaration section.
132+
133+``` pascal
134+type
135+ PObjectDef = ^TObjectDef;
136+ TObjectDef = object(TDef)
137+ ChildOf: PObjectDef; // This is a pointer to the parent object
138+ // definition. It is set to nil, if
139+ // this object definition has no parent.
140+ ObjName: PString; // This is the object name
141+ SymTable: PSymTable; // This is a pointer to the symbol
142+ // table entries within this object.
143+ ObjectOptions: TObjectOptions; // The options for this object, see
144+ // the following table for the possible
145+ // options for the object.
146+ VMT_Offset: Longint; // This is the offset from the start
147+ // of the object image in memory
148+ // where the virtual method table is
149+ // located.
150+ Writing_Class_Record_Stab: Boolean;
151+ end;
152+```
153+
154+| Object Options(TObjectOptions) | Description |
155+|----|----|
156+| oo_is_class | This is a delphi styled class declaration, and not a Turbo Pascal object. |
157+| oo_is_forward | This flag is set to indicate that the object has been declared in a type section, but there is no implementation yet. |
158+| oo_has_virtual | This object / class contains virtual methods |
159+| oo_has_private | This object / class contains private fields or methods |
160+| oo_has_protected | This object / class contains protected fields or methods |
161+| oo_has_constructor | This object / class has a constructor method |
162+| oo_has_destructor | This object / class has a destructor method |
163+| oo_has_vmt | This object / class has a virtual method table |
164+| oo_has_msgstr | This object / class contains one or more message handlers |
165+| oo_has_msgint | This object / class contains one or more message handlers |
166+| oo_has_abstract | This object / class contains one or more abstract methods |
167+| oo_can_have_published | the class has runtime type information, i.e. you can publish properties |
168+| oo_cpp_class | the object/class uses an C++ compatible class layout |
169+| oo_interface | this class is a delphi styled interface |
170+
171+### class reference definition (TClassRefDef)
172+
173+### array definition (TArrayDef)
174+
175+This definition is created when an array type declaration is parsed. It contains all the information necessary for array type checking and code generation.
176+
177+``` pascal
178+type
179+ PArrayDef = ^TArrayDef;
180+ TArrayDef = object(TDef)
181+ IsVariant: Boolean;
182+ IsConstructor : Boolean;
183+ RangeNr: Longint; // Label number associated with the index values
184+ // when range checking is on
185+ LowRange : Longint; // The lower index range of the array definition
186+ HighRange : Longint; // The higher index range of the array definition
187+ ElementType : TType; // The type information for the elements of the array
188+ RangeType : TType; // The type information for the index ranges of the array
189+ IsArrayofConst : Boolean;
190+ end;
191+```
192+
193+### record definition (TRecordDef)
194+
195+The record definition entry is created each time a record type declaration is parsed. It contains the symbol table to the elements in the record.
196+
197+``` pascal
198+type
199+ PRecordDef = ^TRecordDef;
200+ TRecordDef = object(TDef)
201+ SymTable: PSymTable; // This is a pointer to the symbol table entries within
202+ // this record.
203+ end;
204+```
205+
206+### ordinal definition (TOrdDef)
207+
208+This type definition is the one used for all ordinal values such as char, bytes and other numeric integer type values. Some of the predefined type definitions are automatically created and loaded when the compiler starts. Others are created at compile time, when declared.
209+
210+``` pascal
211+type
212+ POrdDef = ^TOrdDef;
213+ TOrdDef = object(TDef)
214+ Low: Longint; // The minimum value of this ordinal type
215+ High: Longint; // The maximum value of this ordinal type
216+ Typ: TBaseType; // The type of ordinal value
217+ end;
218+```
219+
220+| Base ordinal type (TBaseType) | Description |
221+|----|----|
222+| uauto | user defined ordinal type definition |
223+| uvoid | Represents a void return value or node |
224+| uchar | ASCII character (1 byte) |
225+| u8bit | unsigned 8-bit value |
226+| u16bit | unsigned 16-bit value |
227+| u32bit | unsigned 32-bit value |
228+| s16bit | signed 16-bit value |
229+| s32bit | signed 32-bit value |
230+| bool8bit | boolean 8-bit value |
231+| bool16bit | boolean 16-bit value |
232+| bool32bit | boolean 32-bit value |
233+| u64bit | unsigned 64-bit value (not fully supported/tested) |
234+| s64bit | signed 64-bit value |
235+| uwidechar | Currently not supported and unused |
236+
237+### float definition (TFloatDef)
238+
239+This type definition is the one used for all floating point values such as SINGLE, DOUBLE. Some of the predefined type definitions are automatically created and loaded when the compiler starts.
240+
241+``` pascal
242+type
243+ PFloatDef = ^TFloatDef;
244+ TFloatDef = object(TDef)
245+ Typ: TFloatType; // The type of floating point value.
246+ end;
247+```
248+
249+| Base floating point type (TFloatType) | Description |
250+|----|----|
251+| s32real | IEEE Single precision floating point value |
252+| s64real | IEEE Double precision floating point value |
253+| s80real | Extended precision floating point value (cpu-specific, usually maps to double) |
254+| s64comp | 63-bit signed value, using 1 bit for sign indication |
255+| f16bit | *Unsupported* |
256+| f32bit | *Unsupported* |
257+
258+### abstract procedure definition (tabstractprocdef)
259+
260+This is the base of all routine type definitions. This object is abstract, and is not directly used in a useful way. The derived object of this object are used for the actual parsing process.
261+
262+``` pascal
263+type
264+ PAbstractProcDef = ^TAbstractProcDef;
265+ TAbstractProcDef = object(TDef)
266+ SymtableLevel: Byte;
267+ Fpu_Used: Byte; // Number of floating point registers
268+ // used in this routine
269+ RetType: TType; // Type information for the return value
270+ // (uvoid if it returns nothing)
271+ ProcTypeOption: TProcTypeOption; // Indicates the type of routine it is.
272+ ProcCallOptions: TProcCallOptions; // Indicates the calling convention
273+ // of the routine.
274+ ProcOptions: TProcOptions; // Indicates general procedure options.
275+ Para: PLinkedList; // This is a linked list of parameters
276+ // (pparaitem list)
277+ end;
278+```
279+
280+| Procedure options (TProcTypeOption) | Description |
281+|----|----|
282+| poType_ProgInit | Routine is the program entry point (defined as 'main' in the compiler). |
283+| poType_UnitInit | Routine is the unit initialization code (defined as unitname_init in the compiler |
284+| poType_UnitFinalize | Routine is the unit exit code (defined as unitname_finalize in the compiler) |
285+| poType_Constructor | Routine is an object or class constructor |
286+| poType_Destructor | Routine is an object or class destructor |
287+| poType_Operator | Procedure is an operator |
288+
289+| call options (TProcCallOptions) | Description |
290+|----|----|
291+| pocall_clearstack | The routine caller clears the stack upon return |
292+| pocall_leftright | Send parameters to routine from left to right |
293+| pocall_cdecl | Passing parameters is done using the GCC alignment scheme, passing parameter values is directly copied into the stack space |
294+| pocall_register | unused (Send parameters via registers) |
295+| pocall_stdcall | Passing parameters is done using GCC alignment scheme, standard GCC registers are saved |
296+| pocall_safecall | Standard GCC registers are saved |
297+| pocall_palmsssyscall | This is a special syscall macro for embedded system |
298+| pocall_system | unused |
299+| pocall_inline | Routine is an inline assembler macro (not a true call) |
300+| pocall_internproc | System unit code generator helper routine |
301+| pocall_internconst | System unit code generator helper macro routine |
302+
303+| routine options (TProcOptions) | Description |
304+|----|----|
305+| po_classmethod | This is a class method |
306+| po_virtualmethod | This is a virtual method |
307+| po_abstractmethod | This is an abstract method |
308+| po_staticmethod | This is a static method |
309+| po_overridingmethod | This is an overriden method (with po_virtual flag usually) |
310+| po_methodpointer | This is a method pointer (not a normal routine pointer) |
311+| po_containsself | self is passed explicitly as a parameter to the method |
312+| po_interrupt | This routine is an interrupt handler |
313+| po_iocheck | IO checking should be done after a call to the procedure |
314+| po_assembler | The routine is in assembler |
315+| po_msgstr | method for string message handling |
316+| po_msgint | method for int message handling |
317+| po_exports | Routine has export directive |
318+| po_external | Routine is external (in other object or lib) |
319+| po_savestdregs | Routine entry should save all registers used by GCC |
320+| po_saveregisters | Routine entry should save all registers |
321+| po_overload | Routine is declared as being overloaded |
322+
323+### procedural variable definition (TProcVarDef)
324+
325+This definition is created when a procedure variable type is declared. It gives information on the type of a procedure, and is used when assigning and directly calling a routine through a pointer.
326+
327+``` pascal
328+type
329+ PProcVarDef = ^TProcVarDef;
330+ TProcVarDef = object(TAbstractProcDef)
331+ end;
332+```
333+
334+### procedure definition (TProcDef)
335+
336+When a procedure head is parsed, the definition of the routine is created. Thereafter, other fields containing information on the definition of the routine are populated as required.
337+
338+``` pascal
339+type
340+ PProcDef = ^TProcDef;
341+ TProcDef = object(TAbstractProcDef)
342+ ForwardDef: Boolean; // TRUE if this is a forward definition
343+ InterfaceDef: Boolean;
344+ ExtNumber: Longint;
345+ MessageInf: TMessageInf;
346+ NextOverloaded: PProcDef;
347+ FileInfo: TFilePosInfo; // Position in source code for the declaration of
348+ // this routine. Used for error management.
349+ Localst: PSymTable; // The local variables symbol table
350+ Parast: PSymTable; // The parameter symbol table
351+ ProcSym: PProcSym; // Points to owner of this definition
352+ LastRef: PRef;
353+ DefRef: PRef;
354+ CrossRef: PRef;
355+ LastWritten: PRef;
356+ RefCount: Longint;
357+ _Class: ProbjectDef;
358+ Code: Pointer; // The actual code for the routine
359+ // (only for inlined routines)
360+ UsedRegisters: TRegisterSet; // The set of registers used in this routine
361+ HasForward: Boolean;
362+ Count: Boolean;
363+ Is_Used: Boolean;
364+ end;
365+```
366+
367+### string definition (TStringDef)
368+
369+This definition represents all string types as well as derived types. Some of the default string type definitions are loaded when the compiler starts up. Others are created at compile time as they are declared with a specific length type.
370+
371+``` pascal
372+type
373+ PStringDef = ^TStringDef;
374+ TStringDef = object(TDef)
375+ String_Typ: TStringType; // Indicates the string type definition
376+ Len: Longint; // This is the maximum length which can have the string
377+ end;
378+```
379+
380+| String type (TStringType) | Description |
381+|----|----|
382+| st_default | Depends on current compiler switches, can either be a st_ShortString or st_AnsiString |
383+| st_shortstring | short string (length byte followed by actual ASCII characters (1 byte/char)) |
384+| st_longstring | long string (length longint followed by actual ASCII characters (1 byte/char)) |
385+| st_ansistring | long string garbage collected (pointer to a length, reference count followed by actual ASCII characters (1 byte/char)) |
386+| st_widestring | long string garbage collected (pointer to a length, reference count followed by actual unicode characters (1 word/char (utf16))) |
387+
388+### enumeration definition (TEnumDef)
389+
390+An enumeration definition is created each time an enumeration is declared and parsed. Each element in the enumeration will be added to the enumeration symtable and if \$SCOPEDENUMS directive is off then to the unit symtable also.
391+
392+``` pascal
393+type
394+ tenumdef = class(tstoreddef)
395+ has_jumps: Boolean; // True if enum has jumps between elements
396+ minval: aint; // Value of the first element in the enumeration
397+ maxval: aint; // Value of the last element in the enumeration
398+ BaseDef: tenumdef; // In the case where the enumeration is a
399+ // subrange of another enumeration or a unique
400+ // enumeration copied from the another, this gives
401+ // information on the base range of the elements
402+ symtable: TSymtable; // Enumeration symbol table - stores enumeration elements
403+ end;
404+```
405+
406+### set definition (TSetDef)
407+
408+This definition is created when a set type construct is parsed (set of declaration).
409+
410+``` pascal
411+type
412+ PSetDef = ^TSetDef;
413+ TSetDef = object(TDef)
414+ SetType: TSetType; Indicates the storage type of the set.
415+ ElementType: TType; Points the type definition and symbol
416+ table to the elements in the set.
417+ end;
418+```
419+
420+| set type (TSetType) | Description |
421+|----|----|
422+| NormSet | Normal set of up to 256 elements (32 byte storage space required) |
423+| SmallSet | Small set of up to 32 elements (4 byte storage space) |
424+| VarSet | Variable number of element set (storage size is dependent on number of elements) (currently unused and unsupported) |
425+
426+## Definition interface
427+
428+*(last updated for fpc version 1.0.x)*
429+
430+### routines
431+
432+#### TDef.Size
433+
434+| | |
435+|----|----|
436+| Declaration: | `function`` TDef.Size: Longint;` |
437+| Description: | This method returns the true size of the memory space required in bytes for this type definition (after alignment considerations). |
438+
439+#### TDef.Alignment
440+
441+| | |
442+|----|----|
443+| Declaration: | `function`` TDef.Alignment: Longint;` |
444+| Description: | This method returns the alignment of the data for complex types such as records and objects, otherwise returns 0 or 1 (no alignment). |
445+
446+Next chapter: [[The parser]]
447+
448+---
449+*From the Free Pascal wiki, page "Type information", as it stood on 2020-12-28 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).*

Back to history