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