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