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