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