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