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