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