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