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