Mercurial > projects > ldc
annotate dmd/mtype.c @ 557:aef77ae1d32c
Enable unwind code on darwin.
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Fri, 29 Aug 2008 14:16:18 +0200 |
parents | 337554fd34f1 |
children | fbb1a366cfbc |
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); | |
509
337554fd34f1
Fixed mini/missingti.d
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
489
diff
changeset
|
1679 if (next->ty != Tbit) |
337554fd34f1
Fixed mini/missingti.d
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
489
diff
changeset
|
1680 arguments->push(n->getTypeInfo(sc)); // LLVMDC, we don't support the getInternalTypeInfo |
337554fd34f1
Fixed mini/missingti.d
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
489
diff
changeset
|
1681 // optimization arbitrarily, not yet at least... |
159 | 1682 e = new CallExp(e->loc, ec, arguments); |
1683 e->type = next->arrayOf(); | |
1684 } | |
1685 else | |
1686 { | |
1687 e = Type::dotExp(sc, e, ident); | |
1688 } | |
1689 return e; | |
1690 } | |
1691 | |
1692 | |
1693 /***************************** TypeSArray *****************************/ | |
1694 | |
1695 TypeSArray::TypeSArray(Type *t, Expression *dim) | |
1696 : TypeArray(Tsarray, t) | |
1697 { | |
1698 //printf("TypeSArray(%s)\n", dim->toChars()); | |
1699 this->dim = dim; | |
1700 } | |
1701 | |
1702 Type *TypeSArray::syntaxCopy() | |
1703 { | |
1704 Type *t = next->syntaxCopy(); | |
1705 Expression *e = dim->syntaxCopy(); | |
1706 t = new TypeSArray(t, e); | |
1707 return t; | |
1708 } | |
1709 | |
1710 d_uns64 TypeSArray::size(Loc loc) | |
1711 { integer_t sz; | |
1712 | |
1713 if (!dim) | |
1714 return Type::size(loc); | |
1715 sz = dim->toInteger(); | |
1716 if (next->toBasetype()->ty == Tbit) // if array of bits | |
1717 { | |
1718 if (sz + 31 < sz) | |
1719 goto Loverflow; | |
1720 sz = ((sz + 31) & ~31) / 8; // size in bytes, rounded up to 32 bit dwords | |
1721 } | |
1722 else | |
1723 { integer_t n, n2; | |
1724 | |
1725 n = next->size(); | |
1726 n2 = n * sz; | |
1727 if (n && (n2 / n) != sz) | |
1728 goto Loverflow; | |
1729 sz = n2; | |
1730 } | |
1731 return sz; | |
1732 | |
1733 Loverflow: | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1734 error(loc, "index %lld overflow for static array", sz); |
159 | 1735 return 1; |
1736 } | |
1737 | |
1738 unsigned TypeSArray::alignsize() | |
1739 { | |
1740 return next->alignsize(); | |
1741 } | |
1742 | |
1743 /************************** | |
1744 * This evaluates exp while setting length to be the number | |
1745 * of elements in the tuple t. | |
1746 */ | |
1747 Expression *semanticLength(Scope *sc, Type *t, Expression *exp) | |
1748 { | |
1749 if (t->ty == Ttuple) | |
1750 { ScopeDsymbol *sym = new ArrayScopeSymbol((TypeTuple *)t); | |
1751 sym->parent = sc->scopesym; | |
1752 sc = sc->push(sym); | |
1753 | |
1754 exp = exp->semantic(sc); | |
1755 | |
1756 sc->pop(); | |
1757 } | |
1758 else | |
1759 exp = exp->semantic(sc); | |
1760 return exp; | |
1761 } | |
1762 | |
1763 Expression *semanticLength(Scope *sc, TupleDeclaration *s, Expression *exp) | |
1764 { | |
1765 ScopeDsymbol *sym = new ArrayScopeSymbol(s); | |
1766 sym->parent = sc->scopesym; | |
1767 sc = sc->push(sym); | |
1768 | |
1769 exp = exp->semantic(sc); | |
1770 | |
1771 sc->pop(); | |
1772 return exp; | |
1773 } | |
1774 | |
1775 void TypeSArray::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) | |
1776 { | |
1777 //printf("TypeSArray::resolve() %s\n", toChars()); | |
1778 next->resolve(loc, sc, pe, pt, ps); | |
1779 //printf("s = %p, e = %p, t = %p\n", *ps, *pe, *pt); | |
1780 if (*pe) | |
1781 { // It's really an index expression | |
1782 Expression *e; | |
1783 e = new IndexExp(loc, *pe, dim); | |
1784 *pe = e; | |
1785 } | |
1786 else if (*ps) | |
1787 { Dsymbol *s = *ps; | |
1788 TupleDeclaration *td = s->isTupleDeclaration(); | |
1789 if (td) | |
1790 { | |
1791 ScopeDsymbol *sym = new ArrayScopeSymbol(td); | |
1792 sym->parent = sc->scopesym; | |
1793 sc = sc->push(sym); | |
1794 | |
1795 dim = dim->semantic(sc); | |
1796 dim = dim->optimize(WANTvalue | WANTinterpret); | |
1797 uinteger_t d = dim->toUInteger(); | |
1798 | |
1799 sc = sc->pop(); | |
1800 | |
1801 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
|
1802 { error(loc, "tuple index %llu exceeds %u", d, td->objects->dim); |
159 | 1803 goto Ldefault; |
1804 } | |
1805 Object *o = (Object *)td->objects->data[(size_t)d]; | |
1806 if (o->dyncast() == DYNCAST_DSYMBOL) | |
1807 { | |
1808 *ps = (Dsymbol *)o; | |
1809 return; | |
1810 } | |
1811 if (o->dyncast() == DYNCAST_EXPRESSION) | |
1812 { | |
1813 *ps = NULL; | |
1814 *pe = (Expression *)o; | |
1815 return; | |
1816 } | |
1817 | |
1818 /* Create a new TupleDeclaration which | |
1819 * is a slice [d..d+1] out of the old one. | |
1820 * Do it this way because TemplateInstance::semanticTiargs() | |
1821 * can handle unresolved Objects this way. | |
1822 */ | |
1823 Objects *objects = new Objects; | |
1824 objects->setDim(1); | |
1825 objects->data[0] = o; | |
1826 | |
1827 TupleDeclaration *tds = new TupleDeclaration(loc, td->ident, objects); | |
1828 *ps = tds; | |
1829 } | |
1830 else | |
1831 goto Ldefault; | |
1832 } | |
1833 else | |
1834 { | |
1835 Ldefault: | |
1836 Type::resolve(loc, sc, pe, pt, ps); | |
1837 } | |
1838 } | |
1839 | |
1840 Type *TypeSArray::semantic(Loc loc, Scope *sc) | |
1841 { | |
1842 //printf("TypeSArray::semantic() %s\n", toChars()); | |
1843 | |
1844 Type *t; | |
1845 Expression *e; | |
1846 Dsymbol *s; | |
1847 next->resolve(loc, sc, &e, &t, &s); | |
1848 if (dim && s && s->isTupleDeclaration()) | |
1849 { TupleDeclaration *sd = s->isTupleDeclaration(); | |
1850 | |
1851 dim = semanticLength(sc, sd, dim); | |
1852 dim = dim->optimize(WANTvalue | WANTinterpret); | |
1853 uinteger_t d = dim->toUInteger(); | |
1854 | |
1855 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
|
1856 { error(loc, "tuple index %llu exceeds %u", d, sd->objects->dim); |
159 | 1857 return Type::terror; |
1858 } | |
1859 Object *o = (Object *)sd->objects->data[(size_t)d]; | |
1860 if (o->dyncast() != DYNCAST_TYPE) | |
1861 { error(loc, "%s is not a type", toChars()); | |
1862 return Type::terror; | |
1863 } | |
1864 t = (Type *)o; | |
1865 return t; | |
1866 } | |
1867 | |
1868 next = next->semantic(loc,sc); | |
1869 Type *tbn = next->toBasetype(); | |
1870 | |
1871 if (dim) | |
1872 { integer_t n, n2; | |
1873 | |
1874 dim = semanticLength(sc, tbn, dim); | |
1875 | |
1876 dim = dim->optimize(WANTvalue | WANTinterpret); | |
1877 if (sc->parameterSpecialization && dim->op == TOKvar && | |
1878 ((VarExp *)dim)->var->storage_class & STCtemplateparameter) | |
1879 { | |
1880 /* It could be a template parameter N which has no value yet: | |
1881 * template Foo(T : T[N], size_t N); | |
1882 */ | |
1883 return this; | |
1884 } | |
1885 integer_t d1 = dim->toInteger(); | |
1886 dim = dim->castTo(sc, tsize_t); | |
1887 dim = dim->optimize(WANTvalue); | |
1888 integer_t d2 = dim->toInteger(); | |
1889 | |
1890 if (d1 != d2) | |
1891 goto Loverflow; | |
1892 | |
1893 if (tbn->isintegral() || | |
1894 tbn->isfloating() || | |
1895 tbn->ty == Tpointer || | |
1896 tbn->ty == Tarray || | |
1897 tbn->ty == Tsarray || | |
1898 tbn->ty == Taarray || | |
1899 tbn->ty == Tclass) | |
1900 { | |
1901 /* Only do this for types that don't need to have semantic() | |
1902 * run on them for the size, since they may be forward referenced. | |
1903 */ | |
1904 n = tbn->size(loc); | |
1905 n2 = n * d2; | |
1906 if ((int)n2 < 0) | |
1907 goto Loverflow; | |
1908 if (n2 >= 0x1000000) // put a 'reasonable' limit on it | |
1909 goto Loverflow; | |
1910 if (n && n2 / n != d2) | |
1911 { | |
1912 Loverflow: | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1913 error(loc, "index %lld overflow for static array", d1); |
159 | 1914 dim = new IntegerExp(0, 1, tsize_t); |
1915 } | |
1916 } | |
1917 } | |
1918 switch (tbn->ty) | |
1919 { | |
1920 case Ttuple: | |
1921 { // Index the tuple to get the type | |
1922 assert(dim); | |
1923 TypeTuple *tt = (TypeTuple *)tbn; | |
1924 uinteger_t d = dim->toUInteger(); | |
1925 | |
1926 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
|
1927 { error(loc, "tuple index %llu exceeds %u", d, tt->arguments->dim); |
159 | 1928 return Type::terror; |
1929 } | |
1930 Argument *arg = (Argument *)tt->arguments->data[(size_t)d]; | |
1931 return arg->type; | |
1932 } | |
1933 case Tfunction: | |
1934 case Tnone: | |
1935 error(loc, "can't have array of %s", tbn->toChars()); | |
1936 tbn = next = tint32; | |
1937 break; | |
1938 } | |
1939 if (tbn->isauto()) | |
1940 error(loc, "cannot have array of auto %s", tbn->toChars()); | |
1941 return merge(); | |
1942 } | |
1943 | |
1944 void TypeSArray::toDecoBuffer(OutBuffer *buf) | |
1945 { | |
1946 buf->writeByte(mangleChar[ty]); | |
1947 if (dim) | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1948 buf->printf("%llu", dim->toInteger()); |
159 | 1949 if (next) |
1950 next->toDecoBuffer(buf); | |
1951 } | |
1952 | |
1953 void TypeSArray::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
1954 { | |
1955 if (mod != this->mod) | |
1956 { toCBuffer3(buf, hgs, mod); | |
1957 return; | |
1958 } | |
1959 next->toCBuffer2(buf, hgs, this->mod); | |
1960 buf->printf("[%s]", dim->toChars()); | |
1961 } | |
1962 | |
1963 Expression *TypeSArray::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
1964 { | |
1965 #if LOGDOTEXP | |
1966 printf("TypeSArray::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
1967 #endif | |
1968 if (ident == Id::length) | |
1969 { | |
1970 e = dim; | |
1971 } | |
1972 else if (ident == Id::ptr) | |
1973 { | |
1974 e = e->castTo(sc, next->pointerTo()); | |
1975 } | |
1976 else | |
1977 { | |
1978 e = TypeArray::dotExp(sc, e, ident); | |
1979 } | |
1980 return e; | |
1981 } | |
1982 | |
1983 int TypeSArray::isString() | |
1984 { | |
1985 TY nty = next->toBasetype()->ty; | |
1986 return nty == Tchar || nty == Twchar || nty == Tdchar; | |
1987 } | |
1988 | |
1989 unsigned TypeSArray::memalign(unsigned salign) | |
1990 { | |
1991 return next->memalign(salign); | |
1992 } | |
1993 | |
1994 MATCH TypeSArray::implicitConvTo(Type *to) | |
1995 { | |
1996 //printf("TypeSArray::implicitConvTo()\n"); | |
1997 | |
1998 // Allow implicit conversion of static array to pointer or dynamic array | |
1999 if ((IMPLICIT_ARRAY_TO_PTR && to->ty == Tpointer) && | |
2000 (to->next->ty == Tvoid || next->equals(to->next) | |
2001 /*|| to->next->isBaseOf(next)*/)) | |
2002 { | |
2003 return MATCHconvert; | |
2004 } | |
2005 if (to->ty == Tarray) | |
2006 { int offset = 0; | |
2007 | |
2008 if (next->equals(to->next) || | |
2009 (to->next->isBaseOf(next, &offset) && offset == 0) || | |
2010 to->next->ty == Tvoid) | |
2011 return MATCHconvert; | |
2012 } | |
2013 #if 0 | |
2014 if (to->ty == Tsarray) | |
2015 { | |
2016 TypeSArray *tsa = (TypeSArray *)to; | |
2017 | |
2018 if (next->equals(tsa->next) && dim->equals(tsa->dim)) | |
2019 { | |
2020 return MATCHconvert; | |
2021 } | |
2022 } | |
2023 #endif | |
2024 return Type::implicitConvTo(to); | |
2025 } | |
2026 | |
2027 Expression *TypeSArray::defaultInit(Loc loc) | |
2028 { | |
2029 #if LOGDEFAULTINIT | |
2030 printf("TypeSArray::defaultInit() '%s'\n", toChars()); | |
2031 #endif | |
2032 return next->defaultInit(loc); | |
2033 } | |
2034 | |
2035 int TypeSArray::isZeroInit() | |
2036 { | |
2037 return next->isZeroInit(); | |
2038 } | |
2039 | |
2040 | |
2041 Expression *TypeSArray::toExpression() | |
2042 { | |
2043 Expression *e = next->toExpression(); | |
2044 if (e) | |
2045 { Expressions *arguments = new Expressions(); | |
2046 arguments->push(dim); | |
2047 e = new ArrayExp(dim->loc, e, arguments); | |
2048 } | |
2049 return e; | |
2050 } | |
2051 | |
2052 int TypeSArray::hasPointers() | |
2053 { | |
2054 return next->hasPointers(); | |
2055 } | |
2056 | |
2057 /***************************** TypeDArray *****************************/ | |
2058 | |
2059 TypeDArray::TypeDArray(Type *t) | |
2060 : TypeArray(Tarray, t) | |
2061 { | |
2062 //printf("TypeDArray(t = %p)\n", t); | |
2063 } | |
2064 | |
2065 Type *TypeDArray::syntaxCopy() | |
2066 { | |
2067 Type *t = next->syntaxCopy(); | |
2068 if (t == next) | |
2069 t = this; | |
2070 else | |
2071 t = new TypeDArray(t); | |
2072 return t; | |
2073 } | |
2074 | |
2075 d_uns64 TypeDArray::size(Loc loc) | |
2076 { | |
2077 //printf("TypeDArray::size()\n"); | |
2078 return PTRSIZE * 2; | |
2079 } | |
2080 | |
2081 unsigned TypeDArray::alignsize() | |
2082 { | |
2083 // A DArray consists of two ptr-sized values, so align it on pointer size | |
2084 // boundary | |
2085 return PTRSIZE; | |
2086 } | |
2087 | |
2088 Type *TypeDArray::semantic(Loc loc, Scope *sc) | |
2089 { Type *tn = next; | |
2090 | |
2091 tn = next->semantic(loc,sc); | |
2092 Type *tbn = tn->toBasetype(); | |
2093 switch (tbn->ty) | |
2094 { | |
2095 case Tfunction: | |
2096 case Tnone: | |
2097 case Ttuple: | |
2098 error(loc, "can't have array of %s", tbn->toChars()); | |
2099 tn = next = tint32; | |
2100 break; | |
2101 } | |
2102 if (tn->isauto()) | |
2103 error(loc, "cannot have array of auto %s", tn->toChars()); | |
2104 if (next != tn) | |
2105 //deco = NULL; // redo | |
2106 return tn->arrayOf(); | |
2107 return merge(); | |
2108 } | |
2109 | |
2110 void TypeDArray::toDecoBuffer(OutBuffer *buf) | |
2111 { | |
2112 buf->writeByte(mangleChar[ty]); | |
2113 if (next) | |
2114 next->toDecoBuffer(buf); | |
2115 } | |
2116 | |
2117 void TypeDArray::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
2118 { | |
2119 if (mod != this->mod) | |
2120 { toCBuffer3(buf, hgs, mod); | |
2121 return; | |
2122 } | |
2123 next->toCBuffer2(buf, hgs, this->mod); | |
2124 buf->writestring("[]"); | |
2125 } | |
2126 | |
2127 Expression *TypeDArray::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
2128 { | |
2129 #if LOGDOTEXP | |
2130 printf("TypeDArray::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
2131 #endif | |
2132 if (ident == Id::length) | |
2133 { | |
2134 if (e->op == TOKstring) | |
2135 { StringExp *se = (StringExp *)e; | |
2136 | |
2137 return new IntegerExp(se->loc, se->len, Type::tindex); | |
2138 } | |
2139 e = new ArrayLengthExp(e->loc, e); | |
2140 e->type = Type::tsize_t; | |
2141 return e; | |
2142 } | |
2143 else if (ident == Id::ptr) | |
2144 { | |
2145 e = e->castTo(sc, next->pointerTo()); | |
2146 return e; | |
2147 } | |
2148 else | |
2149 { | |
2150 e = TypeArray::dotExp(sc, e, ident); | |
2151 } | |
2152 return e; | |
2153 } | |
2154 | |
2155 int TypeDArray::isString() | |
2156 { | |
2157 TY nty = next->toBasetype()->ty; | |
2158 return nty == Tchar || nty == Twchar || nty == Tdchar; | |
2159 } | |
2160 | |
2161 MATCH TypeDArray::implicitConvTo(Type *to) | |
2162 { | |
2163 //printf("TypeDArray::implicitConvTo()\n"); | |
2164 | |
2165 // Allow implicit conversion of array to pointer | |
2166 if (IMPLICIT_ARRAY_TO_PTR && | |
2167 to->ty == Tpointer && | |
2168 (to->next->ty == Tvoid || next->equals(to->next) /*|| to->next->isBaseOf(next)*/)) | |
2169 { | |
2170 return MATCHconvert; | |
2171 } | |
2172 | |
2173 if (to->ty == Tarray) | |
2174 { int offset = 0; | |
2175 | |
2176 if ((to->next->isBaseOf(next, &offset) && offset == 0) || | |
2177 to->next->ty == Tvoid) | |
2178 return MATCHconvert; | |
2179 } | |
2180 return Type::implicitConvTo(to); | |
2181 } | |
2182 | |
2183 Expression *TypeDArray::defaultInit(Loc loc) | |
2184 { | |
2185 #if LOGDEFAULTINIT | |
2186 printf("TypeDArray::defaultInit() '%s'\n", toChars()); | |
2187 #endif | |
2188 Expression *e; | |
2189 e = new NullExp(loc); | |
2190 e->type = this; | |
2191 return e; | |
2192 } | |
2193 | |
2194 int TypeDArray::isZeroInit() | |
2195 { | |
2196 return 1; | |
2197 } | |
2198 | |
2199 int TypeDArray::checkBoolean() | |
2200 { | |
2201 return TRUE; | |
2202 } | |
2203 | |
2204 int TypeDArray::hasPointers() | |
2205 { | |
2206 return TRUE; | |
2207 } | |
2208 | |
2209 /***************************** TypeAArray *****************************/ | |
2210 | |
2211 TypeAArray::TypeAArray(Type *t, Type *index) | |
2212 : TypeArray(Taarray, t) | |
2213 { | |
2214 this->index = index; | |
2215 this->key = NULL; | |
2216 } | |
2217 | |
2218 Type *TypeAArray::syntaxCopy() | |
2219 { | |
2220 Type *t = next->syntaxCopy(); | |
2221 Type *ti = index->syntaxCopy(); | |
2222 if (t == next && ti == index) | |
2223 t = this; | |
2224 else | |
2225 t = new TypeAArray(t, ti); | |
2226 return t; | |
2227 } | |
2228 | |
2229 d_uns64 TypeAArray::size(Loc loc) | |
2230 { | |
2231 return PTRSIZE /* * 2*/; | |
2232 } | |
2233 | |
2234 | |
2235 Type *TypeAArray::semantic(Loc loc, Scope *sc) | |
2236 { | |
2237 //printf("TypeAArray::semantic() %s index->ty = %d\n", toChars(), index->ty); | |
2238 | |
2239 // Deal with the case where we thought the index was a type, but | |
2240 // in reality it was an expression. | |
2241 if (index->ty == Tident || index->ty == Tinstance || index->ty == Tsarray) | |
2242 { | |
2243 Expression *e; | |
2244 Type *t; | |
2245 Dsymbol *s; | |
2246 | |
2247 index->resolve(loc, sc, &e, &t, &s); | |
2248 if (e) | |
2249 { // It was an expression - | |
2250 // Rewrite as a static array | |
2251 TypeSArray *tsa; | |
2252 | |
2253 tsa = new TypeSArray(next, e); | |
2254 return tsa->semantic(loc,sc); | |
2255 } | |
2256 else if (t) | |
2257 index = t; | |
2258 else | |
2259 index->error(loc, "index is not a type or an expression"); | |
2260 } | |
2261 else | |
2262 index = index->semantic(loc,sc); | |
2263 | |
2264 // Compute key type; the purpose of the key type is to | |
2265 // minimize the permutations of runtime library | |
2266 // routines as much as possible. | |
2267 key = index->toBasetype(); | |
2268 switch (key->ty) | |
2269 { | |
2270 #if 0 | |
2271 case Tint8: | |
2272 case Tuns8: | |
2273 case Tint16: | |
2274 case Tuns16: | |
2275 key = tint32; | |
2276 break; | |
2277 #endif | |
2278 | |
2279 case Tsarray: | |
2280 #if 0 | |
2281 // Convert to Tarray | |
2282 key = key->next->arrayOf(); | |
2283 #endif | |
2284 break; | |
2285 case Tbit: | |
2286 case Tbool: | |
2287 case Tfunction: | |
2288 case Tvoid: | |
2289 case Tnone: | |
2290 error(loc, "can't have associative array key of %s", key->toChars()); | |
2291 break; | |
2292 } | |
2293 next = next->semantic(loc,sc); | |
2294 switch (next->toBasetype()->ty) | |
2295 { | |
2296 case Tfunction: | |
2297 case Tnone: | |
2298 error(loc, "can't have associative array of %s", next->toChars()); | |
2299 break; | |
2300 } | |
2301 if (next->isauto()) | |
2302 error(loc, "cannot have array of auto %s", next->toChars()); | |
2303 | |
2304 return merge(); | |
2305 } | |
2306 | |
336 | 2307 void TypeAArray::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) |
2308 { | |
2309 //printf("TypeAArray::resolve() %s\n", toChars()); | |
2310 | |
2311 // Deal with the case where we thought the index was a type, but | |
2312 // in reality it was an expression. | |
2313 if (index->ty == Tident || index->ty == Tinstance || index->ty == Tsarray) | |
2314 { | |
2315 Expression *e; | |
2316 Type *t; | |
2317 Dsymbol *s; | |
2318 | |
2319 index->resolve(loc, sc, &e, &t, &s); | |
2320 if (e) | |
2321 { // It was an expression - | |
2322 // Rewrite as a static array | |
2323 | |
2324 TypeSArray *tsa = new TypeSArray(next, e); | |
2325 return tsa->resolve(loc, sc, pe, pt, ps); | |
2326 } | |
2327 else if (t) | |
2328 index = t; | |
2329 else | |
2330 index->error(loc, "index is not a type or an expression"); | |
2331 } | |
2332 Type::resolve(loc, sc, pe, pt, ps); | |
2333 } | |
2334 | |
2335 | |
159 | 2336 Expression *TypeAArray::dotExp(Scope *sc, Expression *e, Identifier *ident) |
2337 { | |
2338 #if LOGDOTEXP | |
2339 printf("TypeAArray::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
2340 #endif | |
2341 if (ident == Id::length) | |
2342 { | |
2343 Expression *ec; | |
2344 Expressions *arguments; | |
2345 | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
375
diff
changeset
|
2346 //LLVMDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2347 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
|
2348 if(!aaLen_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2349 Arguments* args = new Arguments; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2350 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
|
2351 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
|
2352 } |
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 ec = new VarExp(0, aaLen_fd); |
159 | 2355 arguments = new Expressions(); |
2356 arguments->push(e); | |
2357 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
|
2358 e->type = aaLen_fd->type->next; |
159 | 2359 } |
2360 else if (ident == Id::keys) | |
2361 { | |
2362 Expression *ec; | |
2363 Expressions *arguments; | |
2364 int size = key->size(e->loc); | |
2365 | |
2366 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
|
2367 //LLVMDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2368 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
|
2369 if(!aaKeys_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2370 Arguments* args = new Arguments; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2371 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
|
2372 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
|
2373 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
|
2374 } |
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 ec = new VarExp(0, aaKeys_fd); |
159 | 2377 arguments = new Expressions(); |
2378 arguments->push(e); | |
2379 arguments->push(new IntegerExp(0, size, Type::tsize_t)); | |
2380 e = new CallExp(e->loc, ec, arguments); | |
2381 e->type = index->arrayOf(); | |
2382 } | |
2383 else if (ident == Id::values) | |
2384 { | |
2385 Expression *ec; | |
2386 Expressions *arguments; | |
2387 | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
375
diff
changeset
|
2388 //LLVMDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2389 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
|
2390 if(!aaValues_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2391 Arguments* args = new Arguments; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2392 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
|
2393 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
|
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 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
|
2396 } |
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 ec = new VarExp(0, aaValues_fd); |
159 | 2399 arguments = new Expressions(); |
2400 arguments->push(e); | |
2401 size_t keysize = key->size(e->loc); | |
2402 keysize = (keysize + 4 - 1) & ~(4 - 1); | |
2403 arguments->push(new IntegerExp(0, keysize, Type::tsize_t)); | |
2404 arguments->push(new IntegerExp(0, next->size(e->loc), Type::tsize_t)); | |
2405 e = new CallExp(e->loc, ec, arguments); | |
2406 e->type = next->arrayOf(); | |
2407 } | |
2408 else if (ident == Id::rehash) | |
2409 { | |
2410 Expression *ec; | |
2411 Expressions *arguments; | |
2412 | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
375
diff
changeset
|
2413 //LLVMDC: Build arguments. |
389
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2414 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
|
2415 if(!aaRehash_fd) { |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2416 Arguments* args = new Arguments; |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2417 args->push(new Argument(STCin, Type::topaque->pointerTo(), NULL, NULL)); |
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::typeinfo->type, NULL, NULL)); |
722f5e90c39c
Made setup for runtime calls in dmd frontend allocate less.
Christian Kamm <kamm incasoftware de>
parents:
387
diff
changeset
|
2419 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
|
2420 } |
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 ec = new VarExp(0, aaRehash_fd); |
159 | 2423 arguments = new Expressions(); |
2424 arguments->push(e->addressOf(sc)); | |
2425 arguments->push(key->getInternalTypeInfo(sc)); | |
2426 e = new CallExp(e->loc, ec, arguments); | |
2427 e->type = this; | |
2428 } | |
2429 else | |
2430 { | |
2431 e = Type::dotExp(sc, e, ident); | |
2432 } | |
2433 return e; | |
2434 } | |
2435 | |
2436 void TypeAArray::toDecoBuffer(OutBuffer *buf) | |
2437 { | |
2438 buf->writeByte(mangleChar[ty]); | |
2439 index->toDecoBuffer(buf); | |
2440 next->toDecoBuffer(buf); | |
2441 } | |
2442 | |
2443 void TypeAArray::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
2444 { | |
2445 if (mod != this->mod) | |
2446 { toCBuffer3(buf, hgs, mod); | |
2447 return; | |
2448 } | |
2449 next->toCBuffer2(buf, hgs, this->mod); | |
2450 buf->writeByte('['); | |
2451 index->toCBuffer2(buf, hgs, 0); | |
2452 buf->writeByte(']'); | |
2453 } | |
2454 | |
2455 Expression *TypeAArray::defaultInit(Loc loc) | |
2456 { | |
2457 #if LOGDEFAULTINIT | |
2458 printf("TypeAArray::defaultInit() '%s'\n", toChars()); | |
2459 #endif | |
2460 Expression *e; | |
2461 e = new NullExp(loc); | |
2462 e->type = this; | |
2463 return e; | |
2464 } | |
2465 | |
211
f66219e0d530
[svn r227] Fixed: crash in lifetime.d when resizing array of AAs by .length assignment.
lindquist
parents:
162
diff
changeset
|
2466 int TypeAArray::isZeroInit() |
f66219e0d530
[svn r227] Fixed: crash in lifetime.d when resizing array of AAs by .length assignment.
lindquist
parents:
162
diff
changeset
|
2467 { |
f66219e0d530
[svn r227] Fixed: crash in lifetime.d when resizing array of AAs by .length assignment.
lindquist
parents:
162
diff
changeset
|
2468 return 1; |
f66219e0d530
[svn r227] Fixed: crash in lifetime.d when resizing array of AAs by .length assignment.
lindquist
parents:
162
diff
changeset
|
2469 } |
f66219e0d530
[svn r227] Fixed: crash in lifetime.d when resizing array of AAs by .length assignment.
lindquist
parents:
162
diff
changeset
|
2470 |
159 | 2471 int TypeAArray::checkBoolean() |
2472 { | |
2473 return TRUE; | |
2474 } | |
2475 | |
2476 int TypeAArray::hasPointers() | |
2477 { | |
2478 return TRUE; | |
2479 } | |
2480 | |
2481 /***************************** TypePointer *****************************/ | |
2482 | |
2483 TypePointer::TypePointer(Type *t) | |
2484 : Type(Tpointer, t) | |
2485 { | |
2486 } | |
2487 | |
2488 Type *TypePointer::syntaxCopy() | |
2489 { | |
2490 Type *t = next->syntaxCopy(); | |
2491 if (t == next) | |
2492 t = this; | |
2493 else | |
2494 t = new TypePointer(t); | |
2495 return t; | |
2496 } | |
2497 | |
2498 Type *TypePointer::semantic(Loc loc, Scope *sc) | |
2499 { | |
2500 //printf("TypePointer::semantic()\n"); | |
2501 Type *n = next->semantic(loc, sc); | |
2502 switch (n->toBasetype()->ty) | |
2503 { | |
2504 case Ttuple: | |
2505 error(loc, "can't have pointer to %s", n->toChars()); | |
2506 n = tint32; | |
2507 break; | |
2508 } | |
2509 if (n != next) | |
2510 deco = NULL; | |
2511 next = n; | |
2512 return merge(); | |
2513 } | |
2514 | |
2515 | |
2516 d_uns64 TypePointer::size(Loc loc) | |
2517 { | |
2518 return PTRSIZE; | |
2519 } | |
2520 | |
2521 void TypePointer::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
2522 { | |
2523 //printf("TypePointer::toCBuffer2() next = %d\n", next->ty); | |
2524 if (mod != this->mod) | |
2525 { toCBuffer3(buf, hgs, mod); | |
2526 return; | |
2527 } | |
2528 next->toCBuffer2(buf, hgs, this->mod); | |
2529 if (next->ty != Tfunction) | |
2530 buf->writeByte('*'); | |
2531 } | |
2532 | |
2533 MATCH TypePointer::implicitConvTo(Type *to) | |
2534 { | |
2535 //printf("TypePointer::implicitConvTo()\n"); | |
2536 | |
2537 if (this == to) | |
2538 return MATCHexact; | |
2539 if (to->ty == Tpointer && to->next) | |
2540 { | |
2541 if (to->next->ty == Tvoid) | |
2542 return MATCHconvert; | |
2543 | |
2544 #if 0 | |
2545 if (to->next->isBaseOf(next)) | |
2546 return MATCHconvert; | |
2547 #endif | |
2548 | |
2549 if (next->ty == Tfunction && to->next->ty == Tfunction) | |
2550 { TypeFunction *tf; | |
2551 TypeFunction *tfto; | |
2552 | |
2553 tf = (TypeFunction *)(next); | |
2554 tfto = (TypeFunction *)(to->next); | |
2555 return tfto->equals(tf) ? MATCHexact : MATCHnomatch; | |
2556 } | |
2557 } | |
2558 // if (to->ty == Tvoid) | |
2559 // return MATCHconvert; | |
2560 return MATCHnomatch; | |
2561 } | |
2562 | |
2563 int TypePointer::isscalar() | |
2564 { | |
2565 return TRUE; | |
2566 } | |
2567 | |
2568 Expression *TypePointer::defaultInit(Loc loc) | |
2569 { | |
2570 #if LOGDEFAULTINIT | |
2571 printf("TypePointer::defaultInit() '%s'\n", toChars()); | |
2572 #endif | |
2573 Expression *e; | |
2574 e = new NullExp(loc); | |
2575 e->type = this; | |
2576 return e; | |
2577 } | |
2578 | |
2579 int TypePointer::isZeroInit() | |
2580 { | |
2581 return 1; | |
2582 } | |
2583 | |
2584 int TypePointer::hasPointers() | |
2585 { | |
2586 return TRUE; | |
2587 } | |
2588 | |
2589 | |
2590 /***************************** TypeReference *****************************/ | |
2591 | |
2592 TypeReference::TypeReference(Type *t) | |
2593 : Type(Treference, t) | |
2594 { | |
2595 if (t->ty == Tbit) | |
2596 error(0,"cannot make reference to a bit"); | |
2597 // BUG: what about references to static arrays? | |
2598 } | |
2599 | |
2600 Type *TypeReference::syntaxCopy() | |
2601 { | |
2602 Type *t = next->syntaxCopy(); | |
2603 if (t == next) | |
2604 t = this; | |
2605 else | |
2606 t = new TypeReference(t); | |
2607 return t; | |
2608 } | |
2609 | |
2610 d_uns64 TypeReference::size(Loc loc) | |
2611 { | |
2612 return PTRSIZE; | |
2613 } | |
2614 | |
2615 void TypeReference::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
2616 { | |
2617 if (mod != this->mod) | |
2618 { toCBuffer3(buf, hgs, mod); | |
2619 return; | |
2620 } | |
2621 next->toCBuffer2(buf, hgs, this->mod); | |
2622 buf->writeByte('&'); | |
2623 } | |
2624 | |
2625 Expression *TypeReference::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
2626 { | |
2627 #if LOGDOTEXP | |
2628 printf("TypeReference::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
2629 #endif | |
2630 | |
2631 // References just forward things along | |
2632 return next->dotExp(sc, e, ident); | |
2633 } | |
2634 | |
2635 Expression *TypeReference::defaultInit(Loc loc) | |
2636 { | |
2637 #if LOGDEFAULTINIT | |
2638 printf("TypeReference::defaultInit() '%s'\n", toChars()); | |
2639 #endif | |
2640 Expression *e; | |
2641 e = new NullExp(loc); | |
2642 e->type = this; | |
2643 return e; | |
2644 } | |
2645 | |
2646 int TypeReference::isZeroInit() | |
2647 { | |
2648 return 1; | |
2649 } | |
2650 | |
2651 | |
2652 /***************************** TypeFunction *****************************/ | |
2653 | |
2654 TypeFunction::TypeFunction(Arguments *parameters, Type *treturn, int varargs, enum LINK linkage) | |
2655 : Type(Tfunction, treturn) | |
2656 { | |
2657 //if (!treturn) *(char*)0=0; | |
2658 // assert(treturn); | |
2659 this->parameters = parameters; | |
2660 this->varargs = varargs; | |
2661 this->linkage = linkage; | |
2662 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
|
2663 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
|
2664 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
|
2665 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
|
2666 this->retAttrs = 0; |
159 | 2667 } |
2668 | |
2669 Type *TypeFunction::syntaxCopy() | |
2670 { | |
2671 Type *treturn = next ? next->syntaxCopy() : NULL; | |
2672 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
|
2673 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
|
2674 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
|
2675 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
|
2676 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
|
2677 t->retAttrs = retAttrs; |
159 | 2678 return t; |
2679 } | |
2680 | |
2681 /******************************* | |
2682 * Returns: | |
2683 * 0 types are distinct | |
2684 * 1 this is covariant with t | |
2685 * 2 arguments match as far as overloading goes, | |
2686 * but types are not covariant | |
2687 * 3 cannot determine covariance because of forward references | |
2688 */ | |
2689 | |
2690 int Type::covariant(Type *t) | |
2691 { | |
2692 #if 0 | |
2693 printf("Type::covariant(t = %s) %s\n", t->toChars(), toChars()); | |
2694 printf("deco = %p, %p\n", deco, t->deco); | |
2695 printf("ty = %d\n", next->ty); | |
2696 #endif | |
2697 | |
2698 int inoutmismatch = 0; | |
2699 | |
2700 if (equals(t)) | |
2701 goto Lcovariant; | |
2702 if (ty != Tfunction || t->ty != Tfunction) | |
2703 goto Ldistinct; | |
2704 | |
2705 { | |
2706 TypeFunction *t1 = (TypeFunction *)this; | |
2707 TypeFunction *t2 = (TypeFunction *)t; | |
2708 | |
2709 if (t1->varargs != t2->varargs) | |
2710 goto Ldistinct; | |
2711 | |
2712 if (t1->parameters && t2->parameters) | |
2713 { | |
2714 size_t dim = Argument::dim(t1->parameters); | |
2715 if (dim != Argument::dim(t2->parameters)) | |
2716 goto Ldistinct; | |
2717 | |
2718 for (size_t i = 0; i < dim; i++) | |
2719 { Argument *arg1 = Argument::getNth(t1->parameters, i); | |
2720 Argument *arg2 = Argument::getNth(t2->parameters, i); | |
2721 | |
2722 if (!arg1->type->equals(arg2->type)) | |
2723 goto Ldistinct; | |
2724 if (arg1->storageClass != arg2->storageClass) | |
2725 inoutmismatch = 1; | |
2726 } | |
2727 } | |
2728 else if (t1->parameters != t2->parameters) | |
2729 goto Ldistinct; | |
2730 | |
2731 // The argument lists match | |
2732 if (inoutmismatch) | |
2733 goto Lnotcovariant; | |
2734 if (t1->linkage != t2->linkage) | |
2735 goto Lnotcovariant; | |
2736 | |
2737 Type *t1n = t1->next; | |
2738 Type *t2n = t2->next; | |
2739 | |
2740 if (t1n->equals(t2n)) | |
2741 goto Lcovariant; | |
2742 if (t1n->ty != Tclass || t2n->ty != Tclass) | |
2743 goto Lnotcovariant; | |
2744 | |
2745 // If t1n is forward referenced: | |
2746 ClassDeclaration *cd = ((TypeClass *)t1n)->sym; | |
2747 if (!cd->baseClass && cd->baseclasses.dim && !cd->isInterfaceDeclaration()) | |
2748 { | |
2749 return 3; | |
2750 } | |
2751 | |
2752 if (t1n->implicitConvTo(t2n)) | |
2753 goto Lcovariant; | |
2754 goto Lnotcovariant; | |
2755 } | |
2756 | |
2757 Lcovariant: | |
2758 //printf("\tcovaraint: 1\n"); | |
2759 return 1; | |
2760 | |
2761 Ldistinct: | |
2762 //printf("\tcovaraint: 0\n"); | |
2763 return 0; | |
2764 | |
2765 Lnotcovariant: | |
2766 //printf("\tcovaraint: 2\n"); | |
2767 return 2; | |
2768 } | |
2769 | |
2770 void TypeFunction::toDecoBuffer(OutBuffer *buf) | |
2771 { unsigned char mc; | |
2772 | |
2773 //printf("TypeFunction::toDecoBuffer() this = %p %s\n", this, toChars()); | |
2774 //static int nest; if (++nest == 50) *(char*)0=0; | |
2775 if (inuse) | |
2776 { inuse = 2; // flag error to caller | |
2777 return; | |
2778 } | |
2779 inuse++; | |
2780 switch (linkage) | |
2781 { | |
2782 case LINKd: mc = 'F'; break; | |
2783 case LINKc: mc = 'U'; break; | |
2784 case LINKwindows: mc = 'W'; break; | |
2785 case LINKpascal: mc = 'V'; break; | |
2786 case LINKcpp: mc = 'R'; break; | |
2787 default: | |
2788 assert(0); | |
2789 } | |
2790 buf->writeByte(mc); | |
2791 // Write argument types | |
2792 Argument::argsToDecoBuffer(buf, parameters); | |
2793 //if (buf->data[buf->offset - 1] == '@') halt(); | |
2794 buf->writeByte('Z' - varargs); // mark end of arg list | |
2795 next->toDecoBuffer(buf); | |
2796 inuse--; | |
2797 } | |
2798 | |
2799 void TypeFunction::toCBuffer(OutBuffer *buf, Identifier *ident, HdrGenState *hgs) | |
2800 { | |
2801 char *p = NULL; | |
2802 | |
2803 if (inuse) | |
2804 { inuse = 2; // flag error to caller | |
2805 return; | |
2806 } | |
2807 inuse++; | |
2808 if (next && (!ident || ident->toHChars2() == ident->toChars())) | |
2809 next->toCBuffer2(buf, hgs, 0); | |
2810 if (hgs->ddoc != 1) | |
2811 { | |
2812 switch (linkage) | |
2813 { | |
2814 case LINKd: p = NULL; break; | |
2815 case LINKc: p = "C "; break; | |
2816 case LINKwindows: p = "Windows "; break; | |
2817 case LINKpascal: p = "Pascal "; break; | |
2818 case LINKcpp: p = "C++ "; break; | |
2819 default: | |
2820 assert(0); | |
2821 } | |
2822 } | |
2823 | |
2824 if (!hgs->hdrgen && p) | |
2825 buf->writestring(p); | |
2826 if (ident) | |
2827 { buf->writeByte(' '); | |
2828 buf->writestring(ident->toHChars2()); | |
2829 } | |
2830 Argument::argsToCBuffer(buf, hgs, parameters, varargs); | |
2831 inuse--; | |
2832 } | |
2833 | |
2834 void TypeFunction::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
2835 { | |
2836 char *p = NULL; | |
2837 | |
2838 if (inuse) | |
2839 { inuse = 2; // flag error to caller | |
2840 return; | |
2841 } | |
2842 inuse++; | |
2843 if (next) | |
2844 next->toCBuffer2(buf, hgs, 0); | |
2845 if (hgs->ddoc != 1) | |
2846 { | |
2847 switch (linkage) | |
2848 { | |
2849 case LINKd: p = NULL; break; | |
2850 case LINKc: p = "C "; break; | |
2851 case LINKwindows: p = "Windows "; break; | |
2852 case LINKpascal: p = "Pascal "; break; | |
2853 case LINKcpp: p = "C++ "; break; | |
2854 default: | |
2855 assert(0); | |
2856 } | |
2857 } | |
2858 | |
2859 if (!hgs->hdrgen && p) | |
2860 buf->writestring(p); | |
2861 buf->writestring(" function"); | |
2862 Argument::argsToCBuffer(buf, hgs, parameters, varargs); | |
2863 inuse--; | |
2864 } | |
2865 | |
2866 Type *TypeFunction::semantic(Loc loc, Scope *sc) | |
2867 { | |
2868 if (deco) // if semantic() already run | |
2869 { | |
2870 //printf("already done\n"); | |
2871 return this; | |
2872 } | |
2873 //printf("TypeFunction::semantic() this = %p\n", this); | |
2874 | |
2875 TypeFunction *tf = (TypeFunction *)mem.malloc(sizeof(TypeFunction)); | |
2876 memcpy(tf, this, sizeof(TypeFunction)); | |
2877 if (parameters) | |
2878 { tf->parameters = (Arguments *)parameters->copy(); | |
2879 for (size_t i = 0; i < parameters->dim; i++) | |
2880 { Argument *arg = (Argument *)parameters->data[i]; | |
2881 Argument *cpy = (Argument *)mem.malloc(sizeof(Argument)); | |
2882 memcpy(cpy, arg, sizeof(Argument)); | |
2883 tf->parameters->data[i] = (void *)cpy; | |
2884 } | |
2885 } | |
2886 | |
2887 tf->linkage = sc->linkage; | |
2888 if (!tf->next) | |
2889 { | |
2890 assert(global.errors); | |
2891 tf->next = tvoid; | |
2892 } | |
2893 tf->next = tf->next->semantic(loc,sc); | |
2894 if (tf->next->toBasetype()->ty == Tsarray) | |
2895 { error(loc, "functions cannot return static array %s", tf->next->toChars()); | |
2896 tf->next = Type::terror; | |
2897 } | |
2898 if (tf->next->toBasetype()->ty == Tfunction) | |
2899 { error(loc, "functions cannot return a function"); | |
2900 tf->next = Type::terror; | |
2901 } | |
2902 if (tf->next->toBasetype()->ty == Ttuple) | |
2903 { error(loc, "functions cannot return a tuple"); | |
2904 tf->next = Type::terror; | |
2905 } | |
2906 if (tf->next->isauto() && !(sc->flags & SCOPEctor)) | |
2907 error(loc, "functions cannot return auto %s", tf->next->toChars()); | |
2908 | |
2909 if (tf->parameters) | |
2910 { size_t dim = Argument::dim(tf->parameters); | |
2911 | |
2912 for (size_t i = 0; i < dim; i++) | |
2913 { Argument *arg = Argument::getNth(tf->parameters, i); | |
2914 Type *t; | |
2915 | |
2916 tf->inuse++; | |
2917 arg->type = arg->type->semantic(loc,sc); | |
2918 if (tf->inuse == 1) tf->inuse--; | |
2919 t = arg->type->toBasetype(); | |
2920 | |
2921 if (arg->storageClass & (STCout | STCref | STClazy)) | |
2922 { | |
2923 if (t->ty == Tsarray) | |
2924 error(loc, "cannot have out or ref parameter of type %s", t->toChars()); | |
2925 } | |
2926 if (!(arg->storageClass & STClazy) && t->ty == Tvoid) | |
2927 error(loc, "cannot have parameter of type %s", arg->type->toChars()); | |
2928 | |
2929 if (arg->defaultArg) | |
2930 { | |
2931 arg->defaultArg = arg->defaultArg->semantic(sc); | |
2932 arg->defaultArg = resolveProperties(sc, arg->defaultArg); | |
2933 arg->defaultArg = arg->defaultArg->implicitCastTo(sc, arg->type); | |
2934 } | |
2935 | |
2936 /* If arg turns out to be a tuple, the number of parameters may | |
2937 * change. | |
2938 */ | |
2939 if (t->ty == Ttuple) | |
2940 { dim = Argument::dim(tf->parameters); | |
2941 i--; | |
2942 } | |
2943 } | |
2944 } | |
2945 tf->deco = tf->merge()->deco; | |
2946 | |
2947 if (tf->inuse) | |
2948 { error(loc, "recursive type"); | |
2949 tf->inuse = 0; | |
2950 return terror; | |
2951 } | |
2952 | |
2953 if (tf->varargs == 1 && tf->linkage != LINKd && Argument::dim(tf->parameters) == 0) | |
2954 error(loc, "variadic functions with non-D linkage must have at least one parameter"); | |
2955 | |
2956 /* Don't return merge(), because arg identifiers and default args | |
2957 * can be different | |
2958 * even though the types match | |
2959 */ | |
2960 return tf; | |
2961 } | |
2962 | |
2963 /******************************** | |
2964 * 'args' are being matched to function 'this' | |
2965 * Determine match level. | |
2966 * Returns: | |
2967 * MATCHxxxx | |
2968 */ | |
2969 | |
2970 int TypeFunction::callMatch(Expressions *args) | |
2971 { | |
2972 //printf("TypeFunction::callMatch()\n"); | |
2973 int match = MATCHexact; // assume exact match | |
2974 | |
2975 size_t nparams = Argument::dim(parameters); | |
2976 size_t nargs = args ? args->dim : 0; | |
2977 if (nparams == nargs) | |
2978 ; | |
2979 else if (nargs > nparams) | |
2980 { | |
2981 if (varargs == 0) | |
2982 goto Nomatch; // too many args; no match | |
2983 match = MATCHconvert; // match ... with a "conversion" match level | |
2984 } | |
2985 | |
2986 for (size_t u = 0; u < nparams; u++) | |
2987 { int m; | |
2988 Expression *arg; | |
2989 | |
2990 // BUG: what about out and ref? | |
2991 | |
2992 Argument *p = Argument::getNth(parameters, u); | |
2993 assert(p); | |
2994 if (u >= nargs) | |
2995 { | |
2996 if (p->defaultArg) | |
2997 continue; | |
2998 if (varargs == 2 && u + 1 == nparams) | |
2999 goto L1; | |
3000 goto Nomatch; // not enough arguments | |
3001 } | |
3002 arg = (Expression *)args->data[u]; | |
3003 assert(arg); | |
3004 if (p->storageClass & STClazy && p->type->ty == Tvoid && arg->type->ty != Tvoid) | |
3005 m = MATCHconvert; | |
3006 else | |
3007 m = arg->implicitConvTo(p->type); | |
3008 //printf("\tm = %d\n", m); | |
3009 if (m == MATCHnomatch) // if no match | |
3010 { | |
3011 L1: | |
3012 if (varargs == 2 && u + 1 == nparams) // if last varargs param | |
3013 { Type *tb = p->type->toBasetype(); | |
3014 TypeSArray *tsa; | |
3015 integer_t sz; | |
3016 | |
3017 switch (tb->ty) | |
3018 { | |
3019 case Tsarray: | |
3020 tsa = (TypeSArray *)tb; | |
3021 sz = tsa->dim->toInteger(); | |
3022 if (sz != nargs - u) | |
3023 goto Nomatch; | |
3024 case Tarray: | |
3025 for (; u < nargs; u++) | |
3026 { | |
3027 arg = (Expression *)args->data[u]; | |
3028 assert(arg); | |
3029 #if 1 | |
3030 /* If lazy array of delegates, | |
3031 * convert arg(s) to delegate(s) | |
3032 */ | |
3033 Type *tret = p->isLazyArray(); | |
3034 if (tret) | |
3035 { | |
3036 if (tb->next->equals(arg->type)) | |
3037 { m = MATCHexact; | |
3038 } | |
3039 else | |
3040 { | |
3041 m = arg->implicitConvTo(tret); | |
3042 if (m == MATCHnomatch) | |
3043 { | |
3044 if (tret->toBasetype()->ty == Tvoid) | |
3045 m = MATCHconvert; | |
3046 } | |
3047 } | |
3048 } | |
3049 else | |
3050 m = arg->implicitConvTo(tb->next); | |
3051 #else | |
3052 m = arg->implicitConvTo(tb->next); | |
3053 #endif | |
3054 if (m == 0) | |
3055 goto Nomatch; | |
3056 if (m < match) | |
3057 match = m; | |
3058 } | |
3059 goto Ldone; | |
3060 | |
3061 case Tclass: | |
3062 // Should see if there's a constructor match? | |
3063 // Or just leave it ambiguous? | |
3064 goto Ldone; | |
3065 | |
3066 default: | |
3067 goto Nomatch; | |
3068 } | |
3069 } | |
3070 goto Nomatch; | |
3071 } | |
3072 if (m < match) | |
3073 match = m; // pick worst match | |
3074 } | |
3075 | |
3076 Ldone: | |
3077 //printf("match = %d\n", match); | |
3078 return match; | |
3079 | |
3080 Nomatch: | |
3081 //printf("no match\n"); | |
3082 return MATCHnomatch; | |
3083 } | |
3084 | |
3085 Type *TypeFunction::reliesOnTident() | |
3086 { | |
3087 if (parameters) | |
3088 { | |
3089 for (size_t i = 0; i < parameters->dim; i++) | |
3090 { Argument *arg = (Argument *)parameters->data[i]; | |
3091 Type *t = arg->type->reliesOnTident(); | |
3092 if (t) | |
3093 return t; | |
3094 } | |
3095 } | |
3096 return next->reliesOnTident(); | |
3097 } | |
3098 | |
3099 /***************************** TypeDelegate *****************************/ | |
3100 | |
3101 TypeDelegate::TypeDelegate(Type *t) | |
3102 : Type(Tfunction, t) | |
3103 { | |
3104 ty = Tdelegate; | |
3105 } | |
3106 | |
3107 Type *TypeDelegate::syntaxCopy() | |
3108 { | |
3109 Type *t = next->syntaxCopy(); | |
3110 if (t == next) | |
3111 t = this; | |
3112 else | |
3113 t = new TypeDelegate(t); | |
3114 return t; | |
3115 } | |
3116 | |
3117 Type *TypeDelegate::semantic(Loc loc, Scope *sc) | |
3118 { | |
3119 if (deco) // if semantic() already run | |
3120 { | |
3121 //printf("already done\n"); | |
3122 return this; | |
3123 } | |
3124 next = next->semantic(loc,sc); | |
3125 return merge(); | |
3126 } | |
3127 | |
3128 d_uns64 TypeDelegate::size(Loc loc) | |
3129 { | |
3130 return PTRSIZE * 2; | |
3131 } | |
3132 | |
3133 void TypeDelegate::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
3134 { | |
3135 if (mod != this->mod) | |
3136 { toCBuffer3(buf, hgs, mod); | |
3137 return; | |
3138 } | |
3139 TypeFunction *tf = (TypeFunction *)next; | |
3140 | |
3141 tf->next->toCBuffer2(buf, hgs, 0); | |
3142 buf->writestring(" delegate"); | |
3143 Argument::argsToCBuffer(buf, hgs, tf->parameters, tf->varargs); | |
3144 } | |
3145 | |
3146 Expression *TypeDelegate::defaultInit(Loc loc) | |
3147 { | |
3148 #if LOGDEFAULTINIT | |
3149 printf("TypeDelegate::defaultInit() '%s'\n", toChars()); | |
3150 #endif | |
3151 Expression *e; | |
3152 e = new NullExp(loc); | |
3153 e->type = this; | |
3154 return e; | |
3155 } | |
3156 | |
3157 int TypeDelegate::isZeroInit() | |
3158 { | |
3159 return 1; | |
3160 } | |
3161 | |
3162 int TypeDelegate::checkBoolean() | |
3163 { | |
3164 return TRUE; | |
3165 } | |
3166 | |
3167 Expression *TypeDelegate::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
3168 { | |
3169 #if LOGDOTEXP | |
3170 printf("TypeDelegate::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
3171 #endif | |
3172 if (ident == Id::ptr) | |
3173 { | |
3174 e->type = tvoidptr; | |
3175 return e; | |
3176 } | |
3177 else if (ident == Id::funcptr) | |
3178 { | |
3179 e = e->addressOf(sc); | |
3180 e->type = tvoidptr; | |
3181 e = new AddExp(e->loc, e, new IntegerExp(PTRSIZE)); | |
3182 e->type = tvoidptr; | |
3183 e = new PtrExp(e->loc, e); | |
3184 e->type = next->pointerTo(); | |
3185 return e; | |
3186 } | |
3187 else | |
3188 { | |
3189 e = Type::dotExp(sc, e, ident); | |
3190 } | |
3191 return e; | |
3192 } | |
3193 | |
3194 int TypeDelegate::hasPointers() | |
3195 { | |
3196 return TRUE; | |
3197 } | |
3198 | |
3199 | |
3200 | |
3201 /***************************** TypeQualified *****************************/ | |
3202 | |
3203 TypeQualified::TypeQualified(TY ty, Loc loc) | |
3204 : Type(ty, NULL) | |
3205 { | |
3206 this->loc = loc; | |
3207 } | |
3208 | |
3209 void TypeQualified::syntaxCopyHelper(TypeQualified *t) | |
3210 { | |
3211 //printf("TypeQualified::syntaxCopyHelper(%s) %s\n", t->toChars(), toChars()); | |
3212 idents.setDim(t->idents.dim); | |
3213 for (int i = 0; i < idents.dim; i++) | |
3214 { | |
3215 Identifier *id = (Identifier *)t->idents.data[i]; | |
3216 if (id->dyncast() == DYNCAST_DSYMBOL) | |
3217 { | |
3218 TemplateInstance *ti = (TemplateInstance *)id; | |
3219 | |
3220 ti = (TemplateInstance *)ti->syntaxCopy(NULL); | |
3221 id = (Identifier *)ti; | |
3222 } | |
3223 idents.data[i] = id; | |
3224 } | |
3225 } | |
3226 | |
3227 | |
3228 void TypeQualified::addIdent(Identifier *ident) | |
3229 { | |
3230 idents.push(ident); | |
3231 } | |
3232 | |
3233 void TypeQualified::toCBuffer2Helper(OutBuffer *buf, HdrGenState *hgs) | |
3234 { | |
3235 int i; | |
3236 | |
3237 for (i = 0; i < idents.dim; i++) | |
3238 { Identifier *id = (Identifier *)idents.data[i]; | |
3239 | |
3240 buf->writeByte('.'); | |
3241 | |
3242 if (id->dyncast() == DYNCAST_DSYMBOL) | |
3243 { | |
3244 TemplateInstance *ti = (TemplateInstance *)id; | |
3245 ti->toCBuffer(buf, hgs); | |
3246 } | |
3247 else | |
3248 buf->writestring(id->toChars()); | |
3249 } | |
3250 } | |
3251 | |
3252 d_uns64 TypeQualified::size(Loc loc) | |
3253 { | |
3254 error(this->loc, "size of type %s is not known", toChars()); | |
3255 return 1; | |
3256 } | |
3257 | |
3258 /************************************* | |
3259 * Takes an array of Identifiers and figures out if | |
3260 * it represents a Type or an Expression. | |
3261 * Output: | |
3262 * if expression, *pe is set | |
3263 * if type, *pt is set | |
3264 */ | |
3265 | |
3266 void TypeQualified::resolveHelper(Loc loc, Scope *sc, | |
3267 Dsymbol *s, Dsymbol *scopesym, | |
3268 Expression **pe, Type **pt, Dsymbol **ps) | |
3269 { | |
3270 Identifier *id = NULL; | |
3271 int i; | |
3272 VarDeclaration *v; | |
3273 EnumMember *em; | |
3274 TupleDeclaration *td; | |
3275 Type *t; | |
3276 Expression *e; | |
3277 | |
3278 #if 0 | |
3279 printf("TypeQualified::resolveHelper(sc = %p, idents = '%s')\n", sc, toChars()); | |
3280 if (scopesym) | |
3281 printf("\tscopesym = '%s'\n", scopesym->toChars()); | |
3282 #endif | |
3283 *pe = NULL; | |
3284 *pt = NULL; | |
3285 *ps = NULL; | |
3286 if (s) | |
3287 { | |
3288 //printf("\t1: s = '%s' %p, kind = '%s'\n",s->toChars(), s, s->kind()); | |
336 | 3289 s->checkDeprecated(loc, sc); // check for deprecated aliases |
159 | 3290 s = s->toAlias(); |
3291 //printf("\t2: s = '%s' %p, kind = '%s'\n",s->toChars(), s, s->kind()); | |
3292 for (i = 0; i < idents.dim; i++) | |
3293 { Dsymbol *sm; | |
3294 | |
3295 id = (Identifier *)idents.data[i]; | |
3296 sm = s->searchX(loc, sc, id); | |
3297 //printf("\t3: s = '%s' %p, kind = '%s'\n",s->toChars(), s, s->kind()); | |
3298 //printf("getType = '%s'\n", s->getType()->toChars()); | |
3299 if (!sm) | |
3300 { | |
3301 v = s->isVarDeclaration(); | |
3302 if (v && id == Id::length) | |
3303 { | |
3304 if (v->isConst() && v->getExpInitializer()) | |
3305 { e = v->getExpInitializer()->exp; | |
3306 } | |
3307 else | |
3308 e = new VarExp(loc, v); | |
3309 t = e->type; | |
3310 if (!t) | |
3311 goto Lerror; | |
3312 goto L3; | |
3313 } | |
3314 t = s->getType(); | |
3315 if (!t && s->isDeclaration()) | |
3316 t = s->isDeclaration()->type; | |
3317 if (t) | |
3318 { | |
3319 sm = t->toDsymbol(sc); | |
3320 if (sm) | |
3321 { sm = sm->search(loc, id, 0); | |
3322 if (sm) | |
3323 goto L2; | |
3324 } | |
3325 //e = t->getProperty(loc, id); | |
3326 e = new TypeExp(loc, t); | |
3327 e = t->dotExp(sc, e, id); | |
3328 i++; | |
3329 L3: | |
3330 for (; i < idents.dim; i++) | |
3331 { | |
3332 id = (Identifier *)idents.data[i]; | |
3333 //printf("e: '%s', id: '%s', type = %p\n", e->toChars(), id->toChars(), e->type); | |
3334 e = e->type->dotExp(sc, e, id); | |
3335 } | |
3336 *pe = e; | |
3337 } | |
3338 else | |
3339 Lerror: | |
3340 error(loc, "identifier '%s' of '%s' is not defined", id->toChars(), toChars()); | |
3341 return; | |
3342 } | |
3343 L2: | |
3344 s = sm->toAlias(); | |
3345 } | |
3346 | |
3347 v = s->isVarDeclaration(); | |
3348 if (v) | |
3349 { | |
3350 // It's not a type, it's an expression | |
3351 if (v->isConst() && v->getExpInitializer()) | |
3352 { | |
3353 ExpInitializer *ei = v->getExpInitializer(); | |
3354 assert(ei); | |
3355 *pe = ei->exp->copy(); // make copy so we can change loc | |
3356 (*pe)->loc = loc; | |
3357 } | |
3358 else | |
3359 { | |
3360 #if 0 | |
3361 WithScopeSymbol *withsym; | |
3362 if (scopesym && (withsym = scopesym->isWithScopeSymbol()) != NULL) | |
3363 { | |
3364 // Same as wthis.ident | |
3365 e = new VarExp(loc, withsym->withstate->wthis); | |
3366 e = new DotIdExp(loc, e, ident); | |
3367 //assert(0); // BUG: should handle this | |
3368 } | |
3369 else | |
3370 #endif | |
3371 *pe = new VarExp(loc, v); | |
3372 } | |
3373 return; | |
3374 } | |
3375 em = s->isEnumMember(); | |
3376 if (em) | |
3377 { | |
3378 // It's not a type, it's an expression | |
3379 *pe = em->value->copy(); | |
3380 return; | |
3381 } | |
3382 | |
3383 L1: | |
3384 t = s->getType(); | |
3385 if (!t) | |
3386 { | |
3387 // If the symbol is an import, try looking inside the import | |
3388 Import *si; | |
3389 | |
3390 si = s->isImport(); | |
3391 if (si) | |
3392 { | |
3393 s = si->search(loc, s->ident, 0); | |
3394 if (s && s != si) | |
3395 goto L1; | |
3396 s = si; | |
3397 } | |
3398 *ps = s; | |
3399 return; | |
3400 } | |
3401 if (t->ty == Tinstance && t != this && !t->deco) | |
3402 { error(loc, "forward reference to '%s'", t->toChars()); | |
3403 return; | |
3404 } | |
3405 | |
3406 if (t != this) | |
3407 { | |
3408 if (t->reliesOnTident()) | |
3409 { | |
3410 Scope *scx; | |
3411 | |
3412 for (scx = sc; 1; scx = scx->enclosing) | |
3413 { | |
3414 if (!scx) | |
3415 { error(loc, "forward reference to '%s'", t->toChars()); | |
3416 return; | |
3417 } | |
3418 if (scx->scopesym == scopesym) | |
3419 break; | |
3420 } | |
3421 t = t->semantic(loc, scx); | |
3422 //((TypeIdentifier *)t)->resolve(loc, scx, pe, &t, ps); | |
3423 } | |
3424 } | |
3425 if (t->ty == Ttuple) | |
3426 *pt = t; | |
3427 else | |
3428 *pt = t->merge(); | |
3429 } | |
3430 if (!s) | |
3431 { | |
3432 error(loc, "identifier '%s' is not defined", toChars()); | |
3433 } | |
3434 } | |
3435 | |
3436 /***************************** TypeIdentifier *****************************/ | |
3437 | |
3438 TypeIdentifier::TypeIdentifier(Loc loc, Identifier *ident) | |
3439 : TypeQualified(Tident, loc) | |
3440 { | |
3441 this->ident = ident; | |
3442 } | |
3443 | |
3444 | |
3445 Type *TypeIdentifier::syntaxCopy() | |
3446 { | |
3447 TypeIdentifier *t; | |
3448 | |
3449 t = new TypeIdentifier(loc, ident); | |
3450 t->syntaxCopyHelper(this); | |
3451 return t; | |
3452 } | |
3453 | |
3454 void TypeIdentifier::toDecoBuffer(OutBuffer *buf) | |
3455 { unsigned len; | |
3456 char *name; | |
3457 | |
3458 name = ident->toChars(); | |
3459 len = strlen(name); | |
3460 buf->printf("%c%d%s", mangleChar[ty], len, name); | |
3461 //buf->printf("%c%s", mangleChar[ty], name); | |
3462 } | |
3463 | |
3464 void TypeIdentifier::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
3465 { | |
3466 if (mod != this->mod) | |
3467 { toCBuffer3(buf, hgs, mod); | |
3468 return; | |
3469 } | |
3470 buf->writestring(this->ident->toChars()); | |
3471 toCBuffer2Helper(buf, hgs); | |
3472 } | |
3473 | |
3474 /************************************* | |
3475 * Takes an array of Identifiers and figures out if | |
3476 * it represents a Type or an Expression. | |
3477 * Output: | |
3478 * if expression, *pe is set | |
3479 * if type, *pt is set | |
3480 */ | |
3481 | |
3482 void TypeIdentifier::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) | |
3483 { Dsymbol *s; | |
3484 Dsymbol *scopesym; | |
3485 | |
3486 //printf("TypeIdentifier::resolve(sc = %p, idents = '%s')\n", sc, toChars()); | |
3487 s = sc->search(loc, ident, &scopesym); | |
3488 resolveHelper(loc, sc, s, scopesym, pe, pt, ps); | |
3489 } | |
3490 | |
3491 /***************************************** | |
3492 * See if type resolves to a symbol, if so, | |
3493 * return that symbol. | |
3494 */ | |
3495 | |
3496 Dsymbol *TypeIdentifier::toDsymbol(Scope *sc) | |
3497 { | |
3498 //printf("TypeIdentifier::toDsymbol('%s')\n", toChars()); | |
3499 if (!sc) | |
3500 return NULL; | |
3501 //printf("ident = '%s'\n", ident->toChars()); | |
3502 | |
3503 Dsymbol *scopesym; | |
3504 Dsymbol *s = sc->search(loc, ident, &scopesym); | |
3505 if (s) | |
3506 { | |
3507 for (int i = 0; i < idents.dim; i++) | |
3508 { | |
3509 Identifier *id = (Identifier *)idents.data[i]; | |
3510 s = s->searchX(loc, sc, id); | |
3511 if (!s) // failed to find a symbol | |
3512 { //printf("\tdidn't find a symbol\n"); | |
3513 break; | |
3514 } | |
3515 } | |
3516 } | |
3517 return s; | |
3518 } | |
3519 | |
3520 Type *TypeIdentifier::semantic(Loc loc, Scope *sc) | |
3521 { | |
3522 Type *t; | |
3523 Expression *e; | |
3524 Dsymbol *s; | |
3525 | |
3526 //printf("TypeIdentifier::semantic(%s)\n", toChars()); | |
3527 resolve(loc, sc, &e, &t, &s); | |
3528 if (t) | |
3529 { | |
3530 //printf("\tit's a type %d, %s, %s\n", t->ty, t->toChars(), t->deco); | |
3531 | |
3532 if (t->ty == Ttypedef) | |
3533 { TypeTypedef *tt = (TypeTypedef *)t; | |
3534 | |
3535 if (tt->sym->sem == 1) | |
3536 error(loc, "circular reference of typedef %s", tt->toChars()); | |
3537 } | |
3538 } | |
3539 else | |
3540 { | |
3541 #ifdef DEBUG | |
3542 if (!global.gag) | |
3543 printf("1: "); | |
3544 #endif | |
3545 if (s) | |
3546 { | |
3547 s->error(loc, "is used as a type"); | |
3548 } | |
3549 else | |
3550 error(loc, "%s is used as a type", toChars()); | |
3551 t = tvoid; | |
3552 } | |
3553 //t->print(); | |
3554 return t; | |
3555 } | |
3556 | |
3557 Type *TypeIdentifier::reliesOnTident() | |
3558 { | |
3559 return this; | |
3560 } | |
3561 | |
3562 Expression *TypeIdentifier::toExpression() | |
3563 { | |
3564 Expression *e = new IdentifierExp(loc, ident); | |
3565 for (int i = 0; i < idents.dim; i++) | |
3566 { | |
3567 Identifier *id = (Identifier *)idents.data[i]; | |
3568 e = new DotIdExp(loc, e, id); | |
3569 } | |
3570 | |
3571 return e; | |
3572 } | |
3573 | |
3574 /***************************** TypeInstance *****************************/ | |
3575 | |
3576 TypeInstance::TypeInstance(Loc loc, TemplateInstance *tempinst) | |
3577 : TypeQualified(Tinstance, loc) | |
3578 { | |
3579 this->tempinst = tempinst; | |
3580 } | |
3581 | |
3582 Type *TypeInstance::syntaxCopy() | |
3583 { | |
3584 //printf("TypeInstance::syntaxCopy() %s, %d\n", toChars(), idents.dim); | |
3585 TypeInstance *t; | |
3586 | |
3587 t = new TypeInstance(loc, (TemplateInstance *)tempinst->syntaxCopy(NULL)); | |
3588 t->syntaxCopyHelper(this); | |
3589 return t; | |
3590 } | |
3591 | |
3592 | |
3593 void TypeInstance::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
3594 { | |
3595 if (mod != this->mod) | |
3596 { toCBuffer3(buf, hgs, mod); | |
3597 return; | |
3598 } | |
3599 tempinst->toCBuffer(buf, hgs); | |
3600 toCBuffer2Helper(buf, hgs); | |
3601 } | |
3602 | |
3603 void TypeInstance::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) | |
3604 { | |
3605 // Note close similarity to TypeIdentifier::resolve() | |
3606 | |
3607 Dsymbol *s; | |
3608 | |
3609 *pe = NULL; | |
3610 *pt = NULL; | |
3611 *ps = NULL; | |
3612 | |
3613 #if 0 | |
3614 if (!idents.dim) | |
3615 { | |
3616 error(loc, "template instance '%s' has no identifier", toChars()); | |
3617 return; | |
3618 } | |
3619 #endif | |
3620 //id = (Identifier *)idents.data[0]; | |
3621 //printf("TypeInstance::resolve(sc = %p, idents = '%s')\n", sc, id->toChars()); | |
3622 s = tempinst; | |
3623 if (s) | |
3624 s->semantic(sc); | |
3625 resolveHelper(loc, sc, s, NULL, pe, pt, ps); | |
3626 //printf("pt = '%s'\n", (*pt)->toChars()); | |
3627 } | |
3628 | |
3629 Type *TypeInstance::semantic(Loc loc, Scope *sc) | |
3630 { | |
3631 Type *t; | |
3632 Expression *e; | |
3633 Dsymbol *s; | |
3634 | |
3635 //printf("TypeInstance::semantic(%s)\n", toChars()); | |
3636 | |
3637 if (sc->parameterSpecialization) | |
3638 { | |
3639 unsigned errors = global.errors; | |
3640 global.gag++; | |
3641 | |
3642 resolve(loc, sc, &e, &t, &s); | |
3643 | |
3644 global.gag--; | |
3645 if (errors != global.errors) | |
3646 { if (global.gag == 0) | |
3647 global.errors = errors; | |
3648 return this; | |
3649 } | |
3650 } | |
3651 else | |
3652 resolve(loc, sc, &e, &t, &s); | |
3653 | |
3654 if (!t) | |
3655 { | |
3656 #ifdef DEBUG | |
3657 printf("2: "); | |
3658 #endif | |
3659 error(loc, "%s is used as a type", toChars()); | |
3660 t = tvoid; | |
3661 } | |
3662 return t; | |
3663 } | |
3664 | |
3665 | |
3666 /***************************** TypeTypeof *****************************/ | |
3667 | |
3668 TypeTypeof::TypeTypeof(Loc loc, Expression *exp) | |
3669 : TypeQualified(Ttypeof, loc) | |
3670 { | |
3671 this->exp = exp; | |
3672 } | |
3673 | |
3674 Type *TypeTypeof::syntaxCopy() | |
3675 { | |
3676 TypeTypeof *t; | |
3677 | |
3678 t = new TypeTypeof(loc, exp->syntaxCopy()); | |
3679 t->syntaxCopyHelper(this); | |
3680 return t; | |
3681 } | |
3682 | |
3683 Dsymbol *TypeTypeof::toDsymbol(Scope *sc) | |
3684 { | |
3685 Type *t; | |
3686 | |
336 | 3687 t = semantic(loc, sc); |
159 | 3688 if (t == this) |
3689 return NULL; | |
3690 return t->toDsymbol(sc); | |
3691 } | |
3692 | |
3693 void TypeTypeof::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
3694 { | |
3695 if (mod != this->mod) | |
3696 { toCBuffer3(buf, hgs, mod); | |
3697 return; | |
3698 } | |
3699 buf->writestring("typeof("); | |
3700 exp->toCBuffer(buf, hgs); | |
3701 buf->writeByte(')'); | |
3702 toCBuffer2Helper(buf, hgs); | |
3703 } | |
3704 | |
3705 Type *TypeTypeof::semantic(Loc loc, Scope *sc) | |
3706 { Expression *e; | |
3707 Type *t; | |
3708 | |
3709 //printf("TypeTypeof::semantic() %p\n", this); | |
3710 | |
3711 //static int nest; if (++nest == 50) *(char*)0=0; | |
3712 | |
3713 #if 0 | |
3714 /* Special case for typeof(this) and typeof(super) since both | |
3715 * should work even if they are not inside a non-static member function | |
3716 */ | |
3717 if (exp->op == TOKthis || exp->op == TOKsuper) | |
3718 { | |
3719 // Find enclosing struct or class | |
3720 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
3721 { | |
3722 ClassDeclaration *cd; | |
3723 StructDeclaration *sd; | |
3724 | |
3725 if (!s) | |
3726 { | |
3727 error(loc, "%s is not in a struct or class scope", exp->toChars()); | |
3728 goto Lerr; | |
3729 } | |
3730 cd = s->isClassDeclaration(); | |
3731 if (cd) | |
3732 { | |
3733 if (exp->op == TOKsuper) | |
3734 { | |
3735 cd = cd->baseClass; | |
3736 if (!cd) | |
3737 { error(loc, "class %s has no 'super'", s->toChars()); | |
3738 goto Lerr; | |
3739 } | |
3740 } | |
3741 t = cd->type; | |
3742 break; | |
3743 } | |
3744 sd = s->isStructDeclaration(); | |
3745 if (sd) | |
3746 { | |
3747 if (exp->op == TOKsuper) | |
3748 { | |
3749 error(loc, "struct %s has no 'super'", sd->toChars()); | |
3750 goto Lerr; | |
3751 } | |
3752 t = sd->type->pointerTo(); | |
3753 break; | |
3754 } | |
3755 } | |
3756 } | |
3757 else | |
3758 #endif | |
3759 { | |
3760 sc->intypeof++; | |
3761 exp = exp->semantic(sc); | |
3762 sc->intypeof--; | |
336 | 3763 if (exp->op == TOKtype) |
3764 { | |
3765 error(loc, "argument %s to typeof is not an expression", exp->toChars()); | |
3766 } | |
159 | 3767 t = exp->type; |
3768 if (!t) | |
3769 { | |
3770 error(loc, "expression (%s) has no type", exp->toChars()); | |
3771 goto Lerr; | |
3772 } | |
3773 } | |
3774 | |
3775 if (idents.dim) | |
3776 { | |
3777 Dsymbol *s = t->toDsymbol(sc); | |
3778 for (size_t i = 0; i < idents.dim; i++) | |
3779 { | |
3780 if (!s) | |
3781 break; | |
3782 Identifier *id = (Identifier *)idents.data[i]; | |
3783 s = s->searchX(loc, sc, id); | |
3784 } | |
3785 if (s) | |
3786 { | |
3787 t = s->getType(); | |
3788 if (!t) | |
3789 { error(loc, "%s is not a type", s->toChars()); | |
3790 goto Lerr; | |
3791 } | |
3792 } | |
3793 else | |
3794 { error(loc, "cannot resolve .property for %s", toChars()); | |
3795 goto Lerr; | |
3796 } | |
3797 } | |
3798 return t; | |
3799 | |
3800 Lerr: | |
3801 return tvoid; | |
3802 } | |
3803 | |
3804 d_uns64 TypeTypeof::size(Loc loc) | |
3805 { | |
3806 if (exp->type) | |
3807 return exp->type->size(loc); | |
3808 else | |
3809 return TypeQualified::size(loc); | |
3810 } | |
3811 | |
3812 | |
3813 | |
3814 /***************************** TypeEnum *****************************/ | |
3815 | |
3816 TypeEnum::TypeEnum(EnumDeclaration *sym) | |
3817 : Type(Tenum, NULL) | |
3818 { | |
3819 this->sym = sym; | |
3820 } | |
3821 | |
3822 char *TypeEnum::toChars() | |
3823 { | |
3824 return sym->toChars(); | |
3825 } | |
3826 | |
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
|
3827 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
|
3828 { |
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 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
|
3830 } |
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 |
159 | 3832 Type *TypeEnum::semantic(Loc loc, Scope *sc) |
3833 { | |
3834 sym->semantic(sc); | |
3835 return merge(); | |
3836 } | |
3837 | |
3838 d_uns64 TypeEnum::size(Loc loc) | |
3839 { | |
3840 if (!sym->memtype) | |
3841 { | |
3842 error(loc, "enum %s is forward referenced", sym->toChars()); | |
3843 return 4; | |
3844 } | |
3845 return sym->memtype->size(loc); | |
3846 } | |
3847 | |
3848 unsigned TypeEnum::alignsize() | |
3849 { | |
3850 if (!sym->memtype) | |
3851 { | |
3852 #ifdef DEBUG | |
3853 printf("1: "); | |
3854 #endif | |
3855 error(0, "enum %s is forward referenced", sym->toChars()); | |
3856 return 4; | |
3857 } | |
3858 return sym->memtype->alignsize(); | |
3859 } | |
3860 | |
3861 Dsymbol *TypeEnum::toDsymbol(Scope *sc) | |
3862 { | |
3863 return sym; | |
3864 } | |
3865 | |
3866 Type *TypeEnum::toBasetype() | |
3867 { | |
3868 if (!sym->memtype) | |
3869 { | |
3870 #ifdef DEBUG | |
3871 printf("2: "); | |
3872 #endif | |
3873 error(sym->loc, "enum %s is forward referenced", sym->toChars()); | |
3874 return tint32; | |
3875 } | |
3876 return sym->memtype->toBasetype(); | |
3877 } | |
3878 | |
3879 void TypeEnum::toDecoBuffer(OutBuffer *buf) | |
3880 { char *name; | |
3881 | |
3882 name = sym->mangle(); | |
3883 // if (name[0] == '_' && name[1] == 'D') | |
3884 // name += 2; | |
3885 buf->printf("%c%s", mangleChar[ty], name); | |
3886 } | |
3887 | |
3888 void TypeEnum::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
3889 { | |
3890 if (mod != this->mod) | |
3891 { toCBuffer3(buf, hgs, mod); | |
3892 return; | |
3893 } | |
3894 buf->writestring(sym->toChars()); | |
3895 } | |
3896 | |
3897 Expression *TypeEnum::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
3898 { | |
3899 EnumMember *m; | |
3900 Dsymbol *s; | |
3901 Expression *em; | |
3902 | |
3903 #if LOGDOTEXP | |
3904 printf("TypeEnum::dotExp(e = '%s', ident = '%s') '%s'\n", e->toChars(), ident->toChars(), toChars()); | |
3905 #endif | |
3906 if (!sym->symtab) | |
3907 goto Lfwd; | |
3908 s = sym->symtab->lookup(ident); | |
3909 if (!s) | |
3910 { | |
3911 return getProperty(e->loc, ident); | |
3912 } | |
3913 m = s->isEnumMember(); | |
3914 em = m->value->copy(); | |
3915 em->loc = e->loc; | |
3916 return em; | |
3917 | |
3918 Lfwd: | |
3919 error(e->loc, "forward reference of %s.%s", toChars(), ident->toChars()); | |
3920 return new IntegerExp(0, 0, this); | |
3921 } | |
3922 | |
3923 Expression *TypeEnum::getProperty(Loc loc, Identifier *ident) | |
3924 { Expression *e; | |
3925 | |
3926 if (ident == Id::max) | |
3927 { | |
3928 if (!sym->symtab) | |
3929 goto Lfwd; | |
3930 e = new IntegerExp(0, sym->maxval, this); | |
3931 } | |
3932 else if (ident == Id::min) | |
3933 { | |
3934 if (!sym->symtab) | |
3935 goto Lfwd; | |
3936 e = new IntegerExp(0, sym->minval, this); | |
3937 } | |
3938 else if (ident == Id::init) | |
3939 { | |
3940 if (!sym->symtab) | |
3941 goto Lfwd; | |
3942 e = defaultInit(loc); | |
3943 } | |
3944 else | |
3945 { | |
3946 if (!sym->memtype) | |
3947 goto Lfwd; | |
3948 e = sym->memtype->getProperty(loc, ident); | |
3949 } | |
3950 return e; | |
3951 | |
3952 Lfwd: | |
3953 error(loc, "forward reference of %s.%s", toChars(), ident->toChars()); | |
3954 return new IntegerExp(0, 0, this); | |
3955 } | |
3956 | |
3957 int TypeEnum::isintegral() | |
3958 { | |
3959 return 1; | |
3960 } | |
3961 | |
3962 int TypeEnum::isfloating() | |
3963 { | |
3964 return 0; | |
3965 } | |
3966 | |
3967 int TypeEnum::isunsigned() | |
3968 { | |
3969 return sym->memtype->isunsigned(); | |
3970 } | |
3971 | |
3972 int TypeEnum::isscalar() | |
3973 { | |
3974 return 1; | |
3975 //return sym->memtype->isscalar(); | |
3976 } | |
3977 | |
3978 MATCH TypeEnum::implicitConvTo(Type *to) | |
3979 { MATCH m; | |
3980 | |
3981 //printf("TypeEnum::implicitConvTo()\n"); | |
3982 if (this->equals(to)) | |
3983 m = MATCHexact; // exact match | |
3984 else if (sym->memtype->implicitConvTo(to)) | |
3985 m = MATCHconvert; // match with conversions | |
3986 else | |
3987 m = MATCHnomatch; // no match | |
3988 return m; | |
3989 } | |
3990 | |
3991 Expression *TypeEnum::defaultInit(Loc loc) | |
3992 { | |
3993 #if LOGDEFAULTINIT | |
3994 printf("TypeEnum::defaultInit() '%s'\n", toChars()); | |
3995 #endif | |
3996 // Initialize to first member of enum | |
3997 Expression *e; | |
3998 e = new IntegerExp(loc, sym->defaultval, this); | |
3999 return e; | |
4000 } | |
4001 | |
4002 int TypeEnum::isZeroInit() | |
4003 { | |
4004 return (sym->defaultval == 0); | |
4005 } | |
4006 | |
4007 int TypeEnum::hasPointers() | |
4008 { | |
4009 return toBasetype()->hasPointers(); | |
4010 } | |
4011 | |
4012 /***************************** TypeTypedef *****************************/ | |
4013 | |
4014 TypeTypedef::TypeTypedef(TypedefDeclaration *sym) | |
4015 : Type(Ttypedef, NULL) | |
4016 { | |
4017 this->sym = sym; | |
4018 } | |
4019 | |
4020 Type *TypeTypedef::syntaxCopy() | |
4021 { | |
4022 return this; | |
4023 } | |
4024 | |
4025 char *TypeTypedef::toChars() | |
4026 { | |
4027 return sym->toChars(); | |
4028 } | |
4029 | |
4030 Type *TypeTypedef::semantic(Loc loc, Scope *sc) | |
4031 { | |
4032 //printf("TypeTypedef::semantic(%s), sem = %d\n", toChars(), sym->sem); | |
4033 sym->semantic(sc); | |
4034 return merge(); | |
4035 } | |
4036 | |
4037 d_uns64 TypeTypedef::size(Loc loc) | |
4038 { | |
4039 return sym->basetype->size(loc); | |
4040 } | |
4041 | |
4042 unsigned TypeTypedef::alignsize() | |
4043 { | |
4044 return sym->basetype->alignsize(); | |
4045 } | |
4046 | |
4047 Dsymbol *TypeTypedef::toDsymbol(Scope *sc) | |
4048 { | |
4049 return sym; | |
4050 } | |
4051 | |
4052 void TypeTypedef::toDecoBuffer(OutBuffer *buf) | |
4053 { unsigned len; | |
4054 char *name; | |
4055 | |
4056 name = sym->mangle(); | |
4057 // if (name[0] == '_' && name[1] == 'D') | |
4058 // name += 2; | |
4059 //len = strlen(name); | |
4060 //buf->printf("%c%d%s", mangleChar[ty], len, name); | |
4061 buf->printf("%c%s", mangleChar[ty], name); | |
4062 } | |
4063 | |
4064 void TypeTypedef::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
4065 { | |
4066 //printf("TypeTypedef::toCBuffer2() '%s'\n", sym->toChars()); | |
4067 if (mod != this->mod) | |
4068 { toCBuffer3(buf, hgs, mod); | |
4069 return; | |
4070 } | |
4071 buf->writestring(sym->toChars()); | |
4072 } | |
4073 | |
4074 Expression *TypeTypedef::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
4075 { | |
4076 #if LOGDOTEXP | |
4077 printf("TypeTypedef::dotExp(e = '%s', ident = '%s') '%s'\n", e->toChars(), ident->toChars(), toChars()); | |
4078 #endif | |
4079 if (ident == Id::init) | |
4080 { | |
4081 return Type::dotExp(sc, e, ident); | |
4082 } | |
4083 return sym->basetype->dotExp(sc, e, ident); | |
4084 } | |
4085 | |
4086 Expression *TypeTypedef::getProperty(Loc loc, Identifier *ident) | |
4087 { | |
4088 if (ident == Id::init) | |
4089 { | |
4090 return Type::getProperty(loc, ident); | |
4091 } | |
4092 return sym->basetype->getProperty(loc, ident); | |
4093 } | |
4094 | |
4095 int TypeTypedef::isbit() | |
4096 { | |
4097 return sym->basetype->isbit(); | |
4098 } | |
4099 | |
4100 int TypeTypedef::isintegral() | |
4101 { | |
4102 //printf("TypeTypedef::isintegral()\n"); | |
4103 //printf("sym = '%s'\n", sym->toChars()); | |
4104 //printf("basetype = '%s'\n", sym->basetype->toChars()); | |
4105 return sym->basetype->isintegral(); | |
4106 } | |
4107 | |
4108 int TypeTypedef::isfloating() | |
4109 { | |
4110 return sym->basetype->isfloating(); | |
4111 } | |
4112 | |
4113 int TypeTypedef::isreal() | |
4114 { | |
4115 return sym->basetype->isreal(); | |
4116 } | |
4117 | |
4118 int TypeTypedef::isimaginary() | |
4119 { | |
4120 return sym->basetype->isimaginary(); | |
4121 } | |
4122 | |
4123 int TypeTypedef::iscomplex() | |
4124 { | |
4125 return sym->basetype->iscomplex(); | |
4126 } | |
4127 | |
4128 int TypeTypedef::isunsigned() | |
4129 { | |
4130 return sym->basetype->isunsigned(); | |
4131 } | |
4132 | |
4133 int TypeTypedef::isscalar() | |
4134 { | |
4135 return sym->basetype->isscalar(); | |
4136 } | |
4137 | |
4138 int TypeTypedef::checkBoolean() | |
4139 { | |
4140 return sym->basetype->checkBoolean(); | |
4141 } | |
4142 | |
4143 Type *TypeTypedef::toBasetype() | |
4144 { | |
4145 if (sym->inuse) | |
4146 { | |
4147 sym->error("circular definition"); | |
4148 sym->basetype = Type::terror; | |
4149 return Type::terror; | |
4150 } | |
4151 sym->inuse = 1; | |
4152 Type *t = sym->basetype->toBasetype(); | |
4153 sym->inuse = 0; | |
4154 return t; | |
4155 } | |
4156 | |
4157 MATCH TypeTypedef::implicitConvTo(Type *to) | |
4158 { MATCH m; | |
4159 | |
4160 //printf("TypeTypedef::implicitConvTo()\n"); | |
4161 if (this->equals(to)) | |
4162 m = MATCHexact; // exact match | |
4163 else if (sym->basetype->implicitConvTo(to)) | |
4164 m = MATCHconvert; // match with conversions | |
4165 else | |
4166 m = MATCHnomatch; // no match | |
4167 return m; | |
4168 } | |
4169 | |
4170 Expression *TypeTypedef::defaultInit(Loc loc) | |
4171 { Expression *e; | |
4172 Type *bt; | |
4173 | |
4174 #if LOGDEFAULTINIT | |
4175 printf("TypeTypedef::defaultInit() '%s'\n", toChars()); | |
4176 #endif | |
4177 if (sym->init) | |
4178 { | |
4179 //sym->init->toExpression()->print(); | |
4180 return sym->init->toExpression(); | |
4181 } | |
4182 bt = sym->basetype; | |
4183 e = bt->defaultInit(loc); | |
4184 e->type = this; | |
4185 while (bt->ty == Tsarray) | |
4186 { | |
4187 e->type = bt->next; | |
4188 bt = bt->next->toBasetype(); | |
4189 } | |
4190 return e; | |
4191 } | |
4192 | |
4193 int TypeTypedef::isZeroInit() | |
4194 { | |
4195 if (sym->init) | |
4196 { | |
4197 if (sym->init->isVoidInitializer()) | |
4198 return 1; // initialize voids to 0 | |
4199 Expression *e = sym->init->toExpression(); | |
4200 if (e && e->isBool(FALSE)) | |
4201 return 1; | |
4202 return 0; // assume not | |
4203 } | |
4204 if (sym->inuse) | |
4205 { | |
4206 sym->error("circular definition"); | |
4207 sym->basetype = Type::terror; | |
4208 } | |
4209 sym->inuse = 1; | |
4210 int result = sym->basetype->isZeroInit(); | |
4211 sym->inuse = 0; | |
4212 return result; | |
4213 } | |
4214 | |
4215 int TypeTypedef::hasPointers() | |
4216 { | |
4217 return toBasetype()->hasPointers(); | |
4218 } | |
4219 | |
4220 /***************************** TypeStruct *****************************/ | |
4221 | |
4222 TypeStruct::TypeStruct(StructDeclaration *sym) | |
4223 : Type(Tstruct, NULL) | |
4224 { | |
4225 this->sym = sym; | |
4226 } | |
4227 | |
4228 char *TypeStruct::toChars() | |
4229 { | |
4230 //printf("sym.parent: %s, deco = %s\n", sym->parent->toChars(), deco); | |
4231 TemplateInstance *ti = sym->parent->isTemplateInstance(); | |
4232 if (ti && ti->toAlias() == sym) | |
4233 return ti->toChars(); | |
4234 return sym->toChars(); | |
4235 } | |
4236 | |
4237 Type *TypeStruct::syntaxCopy() | |
4238 { | |
4239 return this; | |
4240 } | |
4241 | |
4242 Type *TypeStruct::semantic(Loc loc, Scope *sc) | |
4243 { | |
4244 //printf("TypeStruct::semantic('%s')\n", sym->toChars()); | |
4245 | |
4246 /* Cannot do semantic for sym because scope chain may not | |
4247 * be right. | |
4248 */ | |
4249 //sym->semantic(sc); | |
4250 | |
4251 return merge(); | |
4252 } | |
4253 | |
4254 d_uns64 TypeStruct::size(Loc loc) | |
4255 { | |
4256 return sym->size(loc); | |
4257 } | |
4258 | |
4259 unsigned TypeStruct::alignsize() | |
4260 { unsigned sz; | |
4261 | |
4262 sym->size(0); // give error for forward references | |
4263 sz = sym->alignsize; | |
4264 if (sz > sym->structalign) | |
4265 sz = sym->structalign; | |
4266 return sz; | |
4267 } | |
4268 | |
4269 Dsymbol *TypeStruct::toDsymbol(Scope *sc) | |
4270 { | |
4271 return sym; | |
4272 } | |
4273 | |
4274 void TypeStruct::toDecoBuffer(OutBuffer *buf) | |
4275 { unsigned len; | |
4276 char *name; | |
4277 | |
4278 name = sym->mangle(); | |
4279 //printf("TypeStruct::toDecoBuffer('%s') = '%s'\n", toChars(), name); | |
4280 // if (name[0] == '_' && name[1] == 'D') | |
4281 // name += 2; | |
4282 //len = strlen(name); | |
4283 //buf->printf("%c%d%s", mangleChar[ty], len, name); | |
4284 buf->printf("%c%s", mangleChar[ty], name); | |
4285 } | |
4286 | |
4287 void TypeStruct::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
4288 { | |
4289 if (mod != this->mod) | |
4290 { toCBuffer3(buf, hgs, mod); | |
4291 return; | |
4292 } | |
4293 TemplateInstance *ti = sym->parent->isTemplateInstance(); | |
4294 if (ti && ti->toAlias() == sym) | |
4295 buf->writestring(ti->toChars()); | |
4296 else | |
4297 buf->writestring(sym->toChars()); | |
4298 } | |
4299 | |
4300 Expression *TypeStruct::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
4301 { unsigned offset; | |
4302 | |
4303 Expression *b; | |
4304 VarDeclaration *v; | |
4305 Dsymbol *s; | |
4306 DotVarExp *de; | |
4307 Declaration *d; | |
4308 | |
4309 #if LOGDOTEXP | |
4310 printf("TypeStruct::dotExp(e = '%s', ident = '%s')\n", e->toChars(), ident->toChars()); | |
4311 #endif | |
4312 if (!sym->members) | |
4313 { | |
4314 error(e->loc, "struct %s is forward referenced", sym->toChars()); | |
4315 return new IntegerExp(e->loc, 0, Type::tint32); | |
4316 } | |
4317 | |
4318 if (ident == Id::tupleof) | |
4319 { | |
4320 /* Create a TupleExp | |
4321 */ | |
336 | 4322 e = e->semantic(sc); // do this before turning on noaccesscheck |
159 | 4323 Expressions *exps = new Expressions; |
4324 exps->reserve(sym->fields.dim); | |
4325 for (size_t i = 0; i < sym->fields.dim; i++) | |
4326 { VarDeclaration *v = (VarDeclaration *)sym->fields.data[i]; | |
4327 Expression *fe = new DotVarExp(e->loc, e, v); | |
4328 exps->push(fe); | |
4329 } | |
4330 e = new TupleExp(e->loc, exps); | |
336 | 4331 sc = sc->push(); |
4332 sc->noaccesscheck = 1; | |
159 | 4333 e = e->semantic(sc); |
336 | 4334 sc->pop(); |
159 | 4335 return e; |
4336 } | |
4337 | |
4338 if (e->op == TOKdotexp) | |
4339 { DotExp *de = (DotExp *)e; | |
4340 | |
4341 if (de->e1->op == TOKimport) | |
4342 { | |
4343 ScopeExp *se = (ScopeExp *)de->e1; | |
4344 | |
4345 s = se->sds->search(e->loc, ident, 0); | |
4346 e = de->e1; | |
4347 goto L1; | |
4348 } | |
4349 } | |
4350 | |
4351 s = sym->search(e->loc, ident, 0); | |
4352 L1: | |
4353 if (!s) | |
4354 { | |
4355 //return getProperty(e->loc, ident); | |
4356 return Type::dotExp(sc, e, ident); | |
4357 } | |
336 | 4358 if (!s->isFuncDeclaration()) // because of overloading |
4359 s->checkDeprecated(e->loc, sc); | |
159 | 4360 s = s->toAlias(); |
4361 | |
4362 v = s->isVarDeclaration(); | |
4363 if (v && v->isConst()) | |
4364 { ExpInitializer *ei = v->getExpInitializer(); | |
4365 | |
4366 if (ei) | |
4367 { e = ei->exp->copy(); // need to copy it if it's a StringExp | |
4368 e = e->semantic(sc); | |
4369 return e; | |
4370 } | |
4371 } | |
4372 | |
4373 if (s->getType()) | |
4374 { | |
4375 //return new DotTypeExp(e->loc, e, s); | |
4376 return new TypeExp(e->loc, s->getType()); | |
4377 } | |
4378 | |
4379 EnumMember *em = s->isEnumMember(); | |
4380 if (em) | |
4381 { | |
4382 assert(em->value); | |
4383 return em->value->copy(); | |
4384 } | |
4385 | |
4386 TemplateMixin *tm = s->isTemplateMixin(); | |
4387 if (tm) | |
4388 { Expression *de; | |
4389 | |
4390 de = new DotExp(e->loc, e, new ScopeExp(e->loc, tm)); | |
4391 de->type = e->type; | |
4392 return de; | |
4393 } | |
4394 | |
4395 TemplateDeclaration *td = s->isTemplateDeclaration(); | |
4396 if (td) | |
4397 { | |
4398 e = new DotTemplateExp(e->loc, e, td); | |
4399 e->semantic(sc); | |
4400 return e; | |
4401 } | |
4402 | |
4403 TemplateInstance *ti = s->isTemplateInstance(); | |
4404 if (ti) | |
4405 { if (!ti->semanticdone) | |
4406 ti->semantic(sc); | |
4407 s = ti->inst->toAlias(); | |
4408 if (!s->isTemplateInstance()) | |
4409 goto L1; | |
4410 Expression *de = new DotExp(e->loc, e, new ScopeExp(e->loc, ti)); | |
4411 de->type = e->type; | |
4412 return de; | |
4413 } | |
4414 | |
4415 d = s->isDeclaration(); | |
4416 #ifdef DEBUG | |
4417 if (!d) | |
4418 printf("d = %s '%s'\n", s->kind(), s->toChars()); | |
4419 #endif | |
4420 assert(d); | |
4421 | |
4422 if (e->op == TOKtype) | |
4423 { FuncDeclaration *fd = sc->func; | |
4424 | |
4425 if (d->needThis() && fd && fd->vthis) | |
4426 { | |
4427 e = new DotVarExp(e->loc, new ThisExp(e->loc), d); | |
4428 e = e->semantic(sc); | |
4429 return e; | |
4430 } | |
4431 if (d->isTupleDeclaration()) | |
4432 { | |
4433 e = new TupleExp(e->loc, d->isTupleDeclaration()); | |
4434 e = e->semantic(sc); | |
4435 return e; | |
4436 } | |
4437 return new VarExp(e->loc, d); | |
4438 } | |
4439 | |
4440 if (d->isDataseg()) | |
4441 { | |
4442 // (e, d) | |
4443 VarExp *ve; | |
4444 | |
4445 accessCheck(e->loc, sc, e, d); | |
4446 ve = new VarExp(e->loc, d); | |
4447 e = new CommaExp(e->loc, e, ve); | |
4448 e->type = d->type; | |
4449 return e; | |
4450 } | |
4451 | |
4452 if (v) | |
4453 { | |
4454 if (v->toParent() != sym) | |
4455 sym->error(e->loc, "'%s' is not a member", v->toChars()); | |
4456 | |
4457 // *(&e + offset) | |
4458 accessCheck(e->loc, sc, e, d); | |
4459 b = new AddrExp(e->loc, e); | |
4460 b->type = e->type->pointerTo(); | |
4461 b = new AddExp(e->loc, b, new IntegerExp(e->loc, v->offset, Type::tint32)); | |
4462 #if IN_LLVM | |
4463 // LLVMDC modification | |
4464 // this is *essential* | |
4465 ((AddExp*)b)->llvmFieldIndex = true; | |
4466 #endif | |
4467 b->type = v->type->pointerTo(); | |
4468 e = new PtrExp(e->loc, b); | |
4469 e->type = v->type; | |
4470 return e; | |
4471 } | |
4472 | |
4473 de = new DotVarExp(e->loc, e, d); | |
4474 return de->semantic(sc); | |
4475 } | |
4476 | |
4477 unsigned TypeStruct::memalign(unsigned salign) | |
4478 { | |
4479 sym->size(0); // give error for forward references | |
4480 return sym->structalign; | |
4481 } | |
4482 | |
4483 Expression *TypeStruct::defaultInit(Loc loc) | |
4484 { Symbol *s; | |
4485 Declaration *d; | |
4486 | |
4487 #if LOGDEFAULTINIT | |
4488 printf("TypeStruct::defaultInit() '%s'\n", toChars()); | |
4489 #endif | |
4490 s = sym->toInitializer(); | |
4491 d = new SymbolDeclaration(sym->loc, s, sym); | |
4492 assert(d); | |
4493 d->type = this; | |
4494 return new VarExp(sym->loc, d); | |
4495 } | |
4496 | |
4497 int TypeStruct::isZeroInit() | |
4498 { | |
4499 return sym->zeroInit; | |
4500 } | |
4501 | |
4502 int TypeStruct::checkBoolean() | |
4503 { | |
4504 return FALSE; | |
4505 } | |
4506 | |
4507 int TypeStruct::hasPointers() | |
4508 { | |
4509 StructDeclaration *s = sym; | |
4510 | |
4511 sym->size(0); // give error for forward references | |
4512 if (s->members) | |
4513 { | |
4514 for (size_t i = 0; i < s->members->dim; i++) | |
4515 { | |
4516 Dsymbol *sm = (Dsymbol *)s->members->data[i]; | |
4517 if (sm->hasPointers()) | |
4518 return TRUE; | |
4519 } | |
4520 } | |
4521 return FALSE; | |
4522 } | |
4523 | |
4524 | |
4525 /***************************** TypeClass *****************************/ | |
4526 | |
4527 TypeClass::TypeClass(ClassDeclaration *sym) | |
4528 : Type(Tclass, NULL) | |
4529 { | |
4530 this->sym = sym; | |
4531 } | |
4532 | |
4533 char *TypeClass::toChars() | |
4534 { | |
4535 return sym->toPrettyChars(); | |
4536 } | |
4537 | |
4538 Type *TypeClass::syntaxCopy() | |
4539 { | |
4540 return this; | |
4541 } | |
4542 | |
4543 Type *TypeClass::semantic(Loc loc, Scope *sc) | |
4544 { | |
4545 //printf("TypeClass::semantic(%s)\n", sym->toChars()); | |
4546 if (sym->scope) | |
4547 sym->semantic(sym->scope); | |
4548 return merge(); | |
4549 } | |
4550 | |
4551 d_uns64 TypeClass::size(Loc loc) | |
4552 { | |
4553 return PTRSIZE; | |
4554 } | |
4555 | |
4556 Dsymbol *TypeClass::toDsymbol(Scope *sc) | |
4557 { | |
4558 return sym; | |
4559 } | |
4560 | |
4561 void TypeClass::toDecoBuffer(OutBuffer *buf) | |
4562 { unsigned len; | |
4563 char *name; | |
4564 | |
4565 name = sym->mangle(); | |
4566 // if (name[0] == '_' && name[1] == 'D') | |
4567 // name += 2; | |
4568 //printf("TypeClass::toDecoBuffer('%s') = '%s'\n", toChars(), name); | |
4569 //len = strlen(name); | |
4570 //buf->printf("%c%d%s", mangleChar[ty], len, name); | |
4571 buf->printf("%c%s", mangleChar[ty], name); | |
4572 } | |
4573 | |
4574 void TypeClass::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
4575 { | |
4576 if (mod != this->mod) | |
4577 { toCBuffer3(buf, hgs, mod); | |
4578 return; | |
4579 } | |
4580 buf->writestring(sym->toChars()); | |
4581 } | |
4582 | |
4583 Expression *TypeClass::dotExp(Scope *sc, Expression *e, Identifier *ident) | |
4584 { unsigned offset; | |
4585 | |
4586 Expression *b; | |
4587 VarDeclaration *v; | |
4588 Dsymbol *s; | |
4589 DotVarExp *de; | |
4590 Declaration *d; | |
4591 | |
4592 #if LOGDOTEXP | |
4593 printf("TypeClass::dotExp(e='%s', ident='%s')\n", e->toChars(), ident->toChars()); | |
4594 #endif | |
4595 | |
4596 if (e->op == TOKdotexp) | |
4597 { DotExp *de = (DotExp *)e; | |
4598 | |
4599 if (de->e1->op == TOKimport) | |
4600 { | |
4601 ScopeExp *se = (ScopeExp *)de->e1; | |
4602 | |
4603 s = se->sds->search(e->loc, ident, 0); | |
4604 e = de->e1; | |
4605 goto L1; | |
4606 } | |
4607 } | |
4608 | |
4609 if (ident == Id::tupleof) | |
4610 { | |
4611 /* Create a TupleExp | |
4612 */ | |
336 | 4613 e = e->semantic(sc); // do this before turning on noaccesscheck |
159 | 4614 Expressions *exps = new Expressions; |
4615 exps->reserve(sym->fields.dim); | |
4616 for (size_t i = 0; i < sym->fields.dim; i++) | |
4617 { VarDeclaration *v = (VarDeclaration *)sym->fields.data[i]; | |
4618 Expression *fe = new DotVarExp(e->loc, e, v); | |
4619 exps->push(fe); | |
4620 } | |
4621 e = new TupleExp(e->loc, exps); | |
336 | 4622 sc = sc->push(); |
4623 sc->noaccesscheck = 1; | |
159 | 4624 e = e->semantic(sc); |
336 | 4625 sc->pop(); |
159 | 4626 return e; |
4627 } | |
4628 | |
4629 s = sym->search(e->loc, ident, 0); | |
4630 L1: | |
4631 if (!s) | |
4632 { | |
4633 // See if it's a base class | |
4634 ClassDeclaration *cbase; | |
4635 for (cbase = sym->baseClass; cbase; cbase = cbase->baseClass) | |
4636 { | |
4637 if (cbase->ident->equals(ident)) | |
4638 { | |
4639 e = new DotTypeExp(0, e, cbase); | |
4640 return e; | |
4641 } | |
4642 } | |
4643 | |
4644 if (ident == Id::classinfo) | |
4645 { | |
4646 Type *t; | |
4647 | |
4648 assert(ClassDeclaration::classinfo); | |
4649 t = ClassDeclaration::classinfo->type; | |
4650 if (e->op == TOKtype || e->op == TOKdottype) | |
4651 { | |
4652 /* For type.classinfo, we know the classinfo | |
4653 * at compile time. | |
4654 */ | |
4655 if (!sym->vclassinfo) | |
4656 sym->vclassinfo = new ClassInfoDeclaration(sym); | |
4657 e = new VarExp(e->loc, sym->vclassinfo); | |
4658 e = e->addressOf(sc); | |
4659 e->type = t; // do this so we don't get redundant dereference | |
4660 } | |
4661 else | |
4662 { | |
4663 /* For class objects, the classinfo reference is the first | |
4664 * entry in the vtbl[] | |
4665 */ | |
4666 #if IN_LLVM | |
4667 | |
4668 Type* ct; | |
4669 if (sym->isInterfaceDeclaration()) { | |
4670 ct = t->pointerTo()->pointerTo()->pointerTo(); | |
4671 } | |
4672 else { | |
4673 ct = t->pointerTo()->pointerTo(); | |
4674 } | |
4675 | |
4676 e = e->castTo(sc, ct); | |
4677 e = new PtrExp(e->loc, e); | |
4678 e->type = ct->next; | |
4679 e = new PtrExp(e->loc, e); | |
4680 e->type = ct->next->next; | |
4681 | |
4682 if (sym->isInterfaceDeclaration()) | |
4683 { | |
4684 if (sym->isCOMinterface()) | |
4685 { /* COM interface vtbl[]s are different in that the | |
4686 * first entry is always pointer to QueryInterface(). | |
4687 * We can't get a .classinfo for it. | |
4688 */ | |
4689 error(e->loc, "no .classinfo for COM interface objects"); | |
4690 } | |
4691 /* For an interface, the first entry in the vtbl[] | |
4692 * is actually a pointer to an instance of struct Interface. | |
4693 * The first member of Interface is the .classinfo, | |
4694 * so add an extra pointer indirection. | |
4695 */ | |
4696 e = new PtrExp(e->loc, e); | |
4697 e->type = ct->next->next->next; | |
4698 } | |
336 | 4699 } |
159 | 4700 |
4701 #else | |
4702 | |
4703 e = new PtrExp(e->loc, e); | |
4704 e->type = t->pointerTo(); | |
4705 if (sym->isInterfaceDeclaration()) | |
4706 { | |
4707 if (sym->isCOMinterface()) | |
4708 { /* COM interface vtbl[]s are different in that the | |
4709 * first entry is always pointer to QueryInterface(). | |
4710 * We can't get a .classinfo for it. | |
4711 */ | |
4712 error(e->loc, "no .classinfo for COM interface objects"); | |
4713 } | |
4714 /* For an interface, the first entry in the vtbl[] | |
4715 * is actually a pointer to an instance of struct Interface. | |
4716 * The first member of Interface is the .classinfo, | |
4717 * so add an extra pointer indirection. | |
4718 */ | |
4719 e->type = e->type->pointerTo(); | |
4720 e = new PtrExp(e->loc, e); | |
4721 e->type = t->pointerTo(); | |
4722 } | |
4723 e = new PtrExp(e->loc, e, t); | |
336 | 4724 } |
4725 | |
4726 #endif // !LLVMDC | |
4727 | |
4728 return e; | |
4729 } | |
4730 | |
4731 if (ident == Id::__vptr) | |
4732 { /* The pointer to the vtbl[] | |
4733 * *cast(void***)e | |
4734 */ | |
4735 e = e->castTo(sc, tvoidptr->pointerTo()->pointerTo()); | |
4736 e = new PtrExp(e->loc, e); | |
4737 e = e->semantic(sc); | |
4738 return e; | |
4739 } | |
4740 | |
4741 if (ident == Id::__monitor) | |
4742 { /* The handle to the monitor (call it a void*) | |
4743 * *(cast(void**)e + 1) | |
4744 */ | |
4745 e = e->castTo(sc, tvoidptr->pointerTo()); | |
4746 e = new AddExp(e->loc, e, new IntegerExp(1)); | |
4747 e = new PtrExp(e->loc, e); | |
4748 e = e->semantic(sc); | |
159 | 4749 return e; |
4750 } | |
4751 | |
4752 if (ident == Id::typeinfo) | |
4753 { | |
4754 if (!global.params.useDeprecated) | |
4755 error(e->loc, ".typeinfo deprecated, use typeid(type)"); | |
4756 return getTypeInfo(sc); | |
4757 } | |
4758 if (ident == Id::outer && sym->vthis) | |
4759 { | |
4760 s = sym->vthis; | |
4761 } | |
4762 else | |
4763 { | |
4764 //return getProperty(e->loc, ident); | |
4765 return Type::dotExp(sc, e, ident); | |
4766 } | |
4767 } | |
336 | 4768 if (!s->isFuncDeclaration()) // because of overloading |
4769 s->checkDeprecated(e->loc, sc); | |
159 | 4770 s = s->toAlias(); |
4771 v = s->isVarDeclaration(); | |
4772 if (v && v->isConst()) | |
4773 { ExpInitializer *ei = v->getExpInitializer(); | |
4774 | |
4775 if (ei) | |
4776 { e = ei->exp->copy(); // need to copy it if it's a StringExp | |
4777 e = e->semantic(sc); | |
4778 return e; | |
4779 } | |
4780 } | |
4781 | |
4782 if (s->getType()) | |
4783 { | |
4784 // if (e->op == TOKtype) | |
4785 return new TypeExp(e->loc, s->getType()); | |
4786 // return new DotTypeExp(e->loc, e, s); | |
4787 } | |
4788 | |
4789 EnumMember *em = s->isEnumMember(); | |
4790 if (em) | |
4791 { | |
4792 assert(em->value); | |
4793 return em->value->copy(); | |
4794 } | |
4795 | |
4796 TemplateMixin *tm = s->isTemplateMixin(); | |
4797 if (tm) | |
4798 { Expression *de; | |
4799 | |
4800 de = new DotExp(e->loc, e, new ScopeExp(e->loc, tm)); | |
4801 de->type = e->type; | |
4802 return de; | |
4803 } | |
4804 | |
4805 TemplateDeclaration *td = s->isTemplateDeclaration(); | |
4806 if (td) | |
4807 { | |
4808 e = new DotTemplateExp(e->loc, e, td); | |
4809 e->semantic(sc); | |
4810 return e; | |
4811 } | |
4812 | |
4813 TemplateInstance *ti = s->isTemplateInstance(); | |
4814 if (ti) | |
4815 { if (!ti->semanticdone) | |
4816 ti->semantic(sc); | |
4817 s = ti->inst->toAlias(); | |
4818 if (!s->isTemplateInstance()) | |
4819 goto L1; | |
4820 Expression *de = new DotExp(e->loc, e, new ScopeExp(e->loc, ti)); | |
4821 de->type = e->type; | |
4822 return de; | |
4823 } | |
4824 | |
4825 d = s->isDeclaration(); | |
4826 if (!d) | |
4827 { | |
4828 e->error("%s.%s is not a declaration", e->toChars(), ident->toChars()); | |
4829 return new IntegerExp(e->loc, 1, Type::tint32); | |
4830 } | |
4831 | |
4832 if (e->op == TOKtype) | |
4833 { | |
4834 VarExp *ve; | |
4835 | |
4836 if (d->needThis() && (hasThis(sc) || !d->isFuncDeclaration())) | |
4837 { | |
4838 if (sc->func) | |
4839 { | |
4840 ClassDeclaration *thiscd; | |
4841 thiscd = sc->func->toParent()->isClassDeclaration(); | |
4842 | |
4843 if (thiscd) | |
4844 { | |
4845 ClassDeclaration *cd = e->type->isClassHandle(); | |
4846 | |
4847 if (cd == thiscd) | |
4848 { | |
4849 e = new ThisExp(e->loc); | |
4850 e = new DotTypeExp(e->loc, e, cd); | |
4851 de = new DotVarExp(e->loc, e, d); | |
4852 e = de->semantic(sc); | |
4853 return e; | |
4854 } | |
4855 else if ((!cd || !cd->isBaseOf(thiscd, NULL)) && | |
4856 !d->isFuncDeclaration()) | |
4857 e->error("'this' is required, but %s is not a base class of %s", e->type->toChars(), thiscd->toChars()); | |
4858 } | |
4859 } | |
4860 | |
4861 de = new DotVarExp(e->loc, new ThisExp(e->loc), d); | |
4862 e = de->semantic(sc); | |
4863 return e; | |
4864 } | |
4865 else if (d->isTupleDeclaration()) | |
4866 { | |
4867 e = new TupleExp(e->loc, d->isTupleDeclaration()); | |
4868 e = e->semantic(sc); | |
4869 return e; | |
4870 } | |
4871 else | |
4872 ve = new VarExp(e->loc, d); | |
4873 return ve; | |
4874 } | |
4875 | |
4876 if (d->isDataseg()) | |
4877 { | |
4878 // (e, d) | |
4879 VarExp *ve; | |
4880 | |
4881 accessCheck(e->loc, sc, e, d); | |
4882 ve = new VarExp(e->loc, d); | |
4883 e = new CommaExp(e->loc, e, ve); | |
4884 e->type = d->type; | |
4885 return e; | |
4886 } | |
4887 | |
4888 if (d->parent && d->toParent()->isModule()) | |
4889 { | |
4890 // (e, d) | |
4891 VarExp *ve; | |
4892 | |
4893 ve = new VarExp(e->loc, d); | |
4894 e = new CommaExp(e->loc, e, ve); | |
4895 e->type = d->type; | |
4896 return e; | |
4897 } | |
4898 | |
4899 de = new DotVarExp(e->loc, e, d); | |
4900 return de->semantic(sc); | |
4901 } | |
4902 | |
4903 ClassDeclaration *TypeClass::isClassHandle() | |
4904 { | |
4905 return sym; | |
4906 } | |
4907 | |
4908 int TypeClass::isauto() | |
4909 { | |
4910 return sym->isauto; | |
4911 } | |
4912 | |
4913 int TypeClass::isBaseOf(Type *t, int *poffset) | |
4914 { | |
4915 if (t->ty == Tclass) | |
4916 { ClassDeclaration *cd; | |
4917 | |
4918 cd = ((TypeClass *)t)->sym; | |
4919 if (sym->isBaseOf(cd, poffset)) | |
4920 return 1; | |
4921 } | |
4922 return 0; | |
4923 } | |
4924 | |
4925 MATCH TypeClass::implicitConvTo(Type *to) | |
4926 { | |
4927 //printf("TypeClass::implicitConvTo('%s')\n", to->toChars()); | |
4928 if (this == to) | |
4929 return MATCHexact; | |
4930 | |
4931 ClassDeclaration *cdto = to->isClassHandle(); | |
4932 if (cdto && cdto->isBaseOf(sym, NULL)) | |
4933 { //printf("is base\n"); | |
4934 return MATCHconvert; | |
4935 } | |
4936 | |
4937 if (global.params.Dversion == 1) | |
4938 { | |
4939 // Allow conversion to (void *) | |
4940 if (to->ty == Tpointer && to->next->ty == Tvoid) | |
4941 return MATCHconvert; | |
4942 } | |
4943 | |
4944 return MATCHnomatch; | |
4945 } | |
4946 | |
4947 Expression *TypeClass::defaultInit(Loc loc) | |
4948 { | |
4949 #if LOGDEFAULTINIT | |
4950 printf("TypeClass::defaultInit() '%s'\n", toChars()); | |
4951 #endif | |
4952 Expression *e; | |
4953 e = new NullExp(loc); | |
4954 e->type = this; | |
4955 return e; | |
4956 } | |
4957 | |
4958 int TypeClass::isZeroInit() | |
4959 { | |
4960 return 1; | |
4961 } | |
4962 | |
4963 int TypeClass::checkBoolean() | |
4964 { | |
4965 return TRUE; | |
4966 } | |
4967 | |
4968 int TypeClass::hasPointers() | |
4969 { | |
4970 return TRUE; | |
4971 } | |
4972 | |
4973 /***************************** TypeTuple *****************************/ | |
4974 | |
4975 TypeTuple::TypeTuple(Arguments *arguments) | |
4976 : Type(Ttuple, NULL) | |
4977 { | |
4978 //printf("TypeTuple(this = %p)\n", this); | |
4979 this->arguments = arguments; | |
4980 #ifdef DEBUG | |
4981 if (arguments) | |
4982 { | |
4983 for (size_t i = 0; i < arguments->dim; i++) | |
4984 { | |
4985 Argument *arg = (Argument *)arguments->data[i]; | |
4986 assert(arg && arg->type); | |
4987 } | |
4988 } | |
4989 #endif | |
4990 } | |
4991 | |
4992 /**************** | |
4993 * Form TypeTuple from the types of the expressions. | |
4994 * Assume exps[] is already tuple expanded. | |
4995 */ | |
4996 | |
4997 TypeTuple::TypeTuple(Expressions *exps) | |
4998 : Type(Ttuple, NULL) | |
4999 { | |
5000 Arguments *arguments = new Arguments; | |
5001 if (exps) | |
5002 { | |
5003 arguments->setDim(exps->dim); | |
5004 for (size_t i = 0; i < exps->dim; i++) | |
5005 { Expression *e = (Expression *)exps->data[i]; | |
5006 if (e->type->ty == Ttuple) | |
5007 e->error("cannot form tuple of tuples"); | |
5008 Argument *arg = new Argument(STCin, e->type, NULL, NULL); | |
5009 arguments->data[i] = (void *)arg; | |
5010 } | |
5011 } | |
5012 this->arguments = arguments; | |
5013 } | |
5014 | |
5015 Type *TypeTuple::syntaxCopy() | |
5016 { | |
5017 Arguments *args = Argument::arraySyntaxCopy(arguments); | |
5018 Type *t = new TypeTuple(args); | |
5019 return t; | |
5020 } | |
5021 | |
5022 Type *TypeTuple::semantic(Loc loc, Scope *sc) | |
5023 { | |
5024 //printf("TypeTuple::semantic(this = %p)\n", this); | |
5025 if (!deco) | |
5026 deco = merge()->deco; | |
5027 | |
5028 /* Don't return merge(), because a tuple with one type has the | |
5029 * same deco as that type. | |
5030 */ | |
5031 return this; | |
5032 } | |
5033 | |
5034 int TypeTuple::equals(Object *o) | |
5035 { Type *t; | |
5036 | |
5037 t = (Type *)o; | |
5038 //printf("TypeTuple::equals(%s, %s)\n", toChars(), t->toChars()); | |
5039 if (this == t) | |
5040 { | |
5041 return 1; | |
5042 } | |
5043 if (t->ty == Ttuple) | |
5044 { TypeTuple *tt = (TypeTuple *)t; | |
5045 | |
5046 if (arguments->dim == tt->arguments->dim) | |
5047 { | |
5048 for (size_t i = 0; i < tt->arguments->dim; i++) | |
5049 { Argument *arg1 = (Argument *)arguments->data[i]; | |
5050 Argument *arg2 = (Argument *)tt->arguments->data[i]; | |
5051 | |
5052 if (!arg1->type->equals(arg2->type)) | |
5053 return 0; | |
5054 } | |
5055 return 1; | |
5056 } | |
5057 } | |
5058 return 0; | |
5059 } | |
5060 | |
5061 Type *TypeTuple::reliesOnTident() | |
5062 { | |
5063 if (arguments) | |
5064 { | |
5065 for (size_t i = 0; i < arguments->dim; i++) | |
5066 { | |
5067 Argument *arg = (Argument *)arguments->data[i]; | |
5068 Type *t = arg->type->reliesOnTident(); | |
5069 if (t) | |
5070 return t; | |
5071 } | |
5072 } | |
5073 return NULL; | |
5074 } | |
5075 | |
5076 void TypeTuple::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
5077 { | |
5078 Argument::argsToCBuffer(buf, hgs, arguments, 0); | |
5079 } | |
5080 | |
5081 void TypeTuple::toDecoBuffer(OutBuffer *buf) | |
5082 { | |
5083 //printf("TypeTuple::toDecoBuffer() this = %p\n", this); | |
5084 OutBuffer buf2; | |
5085 Argument::argsToDecoBuffer(&buf2, arguments); | |
5086 unsigned len = buf2.offset; | |
5087 buf->printf("%c%d%.*s", mangleChar[ty], len, len, (char *)buf2.extractData()); | |
5088 } | |
5089 | |
5090 Expression *TypeTuple::getProperty(Loc loc, Identifier *ident) | |
5091 { Expression *e; | |
5092 | |
5093 #if LOGDOTEXP | |
5094 printf("TypeTuple::getProperty(type = '%s', ident = '%s')\n", toChars(), ident->toChars()); | |
5095 #endif | |
5096 if (ident == Id::length) | |
5097 { | |
5098 e = new IntegerExp(loc, arguments->dim, Type::tsize_t); | |
5099 } | |
5100 else | |
5101 { | |
5102 error(loc, "no property '%s' for tuple '%s'", ident->toChars(), toChars()); | |
5103 e = new IntegerExp(loc, 1, Type::tint32); | |
5104 } | |
5105 return e; | |
5106 } | |
5107 | |
5108 /***************************** TypeSlice *****************************/ | |
5109 | |
5110 /* This is so we can slice a TypeTuple */ | |
5111 | |
5112 TypeSlice::TypeSlice(Type *next, Expression *lwr, Expression *upr) | |
5113 : Type(Tslice, next) | |
5114 { | |
5115 //printf("TypeSlice[%s .. %s]\n", lwr->toChars(), upr->toChars()); | |
5116 this->lwr = lwr; | |
5117 this->upr = upr; | |
5118 } | |
5119 | |
5120 Type *TypeSlice::syntaxCopy() | |
5121 { | |
5122 Type *t = new TypeSlice(next->syntaxCopy(), lwr->syntaxCopy(), upr->syntaxCopy()); | |
5123 return t; | |
5124 } | |
5125 | |
5126 Type *TypeSlice::semantic(Loc loc, Scope *sc) | |
5127 { | |
5128 //printf("TypeSlice::semantic() %s\n", toChars()); | |
5129 next = next->semantic(loc, sc); | |
5130 //printf("next: %s\n", next->toChars()); | |
5131 | |
5132 Type *tbn = next->toBasetype(); | |
5133 if (tbn->ty != Ttuple) | |
5134 { error(loc, "can only slice tuple types, not %s", tbn->toChars()); | |
5135 return Type::terror; | |
5136 } | |
5137 TypeTuple *tt = (TypeTuple *)tbn; | |
5138 | |
5139 lwr = semanticLength(sc, tbn, lwr); | |
5140 lwr = lwr->optimize(WANTvalue); | |
5141 uinteger_t i1 = lwr->toUInteger(); | |
5142 | |
5143 upr = semanticLength(sc, tbn, upr); | |
5144 upr = upr->optimize(WANTvalue); | |
5145 uinteger_t i2 = upr->toUInteger(); | |
5146 | |
5147 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
|
5148 { error(loc, "slice [%llu..%llu] is out of range of [0..%u]", i1, i2, tt->arguments->dim); |
159 | 5149 return Type::terror; |
5150 } | |
5151 | |
5152 Arguments *args = new Arguments; | |
5153 args->reserve(i2 - i1); | |
5154 for (size_t i = i1; i < i2; i++) | |
5155 { Argument *arg = (Argument *)tt->arguments->data[i]; | |
5156 args->push(arg); | |
5157 } | |
5158 | |
5159 return new TypeTuple(args); | |
5160 } | |
5161 | |
5162 void TypeSlice::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps) | |
5163 { | |
5164 next->resolve(loc, sc, pe, pt, ps); | |
5165 if (*pe) | |
5166 { // It's really a slice expression | |
5167 Expression *e; | |
5168 e = new SliceExp(loc, *pe, lwr, upr); | |
5169 *pe = e; | |
5170 } | |
5171 else if (*ps) | |
5172 { Dsymbol *s = *ps; | |
5173 TupleDeclaration *td = s->isTupleDeclaration(); | |
5174 if (td) | |
5175 { | |
5176 /* It's a slice of a TupleDeclaration | |
5177 */ | |
5178 ScopeDsymbol *sym = new ArrayScopeSymbol(td); | |
5179 sym->parent = sc->scopesym; | |
5180 sc = sc->push(sym); | |
5181 | |
5182 lwr = lwr->semantic(sc); | |
5183 lwr = lwr->optimize(WANTvalue); | |
5184 uinteger_t i1 = lwr->toUInteger(); | |
5185 | |
5186 upr = upr->semantic(sc); | |
5187 upr = upr->optimize(WANTvalue); | |
5188 uinteger_t i2 = upr->toUInteger(); | |
5189 | |
5190 sc = sc->pop(); | |
5191 | |
5192 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
|
5193 { error(loc, "slice [%llu..%llu] is out of range of [0..%u]", i1, i2, td->objects->dim); |
159 | 5194 goto Ldefault; |
5195 } | |
5196 | |
5197 if (i1 == 0 && i2 == td->objects->dim) | |
5198 { | |
5199 *ps = td; | |
5200 return; | |
5201 } | |
5202 | |
5203 /* Create a new TupleDeclaration which | |
5204 * is a slice [i1..i2] out of the old one. | |
5205 */ | |
5206 Objects *objects = new Objects; | |
5207 objects->setDim(i2 - i1); | |
5208 for (size_t i = 0; i < objects->dim; i++) | |
5209 { | |
5210 objects->data[i] = td->objects->data[(size_t)i1 + i]; | |
5211 } | |
5212 | |
5213 TupleDeclaration *tds = new TupleDeclaration(loc, td->ident, objects); | |
5214 *ps = tds; | |
5215 } | |
5216 else | |
5217 goto Ldefault; | |
5218 } | |
5219 else | |
5220 { | |
5221 Ldefault: | |
5222 Type::resolve(loc, sc, pe, pt, ps); | |
5223 } | |
5224 } | |
5225 | |
5226 void TypeSlice::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod) | |
5227 { | |
5228 if (mod != this->mod) | |
5229 { toCBuffer3(buf, hgs, mod); | |
5230 return; | |
5231 } | |
5232 next->toCBuffer2(buf, hgs, this->mod); | |
5233 | |
5234 buf->printf("[%s .. ", lwr->toChars()); | |
5235 buf->printf("%s]", upr->toChars()); | |
5236 } | |
5237 | |
379
d632801b15f0
Introducing opaque type to dmd frontent to be used with certain runtime array
Christian Kamm <kamm incasoftware de>
parents:
378
diff
changeset
|
5238 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
|
5239 { |
d632801b15f0
Introducing opaque type to dmd frontent to be used with certain runtime array
Christian Kamm <kamm incasoftware de>
parents:
378
diff
changeset
|
5240 //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
|
5241 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
|
5242 { 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
|
5243 return; |
d632801b15f0
Introducing opaque type to dmd frontent to be used with certain runtime array
Christian Kamm <kamm incasoftware de>
parents:
378
diff
changeset
|
5244 } |
d632801b15f0
Introducing opaque type to dmd frontent to be used with certain runtime array
Christian Kamm <kamm incasoftware de>
parents:
378
diff
changeset
|
5245 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
|
5246 } |
d632801b15f0
Introducing opaque type to dmd frontent to be used with certain runtime array
Christian Kamm <kamm incasoftware de>
parents:
378
diff
changeset
|
5247 |
159 | 5248 /***************************** Argument *****************************/ |
5249 | |
5250 Argument::Argument(unsigned storageClass, Type *type, Identifier *ident, Expression *defaultArg) | |
5251 { | |
5252 this->type = type; | |
5253 this->ident = ident; | |
5254 this->storageClass = storageClass; | |
5255 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
|
5256 this->llvmAttrs = 0; |
159 | 5257 } |
5258 | |
5259 Argument *Argument::syntaxCopy() | |
5260 { | |
5261 Argument *a = new Argument(storageClass, | |
5262 type ? type->syntaxCopy() : NULL, | |
5263 ident, | |
5264 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
|
5265 a->llvmAttrs = llvmAttrs; |
159 | 5266 return a; |
5267 } | |
5268 | |
5269 Arguments *Argument::arraySyntaxCopy(Arguments *args) | |
5270 { Arguments *a = NULL; | |
5271 | |
5272 if (args) | |
5273 { | |
5274 a = new Arguments(); | |
5275 a->setDim(args->dim); | |
5276 for (size_t i = 0; i < a->dim; i++) | |
5277 { Argument *arg = (Argument *)args->data[i]; | |
5278 | |
5279 arg = arg->syntaxCopy(); | |
5280 a->data[i] = (void *)arg; | |
5281 } | |
5282 } | |
5283 return a; | |
5284 } | |
5285 | |
5286 char *Argument::argsTypesToChars(Arguments *args, int varargs) | |
5287 { OutBuffer *buf; | |
5288 | |
5289 buf = new OutBuffer(); | |
5290 | |
5291 buf->writeByte('('); | |
5292 if (args) | |
5293 { int i; | |
5294 OutBuffer argbuf; | |
5295 HdrGenState hgs; | |
5296 | |
5297 for (i = 0; i < args->dim; i++) | |
5298 { Argument *arg; | |
5299 | |
5300 if (i) | |
5301 buf->writeByte(','); | |
5302 arg = (Argument *)args->data[i]; | |
5303 argbuf.reset(); | |
5304 arg->type->toCBuffer2(&argbuf, &hgs, 0); | |
5305 buf->write(&argbuf); | |
5306 } | |
5307 if (varargs) | |
5308 { | |
5309 if (i && varargs == 1) | |
5310 buf->writeByte(','); | |
5311 buf->writestring("..."); | |
5312 } | |
5313 } | |
5314 buf->writeByte(')'); | |
5315 | |
5316 return buf->toChars(); | |
5317 } | |
5318 | |
5319 void Argument::argsToCBuffer(OutBuffer *buf, HdrGenState *hgs, Arguments *arguments, int varargs) | |
5320 { | |
5321 buf->writeByte('('); | |
5322 if (arguments) | |
5323 { int i; | |
5324 OutBuffer argbuf; | |
5325 | |
5326 for (i = 0; i < arguments->dim; i++) | |
5327 { Argument *arg; | |
5328 | |
5329 if (i) | |
5330 buf->writestring(", "); | |
5331 arg = (Argument *)arguments->data[i]; | |
5332 if (arg->storageClass & STCout) | |
5333 buf->writestring("out "); | |
5334 else if (arg->storageClass & STCref) | |
5335 buf->writestring((global.params.Dversion == 1) | |
5336 ? (char *)"inout " : (char *)"ref "); | |
5337 else if (arg->storageClass & STClazy) | |
5338 buf->writestring("lazy "); | |
5339 argbuf.reset(); | |
5340 arg->type->toCBuffer(&argbuf, arg->ident, hgs); | |
5341 if (arg->defaultArg) | |
5342 { | |
5343 argbuf.writestring(" = "); | |
5344 arg->defaultArg->toCBuffer(&argbuf, hgs); | |
5345 } | |
5346 buf->write(&argbuf); | |
5347 } | |
5348 if (varargs) | |
5349 { | |
5350 if (i && varargs == 1) | |
5351 buf->writeByte(','); | |
5352 buf->writestring("..."); | |
5353 } | |
5354 } | |
5355 buf->writeByte(')'); | |
5356 } | |
5357 | |
5358 | |
5359 void Argument::argsToDecoBuffer(OutBuffer *buf, Arguments *arguments) | |
5360 { | |
5361 //printf("Argument::argsToDecoBuffer()\n"); | |
5362 | |
5363 // Write argument types | |
5364 if (arguments) | |
5365 { | |
5366 size_t dim = Argument::dim(arguments); | |
5367 for (size_t i = 0; i < dim; i++) | |
5368 { | |
5369 Argument *arg = Argument::getNth(arguments, i); | |
5370 arg->toDecoBuffer(buf); | |
5371 } | |
5372 } | |
5373 } | |
5374 | |
5375 /**************************************************** | |
5376 * Determine if parameter is a lazy array of delegates. | |
5377 * If so, return the return type of those delegates. | |
5378 * If not, return NULL. | |
5379 */ | |
5380 | |
5381 Type *Argument::isLazyArray() | |
5382 { | |
5383 // if (inout == Lazy) | |
5384 { | |
5385 Type *tb = type->toBasetype(); | |
5386 if (tb->ty == Tsarray || tb->ty == Tarray) | |
5387 { | |
5388 Type *tel = tb->next->toBasetype(); | |
5389 if (tel->ty == Tdelegate) | |
5390 { | |
5391 TypeDelegate *td = (TypeDelegate *)tel; | |
5392 TypeFunction *tf = (TypeFunction *)td->next; | |
5393 | |
5394 if (!tf->varargs && Argument::dim(tf->parameters) == 0) | |
5395 { | |
5396 return tf->next; // return type of delegate | |
5397 } | |
5398 } | |
5399 } | |
5400 } | |
5401 return NULL; | |
5402 } | |
5403 | |
5404 void Argument::toDecoBuffer(OutBuffer *buf) | |
5405 { | |
5406 switch (storageClass & (STCin | STCout | STCref | STClazy)) | |
5407 { case 0: | |
5408 case STCin: | |
5409 break; | |
5410 case STCout: | |
5411 buf->writeByte('J'); | |
5412 break; | |
5413 case STCref: | |
5414 buf->writeByte('K'); | |
5415 break; | |
5416 case STClazy: | |
5417 buf->writeByte('L'); | |
5418 break; | |
5419 default: | |
5420 #ifdef DEBUG | |
5421 halt(); | |
5422 #endif | |
5423 assert(0); | |
5424 } | |
5425 type->toDecoBuffer(buf); | |
5426 } | |
5427 | |
5428 /*************************************** | |
5429 * Determine number of arguments, folding in tuples. | |
5430 */ | |
5431 | |
5432 size_t Argument::dim(Arguments *args) | |
5433 { | |
5434 size_t n = 0; | |
5435 if (args) | |
5436 { | |
5437 for (size_t i = 0; i < args->dim; i++) | |
5438 { Argument *arg = (Argument *)args->data[i]; | |
5439 Type *t = arg->type->toBasetype(); | |
5440 | |
5441 if (t->ty == Ttuple) | |
5442 { TypeTuple *tu = (TypeTuple *)t; | |
5443 n += dim(tu->arguments); | |
5444 } | |
5445 else | |
5446 n++; | |
5447 } | |
5448 } | |
5449 return n; | |
5450 } | |
5451 | |
5452 /*************************************** | |
5453 * Get nth Argument, folding in tuples. | |
5454 * Returns: | |
5455 * Argument* nth Argument | |
5456 * NULL not found, *pn gets incremented by the number | |
5457 * of Arguments | |
5458 */ | |
5459 | |
5460 Argument *Argument::getNth(Arguments *args, size_t nth, size_t *pn) | |
5461 { | |
5462 if (!args) | |
5463 return NULL; | |
5464 | |
5465 size_t n = 0; | |
5466 for (size_t i = 0; i < args->dim; i++) | |
5467 { Argument *arg = (Argument *)args->data[i]; | |
5468 Type *t = arg->type->toBasetype(); | |
5469 | |
5470 if (t->ty == Ttuple) | |
5471 { TypeTuple *tu = (TypeTuple *)t; | |
5472 arg = getNth(tu->arguments, nth - n, &n); | |
5473 if (arg) | |
5474 return arg; | |
5475 } | |
5476 else if (n == nth) | |
5477 return arg; | |
5478 else | |
5479 n++; | |
5480 } | |
5481 | |
5482 if (pn) | |
5483 *pn += n; | |
5484 return NULL; | |
5485 } |