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