-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtype.d
380 lines (307 loc) · 9.56 KB
/
type.d
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Written in the D programming language
// Author: Timon Gehr
// License: http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0
import std.array, std.algorithm, std.range, std.conv, std.string;
import lexer, parser, util;
import semantic, scope_, vrange, visitors;
import analyze;
import variant;
import rope_;
import std.traits : Unqual;
abstract class Type: Expression{ //Types can be part of Expressions and vice-versa
this(){type = get!void();}
private this(int){type = this;} // break infinite recursion for 'void'
override string toString(){return "Type";}
override @property string kind(){return "type";}
static BasicType get(T)() if(.isBasicType!T){
if(uniqueType!T) return uniqueType!T;
uniqueType!T=New!BasicType(Tok!(T.stringof));
uniqueType!T.sstate = SemState.completed;
return uniqueType!T;
}
static BasicType get(T:Cent)(){
if(uniqueType!T) return uniqueType!T;
uniqueType!T=New!BasicType(Tok!"cent");
uniqueType!T.sstate = SemState.completed;
return uniqueType!T;
}
static BasicType get(T:UCent)(){
if(uniqueType!T) return uniqueType!T;
uniqueType!T=New!BasicType(Tok!"ucent");
uniqueType!T.sstate = SemState.completed;
return uniqueType!T;
}
static Type get(T: T*)(){
if(uniqueType!(T*)) return uniqueType!(T*);
return uniqueType!(T*)=get!T().getPointer();
}
static Type get(T: T[])(){
if(uniqueType!(T[])) return uniqueType!(T[]);
return uniqueType!(T[])=get!T().getDynArr();
}
static Type get(T: T[N], ulong N)(){
if(uniqueType!(T[N])) return uniqueType!(T[N]);
return uniqueType!(T[N])=get!T().getArray(N);
}
static Type get(T: const(U),U)()if(!is(T==U)){
if(uniqueType!T) return uniqueType!T;
return uniqueType!T=get!U().getConst();
}
static Type get(T: immutable(T))(){
if(uniqueType!(immutable(T))) return uniqueType!(immutable(T));
return uniqueType!(immutable(T))=get!T().getImmutable();
}
static Type get(T: shared(T))(){
if(uniqueType!(shared(T))) return uniqueType!(shared(T));
return uniqueType!(shared(T))=get!T().getShared();
}
static Type get(T: inout(T))(){
if(uniqueType!(inout(T))) return uniqueType!(inout(T));
return uniqueType!(inout(T))=get!T().getInout();
}
static Type get(T)() if(!.isBasicType!T){
if(uniqueType!T) return uniqueType!T;
pragma(msg,"TODO: fix. do not know how to get type "~T.stringof);
return uniqueType!T=New!BasicType(Tok!"void"); // TODO: improve
}
static Type get(T:typeof(null))(){
if(uniqueType!T) return uniqueType!T;
return uniqueType!T=New!NullPtrTy();
}
static Type get(T:EmptyArray)(){ // typeof([])
if(uniqueType!T) return uniqueType!T;
return uniqueType!T=New!EmptyArrTy();
}
static Type get(T:Size_t)(){ // typeof((new int[]).length)
if(Size_t.size==4) return get!uint();
else if(Size_t.size==8) return get!ulong();
else assert(0,"TODO: gracefully melt down");
}
mixin DownCastMethod;
mixin DownCastMethods!(
BasicType,
QualifiedTy,
ConstTy,
ImmutableTy,
SharedTy,
InoutTy,
PointerTy,
DynArrTy,
ArrayTy,
FunctionTy,
DelegateTy,
AggregateTy,
EnumTy,
MatcherTy,
);
mixin Visitors;
protected:
static template uniqueType(T){
static if(.isBasicType!T||is(T==Cent)||is(T==UCent)) BasicType uniqueType;
else Type uniqueType;
}
Type[long] arrType;
}
class ErrorTy: Type{
this(){ sstate = SemState.error; }
override string toString(){return _brk("__error");}
mixin Visitors;
}
// dummy types:
//struct Null{}
struct EmptyArray{}
struct Struct{}
struct Class{}
struct Size_t{
static int size = 8;
static @property string suffix(){
return size==4?"U":size==8?"LU":(assert(0),"");
}
}
class NullPtrTy: Type{ // typeof(null)
this(){sstate = SemState.completed;}
override string toString(){return "typeof(null)";}
mixin Visitors;
}
class EmptyArrTy: Type{ // typeof([])
this(){sstate = SemState.completed;}
override string toString(){return "typeof([])";}
mixin Visitors;
}
enum VarArgs{
none,
cStyle,
dStyle,
}
class FunctionTy: Type{
STC stc;
Expression rret;
Type ret;
Parameter[] params;
VarArgs vararg;
this(STC stc, Expression retn,Parameter[] plist,VarArgs va){this.stc=stc&~STCauto; rret=retn; params=plist; vararg=va;}
override string toString(){return (rret&&!ret?rret.toString():ret?ret.toString():"auto")~pListToString();}
string pListToString(){
return "("~join(map!(to!string)(params),",")~(vararg==VarArgs.cStyle?(params.length?",":"")~"...)":vararg==VarArgs.dStyle?"...)":")")~STCtoString(stc);
}
mixin DownCastMethod;
mixin Visitors;
}
/+class FunctionPtr: Type{ // merged into PointerTy. TODO: invariant ft.rret !is null
FunctionTy ft;
this(FunctionTy ft)in{assert(ft !is null&&ft.rret !is null);}body{this.ft=ft;}
override string toString(){return ft.rret.toString()~" function"~ft.pListToString()~(ft.stc?" "~STCtoString(ft.stc):"");}
mixin Visitors;
}+/
class DelegateTy: Type{
FunctionTy ft;
this(FunctionTy ft)in{assert(ft !is null);}body{this.ft=ft;}
override string toString(){return (ft.rret&&!ft.ret?ft.rret.toString():ft.ret?ft.ret.toString:"auto")~" delegate"~ft.pListToString();}
mixin DownCastMethod;
mixin Visitors;
}
class TypeofExp: Type{
Expression e;
this(Expression exp){e=exp;}
override string toString(){return _brk("typeof("~e.toString()~")");}
mixin Visitors;
}
class TypeofReturnExp: Type{
override string toString(){return _brk("typeof(return)");}
mixin Visitors;
}
enum integralTypes = ["bool","byte","ubyte","short","ushort","int","uint","long","ulong","cent","ucent","char","wchar","dchar"];
enum basicTypes = integralTypes~["float","double","real","ifloat","idouble","ireal","cfloat","cdouble","creal","void"];
template isBasicType(T){
enum isBasicType=canFind(basicTypes,T.stringof);
}
import cent_;
template BasicTypeRep(string ty) if(basicTypes.canFind(ty)){
static if(ty=="cent") alias Cent BasicTypeRep;
else static if(ty=="ucent") alias UCent BasicTypeRep;
else mixin(`alias `~ty~` BasicTypeRep;`);
}
int basicTypeBitSize(TokenType op){
switch(op){
case Tok!"void":
case Tok!"bool", Tok!"char", Tok!"byte", Tok!"ubyte":
return 8;
case Tok!"wchar", Tok!"short", Tok!"ushort":
return 16;
case Tok!"dchar", Tok!"int", Tok!"uint":
return 32;
case Tok!"long", Tok!"ulong":
return 64;
case Tok!"cent", Tok!"ucent":
return 128;
case Tok!"float", Tok!"ifloat":
return 32;
case Tok!"double", Tok!"idouble":
return 64;
case Tok!"real", Tok!"ireal":
return 96; // TODO: this is architecture-dependent
case Tok!"cfloat": return 64;
case Tok!"cdouble": return 128;
case Tok!"creal": return 192; // TODO: verify
default: return -1;
}
}
bool basicTypeIsSigned(TokenType op){
switch(op){
case Tok!"bool", Tok!"ubyte", Tok!"ushort", Tok!"dchar", Tok!"uint", Tok!"ulong":
return false;
default: return true;
}
}
final class BasicType: Type{
TokenType op;
this(TokenType op)in{assert(strength[op]||op==Tok!"void");}body{
this.op=op;
if(op==Tok!"void") super(0);
else super();
}
override string toString(){return _brk(TokenTypeToString(op));}
mixin DownCastMethod;
mixin Visitors;
}
class PointerTy: Type{
Expression e;
this(Expression next)in{assert(next&&1);}body{e=next;}
override string toString(){
if(auto tt=e.isType())if(auto ft=tt.isFunctionTy()) return (ft.rret&&!ft.ret?ft.rret.toString():ft.ret?ft.ret.toString():"auto")~" function"~ft.pListToString();
return _brk(e.toString()~'*');
}
override PointerTy isPointerTy(){return this;}
mixin Visitors;
}
class DynArrTy: Type{
Expression e;
this(Expression next)in{assert(next&&1);}body{e=next;}
override string toString(){return _brk(e.toString()~"[]");}
override DynArrTy isDynArrTy(){return this;}
mixin Visitors;
}
// class SliceTy: DynArrTy{
// this(Expression next)in{assert(next&&1);}body{super(next);}
// override SliceTy isSliceTy(){return this;}
// mixin Visitors;
// }
class ArrayTy: Type{
Expression e;
ulong length;
this(Expression next, long len)in{assert(next&&1);}body{e=next; length=len;}
override string toString(){return _brk(e.toString()~'['~to!string(length)~']');}
override ArrayTy isArrayTy(){return this;}
mixin Visitors;
}
template QualifiedType(TokenType op){
static if(op==Tok!"const") alias ConstTy QualifiedType;
else static if(op==Tok!"immutable") alias ImmutableTy QualifiedType;
else static if(op==Tok!"shared") alias SharedTy QualifiedType;
else static if(op==Tok!"inout") alias InoutTy QualifiedType;
}
class QualifiedTy: Type{
Expression e;
mixin DownCastMethod;
}
class ConstTy: QualifiedTy{
this(Expression type){e=type;}
override string toString(){return _brk("const("~e.toString()~')');}
override ConstTy isConstTy(){return this;}
mixin Visitors;
}
class ImmutableTy: QualifiedTy{
this(Expression type){e=type;}
override string toString(){return _brk("immutable("~e.toString()~')');}
override ImmutableTy isImmutableTy(){return this;}
mixin Visitors;
}
class SharedTy: QualifiedTy{
this(Expression type){e=type;}
override string toString(){return _brk("shared("~e.toString()~')');}
override SharedTy isSharedTy(){return this;}
mixin Visitors;
}
class InoutTy: QualifiedTy{
this(Expression type){e=type;}
override string toString(){return _brk("inout("~e.toString()~')');}
override InoutTy isInoutTy(){return this;}
mixin Visitors;
}
class AggregateTy: Type{
AggregateDecl decl;
this(AggregateDecl decl){ this.decl = decl; sstate = SemState.completed; }
mixin DownCastMethod;
mixin Visitors;
mixin DeepDup!AggregateTy; // workaround for DMD bug. Should actually be in visitors.d
mixin CTFEInterpret!AggregateTy; // ditto
}
class EnumTy: Type{
EnumDecl decl;
this(EnumDecl decl)in{assert(decl&&decl.name);}body{
this.decl = decl;
sstate = SemState.completed;
}
mixin DownCastMethod;
mixin Visitors;
}