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