Mercurial > projects > ldc
annotate dmd/mtype.c @ 1188:442ab244c455
Apply small includes patch for FreeBSD. Thanks kimelto!
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Thu, 02 Apr 2009 19:16:44 +0200 |
parents | dbe4af57b240 |
children | 3251ce06c820 |
rev | line source |
---|---|
159 | 1 |
2 // Compiler implementation of the D programming language | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3 // Copyright (c) 1999-2009 by Digital Mars |
159 | 4 // All Rights Reserved |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
872
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
846
diff
changeset
|
11 #define __C99FEATURES__ 1 // Needed on Solaris for NaN and more |
159 | 12 #define __USE_ISOC99 1 // so signbit() gets defined |
872
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
846
diff
changeset
|
13 |
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
846
diff
changeset
|
14 #if (defined (__SVR4) && defined (__sun)) |
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
846
diff
changeset
|
15 #include <alloca.h> |
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
846
diff
changeset
|
16 #endif |
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
846
diff
changeset
|
17 |
1147
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
18 #ifdef __DMC__ |
1133
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1124
diff
changeset
|
19 #include <math.h> |
1147
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
20 #else |
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
21 #include <cmath> |
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
22 #endif |
159 | 23 |
24 #include <stdio.h> | |
25 #include <assert.h> | |
26 #include <float.h> | |
27 | |
28 #ifdef __DMC__ | |
29 #include <fp.h> | |
30 #endif | |
31 | |
32 #if _MSC_VER | |
33 #include <malloc.h> | |
34 #include <complex> | |
35 #include <limits> | |
36 #elif __DMC__ | |
37 #include <complex.h> | |
285
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
270
diff
changeset
|
38 #elif __MINGW32__ |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
270
diff
changeset
|
39 #include <malloc.h> |
159 | 40 #else |
41 //#define signbit 56 | |
42 #endif | |
43 | |
1188
442ab244c455
Apply small includes patch for FreeBSD. Thanks kimelto!
Christian Kamm <kamm incasoftware de>
parents:
1147
diff
changeset
|
44 #if __GNUC__ |
900
177afbf5297b
Solaris compatibility and proc map parsing #183, thanks BlueZeniX!
Christian Kamm <kamm incasoftware de>
parents:
881
diff
changeset
|
45 #if !(defined (__SVR4) && defined (__sun)) |
159 | 46 #include <bits/nan.h> |
47 #include <bits/mathdef.h> | |
900
177afbf5297b
Solaris compatibility and proc map parsing #183, thanks BlueZeniX!
Christian Kamm <kamm incasoftware de>
parents:
881
diff
changeset
|
48 #endif |
1188
442ab244c455
Apply small includes patch for FreeBSD. Thanks kimelto!
Christian Kamm <kamm incasoftware de>
parents:
1147
diff
changeset
|
49 #endif |
159 | 50 static double zero = 0; |
51 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
52 #include "rmem.h" |
159 | 53 |
54 #include "dsymbol.h" | |
55 #include "mtype.h" | |
56 #include "scope.h" | |
57 #include "init.h" | |
58 #include "expression.h" | |
59 #include "attrib.h" | |
60 #include "declaration.h" | |
61 #include "template.h" | |
62 #include "id.h" | |
63 #include "enum.h" | |
64 #include "import.h" | |
65 #include "aggregate.h" | |
66 #include "hdrgen.h" | |
67 | |
1147
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
68 #if IN_LLVM |
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
69 //#include "gen/tollvm.h" |
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
70 Ir* Type::sir = NULL; |
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
71 unsigned GetTypeAlignment(Ir* ir, Type* t); |
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
72 #endif |
1009
b1b23a64ad8c
Use LLVM alignment data instead of hand-picked.
Frits van Bommel <fvbommel wxs.nl>
parents:
1008
diff
changeset
|
73 |
159 | 74 FuncDeclaration *hasThis(Scope *sc); |
75 | |
76 | |
77 #define LOGDOTEXP 0 // log ::dotExp() | |
78 #define LOGDEFAULTINIT 0 // log ::defaultInit() | |
79 | |
80 // Allow implicit conversion of T[] to T* | |
81 #define IMPLICIT_ARRAY_TO_PTR global.params.useDeprecated | |
82 | |
83 /* These have default values for 32 bit code, they get | |
84 * adjusted for 64 bit code. | |
85 */ | |
86 | |
87 int PTRSIZE = 4; | |
88 #if IN_LLVM | |
89 int REALSIZE = 8; | |
90 int REALPAD = 0; | |
91 #elif TARGET_LINUX | |
92 int REALSIZE = 12; | |
93 int REALPAD = 2; | |
94 #else | |
95 int REALSIZE = 10; | |
96 int REALPAD = 0; | |
97 #endif | |
98 int Tsize_t = Tuns32; | |
99 int Tptrdiff_t = Tint32; | |
100 | |
101 /***************************** Type *****************************/ | |
102 | |
103 ClassDeclaration *Type::typeinfo; | |
104 ClassDeclaration *Type::typeinfoclass; | |
105 ClassDeclaration *Type::typeinfointerface; | |
106 ClassDeclaration *Type::typeinfostruct; | |
107 ClassDeclaration *Type::typeinfotypedef; | |
108 ClassDeclaration *Type::typeinfopointer; | |
109 ClassDeclaration *Type::typeinfoarray; | |
110 ClassDeclaration *Type::typeinfostaticarray; | |
111 ClassDeclaration *Type::typeinfoassociativearray; | |
112 ClassDeclaration *Type::typeinfoenum; | |
113 ClassDeclaration *Type::typeinfofunction; | |
114 ClassDeclaration *Type::typeinfodelegate; | |
115 ClassDeclaration *Type::typeinfotypelist; | |
116 | |
117 Type *Type::tvoidptr; | |
118 Type *Type::basic[TMAX]; | |
119 unsigned char Type::mangleChar[TMAX]; | |
120 StringTable Type::stringtable; | |
121 | |
122 | |
123 Type::Type(TY ty, Type *next) | |
124 { | |
125 this->ty = ty; | |
126 this->mod = 0; | |
127 this->next = next; | |
128 this->deco = NULL; | |
336 | 129 #if DMDV2 |
159 | 130 this->cto = NULL; |
131 this->ito = NULL; | |
132 #endif | |
133 this->pto = NULL; | |
134 this->rto = NULL; | |
135 this->arrayof = NULL; | |
136 this->vtinfo = NULL; | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
137 #if IN_DMD |
159 | 138 this->ctype = NULL; |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
139 #endif |
159 | 140 } |
141 | |
142 Type *Type::syntaxCopy() | |
143 { | |
144 print(); | |
145 fprintf(stdmsg, "ty = %d\n", ty); | |
146 assert(0); | |
147 return this; | |
148 } | |
149 | |
150 int Type::equals(Object *o) | |
151 { Type *t; | |
152 | |
153 t = (Type *)o; | |
154 //printf("Type::equals(%s, %s)\n", toChars(), t->toChars()); | |
155 if (this == o || | |
156 (t && deco == t->deco) && // deco strings are unique | |
157 deco != NULL) // and semantic() has been run | |
158 { | |
159 //printf("deco = '%s', t->deco = '%s'\n", deco, t->deco); | |
160 return 1; | |
161 } | |
162 //if (deco && t && t->deco) printf("deco = '%s', t->deco = '%s'\n", deco, t->deco); | |
163 return 0; | |
164 } | |
165 | |
166 char Type::needThisPrefix() | |
167 { | |
168 return 'M'; // name mangling prefix for functions needing 'this' | |
169 } | |
170 | |
1147
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
171 #if IN_LLVM |
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
172 void Type::init(Ir* _sir) |
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
173 #else |
159 | 174 void Type::init() |
1147
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
175 #endif |
159 | 176 { int i; |
177 int j; | |
178 | |
179 Lexer::initKeywords(); | |
180 | |
181 mangleChar[Tarray] = 'A'; | |
182 mangleChar[Tsarray] = 'G'; | |
183 mangleChar[Taarray] = 'H'; | |
184 mangleChar[Tpointer] = 'P'; | |
185 mangleChar[Treference] = 'R'; | |
186 mangleChar[Tfunction] = 'F'; | |
187 mangleChar[Tident] = 'I'; | |
188 mangleChar[Tclass] = 'C'; | |
189 mangleChar[Tstruct] = 'S'; | |
190 mangleChar[Tenum] = 'E'; | |
191 mangleChar[Ttypedef] = 'T'; | |
192 mangleChar[Tdelegate] = 'D'; | |
193 | |
194 mangleChar[Tnone] = 'n'; | |
195 mangleChar[Tvoid] = 'v'; | |
196 mangleChar[Tint8] = 'g'; | |
197 mangleChar[Tuns8] = 'h'; | |
198 mangleChar[Tint16] = 's'; | |
199 mangleChar[Tuns16] = 't'; | |
200 mangleChar[Tint32] = 'i'; | |
201 mangleChar[Tuns32] = 'k'; | |
202 mangleChar[Tint64] = 'l'; | |
203 mangleChar[Tuns64] = 'm'; | |
204 mangleChar[Tfloat32] = 'f'; | |
205 mangleChar[Tfloat64] = 'd'; | |
206 mangleChar[Tfloat80] = 'e'; | |
207 | |
208 mangleChar[Timaginary32] = 'o'; | |
209 mangleChar[Timaginary64] = 'p'; | |
210 mangleChar[Timaginary80] = 'j'; | |
211 mangleChar[Tcomplex32] = 'q'; | |
212 mangleChar[Tcomplex64] = 'r'; | |
213 mangleChar[Tcomplex80] = 'c'; | |
214 | |
215 mangleChar[Tbool] = 'b'; | |
216 mangleChar[Tascii] = 'a'; | |
217 mangleChar[Twchar] = 'u'; | |
218 mangleChar[Tdchar] = 'w'; | |
219 | |
220 mangleChar[Tbit] = '@'; | |
221 mangleChar[Tinstance] = '@'; | |
222 mangleChar[Terror] = '@'; | |
223 mangleChar[Ttypeof] = '@'; | |
224 mangleChar[Ttuple] = 'B'; | |
225 mangleChar[Tslice] = '@'; | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
226 mangleChar[Treturn] = '@'; |
159 | 227 |
228 for (i = 0; i < TMAX; i++) | |
229 { if (!mangleChar[i]) | |
230 fprintf(stdmsg, "ty = %d\n", i); | |
231 assert(mangleChar[i]); | |
232 } | |
233 | |
234 // Set basic types | |
235 static TY basetab[] = | |
236 { Tvoid, Tint8, Tuns8, Tint16, Tuns16, Tint32, Tuns32, Tint64, Tuns64, | |
237 Tfloat32, Tfloat64, Tfloat80, | |
238 Timaginary32, Timaginary64, Timaginary80, | |
239 Tcomplex32, Tcomplex64, Tcomplex80, | |
240 Tbit, Tbool, | |
241 Tascii, Twchar, Tdchar }; | |
242 | |
243 for (i = 0; i < sizeof(basetab) / sizeof(basetab[0]); i++) | |
244 basic[basetab[i]] = new TypeBasic(basetab[i]); | |
245 basic[Terror] = basic[Tint32]; | |
246 | |
247 tvoidptr = tvoid->pointerTo(); | |
248 | |
1147
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
249 // LDC |
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
250 sir = _sir; |
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
251 |
270
d9d5d59873d8
[svn r291] Fixed a bunch of the old Phobos tests to work with Tango.
lindquist
parents:
243
diff
changeset
|
252 // set size_t / ptrdiff_t types and pointer size |
159 | 253 if (global.params.is64bit) |
254 { | |
742
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
255 Tsize_t = Tuns64; |
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
256 Tptrdiff_t = Tint64; |
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
257 PTRSIZE = 8; |
159 | 258 } |
259 else | |
260 { | |
742
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
261 Tsize_t = Tuns32; |
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
262 Tptrdiff_t = Tint32; |
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
263 PTRSIZE = 4; |
243
4d006f7b2ada
[svn r260] Changed some of the LLVMDC specific code in the Tango core and did some minor cleanups.
lindquist
parents:
217
diff
changeset
|
264 } |
4d006f7b2ada
[svn r260] Changed some of the LLVMDC specific code in the Tango core and did some minor cleanups.
lindquist
parents:
217
diff
changeset
|
265 |
4d006f7b2ada
[svn r260] Changed some of the LLVMDC specific code in the Tango core and did some minor cleanups.
lindquist
parents:
217
diff
changeset
|
266 // set real size and padding |
445
cc40db549aea
Changed the handling of variadic intrinsics a bit.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
389
diff
changeset
|
267 if (global.params.cpu == ARCHx86) |
243
4d006f7b2ada
[svn r260] Changed some of the LLVMDC specific code in the Tango core and did some minor cleanups.
lindquist
parents:
217
diff
changeset
|
268 { |
742
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
269 REALSIZE = 12; |
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
270 REALPAD = 2; |
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
271 } |
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
272 else if (global.params.cpu == ARCHx86_64) |
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
273 { |
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
274 REALSIZE = 16; |
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
275 REALPAD = 6; |
243
4d006f7b2ada
[svn r260] Changed some of the LLVMDC specific code in the Tango core and did some minor cleanups.
lindquist
parents:
217
diff
changeset
|
276 } |
4d006f7b2ada
[svn r260] Changed some of the LLVMDC specific code in the Tango core and did some minor cleanups.
lindquist
parents:
217
diff
changeset
|
277 else |
4d006f7b2ada
[svn r260] Changed some of the LLVMDC specific code in the Tango core and did some minor cleanups.
lindquist
parents:
217
diff
changeset
|
278 { |
742
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
279 REALSIZE = 8; |
05e7657a7811
Fix REALSIZE, REALPAD and alignment of double and real for x86-64.
Christian Kamm <kamm incasoftware de>
parents:
739
diff
changeset
|
280 REALPAD = 0; |
159 | 281 } |
282 } | |
283 | |
284 d_uns64 Type::size() | |
285 { | |
286 return size(0); | |
287 } | |
288 | |
289 d_uns64 Type::size(Loc loc) | |
290 { | |
291 error(loc, "no size for type %s", toChars()); | |
292 return 1; | |
293 } | |
294 | |
295 unsigned Type::alignsize() | |
296 { | |
297 return size(0); | |
298 } | |
299 | |
300 Type *Type::semantic(Loc loc, Scope *sc) | |
301 { | |
302 if (next) | |
303 next = next->semantic(loc,sc); | |
304 return merge(); | |
305 } | |
306 | |
307 Type *Type::pointerTo() | |
308 { | |
309 if (!pto) | |
310 { Type *t; | |
311 | |
312 t = new TypePointer(this); | |
313 pto = t->merge(); | |
314 } | |
315 return pto; | |
316 } | |
317 | |
318 Type *Type::referenceTo() | |
319 { | |
320 if (!rto) | |
321 { Type *t; | |
322 | |
323 t = new TypeReference(this); | |
324 rto = t->merge(); | |
325 } | |
326 return rto; | |
327 } | |
328 | |
329 Type *Type::arrayOf() | |
330 { | |
331 if (!arrayof) | |
332 { Type *t; | |
333 | |
334 t = new TypeDArray(this); | |
335 arrayof = t->merge(); | |
336 } | |
337 return arrayof; | |
338 } | |
339 | |
340 Dsymbol *Type::toDsymbol(Scope *sc) | |
341 { | |
342 return NULL; | |
343 } | |
344 | |
345 /******************************* | |
346 * If this is a shell around another type, | |
347 * get that other type. | |
348 */ | |
349 | |
350 Type *Type::toBasetype() | |
351 { | |
352 return this; | |
353 } | |
354 | |
355 /******************************** | |
356 * Name mangling. | |
357 */ | |
358 | |
359 void Type::toDecoBuffer(OutBuffer *buf) | |
360 { | |
361 buf->writeByte(mangleChar[ty]); | |
362 if (next) | |
363 { | |
364 assert(next != this); | |
365 //printf("this = %p, ty = %d, next = %p, ty = %d\n", this, this->ty, next, next->ty); | |
366 next->toDecoBuffer(buf); | |
367 } | |
368 } | |
369 | |
370 /******************************** | |
371 * For pretty-printing a type. | |
372 */ | |
373 | |
374 char *Type::toChars() | |
375 { OutBuffer *buf; | |
376 HdrGenState hgs; | |
377 | |
378 buf = new OutBuffer(); | |
379 toCBuffer(buf, NULL, &hgs); | |
380 return buf->toChars(); | |
381 } | |
382 | |
383 void Type::toCBuffer(OutBuffer *buf, Identifier *ident, HdrGenState *hgs) | |
384 { | |
385 toCBuffer2(buf, hgs, 0); | |
386 if (ident) | |
387 { buf->writeByte(' '); | |
388 buf->writestring(ident->toChars()); | |
389 } | |
390 } | |
391 | |
392 void Type::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
393 { | |
394 if (mod != this->mod) | |
395 { toCBuffer3(buf, hgs, mod); | |
396 return; | |
397 } | |
398 buf->writestring(toChars()); | |
399 } | |
400 | |
401 void Type::toCBuffer3(OutBuffer *buf, HdrGenState *hgs, int mod) | |
402 { | |
403 if (mod != this->mod) | |
404 { char *p; | |
405 | |
406 switch (this->mod) | |
407 { | |
408 case 0: | |
409 toCBuffer2(buf, hgs, this->mod); | |
410 break; | |
411 case MODconst: | |
412 p = "const("; | |
413 goto L1; | |
414 case MODinvariant: | |
415 p = "invariant("; | |
416 L1: buf->writestring(p); | |
417 toCBuffer2(buf, hgs, this->mod); | |
418 buf->writeByte(')'); | |
419 break; | |
420 default: | |
421 assert(0); | |
422 } | |
423 } | |
424 } | |
425 | |
426 | |
427 /************************************ | |
428 */ | |
429 | |
430 Type *Type::merge() | |
431 { Type *t; | |
432 | |
433 //printf("merge(%s)\n", toChars()); | |
434 t = this; | |
435 assert(t); | |
436 if (!deco) | |
437 { | |
438 OutBuffer buf; | |
439 StringValue *sv; | |
440 | |
441 if (next) | |
442 next = next->merge(); | |
443 toDecoBuffer(&buf); | |
444 sv = stringtable.update((char *)buf.data, buf.offset); | |
445 if (sv->ptrvalue) | |
446 { t = (Type *) sv->ptrvalue; | |
447 assert(t->deco); | |
448 //printf("old value, deco = '%s' %p\n", t->deco, t->deco); | |
449 } | |
450 else | |
451 { | |
452 sv->ptrvalue = this; | |
453 deco = sv->lstring.string; | |
454 //printf("new value, deco = '%s' %p\n", t->deco, t->deco); | |
455 } | |
456 } | |
457 return t; | |
458 } | |
459 | |
460 int Type::isbit() | |
461 { | |
462 return FALSE; | |
463 } | |
464 | |
465 int Type::isintegral() | |
466 { | |
467 return FALSE; | |
468 } | |
469 | |
470 int Type::isfloating() | |
471 { | |
472 return FALSE; | |
473 } | |
474 | |
475 int Type::isreal() | |
476 { | |
477 return FALSE; | |
478 } | |
479 | |
480 int Type::isimaginary() | |
481 { | |
482 return FALSE; | |
483 } | |
484 | |
485 int Type::iscomplex() | |
486 { | |
487 return FALSE; | |
488 } | |
489 | |
490 int Type::isscalar() | |
491 { | |
492 return FALSE; | |
493 } | |
494 | |
495 int Type::isunsigned() | |
496 { | |
497 return FALSE; | |
498 } | |
499 | |
500 ClassDeclaration *Type::isClassHandle() | |
501 { | |
502 return NULL; | |
503 } | |
504 | |
505 int Type::isauto() | |
506 { | |
507 return FALSE; | |
508 } | |
509 | |
510 int Type::isString() | |
511 { | |
512 return FALSE; | |
513 } | |
514 | |
515 int Type::checkBoolean() | |
516 { | |
517 return isscalar(); | |
518 } | |
519 | |
520 /********************************* | |
521 * Check type to see if it is based on a deprecated symbol. | |
522 */ | |
523 | |
524 void Type::checkDeprecated(Loc loc, Scope *sc) | |
525 { | |
526 Type *t; | |
527 Dsymbol *s; | |
528 | |
529 for (t = this; t; t = t->next) | |
530 { | |
531 s = t->toDsymbol(sc); | |
532 if (s) | |
533 s->checkDeprecated(loc, sc); | |
534 } | |
535 } | |
536 | |
537 | |
538 Expression *Type::defaultInit(Loc loc) | |
539 { | |
540 #if LOGDEFAULTINIT | |
541 printf("Type::defaultInit() '%s'\n", toChars()); | |
542 #endif | |
543 return NULL; | |
544 } | |
545 | |
546 int Type::isZeroInit() | |
547 { | |
548 return 0; // assume not | |
549 } | |
550 | |
551 int Type::isBaseOf(Type *t, int *poffset) | |
552 { | |
553 return 0; // assume not | |
554 } | |
555 | |
556 /******************************** | |
557 * Determine if 'this' can be implicitly converted | |
558 * to type 'to'. | |
559 * Returns: | |
560 * 0 can't convert | |
561 * 1 can convert using implicit conversions | |
562 * 2 this and to are the same type | |
563 */ | |
564 | |
565 MATCH Type::implicitConvTo(Type *to) | |
566 { | |
567 //printf("Type::implicitConvTo(this=%p, to=%p)\n", this, to); | |
568 //printf("\tthis->next=%p, to->next=%p\n", this->next, to->next); | |
569 if (this == to) | |
570 return MATCHexact; | |
571 // if (to->ty == Tvoid) | |
572 // return 1; | |
573 return MATCHnomatch; | |
574 } | |
575 | |
576 Expression *Type::getProperty(Loc loc, Identifier *ident) | |
577 { Expression *e; | |
578 | |
579 #if LOGDOTEXP | |
580 printf("Type::getProperty(type = '%s', ident = '%s')\n", toChars(), ident->toChars()); | |
581 #endif | |
582 if (ident == Id::__sizeof) | |
583 { | |
584 e = new IntegerExp(loc, size(loc), Type::tsize_t); | |
585 } | |
586 else if (ident == Id::size) | |
587 { | |
588 error(loc, ".size property should be replaced with .sizeof"); | |
589 e = new IntegerExp(loc, size(loc), Type::tsize_t); | |
590 } | |
591 else if (ident == Id::alignof) | |
592 { | |
593 e = new IntegerExp(loc, alignsize(), Type::tsize_t); | |
594 } | |
595 else if (ident == Id::typeinfo) | |
596 { | |
597 if (!global.params.useDeprecated) | |
598 error(loc, ".typeinfo deprecated, use typeid(type)"); | |
599 e = getTypeInfo(NULL); | |
600 } | |
601 else if (ident == Id::init) | |
602 { | |
603 if (ty == Tvoid) | |
604 error(loc, "void does not have an initializer"); | |
605 e = defaultInit(loc); | |
606 } | |
607 else if (ident == Id::mangleof) | |
608 { | |
609 assert(deco); | |
610 e = new StringExp(loc, deco, strlen(deco), 'c'); | |
611 Scope sc; | |
612 e = e->semantic(&sc); | |
613 } | |
614 else if (ident == Id::stringof) | |
615 { char *s = toChars(); | |
616 e = new StringExp(loc, s, strlen(s), 'c'); | |
617 Scope sc; | |
618 e = e->semantic(&sc); | |
619 } | |
620 else | |
621 { | |
622 error(loc, "no property '%s' for type '%s'", ident->toChars(), toChars()); | |
623 e = new IntegerExp(loc, 1, Type::tint32); | |
624 } | |
625 return e; | |
626 } | |
627 | |
628 Expression *Type::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
629 { VarDeclaration *v = NULL; | |
630 | |
631 #if LOGDOTEXP | |
632 printf("Type::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
633 #endif | |
634 if (e->op == TOKdotvar) | |
635 { | |
636 DotVarExp *dv = (DotVarExp *)e; | |
637 v = dv->var->isVarDeclaration(); | |
638 } | |
639 else if (e->op == TOKvar) | |
640 { | |
641 VarExp *ve = (VarExp *)e; | |
642 v = ve->var->isVarDeclaration(); | |
643 } | |
644 if (v) | |
645 { | |
646 if (ident == Id::offset) | |
647 { | |
648 if (!global.params.useDeprecated) | |
649 error(e->loc, ".offset deprecated, use .offsetof"); | |
650 goto Loffset; | |
651 } | |
652 else if (ident == Id::offsetof) | |
653 { | |
654 Loffset: | |
655 if (v->storage_class & STCfield) | |
656 { | |
657 e = new IntegerExp(e->loc, v->offset, Type::tsize_t); | |
658 return e; | |
659 } | |
660 } | |
661 else if (ident == Id::init) | |
662 { | |
663 #if 0 | |
664 if (v->init) | |
665 { | |
666 if (v->init->isVoidInitializer()) | |
667 error(e->loc, "%s.init is void", v->toChars()); | |
668 else | |
669 { Loc loc = e->loc; | |
670 e = v->init->toExpression(); | |
671 if (e->op == TOKassign || e->op == TOKconstruct) | |
672 { | |
673 e = ((AssignExp *)e)->e2; | |
674 | |
675 /* Take care of case where we used a 0 | |
676 * to initialize the struct. | |
677 */ | |
678 if (e->type == Type::tint32 && | |
679 e->isBool(0) && | |
680 v->type->toBasetype()->ty == Tstruct) | |
681 { | |
682 e = v->type->defaultInit(loc); | |
683 } | |
684 } | |
685 e = e->optimize(WANTvalue | WANTinterpret); | |
686 // if (!e->isConst()) | |
687 // error(loc, ".init cannot be evaluated at compile time"); | |
688 } | |
689 return e; | |
690 } | |
691 #endif | |
692 Expression *ex = defaultInit(e->loc); | |
693 return ex; | |
694 } | |
695 } | |
696 if (ident == Id::typeinfo) | |
697 { | |
698 if (!global.params.useDeprecated) | |
699 error(e->loc, ".typeinfo deprecated, use typeid(type)"); | |
700 e = getTypeInfo(sc); | |
701 return e; | |
702 } | |
703 if (ident == Id::stringof) | |
704 { char *s = e->toChars(); | |
705 e = new StringExp(e->loc, s, strlen(s), 'c'); | |
706 Scope sc; | |
707 e = e->semantic(&sc); | |
708 return e; | |
709 } | |
710 return getProperty(e->loc, ident); | |
711 } | |
712 | |
713 unsigned Type::memalign(unsigned salign) | |
714 { | |
715 return salign; | |
716 } | |
717 | |
718 void Type::error(Loc loc, const char *format, ...) | |
719 { | |
720 va_list ap; | |
721 va_start(ap, format); | |
722 ::verror(loc, format, ap); | |
723 va_end( ap ); | |
724 } | |
725 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
726 void Type::warning(Loc loc, const char *format, ...) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
727 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
728 if (global.params.warnings && !global.gag) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
729 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
730 va_list ap; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
731 va_start(ap, format); |
1124
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
732 ::vwarning(loc, format, ap); |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
733 va_end( ap ); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
734 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
735 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
736 |
159 | 737 Identifier *Type::getTypeInfoIdent(int internal) |
738 { | |
739 // _init_10TypeInfo_%s | |
740 OutBuffer buf; | |
741 Identifier *id; | |
742 char *name; | |
743 int len; | |
744 | |
745 //toTypeInfoBuffer(&buf); | |
746 if (internal) | |
747 { buf.writeByte(mangleChar[ty]); | |
748 if (ty == Tarray) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
749 buf.writeByte(mangleChar[((TypeArray *)this)->next->ty]); |
159 | 750 } |
751 else | |
752 toDecoBuffer(&buf); | |
753 len = buf.offset; | |
754 name = (char *)alloca(19 + sizeof(len) * 3 + len + 1); | |
755 buf.writeByte(0); | |
756 sprintf(name, "_D%dTypeInfo_%s6__initZ", 9 + len, buf.data); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
757 // LDC |
375
3c4460b988bd
Do not strip the leading underscore for typeinfo mangles on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
758 // it is not clear where the underscore that's stripped here is added back in |
3c4460b988bd
Do not strip the leading underscore for typeinfo mangles on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
759 // if (global.params.isWindows) |
3c4460b988bd
Do not strip the leading underscore for typeinfo mangles on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
760 // name++; // C mangling will add it back in |
159 | 761 //printf("name = %s\n", name); |
762 id = Lexer::idPool(name); | |
763 return id; | |
764 } | |
765 | |
766 TypeBasic *Type::isTypeBasic() | |
767 { | |
768 return NULL; | |
769 } | |
770 | |
771 | |
772 void Type::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) | |
773 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
774 //printf("Type::resolve() %s, %d\n", toChars(), ty); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
775 Type *t = semantic(loc, sc); |
159 | 776 *pt = t; |
777 *pe = NULL; | |
778 *ps = NULL; | |
779 } | |
780 | |
781 /******************************* | |
782 * If one of the subtypes of this type is a TypeIdentifier, | |
783 * i.e. it's an unresolved type, return that type. | |
784 */ | |
785 | |
786 Type *Type::reliesOnTident() | |
787 { | |
788 if (!next) | |
789 return NULL; | |
790 else | |
791 return next->reliesOnTident(); | |
792 } | |
793 | |
794 /******************************** | |
795 * We've mistakenly parsed this as a type. | |
796 * Redo it as an Expression. | |
797 * NULL if cannot. | |
798 */ | |
799 | |
800 Expression *Type::toExpression() | |
801 { | |
802 return NULL; | |
803 } | |
804 | |
805 /*************************************** | |
806 * Return !=0 if type has pointers that need to | |
807 * be scanned by the GC during a collection cycle. | |
808 */ | |
809 | |
810 int Type::hasPointers() | |
811 { | |
812 return FALSE; | |
813 } | |
814 | |
815 /* ============================= TypeBasic =========================== */ | |
816 | |
817 TypeBasic::TypeBasic(TY ty) | |
818 : Type(ty, NULL) | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
642
diff
changeset
|
819 { const char *c; |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
642
diff
changeset
|
820 const char *d; |
159 | 821 unsigned flags; |
822 | |
823 #define TFLAGSintegral 1 | |
824 #define TFLAGSfloating 2 | |
825 #define TFLAGSunsigned 4 | |
826 #define TFLAGSreal 8 | |
827 #define TFLAGSimaginary 0x10 | |
828 #define TFLAGScomplex 0x20 | |
829 | |
830 flags = 0; | |
831 switch (ty) | |
832 { | |
833 case Tvoid: d = Token::toChars(TOKvoid); | |
834 c = "void"; | |
835 break; | |
836 | |
837 case Tint8: d = Token::toChars(TOKint8); | |
838 c = "byte"; | |
839 flags |= TFLAGSintegral; | |
840 break; | |
841 | |
842 case Tuns8: d = Token::toChars(TOKuns8); | |
843 c = "ubyte"; | |
844 flags |= TFLAGSintegral | TFLAGSunsigned; | |
845 break; | |
846 | |
847 case Tint16: d = Token::toChars(TOKint16); | |
848 c = "short"; | |
849 flags |= TFLAGSintegral; | |
850 break; | |
851 | |
852 case Tuns16: d = Token::toChars(TOKuns16); | |
853 c = "ushort"; | |
854 flags |= TFLAGSintegral | TFLAGSunsigned; | |
855 break; | |
856 | |
857 case Tint32: d = Token::toChars(TOKint32); | |
858 c = "int"; | |
859 flags |= TFLAGSintegral; | |
860 break; | |
861 | |
862 case Tuns32: d = Token::toChars(TOKuns32); | |
863 c = "uint"; | |
864 flags |= TFLAGSintegral | TFLAGSunsigned; | |
865 break; | |
866 | |
867 case Tfloat32: d = Token::toChars(TOKfloat32); | |
868 c = "float"; | |
869 flags |= TFLAGSfloating | TFLAGSreal; | |
870 break; | |
871 | |
872 case Tint64: d = Token::toChars(TOKint64); | |
873 c = "long"; | |
874 flags |= TFLAGSintegral; | |
875 break; | |
876 | |
877 case Tuns64: d = Token::toChars(TOKuns64); | |
878 c = "ulong"; | |
879 flags |= TFLAGSintegral | TFLAGSunsigned; | |
880 break; | |
881 | |
882 case Tfloat64: d = Token::toChars(TOKfloat64); | |
883 c = "double"; | |
884 flags |= TFLAGSfloating | TFLAGSreal; | |
885 break; | |
886 | |
887 case Tfloat80: d = Token::toChars(TOKfloat80); | |
888 c = "real"; | |
889 flags |= TFLAGSfloating | TFLAGSreal; | |
890 break; | |
891 | |
892 case Timaginary32: d = Token::toChars(TOKimaginary32); | |
893 c = "ifloat"; | |
894 flags |= TFLAGSfloating | TFLAGSimaginary; | |
895 break; | |
896 | |
897 case Timaginary64: d = Token::toChars(TOKimaginary64); | |
898 c = "idouble"; | |
899 flags |= TFLAGSfloating | TFLAGSimaginary; | |
900 break; | |
901 | |
902 case Timaginary80: d = Token::toChars(TOKimaginary80); | |
903 c = "ireal"; | |
904 flags |= TFLAGSfloating | TFLAGSimaginary; | |
905 break; | |
906 | |
907 case Tcomplex32: d = Token::toChars(TOKcomplex32); | |
908 c = "cfloat"; | |
909 flags |= TFLAGSfloating | TFLAGScomplex; | |
910 break; | |
911 | |
912 case Tcomplex64: d = Token::toChars(TOKcomplex64); | |
913 c = "cdouble"; | |
914 flags |= TFLAGSfloating | TFLAGScomplex; | |
915 break; | |
916 | |
917 case Tcomplex80: d = Token::toChars(TOKcomplex80); | |
918 c = "creal"; | |
919 flags |= TFLAGSfloating | TFLAGScomplex; | |
920 break; | |
921 | |
922 | |
923 case Tbit: d = Token::toChars(TOKbit); | |
924 c = "bit"; | |
925 flags |= TFLAGSintegral | TFLAGSunsigned; | |
926 break; | |
927 | |
928 case Tbool: d = "bool"; | |
929 c = d; | |
930 flags |= TFLAGSintegral | TFLAGSunsigned; | |
931 break; | |
932 | |
933 case Tascii: d = Token::toChars(TOKchar); | |
934 c = "char"; | |
935 flags |= TFLAGSintegral | TFLAGSunsigned; | |
936 break; | |
937 | |
938 case Twchar: d = Token::toChars(TOKwchar); | |
939 c = "wchar"; | |
940 flags |= TFLAGSintegral | TFLAGSunsigned; | |
941 break; | |
942 | |
943 case Tdchar: d = Token::toChars(TOKdchar); | |
944 c = "dchar"; | |
945 flags |= TFLAGSintegral | TFLAGSunsigned; | |
946 break; | |
947 | |
948 default: assert(0); | |
949 } | |
950 this->dstring = d; | |
951 this->cstring = c; | |
952 this->flags = flags; | |
953 merge(); | |
954 } | |
955 | |
956 Type *TypeBasic::syntaxCopy() | |
957 { | |
958 // No semantic analysis done on basic types, no need to copy | |
959 return this; | |
960 } | |
961 | |
962 | |
963 char *TypeBasic::toChars() | |
964 { | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
642
diff
changeset
|
965 return (char *)dstring; |
159 | 966 } |
967 | |
968 void TypeBasic::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
969 { | |
970 //printf("TypeBasic::toCBuffer2(mod = %d, this->mod = %d)\n", mod, this->mod); | |
971 if (mod != this->mod) | |
972 { toCBuffer3(buf, hgs, mod); | |
973 return; | |
974 } | |
975 buf->writestring(dstring); | |
976 } | |
977 | |
978 d_uns64 TypeBasic::size(Loc loc) | |
979 { unsigned size; | |
980 | |
981 //printf("TypeBasic::size()\n"); | |
982 switch (ty) | |
983 { | |
984 case Tint8: | |
985 case Tuns8: size = 1; break; | |
986 case Tint16: | |
987 case Tuns16: size = 2; break; | |
988 case Tint32: | |
989 case Tuns32: | |
990 case Tfloat32: | |
991 case Timaginary32: | |
992 size = 4; break; | |
993 case Tint64: | |
994 case Tuns64: | |
995 case Tfloat64: | |
996 case Timaginary64: | |
997 size = 8; break; | |
998 case Tfloat80: | |
999 case Timaginary80: | |
1000 size = REALSIZE; break; | |
1001 case Tcomplex32: | |
1002 size = 8; break; | |
1003 case Tcomplex64: | |
1004 size = 16; break; | |
1005 case Tcomplex80: | |
1006 size = REALSIZE * 2; break; | |
1007 | |
1008 case Tvoid: | |
1009 //size = Type::size(); // error message | |
1010 size = 1; | |
1011 break; | |
1012 | |
1013 case Tbit: size = 1; break; | |
1014 case Tbool: size = 1; break; | |
1015 case Tascii: size = 1; break; | |
1016 case Twchar: size = 2; break; | |
1017 case Tdchar: size = 4; break; | |
1018 | |
1019 default: | |
1020 assert(0); | |
1021 break; | |
1022 } | |
1023 //printf("TypeBasic::size() = %d\n", size); | |
1024 return size; | |
1025 } | |
1026 | |
1027 unsigned TypeBasic::alignsize() | |
1009
b1b23a64ad8c
Use LLVM alignment data instead of hand-picked.
Frits van Bommel <fvbommel wxs.nl>
parents:
1008
diff
changeset
|
1028 { |
1014
47f8b54f90b3
Fixed alignsize for void types. (it's one byte)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
1009
diff
changeset
|
1029 if (ty == Tvoid) |
47f8b54f90b3
Fixed alignsize for void types. (it's one byte)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
1009
diff
changeset
|
1030 return 1; |
1147
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
1031 return GetTypeAlignment(sir, this); |
159 | 1032 } |
1033 | |
1034 | |
1035 Expression *TypeBasic::getProperty(Loc loc, Identifier *ident) | |
1036 { | |
1037 Expression *e; | |
1038 d_int64 ivalue; | |
1039 #ifdef IN_GCC | |
1040 real_t fvalue; | |
1041 #else | |
1042 d_float80 fvalue; | |
1043 #endif | |
1044 | |
1045 //printf("TypeBasic::getProperty('%s')\n", ident->toChars()); | |
1046 if (ident == Id::max) | |
1047 { | |
1048 switch (ty) | |
1049 { | |
1050 case Tint8: ivalue = 0x7F; goto Livalue; | |
1051 case Tuns8: ivalue = 0xFF; goto Livalue; | |
1052 case Tint16: ivalue = 0x7FFFUL; goto Livalue; | |
1053 case Tuns16: ivalue = 0xFFFFUL; goto Livalue; | |
1054 case Tint32: ivalue = 0x7FFFFFFFUL; goto Livalue; | |
1055 case Tuns32: ivalue = 0xFFFFFFFFUL; goto Livalue; | |
1056 case Tint64: ivalue = 0x7FFFFFFFFFFFFFFFLL; goto Livalue; | |
1057 case Tuns64: ivalue = 0xFFFFFFFFFFFFFFFFULL; goto Livalue; | |
1058 case Tbit: ivalue = 1; goto Livalue; | |
1059 case Tbool: ivalue = 1; goto Livalue; | |
1060 case Tchar: ivalue = 0xFF; goto Livalue; | |
1061 case Twchar: ivalue = 0xFFFFUL; goto Livalue; | |
1062 case Tdchar: ivalue = 0x10FFFFUL; goto Livalue; | |
1063 | |
1064 case Tcomplex32: | |
1065 case Timaginary32: | |
1066 case Tfloat32: fvalue = FLT_MAX; goto Lfvalue; | |
1067 case Tcomplex64: | |
1068 case Timaginary64: | |
1069 case Tfloat64: fvalue = DBL_MAX; goto Lfvalue; | |
1070 case Tcomplex80: | |
1071 case Timaginary80: | |
1072 case Tfloat80: fvalue = LDBL_MAX; goto Lfvalue; | |
1073 } | |
1074 } | |
1075 else if (ident == Id::min) | |
1076 { | |
1077 switch (ty) | |
1078 { | |
1079 case Tint8: ivalue = -128; goto Livalue; | |
1080 case Tuns8: ivalue = 0; goto Livalue; | |
1081 case Tint16: ivalue = -32768; goto Livalue; | |
1082 case Tuns16: ivalue = 0; goto Livalue; | |
1083 case Tint32: ivalue = -2147483647L - 1; goto Livalue; | |
1084 case Tuns32: ivalue = 0; goto Livalue; | |
1085 case Tint64: ivalue = (-9223372036854775807LL-1LL); goto Livalue; | |
1086 case Tuns64: ivalue = 0; goto Livalue; | |
1087 case Tbit: ivalue = 0; goto Livalue; | |
1088 case Tbool: ivalue = 0; goto Livalue; | |
1089 case Tchar: ivalue = 0; goto Livalue; | |
1090 case Twchar: ivalue = 0; goto Livalue; | |
1091 case Tdchar: ivalue = 0; goto Livalue; | |
1092 | |
1093 case Tcomplex32: | |
1094 case Timaginary32: | |
1095 case Tfloat32: fvalue = FLT_MIN; goto Lfvalue; | |
1096 case Tcomplex64: | |
1097 case Timaginary64: | |
1098 case Tfloat64: fvalue = DBL_MIN; goto Lfvalue; | |
1099 case Tcomplex80: | |
1100 case Timaginary80: | |
1101 case Tfloat80: fvalue = LDBL_MIN; goto Lfvalue; | |
1102 } | |
1103 } | |
1104 else if (ident == Id::nan) | |
1105 { | |
1106 switch (ty) | |
1107 { | |
1108 case Tcomplex32: | |
1109 case Tcomplex64: | |
1110 case Tcomplex80: | |
1111 case Timaginary32: | |
1112 case Timaginary64: | |
1113 case Timaginary80: | |
1114 case Tfloat32: | |
1115 case Tfloat64: | |
1116 case Tfloat80: | |
1117 { | |
1118 #if IN_GCC | |
1119 // mode doesn't matter, will be converted in RealExp anyway | |
1120 fvalue = real_t::getnan(real_t::LongDouble); | |
1121 #elif __GNUC__ | |
1122 // gcc nan's have the sign bit set by default, so turn it off | |
1123 // Need the volatile to prevent gcc from doing incorrect | |
1124 // constant folding. | |
1125 volatile d_float80 foo; | |
1126 foo = NAN; | |
1008
4c20df3837f9
Preparation for next patch. Don't ask about why this is needed.
Frits van Bommel <fvbommel wxs.nl>
parents:
1006
diff
changeset
|
1127 if (std::signbit(foo)) // signbit sometimes, not always, set |
159 | 1128 foo = -foo; // turn off sign bit |
1129 fvalue = foo; | |
1130 #elif _MSC_VER | |
1131 unsigned long nan[2]= { 0xFFFFFFFF, 0x7FFFFFFF }; | |
1132 fvalue = *(double*)nan; | |
1133 #else | |
1134 fvalue = NAN; | |
1135 #endif | |
1136 goto Lfvalue; | |
1137 } | |
1138 } | |
1139 } | |
1140 else if (ident == Id::infinity) | |
1141 { | |
1142 switch (ty) | |
1143 { | |
1144 case Tcomplex32: | |
1145 case Tcomplex64: | |
1146 case Tcomplex80: | |
1147 case Timaginary32: | |
1148 case Timaginary64: | |
1149 case Timaginary80: | |
1150 case Tfloat32: | |
1151 case Tfloat64: | |
1152 case Tfloat80: | |
1153 #if IN_GCC | |
1154 fvalue = real_t::getinfinity(); | |
1155 #elif __GNUC__ | |
1156 fvalue = 1 / zero; | |
1157 #elif _MSC_VER | |
1158 fvalue = std::numeric_limits<long double>::infinity(); | |
1159 #else | |
1160 fvalue = INFINITY; | |
1161 #endif | |
1162 goto Lfvalue; | |
1163 } | |
1164 } | |
1165 else if (ident == Id::dig) | |
1166 { | |
1167 switch (ty) | |
1168 { | |
1169 case Tcomplex32: | |
1170 case Timaginary32: | |
1171 case Tfloat32: ivalue = FLT_DIG; goto Lint; | |
1172 case Tcomplex64: | |
1173 case Timaginary64: | |
1174 case Tfloat64: ivalue = DBL_DIG; goto Lint; | |
1175 case Tcomplex80: | |
1176 case Timaginary80: | |
1177 case Tfloat80: ivalue = LDBL_DIG; goto Lint; | |
1178 } | |
1179 } | |
1180 else if (ident == Id::epsilon) | |
1181 { | |
1182 switch (ty) | |
1183 { | |
1184 case Tcomplex32: | |
1185 case Timaginary32: | |
1186 case Tfloat32: fvalue = FLT_EPSILON; goto Lfvalue; | |
1187 case Tcomplex64: | |
1188 case Timaginary64: | |
1189 case Tfloat64: fvalue = DBL_EPSILON; goto Lfvalue; | |
1190 case Tcomplex80: | |
1191 case Timaginary80: | |
1192 case Tfloat80: fvalue = LDBL_EPSILON; goto Lfvalue; | |
1193 } | |
1194 } | |
1195 else if (ident == Id::mant_dig) | |
1196 { | |
1197 switch (ty) | |
1198 { | |
1199 case Tcomplex32: | |
1200 case Timaginary32: | |
1201 case Tfloat32: ivalue = FLT_MANT_DIG; goto Lint; | |
1202 case Tcomplex64: | |
1203 case Timaginary64: | |
1204 case Tfloat64: ivalue = DBL_MANT_DIG; goto Lint; | |
1205 case Tcomplex80: | |
1206 case Timaginary80: | |
1207 case Tfloat80: ivalue = LDBL_MANT_DIG; goto Lint; | |
1208 } | |
1209 } | |
1210 else if (ident == Id::max_10_exp) | |
1211 { | |
1212 switch (ty) | |
1213 { | |
1214 case Tcomplex32: | |
1215 case Timaginary32: | |
1216 case Tfloat32: ivalue = FLT_MAX_10_EXP; goto Lint; | |
1217 case Tcomplex64: | |
1218 case Timaginary64: | |
1219 case Tfloat64: ivalue = DBL_MAX_10_EXP; goto Lint; | |
1220 case Tcomplex80: | |
1221 case Timaginary80: | |
1222 case Tfloat80: ivalue = LDBL_MAX_10_EXP; goto Lint; | |
1223 } | |
1224 } | |
1225 else if (ident == Id::max_exp) | |
1226 { | |
1227 switch (ty) | |
1228 { | |
1229 case Tcomplex32: | |
1230 case Timaginary32: | |
1231 case Tfloat32: ivalue = FLT_MAX_EXP; goto Lint; | |
1232 case Tcomplex64: | |
1233 case Timaginary64: | |
1234 case Tfloat64: ivalue = DBL_MAX_EXP; goto Lint; | |
1235 case Tcomplex80: | |
1236 case Timaginary80: | |
1237 case Tfloat80: ivalue = LDBL_MAX_EXP; goto Lint; | |
1238 } | |
1239 } | |
1240 else if (ident == Id::min_10_exp) | |
1241 { | |
1242 switch (ty) | |
1243 { | |
1244 case Tcomplex32: | |
1245 case Timaginary32: | |
1246 case Tfloat32: ivalue = FLT_MIN_10_EXP; goto Lint; | |
1247 case Tcomplex64: | |
1248 case Timaginary64: | |
1249 case Tfloat64: ivalue = DBL_MIN_10_EXP; goto Lint; | |
1250 case Tcomplex80: | |
1251 case Timaginary80: | |
1252 case Tfloat80: ivalue = LDBL_MIN_10_EXP; goto Lint; | |
1253 } | |
1254 } | |
1255 else if (ident == Id::min_exp) | |
1256 { | |
1257 switch (ty) | |
1258 { | |
1259 case Tcomplex32: | |
1260 case Timaginary32: | |
1261 case Tfloat32: ivalue = FLT_MIN_EXP; goto Lint; | |
1262 case Tcomplex64: | |
1263 case Timaginary64: | |
1264 case Tfloat64: ivalue = DBL_MIN_EXP; goto Lint; | |
1265 case Tcomplex80: | |
1266 case Timaginary80: | |
1267 case Tfloat80: ivalue = LDBL_MIN_EXP; goto Lint; | |
1268 } | |
1269 } | |
1270 | |
1271 Ldefault: | |
1272 return Type::getProperty(loc, ident); | |
1273 | |
1274 Livalue: | |
1275 e = new IntegerExp(loc, ivalue, this); | |
1276 return e; | |
1277 | |
1278 Lfvalue: | |
1279 if (isreal() || isimaginary()) | |
1280 e = new RealExp(loc, fvalue, this); | |
1281 else | |
1282 { | |
1283 complex_t cvalue; | |
1284 | |
1285 #if __DMC__ | |
1286 //((real_t *)&cvalue)[0] = fvalue; | |
1287 //((real_t *)&cvalue)[1] = fvalue; | |
1288 cvalue = fvalue + fvalue * I; | |
1289 #else | |
1290 cvalue.re = fvalue; | |
1291 cvalue.im = fvalue; | |
1292 #endif | |
1293 //for (int i = 0; i < 20; i++) | |
1294 // printf("%02x ", ((unsigned char *)&cvalue)[i]); | |
1295 //printf("\n"); | |
1296 e = new ComplexExp(loc, cvalue, this); | |
1297 } | |
1298 return e; | |
1299 | |
1300 Lint: | |
1301 e = new IntegerExp(loc, ivalue, Type::tint32); | |
1302 return e; | |
1303 } | |
1304 | |
1305 Expression *TypeBasic::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
1306 { | |
1307 #if LOGDOTEXP | |
1308 printf("TypeBasic::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
1309 #endif | |
1310 Type *t; | |
1311 | |
1312 if (ident == Id::re) | |
1313 { | |
1314 switch (ty) | |
1315 { | |
1316 case Tcomplex32: t = tfloat32; goto L1; | |
1317 case Tcomplex64: t = tfloat64; goto L1; | |
1318 case Tcomplex80: t = tfloat80; goto L1; | |
1319 L1: | |
1320 e = e->castTo(sc, t); | |
1321 break; | |
1322 | |
1323 case Tfloat32: | |
1324 case Tfloat64: | |
1325 case Tfloat80: | |
1326 break; | |
1327 | |
1328 case Timaginary32: t = tfloat32; goto L2; | |
1329 case Timaginary64: t = tfloat64; goto L2; | |
1330 case Timaginary80: t = tfloat80; goto L2; | |
1331 L2: | |
1332 e = new RealExp(0, 0.0, t); | |
1333 break; | |
1334 | |
1335 default: | |
1336 return Type::getProperty(e->loc, ident); | |
1337 } | |
1338 } | |
1339 else if (ident == Id::im) | |
1340 { Type *t2; | |
1341 | |
1342 switch (ty) | |
1343 { | |
1344 case Tcomplex32: t = timaginary32; t2 = tfloat32; goto L3; | |
1345 case Tcomplex64: t = timaginary64; t2 = tfloat64; goto L3; | |
1346 case Tcomplex80: t = timaginary80; t2 = tfloat80; goto L3; | |
1347 L3: | |
1348 e = e->castTo(sc, t); | |
1349 e->type = t2; | |
1350 break; | |
1351 | |
1352 case Timaginary32: t = tfloat32; goto L4; | |
1353 case Timaginary64: t = tfloat64; goto L4; | |
1354 case Timaginary80: t = tfloat80; goto L4; | |
1355 L4: | |
1356 e->type = t; | |
1357 break; | |
1358 | |
1359 case Tfloat32: | |
1360 case Tfloat64: | |
1361 case Tfloat80: | |
1362 e = new RealExp(0, 0.0, this); | |
1363 break; | |
1364 | |
1365 default: | |
1366 return Type::getProperty(e->loc, ident); | |
1367 } | |
1368 } | |
1369 else | |
1370 { | |
1371 return Type::dotExp(sc, e, ident); | |
1372 } | |
1373 return e; | |
1374 } | |
1375 | |
1376 Expression *TypeBasic::defaultInit(Loc loc) | |
1377 { integer_t value = 0; | |
1378 | |
1379 #if LOGDEFAULTINIT | |
1380 printf("TypeBasic::defaultInit() '%s'\n", toChars()); | |
1381 #endif | |
1382 switch (ty) | |
1383 { | |
591
e6bcc4d9e5ff
Add _d_newarrayvT and _d_newarraymvT to create arrays without initialization.
Christian Kamm <kamm incasoftware de>
parents:
585
diff
changeset
|
1384 case Tvoid: |
e6bcc4d9e5ff
Add _d_newarrayvT and _d_newarraymvT to create arrays without initialization.
Christian Kamm <kamm incasoftware de>
parents:
585
diff
changeset
|
1385 return new IntegerExp(loc, value, Type::tbool); |
e6bcc4d9e5ff
Add _d_newarrayvT and _d_newarraymvT to create arrays without initialization.
Christian Kamm <kamm incasoftware de>
parents:
585
diff
changeset
|
1386 |
159 | 1387 case Tchar: |
1388 value = 0xFF; | |
1389 break; | |
1390 | |
1391 case Twchar: | |
1392 case Tdchar: | |
1393 value = 0xFFFF; | |
1394 break; | |
1395 | |
1396 case Timaginary32: | |
1397 case Timaginary64: | |
1398 case Timaginary80: | |
1399 case Tfloat32: | |
1400 case Tfloat64: | |
1401 case Tfloat80: | |
1402 case Tcomplex32: | |
1403 case Tcomplex64: | |
1404 case Tcomplex80: | |
1405 return getProperty(loc, Id::nan); | |
1406 } | |
1407 return new IntegerExp(loc, value, this); | |
1408 } | |
1409 | |
1410 int TypeBasic::isZeroInit() | |
1411 { | |
1412 switch (ty) | |
1413 { | |
1414 case Tchar: | |
1415 case Twchar: | |
1416 case Tdchar: | |
1417 case Timaginary32: | |
1418 case Timaginary64: | |
1419 case Timaginary80: | |
1420 case Tfloat32: | |
1421 case Tfloat64: | |
1422 case Tfloat80: | |
1423 case Tcomplex32: | |
1424 case Tcomplex64: | |
1425 case Tcomplex80: | |
1426 return 0; // no | |
1427 } | |
1428 return 1; // yes | |
1429 } | |
1430 | |
1431 int TypeBasic::isbit() | |
1432 { | |
1433 return (ty == Tbit); | |
1434 } | |
1435 | |
1436 int TypeBasic::isintegral() | |
1437 { | |
1438 //printf("TypeBasic::isintegral('%s') x%x\n", toChars(), flags); | |
1439 return flags & TFLAGSintegral; | |
1440 } | |
1441 | |
1442 int TypeBasic::isfloating() | |
1443 { | |
1444 return flags & TFLAGSfloating; | |
1445 } | |
1446 | |
1447 int TypeBasic::isreal() | |
1448 { | |
1449 return flags & TFLAGSreal; | |
1450 } | |
1451 | |
1452 int TypeBasic::isimaginary() | |
1453 { | |
1454 return flags & TFLAGSimaginary; | |
1455 } | |
1456 | |
1457 int TypeBasic::iscomplex() | |
1458 { | |
1459 return flags & TFLAGScomplex; | |
1460 } | |
1461 | |
1462 int TypeBasic::isunsigned() | |
1463 { | |
1464 return flags & TFLAGSunsigned; | |
1465 } | |
1466 | |
1467 int TypeBasic::isscalar() | |
1468 { | |
1469 return flags & (TFLAGSintegral | TFLAGSfloating); | |
1470 } | |
1471 | |
1472 MATCH TypeBasic::implicitConvTo(Type *to) | |
1473 { | |
1474 //printf("TypeBasic::implicitConvTo(%s) from %s\n", to->toChars(), toChars()); | |
1475 if (this == to) | |
1476 return MATCHexact; | |
1477 | |
1478 if (ty == Tvoid || to->ty == Tvoid) | |
1479 return MATCHnomatch; | |
1480 if (1 || global.params.Dversion == 1) | |
1481 { | |
1482 if (to->ty == Tbool) | |
1483 return MATCHnomatch; | |
1484 } | |
1485 else | |
1486 { | |
1487 if (ty == Tbool || to->ty == Tbool) | |
1488 return MATCHnomatch; | |
1489 } | |
1490 if (!to->isTypeBasic()) | |
1491 return MATCHnomatch; | |
1492 | |
1493 TypeBasic *tob = (TypeBasic *)to; | |
1494 if (flags & TFLAGSintegral) | |
1495 { | |
1496 // Disallow implicit conversion of integers to imaginary or complex | |
1497 if (tob->flags & (TFLAGSimaginary | TFLAGScomplex)) | |
1498 return MATCHnomatch; | |
1499 | |
1500 // If converting to integral | |
1501 if (0 && global.params.Dversion > 1 && tob->flags & TFLAGSintegral) | |
1502 { d_uns64 sz = size(0); | |
1503 d_uns64 tosz = tob->size(0); | |
1504 | |
1505 /* Can't convert to smaller size or, if same size, change sign | |
1506 */ | |
1507 if (sz > tosz) | |
1508 return MATCHnomatch; | |
1509 | |
1510 /*if (sz == tosz && (flags ^ tob->flags) & TFLAGSunsigned) | |
1511 return MATCHnomatch;*/ | |
1512 } | |
1513 } | |
1514 else if (flags & TFLAGSfloating) | |
1515 { | |
1516 // Disallow implicit conversion of floating point to integer | |
1517 if (tob->flags & TFLAGSintegral) | |
1518 return MATCHnomatch; | |
1519 | |
1520 assert(tob->flags & TFLAGSfloating); | |
1521 | |
1522 // Disallow implicit conversion from complex to non-complex | |
1523 if (flags & TFLAGScomplex && !(tob->flags & TFLAGScomplex)) | |
1524 return MATCHnomatch; | |
1525 | |
1526 // Disallow implicit conversion of real or imaginary to complex | |
1527 if (flags & (TFLAGSreal | TFLAGSimaginary) && | |
1528 tob->flags & TFLAGScomplex) | |
1529 return MATCHnomatch; | |
1530 | |
1531 // Disallow implicit conversion to-from real and imaginary | |
1532 if ((flags & (TFLAGSreal | TFLAGSimaginary)) != | |
1533 (tob->flags & (TFLAGSreal | TFLAGSimaginary))) | |
1534 return MATCHnomatch; | |
1535 } | |
1536 return MATCHconvert; | |
1537 } | |
1538 | |
1539 TypeBasic *TypeBasic::isTypeBasic() | |
1540 { | |
1541 return (TypeBasic *)this; | |
1542 } | |
1543 | |
1544 /***************************** TypeArray *****************************/ | |
1545 | |
1546 TypeArray::TypeArray(TY ty, Type *next) | |
1547 : Type(ty, next) | |
1548 { | |
1549 } | |
1550 | |
1551 Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
1552 { | |
1553 Type *n = this->next->toBasetype(); // uncover any typedef's | |
1554 | |
1555 #if LOGDOTEXP | |
1556 printf("TypeArray::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
1557 #endif | |
1558 if (ident == Id::reverse && (n->ty == Tchar || n->ty == Twchar)) | |
1559 { | |
1560 Expression *ec; | |
1561 Expressions *arguments; | |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1562 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
1563 //LDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1564 static FuncDeclaration *adReverseChar_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1565 if(!adReverseChar_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1566 Arguments* args = new Arguments; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1567 Type* arrty = Type::tchar->arrayOf(); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1568 args->push(new Argument(STCin, arrty, NULL, NULL)); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1569 adReverseChar_fd = FuncDeclaration::genCfunc(args, arrty, "_adReverseChar"); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1570 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1571 static FuncDeclaration *adReverseWchar_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1572 if(!adReverseWchar_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1573 Arguments* args = new Arguments; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1574 Type* arrty = Type::twchar->arrayOf(); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1575 args->push(new Argument(STCin, arrty, NULL, NULL)); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1576 adReverseWchar_fd = FuncDeclaration::genCfunc(args, arrty, "_adReverseWchar"); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1577 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1578 |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1579 if(n->ty == Twchar) |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1580 ec = new VarExp(0, adReverseWchar_fd); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1581 else |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1582 ec = new VarExp(0, adReverseChar_fd); |
159 | 1583 e = e->castTo(sc, n->arrayOf()); // convert to dynamic array |
1584 arguments = new Expressions(); | |
1585 arguments->push(e); | |
1586 e = new CallExp(e->loc, ec, arguments); | |
1587 e->type = next->arrayOf(); | |
1588 } | |
1589 else if (ident == Id::sort && (n->ty == Tchar || n->ty == Twchar)) | |
1590 { | |
1591 Expression *ec; | |
1592 Expressions *arguments; | |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1593 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
1594 //LDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1595 static FuncDeclaration *adSortChar_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1596 if(!adSortChar_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1597 Arguments* args = new Arguments; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1598 Type* arrty = Type::tchar->arrayOf(); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1599 args->push(new Argument(STCin, arrty, NULL, NULL)); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1600 adSortChar_fd = FuncDeclaration::genCfunc(args, arrty, "_adSortChar"); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1601 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1602 static FuncDeclaration *adSortWchar_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1603 if(!adSortWchar_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1604 Arguments* args = new Arguments; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1605 Type* arrty = Type::twchar->arrayOf(); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1606 args->push(new Argument(STCin, arrty, NULL, NULL)); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1607 adSortWchar_fd = FuncDeclaration::genCfunc(args, arrty, "_adSortWchar"); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1608 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1609 |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1610 if(n->ty == Twchar) |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1611 ec = new VarExp(0, adSortWchar_fd); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1612 else |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1613 ec = new VarExp(0, adSortChar_fd); |
159 | 1614 e = e->castTo(sc, n->arrayOf()); // convert to dynamic array |
1615 arguments = new Expressions(); | |
1616 arguments->push(e); | |
1617 e = new CallExp(e->loc, ec, arguments); | |
1618 e->type = next->arrayOf(); | |
1619 } | |
1620 else if (ident == Id::reverse || ident == Id::dup) | |
1621 { | |
1622 Expression *ec; | |
1623 Expressions *arguments; | |
1624 int size = next->size(e->loc); | |
1625 int dup; | |
1626 | |
1627 assert(size); | |
1628 dup = (ident == Id::dup); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
1629 //LDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1630 static FuncDeclaration *adDup_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1631 if(!adDup_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1632 Arguments* args = new Arguments; |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
375
diff
changeset
|
1633 args->push(new Argument(STCin, Type::typeinfo->type, NULL, NULL)); |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1634 args->push(new Argument(STCin, Type::tvoid->arrayOf(), NULL, NULL)); |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1635 adDup_fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), Id::adDup); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1636 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1637 static FuncDeclaration *adReverse_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1638 if(!adReverse_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1639 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1640 args->push(new Argument(STCin, Type::tvoid->arrayOf(), NULL, NULL)); |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
375
diff
changeset
|
1641 args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1642 adReverse_fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), Id::adReverse); |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
375
diff
changeset
|
1643 } |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1644 |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1645 if(dup) |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1646 ec = new VarExp(0, adDup_fd); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1647 else |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1648 ec = new VarExp(0, adReverse_fd); |
159 | 1649 e = e->castTo(sc, n->arrayOf()); // convert to dynamic array |
1650 arguments = new Expressions(); | |
1651 if (dup) | |
1652 arguments->push(getTypeInfo(sc)); | |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1653 |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1654 // LDC repaint array type to void[] |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1655 if (n->ty != Tvoid) { |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1656 e = new CastExp(e->loc, e, e->type); |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1657 e->type = Type::tvoid->arrayOf(); |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1658 } |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1659 arguments->push(e); |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1660 |
159 | 1661 if (!dup) |
881
1c2faa8325d1
Fixed 64bit problem in mtype.c with _adReverse runtime call, fixes #161 .
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
1662 arguments->push(new IntegerExp(0, size, Type::tsize_t)); |
159 | 1663 e = new CallExp(e->loc, ec, arguments); |
1664 e->type = next->arrayOf(); | |
1665 } | |
1666 else if (ident == Id::sort) | |
1667 { | |
1668 Expression *ec; | |
1669 Expressions *arguments; | |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1670 bool isBit = (n->ty == Tbit); |
159 | 1671 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
1672 //LDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1673 static FuncDeclaration *adSort_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1674 if(!adSort_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1675 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1676 args->push(new Argument(STCin, Type::tvoid->arrayOf(), NULL, NULL)); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1677 args->push(new Argument(STCin, Type::typeinfo->type, NULL, NULL)); |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1678 adSort_fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), "_adSort"); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1679 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1680 static FuncDeclaration *adSortBit_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1681 if(!adSortBit_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1682 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1683 args->push(new Argument(STCin, Type::tvoid->arrayOf(), NULL, NULL)); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1684 args->push(new Argument(STCin, Type::typeinfo->type, NULL, NULL)); |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1685 adSortBit_fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), "_adSortBit"); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1686 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1687 |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1688 if(isBit) |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1689 ec = new VarExp(0, adSortBit_fd); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1690 else |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
1691 ec = new VarExp(0, adSort_fd); |
159 | 1692 e = e->castTo(sc, n->arrayOf()); // convert to dynamic array |
1693 arguments = new Expressions(); | |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1694 |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1695 // LDC repaint array type to void[] |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1696 if (n->ty != Tvoid) { |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1697 e = new CastExp(e->loc, e, e->type); |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1698 e->type = Type::tvoid->arrayOf(); |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1699 } |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1700 arguments->push(e); |
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
1701 |
509
337554fd34f1
Fixed mini/missingti.d
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
489
diff
changeset
|
1702 if (next->ty != Tbit) |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
1703 arguments->push(n->getTypeInfo(sc)); // LDC, we don't support the getInternalTypeInfo |
509
337554fd34f1
Fixed mini/missingti.d
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
489
diff
changeset
|
1704 // optimization arbitrarily, not yet at least... |
159 | 1705 e = new CallExp(e->loc, ec, arguments); |
1706 e->type = next->arrayOf(); | |
1707 } | |
1708 else | |
1709 { | |
1710 e = Type::dotExp(sc, e, ident); | |
1711 } | |
1712 return e; | |
1713 } | |
1714 | |
1715 | |
1716 /***************************** TypeSArray *****************************/ | |
1717 | |
1718 TypeSArray::TypeSArray(Type *t, Expression *dim) | |
1719 : TypeArray(Tsarray, t) | |
1720 { | |
1721 //printf("TypeSArray(%s)\n", dim->toChars()); | |
1722 this->dim = dim; | |
1723 } | |
1724 | |
1725 Type *TypeSArray::syntaxCopy() | |
1726 { | |
1727 Type *t = next->syntaxCopy(); | |
1728 Expression *e = dim->syntaxCopy(); | |
1729 t = new TypeSArray(t, e); | |
1730 return t; | |
1731 } | |
1732 | |
1733 d_uns64 TypeSArray::size(Loc loc) | |
1734 { integer_t sz; | |
1735 | |
1736 if (!dim) | |
1737 return Type::size(loc); | |
1738 sz = dim->toInteger(); | |
1739 if (next->toBasetype()->ty == Tbit) // if array of bits | |
1740 { | |
1741 if (sz + 31 < sz) | |
1742 goto Loverflow; | |
1743 sz = ((sz + 31) & ~31) / 8; // size in bytes, rounded up to 32 bit dwords | |
1744 } | |
1745 else | |
1746 { integer_t n, n2; | |
1747 | |
1748 n = next->size(); | |
1749 n2 = n * sz; | |
1750 if (n && (n2 / n) != sz) | |
1751 goto Loverflow; | |
1752 sz = n2; | |
1753 } | |
1754 return sz; | |
1755 | |
1756 Loverflow: | |
1133
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1124
diff
changeset
|
1757 error(loc, "index %jd overflow for static array", sz); |
159 | 1758 return 1; |
1759 } | |
1760 | |
1761 unsigned TypeSArray::alignsize() | |
1762 { | |
1763 return next->alignsize(); | |
1764 } | |
1765 | |
1766 /************************** | |
1767 * This evaluates exp while setting length to be the number | |
1768 * of elements in the tuple t. | |
1769 */ | |
1770 Expression *semanticLength(Scope *sc, Type *t, Expression *exp) | |
1771 { | |
1772 if (t->ty == Ttuple) | |
1773 { ScopeDsymbol *sym = new ArrayScopeSymbol((TypeTuple *)t); | |
1774 sym->parent = sc->scopesym; | |
1775 sc = sc->push(sym); | |
1776 | |
1777 exp = exp->semantic(sc); | |
1778 | |
1779 sc->pop(); | |
1780 } | |
1781 else | |
1782 exp = exp->semantic(sc); | |
1783 return exp; | |
1784 } | |
1785 | |
1786 Expression *semanticLength(Scope *sc, TupleDeclaration *s, Expression *exp) | |
1787 { | |
1788 ScopeDsymbol *sym = new ArrayScopeSymbol(s); | |
1789 sym->parent = sc->scopesym; | |
1790 sc = sc->push(sym); | |
1791 | |
1792 exp = exp->semantic(sc); | |
1793 | |
1794 sc->pop(); | |
1795 return exp; | |
1796 } | |
1797 | |
1798 void TypeSArray::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) | |
1799 { | |
1800 //printf("TypeSArray::resolve() %s\n", toChars()); | |
1801 next->resolve(loc, sc, pe, pt, ps); | |
1802 //printf("s = %p, e = %p, t = %p\n", *ps, *pe, *pt); | |
1803 if (*pe) | |
1804 { // It's really an index expression | |
1805 Expression *e; | |
1806 e = new IndexExp(loc, *pe, dim); | |
1807 *pe = e; | |
1808 } | |
1809 else if (*ps) | |
1810 { Dsymbol *s = *ps; | |
1811 TupleDeclaration *td = s->isTupleDeclaration(); | |
1812 if (td) | |
1813 { | |
1814 ScopeDsymbol *sym = new ArrayScopeSymbol(td); | |
1815 sym->parent = sc->scopesym; | |
1816 sc = sc->push(sym); | |
1817 | |
1818 dim = dim->semantic(sc); | |
1819 dim = dim->optimize(WANTvalue | WANTinterpret); | |
1820 uinteger_t d = dim->toUInteger(); | |
1821 | |
1822 sc = sc->pop(); | |
1823 | |
1824 if (d >= td->objects->dim) | |
1133
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1124
diff
changeset
|
1825 { error(loc, "tuple index %ju exceeds %u", d, td->objects->dim); |
159 | 1826 goto Ldefault; |
1827 } | |
1828 Object *o = (Object *)td->objects->data[(size_t)d]; | |
1829 if (o->dyncast() == DYNCAST_DSYMBOL) | |
1830 { | |
1831 *ps = (Dsymbol *)o; | |
1832 return; | |
1833 } | |
1834 if (o->dyncast() == DYNCAST_EXPRESSION) | |
1835 { | |
1836 *ps = NULL; | |
1837 *pe = (Expression *)o; | |
1838 return; | |
1839 } | |
1840 | |
1841 /* Create a new TupleDeclaration which | |
1842 * is a slice [d..d+1] out of the old one. | |
1843 * Do it this way because TemplateInstance::semanticTiargs() | |
1844 * can handle unresolved Objects this way. | |
1845 */ | |
1846 Objects *objects = new Objects; | |
1847 objects->setDim(1); | |
1848 objects->data[0] = o; | |
1849 | |
1850 TupleDeclaration *tds = new TupleDeclaration(loc, td->ident, objects); | |
1851 *ps = tds; | |
1852 } | |
1853 else | |
1854 goto Ldefault; | |
1855 } | |
1856 else | |
1857 { | |
1858 Ldefault: | |
1859 Type::resolve(loc, sc, pe, pt, ps); | |
1860 } | |
1861 } | |
1862 | |
1863 Type *TypeSArray::semantic(Loc loc, Scope *sc) | |
1864 { | |
1865 //printf("TypeSArray::semantic() %s\n", toChars()); | |
1866 | |
1867 Type *t; | |
1868 Expression *e; | |
1869 Dsymbol *s; | |
1870 next->resolve(loc, sc, &e, &t, &s); | |
1871 if (dim && s && s->isTupleDeclaration()) | |
1872 { TupleDeclaration *sd = s->isTupleDeclaration(); | |
1873 | |
1874 dim = semanticLength(sc, sd, dim); | |
1875 dim = dim->optimize(WANTvalue | WANTinterpret); | |
1876 uinteger_t d = dim->toUInteger(); | |
1877 | |
1878 if (d >= sd->objects->dim) | |
1133
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1124
diff
changeset
|
1879 { error(loc, "tuple index %ju exceeds %u", d, sd->objects->dim); |
159 | 1880 return Type::terror; |
1881 } | |
1882 Object *o = (Object *)sd->objects->data[(size_t)d]; | |
1883 if (o->dyncast() != DYNCAST_TYPE) | |
1884 { error(loc, "%s is not a type", toChars()); | |
1885 return Type::terror; | |
1886 } | |
1887 t = (Type *)o; | |
1888 return t; | |
1889 } | |
1890 | |
1891 next = next->semantic(loc,sc); | |
1892 Type *tbn = next->toBasetype(); | |
1893 | |
1894 if (dim) | |
1895 { integer_t n, n2; | |
1896 | |
1897 dim = semanticLength(sc, tbn, dim); | |
1898 | |
1899 dim = dim->optimize(WANTvalue | WANTinterpret); | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
1900 if (sc && sc->parameterSpecialization && dim->op == TOKvar && |
159 | 1901 ((VarExp *)dim)->var->storage_class & STCtemplateparameter) |
1902 { | |
1903 /* It could be a template parameter N which has no value yet: | |
1904 * template Foo(T : T[N], size_t N); | |
1905 */ | |
1906 return this; | |
1907 } | |
1908 integer_t d1 = dim->toInteger(); | |
1909 dim = dim->castTo(sc, tsize_t); | |
1910 dim = dim->optimize(WANTvalue); | |
1911 integer_t d2 = dim->toInteger(); | |
1912 | |
1913 if (d1 != d2) | |
1914 goto Loverflow; | |
1915 | |
1916 if (tbn->isintegral() || | |
1917 tbn->isfloating() || | |
1918 tbn->ty == Tpointer || | |
1919 tbn->ty == Tarray || | |
1920 tbn->ty == Tsarray || | |
1921 tbn->ty == Taarray || | |
1922 tbn->ty == Tclass) | |
1923 { | |
1924 /* Only do this for types that don't need to have semantic() | |
1925 * run on them for the size, since they may be forward referenced. | |
1926 */ | |
1927 n = tbn->size(loc); | |
1928 n2 = n * d2; | |
1929 if ((int)n2 < 0) | |
1930 goto Loverflow; | |
1931 if (n2 >= 0x1000000) // put a 'reasonable' limit on it | |
1932 goto Loverflow; | |
1933 if (n && n2 / n != d2) | |
1934 { | |
1935 Loverflow: | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
1936 error(loc, "index %jd overflow for static array", d1); |
159 | 1937 dim = new IntegerExp(0, 1, tsize_t); |
1938 } | |
1939 } | |
1940 } | |
1941 switch (tbn->ty) | |
1942 { | |
1943 case Ttuple: | |
1944 { // Index the tuple to get the type | |
1945 assert(dim); | |
1946 TypeTuple *tt = (TypeTuple *)tbn; | |
1947 uinteger_t d = dim->toUInteger(); | |
1948 | |
1949 if (d >= tt->arguments->dim) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
1950 { error(loc, "tuple index %ju exceeds %u", d, tt->arguments->dim); |
159 | 1951 return Type::terror; |
1952 } | |
1953 Argument *arg = (Argument *)tt->arguments->data[(size_t)d]; | |
1954 return arg->type; | |
1955 } | |
1956 case Tfunction: | |
1957 case Tnone: | |
1958 error(loc, "can't have array of %s", tbn->toChars()); | |
1959 tbn = next = tint32; | |
1960 break; | |
1961 } | |
1962 if (tbn->isauto()) | |
1963 error(loc, "cannot have array of auto %s", tbn->toChars()); | |
1964 return merge(); | |
1965 } | |
1966 | |
1967 void TypeSArray::toDecoBuffer(OutBuffer *buf) | |
1968 { | |
1969 buf->writeByte(mangleChar[ty]); | |
1970 if (dim) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
1971 buf->printf("%ju", dim->toInteger()); |
159 | 1972 if (next) |
1973 next->toDecoBuffer(buf); | |
1974 } | |
1975 | |
1976 void TypeSArray::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
1977 { | |
1978 if (mod != this->mod) | |
1979 { toCBuffer3(buf, hgs, mod); | |
1980 return; | |
1981 } | |
1982 next->toCBuffer2(buf, hgs, this->mod); | |
1983 buf->printf("[%s]", dim->toChars()); | |
1984 } | |
1985 | |
1986 Expression *TypeSArray::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
1987 { | |
1988 #if LOGDOTEXP | |
1989 printf("TypeSArray::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
1990 #endif | |
1991 if (ident == Id::length) | |
1992 { | |
1993 e = dim; | |
1994 } | |
1995 else if (ident == Id::ptr) | |
1996 { | |
1997 e = e->castTo(sc, next->pointerTo()); | |
1998 } | |
1999 else | |
2000 { | |
2001 e = TypeArray::dotExp(sc, e, ident); | |
2002 } | |
2003 return e; | |
2004 } | |
2005 | |
2006 int TypeSArray::isString() | |
2007 { | |
2008 TY nty = next->toBasetype()->ty; | |
2009 return nty == Tchar || nty == Twchar || nty == Tdchar; | |
2010 } | |
2011 | |
2012 unsigned TypeSArray::memalign(unsigned salign) | |
2013 { | |
2014 return next->memalign(salign); | |
2015 } | |
2016 | |
2017 MATCH TypeSArray::implicitConvTo(Type *to) | |
2018 { | |
2019 //printf("TypeSArray::implicitConvTo()\n"); | |
2020 | |
2021 // Allow implicit conversion of static array to pointer or dynamic array | |
2022 if ((IMPLICIT_ARRAY_TO_PTR && to->ty == Tpointer) && | |
2023 (to->next->ty == Tvoid || next->equals(to->next) | |
2024 /*|| to->next->isBaseOf(next)*/)) | |
2025 { | |
2026 return MATCHconvert; | |
2027 } | |
2028 if (to->ty == Tarray) | |
2029 { int offset = 0; | |
2030 | |
2031 if (next->equals(to->next) || | |
2032 (to->next->isBaseOf(next, &offset) && offset == 0) || | |
2033 to->next->ty == Tvoid) | |
2034 return MATCHconvert; | |
2035 } | |
2036 #if 0 | |
2037 if (to->ty == Tsarray) | |
2038 { | |
2039 TypeSArray *tsa = (TypeSArray *)to; | |
2040 | |
2041 if (next->equals(tsa->next) && dim->equals(tsa->dim)) | |
2042 { | |
2043 return MATCHconvert; | |
2044 } | |
2045 } | |
2046 #endif | |
2047 return Type::implicitConvTo(to); | |
2048 } | |
2049 | |
2050 Expression *TypeSArray::defaultInit(Loc loc) | |
2051 { | |
2052 #if LOGDEFAULTINIT | |
2053 printf("TypeSArray::defaultInit() '%s'\n", toChars()); | |
2054 #endif | |
2055 return next->defaultInit(loc); | |
2056 } | |
2057 | |
2058 int TypeSArray::isZeroInit() | |
2059 { | |
2060 return next->isZeroInit(); | |
2061 } | |
2062 | |
2063 | |
2064 Expression *TypeSArray::toExpression() | |
2065 { | |
2066 Expression *e = next->toExpression(); | |
2067 if (e) | |
2068 { Expressions *arguments = new Expressions(); | |
2069 arguments->push(dim); | |
2070 e = new ArrayExp(dim->loc, e, arguments); | |
2071 } | |
2072 return e; | |
2073 } | |
2074 | |
2075 int TypeSArray::hasPointers() | |
2076 { | |
2077 return next->hasPointers(); | |
2078 } | |
2079 | |
2080 /***************************** TypeDArray *****************************/ | |
2081 | |
2082 TypeDArray::TypeDArray(Type *t) | |
2083 : TypeArray(Tarray, t) | |
2084 { | |
2085 //printf("TypeDArray(t = %p)\n", t); | |
2086 } | |
2087 | |
2088 Type *TypeDArray::syntaxCopy() | |
2089 { | |
2090 Type *t = next->syntaxCopy(); | |
2091 if (t == next) | |
2092 t = this; | |
2093 else | |
2094 t = new TypeDArray(t); | |
2095 return t; | |
2096 } | |
2097 | |
2098 d_uns64 TypeDArray::size(Loc loc) | |
2099 { | |
2100 //printf("TypeDArray::size()\n"); | |
2101 return PTRSIZE * 2; | |
2102 } | |
2103 | |
2104 unsigned TypeDArray::alignsize() | |
2105 { | |
2106 // A DArray consists of two ptr-sized values, so align it on pointer size | |
2107 // boundary | |
2108 return PTRSIZE; | |
2109 } | |
2110 | |
2111 Type *TypeDArray::semantic(Loc loc, Scope *sc) | |
2112 { Type *tn = next; | |
2113 | |
2114 tn = next->semantic(loc,sc); | |
2115 Type *tbn = tn->toBasetype(); | |
2116 switch (tbn->ty) | |
2117 { | |
2118 case Tfunction: | |
2119 case Tnone: | |
2120 case Ttuple: | |
2121 error(loc, "can't have array of %s", tbn->toChars()); | |
2122 tn = next = tint32; | |
2123 break; | |
2124 } | |
2125 if (tn->isauto()) | |
2126 error(loc, "cannot have array of auto %s", tn->toChars()); | |
2127 if (next != tn) | |
2128 //deco = NULL; // redo | |
2129 return tn->arrayOf(); | |
2130 return merge(); | |
2131 } | |
2132 | |
2133 void TypeDArray::toDecoBuffer(OutBuffer *buf) | |
2134 { | |
2135 buf->writeByte(mangleChar[ty]); | |
2136 if (next) | |
2137 next->toDecoBuffer(buf); | |
2138 } | |
2139 | |
2140 void TypeDArray::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
2141 { | |
2142 if (mod != this->mod) | |
2143 { toCBuffer3(buf, hgs, mod); | |
2144 return; | |
2145 } | |
2146 next->toCBuffer2(buf, hgs, this->mod); | |
2147 buf->writestring("[]"); | |
2148 } | |
2149 | |
2150 Expression *TypeDArray::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
2151 { | |
2152 #if LOGDOTEXP | |
2153 printf("TypeDArray::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
2154 #endif | |
2155 if (ident == Id::length) | |
2156 { | |
2157 if (e->op == TOKstring) | |
2158 { StringExp *se = (StringExp *)e; | |
2159 | |
2160 return new IntegerExp(se->loc, se->len, Type::tindex); | |
2161 } | |
2162 e = new ArrayLengthExp(e->loc, e); | |
2163 e->type = Type::tsize_t; | |
2164 return e; | |
2165 } | |
2166 else if (ident == Id::ptr) | |
2167 { | |
2168 e = e->castTo(sc, next->pointerTo()); | |
2169 return e; | |
2170 } | |
2171 else | |
2172 { | |
2173 e = TypeArray::dotExp(sc, e, ident); | |
2174 } | |
2175 return e; | |
2176 } | |
2177 | |
2178 int TypeDArray::isString() | |
2179 { | |
2180 TY nty = next->toBasetype()->ty; | |
2181 return nty == Tchar || nty == Twchar || nty == Tdchar; | |
2182 } | |
2183 | |
2184 MATCH TypeDArray::implicitConvTo(Type *to) | |
2185 { | |
2186 //printf("TypeDArray::implicitConvTo()\n"); | |
2187 | |
2188 // Allow implicit conversion of array to pointer | |
2189 if (IMPLICIT_ARRAY_TO_PTR && | |
2190 to->ty == Tpointer && | |
2191 (to->next->ty == Tvoid || next->equals(to->next) /*|| to->next->isBaseOf(next)*/)) | |
2192 { | |
2193 return MATCHconvert; | |
2194 } | |
2195 | |
2196 if (to->ty == Tarray) | |
2197 { int offset = 0; | |
2198 | |
2199 if ((to->next->isBaseOf(next, &offset) && offset == 0) || | |
2200 to->next->ty == Tvoid) | |
2201 return MATCHconvert; | |
2202 } | |
2203 return Type::implicitConvTo(to); | |
2204 } | |
2205 | |
2206 Expression *TypeDArray::defaultInit(Loc loc) | |
2207 { | |
2208 #if LOGDEFAULTINIT | |
2209 printf("TypeDArray::defaultInit() '%s'\n", toChars()); | |
2210 #endif | |
2211 Expression *e; | |
2212 e = new NullExp(loc); | |
2213 e->type = this; | |
2214 return e; | |
2215 } | |
2216 | |
2217 int TypeDArray::isZeroInit() | |
2218 { | |
2219 return 1; | |
2220 } | |
2221 | |
2222 int TypeDArray::checkBoolean() | |
2223 { | |
2224 return TRUE; | |
2225 } | |
2226 | |
2227 int TypeDArray::hasPointers() | |
2228 { | |
2229 return TRUE; | |
2230 } | |
2231 | |
2232 /***************************** TypeAArray *****************************/ | |
2233 | |
2234 TypeAArray::TypeAArray(Type *t, Type *index) | |
2235 : TypeArray(Taarray, t) | |
2236 { | |
2237 this->index = index; | |
2238 this->key = NULL; | |
2239 } | |
2240 | |
2241 Type *TypeAArray::syntaxCopy() | |
2242 { | |
2243 Type *t = next->syntaxCopy(); | |
2244 Type *ti = index->syntaxCopy(); | |
2245 if (t == next && ti == index) | |
2246 t = this; | |
2247 else | |
2248 t = new TypeAArray(t, ti); | |
2249 return t; | |
2250 } | |
2251 | |
2252 d_uns64 TypeAArray::size(Loc loc) | |
2253 { | |
2254 return PTRSIZE /* * 2*/; | |
2255 } | |
2256 | |
2257 | |
2258 Type *TypeAArray::semantic(Loc loc, Scope *sc) | |
2259 { | |
2260 //printf("TypeAArray::semantic() %s index->ty = %d\n", toChars(), index->ty); | |
2261 | |
2262 // Deal with the case where we thought the index was a type, but | |
2263 // in reality it was an expression. | |
2264 if (index->ty == Tident || index->ty == Tinstance || index->ty == Tsarray) | |
2265 { | |
2266 Expression *e; | |
2267 Type *t; | |
2268 Dsymbol *s; | |
2269 | |
2270 index->resolve(loc, sc, &e, &t, &s); | |
2271 if (e) | |
2272 { // It was an expression - | |
2273 // Rewrite as a static array | |
2274 TypeSArray *tsa; | |
2275 | |
2276 tsa = new TypeSArray(next, e); | |
2277 return tsa->semantic(loc,sc); | |
2278 } | |
2279 else if (t) | |
2280 index = t; | |
2281 else | |
2282 index->error(loc, "index is not a type or an expression"); | |
2283 } | |
2284 else | |
2285 index = index->semantic(loc,sc); | |
2286 | |
2287 // Compute key type; the purpose of the key type is to | |
2288 // minimize the permutations of runtime library | |
2289 // routines as much as possible. | |
2290 key = index->toBasetype(); | |
2291 switch (key->ty) | |
2292 { | |
2293 #if 0 | |
2294 case Tint8: | |
2295 case Tuns8: | |
2296 case Tint16: | |
2297 case Tuns16: | |
2298 key = tint32; | |
2299 break; | |
2300 #endif | |
2301 | |
2302 case Tsarray: | |
2303 #if 0 | |
2304 // Convert to Tarray | |
2305 key = key->next->arrayOf(); | |
2306 #endif | |
2307 break; | |
2308 case Tbit: | |
2309 case Tbool: | |
2310 case Tfunction: | |
2311 case Tvoid: | |
2312 case Tnone: | |
2313 error(loc, "can't have associative array key of %s", key->toChars()); | |
2314 break; | |
2315 } | |
2316 next = next->semantic(loc,sc); | |
2317 switch (next->toBasetype()->ty) | |
2318 { | |
2319 case Tfunction: | |
2320 case Tnone: | |
2321 error(loc, "can't have associative array of %s", next->toChars()); | |
2322 break; | |
2323 } | |
2324 if (next->isauto()) | |
2325 error(loc, "cannot have array of auto %s", next->toChars()); | |
2326 | |
2327 return merge(); | |
2328 } | |
2329 | |
336 | 2330 void TypeAArray::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) |
2331 { | |
2332 //printf("TypeAArray::resolve() %s\n", toChars()); | |
2333 | |
2334 // Deal with the case where we thought the index was a type, but | |
2335 // in reality it was an expression. | |
2336 if (index->ty == Tident || index->ty == Tinstance || index->ty == Tsarray) | |
2337 { | |
2338 Expression *e; | |
2339 Type *t; | |
2340 Dsymbol *s; | |
2341 | |
2342 index->resolve(loc, sc, &e, &t, &s); | |
2343 if (e) | |
2344 { // It was an expression - | |
2345 // Rewrite as a static array | |
2346 | |
2347 TypeSArray *tsa = new TypeSArray(next, e); | |
2348 return tsa->resolve(loc, sc, pe, pt, ps); | |
2349 } | |
2350 else if (t) | |
2351 index = t; | |
2352 else | |
2353 index->error(loc, "index is not a type or an expression"); | |
2354 } | |
2355 Type::resolve(loc, sc, pe, pt, ps); | |
2356 } | |
2357 | |
2358 | |
159 | 2359 Expression *TypeAArray::dotExp(Scope *sc, Expression *e, Identifier *ident) |
2360 { | |
2361 #if LOGDOTEXP | |
2362 printf("TypeAArray::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
2363 #endif | |
2364 if (ident == Id::length) | |
2365 { | |
2366 Expression *ec; | |
2367 Expressions *arguments; | |
2368 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
2369 //LDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2370 static FuncDeclaration *aaLen_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2371 if(!aaLen_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2372 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
2373 args->push(new Argument(STCin, Type::tvoid->pointerTo(), NULL, NULL)); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2374 aaLen_fd = FuncDeclaration::genCfunc(args, Type::tsize_t, Id::aaLen); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2375 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2376 |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2377 ec = new VarExp(0, aaLen_fd); |
159 | 2378 arguments = new Expressions(); |
2379 arguments->push(e); | |
2380 e = new CallExp(e->loc, ec, arguments); | |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2381 e->type = aaLen_fd->type->next; |
159 | 2382 } |
2383 else if (ident == Id::keys) | |
2384 { | |
2385 Expression *ec; | |
2386 Expressions *arguments; | |
2387 int size = key->size(e->loc); | |
2388 | |
2389 assert(size); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
2390 //LDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2391 static FuncDeclaration *aaKeys_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2392 if(!aaKeys_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2393 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
2394 args->push(new Argument(STCin, Type::tvoid->pointerTo(), NULL, NULL)); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2395 args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
2396 aaKeys_fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), Id::aaKeys); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2397 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2398 |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2399 ec = new VarExp(0, aaKeys_fd); |
159 | 2400 arguments = new Expressions(); |
2401 arguments->push(e); | |
2402 arguments->push(new IntegerExp(0, size, Type::tsize_t)); | |
2403 e = new CallExp(e->loc, ec, arguments); | |
2404 e->type = index->arrayOf(); | |
2405 } | |
2406 else if (ident == Id::values) | |
2407 { | |
2408 Expression *ec; | |
2409 Expressions *arguments; | |
2410 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
2411 //LDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2412 static FuncDeclaration *aaValues_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2413 if(!aaValues_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2414 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
2415 args->push(new Argument(STCin, Type::tvoid->pointerTo(), NULL, NULL)); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2416 args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2417 args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
2418 aaValues_fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), Id::aaValues); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2419 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2420 |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2421 ec = new VarExp(0, aaValues_fd); |
159 | 2422 arguments = new Expressions(); |
2423 arguments->push(e); | |
2424 size_t keysize = key->size(e->loc); | |
771
bfabbac8e705
Fixed 64bit problem with aaValues runtime calls (assumed 32bits)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
742
diff
changeset
|
2425 keysize = (keysize + PTRSIZE - 1) & ~(PTRSIZE - 1); |
159 | 2426 arguments->push(new IntegerExp(0, keysize, Type::tsize_t)); |
2427 arguments->push(new IntegerExp(0, next->size(e->loc), Type::tsize_t)); | |
2428 e = new CallExp(e->loc, ec, arguments); | |
2429 e->type = next->arrayOf(); | |
2430 } | |
2431 else if (ident == Id::rehash) | |
2432 { | |
2433 Expression *ec; | |
2434 Expressions *arguments; | |
2435 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
2436 //LDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2437 static FuncDeclaration *aaRehash_fd = NULL; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2438 if(!aaRehash_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2439 Arguments* args = new Arguments; |
715
30b42a283c8e
Removed TypeOpaque from DMD.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
2440 args->push(new Argument(STCin, Type::tvoid->pointerTo(), NULL, NULL)); |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2441 args->push(new Argument(STCin, Type::typeinfo->type, NULL, NULL)); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2442 aaRehash_fd = FuncDeclaration::genCfunc(args, Type::tvoidptr, Id::aaRehash); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2443 } |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2444 |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2445 ec = new VarExp(0, aaRehash_fd); |
159 | 2446 arguments = new Expressions(); |
2447 arguments->push(e->addressOf(sc)); | |
975
067bb8f19c36
Fix #217. getInternalTypeInfo doesn't work with LDC.
Christian Kamm <kamm incasoftware de>
parents:
938
diff
changeset
|
2448 arguments->push(key->getTypeInfo(sc)); // LDC doesn't support getInternalTypeInfo, see above |
159 | 2449 e = new CallExp(e->loc, ec, arguments); |
2450 e->type = this; | |
2451 } | |
2452 else | |
2453 { | |
2454 e = Type::dotExp(sc, e, ident); | |
2455 } | |
2456 return e; | |
2457 } | |
2458 | |
2459 void TypeAArray::toDecoBuffer(OutBuffer *buf) | |
2460 { | |
2461 buf->writeByte(mangleChar[ty]); | |
2462 index->toDecoBuffer(buf); | |
2463 next->toDecoBuffer(buf); | |
2464 } | |
2465 | |
2466 void TypeAArray::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
2467 { | |
2468 if (mod != this->mod) | |
2469 { toCBuffer3(buf, hgs, mod); | |
2470 return; | |
2471 } | |
2472 next->toCBuffer2(buf, hgs, this->mod); | |
2473 buf->writeByte('['); | |
2474 index->toCBuffer2(buf, hgs, 0); | |
2475 buf->writeByte(']'); | |
2476 } | |
2477 | |
2478 Expression *TypeAArray::defaultInit(Loc loc) | |
2479 { | |
2480 #if LOGDEFAULTINIT | |
2481 printf("TypeAArray::defaultInit() '%s'\n", toChars()); | |
2482 #endif | |
2483 Expression *e; | |
2484 e = new NullExp(loc); | |
2485 e->type = this; | |
2486 return e; | |
2487 } | |
2488 | |
211
f66219e0d530
[svn r227] Fixed: crash in lifetime.d when resizing array of AAs by .length assignment.
lindquist
parents:
162
diff
changeset
|
2489 int TypeAArray::isZeroInit() |
f66219e0d530
[svn r227] Fixed: crash in lifetime.d when resizing array of AAs by .length assignment.
lindquist
parents:
162
diff
changeset
|
2490 { |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
822
diff
changeset
|
2491 return TRUE; |
211
f66219e0d530
[svn r227] Fixed: crash in lifetime.d when resizing array of AAs by .length assignment.
lindquist
parents:
162
diff
changeset
|
2492 } |
f66219e0d530
[svn r227] Fixed: crash in lifetime.d when resizing array of AAs by .length assignment.
lindquist
parents:
162
diff
changeset
|
2493 |
159 | 2494 int TypeAArray::checkBoolean() |
2495 { | |
2496 return TRUE; | |
2497 } | |
2498 | |
2499 int TypeAArray::hasPointers() | |
2500 { | |
2501 return TRUE; | |
2502 } | |
2503 | |
2504 /***************************** TypePointer *****************************/ | |
2505 | |
2506 TypePointer::TypePointer(Type *t) | |
2507 : Type(Tpointer, t) | |
2508 { | |
2509 } | |
2510 | |
2511 Type *TypePointer::syntaxCopy() | |
2512 { | |
2513 Type *t = next->syntaxCopy(); | |
2514 if (t == next) | |
2515 t = this; | |
2516 else | |
2517 t = new TypePointer(t); | |
2518 return t; | |
2519 } | |
2520 | |
2521 Type *TypePointer::semantic(Loc loc, Scope *sc) | |
2522 { | |
2523 //printf("TypePointer::semantic()\n"); | |
2524 Type *n = next->semantic(loc, sc); | |
2525 switch (n->toBasetype()->ty) | |
2526 { | |
2527 case Ttuple: | |
2528 error(loc, "can't have pointer to %s", n->toChars()); | |
2529 n = tint32; | |
2530 break; | |
2531 } | |
2532 if (n != next) | |
2533 deco = NULL; | |
2534 next = n; | |
2535 return merge(); | |
2536 } | |
2537 | |
2538 | |
2539 d_uns64 TypePointer::size(Loc loc) | |
2540 { | |
2541 return PTRSIZE; | |
2542 } | |
2543 | |
2544 void TypePointer::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
2545 { | |
2546 //printf("TypePointer::toCBuffer2() next = %d\n", next->ty); | |
2547 if (mod != this->mod) | |
2548 { toCBuffer3(buf, hgs, mod); | |
2549 return; | |
2550 } | |
2551 next->toCBuffer2(buf, hgs, this->mod); | |
2552 if (next->ty != Tfunction) | |
2553 buf->writeByte('*'); | |
2554 } | |
2555 | |
2556 MATCH TypePointer::implicitConvTo(Type *to) | |
2557 { | |
2558 //printf("TypePointer::implicitConvTo()\n"); | |
2559 | |
2560 if (this == to) | |
2561 return MATCHexact; | |
2562 if (to->ty == Tpointer && to->next) | |
2563 { | |
2564 if (to->next->ty == Tvoid) | |
2565 return MATCHconvert; | |
2566 | |
2567 #if 0 | |
2568 if (to->next->isBaseOf(next)) | |
2569 return MATCHconvert; | |
2570 #endif | |
2571 | |
2572 if (next->ty == Tfunction && to->next->ty == Tfunction) | |
2573 { TypeFunction *tf; | |
2574 TypeFunction *tfto; | |
2575 | |
2576 tf = (TypeFunction *)(next); | |
2577 tfto = (TypeFunction *)(to->next); | |
2578 return tfto->equals(tf) ? MATCHexact : MATCHnomatch; | |
2579 } | |
2580 } | |
2581 // if (to->ty == Tvoid) | |
2582 // return MATCHconvert; | |
2583 return MATCHnomatch; | |
2584 } | |
2585 | |
2586 int TypePointer::isscalar() | |
2587 { | |
2588 return TRUE; | |
2589 } | |
2590 | |
2591 Expression *TypePointer::defaultInit(Loc loc) | |
2592 { | |
2593 #if LOGDEFAULTINIT | |
2594 printf("TypePointer::defaultInit() '%s'\n", toChars()); | |
2595 #endif | |
2596 Expression *e; | |
2597 e = new NullExp(loc); | |
2598 e->type = this; | |
2599 return e; | |
2600 } | |
2601 | |
2602 int TypePointer::isZeroInit() | |
2603 { | |
2604 return 1; | |
2605 } | |
2606 | |
2607 int TypePointer::hasPointers() | |
2608 { | |
2609 return TRUE; | |
2610 } | |
2611 | |
2612 | |
2613 /***************************** TypeReference *****************************/ | |
2614 | |
2615 TypeReference::TypeReference(Type *t) | |
2616 : Type(Treference, t) | |
2617 { | |
2618 if (t->ty == Tbit) | |
2619 error(0,"cannot make reference to a bit"); | |
2620 // BUG: what about references to static arrays? | |
2621 } | |
2622 | |
2623 Type *TypeReference::syntaxCopy() | |
2624 { | |
2625 Type *t = next->syntaxCopy(); | |
2626 if (t == next) | |
2627 t = this; | |
2628 else | |
2629 t = new TypeReference(t); | |
2630 return t; | |
2631 } | |
2632 | |
2633 d_uns64 TypeReference::size(Loc loc) | |
2634 { | |
2635 return PTRSIZE; | |
2636 } | |
2637 | |
2638 void TypeReference::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
2639 { | |
2640 if (mod != this->mod) | |
2641 { toCBuffer3(buf, hgs, mod); | |
2642 return; | |
2643 } | |
2644 next->toCBuffer2(buf, hgs, this->mod); | |
2645 buf->writeByte('&'); | |
2646 } | |
2647 | |
2648 Expression *TypeReference::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
2649 { | |
2650 #if LOGDOTEXP | |
2651 printf("TypeReference::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
2652 #endif | |
2653 | |
2654 // References just forward things along | |
2655 return next->dotExp(sc, e, ident); | |
2656 } | |
2657 | |
2658 Expression *TypeReference::defaultInit(Loc loc) | |
2659 { | |
2660 #if LOGDEFAULTINIT | |
2661 printf("TypeReference::defaultInit() '%s'\n", toChars()); | |
2662 #endif | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
2663 Expression *e = new NullExp(loc); |
159 | 2664 e->type = this; |
2665 return e; | |
2666 } | |
2667 | |
2668 int TypeReference::isZeroInit() | |
2669 { | |
2670 return 1; | |
2671 } | |
2672 | |
2673 | |
2674 /***************************** TypeFunction *****************************/ | |
2675 | |
2676 TypeFunction::TypeFunction(Arguments *parameters, Type *treturn, int varargs, enum LINK linkage) | |
2677 : Type(Tfunction, treturn) | |
2678 { | |
2679 //if (!treturn) *(char*)0=0; | |
2680 // assert(treturn); | |
2681 this->parameters = parameters; | |
2682 this->varargs = varargs; | |
2683 this->linkage = linkage; | |
2684 this->inuse = 0; | |
2685 } | |
2686 | |
2687 Type *TypeFunction::syntaxCopy() | |
2688 { | |
2689 Type *treturn = next ? next->syntaxCopy() : NULL; | |
2690 Arguments *params = Argument::arraySyntaxCopy(parameters); | |
217
0806379a5eca
[svn r233] Added: -oq command line option for writing fully qualified object names.
lindquist
parents:
211
diff
changeset
|
2691 TypeFunction *t = new TypeFunction(params, treturn, varargs, linkage); |
159 | 2692 return t; |
2693 } | |
2694 | |
2695 /******************************* | |
2696 * Returns: | |
2697 * 0 types are distinct | |
2698 * 1 this is covariant with t | |
2699 * 2 arguments match as far as overloading goes, | |
2700 * but types are not covariant | |
2701 * 3 cannot determine covariance because of forward references | |
2702 */ | |
2703 | |
2704 int Type::covariant(Type *t) | |
2705 { | |
2706 #if 0 | |
2707 printf("Type::covariant(t = %s) %s\n", t->toChars(), toChars()); | |
2708 printf("deco = %p, %p\n", deco, t->deco); | |
2709 printf("ty = %d\n", next->ty); | |
2710 #endif | |
2711 | |
2712 int inoutmismatch = 0; | |
2713 | |
2714 if (equals(t)) | |
2715 goto Lcovariant; | |
2716 if (ty != Tfunction || t->ty != Tfunction) | |
2717 goto Ldistinct; | |
2718 | |
2719 { | |
2720 TypeFunction *t1 = (TypeFunction *)this; | |
2721 TypeFunction *t2 = (TypeFunction *)t; | |
2722 | |
2723 if (t1->varargs != t2->varargs) | |
2724 goto Ldistinct; | |
2725 | |
2726 if (t1->parameters && t2->parameters) | |
2727 { | |
2728 size_t dim = Argument::dim(t1->parameters); | |
2729 if (dim != Argument::dim(t2->parameters)) | |
2730 goto Ldistinct; | |
2731 | |
2732 for (size_t i = 0; i < dim; i++) | |
2733 { Argument *arg1 = Argument::getNth(t1->parameters, i); | |
2734 Argument *arg2 = Argument::getNth(t2->parameters, i); | |
2735 | |
2736 if (!arg1->type->equals(arg2->type)) | |
2737 goto Ldistinct; | |
2738 if (arg1->storageClass != arg2->storageClass) | |
2739 inoutmismatch = 1; | |
2740 } | |
2741 } | |
2742 else if (t1->parameters != t2->parameters) | |
2743 goto Ldistinct; | |
2744 | |
2745 // The argument lists match | |
2746 if (inoutmismatch) | |
2747 goto Lnotcovariant; | |
2748 if (t1->linkage != t2->linkage) | |
2749 goto Lnotcovariant; | |
2750 | |
2751 Type *t1n = t1->next; | |
2752 Type *t2n = t2->next; | |
2753 | |
2754 if (t1n->equals(t2n)) | |
2755 goto Lcovariant; | |
2756 if (t1n->ty != Tclass || t2n->ty != Tclass) | |
2757 goto Lnotcovariant; | |
2758 | |
2759 // If t1n is forward referenced: | |
2760 ClassDeclaration *cd = ((TypeClass *)t1n)->sym; | |
2761 if (!cd->baseClass && cd->baseclasses.dim && !cd->isInterfaceDeclaration()) | |
2762 { | |
2763 return 3; | |
2764 } | |
2765 | |
2766 if (t1n->implicitConvTo(t2n)) | |
2767 goto Lcovariant; | |
2768 goto Lnotcovariant; | |
2769 } | |
2770 | |
2771 Lcovariant: | |
2772 //printf("\tcovaraint: 1\n"); | |
2773 return 1; | |
2774 | |
2775 Ldistinct: | |
2776 //printf("\tcovaraint: 0\n"); | |
2777 return 0; | |
2778 | |
2779 Lnotcovariant: | |
2780 //printf("\tcovaraint: 2\n"); | |
2781 return 2; | |
2782 } | |
2783 | |
2784 void TypeFunction::toDecoBuffer(OutBuffer *buf) | |
2785 { unsigned char mc; | |
2786 | |
2787 //printf("TypeFunction::toDecoBuffer() this = %p %s\n", this, toChars()); | |
2788 //static int nest; if (++nest == 50) *(char*)0=0; | |
2789 if (inuse) | |
2790 { inuse = 2; // flag error to caller | |
2791 return; | |
2792 } | |
2793 inuse++; | |
2794 switch (linkage) | |
2795 { | |
2796 case LINKd: mc = 'F'; break; | |
2797 case LINKc: mc = 'U'; break; | |
2798 case LINKwindows: mc = 'W'; break; | |
2799 case LINKpascal: mc = 'V'; break; | |
2800 case LINKcpp: mc = 'R'; break; | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2801 |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2802 // LDC |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2803 case LINKintrinsic: mc = 'Q'; break; |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2804 |
159 | 2805 default: |
2806 assert(0); | |
2807 } | |
2808 buf->writeByte(mc); | |
2809 // Write argument types | |
2810 Argument::argsToDecoBuffer(buf, parameters); | |
2811 //if (buf->data[buf->offset - 1] == '@') halt(); | |
2812 buf->writeByte('Z' - varargs); // mark end of arg list | |
2813 next->toDecoBuffer(buf); | |
2814 inuse--; | |
2815 } | |
2816 | |
2817 void TypeFunction::toCBuffer(OutBuffer *buf, Identifier *ident, HdrGenState *hgs) | |
2818 { | |
2819 char *p = NULL; | |
2820 | |
2821 if (inuse) | |
2822 { inuse = 2; // flag error to caller | |
2823 return; | |
2824 } | |
2825 inuse++; | |
2826 if (next && (!ident || ident->toHChars2() == ident->toChars())) | |
2827 next->toCBuffer2(buf, hgs, 0); | |
2828 if (hgs->ddoc != 1) | |
2829 { | |
2830 switch (linkage) | |
2831 { | |
2832 case LINKd: p = NULL; break; | |
2833 case LINKc: p = "C "; break; | |
2834 case LINKwindows: p = "Windows "; break; | |
2835 case LINKpascal: p = "Pascal "; break; | |
2836 case LINKcpp: p = "C++ "; break; | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2837 |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2838 // LDC |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2839 case LINKintrinsic: p = "Intrinsic"; break; |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2840 |
159 | 2841 default: |
2842 assert(0); | |
2843 } | |
2844 } | |
2845 | |
2846 if (!hgs->hdrgen && p) | |
2847 buf->writestring(p); | |
2848 if (ident) | |
2849 { buf->writeByte(' '); | |
2850 buf->writestring(ident->toHChars2()); | |
2851 } | |
2852 Argument::argsToCBuffer(buf, hgs, parameters, varargs); | |
2853 inuse--; | |
2854 } | |
2855 | |
2856 void TypeFunction::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
2857 { | |
2858 char *p = NULL; | |
2859 | |
2860 if (inuse) | |
2861 { inuse = 2; // flag error to caller | |
2862 return; | |
2863 } | |
2864 inuse++; | |
2865 if (next) | |
2866 next->toCBuffer2(buf, hgs, 0); | |
2867 if (hgs->ddoc != 1) | |
2868 { | |
2869 switch (linkage) | |
2870 { | |
2871 case LINKd: p = NULL; break; | |
2872 case LINKc: p = "C "; break; | |
2873 case LINKwindows: p = "Windows "; break; | |
2874 case LINKpascal: p = "Pascal "; break; | |
2875 case LINKcpp: p = "C++ "; break; | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2876 |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2877 // LDC |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2878 case LINKintrinsic: p = "Intrinsic"; break; |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
720
diff
changeset
|
2879 |
159 | 2880 default: |
2881 assert(0); | |
2882 } | |
2883 } | |
2884 | |
2885 if (!hgs->hdrgen && p) | |
2886 buf->writestring(p); | |
2887 buf->writestring(" function"); | |
2888 Argument::argsToCBuffer(buf, hgs, parameters, varargs); | |
2889 inuse--; | |
2890 } | |
2891 | |
2892 Type *TypeFunction::semantic(Loc loc, Scope *sc) | |
2893 { | |
2894 if (deco) // if semantic() already run | |
2895 { | |
2896 //printf("already done\n"); | |
2897 return this; | |
2898 } | |
2899 //printf("TypeFunction::semantic() this = %p\n", this); | |
2900 | |
2901 TypeFunction *tf = (TypeFunction *)mem.malloc(sizeof(TypeFunction)); | |
2902 memcpy(tf, this, sizeof(TypeFunction)); | |
2903 if (parameters) | |
2904 { tf->parameters = (Arguments *)parameters->copy(); | |
2905 for (size_t i = 0; i < parameters->dim; i++) | |
2906 { Argument *arg = (Argument *)parameters->data[i]; | |
2907 Argument *cpy = (Argument *)mem.malloc(sizeof(Argument)); | |
2908 memcpy(cpy, arg, sizeof(Argument)); | |
2909 tf->parameters->data[i] = (void *)cpy; | |
2910 } | |
2911 } | |
2912 | |
2913 tf->linkage = sc->linkage; | |
2914 if (!tf->next) | |
2915 { | |
2916 assert(global.errors); | |
2917 tf->next = tvoid; | |
2918 } | |
2919 tf->next = tf->next->semantic(loc,sc); | |
2920 if (tf->next->toBasetype()->ty == Tsarray) | |
2921 { error(loc, "functions cannot return static array %s", tf->next->toChars()); | |
2922 tf->next = Type::terror; | |
2923 } | |
2924 if (tf->next->toBasetype()->ty == Tfunction) | |
2925 { error(loc, "functions cannot return a function"); | |
2926 tf->next = Type::terror; | |
2927 } | |
2928 if (tf->next->toBasetype()->ty == Ttuple) | |
2929 { error(loc, "functions cannot return a tuple"); | |
2930 tf->next = Type::terror; | |
2931 } | |
2932 if (tf->next->isauto() && !(sc->flags & SCOPEctor)) | |
2933 error(loc, "functions cannot return auto %s", tf->next->toChars()); | |
2934 | |
2935 if (tf->parameters) | |
2936 { size_t dim = Argument::dim(tf->parameters); | |
2937 | |
2938 for (size_t i = 0; i < dim; i++) | |
2939 { Argument *arg = Argument::getNth(tf->parameters, i); | |
2940 Type *t; | |
2941 | |
2942 tf->inuse++; | |
2943 arg->type = arg->type->semantic(loc,sc); | |
2944 if (tf->inuse == 1) tf->inuse--; | |
780
2cf440d511bb
Move tuple syntax copy for function arguments after semantic, to make sure
Christian Kamm <kamm incasoftware de>
parents:
772
diff
changeset
|
2945 |
2cf440d511bb
Move tuple syntax copy for function arguments after semantic, to make sure
Christian Kamm <kamm incasoftware de>
parents:
772
diff
changeset
|
2946 // each function needs its own copy of a tuple arg, since |
2cf440d511bb
Move tuple syntax copy for function arguments after semantic, to make sure
Christian Kamm <kamm incasoftware de>
parents:
772
diff
changeset
|
2947 // they mustn't share arg flags like inreg, ... |
2cf440d511bb
Move tuple syntax copy for function arguments after semantic, to make sure
Christian Kamm <kamm incasoftware de>
parents:
772
diff
changeset
|
2948 if (arg->type->ty == Ttuple) { |
2cf440d511bb
Move tuple syntax copy for function arguments after semantic, to make sure
Christian Kamm <kamm incasoftware de>
parents:
772
diff
changeset
|
2949 arg->type = arg->type->syntaxCopy(); |
2cf440d511bb
Move tuple syntax copy for function arguments after semantic, to make sure
Christian Kamm <kamm incasoftware de>
parents:
772
diff
changeset
|
2950 tf->inuse++; |
2cf440d511bb
Move tuple syntax copy for function arguments after semantic, to make sure
Christian Kamm <kamm incasoftware de>
parents:
772
diff
changeset
|
2951 arg->type = arg->type->semantic(loc,sc); |
2cf440d511bb
Move tuple syntax copy for function arguments after semantic, to make sure
Christian Kamm <kamm incasoftware de>
parents:
772
diff
changeset
|
2952 if (tf->inuse == 1) tf->inuse--; |
2cf440d511bb
Move tuple syntax copy for function arguments after semantic, to make sure
Christian Kamm <kamm incasoftware de>
parents:
772
diff
changeset
|
2953 } |
2cf440d511bb
Move tuple syntax copy for function arguments after semantic, to make sure
Christian Kamm <kamm incasoftware de>
parents:
772
diff
changeset
|
2954 |
159 | 2955 t = arg->type->toBasetype(); |
2956 | |
2957 if (arg->storageClass & (STCout | STCref | STClazy)) | |
2958 { | |
2959 if (t->ty == Tsarray) | |
2960 error(loc, "cannot have out or ref parameter of type %s", t->toChars()); | |
2961 } | |
2962 if (!(arg->storageClass & STClazy) && t->ty == Tvoid) | |
2963 error(loc, "cannot have parameter of type %s", arg->type->toChars()); | |
2964 | |
2965 if (arg->defaultArg) | |
2966 { | |
2967 arg->defaultArg = arg->defaultArg->semantic(sc); | |
2968 arg->defaultArg = resolveProperties(sc, arg->defaultArg); | |
2969 arg->defaultArg = arg->defaultArg->implicitCastTo(sc, arg->type); | |
2970 } | |
2971 | |
2972 /* If arg turns out to be a tuple, the number of parameters may | |
2973 * change. | |
2974 */ | |
2975 if (t->ty == Ttuple) | |
2976 { dim = Argument::dim(tf->parameters); | |
2977 i--; | |
2978 } | |
2979 } | |
2980 } | |
2981 tf->deco = tf->merge()->deco; | |
2982 | |
2983 if (tf->inuse) | |
2984 { error(loc, "recursive type"); | |
2985 tf->inuse = 0; | |
2986 return terror; | |
2987 } | |
2988 | |
2989 if (tf->varargs == 1 && tf->linkage != LINKd && Argument::dim(tf->parameters) == 0) | |
2990 error(loc, "variadic functions with non-D linkage must have at least one parameter"); | |
2991 | |
2992 /* Don't return merge(), because arg identifiers and default args | |
2993 * can be different | |
2994 * even though the types match | |
2995 */ | |
2996 return tf; | |
2997 } | |
2998 | |
2999 /******************************** | |
3000 * 'args' are being matched to function 'this' | |
3001 * Determine match level. | |
3002 * Returns: | |
3003 * MATCHxxxx | |
3004 */ | |
3005 | |
3006 int TypeFunction::callMatch(Expressions *args) | |
3007 { | |
3008 //printf("TypeFunction::callMatch()\n"); | |
3009 int match = MATCHexact; // assume exact match | |
3010 | |
3011 size_t nparams = Argument::dim(parameters); | |
3012 size_t nargs = args ? args->dim : 0; | |
3013 if (nparams == nargs) | |
3014 ; | |
3015 else if (nargs > nparams) | |
3016 { | |
3017 if (varargs == 0) | |
3018 goto Nomatch; // too many args; no match | |
3019 match = MATCHconvert; // match ... with a "conversion" match level | |
3020 } | |
3021 | |
3022 for (size_t u = 0; u < nparams; u++) | |
3023 { int m; | |
3024 Expression *arg; | |
3025 | |
3026 // BUG: what about out and ref? | |
3027 | |
3028 Argument *p = Argument::getNth(parameters, u); | |
3029 assert(p); | |
3030 if (u >= nargs) | |
3031 { | |
3032 if (p->defaultArg) | |
3033 continue; | |
3034 if (varargs == 2 && u + 1 == nparams) | |
3035 goto L1; | |
3036 goto Nomatch; // not enough arguments | |
3037 } | |
3038 arg = (Expression *)args->data[u]; | |
3039 assert(arg); | |
3040 if (p->storageClass & STClazy && p->type->ty == Tvoid && arg->type->ty != Tvoid) | |
3041 m = MATCHconvert; | |
3042 else | |
3043 m = arg->implicitConvTo(p->type); | |
3044 //printf("\tm = %d\n", m); | |
3045 if (m == MATCHnomatch) // if no match | |
3046 { | |
3047 L1: | |
3048 if (varargs == 2 && u + 1 == nparams) // if last varargs param | |
3049 { Type *tb = p->type->toBasetype(); | |
3050 TypeSArray *tsa; | |
3051 integer_t sz; | |
3052 | |
3053 switch (tb->ty) | |
3054 { | |
3055 case Tsarray: | |
3056 tsa = (TypeSArray *)tb; | |
3057 sz = tsa->dim->toInteger(); | |
3058 if (sz != nargs - u) | |
3059 goto Nomatch; | |
3060 case Tarray: | |
3061 for (; u < nargs; u++) | |
3062 { | |
3063 arg = (Expression *)args->data[u]; | |
3064 assert(arg); | |
3065 #if 1 | |
3066 /* If lazy array of delegates, | |
3067 * convert arg(s) to delegate(s) | |
3068 */ | |
3069 Type *tret = p->isLazyArray(); | |
3070 if (tret) | |
3071 { | |
3072 if (tb->next->equals(arg->type)) | |
3073 { m = MATCHexact; | |
3074 } | |
3075 else | |
3076 { | |
3077 m = arg->implicitConvTo(tret); | |
3078 if (m == MATCHnomatch) | |
3079 { | |
3080 if (tret->toBasetype()->ty == Tvoid) | |
3081 m = MATCHconvert; | |
3082 } | |
3083 } | |
3084 } | |
3085 else | |
3086 m = arg->implicitConvTo(tb->next); | |
3087 #else | |
3088 m = arg->implicitConvTo(tb->next); | |
3089 #endif | |
3090 if (m == 0) | |
3091 goto Nomatch; | |
3092 if (m < match) | |
3093 match = m; | |
3094 } | |
3095 goto Ldone; | |
3096 | |
3097 case Tclass: | |
3098 // Should see if there's a constructor match? | |
3099 // Or just leave it ambiguous? | |
3100 goto Ldone; | |
3101 | |
3102 default: | |
3103 goto Nomatch; | |
3104 } | |
3105 } | |
3106 goto Nomatch; | |
3107 } | |
3108 if (m < match) | |
3109 match = m; // pick worst match | |
3110 } | |
3111 | |
3112 Ldone: | |
3113 //printf("match = %d\n", match); | |
3114 return match; | |
3115 | |
3116 Nomatch: | |
3117 //printf("no match\n"); | |
3118 return MATCHnomatch; | |
3119 } | |
3120 | |
3121 Type *TypeFunction::reliesOnTident() | |
3122 { | |
3123 if (parameters) | |
3124 { | |
3125 for (size_t i = 0; i < parameters->dim; i++) | |
3126 { Argument *arg = (Argument *)parameters->data[i]; | |
3127 Type *t = arg->type->reliesOnTident(); | |
3128 if (t) | |
3129 return t; | |
3130 } | |
3131 } | |
3132 return next->reliesOnTident(); | |
3133 } | |
3134 | |
3135 /***************************** TypeDelegate *****************************/ | |
3136 | |
3137 TypeDelegate::TypeDelegate(Type *t) | |
3138 : Type(Tfunction, t) | |
3139 { | |
3140 ty = Tdelegate; | |
3141 } | |
3142 | |
3143 Type *TypeDelegate::syntaxCopy() | |
3144 { | |
3145 Type *t = next->syntaxCopy(); | |
3146 if (t == next) | |
3147 t = this; | |
3148 else | |
3149 t = new TypeDelegate(t); | |
3150 return t; | |
3151 } | |
3152 | |
3153 Type *TypeDelegate::semantic(Loc loc, Scope *sc) | |
3154 { | |
3155 if (deco) // if semantic() already run | |
3156 { | |
3157 //printf("already done\n"); | |
3158 return this; | |
3159 } | |
3160 next = next->semantic(loc,sc); | |
3161 return merge(); | |
3162 } | |
3163 | |
3164 d_uns64 TypeDelegate::size(Loc loc) | |
3165 { | |
3166 return PTRSIZE * 2; | |
3167 } | |
3168 | |
797
340acf1535d0
Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
780
diff
changeset
|
3169 // LDC added, no reason to align to 2*PTRSIZE |
340acf1535d0
Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
780
diff
changeset
|
3170 unsigned TypeDelegate::alignsize() |
340acf1535d0
Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
780
diff
changeset
|
3171 { |
340acf1535d0
Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
780
diff
changeset
|
3172 // A Delegate consists of two ptr values, so align it on pointer size |
340acf1535d0
Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
780
diff
changeset
|
3173 // boundary |
340acf1535d0
Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
780
diff
changeset
|
3174 return PTRSIZE; |
340acf1535d0
Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
780
diff
changeset
|
3175 } |
340acf1535d0
Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
780
diff
changeset
|
3176 |
159 | 3177 void TypeDelegate::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) |
3178 { | |
3179 if (mod != this->mod) | |
3180 { toCBuffer3(buf, hgs, mod); | |
3181 return; | |
3182 } | |
3183 TypeFunction *tf = (TypeFunction *)next; | |
3184 | |
3185 tf->next->toCBuffer2(buf, hgs, 0); | |
3186 buf->writestring(" delegate"); | |
3187 Argument::argsToCBuffer(buf, hgs, tf->parameters, tf->varargs); | |
3188 } | |
3189 | |
3190 Expression *TypeDelegate::defaultInit(Loc loc) | |
3191 { | |
3192 #if LOGDEFAULTINIT | |
3193 printf("TypeDelegate::defaultInit() '%s'\n", toChars()); | |
3194 #endif | |
3195 Expression *e; | |
3196 e = new NullExp(loc); | |
3197 e->type = this; | |
3198 return e; | |
3199 } | |
3200 | |
3201 int TypeDelegate::isZeroInit() | |
3202 { | |
3203 return 1; | |
3204 } | |
3205 | |
3206 int TypeDelegate::checkBoolean() | |
3207 { | |
3208 return TRUE; | |
3209 } | |
3210 | |
3211 Expression *TypeDelegate::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
3212 { | |
3213 #if LOGDOTEXP | |
3214 printf("TypeDelegate::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
3215 #endif | |
3216 if (ident == Id::ptr) | |
3217 { | |
599
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
591
diff
changeset
|
3218 e = new GEPExp(e->loc, e, ident, 0); |
159 | 3219 e->type = tvoidptr; |
3220 return e; | |
3221 } | |
3222 else if (ident == Id::funcptr) | |
3223 { | |
599
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
591
diff
changeset
|
3224 e = new GEPExp(e->loc, e, ident, 1); |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
591
diff
changeset
|
3225 e->type = tvoidptr; |
159 | 3226 return e; |
3227 } | |
3228 else | |
3229 { | |
3230 e = Type::dotExp(sc, e, ident); | |
3231 } | |
3232 return e; | |
3233 } | |
3234 | |
3235 int TypeDelegate::hasPointers() | |
3236 { | |
3237 return TRUE; | |
3238 } | |
3239 | |
3240 | |
3241 | |
3242 /***************************** TypeQualified *****************************/ | |
3243 | |
3244 TypeQualified::TypeQualified(TY ty, Loc loc) | |
3245 : Type(ty, NULL) | |
3246 { | |
3247 this->loc = loc; | |
3248 } | |
3249 | |
3250 void TypeQualified::syntaxCopyHelper(TypeQualified *t) | |
3251 { | |
3252 //printf("TypeQualified::syntaxCopyHelper(%s) %s\n", t->toChars(), toChars()); | |
3253 idents.setDim(t->idents.dim); | |
3254 for (int i = 0; i < idents.dim; i++) | |
3255 { | |
3256 Identifier *id = (Identifier *)t->idents.data[i]; | |
3257 if (id->dyncast() == DYNCAST_DSYMBOL) | |
3258 { | |
3259 TemplateInstance *ti = (TemplateInstance *)id; | |
3260 | |
3261 ti = (TemplateInstance *)ti->syntaxCopy(NULL); | |
3262 id = (Identifier *)ti; | |
3263 } | |
3264 idents.data[i] = id; | |
3265 } | |
3266 } | |
3267 | |
3268 | |
3269 void TypeQualified::addIdent(Identifier *ident) | |
3270 { | |
3271 idents.push(ident); | |
3272 } | |
3273 | |
3274 void TypeQualified::toCBuffer2Helper(OutBuffer *buf, HdrGenState *hgs) | |
3275 { | |
3276 int i; | |
3277 | |
3278 for (i = 0; i < idents.dim; i++) | |
3279 { Identifier *id = (Identifier *)idents.data[i]; | |
3280 | |
3281 buf->writeByte('.'); | |
3282 | |
3283 if (id->dyncast() == DYNCAST_DSYMBOL) | |
3284 { | |
3285 TemplateInstance *ti = (TemplateInstance *)id; | |
3286 ti->toCBuffer(buf, hgs); | |
3287 } | |
3288 else | |
3289 buf->writestring(id->toChars()); | |
3290 } | |
3291 } | |
3292 | |
3293 d_uns64 TypeQualified::size(Loc loc) | |
3294 { | |
3295 error(this->loc, "size of type %s is not known", toChars()); | |
3296 return 1; | |
3297 } | |
3298 | |
3299 /************************************* | |
3300 * Takes an array of Identifiers and figures out if | |
3301 * it represents a Type or an Expression. | |
3302 * Output: | |
3303 * if expression, *pe is set | |
3304 * if type, *pt is set | |
3305 */ | |
3306 | |
3307 void TypeQualified::resolveHelper(Loc loc, Scope *sc, | |
3308 Dsymbol *s, Dsymbol *scopesym, | |
3309 Expression **pe, Type **pt, Dsymbol **ps) | |
3310 { | |
3311 Identifier *id = NULL; | |
3312 int i; | |
3313 VarDeclaration *v; | |
3314 EnumMember *em; | |
3315 TupleDeclaration *td; | |
3316 Type *t; | |
3317 Expression *e; | |
3318 | |
3319 #if 0 | |
3320 printf("TypeQualified::resolveHelper(sc = %p, idents = '%s')\n", sc, toChars()); | |
3321 if (scopesym) | |
3322 printf("\tscopesym = '%s'\n", scopesym->toChars()); | |
3323 #endif | |
3324 *pe = NULL; | |
3325 *pt = NULL; | |
3326 *ps = NULL; | |
3327 if (s) | |
3328 { | |
3329 //printf("\t1: s = '%s' %p, kind = '%s'\n",s->toChars(), s, s->kind()); | |
336 | 3330 s->checkDeprecated(loc, sc); // check for deprecated aliases |
159 | 3331 s = s->toAlias(); |
3332 //printf("\t2: s = '%s' %p, kind = '%s'\n",s->toChars(), s, s->kind()); | |
3333 for (i = 0; i < idents.dim; i++) | |
3334 { Dsymbol *sm; | |
3335 | |
3336 id = (Identifier *)idents.data[i]; | |
3337 sm = s->searchX(loc, sc, id); | |
3338 //printf("\t3: s = '%s' %p, kind = '%s'\n",s->toChars(), s, s->kind()); | |
3339 //printf("getType = '%s'\n", s->getType()->toChars()); | |
3340 if (!sm) | |
3341 { | |
3342 v = s->isVarDeclaration(); | |
3343 if (v && id == Id::length) | |
3344 { | |
3345 if (v->isConst() && v->getExpInitializer()) | |
3346 { e = v->getExpInitializer()->exp; | |
3347 } | |
3348 else | |
3349 e = new VarExp(loc, v); | |
3350 t = e->type; | |
3351 if (!t) | |
3352 goto Lerror; | |
3353 goto L3; | |
3354 } | |
3355 t = s->getType(); | |
3356 if (!t && s->isDeclaration()) | |
3357 t = s->isDeclaration()->type; | |
3358 if (t) | |
3359 { | |
3360 sm = t->toDsymbol(sc); | |
3361 if (sm) | |
3362 { sm = sm->search(loc, id, 0); | |
3363 if (sm) | |
3364 goto L2; | |
3365 } | |
3366 //e = t->getProperty(loc, id); | |
3367 e = new TypeExp(loc, t); | |
3368 e = t->dotExp(sc, e, id); | |
3369 i++; | |
3370 L3: | |
3371 for (; i < idents.dim; i++) | |
3372 { | |
3373 id = (Identifier *)idents.data[i]; | |
3374 //printf("e: '%s', id: '%s', type = %p\n", e->toChars(), id->toChars(), e->type); | |
3375 e = e->type->dotExp(sc, e, id); | |
3376 } | |
3377 *pe = e; | |
3378 } | |
3379 else | |
3380 Lerror: | |
3381 error(loc, "identifier '%s' of '%s' is not defined", id->toChars(), toChars()); | |
3382 return; | |
3383 } | |
3384 L2: | |
3385 s = sm->toAlias(); | |
3386 } | |
3387 | |
3388 v = s->isVarDeclaration(); | |
3389 if (v) | |
3390 { | |
3391 // It's not a type, it's an expression | |
3392 if (v->isConst() && v->getExpInitializer()) | |
3393 { | |
3394 ExpInitializer *ei = v->getExpInitializer(); | |
3395 assert(ei); | |
3396 *pe = ei->exp->copy(); // make copy so we can change loc | |
3397 (*pe)->loc = loc; | |
3398 } | |
3399 else | |
3400 { | |
3401 #if 0 | |
3402 WithScopeSymbol *withsym; | |
3403 if (scopesym && (withsym = scopesym->isWithScopeSymbol()) != NULL) | |
3404 { | |
3405 // Same as wthis.ident | |
3406 e = new VarExp(loc, withsym->withstate->wthis); | |
3407 e = new DotIdExp(loc, e, ident); | |
3408 //assert(0); // BUG: should handle this | |
3409 } | |
3410 else | |
3411 #endif | |
3412 *pe = new VarExp(loc, v); | |
3413 } | |
3414 return; | |
3415 } | |
3416 em = s->isEnumMember(); | |
3417 if (em) | |
3418 { | |
3419 // It's not a type, it's an expression | |
3420 *pe = em->value->copy(); | |
3421 return; | |
3422 } | |
3423 | |
3424 L1: | |
3425 t = s->getType(); | |
3426 if (!t) | |
3427 { | |
3428 // If the symbol is an import, try looking inside the import | |
3429 Import *si; | |
3430 | |
3431 si = s->isImport(); | |
3432 if (si) | |
3433 { | |
3434 s = si->search(loc, s->ident, 0); | |
3435 if (s && s != si) | |
3436 goto L1; | |
3437 s = si; | |
3438 } | |
3439 *ps = s; | |
3440 return; | |
3441 } | |
3442 if (t->ty == Tinstance && t != this && !t->deco) | |
3443 { error(loc, "forward reference to '%s'", t->toChars()); | |
3444 return; | |
3445 } | |
3446 | |
3447 if (t != this) | |
3448 { | |
3449 if (t->reliesOnTident()) | |
3450 { | |
3451 Scope *scx; | |
3452 | |
3453 for (scx = sc; 1; scx = scx->enclosing) | |
3454 { | |
3455 if (!scx) | |
3456 { error(loc, "forward reference to '%s'", t->toChars()); | |
3457 return; | |
3458 } | |
3459 if (scx->scopesym == scopesym) | |
3460 break; | |
3461 } | |
3462 t = t->semantic(loc, scx); | |
3463 //((TypeIdentifier *)t)->resolve(loc, scx, pe, &t, ps); | |
3464 } | |
3465 } | |
3466 if (t->ty == Ttuple) | |
772
cd7da2ba14d1
Fix bug reported by downs. Related to delegate types within tuple template parameters.
Christian Kamm <kamm incasoftware de>
parents:
771
diff
changeset
|
3467 *pt = t; |
159 | 3468 else |
3469 *pt = t->merge(); | |
3470 } | |
3471 if (!s) | |
3472 { | |
3473 error(loc, "identifier '%s' is not defined", toChars()); | |
3474 } | |
3475 } | |
3476 | |
3477 /***************************** TypeIdentifier *****************************/ | |
3478 | |
3479 TypeIdentifier::TypeIdentifier(Loc loc, Identifier *ident) | |
3480 : TypeQualified(Tident, loc) | |
3481 { | |
3482 this->ident = ident; | |
3483 } | |
3484 | |
3485 | |
3486 Type *TypeIdentifier::syntaxCopy() | |
3487 { | |
3488 TypeIdentifier *t; | |
3489 | |
3490 t = new TypeIdentifier(loc, ident); | |
3491 t->syntaxCopyHelper(this); | |
3492 return t; | |
3493 } | |
3494 | |
3495 void TypeIdentifier::toDecoBuffer(OutBuffer *buf) | |
3496 { unsigned len; | |
3497 char *name; | |
3498 | |
3499 name = ident->toChars(); | |
3500 len = strlen(name); | |
3501 buf->printf("%c%d%s", mangleChar[ty], len, name); | |
3502 //buf->printf("%c%s", mangleChar[ty], name); | |
3503 } | |
3504 | |
3505 void TypeIdentifier::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
3506 { | |
3507 if (mod != this->mod) | |
3508 { toCBuffer3(buf, hgs, mod); | |
3509 return; | |
3510 } | |
3511 buf->writestring(this->ident->toChars()); | |
3512 toCBuffer2Helper(buf, hgs); | |
3513 } | |
3514 | |
3515 /************************************* | |
3516 * Takes an array of Identifiers and figures out if | |
3517 * it represents a Type or an Expression. | |
3518 * Output: | |
3519 * if expression, *pe is set | |
3520 * if type, *pt is set | |
3521 */ | |
3522 | |
3523 void TypeIdentifier::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) | |
3524 { Dsymbol *s; | |
3525 Dsymbol *scopesym; | |
3526 | |
3527 //printf("TypeIdentifier::resolve(sc = %p, idents = '%s')\n", sc, toChars()); | |
3528 s = sc->search(loc, ident, &scopesym); | |
3529 resolveHelper(loc, sc, s, scopesym, pe, pt, ps); | |
3530 } | |
3531 | |
3532 /***************************************** | |
3533 * See if type resolves to a symbol, if so, | |
3534 * return that symbol. | |
3535 */ | |
3536 | |
3537 Dsymbol *TypeIdentifier::toDsymbol(Scope *sc) | |
3538 { | |
3539 //printf("TypeIdentifier::toDsymbol('%s')\n", toChars()); | |
3540 if (!sc) | |
3541 return NULL; | |
3542 //printf("ident = '%s'\n", ident->toChars()); | |
3543 | |
3544 Dsymbol *scopesym; | |
3545 Dsymbol *s = sc->search(loc, ident, &scopesym); | |
3546 if (s) | |
3547 { | |
3548 for (int i = 0; i < idents.dim; i++) | |
3549 { | |
3550 Identifier *id = (Identifier *)idents.data[i]; | |
3551 s = s->searchX(loc, sc, id); | |
3552 if (!s) // failed to find a symbol | |
3553 { //printf("\tdidn't find a symbol\n"); | |
3554 break; | |
3555 } | |
3556 } | |
3557 } | |
3558 return s; | |
3559 } | |
3560 | |
3561 Type *TypeIdentifier::semantic(Loc loc, Scope *sc) | |
3562 { | |
3563 Type *t; | |
3564 Expression *e; | |
3565 Dsymbol *s; | |
3566 | |
3567 //printf("TypeIdentifier::semantic(%s)\n", toChars()); | |
3568 resolve(loc, sc, &e, &t, &s); | |
3569 if (t) | |
3570 { | |
3571 //printf("\tit's a type %d, %s, %s\n", t->ty, t->toChars(), t->deco); | |
3572 | |
3573 if (t->ty == Ttypedef) | |
3574 { TypeTypedef *tt = (TypeTypedef *)t; | |
3575 | |
3576 if (tt->sym->sem == 1) | |
3577 error(loc, "circular reference of typedef %s", tt->toChars()); | |
3578 } | |
3579 } | |
3580 else | |
3581 { | |
3582 #ifdef DEBUG | |
3583 if (!global.gag) | |
3584 printf("1: "); | |
3585 #endif | |
3586 if (s) | |
3587 { | |
3588 s->error(loc, "is used as a type"); | |
3589 } | |
3590 else | |
3591 error(loc, "%s is used as a type", toChars()); | |
3592 t = tvoid; | |
3593 } | |
3594 //t->print(); | |
3595 return t; | |
3596 } | |
3597 | |
3598 Type *TypeIdentifier::reliesOnTident() | |
3599 { | |
3600 return this; | |
3601 } | |
3602 | |
3603 Expression *TypeIdentifier::toExpression() | |
3604 { | |
3605 Expression *e = new IdentifierExp(loc, ident); | |
3606 for (int i = 0; i < idents.dim; i++) | |
3607 { | |
3608 Identifier *id = (Identifier *)idents.data[i]; | |
3609 e = new DotIdExp(loc, e, id); | |
3610 } | |
3611 | |
3612 return e; | |
3613 } | |
3614 | |
3615 /***************************** TypeInstance *****************************/ | |
3616 | |
3617 TypeInstance::TypeInstance(Loc loc, TemplateInstance *tempinst) | |
3618 : TypeQualified(Tinstance, loc) | |
3619 { | |
3620 this->tempinst = tempinst; | |
3621 } | |
3622 | |
3623 Type *TypeInstance::syntaxCopy() | |
3624 { | |
3625 //printf("TypeInstance::syntaxCopy() %s, %d\n", toChars(), idents.dim); | |
3626 TypeInstance *t; | |
3627 | |
3628 t = new TypeInstance(loc, (TemplateInstance *)tempinst->syntaxCopy(NULL)); | |
3629 t->syntaxCopyHelper(this); | |
3630 return t; | |
3631 } | |
3632 | |
3633 | |
3634 void TypeInstance::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
3635 { | |
3636 if (mod != this->mod) | |
3637 { toCBuffer3(buf, hgs, mod); | |
3638 return; | |
3639 } | |
3640 tempinst->toCBuffer(buf, hgs); | |
3641 toCBuffer2Helper(buf, hgs); | |
3642 } | |
3643 | |
3644 void TypeInstance::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) | |
3645 { | |
3646 // Note close similarity to TypeIdentifier::resolve() | |
3647 | |
3648 Dsymbol *s; | |
3649 | |
3650 *pe = NULL; | |
3651 *pt = NULL; | |
3652 *ps = NULL; | |
3653 | |
3654 #if 0 | |
3655 if (!idents.dim) | |
3656 { | |
3657 error(loc, "template instance '%s' has no identifier", toChars()); | |
3658 return; | |
3659 } | |
3660 #endif | |
3661 //id = (Identifier *)idents.data[0]; | |
3662 //printf("TypeInstance::resolve(sc = %p, idents = '%s')\n", sc, id->toChars()); | |
3663 s = tempinst; | |
3664 if (s) | |
3665 s->semantic(sc); | |
3666 resolveHelper(loc, sc, s, NULL, pe, pt, ps); | |
3667 //printf("pt = '%s'\n", (*pt)->toChars()); | |
3668 } | |
3669 | |
3670 Type *TypeInstance::semantic(Loc loc, Scope *sc) | |
3671 { | |
3672 Type *t; | |
3673 Expression *e; | |
3674 Dsymbol *s; | |
3675 | |
3676 //printf("TypeInstance::semantic(%s)\n", toChars()); | |
3677 | |
3678 if (sc->parameterSpecialization) | |
3679 { | |
3680 unsigned errors = global.errors; | |
3681 global.gag++; | |
3682 | |
3683 resolve(loc, sc, &e, &t, &s); | |
3684 | |
3685 global.gag--; | |
3686 if (errors != global.errors) | |
3687 { if (global.gag == 0) | |
3688 global.errors = errors; | |
3689 return this; | |
3690 } | |
3691 } | |
3692 else | |
3693 resolve(loc, sc, &e, &t, &s); | |
3694 | |
3695 if (!t) | |
3696 { | |
3697 #ifdef DEBUG | |
3698 printf("2: "); | |
3699 #endif | |
3700 error(loc, "%s is used as a type", toChars()); | |
3701 t = tvoid; | |
3702 } | |
3703 return t; | |
3704 } | |
3705 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3706 Dsymbol *TypeInstance::toDsymbol(Scope *sc) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3707 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3708 Type *t; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3709 Expression *e; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3710 Dsymbol *s; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3711 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3712 //printf("TypeInstance::semantic(%s)\n", toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3713 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3714 if (sc->parameterSpecialization) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3715 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3716 unsigned errors = global.errors; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3717 global.gag++; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3718 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3719 resolve(loc, sc, &e, &t, &s); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3720 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3721 global.gag--; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3722 if (errors != global.errors) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3723 { if (global.gag == 0) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3724 global.errors = errors; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3725 return NULL; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3726 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3727 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3728 else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3729 resolve(loc, sc, &e, &t, &s); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3730 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3731 return s; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3732 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
3733 |
159 | 3734 |
3735 /***************************** TypeTypeof *****************************/ | |
3736 | |
3737 TypeTypeof::TypeTypeof(Loc loc, Expression *exp) | |
3738 : TypeQualified(Ttypeof, loc) | |
3739 { | |
3740 this->exp = exp; | |
3741 } | |
3742 | |
3743 Type *TypeTypeof::syntaxCopy() | |
3744 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3745 //printf("TypeTypeof::syntaxCopy() %s\n", toChars()); |
159 | 3746 TypeTypeof *t; |
3747 | |
3748 t = new TypeTypeof(loc, exp->syntaxCopy()); | |
3749 t->syntaxCopyHelper(this); | |
3750 return t; | |
3751 } | |
3752 | |
3753 Dsymbol *TypeTypeof::toDsymbol(Scope *sc) | |
3754 { | |
3755 Type *t; | |
3756 | |
336 | 3757 t = semantic(loc, sc); |
159 | 3758 if (t == this) |
3759 return NULL; | |
3760 return t->toDsymbol(sc); | |
3761 } | |
3762 | |
3763 void TypeTypeof::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
3764 { | |
3765 if (mod != this->mod) | |
3766 { toCBuffer3(buf, hgs, mod); | |
3767 return; | |
3768 } | |
3769 buf->writestring("typeof("); | |
3770 exp->toCBuffer(buf, hgs); | |
3771 buf->writeByte(')'); | |
3772 toCBuffer2Helper(buf, hgs); | |
3773 } | |
3774 | |
3775 Type *TypeTypeof::semantic(Loc loc, Scope *sc) | |
3776 { Expression *e; | |
3777 Type *t; | |
3778 | |
3779 //printf("TypeTypeof::semantic() %p\n", this); | |
3780 | |
3781 //static int nest; if (++nest == 50) *(char*)0=0; | |
3782 | |
3783 #if 0 | |
3784 /* Special case for typeof(this) and typeof(super) since both | |
3785 * should work even if they are not inside a non-static member function | |
3786 */ | |
3787 if (exp->op == TOKthis || exp->op == TOKsuper) | |
3788 { | |
3789 // Find enclosing struct or class | |
3790 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
3791 { | |
3792 ClassDeclaration *cd; | |
3793 StructDeclaration *sd; | |
3794 | |
3795 if (!s) | |
3796 { | |
3797 error(loc, "%s is not in a struct or class scope", exp->toChars()); | |
3798 goto Lerr; | |
3799 } | |
3800 cd = s->isClassDeclaration(); | |
3801 if (cd) | |
3802 { | |
3803 if (exp->op == TOKsuper) | |
3804 { | |
3805 cd = cd->baseClass; | |
3806 if (!cd) | |
3807 { error(loc, "class %s has no 'super'", s->toChars()); | |
3808 goto Lerr; | |
3809 } | |
3810 } | |
3811 t = cd->type; | |
3812 break; | |
3813 } | |
3814 sd = s->isStructDeclaration(); | |
3815 if (sd) | |
3816 { | |
3817 if (exp->op == TOKsuper) | |
3818 { | |
3819 error(loc, "struct %s has no 'super'", sd->toChars()); | |
3820 goto Lerr; | |
3821 } | |
3822 t = sd->type->pointerTo(); | |
3823 break; | |
3824 } | |
3825 } | |
3826 } | |
3827 else | |
3828 #endif | |
3829 { | |
3830 sc->intypeof++; | |
3831 exp = exp->semantic(sc); | |
3832 sc->intypeof--; | |
336 | 3833 if (exp->op == TOKtype) |
3834 { | |
3835 error(loc, "argument %s to typeof is not an expression", exp->toChars()); | |
3836 } | |
159 | 3837 t = exp->type; |
3838 if (!t) | |
3839 { | |
3840 error(loc, "expression (%s) has no type", exp->toChars()); | |
3841 goto Lerr; | |
3842 } | |
3843 } | |
3844 | |
3845 if (idents.dim) | |
3846 { | |
3847 Dsymbol *s = t->toDsymbol(sc); | |
3848 for (size_t i = 0; i < idents.dim; i++) | |
3849 { | |
3850 if (!s) | |
3851 break; | |
3852 Identifier *id = (Identifier *)idents.data[i]; | |
3853 s = s->searchX(loc, sc, id); | |
3854 } | |
3855 if (s) | |
3856 { | |
3857 t = s->getType(); | |
3858 if (!t) | |
3859 { error(loc, "%s is not a type", s->toChars()); | |
3860 goto Lerr; | |
3861 } | |
3862 } | |
3863 else | |
3864 { error(loc, "cannot resolve .property for %s", toChars()); | |
3865 goto Lerr; | |
3866 } | |
3867 } | |
3868 return t; | |
3869 | |
3870 Lerr: | |
3871 return tvoid; | |
3872 } | |
3873 | |
3874 d_uns64 TypeTypeof::size(Loc loc) | |
3875 { | |
3876 if (exp->type) | |
3877 return exp->type->size(loc); | |
3878 else | |
3879 return TypeQualified::size(loc); | |
3880 } | |
3881 | |
3882 | |
3883 | |
3884 /***************************** TypeEnum *****************************/ | |
3885 | |
3886 TypeEnum::TypeEnum(EnumDeclaration *sym) | |
3887 : Type(Tenum, NULL) | |
3888 { | |
3889 this->sym = sym; | |
3890 } | |
3891 | |
3892 char *TypeEnum::toChars() | |
3893 { | |
3894 return sym->toChars(); | |
3895 } | |
3896 | |
486
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
445
diff
changeset
|
3897 Type *TypeEnum::syntaxCopy() |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
445
diff
changeset
|
3898 { |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
445
diff
changeset
|
3899 return this; |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
445
diff
changeset
|
3900 } |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
445
diff
changeset
|
3901 |
159 | 3902 Type *TypeEnum::semantic(Loc loc, Scope *sc) |
3903 { | |
3904 sym->semantic(sc); | |
3905 return merge(); | |
3906 } | |
3907 | |
3908 d_uns64 TypeEnum::size(Loc loc) | |
3909 { | |
3910 if (!sym->memtype) | |
3911 { | |
3912 error(loc, "enum %s is forward referenced", sym->toChars()); | |
3913 return 4; | |
3914 } | |
3915 return sym->memtype->size(loc); | |
3916 } | |
3917 | |
3918 unsigned TypeEnum::alignsize() | |
3919 { | |
3920 if (!sym->memtype) | |
3921 { | |
3922 #ifdef DEBUG | |
3923 printf("1: "); | |
3924 #endif | |
3925 error(0, "enum %s is forward referenced", sym->toChars()); | |
3926 return 4; | |
3927 } | |
3928 return sym->memtype->alignsize(); | |
3929 } | |
3930 | |
3931 Dsymbol *TypeEnum::toDsymbol(Scope *sc) | |
3932 { | |
3933 return sym; | |
3934 } | |
3935 | |
3936 Type *TypeEnum::toBasetype() | |
3937 { | |
3938 if (!sym->memtype) | |
3939 { | |
3940 #ifdef DEBUG | |
3941 printf("2: "); | |
3942 #endif | |
3943 error(sym->loc, "enum %s is forward referenced", sym->toChars()); | |
3944 return tint32; | |
3945 } | |
3946 return sym->memtype->toBasetype(); | |
3947 } | |
3948 | |
3949 void TypeEnum::toDecoBuffer(OutBuffer *buf) | |
3950 { char *name; | |
3951 | |
3952 name = sym->mangle(); | |
3953 // if (name[0] == '_' && name[1] == 'D') | |
3954 // name += 2; | |
3955 buf->printf("%c%s", mangleChar[ty], name); | |
3956 } | |
3957 | |
3958 void TypeEnum::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
3959 { | |
3960 if (mod != this->mod) | |
3961 { toCBuffer3(buf, hgs, mod); | |
3962 return; | |
3963 } | |
3964 buf->writestring(sym->toChars()); | |
3965 } | |
3966 | |
3967 Expression *TypeEnum::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
3968 { | |
3969 EnumMember *m; | |
3970 Dsymbol *s; | |
3971 Expression *em; | |
3972 | |
3973 #if LOGDOTEXP | |
3974 printf("TypeEnum::dotExp(e = '%s', ident = '%s') '%s'\n", e->toChars(), ident->toChars(), toChars()); | |
3975 #endif | |
3976 if (!sym->symtab) | |
3977 goto Lfwd; | |
3978 s = sym->symtab->lookup(ident); | |
3979 if (!s) | |
3980 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3981 if (ident == Id::max || |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3982 ident == Id::min || |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3983 ident == Id::init || |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3984 ident == Id::stringof || |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3985 !sym->memtype |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3986 ) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3987 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3988 return getProperty(e->loc, ident); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3989 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
3990 return sym->memtype->dotExp(sc, e, ident); |
159 | 3991 } |
3992 m = s->isEnumMember(); | |
3993 em = m->value->copy(); | |
3994 em->loc = e->loc; | |
3995 return em; | |
3996 | |
3997 Lfwd: | |
3998 error(e->loc, "forward reference of %s.%s", toChars(), ident->toChars()); | |
3999 return new IntegerExp(0, 0, this); | |
4000 } | |
4001 | |
4002 Expression *TypeEnum::getProperty(Loc loc, Identifier *ident) | |
4003 { Expression *e; | |
4004 | |
4005 if (ident == Id::max) | |
4006 { | |
4007 if (!sym->symtab) | |
4008 goto Lfwd; | |
4009 e = new IntegerExp(0, sym->maxval, this); | |
4010 } | |
4011 else if (ident == Id::min) | |
4012 { | |
4013 if (!sym->symtab) | |
4014 goto Lfwd; | |
4015 e = new IntegerExp(0, sym->minval, this); | |
4016 } | |
4017 else if (ident == Id::init) | |
4018 { | |
4019 if (!sym->symtab) | |
4020 goto Lfwd; | |
4021 e = defaultInit(loc); | |
4022 } | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
822
diff
changeset
|
4023 else if (ident == Id::stringof) |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
822
diff
changeset
|
4024 { char *s = toChars(); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
822
diff
changeset
|
4025 e = new StringExp(loc, s, strlen(s), 'c'); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
822
diff
changeset
|
4026 Scope sc; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
822
diff
changeset
|
4027 e = e->semantic(&sc); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
822
diff
changeset
|
4028 } |
159 | 4029 else |
4030 { | |
4031 if (!sym->memtype) | |
4032 goto Lfwd; | |
4033 e = sym->memtype->getProperty(loc, ident); | |
4034 } | |
4035 return e; | |
4036 | |
4037 Lfwd: | |
4038 error(loc, "forward reference of %s.%s", toChars(), ident->toChars()); | |
4039 return new IntegerExp(0, 0, this); | |
4040 } | |
4041 | |
4042 int TypeEnum::isintegral() | |
4043 { | |
4044 return 1; | |
4045 } | |
4046 | |
4047 int TypeEnum::isfloating() | |
4048 { | |
4049 return 0; | |
4050 } | |
4051 | |
4052 int TypeEnum::isunsigned() | |
4053 { | |
4054 return sym->memtype->isunsigned(); | |
4055 } | |
4056 | |
4057 int TypeEnum::isscalar() | |
4058 { | |
4059 return 1; | |
4060 //return sym->memtype->isscalar(); | |
4061 } | |
4062 | |
4063 MATCH TypeEnum::implicitConvTo(Type *to) | |
4064 { MATCH m; | |
4065 | |
4066 //printf("TypeEnum::implicitConvTo()\n"); | |
4067 if (this->equals(to)) | |
4068 m = MATCHexact; // exact match | |
4069 else if (sym->memtype->implicitConvTo(to)) | |
4070 m = MATCHconvert; // match with conversions | |
4071 else | |
4072 m = MATCHnomatch; // no match | |
4073 return m; | |
4074 } | |
4075 | |
4076 Expression *TypeEnum::defaultInit(Loc loc) | |
4077 { | |
4078 #if LOGDEFAULTINIT | |
4079 printf("TypeEnum::defaultInit() '%s'\n", toChars()); | |
4080 #endif | |
4081 // Initialize to first member of enum | |
4082 Expression *e; | |
4083 e = new IntegerExp(loc, sym->defaultval, this); | |
4084 return e; | |
4085 } | |
4086 | |
4087 int TypeEnum::isZeroInit() | |
4088 { | |
4089 return (sym->defaultval == 0); | |
4090 } | |
4091 | |
4092 int TypeEnum::hasPointers() | |
4093 { | |
4094 return toBasetype()->hasPointers(); | |
4095 } | |
4096 | |
4097 /***************************** TypeTypedef *****************************/ | |
4098 | |
4099 TypeTypedef::TypeTypedef(TypedefDeclaration *sym) | |
4100 : Type(Ttypedef, NULL) | |
4101 { | |
4102 this->sym = sym; | |
4103 } | |
4104 | |
4105 Type *TypeTypedef::syntaxCopy() | |
4106 { | |
4107 return this; | |
4108 } | |
4109 | |
4110 char *TypeTypedef::toChars() | |
4111 { | |
4112 return sym->toChars(); | |
4113 } | |
4114 | |
4115 Type *TypeTypedef::semantic(Loc loc, Scope *sc) | |
4116 { | |
4117 //printf("TypeTypedef::semantic(%s), sem = %d\n", toChars(), sym->sem); | |
4118 sym->semantic(sc); | |
4119 return merge(); | |
4120 } | |
4121 | |
4122 d_uns64 TypeTypedef::size(Loc loc) | |
4123 { | |
4124 return sym->basetype->size(loc); | |
4125 } | |
4126 | |
4127 unsigned TypeTypedef::alignsize() | |
4128 { | |
4129 return sym->basetype->alignsize(); | |
4130 } | |
4131 | |
4132 Dsymbol *TypeTypedef::toDsymbol(Scope *sc) | |
4133 { | |
4134 return sym; | |
4135 } | |
4136 | |
4137 void TypeTypedef::toDecoBuffer(OutBuffer *buf) | |
4138 { unsigned len; | |
4139 char *name; | |
4140 | |
4141 name = sym->mangle(); | |
4142 // if (name[0] == '_' && name[1] == 'D') | |
4143 // name += 2; | |
4144 //len = strlen(name); | |
4145 //buf->printf("%c%d%s", mangleChar[ty], len, name); | |
4146 buf->printf("%c%s", mangleChar[ty], name); | |
4147 } | |
4148 | |
4149 void TypeTypedef::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
4150 { | |
4151 //printf("TypeTypedef::toCBuffer2() '%s'\n", sym->toChars()); | |
4152 if (mod != this->mod) | |
4153 { toCBuffer3(buf, hgs, mod); | |
4154 return; | |
4155 } | |
4156 buf->writestring(sym->toChars()); | |
4157 } | |
4158 | |
4159 Expression *TypeTypedef::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
4160 { | |
4161 #if LOGDOTEXP | |
4162 printf("TypeTypedef::dotExp(e = '%s', ident = '%s') '%s'\n", e->toChars(), ident->toChars(), toChars()); | |
4163 #endif | |
4164 if (ident == Id::init) | |
4165 { | |
4166 return Type::dotExp(sc, e, ident); | |
4167 } | |
4168 return sym->basetype->dotExp(sc, e, ident); | |
4169 } | |
4170 | |
4171 Expression *TypeTypedef::getProperty(Loc loc, Identifier *ident) | |
4172 { | |
4173 if (ident == Id::init) | |
4174 { | |
4175 return Type::getProperty(loc, ident); | |
4176 } | |
4177 return sym->basetype->getProperty(loc, ident); | |
4178 } | |
4179 | |
4180 int TypeTypedef::isbit() | |
4181 { | |
4182 return sym->basetype->isbit(); | |
4183 } | |
4184 | |
4185 int TypeTypedef::isintegral() | |
4186 { | |
4187 //printf("TypeTypedef::isintegral()\n"); | |
4188 //printf("sym = '%s'\n", sym->toChars()); | |
4189 //printf("basetype = '%s'\n", sym->basetype->toChars()); | |
4190 return sym->basetype->isintegral(); | |
4191 } | |
4192 | |
4193 int TypeTypedef::isfloating() | |
4194 { | |
4195 return sym->basetype->isfloating(); | |
4196 } | |
4197 | |
4198 int TypeTypedef::isreal() | |
4199 { | |
4200 return sym->basetype->isreal(); | |
4201 } | |
4202 | |
4203 int TypeTypedef::isimaginary() | |
4204 { | |
4205 return sym->basetype->isimaginary(); | |
4206 } | |
4207 | |
4208 int TypeTypedef::iscomplex() | |
4209 { | |
4210 return sym->basetype->iscomplex(); | |
4211 } | |
4212 | |
4213 int TypeTypedef::isunsigned() | |
4214 { | |
4215 return sym->basetype->isunsigned(); | |
4216 } | |
4217 | |
4218 int TypeTypedef::isscalar() | |
4219 { | |
4220 return sym->basetype->isscalar(); | |
4221 } | |
4222 | |
4223 int TypeTypedef::checkBoolean() | |
4224 { | |
4225 return sym->basetype->checkBoolean(); | |
4226 } | |
4227 | |
4228 Type *TypeTypedef::toBasetype() | |
4229 { | |
4230 if (sym->inuse) | |
4231 { | |
4232 sym->error("circular definition"); | |
4233 sym->basetype = Type::terror; | |
4234 return Type::terror; | |
4235 } | |
4236 sym->inuse = 1; | |
4237 Type *t = sym->basetype->toBasetype(); | |
4238 sym->inuse = 0; | |
4239 return t; | |
4240 } | |
4241 | |
4242 MATCH TypeTypedef::implicitConvTo(Type *to) | |
4243 { MATCH m; | |
4244 | |
4245 //printf("TypeTypedef::implicitConvTo()\n"); | |
4246 if (this->equals(to)) | |
4247 m = MATCHexact; // exact match | |
4248 else if (sym->basetype->implicitConvTo(to)) | |
4249 m = MATCHconvert; // match with conversions | |
4250 else | |
4251 m = MATCHnomatch; // no match | |
4252 return m; | |
4253 } | |
4254 | |
4255 Expression *TypeTypedef::defaultInit(Loc loc) | |
4256 { Expression *e; | |
4257 Type *bt; | |
4258 | |
4259 #if LOGDEFAULTINIT | |
4260 printf("TypeTypedef::defaultInit() '%s'\n", toChars()); | |
4261 #endif | |
4262 if (sym->init) | |
4263 { | |
4264 //sym->init->toExpression()->print(); | |
4265 return sym->init->toExpression(); | |
4266 } | |
4267 bt = sym->basetype; | |
4268 e = bt->defaultInit(loc); | |
4269 e->type = this; | |
4270 while (bt->ty == Tsarray) | |
4271 { | |
4272 e->type = bt->next; | |
4273 bt = bt->next->toBasetype(); | |
4274 } | |
4275 return e; | |
4276 } | |
4277 | |
4278 int TypeTypedef::isZeroInit() | |
4279 { | |
4280 if (sym->init) | |
4281 { | |
4282 if (sym->init->isVoidInitializer()) | |
4283 return 1; // initialize voids to 0 | |
4284 Expression *e = sym->init->toExpression(); | |
4285 if (e && e->isBool(FALSE)) | |
4286 return 1; | |
4287 return 0; // assume not | |
4288 } | |
4289 if (sym->inuse) | |
4290 { | |
4291 sym->error("circular definition"); | |
4292 sym->basetype = Type::terror; | |
4293 } | |
4294 sym->inuse = 1; | |
4295 int result = sym->basetype->isZeroInit(); | |
4296 sym->inuse = 0; | |
4297 return result; | |
4298 } | |
4299 | |
4300 int TypeTypedef::hasPointers() | |
4301 { | |
4302 return toBasetype()->hasPointers(); | |
4303 } | |
4304 | |
4305 /***************************** TypeStruct *****************************/ | |
4306 | |
4307 TypeStruct::TypeStruct(StructDeclaration *sym) | |
4308 : Type(Tstruct, NULL) | |
4309 { | |
4310 this->sym = sym; | |
1029
4d366a75d95f
Added hasUnalignedFields helper to check if a type has unaligned fields - as per request from fvbommel. Result is cached in TypeStruct.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
1024
diff
changeset
|
4311 |
4d366a75d95f
Added hasUnalignedFields helper to check if a type has unaligned fields - as per request from fvbommel. Result is cached in TypeStruct.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
1024
diff
changeset
|
4312 // LDC |
4d366a75d95f
Added hasUnalignedFields helper to check if a type has unaligned fields - as per request from fvbommel. Result is cached in TypeStruct.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
1024
diff
changeset
|
4313 this->unaligned = 0; |
159 | 4314 } |
4315 | |
4316 char *TypeStruct::toChars() | |
4317 { | |
4318 //printf("sym.parent: %s, deco = %s\n", sym->parent->toChars(), deco); | |
4319 TemplateInstance *ti = sym->parent->isTemplateInstance(); | |
4320 if (ti && ti->toAlias() == sym) | |
4321 return ti->toChars(); | |
4322 return sym->toChars(); | |
4323 } | |
4324 | |
4325 Type *TypeStruct::syntaxCopy() | |
4326 { | |
4327 return this; | |
4328 } | |
4329 | |
4330 Type *TypeStruct::semantic(Loc loc, Scope *sc) | |
4331 { | |
4332 //printf("TypeStruct::semantic('%s')\n", sym->toChars()); | |
4333 | |
4334 /* Cannot do semantic for sym because scope chain may not | |
4335 * be right. | |
4336 */ | |
4337 //sym->semantic(sc); | |
4338 | |
4339 return merge(); | |
4340 } | |
4341 | |
4342 d_uns64 TypeStruct::size(Loc loc) | |
4343 { | |
4344 return sym->size(loc); | |
4345 } | |
4346 | |
4347 unsigned TypeStruct::alignsize() | |
4348 { unsigned sz; | |
4349 | |
4350 sym->size(0); // give error for forward references | |
4351 sz = sym->alignsize; | |
4352 if (sz > sym->structalign) | |
4353 sz = sym->structalign; | |
4354 return sz; | |
4355 } | |
4356 | |
4357 Dsymbol *TypeStruct::toDsymbol(Scope *sc) | |
4358 { | |
4359 return sym; | |
4360 } | |
4361 | |
4362 void TypeStruct::toDecoBuffer(OutBuffer *buf) | |
4363 { unsigned len; | |
4364 char *name; | |
4365 | |
4366 name = sym->mangle(); | |
4367 //printf("TypeStruct::toDecoBuffer('%s') = '%s'\n", toChars(), name); | |
4368 // if (name[0] == '_' && name[1] == 'D') | |
4369 // name += 2; | |
4370 //len = strlen(name); | |
4371 //buf->printf("%c%d%s", mangleChar[ty], len, name); | |
4372 buf->printf("%c%s", mangleChar[ty], name); | |
4373 } | |
4374 | |
4375 void TypeStruct::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
4376 { | |
4377 if (mod != this->mod) | |
4378 { toCBuffer3(buf, hgs, mod); | |
4379 return; | |
4380 } | |
4381 TemplateInstance *ti = sym->parent->isTemplateInstance(); | |
4382 if (ti && ti->toAlias() == sym) | |
4383 buf->writestring(ti->toChars()); | |
4384 else | |
4385 buf->writestring(sym->toChars()); | |
4386 } | |
4387 | |
4388 Expression *TypeStruct::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
4389 { unsigned offset; | |
4390 | |
4391 Expression *b; | |
4392 VarDeclaration *v; | |
4393 Dsymbol *s; | |
4394 DotVarExp *de; | |
4395 Declaration *d; | |
4396 | |
4397 #if LOGDOTEXP | |
4398 printf("TypeStruct::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
4399 #endif | |
4400 if (!sym->members) | |
4401 { | |
4402 error(e->loc, "struct %s is forward referenced", sym->toChars()); | |
4403 return new IntegerExp(e->loc, 0, Type::tint32); | |
4404 } | |
4405 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4406 /* If e.tupleof |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4407 */ |
159 | 4408 if (ident == Id::tupleof) |
4409 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4410 /* Create a TupleExp out of the fields of the struct e: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4411 * (e.field0, e.field1, e.field2, ...) |
159 | 4412 */ |
336 | 4413 e = e->semantic(sc); // do this before turning on noaccesscheck |
159 | 4414 Expressions *exps = new Expressions; |
4415 exps->reserve(sym->fields.dim); | |
4416 for (size_t i = 0; i < sym->fields.dim; i++) | |
4417 { VarDeclaration *v = (VarDeclaration *)sym->fields.data[i]; | |
4418 Expression *fe = new DotVarExp(e->loc, e, v); | |
4419 exps->push(fe); | |
4420 } | |
4421 e = new TupleExp(e->loc, exps); | |
336 | 4422 sc = sc->push(); |
4423 sc->noaccesscheck = 1; | |
159 | 4424 e = e->semantic(sc); |
336 | 4425 sc->pop(); |
159 | 4426 return e; |
4427 } | |
4428 | |
4429 if (e->op == TOKdotexp) | |
4430 { DotExp *de = (DotExp *)e; | |
4431 | |
4432 if (de->e1->op == TOKimport) | |
4433 { | |
4434 ScopeExp *se = (ScopeExp *)de->e1; | |
4435 | |
4436 s = se->sds->search(e->loc, ident, 0); | |
4437 e = de->e1; | |
4438 goto L1; | |
4439 } | |
4440 } | |
4441 | |
4442 s = sym->search(e->loc, ident, 0); | |
4443 L1: | |
4444 if (!s) | |
4445 { | |
4446 //return getProperty(e->loc, ident); | |
4447 return Type::dotExp(sc, e, ident); | |
4448 } | |
336 | 4449 if (!s->isFuncDeclaration()) // because of overloading |
4450 s->checkDeprecated(e->loc, sc); | |
159 | 4451 s = s->toAlias(); |
4452 | |
4453 v = s->isVarDeclaration(); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
4454 if (v && v->isConst() && v->type->toBasetype()->ty != Tsarray) |
159 | 4455 { ExpInitializer *ei = v->getExpInitializer(); |
4456 | |
4457 if (ei) | |
4458 { e = ei->exp->copy(); // need to copy it if it's a StringExp | |
4459 e = e->semantic(sc); | |
4460 return e; | |
4461 } | |
4462 } | |
4463 | |
4464 if (s->getType()) | |
4465 { | |
4466 //return new DotTypeExp(e->loc, e, s); | |
4467 return new TypeExp(e->loc, s->getType()); | |
4468 } | |
4469 | |
4470 EnumMember *em = s->isEnumMember(); | |
4471 if (em) | |
4472 { | |
4473 assert(em->value); | |
4474 return em->value->copy(); | |
4475 } | |
4476 | |
4477 TemplateMixin *tm = s->isTemplateMixin(); | |
4478 if (tm) | |
4479 { Expression *de; | |
4480 | |
4481 de = new DotExp(e->loc, e, new ScopeExp(e->loc, tm)); | |
4482 de->type = e->type; | |
4483 return de; | |
4484 } | |
4485 | |
4486 TemplateDeclaration *td = s->isTemplateDeclaration(); | |
4487 if (td) | |
4488 { | |
4489 e = new DotTemplateExp(e->loc, e, td); | |
4490 e->semantic(sc); | |
4491 return e; | |
4492 } | |
4493 | |
4494 TemplateInstance *ti = s->isTemplateInstance(); | |
4495 if (ti) | |
4496 { if (!ti->semanticdone) | |
4497 ti->semantic(sc); | |
4498 s = ti->inst->toAlias(); | |
4499 if (!s->isTemplateInstance()) | |
4500 goto L1; | |
4501 Expression *de = new DotExp(e->loc, e, new ScopeExp(e->loc, ti)); | |
4502 de->type = e->type; | |
4503 return de; | |
4504 } | |
4505 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4506 Import *timp = s->isImport(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4507 if (timp) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4508 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4509 e = new DsymbolExp(e->loc, s); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4510 e = e->semantic(sc); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4511 return e; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4512 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4513 |
159 | 4514 d = s->isDeclaration(); |
4515 #ifdef DEBUG | |
4516 if (!d) | |
4517 printf("d = %s '%s'\n", s->kind(), s->toChars()); | |
4518 #endif | |
4519 assert(d); | |
4520 | |
4521 if (e->op == TOKtype) | |
4522 { FuncDeclaration *fd = sc->func; | |
4523 | |
4524 if (d->needThis() && fd && fd->vthis) | |
4525 { | |
4526 e = new DotVarExp(e->loc, new ThisExp(e->loc), d); | |
4527 e = e->semantic(sc); | |
4528 return e; | |
4529 } | |
4530 if (d->isTupleDeclaration()) | |
4531 { | |
4532 e = new TupleExp(e->loc, d->isTupleDeclaration()); | |
4533 e = e->semantic(sc); | |
4534 return e; | |
4535 } | |
4536 return new VarExp(e->loc, d); | |
4537 } | |
4538 | |
4539 if (d->isDataseg()) | |
4540 { | |
4541 // (e, d) | |
4542 VarExp *ve; | |
4543 | |
4544 accessCheck(e->loc, sc, e, d); | |
4545 ve = new VarExp(e->loc, d); | |
4546 e = new CommaExp(e->loc, e, ve); | |
4547 e->type = d->type; | |
4548 return e; | |
4549 } | |
4550 | |
4551 if (v) | |
4552 { | |
4553 if (v->toParent() != sym) | |
4554 sym->error(e->loc, "'%s' is not a member", v->toChars()); | |
4555 | |
4556 // *(&e + offset) | |
4557 accessCheck(e->loc, sc, e, d); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
4558 #if 0 |
159 | 4559 b = new AddrExp(e->loc, e); |
4560 b->type = e->type->pointerTo(); | |
4561 b = new AddExp(e->loc, b, new IntegerExp(e->loc, v->offset, Type::tint32)); | |
4562 b->type = v->type->pointerTo(); | |
4563 e = new PtrExp(e->loc, b); | |
4564 e->type = v->type; | |
4565 return e; | |
585
fbb1a366cfbc
Complex number should now follow the D ABI on x86. They're also treated as first class values now. Big change.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
509
diff
changeset
|
4566 #endif |
159 | 4567 } |
4568 | |
4569 de = new DotVarExp(e->loc, e, d); | |
4570 return de->semantic(sc); | |
4571 } | |
4572 | |
4573 unsigned TypeStruct::memalign(unsigned salign) | |
4574 { | |
4575 sym->size(0); // give error for forward references | |
4576 return sym->structalign; | |
4577 } | |
4578 | |
4579 Expression *TypeStruct::defaultInit(Loc loc) | |
913
29c0d1194033
Fix #198 and #199 by making CTFE on static struct initializers work.
Christian Kamm <kamm incasoftware de>
parents:
900
diff
changeset
|
4580 { Declaration *d; |
159 | 4581 |
4582 #if LOGDEFAULTINIT | |
4583 printf("TypeStruct::defaultInit() '%s'\n", toChars()); | |
4584 #endif | |
913
29c0d1194033
Fix #198 and #199 by making CTFE on static struct initializers work.
Christian Kamm <kamm incasoftware de>
parents:
900
diff
changeset
|
4585 d = new StaticStructInitDeclaration(sym->loc, sym); |
159 | 4586 assert(d); |
4587 d->type = this; | |
4588 return new VarExp(sym->loc, d); | |
4589 } | |
4590 | |
4591 int TypeStruct::isZeroInit() | |
4592 { | |
4593 return sym->zeroInit; | |
4594 } | |
4595 | |
4596 int TypeStruct::checkBoolean() | |
4597 { | |
4598 return FALSE; | |
4599 } | |
4600 | |
4601 int TypeStruct::hasPointers() | |
4602 { | |
4603 StructDeclaration *s = sym; | |
4604 | |
4605 sym->size(0); // give error for forward references | |
4606 if (s->members) | |
4607 { | |
4608 for (size_t i = 0; i < s->members->dim; i++) | |
4609 { | |
4610 Dsymbol *sm = (Dsymbol *)s->members->data[i]; | |
4611 if (sm->hasPointers()) | |
4612 return TRUE; | |
4613 } | |
4614 } | |
4615 return FALSE; | |
4616 } | |
4617 | |
4618 | |
4619 /***************************** TypeClass *****************************/ | |
4620 | |
4621 TypeClass::TypeClass(ClassDeclaration *sym) | |
4622 : Type(Tclass, NULL) | |
4623 { | |
4624 this->sym = sym; | |
4625 } | |
4626 | |
4627 char *TypeClass::toChars() | |
4628 { | |
4629 return sym->toPrettyChars(); | |
4630 } | |
4631 | |
4632 Type *TypeClass::syntaxCopy() | |
4633 { | |
4634 return this; | |
4635 } | |
4636 | |
4637 Type *TypeClass::semantic(Loc loc, Scope *sc) | |
4638 { | |
4639 //printf("TypeClass::semantic(%s)\n", sym->toChars()); | |
4640 if (sym->scope) | |
4641 sym->semantic(sym->scope); | |
4642 return merge(); | |
4643 } | |
4644 | |
4645 d_uns64 TypeClass::size(Loc loc) | |
4646 { | |
4647 return PTRSIZE; | |
4648 } | |
4649 | |
4650 Dsymbol *TypeClass::toDsymbol(Scope *sc) | |
4651 { | |
4652 return sym; | |
4653 } | |
4654 | |
4655 void TypeClass::toDecoBuffer(OutBuffer *buf) | |
4656 { unsigned len; | |
4657 char *name; | |
4658 | |
4659 name = sym->mangle(); | |
4660 // if (name[0] == '_' && name[1] == 'D') | |
4661 // name += 2; | |
4662 //printf("TypeClass::toDecoBuffer('%s') = '%s'\n", toChars(), name); | |
4663 //len = strlen(name); | |
4664 //buf->printf("%c%d%s", mangleChar[ty], len, name); | |
4665 buf->printf("%c%s", mangleChar[ty], name); | |
4666 } | |
4667 | |
4668 void TypeClass::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
4669 { | |
4670 if (mod != this->mod) | |
4671 { toCBuffer3(buf, hgs, mod); | |
4672 return; | |
4673 } | |
4674 buf->writestring(sym->toChars()); | |
4675 } | |
4676 | |
4677 Expression *TypeClass::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
4678 { unsigned offset; | |
4679 | |
4680 Expression *b; | |
4681 VarDeclaration *v; | |
4682 Dsymbol *s; | |
4683 DotVarExp *de; | |
4684 Declaration *d; | |
4685 | |
4686 #if LOGDOTEXP | |
4687 printf("TypeClass::dotExp(e='%s', ident='%s')\n", e->toChars(), ident->toChars()); | |
4688 #endif | |
4689 | |
4690 if (e->op == TOKdotexp) | |
4691 { DotExp *de = (DotExp *)e; | |
4692 | |
4693 if (de->e1->op == TOKimport) | |
4694 { | |
4695 ScopeExp *se = (ScopeExp *)de->e1; | |
4696 | |
4697 s = se->sds->search(e->loc, ident, 0); | |
4698 e = de->e1; | |
4699 goto L1; | |
4700 } | |
4701 } | |
4702 | |
4703 if (ident == Id::tupleof) | |
4704 { | |
4705 /* Create a TupleExp | |
4706 */ | |
336 | 4707 e = e->semantic(sc); // do this before turning on noaccesscheck |
159 | 4708 Expressions *exps = new Expressions; |
4709 exps->reserve(sym->fields.dim); | |
4710 for (size_t i = 0; i < sym->fields.dim; i++) | |
4711 { VarDeclaration *v = (VarDeclaration *)sym->fields.data[i]; | |
4712 Expression *fe = new DotVarExp(e->loc, e, v); | |
4713 exps->push(fe); | |
4714 } | |
4715 e = new TupleExp(e->loc, exps); | |
336 | 4716 sc = sc->push(); |
4717 sc->noaccesscheck = 1; | |
159 | 4718 e = e->semantic(sc); |
336 | 4719 sc->pop(); |
159 | 4720 return e; |
4721 } | |
4722 | |
4723 s = sym->search(e->loc, ident, 0); | |
4724 L1: | |
4725 if (!s) | |
4726 { | |
4727 // See if it's a base class | |
4728 ClassDeclaration *cbase; | |
4729 for (cbase = sym->baseClass; cbase; cbase = cbase->baseClass) | |
4730 { | |
4731 if (cbase->ident->equals(ident)) | |
4732 { | |
4733 e = new DotTypeExp(0, e, cbase); | |
4734 return e; | |
4735 } | |
4736 } | |
4737 | |
4738 if (ident == Id::classinfo) | |
4739 { | |
4740 Type *t; | |
4741 | |
4742 assert(ClassDeclaration::classinfo); | |
4743 t = ClassDeclaration::classinfo->type; | |
4744 if (e->op == TOKtype || e->op == TOKdottype) | |
4745 { | |
4746 /* For type.classinfo, we know the classinfo | |
4747 * at compile time. | |
4748 */ | |
4749 if (!sym->vclassinfo) | |
4750 sym->vclassinfo = new ClassInfoDeclaration(sym); | |
4751 e = new VarExp(e->loc, sym->vclassinfo); | |
4752 e = e->addressOf(sc); | |
4753 e->type = t; // do this so we don't get redundant dereference | |
4754 } | |
4755 else | |
4756 { | |
4757 /* For class objects, the classinfo reference is the first | |
4758 * entry in the vtbl[] | |
4759 */ | |
4760 #if IN_LLVM | |
4761 | |
4762 Type* ct; | |
4763 if (sym->isInterfaceDeclaration()) { | |
4764 ct = t->pointerTo()->pointerTo()->pointerTo(); | |
4765 } | |
4766 else { | |
4767 ct = t->pointerTo()->pointerTo(); | |
4768 } | |
4769 | |
4770 e = e->castTo(sc, ct); | |
4771 e = new PtrExp(e->loc, e); | |
4772 e->type = ct->next; | |
4773 e = new PtrExp(e->loc, e); | |
4774 e->type = ct->next->next; | |
4775 | |
4776 if (sym->isInterfaceDeclaration()) | |
4777 { | |
4778 if (sym->isCOMinterface()) | |
4779 { /* COM interface vtbl[]s are different in that the | |
4780 * first entry is always pointer to QueryInterface(). | |
4781 * We can't get a .classinfo for it. | |
4782 */ | |
4783 error(e->loc, "no .classinfo for COM interface objects"); | |
4784 } | |
4785 /* For an interface, the first entry in the vtbl[] | |
4786 * is actually a pointer to an instance of struct Interface. | |
4787 * The first member of Interface is the .classinfo, | |
4788 * so add an extra pointer indirection. | |
4789 */ | |
4790 e = new PtrExp(e->loc, e); | |
4791 e->type = ct->next->next->next; | |
4792 } | |
336 | 4793 } |
159 | 4794 |
4795 #else | |
4796 | |
4797 e = new PtrExp(e->loc, e); | |
4798 e->type = t->pointerTo(); | |
4799 if (sym->isInterfaceDeclaration()) | |
4800 { | |
4801 if (sym->isCOMinterface()) | |
4802 { /* COM interface vtbl[]s are different in that the | |
4803 * first entry is always pointer to QueryInterface(). | |
4804 * We can't get a .classinfo for it. | |
4805 */ | |
4806 error(e->loc, "no .classinfo for COM interface objects"); | |
4807 } | |
4808 /* For an interface, the first entry in the vtbl[] | |
4809 * is actually a pointer to an instance of struct Interface. | |
4810 * The first member of Interface is the .classinfo, | |
4811 * so add an extra pointer indirection. | |
4812 */ | |
4813 e->type = e->type->pointerTo(); | |
4814 e = new PtrExp(e->loc, e); | |
4815 e->type = t->pointerTo(); | |
4816 } | |
4817 e = new PtrExp(e->loc, e, t); | |
336 | 4818 } |
4819 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
4820 #endif // !LDC |
336 | 4821 |
4822 return e; | |
4823 } | |
4824 | |
4825 if (ident == Id::__vptr) | |
4826 { /* The pointer to the vtbl[] | |
4827 * *cast(void***)e | |
4828 */ | |
4829 e = e->castTo(sc, tvoidptr->pointerTo()->pointerTo()); | |
4830 e = new PtrExp(e->loc, e); | |
4831 e = e->semantic(sc); | |
4832 return e; | |
4833 } | |
4834 | |
4835 if (ident == Id::__monitor) | |
4836 { /* The handle to the monitor (call it a void*) | |
4837 * *(cast(void**)e + 1) | |
4838 */ | |
4839 e = e->castTo(sc, tvoidptr->pointerTo()); | |
4840 e = new AddExp(e->loc, e, new IntegerExp(1)); | |
4841 e = new PtrExp(e->loc, e); | |
4842 e = e->semantic(sc); | |
159 | 4843 return e; |
4844 } | |
4845 | |
4846 if (ident == Id::typeinfo) | |
4847 { | |
4848 if (!global.params.useDeprecated) | |
4849 error(e->loc, ".typeinfo deprecated, use typeid(type)"); | |
4850 return getTypeInfo(sc); | |
4851 } | |
4852 if (ident == Id::outer && sym->vthis) | |
4853 { | |
4854 s = sym->vthis; | |
4855 } | |
4856 else | |
4857 { | |
4858 //return getProperty(e->loc, ident); | |
4859 return Type::dotExp(sc, e, ident); | |
4860 } | |
4861 } | |
336 | 4862 if (!s->isFuncDeclaration()) // because of overloading |
4863 s->checkDeprecated(e->loc, sc); | |
159 | 4864 s = s->toAlias(); |
4865 v = s->isVarDeclaration(); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
715
diff
changeset
|
4866 if (v && v->isConst() && v->type->toBasetype()->ty != Tsarray) |
159 | 4867 { ExpInitializer *ei = v->getExpInitializer(); |
4868 | |
4869 if (ei) | |
4870 { e = ei->exp->copy(); // need to copy it if it's a StringExp | |
4871 e = e->semantic(sc); | |
4872 return e; | |
4873 } | |
4874 } | |
4875 | |
4876 if (s->getType()) | |
4877 { | |
4878 // if (e->op == TOKtype) | |
4879 return new TypeExp(e->loc, s->getType()); | |
4880 // return new DotTypeExp(e->loc, e, s); | |
4881 } | |
4882 | |
4883 EnumMember *em = s->isEnumMember(); | |
4884 if (em) | |
4885 { | |
4886 assert(em->value); | |
4887 return em->value->copy(); | |
4888 } | |
4889 | |
4890 TemplateMixin *tm = s->isTemplateMixin(); | |
4891 if (tm) | |
4892 { Expression *de; | |
4893 | |
4894 de = new DotExp(e->loc, e, new ScopeExp(e->loc, tm)); | |
4895 de->type = e->type; | |
4896 return de; | |
4897 } | |
4898 | |
4899 TemplateDeclaration *td = s->isTemplateDeclaration(); | |
4900 if (td) | |
4901 { | |
4902 e = new DotTemplateExp(e->loc, e, td); | |
4903 e->semantic(sc); | |
4904 return e; | |
4905 } | |
4906 | |
4907 TemplateInstance *ti = s->isTemplateInstance(); | |
4908 if (ti) | |
4909 { if (!ti->semanticdone) | |
4910 ti->semantic(sc); | |
4911 s = ti->inst->toAlias(); | |
4912 if (!s->isTemplateInstance()) | |
4913 goto L1; | |
4914 Expression *de = new DotExp(e->loc, e, new ScopeExp(e->loc, ti)); | |
4915 de->type = e->type; | |
4916 return de; | |
4917 } | |
4918 | |
4919 d = s->isDeclaration(); | |
4920 if (!d) | |
4921 { | |
4922 e->error("%s.%s is not a declaration", e->toChars(), ident->toChars()); | |
4923 return new IntegerExp(e->loc, 1, Type::tint32); | |
4924 } | |
4925 | |
4926 if (e->op == TOKtype) | |
4927 { | |
4928 VarExp *ve; | |
4929 | |
4930 if (d->needThis() && (hasThis(sc) || !d->isFuncDeclaration())) | |
4931 { | |
4932 if (sc->func) | |
4933 { | |
4934 ClassDeclaration *thiscd; | |
4935 thiscd = sc->func->toParent()->isClassDeclaration(); | |
4936 | |
4937 if (thiscd) | |
4938 { | |
4939 ClassDeclaration *cd = e->type->isClassHandle(); | |
4940 | |
4941 if (cd == thiscd) | |
4942 { | |
4943 e = new ThisExp(e->loc); | |
4944 e = new DotTypeExp(e->loc, e, cd); | |
4945 de = new DotVarExp(e->loc, e, d); | |
4946 e = de->semantic(sc); | |
4947 return e; | |
4948 } | |
4949 else if ((!cd || !cd->isBaseOf(thiscd, NULL)) && | |
4950 !d->isFuncDeclaration()) | |
4951 e->error("'this' is required, but %s is not a base class of %s", e->type->toChars(), thiscd->toChars()); | |
4952 } | |
4953 } | |
4954 | |
4955 de = new DotVarExp(e->loc, new ThisExp(e->loc), d); | |
4956 e = de->semantic(sc); | |
4957 return e; | |
4958 } | |
4959 else if (d->isTupleDeclaration()) | |
4960 { | |
4961 e = new TupleExp(e->loc, d->isTupleDeclaration()); | |
4962 e = e->semantic(sc); | |
4963 return e; | |
4964 } | |
4965 else | |
4966 ve = new VarExp(e->loc, d); | |
4967 return ve; | |
4968 } | |
4969 | |
4970 if (d->isDataseg()) | |
4971 { | |
4972 // (e, d) | |
4973 VarExp *ve; | |
4974 | |
4975 accessCheck(e->loc, sc, e, d); | |
4976 ve = new VarExp(e->loc, d); | |
4977 e = new CommaExp(e->loc, e, ve); | |
4978 e->type = d->type; | |
4979 return e; | |
4980 } | |
4981 | |
4982 if (d->parent && d->toParent()->isModule()) | |
4983 { | |
4984 // (e, d) | |
4985 VarExp *ve; | |
4986 | |
4987 ve = new VarExp(e->loc, d); | |
4988 e = new CommaExp(e->loc, e, ve); | |
4989 e->type = d->type; | |
4990 return e; | |
4991 } | |
4992 | |
4993 de = new DotVarExp(e->loc, e, d); | |
4994 return de->semantic(sc); | |
4995 } | |
4996 | |
4997 ClassDeclaration *TypeClass::isClassHandle() | |
4998 { | |
4999 return sym; | |
5000 } | |
5001 | |
5002 int TypeClass::isauto() | |
5003 { | |
5004 return sym->isauto; | |
5005 } | |
5006 | |
5007 int TypeClass::isBaseOf(Type *t, int *poffset) | |
5008 { | |
5009 if (t->ty == Tclass) | |
5010 { ClassDeclaration *cd; | |
5011 | |
5012 cd = ((TypeClass *)t)->sym; | |
5013 if (sym->isBaseOf(cd, poffset)) | |
5014 return 1; | |
5015 } | |
5016 return 0; | |
5017 } | |
5018 | |
5019 MATCH TypeClass::implicitConvTo(Type *to) | |
5020 { | |
5021 //printf("TypeClass::implicitConvTo('%s')\n", to->toChars()); | |
5022 if (this == to) | |
5023 return MATCHexact; | |
5024 | |
5025 ClassDeclaration *cdto = to->isClassHandle(); | |
5026 if (cdto && cdto->isBaseOf(sym, NULL)) | |
5027 { //printf("is base\n"); | |
5028 return MATCHconvert; | |
5029 } | |
5030 | |
5031 if (global.params.Dversion == 1) | |
5032 { | |
5033 // Allow conversion to (void *) | |
5034 if (to->ty == Tpointer && to->next->ty == Tvoid) | |
5035 return MATCHconvert; | |
5036 } | |
5037 | |
5038 return MATCHnomatch; | |
5039 } | |
5040 | |
5041 Expression *TypeClass::defaultInit(Loc loc) | |
5042 { | |
5043 #if LOGDEFAULTINIT | |
5044 printf("TypeClass::defaultInit() '%s'\n", toChars()); | |
5045 #endif | |
5046 Expression *e; | |
5047 e = new NullExp(loc); | |
5048 e->type = this; | |
5049 return e; | |
5050 } | |
5051 | |
5052 int TypeClass::isZeroInit() | |
5053 { | |
5054 return 1; | |
5055 } | |
5056 | |
5057 int TypeClass::checkBoolean() | |
5058 { | |
5059 return TRUE; | |
5060 } | |
5061 | |
5062 int TypeClass::hasPointers() | |
5063 { | |
5064 return TRUE; | |
5065 } | |
5066 | |
5067 /***************************** TypeTuple *****************************/ | |
5068 | |
5069 TypeTuple::TypeTuple(Arguments *arguments) | |
5070 : Type(Ttuple, NULL) | |
5071 { | |
5072 //printf("TypeTuple(this = %p)\n", this); | |
5073 this->arguments = arguments; | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
5074 //printf("TypeTuple() %s\n", toChars()); |
159 | 5075 #ifdef DEBUG |
5076 if (arguments) | |
5077 { | |
5078 for (size_t i = 0; i < arguments->dim; i++) | |
5079 { | |
5080 Argument *arg = (Argument *)arguments->data[i]; | |
5081 assert(arg && arg->type); | |
5082 } | |
5083 } | |
5084 #endif | |
5085 } | |
5086 | |
5087 /**************** | |
5088 * Form TypeTuple from the types of the expressions. | |
5089 * Assume exps[] is already tuple expanded. | |
5090 */ | |
5091 | |
5092 TypeTuple::TypeTuple(Expressions *exps) | |
5093 : Type(Ttuple, NULL) | |
5094 { | |
5095 Arguments *arguments = new Arguments; | |
5096 if (exps) | |
5097 { | |
5098 arguments->setDim(exps->dim); | |
5099 for (size_t i = 0; i < exps->dim; i++) | |
5100 { Expression *e = (Expression *)exps->data[i]; | |
5101 if (e->type->ty == Ttuple) | |
5102 e->error("cannot form tuple of tuples"); | |
5103 Argument *arg = new Argument(STCin, e->type, NULL, NULL); | |
5104 arguments->data[i] = (void *)arg; | |
5105 } | |
5106 } | |
5107 this->arguments = arguments; | |
5108 } | |
5109 | |
5110 Type *TypeTuple::syntaxCopy() | |
5111 { | |
5112 Arguments *args = Argument::arraySyntaxCopy(arguments); | |
5113 Type *t = new TypeTuple(args); | |
5114 return t; | |
5115 } | |
5116 | |
5117 Type *TypeTuple::semantic(Loc loc, Scope *sc) | |
5118 { | |
5119 //printf("TypeTuple::semantic(this = %p)\n", this); | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
5120 //printf("TypeTuple::semantic() %s\n", toChars()); |
159 | 5121 if (!deco) |
5122 deco = merge()->deco; | |
5123 | |
5124 /* Don't return merge(), because a tuple with one type has the | |
5125 * same deco as that type. | |
5126 */ | |
5127 return this; | |
5128 } | |
5129 | |
5130 int TypeTuple::equals(Object *o) | |
5131 { Type *t; | |
5132 | |
5133 t = (Type *)o; | |
5134 //printf("TypeTuple::equals(%s, %s)\n", toChars(), t->toChars()); | |
5135 if (this == t) | |
5136 { | |
5137 return 1; | |
5138 } | |
5139 if (t->ty == Ttuple) | |
5140 { TypeTuple *tt = (TypeTuple *)t; | |
5141 | |
5142 if (arguments->dim == tt->arguments->dim) | |
5143 { | |
5144 for (size_t i = 0; i < tt->arguments->dim; i++) | |
5145 { Argument *arg1 = (Argument *)arguments->data[i]; | |
5146 Argument *arg2 = (Argument *)tt->arguments->data[i]; | |
5147 | |
5148 if (!arg1->type->equals(arg2->type)) | |
5149 return 0; | |
5150 } | |
5151 return 1; | |
5152 } | |
5153 } | |
5154 return 0; | |
5155 } | |
5156 | |
5157 Type *TypeTuple::reliesOnTident() | |
5158 { | |
5159 if (arguments) | |
5160 { | |
5161 for (size_t i = 0; i < arguments->dim; i++) | |
5162 { | |
5163 Argument *arg = (Argument *)arguments->data[i]; | |
5164 Type *t = arg->type->reliesOnTident(); | |
5165 if (t) | |
5166 return t; | |
5167 } | |
5168 } | |
5169 return NULL; | |
5170 } | |
5171 | |
5172 void TypeTuple::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
5173 { | |
5174 Argument::argsToCBuffer(buf, hgs, arguments, 0); | |
5175 } | |
5176 | |
5177 void TypeTuple::toDecoBuffer(OutBuffer *buf) | |
5178 { | |
5179 //printf("TypeTuple::toDecoBuffer() this = %p\n", this); | |
5180 OutBuffer buf2; | |
5181 Argument::argsToDecoBuffer(&buf2, arguments); | |
5182 unsigned len = buf2.offset; | |
5183 buf->printf("%c%d%.*s", mangleChar[ty], len, len, (char *)buf2.extractData()); | |
5184 } | |
5185 | |
5186 Expression *TypeTuple::getProperty(Loc loc, Identifier *ident) | |
5187 { Expression *e; | |
5188 | |
5189 #if LOGDOTEXP | |
5190 printf("TypeTuple::getProperty(type = '%s', ident = '%s')\n", toChars(), ident->toChars()); | |
5191 #endif | |
5192 if (ident == Id::length) | |
5193 { | |
5194 e = new IntegerExp(loc, arguments->dim, Type::tsize_t); | |
5195 } | |
5196 else | |
5197 { | |
5198 error(loc, "no property '%s' for tuple '%s'", ident->toChars(), toChars()); | |
5199 e = new IntegerExp(loc, 1, Type::tint32); | |
5200 } | |
5201 return e; | |
5202 } | |
5203 | |
5204 /***************************** TypeSlice *****************************/ | |
5205 | |
5206 /* This is so we can slice a TypeTuple */ | |
5207 | |
5208 TypeSlice::TypeSlice(Type *next, Expression *lwr, Expression *upr) | |
5209 : Type(Tslice, next) | |
5210 { | |
5211 //printf("TypeSlice[%s .. %s]\n", lwr->toChars(), upr->toChars()); | |
5212 this->lwr = lwr; | |
5213 this->upr = upr; | |
5214 } | |
5215 | |
5216 Type *TypeSlice::syntaxCopy() | |
5217 { | |
5218 Type *t = new TypeSlice(next->syntaxCopy(), lwr->syntaxCopy(), upr->syntaxCopy()); | |
5219 return t; | |
5220 } | |
5221 | |
5222 Type *TypeSlice::semantic(Loc loc, Scope *sc) | |
5223 { | |
5224 //printf("TypeSlice::semantic() %s\n", toChars()); | |
5225 next = next->semantic(loc, sc); | |
5226 //printf("next: %s\n", next->toChars()); | |
5227 | |
5228 Type *tbn = next->toBasetype(); | |
5229 if (tbn->ty != Ttuple) | |
5230 { error(loc, "can only slice tuple types, not %s", tbn->toChars()); | |
5231 return Type::terror; | |
5232 } | |
5233 TypeTuple *tt = (TypeTuple *)tbn; | |
5234 | |
5235 lwr = semanticLength(sc, tbn, lwr); | |
5236 lwr = lwr->optimize(WANTvalue); | |
5237 uinteger_t i1 = lwr->toUInteger(); | |
5238 | |
5239 upr = semanticLength(sc, tbn, upr); | |
5240 upr = upr->optimize(WANTvalue); | |
5241 uinteger_t i2 = upr->toUInteger(); | |
5242 | |
5243 if (!(i1 <= i2 && i2 <= tt->arguments->dim)) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
5244 { error(loc, "slice [%ju..%ju] is out of range of [0..%u]", i1, i2, tt->arguments->dim); |
159 | 5245 return Type::terror; |
5246 } | |
5247 | |
5248 Arguments *args = new Arguments; | |
5249 args->reserve(i2 - i1); | |
5250 for (size_t i = i1; i < i2; i++) | |
5251 { Argument *arg = (Argument *)tt->arguments->data[i]; | |
5252 args->push(arg); | |
5253 } | |
5254 | |
5255 return new TypeTuple(args); | |
5256 } | |
5257 | |
5258 void TypeSlice::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) | |
5259 { | |
5260 next->resolve(loc, sc, pe, pt, ps); | |
5261 if (*pe) | |
5262 { // It's really a slice expression | |
5263 Expression *e; | |
5264 e = new SliceExp(loc, *pe, lwr, upr); | |
5265 *pe = e; | |
5266 } | |
5267 else if (*ps) | |
5268 { Dsymbol *s = *ps; | |
5269 TupleDeclaration *td = s->isTupleDeclaration(); | |
5270 if (td) | |
5271 { | |
5272 /* It's a slice of a TupleDeclaration | |
5273 */ | |
5274 ScopeDsymbol *sym = new ArrayScopeSymbol(td); | |
5275 sym->parent = sc->scopesym; | |
5276 sc = sc->push(sym); | |
5277 | |
5278 lwr = lwr->semantic(sc); | |
5279 lwr = lwr->optimize(WANTvalue); | |
5280 uinteger_t i1 = lwr->toUInteger(); | |
5281 | |
5282 upr = upr->semantic(sc); | |
5283 upr = upr->optimize(WANTvalue); | |
5284 uinteger_t i2 = upr->toUInteger(); | |
5285 | |
5286 sc = sc->pop(); | |
5287 | |
5288 if (!(i1 <= i2 && i2 <= td->objects->dim)) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1051
diff
changeset
|
5289 { error(loc, "slice [%ju..%ju] is out of range of [0..%u]", i1, i2, td->objects->dim); |
159 | 5290 goto Ldefault; |
5291 } | |
5292 | |
5293 if (i1 == 0 && i2 == td->objects->dim) | |
5294 { | |
5295 *ps = td; | |
5296 return; | |
5297 } | |
5298 | |
5299 /* Create a new TupleDeclaration which | |
5300 * is a slice [i1..i2] out of the old one. | |
5301 */ | |
5302 Objects *objects = new Objects; | |
5303 objects->setDim(i2 - i1); | |
5304 for (size_t i = 0; i < objects->dim; i++) | |
5305 { | |
5306 objects->data[i] = td->objects->data[(size_t)i1 + i]; | |
5307 } | |
5308 | |
5309 TupleDeclaration *tds = new TupleDeclaration(loc, td->ident, objects); | |
5310 *ps = tds; | |
5311 } | |
5312 else | |
5313 goto Ldefault; | |
5314 } | |
5315 else | |
5316 { | |
5317 Ldefault: | |
5318 Type::resolve(loc, sc, pe, pt, ps); | |
5319 } | |
5320 } | |
5321 | |
5322 void TypeSlice::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
5323 { | |
5324 if (mod != this->mod) | |
5325 { toCBuffer3(buf, hgs, mod); | |
5326 return; | |
5327 } | |
5328 next->toCBuffer2(buf, hgs, this->mod); | |
5329 | |
5330 buf->printf("[%s .. ", lwr->toChars()); | |
5331 buf->printf("%s]", upr->toChars()); | |
5332 } | |
5333 | |
5334 /***************************** Argument *****************************/ | |
5335 | |
5336 Argument::Argument(unsigned storageClass, Type *type, Identifier *ident, Expression *defaultArg) | |
5337 { | |
5338 this->type = type; | |
5339 this->ident = ident; | |
5340 this->storageClass = storageClass; | |
5341 this->defaultArg = defaultArg; | |
5342 } | |
5343 | |
5344 Argument *Argument::syntaxCopy() | |
5345 { | |
5346 Argument *a = new Argument(storageClass, | |
5347 type ? type->syntaxCopy() : NULL, | |
5348 ident, | |
5349 defaultArg ? defaultArg->syntaxCopy() : NULL); | |
5350 return a; | |
5351 } | |
5352 | |
5353 Arguments *Argument::arraySyntaxCopy(Arguments *args) | |
5354 { Arguments *a = NULL; | |
5355 | |
5356 if (args) | |
5357 { | |
5358 a = new Arguments(); | |
5359 a->setDim(args->dim); | |
5360 for (size_t i = 0; i < a->dim; i++) | |
5361 { Argument *arg = (Argument *)args->data[i]; | |
5362 | |
5363 arg = arg->syntaxCopy(); | |
5364 a->data[i] = (void *)arg; | |
5365 } | |
5366 } | |
5367 return a; | |
5368 } | |
5369 | |
5370 char *Argument::argsTypesToChars(Arguments *args, int varargs) | |
5371 { OutBuffer *buf; | |
5372 | |
5373 buf = new OutBuffer(); | |
5374 | |
5375 buf->writeByte('('); | |
5376 if (args) | |
5377 { int i; | |
5378 OutBuffer argbuf; | |
5379 HdrGenState hgs; | |
5380 | |
5381 for (i = 0; i < args->dim; i++) | |
5382 { Argument *arg; | |
5383 | |
5384 if (i) | |
5385 buf->writeByte(','); | |
5386 arg = (Argument *)args->data[i]; | |
5387 argbuf.reset(); | |
5388 arg->type->toCBuffer2(&argbuf, &hgs, 0); | |
5389 buf->write(&argbuf); | |
5390 } | |
5391 if (varargs) | |
5392 { | |
5393 if (i && varargs == 1) | |
5394 buf->writeByte(','); | |
5395 buf->writestring("..."); | |
5396 } | |
5397 } | |
5398 buf->writeByte(')'); | |
5399 | |
5400 return buf->toChars(); | |
5401 } | |
5402 | |
5403 void Argument::argsToCBuffer(OutBuffer *buf, HdrGenState *hgs, Arguments *arguments, int varargs) | |
5404 { | |
5405 buf->writeByte('('); | |
5406 if (arguments) | |
5407 { int i; | |
5408 OutBuffer argbuf; | |
5409 | |
5410 for (i = 0; i < arguments->dim; i++) | |
5411 { Argument *arg; | |
5412 | |
5413 if (i) | |
5414 buf->writestring(", "); | |
5415 arg = (Argument *)arguments->data[i]; | |
5416 if (arg->storageClass & STCout) | |
5417 buf->writestring("out "); | |
5418 else if (arg->storageClass & STCref) | |
5419 buf->writestring((global.params.Dversion == 1) | |
5420 ? (char *)"inout " : (char *)"ref "); | |
5421 else if (arg->storageClass & STClazy) | |
5422 buf->writestring("lazy "); | |
5423 argbuf.reset(); | |
5424 arg->type->toCBuffer(&argbuf, arg->ident, hgs); | |
5425 if (arg->defaultArg) | |
5426 { | |
5427 argbuf.writestring(" = "); | |
5428 arg->defaultArg->toCBuffer(&argbuf, hgs); | |
5429 } | |
5430 buf->write(&argbuf); | |
5431 } | |
5432 if (varargs) | |
5433 { | |
5434 if (i && varargs == 1) | |
5435 buf->writeByte(','); | |
5436 buf->writestring("..."); | |
5437 } | |
5438 } | |
5439 buf->writeByte(')'); | |
5440 } | |
5441 | |
5442 | |
5443 void Argument::argsToDecoBuffer(OutBuffer *buf, Arguments *arguments) | |
5444 { | |
5445 //printf("Argument::argsToDecoBuffer()\n"); | |
5446 | |
5447 // Write argument types | |
5448 if (arguments) | |
5449 { | |
5450 size_t dim = Argument::dim(arguments); | |
5451 for (size_t i = 0; i < dim; i++) | |
5452 { | |
5453 Argument *arg = Argument::getNth(arguments, i); | |
5454 arg->toDecoBuffer(buf); | |
5455 } | |
5456 } | |
5457 } | |
5458 | |
5459 /**************************************************** | |
5460 * Determine if parameter is a lazy array of delegates. | |
5461 * If so, return the return type of those delegates. | |
5462 * If not, return NULL. | |
5463 */ | |
5464 | |
5465 Type *Argument::isLazyArray() | |
5466 { | |
5467 // if (inout == Lazy) | |
5468 { | |
5469 Type *tb = type->toBasetype(); | |
5470 if (tb->ty == Tsarray || tb->ty == Tarray) | |
5471 { | |
5472 Type *tel = tb->next->toBasetype(); | |
5473 if (tel->ty == Tdelegate) | |
5474 { | |
5475 TypeDelegate *td = (TypeDelegate *)tel; | |
5476 TypeFunction *tf = (TypeFunction *)td->next; | |
5477 | |
5478 if (!tf->varargs && Argument::dim(tf->parameters) == 0) | |
5479 { | |
5480 return tf->next; // return type of delegate | |
5481 } | |
5482 } | |
5483 } | |
5484 } | |
5485 return NULL; | |
5486 } | |
5487 | |
5488 void Argument::toDecoBuffer(OutBuffer *buf) | |
5489 { | |
5490 switch (storageClass & (STCin | STCout | STCref | STClazy)) | |
5491 { case 0: | |
5492 case STCin: | |
5493 break; | |
5494 case STCout: | |
5495 buf->writeByte('J'); | |
5496 break; | |
5497 case STCref: | |
5498 buf->writeByte('K'); | |
5499 break; | |
5500 case STClazy: | |
5501 buf->writeByte('L'); | |
5502 break; | |
5503 default: | |
5504 #ifdef DEBUG | |
5505 halt(); | |
5506 #endif | |
5507 assert(0); | |
5508 } | |
5509 type->toDecoBuffer(buf); | |
5510 } | |
5511 | |
5512 /*************************************** | |
5513 * Determine number of arguments, folding in tuples. | |
5514 */ | |
5515 | |
5516 size_t Argument::dim(Arguments *args) | |
5517 { | |
5518 size_t n = 0; | |
5519 if (args) | |
5520 { | |
5521 for (size_t i = 0; i < args->dim; i++) | |
5522 { Argument *arg = (Argument *)args->data[i]; | |
5523 Type *t = arg->type->toBasetype(); | |
5524 | |
5525 if (t->ty == Ttuple) | |
5526 { TypeTuple *tu = (TypeTuple *)t; | |
5527 n += dim(tu->arguments); | |
5528 } | |
5529 else | |
5530 n++; | |
5531 } | |
5532 } | |
5533 return n; | |
5534 } | |
5535 | |
5536 /*************************************** | |
5537 * Get nth Argument, folding in tuples. | |
5538 * Returns: | |
5539 * Argument* nth Argument | |
5540 * NULL not found, *pn gets incremented by the number | |
5541 * of Arguments | |
5542 */ | |
5543 | |
5544 Argument *Argument::getNth(Arguments *args, size_t nth, size_t *pn) | |
5545 { | |
5546 if (!args) | |
5547 return NULL; | |
5548 | |
5549 size_t n = 0; | |
5550 for (size_t i = 0; i < args->dim; i++) | |
5551 { Argument *arg = (Argument *)args->data[i]; | |
5552 Type *t = arg->type->toBasetype(); | |
5553 | |
5554 if (t->ty == Ttuple) | |
5555 { TypeTuple *tu = (TypeTuple *)t; | |
5556 arg = getNth(tu->arguments, nth - n, &n); | |
5557 if (arg) | |
5558 return arg; | |
5559 } | |
5560 else if (n == nth) | |
5561 return arg; | |
5562 else | |
5563 n++; | |
5564 } | |
5565 | |
5566 if (pn) | |
5567 *pn += n; | |
5568 return NULL; | |
5569 } |