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