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