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