Mercurial > projects > ldc
annotate dmd/expression.c @ 35:3cfcb944304e trunk
[svn r39] * Updated to DMD 1.022 with the exception of:
Bugzilla 278: dmd.conf search path doesn't work
This fix was causing crashes for me :/ So for it's the old behaviour
author | lindquist |
---|---|
date | Tue, 09 Oct 2007 06:21:30 +0200 |
parents | 788401029ecf |
children | 0b9b286b67b6 |
rev | line source |
---|---|
1 | 1 |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2007 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 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <ctype.h> | |
14 #include <assert.h> | |
15 #include <complex> | |
16 #include <math.h> | |
17 | |
18 #if _WIN32 && __DMC__ | |
19 extern "C" char * __cdecl __locale_decpoint; | |
20 #endif | |
21 | |
22 #if IN_GCC | |
23 // Issues with using -include total.h (defines integer_t) and then complex.h fails... | |
24 #undef integer_t | |
25 #endif | |
26 | |
27 #ifdef __APPLE__ | |
28 #define integer_t dmd_integer_t | |
29 #endif | |
30 | |
31 #if IN_GCC || IN_LLVM | |
32 #include "mem.h" | |
33 #elif _WIN32 | |
34 #include "..\root\mem.h" | |
35 #elif linux | |
36 #include "../root/mem.h" | |
37 #endif | |
38 | |
39 //#include "port.h" | |
40 #include "mtype.h" | |
41 #include "init.h" | |
42 #include "expression.h" | |
43 #include "template.h" | |
44 #include "utf.h" | |
45 #include "enum.h" | |
46 #include "scope.h" | |
47 #include "statement.h" | |
48 #include "declaration.h" | |
49 #include "aggregate.h" | |
50 #include "import.h" | |
51 #include "id.h" | |
52 #include "dsymbol.h" | |
53 #include "module.h" | |
54 #include "attrib.h" | |
55 #include "hdrgen.h" | |
56 #include "parse.h" | |
57 | |
58 Expression *createTypeInfoArray(Scope *sc, Expression *args[], int dim); | |
59 | |
60 #define LOGSEMANTIC 0 | |
61 | |
62 /********************************** | |
63 * Set operator precedence for each operator. | |
64 */ | |
65 | |
66 // Operator precedence - greater values are higher precedence | |
67 | |
68 enum PREC | |
69 { | |
70 PREC_zero, | |
71 PREC_expr, | |
72 PREC_assign, | |
73 PREC_cond, | |
74 PREC_oror, | |
75 PREC_andand, | |
76 PREC_or, | |
77 PREC_xor, | |
78 PREC_and, | |
79 PREC_equal, | |
80 PREC_rel, | |
81 PREC_shift, | |
82 PREC_add, | |
83 PREC_mul, | |
84 PREC_unary, | |
85 PREC_primary, | |
86 }; | |
87 | |
88 enum PREC precedence[TOKMAX]; | |
89 | |
90 void initPrecedence() | |
91 { | |
92 precedence[TOKimport] = PREC_primary; | |
93 precedence[TOKidentifier] = PREC_primary; | |
94 precedence[TOKthis] = PREC_primary; | |
95 precedence[TOKsuper] = PREC_primary; | |
96 precedence[TOKint64] = PREC_primary; | |
97 precedence[TOKfloat64] = PREC_primary; | |
98 precedence[TOKnull] = PREC_primary; | |
99 precedence[TOKstring] = PREC_primary; | |
100 precedence[TOKarrayliteral] = PREC_primary; | |
101 precedence[TOKtypedot] = PREC_primary; | |
102 precedence[TOKtypeid] = PREC_primary; | |
103 precedence[TOKis] = PREC_primary; | |
104 precedence[TOKassert] = PREC_primary; | |
105 precedence[TOKfunction] = PREC_primary; | |
106 precedence[TOKvar] = PREC_primary; | |
107 | |
108 // post | |
109 precedence[TOKdotti] = PREC_primary; | |
110 precedence[TOKdot] = PREC_primary; | |
111 // precedence[TOKarrow] = PREC_primary; | |
112 precedence[TOKplusplus] = PREC_primary; | |
113 precedence[TOKminusminus] = PREC_primary; | |
114 precedence[TOKcall] = PREC_primary; | |
115 precedence[TOKslice] = PREC_primary; | |
116 precedence[TOKarray] = PREC_primary; | |
117 | |
118 precedence[TOKaddress] = PREC_unary; | |
119 precedence[TOKstar] = PREC_unary; | |
120 precedence[TOKneg] = PREC_unary; | |
121 precedence[TOKuadd] = PREC_unary; | |
122 precedence[TOKnot] = PREC_unary; | |
123 precedence[TOKtobool] = PREC_add; | |
124 precedence[TOKtilde] = PREC_unary; | |
125 precedence[TOKdelete] = PREC_unary; | |
126 precedence[TOKnew] = PREC_unary; | |
127 precedence[TOKcast] = PREC_unary; | |
128 | |
129 precedence[TOKmul] = PREC_mul; | |
130 precedence[TOKdiv] = PREC_mul; | |
131 precedence[TOKmod] = PREC_mul; | |
132 | |
133 precedence[TOKadd] = PREC_add; | |
134 precedence[TOKmin] = PREC_add; | |
135 precedence[TOKcat] = PREC_add; | |
136 | |
137 precedence[TOKshl] = PREC_shift; | |
138 precedence[TOKshr] = PREC_shift; | |
139 precedence[TOKushr] = PREC_shift; | |
140 | |
141 precedence[TOKlt] = PREC_rel; | |
142 precedence[TOKle] = PREC_rel; | |
143 precedence[TOKgt] = PREC_rel; | |
144 precedence[TOKge] = PREC_rel; | |
145 precedence[TOKunord] = PREC_rel; | |
146 precedence[TOKlg] = PREC_rel; | |
147 precedence[TOKleg] = PREC_rel; | |
148 precedence[TOKule] = PREC_rel; | |
149 precedence[TOKul] = PREC_rel; | |
150 precedence[TOKuge] = PREC_rel; | |
151 precedence[TOKug] = PREC_rel; | |
152 precedence[TOKue] = PREC_rel; | |
153 precedence[TOKin] = PREC_rel; | |
154 | |
155 precedence[TOKequal] = PREC_equal; | |
156 precedence[TOKnotequal] = PREC_equal; | |
157 precedence[TOKidentity] = PREC_equal; | |
158 precedence[TOKnotidentity] = PREC_equal; | |
159 | |
160 precedence[TOKand] = PREC_and; | |
161 | |
162 precedence[TOKxor] = PREC_xor; | |
163 | |
164 precedence[TOKor] = PREC_or; | |
165 | |
166 precedence[TOKandand] = PREC_andand; | |
167 | |
168 precedence[TOKoror] = PREC_oror; | |
169 | |
170 precedence[TOKquestion] = PREC_cond; | |
171 | |
172 precedence[TOKassign] = PREC_assign; | |
173 precedence[TOKaddass] = PREC_assign; | |
174 precedence[TOKminass] = PREC_assign; | |
175 precedence[TOKcatass] = PREC_assign; | |
176 precedence[TOKmulass] = PREC_assign; | |
177 precedence[TOKdivass] = PREC_assign; | |
178 precedence[TOKmodass] = PREC_assign; | |
179 precedence[TOKshlass] = PREC_assign; | |
180 precedence[TOKshrass] = PREC_assign; | |
181 precedence[TOKushrass] = PREC_assign; | |
182 precedence[TOKandass] = PREC_assign; | |
183 precedence[TOKorass] = PREC_assign; | |
184 precedence[TOKxorass] = PREC_assign; | |
185 | |
186 precedence[TOKcomma] = PREC_expr; | |
187 } | |
188 | |
189 /***************************************** | |
190 * Determine if 'this' is available. | |
191 * If it is, return the FuncDeclaration that has it. | |
192 */ | |
193 | |
194 FuncDeclaration *hasThis(Scope *sc) | |
195 { FuncDeclaration *fd; | |
196 FuncDeclaration *fdthis; | |
197 | |
198 //printf("hasThis()\n"); | |
199 fdthis = sc->parent->isFuncDeclaration(); | |
200 //printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis->toChars() : ""); | |
201 | |
202 // Go upwards until we find the enclosing member function | |
203 fd = fdthis; | |
204 while (1) | |
205 { | |
206 if (!fd) | |
207 { | |
208 goto Lno; | |
209 } | |
210 if (!fd->isNested()) | |
211 break; | |
212 | |
213 Dsymbol *parent = fd->parent; | |
214 while (parent) | |
215 { | |
216 TemplateInstance *ti = parent->isTemplateInstance(); | |
217 if (ti) | |
218 parent = ti->parent; | |
219 else | |
220 break; | |
221 } | |
222 | |
223 fd = fd->parent->isFuncDeclaration(); | |
224 } | |
225 | |
226 if (!fd->isThis()) | |
227 { //printf("test '%s'\n", fd->toChars()); | |
228 goto Lno; | |
229 } | |
230 | |
231 assert(fd->vthis); | |
232 return fd; | |
233 | |
234 Lno: | |
235 return NULL; // don't have 'this' available | |
236 } | |
237 | |
238 | |
239 /*************************************** | |
240 * Pull out any properties. | |
241 */ | |
242 | |
243 Expression *resolveProperties(Scope *sc, Expression *e) | |
244 { | |
245 //printf("resolveProperties(%s)\n", e->toChars()); | |
246 if (e->type) | |
247 { | |
248 Type *t = e->type->toBasetype(); | |
249 | |
250 if (t->ty == Tfunction) | |
251 { | |
252 e = new CallExp(e->loc, e); | |
253 e = e->semantic(sc); | |
254 } | |
255 | |
256 /* Look for e being a lazy parameter; rewrite as delegate call | |
257 */ | |
258 else if (e->op == TOKvar) | |
259 { VarExp *ve = (VarExp *)e; | |
260 | |
261 if (ve->var->storage_class & STClazy) | |
262 { | |
263 e = new CallExp(e->loc, e); | |
264 e = e->semantic(sc); | |
265 } | |
266 } | |
267 | |
268 else if (e->op == TOKdotexp) | |
269 { | |
270 e->error("expression has no value"); | |
271 } | |
272 } | |
273 return e; | |
274 } | |
275 | |
276 /****************************** | |
277 * Perform semantic() on an array of Expressions. | |
278 */ | |
279 | |
280 void arrayExpressionSemantic(Expressions *exps, Scope *sc) | |
281 { | |
282 if (exps) | |
283 { | |
284 for (size_t i = 0; i < exps->dim; i++) | |
285 { Expression *e = (Expression *)exps->data[i]; | |
286 | |
287 e = e->semantic(sc); | |
288 exps->data[i] = (void *)e; | |
289 } | |
290 } | |
291 } | |
292 | |
293 /**************************************** | |
294 * Expand tuples. | |
295 */ | |
296 | |
297 void expandTuples(Expressions *exps) | |
298 { | |
299 //printf("expandTuples()\n"); | |
300 if (exps) | |
301 { | |
302 for (size_t i = 0; i < exps->dim; i++) | |
303 { Expression *arg = (Expression *)exps->data[i]; | |
304 if (!arg) | |
305 continue; | |
306 | |
307 // Look for tuple with 0 members | |
308 if (arg->op == TOKtype) | |
309 { TypeExp *e = (TypeExp *)arg; | |
310 if (e->type->toBasetype()->ty == Ttuple) | |
311 { TypeTuple *tt = (TypeTuple *)e->type->toBasetype(); | |
312 | |
313 if (!tt->arguments || tt->arguments->dim == 0) | |
314 { | |
315 exps->remove(i); | |
316 if (i == exps->dim) | |
317 return; | |
318 i--; | |
319 continue; | |
320 } | |
321 } | |
322 } | |
323 | |
324 // Inline expand all the tuples | |
325 while (arg->op == TOKtuple) | |
326 { TupleExp *te = (TupleExp *)arg; | |
327 | |
328 exps->remove(i); // remove arg | |
329 exps->insert(i, te->exps); // replace with tuple contents | |
330 if (i == exps->dim) | |
331 return; // empty tuple, no more arguments | |
332 arg = (Expression *)exps->data[i]; | |
333 } | |
334 } | |
335 } | |
336 } | |
337 | |
338 /**************************************** | |
339 * Preprocess arguments to function. | |
340 */ | |
341 | |
342 void preFunctionArguments(Loc loc, Scope *sc, Expressions *exps) | |
343 { | |
344 if (exps) | |
345 { | |
346 expandTuples(exps); | |
347 | |
348 for (size_t i = 0; i < exps->dim; i++) | |
349 { Expression *arg = (Expression *)exps->data[i]; | |
350 | |
351 if (!arg->type) | |
352 { | |
353 #ifdef DEBUG | |
354 if (!global.gag) | |
355 printf("1: \n"); | |
356 #endif | |
357 arg->error("%s is not an expression", arg->toChars()); | |
358 arg = new IntegerExp(arg->loc, 0, Type::tint32); | |
359 } | |
360 | |
361 arg = resolveProperties(sc, arg); | |
362 exps->data[i] = (void *) arg; | |
363 | |
364 //arg->rvalue(); | |
365 #if 0 | |
366 if (arg->type->ty == Tfunction) | |
367 { | |
368 arg = new AddrExp(arg->loc, arg); | |
369 arg = arg->semantic(sc); | |
370 exps->data[i] = (void *) arg; | |
371 } | |
372 #endif | |
373 } | |
374 } | |
375 } | |
376 | |
377 | |
378 /**************************************** | |
379 * Now that we know the exact type of the function we're calling, | |
380 * the arguments[] need to be adjusted: | |
381 * 1) implicitly convert argument to the corresponding parameter type | |
382 * 2) add default arguments for any missing arguments | |
383 * 3) do default promotions on arguments corresponding to ... | |
384 * 4) add hidden _arguments[] argument | |
385 */ | |
386 | |
387 void functionArguments(Loc loc, Scope *sc, TypeFunction *tf, Expressions *arguments) | |
388 { | |
389 unsigned n; | |
390 int done; | |
391 Type *tb; | |
392 | |
393 //printf("functionArguments()\n"); | |
394 assert(arguments); | |
395 size_t nargs = arguments ? arguments->dim : 0; | |
396 size_t nparams = Argument::dim(tf->parameters); | |
397 | |
398 if (nargs > nparams && tf->varargs == 0) | |
399 error(loc, "expected %zu arguments, not %zu", nparams, nargs); | |
400 | |
401 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) | |
402 | |
403 done = 0; | |
404 for (size_t i = 0; i < n; i++) | |
405 { | |
406 Expression *arg; | |
407 | |
408 if (i < nargs) | |
409 arg = (Expression *)arguments->data[i]; | |
410 else | |
411 arg = NULL; | |
412 | |
413 if (i < nparams) | |
414 { | |
415 Argument *p = Argument::getNth(tf->parameters, i); | |
416 | |
417 if (!arg) | |
418 { | |
419 if (!p->defaultArg) | |
420 { | |
421 if (tf->varargs == 2 && i + 1 == nparams) | |
422 goto L2; | |
423 error(loc, "expected %zu arguments, not %zu", nparams, nargs); | |
424 break; | |
425 } | |
426 arg = p->defaultArg->copy(); | |
427 arguments->push(arg); | |
428 nargs++; | |
429 } | |
430 | |
431 if (tf->varargs == 2 && i + 1 == nparams) | |
432 { | |
433 //printf("\t\tvarargs == 2, p->type = '%s'\n", p->type->toChars()); | |
434 if (arg->implicitConvTo(p->type)) | |
435 { | |
436 if (nargs != nparams) | |
437 error(loc, "expected %zu arguments, not %zu", nparams, nargs); | |
438 goto L1; | |
439 } | |
440 L2: | |
441 Type *tb = p->type->toBasetype(); | |
442 Type *tret = p->isLazyArray(); | |
443 switch (tb->ty) | |
444 { | |
445 case Tsarray: | |
446 case Tarray: | |
447 { // Create a static array variable v of type arg->type | |
448 #ifdef IN_GCC | |
449 /* GCC 4.0 does not like zero length arrays used like | |
450 this; pass a null array value instead. Could also | |
451 just make a one-element array. */ | |
452 if (nargs - i == 0) | |
453 { | |
454 arg = new NullExp(loc); | |
455 break; | |
456 } | |
457 #endif | |
458 static int idn; | |
459 char name[10 + sizeof(idn)*3 + 1]; | |
460 sprintf(name, "__arrayArg%d", ++idn); | |
461 Identifier *id = Lexer::idPool(name); | |
462 Type *t = new TypeSArray(tb->next, new IntegerExp(nargs - i)); | |
463 t = t->semantic(loc, sc); | |
464 VarDeclaration *v = new VarDeclaration(loc, t, id, new VoidInitializer(loc)); | |
465 v->semantic(sc); | |
466 v->parent = sc->parent; | |
467 //sc->insert(v); | |
468 | |
469 Expression *c = new DeclarationExp(0, v); | |
470 c->type = v->type; | |
471 | |
472 for (size_t u = i; u < nargs; u++) | |
473 { Expression *a = (Expression *)arguments->data[u]; | |
474 if (tret && !tb->next->equals(a->type)) | |
475 a = a->toDelegate(sc, tret); | |
476 | |
477 Expression *e = new VarExp(loc, v); | |
478 e = new IndexExp(loc, e, new IntegerExp(u + 1 - nparams)); | |
479 e = new AssignExp(loc, e, a); | |
480 if (c) | |
481 c = new CommaExp(loc, c, e); | |
482 else | |
483 c = e; | |
484 } | |
485 arg = new VarExp(loc, v); | |
486 if (c) | |
487 arg = new CommaExp(loc, c, arg); | |
488 break; | |
489 } | |
490 case Tclass: | |
491 { /* Set arg to be: | |
492 * new Tclass(arg0, arg1, ..., argn) | |
493 */ | |
494 Expressions *args = new Expressions(); | |
495 args->setDim(nargs - i); | |
496 for (size_t u = i; u < nargs; u++) | |
497 args->data[u - i] = arguments->data[u]; | |
498 arg = new NewExp(loc, NULL, NULL, p->type, args); | |
499 break; | |
500 } | |
501 default: | |
502 if (!arg) | |
503 { error(loc, "not enough arguments"); | |
504 return; | |
505 } | |
506 break; | |
507 } | |
508 arg = arg->semantic(sc); | |
509 //printf("\targ = '%s'\n", arg->toChars()); | |
510 arguments->setDim(i + 1); | |
511 done = 1; | |
512 } | |
513 | |
514 L1: | |
515 if (!(p->storageClass & STClazy && p->type->ty == Tvoid)) | |
516 arg = arg->implicitCastTo(sc, p->type); | |
517 if (p->storageClass & (STCout | STCref)) | |
518 { | |
519 // BUG: should check that argument to ref is type 'invariant' | |
520 // BUG: assignments to ref should also be type 'invariant' | |
521 arg = arg->modifiableLvalue(sc, NULL); | |
522 | |
523 //if (arg->op == TOKslice) | |
524 //arg->error("cannot modify slice %s", arg->toChars()); | |
525 } | |
526 | |
527 // Convert static arrays to pointers | |
528 tb = arg->type->toBasetype(); | |
529 if (tb->ty == Tsarray) | |
530 { | |
531 arg = arg->checkToPointer(); | |
532 } | |
533 | |
534 // Convert lazy argument to a delegate | |
535 if (p->storageClass & STClazy) | |
536 { | |
537 arg = arg->toDelegate(sc, p->type); | |
538 } | |
539 } | |
540 else | |
541 { | |
542 | |
543 // If not D linkage, do promotions | |
544 if (tf->linkage != LINKd) | |
545 { | |
546 // Promote bytes, words, etc., to ints | |
547 arg = arg->integralPromotions(sc); | |
548 | |
549 // Promote floats to doubles | |
550 switch (arg->type->ty) | |
551 { | |
552 case Tfloat32: | |
553 arg = arg->castTo(sc, Type::tfloat64); | |
554 break; | |
555 | |
556 case Timaginary32: | |
557 arg = arg->castTo(sc, Type::timaginary64); | |
558 break; | |
559 } | |
560 } | |
561 | |
562 // Convert static arrays to dynamic arrays | |
563 tb = arg->type->toBasetype(); | |
564 if (tb->ty == Tsarray) | |
565 { TypeSArray *ts = (TypeSArray *)tb; | |
566 Type *ta = tb->next->arrayOf(); | |
567 if (ts->size(arg->loc) == 0) | |
568 { arg = new NullExp(arg->loc); | |
569 arg->type = ta; | |
570 } | |
571 else | |
572 arg = arg->castTo(sc, ta); | |
573 } | |
574 | |
575 arg->rvalue(); | |
576 } | |
577 arg = arg->optimize(WANTvalue); | |
578 arguments->data[i] = (void *) arg; | |
579 if (done) | |
580 break; | |
581 } | |
582 | |
583 // If D linkage and variadic, add _arguments[] as first argument | |
584 if (tf->linkage == LINKd && tf->varargs == 1) | |
585 { | |
586 Expression *e; | |
587 | |
588 e = createTypeInfoArray(sc, (Expression **)&arguments->data[nparams], | |
589 arguments->dim - nparams); | |
590 arguments->insert(0, e); | |
591 } | |
592 } | |
593 | |
594 /************************************************** | |
595 * Write expression out to buf, but wrap it | |
596 * in ( ) if its precedence is less than pr. | |
597 */ | |
598 | |
599 void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, enum PREC pr) | |
600 { | |
601 if (precedence[e->op] < pr) | |
602 { | |
603 buf->writeByte('('); | |
604 e->toCBuffer(buf, hgs); | |
605 buf->writeByte(')'); | |
606 } | |
607 else | |
608 e->toCBuffer(buf, hgs); | |
609 } | |
610 | |
611 /************************************************** | |
612 * Write out argument list to buf. | |
613 */ | |
614 | |
615 void argsToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
616 { | |
617 if (arguments) | |
618 { | |
619 for (size_t i = 0; i < arguments->dim; i++) | |
620 { Expression *arg = (Expression *)arguments->data[i]; | |
621 | |
19 | 622 if (arg) |
623 { if (i) | |
624 buf->writeByte(','); | |
625 expToCBuffer(buf, hgs, arg, PREC_assign); | |
626 } | |
1 | 627 } |
628 } | |
629 } | |
630 | |
631 /************************************************** | |
632 * Write out argument types to buf. | |
633 */ | |
634 | |
635 void argExpTypesToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
636 { | |
637 if (arguments) | |
638 { OutBuffer argbuf; | |
639 | |
640 for (size_t i = 0; i < arguments->dim; i++) | |
641 { Expression *arg = (Expression *)arguments->data[i]; | |
642 | |
643 if (i) | |
644 buf->writeByte(','); | |
645 argbuf.reset(); | |
646 arg->type->toCBuffer2(&argbuf, NULL, hgs); | |
647 buf->write(&argbuf); | |
648 } | |
649 } | |
650 } | |
651 | |
652 /******************************** Expression **************************/ | |
653 | |
654 Expression::Expression(Loc loc, enum TOK op, int size) | |
655 : loc(loc) | |
656 { | |
657 this->loc = loc; | |
658 this->op = op; | |
659 this->size = size; | |
660 type = NULL; | |
661 } | |
662 | |
663 Expression *Expression::syntaxCopy() | |
664 { | |
665 //printf("Expression::syntaxCopy()\n"); | |
666 //dump(0); | |
667 return copy(); | |
668 } | |
669 | |
670 /********************************* | |
671 * Does *not* do a deep copy. | |
672 */ | |
673 | |
674 Expression *Expression::copy() | |
675 { | |
676 Expression *e; | |
677 if (!size) | |
678 { | |
679 #ifdef DEBUG | |
680 fprintf(stdmsg, "No expression copy for: %s\n", toChars()); | |
681 printf("op = %d\n", op); | |
682 dump(0); | |
683 #endif | |
684 assert(0); | |
685 } | |
686 e = (Expression *)mem.malloc(size); | |
687 return (Expression *)memcpy(e, this, size); | |
688 } | |
689 | |
690 /************************** | |
691 * Semantically analyze Expression. | |
692 * Determine types, fold constants, etc. | |
693 */ | |
694 | |
695 Expression *Expression::semantic(Scope *sc) | |
696 { | |
697 #if LOGSEMANTIC | |
698 printf("Expression::semantic()\n"); | |
699 #endif | |
700 if (type) | |
701 type = type->semantic(loc, sc); | |
702 else | |
703 type = Type::tvoid; | |
704 return this; | |
705 } | |
706 | |
707 void Expression::print() | |
708 { | |
709 fprintf(stdmsg, "%s\n", toChars()); | |
710 fflush(stdmsg); | |
711 } | |
712 | |
713 char *Expression::toChars() | |
714 { OutBuffer *buf; | |
715 HdrGenState hgs; | |
716 | |
717 memset(&hgs, 0, sizeof(hgs)); | |
718 buf = new OutBuffer(); | |
719 toCBuffer(buf, &hgs); | |
720 return buf->toChars(); | |
721 } | |
722 | |
723 void Expression::error(const char *format, ...) | |
724 { | |
725 va_list ap; | |
726 va_start(ap, format); | |
727 ::verror(loc, format, ap); | |
728 va_end( ap ); | |
729 } | |
730 | |
731 void Expression::rvalue() | |
732 { | |
733 if (type && type->toBasetype()->ty == Tvoid) | |
734 { error("expression %s is void and has no value", toChars()); | |
735 #if 0 | |
736 dump(0); | |
737 halt(); | |
738 #endif | |
739 } | |
740 } | |
741 | |
742 Expression *Expression::combine(Expression *e1, Expression *e2) | |
743 { | |
744 if (e1) | |
745 { | |
746 if (e2) | |
747 { | |
748 e1 = new CommaExp(e1->loc, e1, e2); | |
749 e1->type = e2->type; | |
750 } | |
751 } | |
752 else | |
753 e1 = e2; | |
754 return e1; | |
755 } | |
756 | |
757 integer_t Expression::toInteger() | |
758 { | |
759 //printf("Expression %s\n", Token::toChars(op)); | |
760 error("Integer constant expression expected instead of %s", toChars()); | |
761 return 0; | |
762 } | |
763 | |
764 uinteger_t Expression::toUInteger() | |
765 { | |
766 //printf("Expression %s\n", Token::toChars(op)); | |
767 return (uinteger_t)toInteger(); | |
768 } | |
769 | |
770 real_t Expression::toReal() | |
771 { | |
772 error("Floating point constant expression expected instead of %s", toChars()); | |
773 return 0; | |
774 } | |
775 | |
776 real_t Expression::toImaginary() | |
777 { | |
778 error("Floating point constant expression expected instead of %s", toChars()); | |
779 return 0; | |
780 } | |
781 | |
782 complex_t Expression::toComplex() | |
783 { | |
784 error("Floating point constant expression expected instead of %s", toChars()); | |
785 #ifdef IN_GCC | |
786 return complex_t(real_t(0)); // %% nicer | |
787 #else | |
788 return 0; | |
789 #endif | |
790 } | |
791 | |
792 void Expression::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
793 { | |
794 buf->writestring(Token::toChars(op)); | |
795 } | |
796 | |
797 void Expression::toMangleBuffer(OutBuffer *buf) | |
798 { | |
799 error("expression %s is not a valid template value argument", toChars()); | |
800 } | |
801 | |
802 /******************************* | |
803 * Give error if we're not an lvalue. | |
804 * If we can, convert expression to be an lvalue. | |
805 */ | |
806 | |
807 Expression *Expression::toLvalue(Scope *sc, Expression *e) | |
808 { | |
809 if (!e) | |
810 e = this; | |
811 else if (!loc.filename) | |
812 loc = e->loc; | |
813 error("%s is not an lvalue", e->toChars()); | |
814 return this; | |
815 } | |
816 | |
817 Expression *Expression::modifiableLvalue(Scope *sc, Expression *e) | |
818 { | |
819 // See if this expression is a modifiable lvalue (i.e. not const) | |
820 return toLvalue(sc, e); | |
821 } | |
822 | |
823 /************************************ | |
824 * Detect cases where pointers to the stack can 'escape' the | |
825 * lifetime of the stack frame. | |
826 */ | |
827 | |
828 void Expression::checkEscape() | |
829 { | |
830 } | |
831 | |
832 void Expression::checkScalar() | |
833 { | |
834 if (!type->isscalar()) | |
835 error("'%s' is not a scalar, it is a %s", toChars(), type->toChars()); | |
836 } | |
837 | |
838 void Expression::checkNoBool() | |
839 { | |
840 if (type->toBasetype()->ty == Tbool) | |
841 error("operation not allowed on bool '%s'", toChars()); | |
842 } | |
843 | |
844 Expression *Expression::checkIntegral() | |
845 { | |
846 if (!type->isintegral()) | |
847 { error("'%s' is not of integral type, it is a %s", toChars(), type->toChars()); | |
848 return new IntegerExp(0); | |
849 } | |
850 return this; | |
851 } | |
852 | |
853 void Expression::checkArithmetic() | |
854 { | |
855 if (!type->isintegral() && !type->isfloating()) | |
856 error("'%s' is not an arithmetic type", toChars()); | |
857 } | |
858 | |
859 void Expression::checkDeprecated(Scope *sc, Dsymbol *s) | |
860 { | |
861 s->checkDeprecated(loc, sc); | |
862 } | |
863 | |
864 /******************************** | |
865 * Check for expressions that have no use. | |
866 * Input: | |
867 * flag 0 not going to use the result, so issue error message if no | |
868 * side effects | |
869 * 1 the result of the expression is used, but still check | |
870 * for useless subexpressions | |
871 * 2 do not issue error messages, just return !=0 if expression | |
872 * has side effects | |
873 */ | |
874 | |
875 int Expression::checkSideEffect(int flag) | |
876 { | |
877 if (flag == 0) | |
878 { if (op == TOKimport) | |
879 { | |
880 error("%s has no effect", toChars()); | |
881 } | |
882 else | |
883 error("%s has no effect in expression (%s)", | |
884 Token::toChars(op), toChars()); | |
885 } | |
886 return 0; | |
887 } | |
888 | |
889 /***************************** | |
890 * Check that expression can be tested for true or false. | |
891 */ | |
892 | |
893 Expression *Expression::checkToBoolean() | |
894 { | |
895 // Default is 'yes' - do nothing | |
896 | |
897 #ifdef DEBUG | |
898 if (!type) | |
899 dump(0); | |
900 #endif | |
901 | |
902 if (!type->checkBoolean()) | |
903 { | |
904 error("expression %s of type %s does not have a boolean value", toChars(), type->toChars()); | |
905 } | |
906 return this; | |
907 } | |
908 | |
909 /**************************** | |
910 */ | |
911 | |
912 Expression *Expression::checkToPointer() | |
913 { | |
914 Expression *e; | |
915 Type *tb; | |
916 | |
917 //printf("Expression::checkToPointer()\n"); | |
918 e = this; | |
919 | |
920 // If C static array, convert to pointer | |
921 tb = type->toBasetype(); | |
922 if (tb->ty == Tsarray) | |
923 { TypeSArray *ts = (TypeSArray *)tb; | |
924 if (ts->size(loc) == 0) | |
925 e = new NullExp(loc); | |
926 else | |
927 e = new AddrExp(loc, this); | |
928 e->type = tb->next->pointerTo(); | |
929 } | |
930 return e; | |
931 } | |
932 | |
933 /****************************** | |
934 * Take address of expression. | |
935 */ | |
936 | |
937 Expression *Expression::addressOf(Scope *sc) | |
938 { | |
939 Expression *e; | |
940 | |
941 //printf("Expression::addressOf()\n"); | |
942 e = toLvalue(sc, NULL); | |
943 e = new AddrExp(loc, e); | |
944 e->type = type->pointerTo(); | |
945 return e; | |
946 } | |
947 | |
948 /****************************** | |
949 * If this is a reference, dereference it. | |
950 */ | |
951 | |
952 Expression *Expression::deref() | |
953 { | |
954 //printf("Expression::deref()\n"); | |
955 if (type->ty == Treference) | |
956 { Expression *e; | |
957 | |
958 e = new PtrExp(loc, this); | |
959 e->type = type->next; | |
960 return e; | |
961 } | |
962 return this; | |
963 } | |
964 | |
965 /******************************** | |
966 * Does this expression statically evaluate to a boolean TRUE or FALSE? | |
967 */ | |
968 | |
969 int Expression::isBool(int result) | |
970 { | |
971 return FALSE; | |
972 } | |
973 | |
974 /******************************** | |
975 * Does this expression result in either a 1 or a 0? | |
976 */ | |
977 | |
978 int Expression::isBit() | |
979 { | |
980 return FALSE; | |
981 } | |
982 | |
983 Expressions *Expression::arraySyntaxCopy(Expressions *exps) | |
984 { Expressions *a = NULL; | |
985 | |
986 if (exps) | |
987 { | |
988 a = new Expressions(); | |
989 a->setDim(exps->dim); | |
990 for (int i = 0; i < a->dim; i++) | |
991 { Expression *e = (Expression *)exps->data[i]; | |
992 | |
993 e = e->syntaxCopy(); | |
994 a->data[i] = e; | |
995 } | |
996 } | |
997 return a; | |
998 } | |
999 | |
1000 /******************************** IntegerExp **************************/ | |
1001 | |
1002 IntegerExp::IntegerExp(Loc loc, integer_t value, Type *type) | |
1003 : Expression(loc, TOKint64, sizeof(IntegerExp)) | |
1004 { | |
1005 //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type->toChars() : ""); | |
1006 if (type && !type->isscalar()) | |
1007 { | |
1008 error("integral constant must be scalar type, not %s", type->toChars()); | |
1009 type = Type::terror; | |
1010 } | |
1011 this->type = type; | |
1012 this->value = value; | |
1013 } | |
1014 | |
1015 IntegerExp::IntegerExp(integer_t value) | |
1016 : Expression(0, TOKint64, sizeof(IntegerExp)) | |
1017 { | |
1018 this->type = Type::tint32; | |
1019 this->value = value; | |
1020 } | |
1021 | |
1022 int IntegerExp::equals(Object *o) | |
1023 { IntegerExp *ne; | |
1024 | |
1025 if (this == o || | |
1026 (((Expression *)o)->op == TOKint64 && | |
1027 ((ne = (IntegerExp *)o), type->equals(ne->type)) && | |
1028 value == ne->value)) | |
1029 return 1; | |
1030 return 0; | |
1031 } | |
1032 | |
1033 char *IntegerExp::toChars() | |
1034 { | |
1035 #if 1 | |
1036 return Expression::toChars(); | |
1037 #else | |
1038 static char buffer[sizeof(value) * 3 + 1]; | |
1039 | |
1040 sprintf(buffer, "%jd", value); | |
1041 return buffer; | |
1042 #endif | |
1043 } | |
1044 | |
1045 integer_t IntegerExp::toInteger() | |
1046 { Type *t; | |
1047 | |
1048 t = type; | |
1049 while (t) | |
1050 { | |
1051 switch (t->ty) | |
1052 { | |
1053 case Tbit: | |
1054 case Tbool: value = (value != 0); break; | |
1055 case Tint8: value = (d_int8) value; break; | |
1056 case Tchar: | |
1057 case Tuns8: value = (d_uns8) value; break; | |
1058 case Tint16: value = (d_int16) value; break; | |
1059 case Twchar: | |
1060 case Tuns16: value = (d_uns16) value; break; | |
1061 case Tint32: value = (d_int32) value; break; | |
1062 case Tpointer: | |
1063 case Tdchar: | |
1064 case Tuns32: value = (d_uns32) value; break; | |
1065 case Tint64: value = (d_int64) value; break; | |
1066 case Tuns64: value = (d_uns64) value; break; | |
1067 | |
1068 case Tenum: | |
1069 { | |
1070 TypeEnum *te = (TypeEnum *)t; | |
1071 t = te->sym->memtype; | |
1072 continue; | |
1073 } | |
1074 | |
1075 case Ttypedef: | |
1076 { | |
1077 TypeTypedef *tt = (TypeTypedef *)t; | |
1078 t = tt->sym->basetype; | |
1079 continue; | |
1080 } | |
1081 | |
1082 default: | |
1083 print(); | |
1084 type->print(); | |
1085 assert(0); | |
1086 break; | |
1087 } | |
1088 break; | |
1089 } | |
1090 return value; | |
1091 } | |
1092 | |
1093 real_t IntegerExp::toReal() | |
1094 { | |
1095 Type *t; | |
1096 | |
1097 toInteger(); | |
1098 t = type->toBasetype(); | |
1099 if (t->ty == Tuns64) | |
1100 return (real_t)(d_uns64)value; | |
1101 else | |
1102 return (real_t)(d_int64)value; | |
1103 } | |
1104 | |
1105 real_t IntegerExp::toImaginary() | |
1106 { | |
1107 return (real_t) 0; | |
1108 } | |
1109 | |
1110 complex_t IntegerExp::toComplex() | |
1111 { | |
1112 return toReal(); | |
1113 } | |
1114 | |
1115 int IntegerExp::isBool(int result) | |
1116 { | |
1117 return result ? value != 0 : value == 0; | |
1118 } | |
1119 | |
1120 Expression *IntegerExp::semantic(Scope *sc) | |
1121 { | |
1122 if (!type) | |
1123 { | |
1124 // Determine what the type of this number is | |
1125 integer_t number = value; | |
1126 | |
1127 if (number & 0x8000000000000000LL) | |
1128 type = Type::tuns64; | |
1129 else if (number & 0xFFFFFFFF80000000LL) | |
1130 type = Type::tint64; | |
1131 else | |
1132 type = Type::tint32; | |
1133 } | |
1134 else | |
1135 { type = type->semantic(loc, sc); | |
1136 } | |
1137 return this; | |
1138 } | |
1139 | |
1140 Expression *IntegerExp::toLvalue(Scope *sc, Expression *e) | |
1141 { | |
1142 if (!e) | |
1143 e = this; | |
1144 else if (!loc.filename) | |
1145 loc = e->loc; | |
1146 e->error("constant %s is not an lvalue", e->toChars()); | |
1147 return this; | |
1148 } | |
1149 | |
1150 void IntegerExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1151 { | |
1152 integer_t v = toInteger(); | |
1153 | |
1154 if (type) | |
1155 { Type *t = type; | |
1156 | |
1157 L1: | |
1158 switch (t->ty) | |
1159 { | |
1160 case Tenum: | |
1161 { TypeEnum *te = (TypeEnum *)t; | |
1162 buf->printf("cast(%s)", te->sym->toChars()); | |
1163 t = te->sym->memtype; | |
1164 goto L1; | |
1165 } | |
1166 | |
1167 case Ttypedef: | |
1168 { TypeTypedef *tt = (TypeTypedef *)t; | |
1169 buf->printf("cast(%s)", tt->sym->toChars()); | |
1170 t = tt->sym->basetype; | |
1171 goto L1; | |
1172 } | |
1173 | |
1174 case Twchar: // BUG: need to cast(wchar) | |
1175 case Tdchar: // BUG: need to cast(dchar) | |
1176 if ((uinteger_t)v > 0xFF) | |
1177 { | |
1178 buf->printf("'\\U%08x'", v); | |
1179 break; | |
1180 } | |
1181 case Tchar: | |
1182 if (v == '\'') | |
1183 buf->writestring("'\\''"); | |
1184 else if (isprint(v) && v != '\\') | |
1185 buf->printf("'%c'", (int)v); | |
1186 else | |
1187 buf->printf("'\\x%02x'", (int)v); | |
1188 break; | |
1189 | |
1190 case Tint8: | |
1191 buf->writestring("cast(byte)"); | |
1192 goto L2; | |
1193 | |
1194 case Tint16: | |
1195 buf->writestring("cast(short)"); | |
1196 goto L2; | |
1197 | |
1198 case Tint32: | |
1199 L2: | |
1200 buf->printf("%d", (int)v); | |
1201 break; | |
1202 | |
1203 case Tuns8: | |
1204 buf->writestring("cast(ubyte)"); | |
1205 goto L3; | |
1206 | |
1207 case Tuns16: | |
1208 buf->writestring("cast(ushort)"); | |
1209 goto L3; | |
1210 | |
1211 case Tuns32: | |
1212 L3: | |
1213 buf->printf("%du", (unsigned)v); | |
1214 break; | |
1215 | |
1216 case Tint64: | |
1217 buf->printf("%jdL", v); | |
1218 break; | |
1219 | |
1220 case Tuns64: | |
1221 buf->printf("%juLU", v); | |
1222 break; | |
1223 | |
1224 case Tbit: | |
1225 case Tbool: | |
1226 buf->writestring((char *)(v ? "true" : "false")); | |
1227 break; | |
1228 | |
1229 case Tpointer: | |
1230 buf->writestring("cast("); | |
1231 buf->writestring(t->toChars()); | |
1232 buf->writeByte(')'); | |
1233 goto L3; | |
1234 | |
1235 default: | |
1236 #ifdef DEBUG | |
1237 t->print(); | |
1238 #endif | |
1239 assert(0); | |
1240 } | |
1241 } | |
1242 else if (v & 0x8000000000000000LL) | |
1243 buf->printf("0x%jx", v); | |
1244 else | |
1245 buf->printf("%jd", v); | |
1246 } | |
1247 | |
1248 void IntegerExp::toMangleBuffer(OutBuffer *buf) | |
1249 { | |
1250 if ((sinteger_t)value < 0) | |
1251 buf->printf("N%jd", -value); | |
1252 else | |
1253 buf->printf("%jd", value); | |
1254 } | |
1255 | |
1256 /******************************** RealExp **************************/ | |
1257 | |
1258 RealExp::RealExp(Loc loc, real_t value, Type *type) | |
1259 : Expression(loc, TOKfloat64, sizeof(RealExp)) | |
1260 { | |
1261 //printf("RealExp::RealExp(%Lg)\n", value); | |
1262 this->value = value; | |
1263 this->type = type; | |
1264 } | |
1265 | |
1266 char *RealExp::toChars() | |
1267 { | |
1268 static char buffer[sizeof(value) * 3 + 8 + 1 + 1]; | |
1269 | |
1270 #ifdef IN_GCC | |
1271 value.format(buffer, sizeof(buffer)); | |
1272 if (type->isimaginary()) | |
1273 strcat(buffer, "i"); | |
1274 #else | |
1275 sprintf(buffer, type->isimaginary() ? "%Lgi" : "%Lg", value); | |
1276 #endif | |
1277 assert(strlen(buffer) < sizeof(buffer)); | |
1278 return buffer; | |
1279 } | |
1280 | |
1281 integer_t RealExp::toInteger() | |
1282 { | |
1283 #ifdef IN_GCC | |
1284 return toReal().toInt(); | |
1285 #else | |
1286 return (sinteger_t) toReal(); | |
1287 #endif | |
1288 } | |
1289 | |
1290 uinteger_t RealExp::toUInteger() | |
1291 { | |
1292 #ifdef IN_GCC | |
1293 return (uinteger_t) toReal().toInt(); | |
1294 #else | |
1295 return (uinteger_t) toReal(); | |
1296 #endif | |
1297 } | |
1298 | |
1299 real_t RealExp::toReal() | |
1300 { | |
1301 return type->isreal() ? value : 0; | |
1302 } | |
1303 | |
1304 real_t RealExp::toImaginary() | |
1305 { | |
1306 return type->isreal() ? 0 : value; | |
1307 } | |
1308 | |
1309 complex_t RealExp::toComplex() | |
1310 { | |
1311 #ifdef __DMC__ | |
1312 return toReal() + toImaginary() * I; | |
1313 #else | |
1314 return complex_t(toReal(), toImaginary()); | |
1315 #endif | |
1316 } | |
1317 | |
1318 /******************************** | |
1319 * Test to see if two reals are the same. | |
1320 * Regard NaN's as equivalent. | |
1321 * Regard +0 and -0 as different. | |
1322 */ | |
1323 | |
1324 int RealEquals(real_t x1, real_t x2) | |
1325 { | |
1326 return (isnan(x1) && isnan(x2)) || | |
1327 /* In some cases, the REALPAD bytes get garbage in them, | |
1328 * so be sure and ignore them. | |
1329 */ | |
1330 memcmp(&x1, &x2, REALSIZE - REALPAD) == 0; | |
1331 } | |
1332 | |
1333 int RealExp::equals(Object *o) | |
1334 { RealExp *ne; | |
1335 | |
1336 if (this == o || | |
1337 (((Expression *)o)->op == TOKfloat64 && | |
1338 ((ne = (RealExp *)o), type->equals(ne->type)) && | |
1339 RealEquals(value, ne->value) | |
1340 ) | |
1341 ) | |
1342 return 1; | |
1343 return 0; | |
1344 } | |
1345 | |
1346 Expression *RealExp::semantic(Scope *sc) | |
1347 { | |
1348 if (!type) | |
1349 type = Type::tfloat64; | |
1350 else | |
1351 type = type->semantic(loc, sc); | |
1352 return this; | |
1353 } | |
1354 | |
1355 int RealExp::isBool(int result) | |
1356 { | |
1357 #ifdef IN_GCC | |
1358 return result ? (! value.isZero()) : (value.isZero()); | |
1359 #else | |
1360 return result ? (value != 0) | |
1361 : (value == 0); | |
1362 #endif | |
1363 } | |
1364 | |
1365 void floatToBuffer(OutBuffer *buf, Type *type, real_t value) | |
1366 { | |
1367 /* In order to get an exact representation, try converting it | |
1368 * to decimal then back again. If it matches, use it. | |
1369 * If it doesn't, fall back to hex, which is | |
1370 * always exact. | |
1371 */ | |
1372 char buffer[25]; | |
1373 sprintf(buffer, "%Lg", value); | |
1374 assert(strlen(buffer) < sizeof(buffer)); | |
1375 #if _WIN32 && __DMC__ | |
1376 char *save = __locale_decpoint; | |
1377 __locale_decpoint = "."; | |
1378 real_t r = strtold(buffer, NULL); | |
1379 __locale_decpoint = save; | |
1380 #else | |
1381 real_t r = strtold(buffer, NULL); | |
1382 #endif | |
1383 if (r == value) // if exact duplication | |
1384 buf->writestring(buffer); | |
1385 else | |
1386 buf->printf("%La", value); // ensure exact duplication | |
1387 | |
1388 if (type) | |
1389 { | |
1390 Type *t = type->toBasetype(); | |
1391 switch (t->ty) | |
1392 { | |
1393 case Tfloat32: | |
1394 case Timaginary32: | |
1395 case Tcomplex32: | |
1396 buf->writeByte('F'); | |
1397 break; | |
1398 | |
1399 case Tfloat80: | |
1400 case Timaginary80: | |
1401 case Tcomplex80: | |
1402 buf->writeByte('L'); | |
1403 break; | |
1404 | |
1405 default: | |
1406 break; | |
1407 } | |
1408 if (t->isimaginary()) | |
1409 buf->writeByte('i'); | |
1410 } | |
1411 } | |
1412 | |
1413 void RealExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1414 { | |
1415 floatToBuffer(buf, type, value); | |
1416 } | |
1417 | |
1418 void realToMangleBuffer(OutBuffer *buf, real_t value) | |
1419 { | |
1420 /* Rely on %A to get portable mangling. | |
1421 * Must munge result to get only identifier characters. | |
1422 * | |
1423 * Possible values from %A => mangled result | |
1424 * NAN => NAN | |
1425 * -INF => NINF | |
1426 * INF => INF | |
1427 * -0X1.1BC18BA997B95P+79 => N11BC18BA997B95P79 | |
1428 * 0X1.9P+2 => 19P2 | |
1429 */ | |
1430 | |
1431 if (isnan(value)) | |
1432 buf->writestring("NAN"); // no -NAN bugs | |
1433 else | |
1434 { | |
1435 char buffer[32]; | |
1436 int n = sprintf(buffer, "%LA", value); | |
1437 assert(n > 0 && n < sizeof(buffer)); | |
1438 for (int i = 0; i < n; i++) | |
1439 { char c = buffer[i]; | |
1440 | |
1441 switch (c) | |
1442 { | |
1443 case '-': | |
1444 buf->writeByte('N'); | |
1445 break; | |
1446 | |
1447 case '+': | |
1448 case 'X': | |
1449 case '.': | |
1450 break; | |
1451 | |
1452 case '0': | |
1453 if (i < 2) | |
1454 break; // skip leading 0X | |
1455 default: | |
1456 buf->writeByte(c); | |
1457 break; | |
1458 } | |
1459 } | |
1460 } | |
1461 } | |
1462 | |
1463 void RealExp::toMangleBuffer(OutBuffer *buf) | |
1464 { | |
1465 buf->writeByte('e'); | |
1466 realToMangleBuffer(buf, value); | |
1467 } | |
1468 | |
1469 | |
1470 /******************************** ComplexExp **************************/ | |
1471 | |
1472 ComplexExp::ComplexExp(Loc loc, complex_t value, Type *type) | |
1473 : Expression(loc, TOKcomplex80, sizeof(ComplexExp)) | |
1474 { | |
1475 this->value = value; | |
1476 this->type = type; | |
1477 //printf("ComplexExp::ComplexExp(%s)\n", toChars()); | |
1478 } | |
1479 | |
1480 char *ComplexExp::toChars() | |
1481 { | |
1482 static char buffer[sizeof(value) * 3 + 8 + 1]; | |
1483 | |
1484 #ifdef IN_GCC | |
1485 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1486 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1487 creall(value).format(buf1, sizeof(buf1)); | |
1488 cimagl(value).format(buf2, sizeof(buf2)); | |
1489 sprintf(buffer, "(%s+%si)", buf1, buf2); | |
1490 #else | |
1491 sprintf(buffer, "(%Lg+%Lgi)", creall(value), cimagl(value)); | |
1492 assert(strlen(buffer) < sizeof(buffer)); | |
1493 #endif | |
1494 return buffer; | |
1495 } | |
1496 | |
1497 integer_t ComplexExp::toInteger() | |
1498 { | |
1499 #ifdef IN_GCC | |
1500 return (sinteger_t) toReal().toInt(); | |
1501 #else | |
1502 return (sinteger_t) toReal(); | |
1503 #endif | |
1504 } | |
1505 | |
1506 uinteger_t ComplexExp::toUInteger() | |
1507 { | |
1508 #ifdef IN_GCC | |
1509 return (uinteger_t) toReal().toInt(); | |
1510 #else | |
1511 return (uinteger_t) toReal(); | |
1512 #endif | |
1513 } | |
1514 | |
1515 real_t ComplexExp::toReal() | |
1516 { | |
1517 return creall(value); | |
1518 } | |
1519 | |
1520 real_t ComplexExp::toImaginary() | |
1521 { | |
1522 return cimagl(value); | |
1523 } | |
1524 | |
1525 complex_t ComplexExp::toComplex() | |
1526 { | |
1527 return value; | |
1528 } | |
1529 | |
1530 int ComplexExp::equals(Object *o) | |
1531 { ComplexExp *ne; | |
1532 | |
1533 if (this == o || | |
1534 (((Expression *)o)->op == TOKcomplex80 && | |
1535 ((ne = (ComplexExp *)o), type->equals(ne->type)) && | |
1536 RealEquals(creall(value), creall(ne->value)) && | |
1537 RealEquals(cimagl(value), cimagl(ne->value)) | |
1538 ) | |
1539 ) | |
1540 return 1; | |
1541 return 0; | |
1542 } | |
1543 | |
1544 Expression *ComplexExp::semantic(Scope *sc) | |
1545 { | |
1546 if (!type) | |
1547 type = Type::tcomplex80; | |
1548 else | |
1549 type = type->semantic(loc, sc); | |
1550 return this; | |
1551 } | |
1552 | |
1553 int ComplexExp::isBool(int result) | |
1554 { | |
1555 if (result) | |
1556 return (bool)(value); | |
1557 else | |
1558 return !value; | |
1559 } | |
1560 | |
1561 void ComplexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1562 { | |
1563 /* Print as: | |
1564 * (re+imi) | |
1565 */ | |
1566 #ifdef IN_GCC | |
1567 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1568 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1569 creall(value).format(buf1, sizeof(buf1)); | |
1570 cimagl(value).format(buf2, sizeof(buf2)); | |
1571 buf->printf("(%s+%si)", buf1, buf2); | |
1572 #else | |
1573 buf->writeByte('('); | |
1574 floatToBuffer(buf, type, creall(value)); | |
1575 buf->writeByte('+'); | |
1576 floatToBuffer(buf, type, cimagl(value)); | |
1577 buf->writestring("i)"); | |
1578 #endif | |
1579 } | |
1580 | |
1581 void ComplexExp::toMangleBuffer(OutBuffer *buf) | |
1582 { | |
1583 buf->writeByte('c'); | |
1584 real_t r = toReal(); | |
1585 realToMangleBuffer(buf, r); | |
1586 buf->writeByte('c'); // separate the two | |
1587 r = toImaginary(); | |
1588 realToMangleBuffer(buf, r); | |
1589 } | |
1590 | |
1591 /******************************** IdentifierExp **************************/ | |
1592 | |
1593 IdentifierExp::IdentifierExp(Loc loc, Identifier *ident) | |
1594 : Expression(loc, TOKidentifier, sizeof(IdentifierExp)) | |
1595 { | |
1596 this->ident = ident; | |
1597 } | |
1598 | |
1599 Expression *IdentifierExp::semantic(Scope *sc) | |
1600 { | |
1601 Dsymbol *s; | |
1602 Dsymbol *scopesym; | |
1603 | |
1604 #if LOGSEMANTIC | |
1605 printf("IdentifierExp::semantic('%s')\n", ident->toChars()); | |
1606 #endif | |
1607 s = sc->search(loc, ident, &scopesym); | |
1608 if (s) | |
1609 { Expression *e; | |
1610 WithScopeSymbol *withsym; | |
1611 | |
1612 // See if it was a with class | |
1613 withsym = scopesym->isWithScopeSymbol(); | |
1614 if (withsym) | |
1615 { | |
1616 s = s->toAlias(); | |
1617 | |
1618 // Same as wthis.ident | |
1619 if (s->needThis() || s->isTemplateDeclaration()) | |
1620 { | |
1621 e = new VarExp(loc, withsym->withstate->wthis); | |
1622 e = new DotIdExp(loc, e, ident); | |
1623 } | |
1624 else | |
1625 { Type *t = withsym->withstate->wthis->type; | |
1626 if (t->ty == Tpointer) | |
1627 t = t->next; | |
1628 e = new TypeDotIdExp(loc, t, ident); | |
1629 } | |
1630 } | |
1631 else | |
1632 { | |
1633 if (!s->parent && scopesym->isArrayScopeSymbol()) | |
1634 { // Kludge to run semantic() here because | |
1635 // ArrayScopeSymbol::search() doesn't have access to sc. | |
1636 s->semantic(sc); | |
1637 } | |
1638 // Look to see if f is really a function template | |
1639 FuncDeclaration *f = s->isFuncDeclaration(); | |
1640 if (f && f->parent) | |
1641 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
1642 | |
1643 if (ti && | |
1644 !ti->isTemplateMixin() && | |
1645 (ti->name == f->ident || | |
1646 ti->toAlias()->ident == f->ident) | |
1647 && | |
1648 ti->tempdecl && ti->tempdecl->onemember) | |
1649 { | |
1650 TemplateDeclaration *tempdecl = ti->tempdecl; | |
1651 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
1652 tempdecl = tempdecl->overroot; // then get the start | |
1653 e = new TemplateExp(loc, tempdecl); | |
1654 e = e->semantic(sc); | |
1655 return e; | |
1656 } | |
1657 } | |
1658 e = new DsymbolExp(loc, s); | |
1659 } | |
1660 return e->semantic(sc); | |
1661 } | |
1662 error("undefined identifier %s", ident->toChars()); | |
1663 type = Type::terror; | |
1664 return this; | |
1665 } | |
1666 | |
1667 char *IdentifierExp::toChars() | |
1668 { | |
1669 return ident->toChars(); | |
1670 } | |
1671 | |
1672 void IdentifierExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1673 { | |
1674 if (hgs->hdrgen) | |
1675 buf->writestring(ident->toHChars2()); | |
1676 else | |
1677 buf->writestring(ident->toChars()); | |
1678 } | |
1679 | |
1680 Expression *IdentifierExp::toLvalue(Scope *sc, Expression *e) | |
1681 { | |
1682 #if 0 | |
1683 tym = tybasic(e1->ET->Tty); | |
1684 if (!(tyscalar(tym) || | |
1685 tym == TYstruct || | |
1686 tym == TYarray && e->Eoper == TOKaddr)) | |
1687 synerr(EM_lvalue); // lvalue expected | |
1688 #endif | |
1689 return this; | |
1690 } | |
1691 | |
1692 /******************************** DollarExp **************************/ | |
1693 | |
1694 DollarExp::DollarExp(Loc loc) | |
1695 : IdentifierExp(loc, Id::dollar) | |
1696 { | |
1697 } | |
1698 | |
1699 /******************************** DsymbolExp **************************/ | |
1700 | |
1701 DsymbolExp::DsymbolExp(Loc loc, Dsymbol *s) | |
1702 : Expression(loc, TOKdsymbol, sizeof(DsymbolExp)) | |
1703 { | |
1704 this->s = s; | |
1705 } | |
1706 | |
1707 Expression *DsymbolExp::semantic(Scope *sc) | |
1708 { | |
1709 #if LOGSEMANTIC | |
1710 printf("DsymbolExp::semantic('%s')\n", s->toChars()); | |
1711 #endif | |
1712 | |
1713 Lagain: | |
1714 EnumMember *em; | |
1715 Expression *e; | |
1716 VarDeclaration *v; | |
1717 FuncDeclaration *f; | |
1718 FuncLiteralDeclaration *fld; | |
1719 Declaration *d; | |
1720 ClassDeclaration *cd; | |
1721 ClassDeclaration *thiscd = NULL; | |
1722 Import *imp; | |
1723 Package *pkg; | |
1724 Type *t; | |
1725 | |
1726 //printf("DsymbolExp:: %p '%s' is a symbol\n", this, toChars()); | |
1727 //printf("s = '%s', s->kind = '%s'\n", s->toChars(), s->kind()); | |
1728 if (type) | |
1729 return this; | |
1730 if (!s->isFuncDeclaration()) // functions are checked after overloading | |
1731 checkDeprecated(sc, s); | |
1732 s = s->toAlias(); | |
1733 //printf("s = '%s', s->kind = '%s', s->needThis() = %p\n", s->toChars(), s->kind(), s->needThis()); | |
1734 if (!s->isFuncDeclaration()) | |
1735 checkDeprecated(sc, s); | |
1736 | |
1737 if (sc->func) | |
1738 thiscd = sc->func->parent->isClassDeclaration(); | |
1739 | |
1740 // BUG: This should happen after overload resolution for functions, not before | |
1741 if (s->needThis()) | |
1742 { | |
1743 if (hasThis(sc) /*&& !s->isFuncDeclaration()*/) | |
1744 { | |
1745 // Supply an implicit 'this', as in | |
1746 // this.ident | |
1747 | |
1748 DotVarExp *de; | |
1749 | |
1750 de = new DotVarExp(loc, new ThisExp(loc), s->isDeclaration()); | |
1751 return de->semantic(sc); | |
1752 } | |
1753 } | |
1754 | |
1755 em = s->isEnumMember(); | |
1756 if (em) | |
1757 { | |
1758 e = em->value; | |
1759 e = e->semantic(sc); | |
1760 return e; | |
1761 } | |
1762 v = s->isVarDeclaration(); | |
1763 if (v) | |
1764 { | |
1765 //printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
1766 if (!type) | |
1767 { type = v->type; | |
1768 if (!v->type) | |
1769 { error("forward reference of %s", v->toChars()); | |
1770 type = Type::terror; | |
1771 } | |
1772 } | |
1773 if (v->isConst() && type->toBasetype()->ty != Tsarray) | |
1774 { | |
1775 if (v->init) | |
1776 { | |
1777 if (v->inuse) | |
1778 { | |
1779 error("circular reference to '%s'", v->toChars()); | |
1780 type = Type::tint32; | |
1781 return this; | |
1782 } | |
1783 ExpInitializer *ei = v->init->isExpInitializer(); | |
1784 if (ei) | |
1785 { | |
1786 e = ei->exp->copy(); // make copy so we can change loc | |
1787 if (e->op == TOKstring || !e->type) | |
1788 e = e->semantic(sc); | |
1789 e = e->implicitCastTo(sc, type); | |
1790 e->loc = loc; | |
1791 return e; | |
1792 } | |
1793 } | |
1794 else | |
1795 { | |
1796 e = type->defaultInit(); | |
1797 e->loc = loc; | |
1798 return e; | |
1799 } | |
1800 } | |
1801 e = new VarExp(loc, v); | |
1802 e->type = type; | |
1803 e = e->semantic(sc); | |
1804 return e->deref(); | |
1805 } | |
1806 fld = s->isFuncLiteralDeclaration(); | |
1807 if (fld) | |
1808 { //printf("'%s' is a function literal\n", fld->toChars()); | |
1809 e = new FuncExp(loc, fld); | |
1810 return e->semantic(sc); | |
1811 } | |
1812 f = s->isFuncDeclaration(); | |
1813 if (f) | |
1814 { //printf("'%s' is a function\n", f->toChars()); | |
1815 return new VarExp(loc, f); | |
1816 } | |
1817 cd = s->isClassDeclaration(); | |
1818 if (cd && thiscd && cd->isBaseOf(thiscd, NULL) && sc->func->needThis()) | |
1819 { | |
1820 // We need to add an implicit 'this' if cd is this class or a base class. | |
1821 DotTypeExp *dte; | |
1822 | |
1823 dte = new DotTypeExp(loc, new ThisExp(loc), s); | |
1824 return dte->semantic(sc); | |
1825 } | |
1826 imp = s->isImport(); | |
1827 if (imp) | |
1828 { | |
1829 ScopeExp *ie; | |
1830 | |
1831 ie = new ScopeExp(loc, imp->pkg); | |
1832 return ie->semantic(sc); | |
1833 } | |
1834 pkg = s->isPackage(); | |
1835 if (pkg) | |
1836 { | |
1837 ScopeExp *ie; | |
1838 | |
1839 ie = new ScopeExp(loc, pkg); | |
1840 return ie->semantic(sc); | |
1841 } | |
1842 Module *mod = s->isModule(); | |
1843 if (mod) | |
1844 { | |
1845 ScopeExp *ie; | |
1846 | |
1847 ie = new ScopeExp(loc, mod); | |
1848 return ie->semantic(sc); | |
1849 } | |
1850 | |
1851 t = s->getType(); | |
1852 if (t) | |
1853 { | |
1854 return new TypeExp(loc, t); | |
1855 } | |
1856 | |
1857 TupleDeclaration *tup = s->isTupleDeclaration(); | |
1858 if (tup) | |
1859 { | |
1860 e = new TupleExp(loc, tup); | |
1861 e = e->semantic(sc); | |
1862 return e; | |
1863 } | |
1864 | |
1865 TemplateInstance *ti = s->isTemplateInstance(); | |
1866 if (ti && !global.errors) | |
1867 { if (!ti->semanticdone) | |
1868 ti->semantic(sc); | |
1869 s = ti->inst->toAlias(); | |
1870 if (!s->isTemplateInstance()) | |
1871 goto Lagain; | |
1872 e = new ScopeExp(loc, ti); | |
1873 e = e->semantic(sc); | |
1874 return e; | |
1875 } | |
1876 | |
1877 TemplateDeclaration *td = s->isTemplateDeclaration(); | |
1878 if (td) | |
1879 { | |
1880 e = new TemplateExp(loc, td); | |
1881 e = e->semantic(sc); | |
1882 return e; | |
1883 } | |
1884 | |
1885 Lerr: | |
1886 error("%s '%s' is not a variable", s->kind(), s->toChars()); | |
1887 type = Type::terror; | |
1888 return this; | |
1889 } | |
1890 | |
1891 char *DsymbolExp::toChars() | |
1892 { | |
1893 return s->toChars(); | |
1894 } | |
1895 | |
1896 void DsymbolExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1897 { | |
1898 buf->writestring(s->toChars()); | |
1899 } | |
1900 | |
1901 Expression *DsymbolExp::toLvalue(Scope *sc, Expression *e) | |
1902 { | |
1903 #if 0 | |
1904 tym = tybasic(e1->ET->Tty); | |
1905 if (!(tyscalar(tym) || | |
1906 tym == TYstruct || | |
1907 tym == TYarray && e->Eoper == TOKaddr)) | |
1908 synerr(EM_lvalue); // lvalue expected | |
1909 #endif | |
1910 return this; | |
1911 } | |
1912 | |
1913 /******************************** ThisExp **************************/ | |
1914 | |
1915 ThisExp::ThisExp(Loc loc) | |
1916 : Expression(loc, TOKthis, sizeof(ThisExp)) | |
1917 { | |
1918 var = NULL; | |
1919 } | |
1920 | |
1921 Expression *ThisExp::semantic(Scope *sc) | |
1922 { FuncDeclaration *fd; | |
1923 FuncDeclaration *fdthis; | |
1924 int nested = 0; | |
1925 | |
1926 #if LOGSEMANTIC | |
1927 printf("ThisExp::semantic()\n"); | |
1928 #endif | |
1929 if (type) | |
1930 { //assert(global.errors || var); | |
1931 return this; | |
1932 } | |
1933 | |
1934 /* Special case for typeof(this) and typeof(super) since both | |
1935 * should work even if they are not inside a non-static member function | |
1936 */ | |
1937 if (sc->intypeof) | |
1938 { | |
1939 // Find enclosing struct or class | |
1940 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
1941 { | |
1942 ClassDeclaration *cd; | |
1943 StructDeclaration *sd; | |
1944 | |
1945 if (!s) | |
1946 { | |
1947 error("%s is not in a struct or class scope", toChars()); | |
1948 goto Lerr; | |
1949 } | |
1950 cd = s->isClassDeclaration(); | |
1951 if (cd) | |
1952 { | |
1953 type = cd->type; | |
1954 return this; | |
1955 } | |
1956 sd = s->isStructDeclaration(); | |
1957 if (sd) | |
1958 { | |
1959 type = sd->type->pointerTo(); | |
1960 return this; | |
1961 } | |
1962 } | |
1963 } | |
1964 | |
1965 fdthis = sc->parent->isFuncDeclaration(); | |
1966 fd = hasThis(sc); // fd is the uplevel function with the 'this' variable | |
1967 if (!fd) | |
1968 goto Lerr; | |
1969 | |
1970 assert(fd->vthis); | |
1971 var = fd->vthis; | |
1972 assert(var->parent); | |
1973 type = var->type; | |
1974 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
1975 #if 0 | |
1976 if (fd != fdthis) // if nested | |
1977 { | |
1978 fdthis->getLevel(loc, fd); | |
1979 fd->vthis->nestedref = 1; | |
1980 fd->nestedFrameRef = 1; | |
1981 } | |
1982 #endif | |
1983 sc->callSuper |= CSXthis; | |
1984 return this; | |
1985 | |
1986 Lerr: | |
1987 error("'this' is only allowed in non-static member functions, not %s", sc->parent->toChars()); | |
1988 type = Type::tint32; | |
1989 return this; | |
1990 } | |
1991 | |
1992 int ThisExp::isBool(int result) | |
1993 { | |
1994 return result ? TRUE : FALSE; | |
1995 } | |
1996 | |
1997 void ThisExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1998 { | |
1999 buf->writestring("this"); | |
2000 } | |
2001 | |
2002 Expression *ThisExp::toLvalue(Scope *sc, Expression *e) | |
2003 { | |
2004 return this; | |
2005 } | |
2006 | |
2007 /******************************** SuperExp **************************/ | |
2008 | |
2009 SuperExp::SuperExp(Loc loc) | |
2010 : ThisExp(loc) | |
2011 { | |
2012 op = TOKsuper; | |
2013 } | |
2014 | |
2015 Expression *SuperExp::semantic(Scope *sc) | |
2016 { FuncDeclaration *fd; | |
2017 FuncDeclaration *fdthis; | |
2018 ClassDeclaration *cd; | |
2019 Dsymbol *s; | |
2020 | |
2021 #if LOGSEMANTIC | |
2022 printf("SuperExp::semantic('%s')\n", toChars()); | |
2023 #endif | |
2024 if (type) | |
2025 return this; | |
2026 | |
2027 /* Special case for typeof(this) and typeof(super) since both | |
2028 * should work even if they are not inside a non-static member function | |
2029 */ | |
2030 if (sc->intypeof) | |
2031 { | |
2032 // Find enclosing class | |
2033 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2034 { | |
2035 ClassDeclaration *cd; | |
2036 | |
2037 if (!s) | |
2038 { | |
2039 error("%s is not in a class scope", toChars()); | |
2040 goto Lerr; | |
2041 } | |
2042 cd = s->isClassDeclaration(); | |
2043 if (cd) | |
2044 { | |
2045 cd = cd->baseClass; | |
2046 if (!cd) | |
2047 { error("class %s has no 'super'", s->toChars()); | |
2048 goto Lerr; | |
2049 } | |
2050 type = cd->type; | |
2051 return this; | |
2052 } | |
2053 } | |
2054 } | |
2055 | |
2056 fdthis = sc->parent->isFuncDeclaration(); | |
2057 fd = hasThis(sc); | |
2058 if (!fd) | |
2059 goto Lerr; | |
2060 assert(fd->vthis); | |
2061 var = fd->vthis; | |
2062 assert(var->parent); | |
2063 | |
2064 s = fd->toParent(); | |
2065 while (s && s->isTemplateInstance()) | |
2066 s = s->toParent(); | |
2067 assert(s); | |
2068 cd = s->isClassDeclaration(); | |
2069 //printf("parent is %s %s\n", fd->toParent()->kind(), fd->toParent()->toChars()); | |
2070 if (!cd) | |
2071 goto Lerr; | |
2072 if (!cd->baseClass) | |
2073 { | |
2074 error("no base class for %s", cd->toChars()); | |
2075 type = fd->vthis->type; | |
2076 } | |
2077 else | |
2078 { | |
2079 type = cd->baseClass->type; | |
2080 } | |
2081 | |
2082 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2083 #if 0 | |
2084 if (fd != fdthis) | |
2085 { | |
2086 fdthis->getLevel(loc, fd); | |
2087 fd->vthis->nestedref = 1; | |
2088 fd->nestedFrameRef = 1; | |
2089 } | |
2090 #endif | |
2091 | |
2092 sc->callSuper |= CSXsuper; | |
2093 return this; | |
2094 | |
2095 | |
2096 Lerr: | |
2097 error("'super' is only allowed in non-static class member functions"); | |
2098 type = Type::tint32; | |
2099 return this; | |
2100 } | |
2101 | |
2102 void SuperExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2103 { | |
2104 buf->writestring("super"); | |
2105 } | |
2106 | |
2107 | |
2108 /******************************** NullExp **************************/ | |
2109 | |
2110 NullExp::NullExp(Loc loc) | |
2111 : Expression(loc, TOKnull, sizeof(NullExp)) | |
2112 { | |
2113 committed = 0; | |
2114 } | |
2115 | |
2116 Expression *NullExp::semantic(Scope *sc) | |
2117 { | |
2118 #if LOGSEMANTIC | |
2119 printf("NullExp::semantic('%s')\n", toChars()); | |
2120 #endif | |
2121 // NULL is the same as (void *)0 | |
2122 if (!type) | |
2123 type = Type::tvoid->pointerTo(); | |
2124 return this; | |
2125 } | |
2126 | |
2127 int NullExp::isBool(int result) | |
2128 { | |
2129 return result ? FALSE : TRUE; | |
2130 } | |
2131 | |
2132 void NullExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2133 { | |
2134 buf->writestring("null"); | |
2135 } | |
2136 | |
2137 void NullExp::toMangleBuffer(OutBuffer *buf) | |
2138 { | |
2139 buf->writeByte('n'); | |
2140 } | |
2141 | |
2142 /******************************** StringExp **************************/ | |
2143 | |
2144 StringExp::StringExp(Loc loc, char *string) | |
2145 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2146 { | |
2147 this->string = string; | |
2148 this->len = strlen(string); | |
2149 this->sz = 1; | |
2150 this->committed = 0; | |
2151 this->postfix = 0; | |
2152 } | |
2153 | |
2154 StringExp::StringExp(Loc loc, void *string, size_t len) | |
2155 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2156 { | |
2157 this->string = string; | |
2158 this->len = len; | |
2159 this->sz = 1; | |
2160 this->committed = 0; | |
2161 this->postfix = 0; | |
2162 } | |
2163 | |
2164 StringExp::StringExp(Loc loc, void *string, size_t len, unsigned char postfix) | |
2165 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2166 { | |
2167 this->string = string; | |
2168 this->len = len; | |
2169 this->sz = 1; | |
2170 this->committed = 0; | |
2171 this->postfix = postfix; | |
2172 } | |
2173 | |
2174 #if 0 | |
2175 Expression *StringExp::syntaxCopy() | |
2176 { | |
2177 printf("StringExp::syntaxCopy() %s\n", toChars()); | |
2178 return copy(); | |
2179 } | |
2180 #endif | |
2181 | |
2182 int StringExp::equals(Object *o) | |
2183 { | |
2184 //printf("StringExp::equals('%s')\n", o->toChars()); | |
2185 if (o && o->dyncast() == DYNCAST_EXPRESSION) | |
2186 { Expression *e = (Expression *)o; | |
2187 | |
2188 if (e->op == TOKstring) | |
2189 { | |
2190 return compare(o) == 0; | |
2191 } | |
2192 } | |
2193 return FALSE; | |
2194 } | |
2195 | |
2196 char *StringExp::toChars() | |
2197 { | |
2198 OutBuffer buf; | |
2199 HdrGenState hgs; | |
2200 char *p; | |
2201 | |
2202 memset(&hgs, 0, sizeof(hgs)); | |
2203 toCBuffer(&buf, &hgs); | |
2204 buf.writeByte(0); | |
2205 p = (char *)buf.data; | |
2206 buf.data = NULL; | |
2207 return p; | |
2208 } | |
2209 | |
2210 Expression *StringExp::semantic(Scope *sc) | |
2211 { | |
2212 #if LOGSEMANTIC | |
2213 printf("StringExp::semantic() %s\n", toChars()); | |
2214 #endif | |
2215 if (!type) | |
2216 { OutBuffer buffer; | |
2217 size_t newlen = 0; | |
2218 char *p; | |
2219 size_t u; | |
2220 unsigned c; | |
2221 | |
2222 switch (postfix) | |
2223 { | |
2224 case 'd': | |
2225 for (u = 0; u < len;) | |
2226 { | |
2227 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2228 if (p) | |
2229 { error("%s", p); | |
2230 break; | |
2231 } | |
2232 else | |
2233 { buffer.write4(c); | |
2234 newlen++; | |
2235 } | |
2236 } | |
2237 buffer.write4(0); | |
2238 string = buffer.extractData(); | |
2239 len = newlen; | |
2240 sz = 4; | |
2241 type = new TypeSArray(Type::tdchar, new IntegerExp(loc, len, Type::tindex)); | |
2242 committed = 1; | |
2243 break; | |
2244 | |
2245 case 'w': | |
2246 for (u = 0; u < len;) | |
2247 { | |
2248 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2249 if (p) | |
2250 { error("%s", p); | |
2251 break; | |
2252 } | |
2253 else | |
2254 { buffer.writeUTF16(c); | |
2255 newlen++; | |
2256 if (c >= 0x10000) | |
2257 newlen++; | |
2258 } | |
2259 } | |
2260 buffer.writeUTF16(0); | |
2261 string = buffer.extractData(); | |
2262 len = newlen; | |
2263 sz = 2; | |
2264 type = new TypeSArray(Type::twchar, new IntegerExp(loc, len, Type::tindex)); | |
2265 committed = 1; | |
2266 break; | |
2267 | |
2268 case 'c': | |
2269 committed = 1; | |
2270 default: | |
2271 type = new TypeSArray(Type::tchar, new IntegerExp(loc, len, Type::tindex)); | |
2272 break; | |
2273 } | |
2274 type = type->semantic(loc, sc); | |
2275 } | |
2276 return this; | |
2277 } | |
2278 | |
2279 /**************************************** | |
2280 * Convert string to char[]. | |
2281 */ | |
2282 | |
2283 StringExp *StringExp::toUTF8(Scope *sc) | |
2284 { | |
2285 if (sz != 1) | |
2286 { // Convert to UTF-8 string | |
2287 committed = 0; | |
2288 Expression *e = castTo(sc, Type::tchar->arrayOf()); | |
2289 e = e->optimize(WANTvalue); | |
2290 assert(e->op == TOKstring); | |
2291 StringExp *se = (StringExp *)e; | |
2292 assert(se->sz == 1); | |
2293 return se; | |
2294 } | |
2295 return this; | |
2296 } | |
2297 | |
2298 int StringExp::compare(Object *obj) | |
2299 { | |
2300 // Used to sort case statement expressions so we can do an efficient lookup | |
2301 StringExp *se2 = (StringExp *)(obj); | |
2302 | |
2303 // This is a kludge so isExpression() in template.c will return 5 | |
2304 // for StringExp's. | |
2305 if (!se2) | |
2306 return 5; | |
2307 | |
2308 assert(se2->op == TOKstring); | |
2309 | |
2310 int len1 = len; | |
2311 int len2 = se2->len; | |
2312 | |
2313 if (len1 == len2) | |
2314 { | |
2315 switch (sz) | |
2316 { | |
2317 case 1: | |
2318 return strcmp((char *)string, (char *)se2->string); | |
2319 | |
2320 case 2: | |
2321 { unsigned u; | |
2322 d_wchar *s1 = (d_wchar *)string; | |
2323 d_wchar *s2 = (d_wchar *)se2->string; | |
2324 | |
2325 for (u = 0; u < len; u++) | |
2326 { | |
2327 if (s1[u] != s2[u]) | |
2328 return s1[u] - s2[u]; | |
2329 } | |
2330 } | |
2331 | |
2332 case 4: | |
2333 { unsigned u; | |
2334 d_dchar *s1 = (d_dchar *)string; | |
2335 d_dchar *s2 = (d_dchar *)se2->string; | |
2336 | |
2337 for (u = 0; u < len; u++) | |
2338 { | |
2339 if (s1[u] != s2[u]) | |
2340 return s1[u] - s2[u]; | |
2341 } | |
2342 } | |
2343 break; | |
2344 | |
2345 default: | |
2346 assert(0); | |
2347 } | |
2348 } | |
2349 return len1 - len2; | |
2350 } | |
2351 | |
2352 int StringExp::isBool(int result) | |
2353 { | |
2354 return result ? TRUE : FALSE; | |
2355 } | |
2356 | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2357 unsigned StringExp::charAt(size_t i) |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2358 { unsigned value; |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2359 |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2360 switch (sz) |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2361 { |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2362 case 1: |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2363 value = ((unsigned char *)string)[i]; |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2364 break; |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2365 |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2366 case 2: |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2367 value = ((unsigned short *)string)[i]; |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2368 break; |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2369 |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2370 case 4: |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2371 value = ((unsigned int *)string)[i]; |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2372 break; |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2373 |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2374 default: |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2375 assert(0); |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2376 break; |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2377 } |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2378 return value; |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2379 } |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2380 |
1 | 2381 void StringExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2382 { | |
2383 buf->writeByte('"'); | |
2384 for (size_t i = 0; i < len; i++) | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2385 { unsigned c = charAt(i); |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
2386 |
1 | 2387 switch (c) |
2388 { | |
2389 case '"': | |
2390 case '\\': | |
2391 if (!hgs->console) | |
2392 buf->writeByte('\\'); | |
2393 default: | |
2394 if (c <= 0xFF) | |
2395 { if (c <= 0x7F && (isprint(c) || hgs->console)) | |
2396 buf->writeByte(c); | |
2397 else | |
2398 buf->printf("\\x%02x", c); | |
2399 } | |
2400 else if (c <= 0xFFFF) | |
2401 buf->printf("\\x%02x\\x%02x", c & 0xFF, c >> 8); | |
2402 else | |
2403 buf->printf("\\x%02x\\x%02x\\x%02x\\x%02x", | |
2404 c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF, c >> 24); | |
2405 break; | |
2406 } | |
2407 } | |
2408 buf->writeByte('"'); | |
2409 if (postfix) | |
2410 buf->writeByte(postfix); | |
2411 } | |
2412 | |
2413 void StringExp::toMangleBuffer(OutBuffer *buf) | |
2414 { char m; | |
2415 OutBuffer tmp; | |
2416 char *p; | |
2417 unsigned c; | |
2418 size_t u; | |
2419 unsigned char *q; | |
2420 unsigned qlen; | |
2421 | |
2422 /* Write string in UTF-8 format | |
2423 */ | |
2424 switch (sz) | |
2425 { case 1: | |
2426 m = 'a'; | |
2427 q = (unsigned char *)string; | |
2428 qlen = len; | |
2429 break; | |
2430 case 2: | |
2431 m = 'w'; | |
2432 for (u = 0; u < len; ) | |
2433 { | |
2434 p = utf_decodeWchar((unsigned short *)string, len, &u, &c); | |
2435 if (p) | |
2436 error("%s", p); | |
2437 else | |
2438 tmp.writeUTF8(c); | |
2439 } | |
2440 q = tmp.data; | |
2441 qlen = tmp.offset; | |
2442 break; | |
2443 case 4: | |
2444 m = 'd'; | |
2445 for (u = 0; u < len; u++) | |
2446 { | |
2447 c = ((unsigned *)string)[u]; | |
2448 if (!utf_isValidDchar(c)) | |
2449 error("invalid UCS-32 char \\U%08x", c); | |
2450 else | |
2451 tmp.writeUTF8(c); | |
2452 } | |
2453 q = tmp.data; | |
2454 qlen = tmp.offset; | |
2455 break; | |
2456 default: | |
2457 assert(0); | |
2458 } | |
2459 buf->writeByte(m); | |
2460 buf->printf("%d_", qlen); | |
2461 for (size_t i = 0; i < qlen; i++) | |
2462 buf->printf("%02x", q[i]); | |
2463 } | |
2464 | |
2465 /************************ ArrayLiteralExp ************************************/ | |
2466 | |
2467 // [ e1, e2, e3, ... ] | |
2468 | |
2469 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expressions *elements) | |
2470 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2471 { | |
2472 this->elements = elements; | |
2473 } | |
2474 | |
2475 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expression *e) | |
2476 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2477 { | |
2478 elements = new Expressions; | |
2479 elements->push(e); | |
2480 } | |
2481 | |
2482 Expression *ArrayLiteralExp::syntaxCopy() | |
2483 { | |
2484 return new ArrayLiteralExp(loc, arraySyntaxCopy(elements)); | |
2485 } | |
2486 | |
2487 Expression *ArrayLiteralExp::semantic(Scope *sc) | |
2488 { Expression *e; | |
2489 Type *t0 = NULL; | |
2490 | |
2491 #if LOGSEMANTIC | |
2492 printf("ArrayLiteralExp::semantic('%s')\n", toChars()); | |
2493 #endif | |
2494 | |
2495 // Run semantic() on each element | |
2496 for (int i = 0; i < elements->dim; i++) | |
2497 { e = (Expression *)elements->data[i]; | |
2498 e = e->semantic(sc); | |
2499 elements->data[i] = (void *)e; | |
2500 } | |
2501 expandTuples(elements); | |
2502 for (int i = 0; i < elements->dim; i++) | |
2503 { e = (Expression *)elements->data[i]; | |
2504 | |
2505 if (!e->type) | |
2506 error("%s has no value", e->toChars()); | |
2507 e = resolveProperties(sc, e); | |
2508 | |
2509 unsigned char committed = 1; | |
2510 if (e->op == TOKstring) | |
2511 committed = ((StringExp *)e)->committed; | |
2512 | |
2513 if (!t0) | |
2514 { t0 = e->type; | |
2515 // Convert any static arrays to dynamic arrays | |
2516 if (t0->ty == Tsarray) | |
2517 { | |
2518 t0 = t0->next->arrayOf(); | |
2519 e = e->implicitCastTo(sc, t0); | |
2520 } | |
2521 } | |
2522 else | |
2523 e = e->implicitCastTo(sc, t0); | |
2524 if (!committed && e->op == TOKstring) | |
2525 { StringExp *se = (StringExp *)e; | |
2526 se->committed = 0; | |
2527 } | |
2528 elements->data[i] = (void *)e; | |
2529 } | |
2530 | |
2531 if (!t0) | |
2532 t0 = Type::tvoid; | |
2533 type = new TypeSArray(t0, new IntegerExp(elements->dim)); | |
2534 type = type->semantic(loc, sc); | |
2535 return this; | |
2536 } | |
2537 | |
2538 int ArrayLiteralExp::checkSideEffect(int flag) | |
2539 { int f = 0; | |
2540 | |
2541 for (size_t i = 0; i < elements->dim; i++) | |
2542 { Expression *e = (Expression *)elements->data[i]; | |
2543 | |
2544 f |= e->checkSideEffect(2); | |
2545 } | |
2546 if (flag == 0 && f == 0) | |
2547 Expression::checkSideEffect(0); | |
2548 return f; | |
2549 } | |
2550 | |
2551 int ArrayLiteralExp::isBool(int result) | |
2552 { | |
2553 size_t dim = elements ? elements->dim : 0; | |
2554 return result ? (dim != 0) : (dim == 0); | |
2555 } | |
2556 | |
2557 void ArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2558 { | |
2559 buf->writeByte('['); | |
2560 argsToCBuffer(buf, elements, hgs); | |
2561 buf->writeByte(']'); | |
2562 } | |
2563 | |
2564 void ArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
2565 { | |
2566 size_t dim = elements ? elements->dim : 0; | |
2567 buf->printf("A%u", dim); | |
2568 for (size_t i = 0; i < dim; i++) | |
2569 { Expression *e = (Expression *)elements->data[i]; | |
2570 e->toMangleBuffer(buf); | |
2571 } | |
2572 } | |
2573 | |
2574 /************************ AssocArrayLiteralExp ************************************/ | |
2575 | |
2576 // [ key0 : value0, key1 : value1, ... ] | |
2577 | |
2578 AssocArrayLiteralExp::AssocArrayLiteralExp(Loc loc, | |
2579 Expressions *keys, Expressions *values) | |
2580 : Expression(loc, TOKassocarrayliteral, sizeof(AssocArrayLiteralExp)) | |
2581 { | |
2582 assert(keys->dim == values->dim); | |
2583 this->keys = keys; | |
2584 this->values = values; | |
2585 } | |
2586 | |
2587 Expression *AssocArrayLiteralExp::syntaxCopy() | |
2588 { | |
2589 return new AssocArrayLiteralExp(loc, | |
2590 arraySyntaxCopy(keys), arraySyntaxCopy(values)); | |
2591 } | |
2592 | |
2593 Expression *AssocArrayLiteralExp::semantic(Scope *sc) | |
2594 { Expression *e; | |
2595 Type *tkey = NULL; | |
2596 Type *tvalue = NULL; | |
2597 | |
2598 #if LOGSEMANTIC | |
2599 printf("AssocArrayLiteralExp::semantic('%s')\n", toChars()); | |
2600 #endif | |
2601 | |
2602 // Run semantic() on each element | |
2603 for (size_t i = 0; i < keys->dim; i++) | |
2604 { Expression *key = (Expression *)keys->data[i]; | |
2605 Expression *value = (Expression *)values->data[i]; | |
2606 | |
2607 key = key->semantic(sc); | |
2608 value = value->semantic(sc); | |
2609 | |
2610 keys->data[i] = (void *)key; | |
2611 values->data[i] = (void *)value; | |
2612 } | |
2613 expandTuples(keys); | |
2614 expandTuples(values); | |
2615 if (keys->dim != values->dim) | |
2616 { | |
2617 error("number of keys is %u, must match number of values %u", keys->dim, values->dim); | |
2618 keys->setDim(0); | |
2619 values->setDim(0); | |
2620 } | |
2621 for (size_t i = 0; i < keys->dim; i++) | |
2622 { Expression *key = (Expression *)keys->data[i]; | |
2623 Expression *value = (Expression *)values->data[i]; | |
2624 | |
2625 if (!key->type) | |
2626 error("%s has no value", key->toChars()); | |
2627 if (!value->type) | |
2628 error("%s has no value", value->toChars()); | |
2629 key = resolveProperties(sc, key); | |
2630 value = resolveProperties(sc, value); | |
2631 | |
2632 if (!tkey) | |
2633 tkey = key->type; | |
2634 else | |
2635 key = key->implicitCastTo(sc, tkey); | |
2636 keys->data[i] = (void *)key; | |
2637 | |
2638 if (!tvalue) | |
2639 tvalue = value->type; | |
2640 else | |
2641 value = value->implicitCastTo(sc, tvalue); | |
2642 values->data[i] = (void *)value; | |
2643 } | |
2644 | |
2645 if (!tkey) | |
2646 tkey = Type::tvoid; | |
2647 if (!tvalue) | |
2648 tvalue = Type::tvoid; | |
2649 type = new TypeAArray(tvalue, tkey); | |
2650 type = type->semantic(loc, sc); | |
2651 return this; | |
2652 } | |
2653 | |
2654 int AssocArrayLiteralExp::checkSideEffect(int flag) | |
2655 { int f = 0; | |
2656 | |
2657 for (size_t i = 0; i < keys->dim; i++) | |
2658 { Expression *key = (Expression *)keys->data[i]; | |
2659 Expression *value = (Expression *)values->data[i]; | |
2660 | |
2661 f |= key->checkSideEffect(2); | |
2662 f |= value->checkSideEffect(2); | |
2663 } | |
2664 if (flag == 0 && f == 0) | |
2665 Expression::checkSideEffect(0); | |
2666 return f; | |
2667 } | |
2668 | |
2669 int AssocArrayLiteralExp::isBool(int result) | |
2670 { | |
2671 size_t dim = keys->dim; | |
2672 return result ? (dim != 0) : (dim == 0); | |
2673 } | |
2674 | |
2675 void AssocArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2676 { | |
2677 buf->writeByte('['); | |
2678 for (size_t i = 0; i < keys->dim; i++) | |
2679 { Expression *key = (Expression *)keys->data[i]; | |
2680 Expression *value = (Expression *)values->data[i]; | |
2681 | |
2682 if (i) | |
2683 buf->writeByte(','); | |
2684 expToCBuffer(buf, hgs, key, PREC_assign); | |
2685 buf->writeByte(':'); | |
2686 expToCBuffer(buf, hgs, value, PREC_assign); | |
2687 } | |
2688 buf->writeByte(']'); | |
2689 } | |
2690 | |
2691 void AssocArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
2692 { | |
2693 size_t dim = keys->dim; | |
2694 buf->printf("A%u", dim); | |
2695 for (size_t i = 0; i < dim; i++) | |
2696 { Expression *key = (Expression *)keys->data[i]; | |
2697 Expression *value = (Expression *)values->data[i]; | |
2698 | |
2699 key->toMangleBuffer(buf); | |
2700 value->toMangleBuffer(buf); | |
2701 } | |
2702 } | |
2703 | |
2704 /************************ StructLiteralExp ************************************/ | |
2705 | |
2706 // sd( e1, e2, e3, ... ) | |
2707 | |
2708 StructLiteralExp::StructLiteralExp(Loc loc, StructDeclaration *sd, Expressions *elements) | |
2709 : Expression(loc, TOKstructliteral, sizeof(StructLiteralExp)) | |
2710 { | |
2711 this->sd = sd; | |
2712 this->elements = elements; | |
2713 this->sym = NULL; | |
2714 this->soffset = 0; | |
2715 this->fillHoles = 1; | |
2716 } | |
2717 | |
2718 Expression *StructLiteralExp::syntaxCopy() | |
2719 { | |
2720 return new StructLiteralExp(loc, sd, arraySyntaxCopy(elements)); | |
2721 } | |
2722 | |
2723 Expression *StructLiteralExp::semantic(Scope *sc) | |
2724 { Expression *e; | |
2725 | |
2726 #if LOGSEMANTIC | |
2727 printf("StructLiteralExp::semantic('%s')\n", toChars()); | |
2728 #endif | |
2729 | |
2730 // Run semantic() on each element | |
2731 for (size_t i = 0; i < elements->dim; i++) | |
2732 { e = (Expression *)elements->data[i]; | |
2733 if (!e) | |
2734 continue; | |
2735 e = e->semantic(sc); | |
2736 elements->data[i] = (void *)e; | |
2737 } | |
2738 expandTuples(elements); | |
2739 size_t offset = 0; | |
2740 for (size_t i = 0; i < elements->dim; i++) | |
2741 { e = (Expression *)elements->data[i]; | |
2742 if (!e) | |
2743 continue; | |
2744 | |
2745 if (!e->type) | |
2746 error("%s has no value", e->toChars()); | |
2747 e = resolveProperties(sc, e); | |
2748 if (i >= sd->fields.dim) | |
2749 { error("more initializers than fields of %s", sd->toChars()); | |
2750 break; | |
2751 } | |
2752 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
2753 VarDeclaration *v = s->isVarDeclaration(); | |
2754 assert(v); | |
2755 if (v->offset < offset) | |
2756 error("overlapping initialization for %s", v->toChars()); | |
2757 offset = v->offset + v->type->size(); | |
2758 | |
2759 Type *telem = v->type; | |
2760 while (!e->implicitConvTo(telem) && telem->toBasetype()->ty == Tsarray) | |
2761 { /* Static array initialization, as in: | |
2762 * T[3][5] = e; | |
2763 */ | |
2764 telem = telem->toBasetype()->nextOf(); | |
2765 } | |
2766 | |
2767 e = e->implicitCastTo(sc, telem); | |
2768 | |
2769 elements->data[i] = (void *)e; | |
2770 } | |
2771 | |
2772 /* Fill out remainder of elements[] with default initializers for fields[] | |
2773 */ | |
2774 for (size_t i = elements->dim; i < sd->fields.dim; i++) | |
2775 { Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
2776 VarDeclaration *v = s->isVarDeclaration(); | |
2777 assert(v); | |
2778 | |
2779 if (v->offset < offset) | |
2780 { e = NULL; | |
2781 sd->hasUnions = 1; | |
2782 } | |
2783 else | |
2784 { | |
2785 if (v->init) | |
2786 { e = v->init->toExpression(); | |
2787 if (!e) | |
2788 error("cannot make expression out of initializer for %s", v->toChars()); | |
2789 } | |
2790 else | |
2791 { e = v->type->defaultInit(); | |
2792 e->loc = loc; | |
2793 } | |
2794 offset = v->offset + v->type->size(); | |
2795 } | |
2796 elements->push(e); | |
2797 } | |
2798 | |
2799 type = sd->type; | |
2800 return this; | |
2801 } | |
2802 | |
2803 /************************************** | |
2804 * Gets expression at offset of type. | |
2805 * Returns NULL if not found. | |
2806 */ | |
2807 | |
2808 Expression *StructLiteralExp::getField(Type *type, unsigned offset) | |
2809 { Expression *e = NULL; | |
2810 int i = getFieldIndex(type, offset); | |
2811 | |
2812 if (i != -1) | |
2813 { e = (Expression *)elements->data[i]; | |
2814 if (e) | |
2815 { | |
2816 e = e->copy(); | |
2817 e->type = type; | |
2818 } | |
2819 } | |
2820 return e; | |
2821 } | |
2822 | |
2823 /************************************ | |
2824 * Get index of field. | |
2825 * Returns -1 if not found. | |
2826 */ | |
2827 | |
2828 int StructLiteralExp::getFieldIndex(Type *type, unsigned offset) | |
2829 { | |
2830 /* Find which field offset is by looking at the field offsets | |
2831 */ | |
2832 for (size_t i = 0; i < sd->fields.dim; i++) | |
2833 { | |
2834 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
2835 VarDeclaration *v = s->isVarDeclaration(); | |
2836 assert(v); | |
2837 | |
2838 if (offset == v->offset && | |
2839 type->size() == v->type->size()) | |
2840 { Expression *e = (Expression *)elements->data[i]; | |
2841 if (e) | |
2842 { | |
2843 return i; | |
2844 } | |
2845 break; | |
2846 } | |
2847 } | |
2848 return -1; | |
2849 } | |
2850 | |
2851 | |
2852 Expression *StructLiteralExp::toLvalue(Scope *sc, Expression *e) | |
2853 { | |
2854 return this; | |
2855 } | |
2856 | |
2857 | |
2858 int StructLiteralExp::checkSideEffect(int flag) | |
2859 { int f = 0; | |
2860 | |
2861 for (size_t i = 0; i < elements->dim; i++) | |
2862 { Expression *e = (Expression *)elements->data[i]; | |
2863 if (!e) | |
2864 continue; | |
2865 | |
2866 f |= e->checkSideEffect(2); | |
2867 } | |
2868 if (flag == 0 && f == 0) | |
2869 Expression::checkSideEffect(0); | |
2870 return f; | |
2871 } | |
2872 | |
2873 void StructLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2874 { | |
2875 buf->writestring(sd->toChars()); | |
2876 buf->writeByte('('); | |
2877 argsToCBuffer(buf, elements, hgs); | |
2878 buf->writeByte(')'); | |
2879 } | |
2880 | |
2881 void StructLiteralExp::toMangleBuffer(OutBuffer *buf) | |
2882 { | |
2883 size_t dim = elements ? elements->dim : 0; | |
2884 buf->printf("S%u", dim); | |
2885 for (size_t i = 0; i < dim; i++) | |
2886 { Expression *e = (Expression *)elements->data[i]; | |
2887 if (e) | |
2888 e->toMangleBuffer(buf); | |
2889 else | |
2890 buf->writeByte('v'); // 'v' for void | |
2891 } | |
2892 } | |
2893 | |
2894 /************************ TypeDotIdExp ************************************/ | |
2895 | |
2896 /* Things like: | |
2897 * int.size | |
2898 * foo.size | |
2899 * (foo).size | |
2900 * cast(foo).size | |
2901 */ | |
2902 | |
2903 TypeDotIdExp::TypeDotIdExp(Loc loc, Type *type, Identifier *ident) | |
2904 : Expression(loc, TOKtypedot, sizeof(TypeDotIdExp)) | |
2905 { | |
2906 this->type = type; | |
2907 this->ident = ident; | |
2908 } | |
2909 | |
2910 Expression *TypeDotIdExp::syntaxCopy() | |
2911 { | |
2912 TypeDotIdExp *te = new TypeDotIdExp(loc, type->syntaxCopy(), ident); | |
2913 return te; | |
2914 } | |
2915 | |
2916 Expression *TypeDotIdExp::semantic(Scope *sc) | |
2917 { Expression *e; | |
2918 | |
2919 #if LOGSEMANTIC | |
2920 printf("TypeDotIdExp::semantic()\n"); | |
2921 #endif | |
2922 e = new DotIdExp(loc, new TypeExp(loc, type), ident); | |
2923 e = e->semantic(sc); | |
2924 return e; | |
2925 } | |
2926 | |
2927 void TypeDotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2928 { | |
2929 buf->writeByte('('); | |
2930 type->toCBuffer(buf, NULL, hgs); | |
2931 buf->writeByte(')'); | |
2932 buf->writeByte('.'); | |
2933 buf->writestring(ident->toChars()); | |
2934 } | |
2935 | |
2936 /************************************************************/ | |
2937 | |
2938 // Mainly just a placeholder | |
2939 | |
2940 TypeExp::TypeExp(Loc loc, Type *type) | |
2941 : Expression(loc, TOKtype, sizeof(TypeExp)) | |
2942 { | |
2943 //printf("TypeExp::TypeExp(%s)\n", type->toChars()); | |
2944 this->type = type; | |
2945 } | |
2946 | |
2947 Expression *TypeExp::semantic(Scope *sc) | |
2948 { | |
2949 //printf("TypeExp::semantic(%s)\n", type->toChars()); | |
2950 type = type->semantic(loc, sc); | |
2951 return this; | |
2952 } | |
2953 | |
2954 void TypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2955 { | |
2956 type->toCBuffer(buf, NULL, hgs); | |
2957 } | |
2958 | |
2959 /************************************************************/ | |
2960 | |
2961 // Mainly just a placeholder | |
2962 | |
2963 ScopeExp::ScopeExp(Loc loc, ScopeDsymbol *pkg) | |
2964 : Expression(loc, TOKimport, sizeof(ScopeExp)) | |
2965 { | |
2966 //printf("ScopeExp::ScopeExp(pkg = '%s')\n", pkg->toChars()); | |
2967 //static int count; if (++count == 38) *(char*)0=0; | |
2968 this->sds = pkg; | |
2969 } | |
2970 | |
2971 Expression *ScopeExp::syntaxCopy() | |
2972 { | |
2973 ScopeExp *se = new ScopeExp(loc, (ScopeDsymbol *)sds->syntaxCopy(NULL)); | |
2974 return se; | |
2975 } | |
2976 | |
2977 Expression *ScopeExp::semantic(Scope *sc) | |
2978 { | |
2979 TemplateInstance *ti; | |
2980 ScopeDsymbol *sds2; | |
2981 | |
2982 #if LOGSEMANTIC | |
2983 printf("+ScopeExp::semantic('%s')\n", toChars()); | |
2984 #endif | |
2985 Lagain: | |
2986 ti = sds->isTemplateInstance(); | |
2987 if (ti && !global.errors) | |
2988 { Dsymbol *s; | |
2989 if (!ti->semanticdone) | |
2990 ti->semantic(sc); | |
2991 s = ti->inst->toAlias(); | |
2992 sds2 = s->isScopeDsymbol(); | |
2993 if (!sds2) | |
2994 { Expression *e; | |
2995 | |
2996 //printf("s = %s, '%s'\n", s->kind(), s->toChars()); | |
2997 if (ti->withsym) | |
2998 { | |
2999 // Same as wthis.s | |
3000 e = new VarExp(loc, ti->withsym->withstate->wthis); | |
3001 e = new DotVarExp(loc, e, s->isDeclaration()); | |
3002 } | |
3003 else | |
3004 e = new DsymbolExp(loc, s); | |
3005 e = e->semantic(sc); | |
3006 //printf("-1ScopeExp::semantic()\n"); | |
3007 return e; | |
3008 } | |
3009 if (sds2 != sds) | |
3010 { | |
3011 sds = sds2; | |
3012 goto Lagain; | |
3013 } | |
3014 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3015 } | |
3016 else | |
3017 { | |
3018 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3019 //printf("\tparent = '%s'\n", sds->parent->toChars()); | |
3020 sds->semantic(sc); | |
3021 } | |
3022 type = Type::tvoid; | |
3023 //printf("-2ScopeExp::semantic() %s\n", toChars()); | |
3024 return this; | |
3025 } | |
3026 | |
3027 void ScopeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3028 { | |
3029 if (sds->isTemplateInstance()) | |
3030 { | |
3031 sds->toCBuffer(buf, hgs); | |
3032 } | |
3033 else | |
3034 { | |
3035 buf->writestring(sds->kind()); | |
3036 buf->writestring(" "); | |
3037 buf->writestring(sds->toChars()); | |
3038 } | |
3039 } | |
3040 | |
3041 /********************** TemplateExp **************************************/ | |
3042 | |
3043 // Mainly just a placeholder | |
3044 | |
3045 TemplateExp::TemplateExp(Loc loc, TemplateDeclaration *td) | |
3046 : Expression(loc, TOKtemplate, sizeof(TemplateExp)) | |
3047 { | |
3048 //printf("TemplateExp(): %s\n", td->toChars()); | |
3049 this->td = td; | |
3050 } | |
3051 | |
3052 void TemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3053 { | |
3054 buf->writestring(td->toChars()); | |
3055 } | |
3056 | |
3057 void TemplateExp::rvalue() | |
3058 { | |
3059 error("template %s has no value", toChars()); | |
3060 } | |
3061 | |
3062 /********************** NewExp **************************************/ | |
3063 | |
3064 NewExp::NewExp(Loc loc, Expression *thisexp, Expressions *newargs, | |
3065 Type *newtype, Expressions *arguments) | |
3066 : Expression(loc, TOKnew, sizeof(NewExp)) | |
3067 { | |
3068 this->thisexp = thisexp; | |
3069 this->newargs = newargs; | |
3070 this->newtype = newtype; | |
3071 this->arguments = arguments; | |
3072 member = NULL; | |
3073 allocator = NULL; | |
3074 onstack = 0; | |
3075 } | |
3076 | |
3077 Expression *NewExp::syntaxCopy() | |
3078 { | |
3079 return new NewExp(loc, | |
3080 thisexp ? thisexp->syntaxCopy() : NULL, | |
3081 arraySyntaxCopy(newargs), | |
3082 newtype->syntaxCopy(), arraySyntaxCopy(arguments)); | |
3083 } | |
3084 | |
3085 | |
3086 Expression *NewExp::semantic(Scope *sc) | |
3087 { int i; | |
3088 Type *tb; | |
3089 ClassDeclaration *cdthis = NULL; | |
3090 | |
3091 #if LOGSEMANTIC | |
3092 printf("NewExp::semantic() %s\n", toChars()); | |
3093 if (thisexp) | |
3094 printf("\tthisexp = %s\n", thisexp->toChars()); | |
3095 printf("\tnewtype: %s\n", newtype->toChars()); | |
3096 #endif | |
3097 if (type) // if semantic() already run | |
3098 return this; | |
3099 | |
3100 Lagain: | |
3101 if (thisexp) | |
3102 { thisexp = thisexp->semantic(sc); | |
3103 cdthis = thisexp->type->isClassHandle(); | |
3104 if (cdthis) | |
3105 { | |
3106 sc = sc->push(cdthis); | |
3107 type = newtype->semantic(loc, sc); | |
3108 sc = sc->pop(); | |
3109 } | |
3110 else | |
3111 { | |
3112 error("'this' for nested class must be a class type, not %s", thisexp->type->toChars()); | |
3113 type = newtype->semantic(loc, sc); | |
3114 } | |
3115 } | |
3116 else | |
3117 type = newtype->semantic(loc, sc); | |
3118 newtype = type; // in case type gets cast to something else | |
3119 tb = type->toBasetype(); | |
3120 //printf("tb: %s, deco = %s\n", tb->toChars(), tb->deco); | |
3121 | |
3122 arrayExpressionSemantic(newargs, sc); | |
3123 preFunctionArguments(loc, sc, newargs); | |
3124 arrayExpressionSemantic(arguments, sc); | |
3125 preFunctionArguments(loc, sc, arguments); | |
3126 | |
3127 if (thisexp && tb->ty != Tclass) | |
3128 error("e.new is only for allocating nested classes, not %s", tb->toChars()); | |
3129 | |
3130 if (tb->ty == Tclass) | |
3131 { TypeFunction *tf; | |
3132 | |
3133 TypeClass *tc = (TypeClass *)(tb); | |
3134 ClassDeclaration *cd = tc->sym->isClassDeclaration(); | |
3135 if (cd->isInterfaceDeclaration()) | |
3136 error("cannot create instance of interface %s", cd->toChars()); | |
3137 else if (cd->isAbstract()) | |
3138 error("cannot create instance of abstract class %s", cd->toChars()); | |
3139 checkDeprecated(sc, cd); | |
3140 if (cd->isNested()) | |
3141 { /* We need a 'this' pointer for the nested class. | |
3142 * Ensure we have the right one. | |
3143 */ | |
3144 Dsymbol *s = cd->toParent2(); | |
3145 ClassDeclaration *cdn = s->isClassDeclaration(); | |
3146 | |
3147 //printf("isNested, cdn = %s\n", cdn ? cdn->toChars() : "null"); | |
3148 if (cdn) | |
3149 { | |
3150 if (!cdthis) | |
3151 { | |
3152 // Supply an implicit 'this' and try again | |
3153 thisexp = new ThisExp(loc); | |
3154 for (Dsymbol *sp = sc->parent; 1; sp = sp->parent) | |
3155 { if (!sp) | |
3156 { | |
3157 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3158 break; | |
3159 } | |
3160 ClassDeclaration *cdp = sp->isClassDeclaration(); | |
3161 if (!cdp) | |
3162 continue; | |
3163 if (cdp == cdn || cdn->isBaseOf(cdp, NULL)) | |
3164 break; | |
3165 // Add a '.outer' and try again | |
3166 thisexp = new DotIdExp(loc, thisexp, Id::outer); | |
3167 } | |
3168 if (!global.errors) | |
3169 goto Lagain; | |
3170 } | |
3171 if (cdthis) | |
3172 { | |
3173 //printf("cdthis = %s\n", cdthis->toChars()); | |
3174 if (cdthis != cdn && !cdn->isBaseOf(cdthis, NULL)) | |
3175 error("'this' for nested class must be of type %s, not %s", cdn->toChars(), thisexp->type->toChars()); | |
3176 } | |
3177 #if 0 | |
3178 else | |
3179 { | |
3180 for (Dsymbol *sf = sc->func; 1; sf= sf->toParent2()->isFuncDeclaration()) | |
3181 { | |
3182 if (!sf) | |
3183 { | |
3184 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3185 break; | |
3186 } | |
3187 printf("sf = %s\n", sf->toChars()); | |
3188 AggregateDeclaration *ad = sf->isThis(); | |
3189 if (ad && (ad == cdn || cdn->isBaseOf(ad->isClassDeclaration(), NULL))) | |
3190 break; | |
3191 } | |
3192 } | |
3193 #endif | |
3194 } | |
3195 else if (thisexp) | |
3196 error("e.new is only for allocating nested classes"); | |
3197 } | |
3198 else if (thisexp) | |
3199 error("e.new is only for allocating nested classes"); | |
3200 | |
3201 FuncDeclaration *f = cd->ctor; | |
3202 if (f) | |
3203 { | |
3204 assert(f); | |
3205 f = f->overloadResolve(loc, arguments); | |
3206 checkDeprecated(sc, f); | |
3207 member = f->isCtorDeclaration(); | |
3208 assert(member); | |
3209 | |
3210 cd->accessCheck(loc, sc, member); | |
3211 | |
3212 tf = (TypeFunction *)f->type; | |
3213 type = tf->next; | |
3214 | |
3215 if (!arguments) | |
3216 arguments = new Expressions(); | |
3217 functionArguments(loc, sc, tf, arguments); | |
3218 } | |
3219 else | |
3220 { | |
3221 if (arguments && arguments->dim) | |
3222 error("no constructor for %s", cd->toChars()); | |
3223 } | |
3224 | |
3225 if (cd->aggNew) | |
3226 { Expression *e; | |
3227 | |
3228 f = cd->aggNew; | |
3229 | |
3230 // Prepend the uint size argument to newargs[] | |
3231 e = new IntegerExp(loc, cd->size(loc), Type::tuns32); | |
3232 if (!newargs) | |
3233 newargs = new Expressions(); | |
3234 newargs->shift(e); | |
3235 | |
3236 f = f->overloadResolve(loc, newargs); | |
3237 allocator = f->isNewDeclaration(); | |
3238 assert(allocator); | |
3239 | |
3240 tf = (TypeFunction *)f->type; | |
3241 functionArguments(loc, sc, tf, newargs); | |
3242 } | |
3243 else | |
3244 { | |
3245 if (newargs && newargs->dim) | |
3246 error("no allocator for %s", cd->toChars()); | |
3247 } | |
3248 | |
3249 } | |
3250 else if (tb->ty == Tstruct) | |
3251 { | |
3252 TypeStruct *ts = (TypeStruct *)tb; | |
3253 StructDeclaration *sd = ts->sym; | |
3254 FuncDeclaration *f = sd->aggNew; | |
3255 TypeFunction *tf; | |
3256 | |
3257 if (arguments && arguments->dim) | |
3258 error("no constructor for %s", type->toChars()); | |
3259 | |
3260 if (f) | |
3261 { | |
3262 Expression *e; | |
3263 | |
3264 // Prepend the uint size argument to newargs[] | |
3265 e = new IntegerExp(loc, sd->size(loc), Type::tuns32); | |
3266 if (!newargs) | |
3267 newargs = new Expressions(); | |
3268 newargs->shift(e); | |
3269 | |
3270 f = f->overloadResolve(loc, newargs); | |
3271 allocator = f->isNewDeclaration(); | |
3272 assert(allocator); | |
3273 | |
3274 tf = (TypeFunction *)f->type; | |
3275 functionArguments(loc, sc, tf, newargs); | |
3276 | |
3277 e = new VarExp(loc, f); | |
3278 e = new CallExp(loc, e, newargs); | |
3279 e = e->semantic(sc); | |
3280 e->type = type->pointerTo(); | |
3281 return e; | |
3282 } | |
3283 | |
3284 type = type->pointerTo(); | |
3285 } | |
3286 else if (tb->ty == Tarray && (arguments && arguments->dim)) | |
3287 { | |
3288 for (size_t i = 0; i < arguments->dim; i++) | |
3289 { | |
3290 if (tb->ty != Tarray) | |
3291 { error("too many arguments for array"); | |
3292 arguments->dim = i; | |
3293 break; | |
3294 } | |
3295 | |
3296 Expression *arg = (Expression *)arguments->data[i]; | |
3297 arg = resolveProperties(sc, arg); | |
3298 arg = arg->implicitCastTo(sc, Type::tsize_t); | |
3299 if (arg->op == TOKint64 && (long long)arg->toInteger() < 0) | |
3300 error("negative array index %s", arg->toChars()); | |
3301 arguments->data[i] = (void *) arg; | |
3302 tb = tb->next->toBasetype(); | |
3303 } | |
3304 } | |
3305 else if (tb->isscalar()) | |
3306 { | |
3307 if (arguments && arguments->dim) | |
3308 error("no constructor for %s", type->toChars()); | |
3309 | |
3310 type = type->pointerTo(); | |
3311 } | |
3312 else | |
3313 { | |
3314 error("new can only create structs, dynamic arrays or class objects, not %s's", type->toChars()); | |
3315 type = type->pointerTo(); | |
3316 } | |
3317 | |
3318 //printf("NewExp: '%s'\n", toChars()); | |
3319 //printf("NewExp:type '%s'\n", type->toChars()); | |
3320 | |
3321 return this; | |
3322 } | |
3323 | |
3324 int NewExp::checkSideEffect(int flag) | |
3325 { | |
3326 return 1; | |
3327 } | |
3328 | |
3329 void NewExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3330 { int i; | |
3331 | |
3332 if (thisexp) | |
3333 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3334 buf->writeByte('.'); | |
3335 } | |
3336 buf->writestring("new "); | |
3337 if (newargs && newargs->dim) | |
3338 { | |
3339 buf->writeByte('('); | |
3340 argsToCBuffer(buf, newargs, hgs); | |
3341 buf->writeByte(')'); | |
3342 } | |
3343 newtype->toCBuffer(buf, NULL, hgs); | |
3344 if (arguments && arguments->dim) | |
3345 { | |
3346 buf->writeByte('('); | |
3347 argsToCBuffer(buf, arguments, hgs); | |
3348 buf->writeByte(')'); | |
3349 } | |
3350 } | |
3351 | |
3352 /********************** NewAnonClassExp **************************************/ | |
3353 | |
3354 NewAnonClassExp::NewAnonClassExp(Loc loc, Expression *thisexp, | |
3355 Expressions *newargs, ClassDeclaration *cd, Expressions *arguments) | |
3356 : Expression(loc, TOKnewanonclass, sizeof(NewAnonClassExp)) | |
3357 { | |
3358 this->thisexp = thisexp; | |
3359 this->newargs = newargs; | |
3360 this->cd = cd; | |
3361 this->arguments = arguments; | |
3362 } | |
3363 | |
3364 Expression *NewAnonClassExp::syntaxCopy() | |
3365 { | |
3366 return new NewAnonClassExp(loc, | |
3367 thisexp ? thisexp->syntaxCopy() : NULL, | |
3368 arraySyntaxCopy(newargs), | |
3369 (ClassDeclaration *)cd->syntaxCopy(NULL), | |
3370 arraySyntaxCopy(arguments)); | |
3371 } | |
3372 | |
3373 | |
3374 Expression *NewAnonClassExp::semantic(Scope *sc) | |
3375 { | |
3376 #if LOGSEMANTIC | |
3377 printf("NewAnonClassExp::semantic() %s\n", toChars()); | |
3378 //printf("type: %s\n", type->toChars()); | |
3379 #endif | |
3380 | |
3381 Expression *d = new DeclarationExp(loc, cd); | |
3382 d = d->semantic(sc); | |
3383 | |
3384 Expression *n = new NewExp(loc, thisexp, newargs, cd->type, arguments); | |
3385 | |
3386 Expression *c = new CommaExp(loc, d, n); | |
3387 return c->semantic(sc); | |
3388 } | |
3389 | |
3390 int NewAnonClassExp::checkSideEffect(int flag) | |
3391 { | |
3392 return 1; | |
3393 } | |
3394 | |
3395 void NewAnonClassExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3396 { int i; | |
3397 | |
3398 if (thisexp) | |
3399 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3400 buf->writeByte('.'); | |
3401 } | |
3402 buf->writestring("new"); | |
3403 if (newargs && newargs->dim) | |
3404 { | |
3405 buf->writeByte('('); | |
3406 argsToCBuffer(buf, newargs, hgs); | |
3407 buf->writeByte(')'); | |
3408 } | |
3409 buf->writestring(" class "); | |
3410 if (arguments && arguments->dim) | |
3411 { | |
3412 buf->writeByte('('); | |
3413 argsToCBuffer(buf, arguments, hgs); | |
3414 buf->writeByte(')'); | |
3415 } | |
3416 //buf->writestring(" { }"); | |
3417 if (cd) | |
3418 { | |
3419 cd->toCBuffer(buf, hgs); | |
3420 } | |
3421 } | |
3422 | |
3423 /********************** SymOffExp **************************************/ | |
3424 | |
3425 SymOffExp::SymOffExp(Loc loc, Declaration *var, unsigned offset) | |
3426 : Expression(loc, TOKsymoff, sizeof(SymOffExp)) | |
3427 { | |
3428 assert(var); | |
3429 this->var = var; | |
3430 this->offset = offset; | |
3431 VarDeclaration *v = var->isVarDeclaration(); | |
3432 if (v && v->needThis()) | |
3433 error("need 'this' for address of %s", v->toChars()); | |
3434 } | |
3435 | |
3436 Expression *SymOffExp::semantic(Scope *sc) | |
3437 { | |
3438 #if LOGSEMANTIC | |
3439 printf("SymOffExp::semantic('%s')\n", toChars()); | |
3440 #endif | |
3441 //var->semantic(sc); | |
3442 if (!type) | |
3443 type = var->type->pointerTo(); | |
3444 VarDeclaration *v = var->isVarDeclaration(); | |
3445 if (v) | |
3446 v->checkNestedReference(sc, loc); | |
3447 return this; | |
3448 } | |
3449 | |
3450 int SymOffExp::isBool(int result) | |
3451 { | |
3452 return result ? TRUE : FALSE; | |
3453 } | |
3454 | |
3455 void SymOffExp::checkEscape() | |
3456 { | |
3457 VarDeclaration *v = var->isVarDeclaration(); | |
3458 if (v) | |
3459 { | |
3460 if (!v->isDataseg()) | |
3461 error("escaping reference to local variable %s", v->toChars()); | |
3462 } | |
3463 } | |
3464 | |
3465 void SymOffExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3466 { | |
3467 if (offset) | |
3468 buf->printf("(& %s+%u)", var->toChars(), offset); | |
3469 else | |
3470 buf->printf("& %s", var->toChars()); | |
3471 } | |
3472 | |
3473 /******************************** VarExp **************************/ | |
3474 | |
3475 VarExp::VarExp(Loc loc, Declaration *var) | |
3476 : Expression(loc, TOKvar, sizeof(VarExp)) | |
3477 { | |
3478 //printf("VarExp(this = %p, '%s')\n", this, var->toChars()); | |
3479 this->var = var; | |
3480 this->type = var->type; | |
3481 } | |
3482 | |
3483 int VarExp::equals(Object *o) | |
3484 { VarExp *ne; | |
3485 | |
3486 if (this == o || | |
3487 (((Expression *)o)->op == TOKvar && | |
3488 ((ne = (VarExp *)o), type->equals(ne->type)) && | |
3489 var == ne->var)) | |
3490 return 1; | |
3491 return 0; | |
3492 } | |
3493 | |
3494 Expression *VarExp::semantic(Scope *sc) | |
3495 { FuncLiteralDeclaration *fd; | |
3496 | |
3497 #if LOGSEMANTIC | |
3498 printf("VarExp::semantic(%s)\n", toChars()); | |
3499 #endif | |
3500 if (!type) | |
3501 { type = var->type; | |
3502 #if 0 | |
3503 if (var->storage_class & STClazy) | |
3504 { | |
3505 TypeFunction *tf = new TypeFunction(NULL, type, 0, LINKd); | |
3506 type = new TypeDelegate(tf); | |
3507 type = type->semantic(loc, sc); | |
3508 } | |
3509 #endif | |
3510 } | |
3511 | |
3512 VarDeclaration *v = var->isVarDeclaration(); | |
3513 if (v) | |
3514 { | |
3515 if (v->isConst() && type->toBasetype()->ty != Tsarray && v->init) | |
3516 { | |
3517 ExpInitializer *ei = v->init->isExpInitializer(); | |
3518 if (ei) | |
3519 { | |
3520 //ei->exp->implicitCastTo(sc, type)->print(); | |
3521 return ei->exp->implicitCastTo(sc, type); | |
3522 } | |
3523 } | |
3524 v->checkNestedReference(sc, loc); | |
3525 } | |
3526 #if 0 | |
3527 else if ((fd = var->isFuncLiteralDeclaration()) != NULL) | |
3528 { Expression *e; | |
3529 e = new FuncExp(loc, fd); | |
3530 e->type = type; | |
3531 return e; | |
3532 } | |
3533 #endif | |
3534 return this; | |
3535 } | |
3536 | |
3537 char *VarExp::toChars() | |
3538 { | |
3539 return var->toChars(); | |
3540 } | |
3541 | |
3542 void VarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3543 { | |
3544 buf->writestring(var->toChars()); | |
3545 } | |
3546 | |
3547 void VarExp::checkEscape() | |
3548 { | |
3549 VarDeclaration *v = var->isVarDeclaration(); | |
3550 if (v) | |
3551 { Type *tb = v->type->toBasetype(); | |
3552 // if reference type | |
3553 if (tb->ty == Tarray || tb->ty == Tsarray || tb->ty == Tclass) | |
3554 { | |
3555 if ((v->isAuto() || v->isScope()) && !v->noauto) | |
3556 error("escaping reference to auto local %s", v->toChars()); | |
3557 else if (v->storage_class & STCvariadic) | |
3558 error("escaping reference to variadic parameter %s", v->toChars()); | |
3559 } | |
3560 } | |
3561 } | |
3562 | |
3563 Expression *VarExp::toLvalue(Scope *sc, Expression *e) | |
3564 { | |
3565 #if 0 | |
3566 tym = tybasic(e1->ET->Tty); | |
3567 if (!(tyscalar(tym) || | |
3568 tym == TYstruct || | |
3569 tym == TYarray && e->Eoper == TOKaddr)) | |
3570 synerr(EM_lvalue); // lvalue expected | |
3571 #endif | |
3572 if (var->storage_class & STClazy) | |
3573 error("lazy variables cannot be lvalues"); | |
3574 return this; | |
3575 } | |
3576 | |
3577 Expression *VarExp::modifiableLvalue(Scope *sc, Expression *e) | |
3578 { | |
3579 //printf("VarExp::modifiableLvalue('%s')\n", var->toChars()); | |
3580 if (sc->incontract && var->isParameter()) | |
3581 error("cannot modify parameter '%s' in contract", var->toChars()); | |
3582 | |
3583 if (type && type->toBasetype()->ty == Tsarray) | |
3584 error("cannot change reference to static array '%s'", var->toChars()); | |
3585 | |
3586 VarDeclaration *v = var->isVarDeclaration(); | |
3587 if (v && v->canassign == 0 && | |
3588 (var->isConst() || (global.params.Dversion > 1 && var->isFinal()))) | |
3589 error("cannot modify final variable '%s'", var->toChars()); | |
3590 | |
3591 if (var->isCtorinit()) | |
3592 { // It's only modifiable if inside the right constructor | |
3593 Dsymbol *s = sc->func; | |
3594 while (1) | |
3595 { | |
3596 FuncDeclaration *fd = NULL; | |
3597 if (s) | |
3598 fd = s->isFuncDeclaration(); | |
3599 if (fd && | |
3600 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
3601 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
3602 fd->toParent() == var->toParent() | |
3603 ) | |
3604 { | |
3605 VarDeclaration *v = var->isVarDeclaration(); | |
3606 assert(v); | |
3607 v->ctorinit = 1; | |
3608 //printf("setting ctorinit\n"); | |
3609 } | |
3610 else | |
3611 { | |
3612 if (s) | |
3613 { s = s->toParent2(); | |
3614 continue; | |
3615 } | |
3616 else | |
3617 { | |
3618 const char *p = var->isStatic() ? "static " : ""; | |
3619 error("can only initialize %sconst %s inside %sconstructor", | |
3620 p, var->toChars(), p); | |
3621 } | |
3622 } | |
3623 break; | |
3624 } | |
3625 } | |
3626 | |
3627 // See if this expression is a modifiable lvalue (i.e. not const) | |
3628 return toLvalue(sc, e); | |
3629 } | |
3630 | |
3631 | |
3632 /******************************** TupleExp **************************/ | |
3633 | |
3634 TupleExp::TupleExp(Loc loc, Expressions *exps) | |
3635 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
3636 { | |
3637 //printf("TupleExp(this = %p)\n", this); | |
3638 this->exps = exps; | |
3639 this->type = NULL; | |
3640 } | |
3641 | |
3642 | |
3643 TupleExp::TupleExp(Loc loc, TupleDeclaration *tup) | |
3644 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
3645 { | |
3646 exps = new Expressions(); | |
3647 type = NULL; | |
3648 | |
3649 exps->reserve(tup->objects->dim); | |
3650 for (size_t i = 0; i < tup->objects->dim; i++) | |
3651 { Object *o = (Object *)tup->objects->data[i]; | |
3652 if (o->dyncast() == DYNCAST_EXPRESSION) | |
3653 { | |
3654 Expression *e = (Expression *)o; | |
3655 e = e->syntaxCopy(); | |
3656 exps->push(e); | |
3657 } | |
3658 else if (o->dyncast() == DYNCAST_DSYMBOL) | |
3659 { | |
3660 Dsymbol *s = (Dsymbol *)o; | |
3661 Expression *e = new DsymbolExp(loc, s); | |
3662 exps->push(e); | |
3663 } | |
3664 else if (o->dyncast() == DYNCAST_TYPE) | |
3665 { | |
3666 Type *t = (Type *)o; | |
3667 Expression *e = new TypeExp(loc, t); | |
3668 exps->push(e); | |
3669 } | |
3670 else | |
3671 { | |
3672 error("%s is not an expression", o->toChars()); | |
3673 } | |
3674 } | |
3675 } | |
3676 | |
3677 int TupleExp::equals(Object *o) | |
3678 { TupleExp *ne; | |
3679 | |
3680 if (this == o) | |
3681 return 1; | |
3682 if (((Expression *)o)->op == TOKtuple) | |
3683 { | |
3684 TupleExp *te = (TupleExp *)o; | |
3685 if (exps->dim != te->exps->dim) | |
3686 return 0; | |
3687 for (size_t i = 0; i < exps->dim; i++) | |
3688 { Expression *e1 = (Expression *)exps->data[i]; | |
3689 Expression *e2 = (Expression *)te->exps->data[i]; | |
3690 | |
3691 if (!e1->equals(e2)) | |
3692 return 0; | |
3693 } | |
3694 return 1; | |
3695 } | |
3696 return 0; | |
3697 } | |
3698 | |
3699 Expression *TupleExp::syntaxCopy() | |
3700 { | |
3701 return new TupleExp(loc, arraySyntaxCopy(exps)); | |
3702 } | |
3703 | |
3704 Expression *TupleExp::semantic(Scope *sc) | |
3705 { | |
3706 #if LOGSEMANTIC | |
3707 printf("+TupleExp::semantic(%s)\n", toChars()); | |
3708 #endif | |
3709 if (type) | |
3710 return this; | |
3711 | |
3712 // Run semantic() on each argument | |
3713 for (size_t i = 0; i < exps->dim; i++) | |
3714 { Expression *e = (Expression *)exps->data[i]; | |
3715 | |
3716 e = e->semantic(sc); | |
3717 if (!e->type) | |
3718 { error("%s has no value", e->toChars()); | |
3719 e->type = Type::terror; | |
3720 } | |
3721 exps->data[i] = (void *)e; | |
3722 } | |
3723 | |
3724 expandTuples(exps); | |
3725 if (0 && exps->dim == 1) | |
3726 { | |
3727 return (Expression *)exps->data[0]; | |
3728 } | |
3729 type = new TypeTuple(exps); | |
3730 //printf("-TupleExp::semantic(%s)\n", toChars()); | |
3731 return this; | |
3732 } | |
3733 | |
3734 void TupleExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3735 { | |
3736 buf->writestring("tuple("); | |
3737 argsToCBuffer(buf, exps, hgs); | |
3738 buf->writeByte(')'); | |
3739 } | |
3740 | |
3741 int TupleExp::checkSideEffect(int flag) | |
3742 { int f = 0; | |
3743 | |
3744 for (int i = 0; i < exps->dim; i++) | |
3745 { Expression *e = (Expression *)exps->data[i]; | |
3746 | |
3747 f |= e->checkSideEffect(2); | |
3748 } | |
3749 if (flag == 0 && f == 0) | |
3750 Expression::checkSideEffect(0); | |
3751 return f; | |
3752 } | |
3753 | |
3754 void TupleExp::checkEscape() | |
3755 { | |
3756 for (size_t i = 0; i < exps->dim; i++) | |
3757 { Expression *e = (Expression *)exps->data[i]; | |
3758 e->checkEscape(); | |
3759 } | |
3760 } | |
3761 | |
3762 /******************************** FuncExp *********************************/ | |
3763 | |
3764 FuncExp::FuncExp(Loc loc, FuncLiteralDeclaration *fd) | |
3765 : Expression(loc, TOKfunction, sizeof(FuncExp)) | |
3766 { | |
3767 this->fd = fd; | |
3768 } | |
3769 | |
3770 Expression *FuncExp::syntaxCopy() | |
3771 { | |
3772 return new FuncExp(loc, (FuncLiteralDeclaration *)fd->syntaxCopy(NULL)); | |
3773 } | |
3774 | |
3775 Expression *FuncExp::semantic(Scope *sc) | |
3776 { | |
3777 #if LOGSEMANTIC | |
3778 printf("FuncExp::semantic(%s)\n", toChars()); | |
3779 #endif | |
3780 if (!type) | |
3781 { | |
3782 fd->semantic(sc); | |
3783 fd->parent = sc->parent; | |
3784 if (global.errors) | |
3785 { | |
3786 if (!fd->type->next) | |
3787 fd->type->next = Type::terror; | |
3788 } | |
3789 else | |
3790 { | |
3791 fd->semantic2(sc); | |
3792 if (!global.errors) | |
3793 { | |
3794 fd->semantic3(sc); | |
3795 | |
3796 if (!global.errors && global.params.useInline) | |
3797 fd->inlineScan(); | |
3798 } | |
3799 } | |
3800 | |
3801 // Type is a "delegate to" or "pointer to" the function literal | |
3802 if (fd->isNested()) | |
3803 { | |
3804 type = new TypeDelegate(fd->type); | |
3805 type = type->semantic(loc, sc); | |
3806 } | |
3807 else | |
3808 { | |
3809 type = fd->type->pointerTo(); | |
3810 } | |
3811 } | |
3812 return this; | |
3813 } | |
3814 | |
3815 char *FuncExp::toChars() | |
3816 { | |
3817 return fd->toChars(); | |
3818 } | |
3819 | |
3820 void FuncExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3821 { | |
3822 buf->writestring(fd->toChars()); | |
3823 } | |
3824 | |
3825 | |
3826 /******************************** DeclarationExp **************************/ | |
3827 | |
3828 DeclarationExp::DeclarationExp(Loc loc, Dsymbol *declaration) | |
3829 : Expression(loc, TOKdeclaration, sizeof(DeclarationExp)) | |
3830 { | |
3831 this->declaration = declaration; | |
3832 } | |
3833 | |
3834 Expression *DeclarationExp::syntaxCopy() | |
3835 { | |
3836 return new DeclarationExp(loc, declaration->syntaxCopy(NULL)); | |
3837 } | |
3838 | |
3839 Expression *DeclarationExp::semantic(Scope *sc) | |
3840 { | |
3841 if (type) | |
3842 return this; | |
3843 | |
3844 #if LOGSEMANTIC | |
3845 printf("DeclarationExp::semantic() %s\n", toChars()); | |
3846 #endif | |
3847 | |
3848 /* This is here to support extern(linkage) declaration, | |
3849 * where the extern(linkage) winds up being an AttribDeclaration | |
3850 * wrapper. | |
3851 */ | |
3852 Dsymbol *s = declaration; | |
3853 | |
3854 AttribDeclaration *ad = declaration->isAttribDeclaration(); | |
3855 if (ad) | |
3856 { | |
3857 if (ad->decl && ad->decl->dim == 1) | |
3858 s = (Dsymbol *)ad->decl->data[0]; | |
3859 } | |
3860 | |
3861 if (s->isVarDeclaration()) | |
3862 { // Do semantic() on initializer first, so: | |
3863 // int a = a; | |
3864 // will be illegal. | |
3865 declaration->semantic(sc); | |
3866 s->parent = sc->parent; | |
3867 } | |
3868 | |
3869 //printf("inserting '%s' %p into sc = %p\n", s->toChars(), s, sc); | |
3870 // Insert into both local scope and function scope. | |
3871 // Must be unique in both. | |
3872 if (s->ident) | |
3873 { | |
3874 if (!sc->insert(s)) | |
3875 error("declaration %s is already defined", s->toPrettyChars()); | |
3876 else if (sc->func) | |
3877 { VarDeclaration *v = s->isVarDeclaration(); | |
3878 if ((s->isFuncDeclaration() /*|| v && v->storage_class & STCstatic*/) && | |
3879 !sc->func->localsymtab->insert(s)) | |
3880 error("declaration %s is already defined in another scope in %s", s->toPrettyChars(), sc->func->toChars()); | |
3881 else if (!global.params.useDeprecated) | |
3882 { // Disallow shadowing | |
3883 | |
3884 for (Scope *scx = sc->enclosing; scx && scx->func == sc->func; scx = scx->enclosing) | |
3885 { Dsymbol *s2; | |
3886 | |
3887 if (scx->scopesym && scx->scopesym->symtab && | |
3888 (s2 = scx->scopesym->symtab->lookup(s->ident)) != NULL && | |
3889 s != s2) | |
3890 { | |
3891 error("shadowing declaration %s is deprecated", s->toPrettyChars()); | |
3892 } | |
3893 } | |
3894 } | |
3895 } | |
3896 } | |
3897 if (!s->isVarDeclaration()) | |
3898 { | |
3899 declaration->semantic(sc); | |
3900 s->parent = sc->parent; | |
3901 } | |
3902 if (!global.errors) | |
3903 { | |
3904 declaration->semantic2(sc); | |
3905 if (!global.errors) | |
3906 { | |
3907 declaration->semantic3(sc); | |
3908 | |
3909 if (!global.errors && global.params.useInline) | |
3910 declaration->inlineScan(); | |
3911 } | |
3912 } | |
3913 | |
3914 type = Type::tvoid; | |
3915 return this; | |
3916 } | |
3917 | |
3918 int DeclarationExp::checkSideEffect(int flag) | |
3919 { | |
3920 return 1; | |
3921 } | |
3922 | |
3923 void DeclarationExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3924 { | |
3925 declaration->toCBuffer(buf, hgs); | |
3926 } | |
3927 | |
3928 | |
3929 /************************ TypeidExp ************************************/ | |
3930 | |
3931 /* | |
3932 * typeid(int) | |
3933 */ | |
3934 | |
3935 TypeidExp::TypeidExp(Loc loc, Type *typeidType) | |
3936 : Expression(loc, TOKtypeid, sizeof(TypeidExp)) | |
3937 { | |
3938 this->typeidType = typeidType; | |
3939 } | |
3940 | |
3941 | |
3942 Expression *TypeidExp::syntaxCopy() | |
3943 { | |
3944 return new TypeidExp(loc, typeidType->syntaxCopy()); | |
3945 } | |
3946 | |
3947 | |
3948 Expression *TypeidExp::semantic(Scope *sc) | |
3949 { Expression *e; | |
3950 | |
3951 #if LOGSEMANTIC | |
3952 printf("TypeidExp::semantic()\n"); | |
3953 #endif | |
3954 typeidType = typeidType->semantic(loc, sc); | |
3955 e = typeidType->getTypeInfo(sc); | |
3956 return e; | |
3957 } | |
3958 | |
3959 void TypeidExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3960 { | |
3961 buf->writestring("typeid("); | |
3962 typeidType->toCBuffer(buf, NULL, hgs); | |
3963 buf->writeByte(')'); | |
3964 } | |
3965 | |
3966 /************************************************************/ | |
3967 | |
3968 HaltExp::HaltExp(Loc loc) | |
3969 : Expression(loc, TOKhalt, sizeof(HaltExp)) | |
3970 { | |
3971 } | |
3972 | |
3973 Expression *HaltExp::semantic(Scope *sc) | |
3974 { | |
3975 #if LOGSEMANTIC | |
3976 printf("HaltExp::semantic()\n"); | |
3977 #endif | |
3978 type = Type::tvoid; | |
3979 return this; | |
3980 } | |
3981 | |
3982 int HaltExp::checkSideEffect(int flag) | |
3983 { | |
3984 return 1; | |
3985 } | |
3986 | |
3987 void HaltExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3988 { | |
3989 buf->writestring("halt"); | |
3990 } | |
3991 | |
3992 /************************************************************/ | |
3993 | |
19 | 3994 IsExp::IsExp(Loc loc, Type *targ, Identifier *id, enum TOK tok, |
1 | 3995 Type *tspec, enum TOK tok2) |
19 | 3996 : Expression(loc, TOKis, sizeof(IsExp)) |
1 | 3997 { |
3998 this->targ = targ; | |
3999 this->id = id; | |
4000 this->tok = tok; | |
4001 this->tspec = tspec; | |
4002 this->tok2 = tok2; | |
4003 } | |
4004 | |
19 | 4005 Expression *IsExp::syntaxCopy() |
4006 { | |
4007 return new IsExp(loc, | |
1 | 4008 targ->syntaxCopy(), |
4009 id, | |
4010 tok, | |
4011 tspec ? tspec->syntaxCopy() : NULL, | |
4012 tok2); | |
4013 } | |
4014 | |
19 | 4015 Expression *IsExp::semantic(Scope *sc) |
1 | 4016 { Type *tded; |
4017 | |
19 | 4018 //printf("IsExp::semantic()\n"); |
1 | 4019 if (id && !(sc->flags & SCOPEstaticif)) |
4020 error("can only declare type aliases within static if conditionals"); | |
4021 | |
4022 unsigned errors_save = global.errors; | |
4023 global.errors = 0; | |
4024 global.gag++; // suppress printing of error messages | |
4025 targ = targ->semantic(loc, sc); | |
4026 global.gag--; | |
4027 unsigned gerrors = global.errors; | |
4028 global.errors = errors_save; | |
4029 | |
4030 if (gerrors) // if any errors happened | |
4031 { // then condition is false | |
4032 goto Lno; | |
4033 } | |
4034 else if (tok2 != TOKreserved) | |
4035 { | |
4036 switch (tok2) | |
4037 { | |
4038 case TOKtypedef: | |
4039 if (targ->ty != Ttypedef) | |
4040 goto Lno; | |
4041 tded = ((TypeTypedef *)targ)->sym->basetype; | |
4042 break; | |
4043 | |
4044 case TOKstruct: | |
4045 if (targ->ty != Tstruct) | |
4046 goto Lno; | |
4047 if (((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4048 goto Lno; | |
4049 tded = targ; | |
4050 break; | |
4051 | |
4052 case TOKunion: | |
4053 if (targ->ty != Tstruct) | |
4054 goto Lno; | |
4055 if (!((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4056 goto Lno; | |
4057 tded = targ; | |
4058 break; | |
4059 | |
4060 case TOKclass: | |
4061 if (targ->ty != Tclass) | |
4062 goto Lno; | |
4063 if (((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4064 goto Lno; | |
4065 tded = targ; | |
4066 break; | |
4067 | |
4068 case TOKinterface: | |
4069 if (targ->ty != Tclass) | |
4070 goto Lno; | |
4071 if (!((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4072 goto Lno; | |
4073 tded = targ; | |
4074 break; | |
4075 | |
4076 case TOKsuper: | |
4077 // If class or interface, get the base class and interfaces | |
4078 if (targ->ty != Tclass) | |
4079 goto Lno; | |
4080 else | |
4081 { ClassDeclaration *cd = ((TypeClass *)targ)->sym; | |
4082 Arguments *args = new Arguments; | |
4083 args->reserve(cd->baseclasses.dim); | |
4084 for (size_t i = 0; i < cd->baseclasses.dim; i++) | |
4085 { BaseClass *b = (BaseClass *)cd->baseclasses.data[i]; | |
4086 args->push(new Argument(STCin, b->type, NULL, NULL)); | |
4087 } | |
4088 tded = new TypeTuple(args); | |
4089 } | |
4090 break; | |
4091 | |
4092 case TOKenum: | |
4093 if (targ->ty != Tenum) | |
4094 goto Lno; | |
4095 tded = ((TypeEnum *)targ)->sym->memtype; | |
4096 break; | |
4097 | |
4098 case TOKdelegate: | |
4099 if (targ->ty != Tdelegate) | |
4100 goto Lno; | |
4101 tded = targ->next; // the underlying function type | |
4102 break; | |
4103 | |
4104 case TOKfunction: | |
4105 { if (targ->ty != Tfunction) | |
4106 goto Lno; | |
4107 tded = targ; | |
4108 | |
4109 /* Generate tuple from function parameter types. | |
4110 */ | |
4111 assert(tded->ty == Tfunction); | |
4112 Arguments *params = ((TypeFunction *)tded)->parameters; | |
4113 size_t dim = Argument::dim(params); | |
4114 Arguments *args = new Arguments; | |
4115 args->reserve(dim); | |
4116 for (size_t i = 0; i < dim; i++) | |
4117 { Argument *arg = Argument::getNth(params, i); | |
4118 assert(arg && arg->type); | |
4119 args->push(new Argument(arg->storageClass, arg->type, NULL, NULL)); | |
4120 } | |
4121 tded = new TypeTuple(args); | |
4122 break; | |
4123 } | |
4124 case TOKreturn: | |
4125 /* Get the 'return type' for the function, | |
4126 * delegate, or pointer to function. | |
4127 */ | |
4128 if (targ->ty == Tfunction) | |
4129 tded = targ->next; | |
4130 else if (targ->ty == Tdelegate) | |
4131 tded = targ->next->next; | |
4132 else if (targ->ty == Tpointer && targ->next->ty == Tfunction) | |
4133 tded = targ->next->next; | |
4134 else | |
4135 goto Lno; | |
4136 break; | |
4137 | |
4138 default: | |
4139 assert(0); | |
4140 } | |
4141 goto Lyes; | |
4142 } | |
4143 else if (id && tspec) | |
4144 { | |
4145 /* Evaluate to TRUE if targ matches tspec. | |
4146 * If TRUE, declare id as an alias for the specialized type. | |
4147 */ | |
4148 | |
4149 MATCH m; | |
4150 TemplateTypeParameter tp(loc, id, NULL, NULL); | |
4151 | |
4152 TemplateParameters parameters; | |
4153 parameters.setDim(1); | |
4154 parameters.data[0] = (void *)&tp; | |
4155 | |
4156 Objects dedtypes; | |
4157 dedtypes.setDim(1); | |
4158 dedtypes.data[0] = NULL; | |
4159 | |
4160 m = targ->deduceType(NULL, tspec, ¶meters, &dedtypes); | |
4161 if (m == MATCHnomatch || | |
4162 (m != MATCHexact && tok == TOKequal)) | |
4163 goto Lno; | |
4164 else | |
4165 { | |
4166 assert(dedtypes.dim == 1); | |
4167 tded = (Type *)dedtypes.data[0]; | |
4168 if (!tded) | |
4169 tded = targ; | |
4170 goto Lyes; | |
4171 } | |
4172 } | |
4173 else if (id) | |
4174 { | |
4175 /* Declare id as an alias for type targ. Evaluate to TRUE | |
4176 */ | |
4177 tded = targ; | |
4178 goto Lyes; | |
4179 } | |
4180 else if (tspec) | |
4181 { | |
4182 /* Evaluate to TRUE if targ matches tspec | |
4183 */ | |
4184 tspec = tspec->semantic(loc, sc); | |
4185 //printf("targ = %s\n", targ->toChars()); | |
4186 //printf("tspec = %s\n", tspec->toChars()); | |
4187 if (tok == TOKcolon) | |
4188 { if (targ->implicitConvTo(tspec)) | |
4189 goto Lyes; | |
4190 else | |
4191 goto Lno; | |
4192 } | |
4193 else /* == */ | |
4194 { if (targ->equals(tspec)) | |
4195 goto Lyes; | |
4196 else | |
4197 goto Lno; | |
4198 } | |
4199 } | |
4200 | |
4201 Lyes: | |
4202 if (id) | |
4203 { | |
4204 Dsymbol *s = new AliasDeclaration(loc, id, tded); | |
4205 s->semantic(sc); | |
4206 sc->insert(s); | |
4207 if (sc->sd) | |
4208 s->addMember(sc, sc->sd, 1); | |
4209 } | |
4210 return new IntegerExp(1); | |
4211 | |
4212 Lno: | |
4213 return new IntegerExp(0); | |
4214 } | |
4215 | |
19 | 4216 void IsExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
1 | 4217 { |
4218 buf->writestring("is("); | |
4219 targ->toCBuffer(buf, id, hgs); | |
19 | 4220 if (tok2 != TOKreserved) |
4221 { | |
4222 buf->printf(" %s %s", Token::toChars(tok), Token::toChars(tok2)); | |
4223 } | |
4224 else if (tspec) | |
1 | 4225 { |
4226 if (tok == TOKcolon) | |
4227 buf->writestring(" : "); | |
4228 else | |
4229 buf->writestring(" == "); | |
4230 tspec->toCBuffer(buf, NULL, hgs); | |
4231 } | |
19 | 4232 #if V2 |
4233 if (parameters) | |
4234 { // First parameter is already output, so start with second | |
4235 for (int i = 1; i < parameters->dim; i++) | |
4236 { | |
4237 buf->writeByte(','); | |
4238 TemplateParameter *tp = (TemplateParameter *)parameters->data[i]; | |
4239 tp->toCBuffer(buf, hgs); | |
4240 } | |
4241 } | |
4242 #endif | |
1 | 4243 buf->writeByte(')'); |
4244 } | |
4245 | |
4246 | |
4247 /************************************************************/ | |
4248 | |
4249 UnaExp::UnaExp(Loc loc, enum TOK op, int size, Expression *e1) | |
4250 : Expression(loc, op, size) | |
4251 { | |
4252 this->e1 = e1; | |
4253 } | |
4254 | |
4255 Expression *UnaExp::syntaxCopy() | |
4256 { UnaExp *e; | |
4257 | |
4258 e = (UnaExp *)copy(); | |
4259 e->type = NULL; | |
4260 e->e1 = e->e1->syntaxCopy(); | |
4261 return e; | |
4262 } | |
4263 | |
4264 Expression *UnaExp::semantic(Scope *sc) | |
4265 { | |
4266 #if LOGSEMANTIC | |
4267 printf("UnaExp::semantic('%s')\n", toChars()); | |
4268 #endif | |
4269 e1 = e1->semantic(sc); | |
4270 // if (!e1->type) | |
4271 // error("%s has no value", e1->toChars()); | |
4272 return this; | |
4273 } | |
4274 | |
4275 void UnaExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4276 { | |
4277 buf->writestring(Token::toChars(op)); | |
4278 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4279 } | |
4280 | |
4281 /************************************************************/ | |
4282 | |
4283 BinExp::BinExp(Loc loc, enum TOK op, int size, Expression *e1, Expression *e2) | |
4284 : Expression(loc, op, size) | |
4285 { | |
4286 this->e1 = e1; | |
4287 this->e2 = e2; | |
4288 } | |
4289 | |
4290 Expression *BinExp::syntaxCopy() | |
4291 { BinExp *e; | |
4292 | |
4293 e = (BinExp *)copy(); | |
4294 e->type = NULL; | |
4295 e->e1 = e->e1->syntaxCopy(); | |
4296 e->e2 = e->e2->syntaxCopy(); | |
4297 return e; | |
4298 } | |
4299 | |
4300 Expression *BinExp::semantic(Scope *sc) | |
4301 { | |
4302 #if LOGSEMANTIC | |
4303 printf("BinExp::semantic('%s')\n", toChars()); | |
4304 #endif | |
4305 e1 = e1->semantic(sc); | |
4306 if (!e1->type) | |
4307 { | |
4308 error("%s has no value", e1->toChars()); | |
4309 e1->type = Type::terror; | |
4310 } | |
4311 e2 = e2->semantic(sc); | |
4312 if (!e2->type) | |
4313 { | |
4314 error("%s has no value", e2->toChars()); | |
4315 e2->type = Type::terror; | |
4316 } | |
4317 assert(e1->type); | |
4318 return this; | |
4319 } | |
4320 | |
4321 Expression *BinExp::semanticp(Scope *sc) | |
4322 { | |
4323 BinExp::semantic(sc); | |
4324 e1 = resolveProperties(sc, e1); | |
4325 e2 = resolveProperties(sc, e2); | |
4326 return this; | |
4327 } | |
4328 | |
4329 /*************************** | |
4330 * Common semantic routine for some xxxAssignExp's. | |
4331 */ | |
4332 | |
4333 Expression *BinExp::commonSemanticAssign(Scope *sc) | |
4334 { Expression *e; | |
4335 | |
4336 if (!type) | |
4337 { | |
4338 BinExp::semantic(sc); | |
4339 e2 = resolveProperties(sc, e2); | |
4340 | |
4341 e = op_overload(sc); | |
4342 if (e) | |
4343 return e; | |
4344 | |
4345 e1 = e1->modifiableLvalue(sc, NULL); | |
4346 e1->checkScalar(); | |
4347 type = e1->type; | |
4348 if (type->toBasetype()->ty == Tbool) | |
4349 { | |
4350 error("operator not allowed on bool expression %s", toChars()); | |
4351 } | |
4352 typeCombine(sc); | |
4353 e1->checkArithmetic(); | |
4354 e2->checkArithmetic(); | |
4355 | |
4356 if (op == TOKmodass && e2->type->iscomplex()) | |
4357 { error("cannot perform modulo complex arithmetic"); | |
4358 return new IntegerExp(0); | |
4359 } | |
4360 } | |
4361 return this; | |
4362 } | |
4363 | |
4364 Expression *BinExp::commonSemanticAssignIntegral(Scope *sc) | |
4365 { Expression *e; | |
4366 | |
4367 if (!type) | |
4368 { | |
4369 BinExp::semantic(sc); | |
4370 e2 = resolveProperties(sc, e2); | |
4371 | |
4372 e = op_overload(sc); | |
4373 if (e) | |
4374 return e; | |
4375 | |
4376 e1 = e1->modifiableLvalue(sc, NULL); | |
4377 e1->checkScalar(); | |
4378 type = e1->type; | |
4379 if (type->toBasetype()->ty == Tbool) | |
4380 { | |
4381 e2 = e2->implicitCastTo(sc, type); | |
4382 } | |
4383 | |
4384 typeCombine(sc); | |
4385 e1->checkIntegral(); | |
4386 e2->checkIntegral(); | |
4387 } | |
4388 return this; | |
4389 } | |
4390 | |
4391 int BinExp::checkSideEffect(int flag) | |
4392 { | |
4393 if (op == TOKplusplus || | |
4394 op == TOKminusminus || | |
4395 op == TOKassign || | |
4396 op == TOKaddass || | |
4397 op == TOKminass || | |
4398 op == TOKcatass || | |
4399 op == TOKmulass || | |
4400 op == TOKdivass || | |
4401 op == TOKmodass || | |
4402 op == TOKshlass || | |
4403 op == TOKshrass || | |
4404 op == TOKushrass || | |
4405 op == TOKandass || | |
4406 op == TOKorass || | |
4407 op == TOKxorass || | |
4408 op == TOKin || | |
4409 op == TOKremove) | |
4410 return 1; | |
4411 return Expression::checkSideEffect(flag); | |
4412 } | |
4413 | |
4414 void BinExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4415 { | |
4416 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4417 buf->writeByte(' '); | |
4418 buf->writestring(Token::toChars(op)); | |
4419 buf->writeByte(' '); | |
4420 expToCBuffer(buf, hgs, e2, (enum PREC)(precedence[op] + 1)); | |
4421 } | |
4422 | |
4423 int BinExp::isunsigned() | |
4424 { | |
4425 return e1->type->isunsigned() || e2->type->isunsigned(); | |
4426 } | |
4427 | |
4428 void BinExp::incompatibleTypes() | |
4429 { | |
4430 error("incompatible types for ((%s) %s (%s)): '%s' and '%s'", | |
4431 e1->toChars(), Token::toChars(op), e2->toChars(), | |
4432 e1->type->toChars(), e2->type->toChars()); | |
4433 } | |
4434 | |
4435 /************************************************************/ | |
4436 | |
4437 CompileExp::CompileExp(Loc loc, Expression *e) | |
4438 : UnaExp(loc, TOKmixin, sizeof(CompileExp), e) | |
4439 { | |
4440 } | |
4441 | |
4442 Expression *CompileExp::semantic(Scope *sc) | |
4443 { | |
4444 #if LOGSEMANTIC | |
4445 printf("CompileExp::semantic('%s')\n", toChars()); | |
4446 #endif | |
4447 UnaExp::semantic(sc); | |
4448 e1 = resolveProperties(sc, e1); | |
4449 e1 = e1->optimize(WANTvalue | WANTinterpret); | |
4450 if (e1->op != TOKstring) | |
4451 { error("argument to mixin must be a string, not (%s)", e1->toChars()); | |
4452 type = Type::terror; | |
4453 return this; | |
4454 } | |
4455 StringExp *se = (StringExp *)e1; | |
4456 se = se->toUTF8(sc); | |
4457 Parser p(sc->module, (unsigned char *)se->string, se->len, 0); | |
4458 p.loc = loc; | |
4459 p.nextToken(); | |
4460 Expression *e = p.parseExpression(); | |
4461 if (p.token.value != TOKeof) | |
4462 error("incomplete mixin expression (%s)", se->toChars()); | |
4463 return e->semantic(sc); | |
4464 } | |
4465 | |
4466 void CompileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4467 { | |
4468 buf->writestring("mixin("); | |
4469 expToCBuffer(buf, hgs, e1, PREC_assign); | |
4470 buf->writeByte(')'); | |
4471 } | |
4472 | |
4473 /************************************************************/ | |
4474 | |
4475 FileExp::FileExp(Loc loc, Expression *e) | |
4476 : UnaExp(loc, TOKmixin, sizeof(FileExp), e) | |
4477 { | |
4478 } | |
4479 | |
4480 Expression *FileExp::semantic(Scope *sc) | |
4481 { char *name; | |
4482 StringExp *se; | |
4483 | |
4484 #if LOGSEMANTIC | |
4485 printf("FileExp::semantic('%s')\n", toChars()); | |
4486 #endif | |
4487 UnaExp::semantic(sc); | |
4488 e1 = resolveProperties(sc, e1); | |
4489 e1 = e1->optimize(WANTvalue); | |
4490 if (e1->op != TOKstring) | |
4491 { error("file name argument must be a string, not (%s)", e1->toChars()); | |
4492 goto Lerror; | |
4493 } | |
4494 se = (StringExp *)e1; | |
4495 se = se->toUTF8(sc); | |
4496 name = (char *)se->string; | |
4497 | |
4498 if (!global.params.fileImppath) | |
4499 { error("need -Jpath switch to import text file %s", name); | |
4500 goto Lerror; | |
4501 } | |
4502 | |
4503 if (name != FileName::name(name)) | |
4504 { error("use -Jpath switch to provide path for filename %s", name); | |
4505 goto Lerror; | |
4506 } | |
4507 | |
4508 name = FileName::searchPath(global.filePath, name, 0); | |
4509 if (!name) | |
4510 { error("file %s cannot be found, check -Jpath", se->toChars()); | |
4511 goto Lerror; | |
4512 } | |
4513 | |
4514 if (global.params.verbose) | |
4515 printf("file %s\t(%s)\n", se->string, name); | |
4516 | |
4517 { File f(name); | |
4518 if (f.read()) | |
4519 { error("cannot read file %s", f.toChars()); | |
4520 goto Lerror; | |
4521 } | |
4522 else | |
4523 { | |
4524 f.ref = 1; | |
4525 se = new StringExp(loc, f.buffer, f.len); | |
4526 } | |
4527 } | |
4528 Lret: | |
4529 return se->semantic(sc); | |
4530 | |
4531 Lerror: | |
4532 se = new StringExp(loc, ""); | |
4533 goto Lret; | |
4534 } | |
4535 | |
4536 void FileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4537 { | |
4538 buf->writestring("import("); | |
4539 expToCBuffer(buf, hgs, e1, PREC_assign); | |
4540 buf->writeByte(')'); | |
4541 } | |
4542 | |
4543 /************************************************************/ | |
4544 | |
4545 AssertExp::AssertExp(Loc loc, Expression *e, Expression *msg) | |
4546 : UnaExp(loc, TOKassert, sizeof(AssertExp), e) | |
4547 { | |
4548 this->msg = msg; | |
4549 } | |
4550 | |
4551 Expression *AssertExp::syntaxCopy() | |
4552 { | |
4553 AssertExp *ae = new AssertExp(loc, e1->syntaxCopy(), | |
4554 msg ? msg->syntaxCopy() : NULL); | |
4555 return ae; | |
4556 } | |
4557 | |
4558 Expression *AssertExp::semantic(Scope *sc) | |
4559 { | |
4560 #if LOGSEMANTIC | |
4561 printf("AssertExp::semantic('%s')\n", toChars()); | |
4562 #endif | |
4563 UnaExp::semantic(sc); | |
4564 e1 = resolveProperties(sc, e1); | |
4565 // BUG: see if we can do compile time elimination of the Assert | |
4566 e1 = e1->optimize(WANTvalue); | |
4567 e1 = e1->checkToBoolean(); | |
4568 if (msg) | |
4569 { | |
4570 msg = msg->semantic(sc); | |
4571 msg = resolveProperties(sc, msg); | |
4572 msg = msg->implicitCastTo(sc, Type::tchar->arrayOf()); | |
4573 msg = msg->optimize(WANTvalue); | |
4574 } | |
4575 if (e1->isBool(FALSE)) | |
4576 { | |
4577 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
4578 fd->hasReturnExp |= 4; | |
4579 | |
4580 if (!global.params.useAssert) | |
4581 { Expression *e = new HaltExp(loc); | |
4582 e = e->semantic(sc); | |
4583 return e; | |
4584 } | |
4585 } | |
4586 type = Type::tvoid; | |
4587 return this; | |
4588 } | |
4589 | |
4590 int AssertExp::checkSideEffect(int flag) | |
4591 { | |
4592 return 1; | |
4593 } | |
4594 | |
4595 void AssertExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4596 { | |
4597 buf->writestring("assert("); | |
4598 expToCBuffer(buf, hgs, e1, PREC_assign); | |
4599 if (msg) | |
4600 { | |
4601 buf->writeByte(','); | |
4602 expToCBuffer(buf, hgs, msg, PREC_assign); | |
4603 } | |
4604 buf->writeByte(')'); | |
4605 } | |
4606 | |
4607 /************************************************************/ | |
4608 | |
4609 DotIdExp::DotIdExp(Loc loc, Expression *e, Identifier *ident) | |
4610 : UnaExp(loc, TOKdot, sizeof(DotIdExp), e) | |
4611 { | |
4612 this->ident = ident; | |
4613 } | |
4614 | |
4615 Expression *DotIdExp::semantic(Scope *sc) | |
4616 { Expression *e; | |
4617 Expression *eleft; | |
4618 Expression *eright; | |
4619 | |
4620 #if LOGSEMANTIC | |
4621 printf("DotIdExp::semantic(this = %p, '%s')\n", this, toChars()); | |
4622 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
4623 #endif | |
4624 | |
4625 //{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; } | |
4626 | |
4627 #if 0 | |
4628 /* Don't do semantic analysis if we'll be converting | |
4629 * it to a string. | |
4630 */ | |
4631 if (ident == Id::stringof) | |
4632 { char *s = e1->toChars(); | |
4633 e = new StringExp(loc, s, strlen(s), 'c'); | |
4634 e = e->semantic(sc); | |
4635 return e; | |
4636 } | |
4637 #endif | |
4638 | |
4639 /* Special case: rewrite this.id and super.id | |
4640 * to be classtype.id and baseclasstype.id | |
4641 * if we have no this pointer. | |
4642 */ | |
4643 if ((e1->op == TOKthis || e1->op == TOKsuper) && !hasThis(sc)) | |
4644 { ClassDeclaration *cd; | |
4645 StructDeclaration *sd; | |
4646 AggregateDeclaration *ad; | |
4647 | |
4648 ad = sc->getStructClassScope(); | |
4649 if (ad) | |
4650 { | |
4651 cd = ad->isClassDeclaration(); | |
4652 if (cd) | |
4653 { | |
4654 if (e1->op == TOKthis) | |
4655 { | |
4656 e = new TypeDotIdExp(loc, cd->type, ident); | |
4657 return e->semantic(sc); | |
4658 } | |
4659 else if (cd->baseClass && e1->op == TOKsuper) | |
4660 { | |
4661 e = new TypeDotIdExp(loc, cd->baseClass->type, ident); | |
4662 return e->semantic(sc); | |
4663 } | |
4664 } | |
4665 else | |
4666 { | |
4667 sd = ad->isStructDeclaration(); | |
4668 if (sd) | |
4669 { | |
4670 if (e1->op == TOKthis) | |
4671 { | |
4672 e = new TypeDotIdExp(loc, sd->type, ident); | |
4673 return e->semantic(sc); | |
4674 } | |
4675 } | |
4676 } | |
4677 } | |
4678 } | |
4679 | |
4680 UnaExp::semantic(sc); | |
4681 | |
4682 if (e1->op == TOKdotexp) | |
4683 { | |
4684 DotExp *de = (DotExp *)e1; | |
4685 eleft = de->e1; | |
4686 eright = de->e2; | |
4687 } | |
4688 else | |
4689 { | |
4690 e1 = resolveProperties(sc, e1); | |
4691 eleft = NULL; | |
4692 eright = e1; | |
4693 } | |
4694 | |
4695 if (e1->op == TOKtuple && ident == Id::length) | |
4696 { | |
4697 TupleExp *te = (TupleExp *)e1; | |
4698 e = new IntegerExp(loc, te->exps->dim, Type::tsize_t); | |
4699 return e; | |
4700 } | |
4701 | |
4702 if (eright->op == TOKimport) // also used for template alias's | |
4703 { | |
4704 Dsymbol *s; | |
4705 ScopeExp *ie = (ScopeExp *)eright; | |
4706 | |
4707 s = ie->sds->search(loc, ident, 0); | |
4708 if (s) | |
4709 { | |
4710 s = s->toAlias(); | |
4711 checkDeprecated(sc, s); | |
4712 | |
4713 EnumMember *em = s->isEnumMember(); | |
4714 if (em) | |
4715 { | |
4716 e = em->value; | |
4717 e = e->semantic(sc); | |
4718 return e; | |
4719 } | |
4720 | |
4721 VarDeclaration *v = s->isVarDeclaration(); | |
4722 if (v) | |
4723 { | |
4724 //printf("DotIdExp:: Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
4725 if (v->inuse) | |
4726 { | |
4727 error("circular reference to '%s'", v->toChars()); | |
4728 type = Type::tint32; | |
4729 return this; | |
4730 } | |
4731 type = v->type; | |
4732 if (v->isConst()) | |
4733 { | |
4734 if (v->init) | |
4735 { | |
4736 ExpInitializer *ei = v->init->isExpInitializer(); | |
4737 if (ei) | |
4738 { | |
4739 //printf("\tei: %p (%s)\n", ei->exp, ei->exp->toChars()); | |
4740 //ei->exp = ei->exp->semantic(sc); | |
4741 if (ei->exp->type == type) | |
4742 { | |
4743 e = ei->exp->copy(); // make copy so we can change loc | |
4744 e->loc = loc; | |
4745 return e; | |
4746 } | |
4747 } | |
4748 } | |
4749 else if (type->isscalar()) | |
4750 { | |
4751 e = type->defaultInit(); | |
4752 e->loc = loc; | |
4753 return e; | |
4754 } | |
4755 } | |
4756 if (v->needThis()) | |
4757 { | |
4758 if (!eleft) | |
4759 eleft = new ThisExp(loc); | |
4760 e = new DotVarExp(loc, eleft, v); | |
4761 e = e->semantic(sc); | |
4762 } | |
4763 else | |
4764 { | |
4765 e = new VarExp(loc, v); | |
4766 if (eleft) | |
4767 { e = new CommaExp(loc, eleft, e); | |
4768 e->type = v->type; | |
4769 } | |
4770 } | |
4771 return e->deref(); | |
4772 } | |
4773 | |
4774 FuncDeclaration *f = s->isFuncDeclaration(); | |
4775 if (f) | |
4776 { | |
4777 //printf("it's a function\n"); | |
4778 if (f->needThis()) | |
4779 { | |
4780 if (!eleft) | |
4781 eleft = new ThisExp(loc); | |
4782 e = new DotVarExp(loc, eleft, f); | |
4783 e = e->semantic(sc); | |
4784 } | |
4785 else | |
4786 { | |
4787 e = new VarExp(loc, f); | |
4788 if (eleft) | |
4789 { e = new CommaExp(loc, eleft, e); | |
4790 e->type = f->type; | |
4791 } | |
4792 } | |
4793 return e; | |
4794 } | |
4795 | |
4796 Type *t = s->getType(); | |
4797 if (t) | |
4798 { | |
4799 return new TypeExp(loc, t); | |
4800 } | |
4801 | |
4802 ScopeDsymbol *sds = s->isScopeDsymbol(); | |
4803 if (sds) | |
4804 { | |
4805 //printf("it's a ScopeDsymbol\n"); | |
4806 e = new ScopeExp(loc, sds); | |
4807 e = e->semantic(sc); | |
4808 if (eleft) | |
4809 e = new DotExp(loc, eleft, e); | |
4810 return e; | |
4811 } | |
4812 | |
4813 Import *imp = s->isImport(); | |
4814 if (imp) | |
4815 { | |
4816 ScopeExp *ie; | |
4817 | |
4818 ie = new ScopeExp(loc, imp->pkg); | |
4819 return ie->semantic(sc); | |
4820 } | |
4821 | |
4822 // BUG: handle other cases like in IdentifierExp::semantic() | |
4823 #ifdef DEBUG | |
4824 printf("s = '%s', kind = '%s'\n", s->toChars(), s->kind()); | |
4825 #endif | |
4826 assert(0); | |
4827 } | |
4828 else if (ident == Id::stringof) | |
4829 { char *s = ie->toChars(); | |
4830 e = new StringExp(loc, s, strlen(s), 'c'); | |
4831 e = e->semantic(sc); | |
4832 return e; | |
4833 } | |
4834 error("undefined identifier %s", toChars()); | |
4835 type = Type::tvoid; | |
4836 return this; | |
4837 } | |
4838 else if (e1->type->ty == Tpointer && | |
4839 ident != Id::init && ident != Id::__sizeof && | |
4840 ident != Id::alignof && ident != Id::offsetof && | |
4841 ident != Id::mangleof && ident != Id::stringof) | |
4842 { | |
4843 e = new PtrExp(loc, e1); | |
4844 e->type = e1->type->next; | |
4845 return e->type->dotExp(sc, e, ident); | |
4846 } | |
4847 else | |
4848 { | |
4849 e = e1->type->dotExp(sc, e1, ident); | |
4850 e = e->semantic(sc); | |
4851 return e; | |
4852 } | |
4853 } | |
4854 | |
4855 void DotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4856 { | |
4857 //printf("DotIdExp::toCBuffer()\n"); | |
4858 expToCBuffer(buf, hgs, e1, PREC_primary); | |
4859 buf->writeByte('.'); | |
4860 buf->writestring(ident->toChars()); | |
4861 } | |
4862 | |
4863 /********************** DotTemplateExp ***********************************/ | |
4864 | |
4865 // Mainly just a placeholder | |
4866 | |
4867 DotTemplateExp::DotTemplateExp(Loc loc, Expression *e, TemplateDeclaration *td) | |
4868 : UnaExp(loc, TOKdottd, sizeof(DotTemplateExp), e) | |
4869 | |
4870 { | |
4871 this->td = td; | |
4872 } | |
4873 | |
4874 void DotTemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4875 { | |
4876 expToCBuffer(buf, hgs, e1, PREC_primary); | |
4877 buf->writeByte('.'); | |
4878 buf->writestring(td->toChars()); | |
4879 } | |
4880 | |
4881 | |
4882 /************************************************************/ | |
4883 | |
4884 DotVarExp::DotVarExp(Loc loc, Expression *e, Declaration *v) | |
4885 : UnaExp(loc, TOKdotvar, sizeof(DotVarExp), e) | |
4886 { | |
4887 //printf("DotVarExp()\n"); | |
4888 this->var = v; | |
4889 } | |
4890 | |
4891 Expression *DotVarExp::semantic(Scope *sc) | |
4892 { | |
4893 #if LOGSEMANTIC | |
4894 printf("DotVarExp::semantic('%s')\n", toChars()); | |
4895 #endif | |
4896 if (!type) | |
4897 { | |
4898 var = var->toAlias()->isDeclaration(); | |
4899 | |
4900 TupleDeclaration *tup = var->isTupleDeclaration(); | |
4901 if (tup) | |
4902 { /* Replace: | |
4903 * e1.tuple(a, b, c) | |
4904 * with: | |
4905 * tuple(e1.a, e1.b, e1.c) | |
4906 */ | |
4907 Expressions *exps = new Expressions; | |
4908 | |
4909 exps->reserve(tup->objects->dim); | |
4910 for (size_t i = 0; i < tup->objects->dim; i++) | |
4911 { Object *o = (Object *)tup->objects->data[i]; | |
4912 if (o->dyncast() != DYNCAST_EXPRESSION) | |
4913 { | |
4914 error("%s is not an expression", o->toChars()); | |
4915 } | |
4916 else | |
4917 { | |
4918 Expression *e = (Expression *)o; | |
4919 if (e->op != TOKdsymbol) | |
4920 error("%s is not a member", e->toChars()); | |
4921 else | |
4922 { DsymbolExp *ve = (DsymbolExp *)e; | |
4923 | |
4924 e = new DotVarExp(loc, e1, ve->s->isDeclaration()); | |
4925 exps->push(e); | |
4926 } | |
4927 } | |
4928 } | |
4929 Expression *e = new TupleExp(loc, exps); | |
4930 e = e->semantic(sc); | |
4931 return e; | |
4932 } | |
4933 | |
4934 e1 = e1->semantic(sc); | |
4935 type = var->type; | |
4936 if (!type && global.errors) | |
4937 { // var is goofed up, just return 0 | |
4938 return new IntegerExp(0); | |
4939 } | |
4940 assert(type); | |
4941 | |
4942 if (!var->isFuncDeclaration()) // for functions, do checks after overload resolution | |
4943 { | |
4944 AggregateDeclaration *ad = var->toParent()->isAggregateDeclaration(); | |
4945 L1: | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
4946 Type *t = e1->type->toBasetype(); |
1 | 4947 |
4948 if (ad && | |
4949 !(t->ty == Tpointer && t->next->ty == Tstruct && | |
4950 ((TypeStruct *)t->next)->sym == ad) | |
4951 && | |
4952 !(t->ty == Tstruct && | |
4953 ((TypeStruct *)t)->sym == ad) | |
4954 ) | |
4955 { | |
4956 ClassDeclaration *cd = ad->isClassDeclaration(); | |
4957 ClassDeclaration *tcd = t->isClassHandle(); | |
4958 | |
4959 if (!cd || !tcd || | |
4960 !(tcd == cd || cd->isBaseOf(tcd, NULL)) | |
4961 ) | |
4962 { | |
4963 if (tcd && tcd->isNested()) | |
4964 { // Try again with outer scope | |
4965 | |
4966 e1 = new DotVarExp(loc, e1, tcd->vthis); | |
4967 e1 = e1->semantic(sc); | |
4968 | |
4969 // Skip over nested functions, and get the enclosing | |
4970 // class type. | |
4971 Dsymbol *s = tcd->toParent(); | |
4972 while (s && s->isFuncDeclaration()) | |
4973 { FuncDeclaration *f = s->isFuncDeclaration(); | |
4974 if (f->vthis) | |
4975 { | |
4976 e1 = new VarExp(loc, f->vthis); | |
4977 } | |
4978 s = s->toParent(); | |
4979 } | |
4980 if (s && s->isClassDeclaration()) | |
4981 e1->type = s->isClassDeclaration()->type; | |
4982 | |
4983 goto L1; | |
4984 } | |
4985 #ifdef DEBUG | |
4986 printf("2: "); | |
4987 #endif | |
4988 error("this for %s needs to be type %s not type %s", | |
4989 var->toChars(), ad->toChars(), t->toChars()); | |
4990 } | |
4991 } | |
4992 accessCheck(loc, sc, e1, var); | |
4993 } | |
4994 } | |
4995 //printf("-DotVarExp::semantic('%s')\n", toChars()); | |
4996 return this; | |
4997 } | |
4998 | |
4999 Expression *DotVarExp::toLvalue(Scope *sc, Expression *e) | |
5000 { | |
5001 //printf("DotVarExp::toLvalue(%s)\n", toChars()); | |
5002 return this; | |
5003 } | |
5004 | |
5005 Expression *DotVarExp::modifiableLvalue(Scope *sc, Expression *e) | |
5006 { | |
5007 //printf("DotVarExp::modifiableLvalue(%s)\n", toChars()); | |
5008 | |
5009 if (var->isCtorinit()) | |
5010 { // It's only modifiable if inside the right constructor | |
5011 Dsymbol *s = sc->func; | |
5012 while (1) | |
5013 { | |
5014 FuncDeclaration *fd = NULL; | |
5015 if (s) | |
5016 fd = s->isFuncDeclaration(); | |
5017 if (fd && | |
5018 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
5019 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
5020 fd->toParent() == var->toParent() && | |
5021 e1->op == TOKthis | |
5022 ) | |
5023 { | |
5024 VarDeclaration *v = var->isVarDeclaration(); | |
5025 assert(v); | |
5026 v->ctorinit = 1; | |
5027 //printf("setting ctorinit\n"); | |
5028 } | |
5029 else | |
5030 { | |
5031 if (s) | |
5032 { s = s->toParent2(); | |
5033 continue; | |
5034 } | |
5035 else | |
5036 { | |
5037 const char *p = var->isStatic() ? "static " : ""; | |
5038 error("can only initialize %sconst member %s inside %sconstructor", | |
5039 p, var->toChars(), p); | |
5040 } | |
5041 } | |
5042 break; | |
5043 } | |
5044 } | |
5045 return this; | |
5046 } | |
5047 | |
5048 void DotVarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5049 { | |
5050 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5051 buf->writeByte('.'); | |
5052 buf->writestring(var->toChars()); | |
5053 } | |
5054 | |
5055 /************************************************************/ | |
5056 | |
5057 /* Things like: | |
5058 * foo.bar!(args) | |
5059 */ | |
5060 | |
5061 DotTemplateInstanceExp::DotTemplateInstanceExp(Loc loc, Expression *e, TemplateInstance *ti) | |
5062 : UnaExp(loc, TOKdotti, sizeof(DotTemplateInstanceExp), e) | |
5063 { | |
5064 //printf("DotTemplateInstanceExp()\n"); | |
5065 this->ti = ti; | |
5066 } | |
5067 | |
5068 Expression *DotTemplateInstanceExp::syntaxCopy() | |
5069 { | |
5070 DotTemplateInstanceExp *de = new DotTemplateInstanceExp(loc, | |
5071 e1->syntaxCopy(), | |
5072 (TemplateInstance *)ti->syntaxCopy(NULL)); | |
5073 return de; | |
5074 } | |
5075 | |
5076 Expression *DotTemplateInstanceExp::semantic(Scope *sc) | |
5077 { Dsymbol *s; | |
5078 Dsymbol *s2; | |
5079 TemplateDeclaration *td; | |
5080 Expression *e; | |
5081 Identifier *id; | |
5082 Type *t1; | |
5083 Expression *eleft = NULL; | |
5084 Expression *eright; | |
5085 | |
5086 #if LOGSEMANTIC | |
5087 printf("DotTemplateInstanceExp::semantic('%s')\n", toChars()); | |
5088 #endif | |
5089 //e1->print(); | |
5090 //print(); | |
5091 e1 = e1->semantic(sc); | |
5092 t1 = e1->type; | |
5093 if (t1) | |
5094 t1 = t1->toBasetype(); | |
5095 //t1->print(); | |
5096 if (e1->op == TOKdotexp) | |
5097 { DotExp *de = (DotExp *)e1; | |
5098 eleft = de->e1; | |
5099 eright = de->e2; | |
5100 } | |
5101 else | |
5102 { eleft = NULL; | |
5103 eright = e1; | |
5104 } | |
5105 if (eright->op == TOKimport) | |
5106 { | |
5107 s = ((ScopeExp *)eright)->sds; | |
5108 } | |
5109 else if (e1->op == TOKtype) | |
5110 { | |
5111 s = t1->isClassHandle(); | |
5112 if (!s) | |
5113 { if (t1->ty == Tstruct) | |
5114 s = ((TypeStruct *)t1)->sym; | |
5115 else | |
5116 goto L1; | |
5117 } | |
5118 } | |
5119 else if (t1 && (t1->ty == Tstruct || t1->ty == Tclass)) | |
5120 { | |
5121 s = t1->toDsymbol(sc); | |
5122 eleft = e1; | |
5123 } | |
5124 else if (t1 && t1->ty == Tpointer) | |
5125 { | |
5126 t1 = t1->next->toBasetype(); | |
5127 if (t1->ty != Tstruct) | |
5128 goto L1; | |
5129 s = t1->toDsymbol(sc); | |
5130 eleft = e1; | |
5131 } | |
5132 else | |
5133 { | |
5134 L1: | |
5135 error("template %s is not a member of %s", ti->toChars(), e1->toChars()); | |
5136 goto Lerr; | |
5137 } | |
5138 | |
5139 assert(s); | |
5140 id = ti->name; | |
5141 s2 = s->search(loc, id, 0); | |
5142 if (!s2) | |
5143 { error("template identifier %s is not a member of %s %s", id->toChars(), s->kind(), s->ident->toChars()); | |
5144 goto Lerr; | |
5145 } | |
5146 s = s2; | |
5147 s->semantic(sc); | |
5148 s = s->toAlias(); | |
5149 td = s->isTemplateDeclaration(); | |
5150 if (!td) | |
5151 { | |
5152 error("%s is not a template", id->toChars()); | |
5153 goto Lerr; | |
5154 } | |
5155 if (global.errors) | |
5156 goto Lerr; | |
5157 | |
5158 ti->tempdecl = td; | |
5159 | |
5160 if (eleft) | |
5161 { Declaration *v; | |
5162 | |
5163 ti->semantic(sc); | |
5164 s = ti->inst->toAlias(); | |
5165 v = s->isDeclaration(); | |
5166 if (v) | |
5167 { e = new DotVarExp(loc, eleft, v); | |
5168 e = e->semantic(sc); | |
5169 return e; | |
5170 } | |
5171 } | |
5172 | |
5173 e = new ScopeExp(loc, ti); | |
5174 if (eleft) | |
5175 { | |
5176 e = new DotExp(loc, eleft, e); | |
5177 } | |
5178 e = e->semantic(sc); | |
5179 return e; | |
5180 | |
5181 Lerr: | |
5182 return new IntegerExp(0); | |
5183 } | |
5184 | |
5185 void DotTemplateInstanceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5186 { | |
5187 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5188 buf->writeByte('.'); | |
5189 ti->toCBuffer(buf, hgs); | |
5190 } | |
5191 | |
5192 /************************************************************/ | |
5193 | |
5194 DelegateExp::DelegateExp(Loc loc, Expression *e, FuncDeclaration *f) | |
5195 : UnaExp(loc, TOKdelegate, sizeof(DelegateExp), e) | |
5196 { | |
5197 this->func = f; | |
5198 } | |
5199 | |
5200 Expression *DelegateExp::semantic(Scope *sc) | |
5201 { | |
5202 #if LOGSEMANTIC | |
5203 printf("DelegateExp::semantic('%s')\n", toChars()); | |
5204 #endif | |
5205 if (!type) | |
5206 { | |
5207 e1 = e1->semantic(sc); | |
5208 type = new TypeDelegate(func->type); | |
5209 type = type->semantic(loc, sc); | |
5210 //----------------- | |
5211 /* For func, we need to get the | |
5212 * right 'this' pointer if func is in an outer class, but our | |
5213 * existing 'this' pointer is in an inner class. | |
5214 * This code is analogous to that used for variables | |
5215 * in DotVarExp::semantic(). | |
5216 */ | |
5217 AggregateDeclaration *ad = func->toParent()->isAggregateDeclaration(); | |
5218 L10: | |
5219 Type *t = e1->type; | |
5220 if (func->needThis() && ad && | |
5221 !(t->ty == Tpointer && t->next->ty == Tstruct && | |
5222 ((TypeStruct *)t->next)->sym == ad) && | |
5223 !(t->ty == Tstruct && ((TypeStruct *)t)->sym == ad) | |
5224 ) | |
5225 { | |
5226 ClassDeclaration *cd = ad->isClassDeclaration(); | |
5227 ClassDeclaration *tcd = t->isClassHandle(); | |
5228 | |
5229 if (!cd || !tcd || | |
5230 !(tcd == cd || cd->isBaseOf(tcd, NULL)) | |
5231 ) | |
5232 { | |
5233 if (tcd && tcd->isNested()) | |
5234 { // Try again with outer scope | |
5235 | |
5236 e1 = new DotVarExp(loc, e1, tcd->vthis); | |
5237 e1 = e1->semantic(sc); | |
5238 goto L10; | |
5239 } | |
5240 #ifdef DEBUG | |
5241 printf("3: "); | |
5242 #endif | |
5243 error("this for %s needs to be type %s not type %s", | |
5244 func->toChars(), ad->toChars(), t->toChars()); | |
5245 } | |
5246 } | |
5247 //----------------- | |
5248 } | |
5249 return this; | |
5250 } | |
5251 | |
5252 void DelegateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5253 { | |
5254 buf->writeByte('&'); | |
5255 if (!func->isNested()) | |
5256 { | |
5257 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5258 buf->writeByte('.'); | |
5259 } | |
5260 buf->writestring(func->toChars()); | |
5261 } | |
5262 | |
5263 /************************************************************/ | |
5264 | |
5265 DotTypeExp::DotTypeExp(Loc loc, Expression *e, Dsymbol *s) | |
5266 : UnaExp(loc, TOKdottype, sizeof(DotTypeExp), e) | |
5267 { | |
5268 this->sym = s; | |
5269 this->type = s->getType(); | |
5270 } | |
5271 | |
5272 Expression *DotTypeExp::semantic(Scope *sc) | |
5273 { | |
5274 #if LOGSEMANTIC | |
5275 printf("DotTypeExp::semantic('%s')\n", toChars()); | |
5276 #endif | |
5277 UnaExp::semantic(sc); | |
5278 return this; | |
5279 } | |
5280 | |
5281 void DotTypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5282 { | |
5283 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5284 buf->writeByte('.'); | |
5285 buf->writestring(sym->toChars()); | |
5286 } | |
5287 | |
5288 /************************************************************/ | |
5289 | |
5290 CallExp::CallExp(Loc loc, Expression *e, Expressions *exps) | |
5291 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5292 { | |
5293 this->arguments = exps; | |
5294 } | |
5295 | |
5296 CallExp::CallExp(Loc loc, Expression *e) | |
5297 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5298 { | |
5299 this->arguments = NULL; | |
5300 } | |
5301 | |
5302 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1) | |
5303 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5304 { | |
5305 Expressions *arguments = new Expressions(); | |
5306 arguments->setDim(1); | |
5307 arguments->data[0] = (void *)earg1; | |
5308 | |
5309 this->arguments = arguments; | |
5310 } | |
5311 | |
5312 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1, Expression *earg2) | |
5313 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5314 { | |
5315 Expressions *arguments = new Expressions(); | |
5316 arguments->setDim(2); | |
5317 arguments->data[0] = (void *)earg1; | |
5318 arguments->data[1] = (void *)earg2; | |
5319 | |
5320 this->arguments = arguments; | |
5321 } | |
5322 | |
5323 Expression *CallExp::syntaxCopy() | |
5324 { | |
5325 return new CallExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
5326 } | |
5327 | |
5328 | |
5329 Expression *CallExp::semantic(Scope *sc) | |
5330 { | |
5331 TypeFunction *tf; | |
5332 FuncDeclaration *f; | |
5333 int i; | |
5334 Type *t1; | |
5335 int istemp; | |
5336 | |
5337 #if LOGSEMANTIC | |
19 | 5338 printf("CallExp::semantic() %s\n", toChars()); |
1 | 5339 #endif |
5340 if (type) | |
5341 return this; // semantic() already run | |
5342 #if 0 | |
5343 if (arguments && arguments->dim) | |
5344 { | |
5345 Expression *earg = (Expression *)arguments->data[0]; | |
5346 earg->print(); | |
5347 if (earg->type) earg->type->print(); | |
5348 } | |
5349 #endif | |
5350 | |
5351 if (e1->op == TOKdelegate) | |
5352 { DelegateExp *de = (DelegateExp *)e1; | |
5353 | |
5354 e1 = new DotVarExp(de->loc, de->e1, de->func); | |
5355 return semantic(sc); | |
5356 } | |
5357 | |
5358 /* Transform: | |
5359 * array.id(args) into id(array,args) | |
5360 * aa.remove(arg) into delete aa[arg] | |
5361 */ | |
5362 if (e1->op == TOKdot) | |
5363 { | |
5364 // BUG: we should handle array.a.b.c.e(args) too | |
5365 | |
5366 DotIdExp *dotid = (DotIdExp *)(e1); | |
5367 dotid->e1 = dotid->e1->semantic(sc); | |
5368 assert(dotid->e1); | |
5369 if (dotid->e1->type) | |
5370 { | |
5371 TY e1ty = dotid->e1->type->toBasetype()->ty; | |
5372 if (e1ty == Taarray && dotid->ident == Id::remove) | |
5373 { | |
5374 if (!arguments || arguments->dim != 1) | |
5375 { error("expected key as argument to aa.remove()"); | |
5376 goto Lagain; | |
5377 } | |
5378 Expression *key = (Expression *)arguments->data[0]; | |
5379 key = key->semantic(sc); | |
5380 key = resolveProperties(sc, key); | |
5381 key->rvalue(); | |
5382 | |
5383 TypeAArray *taa = (TypeAArray *)dotid->e1->type->toBasetype(); | |
5384 key = key->implicitCastTo(sc, taa->index); | |
5385 key = key->implicitCastTo(sc, taa->key); | |
5386 | |
5387 return new RemoveExp(loc, dotid->e1, key); | |
5388 } | |
5389 else if (e1ty == Tarray || e1ty == Tsarray || e1ty == Taarray) | |
5390 { | |
5391 if (!arguments) | |
5392 arguments = new Expressions(); | |
5393 arguments->shift(dotid->e1); | |
5394 e1 = new IdentifierExp(dotid->loc, dotid->ident); | |
5395 } | |
5396 } | |
5397 } | |
5398 | |
5399 istemp = 0; | |
5400 Lagain: | |
5401 f = NULL; | |
5402 if (e1->op == TOKthis || e1->op == TOKsuper) | |
5403 { | |
5404 // semantic() run later for these | |
5405 } | |
5406 else | |
5407 { | |
5408 UnaExp::semantic(sc); | |
5409 | |
5410 /* Look for e1 being a lazy parameter | |
5411 */ | |
5412 if (e1->op == TOKvar) | |
5413 { VarExp *ve = (VarExp *)e1; | |
5414 | |
5415 if (ve->var->storage_class & STClazy) | |
5416 { | |
5417 TypeFunction *tf = new TypeFunction(NULL, ve->var->type, 0, LINKd); | |
5418 TypeDelegate *t = new TypeDelegate(tf); | |
5419 ve->type = t->semantic(loc, sc); | |
5420 } | |
5421 } | |
5422 | |
5423 if (e1->op == TOKimport) | |
5424 { // Perhaps this should be moved to ScopeExp::semantic() | |
5425 ScopeExp *se = (ScopeExp *)e1; | |
5426 e1 = new DsymbolExp(loc, se->sds); | |
5427 e1 = e1->semantic(sc); | |
5428 } | |
5429 #if 1 // patch for #540 by Oskar Linde | |
5430 else if (e1->op == TOKdotexp) | |
5431 { | |
5432 DotExp *de = (DotExp *) e1; | |
5433 | |
5434 if (de->e2->op == TOKimport) | |
5435 { // This should *really* be moved to ScopeExp::semantic() | |
5436 ScopeExp *se = (ScopeExp *)de->e2; | |
5437 de->e2 = new DsymbolExp(loc, se->sds); | |
5438 de->e2 = de->e2->semantic(sc); | |
5439 } | |
5440 | |
5441 if (de->e2->op == TOKtemplate) | |
5442 { TemplateExp *te = (TemplateExp *) de->e2; | |
5443 e1 = new DotTemplateExp(loc,de->e1,te->td); | |
5444 } | |
5445 } | |
5446 #endif | |
5447 } | |
5448 | |
5449 if (e1->op == TOKcomma) | |
5450 { | |
5451 CommaExp *ce = (CommaExp *)e1; | |
5452 | |
5453 e1 = ce->e2; | |
5454 e1->type = ce->type; | |
5455 ce->e2 = this; | |
5456 ce->type = NULL; | |
5457 return ce->semantic(sc); | |
5458 } | |
5459 | |
5460 t1 = NULL; | |
5461 if (e1->type) | |
5462 t1 = e1->type->toBasetype(); | |
5463 | |
5464 // Check for call operator overload | |
5465 if (t1) | |
5466 { AggregateDeclaration *ad; | |
5467 | |
5468 if (t1->ty == Tstruct) | |
5469 { | |
5470 ad = ((TypeStruct *)t1)->sym; | |
5471 if (search_function(ad, Id::call)) | |
5472 goto L1; // overload of opCall, therefore it's a call | |
19 | 5473 |
5474 if (e1->op != TOKtype) | |
5475 error("%s %s does not overload ()", ad->kind(), ad->toChars()); | |
1 | 5476 /* It's a struct literal |
5477 */ | |
5478 Expression *e = new StructLiteralExp(loc, (StructDeclaration *)ad, arguments); | |
5479 e = e->semantic(sc); | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
5480 e->type = e1->type; // in case e1->type was a typedef |
1 | 5481 return e; |
5482 } | |
5483 else if (t1->ty == Tclass) | |
5484 { | |
5485 ad = ((TypeClass *)t1)->sym; | |
5486 goto L1; | |
5487 L1: | |
5488 // Rewrite as e1.call(arguments) | |
5489 Expression *e = new DotIdExp(loc, e1, Id::call); | |
5490 e = new CallExp(loc, e, arguments); | |
5491 e = e->semantic(sc); | |
5492 return e; | |
5493 } | |
5494 } | |
5495 | |
5496 arrayExpressionSemantic(arguments, sc); | |
5497 preFunctionArguments(loc, sc, arguments); | |
5498 | |
5499 if (e1->op == TOKdotvar && t1->ty == Tfunction || | |
5500 e1->op == TOKdottd) | |
5501 { | |
5502 DotVarExp *dve; | |
5503 DotTemplateExp *dte; | |
5504 AggregateDeclaration *ad; | |
5505 UnaExp *ue = (UnaExp *)(e1); | |
5506 | |
5507 if (e1->op == TOKdotvar) | |
5508 { // Do overload resolution | |
5509 dve = (DotVarExp *)(e1); | |
5510 | |
5511 f = dve->var->isFuncDeclaration(); | |
5512 assert(f); | |
5513 f = f->overloadResolve(loc, arguments); | |
5514 | |
5515 ad = f->toParent()->isAggregateDeclaration(); | |
5516 } | |
5517 else | |
5518 { dte = (DotTemplateExp *)(e1); | |
5519 TemplateDeclaration *td = dte->td; | |
5520 assert(td); | |
5521 if (!arguments) | |
5522 // Should fix deduce() so it works on NULL argument | |
5523 arguments = new Expressions(); | |
5524 f = td->deduce(sc, loc, NULL, arguments); | |
5525 if (!f) | |
5526 { type = Type::terror; | |
5527 return this; | |
5528 } | |
5529 ad = td->toParent()->isAggregateDeclaration(); | |
5530 } | |
5531 /* Now that we have the right function f, we need to get the | |
5532 * right 'this' pointer if f is in an outer class, but our | |
5533 * existing 'this' pointer is in an inner class. | |
5534 * This code is analogous to that used for variables | |
5535 * in DotVarExp::semantic(). | |
5536 */ | |
5537 L10: | |
5538 Type *t = ue->e1->type->toBasetype(); | |
5539 if (f->needThis() && ad && | |
5540 !(t->ty == Tpointer && t->next->ty == Tstruct && | |
5541 ((TypeStruct *)t->next)->sym == ad) && | |
5542 !(t->ty == Tstruct && ((TypeStruct *)t)->sym == ad) | |
5543 ) | |
5544 { | |
5545 ClassDeclaration *cd = ad->isClassDeclaration(); | |
5546 ClassDeclaration *tcd = t->isClassHandle(); | |
5547 | |
5548 if (!cd || !tcd || | |
5549 !(tcd == cd || cd->isBaseOf(tcd, NULL)) | |
5550 ) | |
5551 { | |
5552 if (tcd && tcd->isNested()) | |
5553 { // Try again with outer scope | |
5554 | |
5555 ue->e1 = new DotVarExp(loc, ue->e1, tcd->vthis); | |
5556 ue->e1 = ue->e1->semantic(sc); | |
5557 goto L10; | |
5558 } | |
5559 #ifdef DEBUG | |
5560 printf("1: "); | |
5561 #endif | |
5562 error("this for %s needs to be type %s not type %s", | |
5563 f->toChars(), ad->toChars(), t->toChars()); | |
5564 } | |
5565 } | |
5566 | |
5567 checkDeprecated(sc, f); | |
5568 accessCheck(loc, sc, ue->e1, f); | |
5569 if (!f->needThis()) | |
5570 { | |
5571 VarExp *ve = new VarExp(loc, f); | |
5572 e1 = new CommaExp(loc, ue->e1, ve); | |
5573 e1->type = f->type; | |
5574 } | |
5575 else | |
5576 { | |
5577 if (e1->op == TOKdotvar) | |
5578 dve->var = f; | |
5579 else | |
5580 e1 = new DotVarExp(loc, dte->e1, f); | |
5581 e1->type = f->type; | |
5582 | |
5583 // See if we need to adjust the 'this' pointer | |
5584 AggregateDeclaration *ad = f->isThis(); | |
5585 ClassDeclaration *cd = ue->e1->type->isClassHandle(); | |
5586 if (ad && cd && ad->isClassDeclaration() && ad != cd && | |
5587 ue->e1->op != TOKsuper) | |
5588 { | |
5589 ue->e1 = ue->e1->castTo(sc, ad->type); //new CastExp(loc, ue->e1, ad->type); | |
5590 ue->e1 = ue->e1->semantic(sc); | |
5591 } | |
5592 } | |
5593 t1 = e1->type; | |
5594 } | |
5595 else if (e1->op == TOKsuper) | |
5596 { | |
5597 // Base class constructor call | |
5598 ClassDeclaration *cd = NULL; | |
5599 | |
5600 if (sc->func) | |
5601 cd = sc->func->toParent()->isClassDeclaration(); | |
5602 if (!cd || !cd->baseClass || !sc->func->isCtorDeclaration()) | |
5603 { | |
5604 error("super class constructor call must be in a constructor"); | |
5605 type = Type::terror; | |
5606 return this; | |
5607 } | |
5608 else | |
5609 { | |
5610 f = cd->baseClass->ctor; | |
5611 if (!f) | |
5612 { error("no super class constructor for %s", cd->baseClass->toChars()); | |
5613 type = Type::terror; | |
5614 return this; | |
5615 } | |
5616 else | |
5617 { | |
5618 #if 0 | |
5619 if (sc->callSuper & (CSXthis | CSXsuper)) | |
5620 error("reference to this before super()"); | |
5621 #endif | |
5622 if (sc->noctor || sc->callSuper & CSXlabel) | |
5623 error("constructor calls not allowed in loops or after labels"); | |
5624 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
5625 error("multiple constructor calls"); | |
5626 sc->callSuper |= CSXany_ctor | CSXsuper_ctor; | |
5627 | |
5628 f = f->overloadResolve(loc, arguments); | |
5629 checkDeprecated(sc, f); | |
5630 e1 = new DotVarExp(e1->loc, e1, f); | |
5631 e1 = e1->semantic(sc); | |
5632 t1 = e1->type; | |
5633 } | |
5634 } | |
5635 } | |
5636 else if (e1->op == TOKthis) | |
5637 { | |
5638 // same class constructor call | |
5639 ClassDeclaration *cd = NULL; | |
5640 | |
5641 if (sc->func) | |
5642 cd = sc->func->toParent()->isClassDeclaration(); | |
5643 if (!cd || !sc->func->isCtorDeclaration()) | |
5644 { | |
5645 error("class constructor call must be in a constructor"); | |
5646 type = Type::terror; | |
5647 return this; | |
5648 } | |
5649 else | |
5650 { | |
5651 #if 0 | |
5652 if (sc->callSuper & (CSXthis | CSXsuper)) | |
5653 error("reference to this before super()"); | |
5654 #endif | |
5655 if (sc->noctor || sc->callSuper & CSXlabel) | |
5656 error("constructor calls not allowed in loops or after labels"); | |
5657 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
5658 error("multiple constructor calls"); | |
5659 sc->callSuper |= CSXany_ctor | CSXthis_ctor; | |
5660 | |
5661 f = cd->ctor; | |
5662 f = f->overloadResolve(loc, arguments); | |
5663 checkDeprecated(sc, f); | |
5664 e1 = new DotVarExp(e1->loc, e1, f); | |
5665 e1 = e1->semantic(sc); | |
5666 t1 = e1->type; | |
5667 | |
5668 // BUG: this should really be done by checking the static | |
5669 // call graph | |
5670 if (f == sc->func) | |
5671 error("cyclic constructor call"); | |
5672 } | |
5673 } | |
5674 else if (!t1) | |
5675 { | |
5676 error("function expected before (), not '%s'", e1->toChars()); | |
5677 type = Type::terror; | |
5678 return this; | |
5679 } | |
5680 else if (t1->ty != Tfunction) | |
5681 { | |
5682 if (t1->ty == Tdelegate) | |
5683 { | |
5684 assert(t1->next->ty == Tfunction); | |
5685 tf = (TypeFunction *)(t1->next); | |
5686 goto Lcheckargs; | |
5687 } | |
5688 else if (t1->ty == Tpointer && t1->next->ty == Tfunction) | |
5689 { Expression *e; | |
5690 | |
5691 e = new PtrExp(loc, e1); | |
5692 t1 = t1->next; | |
5693 e->type = t1; | |
5694 e1 = e; | |
5695 } | |
5696 else if (e1->op == TOKtemplate) | |
5697 { | |
5698 TemplateExp *te = (TemplateExp *)e1; | |
5699 f = te->td->deduce(sc, loc, NULL, arguments); | |
5700 if (!f) | |
5701 { type = Type::terror; | |
5702 return this; | |
5703 } | |
5704 if (f->needThis() && hasThis(sc)) | |
5705 { | |
5706 // Supply an implicit 'this', as in | |
5707 // this.ident | |
5708 | |
5709 e1 = new DotTemplateExp(loc, (new ThisExp(loc))->semantic(sc), te->td); | |
5710 goto Lagain; | |
5711 } | |
5712 | |
5713 e1 = new VarExp(loc, f); | |
5714 goto Lagain; | |
5715 } | |
5716 else | |
5717 { error("function expected before (), not %s of type %s", e1->toChars(), e1->type->toChars()); | |
5718 type = Type::terror; | |
5719 return this; | |
5720 } | |
5721 } | |
5722 else if (e1->op == TOKvar) | |
5723 { | |
5724 // Do overload resolution | |
5725 VarExp *ve = (VarExp *)e1; | |
5726 | |
5727 f = ve->var->isFuncDeclaration(); | |
5728 assert(f); | |
5729 | |
5730 // Look to see if f is really a function template | |
5731 if (0 && !istemp && f->parent) | |
5732 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
5733 | |
5734 if (ti && | |
5735 (ti->name == f->ident || | |
5736 ti->toAlias()->ident == f->ident) | |
5737 && | |
5738 ti->tempdecl) | |
5739 { | |
5740 /* This is so that one can refer to the enclosing | |
5741 * template, even if it has the same name as a member | |
5742 * of the template, if it has a !(arguments) | |
5743 */ | |
5744 TemplateDeclaration *tempdecl = ti->tempdecl; | |
5745 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
5746 tempdecl = tempdecl->overroot; // then get the start | |
5747 e1 = new TemplateExp(loc, tempdecl); | |
5748 istemp = 1; | |
5749 goto Lagain; | |
5750 } | |
5751 } | |
5752 | |
5753 f = f->overloadResolve(loc, arguments); | |
5754 checkDeprecated(sc, f); | |
5755 | |
5756 if (f->needThis() && hasThis(sc)) | |
5757 { | |
5758 // Supply an implicit 'this', as in | |
5759 // this.ident | |
5760 | |
5761 e1 = new DotVarExp(loc, new ThisExp(loc), f); | |
5762 goto Lagain; | |
5763 } | |
5764 | |
5765 accessCheck(loc, sc, NULL, f); | |
5766 | |
5767 ve->var = f; | |
5768 ve->type = f->type; | |
5769 t1 = f->type; | |
5770 } | |
5771 assert(t1->ty == Tfunction); | |
5772 tf = (TypeFunction *)(t1); | |
5773 | |
5774 Lcheckargs: | |
5775 assert(tf->ty == Tfunction); | |
5776 type = tf->next; | |
5777 | |
5778 if (!arguments) | |
5779 arguments = new Expressions(); | |
5780 functionArguments(loc, sc, tf, arguments); | |
5781 | |
5782 assert(type); | |
5783 | |
5784 if (f && f->tintro) | |
5785 { | |
5786 Type *t = type; | |
5787 int offset = 0; | |
5788 | |
5789 if (f->tintro->next->isBaseOf(t, &offset) && offset) | |
5790 { | |
5791 type = f->tintro->next; | |
5792 return castTo(sc, t); | |
5793 } | |
5794 } | |
5795 | |
5796 return this; | |
5797 } | |
5798 | |
5799 int CallExp::checkSideEffect(int flag) | |
5800 { | |
5801 return 1; | |
5802 } | |
5803 | |
5804 Expression *CallExp::toLvalue(Scope *sc, Expression *e) | |
5805 { | |
5806 if (type->toBasetype()->ty == Tstruct) | |
5807 return this; | |
5808 else | |
5809 return Expression::toLvalue(sc, e); | |
5810 } | |
5811 | |
5812 void CallExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5813 { int i; | |
5814 | |
5815 expToCBuffer(buf, hgs, e1, precedence[op]); | |
5816 buf->writeByte('('); | |
5817 argsToCBuffer(buf, arguments, hgs); | |
5818 buf->writeByte(')'); | |
5819 } | |
5820 | |
5821 | |
5822 /************************************************************/ | |
5823 | |
5824 AddrExp::AddrExp(Loc loc, Expression *e) | |
5825 : UnaExp(loc, TOKaddress, sizeof(AddrExp), e) | |
5826 { | |
5827 } | |
5828 | |
5829 Expression *AddrExp::semantic(Scope *sc) | |
5830 { | |
5831 #if LOGSEMANTIC | |
5832 printf("AddrExp::semantic('%s')\n", toChars()); | |
5833 #endif | |
5834 if (!type) | |
5835 { | |
5836 UnaExp::semantic(sc); | |
5837 e1 = e1->toLvalue(sc, NULL); | |
5838 if (!e1->type) | |
5839 { | |
5840 error("cannot take address of %s", e1->toChars()); | |
5841 type = Type::tint32; | |
5842 return this; | |
5843 } | |
5844 type = e1->type->pointerTo(); | |
5845 | |
5846 // See if this should really be a delegate | |
5847 if (e1->op == TOKdotvar) | |
5848 { | |
5849 DotVarExp *dve = (DotVarExp *)e1; | |
5850 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
5851 | |
5852 if (f) | |
5853 { Expression *e; | |
5854 | |
5855 e = new DelegateExp(loc, dve->e1, f); | |
5856 e = e->semantic(sc); | |
5857 return e; | |
5858 } | |
5859 } | |
5860 else if (e1->op == TOKvar) | |
5861 { | |
5862 VarExp *dve = (VarExp *)e1; | |
5863 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
5864 | |
5865 if (f && f->isNested()) | |
5866 { Expression *e; | |
5867 | |
5868 e = new DelegateExp(loc, e1, f); | |
5869 e = e->semantic(sc); | |
5870 return e; | |
5871 } | |
5872 } | |
5873 else if (e1->op == TOKarray) | |
5874 { | |
5875 if (e1->type->toBasetype()->ty == Tbit) | |
5876 error("cannot take address of bit in array"); | |
5877 } | |
5878 return optimize(WANTvalue); | |
5879 } | |
5880 return this; | |
5881 } | |
5882 | |
5883 /************************************************************/ | |
5884 | |
5885 PtrExp::PtrExp(Loc loc, Expression *e) | |
5886 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
5887 { | |
5888 if (e->type) | |
5889 type = e->type->next; | |
5890 } | |
5891 | |
5892 PtrExp::PtrExp(Loc loc, Expression *e, Type *t) | |
5893 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
5894 { | |
5895 type = t; | |
5896 } | |
5897 | |
5898 Expression *PtrExp::semantic(Scope *sc) | |
5899 { Type *tb; | |
5900 | |
5901 #if LOGSEMANTIC | |
5902 printf("PtrExp::semantic('%s')\n", toChars()); | |
5903 #endif | |
5904 UnaExp::semantic(sc); | |
5905 e1 = resolveProperties(sc, e1); | |
5906 if (type) | |
5907 return this; | |
5908 if (!e1->type) | |
5909 printf("PtrExp::semantic('%s')\n", toChars()); | |
5910 tb = e1->type->toBasetype(); | |
5911 switch (tb->ty) | |
5912 { | |
5913 case Tpointer: | |
5914 type = tb->next; | |
5915 if (type->isbit()) | |
5916 { Expression *e; | |
5917 | |
5918 // Rewrite *p as p[0] | |
5919 e = new IndexExp(loc, e1, new IntegerExp(0)); | |
5920 return e->semantic(sc); | |
5921 } | |
5922 break; | |
5923 | |
5924 case Tsarray: | |
5925 case Tarray: | |
5926 type = tb->next; | |
5927 e1 = e1->castTo(sc, type->pointerTo()); | |
5928 break; | |
5929 | |
5930 default: | |
5931 error("can only * a pointer, not a '%s'", e1->type->toChars()); | |
5932 type = Type::tint32; | |
5933 break; | |
5934 } | |
5935 rvalue(); | |
5936 return this; | |
5937 } | |
5938 | |
5939 Expression *PtrExp::toLvalue(Scope *sc, Expression *e) | |
5940 { | |
5941 #if 0 | |
5942 tym = tybasic(e1->ET->Tty); | |
5943 if (!(tyscalar(tym) || | |
5944 tym == TYstruct || | |
5945 tym == TYarray && e->Eoper == TOKaddr)) | |
5946 synerr(EM_lvalue); // lvalue expected | |
5947 #endif | |
5948 return this; | |
5949 } | |
5950 | |
5951 void PtrExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5952 { | |
5953 buf->writeByte('*'); | |
5954 expToCBuffer(buf, hgs, e1, precedence[op]); | |
5955 } | |
5956 | |
5957 /************************************************************/ | |
5958 | |
5959 NegExp::NegExp(Loc loc, Expression *e) | |
5960 : UnaExp(loc, TOKneg, sizeof(NegExp), e) | |
5961 { | |
5962 } | |
5963 | |
5964 Expression *NegExp::semantic(Scope *sc) | |
5965 { Expression *e; | |
5966 | |
5967 #if LOGSEMANTIC | |
5968 printf("NegExp::semantic('%s')\n", toChars()); | |
5969 #endif | |
5970 if (!type) | |
5971 { | |
5972 UnaExp::semantic(sc); | |
5973 e1 = resolveProperties(sc, e1); | |
5974 e = op_overload(sc); | |
5975 if (e) | |
5976 return e; | |
5977 | |
5978 e1->checkNoBool(); | |
5979 e1->checkArithmetic(); | |
5980 type = e1->type; | |
5981 } | |
5982 return this; | |
5983 } | |
5984 | |
5985 /************************************************************/ | |
5986 | |
5987 UAddExp::UAddExp(Loc loc, Expression *e) | |
5988 : UnaExp(loc, TOKuadd, sizeof(UAddExp), e) | |
5989 { | |
5990 } | |
5991 | |
5992 Expression *UAddExp::semantic(Scope *sc) | |
5993 { Expression *e; | |
5994 | |
5995 #if LOGSEMANTIC | |
5996 printf("UAddExp::semantic('%s')\n", toChars()); | |
5997 #endif | |
5998 assert(!type); | |
5999 UnaExp::semantic(sc); | |
6000 e1 = resolveProperties(sc, e1); | |
6001 e = op_overload(sc); | |
6002 if (e) | |
6003 return e; | |
6004 e1->checkNoBool(); | |
6005 e1->checkArithmetic(); | |
6006 return e1; | |
6007 } | |
6008 | |
6009 /************************************************************/ | |
6010 | |
6011 ComExp::ComExp(Loc loc, Expression *e) | |
6012 : UnaExp(loc, TOKtilde, sizeof(ComExp), e) | |
6013 { | |
6014 } | |
6015 | |
6016 Expression *ComExp::semantic(Scope *sc) | |
6017 { Expression *e; | |
6018 | |
6019 if (!type) | |
6020 { | |
6021 UnaExp::semantic(sc); | |
6022 e1 = resolveProperties(sc, e1); | |
6023 e = op_overload(sc); | |
6024 if (e) | |
6025 return e; | |
6026 | |
6027 e1->checkNoBool(); | |
6028 e1 = e1->checkIntegral(); | |
6029 type = e1->type; | |
6030 } | |
6031 return this; | |
6032 } | |
6033 | |
6034 /************************************************************/ | |
6035 | |
6036 NotExp::NotExp(Loc loc, Expression *e) | |
6037 : UnaExp(loc, TOKnot, sizeof(NotExp), e) | |
6038 { | |
6039 } | |
6040 | |
6041 Expression *NotExp::semantic(Scope *sc) | |
6042 { | |
6043 UnaExp::semantic(sc); | |
6044 e1 = resolveProperties(sc, e1); | |
6045 e1 = e1->checkToBoolean(); | |
6046 type = Type::tboolean; | |
6047 return this; | |
6048 } | |
6049 | |
6050 int NotExp::isBit() | |
6051 { | |
6052 return TRUE; | |
6053 } | |
6054 | |
6055 | |
6056 | |
6057 /************************************************************/ | |
6058 | |
6059 BoolExp::BoolExp(Loc loc, Expression *e, Type *t) | |
6060 : UnaExp(loc, TOKtobool, sizeof(BoolExp), e) | |
6061 { | |
6062 type = t; | |
6063 } | |
6064 | |
6065 Expression *BoolExp::semantic(Scope *sc) | |
6066 { | |
6067 UnaExp::semantic(sc); | |
6068 e1 = resolveProperties(sc, e1); | |
6069 e1 = e1->checkToBoolean(); | |
6070 type = Type::tboolean; | |
6071 return this; | |
6072 } | |
6073 | |
6074 int BoolExp::isBit() | |
6075 { | |
6076 return TRUE; | |
6077 } | |
6078 | |
6079 /************************************************************/ | |
6080 | |
6081 DeleteExp::DeleteExp(Loc loc, Expression *e) | |
6082 : UnaExp(loc, TOKdelete, sizeof(DeleteExp), e) | |
6083 { | |
6084 } | |
6085 | |
6086 Expression *DeleteExp::semantic(Scope *sc) | |
6087 { | |
6088 Type *tb; | |
6089 | |
6090 UnaExp::semantic(sc); | |
6091 e1 = resolveProperties(sc, e1); | |
6092 e1 = e1->toLvalue(sc, NULL); | |
6093 type = Type::tvoid; | |
6094 | |
6095 tb = e1->type->toBasetype(); | |
6096 switch (tb->ty) | |
6097 { case Tclass: | |
6098 { TypeClass *tc = (TypeClass *)tb; | |
6099 ClassDeclaration *cd = tc->sym; | |
6100 | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
6101 if (cd->isCOMinterface()) |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
6102 { /* Because COM classes are deleted by IUnknown.Release() |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
6103 */ |
1 | 6104 error("cannot delete instance of COM interface %s", cd->toChars()); |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
6105 } |
1 | 6106 break; |
6107 } | |
6108 case Tpointer: | |
6109 tb = tb->next->toBasetype(); | |
6110 if (tb->ty == Tstruct) | |
6111 { | |
6112 TypeStruct *ts = (TypeStruct *)tb; | |
6113 StructDeclaration *sd = ts->sym; | |
6114 FuncDeclaration *f = sd->aggDelete; | |
6115 | |
6116 if (f) | |
6117 { | |
6118 Expression *e; | |
6119 Expression *ec; | |
6120 Type *tpv = Type::tvoid->pointerTo(); | |
6121 | |
6122 e = e1; | |
6123 e->type = tpv; | |
6124 ec = new VarExp(loc, f); | |
6125 e = new CallExp(loc, ec, e); | |
6126 return e->semantic(sc); | |
6127 } | |
6128 } | |
6129 break; | |
6130 | |
6131 case Tarray: | |
6132 break; | |
6133 | |
6134 default: | |
6135 if (e1->op == TOKindex) | |
6136 { | |
6137 IndexExp *ae = (IndexExp *)(e1); | |
6138 Type *tb1 = ae->e1->type->toBasetype(); | |
6139 if (tb1->ty == Taarray) | |
6140 break; | |
6141 } | |
6142 error("cannot delete type %s", e1->type->toChars()); | |
6143 break; | |
6144 } | |
6145 | |
6146 if (e1->op == TOKindex) | |
6147 { | |
6148 IndexExp *ae = (IndexExp *)(e1); | |
6149 Type *tb1 = ae->e1->type->toBasetype(); | |
6150 if (tb1->ty == Taarray) | |
6151 { if (!global.params.useDeprecated) | |
6152 error("delete aa[key] deprecated, use aa.remove(key)"); | |
6153 } | |
6154 } | |
6155 | |
6156 return this; | |
6157 } | |
6158 | |
6159 int DeleteExp::checkSideEffect(int flag) | |
6160 { | |
6161 return 1; | |
6162 } | |
6163 | |
6164 Expression *DeleteExp::checkToBoolean() | |
6165 { | |
6166 error("delete does not give a boolean result"); | |
6167 return this; | |
6168 } | |
6169 | |
6170 void DeleteExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6171 { | |
6172 buf->writestring("delete "); | |
6173 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6174 } | |
6175 | |
6176 /************************************************************/ | |
6177 | |
6178 CastExp::CastExp(Loc loc, Expression *e, Type *t) | |
6179 : UnaExp(loc, TOKcast, sizeof(CastExp), e) | |
6180 { | |
6181 to = t; | |
6182 } | |
6183 | |
6184 Expression *CastExp::syntaxCopy() | |
6185 { | |
6186 return new CastExp(loc, e1->syntaxCopy(), to->syntaxCopy()); | |
6187 } | |
6188 | |
6189 | |
6190 Expression *CastExp::semantic(Scope *sc) | |
6191 { Expression *e; | |
6192 BinExp *b; | |
6193 UnaExp *u; | |
6194 | |
6195 #if LOGSEMANTIC | |
6196 printf("CastExp::semantic('%s')\n", toChars()); | |
6197 #endif | |
6198 | |
6199 //static int x; assert(++x < 10); | |
6200 | |
6201 if (type) | |
6202 return this; | |
6203 UnaExp::semantic(sc); | |
6204 if (e1->type) // if not a tuple | |
6205 { | |
6206 e1 = resolveProperties(sc, e1); | |
6207 to = to->semantic(loc, sc); | |
6208 | |
6209 e = op_overload(sc); | |
6210 if (e) | |
6211 { | |
6212 return e->implicitCastTo(sc, to); | |
6213 } | |
6214 | |
6215 Type *tob = to->toBasetype(); | |
6216 if (tob->ty == Tstruct && !tob->equals(e1->type->toBasetype())) | |
6217 { | |
6218 /* Look to replace: | |
6219 * cast(S)t | |
6220 * with: | |
6221 * S(t) | |
6222 */ | |
6223 | |
6224 // Rewrite as to.call(e1) | |
6225 e = new TypeExp(loc, to); | |
6226 e = new DotIdExp(loc, e, Id::call); | |
6227 e = new CallExp(loc, e, e1); | |
6228 e = e->semantic(sc); | |
6229 return e; | |
6230 } | |
6231 } | |
6232 e = e1->castTo(sc, to); | |
6233 return e; | |
6234 } | |
6235 | |
6236 int CastExp::checkSideEffect(int flag) | |
6237 { | |
6238 /* if not: | |
6239 * cast(void) | |
6240 * cast(classtype)func() | |
6241 */ | |
6242 if (!to->equals(Type::tvoid) && | |
6243 !(to->ty == Tclass && e1->op == TOKcall && e1->type->ty == Tclass)) | |
6244 return Expression::checkSideEffect(flag); | |
6245 return 1; | |
6246 } | |
6247 | |
6248 void CastExp::checkEscape() | |
6249 { Type *tb = type->toBasetype(); | |
6250 if (tb->ty == Tarray && e1->op == TOKvar && | |
6251 e1->type->toBasetype()->ty == Tsarray) | |
6252 { VarExp *ve = (VarExp *)e1; | |
6253 VarDeclaration *v = ve->var->isVarDeclaration(); | |
6254 if (v) | |
6255 { | |
6256 if (!v->isDataseg() && !v->isParameter()) | |
6257 error("escaping reference to local %s", v->toChars()); | |
6258 } | |
6259 } | |
6260 } | |
6261 | |
6262 void CastExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6263 { | |
6264 buf->writestring("cast("); | |
6265 to->toCBuffer(buf, NULL, hgs); | |
6266 buf->writeByte(')'); | |
6267 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6268 } | |
6269 | |
6270 | |
6271 /************************************************************/ | |
6272 | |
6273 SliceExp::SliceExp(Loc loc, Expression *e1, Expression *lwr, Expression *upr) | |
6274 : UnaExp(loc, TOKslice, sizeof(SliceExp), e1) | |
6275 { | |
6276 this->upr = upr; | |
6277 this->lwr = lwr; | |
6278 lengthVar = NULL; | |
6279 } | |
6280 | |
6281 Expression *SliceExp::syntaxCopy() | |
6282 { | |
6283 Expression *lwr = NULL; | |
6284 if (this->lwr) | |
6285 lwr = this->lwr->syntaxCopy(); | |
6286 | |
6287 Expression *upr = NULL; | |
6288 if (this->upr) | |
6289 upr = this->upr->syntaxCopy(); | |
6290 | |
6291 return new SliceExp(loc, e1->syntaxCopy(), lwr, upr); | |
6292 } | |
6293 | |
6294 Expression *SliceExp::semantic(Scope *sc) | |
6295 { Expression *e; | |
6296 AggregateDeclaration *ad; | |
6297 //FuncDeclaration *fd; | |
6298 ScopeDsymbol *sym; | |
6299 | |
6300 #if LOGSEMANTIC | |
6301 printf("SliceExp::semantic('%s')\n", toChars()); | |
6302 #endif | |
6303 if (type) | |
6304 return this; | |
6305 | |
6306 UnaExp::semantic(sc); | |
6307 e1 = resolveProperties(sc, e1); | |
6308 | |
6309 e = this; | |
6310 | |
6311 Type *t = e1->type->toBasetype(); | |
6312 if (t->ty == Tpointer) | |
6313 { | |
6314 if (!lwr || !upr) | |
6315 error("need upper and lower bound to slice pointer"); | |
6316 } | |
6317 else if (t->ty == Tarray) | |
6318 { | |
6319 } | |
6320 else if (t->ty == Tsarray) | |
6321 { | |
6322 } | |
6323 else if (t->ty == Tclass) | |
6324 { | |
6325 ad = ((TypeClass *)t)->sym; | |
6326 goto L1; | |
6327 } | |
6328 else if (t->ty == Tstruct) | |
6329 { | |
6330 ad = ((TypeStruct *)t)->sym; | |
6331 | |
6332 L1: | |
6333 if (search_function(ad, Id::slice)) | |
6334 { | |
6335 // Rewrite as e1.slice(lwr, upr) | |
6336 e = new DotIdExp(loc, e1, Id::slice); | |
6337 | |
6338 if (lwr) | |
6339 { | |
6340 assert(upr); | |
6341 e = new CallExp(loc, e, lwr, upr); | |
6342 } | |
6343 else | |
6344 { assert(!upr); | |
6345 e = new CallExp(loc, e); | |
6346 } | |
6347 e = e->semantic(sc); | |
6348 return e; | |
6349 } | |
6350 goto Lerror; | |
6351 } | |
6352 else if (t->ty == Ttuple) | |
6353 { | |
6354 if (!lwr && !upr) | |
6355 return e1; | |
6356 if (!lwr || !upr) | |
6357 { error("need upper and lower bound to slice tuple"); | |
6358 goto Lerror; | |
6359 } | |
6360 } | |
6361 else | |
6362 goto Lerror; | |
6363 | |
6364 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
6365 { | |
6366 sym = new ArrayScopeSymbol(this); | |
6367 sym->loc = loc; | |
6368 sym->parent = sc->scopesym; | |
6369 sc = sc->push(sym); | |
6370 } | |
6371 | |
6372 if (lwr) | |
6373 { lwr = lwr->semantic(sc); | |
6374 lwr = resolveProperties(sc, lwr); | |
6375 lwr = lwr->implicitCastTo(sc, Type::tsize_t); | |
6376 } | |
6377 if (upr) | |
6378 { upr = upr->semantic(sc); | |
6379 upr = resolveProperties(sc, upr); | |
6380 upr = upr->implicitCastTo(sc, Type::tsize_t); | |
6381 } | |
6382 | |
6383 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
6384 sc->pop(); | |
6385 | |
6386 if (t->ty == Ttuple) | |
6387 { | |
6388 lwr = lwr->optimize(WANTvalue); | |
6389 upr = upr->optimize(WANTvalue); | |
6390 uinteger_t i1 = lwr->toUInteger(); | |
6391 uinteger_t i2 = upr->toUInteger(); | |
6392 | |
6393 size_t length; | |
6394 TupleExp *te; | |
6395 TypeTuple *tup; | |
6396 | |
6397 if (e1->op == TOKtuple) // slicing an expression tuple | |
6398 { te = (TupleExp *)e1; | |
6399 length = te->exps->dim; | |
6400 } | |
6401 else if (e1->op == TOKtype) // slicing a type tuple | |
6402 { tup = (TypeTuple *)t; | |
6403 length = Argument::dim(tup->arguments); | |
6404 } | |
6405 else | |
6406 assert(0); | |
6407 | |
6408 if (i1 <= i2 && i2 <= length) | |
6409 { size_t j1 = (size_t) i1; | |
6410 size_t j2 = (size_t) i2; | |
6411 | |
6412 if (e1->op == TOKtuple) | |
6413 { Expressions *exps = new Expressions; | |
6414 exps->setDim(j2 - j1); | |
6415 for (size_t i = 0; i < j2 - j1; i++) | |
6416 { Expression *e = (Expression *)te->exps->data[j1 + i]; | |
6417 exps->data[i] = (void *)e; | |
6418 } | |
6419 e = new TupleExp(loc, exps); | |
6420 } | |
6421 else | |
6422 { Arguments *args = new Arguments; | |
6423 args->reserve(j2 - j1); | |
6424 for (size_t i = j1; i < j2; i++) | |
6425 { Argument *arg = Argument::getNth(tup->arguments, i); | |
6426 args->push(arg); | |
6427 } | |
6428 e = new TypeExp(e1->loc, new TypeTuple(args)); | |
6429 } | |
6430 e = e->semantic(sc); | |
6431 } | |
6432 else | |
6433 { | |
6434 error("string slice [%ju .. %ju] is out of bounds", i1, i2); | |
6435 e = e1; | |
6436 } | |
6437 return e; | |
6438 } | |
6439 | |
6440 type = t->next->arrayOf(); | |
6441 return e; | |
6442 | |
6443 Lerror: | |
6444 char *s; | |
6445 if (t->ty == Tvoid) | |
6446 s = e1->toChars(); | |
6447 else | |
6448 s = t->toChars(); | |
6449 error("%s cannot be sliced with []", s); | |
6450 type = Type::terror; | |
6451 return e; | |
6452 } | |
6453 | |
6454 void SliceExp::checkEscape() | |
6455 { | |
6456 e1->checkEscape(); | |
6457 } | |
6458 | |
6459 Expression *SliceExp::toLvalue(Scope *sc, Expression *e) | |
6460 { | |
6461 return this; | |
6462 } | |
6463 | |
6464 Expression *SliceExp::modifiableLvalue(Scope *sc, Expression *e) | |
6465 { | |
6466 error("slice expression %s is not a modifiable lvalue", toChars()); | |
6467 return this; | |
6468 } | |
6469 | |
6470 void SliceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6471 { | |
6472 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6473 buf->writeByte('['); | |
6474 if (upr || lwr) | |
6475 { | |
6476 if (lwr) | |
6477 expToCBuffer(buf, hgs, lwr, PREC_assign); | |
6478 else | |
6479 buf->writeByte('0'); | |
6480 buf->writestring(".."); | |
6481 if (upr) | |
6482 expToCBuffer(buf, hgs, upr, PREC_assign); | |
6483 else | |
6484 buf->writestring("length"); // BUG: should be array.length | |
6485 } | |
6486 buf->writeByte(']'); | |
6487 } | |
6488 | |
6489 /********************** ArrayLength **************************************/ | |
6490 | |
6491 ArrayLengthExp::ArrayLengthExp(Loc loc, Expression *e1) | |
6492 : UnaExp(loc, TOKarraylength, sizeof(ArrayLengthExp), e1) | |
6493 { | |
6494 } | |
6495 | |
6496 Expression *ArrayLengthExp::semantic(Scope *sc) | |
6497 { Expression *e; | |
6498 | |
6499 #if LOGSEMANTIC | |
6500 printf("ArrayLengthExp::semantic('%s')\n", toChars()); | |
6501 #endif | |
6502 if (!type) | |
6503 { | |
6504 UnaExp::semantic(sc); | |
6505 e1 = resolveProperties(sc, e1); | |
6506 | |
6507 type = Type::tsize_t; | |
6508 } | |
6509 return this; | |
6510 } | |
6511 | |
6512 void ArrayLengthExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6513 { | |
6514 expToCBuffer(buf, hgs, e1, PREC_primary); | |
6515 buf->writestring(".length"); | |
6516 } | |
6517 | |
6518 /*********************** ArrayExp *************************************/ | |
6519 | |
6520 // e1 [ i1, i2, i3, ... ] | |
6521 | |
6522 ArrayExp::ArrayExp(Loc loc, Expression *e1, Expressions *args) | |
6523 : UnaExp(loc, TOKarray, sizeof(ArrayExp), e1) | |
6524 { | |
6525 arguments = args; | |
6526 } | |
6527 | |
6528 Expression *ArrayExp::syntaxCopy() | |
6529 { | |
6530 return new ArrayExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
6531 } | |
6532 | |
6533 Expression *ArrayExp::semantic(Scope *sc) | |
6534 { Expression *e; | |
6535 Type *t1; | |
6536 | |
6537 #if LOGSEMANTIC | |
6538 printf("ArrayExp::semantic('%s')\n", toChars()); | |
6539 #endif | |
6540 UnaExp::semantic(sc); | |
6541 e1 = resolveProperties(sc, e1); | |
6542 | |
6543 t1 = e1->type->toBasetype(); | |
6544 if (t1->ty != Tclass && t1->ty != Tstruct) | |
6545 { // Convert to IndexExp | |
6546 if (arguments->dim != 1) | |
6547 error("only one index allowed to index %s", t1->toChars()); | |
6548 e = new IndexExp(loc, e1, (Expression *)arguments->data[0]); | |
6549 return e->semantic(sc); | |
6550 } | |
6551 | |
6552 // Run semantic() on each argument | |
6553 for (size_t i = 0; i < arguments->dim; i++) | |
6554 { e = (Expression *)arguments->data[i]; | |
6555 | |
6556 e = e->semantic(sc); | |
6557 if (!e->type) | |
6558 error("%s has no value", e->toChars()); | |
6559 arguments->data[i] = (void *)e; | |
6560 } | |
6561 | |
6562 expandTuples(arguments); | |
6563 assert(arguments && arguments->dim); | |
6564 | |
6565 e = op_overload(sc); | |
6566 if (!e) | |
6567 { error("no [] operator overload for type %s", e1->type->toChars()); | |
6568 e = e1; | |
6569 } | |
6570 return e; | |
6571 } | |
6572 | |
6573 | |
6574 Expression *ArrayExp::toLvalue(Scope *sc, Expression *e) | |
6575 { | |
6576 if (type && type->toBasetype()->ty == Tvoid) | |
6577 error("voids have no value"); | |
6578 return this; | |
6579 } | |
6580 | |
6581 | |
6582 void ArrayExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6583 { int i; | |
6584 | |
6585 expToCBuffer(buf, hgs, e1, PREC_primary); | |
6586 buf->writeByte('['); | |
6587 argsToCBuffer(buf, arguments, hgs); | |
6588 buf->writeByte(']'); | |
6589 } | |
6590 | |
6591 /************************* DotExp ***********************************/ | |
6592 | |
6593 DotExp::DotExp(Loc loc, Expression *e1, Expression *e2) | |
6594 : BinExp(loc, TOKdotexp, sizeof(DotExp), e1, e2) | |
6595 { | |
6596 } | |
6597 | |
6598 Expression *DotExp::semantic(Scope *sc) | |
6599 { | |
6600 #if LOGSEMANTIC | |
6601 printf("DotExp::semantic('%s')\n", toChars()); | |
6602 if (type) printf("\ttype = %s\n", type->toChars()); | |
6603 #endif | |
6604 e1 = e1->semantic(sc); | |
6605 e2 = e2->semantic(sc); | |
6606 if (e2->op == TOKimport) | |
6607 { | |
6608 ScopeExp *se = (ScopeExp *)e2; | |
6609 TemplateDeclaration *td = se->sds->isTemplateDeclaration(); | |
6610 if (td) | |
6611 { Expression *e = new DotTemplateExp(loc, e1, td); | |
6612 e = e->semantic(sc); | |
6613 return e; | |
6614 } | |
6615 } | |
6616 if (!type) | |
6617 type = e2->type; | |
6618 return this; | |
6619 } | |
6620 | |
6621 | |
6622 /************************* CommaExp ***********************************/ | |
6623 | |
6624 CommaExp::CommaExp(Loc loc, Expression *e1, Expression *e2) | |
6625 : BinExp(loc, TOKcomma, sizeof(CommaExp), e1, e2) | |
6626 { | |
6627 } | |
6628 | |
6629 Expression *CommaExp::semantic(Scope *sc) | |
6630 { | |
6631 if (!type) | |
6632 { BinExp::semanticp(sc); | |
6633 type = e2->type; | |
6634 } | |
6635 return this; | |
6636 } | |
6637 | |
6638 void CommaExp::checkEscape() | |
6639 { | |
6640 e2->checkEscape(); | |
6641 } | |
6642 | |
6643 Expression *CommaExp::toLvalue(Scope *sc, Expression *e) | |
6644 { | |
6645 e2 = e2->toLvalue(sc, NULL); | |
6646 return this; | |
6647 } | |
6648 | |
6649 Expression *CommaExp::modifiableLvalue(Scope *sc, Expression *e) | |
6650 { | |
6651 e2 = e2->modifiableLvalue(sc, e); | |
6652 return this; | |
6653 } | |
6654 | |
6655 int CommaExp::isBool(int result) | |
6656 { | |
6657 return e2->isBool(result); | |
6658 } | |
6659 | |
6660 int CommaExp::checkSideEffect(int flag) | |
6661 { | |
6662 if (flag == 2) | |
6663 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
6664 else | |
6665 { | |
6666 // Don't check e1 until we cast(void) the a,b code generation | |
6667 return e2->checkSideEffect(flag); | |
6668 } | |
6669 } | |
6670 | |
6671 /************************** IndexExp **********************************/ | |
6672 | |
6673 // e1 [ e2 ] | |
6674 | |
6675 IndexExp::IndexExp(Loc loc, Expression *e1, Expression *e2) | |
6676 : BinExp(loc, TOKindex, sizeof(IndexExp), e1, e2) | |
6677 { | |
6678 //printf("IndexExp::IndexExp('%s')\n", toChars()); | |
6679 lengthVar = NULL; | |
6680 modifiable = 0; // assume it is an rvalue | |
6681 } | |
6682 | |
6683 Expression *IndexExp::semantic(Scope *sc) | |
6684 { Expression *e; | |
6685 BinExp *b; | |
6686 UnaExp *u; | |
6687 Type *t1; | |
6688 ScopeDsymbol *sym; | |
6689 | |
6690 #if LOGSEMANTIC | |
6691 printf("IndexExp::semantic('%s')\n", toChars()); | |
6692 #endif | |
6693 if (type) | |
6694 return this; | |
6695 if (!e1->type) | |
6696 e1 = e1->semantic(sc); | |
6697 assert(e1->type); // semantic() should already be run on it | |
6698 e = this; | |
6699 | |
6700 // Note that unlike C we do not implement the int[ptr] | |
6701 | |
6702 t1 = e1->type->toBasetype(); | |
6703 | |
6704 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
6705 { // Create scope for 'length' variable | |
6706 sym = new ArrayScopeSymbol(this); | |
6707 sym->loc = loc; | |
6708 sym->parent = sc->scopesym; | |
6709 sc = sc->push(sym); | |
6710 } | |
6711 | |
6712 e2 = e2->semantic(sc); | |
6713 if (!e2->type) | |
6714 { | |
6715 error("%s has no value", e2->toChars()); | |
6716 e2->type = Type::terror; | |
6717 } | |
6718 e2 = resolveProperties(sc, e2); | |
6719 | |
6720 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
6721 sc = sc->pop(); | |
6722 | |
6723 switch (t1->ty) | |
6724 { | |
6725 case Tpointer: | |
6726 case Tarray: | |
6727 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
6728 e->type = t1->next; | |
6729 break; | |
6730 | |
6731 case Tsarray: | |
6732 { | |
6733 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
6734 | |
6735 TypeSArray *tsa = (TypeSArray *)t1; | |
6736 | |
6737 #if 0 // Don't do now, because it might be short-circuit evaluated | |
6738 // Do compile time array bounds checking if possible | |
6739 e2 = e2->optimize(WANTvalue); | |
6740 if (e2->op == TOKint64) | |
6741 { | |
6742 integer_t index = e2->toInteger(); | |
6743 integer_t length = tsa->dim->toInteger(); | |
6744 if (index < 0 || index >= length) | |
6745 error("array index [%lld] is outside array bounds [0 .. %lld]", | |
6746 index, length); | |
6747 } | |
6748 #endif | |
6749 e->type = t1->next; | |
6750 break; | |
6751 } | |
6752 | |
6753 case Taarray: | |
6754 { TypeAArray *taa = (TypeAArray *)t1; | |
6755 | |
6756 e2 = e2->implicitCastTo(sc, taa->index); // type checking | |
6757 e2 = e2->implicitCastTo(sc, taa->key); // actual argument type | |
6758 type = taa->next; | |
6759 break; | |
6760 } | |
6761 | |
6762 case Ttuple: | |
6763 { | |
6764 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
6765 e2 = e2->optimize(WANTvalue); | |
6766 uinteger_t index = e2->toUInteger(); | |
6767 size_t length; | |
6768 TupleExp *te; | |
6769 TypeTuple *tup; | |
6770 | |
6771 if (e1->op == TOKtuple) | |
6772 { te = (TupleExp *)e1; | |
6773 length = te->exps->dim; | |
6774 } | |
6775 else if (e1->op == TOKtype) | |
6776 { | |
6777 tup = (TypeTuple *)t1; | |
6778 length = Argument::dim(tup->arguments); | |
6779 } | |
6780 else | |
6781 assert(0); | |
6782 | |
6783 if (index < length) | |
6784 { | |
6785 | |
6786 if (e1->op == TOKtuple) | |
6787 e = (Expression *)te->exps->data[(size_t)index]; | |
6788 else | |
6789 e = new TypeExp(e1->loc, Argument::getNth(tup->arguments, (size_t)index)->type); | |
6790 } | |
6791 else | |
6792 { | |
6793 error("array index [%ju] is outside array bounds [0 .. %zu]", | |
6794 index, length); | |
6795 e = e1; | |
6796 } | |
6797 break; | |
6798 } | |
6799 | |
6800 default: | |
6801 error("%s must be an array or pointer type, not %s", | |
6802 e1->toChars(), e1->type->toChars()); | |
6803 type = Type::tint32; | |
6804 break; | |
6805 } | |
6806 return e; | |
6807 } | |
6808 | |
6809 Expression *IndexExp::toLvalue(Scope *sc, Expression *e) | |
6810 { | |
6811 // if (type && type->toBasetype()->ty == Tvoid) | |
6812 // error("voids have no value"); | |
6813 return this; | |
6814 } | |
6815 | |
6816 Expression *IndexExp::modifiableLvalue(Scope *sc, Expression *e) | |
6817 { | |
6818 //printf("IndexExp::modifiableLvalue(%s)\n", toChars()); | |
6819 modifiable = 1; | |
6820 if (e1->op == TOKstring) | |
6821 error("string literals are immutable"); | |
6822 if (e1->type->toBasetype()->ty == Taarray) | |
6823 e1 = e1->modifiableLvalue(sc, e1); | |
6824 return toLvalue(sc, e); | |
6825 } | |
6826 | |
6827 void IndexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6828 { | |
6829 expToCBuffer(buf, hgs, e1, PREC_primary); | |
6830 buf->writeByte('['); | |
6831 expToCBuffer(buf, hgs, e2, PREC_assign); | |
6832 buf->writeByte(']'); | |
6833 } | |
6834 | |
6835 | |
6836 /************************* PostExp ***********************************/ | |
6837 | |
6838 PostExp::PostExp(enum TOK op, Loc loc, Expression *e) | |
6839 : BinExp(loc, op, sizeof(PostExp), e, | |
6840 new IntegerExp(loc, 1, Type::tint32)) | |
6841 { | |
6842 } | |
6843 | |
6844 Expression *PostExp::semantic(Scope *sc) | |
6845 { Expression *e = this; | |
6846 | |
6847 if (!type) | |
6848 { | |
6849 BinExp::semantic(sc); | |
6850 e2 = resolveProperties(sc, e2); | |
6851 | |
6852 e = op_overload(sc); | |
6853 if (e) | |
6854 return e; | |
6855 | |
6856 e = this; | |
6857 e1 = e1->modifiableLvalue(sc, NULL); | |
6858 e1->checkScalar(); | |
6859 e1->checkNoBool(); | |
6860 if (e1->type->ty == Tpointer) | |
6861 e = scaleFactor(sc); | |
6862 else | |
6863 e2 = e2->castTo(sc, e1->type); | |
6864 e->type = e1->type; | |
6865 } | |
6866 return e; | |
6867 } | |
6868 | |
6869 void PostExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6870 { | |
6871 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6872 buf->writestring((op == TOKplusplus) ? (char *)"++" : (char *)"--"); | |
6873 } | |
6874 | |
6875 /************************************************************/ | |
6876 | |
6877 /* Can be TOKconstruct too */ | |
6878 | |
6879 AssignExp::AssignExp(Loc loc, Expression *e1, Expression *e2) | |
6880 : BinExp(loc, TOKassign, sizeof(AssignExp), e1, e2) | |
6881 { | |
6882 ismemset = 0; | |
6883 } | |
6884 | |
6885 Expression *AssignExp::semantic(Scope *sc) | |
6886 { Type *t1; | |
6887 Expression *e1old = e1; | |
6888 | |
6889 #if LOGSEMANTIC | |
6890 printf("AssignExp::semantic('%s')\n", toChars()); | |
6891 #endif | |
6892 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
6893 | |
6894 /* Look for operator overloading of a[i]=value. | |
6895 * Do it before semantic() otherwise the a[i] will have been | |
6896 * converted to a.opIndex() already. | |
6897 */ | |
6898 if (e1->op == TOKarray) | |
6899 { Type *t1; | |
6900 ArrayExp *ae = (ArrayExp *)e1; | |
6901 AggregateDeclaration *ad; | |
6902 Identifier *id = Id::index; | |
6903 | |
6904 ae->e1 = ae->e1->semantic(sc); | |
6905 t1 = ae->e1->type->toBasetype(); | |
6906 if (t1->ty == Tstruct) | |
6907 { | |
6908 ad = ((TypeStruct *)t1)->sym; | |
6909 goto L1; | |
6910 } | |
6911 else if (t1->ty == Tclass) | |
6912 { | |
6913 ad = ((TypeClass *)t1)->sym; | |
6914 L1: | |
6915 // Rewrite (a[i] = value) to (a.opIndexAssign(value, i)) | |
6916 if (search_function(ad, Id::indexass)) | |
6917 { Expression *e = new DotIdExp(loc, ae->e1, Id::indexass); | |
6918 Expressions *a = (Expressions *)ae->arguments->copy(); | |
6919 | |
6920 a->insert(0, e2); | |
6921 e = new CallExp(loc, e, a); | |
6922 e = e->semantic(sc); | |
6923 return e; | |
6924 } | |
6925 else | |
6926 { | |
6927 // Rewrite (a[i] = value) to (a.opIndex(i, value)) | |
6928 if (search_function(ad, id)) | |
6929 { Expression *e = new DotIdExp(loc, ae->e1, id); | |
6930 | |
6931 if (1 || !global.params.useDeprecated) | |
6932 error("operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)"); | |
6933 | |
6934 e = new CallExp(loc, e, (Expression *)ae->arguments->data[0], e2); | |
6935 e = e->semantic(sc); | |
6936 return e; | |
6937 } | |
6938 } | |
6939 } | |
6940 } | |
6941 /* Look for operator overloading of a[i..j]=value. | |
6942 * Do it before semantic() otherwise the a[i..j] will have been | |
6943 * converted to a.opSlice() already. | |
6944 */ | |
6945 if (e1->op == TOKslice) | |
6946 { Type *t1; | |
6947 SliceExp *ae = (SliceExp *)e1; | |
6948 AggregateDeclaration *ad; | |
6949 Identifier *id = Id::index; | |
6950 | |
6951 ae->e1 = ae->e1->semantic(sc); | |
6952 ae->e1 = resolveProperties(sc, ae->e1); | |
6953 t1 = ae->e1->type->toBasetype(); | |
6954 if (t1->ty == Tstruct) | |
6955 { | |
6956 ad = ((TypeStruct *)t1)->sym; | |
6957 goto L2; | |
6958 } | |
6959 else if (t1->ty == Tclass) | |
6960 { | |
6961 ad = ((TypeClass *)t1)->sym; | |
6962 L2: | |
6963 // Rewrite (a[i..j] = value) to (a.opIndexAssign(value, i, j)) | |
6964 if (search_function(ad, Id::sliceass)) | |
6965 { Expression *e = new DotIdExp(loc, ae->e1, Id::sliceass); | |
6966 Expressions *a = new Expressions(); | |
6967 | |
6968 a->push(e2); | |
6969 if (ae->lwr) | |
6970 { a->push(ae->lwr); | |
6971 assert(ae->upr); | |
6972 a->push(ae->upr); | |
6973 } | |
6974 else | |
6975 assert(!ae->upr); | |
6976 e = new CallExp(loc, e, a); | |
6977 e = e->semantic(sc); | |
6978 return e; | |
6979 } | |
6980 } | |
6981 } | |
6982 | |
6983 BinExp::semantic(sc); | |
6984 e2 = resolveProperties(sc, e2); | |
6985 assert(e1->type); | |
6986 | |
6987 t1 = e1->type->toBasetype(); | |
6988 | |
6989 if (t1->ty == Tfunction) | |
6990 { // Rewrite f=value to f(value) | |
6991 Expression *e; | |
6992 | |
6993 e = new CallExp(loc, e1, e2); | |
6994 e = e->semantic(sc); | |
6995 return e; | |
6996 } | |
6997 | |
6998 /* If it is an assignment from a 'foreign' type, | |
6999 * check for operator overloading. | |
7000 */ | |
7001 if (t1->ty == Tclass || t1->ty == Tstruct) | |
7002 { | |
7003 if (!e2->type->implicitConvTo(e1->type)) | |
7004 { | |
7005 Expression *e = op_overload(sc); | |
7006 if (e) | |
7007 return e; | |
7008 } | |
7009 } | |
7010 | |
7011 e2->rvalue(); | |
7012 | |
7013 if (e1->op == TOKarraylength) | |
7014 { | |
7015 // e1 is not an lvalue, but we let code generator handle it | |
7016 ArrayLengthExp *ale = (ArrayLengthExp *)e1; | |
7017 | |
7018 ale->e1 = ale->e1->modifiableLvalue(sc, NULL); | |
7019 } | |
7020 else if (e1->op == TOKslice) | |
7021 ; | |
7022 else | |
7023 { // Try to do a decent error message with the expression | |
7024 // before it got constant folded | |
7025 e1 = e1->modifiableLvalue(sc, e1old); | |
7026 } | |
7027 | |
7028 if (e1->op == TOKslice && | |
7029 t1->next && | |
7030 !(t1->next->equals(e2->type->next) /*|| | |
7031 (t1->next->ty == Tchar && e2->op == TOKstring)*/) | |
7032 ) | |
7033 { // memset | |
7034 e2 = e2->implicitCastTo(sc, t1->next); | |
7035 } | |
7036 #if 0 | |
7037 else if (e1->op == TOKslice && | |
7038 e2->op == TOKstring && | |
7039 ((StringExp *)e2)->len == 1) | |
7040 { // memset | |
7041 e2 = e2->implicitCastTo(sc, e1->type->next); | |
7042 } | |
7043 #endif | |
7044 else if (t1->ty == Tsarray) | |
7045 { | |
7046 error("cannot assign to static array %s", e1->toChars()); | |
7047 } | |
7048 else | |
7049 { | |
7050 e2 = e2->implicitCastTo(sc, e1->type); | |
7051 } | |
7052 type = e1->type; | |
7053 assert(type); | |
7054 return this; | |
7055 } | |
7056 | |
7057 Expression *AssignExp::checkToBoolean() | |
7058 { | |
7059 // Things like: | |
7060 // if (a = b) ... | |
7061 // are usually mistakes. | |
7062 | |
7063 error("'=' does not give a boolean result"); | |
7064 return this; | |
7065 } | |
7066 | |
7067 /************************************************************/ | |
7068 | |
7069 AddAssignExp::AddAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7070 : BinExp(loc, TOKaddass, sizeof(AddAssignExp), e1, e2) | |
7071 { | |
7072 } | |
7073 | |
7074 Expression *AddAssignExp::semantic(Scope *sc) | |
7075 { Expression *e; | |
7076 | |
7077 if (type) | |
7078 return this; | |
7079 | |
7080 BinExp::semantic(sc); | |
7081 e2 = resolveProperties(sc, e2); | |
7082 | |
7083 e = op_overload(sc); | |
7084 if (e) | |
7085 return e; | |
7086 | |
7087 e1 = e1->modifiableLvalue(sc, NULL); | |
7088 | |
7089 Type *tb1 = e1->type->toBasetype(); | |
7090 Type *tb2 = e2->type->toBasetype(); | |
7091 | |
7092 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && | |
7093 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
7094 tb1->next->equals(tb2->next) | |
7095 ) | |
7096 { | |
7097 type = e1->type; | |
7098 e = this; | |
7099 } | |
7100 else | |
7101 { | |
7102 e1->checkScalar(); | |
7103 e1->checkNoBool(); | |
7104 if (tb1->ty == Tpointer && tb2->isintegral()) | |
7105 e = scaleFactor(sc); | |
7106 else if (tb1->ty == Tbit || tb1->ty == Tbool) | |
7107 { | |
7108 #if 0 | |
7109 // Need to rethink this | |
7110 if (e1->op != TOKvar) | |
7111 { // Rewrite e1+=e2 to (v=&e1),*v=*v+e2 | |
7112 VarDeclaration *v; | |
7113 Expression *ea; | |
7114 Expression *ex; | |
7115 | |
7116 char name[6+6+1]; | |
7117 Identifier *id; | |
7118 static int idn; | |
7119 sprintf(name, "__name%d", ++idn); | |
7120 id = Lexer::idPool(name); | |
7121 | |
7122 v = new VarDeclaration(loc, tb1->pointerTo(), id, NULL); | |
7123 v->semantic(sc); | |
7124 if (!sc->insert(v)) | |
7125 assert(0); | |
7126 v->parent = sc->func; | |
7127 | |
7128 ea = new AddrExp(loc, e1); | |
7129 ea = new AssignExp(loc, new VarExp(loc, v), ea); | |
7130 | |
7131 ex = new VarExp(loc, v); | |
7132 ex = new PtrExp(loc, ex); | |
7133 e = new AddExp(loc, ex, e2); | |
7134 e = new CastExp(loc, e, e1->type); | |
7135 e = new AssignExp(loc, ex->syntaxCopy(), e); | |
7136 | |
7137 e = new CommaExp(loc, ea, e); | |
7138 } | |
7139 else | |
7140 #endif | |
7141 { // Rewrite e1+=e2 to e1=e1+e2 | |
7142 // BUG: doesn't account for side effects in e1 | |
7143 // BUG: other assignment operators for bits aren't handled at all | |
7144 e = new AddExp(loc, e1, e2); | |
7145 e = new CastExp(loc, e, e1->type); | |
7146 e = new AssignExp(loc, e1->syntaxCopy(), e); | |
7147 } | |
7148 e = e->semantic(sc); | |
7149 } | |
7150 else | |
7151 { | |
7152 type = e1->type; | |
7153 typeCombine(sc); | |
7154 e1->checkArithmetic(); | |
7155 e2->checkArithmetic(); | |
7156 if (type->isreal() || type->isimaginary()) | |
7157 { | |
7158 assert(global.errors || e2->type->isfloating()); | |
7159 e2 = e2->castTo(sc, e1->type); | |
7160 } | |
7161 e = this; | |
7162 } | |
7163 } | |
7164 return e; | |
7165 } | |
7166 | |
7167 /************************************************************/ | |
7168 | |
7169 MinAssignExp::MinAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7170 : BinExp(loc, TOKminass, sizeof(MinAssignExp), e1, e2) | |
7171 { | |
7172 } | |
7173 | |
7174 Expression *MinAssignExp::semantic(Scope *sc) | |
7175 { Expression *e; | |
7176 | |
7177 if (type) | |
7178 return this; | |
7179 | |
7180 BinExp::semantic(sc); | |
7181 e2 = resolveProperties(sc, e2); | |
7182 | |
7183 e = op_overload(sc); | |
7184 if (e) | |
7185 return e; | |
7186 | |
7187 e1 = e1->modifiableLvalue(sc, NULL); | |
7188 e1->checkScalar(); | |
7189 e1->checkNoBool(); | |
7190 if (e1->type->ty == Tpointer && e2->type->isintegral()) | |
7191 e = scaleFactor(sc); | |
7192 else | |
7193 { | |
7194 type = e1->type; | |
7195 typeCombine(sc); | |
7196 e1->checkArithmetic(); | |
7197 e2->checkArithmetic(); | |
7198 if (type->isreal() || type->isimaginary()) | |
7199 { | |
7200 assert(e2->type->isfloating()); | |
7201 e2 = e2->castTo(sc, e1->type); | |
7202 } | |
7203 e = this; | |
7204 } | |
7205 return e; | |
7206 } | |
7207 | |
7208 /************************************************************/ | |
7209 | |
7210 CatAssignExp::CatAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7211 : BinExp(loc, TOKcatass, sizeof(CatAssignExp), e1, e2) | |
7212 { | |
7213 } | |
7214 | |
7215 Expression *CatAssignExp::semantic(Scope *sc) | |
7216 { Expression *e; | |
7217 | |
7218 BinExp::semantic(sc); | |
7219 e2 = resolveProperties(sc, e2); | |
7220 | |
7221 e = op_overload(sc); | |
7222 if (e) | |
7223 return e; | |
7224 | |
7225 if (e1->op == TOKslice) | |
7226 { SliceExp *se = (SliceExp *)e1; | |
7227 | |
7228 if (se->e1->type->toBasetype()->ty == Tsarray) | |
7229 error("cannot append to static array %s", se->e1->type->toChars()); | |
7230 } | |
7231 | |
7232 e1 = e1->modifiableLvalue(sc, NULL); | |
7233 | |
7234 Type *tb1 = e1->type->toBasetype(); | |
7235 Type *tb2 = e2->type->toBasetype(); | |
7236 | |
7237 if ((tb1->ty == Tarray) && | |
7238 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
7239 e2->implicitConvTo(e1->type) | |
7240 //e1->type->next->equals(e2->type->next) | |
7241 ) | |
7242 { // Append array | |
7243 e2 = e2->castTo(sc, e1->type); | |
7244 type = e1->type; | |
7245 e = this; | |
7246 } | |
7247 else if ((tb1->ty == Tarray) && | |
7248 e2->implicitConvTo(tb1->next) | |
7249 ) | |
7250 { // Append element | |
7251 e2 = e2->castTo(sc, tb1->next); | |
7252 type = e1->type; | |
7253 e = this; | |
7254 } | |
7255 else | |
7256 { | |
7257 error("cannot append type %s to type %s", tb2->toChars(), tb1->toChars()); | |
7258 type = Type::tint32; | |
7259 e = this; | |
7260 } | |
7261 return e; | |
7262 } | |
7263 | |
7264 /************************************************************/ | |
7265 | |
7266 MulAssignExp::MulAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7267 : BinExp(loc, TOKmulass, sizeof(MulAssignExp), e1, e2) | |
7268 { | |
7269 } | |
7270 | |
7271 Expression *MulAssignExp::semantic(Scope *sc) | |
7272 { Expression *e; | |
7273 | |
7274 BinExp::semantic(sc); | |
7275 e2 = resolveProperties(sc, e2); | |
7276 | |
7277 e = op_overload(sc); | |
7278 if (e) | |
7279 return e; | |
7280 | |
7281 e1 = e1->modifiableLvalue(sc, NULL); | |
7282 e1->checkScalar(); | |
7283 e1->checkNoBool(); | |
7284 type = e1->type; | |
7285 typeCombine(sc); | |
7286 e1->checkArithmetic(); | |
7287 e2->checkArithmetic(); | |
7288 if (e2->type->isfloating()) | |
7289 { Type *t1; | |
7290 Type *t2; | |
7291 | |
7292 t1 = e1->type; | |
7293 t2 = e2->type; | |
7294 if (t1->isreal()) | |
7295 { | |
7296 if (t2->isimaginary() || t2->iscomplex()) | |
7297 { | |
7298 e2 = e2->castTo(sc, t1); | |
7299 } | |
7300 } | |
7301 else if (t1->isimaginary()) | |
7302 { | |
7303 if (t2->isimaginary() || t2->iscomplex()) | |
7304 { | |
7305 switch (t1->ty) | |
7306 { | |
7307 case Timaginary32: t2 = Type::tfloat32; break; | |
7308 case Timaginary64: t2 = Type::tfloat64; break; | |
7309 case Timaginary80: t2 = Type::tfloat80; break; | |
7310 default: | |
7311 assert(0); | |
7312 } | |
7313 e2 = e2->castTo(sc, t2); | |
7314 } | |
7315 } | |
7316 } | |
7317 return this; | |
7318 } | |
7319 | |
7320 /************************************************************/ | |
7321 | |
7322 DivAssignExp::DivAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7323 : BinExp(loc, TOKdivass, sizeof(DivAssignExp), e1, e2) | |
7324 { | |
7325 } | |
7326 | |
7327 Expression *DivAssignExp::semantic(Scope *sc) | |
7328 { Expression *e; | |
7329 | |
7330 BinExp::semantic(sc); | |
7331 e2 = resolveProperties(sc, e2); | |
7332 | |
7333 e = op_overload(sc); | |
7334 if (e) | |
7335 return e; | |
7336 | |
7337 e1 = e1->modifiableLvalue(sc, NULL); | |
7338 e1->checkScalar(); | |
7339 e1->checkNoBool(); | |
7340 type = e1->type; | |
7341 typeCombine(sc); | |
7342 e1->checkArithmetic(); | |
7343 e2->checkArithmetic(); | |
7344 if (e2->type->isimaginary()) | |
7345 { Type *t1; | |
7346 Type *t2; | |
7347 | |
7348 t1 = e1->type; | |
7349 if (t1->isreal()) | |
7350 { // x/iv = i(-x/v) | |
7351 // Therefore, the result is 0 | |
7352 e2 = new CommaExp(loc, e2, new RealExp(loc, 0, t1)); | |
7353 e2->type = t1; | |
7354 e = new AssignExp(loc, e1, e2); | |
7355 e->type = t1; | |
7356 return e; | |
7357 } | |
7358 else if (t1->isimaginary()) | |
7359 { Expression *e; | |
7360 | |
7361 switch (t1->ty) | |
7362 { | |
7363 case Timaginary32: t2 = Type::tfloat32; break; | |
7364 case Timaginary64: t2 = Type::tfloat64; break; | |
7365 case Timaginary80: t2 = Type::tfloat80; break; | |
7366 default: | |
7367 assert(0); | |
7368 } | |
7369 e2 = e2->castTo(sc, t2); | |
7370 e = new AssignExp(loc, e1, e2); | |
7371 e->type = t1; | |
7372 return e; | |
7373 } | |
7374 } | |
7375 return this; | |
7376 } | |
7377 | |
7378 /************************************************************/ | |
7379 | |
7380 ModAssignExp::ModAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7381 : BinExp(loc, TOKmodass, sizeof(ModAssignExp), e1, e2) | |
7382 { | |
7383 } | |
7384 | |
7385 Expression *ModAssignExp::semantic(Scope *sc) | |
7386 { | |
7387 return commonSemanticAssign(sc); | |
7388 } | |
7389 | |
7390 /************************************************************/ | |
7391 | |
7392 ShlAssignExp::ShlAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7393 : BinExp(loc, TOKshlass, sizeof(ShlAssignExp), e1, e2) | |
7394 { | |
7395 } | |
7396 | |
7397 Expression *ShlAssignExp::semantic(Scope *sc) | |
7398 { Expression *e; | |
7399 | |
7400 //printf("ShlAssignExp::semantic()\n"); | |
7401 BinExp::semantic(sc); | |
7402 e2 = resolveProperties(sc, e2); | |
7403 | |
7404 e = op_overload(sc); | |
7405 if (e) | |
7406 return e; | |
7407 | |
7408 e1 = e1->modifiableLvalue(sc, NULL); | |
7409 e1->checkScalar(); | |
7410 e1->checkNoBool(); | |
7411 type = e1->type; | |
7412 typeCombine(sc); | |
7413 e1->checkIntegral(); | |
7414 e2 = e2->checkIntegral(); | |
7415 e2 = e2->castTo(sc, Type::tshiftcnt); | |
7416 return this; | |
7417 } | |
7418 | |
7419 /************************************************************/ | |
7420 | |
7421 ShrAssignExp::ShrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7422 : BinExp(loc, TOKshrass, sizeof(ShrAssignExp), e1, e2) | |
7423 { | |
7424 } | |
7425 | |
7426 Expression *ShrAssignExp::semantic(Scope *sc) | |
7427 { Expression *e; | |
7428 | |
7429 BinExp::semantic(sc); | |
7430 e2 = resolveProperties(sc, e2); | |
7431 | |
7432 e = op_overload(sc); | |
7433 if (e) | |
7434 return e; | |
7435 | |
7436 e1 = e1->modifiableLvalue(sc, NULL); | |
7437 e1->checkScalar(); | |
7438 e1->checkNoBool(); | |
7439 type = e1->type; | |
7440 typeCombine(sc); | |
7441 e1->checkIntegral(); | |
7442 e2 = e2->checkIntegral(); | |
7443 e2 = e2->castTo(sc, Type::tshiftcnt); | |
7444 return this; | |
7445 } | |
7446 | |
7447 /************************************************************/ | |
7448 | |
7449 UshrAssignExp::UshrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7450 : BinExp(loc, TOKushrass, sizeof(UshrAssignExp), e1, e2) | |
7451 { | |
7452 } | |
7453 | |
7454 Expression *UshrAssignExp::semantic(Scope *sc) | |
7455 { Expression *e; | |
7456 | |
7457 BinExp::semantic(sc); | |
7458 e2 = resolveProperties(sc, e2); | |
7459 | |
7460 e = op_overload(sc); | |
7461 if (e) | |
7462 return e; | |
7463 | |
7464 e1 = e1->modifiableLvalue(sc, NULL); | |
7465 e1->checkScalar(); | |
7466 e1->checkNoBool(); | |
7467 type = e1->type; | |
7468 typeCombine(sc); | |
7469 e1->checkIntegral(); | |
7470 e2 = e2->checkIntegral(); | |
7471 e2 = e2->castTo(sc, Type::tshiftcnt); | |
7472 return this; | |
7473 } | |
7474 | |
7475 /************************************************************/ | |
7476 | |
7477 AndAssignExp::AndAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7478 : BinExp(loc, TOKandass, sizeof(AndAssignExp), e1, e2) | |
7479 { | |
7480 } | |
7481 | |
7482 Expression *AndAssignExp::semantic(Scope *sc) | |
7483 { | |
7484 return commonSemanticAssignIntegral(sc); | |
7485 } | |
7486 | |
7487 /************************************************************/ | |
7488 | |
7489 OrAssignExp::OrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7490 : BinExp(loc, TOKorass, sizeof(OrAssignExp), e1, e2) | |
7491 { | |
7492 } | |
7493 | |
7494 Expression *OrAssignExp::semantic(Scope *sc) | |
7495 { | |
7496 return commonSemanticAssignIntegral(sc); | |
7497 } | |
7498 | |
7499 /************************************************************/ | |
7500 | |
7501 XorAssignExp::XorAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7502 : BinExp(loc, TOKxorass, sizeof(XorAssignExp), e1, e2) | |
7503 { | |
7504 } | |
7505 | |
7506 Expression *XorAssignExp::semantic(Scope *sc) | |
7507 { | |
7508 return commonSemanticAssignIntegral(sc); | |
7509 } | |
7510 | |
7511 /************************* AddExp *****************************/ | |
7512 | |
7513 AddExp::AddExp(Loc loc, Expression *e1, Expression *e2) | |
7514 : BinExp(loc, TOKadd, sizeof(AddExp), e1, e2) | |
7515 { | |
7516 } | |
7517 | |
7518 Expression *AddExp::semantic(Scope *sc) | |
7519 { Expression *e; | |
7520 | |
7521 #if LOGSEMANTIC | |
7522 printf("AddExp::semantic('%s')\n", toChars()); | |
7523 #endif | |
7524 if (!type) | |
7525 { | |
7526 BinExp::semanticp(sc); | |
7527 | |
7528 e = op_overload(sc); | |
7529 if (e) | |
7530 return e; | |
7531 | |
7532 Type *tb1 = e1->type->toBasetype(); | |
7533 Type *tb2 = e2->type->toBasetype(); | |
7534 | |
7535 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && | |
7536 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
7537 tb1->next->equals(tb2->next) | |
7538 ) | |
7539 { | |
7540 type = e1->type; | |
7541 e = this; | |
7542 } | |
7543 else if (tb1->ty == Tpointer && e2->type->isintegral() || | |
7544 tb2->ty == Tpointer && e1->type->isintegral()) | |
7545 e = scaleFactor(sc); | |
7546 else if (tb1->ty == Tpointer && tb2->ty == Tpointer) | |
7547 { | |
7548 incompatibleTypes(); | |
7549 type = e1->type; | |
7550 e = this; | |
7551 } | |
7552 else | |
7553 { | |
7554 typeCombine(sc); | |
7555 if ((e1->type->isreal() && e2->type->isimaginary()) || | |
7556 (e1->type->isimaginary() && e2->type->isreal())) | |
7557 { | |
7558 switch (type->toBasetype()->ty) | |
7559 { | |
7560 case Tfloat32: | |
7561 case Timaginary32: | |
7562 type = Type::tcomplex32; | |
7563 break; | |
7564 | |
7565 case Tfloat64: | |
7566 case Timaginary64: | |
7567 type = Type::tcomplex64; | |
7568 break; | |
7569 | |
7570 case Tfloat80: | |
7571 case Timaginary80: | |
7572 type = Type::tcomplex80; | |
7573 break; | |
7574 | |
7575 default: | |
7576 assert(0); | |
7577 } | |
7578 } | |
7579 e = this; | |
7580 } | |
7581 return e; | |
7582 } | |
7583 return this; | |
7584 } | |
7585 | |
7586 /************************************************************/ | |
7587 | |
7588 MinExp::MinExp(Loc loc, Expression *e1, Expression *e2) | |
7589 : BinExp(loc, TOKmin, sizeof(MinExp), e1, e2) | |
7590 { | |
7591 } | |
7592 | |
7593 Expression *MinExp::semantic(Scope *sc) | |
7594 { Expression *e; | |
7595 Type *t1; | |
7596 Type *t2; | |
7597 | |
7598 #if LOGSEMANTIC | |
7599 printf("MinExp::semantic('%s')\n", toChars()); | |
7600 #endif | |
7601 if (type) | |
7602 return this; | |
7603 | |
7604 BinExp::semanticp(sc); | |
7605 | |
7606 e = op_overload(sc); | |
7607 if (e) | |
7608 return e; | |
7609 | |
7610 e = this; | |
7611 t1 = e1->type->toBasetype(); | |
7612 t2 = e2->type->toBasetype(); | |
7613 if (t1->ty == Tpointer) | |
7614 { | |
7615 if (t2->ty == Tpointer) | |
7616 { // Need to divide the result by the stride | |
7617 // Replace (ptr - ptr) with (ptr - ptr) / stride | |
7618 d_int64 stride; | |
7619 Expression *e; | |
7620 | |
7621 typeCombine(sc); // make sure pointer types are compatible | |
7622 type = Type::tptrdiff_t; | |
7623 stride = t2->next->size(); | |
7624 e = new DivExp(loc, this, new IntegerExp(0, stride, Type::tptrdiff_t)); | |
7625 e->type = Type::tptrdiff_t; | |
7626 return e; | |
7627 } | |
7628 else if (t2->isintegral()) | |
7629 e = scaleFactor(sc); | |
7630 else | |
7631 { error("incompatible types for -"); | |
7632 return new IntegerExp(0); | |
7633 } | |
7634 } | |
7635 else if (t2->ty == Tpointer) | |
7636 { | |
7637 type = e2->type; | |
7638 error("can't subtract pointer from %s", e1->type->toChars()); | |
7639 return new IntegerExp(0); | |
7640 } | |
7641 else | |
7642 { | |
7643 typeCombine(sc); | |
7644 t1 = e1->type->toBasetype(); | |
7645 t2 = e2->type->toBasetype(); | |
7646 if ((t1->isreal() && t2->isimaginary()) || | |
7647 (t1->isimaginary() && t2->isreal())) | |
7648 { | |
7649 switch (type->ty) | |
7650 { | |
7651 case Tfloat32: | |
7652 case Timaginary32: | |
7653 type = Type::tcomplex32; | |
7654 break; | |
7655 | |
7656 case Tfloat64: | |
7657 case Timaginary64: | |
7658 type = Type::tcomplex64; | |
7659 break; | |
7660 | |
7661 case Tfloat80: | |
7662 case Timaginary80: | |
7663 type = Type::tcomplex80; | |
7664 break; | |
7665 | |
7666 default: | |
7667 assert(0); | |
7668 } | |
7669 } | |
7670 } | |
7671 return e; | |
7672 } | |
7673 | |
7674 /************************* CatExp *****************************/ | |
7675 | |
7676 CatExp::CatExp(Loc loc, Expression *e1, Expression *e2) | |
7677 : BinExp(loc, TOKcat, sizeof(CatExp), e1, e2) | |
7678 { | |
7679 } | |
7680 | |
7681 Expression *CatExp::semantic(Scope *sc) | |
7682 { Expression *e; | |
7683 | |
7684 //printf("CatExp::semantic() %s\n", toChars()); | |
7685 if (!type) | |
7686 { | |
7687 BinExp::semanticp(sc); | |
7688 e = op_overload(sc); | |
7689 if (e) | |
7690 return e; | |
7691 | |
7692 Type *tb1 = e1->type->toBasetype(); | |
7693 Type *tb2 = e2->type->toBasetype(); | |
7694 | |
7695 | |
7696 /* BUG: Should handle things like: | |
7697 * char c; | |
7698 * c ~ ' ' | |
7699 * ' ' ~ c; | |
7700 */ | |
7701 | |
7702 #if 0 | |
7703 e1->type->print(); | |
7704 e2->type->print(); | |
7705 #endif | |
7706 if ((tb1->ty == Tsarray || tb1->ty == Tarray) && | |
7707 e2->type->equals(tb1->next)) | |
7708 { | |
7709 type = tb1->next->arrayOf(); | |
7710 if (tb2->ty == Tarray) | |
7711 { // Make e2 into [e2] | |
7712 e2 = new ArrayLiteralExp(e2->loc, e2); | |
7713 e2->type = type; | |
7714 } | |
7715 return this; | |
7716 } | |
7717 else if ((tb2->ty == Tsarray || tb2->ty == Tarray) && | |
7718 e1->type->equals(tb2->next)) | |
7719 { | |
7720 type = tb2->next->arrayOf(); | |
7721 if (tb1->ty == Tarray) | |
7722 { // Make e1 into [e1] | |
7723 e1 = new ArrayLiteralExp(e1->loc, e1); | |
7724 e1->type = type; | |
7725 } | |
7726 return this; | |
7727 } | |
7728 | |
7729 typeCombine(sc); | |
7730 | |
7731 if (type->toBasetype()->ty == Tsarray) | |
7732 type = type->toBasetype()->next->arrayOf(); | |
7733 #if 0 | |
7734 e1->type->print(); | |
7735 e2->type->print(); | |
7736 type->print(); | |
7737 print(); | |
7738 #endif | |
7739 if (e1->op == TOKstring && e2->op == TOKstring) | |
7740 e = optimize(WANTvalue); | |
7741 else if (e1->type->equals(e2->type) && | |
7742 (e1->type->toBasetype()->ty == Tarray || | |
7743 e1->type->toBasetype()->ty == Tsarray)) | |
7744 { | |
7745 e = this; | |
7746 } | |
7747 else | |
7748 { | |
7749 error("Can only concatenate arrays, not (%s ~ %s)", | |
7750 e1->type->toChars(), e2->type->toChars()); | |
7751 type = Type::tint32; | |
7752 e = this; | |
7753 } | |
7754 e->type = e->type->semantic(loc, sc); | |
7755 return e; | |
7756 } | |
7757 return this; | |
7758 } | |
7759 | |
7760 /************************************************************/ | |
7761 | |
7762 MulExp::MulExp(Loc loc, Expression *e1, Expression *e2) | |
7763 : BinExp(loc, TOKmul, sizeof(MulExp), e1, e2) | |
7764 { | |
7765 } | |
7766 | |
7767 Expression *MulExp::semantic(Scope *sc) | |
7768 { Expression *e; | |
7769 | |
7770 #if 0 | |
7771 printf("MulExp::semantic() %s\n", toChars()); | |
7772 #endif | |
7773 if (type) | |
7774 { | |
7775 return this; | |
7776 } | |
7777 | |
7778 BinExp::semanticp(sc); | |
7779 e = op_overload(sc); | |
7780 if (e) | |
7781 return e; | |
7782 | |
7783 typeCombine(sc); | |
7784 e1->checkArithmetic(); | |
7785 e2->checkArithmetic(); | |
7786 if (type->isfloating()) | |
7787 { Type *t1 = e1->type; | |
7788 Type *t2 = e2->type; | |
7789 | |
7790 if (t1->isreal()) | |
7791 { | |
7792 type = t2; | |
7793 } | |
7794 else if (t2->isreal()) | |
7795 { | |
7796 type = t1; | |
7797 } | |
7798 else if (t1->isimaginary()) | |
7799 { | |
7800 if (t2->isimaginary()) | |
7801 { Expression *e; | |
7802 | |
7803 switch (t1->ty) | |
7804 { | |
7805 case Timaginary32: type = Type::tfloat32; break; | |
7806 case Timaginary64: type = Type::tfloat64; break; | |
7807 case Timaginary80: type = Type::tfloat80; break; | |
7808 default: assert(0); | |
7809 } | |
7810 | |
7811 // iy * iv = -yv | |
7812 e1->type = type; | |
7813 e2->type = type; | |
7814 e = new NegExp(loc, this); | |
7815 e = e->semantic(sc); | |
7816 return e; | |
7817 } | |
7818 else | |
7819 type = t2; // t2 is complex | |
7820 } | |
7821 else if (t2->isimaginary()) | |
7822 { | |
7823 type = t1; // t1 is complex | |
7824 } | |
7825 } | |
7826 return this; | |
7827 } | |
7828 | |
7829 /************************************************************/ | |
7830 | |
7831 DivExp::DivExp(Loc loc, Expression *e1, Expression *e2) | |
7832 : BinExp(loc, TOKdiv, sizeof(DivExp), e1, e2) | |
7833 { | |
7834 } | |
7835 | |
7836 Expression *DivExp::semantic(Scope *sc) | |
7837 { Expression *e; | |
7838 | |
7839 if (type) | |
7840 return this; | |
7841 | |
7842 BinExp::semanticp(sc); | |
7843 e = op_overload(sc); | |
7844 if (e) | |
7845 return e; | |
7846 | |
7847 typeCombine(sc); | |
7848 e1->checkArithmetic(); | |
7849 e2->checkArithmetic(); | |
7850 if (type->isfloating()) | |
7851 { Type *t1 = e1->type; | |
7852 Type *t2 = e2->type; | |
7853 | |
7854 if (t1->isreal()) | |
7855 { | |
7856 type = t2; | |
7857 if (t2->isimaginary()) | |
7858 { Expression *e; | |
7859 | |
7860 // x/iv = i(-x/v) | |
7861 e2->type = t1; | |
7862 e = new NegExp(loc, this); | |
7863 e = e->semantic(sc); | |
7864 return e; | |
7865 } | |
7866 } | |
7867 else if (t2->isreal()) | |
7868 { | |
7869 type = t1; | |
7870 } | |
7871 else if (t1->isimaginary()) | |
7872 { | |
7873 if (t2->isimaginary()) | |
7874 { | |
7875 switch (t1->ty) | |
7876 { | |
7877 case Timaginary32: type = Type::tfloat32; break; | |
7878 case Timaginary64: type = Type::tfloat64; break; | |
7879 case Timaginary80: type = Type::tfloat80; break; | |
7880 default: assert(0); | |
7881 } | |
7882 } | |
7883 else | |
7884 type = t2; // t2 is complex | |
7885 } | |
7886 else if (t2->isimaginary()) | |
7887 { | |
7888 type = t1; // t1 is complex | |
7889 } | |
7890 } | |
7891 return this; | |
7892 } | |
7893 | |
7894 /************************************************************/ | |
7895 | |
7896 ModExp::ModExp(Loc loc, Expression *e1, Expression *e2) | |
7897 : BinExp(loc, TOKmod, sizeof(ModExp), e1, e2) | |
7898 { | |
7899 } | |
7900 | |
7901 Expression *ModExp::semantic(Scope *sc) | |
7902 { Expression *e; | |
7903 | |
7904 if (type) | |
7905 return this; | |
7906 | |
7907 BinExp::semanticp(sc); | |
7908 e = op_overload(sc); | |
7909 if (e) | |
7910 return e; | |
7911 | |
7912 typeCombine(sc); | |
7913 e1->checkArithmetic(); | |
7914 e2->checkArithmetic(); | |
7915 if (type->isfloating()) | |
7916 { type = e1->type; | |
7917 if (e2->type->iscomplex()) | |
7918 { error("cannot perform modulo complex arithmetic"); | |
7919 return new IntegerExp(0); | |
7920 } | |
7921 } | |
7922 return this; | |
7923 } | |
7924 | |
7925 /************************************************************/ | |
7926 | |
7927 ShlExp::ShlExp(Loc loc, Expression *e1, Expression *e2) | |
7928 : BinExp(loc, TOKshl, sizeof(ShlExp), e1, e2) | |
7929 { | |
7930 } | |
7931 | |
7932 Expression *ShlExp::semantic(Scope *sc) | |
7933 { Expression *e; | |
7934 | |
7935 //printf("ShlExp::semantic(), type = %p\n", type); | |
7936 if (!type) | |
7937 { BinExp::semanticp(sc); | |
7938 e = op_overload(sc); | |
7939 if (e) | |
7940 return e; | |
7941 e1 = e1->checkIntegral(); | |
7942 e2 = e2->checkIntegral(); | |
7943 e1 = e1->integralPromotions(sc); | |
7944 e2 = e2->castTo(sc, Type::tshiftcnt); | |
7945 type = e1->type; | |
7946 } | |
7947 return this; | |
7948 } | |
7949 | |
7950 /************************************************************/ | |
7951 | |
7952 ShrExp::ShrExp(Loc loc, Expression *e1, Expression *e2) | |
7953 : BinExp(loc, TOKshr, sizeof(ShrExp), e1, e2) | |
7954 { | |
7955 } | |
7956 | |
7957 Expression *ShrExp::semantic(Scope *sc) | |
7958 { Expression *e; | |
7959 | |
7960 if (!type) | |
7961 { BinExp::semanticp(sc); | |
7962 e = op_overload(sc); | |
7963 if (e) | |
7964 return e; | |
7965 e1 = e1->checkIntegral(); | |
7966 e2 = e2->checkIntegral(); | |
7967 e1 = e1->integralPromotions(sc); | |
7968 e2 = e2->castTo(sc, Type::tshiftcnt); | |
7969 type = e1->type; | |
7970 } | |
7971 return this; | |
7972 } | |
7973 | |
7974 /************************************************************/ | |
7975 | |
7976 UshrExp::UshrExp(Loc loc, Expression *e1, Expression *e2) | |
7977 : BinExp(loc, TOKushr, sizeof(UshrExp), e1, e2) | |
7978 { | |
7979 } | |
7980 | |
7981 Expression *UshrExp::semantic(Scope *sc) | |
7982 { Expression *e; | |
7983 | |
7984 if (!type) | |
7985 { BinExp::semanticp(sc); | |
7986 e = op_overload(sc); | |
7987 if (e) | |
7988 return e; | |
7989 e1 = e1->checkIntegral(); | |
7990 e2 = e2->checkIntegral(); | |
7991 e1 = e1->integralPromotions(sc); | |
7992 e2 = e2->castTo(sc, Type::tshiftcnt); | |
7993 type = e1->type; | |
7994 } | |
7995 return this; | |
7996 } | |
7997 | |
7998 /************************************************************/ | |
7999 | |
8000 AndExp::AndExp(Loc loc, Expression *e1, Expression *e2) | |
8001 : BinExp(loc, TOKand, sizeof(AndExp), e1, e2) | |
8002 { | |
8003 } | |
8004 | |
8005 Expression *AndExp::semantic(Scope *sc) | |
8006 { Expression *e; | |
8007 | |
8008 if (!type) | |
8009 { BinExp::semanticp(sc); | |
8010 e = op_overload(sc); | |
8011 if (e) | |
8012 return e; | |
8013 if (e1->type->toBasetype()->ty == Tbool && | |
8014 e2->type->toBasetype()->ty == Tbool) | |
8015 { | |
8016 type = e1->type; | |
8017 e = this; | |
8018 } | |
8019 else | |
8020 { | |
8021 typeCombine(sc); | |
8022 e1->checkIntegral(); | |
8023 e2->checkIntegral(); | |
8024 } | |
8025 } | |
8026 return this; | |
8027 } | |
8028 | |
8029 /************************************************************/ | |
8030 | |
8031 OrExp::OrExp(Loc loc, Expression *e1, Expression *e2) | |
8032 : BinExp(loc, TOKor, sizeof(OrExp), e1, e2) | |
8033 { | |
8034 } | |
8035 | |
8036 Expression *OrExp::semantic(Scope *sc) | |
8037 { Expression *e; | |
8038 | |
8039 if (!type) | |
8040 { BinExp::semanticp(sc); | |
8041 e = op_overload(sc); | |
8042 if (e) | |
8043 return e; | |
8044 if (e1->type->toBasetype()->ty == Tbool && | |
8045 e2->type->toBasetype()->ty == Tbool) | |
8046 { | |
8047 type = e1->type; | |
8048 e = this; | |
8049 } | |
8050 else | |
8051 { | |
8052 typeCombine(sc); | |
8053 e1->checkIntegral(); | |
8054 e2->checkIntegral(); | |
8055 } | |
8056 } | |
8057 return this; | |
8058 } | |
8059 | |
8060 /************************************************************/ | |
8061 | |
8062 XorExp::XorExp(Loc loc, Expression *e1, Expression *e2) | |
8063 : BinExp(loc, TOKxor, sizeof(XorExp), e1, e2) | |
8064 { | |
8065 } | |
8066 | |
8067 Expression *XorExp::semantic(Scope *sc) | |
8068 { Expression *e; | |
8069 | |
8070 if (!type) | |
8071 { BinExp::semanticp(sc); | |
8072 e = op_overload(sc); | |
8073 if (e) | |
8074 return e; | |
8075 if (e1->type->toBasetype()->ty == Tbool && | |
8076 e2->type->toBasetype()->ty == Tbool) | |
8077 { | |
8078 type = e1->type; | |
8079 e = this; | |
8080 } | |
8081 else | |
8082 { | |
8083 typeCombine(sc); | |
8084 e1->checkIntegral(); | |
8085 e2->checkIntegral(); | |
8086 } | |
8087 } | |
8088 return this; | |
8089 } | |
8090 | |
8091 | |
8092 /************************************************************/ | |
8093 | |
8094 OrOrExp::OrOrExp(Loc loc, Expression *e1, Expression *e2) | |
8095 : BinExp(loc, TOKoror, sizeof(OrOrExp), e1, e2) | |
8096 { | |
8097 } | |
8098 | |
8099 Expression *OrOrExp::semantic(Scope *sc) | |
8100 { | |
8101 unsigned cs1; | |
8102 | |
8103 // same as for AndAnd | |
8104 e1 = e1->semantic(sc); | |
8105 e1 = resolveProperties(sc, e1); | |
8106 e1 = e1->checkToPointer(); | |
8107 e1 = e1->checkToBoolean(); | |
8108 cs1 = sc->callSuper; | |
8109 | |
8110 if (sc->flags & SCOPEstaticif) | |
8111 { | |
8112 /* If in static if, don't evaluate e2 if we don't have to. | |
8113 */ | |
8114 e1 = e1->optimize(WANTflags); | |
8115 if (e1->isBool(TRUE)) | |
8116 { | |
8117 return new IntegerExp(loc, 1, Type::tboolean); | |
8118 } | |
8119 } | |
8120 | |
8121 e2 = e2->semantic(sc); | |
8122 sc->mergeCallSuper(loc, cs1); | |
8123 e2 = resolveProperties(sc, e2); | |
8124 e2 = e2->checkToPointer(); | |
8125 | |
8126 type = Type::tboolean; | |
8127 if (e1->type->ty == Tvoid) | |
8128 type = Type::tvoid; | |
8129 if (e2->op == TOKtype || e2->op == TOKimport) | |
8130 error("%s is not an expression", e2->toChars()); | |
8131 return this; | |
8132 } | |
8133 | |
8134 Expression *OrOrExp::checkToBoolean() | |
8135 { | |
8136 e2 = e2->checkToBoolean(); | |
8137 return this; | |
8138 } | |
8139 | |
8140 int OrOrExp::isBit() | |
8141 { | |
8142 return TRUE; | |
8143 } | |
8144 | |
8145 int OrOrExp::checkSideEffect(int flag) | |
8146 { | |
8147 if (flag == 2) | |
8148 { | |
8149 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
8150 } | |
8151 else | |
8152 { e1->checkSideEffect(1); | |
8153 return e2->checkSideEffect(flag); | |
8154 } | |
8155 } | |
8156 | |
8157 /************************************************************/ | |
8158 | |
8159 AndAndExp::AndAndExp(Loc loc, Expression *e1, Expression *e2) | |
8160 : BinExp(loc, TOKandand, sizeof(AndAndExp), e1, e2) | |
8161 { | |
8162 } | |
8163 | |
8164 Expression *AndAndExp::semantic(Scope *sc) | |
8165 { | |
8166 unsigned cs1; | |
8167 | |
8168 // same as for OrOr | |
8169 e1 = e1->semantic(sc); | |
8170 e1 = resolveProperties(sc, e1); | |
8171 e1 = e1->checkToPointer(); | |
8172 e1 = e1->checkToBoolean(); | |
8173 cs1 = sc->callSuper; | |
8174 | |
8175 if (sc->flags & SCOPEstaticif) | |
8176 { | |
8177 /* If in static if, don't evaluate e2 if we don't have to. | |
8178 */ | |
8179 e1 = e1->optimize(WANTflags); | |
8180 if (e1->isBool(FALSE)) | |
8181 { | |
8182 return new IntegerExp(loc, 0, Type::tboolean); | |
8183 } | |
8184 } | |
8185 | |
8186 e2 = e2->semantic(sc); | |
8187 sc->mergeCallSuper(loc, cs1); | |
8188 e2 = resolveProperties(sc, e2); | |
8189 e2 = e2->checkToPointer(); | |
8190 | |
8191 type = Type::tboolean; | |
8192 if (e1->type->ty == Tvoid) | |
8193 type = Type::tvoid; | |
8194 if (e2->op == TOKtype || e2->op == TOKimport) | |
8195 error("%s is not an expression", e2->toChars()); | |
8196 return this; | |
8197 } | |
8198 | |
8199 Expression *AndAndExp::checkToBoolean() | |
8200 { | |
8201 e2 = e2->checkToBoolean(); | |
8202 return this; | |
8203 } | |
8204 | |
8205 int AndAndExp::isBit() | |
8206 { | |
8207 return TRUE; | |
8208 } | |
8209 | |
8210 int AndAndExp::checkSideEffect(int flag) | |
8211 { | |
8212 if (flag == 2) | |
8213 { | |
8214 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
8215 } | |
8216 else | |
8217 { | |
8218 e1->checkSideEffect(1); | |
8219 return e2->checkSideEffect(flag); | |
8220 } | |
8221 } | |
8222 | |
8223 /************************************************************/ | |
8224 | |
8225 InExp::InExp(Loc loc, Expression *e1, Expression *e2) | |
8226 : BinExp(loc, TOKin, sizeof(InExp), e1, e2) | |
8227 { | |
8228 } | |
8229 | |
8230 Expression *InExp::semantic(Scope *sc) | |
8231 { Expression *e; | |
8232 | |
8233 if (type) | |
8234 return this; | |
8235 | |
8236 BinExp::semanticp(sc); | |
8237 e = op_overload(sc); | |
8238 if (e) | |
8239 return e; | |
8240 | |
8241 //type = Type::tboolean; | |
8242 Type *t2b = e2->type->toBasetype(); | |
8243 if (t2b->ty != Taarray) | |
8244 { | |
8245 error("rvalue of in expression must be an associative array, not %s", e2->type->toChars()); | |
8246 type = Type::terror; | |
8247 } | |
8248 else | |
8249 { | |
8250 TypeAArray *ta = (TypeAArray *)t2b; | |
8251 | |
8252 // Convert key to type of key | |
8253 e1 = e1->implicitCastTo(sc, ta->index); | |
8254 | |
8255 // Return type is pointer to value | |
8256 type = ta->next->pointerTo(); | |
8257 } | |
8258 return this; | |
8259 } | |
8260 | |
8261 int InExp::isBit() | |
8262 { | |
8263 return FALSE; | |
8264 } | |
8265 | |
8266 | |
8267 /************************************************************/ | |
8268 | |
8269 /* This deletes the key e1 from the associative array e2 | |
8270 */ | |
8271 | |
8272 RemoveExp::RemoveExp(Loc loc, Expression *e1, Expression *e2) | |
8273 : BinExp(loc, TOKremove, sizeof(RemoveExp), e1, e2) | |
8274 { | |
8275 type = Type::tvoid; | |
8276 } | |
8277 | |
8278 /************************************************************/ | |
8279 | |
8280 CmpExp::CmpExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
8281 : BinExp(loc, op, sizeof(CmpExp), e1, e2) | |
8282 { | |
8283 } | |
8284 | |
8285 Expression *CmpExp::semantic(Scope *sc) | |
8286 { Expression *e; | |
8287 Type *t1; | |
8288 Type *t2; | |
8289 | |
8290 #if LOGSEMANTIC | |
8291 printf("CmpExp::semantic('%s')\n", toChars()); | |
8292 #endif | |
8293 if (type) | |
8294 return this; | |
8295 | |
8296 BinExp::semanticp(sc); | |
8297 e = op_overload(sc); | |
8298 if (e) | |
8299 { | |
8300 e = new CmpExp(op, loc, e, new IntegerExp(loc, 0, Type::tint32)); | |
8301 e = e->semantic(sc); | |
8302 return e; | |
8303 } | |
8304 | |
8305 typeCombine(sc); | |
8306 type = Type::tboolean; | |
8307 | |
8308 // Special handling for array comparisons | |
8309 t1 = e1->type->toBasetype(); | |
8310 t2 = e2->type->toBasetype(); | |
8311 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
8312 (t2->ty == Tarray || t2->ty == Tsarray)) | |
8313 { | |
8314 if (!t1->next->equals(t2->next)) | |
8315 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
8316 e = this; | |
8317 } | |
8318 else if (t1->ty == Tstruct || t2->ty == Tstruct || | |
8319 (t1->ty == Tclass && t2->ty == Tclass)) | |
8320 { | |
8321 if (t2->ty == Tstruct) | |
8322 error("need member function opCmp() for %s %s to compare", t2->toDsymbol(sc)->kind(), t2->toChars()); | |
8323 else | |
8324 error("need member function opCmp() for %s %s to compare", t1->toDsymbol(sc)->kind(), t1->toChars()); | |
8325 e = this; | |
8326 } | |
8327 #if 1 | |
8328 else if (t1->iscomplex() || t2->iscomplex()) | |
8329 { | |
8330 error("compare not defined for complex operands"); | |
8331 e = new IntegerExp(0); | |
8332 } | |
8333 #endif | |
8334 else | |
8335 e = this; | |
8336 return e; | |
8337 } | |
8338 | |
8339 int CmpExp::isBit() | |
8340 { | |
8341 return TRUE; | |
8342 } | |
8343 | |
8344 | |
8345 /************************************************************/ | |
8346 | |
8347 EqualExp::EqualExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
8348 : BinExp(loc, op, sizeof(EqualExp), e1, e2) | |
8349 { | |
8350 } | |
8351 | |
8352 Expression *EqualExp::semantic(Scope *sc) | |
8353 { Expression *e; | |
8354 Type *t1; | |
8355 Type *t2; | |
8356 | |
8357 //printf("EqualExp::semantic('%s')\n", toChars()); | |
8358 if (type) | |
8359 return this; | |
8360 | |
8361 BinExp::semanticp(sc); | |
8362 | |
8363 /* Before checking for operator overloading, check to see if we're | |
8364 * comparing the addresses of two statics. If so, we can just see | |
8365 * if they are the same symbol. | |
8366 */ | |
8367 if (e1->op == TOKaddress && e2->op == TOKaddress) | |
8368 { AddrExp *ae1 = (AddrExp *)e1; | |
8369 AddrExp *ae2 = (AddrExp *)e2; | |
8370 | |
8371 if (ae1->e1->op == TOKvar && ae2->e1->op == TOKvar) | |
8372 { VarExp *ve1 = (VarExp *)ae1->e1; | |
8373 VarExp *ve2 = (VarExp *)ae2->e1; | |
8374 | |
8375 if (ve1->var == ve2->var /*|| ve1->var->toSymbol() == ve2->var->toSymbol()*/) | |
8376 { | |
8377 // They are the same, result is 'true' for ==, 'false' for != | |
8378 e = new IntegerExp(loc, (op == TOKequal), Type::tboolean); | |
8379 return e; | |
8380 } | |
8381 } | |
8382 } | |
8383 | |
8384 //if (e2->op != TOKnull) | |
8385 { | |
8386 e = op_overload(sc); | |
8387 if (e) | |
8388 { | |
8389 if (op == TOKnotequal) | |
8390 { | |
8391 e = new NotExp(e->loc, e); | |
8392 e = e->semantic(sc); | |
8393 } | |
8394 return e; | |
8395 } | |
8396 } | |
8397 | |
8398 e = typeCombine(sc); | |
8399 type = Type::tboolean; | |
8400 | |
8401 // Special handling for array comparisons | |
8402 t1 = e1->type->toBasetype(); | |
8403 t2 = e2->type->toBasetype(); | |
8404 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
8405 (t2->ty == Tarray || t2->ty == Tsarray)) | |
8406 { | |
8407 if (!t1->next->equals(t2->next)) | |
8408 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
8409 } | |
8410 else | |
8411 { | |
8412 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
8413 { | |
8414 // Cast both to complex | |
8415 e1 = e1->castTo(sc, Type::tcomplex80); | |
8416 e2 = e2->castTo(sc, Type::tcomplex80); | |
8417 } | |
8418 } | |
8419 return e; | |
8420 } | |
8421 | |
8422 int EqualExp::isBit() | |
8423 { | |
8424 return TRUE; | |
8425 } | |
8426 | |
8427 | |
8428 | |
8429 /************************************************************/ | |
8430 | |
8431 IdentityExp::IdentityExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
8432 : BinExp(loc, op, sizeof(IdentityExp), e1, e2) | |
8433 { | |
8434 } | |
8435 | |
8436 Expression *IdentityExp::semantic(Scope *sc) | |
8437 { | |
8438 if (type) | |
8439 return this; | |
8440 | |
8441 BinExp::semanticp(sc); | |
8442 type = Type::tboolean; | |
8443 typeCombine(sc); | |
8444 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
8445 { | |
8446 // Cast both to complex | |
8447 e1 = e1->castTo(sc, Type::tcomplex80); | |
8448 e2 = e2->castTo(sc, Type::tcomplex80); | |
8449 } | |
8450 return this; | |
8451 } | |
8452 | |
8453 int IdentityExp::isBit() | |
8454 { | |
8455 return TRUE; | |
8456 } | |
8457 | |
8458 | |
8459 /****************************************************************/ | |
8460 | |
8461 CondExp::CondExp(Loc loc, Expression *econd, Expression *e1, Expression *e2) | |
8462 : BinExp(loc, TOKquestion, sizeof(CondExp), e1, e2) | |
8463 { | |
8464 this->econd = econd; | |
8465 } | |
8466 | |
8467 Expression *CondExp::syntaxCopy() | |
8468 { | |
8469 return new CondExp(loc, econd->syntaxCopy(), e1->syntaxCopy(), e2->syntaxCopy()); | |
8470 } | |
8471 | |
8472 | |
8473 Expression *CondExp::semantic(Scope *sc) | |
8474 { Type *t1; | |
8475 Type *t2; | |
8476 unsigned cs0; | |
8477 unsigned cs1; | |
8478 | |
8479 #if LOGSEMANTIC | |
8480 printf("CondExp::semantic('%s')\n", toChars()); | |
8481 #endif | |
8482 if (type) | |
8483 return this; | |
8484 | |
8485 econd = econd->semantic(sc); | |
8486 econd = resolveProperties(sc, econd); | |
8487 econd = econd->checkToPointer(); | |
8488 econd = econd->checkToBoolean(); | |
8489 | |
8490 #if 0 /* this cannot work right because the types of e1 and e2 | |
8491 * both contribute to the type of the result. | |
8492 */ | |
8493 if (sc->flags & SCOPEstaticif) | |
8494 { | |
8495 /* If in static if, don't evaluate what we don't have to. | |
8496 */ | |
8497 econd = econd->optimize(WANTflags); | |
8498 if (econd->isBool(TRUE)) | |
8499 { | |
8500 e1 = e1->semantic(sc); | |
8501 e1 = resolveProperties(sc, e1); | |
8502 return e1; | |
8503 } | |
8504 else if (econd->isBool(FALSE)) | |
8505 { | |
8506 e2 = e2->semantic(sc); | |
8507 e2 = resolveProperties(sc, e2); | |
8508 return e2; | |
8509 } | |
8510 } | |
8511 #endif | |
8512 | |
8513 | |
8514 cs0 = sc->callSuper; | |
8515 e1 = e1->semantic(sc); | |
8516 e1 = resolveProperties(sc, e1); | |
8517 cs1 = sc->callSuper; | |
8518 sc->callSuper = cs0; | |
8519 e2 = e2->semantic(sc); | |
8520 e2 = resolveProperties(sc, e2); | |
8521 sc->mergeCallSuper(loc, cs1); | |
8522 | |
8523 | |
8524 // If either operand is void, the result is void | |
8525 t1 = e1->type; | |
8526 t2 = e2->type; | |
8527 if (t1->ty == Tvoid || t2->ty == Tvoid) | |
8528 type = Type::tvoid; | |
8529 else if (t1 == t2) | |
8530 type = t1; | |
8531 else | |
8532 { | |
8533 typeCombine(sc); | |
8534 switch (e1->type->toBasetype()->ty) | |
8535 { | |
8536 case Tcomplex32: | |
8537 case Tcomplex64: | |
8538 case Tcomplex80: | |
8539 e2 = e2->castTo(sc, e1->type); | |
8540 break; | |
8541 } | |
8542 switch (e2->type->toBasetype()->ty) | |
8543 { | |
8544 case Tcomplex32: | |
8545 case Tcomplex64: | |
8546 case Tcomplex80: | |
8547 e1 = e1->castTo(sc, e2->type); | |
8548 break; | |
8549 } | |
8550 } | |
8551 return this; | |
8552 } | |
8553 | |
8554 Expression *CondExp::toLvalue(Scope *sc, Expression *ex) | |
8555 { | |
8556 PtrExp *e; | |
8557 | |
8558 // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2) | |
8559 e = new PtrExp(loc, this, type); | |
8560 | |
8561 e1 = e1->addressOf(sc); | |
8562 //e1 = e1->toLvalue(sc, NULL); | |
8563 | |
8564 e2 = e2->addressOf(sc); | |
8565 //e2 = e2->toLvalue(sc, NULL); | |
8566 | |
8567 typeCombine(sc); | |
8568 | |
8569 type = e2->type; | |
8570 return e; | |
8571 } | |
8572 | |
8573 Expression *CondExp::modifiableLvalue(Scope *sc, Expression *e) | |
8574 { | |
8575 error("conditional expression %s is not a modifiable lvalue", toChars()); | |
8576 return this; | |
8577 } | |
8578 | |
8579 void CondExp::checkEscape() | |
8580 { | |
8581 e1->checkEscape(); | |
8582 e2->checkEscape(); | |
8583 } | |
8584 | |
8585 | |
8586 Expression *CondExp::checkToBoolean() | |
8587 { | |
8588 e1 = e1->checkToBoolean(); | |
8589 e2 = e2->checkToBoolean(); | |
8590 return this; | |
8591 } | |
8592 | |
8593 int CondExp::checkSideEffect(int flag) | |
8594 { | |
8595 if (flag == 2) | |
8596 { | |
8597 return econd->checkSideEffect(2) || | |
8598 e1->checkSideEffect(2) || | |
8599 e2->checkSideEffect(2); | |
8600 } | |
8601 else | |
8602 { | |
8603 econd->checkSideEffect(1); | |
8604 e1->checkSideEffect(flag); | |
8605 return e2->checkSideEffect(flag); | |
8606 } | |
8607 } | |
8608 | |
8609 void CondExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
8610 { | |
8611 expToCBuffer(buf, hgs, econd, PREC_oror); | |
8612 buf->writestring(" ? "); | |
8613 expToCBuffer(buf, hgs, e1, PREC_expr); | |
8614 buf->writestring(" : "); | |
8615 expToCBuffer(buf, hgs, e2, PREC_cond); | |
8616 } | |
8617 | |
8618 |