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