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