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