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