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