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