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