Mercurial > projects > dil
comparison src/dil/semantic/Types.d @ 806:bcb74c9b895c
Moved out files in the trunk folder to the root.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sun, 09 Mar 2008 00:12:19 +0100 |
parents | trunk/src/dil/semantic/Types.d@9e6c6bb73e5f |
children | 1d06b4aed7cf |
comparison
equal
deleted
inserted
replaced
805:a3fab8b74a7d | 806:bcb74c9b895c |
---|---|
1 /++ | |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
5 module dil.semantic.Types; | |
6 | |
7 import dil.semantic.Symbol; | |
8 import dil.semantic.TypesEnum; | |
9 import dil.lexer.Identifier; | |
10 import dil.CompilerInfo; | |
11 | |
12 /// The base type for all type structures. | |
13 abstract class Type/* : Symbol*/ | |
14 { | |
15 Type next; /// The next type in the type structure. | |
16 TYP tid; /// The ID of the type. | |
17 Symbol symbol; /// Not null if this type has a symbol. | |
18 | |
19 this(){} | |
20 | |
21 /// Constructs a Type object. | |
22 /// Params: | |
23 /// next = the type's next type. | |
24 /// tid = the type's ID. | |
25 this(Type next, TYP tid) | |
26 { | |
27 // this.sid = SYM.Type; | |
28 | |
29 this.next = next; | |
30 this.tid = tid; | |
31 } | |
32 | |
33 /// Returns a pointer type to this type. | |
34 TypePointer ptrTo() | |
35 { | |
36 return new TypePointer(this); | |
37 } | |
38 | |
39 /// Returns a dynamic array type using this type as its base. | |
40 TypeDArray arrayOf() | |
41 { | |
42 return new TypeDArray(this); | |
43 } | |
44 | |
45 /// Returns an associative array type using this type as its base. | |
46 /// Params: | |
47 /// key = the key type. | |
48 TypeAArray arrayOf(Type key) | |
49 { | |
50 return new TypeAArray(this, key); | |
51 } | |
52 | |
53 /// Returns the byte size of this type. | |
54 final size_t sizeOf() | |
55 { | |
56 return MITable.getSize(this); | |
57 } | |
58 | |
59 /// Size is not in MITable. Find out via virtual method. | |
60 size_t sizeOf_() | |
61 { | |
62 return sizeOf(); | |
63 } | |
64 | |
65 /// Returns true if this type has a symbol. | |
66 bool hasSymbol() | |
67 { | |
68 return symbol !is null; | |
69 } | |
70 } | |
71 | |
72 /// All basic types. E.g.: int, char, real etc. | |
73 class TypeBasic : Type | |
74 { | |
75 this(TYP typ) | |
76 { | |
77 super(null, typ); | |
78 } | |
79 } | |
80 | |
81 /// Dynamic array type. | |
82 class TypeDArray : Type | |
83 { | |
84 this(Type next) | |
85 { | |
86 super(next, TYP.DArray); | |
87 } | |
88 } | |
89 | |
90 /// Associative array type. | |
91 class TypeAArray : Type | |
92 { | |
93 Type keyType; | |
94 this(Type next, Type keyType) | |
95 { | |
96 super(next, TYP.AArray); | |
97 this.keyType = keyType; | |
98 } | |
99 } | |
100 | |
101 /// Static array type. | |
102 class TypeSArray : Type | |
103 { | |
104 size_t dimension; | |
105 this(Type next, size_t dimension) | |
106 { | |
107 super(next, TYP.SArray); | |
108 this.dimension = dimension; | |
109 } | |
110 } | |
111 | |
112 /// Pointer type. | |
113 class TypePointer : Type | |
114 { | |
115 this(Type next) | |
116 { | |
117 super(next, TYP.Pointer); | |
118 } | |
119 } | |
120 | |
121 /// Reference type. | |
122 class TypeReference : Type | |
123 { | |
124 this(Type next) | |
125 { | |
126 super(next, TYP.Reference); | |
127 } | |
128 } | |
129 | |
130 /// Enum type. | |
131 class TypeEnum : Type | |
132 { | |
133 this(Symbol symbol, Type baseType) | |
134 { | |
135 super(baseType, TYP.Enum); | |
136 this.symbol = symbol; | |
137 } | |
138 | |
139 Type baseType() | |
140 { | |
141 return next; | |
142 } | |
143 } | |
144 | |
145 /// Struct type. | |
146 class TypeStruct : Type | |
147 { | |
148 this(Symbol symbol) | |
149 { | |
150 super(null, TYP.Struct); | |
151 this.symbol = symbol; | |
152 } | |
153 } | |
154 | |
155 /// Class type. | |
156 class TypeClass : Type | |
157 { | |
158 this(Symbol symbol) | |
159 { | |
160 super(null, TYP.Class); | |
161 this.symbol = symbol; | |
162 } | |
163 } | |
164 | |
165 /// Typedef type. | |
166 class TypeTypedef : Type | |
167 { | |
168 this(Type next) | |
169 { | |
170 super(next, TYP.Typedef); | |
171 } | |
172 } | |
173 | |
174 /// Function type. | |
175 class TypeFunction : Type | |
176 { | |
177 this(Type next) | |
178 { | |
179 super(next, TYP.Function); | |
180 } | |
181 } | |
182 | |
183 /// Delegate type. | |
184 class TypeDelegate : Type | |
185 { | |
186 this(Type next) | |
187 { | |
188 super(next, TYP.Delegate); | |
189 } | |
190 } | |
191 | |
192 /// Identifier type. | |
193 class TypeIdentifier : Type | |
194 { | |
195 Identifier* ident; | |
196 this(Identifier* ident) | |
197 { | |
198 super(null, TYP.Identifier); | |
199 } | |
200 } | |
201 | |
202 /// Template instantiation type. | |
203 class TypeTemplInstance : Type | |
204 { | |
205 this() | |
206 { | |
207 super(null, TYP.TInstance); | |
208 } | |
209 } | |
210 | |
211 /// Template tuple type. | |
212 class TypeTuple : Type | |
213 { | |
214 this(Type next) | |
215 { | |
216 super(next, TYP.Tuple); | |
217 } | |
218 } | |
219 | |
220 /// Constant type. D2.0 | |
221 class TypeConst : Type | |
222 { | |
223 this(Type next) | |
224 { | |
225 super(next, TYP.Const); | |
226 } | |
227 } | |
228 | |
229 /// Invariant type. D2.0 | |
230 class TypeInvariant : Type | |
231 { | |
232 this(Type next) | |
233 { | |
234 super(next, TYP.Const); | |
235 } | |
236 } | |
237 | |
238 /// Represents a value related to a Type. | |
239 union Value | |
240 { | |
241 void* pvoid; | |
242 bool bool_; | |
243 dchar dchar_; | |
244 long long_; | |
245 ulong ulong_; | |
246 int int_; | |
247 uint uint_; | |
248 float float_; | |
249 double double_; | |
250 real real_; | |
251 creal creal_; | |
252 } | |
253 | |
254 /// Information related to a Type. | |
255 struct TypeMetaInfo | |
256 { | |
257 char mangle; /// Mangle character of the type. | |
258 ushort size; /// Byte size of the type. | |
259 Value* defaultInit; /// Default initialization value. | |
260 } | |
261 | |
262 /// Namespace for the meta info table. | |
263 struct MITable | |
264 { | |
265 static: | |
266 const ushort SIZE_NOT_AVAILABLE = 0; /// Size not available. | |
267 const Value VZERO = {int_:0}; /// Value 0. | |
268 const Value VNULL = {pvoid:null}; /// Value null. | |
269 const Value V0xFF = {dchar_:0xFF}; /// Value 0xFF. | |
270 const Value V0xFFFF = {dchar_:0xFFFF}; /// Value 0xFFFF. | |
271 const Value VFALSE = {bool_:false}; /// Value false. | |
272 const Value VNAN = {float_:float.nan}; /// Value NAN. | |
273 const Value VCNAN = {creal_:creal.nan}; /// Value complex NAN. | |
274 private alias SIZE_NOT_AVAILABLE SNA; | |
275 private alias PTR_SIZE PS; | |
276 /// The meta info table. | |
277 private const TypeMetaInfo metaInfoTable[] = [ | |
278 {'?', SNA}, // Error | |
279 | |
280 {'a', 1, &V0xFF}, // Char | |
281 {'u', 2, &V0xFFFF}, // Wchar | |
282 {'w', 4, &V0xFFFF}, // Dchar | |
283 {'b', 1, &VFALSE}, // Bool | |
284 {'g', 1, &VZERO}, // Byte | |
285 {'h', 1, &VZERO}, // Ubyte | |
286 {'s', 2, &VZERO}, // Short | |
287 {'t', 2, &VZERO}, // Ushort | |
288 {'i', 4, &VZERO}, // Int | |
289 {'k', 4, &VZERO}, // Uint | |
290 {'l', 8, &VZERO}, // Long | |
291 {'m', 8, &VZERO}, // Ulong | |
292 {'?', 16, &VZERO}, // Cent | |
293 {'?', 16, &VZERO}, // Ucent | |
294 {'f', 4, &VNAN}, // Float | |
295 {'d', 8, &VNAN}, // Double | |
296 {'e', 12, &VNAN}, // Real | |
297 {'o', 4, &VNAN}, // Ifloat | |
298 {'p', 8, &VNAN}, // Idouble | |
299 {'j', 12, &VNAN}, // Ireal | |
300 {'q', 8, &VCNAN}, // Cfloat | |
301 {'r', 16, &VCNAN}, // Cdouble | |
302 {'c', 24, &VCNAN}, // Creal | |
303 {'v', 1}, // void | |
304 | |
305 {'n', SNA}, // None | |
306 | |
307 {'A', PS*2, &VNULL}, // Dynamic array | |
308 {'G', PS*2, &VNULL}, // Static array | |
309 {'H', PS*2, &VNULL}, // Associative array | |
310 | |
311 {'E', SNA}, // Enum | |
312 {'S', SNA}, // Struct | |
313 {'C', PS, &VNULL}, // Class | |
314 {'T', SNA}, // Typedef | |
315 {'F', PS}, // Function | |
316 {'D', PS*2, &VNULL}, // Delegate | |
317 {'P', PS, &VNULL}, // Pointer | |
318 {'R', PS, &VNULL}, // Reference | |
319 {'I', SNA}, // Identifier | |
320 {'?', SNA}, // Template instance | |
321 {'B', SNA}, // Tuple | |
322 {'x', SNA}, // Const, D2 | |
323 {'y', SNA}, // Invariant, D2 | |
324 ]; | |
325 static assert(metaInfoTable.length == TYP.max+1); | |
326 | |
327 /// Returns the size of a type. | |
328 size_t getSize(Type type) | |
329 { | |
330 auto size = metaInfoTable[type.tid].size; | |
331 if (size == SIZE_NOT_AVAILABLE) | |
332 return type.sizeOf_(); | |
333 return size; | |
334 } | |
335 } | |
336 | |
337 /// Namespace for a set of predefined types. | |
338 struct Types | |
339 { | |
340 static: | |
341 /// Predefined basic types. | |
342 TypeBasic Char, Wchar, Dchar, Bool, | |
343 Byte, Ubyte, Short, Ushort, | |
344 Int, Uint, Long, Ulong, | |
345 Cent, Ucent, | |
346 Float, Double, Real, | |
347 Ifloat, Idouble, Ireal, | |
348 Cfloat, Cdouble, Creal, Void; | |
349 | |
350 TypeBasic Size_t; /// The size type. | |
351 TypeBasic Ptrdiff_t; /// The pointer difference type. | |
352 TypePointer Void_ptr; /// The void pointer type. | |
353 TypeBasic Error; /// The error type. | |
354 TypeBasic Undefined; /// The undefined type. | |
355 | |
356 /// Allocates an instance of TypeBasic and assigns it to typeName. | |
357 template newTB(char[] typeName) | |
358 { | |
359 const newTB = mixin(typeName~" = new TypeBasic(TYP."~typeName~")"); | |
360 } | |
361 | |
362 /// Initializes predefined types. | |
363 static this() | |
364 { | |
365 newTB!("Char"); | |
366 newTB!("Wchar"); | |
367 newTB!("Dchar"); | |
368 newTB!("Bool"); | |
369 newTB!("Byte"); | |
370 newTB!("Ubyte"); | |
371 newTB!("Short"); | |
372 newTB!("Ushort"); | |
373 newTB!("Int"); | |
374 newTB!("Uint"); | |
375 newTB!("Long"); | |
376 newTB!("Ulong"); | |
377 newTB!("Cent"); | |
378 newTB!("Ucent"); | |
379 newTB!("Float"); | |
380 newTB!("Double"); | |
381 newTB!("Real"); | |
382 newTB!("Ifloat"); | |
383 newTB!("Idouble"); | |
384 newTB!("Ireal"); | |
385 newTB!("Cfloat"); | |
386 newTB!("Cdouble"); | |
387 newTB!("Creal"); | |
388 newTB!("Void"); | |
389 version(X86_64) | |
390 { | |
391 Size_t = Ulong; | |
392 Ptrdiff_t = Long; | |
393 } | |
394 else | |
395 { | |
396 Size_t = Uint; | |
397 Ptrdiff_t = Int; | |
398 } | |
399 Void_ptr = Void.ptrTo; | |
400 Error = new TypeBasic(TYP.Error); | |
401 Undefined = new TypeBasic(TYP.Error); | |
402 } | |
403 } |