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