-
Notifications
You must be signed in to change notification settings - Fork 1
/
ntypes.pas
173 lines (140 loc) · 3.84 KB
/
ntypes.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
unit ntypes;
{$mode objfpc}{$H+}
interface
uses symtab;
var TypeNil,
TypeInteger,
TypeChar,
TypeBool,
TypeSet,
TypeReal,
TypeLongReal: TType;
function BaseType(a: PType): PType;
function Same(a,b: PType): boolean;
function AssignableTo(a,b: PType): boolean;
function Extends(a, b: PType): boolean;
function TypeSize(a: PType): longint;
function ArrayType(Alen: longint; ABase: PType): PType;
function IsByref(const f: TField): boolean;
function IsRecord(a: PType): boolean;
procedure AddField(a: PType; const AName: string; Exported, IsVar: boolean; TypDecl, Typ: PType);
function HasField(a: PType; const AName: string): boolean;
function FindField(a: PType; const AName: string; out Ofs: word): PType;
implementation
function BaseType(a: PType): PType;
begin
result := nil;
if a^.TypeKind = typRecord then
result := a^.BaseType;
end;
function Same(a, b: PType): boolean;
begin
result := (a = b);
end;
function AssignableTo(a, b: PType): boolean;
begin
result := same(a, b);
end;
function Extends(a, b: PType): boolean;
begin
if a = nil then exit(false);
result := Same(a,b) or
Extends(BaseType(a), b);
end;
function TypeSize(a: PType): longint;
var i: longint;
begin
result := 0;
if not assigned(a) then exit;
case a^.TypeKind of
typBasic: result := 1;
typArray: result := a^.ArrayLen*TypeSize(a^.BaseType);
typRecord:
begin
if assigned(a^.BaseType) then
result := sizeof(a^.BaseType)
else
result := 3;
for i := 0 to high(a^.Fields) do
inc(result, TypeSize(a^.Fields[i].Typ));
end;
typPointer,
typFunc: result := 1;
end;
end;
function ArrayType(Alen: longint; ABase: PType): PType;
begin
new(result);
result^.TypeKind := typArray;
result^.BaseType := ABase;
result^.ArrayLen := Alen;
end;
function IsByref(const f: TField): boolean;
begin
result := f.IsVar or
(f.Typ^.TypeKind = typRecord) or
(f.Typ^.TypeKind = typArray) or
(f.Typ^.TypeKind = typOpenArray);
end;
function IsRecord(a: PType): boolean;
begin
result := assigned(a) and
((a^.TypeKind = typRecord) or
(a^.TypeKind = typPointer));
end;
procedure AddField(a: PType; const AName: string; Exported, IsVar: boolean; TypDecl, Typ: PType);
begin
setlength(a^.Fields, high(a^.Fields)+2);
a^.Fields[high(a^.Fields)].Name := AName;
a^.Fields[high(a^.Fields)].IsVar := IsVar;
a^.Fields[high(a^.Fields)].Exported := Exported;
a^.Fields[high(a^.Fields)].Typ := Typ;
a^.Fields[high(a^.Fields)].Decl := TypDecl;
end;
function HasField(a: PType; const AName: string): boolean;
var i: longint;
begin
result := false;
for i := 0 to high(a^.Fields) do
if a^.Fields[i].Name = AName then
exit(true);
a := BaseType(a);
if not assigned(a) then
exit;
result := HasField(a, AName);
end;
function FindField(a: PType; const AName: string; out Ofs: word): PType;
var i: longint;
begin
result := nil;
if assigned(a^.BaseType) then
ofs := TypeSize(a^.BaseType)
else
ofs := 3;
for i := 0 to high(a^.Fields) do
begin
if a^.Fields[i].Name = AName then
exit(a^.Fields[i].Typ);
inc(ofs, TypeSize(a^.Fields[i].Typ));
end;
a := BaseType(a);
if not assigned(a) then exit;
result := FindField(a, AName, ofs);
end;
function BasicType(s: TSimpleKind): TType;
begin
result.TypeKind := typBasic;
result.SimpleKind := s;
result.BaseType := nil;
result.FuncRet := nil;
result.ArrayLen := 0;
end;
initialization
TypeNil := BasicType(skNil);
TypeInteger := BasicType(skInt);
TypeChar := BasicType(skChar);
TypeBool := BasicType(skBool);
TypeSet := BasicType(skSet);
TypeReal := BasicType(skReal);
TypeLongReal := BasicType(skLongReal);
end.