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