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