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