Don't allow "a~b" if either could be null
[delight/core.git] / dmd / expression.c
blob4c1f4cd0a6c536409f547da788a548eb29fffd2a
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.
11 /* NOTE: This file has been patched from the original DMD distribution to
12 work with the GDC compiler.
14 Modified by David Friedman, December 2006
17 // Issues with using -include total.h (defines integer_t) and then complex.h fails...
18 #undef integer_t
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <assert.h>
24 #include <complex.h>
25 #include <math.h>
27 #ifdef __APPLE__
28 #define integer_t dmd_integer_t
29 #endif
31 #if _WIN32 && __DMC__
32 extern "C" char * __cdecl __locale_decpoint;
33 #endif
35 #if IN_GCC
36 #include "mem.h"
37 #elif _WIN32
38 #include "..\root\mem.h"
39 #elif linux
40 #include "../root/mem.h"
41 #endif
42 //#include "port.h"
43 #include "mtype.h"
44 #include "init.h"
45 #include "expression.h"
46 #include "template.h"
47 #include "utf.h"
48 #include "enum.h"
49 #include "scope.h"
50 #include "statement.h"
51 #include "declaration.h"
52 #include "aggregate.h"
53 #include "import.h"
54 #include "id.h"
55 #include "dsymbol.h"
56 #include "module.h"
57 #include "attrib.h"
58 #include "hdrgen.h"
59 #include "parse.h"
61 Expression *createTypeInfoArray(Scope *sc, Expression *args[], int dim);
63 #define LOGSEMANTIC 0
65 /**********************************
66 * Set operator precedence for each operator.
69 // Operator precedence - greater values are higher precedence
71 enum PREC
73 PREC_zero,
74 PREC_expr,
75 PREC_assign,
76 PREC_cond,
77 PREC_oror,
78 PREC_andand,
79 PREC_or,
80 PREC_xor,
81 PREC_and,
82 PREC_equal,
83 PREC_rel,
84 PREC_shift,
85 PREC_add,
86 PREC_mul,
87 PREC_unary,
88 PREC_primary,
91 enum PREC precedence[TOKMAX];
93 void initPrecedence()
95 precedence[TOKdotvar] = PREC_primary;
96 precedence[TOKimport] = PREC_primary;
97 precedence[TOKidentifier] = PREC_primary;
98 precedence[TOKthis] = PREC_primary;
99 precedence[TOKsuper] = PREC_primary;
100 precedence[TOKint64] = PREC_primary;
101 precedence[TOKfloat64] = PREC_primary;
102 precedence[TOKnull] = PREC_primary;
103 precedence[TOKstring] = PREC_primary;
104 precedence[TOKarrayliteral] = PREC_primary;
105 precedence[TOKtypedot] = PREC_primary;
106 precedence[TOKtypeid] = PREC_primary;
107 precedence[TOKis] = PREC_primary;
108 precedence[TOKassert] = PREC_primary;
109 precedence[TOKfunction] = PREC_primary;
110 precedence[TOKvar] = PREC_primary;
112 // post
113 precedence[TOKdotti] = PREC_primary;
114 precedence[TOKdot] = PREC_primary;
115 // precedence[TOKarrow] = PREC_primary;
116 precedence[TOKplusplus] = PREC_primary;
117 precedence[TOKminusminus] = PREC_primary;
118 precedence[TOKcall] = PREC_primary;
119 precedence[TOKslice] = PREC_primary;
120 precedence[TOKarray] = PREC_primary;
122 precedence[TOKaddress] = PREC_unary;
123 precedence[TOKstar] = PREC_unary;
124 precedence[TOKneg] = PREC_unary;
125 precedence[TOKuadd] = PREC_unary;
126 precedence[TOKnot] = PREC_unary;
127 precedence[TOKtobool] = PREC_add;
128 precedence[TOKtilde] = PREC_unary;
129 precedence[TOKdelete] = PREC_unary;
130 precedence[TOKnew] = PREC_unary;
131 precedence[TOKcast] = PREC_unary;
133 precedence[TOKmul] = PREC_mul;
134 precedence[TOKdiv] = PREC_mul;
135 precedence[TOKmod] = PREC_mul;
137 precedence[TOKadd] = PREC_add;
138 precedence[TOKmin] = PREC_add;
139 precedence[TOKcat] = PREC_add;
141 precedence[TOKshl] = PREC_shift;
142 precedence[TOKshr] = PREC_shift;
143 precedence[TOKushr] = PREC_shift;
145 precedence[TOKlt] = PREC_rel;
146 precedence[TOKle] = PREC_rel;
147 precedence[TOKgt] = PREC_rel;
148 precedence[TOKge] = PREC_rel;
149 precedence[TOKunord] = PREC_rel;
150 precedence[TOKlg] = PREC_rel;
151 precedence[TOKleg] = PREC_rel;
152 precedence[TOKule] = PREC_rel;
153 precedence[TOKul] = PREC_rel;
154 precedence[TOKuge] = PREC_rel;
155 precedence[TOKug] = PREC_rel;
156 precedence[TOKue] = PREC_rel;
157 precedence[TOKin] = PREC_rel;
159 precedence[TOKequal] = PREC_equal;
160 precedence[TOKnotequal] = PREC_equal;
161 precedence[TOKidentity] = PREC_equal;
162 precedence[TOKnotidentity] = PREC_equal;
164 precedence[TOKand] = PREC_and;
166 precedence[TOKxor] = PREC_xor;
168 precedence[TOKor] = PREC_or;
170 precedence[TOKandand] = PREC_andand;
172 precedence[TOKoror] = PREC_oror;
174 precedence[TOKquestion] = PREC_cond;
176 precedence[TOKassign] = PREC_assign;
177 precedence[TOKconstruct] = PREC_assign;
178 precedence[TOKblit] = PREC_assign;
179 precedence[TOKaddass] = PREC_assign;
180 precedence[TOKminass] = PREC_assign;
181 precedence[TOKcatass] = PREC_assign;
182 precedence[TOKmulass] = PREC_assign;
183 precedence[TOKdivass] = PREC_assign;
184 precedence[TOKmodass] = PREC_assign;
185 precedence[TOKshlass] = PREC_assign;
186 precedence[TOKshrass] = PREC_assign;
187 precedence[TOKushrass] = PREC_assign;
188 precedence[TOKandass] = PREC_assign;
189 precedence[TOKorass] = PREC_assign;
190 precedence[TOKxorass] = PREC_assign;
192 precedence[TOKcomma] = PREC_expr;
195 /*************************************************************
196 * Now that we have the right function f, we need to get the
197 * right 'this' pointer if f is in an outer class, but our
198 * existing 'this' pointer is in an inner class.
199 * This code is analogous to that used for variables
200 * in DotVarExp::semantic().
203 Expression *getRightThis(Loc loc, Scope *sc, AggregateDeclaration *ad, Expression *e1, Declaration *var)
206 Type *t = e1->type->toBasetype();
208 if (ad &&
209 !(t->ty == Tpointer && t->next->ty == Tstruct &&
210 ((TypeStruct *)t->next)->sym == ad)
212 !(t->ty == Tstruct &&
213 ((TypeStruct *)t)->sym == ad)
216 ClassDeclaration *cd = ad->isClassDeclaration();
217 ClassDeclaration *tcd = t->isClassHandle();
219 if (!cd || !tcd ||
220 !(tcd == cd || cd->isBaseOf(tcd, NULL))
223 if (tcd && tcd->isNested())
224 { // Try again with outer scope
226 e1 = new DotVarExp(loc, e1, tcd->vthis);
227 e1 = e1->semantic(sc);
229 // Skip over nested functions, and get the enclosing
230 // class type.
231 Dsymbol *s = tcd->toParent();
232 while (s && s->isFuncDeclaration())
233 { FuncDeclaration *f = s->isFuncDeclaration();
234 if (f->vthis)
236 e1 = new VarExp(loc, f->vthis);
238 s = s->toParent();
240 if (s && s->isClassDeclaration())
241 e1->type = s->isClassDeclaration()->type;
242 e1 = e1->semantic(sc);
243 goto L1;
245 #ifdef DEBUG
246 printf("2: ");
247 #endif
248 error("this for %s needs to be type %s not type %s",
249 var->toChars(), ad->toChars(), t->toChars());
252 return e1;
255 /*****************************************
256 * Determine if 'this' is available.
257 * If it is, return the FuncDeclaration that has it.
260 FuncDeclaration *hasThis(Scope *sc)
261 { FuncDeclaration *fd;
262 FuncDeclaration *fdthis;
264 //printf("hasThis()\n");
265 fdthis = sc->parent->isFuncDeclaration();
266 //printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis->toChars() : "");
268 // Go upwards until we find the enclosing member function
269 fd = fdthis;
270 while (1)
272 if (!fd)
274 goto Lno;
276 if (!fd->isNested())
277 break;
279 Dsymbol *parent = fd->parent;
280 while (parent)
282 TemplateInstance *ti = parent->isTemplateInstance();
283 if (ti)
284 parent = ti->parent;
285 else
286 break;
289 fd = fd->parent->isFuncDeclaration();
292 if (!fd->isThis())
293 { //printf("test '%s'\n", fd->toChars());
294 goto Lno;
297 assert(fd->vthis);
298 return fd;
300 Lno:
301 return NULL; // don't have 'this' available
305 /***************************************
306 * Pull out any properties.
309 Expression *resolveProperties(Scope *sc, Expression *e)
311 //printf("resolveProperties(%s)\n", e->toChars());
312 if (e->type)
314 Type *t = e->type->toBasetype();
316 if (t->ty == Tfunction)
318 e = new CallExp(e->loc, e);
319 e = e->semantic(sc);
322 /* Look for e being a lazy parameter; rewrite as delegate call
324 else if (e->op == TOKvar)
325 { VarExp *ve = (VarExp *)e;
327 if (ve->var->storage_class & STClazy)
329 e = new CallExp(e->loc, e);
330 e = e->semantic(sc);
334 else if (e->op == TOKdotexp)
336 e->error("expression has no value");
339 return e;
342 /******************************
343 * Perform semantic() on an array of Expressions.
346 void arrayExpressionSemantic(Expressions *exps, Scope *sc)
348 if (exps)
350 for (size_t i = 0; i < exps->dim; i++)
351 { Expression *e = (Expression *)exps->data[i];
353 e = e->semantic(sc);
354 exps->data[i] = (void *)e;
359 /****************************************
360 * Expand tuples.
363 void expandTuples(Expressions *exps)
365 //printf("expandTuples()\n");
366 if (exps)
368 for (size_t i = 0; i < exps->dim; i++)
369 { Expression *arg = (Expression *)exps->data[i];
370 if (!arg)
371 continue;
373 // Look for tuple with 0 members
374 if (arg->op == TOKtype)
375 { TypeExp *e = (TypeExp *)arg;
376 if (e->type->toBasetype()->ty == Ttuple)
377 { TypeTuple *tt = (TypeTuple *)e->type->toBasetype();
379 if (!tt->arguments || tt->arguments->dim == 0)
381 exps->remove(i);
382 if (i == exps->dim)
383 return;
384 i--;
385 continue;
390 // Inline expand all the tuples
391 while (arg->op == TOKtuple)
392 { TupleExp *te = (TupleExp *)arg;
394 exps->remove(i); // remove arg
395 exps->insert(i, te->exps); // replace with tuple contents
396 if (i == exps->dim)
397 return; // empty tuple, no more arguments
398 arg = (Expression *)exps->data[i];
404 /****************************************
405 * Preprocess arguments to function.
408 void preFunctionArguments(Loc loc, Scope *sc, Expressions *exps)
410 if (exps)
412 expandTuples(exps);
414 for (size_t i = 0; i < exps->dim; i++)
415 { Expression *arg = (Expression *)exps->data[i];
417 if (!arg->type)
419 #ifdef DEBUG
420 if (!global.gag)
421 printf("1: \n");
422 #endif
423 arg->error("%s is not an expression", arg->toChars());
424 arg = new IntegerExp(arg->loc, 0, Type::tint32);
427 arg = resolveProperties(sc, arg);
428 exps->data[i] = (void *) arg;
430 //arg->rvalue();
431 #if 0
432 if (arg->type->ty == Tfunction)
434 arg = new AddrExp(arg->loc, arg);
435 arg = arg->semantic(sc);
436 exps->data[i] = (void *) arg;
438 #endif
444 /****************************************
445 * Now that we know the exact type of the function we're calling,
446 * the arguments[] need to be adjusted:
447 * 1) implicitly convert argument to the corresponding parameter type
448 * 2) add default arguments for any missing arguments
449 * 3) do default promotions on arguments corresponding to ...
450 * 4) add hidden _arguments[] argument
453 void functionArguments(Loc loc, Scope *sc, TypeFunction *tf, Expressions *arguments)
455 unsigned n;
456 int done;
457 Type *tb;
459 //printf("functionArguments()\n");
460 assert(arguments);
461 size_t nargs = arguments ? arguments->dim : 0;
462 size_t nparams = Argument::dim(tf->parameters);
464 if (nargs > nparams && tf->varargs == 0)
465 error(loc, "expected %"PRIuSIZE" arguments, not %"PRIuSIZE, nparams, nargs);
467 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams)
469 done = 0;
470 for (size_t i = 0; i < n; i++)
472 Expression *arg;
474 if (i < nargs)
475 arg = (Expression *)arguments->data[i];
476 else
477 arg = NULL;
479 if (i < nparams)
481 Argument *p = Argument::getNth(tf->parameters, i);
483 if (!arg)
485 if (!p->defaultArg)
487 if (tf->varargs == 2 && i + 1 == nparams)
488 goto L2;
489 error(loc, "expected %"PRIuSIZE" arguments, not %"PRIuSIZE, nparams, nargs);
490 break;
492 arg = p->defaultArg->copy();
493 arguments->push(arg);
494 nargs++;
497 if (tf->varargs == 2 && i + 1 == nparams)
499 //printf("\t\tvarargs == 2, p->type = '%s'\n", p->type->toChars());
500 if (arg->implicitConvTo(p->type))
502 if (nargs != nparams)
503 error(loc, "expected %"PRIuSIZE" arguments, not %"PRIuSIZE, nparams, nargs);
504 goto L1;
507 Type *tb = p->type->toBasetype();
508 Type *tret = p->isLazyArray();
509 switch (tb->ty)
511 case Tsarray:
512 case Tarray:
513 { // Create a static array variable v of type arg->type
514 #ifdef IN_GCC
515 /* GCC 4.0 does not like zero length arrays used like
516 this; pass a null array value instead. Could also
517 just make a one-element array. */
518 if (nargs - i == 0)
520 arg = new NullExp(loc);
521 break;
523 #endif
524 static int idn;
525 char name[10 + sizeof(idn)*3 + 1];
526 sprintf(name, "__arrayArg%d", ++idn);
527 Identifier *id = Lexer::idPool(name);
528 Type *t = new TypeSArray(tb->next, new IntegerExp(nargs - i));
529 t = t->semantic(loc, sc);
530 VarDeclaration *v = new VarDeclaration(loc, t, id, new VoidInitializer(loc));
531 v->semantic(sc);
532 v->parent = sc->parent;
533 //sc->insert(v);
535 Expression *c = new DeclarationExp(0, v);
536 c->type = v->type;
538 for (size_t u = i; u < nargs; u++)
539 { Expression *a = (Expression *)arguments->data[u];
540 if (tret && !tb->next->equals(a->type))
541 a = a->toDelegate(sc, tret);
543 Expression *e = new VarExp(loc, v);
544 e = new IndexExp(loc, e, new IntegerExp(u + 1 - nparams));
545 e = new AssignExp(loc, e, a);
546 if (c)
547 c = new CommaExp(loc, c, e);
548 else
549 c = e;
551 arg = new VarExp(loc, v);
552 if (c)
553 arg = new CommaExp(loc, c, arg);
554 break;
556 case Tclass:
557 { /* Set arg to be:
558 * new Tclass(arg0, arg1, ..., argn)
560 Expressions *args = new Expressions();
561 args->setDim(nargs - i);
562 for (size_t u = i; u < nargs; u++)
563 args->data[u - i] = arguments->data[u];
564 arg = new NewExp(loc, NULL, NULL, p->type, args);
565 break;
567 default:
568 if (!arg)
569 { error(loc, "not enough arguments");
570 return;
572 break;
574 arg = arg->semantic(sc);
575 //printf("\targ = '%s'\n", arg->toChars());
576 arguments->setDim(i + 1);
577 done = 1;
581 if (!(p->storageClass & STClazy && p->type->ty == Tvoid))
582 arg = arg->implicitCastTo(sc, p->type);
583 if (p->storageClass & (STCout | STCref))
585 // BUG: should check that argument to ref is type 'invariant'
586 // BUG: assignments to ref should also be type 'invariant'
587 arg = arg->modifiableLvalue(sc, arg);
589 //if (arg->op == TOKslice)
590 //arg->error("cannot modify slice %s", arg->toChars());
593 // Convert static arrays to pointers
594 tb = arg->type->toBasetype();
595 if (tb->ty == Tsarray)
597 arg = arg->checkToPointer();
600 // Convert lazy argument to a delegate
601 if (p->storageClass & STClazy)
603 arg = arg->toDelegate(sc, p->type);
606 else
609 // If not D linkage, do promotions
610 if (tf->linkage != LINKd)
612 // Promote bytes, words, etc., to ints
613 arg = arg->integralPromotions(sc);
615 // Promote floats to doubles
616 switch (arg->type->ty)
618 case Tfloat32:
619 arg = arg->castTo(sc, Type::tfloat64);
620 break;
622 case Timaginary32:
623 arg = arg->castTo(sc, Type::timaginary64);
624 break;
628 // Convert static arrays to dynamic arrays
629 tb = arg->type->toBasetype();
630 if (tb->ty == Tsarray)
631 { TypeSArray *ts = (TypeSArray *)tb;
632 Type *ta = tb->next->arrayOf();
633 if (ts->size(arg->loc) == 0)
634 { arg = new NullExp(arg->loc);
635 arg->type = ta;
637 else
638 arg = arg->castTo(sc, ta);
641 arg->rvalue();
643 arg = arg->optimize(WANTvalue);
644 arguments->data[i] = (void *) arg;
645 if (done)
646 break;
649 // If D linkage and variadic, add _arguments[] as first argument
650 if (tf->linkage == LINKd && tf->varargs == 1)
652 Expression *e;
654 e = createTypeInfoArray(sc, (Expression **)&arguments->data[nparams],
655 arguments->dim - nparams);
656 arguments->insert(0, e);
660 /**************************************************
661 * Write expression out to buf, but wrap it
662 * in ( ) if its precedence is less than pr.
665 void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, enum PREC pr)
667 if (precedence[e->op] < pr)
669 buf->writeByte('(');
670 e->toCBuffer(buf, hgs);
671 buf->writeByte(')');
673 else
674 e->toCBuffer(buf, hgs);
677 /**************************************************
678 * Write out argument list to buf.
681 void argsToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs)
683 if (arguments)
685 for (size_t i = 0; i < arguments->dim; i++)
686 { Expression *arg = (Expression *)arguments->data[i];
688 if (arg)
689 { if (i)
690 buf->writeByte(',');
691 expToCBuffer(buf, hgs, arg, PREC_assign);
697 /**************************************************
698 * Write out argument types to buf.
701 void argExpTypesToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs)
703 if (arguments)
704 { OutBuffer argbuf;
706 for (size_t i = 0; i < arguments->dim; i++)
707 { Expression *arg = (Expression *)arguments->data[i];
709 if (i)
710 buf->writeByte(',');
711 argbuf.reset();
712 arg->type->toCBuffer2(&argbuf, hgs, 0);
713 buf->write(&argbuf);
718 /******************************** Expression **************************/
720 Expression::Expression(Loc loc, enum TOK op, int size)
721 : loc(loc)
723 this->loc = loc;
724 this->op = op;
725 this->size = size;
726 type = NULL;
729 Expression *Expression::syntaxCopy()
731 //printf("Expression::syntaxCopy()\n");
732 //dump(0);
733 return copy();
736 /*********************************
737 * Does *not* do a deep copy.
740 Expression *Expression::copy()
742 Expression *e;
743 if (!size)
745 #ifdef DEBUG
746 fprintf(stdmsg, "No expression copy for: %s\n", toChars());
747 printf("op = %d\n", op);
748 dump(0);
749 #endif
750 assert(0);
752 e = (Expression *)mem.malloc(size);
753 return (Expression *)memcpy(e, this, size);
756 /**************************
757 * Semantically analyze Expression.
758 * Determine types, fold constants, etc.
761 Expression *Expression::semantic(Scope *sc)
763 #if LOGSEMANTIC
764 printf("Expression::semantic()\n");
765 #endif
766 if (type)
767 type = type->semantic(loc, sc);
768 else
769 type = Type::tvoid;
770 return this;
773 void Expression::print()
775 fprintf(stdmsg, "%s\n", toChars());
776 fflush(stdmsg);
779 char *Expression::toChars()
780 { OutBuffer *buf;
781 HdrGenState hgs;
783 memset(&hgs, 0, sizeof(hgs));
784 buf = new OutBuffer();
785 toCBuffer(buf, &hgs);
786 return buf->toChars();
789 void Expression::error(const char *format, ...)
791 va_list ap;
792 va_start(ap, format);
793 ::verror(loc, format, ap);
794 va_end( ap );
797 void Expression::rvalue()
799 if (type && type->toBasetype()->ty == Tvoid)
800 { error("expression %s is void and has no value", toChars());
801 #if 0
802 dump(0);
803 halt();
804 #endif
808 Expression *Expression::combine(Expression *e1, Expression *e2)
810 if (e1)
812 if (e2)
814 e1 = new CommaExp(e1->loc, e1, e2);
815 e1->type = e2->type;
818 else
819 e1 = e2;
820 return e1;
823 integer_t Expression::toInteger()
825 //printf("Expression %s\n", Token::toChars(op));
826 error("Integer constant expression expected instead of %s", toChars());
827 return 0;
830 uinteger_t Expression::toUInteger()
832 //printf("Expression %s\n", Token::toChars(op));
833 return (uinteger_t)toInteger();
836 real_t Expression::toReal()
838 error("Floating point constant expression expected instead of %s", toChars());
839 return 0;
842 real_t Expression::toImaginary()
844 error("Floating point constant expression expected instead of %s", toChars());
845 return 0;
848 complex_t Expression::toComplex()
850 error("Floating point constant expression expected instead of %s", toChars());
851 #ifdef IN_GCC
852 return complex_t(real_t(0)); // %% nicer
853 #else
854 return 0;
855 #endif
858 void Expression::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
860 buf->writestring(Token::toChars(op));
863 void Expression::toMangleBuffer(OutBuffer *buf)
865 error("expression %s is not a valid template value argument", toChars());
868 /*******************************
869 * Give error if we're not an lvalue.
870 * If we can, convert expression to be an lvalue.
873 Expression *Expression::toLvalue(Scope *sc, Expression *e)
875 if (!e)
876 e = this;
877 else if (!loc.filename)
878 loc = e->loc;
879 error("%s is not an lvalue", e->toChars());
880 return this;
883 Expression *Expression::modifiableLvalue(Scope *sc, Expression *e)
885 // See if this expression is a modifiable lvalue (i.e. not const)
886 return toLvalue(sc, e);
889 /************************************
890 * Detect cases where pointers to the stack can 'escape' the
891 * lifetime of the stack frame.
894 void Expression::checkEscape()
898 void Expression::checkScalar()
900 if (!type->isscalar())
901 error("'%s' is not a scalar, it is a %s", toChars(), type->toChars());
904 void Expression::checkNoBool()
906 if (type->toBasetype()->ty == Tbool)
907 error("operation not allowed on bool '%s'", toChars());
910 Expression *Expression::checkIntegral()
912 if (!type->isintegral())
913 { error("'%s' is not of integral type, it is a %s", toChars(), type->toChars());
914 return new IntegerExp(0);
916 return this;
919 Expression *Expression::checkArithmetic()
921 if (!type->isintegral() && !type->isfloating())
922 { error("'%s' is not of arithmetic type, it is a %s", toChars(), type->toChars());
923 return new IntegerExp(0);
925 return this;
928 void Expression::checkDeprecated(Scope *sc, Dsymbol *s)
930 s->checkDeprecated(loc, sc);
933 /********************************
934 * Check for expressions that have no use.
935 * Input:
936 * flag 0 not going to use the result, so issue error message if no
937 * side effects
938 * 1 the result of the expression is used, but still check
939 * for useless subexpressions
940 * 2 do not issue error messages, just return !=0 if expression
941 * has side effects
944 int Expression::checkSideEffect(int flag)
946 if (flag == 0)
947 { if (op == TOKimport)
949 error("%s has no effect", toChars());
951 else
952 error("%s has no effect in expression (%s)",
953 Token::toChars(op), toChars());
955 return 0;
958 /*****************************
959 * Check that expression can be tested for true or false.
962 Expression *Expression::checkToBoolean()
964 // Default is 'yes' - do nothing
966 #ifdef DEBUG
967 if (!type)
968 dump(0);
969 #endif
971 if (!type->checkBoolean())
973 error("expression %s of type %s does not have a boolean value", toChars(), type->toChars());
975 return this;
978 /****************************
981 Expression *Expression::checkToPointer()
983 Expression *e;
984 Type *tb;
986 //printf("Expression::checkToPointer()\n");
987 e = this;
989 // If C static array, convert to pointer
990 tb = type->toBasetype();
991 if (tb->ty == Tsarray)
992 { TypeSArray *ts = (TypeSArray *)tb;
993 if (ts->size(loc) == 0)
994 e = new NullExp(loc);
995 else
996 e = new AddrExp(loc, this);
997 e->type = tb->next->pointerTo();
999 return e;
1002 /******************************
1003 * Take address of expression.
1006 Expression *Expression::addressOf(Scope *sc)
1008 Expression *e;
1010 //printf("Expression::addressOf()\n");
1011 e = toLvalue(sc, NULL);
1012 e = new AddrExp(loc, e);
1013 e->type = type->pointerTo();
1014 return e;
1017 /******************************
1018 * If this is a reference, dereference it.
1021 Expression *Expression::deref()
1023 //printf("Expression::deref()\n");
1024 if (type->ty == Treference)
1025 { Expression *e;
1027 e = new PtrExp(loc, this);
1028 e->type = type->next;
1029 return e;
1031 return this;
1034 /********************************
1035 * Does this expression statically evaluate to a boolean TRUE or FALSE?
1038 int Expression::isBool(int result)
1040 return FALSE;
1043 /********************************
1044 * Does this expression result in either a 1 or a 0?
1047 int Expression::isBit()
1049 return FALSE;
1052 Expressions *Expression::arraySyntaxCopy(Expressions *exps)
1053 { Expressions *a = NULL;
1055 if (exps)
1057 a = new Expressions();
1058 a->setDim(exps->dim);
1059 for (int i = 0; i < a->dim; i++)
1060 { Expression *e = (Expression *)exps->data[i];
1062 e = e->syntaxCopy();
1063 a->data[i] = e;
1066 return a;
1069 /******************************** IntegerExp **************************/
1071 IntegerExp::IntegerExp(Loc loc, integer_t value, Type *type)
1072 : Expression(loc, TOKint64, sizeof(IntegerExp))
1074 //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type->toChars() : "");
1075 if (type && !type->isscalar())
1077 error("integral constant must be scalar type, not %s", type->toChars());
1078 type = Type::terror;
1080 this->type = type;
1081 this->value = value;
1084 IntegerExp::IntegerExp(integer_t value)
1085 : Expression(0, TOKint64, sizeof(IntegerExp))
1087 this->type = Type::tint32;
1088 this->value = value;
1091 int IntegerExp::equals(Object *o)
1092 { IntegerExp *ne;
1094 if (this == o ||
1095 (((Expression *)o)->op == TOKint64 &&
1096 ((ne = (IntegerExp *)o), type->equals(ne->type)) &&
1097 value == ne->value))
1098 return 1;
1099 return 0;
1102 char *IntegerExp::toChars()
1104 #if 1
1105 return Expression::toChars();
1106 #else
1107 static char buffer[sizeof(value) * 3 + 1];
1109 sprintf(buffer, "%"PRIdMAX, value);
1110 return buffer;
1111 #endif
1114 integer_t IntegerExp::toInteger()
1115 { Type *t;
1117 t = type;
1118 while (t)
1120 switch (t->ty)
1122 case Tbit:
1123 case Tbool: value = (value != 0); break;
1124 case Tint8: value = (d_int8) value; break;
1125 case Tchar:
1126 case Tuns8: value = (d_uns8) value; break;
1127 case Tint16: value = (d_int16) value; break;
1128 case Twchar:
1129 case Tuns16: value = (d_uns16) value; break;
1130 case Tint32: value = (d_int32) value; break;
1131 case Tdchar:
1132 case Tuns32: value = (d_uns32) value; break;
1133 case Tint64: value = (d_int64) value; break;
1134 case Tuns64: value = (d_uns64) value; break;
1136 case Tpointer:
1137 switch (PTRSIZE)
1139 case 4: value = (d_uns32) value; break;
1140 case 8: value = (d_uns64) value; break;
1141 default:
1142 assert(PTRSIZE == 4 || PTRSIZE == 8);
1144 break;
1146 case Tenum:
1148 TypeEnum *te = (TypeEnum *)t;
1149 t = te->sym->memtype;
1150 continue;
1153 case Ttypedef:
1155 TypeTypedef *tt = (TypeTypedef *)t;
1156 t = tt->sym->basetype;
1157 continue;
1160 default:
1161 print();
1162 type->print();
1163 assert(0);
1164 break;
1166 break;
1168 return value;
1171 real_t IntegerExp::toReal()
1173 Type *t;
1175 toInteger();
1176 t = type->toBasetype();
1177 if (t->ty == Tuns64)
1178 return (real_t)(d_uns64)value;
1179 else
1180 return (real_t)(d_int64)value;
1183 real_t IntegerExp::toImaginary()
1185 return (real_t) 0;
1188 complex_t IntegerExp::toComplex()
1190 return toReal();
1193 int IntegerExp::isBool(int result)
1195 return result ? value != 0 : value == 0;
1198 Expression *IntegerExp::semantic(Scope *sc)
1200 if (!type)
1202 // Determine what the type of this number is
1203 integer_t number = value;
1205 if (number & 0x8000000000000000LL)
1206 type = Type::tuns64;
1207 else if (number & 0xFFFFFFFF80000000LL)
1208 type = Type::tint64;
1209 else
1210 type = Type::tint32;
1212 else
1213 { type = type->semantic(loc, sc);
1215 return this;
1218 Expression *IntegerExp::toLvalue(Scope *sc, Expression *e)
1220 if (!e)
1221 e = this;
1222 else if (!loc.filename)
1223 loc = e->loc;
1224 e->error("constant %s is not an lvalue", e->toChars());
1225 return this;
1228 void IntegerExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
1230 integer_t v = toInteger();
1232 if (type)
1233 { Type *t = type;
1236 switch (t->ty)
1238 case Tenum:
1239 { TypeEnum *te = (TypeEnum *)t;
1240 buf->printf("cast(%s)", te->sym->toChars());
1241 t = te->sym->memtype;
1242 goto L1;
1245 case Ttypedef:
1246 { TypeTypedef *tt = (TypeTypedef *)t;
1247 buf->printf("cast(%s)", tt->sym->toChars());
1248 t = tt->sym->basetype;
1249 goto L1;
1252 case Twchar: // BUG: need to cast(wchar)
1253 case Tdchar: // BUG: need to cast(dchar)
1254 if ((uinteger_t)v > 0xFF)
1256 buf->printf("'\\U%08x'", v);
1257 break;
1259 case Tchar:
1260 if (v == '\'')
1261 buf->writestring("'\\''");
1262 else if (isprint(v) && v != '\\')
1263 buf->printf("'%c'", (int)v);
1264 else
1265 buf->printf("'\\x%02x'", (int)v);
1266 break;
1268 case Tint8:
1269 buf->writestring("cast(byte)");
1270 goto L2;
1272 case Tint16:
1273 buf->writestring("cast(short)");
1274 goto L2;
1276 case Tint32:
1278 buf->printf("%d", (int)v);
1279 break;
1281 case Tuns8:
1282 buf->writestring("cast(ubyte)");
1283 goto L3;
1285 case Tuns16:
1286 buf->writestring("cast(ushort)");
1287 goto L3;
1289 case Tuns32:
1291 buf->printf("%du", (unsigned)v);
1292 break;
1294 case Tint64:
1295 buf->printf("%"PRIdMAX"L", v);
1296 break;
1298 case Tuns64:
1299 buf->printf("%"PRIuMAX"LU", v);
1300 break;
1302 case Tbit:
1303 case Tbool:
1304 buf->writestring((char *)(v ? "true" : "false"));
1305 break;
1307 case Tpointer:
1308 buf->writestring("cast(");
1309 buf->writestring(t->toChars());
1310 buf->writeByte(')');
1311 goto L3;
1313 default:
1314 #ifdef DEBUG
1315 t->print();
1316 #endif
1317 assert(0);
1320 else if (v & 0x8000000000000000LL)
1321 buf->printf("0x%"PRIxMAX, v);
1322 else
1323 buf->printf("%"PRIdMAX, v);
1326 void IntegerExp::toMangleBuffer(OutBuffer *buf)
1328 if ((sinteger_t)value < 0)
1329 buf->printf("N%"PRIdMAX, -value);
1330 else
1331 buf->printf("%"PRIdMAX, value);
1334 /******************************** RealExp **************************/
1336 RealExp::RealExp(Loc loc, real_t value, Type *type)
1337 : Expression(loc, TOKfloat64, sizeof(RealExp))
1339 //printf("RealExp::RealExp(%Lg)\n", value);
1340 this->value = value;
1341 this->type = type;
1344 char *RealExp::toChars()
1346 char buffer[sizeof(value) * 3 + 8 + 1 + 1];
1348 #ifdef IN_GCC
1349 value.format(buffer, sizeof(buffer));
1350 if (type->isimaginary())
1351 strcat(buffer, "i");
1352 #else
1353 sprintf(buffer, type->isimaginary() ? "%Lgi" : "%Lg", value);
1354 #endif
1355 assert(strlen(buffer) < sizeof(buffer));
1356 return mem.strdup(buffer);
1359 integer_t RealExp::toInteger()
1361 #ifdef IN_GCC
1362 return (d_int64) toReal().toInt();
1363 #else
1364 return (sinteger_t) toReal();
1365 #endif
1368 uinteger_t RealExp::toUInteger()
1370 #ifdef IN_GCC
1371 return (uinteger_t) toReal().toInt();
1372 #else
1373 return (uinteger_t) toReal();
1374 #endif
1377 real_t RealExp::toReal()
1379 return type->isreal() ? value : 0;
1382 real_t RealExp::toImaginary()
1384 return type->isreal() ? 0 : value;
1387 complex_t RealExp::toComplex()
1389 #ifdef __DMC__
1390 return toReal() + toImaginary() * I;
1391 #else
1392 return complex_t(toReal(), toImaginary());
1393 #endif
1396 /********************************
1397 * Test to see if two reals are the same.
1398 * Regard NaN's as equivalent.
1399 * Regard +0 and -0 as different.
1402 int RealEquals(real_t x1, real_t x2)
1404 #ifndef IN_GCC
1405 return (isnan(x1) && isnan(x2)) ||
1406 /* In some cases, the REALPAD bytes get garbage in them,
1407 * so be sure and ignore them.
1409 memcmp(&x1, &x2, REALSIZE - REALPAD) == 0;
1410 #else
1411 return (x1.isNan() && x2.isNan()) || x1.isIdenticalTo(x2);
1412 #endif
1415 int RealExp::equals(Object *o)
1416 { RealExp *ne;
1418 if (this == o ||
1419 (((Expression *)o)->op == TOKfloat64 &&
1420 ((ne = (RealExp *)o), type->equals(ne->type)) &&
1421 RealEquals(value, ne->value)
1424 return 1;
1425 return 0;
1428 Expression *RealExp::semantic(Scope *sc)
1430 if (!type)
1431 type = Type::tfloat64;
1432 else
1433 type = type->semantic(loc, sc);
1434 return this;
1437 int RealExp::isBool(int result)
1439 #ifdef IN_GCC
1440 return result ? (! value.isZero()) : (value.isZero());
1441 #else
1442 return result ? (value != 0)
1443 : (value == 0);
1444 #endif
1447 void floatToBuffer(OutBuffer *buf, Type *type, const real_t & value)
1449 /* In order to get an exact representation, try converting it
1450 * to decimal then back again. If it matches, use it.
1451 * If it doesn't, fall back to hex, which is
1452 * always exact.
1454 char buffer[48];
1455 #ifdef IN_GCC
1456 real_t parsed_value;
1458 value.format(buffer, sizeof(buffer));
1459 parsed_value = real_t::parse(buffer, real_t::LongDouble);
1460 if (parsed_value.isIdenticalTo( value ))
1461 buf->writestring(buffer);
1462 else
1464 value.formatHex(buffer, sizeof(buffer));
1465 buf->writestring(buffer);
1467 #else
1468 sprintf(buffer, "%Lg", value);
1469 assert(strlen(buffer) < sizeof(buffer));
1470 #if _WIN32 && __DMC__
1471 char *save = __locale_decpoint;
1472 __locale_decpoint = ".";
1473 real_t r = strtold(buffer, NULL);
1474 __locale_decpoint = save;
1475 #else
1476 real_t r = strtold(buffer, NULL);
1477 #endif
1478 if (r == value) // if exact duplication
1479 buf->writestring(buffer);
1480 else
1481 buf->printf("%La", value); // ensure exact duplication
1482 #endif
1484 if (type)
1486 Type *t = type->toBasetype();
1487 switch (t->ty)
1489 case Tfloat32:
1490 case Timaginary32:
1491 case Tcomplex32:
1492 buf->writeByte('F');
1493 break;
1495 case Tfloat80:
1496 case Timaginary80:
1497 case Tcomplex80:
1498 buf->writeByte('L');
1499 break;
1501 default:
1502 break;
1504 if (t->isimaginary())
1505 buf->writeByte('i');
1509 void RealExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
1511 floatToBuffer(buf, type, value);
1514 void realToMangleBuffer(OutBuffer *buf, real_t value)
1516 /* Rely on %A to get portable mangling.
1517 * Must munge result to get only identifier characters.
1519 * Possible values from %A => mangled result
1520 * NAN => NAN
1521 * -INF => NINF
1522 * INF => INF
1523 * -0X1.1BC18BA997B95P+79 => N11BC18BA997B95P79
1524 * 0X1.9P+2 => 19P2
1527 #ifdef IN_GCC
1528 if (value.isNan())
1529 buf->writestring("NAN"); // no -NAN bugs
1530 else if (value.isInf())
1531 buf->writestring(value.isNegative()?"NINF":"INF");
1532 #else
1533 if (isnan(value))
1534 buf->writestring("NAN"); // no -NAN bugs
1535 #endif
1536 else
1538 char buffer[64];
1539 #ifdef IN_GCC
1540 value.formatHex(buffer, sizeof(buffer));
1541 int n = strlen(buffer);
1542 #else
1543 int n = sprintf(buffer, "%LA", value);
1544 assert(n > 0 && n < sizeof(buffer));
1545 #endif
1546 for (int i = 0; i < n; i++)
1547 { char c = buffer[i];
1549 switch (c)
1551 case '-':
1552 buf->writeByte('N');
1553 break;
1555 case '+':
1556 case 'x':
1557 case 'X':
1558 case '.':
1559 break;
1561 case '0':
1562 if (i < 2)
1563 break; // skip leading 0X
1564 default:
1565 buf->writeByte(c);
1566 break;
1572 void RealExp::toMangleBuffer(OutBuffer *buf)
1574 buf->writeByte('e');
1575 realToMangleBuffer(buf, value);
1579 /******************************** ComplexExp **************************/
1581 ComplexExp::ComplexExp(Loc loc, complex_t value, Type *type)
1582 : Expression(loc, TOKcomplex80, sizeof(ComplexExp))
1584 this->value = value;
1585 this->type = type;
1586 //printf("ComplexExp::ComplexExp(%s)\n", toChars());
1589 char *ComplexExp::toChars()
1591 char buffer[sizeof(value) * 3 + 8 + 1];
1593 #ifdef IN_GCC
1594 char buf1[sizeof(value) * 3 + 8 + 1];
1595 char buf2[sizeof(value) * 3 + 8 + 1];
1596 creall(value).format(buf1, sizeof(buf1));
1597 cimagl(value).format(buf2, sizeof(buf2));
1598 sprintf(buffer, "(%s+%si)", buf1, buf2);
1599 #else
1600 sprintf(buffer, "(%Lg+%Lgi)", creall(value), cimagl(value));
1601 assert(strlen(buffer) < sizeof(buffer));
1602 #endif
1603 return mem.strdup(buffer);
1606 integer_t ComplexExp::toInteger()
1608 #ifdef IN_GCC
1609 return (d_int64) toReal().toInt();
1610 #else
1611 return (sinteger_t) toReal();
1612 #endif
1615 uinteger_t ComplexExp::toUInteger()
1617 #ifdef IN_GCC
1618 return (uinteger_t) toReal().toInt();
1619 #else
1620 return (uinteger_t) toReal();
1621 #endif
1624 real_t ComplexExp::toReal()
1626 return creall(value);
1629 real_t ComplexExp::toImaginary()
1631 return cimagl(value);
1634 complex_t ComplexExp::toComplex()
1636 return value;
1639 int ComplexExp::equals(Object *o)
1640 { ComplexExp *ne;
1642 if (this == o ||
1643 (((Expression *)o)->op == TOKcomplex80 &&
1644 ((ne = (ComplexExp *)o), type->equals(ne->type)) &&
1645 #ifndef IN_GCC
1646 RealEquals(creall(value), creall(ne->value)) &&
1647 RealEquals(cimagl(value), cimagl(ne->value))
1648 #else
1649 RealEquals(value.re, ne->value.re) &&
1650 RealEquals(value.im, ne->value.im)
1651 #endif
1654 return 1;
1655 return 0;
1658 Expression *ComplexExp::semantic(Scope *sc)
1660 if (!type)
1661 type = Type::tcomplex80;
1662 else
1663 type = type->semantic(loc, sc);
1664 return this;
1667 int ComplexExp::isBool(int result)
1669 if (result)
1670 return (bool)(value);
1671 else
1672 return !value;
1675 void ComplexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
1677 /* Print as:
1678 * (re+imi)
1680 #ifdef IN_GCC
1681 char buf1[sizeof(value) * 3 + 8 + 1];
1682 char buf2[sizeof(value) * 3 + 8 + 1];
1683 creall(value).format(buf1, sizeof(buf1));
1684 cimagl(value).format(buf2, sizeof(buf2));
1685 buf->printf("(%s+%si)", buf1, buf2);
1686 #else
1687 buf->writeByte('(');
1688 floatToBuffer(buf, type, creall(value));
1689 buf->writeByte('+');
1690 floatToBuffer(buf, type, cimagl(value));
1691 buf->writestring("i)");
1692 #endif
1695 void ComplexExp::toMangleBuffer(OutBuffer *buf)
1697 buf->writeByte('c');
1698 real_t r = toReal();
1699 realToMangleBuffer(buf, r);
1700 buf->writeByte('c'); // separate the two
1701 r = toImaginary();
1702 realToMangleBuffer(buf, r);
1705 /******************************** IdentifierExp **************************/
1707 IdentifierExp::IdentifierExp(Loc loc, Identifier *ident)
1708 : Expression(loc, TOKidentifier, sizeof(IdentifierExp))
1710 this->ident = ident;
1713 Expression *IdentifierExp::semantic(Scope *sc)
1715 Dsymbol *s;
1716 Dsymbol *scopesym;
1718 #if LOGSEMANTIC
1719 printf("IdentifierExp::semantic('%s')\n", ident->toChars());
1720 #endif
1721 s = sc->search(loc, ident, &scopesym);
1722 if (s)
1723 { Expression *e;
1724 WithScopeSymbol *withsym;
1726 // See if it was a with class
1727 withsym = scopesym->isWithScopeSymbol();
1728 if (withsym)
1730 s = s->toAlias();
1732 // Same as wthis.ident
1733 if (s->needThis() || s->isTemplateDeclaration())
1735 e = new VarExp(loc, withsym->withstate->wthis);
1736 e = new DotIdExp(loc, e, ident);
1738 else
1739 { Type *t = withsym->withstate->wthis->type;
1740 if (t->ty == Tpointer)
1741 t = t->next;
1742 e = new TypeDotIdExp(loc, t, ident);
1745 else
1747 if (!s->parent && scopesym->isArrayScopeSymbol())
1748 { // Kludge to run semantic() here because
1749 // ArrayScopeSymbol::search() doesn't have access to sc.
1750 s->semantic(sc);
1752 // Look to see if f is really a function template
1753 FuncDeclaration *f = s->isFuncDeclaration();
1754 if (f && f->parent)
1755 { TemplateInstance *ti = f->parent->isTemplateInstance();
1757 if (ti &&
1758 !ti->isTemplateMixin() &&
1759 (ti->name == f->ident ||
1760 ti->toAlias()->ident == f->ident)
1762 ti->tempdecl && ti->tempdecl->onemember)
1764 TemplateDeclaration *tempdecl = ti->tempdecl;
1765 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's
1766 tempdecl = tempdecl->overroot; // then get the start
1767 e = new TemplateExp(loc, tempdecl);
1768 e = e->semantic(sc);
1769 return e;
1772 e = new DsymbolExp(loc, s);
1774 return e->semantic(sc);
1776 error("undefined identifier %s", ident->toChars());
1777 type = Type::terror;
1778 return this;
1781 char *IdentifierExp::toChars()
1783 return ident->toChars();
1786 void IdentifierExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
1788 if (hgs->hdrgen)
1789 buf->writestring(ident->toHChars2());
1790 else
1791 buf->writestring(ident->toChars());
1794 Expression *IdentifierExp::toLvalue(Scope *sc, Expression *e)
1796 #if 0
1797 tym = tybasic(e1->ET->Tty);
1798 if (!(tyscalar(tym) ||
1799 tym == TYstruct ||
1800 tym == TYarray && e->Eoper == TOKaddr))
1801 synerr(EM_lvalue); // lvalue expected
1802 #endif
1803 return this;
1806 /******************************** DollarExp **************************/
1808 DollarExp::DollarExp(Loc loc)
1809 : IdentifierExp(loc, Id::dollar)
1813 /******************************** DsymbolExp **************************/
1815 DsymbolExp::DsymbolExp(Loc loc, Dsymbol *s)
1816 : Expression(loc, TOKdsymbol, sizeof(DsymbolExp))
1818 this->s = s;
1821 Expression *DsymbolExp::semantic(Scope *sc)
1823 #if LOGSEMANTIC
1824 printf("DsymbolExp::semantic('%s')\n", s->toChars());
1825 #endif
1827 Lagain:
1828 EnumMember *em;
1829 Expression *e;
1830 VarDeclaration *v;
1831 FuncDeclaration *f;
1832 FuncLiteralDeclaration *fld;
1833 Declaration *d;
1834 ClassDeclaration *cd;
1835 ClassDeclaration *thiscd = NULL;
1836 Import *imp;
1837 Package *pkg;
1838 Type *t;
1840 //printf("DsymbolExp:: %p '%s' is a symbol\n", this, toChars());
1841 //printf("s = '%s', s->kind = '%s'\n", s->toChars(), s->kind());
1842 if (type)
1843 return this;
1844 if (!s->isFuncDeclaration()) // functions are checked after overloading
1845 checkDeprecated(sc, s);
1846 s = s->toAlias();
1847 //printf("s = '%s', s->kind = '%s', s->needThis() = %p\n", s->toChars(), s->kind(), s->needThis());
1848 if (!s->isFuncDeclaration())
1849 checkDeprecated(sc, s);
1851 if (sc->func)
1852 thiscd = sc->func->parent->isClassDeclaration();
1854 // BUG: This should happen after overload resolution for functions, not before
1855 if (s->needThis())
1857 if (hasThis(sc) /*&& !s->isFuncDeclaration()*/)
1859 // Supply an implicit 'this', as in
1860 // this.ident
1862 DotVarExp *de;
1864 de = new DotVarExp(loc, new ThisExp(loc), s->isDeclaration());
1865 return de->semantic(sc);
1869 em = s->isEnumMember();
1870 if (em)
1872 e = em->value->copy();
1873 e = e->semantic(sc);
1874 return e;
1876 v = s->isVarDeclaration();
1877 if (v)
1879 //printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars());
1880 if (!type)
1881 { type = v->type;
1882 if (!v->type)
1883 { error("forward reference of %s", v->toChars());
1884 type = Type::terror;
1887 if (v->isConst() && type->toBasetype()->ty != Tsarray)
1889 if (v->init)
1891 if (v->inuse)
1893 error("circular reference to '%s'", v->toChars());
1894 type = Type::tint32;
1895 return this;
1897 ExpInitializer *ei = v->init->isExpInitializer();
1898 if (ei)
1900 e = ei->exp->copy(); // make copy so we can change loc
1901 if (e->op == TOKstring || !e->type)
1902 e = e->semantic(sc);
1903 e = e->implicitCastTo(sc, type);
1904 e->loc = loc;
1905 return e;
1908 else
1910 e = type->defaultInit();
1911 e->loc = loc;
1912 return e;
1915 e = new VarExp(loc, v);
1916 e->type = type;
1917 e = e->semantic(sc);
1918 return e->deref();
1920 fld = s->isFuncLiteralDeclaration();
1921 if (fld)
1922 { //printf("'%s' is a function literal\n", fld->toChars());
1923 e = new FuncExp(loc, fld);
1924 return e->semantic(sc);
1926 f = s->isFuncDeclaration();
1927 if (f)
1928 { //printf("'%s' is a function\n", f->toChars());
1929 return new VarExp(loc, f);
1931 cd = s->isClassDeclaration();
1932 if (cd && thiscd && cd->isBaseOf(thiscd, NULL) && sc->func->needThis())
1934 // We need to add an implicit 'this' if cd is this class or a base class.
1935 DotTypeExp *dte;
1937 dte = new DotTypeExp(loc, new ThisExp(loc), s);
1938 return dte->semantic(sc);
1940 imp = s->isImport();
1941 if (imp)
1943 ScopeExp *ie;
1945 ie = new ScopeExp(loc, imp->pkg);
1946 return ie->semantic(sc);
1948 pkg = s->isPackage();
1949 if (pkg)
1951 ScopeExp *ie;
1953 ie = new ScopeExp(loc, pkg);
1954 return ie->semantic(sc);
1956 Module *mod = s->isModule();
1957 if (mod)
1959 ScopeExp *ie;
1961 ie = new ScopeExp(loc, mod);
1962 return ie->semantic(sc);
1965 t = s->getType();
1966 if (t)
1968 return new TypeExp(loc, t);
1971 TupleDeclaration *tup = s->isTupleDeclaration();
1972 if (tup)
1974 e = new TupleExp(loc, tup);
1975 e = e->semantic(sc);
1976 return e;
1979 TemplateInstance *ti = s->isTemplateInstance();
1980 if (ti && !global.errors)
1981 { if (!ti->semanticdone)
1982 ti->semantic(sc);
1983 s = ti->inst->toAlias();
1984 if (!s->isTemplateInstance())
1985 goto Lagain;
1986 e = new ScopeExp(loc, ti);
1987 e = e->semantic(sc);
1988 return e;
1991 TemplateDeclaration *td = s->isTemplateDeclaration();
1992 if (td)
1994 e = new TemplateExp(loc, td);
1995 e = e->semantic(sc);
1996 return e;
1999 Lerr:
2000 error("%s '%s' is not a variable", s->kind(), s->toChars());
2001 type = Type::terror;
2002 return this;
2005 char *DsymbolExp::toChars()
2007 return s->toChars();
2010 void DsymbolExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
2012 buf->writestring(s->toChars());
2015 Expression *DsymbolExp::toLvalue(Scope *sc, Expression *e)
2017 #if 0
2018 tym = tybasic(e1->ET->Tty);
2019 if (!(tyscalar(tym) ||
2020 tym == TYstruct ||
2021 tym == TYarray && e->Eoper == TOKaddr))
2022 synerr(EM_lvalue); // lvalue expected
2023 #endif
2024 return this;
2027 /******************************** GetLoggerExp **************************/
2029 GetLoggerExp::GetLoggerExp(Loc loc)
2030 : Expression(loc, TOKlogger, sizeof(GetLoggerExp))
2034 Expression *GetLoggerExp::semantic(Scope *sc)
2036 Expression *log = new DotIdExp(loc, new IdentifierExp(loc, Id::empty), Id::__log);
2037 return log->semantic(sc);
2040 void GetLoggerExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
2042 buf->writestring("log");
2045 /******************************** ThisExp **************************/
2047 ThisExp::ThisExp(Loc loc)
2048 : Expression(loc, TOKthis, sizeof(ThisExp))
2050 var = NULL;
2053 Expression *ThisExp::semantic(Scope *sc)
2054 { FuncDeclaration *fd;
2055 FuncDeclaration *fdthis;
2056 int nested = 0;
2058 #if LOGSEMANTIC
2059 printf("ThisExp::semantic()\n");
2060 #endif
2061 if (type)
2062 { //assert(global.errors || var);
2063 return this;
2066 /* Special case for typeof(this) and typeof(super) since both
2067 * should work even if they are not inside a non-static member function
2069 if (sc->intypeof)
2071 // Find enclosing struct or class
2072 for (Dsymbol *s = sc->parent; 1; s = s->parent)
2074 ClassDeclaration *cd;
2075 StructDeclaration *sd;
2077 if (!s)
2079 error("%s is not in a struct or class scope", toChars());
2080 goto Lerr;
2082 cd = s->isClassDeclaration();
2083 if (cd)
2085 type = cd->type;
2086 return this;
2088 sd = s->isStructDeclaration();
2089 if (sd)
2091 type = sd->type->pointerTo();
2092 return this;
2097 fdthis = sc->parent->isFuncDeclaration();
2098 fd = hasThis(sc); // fd is the uplevel function with the 'this' variable
2099 if (!fd)
2100 goto Lerr;
2102 assert(fd->vthis);
2103 var = fd->vthis;
2104 assert(var->parent);
2105 type = var->type;
2106 var->isVarDeclaration()->checkNestedReference(sc, loc);
2107 #if 0
2108 if (fd != fdthis) // if nested
2110 fdthis->getLevel(loc, fd);
2111 fd->vthis->nestedref = 1;
2112 fd->nestedFrameRef = 1;
2114 #endif
2115 sc->callSuper |= CSXthis;
2116 return this;
2118 Lerr:
2119 error("'this' is only allowed in non-static member functions, not %s", sc->parent->toChars());
2120 type = Type::tint32;
2121 return this;
2124 int ThisExp::isBool(int result)
2126 return result ? TRUE : FALSE;
2129 void ThisExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
2131 buf->writestring("this");
2134 Expression *ThisExp::toLvalue(Scope *sc, Expression *e)
2136 return this;
2139 /******************************** SuperExp **************************/
2141 SuperExp::SuperExp(Loc loc)
2142 : ThisExp(loc)
2144 op = TOKsuper;
2147 Expression *SuperExp::semantic(Scope *sc)
2148 { FuncDeclaration *fd;
2149 FuncDeclaration *fdthis;
2150 ClassDeclaration *cd;
2151 Dsymbol *s;
2153 #if LOGSEMANTIC
2154 printf("SuperExp::semantic('%s')\n", toChars());
2155 #endif
2156 if (type)
2157 return this;
2159 /* Special case for typeof(this) and typeof(super) since both
2160 * should work even if they are not inside a non-static member function
2162 if (sc->intypeof)
2164 // Find enclosing class
2165 for (Dsymbol *s = sc->parent; 1; s = s->parent)
2167 ClassDeclaration *cd;
2169 if (!s)
2171 error("%s is not in a class scope", toChars());
2172 goto Lerr;
2174 cd = s->isClassDeclaration();
2175 if (cd)
2177 cd = cd->baseClass;
2178 if (!cd)
2179 { error("class %s has no 'super'", s->toChars());
2180 goto Lerr;
2182 type = cd->type;
2183 return this;
2188 fdthis = sc->parent->isFuncDeclaration();
2189 fd = hasThis(sc);
2190 if (!fd)
2191 goto Lerr;
2192 assert(fd->vthis);
2193 var = fd->vthis;
2194 assert(var->parent);
2196 s = fd->toParent();
2197 while (s && s->isTemplateInstance())
2198 s = s->toParent();
2199 assert(s);
2200 cd = s->isClassDeclaration();
2201 //printf("parent is %s %s\n", fd->toParent()->kind(), fd->toParent()->toChars());
2202 if (!cd)
2203 goto Lerr;
2204 if (!cd->baseClass)
2206 error("no base class for %s", cd->toChars());
2207 type = fd->vthis->type;
2209 else
2211 type = cd->baseClass->type;
2214 var->isVarDeclaration()->checkNestedReference(sc, loc);
2215 #if 0
2216 if (fd != fdthis)
2218 fdthis->getLevel(loc, fd);
2219 fd->vthis->nestedref = 1;
2220 fd->nestedFrameRef = 1;
2222 #endif
2224 sc->callSuper |= CSXsuper;
2225 return this;
2228 Lerr:
2229 error("'super' is only allowed in non-static class member functions");
2230 type = Type::tint32;
2231 return this;
2234 void SuperExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
2236 buf->writestring("super");
2240 /******************************** NullExp **************************/
2242 NullExp::NullExp(Loc loc)
2243 : Expression(loc, TOKnull, sizeof(NullExp))
2245 committed = 0;
2248 Expression *NullExp::semantic(Scope *sc)
2250 #if LOGSEMANTIC
2251 printf("NullExp::semantic('%s')\n", toChars());
2252 #endif
2253 // NULL is the same as (void *)0
2254 if (!type)
2255 type = Type::tvoid->pointerTo();
2256 return this;
2259 int NullExp::isBool(int result)
2261 return result ? FALSE : TRUE;
2264 void NullExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
2266 buf->writestring("null");
2269 void NullExp::toMangleBuffer(OutBuffer *buf)
2271 buf->writeByte('n');
2274 /******************************** StringExp **************************/
2276 StringExp::StringExp(Loc loc, char *string)
2277 : Expression(loc, TOKstring, sizeof(StringExp))
2279 this->string = string;
2280 this->len = strlen(string);
2281 this->sz = 1;
2282 this->committed = 0;
2283 this->postfix = 0;
2286 StringExp::StringExp(Loc loc, void *string, size_t len)
2287 : Expression(loc, TOKstring, sizeof(StringExp))
2289 this->string = string;
2290 this->len = len;
2291 this->sz = 1;
2292 this->committed = 0;
2293 this->postfix = 0;
2296 StringExp::StringExp(Loc loc, void *string, size_t len, unsigned char postfix)
2297 : Expression(loc, TOKstring, sizeof(StringExp))
2299 this->string = string;
2300 this->len = len;
2301 this->sz = 1;
2302 this->committed = 0;
2303 this->postfix = postfix;
2306 #if 0
2307 Expression *StringExp::syntaxCopy()
2309 printf("StringExp::syntaxCopy() %s\n", toChars());
2310 return copy();
2312 #endif
2314 int StringExp::equals(Object *o)
2316 //printf("StringExp::equals('%s')\n", o->toChars());
2317 if (o && o->dyncast() == DYNCAST_EXPRESSION)
2318 { Expression *e = (Expression *)o;
2320 if (e->op == TOKstring)
2322 return compare(o) == 0;
2325 return FALSE;
2328 char *StringExp::toChars()
2330 OutBuffer buf;
2331 HdrGenState hgs;
2332 char *p;
2334 memset(&hgs, 0, sizeof(hgs));
2335 toCBuffer(&buf, &hgs);
2336 buf.writeByte(0);
2337 p = (char *)buf.data;
2338 buf.data = NULL;
2339 return p;
2342 Expression *StringExp::semantic(Scope *sc)
2344 #if LOGSEMANTIC
2345 printf("StringExp::semantic() %s\n", toChars());
2346 #endif
2347 if (!type)
2348 { OutBuffer buffer;
2349 size_t newlen = 0;
2350 char *p;
2351 size_t u;
2352 unsigned c;
2354 switch (postfix)
2356 case 'd':
2357 for (u = 0; u < len;)
2359 p = utf_decodeChar((unsigned char *)string, len, &u, &c);
2360 if (p)
2361 { error("%s", p);
2362 break;
2364 else
2365 { buffer.write4(c);
2366 newlen++;
2369 buffer.write4(0);
2370 string = buffer.extractData();
2371 len = newlen;
2372 sz = 4;
2373 type = new TypeSArray(Type::tdchar, new IntegerExp(loc, len, Type::tindex));
2374 committed = 1;
2375 break;
2377 case 'w':
2378 for (u = 0; u < len;)
2380 p = utf_decodeChar((unsigned char *)string, len, &u, &c);
2381 if (p)
2382 { error("%s", p);
2383 break;
2385 else
2386 { buffer.writeUTF16(c);
2387 newlen++;
2388 if (c >= 0x10000)
2389 newlen++;
2392 buffer.writeUTF16(0);
2393 string = buffer.extractData();
2394 len = newlen;
2395 sz = 2;
2396 type = new TypeSArray(Type::twchar, new IntegerExp(loc, len, Type::tindex));
2397 committed = 1;
2398 break;
2400 case 'c':
2401 committed = 1;
2402 default:
2403 type = new TypeSArray(Type::tchar, new IntegerExp(loc, len, Type::tindex));
2404 break;
2406 type = type->semantic(loc, sc);
2408 return this;
2411 /****************************************
2412 * Convert string to char[].
2415 StringExp *StringExp::toUTF8(Scope *sc)
2417 if (sz != 1)
2418 { // Convert to UTF-8 string
2419 committed = 0;
2420 Expression *e = castTo(sc, Type::tchar->arrayOf());
2421 e = e->optimize(WANTvalue);
2422 assert(e->op == TOKstring);
2423 StringExp *se = (StringExp *)e;
2424 assert(se->sz == 1);
2425 return se;
2427 return this;
2430 int StringExp::compare(Object *obj)
2432 // Used to sort case statement expressions so we can do an efficient lookup
2433 StringExp *se2 = (StringExp *)(obj);
2435 // This is a kludge so isExpression() in template.c will return 5
2436 // for StringExp's.
2437 if (!se2)
2438 return 5;
2440 assert(se2->op == TOKstring);
2442 int len1 = len;
2443 int len2 = se2->len;
2445 if (len1 == len2)
2447 switch (sz)
2449 case 1:
2450 return strcmp((char *)string, (char *)se2->string);
2452 case 2:
2453 { unsigned u;
2454 d_wchar *s1 = (d_wchar *)string;
2455 d_wchar *s2 = (d_wchar *)se2->string;
2457 for (u = 0; u < len; u++)
2459 if (s1[u] != s2[u])
2460 return s1[u] - s2[u];
2464 case 4:
2465 { unsigned u;
2466 d_dchar *s1 = (d_dchar *)string;
2467 d_dchar *s2 = (d_dchar *)se2->string;
2469 for (u = 0; u < len; u++)
2471 if (s1[u] != s2[u])
2472 return s1[u] - s2[u];
2475 break;
2477 default:
2478 assert(0);
2481 return len1 - len2;
2484 int StringExp::isBool(int result)
2486 return result ? TRUE : FALSE;
2489 unsigned StringExp::charAt(size_t i)
2490 { unsigned value;
2492 switch (sz)
2494 case 1:
2495 value = ((unsigned char *)string)[i];
2496 break;
2498 case 2:
2499 value = ((unsigned short *)string)[i];
2500 break;
2502 case 4:
2503 value = ((unsigned int *)string)[i];
2504 break;
2506 default:
2507 assert(0);
2508 break;
2510 return value;
2513 void StringExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
2515 buf->writeByte('"');
2516 for (size_t i = 0; i < len; i++)
2517 { unsigned c = charAt(i);
2519 switch (c)
2521 case '"':
2522 case '\\':
2523 if (!hgs->console)
2524 buf->writeByte('\\');
2525 default:
2526 if (c <= 0xFF)
2527 { if (c <= 0x7F && (isprint(c) || hgs->console))
2528 buf->writeByte(c);
2529 else
2530 buf->printf("\\x%02x", c);
2532 else if (c <= 0xFFFF)
2533 buf->printf("\\x%02x\\x%02x", c & 0xFF, c >> 8);
2534 else
2535 buf->printf("\\x%02x\\x%02x\\x%02x\\x%02x",
2536 c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF, c >> 24);
2537 break;
2540 buf->writeByte('"');
2541 if (postfix)
2542 buf->writeByte(postfix);
2545 void StringExp::toMangleBuffer(OutBuffer *buf)
2546 { char m;
2547 OutBuffer tmp;
2548 char *p;
2549 dchar_t c;
2550 size_t u;
2551 unsigned char *q;
2552 unsigned qlen;
2554 /* Write string in UTF-8 format
2556 switch (sz)
2557 { case 1:
2558 m = 'a';
2559 q = (unsigned char *)string;
2560 qlen = len;
2561 break;
2562 case 2:
2563 m = 'w';
2564 for (u = 0; u < len; )
2566 p = utf_decodeWchar((unsigned short *)string, len, &u, &c);
2567 if (p)
2568 error("%s", p);
2569 else
2570 tmp.writeUTF8(c);
2572 q = tmp.data;
2573 qlen = tmp.offset;
2574 break;
2575 case 4:
2576 m = 'd';
2577 for (u = 0; u < len; u++)
2579 c = ((unsigned *)string)[u];
2580 if (!utf_isValidDchar(c))
2581 error("invalid UCS-32 char \\U%08x", c);
2582 else
2583 tmp.writeUTF8(c);
2585 q = tmp.data;
2586 qlen = tmp.offset;
2587 break;
2588 default:
2589 assert(0);
2591 buf->writeByte(m);
2592 buf->printf("%d_", qlen);
2593 for (size_t i = 0; i < qlen; i++)
2594 buf->printf("%02x", q[i]);
2597 /************************ ArrayLiteralExp ************************************/
2599 // [ e1, e2, e3, ... ]
2601 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expressions *elements)
2602 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp))
2604 this->elements = elements;
2607 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expression *e)
2608 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp))
2610 elements = new Expressions;
2611 elements->push(e);
2614 Expression *ArrayLiteralExp::syntaxCopy()
2616 return new ArrayLiteralExp(loc, arraySyntaxCopy(elements));
2619 Expression *ArrayLiteralExp::semantic(Scope *sc)
2620 { Expression *e;
2621 Type *t0 = NULL;
2623 #if LOGSEMANTIC
2624 printf("ArrayLiteralExp::semantic('%s')\n", toChars());
2625 #endif
2626 if (type)
2627 return this;
2629 // Run semantic() on each element
2630 for (int i = 0; i < elements->dim; i++)
2631 { e = (Expression *)elements->data[i];
2632 e = e->semantic(sc);
2633 elements->data[i] = (void *)e;
2635 expandTuples(elements);
2636 for (int i = 0; i < elements->dim; i++)
2637 { e = (Expression *)elements->data[i];
2639 if (!e->type)
2640 error("%s has no value", e->toChars());
2641 e = resolveProperties(sc, e);
2643 unsigned char committed = 1;
2644 if (e->op == TOKstring)
2645 committed = ((StringExp *)e)->committed;
2647 if (!t0)
2648 { t0 = e->type;
2649 // Convert any static arrays to dynamic arrays
2650 if (t0->ty == Tsarray)
2652 t0 = t0->next->arrayOf();
2653 e = e->implicitCastTo(sc, t0);
2656 else
2657 e = e->implicitCastTo(sc, t0);
2658 if (!committed && e->op == TOKstring)
2659 { StringExp *se = (StringExp *)e;
2660 se->committed = 0;
2662 elements->data[i] = (void *)e;
2665 if (!t0)
2666 t0 = Type::tvoid;
2667 type = new TypeSArray(t0, new IntegerExp(elements->dim));
2668 type = type->semantic(loc, sc);
2669 return this;
2672 int ArrayLiteralExp::checkSideEffect(int flag)
2673 { int f = 0;
2675 for (size_t i = 0; i < elements->dim; i++)
2676 { Expression *e = (Expression *)elements->data[i];
2678 f |= e->checkSideEffect(2);
2680 if (flag == 0 && f == 0)
2681 Expression::checkSideEffect(0);
2682 return f;
2685 int ArrayLiteralExp::isBool(int result)
2687 size_t dim = elements ? elements->dim : 0;
2688 return result ? (dim != 0) : (dim == 0);
2691 void ArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
2693 buf->writeByte('[');
2694 argsToCBuffer(buf, elements, hgs);
2695 buf->writeByte(']');
2698 void ArrayLiteralExp::toMangleBuffer(OutBuffer *buf)
2700 size_t dim = elements ? elements->dim : 0;
2701 buf->printf("A%"PRIuSIZE, dim);
2702 for (size_t i = 0; i < dim; i++)
2703 { Expression *e = (Expression *)elements->data[i];
2704 e->toMangleBuffer(buf);
2708 /************************ AssocArrayLiteralExp ************************************/
2710 // [ key0 : value0, key1 : value1, ... ]
2712 AssocArrayLiteralExp::AssocArrayLiteralExp(Loc loc,
2713 Expressions *keys, Expressions *values)
2714 : Expression(loc, TOKassocarrayliteral, sizeof(AssocArrayLiteralExp))
2716 assert(keys->dim == values->dim);
2717 this->keys = keys;
2718 this->values = values;
2721 Expression *AssocArrayLiteralExp::syntaxCopy()
2723 return new AssocArrayLiteralExp(loc,
2724 arraySyntaxCopy(keys), arraySyntaxCopy(values));
2727 Expression *AssocArrayLiteralExp::semantic(Scope *sc)
2728 { Expression *e;
2729 Type *tkey = NULL;
2730 Type *tvalue = NULL;
2732 #if LOGSEMANTIC
2733 printf("AssocArrayLiteralExp::semantic('%s')\n", toChars());
2734 #endif
2736 // Run semantic() on each element
2737 for (size_t i = 0; i < keys->dim; i++)
2738 { Expression *key = (Expression *)keys->data[i];
2739 Expression *value = (Expression *)values->data[i];
2741 key = key->semantic(sc);
2742 value = value->semantic(sc);
2744 keys->data[i] = (void *)key;
2745 values->data[i] = (void *)value;
2747 expandTuples(keys);
2748 expandTuples(values);
2749 if (keys->dim != values->dim)
2751 error("number of keys is %u, must match number of values %u", keys->dim, values->dim);
2752 keys->setDim(0);
2753 values->setDim(0);
2755 for (size_t i = 0; i < keys->dim; i++)
2756 { Expression *key = (Expression *)keys->data[i];
2757 Expression *value = (Expression *)values->data[i];
2759 if (!key->type)
2760 error("%s has no value", key->toChars());
2761 if (!value->type)
2762 error("%s has no value", value->toChars());
2763 key = resolveProperties(sc, key);
2764 value = resolveProperties(sc, value);
2766 if (!tkey)
2767 tkey = key->type;
2768 else
2769 key = key->implicitCastTo(sc, tkey);
2770 keys->data[i] = (void *)key;
2772 if (!tvalue)
2773 tvalue = value->type;
2774 else
2775 value = value->implicitCastTo(sc, tvalue);
2776 values->data[i] = (void *)value;
2779 if (!tkey)
2780 tkey = Type::tvoid;
2781 if (!tvalue)
2782 tvalue = Type::tvoid;
2783 type = new TypeAArray(tvalue, tkey);
2784 type = type->semantic(loc, sc);
2785 return this;
2788 int AssocArrayLiteralExp::checkSideEffect(int flag)
2789 { int f = 0;
2791 for (size_t i = 0; i < keys->dim; i++)
2792 { Expression *key = (Expression *)keys->data[i];
2793 Expression *value = (Expression *)values->data[i];
2795 f |= key->checkSideEffect(2);
2796 f |= value->checkSideEffect(2);
2798 if (flag == 0 && f == 0)
2799 Expression::checkSideEffect(0);
2800 return f;
2803 int AssocArrayLiteralExp::isBool(int result)
2805 size_t dim = keys->dim;
2806 return result ? (dim != 0) : (dim == 0);
2809 void AssocArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
2811 buf->writeByte('[');
2812 for (size_t i = 0; i < keys->dim; i++)
2813 { Expression *key = (Expression *)keys->data[i];
2814 Expression *value = (Expression *)values->data[i];
2816 if (i)
2817 buf->writeByte(',');
2818 expToCBuffer(buf, hgs, key, PREC_assign);
2819 buf->writeByte(':');
2820 expToCBuffer(buf, hgs, value, PREC_assign);
2822 buf->writeByte(']');
2825 void AssocArrayLiteralExp::toMangleBuffer(OutBuffer *buf)
2827 size_t dim = keys->dim;
2828 buf->printf("A%u", dim);
2829 for (size_t i = 0; i < dim; i++)
2830 { Expression *key = (Expression *)keys->data[i];
2831 Expression *value = (Expression *)values->data[i];
2833 key->toMangleBuffer(buf);
2834 value->toMangleBuffer(buf);
2838 /************************ StructLiteralExp ************************************/
2840 // sd( e1, e2, e3, ... )
2842 StructLiteralExp::StructLiteralExp(Loc loc, StructDeclaration *sd, Expressions *elements)
2843 : Expression(loc, TOKstructliteral, sizeof(StructLiteralExp))
2845 this->sd = sd;
2846 this->elements = elements;
2847 this->sym = NULL;
2848 this->soffset = 0;
2849 this->fillHoles = 1;
2852 Expression *StructLiteralExp::syntaxCopy()
2854 return new StructLiteralExp(loc, sd, arraySyntaxCopy(elements));
2857 Expression *StructLiteralExp::semantic(Scope *sc)
2858 { Expression *e;
2860 #if LOGSEMANTIC
2861 printf("StructLiteralExp::semantic('%s')\n", toChars());
2862 #endif
2864 // Run semantic() on each element
2865 for (size_t i = 0; i < elements->dim; i++)
2866 { e = (Expression *)elements->data[i];
2867 if (!e)
2868 continue;
2869 e = e->semantic(sc);
2870 elements->data[i] = (void *)e;
2872 expandTuples(elements);
2873 size_t offset = 0;
2874 for (size_t i = 0; i < elements->dim; i++)
2875 { e = (Expression *)elements->data[i];
2876 if (!e)
2877 continue;
2879 if (!e->type)
2880 error("%s has no value", e->toChars());
2881 e = resolveProperties(sc, e);
2882 if (i >= sd->fields.dim)
2883 { error("more initializers than fields of %s", sd->toChars());
2884 break;
2886 Dsymbol *s = (Dsymbol *)sd->fields.data[i];
2887 VarDeclaration *v = s->isVarDeclaration();
2888 assert(v);
2889 if (v->offset < offset)
2890 error("overlapping initialization for %s", v->toChars());
2891 offset = v->offset + v->type->size();
2893 Type *telem = v->type;
2894 while (!e->implicitConvTo(telem) && telem->toBasetype()->ty == Tsarray)
2895 { /* Static array initialization, as in:
2896 * T[3][5] = e;
2898 telem = telem->toBasetype()->nextOf();
2901 e = e->implicitCastTo(sc, telem);
2903 elements->data[i] = (void *)e;
2906 /* Fill out remainder of elements[] with default initializers for fields[]
2908 for (size_t i = elements->dim; i < sd->fields.dim; i++)
2909 { Dsymbol *s = (Dsymbol *)sd->fields.data[i];
2910 VarDeclaration *v = s->isVarDeclaration();
2911 assert(v);
2913 if (v->offset < offset)
2914 { e = NULL;
2915 sd->hasUnions = 1;
2917 else
2919 if (v->init)
2920 { e = v->init->toExpression();
2921 if (!e)
2922 error("cannot make expression out of initializer for %s", v->toChars());
2924 else
2925 { e = v->type->defaultInit();
2926 e->loc = loc;
2928 offset = v->offset + v->type->size();
2930 elements->push(e);
2933 type = sd->type;
2934 return this;
2937 /**************************************
2938 * Gets expression at offset of type.
2939 * Returns NULL if not found.
2942 Expression *StructLiteralExp::getField(Type *type, unsigned offset)
2943 { Expression *e = NULL;
2944 int i = getFieldIndex(type, offset);
2946 if (i != -1)
2947 { e = (Expression *)elements->data[i];
2948 if (e)
2950 e = e->copy();
2951 e->type = type;
2954 return e;
2957 /************************************
2958 * Get index of field.
2959 * Returns -1 if not found.
2962 int StructLiteralExp::getFieldIndex(Type *type, unsigned offset)
2964 /* Find which field offset is by looking at the field offsets
2966 for (size_t i = 0; i < sd->fields.dim; i++)
2968 Dsymbol *s = (Dsymbol *)sd->fields.data[i];
2969 VarDeclaration *v = s->isVarDeclaration();
2970 assert(v);
2972 if (offset == v->offset &&
2973 type->size() == v->type->size())
2974 { Expression *e = (Expression *)elements->data[i];
2975 if (e)
2977 return i;
2979 break;
2982 return -1;
2986 Expression *StructLiteralExp::toLvalue(Scope *sc, Expression *e)
2988 return this;
2992 int StructLiteralExp::checkSideEffect(int flag)
2993 { int f = 0;
2995 for (size_t i = 0; i < elements->dim; i++)
2996 { Expression *e = (Expression *)elements->data[i];
2997 if (!e)
2998 continue;
3000 f |= e->checkSideEffect(2);
3002 if (flag == 0 && f == 0)
3003 Expression::checkSideEffect(0);
3004 return f;
3007 void StructLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3009 buf->writestring(sd->toChars());
3010 buf->writeByte('(');
3011 argsToCBuffer(buf, elements, hgs);
3012 buf->writeByte(')');
3015 void StructLiteralExp::toMangleBuffer(OutBuffer *buf)
3017 size_t dim = elements ? elements->dim : 0;
3018 buf->printf("S%u", dim);
3019 for (size_t i = 0; i < dim; i++)
3020 { Expression *e = (Expression *)elements->data[i];
3021 if (e)
3022 e->toMangleBuffer(buf);
3023 else
3024 buf->writeByte('v'); // 'v' for void
3028 /************************ TypeDotIdExp ************************************/
3030 /* Things like:
3031 * int.size
3032 * foo.size
3033 * (foo).size
3034 * cast(foo).size
3037 TypeDotIdExp::TypeDotIdExp(Loc loc, Type *type, Identifier *ident)
3038 : Expression(loc, TOKtypedot, sizeof(TypeDotIdExp))
3040 this->type = type;
3041 this->ident = ident;
3044 Expression *TypeDotIdExp::syntaxCopy()
3046 TypeDotIdExp *te = new TypeDotIdExp(loc, type->syntaxCopy(), ident);
3047 return te;
3050 Expression *TypeDotIdExp::semantic(Scope *sc)
3051 { Expression *e;
3053 #if LOGSEMANTIC
3054 printf("TypeDotIdExp::semantic()\n");
3055 #endif
3056 e = new DotIdExp(loc, new TypeExp(loc, type), ident);
3057 e = e->semantic(sc);
3058 return e;
3061 void TypeDotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3063 buf->writeByte('(');
3064 type->toCBuffer(buf, NULL, hgs);
3065 buf->writeByte(')');
3066 buf->writeByte('.');
3067 buf->writestring(ident->toChars());
3070 /************************************************************/
3072 // Mainly just a placeholder
3074 TypeExp::TypeExp(Loc loc, Type *type)
3075 : Expression(loc, TOKtype, sizeof(TypeExp))
3077 //printf("TypeExp::TypeExp(%s)\n", type->toChars());
3078 this->type = type;
3081 Expression *TypeExp::semantic(Scope *sc)
3083 //printf("TypeExp::semantic(%s)\n", type->toChars());
3084 type = type->semantic(loc, sc);
3085 return this;
3088 void TypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3090 type->toCBuffer(buf, NULL, hgs);
3093 /************************************************************/
3095 // Mainly just a placeholder
3097 ScopeExp::ScopeExp(Loc loc, ScopeDsymbol *pkg)
3098 : Expression(loc, TOKimport, sizeof(ScopeExp))
3100 //printf("ScopeExp::ScopeExp(pkg = '%s')\n", pkg->toChars());
3101 //static int count; if (++count == 38) *(char*)0=0;
3102 this->sds = pkg;
3105 Expression *ScopeExp::syntaxCopy()
3107 ScopeExp *se = new ScopeExp(loc, (ScopeDsymbol *)sds->syntaxCopy(NULL));
3108 return se;
3111 Expression *ScopeExp::semantic(Scope *sc)
3113 TemplateInstance *ti;
3114 ScopeDsymbol *sds2;
3116 #if LOGSEMANTIC
3117 printf("+ScopeExp::semantic('%s')\n", toChars());
3118 #endif
3119 Lagain:
3120 ti = sds->isTemplateInstance();
3121 if (ti && !global.errors)
3122 { Dsymbol *s;
3123 if (!ti->semanticdone)
3124 ti->semantic(sc);
3125 s = ti->inst->toAlias();
3126 sds2 = s->isScopeDsymbol();
3127 if (!sds2)
3128 { Expression *e;
3130 //printf("s = %s, '%s'\n", s->kind(), s->toChars());
3131 if (ti->withsym)
3133 // Same as wthis.s
3134 e = new VarExp(loc, ti->withsym->withstate->wthis);
3135 e = new DotVarExp(loc, e, s->isDeclaration());
3137 else
3138 e = new DsymbolExp(loc, s);
3139 e = e->semantic(sc);
3140 //printf("-1ScopeExp::semantic()\n");
3141 return e;
3143 if (sds2 != sds)
3145 sds = sds2;
3146 goto Lagain;
3148 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars());
3150 else
3152 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars());
3153 //printf("\tparent = '%s'\n", sds->parent->toChars());
3154 sds->semantic(sc);
3156 type = Type::tvoid;
3157 //printf("-2ScopeExp::semantic() %s\n", toChars());
3158 return this;
3161 void ScopeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3163 if (sds->isTemplateInstance())
3165 sds->toCBuffer(buf, hgs);
3167 else
3169 buf->writestring(sds->kind());
3170 buf->writestring(" ");
3171 buf->writestring(sds->toChars());
3175 /********************** TemplateExp **************************************/
3177 // Mainly just a placeholder
3179 TemplateExp::TemplateExp(Loc loc, TemplateDeclaration *td)
3180 : Expression(loc, TOKtemplate, sizeof(TemplateExp))
3182 //printf("TemplateExp(): %s\n", td->toChars());
3183 this->td = td;
3186 void TemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3188 buf->writestring(td->toChars());
3191 void TemplateExp::rvalue()
3193 error("template %s has no value", toChars());
3196 /********************** NewExp **************************************/
3198 NewExp::NewExp(Loc loc, Expression *thisexp, Expressions *newargs,
3199 Type *newtype, Expressions *arguments)
3200 : Expression(loc, TOKnew, sizeof(NewExp))
3202 this->thisexp = thisexp;
3203 this->newargs = newargs;
3204 this->newtype = newtype;
3205 this->arguments = arguments;
3206 member = NULL;
3207 allocator = NULL;
3208 onstack = 0;
3211 Expression *NewExp::syntaxCopy()
3213 return new NewExp(loc,
3214 thisexp ? thisexp->syntaxCopy() : NULL,
3215 arraySyntaxCopy(newargs),
3216 newtype->syntaxCopy(), arraySyntaxCopy(arguments));
3220 Expression *NewExp::semantic(Scope *sc)
3221 { int i;
3222 Type *tb;
3223 ClassDeclaration *cdthis = NULL;
3225 #if LOGSEMANTIC
3226 printf("NewExp::semantic() %s\n", toChars());
3227 if (thisexp)
3228 printf("\tthisexp = %s\n", thisexp->toChars());
3229 printf("\tnewtype: %s\n", newtype->toChars());
3230 #endif
3231 if (type) // if semantic() already run
3232 return this;
3234 Lagain:
3235 if (thisexp)
3236 { thisexp = thisexp->semantic(sc);
3237 cdthis = thisexp->type->isClassHandle();
3238 if (cdthis)
3240 sc = sc->push(cdthis);
3241 type = newtype->semantic(loc, sc);
3242 sc = sc->pop();
3244 else
3246 error("'this' for nested class must be a class type, not %s", thisexp->type->toChars());
3247 type = newtype->semantic(loc, sc);
3250 else
3251 type = newtype->semantic(loc, sc);
3252 newtype = type; // in case type gets cast to something else
3253 tb = type->toBasetype();
3254 //printf("tb: %s, deco = %s\n", tb->toChars(), tb->deco);
3256 arrayExpressionSemantic(newargs, sc);
3257 preFunctionArguments(loc, sc, newargs);
3258 arrayExpressionSemantic(arguments, sc);
3259 preFunctionArguments(loc, sc, arguments);
3261 if (thisexp && tb->ty != Tclass)
3262 error("e.new is only for allocating nested classes, not %s", tb->toChars());
3264 if (tb->ty == Tclass)
3265 { TypeFunction *tf;
3267 TypeClass *tc = (TypeClass *)(tb);
3268 ClassDeclaration *cd = tc->sym->isClassDeclaration();
3269 if (cd->isInterfaceDeclaration())
3270 error("cannot create instance of interface %s", cd->toChars());
3271 else if (cd->isAbstract())
3272 { error("cannot create instance of abstract class %s", cd->toChars());
3273 for (int i = 0; i < cd->vtbl.dim; i++)
3274 { FuncDeclaration *fd = ((Dsymbol *)cd->vtbl.data[i])->isFuncDeclaration();
3275 if (fd && fd->isAbstract())
3276 error("function %s is abstract", fd->toChars());
3279 checkDeprecated(sc, cd);
3280 if (cd->isNested())
3281 { /* We need a 'this' pointer for the nested class.
3282 * Ensure we have the right one.
3284 Dsymbol *s = cd->toParent2();
3285 ClassDeclaration *cdn = s->isClassDeclaration();
3287 //printf("isNested, cdn = %s\n", cdn ? cdn->toChars() : "null");
3288 if (cdn)
3290 if (!cdthis)
3292 // Supply an implicit 'this' and try again
3293 thisexp = new ThisExp(loc);
3294 for (Dsymbol *sp = sc->parent; 1; sp = sp->parent)
3295 { if (!sp)
3297 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars());
3298 break;
3300 ClassDeclaration *cdp = sp->isClassDeclaration();
3301 if (!cdp)
3302 continue;
3303 if (cdp == cdn || cdn->isBaseOf(cdp, NULL))
3304 break;
3305 // Add a '.outer' and try again
3306 thisexp = new DotIdExp(loc, thisexp, Id::outer);
3308 if (!global.errors)
3309 goto Lagain;
3311 if (cdthis)
3313 //printf("cdthis = %s\n", cdthis->toChars());
3314 if (cdthis != cdn && !cdn->isBaseOf(cdthis, NULL))
3315 error("'this' for nested class must be of type %s, not %s", cdn->toChars(), thisexp->type->toChars());
3317 #if 0
3318 else
3320 for (Dsymbol *sf = sc->func; 1; sf= sf->toParent2()->isFuncDeclaration())
3322 if (!sf)
3324 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars());
3325 break;
3327 printf("sf = %s\n", sf->toChars());
3328 AggregateDeclaration *ad = sf->isThis();
3329 if (ad && (ad == cdn || cdn->isBaseOf(ad->isClassDeclaration(), NULL)))
3330 break;
3333 #endif
3335 else if (thisexp)
3336 error("e.new is only for allocating nested classes");
3338 else if (thisexp)
3339 error("e.new is only for allocating nested classes");
3341 FuncDeclaration *f = cd->ctor;
3342 if (f)
3344 assert(f);
3345 f = f->overloadResolve(loc, arguments);
3346 checkDeprecated(sc, f);
3347 member = f->isCtorDeclaration();
3348 assert(member);
3350 cd->accessCheck(loc, sc, member);
3352 tf = (TypeFunction *)f->type;
3353 type = tf->next;
3355 if (!arguments)
3356 arguments = new Expressions();
3357 functionArguments(loc, sc, tf, arguments);
3359 else
3361 if (arguments && arguments->dim)
3362 error("no constructor for %s", cd->toChars());
3365 if (cd->aggNew)
3366 { Expression *e;
3368 f = cd->aggNew;
3370 // Prepend the uint size argument to newargs[]
3371 e = new IntegerExp(loc, cd->size(loc), Type::tsize_t);
3372 if (!newargs)
3373 newargs = new Expressions();
3374 newargs->shift(e);
3376 f = f->overloadResolve(loc, newargs);
3377 allocator = f->isNewDeclaration();
3378 assert(allocator);
3380 tf = (TypeFunction *)f->type;
3381 functionArguments(loc, sc, tf, newargs);
3383 else
3385 if (newargs && newargs->dim)
3386 error("no allocator for %s", cd->toChars());
3390 else if (tb->ty == Tstruct)
3392 TypeStruct *ts = (TypeStruct *)tb;
3393 StructDeclaration *sd = ts->sym;
3394 FuncDeclaration *f = sd->aggNew;
3395 TypeFunction *tf;
3397 if (arguments && arguments->dim)
3398 error("no constructor for %s", type->toChars());
3400 if (f)
3402 Expression *e;
3404 // Prepend the uint size argument to newargs[]
3405 e = new IntegerExp(loc, sd->size(loc), Type::tsize_t);
3406 if (!newargs)
3407 newargs = new Expressions();
3408 newargs->shift(e);
3410 f = f->overloadResolve(loc, newargs);
3411 allocator = f->isNewDeclaration();
3412 assert(allocator);
3414 tf = (TypeFunction *)f->type;
3415 functionArguments(loc, sc, tf, newargs);
3417 e = new VarExp(loc, f);
3418 e = new CallExp(loc, e, newargs);
3419 e = e->semantic(sc);
3420 e->type = type->pointerTo();
3421 return e;
3424 type = type->pointerTo();
3426 else if (tb->ty == Tarray && (arguments && arguments->dim))
3428 for (size_t i = 0; i < arguments->dim; i++)
3430 if (tb->ty != Tarray)
3431 { error("too many arguments for array");
3432 arguments->dim = i;
3433 break;
3436 Expression *arg = (Expression *)arguments->data[i];
3437 arg = resolveProperties(sc, arg);
3438 arg = arg->implicitCastTo(sc, Type::tsize_t);
3439 if (arg->op == TOKint64 && (long long)arg->toInteger() < 0)
3440 error("negative array index %s", arg->toChars());
3441 arguments->data[i] = (void *) arg;
3442 tb = tb->next->toBasetype();
3445 else if (tb->isscalar())
3447 if (arguments && arguments->dim)
3448 error("no constructor for %s", type->toChars());
3450 type = type->pointerTo();
3452 else
3454 error("new can only create structs, dynamic arrays or class objects, not %s's", type->toChars());
3455 type = type->pointerTo();
3458 //printf("NewExp: '%s'\n", toChars());
3459 //printf("NewExp:type '%s'\n", type->toChars());
3461 return this;
3464 int NewExp::checkSideEffect(int flag)
3466 return 1;
3469 void NewExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3470 { int i;
3472 if (thisexp)
3473 { expToCBuffer(buf, hgs, thisexp, PREC_primary);
3474 buf->writeByte('.');
3476 buf->writestring("new ");
3477 if (newargs && newargs->dim)
3479 buf->writeByte('(');
3480 argsToCBuffer(buf, newargs, hgs);
3481 buf->writeByte(')');
3483 newtype->toCBuffer(buf, NULL, hgs);
3484 if (arguments && arguments->dim)
3486 buf->writeByte('(');
3487 argsToCBuffer(buf, arguments, hgs);
3488 buf->writeByte(')');
3492 /********************** NewAnonClassExp **************************************/
3494 NewAnonClassExp::NewAnonClassExp(Loc loc, Expression *thisexp,
3495 Expressions *newargs, ClassDeclaration *cd, Expressions *arguments)
3496 : Expression(loc, TOKnewanonclass, sizeof(NewAnonClassExp))
3498 this->thisexp = thisexp;
3499 this->newargs = newargs;
3500 this->cd = cd;
3501 this->arguments = arguments;
3504 Expression *NewAnonClassExp::syntaxCopy()
3506 return new NewAnonClassExp(loc,
3507 thisexp ? thisexp->syntaxCopy() : NULL,
3508 arraySyntaxCopy(newargs),
3509 (ClassDeclaration *)cd->syntaxCopy(NULL),
3510 arraySyntaxCopy(arguments));
3514 Expression *NewAnonClassExp::semantic(Scope *sc)
3516 #if LOGSEMANTIC
3517 printf("NewAnonClassExp::semantic() %s\n", toChars());
3518 //printf("type: %s\n", type->toChars());
3519 #endif
3521 Expression *d = new DeclarationExp(loc, cd);
3522 d = d->semantic(sc);
3524 Expression *n = new NewExp(loc, thisexp, newargs, cd->type, arguments);
3526 Expression *c = new CommaExp(loc, d, n);
3527 return c->semantic(sc);
3530 int NewAnonClassExp::checkSideEffect(int flag)
3532 return 1;
3535 void NewAnonClassExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3536 { int i;
3538 if (thisexp)
3539 { expToCBuffer(buf, hgs, thisexp, PREC_primary);
3540 buf->writeByte('.');
3542 buf->writestring("new");
3543 if (newargs && newargs->dim)
3545 buf->writeByte('(');
3546 argsToCBuffer(buf, newargs, hgs);
3547 buf->writeByte(')');
3549 buf->writestring(" class ");
3550 if (arguments && arguments->dim)
3552 buf->writeByte('(');
3553 argsToCBuffer(buf, arguments, hgs);
3554 buf->writeByte(')');
3556 //buf->writestring(" { }");
3557 if (cd)
3559 cd->toCBuffer(buf, hgs);
3563 /********************** SymOffExp **************************************/
3565 SymOffExp::SymOffExp(Loc loc, Declaration *var, target_size_t offset)
3566 : Expression(loc, TOKsymoff, sizeof(SymOffExp))
3568 assert(var);
3569 this->var = var;
3570 this->offset = offset;
3571 VarDeclaration *v = var->isVarDeclaration();
3572 if (v && v->needThis())
3573 error("need 'this' for address of %s", v->toChars());
3576 Expression *SymOffExp::semantic(Scope *sc)
3578 #if LOGSEMANTIC
3579 printf("SymOffExp::semantic('%s')\n", toChars());
3580 #endif
3581 //var->semantic(sc);
3582 if (!type)
3583 type = var->type->pointerTo();
3584 VarDeclaration *v = var->isVarDeclaration();
3585 if (v)
3586 v->checkNestedReference(sc, loc);
3587 return this;
3590 int SymOffExp::isBool(int result)
3592 return result ? TRUE : FALSE;
3595 void SymOffExp::checkEscape()
3597 VarDeclaration *v = var->isVarDeclaration();
3598 if (v)
3600 if (!v->isDataseg())
3601 error("escaping reference to local variable %s", v->toChars());
3605 void SymOffExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3607 if (offset)
3608 buf->printf("(& %s+%"PRIuTSIZE")", var->toChars(), offset);
3609 else
3610 buf->printf("& %s", var->toChars());
3613 /******************************** VarExp **************************/
3615 VarExp::VarExp(Loc loc, Declaration *var)
3616 : Expression(loc, TOKvar, sizeof(VarExp))
3618 //printf("VarExp(this = %p, '%s')\n", this, var->toChars());
3619 this->var = var;
3620 this->type = var->type;
3623 int VarExp::equals(Object *o)
3624 { VarExp *ne;
3626 if (this == o ||
3627 (((Expression *)o)->op == TOKvar &&
3628 ((ne = (VarExp *)o), type->equals(ne->type)) &&
3629 var == ne->var))
3630 return 1;
3631 return 0;
3634 Expression *VarExp::semantic(Scope *sc)
3635 { FuncLiteralDeclaration *fd;
3637 #if LOGSEMANTIC
3638 printf("VarExp::semantic(%s)\n", toChars());
3639 #endif
3640 if (!type)
3641 { type = var->type;
3642 #if 0
3643 if (var->storage_class & STClazy)
3645 TypeFunction *tf = new TypeFunction(NULL, type, 0, LINKd);
3646 type = new TypeDelegate(tf);
3647 type = type->semantic(loc, sc);
3649 #endif
3652 VarDeclaration *v = var->isVarDeclaration();
3653 if (v)
3655 if (v->isConst() && type->toBasetype()->ty != Tsarray && v->init)
3657 ExpInitializer *ei = v->init->isExpInitializer();
3658 if (ei)
3660 //ei->exp->implicitCastTo(sc, type)->print();
3661 return ei->exp->implicitCastTo(sc, type);
3664 v->checkNestedReference(sc, loc);
3666 #if 0
3667 else if ((fd = var->isFuncLiteralDeclaration()) != NULL)
3668 { Expression *e;
3669 e = new FuncExp(loc, fd);
3670 e->type = type;
3671 return e;
3673 #endif
3674 return this;
3677 char *VarExp::toChars()
3679 return var->toChars();
3682 void VarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3684 buf->writestring(var->toChars());
3687 void VarExp::checkEscape()
3689 VarDeclaration *v = var->isVarDeclaration();
3690 if (v)
3691 { Type *tb = v->type->toBasetype();
3692 // if reference type
3693 if (tb->ty == Tarray || tb->ty == Tsarray || tb->ty == Tclass)
3695 if ((v->isAuto() || v->isScope()) && !v->noauto)
3696 error("escaping reference to auto local %s", v->toChars());
3697 else if (v->storage_class & STCvariadic)
3698 error("escaping reference to variadic parameter %s", v->toChars());
3703 Expression *VarExp::toLvalue(Scope *sc, Expression *e)
3705 #if 0
3706 tym = tybasic(e1->ET->Tty);
3707 if (!(tyscalar(tym) ||
3708 tym == TYstruct ||
3709 tym == TYarray && e->Eoper == TOKaddr))
3710 synerr(EM_lvalue); // lvalue expected
3711 #endif
3712 if (var->storage_class & STClazy)
3713 error("lazy variables cannot be lvalues");
3714 return this;
3717 Expression *VarExp::modifiableLvalue(Scope *sc, Expression *e)
3719 //printf("VarExp::modifiableLvalue('%s')\n", var->toChars());
3720 if (sc->incontract && var->isParameter())
3721 error("cannot modify parameter '%s' in contract", var->toChars());
3723 if (type && type->toBasetype()->ty == Tsarray)
3724 error("cannot change reference to static array '%s'", var->toChars());
3726 VarDeclaration *v = var->isVarDeclaration();
3727 if (v && v->canassign == 0 &&
3728 (var->isConst() || (global.params.Dversion > 1 && var->isFinal())))
3729 error("cannot modify final variable '%s'", var->toChars());
3731 if (var->isCtorinit())
3732 { // It's only modifiable if inside the right constructor
3733 Dsymbol *s = sc->func;
3734 while (1)
3736 FuncDeclaration *fd = NULL;
3737 if (s)
3738 fd = s->isFuncDeclaration();
3739 if (fd &&
3740 ((fd->isCtorDeclaration() && var->storage_class & STCfield) ||
3741 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) &&
3742 fd->toParent() == var->toParent()
3745 VarDeclaration *v = var->isVarDeclaration();
3746 assert(v);
3747 v->ctorinit = 1;
3748 //printf("setting ctorinit\n");
3750 else
3752 if (s)
3753 { s = s->toParent2();
3754 continue;
3756 else
3758 const char *p = var->isStatic() ? "static " : "";
3759 error("can only initialize %sconst %s inside %sconstructor",
3760 p, var->toChars(), p);
3763 break;
3767 // See if this expression is a modifiable lvalue (i.e. not const)
3768 return toLvalue(sc, e);
3772 /******************************** TupleExp **************************/
3774 TupleExp::TupleExp(Loc loc, Expressions *exps)
3775 : Expression(loc, TOKtuple, sizeof(TupleExp))
3777 //printf("TupleExp(this = %p)\n", this);
3778 this->exps = exps;
3779 this->type = NULL;
3783 TupleExp::TupleExp(Loc loc, TupleDeclaration *tup)
3784 : Expression(loc, TOKtuple, sizeof(TupleExp))
3786 exps = new Expressions();
3787 type = NULL;
3789 exps->reserve(tup->objects->dim);
3790 for (size_t i = 0; i < tup->objects->dim; i++)
3791 { Object *o = (Object *)tup->objects->data[i];
3792 if (o->dyncast() == DYNCAST_EXPRESSION)
3794 Expression *e = (Expression *)o;
3795 e = e->syntaxCopy();
3796 exps->push(e);
3798 else if (o->dyncast() == DYNCAST_DSYMBOL)
3800 Dsymbol *s = (Dsymbol *)o;
3801 Expression *e = new DsymbolExp(loc, s);
3802 exps->push(e);
3804 else if (o->dyncast() == DYNCAST_TYPE)
3806 Type *t = (Type *)o;
3807 Expression *e = new TypeExp(loc, t);
3808 exps->push(e);
3810 else
3812 error("%s is not an expression", o->toChars());
3817 int TupleExp::equals(Object *o)
3818 { TupleExp *ne;
3820 if (this == o)
3821 return 1;
3822 if (((Expression *)o)->op == TOKtuple)
3824 TupleExp *te = (TupleExp *)o;
3825 if (exps->dim != te->exps->dim)
3826 return 0;
3827 for (size_t i = 0; i < exps->dim; i++)
3828 { Expression *e1 = (Expression *)exps->data[i];
3829 Expression *e2 = (Expression *)te->exps->data[i];
3831 if (!e1->equals(e2))
3832 return 0;
3834 return 1;
3836 return 0;
3839 Expression *TupleExp::syntaxCopy()
3841 return new TupleExp(loc, arraySyntaxCopy(exps));
3844 Expression *TupleExp::semantic(Scope *sc)
3846 #if LOGSEMANTIC
3847 printf("+TupleExp::semantic(%s)\n", toChars());
3848 #endif
3849 if (type)
3850 return this;
3852 // Run semantic() on each argument
3853 for (size_t i = 0; i < exps->dim; i++)
3854 { Expression *e = (Expression *)exps->data[i];
3856 e = e->semantic(sc);
3857 if (!e->type)
3858 { error("%s has no value", e->toChars());
3859 e->type = Type::terror;
3861 exps->data[i] = (void *)e;
3864 expandTuples(exps);
3865 if (0 && exps->dim == 1)
3867 return (Expression *)exps->data[0];
3869 type = new TypeTuple(exps);
3870 //printf("-TupleExp::semantic(%s)\n", toChars());
3871 return this;
3874 void TupleExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3876 buf->writestring("tuple(");
3877 argsToCBuffer(buf, exps, hgs);
3878 buf->writeByte(')');
3881 int TupleExp::checkSideEffect(int flag)
3882 { int f = 0;
3884 for (int i = 0; i < exps->dim; i++)
3885 { Expression *e = (Expression *)exps->data[i];
3887 f |= e->checkSideEffect(2);
3889 if (flag == 0 && f == 0)
3890 Expression::checkSideEffect(0);
3891 return f;
3894 void TupleExp::checkEscape()
3896 for (size_t i = 0; i < exps->dim; i++)
3897 { Expression *e = (Expression *)exps->data[i];
3898 e->checkEscape();
3902 /******************************** FuncExp *********************************/
3904 FuncExp::FuncExp(Loc loc, FuncLiteralDeclaration *fd)
3905 : Expression(loc, TOKfunction, sizeof(FuncExp))
3907 this->fd = fd;
3910 Expression *FuncExp::syntaxCopy()
3912 return new FuncExp(loc, (FuncLiteralDeclaration *)fd->syntaxCopy(NULL));
3915 Expression *FuncExp::semantic(Scope *sc)
3917 #if LOGSEMANTIC
3918 printf("FuncExp::semantic(%s)\n", toChars());
3919 #endif
3920 if (!type)
3922 fd->semantic(sc);
3923 fd->parent = sc->parent;
3924 if (global.errors)
3926 if (!fd->type->next)
3927 fd->type->next = Type::terror;
3929 else
3931 fd->semantic2(sc);
3932 if (!global.errors)
3934 fd->semantic3(sc);
3936 if (!global.errors && global.params.useInline)
3937 fd->inlineScan();
3941 // Type is a "delegate to" or "pointer to" the function literal
3942 if (fd->isNested())
3944 type = new TypeDelegate(fd->type);
3945 type = type->semantic(loc, sc);
3947 else
3949 type = fd->type->pointerTo();
3952 return this;
3955 char *FuncExp::toChars()
3957 return fd->toChars();
3960 void FuncExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
3962 buf->writestring(fd->toChars());
3966 /******************************** DeclarationExp **************************/
3968 DeclarationExp::DeclarationExp(Loc loc, Dsymbol *declaration)
3969 : Expression(loc, TOKdeclaration, sizeof(DeclarationExp))
3971 this->declaration = declaration;
3974 Expression *DeclarationExp::syntaxCopy()
3976 return new DeclarationExp(loc, declaration->syntaxCopy(NULL));
3979 Expression *DeclarationExp::semantic(Scope *sc)
3981 if (type)
3982 return this;
3984 #if LOGSEMANTIC
3985 printf("DeclarationExp::semantic() %s\n", toChars());
3986 #endif
3988 /* This is here to support extern(linkage) declaration,
3989 * where the extern(linkage) winds up being an AttribDeclaration
3990 * wrapper.
3992 Dsymbol *s = declaration;
3994 AttribDeclaration *ad = declaration->isAttribDeclaration();
3995 if (ad)
3997 if (ad->decl && ad->decl->dim == 1)
3998 s = (Dsymbol *)ad->decl->data[0];
4001 if (s->isVarDeclaration())
4002 { // Do semantic() on initializer first, so:
4003 // int a = a;
4004 // will be illegal.
4005 declaration->semantic(sc);
4006 s->parent = sc->parent;
4009 //printf("inserting '%s' %p into sc = %p\n", s->toChars(), s, sc);
4010 // Insert into both local scope and function scope.
4011 // Must be unique in both.
4012 if (s->ident)
4014 if (!sc->insert(s))
4015 error("declaration %s is already defined", s->toPrettyChars());
4016 else if (sc->func)
4017 { VarDeclaration *v = s->isVarDeclaration();
4018 if ((s->isFuncDeclaration() /*|| v && v->storage_class & STCstatic*/) &&
4019 !sc->func->localsymtab->insert(s))
4020 error("declaration %s is already defined in another scope in %s", s->toPrettyChars(), sc->func->toChars());
4021 else if (!global.params.useDeprecated)
4022 { // Disallow shadowing
4024 for (Scope *scx = sc->enclosing; scx && scx->func == sc->func; scx = scx->enclosing)
4025 { Dsymbol *s2;
4027 if (scx->scopesym && scx->scopesym->symtab &&
4028 (s2 = scx->scopesym->symtab->lookup(s->ident)) != NULL &&
4029 s != s2)
4031 error("shadowing declaration %s is deprecated", s->toPrettyChars());
4037 if (!s->isVarDeclaration())
4039 declaration->semantic(sc);
4040 s->parent = sc->parent;
4042 if (!global.errors)
4044 declaration->semantic2(sc);
4045 if (!global.errors)
4047 declaration->semantic3(sc);
4049 if (!global.errors && global.params.useInline)
4050 declaration->inlineScan();
4054 type = Type::tvoid;
4055 return this;
4058 int DeclarationExp::checkSideEffect(int flag)
4060 return 1;
4063 void DeclarationExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
4065 declaration->toCBuffer(buf, hgs);
4069 /************************ TypeidExp ************************************/
4072 * typeid(int)
4075 TypeidExp::TypeidExp(Loc loc, Type *typeidType)
4076 : Expression(loc, TOKtypeid, sizeof(TypeidExp))
4078 this->typeidType = typeidType;
4082 Expression *TypeidExp::syntaxCopy()
4084 return new TypeidExp(loc, typeidType->syntaxCopy());
4088 Expression *TypeidExp::semantic(Scope *sc)
4089 { Expression *e;
4091 #if LOGSEMANTIC
4092 printf("TypeidExp::semantic()\n");
4093 #endif
4094 typeidType = typeidType->semantic(loc, sc);
4095 e = typeidType->getTypeInfo(sc);
4096 return e;
4099 void TypeidExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
4101 buf->writestring("typeid(");
4102 typeidType->toCBuffer(buf, NULL, hgs);
4103 buf->writeByte(')');
4106 /************************************************************/
4108 HaltExp::HaltExp(Loc loc)
4109 : Expression(loc, TOKhalt, sizeof(HaltExp))
4113 Expression *HaltExp::semantic(Scope *sc)
4115 #if LOGSEMANTIC
4116 printf("HaltExp::semantic()\n");
4117 #endif
4118 type = Type::tvoid;
4119 return this;
4122 int HaltExp::checkSideEffect(int flag)
4124 return 1;
4127 void HaltExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
4129 buf->writestring("halt");
4132 /************************************************************/
4134 IsExp::IsExp(Loc loc, Type *targ, Identifier *id, enum TOK tok,
4135 Type *tspec, enum TOK tok2)
4136 : Expression(loc, TOKis, sizeof(IsExp))
4138 this->targ = targ;
4139 this->id = id;
4140 this->tok = tok;
4141 this->tspec = tspec;
4142 this->tok2 = tok2;
4145 Expression *IsExp::syntaxCopy()
4147 return new IsExp(loc,
4148 targ->syntaxCopy(),
4150 tok,
4151 tspec ? tspec->syntaxCopy() : NULL,
4152 tok2);
4155 Expression *IsExp::semantic(Scope *sc)
4156 { Type *tded;
4158 //printf("IsExp::semantic()\n");
4159 if (id && !(sc->flags & SCOPEstaticif))
4160 error("can only declare type aliases within static if conditionals");
4162 unsigned errors_save = global.errors;
4163 global.errors = 0;
4164 global.gag++; // suppress printing of error messages
4165 targ = targ->semantic(loc, sc);
4166 global.gag--;
4167 unsigned gerrors = global.errors;
4168 global.errors = errors_save;
4170 if (gerrors) // if any errors happened
4171 { // then condition is false
4172 goto Lno;
4174 else if (tok2 != TOKreserved)
4176 switch (tok2)
4178 case TOKtypedef:
4179 if (targ->ty != Ttypedef)
4180 goto Lno;
4181 tded = ((TypeTypedef *)targ)->sym->basetype;
4182 break;
4184 case TOKstruct:
4185 if (targ->ty != Tstruct)
4186 goto Lno;
4187 if (((TypeStruct *)targ)->sym->isUnionDeclaration())
4188 goto Lno;
4189 tded = targ;
4190 break;
4192 case TOKunion:
4193 if (targ->ty != Tstruct)
4194 goto Lno;
4195 if (!((TypeStruct *)targ)->sym->isUnionDeclaration())
4196 goto Lno;
4197 tded = targ;
4198 break;
4200 case TOKclass:
4201 if (targ->ty != Tclass)
4202 goto Lno;
4203 if (((TypeClass *)targ)->sym->isInterfaceDeclaration())
4204 goto Lno;
4205 tded = targ;
4206 break;
4208 case TOKinterface:
4209 if (targ->ty != Tclass)
4210 goto Lno;
4211 if (!((TypeClass *)targ)->sym->isInterfaceDeclaration())
4212 goto Lno;
4213 tded = targ;
4214 break;
4216 case TOKsuper:
4217 // If class or interface, get the base class and interfaces
4218 if (targ->ty != Tclass)
4219 goto Lno;
4220 else
4221 { ClassDeclaration *cd = ((TypeClass *)targ)->sym;
4222 Arguments *args = new Arguments;
4223 args->reserve(cd->baseclasses.dim);
4224 for (size_t i = 0; i < cd->baseclasses.dim; i++)
4225 { BaseClass *b = (BaseClass *)cd->baseclasses.data[i];
4226 args->push(new Argument(STCin, b->type, NULL, NULL));
4228 tded = new TypeTuple(args);
4230 break;
4232 case TOKenum:
4233 if (targ->ty != Tenum)
4234 goto Lno;
4235 tded = ((TypeEnum *)targ)->sym->memtype;
4236 break;
4238 case TOKdelegate:
4239 if (targ->ty != Tdelegate)
4240 goto Lno;
4241 tded = targ->next; // the underlying function type
4242 break;
4244 case TOKfunction:
4245 { if (targ->ty != Tfunction)
4246 goto Lno;
4247 tded = targ;
4249 /* Generate tuple from function parameter types.
4251 assert(tded->ty == Tfunction);
4252 Arguments *params = ((TypeFunction *)tded)->parameters;
4253 size_t dim = Argument::dim(params);
4254 Arguments *args = new Arguments;
4255 args->reserve(dim);
4256 for (size_t i = 0; i < dim; i++)
4257 { Argument *arg = Argument::getNth(params, i);
4258 assert(arg && arg->type);
4259 args->push(new Argument(arg->storageClass, arg->type, NULL, NULL));
4261 tded = new TypeTuple(args);
4262 break;
4264 case TOKreturn:
4265 /* Get the 'return type' for the function,
4266 * delegate, or pointer to function.
4268 if (targ->ty == Tfunction)
4269 tded = targ->next;
4270 else if (targ->ty == Tdelegate)
4271 tded = targ->next->next;
4272 else if (targ->ty == Tpointer && targ->next->ty == Tfunction)
4273 tded = targ->next->next;
4274 else
4275 goto Lno;
4276 break;
4278 default:
4279 assert(0);
4281 goto Lyes;
4283 else if (id && tspec)
4285 /* Evaluate to TRUE if targ matches tspec.
4286 * If TRUE, declare id as an alias for the specialized type.
4289 MATCH m;
4290 TemplateTypeParameter tp(loc, id, NULL, NULL);
4292 TemplateParameters parameters;
4293 parameters.setDim(1);
4294 parameters.data[0] = (void *)&tp;
4296 Objects dedtypes;
4297 dedtypes.setDim(1);
4298 dedtypes.data[0] = NULL;
4300 m = targ->deduceType(NULL, tspec, &parameters, &dedtypes);
4301 if (m == MATCHnomatch ||
4302 (m != MATCHexact && tok == TOKequal))
4303 goto Lno;
4304 else
4306 assert(dedtypes.dim == 1);
4307 tded = (Type *)dedtypes.data[0];
4308 if (!tded)
4309 tded = targ;
4310 goto Lyes;
4313 else if (id)
4315 /* Declare id as an alias for type targ. Evaluate to TRUE
4317 tded = targ;
4318 goto Lyes;
4320 else if (tspec)
4322 /* Evaluate to TRUE if targ matches tspec
4324 tspec = tspec->semantic(loc, sc);
4325 //printf("targ = %s\n", targ->toChars());
4326 //printf("tspec = %s\n", tspec->toChars());
4327 if (tok == TOKcolon)
4328 { if (targ->implicitConvTo(tspec))
4329 goto Lyes;
4330 else
4331 goto Lno;
4333 else /* == */
4334 { if (targ->equals(tspec))
4335 goto Lyes;
4336 else
4337 goto Lno;
4341 Lyes:
4342 if (id)
4344 Dsymbol *s = new AliasDeclaration(loc, id, tded);
4345 s->semantic(sc);
4346 sc->insert(s);
4347 if (sc->sd)
4348 s->addMember(sc, sc->sd, 1);
4350 return new IntegerExp(1);
4352 Lno:
4353 return new IntegerExp(0);
4356 void IsExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
4358 buf->writestring("is(");
4359 targ->toCBuffer(buf, id, hgs);
4360 if (tok2 != TOKreserved)
4362 buf->printf(" %s %s", Token::toChars(tok), Token::toChars(tok2));
4364 else if (tspec)
4366 if (tok == TOKcolon)
4367 buf->writestring(" : ");
4368 else
4369 buf->writestring(" == ");
4370 tspec->toCBuffer(buf, NULL, hgs);
4372 #if V2
4373 if (parameters)
4374 { // First parameter is already output, so start with second
4375 for (int i = 1; i < parameters->dim; i++)
4377 buf->writeByte(',');
4378 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
4379 tp->toCBuffer(buf, hgs);
4382 #endif
4383 buf->writeByte(')');
4387 /************************************************************/
4389 UnaExp::UnaExp(Loc loc, enum TOK op, int size, Expression *e1)
4390 : Expression(loc, op, size)
4392 this->e1 = e1;
4395 Expression *UnaExp::syntaxCopy()
4396 { UnaExp *e;
4398 e = (UnaExp *)copy();
4399 e->type = NULL;
4400 e->e1 = e->e1->syntaxCopy();
4401 return e;
4404 Expression *UnaExp::semantic(Scope *sc)
4406 #if LOGSEMANTIC
4407 printf("UnaExp::semantic('%s')\n", toChars());
4408 #endif
4409 e1 = e1->semantic(sc);
4410 // if (!e1->type)
4411 // error("%s has no value", e1->toChars());
4412 return this;
4415 void UnaExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
4417 buf->writestring(Token::toChars(op));
4418 expToCBuffer(buf, hgs, e1, precedence[op]);
4421 /************************************************************/
4423 BinExp::BinExp(Loc loc, enum TOK op, int size, Expression *e1, Expression *e2)
4424 : Expression(loc, op, size)
4426 this->e1 = e1;
4427 this->e2 = e2;
4430 Expression *BinExp::syntaxCopy()
4431 { BinExp *e;
4433 e = (BinExp *)copy();
4434 e->type = NULL;
4435 e->e1 = e->e1->syntaxCopy();
4436 e->e2 = e->e2->syntaxCopy();
4437 return e;
4440 Expression *BinExp::semantic(Scope *sc)
4442 #if LOGSEMANTIC
4443 printf("BinExp::semantic('%s')\n", toChars());
4444 #endif
4445 e1 = e1->semantic(sc);
4446 if (!e1->type)
4448 error("%s has no value", e1->toChars());
4449 e1->type = Type::terror;
4451 e2 = e2->semantic(sc);
4452 if (!e2->type)
4454 error("%s has no value", e2->toChars());
4455 e2->type = Type::terror;
4457 assert(e1->type);
4458 return this;
4461 Expression *BinExp::semanticp(Scope *sc)
4463 BinExp::semantic(sc);
4464 e1 = resolveProperties(sc, e1);
4465 e2 = resolveProperties(sc, e2);
4466 return this;
4469 /***************************
4470 * Common semantic routine for some xxxAssignExp's.
4473 Expression *BinExp::commonSemanticAssign(Scope *sc)
4474 { Expression *e;
4476 if (!type)
4478 BinExp::semantic(sc);
4479 e2 = resolveProperties(sc, e2);
4481 e = op_overload(sc);
4482 if (e)
4483 return e;
4485 e1 = e1->modifiableLvalue(sc, e1);
4486 e1->checkScalar();
4487 type = e1->type;
4488 if (type->toBasetype()->ty == Tbool)
4490 error("operator not allowed on bool expression %s", toChars());
4492 typeCombine(sc);
4493 e1->checkArithmetic();
4494 e2->checkArithmetic();
4496 if (op == TOKmodass && e2->type->iscomplex())
4497 { error("cannot perform modulo complex arithmetic");
4498 return new IntegerExp(0);
4501 return this;
4504 Expression *BinExp::commonSemanticAssignIntegral(Scope *sc)
4505 { Expression *e;
4507 if (!type)
4509 BinExp::semantic(sc);
4510 e2 = resolveProperties(sc, e2);
4512 e = op_overload(sc);
4513 if (e)
4514 return e;
4516 e1 = e1->modifiableLvalue(sc, e1);
4517 e1->checkScalar();
4518 type = e1->type;
4519 if (type->toBasetype()->ty == Tbool)
4521 e2 = e2->implicitCastTo(sc, type);
4524 typeCombine(sc);
4525 e1->checkIntegral();
4526 e2->checkIntegral();
4528 return this;
4531 int BinExp::checkSideEffect(int flag)
4533 if (op == TOKplusplus ||
4534 op == TOKminusminus ||
4535 op == TOKassign ||
4536 op == TOKconstruct ||
4537 op == TOKblit ||
4538 op == TOKaddass ||
4539 op == TOKminass ||
4540 op == TOKcatass ||
4541 op == TOKmulass ||
4542 op == TOKdivass ||
4543 op == TOKmodass ||
4544 op == TOKshlass ||
4545 op == TOKshrass ||
4546 op == TOKushrass ||
4547 op == TOKandass ||
4548 op == TOKorass ||
4549 op == TOKxorass ||
4550 op == TOKin ||
4551 op == TOKremove)
4552 return 1;
4553 return Expression::checkSideEffect(flag);
4556 void BinExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
4558 expToCBuffer(buf, hgs, e1, precedence[op]);
4559 buf->writeByte(' ');
4560 buf->writestring(Token::toChars(op));
4561 buf->writeByte(' ');
4562 expToCBuffer(buf, hgs, e2, (enum PREC)(precedence[op] + 1));
4565 int BinExp::isunsigned()
4567 return e1->type->isunsigned() || e2->type->isunsigned();
4570 void BinExp::incompatibleTypes()
4572 error("incompatible types for ((%s) %s (%s)): '%s' and '%s'",
4573 e1->toChars(), Token::toChars(op), e2->toChars(),
4574 e1->type->toChars(), e2->type->toChars());
4577 /************************************************************/
4579 CompileExp::CompileExp(Loc loc, Expression *e)
4580 : UnaExp(loc, TOKmixin, sizeof(CompileExp), e)
4584 Expression *CompileExp::semantic(Scope *sc)
4586 #if LOGSEMANTIC
4587 printf("CompileExp::semantic('%s')\n", toChars());
4588 #endif
4589 UnaExp::semantic(sc);
4590 e1 = resolveProperties(sc, e1);
4591 e1 = e1->optimize(WANTvalue | WANTinterpret);
4592 if (e1->op != TOKstring)
4593 { error("argument to mixin must be a string, not (%s)", e1->toChars());
4594 type = Type::terror;
4595 return this;
4597 StringExp *se = (StringExp *)e1;
4598 se = se->toUTF8(sc);
4599 Parser p(sc->module, (unsigned char *)se->string, se->len, 0);
4600 p.loc = loc;
4601 p.nextToken();
4602 Expression *e = p.parseExpression();
4603 if (p.token.value != TOKeof)
4604 error("incomplete mixin expression (%s)", se->toChars());
4605 return e->semantic(sc);
4608 void CompileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
4610 buf->writestring("mixin(");
4611 expToCBuffer(buf, hgs, e1, PREC_assign);
4612 buf->writeByte(')');
4615 /************************************************************/
4617 FileExp::FileExp(Loc loc, Expression *e)
4618 : UnaExp(loc, TOKmixin, sizeof(FileExp), e)
4622 Expression *FileExp::semantic(Scope *sc)
4623 { char *name;
4624 StringExp *se;
4626 #if LOGSEMANTIC
4627 printf("FileExp::semantic('%s')\n", toChars());
4628 #endif
4629 UnaExp::semantic(sc);
4630 e1 = resolveProperties(sc, e1);
4631 e1 = e1->optimize(WANTvalue);
4632 if (e1->op != TOKstring)
4633 { error("file name argument must be a string, not (%s)", e1->toChars());
4634 goto Lerror;
4636 se = (StringExp *)e1;
4637 se = se->toUTF8(sc);
4638 name = (char *)se->string;
4640 if (!global.params.fileImppath)
4641 { error("need -Jpath switch to import text file %s", name);
4642 goto Lerror;
4645 if (name != FileName::name(name))
4646 { error("use -Jpath switch to provide path for filename %s", name);
4647 goto Lerror;
4650 name = FileName::searchPath(global.filePath, name, 0);
4651 if (!name)
4652 { error("file %s cannot be found, check -Jpath", se->toChars());
4653 goto Lerror;
4656 if (global.params.verbose)
4657 printf("file %s\t(%s)\n", se->string, name);
4659 { File f(name);
4660 if (f.read())
4661 { error("cannot read file %s", f.toChars());
4662 goto Lerror;
4664 else
4666 f.ref = 1;
4667 se = new StringExp(loc, f.buffer, f.len);
4670 Lret:
4671 return se->semantic(sc);
4673 Lerror:
4674 se = new StringExp(loc, "");
4675 goto Lret;
4678 void FileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
4680 buf->writestring("import(");
4681 expToCBuffer(buf, hgs, e1, PREC_assign);
4682 buf->writeByte(')');
4685 /************************************************************/
4687 AssertExp::AssertExp(Loc loc, Expression *e, Expression *msg)
4688 : UnaExp(loc, TOKassert, sizeof(AssertExp), e)
4690 this->msg = msg;
4693 Expression *AssertExp::syntaxCopy()
4695 AssertExp *ae = new AssertExp(loc, e1->syntaxCopy(),
4696 msg ? msg->syntaxCopy() : NULL);
4697 return ae;
4700 Expression *AssertExp::semantic(Scope *sc)
4702 #if LOGSEMANTIC
4703 printf("AssertExp::semantic('%s')\n", toChars());
4704 #endif
4705 UnaExp::semantic(sc);
4706 e1 = resolveProperties(sc, e1);
4707 // BUG: see if we can do compile time elimination of the Assert
4708 e1 = e1->optimize(WANTvalue);
4709 e1 = e1->checkToBoolean();
4710 if (msg)
4712 msg = msg->semantic(sc);
4713 msg = resolveProperties(sc, msg);
4714 msg = msg->implicitCastTo(sc, Type::tchar->arrayOf());
4715 msg = msg->optimize(WANTvalue);
4717 if (e1->isBool(FALSE))
4719 FuncDeclaration *fd = sc->parent->isFuncDeclaration();
4720 fd->hasReturnExp |= 4;
4722 if (!global.params.useAssert)
4723 { Expression *e = new HaltExp(loc);
4724 e = e->semantic(sc);
4725 return e;
4728 type = Type::tvoid;
4729 return this;
4732 int AssertExp::checkSideEffect(int flag)
4734 return 1;
4737 void AssertExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
4739 buf->writestring("assert(");
4740 expToCBuffer(buf, hgs, e1, PREC_assign);
4741 if (msg)
4743 buf->writeByte(',');
4744 expToCBuffer(buf, hgs, msg, PREC_assign);
4746 buf->writeByte(')');
4749 /************************************************************/
4751 DotIdExp::DotIdExp(Loc loc, Expression *e, Identifier *ident)
4752 : UnaExp(loc, TOKdot, sizeof(DotIdExp), e)
4754 this->ident = ident;
4757 Expression *DotIdExp::semantic(Scope *sc)
4758 { Expression *e;
4759 Expression *eleft;
4760 Expression *eright;
4762 #if LOGSEMANTIC
4763 printf("DotIdExp::semantic(this = %p, '%s')\n", this, toChars());
4764 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op));
4765 #endif
4767 //{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; }
4769 #if 0
4770 /* Don't do semantic analysis if we'll be converting
4771 * it to a string.
4773 if (ident == Id::stringof)
4774 { char *s = e1->toChars();
4775 e = new StringExp(loc, s, strlen(s), 'c');
4776 e = e->semantic(sc);
4777 return e;
4779 #endif
4781 /* Special case: rewrite this.id and super.id
4782 * to be classtype.id and baseclasstype.id
4783 * if we have no this pointer.
4785 if ((e1->op == TOKthis || e1->op == TOKsuper) && !hasThis(sc))
4786 { ClassDeclaration *cd;
4787 StructDeclaration *sd;
4788 AggregateDeclaration *ad;
4790 ad = sc->getStructClassScope();
4791 if (ad)
4793 cd = ad->isClassDeclaration();
4794 if (cd)
4796 if (e1->op == TOKthis)
4798 e = new TypeDotIdExp(loc, cd->type, ident);
4799 return e->semantic(sc);
4801 else if (cd->baseClass && e1->op == TOKsuper)
4803 e = new TypeDotIdExp(loc, cd->baseClass->type, ident);
4804 return e->semantic(sc);
4807 else
4809 sd = ad->isStructDeclaration();
4810 if (sd)
4812 if (e1->op == TOKthis)
4814 e = new TypeDotIdExp(loc, sd->type, ident);
4815 return e->semantic(sc);
4822 UnaExp::semantic(sc);
4824 if (e1->op == TOKdotexp)
4826 DotExp *de = (DotExp *)e1;
4827 eleft = de->e1;
4828 eright = de->e2;
4830 else
4832 e1 = resolveProperties(sc, e1);
4833 eleft = NULL;
4834 eright = e1;
4837 if (e1->op == TOKtuple && ident == Id::length)
4839 TupleExp *te = (TupleExp *)e1;
4840 e = new IntegerExp(loc, te->exps->dim, Type::tsize_t);
4841 return e;
4844 if (eright->op == TOKimport) // also used for template alias's
4846 Dsymbol *s;
4847 ScopeExp *ie = (ScopeExp *)eright;
4849 s = ie->sds->search(loc, ident, 0);
4850 if (s)
4852 s = s->toAlias();
4853 checkDeprecated(sc, s);
4855 EnumMember *em = s->isEnumMember();
4856 if (em)
4858 e = em->value;
4859 e = e->semantic(sc);
4860 return e;
4863 VarDeclaration *v = s->isVarDeclaration();
4864 if (v)
4866 //printf("DotIdExp:: Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars());
4867 if (v->inuse)
4869 error("circular reference to '%s'", v->toChars());
4870 type = Type::tint32;
4871 return this;
4873 type = v->type;
4874 if (v->isConst())
4876 if (v->init)
4878 ExpInitializer *ei = v->init->isExpInitializer();
4879 if (ei)
4881 //printf("\tei: %p (%s)\n", ei->exp, ei->exp->toChars());
4882 //ei->exp = ei->exp->semantic(sc);
4883 if (ei->exp->type == type)
4885 e = ei->exp->copy(); // make copy so we can change loc
4886 e->loc = loc;
4887 return e;
4891 else if (type->isscalar())
4893 e = type->defaultInit();
4894 e->loc = loc;
4895 return e;
4898 if (v->needThis())
4900 if (!eleft)
4901 eleft = new ThisExp(loc);
4902 e = new DotVarExp(loc, eleft, v);
4903 e = e->semantic(sc);
4905 else
4907 e = new VarExp(loc, v);
4908 if (eleft)
4909 { e = new CommaExp(loc, eleft, e);
4910 e->type = v->type;
4913 return e->deref();
4916 FuncDeclaration *f = s->isFuncDeclaration();
4917 if (f)
4919 //printf("it's a function\n");
4920 if (f->needThis())
4922 if (!eleft)
4923 eleft = new ThisExp(loc);
4924 e = new DotVarExp(loc, eleft, f);
4925 e = e->semantic(sc);
4927 else
4929 e = new VarExp(loc, f);
4930 if (eleft)
4931 { e = new CommaExp(loc, eleft, e);
4932 e->type = f->type;
4935 return e;
4938 Type *t = s->getType();
4939 if (t)
4941 return new TypeExp(loc, t);
4944 TupleDeclaration *tup = s->isTupleDeclaration();
4945 if (tup)
4947 if (eleft)
4948 error("cannot have e.tuple");
4949 e = new TupleExp(loc, tup);
4950 e = e->semantic(sc);
4951 return e;
4954 ScopeDsymbol *sds = s->isScopeDsymbol();
4955 if (sds)
4957 //printf("it's a ScopeDsymbol\n");
4958 e = new ScopeExp(loc, sds);
4959 e = e->semantic(sc);
4960 if (eleft)
4961 e = new DotExp(loc, eleft, e);
4962 return e;
4965 Import *imp = s->isImport();
4966 if (imp)
4968 ScopeExp *ie;
4970 ie = new ScopeExp(loc, imp->pkg);
4971 return ie->semantic(sc);
4974 // BUG: handle other cases like in IdentifierExp::semantic()
4975 #ifdef DEBUG
4976 printf("s = '%s', kind = '%s'\n", s->toChars(), s->kind());
4977 #endif
4978 assert(0);
4980 else if (ident == Id::stringof)
4981 { char *s = ie->toChars();
4982 e = new StringExp(loc, s, strlen(s), 'c');
4983 e = e->semantic(sc);
4984 return e;
4986 error("undefined identifier %s", toChars());
4987 type = Type::tvoid;
4988 return this;
4990 else if (e1->type->ty == Tpointer &&
4991 ident != Id::init && ident != Id::__sizeof &&
4992 ident != Id::alignof && ident != Id::offsetof &&
4993 ident != Id::mangleof && ident != Id::stringof)
4995 e = new PtrExp(loc, e1);
4996 e->type = e1->type->next;
4997 return e->type->dotExp(sc, e, ident);
4999 else
5001 e = e1->type->dotExp(sc, e1, ident);
5002 e = e->semantic(sc);
5003 return e;
5007 void DotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
5009 //printf("DotIdExp::toCBuffer()\n");
5010 expToCBuffer(buf, hgs, e1, PREC_primary);
5011 buf->writeByte('.');
5012 buf->writestring(ident->toChars());
5015 /********************** DotTemplateExp ***********************************/
5017 // Mainly just a placeholder
5019 DotTemplateExp::DotTemplateExp(Loc loc, Expression *e, TemplateDeclaration *td)
5020 : UnaExp(loc, TOKdottd, sizeof(DotTemplateExp), e)
5023 this->td = td;
5026 void DotTemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
5028 expToCBuffer(buf, hgs, e1, PREC_primary);
5029 buf->writeByte('.');
5030 buf->writestring(td->toChars());
5034 /************************************************************/
5036 DotVarExp::DotVarExp(Loc loc, Expression *e, Declaration *v)
5037 : UnaExp(loc, TOKdotvar, sizeof(DotVarExp), e)
5039 //printf("DotVarExp()\n");
5040 this->var = v;
5043 Expression *DotVarExp::semantic(Scope *sc)
5045 #if LOGSEMANTIC
5046 printf("DotVarExp::semantic('%s')\n", toChars());
5047 #endif
5048 if (!type)
5050 var = var->toAlias()->isDeclaration();
5052 TupleDeclaration *tup = var->isTupleDeclaration();
5053 if (tup)
5054 { /* Replace:
5055 * e1.tuple(a, b, c)
5056 * with:
5057 * tuple(e1.a, e1.b, e1.c)
5059 Expressions *exps = new Expressions;
5061 exps->reserve(tup->objects->dim);
5062 for (size_t i = 0; i < tup->objects->dim; i++)
5063 { Object *o = (Object *)tup->objects->data[i];
5064 if (o->dyncast() != DYNCAST_EXPRESSION)
5066 error("%s is not an expression", o->toChars());
5068 else
5070 Expression *e = (Expression *)o;
5071 if (e->op != TOKdsymbol)
5072 error("%s is not a member", e->toChars());
5073 else
5074 { DsymbolExp *ve = (DsymbolExp *)e;
5076 e = new DotVarExp(loc, e1, ve->s->isDeclaration());
5077 exps->push(e);
5081 Expression *e = new TupleExp(loc, exps);
5082 e = e->semantic(sc);
5083 return e;
5086 e1 = e1->semantic(sc);
5087 type = var->type;
5088 if (!type && global.errors)
5089 { // var is goofed up, just return 0
5090 return new IntegerExp(0);
5092 assert(type);
5094 if (!var->isFuncDeclaration()) // for functions, do checks after overload resolution
5096 AggregateDeclaration *ad = var->toParent()->isAggregateDeclaration();
5097 e1 = getRightThis(loc, sc, ad, e1, var);
5098 accessCheck(loc, sc, e1, var);
5101 //printf("-DotVarExp::semantic('%s')\n", toChars());
5102 return this;
5105 Expression *DotVarExp::toLvalue(Scope *sc, Expression *e)
5107 //printf("DotVarExp::toLvalue(%s)\n", toChars());
5108 return this;
5111 Expression *DotVarExp::modifiableLvalue(Scope *sc, Expression *e)
5113 //printf("DotVarExp::modifiableLvalue(%s)\n", toChars());
5115 if (var->isCtorinit())
5116 { // It's only modifiable if inside the right constructor
5117 Dsymbol *s = sc->func;
5118 while (1)
5120 FuncDeclaration *fd = NULL;
5121 if (s)
5122 fd = s->isFuncDeclaration();
5123 if (fd &&
5124 ((fd->isCtorDeclaration() && var->storage_class & STCfield) ||
5125 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) &&
5126 fd->toParent() == var->toParent() &&
5127 e1->op == TOKthis
5130 VarDeclaration *v = var->isVarDeclaration();
5131 assert(v);
5132 v->ctorinit = 1;
5133 //printf("setting ctorinit\n");
5135 else
5137 if (s)
5138 { s = s->toParent2();
5139 continue;
5141 else
5143 const char *p = var->isStatic() ? "static " : "";
5144 error("can only initialize %sconst member %s inside %sconstructor",
5145 p, var->toChars(), p);
5148 break;
5151 return this;
5154 void DotVarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
5156 expToCBuffer(buf, hgs, e1, PREC_primary);
5157 buf->writeByte('.');
5158 buf->writestring(var->toChars());
5161 /************************************************************/
5163 /* Things like:
5164 * foo.bar!(args)
5167 DotTemplateInstanceExp::DotTemplateInstanceExp(Loc loc, Expression *e, TemplateInstance *ti)
5168 : UnaExp(loc, TOKdotti, sizeof(DotTemplateInstanceExp), e)
5170 //printf("DotTemplateInstanceExp()\n");
5171 this->ti = ti;
5174 Expression *DotTemplateInstanceExp::syntaxCopy()
5176 DotTemplateInstanceExp *de = new DotTemplateInstanceExp(loc,
5177 e1->syntaxCopy(),
5178 (TemplateInstance *)ti->syntaxCopy(NULL));
5179 return de;
5182 Expression *DotTemplateInstanceExp::semantic(Scope *sc)
5183 { Dsymbol *s;
5184 Dsymbol *s2;
5185 TemplateDeclaration *td;
5186 Expression *e;
5187 Identifier *id;
5188 Type *t1;
5189 Expression *eleft = NULL;
5190 Expression *eright;
5192 #if LOGSEMANTIC
5193 printf("DotTemplateInstanceExp::semantic('%s')\n", toChars());
5194 #endif
5195 //e1->print();
5196 //print();
5197 e1 = e1->semantic(sc);
5198 t1 = e1->type;
5199 if (t1)
5200 t1 = t1->toBasetype();
5201 //t1->print();
5202 if (e1->op == TOKdotexp)
5203 { DotExp *de = (DotExp *)e1;
5204 eleft = de->e1;
5205 eright = de->e2;
5207 else
5208 { eleft = NULL;
5209 eright = e1;
5211 if (eright->op == TOKimport)
5213 s = ((ScopeExp *)eright)->sds;
5215 else if (e1->op == TOKtype)
5217 s = t1->isClassHandle();
5218 if (!s)
5219 { if (t1->ty == Tstruct)
5220 s = ((TypeStruct *)t1)->sym;
5221 else
5222 goto L1;
5225 else if (t1 && (t1->ty == Tstruct || t1->ty == Tclass))
5227 s = t1->toDsymbol(sc);
5228 eleft = e1;
5230 else if (t1 && t1->ty == Tpointer)
5232 t1 = t1->next->toBasetype();
5233 if (t1->ty != Tstruct)
5234 goto L1;
5235 s = t1->toDsymbol(sc);
5236 eleft = e1;
5238 else
5241 error("template %s is not a member of %s", ti->toChars(), e1->toChars());
5242 goto Lerr;
5245 assert(s);
5246 id = ti->name;
5247 s2 = s->search(loc, id, 0);
5248 if (!s2)
5249 { error("template identifier %s is not a member of %s %s", id->toChars(), s->kind(), s->ident->toChars());
5250 goto Lerr;
5252 s = s2;
5253 s->semantic(sc);
5254 s = s->toAlias();
5255 td = s->isTemplateDeclaration();
5256 if (!td)
5258 error("%s is not a template", id->toChars());
5259 goto Lerr;
5261 if (global.errors)
5262 goto Lerr;
5264 ti->tempdecl = td;
5266 if (eleft)
5267 { Declaration *v;
5269 ti->semantic(sc);
5270 s = ti->inst->toAlias();
5271 v = s->isDeclaration();
5272 if (v)
5273 { e = new DotVarExp(loc, eleft, v);
5274 e = e->semantic(sc);
5275 return e;
5279 e = new ScopeExp(loc, ti);
5280 if (eleft)
5282 e = new DotExp(loc, eleft, e);
5284 e = e->semantic(sc);
5285 return e;
5287 Lerr:
5288 return new IntegerExp(0);
5291 void DotTemplateInstanceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
5293 expToCBuffer(buf, hgs, e1, PREC_primary);
5294 buf->writeByte('.');
5295 ti->toCBuffer(buf, hgs);
5298 /************************************************************/
5300 DelegateExp::DelegateExp(Loc loc, Expression *e, FuncDeclaration *f)
5301 : UnaExp(loc, TOKdelegate, sizeof(DelegateExp), e)
5303 this->func = f;
5306 Expression *DelegateExp::semantic(Scope *sc)
5308 #if LOGSEMANTIC
5309 printf("DelegateExp::semantic('%s')\n", toChars());
5310 #endif
5311 if (!type)
5313 e1 = e1->semantic(sc);
5314 type = new TypeDelegate(func->type);
5315 type = type->semantic(loc, sc);
5316 AggregateDeclaration *ad = func->toParent()->isAggregateDeclaration();
5317 if (func->needThis())
5318 e1 = getRightThis(loc, sc, ad, e1, func);
5320 return this;
5323 void DelegateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
5325 buf->writeByte('&');
5326 if (!func->isNested())
5328 expToCBuffer(buf, hgs, e1, PREC_primary);
5329 buf->writeByte('.');
5331 buf->writestring(func->toChars());
5334 /************************************************************/
5336 DotTypeExp::DotTypeExp(Loc loc, Expression *e, Dsymbol *s)
5337 : UnaExp(loc, TOKdottype, sizeof(DotTypeExp), e)
5339 this->sym = s;
5340 this->type = s->getType();
5343 Expression *DotTypeExp::semantic(Scope *sc)
5345 #if LOGSEMANTIC
5346 printf("DotTypeExp::semantic('%s')\n", toChars());
5347 #endif
5348 UnaExp::semantic(sc);
5349 return this;
5352 void DotTypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
5354 expToCBuffer(buf, hgs, e1, PREC_primary);
5355 buf->writeByte('.');
5356 buf->writestring(sym->toChars());
5359 /************************************************************/
5361 CallExp::CallExp(Loc loc, Expression *e, Expressions *exps)
5362 : UnaExp(loc, TOKcall, sizeof(CallExp), e)
5364 this->arguments = exps;
5367 CallExp::CallExp(Loc loc, Expression *e)
5368 : UnaExp(loc, TOKcall, sizeof(CallExp), e)
5370 this->arguments = NULL;
5373 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1)
5374 : UnaExp(loc, TOKcall, sizeof(CallExp), e)
5376 Expressions *arguments = new Expressions();
5377 arguments->setDim(1);
5378 arguments->data[0] = (void *)earg1;
5380 this->arguments = arguments;
5383 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1, Expression *earg2)
5384 : UnaExp(loc, TOKcall, sizeof(CallExp), e)
5386 Expressions *arguments = new Expressions();
5387 arguments->setDim(2);
5388 arguments->data[0] = (void *)earg1;
5389 arguments->data[1] = (void *)earg2;
5391 this->arguments = arguments;
5394 Expression *CallExp::syntaxCopy()
5396 return new CallExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments));
5400 Expression *CallExp::semantic(Scope *sc)
5402 TypeFunction *tf;
5403 FuncDeclaration *f;
5404 int i;
5405 Type *t1;
5406 int istemp;
5408 #if LOGSEMANTIC
5409 printf("CallExp::semantic() %s\n", toChars());
5410 #endif
5411 if (type)
5412 return this; // semantic() already run
5413 #if 0
5414 if (arguments && arguments->dim)
5416 Expression *earg = (Expression *)arguments->data[0];
5417 earg->print();
5418 if (earg->type) earg->type->print();
5420 #endif
5422 if (e1->op == TOKdelegate)
5423 { DelegateExp *de = (DelegateExp *)e1;
5425 e1 = new DotVarExp(de->loc, de->e1, de->func);
5426 return semantic(sc);
5429 /* Transform:
5430 * array.id(args) into id(array,args)
5431 * aa.remove(arg) into delete aa[arg]
5433 if (e1->op == TOKdot)
5435 // BUG: we should handle array.a.b.c.e(args) too
5437 DotIdExp *dotid = (DotIdExp *)(e1);
5438 dotid->e1 = dotid->e1->semantic(sc);
5439 assert(dotid->e1);
5440 if (dotid->e1->type)
5442 TY e1ty = dotid->e1->type->toBasetype()->ty;
5443 if (e1ty == Taarray && dotid->ident == Id::remove)
5445 if (!arguments || arguments->dim != 1)
5446 { error("expected key as argument to aa.remove()");
5447 goto Lagain;
5449 Expression *key = (Expression *)arguments->data[0];
5450 key = key->semantic(sc);
5451 key = resolveProperties(sc, key);
5452 key->rvalue();
5454 TypeAArray *taa = (TypeAArray *)dotid->e1->type->toBasetype();
5455 key = key->implicitCastTo(sc, taa->index);
5456 key = key->implicitCastTo(sc, taa->key);
5458 return new RemoveExp(loc, dotid->e1, key);
5460 else if (e1ty == Tarray || e1ty == Tsarray || e1ty == Taarray)
5462 if (!arguments)
5463 arguments = new Expressions();
5464 arguments->shift(dotid->e1);
5465 e1 = new IdentifierExp(dotid->loc, dotid->ident);
5470 istemp = 0;
5471 Lagain:
5472 f = NULL;
5473 if (e1->op == TOKthis || e1->op == TOKsuper)
5475 // semantic() run later for these
5477 else
5479 UnaExp::semantic(sc);
5481 /* Look for e1 being a lazy parameter
5483 if (e1->op == TOKvar)
5484 { VarExp *ve = (VarExp *)e1;
5486 if (ve->var->storage_class & STClazy)
5488 TypeFunction *tf = new TypeFunction(NULL, ve->var->type, 0, LINKd);
5489 TypeDelegate *t = new TypeDelegate(tf);
5490 ve->type = t->semantic(loc, sc);
5494 if (e1->op == TOKimport)
5495 { // Perhaps this should be moved to ScopeExp::semantic()
5496 ScopeExp *se = (ScopeExp *)e1;
5497 e1 = new DsymbolExp(loc, se->sds);
5498 e1 = e1->semantic(sc);
5500 #if 1 // patch for #540 by Oskar Linde
5501 else if (e1->op == TOKdotexp)
5503 DotExp *de = (DotExp *) e1;
5505 if (de->e2->op == TOKimport)
5506 { // This should *really* be moved to ScopeExp::semantic()
5507 ScopeExp *se = (ScopeExp *)de->e2;
5508 de->e2 = new DsymbolExp(loc, se->sds);
5509 de->e2 = de->e2->semantic(sc);
5512 if (de->e2->op == TOKtemplate)
5513 { TemplateExp *te = (TemplateExp *) de->e2;
5514 e1 = new DotTemplateExp(loc,de->e1,te->td);
5517 #endif
5520 if (e1->op == TOKcomma)
5522 CommaExp *ce = (CommaExp *)e1;
5524 e1 = ce->e2;
5525 e1->type = ce->type;
5526 ce->e2 = this;
5527 ce->type = NULL;
5528 return ce->semantic(sc);
5531 t1 = NULL;
5532 if (e1->type)
5533 t1 = e1->type->toBasetype();
5535 // Check for call operator overload
5536 if (t1)
5537 { AggregateDeclaration *ad;
5539 if (t1->ty == Tstruct)
5541 ad = ((TypeStruct *)t1)->sym;
5542 if (search_function(ad, Id::call))
5543 goto L1; // overload of opCall, therefore it's a call
5545 if (e1->op != TOKtype)
5546 error("%s %s does not overload ()", ad->kind(), ad->toChars());
5547 /* It's a struct literal
5549 Expression *e = new StructLiteralExp(loc, (StructDeclaration *)ad, arguments);
5550 e = e->semantic(sc);
5551 e->type = e1->type; // in case e1->type was a typedef
5552 return e;
5554 else if (t1->ty == Tclass)
5556 ad = ((TypeClass *)t1)->sym;
5557 goto L1;
5559 // Rewrite as e1.call(arguments)
5560 Expression *e = new DotIdExp(loc, e1, Id::call);
5561 e = new CallExp(loc, e, arguments);
5562 e = e->semantic(sc);
5563 return e;
5567 arrayExpressionSemantic(arguments, sc);
5568 preFunctionArguments(loc, sc, arguments);
5570 if (e1->op == TOKdotvar && t1->ty == Tfunction ||
5571 e1->op == TOKdottd)
5573 DotVarExp *dve;
5574 DotTemplateExp *dte;
5575 AggregateDeclaration *ad;
5576 UnaExp *ue = (UnaExp *)(e1);
5578 if (e1->op == TOKdotvar)
5579 { // Do overload resolution
5580 dve = (DotVarExp *)(e1);
5582 f = dve->var->isFuncDeclaration();
5583 assert(f);
5584 f = f->overloadResolve(loc, arguments);
5586 ad = f->toParent()->isAggregateDeclaration();
5588 else
5589 { dte = (DotTemplateExp *)(e1);
5590 TemplateDeclaration *td = dte->td;
5591 assert(td);
5592 if (!arguments)
5593 // Should fix deduceFunctionTemplate() so it works on NULL argument
5594 arguments = new Expressions();
5595 f = td->deduceFunctionTemplate(sc, loc, NULL, arguments);
5596 if (!f)
5597 { type = Type::terror;
5598 return this;
5600 ad = td->toParent()->isAggregateDeclaration();
5602 if (f->needThis())
5603 ue->e1 = getRightThis(loc, sc, ad, ue->e1, f);
5605 checkDeprecated(sc, f);
5606 accessCheck(loc, sc, ue->e1, f);
5607 if (!f->needThis())
5609 VarExp *ve = new VarExp(loc, f);
5610 e1 = new CommaExp(loc, ue->e1, ve);
5611 e1->type = f->type;
5613 else
5615 if (e1->op == TOKdotvar)
5616 dve->var = f;
5617 else
5618 e1 = new DotVarExp(loc, dte->e1, f);
5619 e1->type = f->type;
5621 // See if we need to adjust the 'this' pointer
5622 AggregateDeclaration *ad = f->isThis();
5623 ClassDeclaration *cd = ue->e1->type->isClassHandle();
5624 if (ad && cd && ad->isClassDeclaration() && ad != cd &&
5625 ue->e1->op != TOKsuper)
5627 ue->e1 = ue->e1->castTo(sc, ad->type); //new CastExp(loc, ue->e1, ad->type);
5628 ue->e1 = ue->e1->semantic(sc);
5631 t1 = e1->type;
5633 else if (e1->op == TOKsuper)
5635 // Base class constructor call
5636 ClassDeclaration *cd = NULL;
5638 if (sc->func)
5639 cd = sc->func->toParent()->isClassDeclaration();
5640 if (!cd || !cd->baseClass || !sc->func->isCtorDeclaration())
5642 error("super class constructor call must be in a constructor");
5643 type = Type::terror;
5644 return this;
5646 else
5648 f = cd->baseClass->ctor;
5649 if (!f)
5650 { error("no super class constructor for %s", cd->baseClass->toChars());
5651 type = Type::terror;
5652 return this;
5654 else
5656 #if 0
5657 if (sc->callSuper & (CSXthis | CSXsuper))
5658 error("reference to this before super()");
5659 #endif
5660 if (sc->noctor || sc->callSuper & CSXlabel)
5661 error("constructor calls not allowed in loops or after labels");
5662 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor))
5663 error("multiple constructor calls");
5664 sc->callSuper |= CSXany_ctor | CSXsuper_ctor;
5666 f = f->overloadResolve(loc, arguments);
5667 checkDeprecated(sc, f);
5668 e1 = new DotVarExp(e1->loc, e1, f);
5669 e1 = e1->semantic(sc);
5670 t1 = e1->type;
5674 else if (e1->op == TOKthis)
5676 // same class constructor call
5677 ClassDeclaration *cd = NULL;
5679 if (sc->func)
5680 cd = sc->func->toParent()->isClassDeclaration();
5681 if (!cd || !sc->func->isCtorDeclaration())
5683 error("class constructor call must be in a constructor");
5684 type = Type::terror;
5685 return this;
5687 else
5689 #if 0
5690 if (sc->callSuper & (CSXthis | CSXsuper))
5691 error("reference to this before super()");
5692 #endif
5693 if (sc->noctor || sc->callSuper & CSXlabel)
5694 error("constructor calls not allowed in loops or after labels");
5695 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor))
5696 error("multiple constructor calls");
5697 sc->callSuper |= CSXany_ctor | CSXthis_ctor;
5699 f = cd->ctor;
5700 f = f->overloadResolve(loc, arguments);
5701 checkDeprecated(sc, f);
5702 e1 = new DotVarExp(e1->loc, e1, f);
5703 e1 = e1->semantic(sc);
5704 t1 = e1->type;
5706 // BUG: this should really be done by checking the static
5707 // call graph
5708 if (f == sc->func)
5709 error("cyclic constructor call");
5712 else if (!t1)
5714 error("function expected before (), not '%s'", e1->toChars());
5715 type = Type::terror;
5716 return this;
5718 else if (t1->ty != Tfunction)
5720 if (t1->ty == Tdelegate)
5722 assert(t1->next->ty == Tfunction);
5723 tf = (TypeFunction *)(t1->next);
5724 goto Lcheckargs;
5726 else if (t1->ty == Tpointer && t1->next->ty == Tfunction)
5727 { Expression *e;
5729 e = new PtrExp(loc, e1);
5730 t1 = t1->next;
5731 e->type = t1;
5732 e1 = e;
5734 else if (e1->op == TOKtemplate)
5736 TemplateExp *te = (TemplateExp *)e1;
5737 f = te->td->deduceFunctionTemplate(sc, loc, NULL, arguments);
5738 if (!f)
5739 { type = Type::terror;
5740 return this;
5742 if (f->needThis() && hasThis(sc))
5744 // Supply an implicit 'this', as in
5745 // this.ident
5747 e1 = new DotTemplateExp(loc, (new ThisExp(loc))->semantic(sc), te->td);
5748 goto Lagain;
5751 e1 = new VarExp(loc, f);
5752 goto Lagain;
5754 else
5755 { error("function expected before (), not %s of type %s", e1->toChars(), e1->type->toChars());
5756 type = Type::terror;
5757 return this;
5760 else if (e1->op == TOKvar)
5762 // Do overload resolution
5763 VarExp *ve = (VarExp *)e1;
5765 f = ve->var->isFuncDeclaration();
5766 assert(f);
5768 // Look to see if f is really a function template
5769 if (0 && !istemp && f->parent)
5770 { TemplateInstance *ti = f->parent->isTemplateInstance();
5772 if (ti &&
5773 (ti->name == f->ident ||
5774 ti->toAlias()->ident == f->ident)
5776 ti->tempdecl)
5778 /* This is so that one can refer to the enclosing
5779 * template, even if it has the same name as a member
5780 * of the template, if it has a !(arguments)
5782 TemplateDeclaration *tempdecl = ti->tempdecl;
5783 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's
5784 tempdecl = tempdecl->overroot; // then get the start
5785 e1 = new TemplateExp(loc, tempdecl);
5786 istemp = 1;
5787 goto Lagain;
5791 f = f->overloadResolve(loc, arguments);
5792 checkDeprecated(sc, f);
5794 if (f->needThis() && hasThis(sc))
5796 // Supply an implicit 'this', as in
5797 // this.ident
5799 e1 = new DotVarExp(loc, new ThisExp(loc), f);
5800 goto Lagain;
5803 accessCheck(loc, sc, NULL, f);
5805 ve->var = f;
5806 ve->type = f->type;
5807 t1 = f->type;
5809 assert(t1->ty == Tfunction);
5810 tf = (TypeFunction *)(t1);
5812 Lcheckargs:
5813 assert(tf->ty == Tfunction);
5814 type = tf->next;
5816 if (!arguments)
5817 arguments = new Expressions();
5818 functionArguments(loc, sc, tf, arguments);
5820 assert(type);
5822 if (f && f->tintro)
5824 Type *t = type;
5825 target_ptrdiff_t offset = 0;
5827 if (f->tintro->next->isBaseOf(t, &offset) && offset)
5829 type = f->tintro->next;
5830 return castTo(sc, t);
5834 return this;
5837 int CallExp::checkSideEffect(int flag)
5839 return 1;
5842 Expression *CallExp::toLvalue(Scope *sc, Expression *e)
5844 if (type->toBasetype()->ty == Tstruct)
5845 return this;
5846 else
5847 return Expression::toLvalue(sc, e);
5850 void CallExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
5851 { int i;
5853 expToCBuffer(buf, hgs, e1, precedence[op]);
5854 buf->writeByte('(');
5855 argsToCBuffer(buf, arguments, hgs);
5856 buf->writeByte(')');
5860 /************************************************************/
5862 AddrExp::AddrExp(Loc loc, Expression *e)
5863 : UnaExp(loc, TOKaddress, sizeof(AddrExp), e)
5867 Expression *AddrExp::semantic(Scope *sc)
5869 #if LOGSEMANTIC
5870 printf("AddrExp::semantic('%s')\n", toChars());
5871 #endif
5872 if (!type)
5874 UnaExp::semantic(sc);
5875 e1 = e1->toLvalue(sc, NULL);
5876 if (!e1->type)
5878 error("cannot take address of %s", e1->toChars());
5879 type = Type::tint32;
5880 return this;
5882 type = e1->type->pointerTo();
5884 // See if this should really be a delegate
5885 if (e1->op == TOKdotvar)
5887 DotVarExp *dve = (DotVarExp *)e1;
5888 FuncDeclaration *f = dve->var->isFuncDeclaration();
5890 if (f)
5891 { Expression *e;
5893 e = new DelegateExp(loc, dve->e1, f);
5894 e = e->semantic(sc);
5895 return e;
5898 else if (e1->op == TOKvar)
5900 VarExp *dve = (VarExp *)e1;
5901 FuncDeclaration *f = dve->var->isFuncDeclaration();
5903 if (f && f->isNested())
5904 { Expression *e;
5906 e = new DelegateExp(loc, e1, f);
5907 e = e->semantic(sc);
5908 return e;
5911 else if (e1->op == TOKarray)
5913 if (e1->type->toBasetype()->ty == Tbit)
5914 error("cannot take address of bit in array");
5916 return optimize(WANTvalue);
5918 return this;
5921 /************************************************************/
5923 PtrExp::PtrExp(Loc loc, Expression *e)
5924 : UnaExp(loc, TOKstar, sizeof(PtrExp), e)
5926 if (e->type)
5927 type = e->type->next;
5930 PtrExp::PtrExp(Loc loc, Expression *e, Type *t)
5931 : UnaExp(loc, TOKstar, sizeof(PtrExp), e)
5933 type = t;
5936 Expression *PtrExp::semantic(Scope *sc)
5937 { Type *tb;
5939 #if LOGSEMANTIC
5940 printf("PtrExp::semantic('%s')\n", toChars());
5941 #endif
5942 UnaExp::semantic(sc);
5943 e1 = resolveProperties(sc, e1);
5944 if (type)
5945 return this;
5946 if (!e1->type)
5947 printf("PtrExp::semantic('%s')\n", toChars());
5948 tb = e1->type->toBasetype();
5949 switch (tb->ty)
5951 case Tpointer:
5952 type = tb->next;
5953 if (type->isbit())
5954 { Expression *e;
5956 // Rewrite *p as p[0]
5957 e = new IndexExp(loc, e1, new IntegerExp(0));
5958 return e->semantic(sc);
5960 break;
5962 case Tsarray:
5963 case Tarray:
5964 type = tb->next;
5965 e1 = e1->castTo(sc, type->pointerTo());
5966 break;
5968 default:
5969 error("can only * a pointer, not a '%s'", e1->type->toChars());
5970 type = Type::tint32;
5971 break;
5973 rvalue();
5974 return this;
5977 Expression *PtrExp::toLvalue(Scope *sc, Expression *e)
5979 #if 0
5980 tym = tybasic(e1->ET->Tty);
5981 if (!(tyscalar(tym) ||
5982 tym == TYstruct ||
5983 tym == TYarray && e->Eoper == TOKaddr))
5984 synerr(EM_lvalue); // lvalue expected
5985 #endif
5986 return this;
5989 void PtrExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
5991 buf->writeByte('*');
5992 expToCBuffer(buf, hgs, e1, precedence[op]);
5995 /************************************************************/
5997 NegExp::NegExp(Loc loc, Expression *e)
5998 : UnaExp(loc, TOKneg, sizeof(NegExp), e)
6002 Expression *NegExp::semantic(Scope *sc)
6003 { Expression *e;
6005 #if LOGSEMANTIC
6006 printf("NegExp::semantic('%s')\n", toChars());
6007 #endif
6008 if (!type)
6010 UnaExp::semantic(sc);
6011 e1 = resolveProperties(sc, e1);
6012 e = op_overload(sc);
6013 if (e)
6014 return e;
6016 e1->checkNoBool();
6017 e1->checkArithmetic();
6018 type = e1->type;
6020 return this;
6023 /************************************************************/
6025 UAddExp::UAddExp(Loc loc, Expression *e)
6026 : UnaExp(loc, TOKuadd, sizeof(UAddExp), e)
6030 Expression *UAddExp::semantic(Scope *sc)
6031 { Expression *e;
6033 #if LOGSEMANTIC
6034 printf("UAddExp::semantic('%s')\n", toChars());
6035 #endif
6036 assert(!type);
6037 UnaExp::semantic(sc);
6038 e1 = resolveProperties(sc, e1);
6039 e = op_overload(sc);
6040 if (e)
6041 return e;
6042 e1->checkNoBool();
6043 e1->checkArithmetic();
6044 return e1;
6047 /************************************************************/
6049 ComExp::ComExp(Loc loc, Expression *e)
6050 : UnaExp(loc, TOKtilde, sizeof(ComExp), e)
6054 Expression *ComExp::semantic(Scope *sc)
6055 { Expression *e;
6057 if (!type)
6059 UnaExp::semantic(sc);
6060 e1 = resolveProperties(sc, e1);
6061 e = op_overload(sc);
6062 if (e)
6063 return e;
6065 e1->checkNoBool();
6066 e1 = e1->checkIntegral();
6067 type = e1->type;
6069 return this;
6072 /************************************************************/
6074 NotExp::NotExp(Loc loc, Expression *e)
6075 : UnaExp(loc, TOKnot, sizeof(NotExp), e)
6079 Expression *NotExp::semantic(Scope *sc)
6081 UnaExp::semantic(sc);
6082 e1 = resolveProperties(sc, e1);
6083 e1 = e1->checkToBoolean();
6084 type = Type::tboolean;
6085 return this;
6088 int NotExp::isBit()
6090 return TRUE;
6095 /************************************************************/
6097 BoolExp::BoolExp(Loc loc, Expression *e, Type *t)
6098 : UnaExp(loc, TOKtobool, sizeof(BoolExp), e)
6100 type = t;
6103 Expression *BoolExp::semantic(Scope *sc)
6105 UnaExp::semantic(sc);
6106 e1 = resolveProperties(sc, e1);
6107 e1 = e1->checkToBoolean();
6108 type = Type::tboolean;
6109 return this;
6112 int BoolExp::isBit()
6114 return TRUE;
6117 /************************************************************/
6119 DeleteExp::DeleteExp(Loc loc, Expression *e)
6120 : UnaExp(loc, TOKdelete, sizeof(DeleteExp), e)
6124 Expression *DeleteExp::semantic(Scope *sc)
6126 Type *tb;
6128 UnaExp::semantic(sc);
6129 e1 = resolveProperties(sc, e1);
6130 e1 = e1->toLvalue(sc, NULL);
6131 type = Type::tvoid;
6133 tb = e1->type->toBasetype();
6134 switch (tb->ty)
6135 { case Tclass:
6136 { TypeClass *tc = (TypeClass *)tb;
6137 ClassDeclaration *cd = tc->sym;
6139 if (cd->isCOMinterface())
6140 { /* Because COM classes are deleted by IUnknown.Release()
6142 error("cannot delete instance of COM interface %s", cd->toChars());
6144 break;
6146 case Tpointer:
6147 tb = tb->next->toBasetype();
6148 if (tb->ty == Tstruct)
6150 TypeStruct *ts = (TypeStruct *)tb;
6151 StructDeclaration *sd = ts->sym;
6152 FuncDeclaration *f = sd->aggDelete;
6154 if (f)
6156 Type *tpv = Type::tvoid->pointerTo();
6158 Expression *e = e1->castTo(sc, tpv);
6159 Expression *ec = new VarExp(loc, f);
6160 e = new CallExp(loc, ec, e);
6161 return e->semantic(sc);
6164 break;
6166 case Tarray:
6167 break;
6169 default:
6170 if (e1->op == TOKindex)
6172 IndexExp *ae = (IndexExp *)(e1);
6173 Type *tb1 = ae->e1->type->toBasetype();
6174 if (tb1->ty == Taarray)
6175 break;
6177 error("cannot delete type %s", e1->type->toChars());
6178 break;
6181 if (e1->op == TOKindex)
6183 IndexExp *ae = (IndexExp *)(e1);
6184 Type *tb1 = ae->e1->type->toBasetype();
6185 if (tb1->ty == Taarray)
6186 { if (!global.params.useDeprecated)
6187 error("delete aa[key] deprecated, use aa.remove(key)");
6191 return this;
6194 int DeleteExp::checkSideEffect(int flag)
6196 return 1;
6199 Expression *DeleteExp::checkToBoolean()
6201 error("delete does not give a boolean result");
6202 return this;
6205 void DeleteExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
6207 buf->writestring("delete ");
6208 expToCBuffer(buf, hgs, e1, precedence[op]);
6211 /************************************************************/
6213 CastExp::CastExp(Loc loc, Expression *e, Type *t)
6214 : UnaExp(loc, TOKcast, sizeof(CastExp), e)
6216 to = t;
6219 Expression *CastExp::syntaxCopy()
6221 return new CastExp(loc, e1->syntaxCopy(), to->syntaxCopy());
6225 Expression *CastExp::semantic(Scope *sc)
6226 { Expression *e;
6227 BinExp *b;
6228 UnaExp *u;
6230 #if LOGSEMANTIC
6231 printf("CastExp::semantic('%s')\n", toChars());
6232 #endif
6234 //static int x; assert(++x < 10);
6236 if (type)
6237 return this;
6238 UnaExp::semantic(sc);
6239 if (e1->type) // if not a tuple
6241 e1 = resolveProperties(sc, e1);
6242 to = to->semantic(loc, sc);
6244 e = op_overload(sc);
6245 if (e)
6247 return e->implicitCastTo(sc, to);
6250 Type *tob = to->toBasetype();
6251 if (tob->ty == Tstruct &&
6252 !tob->equals(e1->type->toBasetype()) &&
6253 ((TypeStruct *)to)->sym->search(0, Id::call, 0)
6256 /* Look to replace:
6257 * cast(S)t
6258 * with:
6259 * S(t)
6262 // Rewrite as to.call(e1)
6263 e = new TypeExp(loc, to);
6264 e = new DotIdExp(loc, e, Id::call);
6265 e = new CallExp(loc, e, e1);
6266 e = e->semantic(sc);
6267 return e;
6270 e = e1->castTo(sc, to);
6271 return e;
6274 int CastExp::checkSideEffect(int flag)
6276 /* if not:
6277 * cast(void)
6278 * cast(classtype)func()
6280 if (!to->equals(Type::tvoid) &&
6281 !(to->ty == Tclass && e1->op == TOKcall && e1->type->ty == Tclass))
6282 return Expression::checkSideEffect(flag);
6283 return 1;
6286 void CastExp::checkEscape()
6287 { Type *tb = type->toBasetype();
6288 if (tb->ty == Tarray && e1->op == TOKvar &&
6289 e1->type->toBasetype()->ty == Tsarray)
6290 { VarExp *ve = (VarExp *)e1;
6291 VarDeclaration *v = ve->var->isVarDeclaration();
6292 if (v)
6294 if (!v->isDataseg() && !v->isParameter())
6295 error("escaping reference to local %s", v->toChars());
6300 void CastExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
6302 buf->writestring("cast(");
6303 to->toCBuffer(buf, NULL, hgs);
6304 buf->writeByte(')');
6305 expToCBuffer(buf, hgs, e1, precedence[op]);
6309 /************************************************************/
6311 SliceExp::SliceExp(Loc loc, Expression *e1, Expression *lwr, Expression *upr)
6312 : UnaExp(loc, TOKslice, sizeof(SliceExp), e1)
6314 this->upr = upr;
6315 this->lwr = lwr;
6316 lengthVar = NULL;
6319 Expression *SliceExp::syntaxCopy()
6321 Expression *lwr = NULL;
6322 if (this->lwr)
6323 lwr = this->lwr->syntaxCopy();
6325 Expression *upr = NULL;
6326 if (this->upr)
6327 upr = this->upr->syntaxCopy();
6329 return new SliceExp(loc, e1->syntaxCopy(), lwr, upr);
6332 Expression *SliceExp::semantic(Scope *sc)
6333 { Expression *e;
6334 AggregateDeclaration *ad;
6335 //FuncDeclaration *fd;
6336 ScopeDsymbol *sym;
6338 #if LOGSEMANTIC
6339 printf("SliceExp::semantic('%s')\n", toChars());
6340 #endif
6341 if (type)
6342 return this;
6344 UnaExp::semantic(sc);
6345 e1 = resolveProperties(sc, e1);
6347 e = this;
6349 Type *t = e1->type->toBasetype();
6350 if (t->ty == Tpointer)
6352 if (!lwr || !upr)
6353 error("need upper and lower bound to slice pointer");
6355 else if (t->ty == Tarray)
6358 else if (t->ty == Tsarray)
6361 else if (t->ty == Tclass)
6363 ad = ((TypeClass *)t)->sym;
6364 goto L1;
6366 else if (t->ty == Tstruct)
6368 ad = ((TypeStruct *)t)->sym;
6371 if (search_function(ad, Id::slice))
6373 // Rewrite as e1.slice(lwr, upr)
6374 e = new DotIdExp(loc, e1, Id::slice);
6376 if (lwr)
6378 assert(upr);
6379 e = new CallExp(loc, e, lwr, upr);
6381 else
6382 { assert(!upr);
6383 e = new CallExp(loc, e);
6385 e = e->semantic(sc);
6386 return e;
6388 goto Lerror;
6390 else if (t->ty == Ttuple)
6392 if (!lwr && !upr)
6393 return e1;
6394 if (!lwr || !upr)
6395 { error("need upper and lower bound to slice tuple");
6396 goto Lerror;
6399 else
6400 goto Lerror;
6402 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple)
6404 sym = new ArrayScopeSymbol(this);
6405 sym->loc = loc;
6406 sym->parent = sc->scopesym;
6407 sc = sc->push(sym);
6410 if (lwr)
6411 { lwr = lwr->semantic(sc);
6412 lwr = resolveProperties(sc, lwr);
6413 lwr = lwr->implicitCastTo(sc, Type::tsize_t);
6415 if (upr)
6416 { upr = upr->semantic(sc);
6417 upr = resolveProperties(sc, upr);
6418 upr = upr->implicitCastTo(sc, Type::tsize_t);
6421 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple)
6422 sc->pop();
6424 if (t->ty == Ttuple)
6426 lwr = lwr->optimize(WANTvalue);
6427 upr = upr->optimize(WANTvalue);
6428 uinteger_t i1 = lwr->toUInteger();
6429 uinteger_t i2 = upr->toUInteger();
6431 size_t length;
6432 TupleExp *te;
6433 TypeTuple *tup;
6435 if (e1->op == TOKtuple) // slicing an expression tuple
6436 { te = (TupleExp *)e1;
6437 length = te->exps->dim;
6439 else if (e1->op == TOKtype) // slicing a type tuple
6440 { tup = (TypeTuple *)t;
6441 length = Argument::dim(tup->arguments);
6443 else
6444 assert(0);
6446 if (i1 <= i2 && i2 <= length)
6447 { size_t j1 = (size_t) i1;
6448 size_t j2 = (size_t) i2;
6450 if (e1->op == TOKtuple)
6451 { Expressions *exps = new Expressions;
6452 exps->setDim(j2 - j1);
6453 for (size_t i = 0; i < j2 - j1; i++)
6454 { Expression *e = (Expression *)te->exps->data[j1 + i];
6455 exps->data[i] = (void *)e;
6457 e = new TupleExp(loc, exps);
6459 else
6460 { Arguments *args = new Arguments;
6461 args->reserve(j2 - j1);
6462 for (size_t i = j1; i < j2; i++)
6463 { Argument *arg = Argument::getNth(tup->arguments, i);
6464 args->push(arg);
6466 e = new TypeExp(e1->loc, new TypeTuple(args));
6468 e = e->semantic(sc);
6470 else
6472 error("string slice [%"PRIuMAX" .. %"PRIuMAX"] is out of bounds", i1, i2);
6473 e = e1;
6475 return e;
6478 type = t->next->arrayOf();
6479 return e;
6481 Lerror:
6482 char *s;
6483 if (t->ty == Tvoid)
6484 s = e1->toChars();
6485 else
6486 s = t->toChars();
6487 error("%s cannot be sliced with []", s);
6488 type = Type::terror;
6489 return e;
6492 void SliceExp::checkEscape()
6494 e1->checkEscape();
6497 Expression *SliceExp::toLvalue(Scope *sc, Expression *e)
6499 return this;
6502 Expression *SliceExp::modifiableLvalue(Scope *sc, Expression *e)
6504 error("slice expression %s is not a modifiable lvalue", toChars());
6505 return this;
6508 void SliceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
6510 expToCBuffer(buf, hgs, e1, precedence[op]);
6511 buf->writeByte('[');
6512 if (upr || lwr)
6514 if (lwr)
6515 expToCBuffer(buf, hgs, lwr, PREC_assign);
6516 else
6517 buf->writeByte('0');
6518 buf->writestring("..");
6519 if (upr)
6520 expToCBuffer(buf, hgs, upr, PREC_assign);
6521 else
6522 buf->writestring("length"); // BUG: should be array.length
6524 buf->writeByte(']');
6527 /********************** ArrayLength **************************************/
6529 ArrayLengthExp::ArrayLengthExp(Loc loc, Expression *e1)
6530 : UnaExp(loc, TOKarraylength, sizeof(ArrayLengthExp), e1)
6534 Expression *ArrayLengthExp::semantic(Scope *sc)
6535 { Expression *e;
6537 #if LOGSEMANTIC
6538 printf("ArrayLengthExp::semantic('%s')\n", toChars());
6539 #endif
6540 if (!type)
6542 UnaExp::semantic(sc);
6543 e1 = resolveProperties(sc, e1);
6545 type = Type::tsize_t;
6547 return this;
6550 void ArrayLengthExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
6552 expToCBuffer(buf, hgs, e1, PREC_primary);
6553 buf->writestring(".length");
6556 /*********************** ArrayExp *************************************/
6558 // e1 [ i1, i2, i3, ... ]
6560 ArrayExp::ArrayExp(Loc loc, Expression *e1, Expressions *args)
6561 : UnaExp(loc, TOKarray, sizeof(ArrayExp), e1)
6563 arguments = args;
6566 Expression *ArrayExp::syntaxCopy()
6568 return new ArrayExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments));
6571 Expression *ArrayExp::semantic(Scope *sc)
6572 { Expression *e;
6573 Type *t1;
6575 #if LOGSEMANTIC
6576 printf("ArrayExp::semantic('%s')\n", toChars());
6577 #endif
6578 UnaExp::semantic(sc);
6579 e1 = resolveProperties(sc, e1);
6581 t1 = e1->type->toBasetype();
6582 if (t1->ty != Tclass && t1->ty != Tstruct)
6583 { // Convert to IndexExp
6584 if (arguments->dim != 1)
6585 error("only one index allowed to index %s", t1->toChars());
6586 e = new IndexExp(loc, e1, (Expression *)arguments->data[0]);
6587 return e->semantic(sc);
6590 // Run semantic() on each argument
6591 for (size_t i = 0; i < arguments->dim; i++)
6592 { e = (Expression *)arguments->data[i];
6594 e = e->semantic(sc);
6595 if (!e->type)
6596 error("%s has no value", e->toChars());
6597 arguments->data[i] = (void *)e;
6600 expandTuples(arguments);
6601 assert(arguments && arguments->dim);
6603 e = op_overload(sc);
6604 if (!e)
6605 { error("no [] operator overload for type %s", e1->type->toChars());
6606 e = e1;
6608 return e;
6612 Expression *ArrayExp::toLvalue(Scope *sc, Expression *e)
6614 if (type && type->toBasetype()->ty == Tvoid)
6615 error("voids have no value");
6616 return this;
6620 void ArrayExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
6621 { int i;
6623 expToCBuffer(buf, hgs, e1, PREC_primary);
6624 buf->writeByte('[');
6625 argsToCBuffer(buf, arguments, hgs);
6626 buf->writeByte(']');
6629 /************************* DotExp ***********************************/
6631 DotExp::DotExp(Loc loc, Expression *e1, Expression *e2)
6632 : BinExp(loc, TOKdotexp, sizeof(DotExp), e1, e2)
6636 Expression *DotExp::semantic(Scope *sc)
6638 #if LOGSEMANTIC
6639 printf("DotExp::semantic('%s')\n", toChars());
6640 if (type) printf("\ttype = %s\n", type->toChars());
6641 #endif
6642 e1 = e1->semantic(sc);
6643 e2 = e2->semantic(sc);
6644 if (e2->op == TOKimport)
6646 ScopeExp *se = (ScopeExp *)e2;
6647 TemplateDeclaration *td = se->sds->isTemplateDeclaration();
6648 if (td)
6649 { Expression *e = new DotTemplateExp(loc, e1, td);
6650 e = e->semantic(sc);
6651 return e;
6654 if (!type)
6655 type = e2->type;
6656 return this;
6660 /************************* CommaExp ***********************************/
6662 CommaExp::CommaExp(Loc loc, Expression *e1, Expression *e2)
6663 : BinExp(loc, TOKcomma, sizeof(CommaExp), e1, e2)
6667 Expression *CommaExp::semantic(Scope *sc)
6669 if (!type)
6670 { BinExp::semanticp(sc);
6671 type = e2->type;
6673 return this;
6676 void CommaExp::checkEscape()
6678 e2->checkEscape();
6681 Expression *CommaExp::toLvalue(Scope *sc, Expression *e)
6683 e2 = e2->toLvalue(sc, NULL);
6684 return this;
6687 Expression *CommaExp::modifiableLvalue(Scope *sc, Expression *e)
6689 e2 = e2->modifiableLvalue(sc, e);
6690 return this;
6693 int CommaExp::isBool(int result)
6695 return e2->isBool(result);
6698 int CommaExp::checkSideEffect(int flag)
6700 if (flag == 2)
6701 return e1->checkSideEffect(2) || e2->checkSideEffect(2);
6702 else
6704 // Don't check e1 until we cast(void) the a,b code generation
6705 return e2->checkSideEffect(flag);
6709 /************************** IndexExp **********************************/
6711 // e1 [ e2 ]
6713 IndexExp::IndexExp(Loc loc, Expression *e1, Expression *e2)
6714 : BinExp(loc, TOKindex, sizeof(IndexExp), e1, e2)
6716 //printf("IndexExp::IndexExp('%s')\n", toChars());
6717 lengthVar = NULL;
6718 modifiable = 0; // assume it is an rvalue
6721 Expression *IndexExp::semantic(Scope *sc)
6722 { Expression *e;
6723 BinExp *b;
6724 UnaExp *u;
6725 Type *t1;
6726 ScopeDsymbol *sym;
6728 #if LOGSEMANTIC
6729 printf("IndexExp::semantic('%s')\n", toChars());
6730 #endif
6731 if (type)
6732 return this;
6733 if (!e1->type)
6734 e1 = e1->semantic(sc);
6735 assert(e1->type); // semantic() should already be run on it
6736 e = this;
6738 // Note that unlike C we do not implement the int[ptr]
6740 t1 = e1->type->toBasetype();
6742 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple)
6743 { // Create scope for 'length' variable
6744 sym = new ArrayScopeSymbol(this);
6745 sym->loc = loc;
6746 sym->parent = sc->scopesym;
6747 sc = sc->push(sym);
6750 e2 = e2->semantic(sc);
6751 if (!e2->type)
6753 error("%s has no value", e2->toChars());
6754 e2->type = Type::terror;
6756 e2 = resolveProperties(sc, e2);
6758 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple)
6759 sc = sc->pop();
6761 switch (t1->ty)
6763 case Tpointer:
6764 case Tarray:
6765 e2 = e2->implicitCastTo(sc, Type::tsize_t);
6766 e->type = t1->next;
6767 break;
6769 case Tsarray:
6771 e2 = e2->implicitCastTo(sc, Type::tsize_t);
6773 TypeSArray *tsa = (TypeSArray *)t1;
6775 #if 0 // Don't do now, because it might be short-circuit evaluated
6776 // Do compile time array bounds checking if possible
6777 e2 = e2->optimize(WANTvalue);
6778 if (e2->op == TOKint64)
6780 integer_t index = e2->toInteger();
6781 integer_t length = tsa->dim->toInteger();
6782 if (index < 0 || index >= length)
6783 error("array index [%lld] is outside array bounds [0 .. %lld]",
6784 index, length);
6786 #endif
6787 e->type = t1->next;
6788 break;
6791 case Taarray:
6792 { TypeAArray *taa = (TypeAArray *)t1;
6794 e2 = e2->implicitCastTo(sc, taa->index); // type checking
6795 e2 = e2->implicitCastTo(sc, taa->key); // actual argument type
6796 type = taa->next;
6797 break;
6800 case Ttuple:
6802 e2 = e2->implicitCastTo(sc, Type::tsize_t);
6803 e2 = e2->optimize(WANTvalue);
6804 uinteger_t index = e2->toUInteger();
6805 size_t length;
6806 TupleExp *te;
6807 TypeTuple *tup;
6809 if (e1->op == TOKtuple)
6810 { te = (TupleExp *)e1;
6811 length = te->exps->dim;
6813 else if (e1->op == TOKtype)
6815 tup = (TypeTuple *)t1;
6816 length = Argument::dim(tup->arguments);
6818 else
6819 assert(0);
6821 if (index < length)
6824 if (e1->op == TOKtuple)
6825 e = (Expression *)te->exps->data[(size_t)index];
6826 else
6827 e = new TypeExp(e1->loc, Argument::getNth(tup->arguments, (size_t)index)->type);
6829 else
6831 error("array index [%"PRIuMAX"] is outside array bounds [0 .. %"PRIuSIZE"]",
6832 index, length);
6833 e = e1;
6835 break;
6838 default:
6839 error("%s must be an array or pointer type, not %s",
6840 e1->toChars(), e1->type->toChars());
6841 type = Type::tint32;
6842 break;
6844 return e;
6847 Expression *IndexExp::toLvalue(Scope *sc, Expression *e)
6849 // if (type && type->toBasetype()->ty == Tvoid)
6850 // error("voids have no value");
6851 return this;
6854 Expression *IndexExp::modifiableLvalue(Scope *sc, Expression *e)
6856 //printf("IndexExp::modifiableLvalue(%s)\n", toChars());
6857 modifiable = 1;
6858 if (e1->op == TOKstring)
6859 error("string literals are immutable");
6860 if (e1->type->toBasetype()->ty == Taarray)
6861 e1 = e1->modifiableLvalue(sc, e1);
6862 return toLvalue(sc, e);
6865 void IndexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
6867 expToCBuffer(buf, hgs, e1, PREC_primary);
6868 buf->writeByte('[');
6869 expToCBuffer(buf, hgs, e2, PREC_assign);
6870 buf->writeByte(']');
6874 /************************* PostExp ***********************************/
6876 PostExp::PostExp(enum TOK op, Loc loc, Expression *e)
6877 : BinExp(loc, op, sizeof(PostExp), e,
6878 new IntegerExp(loc, 1, Type::tint32))
6882 Expression *PostExp::semantic(Scope *sc)
6883 { Expression *e = this;
6885 if (!type)
6887 BinExp::semantic(sc);
6888 e2 = resolveProperties(sc, e2);
6890 e = op_overload(sc);
6891 if (e)
6892 return e;
6894 e = this;
6895 e1 = e1->modifiableLvalue(sc, e1);
6896 e1->checkScalar();
6897 e1->checkNoBool();
6898 if (e1->type->ty == Tpointer)
6899 e = scaleFactor(sc);
6900 else
6901 e2 = e2->castTo(sc, e1->type);
6902 e->type = e1->type;
6904 return e;
6907 void PostExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
6909 expToCBuffer(buf, hgs, e1, precedence[op]);
6910 buf->writestring((op == TOKplusplus) ? (char *)"++" : (char *)"--");
6913 /************************************************************/
6915 /* Can be TOKconstruct too */
6917 AssignExp::AssignExp(Loc loc, Expression *e1, Expression *e2)
6918 : BinExp(loc, TOKassign, sizeof(AssignExp), e1, e2)
6920 ismemset = 0;
6923 Expression *AssignExp::semantic(Scope *sc)
6924 { Type *t1;
6925 Expression *e1old = e1;
6927 #if LOGSEMANTIC
6928 printf("AssignExp::semantic('%s')\n", toChars());
6929 #endif
6930 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op));
6932 /* Look for operator overloading of a[i]=value.
6933 * Do it before semantic() otherwise the a[i] will have been
6934 * converted to a.opIndex() already.
6936 if (e1->op == TOKarray)
6937 { Type *t1;
6938 ArrayExp *ae = (ArrayExp *)e1;
6939 AggregateDeclaration *ad;
6940 Identifier *id = Id::index;
6942 ae->e1 = ae->e1->semantic(sc);
6943 t1 = ae->e1->type->toBasetype();
6944 if (t1->ty == Tstruct)
6946 ad = ((TypeStruct *)t1)->sym;
6947 goto L1;
6949 else if (t1->ty == Tclass)
6951 ad = ((TypeClass *)t1)->sym;
6953 // Rewrite (a[i] = value) to (a.opIndexAssign(value, i))
6954 if (search_function(ad, Id::indexass))
6955 { Expression *e = new DotIdExp(loc, ae->e1, Id::indexass);
6956 Expressions *a = (Expressions *)ae->arguments->copy();
6958 a->insert(0, e2);
6959 e = new CallExp(loc, e, a);
6960 e = e->semantic(sc);
6961 return e;
6963 else
6965 // Rewrite (a[i] = value) to (a.opIndex(i, value))
6966 if (search_function(ad, id))
6967 { Expression *e = new DotIdExp(loc, ae->e1, id);
6969 if (1 || !global.params.useDeprecated)
6970 error("operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)");
6972 e = new CallExp(loc, e, (Expression *)ae->arguments->data[0], e2);
6973 e = e->semantic(sc);
6974 return e;
6979 /* Look for operator overloading of a[i..j]=value.
6980 * Do it before semantic() otherwise the a[i..j] will have been
6981 * converted to a.opSlice() already.
6983 if (e1->op == TOKslice)
6984 { Type *t1;
6985 SliceExp *ae = (SliceExp *)e1;
6986 AggregateDeclaration *ad;
6987 Identifier *id = Id::index;
6989 ae->e1 = ae->e1->semantic(sc);
6990 ae->e1 = resolveProperties(sc, ae->e1);
6991 t1 = ae->e1->type->toBasetype();
6992 if (t1->ty == Tstruct)
6994 ad = ((TypeStruct *)t1)->sym;
6995 goto L2;
6997 else if (t1->ty == Tclass)
6999 ad = ((TypeClass *)t1)->sym;
7001 // Rewrite (a[i..j] = value) to (a.opIndexAssign(value, i, j))
7002 if (search_function(ad, Id::sliceass))
7003 { Expression *e = new DotIdExp(loc, ae->e1, Id::sliceass);
7004 Expressions *a = new Expressions();
7006 a->push(e2);
7007 if (ae->lwr)
7008 { a->push(ae->lwr);
7009 assert(ae->upr);
7010 a->push(ae->upr);
7012 else
7013 assert(!ae->upr);
7014 e = new CallExp(loc, e, a);
7015 e = e->semantic(sc);
7016 return e;
7021 BinExp::semantic(sc);
7022 e2 = resolveProperties(sc, e2);
7023 assert(e1->type);
7025 /* Rewrite tuple assignment as a tuple of assignments.
7027 if (e1->op == TOKtuple && e2->op == TOKtuple)
7028 { TupleExp *tup1 = (TupleExp *)e1;
7029 TupleExp *tup2 = (TupleExp *)e2;
7030 size_t dim = tup1->exps->dim;
7031 if (dim != tup2->exps->dim)
7033 error("mismatched tuple lengths, %d and %d", (int)dim, (int)tup2->exps->dim);
7035 else
7036 { Expressions *exps = new Expressions;
7037 exps->setDim(dim);
7039 for (int i = 0; i < dim; i++)
7040 { Expression *ex1 = (Expression *)tup1->exps->data[i];
7041 Expression *ex2 = (Expression *)tup2->exps->data[i];
7042 exps->data[i] = (void *) new AssignExp(loc, ex1, ex2);
7044 Expression *e = new TupleExp(loc, exps);
7045 e = e->semantic(sc);
7046 return e;
7050 t1 = e1->type->toBasetype();
7052 if (t1->ty == Tfunction)
7053 { // Rewrite f=value to f(value)
7054 Expression *e;
7056 e = new CallExp(loc, e1, e2);
7057 e = e->semantic(sc);
7058 return e;
7061 /* If it is an assignment from a 'foreign' type,
7062 * check for operator overloading.
7064 if (t1->ty == Tclass || t1->ty == Tstruct)
7066 if (!e2->type->implicitConvTo(e1->type))
7068 Expression *e = op_overload(sc);
7069 if (e)
7070 return e;
7074 e2->rvalue();
7076 if (e1->op == TOKarraylength)
7078 // e1 is not an lvalue, but we let code generator handle it
7079 ArrayLengthExp *ale = (ArrayLengthExp *)e1;
7081 ale->e1 = ale->e1->modifiableLvalue(sc, e1);
7083 else if (e1->op == TOKslice)
7085 else
7086 { // Try to do a decent error message with the expression
7087 // before it got constant folded
7088 e1 = e1->modifiableLvalue(sc, e1old);
7091 if (e1->op == TOKslice &&
7092 t1->nextOf() &&
7093 e2->implicitConvTo(t1->nextOf())
7094 // !(t1->nextOf()->equals(e2->type->nextOf()))
7096 { // memset
7097 ismemset = 1; // make it easy for back end to tell what this is
7098 e2 = e2->implicitCastTo(sc, t1->next);
7100 else if (t1->ty == Tsarray)
7102 error("cannot assign to static array %s", e1->toChars());
7104 else
7106 e2 = e2->implicitCastTo(sc, e1->type);
7108 type = e1->type;
7109 assert(type);
7110 return this;
7113 Expression *AssignExp::checkToBoolean()
7115 // Things like:
7116 // if (a = b) ...
7117 // are usually mistakes.
7119 error("'=' does not give a boolean result");
7120 return this;
7123 /************************************************************/
7125 AddAssignExp::AddAssignExp(Loc loc, Expression *e1, Expression *e2)
7126 : BinExp(loc, TOKaddass, sizeof(AddAssignExp), e1, e2)
7130 Expression *AddAssignExp::semantic(Scope *sc)
7131 { Expression *e;
7133 if (type)
7134 return this;
7136 BinExp::semantic(sc);
7137 e2 = resolveProperties(sc, e2);
7139 e = op_overload(sc);
7140 if (e)
7141 return e;
7143 e1 = e1->modifiableLvalue(sc, e1);
7145 Type *tb1 = e1->type->toBasetype();
7146 Type *tb2 = e2->type->toBasetype();
7148 if ((tb1->ty == Tarray || tb1->ty == Tsarray) &&
7149 (tb2->ty == Tarray || tb2->ty == Tsarray) &&
7150 tb1->next->equals(tb2->next)
7153 type = e1->type;
7154 e = this;
7156 else
7158 e1->checkScalar();
7159 e1->checkNoBool();
7160 if (tb1->ty == Tpointer && tb2->isintegral())
7161 e = scaleFactor(sc);
7162 else if (tb1->ty == Tbit || tb1->ty == Tbool)
7164 #if 0
7165 // Need to rethink this
7166 if (e1->op != TOKvar)
7167 { // Rewrite e1+=e2 to (v=&e1),*v=*v+e2
7168 VarDeclaration *v;
7169 Expression *ea;
7170 Expression *ex;
7172 char name[6+6+1];
7173 Identifier *id;
7174 static int idn;
7175 sprintf(name, "__name%d", ++idn);
7176 id = Lexer::idPool(name);
7178 v = new VarDeclaration(loc, tb1->pointerTo(), id, NULL);
7179 v->semantic(sc);
7180 if (!sc->insert(v))
7181 assert(0);
7182 v->parent = sc->func;
7184 ea = new AddrExp(loc, e1);
7185 ea = new AssignExp(loc, new VarExp(loc, v), ea);
7187 ex = new VarExp(loc, v);
7188 ex = new PtrExp(loc, ex);
7189 e = new AddExp(loc, ex, e2);
7190 e = new CastExp(loc, e, e1->type);
7191 e = new AssignExp(loc, ex->syntaxCopy(), e);
7193 e = new CommaExp(loc, ea, e);
7195 else
7196 #endif
7197 { // Rewrite e1+=e2 to e1=e1+e2
7198 // BUG: doesn't account for side effects in e1
7199 // BUG: other assignment operators for bits aren't handled at all
7200 e = new AddExp(loc, e1, e2);
7201 e = new CastExp(loc, e, e1->type);
7202 e = new AssignExp(loc, e1->syntaxCopy(), e);
7204 e = e->semantic(sc);
7206 else
7208 type = e1->type;
7209 typeCombine(sc);
7210 e1->checkArithmetic();
7211 e2->checkArithmetic();
7212 if (type->isreal() || type->isimaginary())
7214 assert(global.errors || e2->type->isfloating());
7215 e2 = e2->castTo(sc, e1->type);
7217 e = this;
7220 return e;
7223 /************************************************************/
7225 MinAssignExp::MinAssignExp(Loc loc, Expression *e1, Expression *e2)
7226 : BinExp(loc, TOKminass, sizeof(MinAssignExp), e1, e2)
7230 Expression *MinAssignExp::semantic(Scope *sc)
7231 { Expression *e;
7233 if (type)
7234 return this;
7236 BinExp::semantic(sc);
7237 e2 = resolveProperties(sc, e2);
7239 e = op_overload(sc);
7240 if (e)
7241 return e;
7243 e1 = e1->modifiableLvalue(sc, e1);
7244 e1->checkScalar();
7245 e1->checkNoBool();
7246 if (e1->type->ty == Tpointer && e2->type->isintegral())
7247 e = scaleFactor(sc);
7248 else
7250 e1 = e1->checkArithmetic();
7251 e2 = e2->checkArithmetic();
7252 type = e1->type;
7253 typeCombine(sc);
7254 if (type->isreal() || type->isimaginary())
7256 assert(e2->type->isfloating());
7257 e2 = e2->castTo(sc, e1->type);
7259 e = this;
7261 return e;
7264 /************************************************************/
7266 CatAssignExp::CatAssignExp(Loc loc, Expression *e1, Expression *e2)
7267 : BinExp(loc, TOKcatass, sizeof(CatAssignExp), e1, e2)
7271 Expression *CatAssignExp::semantic(Scope *sc)
7272 { Expression *e;
7274 BinExp::semantic(sc);
7275 e2 = resolveProperties(sc, e2);
7277 e = op_overload(sc);
7278 if (e)
7279 return e;
7281 if (e1->op == TOKslice)
7282 { SliceExp *se = (SliceExp *)e1;
7284 if (se->e1->type->toBasetype()->ty == Tsarray)
7285 error("cannot append to static array %s", se->e1->type->toChars());
7288 e1 = e1->modifiableLvalue(sc, e1);
7290 Type *tb1 = e1->type->toBasetype();
7291 Type *tb2 = e2->type->toBasetype();
7293 e2->rvalue();
7295 if ((tb1->ty == Tarray) &&
7296 (tb2->ty == Tarray || tb2->ty == Tsarray) &&
7297 e2->implicitConvTo(e1->type)
7298 //e1->type->next->equals(e2->type->next)
7300 { // Append array
7301 e2 = e2->castTo(sc, e1->type);
7302 type = e1->type;
7303 e = this;
7305 else if ((tb1->ty == Tarray) &&
7306 e2->implicitConvTo(tb1->next)
7308 { // Append element
7309 e2 = e2->castTo(sc, tb1->next);
7310 type = e1->type;
7311 e = this;
7313 else
7315 error("cannot append type %s to type %s", tb2->toChars(), tb1->toChars());
7316 type = Type::tint32;
7317 e = this;
7319 return e;
7322 /************************************************************/
7324 MulAssignExp::MulAssignExp(Loc loc, Expression *e1, Expression *e2)
7325 : BinExp(loc, TOKmulass, sizeof(MulAssignExp), e1, e2)
7329 Expression *MulAssignExp::semantic(Scope *sc)
7330 { Expression *e;
7332 BinExp::semantic(sc);
7333 e2 = resolveProperties(sc, e2);
7335 e = op_overload(sc);
7336 if (e)
7337 return e;
7339 e1 = e1->modifiableLvalue(sc, e1);
7340 e1->checkScalar();
7341 e1->checkNoBool();
7342 type = e1->type;
7343 typeCombine(sc);
7344 e1->checkArithmetic();
7345 e2->checkArithmetic();
7346 if (e2->type->isfloating())
7347 { Type *t1;
7348 Type *t2;
7350 t1 = e1->type;
7351 t2 = e2->type;
7352 if (t1->isreal())
7354 if (t2->isimaginary() || t2->iscomplex())
7356 e2 = e2->castTo(sc, t1);
7359 else if (t1->isimaginary())
7361 if (t2->isimaginary() || t2->iscomplex())
7363 switch (t1->ty)
7365 case Timaginary32: t2 = Type::tfloat32; break;
7366 case Timaginary64: t2 = Type::tfloat64; break;
7367 case Timaginary80: t2 = Type::tfloat80; break;
7368 default:
7369 assert(0);
7371 e2 = e2->castTo(sc, t2);
7375 return this;
7378 /************************************************************/
7380 DivAssignExp::DivAssignExp(Loc loc, Expression *e1, Expression *e2)
7381 : BinExp(loc, TOKdivass, sizeof(DivAssignExp), e1, e2)
7385 Expression *DivAssignExp::semantic(Scope *sc)
7386 { Expression *e;
7388 BinExp::semantic(sc);
7389 e2 = resolveProperties(sc, e2);
7391 e = op_overload(sc);
7392 if (e)
7393 return e;
7395 e1 = e1->modifiableLvalue(sc, e1);
7396 e1->checkScalar();
7397 e1->checkNoBool();
7398 type = e1->type;
7399 typeCombine(sc);
7400 e1->checkArithmetic();
7401 e2->checkArithmetic();
7402 if (e2->type->isimaginary())
7403 { Type *t1;
7404 Type *t2;
7406 t1 = e1->type;
7407 if (t1->isreal())
7408 { // x/iv = i(-x/v)
7409 // Therefore, the result is 0
7410 e2 = new CommaExp(loc, e2, new RealExp(loc, 0, t1));
7411 e2->type = t1;
7412 e = new AssignExp(loc, e1, e2);
7413 e->type = t1;
7414 return e;
7416 else if (t1->isimaginary())
7417 { Expression *e;
7419 switch (t1->ty)
7421 case Timaginary32: t2 = Type::tfloat32; break;
7422 case Timaginary64: t2 = Type::tfloat64; break;
7423 case Timaginary80: t2 = Type::tfloat80; break;
7424 default:
7425 assert(0);
7427 e2 = e2->castTo(sc, t2);
7428 e = new AssignExp(loc, e1, e2);
7429 e->type = t1;
7430 return e;
7433 return this;
7436 /************************************************************/
7438 ModAssignExp::ModAssignExp(Loc loc, Expression *e1, Expression *e2)
7439 : BinExp(loc, TOKmodass, sizeof(ModAssignExp), e1, e2)
7443 Expression *ModAssignExp::semantic(Scope *sc)
7445 return commonSemanticAssign(sc);
7448 /************************************************************/
7450 ShlAssignExp::ShlAssignExp(Loc loc, Expression *e1, Expression *e2)
7451 : BinExp(loc, TOKshlass, sizeof(ShlAssignExp), e1, e2)
7455 Expression *ShlAssignExp::semantic(Scope *sc)
7456 { Expression *e;
7458 //printf("ShlAssignExp::semantic()\n");
7459 BinExp::semantic(sc);
7460 e2 = resolveProperties(sc, e2);
7462 e = op_overload(sc);
7463 if (e)
7464 return e;
7466 e1 = e1->modifiableLvalue(sc, e1);
7467 e1->checkScalar();
7468 e1->checkNoBool();
7469 type = e1->type;
7470 typeCombine(sc);
7471 e1->checkIntegral();
7472 e2 = e2->checkIntegral();
7473 e2 = e2->castTo(sc, Type::tshiftcnt);
7474 return this;
7477 /************************************************************/
7479 ShrAssignExp::ShrAssignExp(Loc loc, Expression *e1, Expression *e2)
7480 : BinExp(loc, TOKshrass, sizeof(ShrAssignExp), e1, e2)
7484 Expression *ShrAssignExp::semantic(Scope *sc)
7485 { Expression *e;
7487 BinExp::semantic(sc);
7488 e2 = resolveProperties(sc, e2);
7490 e = op_overload(sc);
7491 if (e)
7492 return e;
7494 e1 = e1->modifiableLvalue(sc, e1);
7495 e1->checkScalar();
7496 e1->checkNoBool();
7497 type = e1->type;
7498 typeCombine(sc);
7499 e1->checkIntegral();
7500 e2 = e2->checkIntegral();
7501 e2 = e2->castTo(sc, Type::tshiftcnt);
7502 return this;
7505 /************************************************************/
7507 UshrAssignExp::UshrAssignExp(Loc loc, Expression *e1, Expression *e2)
7508 : BinExp(loc, TOKushrass, sizeof(UshrAssignExp), e1, e2)
7512 Expression *UshrAssignExp::semantic(Scope *sc)
7513 { Expression *e;
7515 BinExp::semantic(sc);
7516 e2 = resolveProperties(sc, e2);
7518 e = op_overload(sc);
7519 if (e)
7520 return e;
7522 e1 = e1->modifiableLvalue(sc, e1);
7523 e1->checkScalar();
7524 e1->checkNoBool();
7525 type = e1->type;
7526 typeCombine(sc);
7527 e1->checkIntegral();
7528 e2 = e2->checkIntegral();
7529 e2 = e2->castTo(sc, Type::tshiftcnt);
7530 return this;
7533 /************************************************************/
7535 AndAssignExp::AndAssignExp(Loc loc, Expression *e1, Expression *e2)
7536 : BinExp(loc, TOKandass, sizeof(AndAssignExp), e1, e2)
7540 Expression *AndAssignExp::semantic(Scope *sc)
7542 return commonSemanticAssignIntegral(sc);
7545 /************************************************************/
7547 OrAssignExp::OrAssignExp(Loc loc, Expression *e1, Expression *e2)
7548 : BinExp(loc, TOKorass, sizeof(OrAssignExp), e1, e2)
7552 Expression *OrAssignExp::semantic(Scope *sc)
7554 return commonSemanticAssignIntegral(sc);
7557 /************************************************************/
7559 XorAssignExp::XorAssignExp(Loc loc, Expression *e1, Expression *e2)
7560 : BinExp(loc, TOKxorass, sizeof(XorAssignExp), e1, e2)
7564 Expression *XorAssignExp::semantic(Scope *sc)
7566 return commonSemanticAssignIntegral(sc);
7569 /************************* AddExp *****************************/
7571 AddExp::AddExp(Loc loc, Expression *e1, Expression *e2)
7572 : BinExp(loc, TOKadd, sizeof(AddExp), e1, e2)
7576 Expression *AddExp::semantic(Scope *sc)
7577 { Expression *e;
7579 #if LOGSEMANTIC
7580 printf("AddExp::semantic('%s')\n", toChars());
7581 #endif
7582 if (!type)
7584 BinExp::semanticp(sc);
7586 e = op_overload(sc);
7587 if (e)
7588 return e;
7590 Type *tb1 = e1->type->toBasetype();
7591 Type *tb2 = e2->type->toBasetype();
7593 if ((tb1->ty == Tarray || tb1->ty == Tsarray) &&
7594 (tb2->ty == Tarray || tb2->ty == Tsarray) &&
7595 tb1->next->equals(tb2->next)
7598 type = e1->type;
7599 e = this;
7601 else if (tb1->ty == Tpointer && e2->type->isintegral() ||
7602 tb2->ty == Tpointer && e1->type->isintegral())
7603 e = scaleFactor(sc);
7604 else if (tb1->ty == Tpointer && tb2->ty == Tpointer)
7606 incompatibleTypes();
7607 type = e1->type;
7608 e = this;
7610 else
7612 typeCombine(sc);
7613 if ((e1->type->isreal() && e2->type->isimaginary()) ||
7614 (e1->type->isimaginary() && e2->type->isreal()))
7616 switch (type->toBasetype()->ty)
7618 case Tfloat32:
7619 case Timaginary32:
7620 type = Type::tcomplex32;
7621 break;
7623 case Tfloat64:
7624 case Timaginary64:
7625 type = Type::tcomplex64;
7626 break;
7628 case Tfloat80:
7629 case Timaginary80:
7630 type = Type::tcomplex80;
7631 break;
7633 default:
7634 assert(0);
7637 e = this;
7639 return e;
7641 return this;
7644 /************************************************************/
7646 MinExp::MinExp(Loc loc, Expression *e1, Expression *e2)
7647 : BinExp(loc, TOKmin, sizeof(MinExp), e1, e2)
7651 Expression *MinExp::semantic(Scope *sc)
7652 { Expression *e;
7653 Type *t1;
7654 Type *t2;
7656 #if LOGSEMANTIC
7657 printf("MinExp::semantic('%s')\n", toChars());
7658 #endif
7659 if (type)
7660 return this;
7662 BinExp::semanticp(sc);
7664 e = op_overload(sc);
7665 if (e)
7666 return e;
7668 e = this;
7669 t1 = e1->type->toBasetype();
7670 t2 = e2->type->toBasetype();
7671 if (t1->ty == Tpointer)
7673 if (t2->ty == Tpointer)
7674 { // Need to divide the result by the stride
7675 // Replace (ptr - ptr) with (ptr - ptr) / stride
7676 d_int64 stride;
7677 Expression *e;
7679 typeCombine(sc); // make sure pointer types are compatible
7680 type = Type::tptrdiff_t;
7681 stride = t2->next->size();
7682 e = new DivExp(loc, this, new IntegerExp(0, stride, Type::tptrdiff_t));
7683 e->type = Type::tptrdiff_t;
7684 return e;
7686 else if (t2->isintegral())
7687 e = scaleFactor(sc);
7688 else
7689 { error("incompatible types for -");
7690 return new IntegerExp(0);
7693 else if (t2->ty == Tpointer)
7695 type = e2->type;
7696 error("can't subtract pointer from %s", e1->type->toChars());
7697 return new IntegerExp(0);
7699 else
7701 typeCombine(sc);
7702 t1 = e1->type->toBasetype();
7703 t2 = e2->type->toBasetype();
7704 if ((t1->isreal() && t2->isimaginary()) ||
7705 (t1->isimaginary() && t2->isreal()))
7707 switch (type->ty)
7709 case Tfloat32:
7710 case Timaginary32:
7711 type = Type::tcomplex32;
7712 break;
7714 case Tfloat64:
7715 case Timaginary64:
7716 type = Type::tcomplex64;
7717 break;
7719 case Tfloat80:
7720 case Timaginary80:
7721 type = Type::tcomplex80;
7722 break;
7724 default:
7725 assert(0);
7729 return e;
7732 /************************* CatExp *****************************/
7734 CatExp::CatExp(Loc loc, Expression *e1, Expression *e2)
7735 : BinExp(loc, TOKcat, sizeof(CatExp), e1, e2)
7739 Expression *CatExp::semantic(Scope *sc)
7740 { Expression *e;
7742 //printf("CatExp::semantic() %s\n", toChars());
7743 if (!type)
7745 BinExp::semanticp(sc);
7746 e = op_overload(sc);
7747 if (e)
7748 return e;
7750 Type *tb1 = e1->type->toBasetype();
7751 Type *tb2 = e2->type->toBasetype();
7754 /* BUG: Should handle things like:
7755 * char c;
7756 * c ~ ' '
7757 * ' ' ~ c;
7760 #if 0
7761 e1->type->print();
7762 e2->type->print();
7763 #endif
7764 if ((tb1->ty == Tsarray || tb1->ty == Tarray) &&
7765 e2->type->equals(tb1->next))
7767 type = tb1->next->arrayOf();
7768 if (tb2->ty == Tarray)
7769 { // Make e2 into [e2]
7770 e2 = new ArrayLiteralExp(e2->loc, e2);
7771 e2->type = type;
7773 return this;
7775 else if ((tb2->ty == Tsarray || tb2->ty == Tarray) &&
7776 e1->type->equals(tb2->next))
7778 type = tb2->next->arrayOf();
7779 if (tb1->ty == Tarray)
7780 { // Make e1 into [e1]
7781 e1 = new ArrayLiteralExp(e1->loc, e1);
7782 e1->type = type;
7784 return this;
7787 typeCombine(sc);
7789 if (type->ty == Tmaybe)
7790 error("Cannot join arrays that may be null (%s)", toChars());
7792 if (type->toBasetype()->ty == Tsarray)
7793 type = type->toBasetype()->next->arrayOf();
7794 #if 0
7795 e1->type->print();
7796 e2->type->print();
7797 type->print();
7798 print();
7799 #endif
7800 if (e1->op == TOKstring && e2->op == TOKstring)
7801 e = optimize(WANTvalue);
7802 else if (e1->type->equals(e2->type) &&
7803 (e1->type->toBasetype()->ty == Tarray ||
7804 e1->type->toBasetype()->ty == Tsarray))
7806 e = this;
7808 else
7810 error("Can only concatenate arrays, not (%s ~ %s)",
7811 e1->type->toChars(), e2->type->toChars());
7812 type = Type::tint32;
7813 e = this;
7815 e->type = e->type->semantic(loc, sc);
7816 return e;
7818 return this;
7821 /************************************************************/
7823 MulExp::MulExp(Loc loc, Expression *e1, Expression *e2)
7824 : BinExp(loc, TOKmul, sizeof(MulExp), e1, e2)
7828 Expression *MulExp::semantic(Scope *sc)
7829 { Expression *e;
7831 #if 0
7832 printf("MulExp::semantic() %s\n", toChars());
7833 #endif
7834 if (type)
7836 return this;
7839 BinExp::semanticp(sc);
7840 e = op_overload(sc);
7841 if (e)
7842 return e;
7844 typeCombine(sc);
7845 e1->checkArithmetic();
7846 e2->checkArithmetic();
7847 if (type->isfloating())
7848 { Type *t1 = e1->type;
7849 Type *t2 = e2->type;
7851 if (t1->isreal())
7853 type = t2;
7855 else if (t2->isreal())
7857 type = t1;
7859 else if (t1->isimaginary())
7861 if (t2->isimaginary())
7862 { Expression *e;
7864 switch (t1->ty)
7866 case Timaginary32: type = Type::tfloat32; break;
7867 case Timaginary64: type = Type::tfloat64; break;
7868 case Timaginary80: type = Type::tfloat80; break;
7869 default: assert(0);
7872 // iy * iv = -yv
7873 e1->type = type;
7874 e2->type = type;
7875 e = new NegExp(loc, this);
7876 e = e->semantic(sc);
7877 return e;
7879 else
7880 type = t2; // t2 is complex
7882 else if (t2->isimaginary())
7884 type = t1; // t1 is complex
7887 return this;
7890 /************************************************************/
7892 DivExp::DivExp(Loc loc, Expression *e1, Expression *e2)
7893 : BinExp(loc, TOKdiv, sizeof(DivExp), e1, e2)
7897 Expression *DivExp::semantic(Scope *sc)
7898 { Expression *e;
7900 if (type)
7901 return this;
7903 BinExp::semanticp(sc);
7904 e = op_overload(sc);
7905 if (e)
7906 return e;
7908 typeCombine(sc);
7909 e1->checkArithmetic();
7910 e2->checkArithmetic();
7911 if (type->isfloating())
7912 { Type *t1 = e1->type;
7913 Type *t2 = e2->type;
7915 if (t1->isreal())
7917 type = t2;
7918 if (t2->isimaginary())
7919 { Expression *e;
7921 // x/iv = i(-x/v)
7922 e2->type = t1;
7923 e = new NegExp(loc, this);
7924 e = e->semantic(sc);
7925 return e;
7928 else if (t2->isreal())
7930 type = t1;
7932 else if (t1->isimaginary())
7934 if (t2->isimaginary())
7936 switch (t1->ty)
7938 case Timaginary32: type = Type::tfloat32; break;
7939 case Timaginary64: type = Type::tfloat64; break;
7940 case Timaginary80: type = Type::tfloat80; break;
7941 default: assert(0);
7944 else
7945 type = t2; // t2 is complex
7947 else if (t2->isimaginary())
7949 type = t1; // t1 is complex
7952 return this;
7955 /************************************************************/
7957 ModExp::ModExp(Loc loc, Expression *e1, Expression *e2)
7958 : BinExp(loc, TOKmod, sizeof(ModExp), e1, e2)
7962 Expression *ModExp::semantic(Scope *sc)
7963 { Expression *e;
7965 if (type)
7966 return this;
7968 BinExp::semanticp(sc);
7969 e = op_overload(sc);
7970 if (e)
7971 return e;
7973 typeCombine(sc);
7974 e1->checkArithmetic();
7975 e2->checkArithmetic();
7976 if (type->isfloating())
7977 { type = e1->type;
7978 if (e2->type->iscomplex())
7979 { error("cannot perform modulo complex arithmetic");
7980 return new IntegerExp(0);
7983 return this;
7986 /************************************************************/
7988 ShlExp::ShlExp(Loc loc, Expression *e1, Expression *e2)
7989 : BinExp(loc, TOKshl, sizeof(ShlExp), e1, e2)
7993 Expression *ShlExp::semantic(Scope *sc)
7994 { Expression *e;
7996 //printf("ShlExp::semantic(), type = %p\n", type);
7997 if (!type)
7998 { BinExp::semanticp(sc);
7999 e = op_overload(sc);
8000 if (e)
8001 return e;
8002 e1 = e1->checkIntegral();
8003 e2 = e2->checkIntegral();
8004 e1 = e1->integralPromotions(sc);
8005 e2 = e2->castTo(sc, Type::tshiftcnt);
8006 type = e1->type;
8008 return this;
8011 /************************************************************/
8013 ShrExp::ShrExp(Loc loc, Expression *e1, Expression *e2)
8014 : BinExp(loc, TOKshr, sizeof(ShrExp), e1, e2)
8018 Expression *ShrExp::semantic(Scope *sc)
8019 { Expression *e;
8021 if (!type)
8022 { BinExp::semanticp(sc);
8023 e = op_overload(sc);
8024 if (e)
8025 return e;
8026 e1 = e1->checkIntegral();
8027 e2 = e2->checkIntegral();
8028 e1 = e1->integralPromotions(sc);
8029 e2 = e2->castTo(sc, Type::tshiftcnt);
8030 type = e1->type;
8032 return this;
8035 /************************************************************/
8037 UshrExp::UshrExp(Loc loc, Expression *e1, Expression *e2)
8038 : BinExp(loc, TOKushr, sizeof(UshrExp), e1, e2)
8042 Expression *UshrExp::semantic(Scope *sc)
8043 { Expression *e;
8045 if (!type)
8046 { BinExp::semanticp(sc);
8047 e = op_overload(sc);
8048 if (e)
8049 return e;
8050 e1 = e1->checkIntegral();
8051 e2 = e2->checkIntegral();
8052 e1 = e1->integralPromotions(sc);
8053 e2 = e2->castTo(sc, Type::tshiftcnt);
8054 type = e1->type;
8056 return this;
8059 /************************************************************/
8061 AndExp::AndExp(Loc loc, Expression *e1, Expression *e2)
8062 : BinExp(loc, TOKand, sizeof(AndExp), e1, e2)
8066 Expression *AndExp::semantic(Scope *sc)
8067 { Expression *e;
8069 if (!type)
8070 { BinExp::semanticp(sc);
8071 e = op_overload(sc);
8072 if (e)
8073 return e;
8074 if (e1->type->toBasetype()->ty == Tbool &&
8075 e2->type->toBasetype()->ty == Tbool)
8077 type = e1->type;
8078 e = this;
8080 else
8082 typeCombine(sc);
8083 e1->checkIntegral();
8084 e2->checkIntegral();
8087 return this;
8090 /************************************************************/
8092 OrExp::OrExp(Loc loc, Expression *e1, Expression *e2)
8093 : BinExp(loc, TOKor, sizeof(OrExp), e1, e2)
8097 Expression *OrExp::semantic(Scope *sc)
8098 { Expression *e;
8100 if (!type)
8101 { BinExp::semanticp(sc);
8102 e = op_overload(sc);
8103 if (e)
8104 return e;
8105 if (e1->type->toBasetype()->ty == Tbool &&
8106 e2->type->toBasetype()->ty == Tbool)
8108 type = e1->type;
8109 e = this;
8111 else
8113 typeCombine(sc);
8114 e1->checkIntegral();
8115 e2->checkIntegral();
8118 return this;
8121 /************************************************************/
8123 XorExp::XorExp(Loc loc, Expression *e1, Expression *e2)
8124 : BinExp(loc, TOKxor, sizeof(XorExp), e1, e2)
8128 Expression *XorExp::semantic(Scope *sc)
8129 { Expression *e;
8131 if (!type)
8132 { BinExp::semanticp(sc);
8133 e = op_overload(sc);
8134 if (e)
8135 return e;
8136 if (e1->type->toBasetype()->ty == Tbool &&
8137 e2->type->toBasetype()->ty == Tbool)
8139 type = e1->type;
8140 e = this;
8142 else
8144 typeCombine(sc);
8145 e1->checkIntegral();
8146 e2->checkIntegral();
8149 return this;
8153 /************************************************************/
8155 OrOrExp::OrOrExp(Loc loc, Expression *e1, Expression *e2)
8156 : BinExp(loc, TOKoror, sizeof(OrOrExp), e1, e2)
8160 Expression *OrOrExp::semantic(Scope *sc)
8162 unsigned cs1;
8164 // same as for AndAnd
8165 e1 = e1->semantic(sc);
8166 e1 = resolveProperties(sc, e1);
8167 e1 = e1->checkToPointer();
8168 e1 = e1->checkToBoolean();
8169 cs1 = sc->callSuper;
8171 if (sc->flags & SCOPEstaticif)
8173 /* If in static if, don't evaluate e2 if we don't have to.
8175 e1 = e1->optimize(WANTflags);
8176 if (e1->isBool(TRUE))
8178 return new IntegerExp(loc, 1, Type::tboolean);
8182 e2 = e2->semantic(sc);
8183 sc->mergeCallSuper(loc, cs1);
8184 e2 = resolveProperties(sc, e2);
8185 e2 = e2->checkToPointer();
8187 type = Type::tboolean;
8188 if (e1->type->ty == Tvoid)
8189 type = Type::tvoid;
8190 if (e2->op == TOKtype || e2->op == TOKimport)
8191 error("%s is not an expression", e2->toChars());
8192 return this;
8195 Expression *OrOrExp::checkToBoolean()
8197 e2 = e2->checkToBoolean();
8198 return this;
8201 int OrOrExp::isBit()
8203 return TRUE;
8206 int OrOrExp::checkSideEffect(int flag)
8208 if (flag == 2)
8210 return e1->checkSideEffect(2) || e2->checkSideEffect(2);
8212 else
8213 { e1->checkSideEffect(1);
8214 return e2->checkSideEffect(flag);
8218 /************************************************************/
8220 AndAndExp::AndAndExp(Loc loc, Expression *e1, Expression *e2)
8221 : BinExp(loc, TOKandand, sizeof(AndAndExp), e1, e2)
8225 Expression *AndAndExp::semantic(Scope *sc)
8227 unsigned cs1;
8229 // same as for OrOr
8230 e1 = e1->semantic(sc);
8231 e1 = resolveProperties(sc, e1);
8232 e1 = e1->checkToPointer();
8233 e1 = e1->checkToBoolean();
8234 cs1 = sc->callSuper;
8236 if (sc->flags & SCOPEstaticif)
8238 /* If in static if, don't evaluate e2 if we don't have to.
8240 e1 = e1->optimize(WANTflags);
8241 if (e1->isBool(FALSE))
8243 return new IntegerExp(loc, 0, Type::tboolean);
8247 e2 = e2->semantic(sc);
8248 sc->mergeCallSuper(loc, cs1);
8249 e2 = resolveProperties(sc, e2);
8250 e2 = e2->checkToPointer();
8252 type = Type::tboolean;
8253 if (e1->type->ty == Tvoid)
8254 type = Type::tvoid;
8255 if (e2->op == TOKtype || e2->op == TOKimport)
8256 error("%s is not an expression", e2->toChars());
8257 return this;
8260 Expression *AndAndExp::checkToBoolean()
8262 e2 = e2->checkToBoolean();
8263 return this;
8266 int AndAndExp::isBit()
8268 return TRUE;
8271 int AndAndExp::checkSideEffect(int flag)
8273 if (flag == 2)
8275 return e1->checkSideEffect(2) || e2->checkSideEffect(2);
8277 else
8279 e1->checkSideEffect(1);
8280 return e2->checkSideEffect(flag);
8284 /************************************************************/
8286 InExp::InExp(Loc loc, Expression *e1, Expression *e2)
8287 : BinExp(loc, TOKin, sizeof(InExp), e1, e2)
8291 Expression *InExp::semantic(Scope *sc)
8292 { Expression *e;
8294 if (type)
8295 return this;
8297 BinExp::semanticp(sc);
8298 e = op_overload(sc);
8299 if (e)
8300 return e;
8302 //type = Type::tboolean;
8303 Type *t2b = e2->type->toBasetype();
8304 if (t2b->ty != Taarray)
8306 error("rvalue of in expression must be an associative array, not %s", e2->type->toChars());
8307 type = Type::terror;
8309 else
8311 TypeAArray *ta = (TypeAArray *)t2b;
8313 // Convert key to type of key
8314 e1 = e1->implicitCastTo(sc, ta->index);
8316 // Return type is pointer to value
8317 type = ta->next->pointerTo();
8319 return this;
8322 int InExp::isBit()
8324 return FALSE;
8328 /************************************************************/
8330 /* This deletes the key e1 from the associative array e2
8333 RemoveExp::RemoveExp(Loc loc, Expression *e1, Expression *e2)
8334 : BinExp(loc, TOKremove, sizeof(RemoveExp), e1, e2)
8336 type = Type::tvoid;
8339 /************************************************************/
8341 CmpExp::CmpExp(enum TOK op, Loc loc, Expression *e1, Expression *e2)
8342 : BinExp(loc, op, sizeof(CmpExp), e1, e2)
8346 Expression *CmpExp::semantic(Scope *sc)
8347 { Expression *e;
8348 Type *t1;
8349 Type *t2;
8351 #if LOGSEMANTIC
8352 printf("CmpExp::semantic('%s')\n", toChars());
8353 #endif
8354 if (type)
8355 return this;
8357 BinExp::semanticp(sc);
8359 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull ||
8360 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull)
8362 error("do not use null when comparing class types");
8365 e = op_overload(sc);
8366 if (e)
8368 e = new CmpExp(op, loc, e, new IntegerExp(loc, 0, Type::tint32));
8369 e = e->semantic(sc);
8370 return e;
8373 typeCombine(sc);
8374 type = Type::tboolean;
8376 // Special handling for array comparisons
8377 t1 = e1->type->toBasetype();
8378 t2 = e2->type->toBasetype();
8379 if ((t1->ty == Tarray || t1->ty == Tsarray) &&
8380 (t2->ty == Tarray || t2->ty == Tsarray))
8382 if (!t1->next->equals(t2->next))
8383 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars());
8384 e = this;
8386 else if (t1->ty == Tstruct || t2->ty == Tstruct ||
8387 (t1->ty == Tclass && t2->ty == Tclass))
8389 if (t2->ty == Tstruct)
8390 error("need member function opCmp() for %s %s to compare", t2->toDsymbol(sc)->kind(), t2->toChars());
8391 else
8392 error("need member function opCmp() for %s %s to compare", t1->toDsymbol(sc)->kind(), t1->toChars());
8393 e = this;
8395 #if 1
8396 else if (t1->iscomplex() || t2->iscomplex())
8398 error("compare not defined for complex operands");
8399 e = new IntegerExp(0);
8401 #endif
8402 else
8403 e = this;
8404 return e;
8407 int CmpExp::isBit()
8409 return TRUE;
8413 /************************************************************/
8415 EqualExp::EqualExp(enum TOK op, Loc loc, Expression *e1, Expression *e2)
8416 : BinExp(loc, op, sizeof(EqualExp), e1, e2)
8418 assert(op == TOKequal || op == TOKnotequal);
8421 Expression *EqualExp::semantic(Scope *sc)
8422 { Expression *e;
8423 Type *t1;
8424 Type *t2;
8426 //printf("EqualExp::semantic('%s')\n", toChars());
8427 if (type)
8428 return this;
8430 BinExp::semanticp(sc);
8432 /* Before checking for operator overloading, check to see if we're
8433 * comparing the addresses of two statics. If so, we can just see
8434 * if they are the same symbol.
8436 if (e1->op == TOKaddress && e2->op == TOKaddress)
8437 { AddrExp *ae1 = (AddrExp *)e1;
8438 AddrExp *ae2 = (AddrExp *)e2;
8440 if (ae1->e1->op == TOKvar && ae2->e1->op == TOKvar)
8441 { VarExp *ve1 = (VarExp *)ae1->e1;
8442 VarExp *ve2 = (VarExp *)ae2->e1;
8444 if (ve1->var == ve2->var /*|| ve1->var->toSymbol() == ve2->var->toSymbol()*/)
8446 // They are the same, result is 'true' for ==, 'false' for !=
8447 e = new IntegerExp(loc, (op == TOKequal), Type::tboolean);
8448 return e;
8453 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull ||
8454 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull)
8456 error("use '%s' instead of '%s' when comparing with null",
8457 Token::toChars(op == TOKequal ? TOKidentity : TOKnotidentity),
8458 Token::toChars(op));
8461 //if (e2->op != TOKnull)
8463 e = op_overload(sc);
8464 if (e)
8466 if (op == TOKnotequal)
8468 e = new NotExp(e->loc, e);
8469 e = e->semantic(sc);
8471 return e;
8475 e = typeCombine(sc);
8476 type = Type::tboolean;
8478 // Special handling for array comparisons
8479 t1 = e1->type->toBasetype();
8480 t2 = e2->type->toBasetype();
8481 if ((t1->ty == Tarray || t1->ty == Tsarray) &&
8482 (t2->ty == Tarray || t2->ty == Tsarray))
8484 if (!t1->next->equals(t2->next))
8485 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars());
8487 else
8489 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating())
8491 // Cast both to complex
8492 e1 = e1->castTo(sc, Type::tcomplex80);
8493 e2 = e2->castTo(sc, Type::tcomplex80);
8496 return e;
8499 int EqualExp::isBit()
8501 return TRUE;
8506 /************************************************************/
8508 IdentityExp::IdentityExp(enum TOK op, Loc loc, Expression *e1, Expression *e2)
8509 : BinExp(loc, op, sizeof(IdentityExp), e1, e2)
8513 Expression *IdentityExp::semantic(Scope *sc)
8515 if (type)
8516 return this;
8518 BinExp::semanticp(sc);
8519 type = Type::tboolean;
8520 typeCombine(sc);
8521 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating())
8523 // Cast both to complex
8524 e1 = e1->castTo(sc, Type::tcomplex80);
8525 e2 = e2->castTo(sc, Type::tcomplex80);
8527 return this;
8530 int IdentityExp::isBit()
8532 return TRUE;
8536 /****************************************************************/
8538 CondExp::CondExp(Loc loc, Expression *econd, Expression *e1, Expression *e2)
8539 : BinExp(loc, TOKquestion, sizeof(CondExp), e1, e2)
8541 this->econd = econd;
8544 Expression *CondExp::syntaxCopy()
8546 return new CondExp(loc, econd->syntaxCopy(), e1->syntaxCopy(), e2->syntaxCopy());
8550 Expression *CondExp::semantic(Scope *sc)
8551 { Type *t1;
8552 Type *t2;
8553 unsigned cs0;
8554 unsigned cs1;
8556 #if LOGSEMANTIC
8557 printf("CondExp::semantic('%s')\n", toChars());
8558 #endif
8559 if (type)
8560 return this;
8562 econd = econd->semantic(sc);
8563 econd = resolveProperties(sc, econd);
8564 econd = econd->checkToPointer();
8565 econd = econd->checkToBoolean();
8567 #if 0 /* this cannot work right because the types of e1 and e2
8568 * both contribute to the type of the result.
8570 if (sc->flags & SCOPEstaticif)
8572 /* If in static if, don't evaluate what we don't have to.
8574 econd = econd->optimize(WANTflags);
8575 if (econd->isBool(TRUE))
8577 e1 = e1->semantic(sc);
8578 e1 = resolveProperties(sc, e1);
8579 return e1;
8581 else if (econd->isBool(FALSE))
8583 e2 = e2->semantic(sc);
8584 e2 = resolveProperties(sc, e2);
8585 return e2;
8588 #endif
8591 cs0 = sc->callSuper;
8592 e1 = e1->semantic(sc);
8593 e1 = resolveProperties(sc, e1);
8594 cs1 = sc->callSuper;
8595 sc->callSuper = cs0;
8596 e2 = e2->semantic(sc);
8597 e2 = resolveProperties(sc, e2);
8598 sc->mergeCallSuper(loc, cs1);
8601 // If either operand is void, the result is void
8602 t1 = e1->type;
8603 t2 = e2->type;
8604 if (t1->ty == Tvoid || t2->ty == Tvoid)
8605 type = Type::tvoid;
8606 else if (t1 == t2)
8607 type = t1;
8608 else
8610 typeCombine(sc);
8611 switch (e1->type->toBasetype()->ty)
8613 case Tcomplex32:
8614 case Tcomplex64:
8615 case Tcomplex80:
8616 e2 = e2->castTo(sc, e1->type);
8617 break;
8619 switch (e2->type->toBasetype()->ty)
8621 case Tcomplex32:
8622 case Tcomplex64:
8623 case Tcomplex80:
8624 e1 = e1->castTo(sc, e2->type);
8625 break;
8628 return this;
8631 Expression *CondExp::toLvalue(Scope *sc, Expression *ex)
8633 PtrExp *e;
8635 // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2)
8636 e = new PtrExp(loc, this, type);
8638 e1 = e1->addressOf(sc);
8639 //e1 = e1->toLvalue(sc, NULL);
8641 e2 = e2->addressOf(sc);
8642 //e2 = e2->toLvalue(sc, NULL);
8644 typeCombine(sc);
8646 type = e2->type;
8647 return e;
8650 Expression *CondExp::modifiableLvalue(Scope *sc, Expression *e)
8652 error("conditional expression %s is not a modifiable lvalue", toChars());
8653 return this;
8656 void CondExp::checkEscape()
8658 e1->checkEscape();
8659 e2->checkEscape();
8663 Expression *CondExp::checkToBoolean()
8665 e1 = e1->checkToBoolean();
8666 e2 = e2->checkToBoolean();
8667 return this;
8670 int CondExp::checkSideEffect(int flag)
8672 if (flag == 2)
8674 return econd->checkSideEffect(2) ||
8675 e1->checkSideEffect(2) ||
8676 e2->checkSideEffect(2);
8678 else
8680 econd->checkSideEffect(1);
8681 e1->checkSideEffect(flag);
8682 return e2->checkSideEffect(flag);
8686 void CondExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
8688 expToCBuffer(buf, hgs, econd, PREC_oror);
8689 buf->writestring(" ? ");
8690 expToCBuffer(buf, hgs, e1, PREC_expr);
8691 buf->writestring(" : ");
8692 expToCBuffer(buf, hgs, e2, PREC_cond);