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