Mercurial > projects > dil
comparison trunk/src/dil/semantic/Types.d @ 798:c24be8d4f6ab
Added documentation comments.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sat, 01 Mar 2008 02:53:06 +0100 |
parents | f7688996bf08 |
children | 9e6c6bb73e5f |
comparison
equal
deleted
inserted
replaced
797:cf2ad5df025c | 798:c24be8d4f6ab |
---|---|
7 import dil.semantic.Symbol; | 7 import dil.semantic.Symbol; |
8 import dil.semantic.TypesEnum; | 8 import dil.semantic.TypesEnum; |
9 import dil.lexer.Identifier; | 9 import dil.lexer.Identifier; |
10 import dil.CompilerInfo; | 10 import dil.CompilerInfo; |
11 | 11 |
12 /// The base type for all type structures. | |
12 abstract class Type/* : Symbol*/ | 13 abstract class Type/* : Symbol*/ |
13 { | 14 { |
14 Type next; /// The next type in the type structure. | 15 Type next; /// The next type in the type structure. |
15 TYP tid; /// The ID of the type. | 16 TYP tid; /// The ID of the type. |
16 Symbol symbol; /// Not null if this type has a symbol. | 17 Symbol symbol; /// Not null if this type has a symbol. |
17 | 18 |
18 this(){} | 19 this(){} |
19 | 20 |
21 /// Constructs a Type object. | |
22 /// Params: | |
23 /// next = the type's next type. | |
24 /// tid = the type's ID. | |
20 this(Type next, TYP tid) | 25 this(Type next, TYP tid) |
21 { | 26 { |
22 // this.sid = SYM.Type; | 27 // this.sid = SYM.Type; |
23 | 28 |
24 this.next = next; | 29 this.next = next; |
25 this.tid = tid; | 30 this.tid = tid; |
26 } | 31 } |
27 | 32 |
33 /// Returns a pointer type to this type. | |
28 TypePointer ptrTo() | 34 TypePointer ptrTo() |
29 { | 35 { |
30 return new TypePointer(this); | 36 return new TypePointer(this); |
31 } | 37 } |
32 | 38 |
33 /// Get byte size of this type. | 39 /// Returns the byte size of this type. |
34 final size_t sizeOf() | 40 final size_t sizeOf() |
35 { | 41 { |
36 return MITable.getSize(this); | 42 return MITable.getSize(this); |
37 } | 43 } |
38 | 44 |
40 size_t sizeOf_() | 46 size_t sizeOf_() |
41 { | 47 { |
42 return sizeOf(); | 48 return sizeOf(); |
43 } | 49 } |
44 | 50 |
51 /// Returns true if this type has a symbol. | |
45 bool hasSymbol() | 52 bool hasSymbol() |
46 { | 53 { |
47 return symbol !is null; | 54 return symbol !is null; |
48 } | 55 } |
49 } | 56 } |
50 | 57 |
58 /// All basic types. E.g.: int, char, real etc. | |
51 class TypeBasic : Type | 59 class TypeBasic : Type |
52 { | 60 { |
53 this(TYP typ) | 61 this(TYP typ) |
54 { | 62 { |
55 super(null, typ); | 63 super(null, typ); |
56 } | 64 } |
57 } | 65 } |
58 | 66 |
59 /// Dynamic array. | 67 /// Dynamic array type. |
60 class TypeDArray : Type | 68 class TypeDArray : Type |
61 { | 69 { |
62 this(Type next) | 70 this(Type next) |
63 { | 71 { |
64 super(next, TYP.DArray); | 72 super(next, TYP.DArray); |
65 } | 73 } |
66 } | 74 } |
67 | 75 |
68 /// Associative array. | 76 /// Associative array type. |
69 class TypeAArray : Type | 77 class TypeAArray : Type |
70 { | 78 { |
71 Type keyType; | 79 Type keyType; |
72 this(Type next, Type keyType) | 80 this(Type next, Type keyType) |
73 { | 81 { |
74 super(next, TYP.AArray); | 82 super(next, TYP.AArray); |
75 this.keyType = keyType; | 83 this.keyType = keyType; |
76 } | 84 } |
77 } | 85 } |
78 | 86 |
79 /// Static array. | 87 /// Static array type. |
80 class TypeSArray : Type | 88 class TypeSArray : Type |
81 { | 89 { |
82 size_t dimension; | 90 size_t dimension; |
83 this(Type next, size_t dimension) | 91 this(Type next, size_t dimension) |
84 { | 92 { |
85 super(next, TYP.SArray); | 93 super(next, TYP.SArray); |
86 this.dimension = dimension; | 94 this.dimension = dimension; |
87 } | 95 } |
88 } | 96 } |
89 | 97 |
98 /// Pointer type. | |
90 class TypePointer : Type | 99 class TypePointer : Type |
91 { | 100 { |
92 this(Type next) | 101 this(Type next) |
93 { | 102 { |
94 super(next, TYP.Pointer); | 103 super(next, TYP.Pointer); |
95 } | 104 } |
96 } | 105 } |
97 | 106 |
107 /// Reference type. | |
98 class TypeReference : Type | 108 class TypeReference : Type |
99 { | 109 { |
100 this(Type next) | 110 this(Type next) |
101 { | 111 { |
102 super(next, TYP.Reference); | 112 super(next, TYP.Reference); |
103 } | 113 } |
104 } | 114 } |
105 | 115 |
116 /// Enum type. | |
106 class TypeEnum : Type | 117 class TypeEnum : Type |
107 { | 118 { |
108 this(Symbol symbol, Type baseType) | 119 this(Symbol symbol, Type baseType) |
109 { | 120 { |
110 super(baseType, TYP.Enum); | 121 super(baseType, TYP.Enum); |
115 { | 126 { |
116 return next; | 127 return next; |
117 } | 128 } |
118 } | 129 } |
119 | 130 |
131 /// Struct type. | |
120 class TypeStruct : Type | 132 class TypeStruct : Type |
121 { | 133 { |
122 this(Symbol symbol) | 134 this(Symbol symbol) |
123 { | 135 { |
124 super(null, TYP.Struct); | 136 super(null, TYP.Struct); |
125 this.symbol = symbol; | 137 this.symbol = symbol; |
126 } | 138 } |
127 } | 139 } |
128 | 140 |
141 /// Class type. | |
129 class TypeClass : Type | 142 class TypeClass : Type |
130 { | 143 { |
131 this(Symbol symbol) | 144 this(Symbol symbol) |
132 { | 145 { |
133 super(null, TYP.Class); | 146 super(null, TYP.Class); |
134 this.symbol = symbol; | 147 this.symbol = symbol; |
135 } | 148 } |
136 } | 149 } |
137 | 150 |
151 /// Typedef type. | |
138 class TypeTypedef : Type | 152 class TypeTypedef : Type |
139 { | 153 { |
140 this(Type next) | 154 this(Type next) |
141 { | 155 { |
142 super(next, TYP.Typedef); | 156 super(next, TYP.Typedef); |
143 } | 157 } |
144 } | 158 } |
145 | 159 |
160 /// Function type. | |
146 class TypeFunction : Type | 161 class TypeFunction : Type |
147 { | 162 { |
148 this(Type next) | 163 this(Type next) |
149 { | 164 { |
150 super(next, TYP.Function); | 165 super(next, TYP.Function); |
151 } | 166 } |
152 } | 167 } |
153 | 168 |
169 /// Delegate type. | |
154 class TypeDelegate : Type | 170 class TypeDelegate : Type |
155 { | 171 { |
156 this(Type next) | 172 this(Type next) |
157 { | 173 { |
158 super(next, TYP.Delegate); | 174 super(next, TYP.Delegate); |
159 } | 175 } |
160 } | 176 } |
161 | 177 |
178 /// Identifier type. | |
162 class TypeIdentifier : Type | 179 class TypeIdentifier : Type |
163 { | 180 { |
164 Identifier* ident; | 181 Identifier* ident; |
165 this(Identifier* ident) | 182 this(Identifier* ident) |
166 { | 183 { |
167 super(null, TYP.Identifier); | 184 super(null, TYP.Identifier); |
168 } | 185 } |
169 } | 186 } |
170 | 187 |
188 /// Template instantiation type. | |
171 class TypeTemplInstance : Type | 189 class TypeTemplInstance : Type |
172 { | 190 { |
173 this() | 191 this() |
174 { | 192 { |
175 super(null, TYP.TInstance); | 193 super(null, TYP.TInstance); |
176 } | 194 } |
177 } | 195 } |
178 | 196 |
197 /// Template tuple type. | |
179 class TypeTuple : Type | 198 class TypeTuple : Type |
180 { | 199 { |
181 this(Type next) | 200 this(Type next) |
182 { | 201 { |
183 super(next, TYP.Tuple); | 202 super(next, TYP.Tuple); |
184 } | 203 } |
185 } | 204 } |
186 | 205 |
206 /// Constant type. D2.0 | |
187 class TypeConst : Type | 207 class TypeConst : Type |
188 { | 208 { |
189 this(Type next) | 209 this(Type next) |
190 { | 210 { |
191 super(next, TYP.Const); | 211 super(next, TYP.Const); |
192 } | 212 } |
193 } | 213 } |
194 | 214 |
215 /// Invariant type. D2.0 | |
195 class TypeInvariant : Type | 216 class TypeInvariant : Type |
196 { | 217 { |
197 this(Type next) | 218 this(Type next) |
198 { | 219 { |
199 super(next, TYP.Const); | 220 super(next, TYP.Const); |
200 } | 221 } |
201 } | 222 } |
202 | 223 |
203 /// Represents a value related to a type. | 224 /// Represents a value related to a Type. |
204 union Value | 225 union Value |
205 { | 226 { |
206 void* pvoid; | 227 void* pvoid; |
207 bool bool_; | 228 bool bool_; |
208 dchar dchar_; | 229 dchar dchar_; |
214 double double_; | 235 double double_; |
215 real real_; | 236 real real_; |
216 creal creal_; | 237 creal creal_; |
217 } | 238 } |
218 | 239 |
240 /// Information related to a Type. | |
219 struct TypeMetaInfo | 241 struct TypeMetaInfo |
220 { | 242 { |
221 char mangle; /// Mangle character of the type. | 243 char mangle; /// Mangle character of the type. |
222 ushort size; /// Byte size of the type. | 244 ushort size; /// Byte size of the type. |
223 Value* defaultInit; /// Default initialization value. | 245 Value* defaultInit; /// Default initialization value. |
224 } | 246 } |
225 | 247 |
248 /// Namespace for the meta info table. | |
226 struct MITable | 249 struct MITable |
227 { | 250 { |
228 static: | 251 static: |
229 const ushort SIZE_NOT_AVAILABLE = 0; /// Size not available. | 252 const ushort SIZE_NOT_AVAILABLE = 0; /// Size not available. |
230 const Value VZERO = {int_:0}; /// Value 0. | 253 const Value VZERO = {int_:0}; /// Value 0. |
234 const Value VFALSE = {bool_:false}; /// Value false. | 257 const Value VFALSE = {bool_:false}; /// Value false. |
235 const Value VNAN = {float_:float.nan}; /// Value NAN. | 258 const Value VNAN = {float_:float.nan}; /// Value NAN. |
236 const Value VCNAN = {creal_:creal.nan}; /// Value complex NAN. | 259 const Value VCNAN = {creal_:creal.nan}; /// Value complex NAN. |
237 private alias SIZE_NOT_AVAILABLE SNA; | 260 private alias SIZE_NOT_AVAILABLE SNA; |
238 private alias PTR_SIZE PS; | 261 private alias PTR_SIZE PS; |
262 /// The meta info table. | |
239 private const TypeMetaInfo metaInfoTable[] = [ | 263 private const TypeMetaInfo metaInfoTable[] = [ |
240 {'?', SNA}, // Error | 264 {'?', SNA}, // Error |
241 | 265 |
242 {'a', 1, &V0xFF}, // Char | 266 {'a', 1, &V0xFF}, // Char |
243 {'u', 2, &V0xFFFF}, // Wchar | 267 {'u', 2, &V0xFFFF}, // Wchar |
284 {'x', SNA}, // Const, D2 | 308 {'x', SNA}, // Const, D2 |
285 {'y', SNA}, // Invariant, D2 | 309 {'y', SNA}, // Invariant, D2 |
286 ]; | 310 ]; |
287 static assert(metaInfoTable.length == TYP.max+1); | 311 static assert(metaInfoTable.length == TYP.max+1); |
288 | 312 |
313 /// Returns the size of a type. | |
289 size_t getSize(Type type) | 314 size_t getSize(Type type) |
290 { | 315 { |
291 auto size = metaInfoTable[type.tid].size; | 316 auto size = metaInfoTable[type.tid].size; |
292 if (size == SIZE_NOT_AVAILABLE) | 317 if (size == SIZE_NOT_AVAILABLE) |
293 return type.sizeOf_(); | 318 return type.sizeOf_(); |
294 return size; | 319 return size; |
295 } | 320 } |
296 } | 321 } |
297 | 322 |
298 /// A set of pre-defined types. | 323 /// Namespace for a set of predefined types. |
299 struct Types | 324 struct Types |
300 { | 325 { |
301 static: | 326 static: |
327 /// Predefined basic types. | |
302 TypeBasic Char, Wchar, Dchar, Bool, | 328 TypeBasic Char, Wchar, Dchar, Bool, |
303 Byte, Ubyte, Short, Ushort, | 329 Byte, Ubyte, Short, Ushort, |
304 Int, Uint, Long, Ulong, | 330 Int, Uint, Long, Ulong, |
305 Cent, Ucent, | 331 Cent, Ucent, |
306 Float, Double, Real, | 332 Float, Double, Real, |
307 Ifloat, Idouble, Ireal, | 333 Ifloat, Idouble, Ireal, |
308 Cfloat, Cdouble, Creal, Void; | 334 Cfloat, Cdouble, Creal, Void; |
309 | 335 |
310 TypeBasic Size_t, Ptrdiff_t; | 336 TypeBasic Size_t; /// The size type. |
311 TypePointer Void_ptr; | 337 TypeBasic Ptrdiff_t; /// The pointer difference type. |
312 TypeBasic Error, Undefined; | 338 TypePointer Void_ptr; /// The void pointer type. |
339 TypeBasic Error; /// The error type. | |
340 TypeBasic Undefined; /// The undefined type. | |
313 | 341 |
314 /// Allocates an instance of TypeBasic and assigns it to typeName. | 342 /// Allocates an instance of TypeBasic and assigns it to typeName. |
315 template newTB(char[] typeName) | 343 template newTB(char[] typeName) |
316 { | 344 { |
317 const newTB = mixin(typeName~" = new TypeBasic(TYP."~typeName~")"); | 345 const newTB = mixin(typeName~" = new TypeBasic(TYP."~typeName~")"); |
318 } | 346 } |
319 | 347 |
348 /// Initializes predefined types. | |
320 static this() | 349 static this() |
321 { | 350 { |
322 newTB!("Char"); | 351 newTB!("Char"); |
323 newTB!("Wchar"); | 352 newTB!("Wchar"); |
324 newTB!("Dchar"); | 353 newTB!("Dchar"); |