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