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