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