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