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