2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2008 by Digital Mars
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...
28 #define integer_t dmd_integer_t
32 extern "C" char * __cdecl __locale_decpoint
;
38 #include "..\root\mem.h"
40 #include "../root/mem.h"
45 #include "expression.h"
50 #include "statement.h"
51 #include "declaration.h"
52 #include "aggregate.h"
61 Expression
*createTypeInfoArray(Scope
*sc
, Expression
*args
[], int dim
);
65 /**********************************
66 * Set operator precedence for each operator.
69 // Operator precedence - greater values are higher precedence
91 enum PREC precedence
[TOKMAX
];
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
;
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();
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();
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
231 Dsymbol
*s
= tcd
->toParent();
232 while (s
&& s
->isFuncDeclaration())
233 { FuncDeclaration
*f
= s
->isFuncDeclaration();
236 e1
= new VarExp(loc
, f
->vthis
);
240 if (s
&& s
->isClassDeclaration())
241 e1
->type
= s
->isClassDeclaration()->type
;
242 e1
= e1
->semantic(sc
);
248 error("this for %s needs to be type %s not type %s",
249 var
->toChars(), ad
->toChars(), t
->toChars());
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
279 Dsymbol
*parent
= fd
->parent
;
282 TemplateInstance
*ti
= parent
->isTemplateInstance();
289 fd
= fd
->parent
->isFuncDeclaration();
293 { //printf("test '%s'\n", fd->toChars());
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());
314 Type
*t
= e
->type
->toBasetype();
316 if (t
->ty
== Tfunction
)
318 e
= new CallExp(e
->loc
, e
);
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
);
334 else if (e
->op
== TOKdotexp
)
336 e
->error("expression has no value");
342 /******************************
343 * Perform semantic() on an array of Expressions.
346 void arrayExpressionSemantic(Expressions
*exps
, Scope
*sc
)
350 for (size_t i
= 0; i
< exps
->dim
; i
++)
351 { Expression
*e
= (Expression
*)exps
->data
[i
];
354 exps
->data
[i
] = (void *)e
;
359 /****************************************
363 void expandTuples(Expressions
*exps
)
365 //printf("expandTuples()\n");
368 for (size_t i
= 0; i
< exps
->dim
; i
++)
369 { Expression
*arg
= (Expression
*)exps
->data
[i
];
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)
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
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
)
414 for (size_t i
= 0; i
< exps
->dim
; i
++)
415 { Expression
*arg
= (Expression
*)exps
->data
[i
];
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
;
432 if (arg
->type
->ty
== Tfunction
)
434 arg
= new AddrExp(arg
->loc
, arg
);
435 arg
= arg
->semantic(sc
);
436 exps
->data
[i
] = (void *) arg
;
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
)
459 //printf("functionArguments()\n");
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)
470 for (size_t i
= 0; i
< n
; i
++)
475 arg
= (Expression
*)arguments
->data
[i
];
481 Argument
*p
= Argument::getNth(tf
->parameters
, i
);
487 if (tf
->varargs
== 2 && i
+ 1 == nparams
)
489 error(loc
, "expected %"PRIuSIZE
" arguments, not %"PRIuSIZE
, nparams
, nargs
);
492 arg
= p
->defaultArg
->copy();
493 arguments
->push(arg
);
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
);
507 Type
*tb
= p
->type
->toBasetype();
508 Type
*tret
= p
->isLazyArray();
513 { // Create a static array variable v of type arg->type
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. */
520 arg
= new NullExp(loc
);
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
));
532 v
->parent
= sc
->parent
;
535 Expression
*c
= new DeclarationExp(0, v
);
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
);
547 c
= new CommaExp(loc
, c
, e
);
551 arg
= new VarExp(loc
, v
);
553 arg
= new CommaExp(loc
, c
, arg
);
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
);
569 { error(loc
, "not enough arguments");
574 arg
= arg
->semantic(sc
);
575 //printf("\targ = '%s'\n", arg->toChars());
576 arguments
->setDim(i
+ 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
);
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
)
619 arg
= arg
->castTo(sc
, Type::tfloat64
);
623 arg
= arg
->castTo(sc
, Type::timaginary64
);
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
);
638 arg
= arg
->castTo(sc
, ta
);
643 arg
= arg
->optimize(WANTvalue
);
644 arguments
->data
[i
] = (void *) arg
;
649 // If D linkage and variadic, add _arguments[] as first argument
650 if (tf
->linkage
== LINKd
&& tf
->varargs
== 1)
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
)
670 e
->toCBuffer(buf
, hgs
);
674 e
->toCBuffer(buf
, hgs
);
677 /**************************************************
678 * Write out argument list to buf.
681 void argsToCBuffer(OutBuffer
*buf
, Expressions
*arguments
, HdrGenState
*hgs
)
685 for (size_t i
= 0; i
< arguments
->dim
; i
++)
686 { Expression
*arg
= (Expression
*)arguments
->data
[i
];
691 expToCBuffer(buf
, hgs
, arg
, PREC_assign
);
697 /**************************************************
698 * Write out argument types to buf.
701 void argExpTypesToCBuffer(OutBuffer
*buf
, Expressions
*arguments
, HdrGenState
*hgs
)
706 for (size_t i
= 0; i
< arguments
->dim
; i
++)
707 { Expression
*arg
= (Expression
*)arguments
->data
[i
];
712 arg
->type
->toCBuffer2(&argbuf
, hgs
, 0);
718 /******************************** Expression **************************/
720 Expression::Expression(Loc loc
, enum TOK op
, int size
)
729 Expression
*Expression::syntaxCopy()
731 //printf("Expression::syntaxCopy()\n");
736 /*********************************
737 * Does *not* do a deep copy.
740 Expression
*Expression::copy()
746 fprintf(stdmsg
, "No expression copy for: %s\n", toChars());
747 printf("op = %d\n", op
);
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
)
764 printf("Expression::semantic()\n");
767 type
= type
->semantic(loc
, sc
);
773 void Expression::print()
775 fprintf(stdmsg
, "%s\n", toChars());
779 char *Expression::toChars()
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
, ...)
792 va_start(ap
, format
);
793 ::verror(loc
, format
, ap
);
797 void Expression::rvalue()
799 if (type
&& type
->toBasetype()->ty
== Tvoid
)
800 { error("expression %s is void and has no value", toChars());
808 Expression
*Expression::combine(Expression
*e1
, Expression
*e2
)
814 e1
= new CommaExp(e1
->loc
, e1
, e2
);
823 integer_t
Expression::toInteger()
825 //printf("Expression %s\n", Token::toChars(op));
826 error("Integer constant expression expected instead of %s", toChars());
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());
842 real_t
Expression::toImaginary()
844 error("Floating point constant expression expected instead of %s", toChars());
848 complex_t
Expression::toComplex()
850 error("Floating point constant expression expected instead of %s", toChars());
852 return complex_t(real_t(0)); // %% nicer
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
)
877 else if (!loc
.filename
)
879 error("%s is not an lvalue", e
->toChars());
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);
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);
928 void Expression::checkDeprecated(Scope
*sc
, Dsymbol
*s
)
930 s
->checkDeprecated(loc
, sc
);
933 /********************************
934 * Check for expressions that have no use.
936 * flag 0 not going to use the result, so issue error message if no
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
944 int Expression::checkSideEffect(int flag
)
947 { if (op
== TOKimport
)
949 error("%s has no effect", toChars());
952 error("%s has no effect in expression (%s)",
953 Token::toChars(op
), toChars());
958 /*****************************
959 * Check that expression can be tested for true or false.
962 Expression
*Expression::checkToBoolean()
964 // Default is 'yes' - do nothing
971 if (!type
->checkBoolean())
973 error("expression %s of type %s does not have a boolean value", toChars(), type
->toChars());
978 /****************************
981 Expression
*Expression::checkToPointer()
986 //printf("Expression::checkToPointer()\n");
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
);
996 e
= new AddrExp(loc
, this);
997 e
->type
= tb
->next
->pointerTo();
1002 /******************************
1003 * Take address of expression.
1006 Expression
*Expression::addressOf(Scope
*sc
)
1010 //printf("Expression::addressOf()\n");
1011 e
= toLvalue(sc
, NULL
);
1012 e
= new AddrExp(loc
, e
);
1013 e
->type
= type
->pointerTo();
1017 /******************************
1018 * If this is a reference, dereference it.
1021 Expression
*Expression::deref()
1023 //printf("Expression::deref()\n");
1024 if (type
->ty
== Treference
)
1027 e
= new PtrExp(loc
, this);
1028 e
->type
= type
->next
;
1034 /********************************
1035 * Does this expression statically evaluate to a boolean TRUE or FALSE?
1038 int Expression::isBool(int result
)
1043 /********************************
1044 * Does this expression result in either a 1 or a 0?
1047 int Expression::isBit()
1052 Expressions
*Expression::arraySyntaxCopy(Expressions
*exps
)
1053 { Expressions
*a
= NULL
;
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();
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
;
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
)
1095 (((Expression
*)o
)->op
== TOKint64
&&
1096 ((ne
= (IntegerExp
*)o
), type
->equals(ne
->type
)) &&
1097 value
== ne
->value
))
1102 char *IntegerExp::toChars()
1105 return Expression::toChars();
1107 static char buffer
[sizeof(value
) * 3 + 1];
1109 sprintf(buffer
, "%"PRIdMAX
, value
);
1114 integer_t
IntegerExp::toInteger()
1123 case Tbool
: value
= (value
!= 0); break;
1124 case Tint8
: value
= (d_int8
) value
; break;
1126 case Tuns8
: value
= (d_uns8
) value
; break;
1127 case Tint16
: value
= (d_int16
) value
; break;
1129 case Tuns16
: value
= (d_uns16
) value
; break;
1130 case Tint32
: value
= (d_int32
) value
; break;
1132 case Tuns32
: value
= (d_uns32
) value
; break;
1133 case Tint64
: value
= (d_int64
) value
; break;
1134 case Tuns64
: value
= (d_uns64
) value
; break;
1139 case 4: value
= (d_uns32
) value
; break;
1140 case 8: value
= (d_uns64
) value
; break;
1142 assert(PTRSIZE
== 4 || PTRSIZE
== 8);
1148 TypeEnum
*te
= (TypeEnum
*)t
;
1149 t
= te
->sym
->memtype
;
1155 TypeTypedef
*tt
= (TypeTypedef
*)t
;
1156 t
= tt
->sym
->basetype
;
1171 real_t
IntegerExp::toReal()
1176 t
= type
->toBasetype();
1177 if (t
->ty
== Tuns64
)
1178 return (real_t
)(d_uns64
)value
;
1180 return (real_t
)(d_int64
)value
;
1183 real_t
IntegerExp::toImaginary()
1188 complex_t
IntegerExp::toComplex()
1193 int IntegerExp::isBool(int result
)
1195 return result
? value
!= 0 : value
== 0;
1198 Expression
*IntegerExp::semantic(Scope
*sc
)
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
;
1210 type
= Type::tint32
;
1213 { type
= type
->semantic(loc
, sc
);
1218 Expression
*IntegerExp::toLvalue(Scope
*sc
, Expression
*e
)
1222 else if (!loc
.filename
)
1224 e
->error("constant %s is not an lvalue", e
->toChars());
1228 void IntegerExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
1230 integer_t v
= toInteger();
1239 { TypeEnum
*te
= (TypeEnum
*)t
;
1240 buf
->printf("cast(%s)", te
->sym
->toChars());
1241 t
= te
->sym
->memtype
;
1246 { TypeTypedef
*tt
= (TypeTypedef
*)t
;
1247 buf
->printf("cast(%s)", tt
->sym
->toChars());
1248 t
= tt
->sym
->basetype
;
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
);
1261 buf
->writestring("'\\''");
1262 else if (isprint(v
) && v
!= '\\')
1263 buf
->printf("'%c'", (int)v
);
1265 buf
->printf("'\\x%02x'", (int)v
);
1269 buf
->writestring("cast(byte)");
1273 buf
->writestring("cast(short)");
1278 buf
->printf("%d", (int)v
);
1282 buf
->writestring("cast(ubyte)");
1286 buf
->writestring("cast(ushort)");
1291 buf
->printf("%du", (unsigned)v
);
1295 buf
->printf("%"PRIdMAX
"L", v
);
1299 buf
->printf("%"PRIuMAX
"LU", v
);
1304 buf
->writestring((char *)(v
? "true" : "false"));
1308 buf
->writestring("cast(");
1309 buf
->writestring(t
->toChars());
1310 buf
->writeByte(')');
1320 else if (v
& 0x8000000000000000LL
)
1321 buf
->printf("0x%"PRIxMAX
, v
);
1323 buf
->printf("%"PRIdMAX
, v
);
1326 void IntegerExp::toMangleBuffer(OutBuffer
*buf
)
1328 if ((sinteger_t
)value
< 0)
1329 buf
->printf("N%"PRIdMAX
, -value
);
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
;
1344 char *RealExp::toChars()
1346 char buffer
[sizeof(value
) * 3 + 8 + 1 + 1];
1349 value
.format(buffer
, sizeof(buffer
));
1350 if (type
->isimaginary())
1351 strcat(buffer
, "i");
1353 sprintf(buffer
, type
->isimaginary() ? "%Lgi" : "%Lg", value
);
1355 assert(strlen(buffer
) < sizeof(buffer
));
1356 return mem
.strdup(buffer
);
1359 integer_t
RealExp::toInteger()
1362 return (d_int64
) toReal().toInt();
1364 return (sinteger_t
) toReal();
1368 uinteger_t
RealExp::toUInteger()
1371 return (uinteger_t
) toReal().toInt();
1373 return (uinteger_t
) toReal();
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()
1390 return toReal() + toImaginary() * I
;
1392 return complex_t(toReal(), toImaginary());
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
)
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;
1411 return (x1
.isNan() && x2
.isNan()) || x1
.isIdenticalTo(x2
);
1415 int RealExp::equals(Object
*o
)
1419 (((Expression
*)o
)->op
== TOKfloat64
&&
1420 ((ne
= (RealExp
*)o
), type
->equals(ne
->type
)) &&
1421 RealEquals(value
, ne
->value
)
1428 Expression
*RealExp::semantic(Scope
*sc
)
1431 type
= Type::tfloat64
;
1433 type
= type
->semantic(loc
, sc
);
1437 int RealExp::isBool(int result
)
1440 return result
? (! value
.isZero()) : (value
.isZero());
1442 return result
? (value
!= 0)
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
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
);
1464 value
.formatHex(buffer
, sizeof(buffer
));
1465 buf
->writestring(buffer
);
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
;
1476 real_t r
= strtold(buffer
, NULL
);
1478 if (r
== value
) // if exact duplication
1479 buf
->writestring(buffer
);
1481 buf
->printf("%La", value
); // ensure exact duplication
1486 Type
*t
= type
->toBasetype();
1492 buf
->writeByte('F');
1498 buf
->writeByte('L');
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
1523 * -0X1.1BC18BA997B95P+79 => N11BC18BA997B95P79
1529 buf
->writestring("NAN"); // no -NAN bugs
1530 else if (value
.isInf())
1531 buf
->writestring(value
.isNegative()?"NINF":"INF");
1534 buf
->writestring("NAN"); // no -NAN bugs
1540 value
.formatHex(buffer
, sizeof(buffer
));
1541 int n
= strlen(buffer
);
1543 int n
= sprintf(buffer
, "%LA", value
);
1544 assert(n
> 0 && n
< sizeof(buffer
));
1546 for (int i
= 0; i
< n
; i
++)
1547 { char c
= buffer
[i
];
1552 buf
->writeByte('N');
1563 break; // skip leading 0X
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
;
1586 //printf("ComplexExp::ComplexExp(%s)\n", toChars());
1589 char *ComplexExp::toChars()
1591 char buffer
[sizeof(value
) * 3 + 8 + 1];
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
);
1600 sprintf(buffer
, "(%Lg+%Lgi)", creall(value
), cimagl(value
));
1601 assert(strlen(buffer
) < sizeof(buffer
));
1603 return mem
.strdup(buffer
);
1606 integer_t
ComplexExp::toInteger()
1609 return (d_int64
) toReal().toInt();
1611 return (sinteger_t
) toReal();
1615 uinteger_t
ComplexExp::toUInteger()
1618 return (uinteger_t
) toReal().toInt();
1620 return (uinteger_t
) toReal();
1624 real_t
ComplexExp::toReal()
1626 return creall(value
);
1629 real_t
ComplexExp::toImaginary()
1631 return cimagl(value
);
1634 complex_t
ComplexExp::toComplex()
1639 int ComplexExp::equals(Object
*o
)
1643 (((Expression
*)o
)->op
== TOKcomplex80
&&
1644 ((ne
= (ComplexExp
*)o
), type
->equals(ne
->type
)) &&
1646 RealEquals(creall(value
), creall(ne
->value
)) &&
1647 RealEquals(cimagl(value
), cimagl(ne
->value
))
1649 RealEquals(value
.re
, ne
->value
.re
) &&
1650 RealEquals(value
.im
, ne
->value
.im
)
1658 Expression
*ComplexExp::semantic(Scope
*sc
)
1661 type
= Type::tcomplex80
;
1663 type
= type
->semantic(loc
, sc
);
1667 int ComplexExp::isBool(int result
)
1670 return (bool)(value
);
1675 void ComplexExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
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
);
1687 buf
->writeByte('(');
1688 floatToBuffer(buf
, type
, creall(value
));
1689 buf
->writeByte('+');
1690 floatToBuffer(buf
, type
, cimagl(value
));
1691 buf
->writestring("i)");
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
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
)
1719 printf("IdentifierExp::semantic('%s')\n", ident
->toChars());
1721 s
= sc
->search(loc
, ident
, &scopesym
);
1724 WithScopeSymbol
*withsym
;
1726 // See if it was a with class
1727 withsym
= scopesym
->isWithScopeSymbol();
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
);
1739 { Type
*t
= withsym
->withstate
->wthis
->type
;
1740 if (t
->ty
== Tpointer
)
1742 e
= new TypeDotIdExp(loc
, t
, ident
);
1747 if (!s
->parent
&& scopesym
->isArrayScopeSymbol())
1748 { // Kludge to run semantic() here because
1749 // ArrayScopeSymbol::search() doesn't have access to sc.
1752 // Look to see if f is really a function template
1753 FuncDeclaration
*f
= s
->isFuncDeclaration();
1755 { TemplateInstance
*ti
= f
->parent
->isTemplateInstance();
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
);
1772 e
= new DsymbolExp(loc
, s
);
1774 return e
->semantic(sc
);
1776 error("undefined identifier %s", ident
->toChars());
1777 type
= Type::terror
;
1781 char *IdentifierExp::toChars()
1783 return ident
->toChars();
1786 void IdentifierExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
1789 buf
->writestring(ident
->toHChars2());
1791 buf
->writestring(ident
->toChars());
1794 Expression
*IdentifierExp::toLvalue(Scope
*sc
, Expression
*e
)
1797 tym
= tybasic(e1
->ET
->Tty
);
1798 if (!(tyscalar(tym
) ||
1800 tym
== TYarray
&& e
->Eoper
== TOKaddr
))
1801 synerr(EM_lvalue
); // lvalue expected
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
))
1821 Expression
*DsymbolExp::semantic(Scope
*sc
)
1824 printf("DsymbolExp::semantic('%s')\n", s
->toChars());
1832 FuncLiteralDeclaration
*fld
;
1834 ClassDeclaration
*cd
;
1835 ClassDeclaration
*thiscd
= NULL
;
1840 //printf("DsymbolExp:: %p '%s' is a symbol\n", this, toChars());
1841 //printf("s = '%s', s->kind = '%s'\n", s->toChars(), s->kind());
1844 if (!s
->isFuncDeclaration()) // functions are checked after overloading
1845 checkDeprecated(sc
, s
);
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
);
1852 thiscd
= sc
->func
->parent
->isClassDeclaration();
1854 // BUG: This should happen after overload resolution for functions, not before
1857 if (hasThis(sc
) /*&& !s->isFuncDeclaration()*/)
1859 // Supply an implicit 'this', as in
1864 de
= new DotVarExp(loc
, new ThisExp(loc
), s
->isDeclaration());
1865 return de
->semantic(sc
);
1869 em
= s
->isEnumMember();
1872 e
= em
->value
->copy();
1873 e
= e
->semantic(sc
);
1876 v
= s
->isVarDeclaration();
1879 //printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars());
1883 { error("forward reference of %s", v
->toChars());
1884 type
= Type::terror
;
1887 if (v
->isConst() && type
->toBasetype()->ty
!= Tsarray
)
1893 error("circular reference to '%s'", v
->toChars());
1894 type
= Type::tint32
;
1897 ExpInitializer
*ei
= v
->init
->isExpInitializer();
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
);
1910 e
= type
->defaultInit();
1915 e
= new VarExp(loc
, v
);
1917 e
= e
->semantic(sc
);
1920 fld
= s
->isFuncLiteralDeclaration();
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();
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.
1937 dte
= new DotTypeExp(loc
, new ThisExp(loc
), s
);
1938 return dte
->semantic(sc
);
1940 imp
= s
->isImport();
1945 ie
= new ScopeExp(loc
, imp
->pkg
);
1946 return ie
->semantic(sc
);
1948 pkg
= s
->isPackage();
1953 ie
= new ScopeExp(loc
, pkg
);
1954 return ie
->semantic(sc
);
1956 Module
*mod
= s
->isModule();
1961 ie
= new ScopeExp(loc
, mod
);
1962 return ie
->semantic(sc
);
1968 return new TypeExp(loc
, t
);
1971 TupleDeclaration
*tup
= s
->isTupleDeclaration();
1974 e
= new TupleExp(loc
, tup
);
1975 e
= e
->semantic(sc
);
1979 TemplateInstance
*ti
= s
->isTemplateInstance();
1980 if (ti
&& !global
.errors
)
1981 { if (!ti
->semanticdone
)
1983 s
= ti
->inst
->toAlias();
1984 if (!s
->isTemplateInstance())
1986 e
= new ScopeExp(loc
, ti
);
1987 e
= e
->semantic(sc
);
1991 TemplateDeclaration
*td
= s
->isTemplateDeclaration();
1994 e
= new TemplateExp(loc
, td
);
1995 e
= e
->semantic(sc
);
2000 error("%s '%s' is not a variable", s
->kind(), s
->toChars());
2001 type
= Type::terror
;
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
)
2018 tym
= tybasic(e1
->ET
->Tty
);
2019 if (!(tyscalar(tym
) ||
2021 tym
== TYarray
&& e
->Eoper
== TOKaddr
))
2022 synerr(EM_lvalue
); // lvalue expected
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
))
2053 Expression
*ThisExp::semantic(Scope
*sc
)
2054 { FuncDeclaration
*fd
;
2055 FuncDeclaration
*fdthis
;
2059 printf("ThisExp::semantic()\n");
2062 { //assert(global.errors || var);
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
2071 // Find enclosing struct or class
2072 for (Dsymbol
*s
= sc
->parent
; 1; s
= s
->parent
)
2074 ClassDeclaration
*cd
;
2075 StructDeclaration
*sd
;
2079 error("%s is not in a struct or class scope", toChars());
2082 cd
= s
->isClassDeclaration();
2088 sd
= s
->isStructDeclaration();
2091 type
= sd
->type
->pointerTo();
2097 fdthis
= sc
->parent
->isFuncDeclaration();
2098 fd
= hasThis(sc
); // fd is the uplevel function with the 'this' variable
2104 assert(var
->parent
);
2106 var
->isVarDeclaration()->checkNestedReference(sc
, loc
);
2108 if (fd
!= fdthis
) // if nested
2110 fdthis
->getLevel(loc
, fd
);
2111 fd
->vthis
->nestedref
= 1;
2112 fd
->nestedFrameRef
= 1;
2115 sc
->callSuper
|= CSXthis
;
2119 error("'this' is only allowed in non-static member functions, not %s", sc
->parent
->toChars());
2120 type
= Type::tint32
;
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
)
2139 /******************************** SuperExp **************************/
2141 SuperExp::SuperExp(Loc loc
)
2147 Expression
*SuperExp::semantic(Scope
*sc
)
2148 { FuncDeclaration
*fd
;
2149 FuncDeclaration
*fdthis
;
2150 ClassDeclaration
*cd
;
2154 printf("SuperExp::semantic('%s')\n", toChars());
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
2164 // Find enclosing class
2165 for (Dsymbol
*s
= sc
->parent
; 1; s
= s
->parent
)
2167 ClassDeclaration
*cd
;
2171 error("%s is not in a class scope", toChars());
2174 cd
= s
->isClassDeclaration();
2179 { error("class %s has no 'super'", s
->toChars());
2188 fdthis
= sc
->parent
->isFuncDeclaration();
2194 assert(var
->parent
);
2197 while (s
&& s
->isTemplateInstance())
2200 cd
= s
->isClassDeclaration();
2201 //printf("parent is %s %s\n", fd->toParent()->kind(), fd->toParent()->toChars());
2206 error("no base class for %s", cd
->toChars());
2207 type
= fd
->vthis
->type
;
2211 type
= cd
->baseClass
->type
;
2214 var
->isVarDeclaration()->checkNestedReference(sc
, loc
);
2218 fdthis
->getLevel(loc
, fd
);
2219 fd
->vthis
->nestedref
= 1;
2220 fd
->nestedFrameRef
= 1;
2224 sc
->callSuper
|= CSXsuper
;
2229 error("'super' is only allowed in non-static class member functions");
2230 type
= Type::tint32
;
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
))
2248 Expression
*NullExp::semantic(Scope
*sc
)
2251 printf("NullExp::semantic('%s')\n", toChars());
2253 // NULL is the same as (void *)0
2255 type
= Type::tvoid
->pointerTo();
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
);
2282 this->committed
= 0;
2286 StringExp::StringExp(Loc loc
, void *string
, size_t len
)
2287 : Expression(loc
, TOKstring
, sizeof(StringExp
))
2289 this->string
= string
;
2292 this->committed
= 0;
2296 StringExp::StringExp(Loc loc
, void *string
, size_t len
, unsigned char postfix
)
2297 : Expression(loc
, TOKstring
, sizeof(StringExp
))
2299 this->string
= string
;
2302 this->committed
= 0;
2303 this->postfix
= postfix
;
2307 Expression
*StringExp::syntaxCopy()
2309 printf("StringExp::syntaxCopy() %s\n", toChars());
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;
2328 char *StringExp::toChars()
2334 memset(&hgs
, 0, sizeof(hgs
));
2335 toCBuffer(&buf
, &hgs
);
2337 p
= (char *)buf
.data
;
2342 Expression
*StringExp::semantic(Scope
*sc
)
2345 printf("StringExp::semantic() %s\n", toChars());
2357 for (u
= 0; u
< len
;)
2359 p
= utf_decodeChar((unsigned char *)string
, len
, &u
, &c
);
2370 string
= buffer
.extractData();
2373 type
= new TypeSArray(Type::tdchar
, new IntegerExp(loc
, len
, Type::tindex
));
2378 for (u
= 0; u
< len
;)
2380 p
= utf_decodeChar((unsigned char *)string
, len
, &u
, &c
);
2386 { buffer
.writeUTF16(c
);
2392 buffer
.writeUTF16(0);
2393 string
= buffer
.extractData();
2396 type
= new TypeSArray(Type::twchar
, new IntegerExp(loc
, len
, Type::tindex
));
2403 type
= new TypeSArray(Type::tchar
, new IntegerExp(loc
, len
, Type::tindex
));
2406 type
= type
->semantic(loc
, sc
);
2411 /****************************************
2412 * Convert string to char[].
2415 StringExp
*StringExp::toUTF8(Scope
*sc
)
2418 { // Convert to UTF-8 string
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);
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
2440 assert(se2
->op
== TOKstring
);
2443 int len2
= se2
->len
;
2450 return strcmp((char *)string
, (char *)se2
->string
);
2454 d_wchar
*s1
= (d_wchar
*)string
;
2455 d_wchar
*s2
= (d_wchar
*)se2
->string
;
2457 for (u
= 0; u
< len
; u
++)
2460 return s1
[u
] - s2
[u
];
2466 d_dchar
*s1
= (d_dchar
*)string
;
2467 d_dchar
*s2
= (d_dchar
*)se2
->string
;
2469 for (u
= 0; u
< len
; u
++)
2472 return s1
[u
] - s2
[u
];
2484 int StringExp::isBool(int result
)
2486 return result
? TRUE
: FALSE
;
2489 unsigned StringExp::charAt(size_t i
)
2495 value
= ((unsigned char *)string
)[i
];
2499 value
= ((unsigned short *)string
)[i
];
2503 value
= ((unsigned int *)string
)[i
];
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
);
2524 buf
->writeByte('\\');
2527 { if (c
<= 0x7F && (isprint(c
) || hgs
->console
))
2530 buf
->printf("\\x%02x", c
);
2532 else if (c
<= 0xFFFF)
2533 buf
->printf("\\x%02x\\x%02x", c
& 0xFF, c
>> 8);
2535 buf
->printf("\\x%02x\\x%02x\\x%02x\\x%02x",
2536 c
& 0xFF, (c
>> 8) & 0xFF, (c
>> 16) & 0xFF, c
>> 24);
2540 buf
->writeByte('"');
2542 buf
->writeByte(postfix
);
2545 void StringExp::toMangleBuffer(OutBuffer
*buf
)
2554 /* Write string in UTF-8 format
2559 q
= (unsigned char *)string
;
2564 for (u
= 0; u
< len
; )
2566 p
= utf_decodeWchar((unsigned short *)string
, len
, &u
, &c
);
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
);
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
;
2614 Expression
*ArrayLiteralExp::syntaxCopy()
2616 return new ArrayLiteralExp(loc
, arraySyntaxCopy(elements
));
2619 Expression
*ArrayLiteralExp::semantic(Scope
*sc
)
2624 printf("ArrayLiteralExp::semantic('%s')\n", toChars());
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
];
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
;
2649 // Convert any static arrays to dynamic arrays
2650 if (t0
->ty
== Tsarray
)
2652 t0
= t0
->next
->arrayOf();
2653 e
= e
->implicitCastTo(sc
, t0
);
2657 e
= e
->implicitCastTo(sc
, t0
);
2658 if (!committed
&& e
->op
== TOKstring
)
2659 { StringExp
*se
= (StringExp
*)e
;
2662 elements
->data
[i
] = (void *)e
;
2667 type
= new TypeSArray(t0
, new IntegerExp(elements
->dim
));
2668 type
= type
->semantic(loc
, sc
);
2672 int ArrayLiteralExp::checkSideEffect(int flag
)
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);
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
);
2718 this->values
= values
;
2721 Expression
*AssocArrayLiteralExp::syntaxCopy()
2723 return new AssocArrayLiteralExp(loc
,
2724 arraySyntaxCopy(keys
), arraySyntaxCopy(values
));
2727 Expression
*AssocArrayLiteralExp::semantic(Scope
*sc
)
2730 Type
*tvalue
= NULL
;
2733 printf("AssocArrayLiteralExp::semantic('%s')\n", toChars());
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
;
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
);
2755 for (size_t i
= 0; i
< keys
->dim
; i
++)
2756 { Expression
*key
= (Expression
*)keys
->data
[i
];
2757 Expression
*value
= (Expression
*)values
->data
[i
];
2760 error("%s has no value", key
->toChars());
2762 error("%s has no value", value
->toChars());
2763 key
= resolveProperties(sc
, key
);
2764 value
= resolveProperties(sc
, value
);
2769 key
= key
->implicitCastTo(sc
, tkey
);
2770 keys
->data
[i
] = (void *)key
;
2773 tvalue
= value
->type
;
2775 value
= value
->implicitCastTo(sc
, tvalue
);
2776 values
->data
[i
] = (void *)value
;
2782 tvalue
= Type::tvoid
;
2783 type
= new TypeAArray(tvalue
, tkey
);
2784 type
= type
->semantic(loc
, sc
);
2788 int AssocArrayLiteralExp::checkSideEffect(int flag
)
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);
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
];
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
))
2846 this->elements
= elements
;
2849 this->fillHoles
= 1;
2852 Expression
*StructLiteralExp::syntaxCopy()
2854 return new StructLiteralExp(loc
, sd
, arraySyntaxCopy(elements
));
2857 Expression
*StructLiteralExp::semantic(Scope
*sc
)
2861 printf("StructLiteralExp::semantic('%s')\n", toChars());
2864 // Run semantic() on each element
2865 for (size_t i
= 0; i
< elements
->dim
; i
++)
2866 { e
= (Expression
*)elements
->data
[i
];
2869 e
= e
->semantic(sc
);
2870 elements
->data
[i
] = (void *)e
;
2872 expandTuples(elements
);
2874 for (size_t i
= 0; i
< elements
->dim
; i
++)
2875 { e
= (Expression
*)elements
->data
[i
];
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());
2886 Dsymbol
*s
= (Dsymbol
*)sd
->fields
.data
[i
];
2887 VarDeclaration
*v
= s
->isVarDeclaration();
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:
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();
2913 if (v
->offset
< offset
)
2920 { e
= v
->init
->toExpression();
2922 error("cannot make expression out of initializer for %s", v
->toChars());
2925 { e
= v
->type
->defaultInit();
2928 offset
= v
->offset
+ v
->type
->size();
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
);
2947 { e
= (Expression
*)elements
->data
[i
];
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();
2972 if (offset
== v
->offset
&&
2973 type
->size() == v
->type
->size())
2974 { Expression
*e
= (Expression
*)elements
->data
[i
];
2986 Expression
*StructLiteralExp::toLvalue(Scope
*sc
, Expression
*e
)
2992 int StructLiteralExp::checkSideEffect(int flag
)
2995 for (size_t i
= 0; i
< elements
->dim
; i
++)
2996 { Expression
*e
= (Expression
*)elements
->data
[i
];
3000 f
|= e
->checkSideEffect(2);
3002 if (flag
== 0 && f
== 0)
3003 Expression::checkSideEffect(0);
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
];
3022 e
->toMangleBuffer(buf
);
3024 buf
->writeByte('v'); // 'v' for void
3028 /************************ TypeDotIdExp ************************************/
3037 TypeDotIdExp::TypeDotIdExp(Loc loc
, Type
*type
, Identifier
*ident
)
3038 : Expression(loc
, TOKtypedot
, sizeof(TypeDotIdExp
))
3041 this->ident
= ident
;
3044 Expression
*TypeDotIdExp::syntaxCopy()
3046 TypeDotIdExp
*te
= new TypeDotIdExp(loc
, type
->syntaxCopy(), ident
);
3050 Expression
*TypeDotIdExp::semantic(Scope
*sc
)
3054 printf("TypeDotIdExp::semantic()\n");
3056 e
= new DotIdExp(loc
, new TypeExp(loc
, type
), ident
);
3057 e
= e
->semantic(sc
);
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());
3081 Expression
*TypeExp::semantic(Scope
*sc
)
3083 //printf("TypeExp::semantic(%s)\n", type->toChars());
3084 type
= type
->semantic(loc
, sc
);
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;
3105 Expression
*ScopeExp::syntaxCopy()
3107 ScopeExp
*se
= new ScopeExp(loc
, (ScopeDsymbol
*)sds
->syntaxCopy(NULL
));
3111 Expression
*ScopeExp::semantic(Scope
*sc
)
3113 TemplateInstance
*ti
;
3117 printf("+ScopeExp::semantic('%s')\n", toChars());
3120 ti
= sds
->isTemplateInstance();
3121 if (ti
&& !global
.errors
)
3123 if (!ti
->semanticdone
)
3125 s
= ti
->inst
->toAlias();
3126 sds2
= s
->isScopeDsymbol();
3130 //printf("s = %s, '%s'\n", s->kind(), s->toChars());
3134 e
= new VarExp(loc
, ti
->withsym
->withstate
->wthis
);
3135 e
= new DotVarExp(loc
, e
, s
->isDeclaration());
3138 e
= new DsymbolExp(loc
, s
);
3139 e
= e
->semantic(sc
);
3140 //printf("-1ScopeExp::semantic()\n");
3148 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars());
3152 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars());
3153 //printf("\tparent = '%s'\n", sds->parent->toChars());
3157 //printf("-2ScopeExp::semantic() %s\n", toChars());
3161 void ScopeExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
3163 if (sds
->isTemplateInstance())
3165 sds
->toCBuffer(buf
, hgs
);
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());
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
;
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
)
3223 ClassDeclaration
*cdthis
= NULL
;
3226 printf("NewExp::semantic() %s\n", toChars());
3228 printf("\tthisexp = %s\n", thisexp
->toChars());
3229 printf("\tnewtype: %s\n", newtype
->toChars());
3231 if (type
) // if semantic() already run
3236 { thisexp
= thisexp
->semantic(sc
);
3237 cdthis
= thisexp
->type
->isClassHandle();
3240 sc
= sc
->push(cdthis
);
3241 type
= newtype
->semantic(loc
, sc
);
3246 error("'this' for nested class must be a class type, not %s", thisexp
->type
->toChars());
3247 type
= newtype
->semantic(loc
, sc
);
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
)
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
);
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");
3292 // Supply an implicit 'this' and try again
3293 thisexp
= new ThisExp(loc
);
3294 for (Dsymbol
*sp
= sc
->parent
; 1; sp
= sp
->parent
)
3297 error("outer class %s 'this' needed to 'new' nested class %s", cdn
->toChars(), cd
->toChars());
3300 ClassDeclaration
*cdp
= sp
->isClassDeclaration();
3303 if (cdp
== cdn
|| cdn
->isBaseOf(cdp
, NULL
))
3305 // Add a '.outer' and try again
3306 thisexp
= new DotIdExp(loc
, thisexp
, Id::outer
);
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());
3320 for (Dsymbol
*sf
= sc
->func
; 1; sf
= sf
->toParent2()->isFuncDeclaration())
3324 error("outer class %s 'this' needed to 'new' nested class %s", cdn
->toChars(), cd
->toChars());
3327 printf("sf = %s\n", sf
->toChars());
3328 AggregateDeclaration
*ad
= sf
->isThis();
3329 if (ad
&& (ad
== cdn
|| cdn
->isBaseOf(ad
->isClassDeclaration(), NULL
)))
3336 error("e.new is only for allocating nested classes");
3339 error("e.new is only for allocating nested classes");
3341 FuncDeclaration
*f
= cd
->ctor
;
3345 f
= f
->overloadResolve(loc
, arguments
);
3346 checkDeprecated(sc
, f
);
3347 member
= f
->isCtorDeclaration();
3350 cd
->accessCheck(loc
, sc
, member
);
3352 tf
= (TypeFunction
*)f
->type
;
3356 arguments
= new Expressions();
3357 functionArguments(loc
, sc
, tf
, arguments
);
3361 if (arguments
&& arguments
->dim
)
3362 error("no constructor for %s", cd
->toChars());
3370 // Prepend the uint size argument to newargs[]
3371 e
= new IntegerExp(loc
, cd
->size(loc
), Type::tsize_t
);
3373 newargs
= new Expressions();
3376 f
= f
->overloadResolve(loc
, newargs
);
3377 allocator
= f
->isNewDeclaration();
3380 tf
= (TypeFunction
*)f
->type
;
3381 functionArguments(loc
, sc
, tf
, newargs
);
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
;
3397 if (arguments
&& arguments
->dim
)
3398 error("no constructor for %s", type
->toChars());
3404 // Prepend the uint size argument to newargs[]
3405 e
= new IntegerExp(loc
, sd
->size(loc
), Type::tsize_t
);
3407 newargs
= new Expressions();
3410 f
= f
->overloadResolve(loc
, newargs
);
3411 allocator
= f
->isNewDeclaration();
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();
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");
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();
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());
3464 int NewExp::checkSideEffect(int flag
)
3469 void NewExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
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
;
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
)
3517 printf("NewAnonClassExp::semantic() %s\n", toChars());
3518 //printf("type: %s\n", type->toChars());
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
)
3535 void NewAnonClassExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
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(" { }");
3559 cd
->toCBuffer(buf
, hgs
);
3563 /********************** SymOffExp **************************************/
3565 SymOffExp::SymOffExp(Loc loc
, Declaration
*var
, target_size_t offset
)
3566 : Expression(loc
, TOKsymoff
, sizeof(SymOffExp
))
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
)
3579 printf("SymOffExp::semantic('%s')\n", toChars());
3581 //var->semantic(sc);
3583 type
= var
->type
->pointerTo();
3584 VarDeclaration
*v
= var
->isVarDeclaration();
3586 v
->checkNestedReference(sc
, loc
);
3590 int SymOffExp::isBool(int result
)
3592 return result
? TRUE
: FALSE
;
3595 void SymOffExp::checkEscape()
3597 VarDeclaration
*v
= var
->isVarDeclaration();
3600 if (!v
->isDataseg())
3601 error("escaping reference to local variable %s", v
->toChars());
3605 void SymOffExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
3608 buf
->printf("(& %s+%"PRIuTSIZE
")", var
->toChars(), offset
);
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());
3620 this->type
= var
->type
;
3623 int VarExp::equals(Object
*o
)
3627 (((Expression
*)o
)->op
== TOKvar
&&
3628 ((ne
= (VarExp
*)o
), type
->equals(ne
->type
)) &&
3634 Expression
*VarExp::semantic(Scope
*sc
)
3635 { FuncLiteralDeclaration
*fd
;
3638 printf("VarExp::semantic(%s)\n", toChars());
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
);
3652 VarDeclaration
*v
= var
->isVarDeclaration();
3655 if (v
->isConst() && type
->toBasetype()->ty
!= Tsarray
&& v
->init
)
3657 ExpInitializer
*ei
= v
->init
->isExpInitializer();
3660 //ei->exp->implicitCastTo(sc, type)->print();
3661 return ei
->exp
->implicitCastTo(sc
, type
);
3664 v
->checkNestedReference(sc
, loc
);
3667 else if ((fd
= var
->isFuncLiteralDeclaration()) != NULL
)
3669 e
= new FuncExp(loc
, fd
);
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();
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
)
3706 tym
= tybasic(e1
->ET
->Tty
);
3707 if (!(tyscalar(tym
) ||
3709 tym
== TYarray
&& e
->Eoper
== TOKaddr
))
3710 synerr(EM_lvalue
); // lvalue expected
3712 if (var
->storage_class
& STClazy
)
3713 error("lazy variables cannot be lvalues");
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
;
3736 FuncDeclaration
*fd
= NULL
;
3738 fd
= s
->isFuncDeclaration();
3740 ((fd
->isCtorDeclaration() && var
->storage_class
& STCfield
) ||
3741 (fd
->isStaticCtorDeclaration() && !(var
->storage_class
& STCfield
))) &&
3742 fd
->toParent() == var
->toParent()
3745 VarDeclaration
*v
= var
->isVarDeclaration();
3748 //printf("setting ctorinit\n");
3753 { s
= s
->toParent2();
3758 const char *p
= var
->isStatic() ? "static " : "";
3759 error("can only initialize %sconst %s inside %sconstructor",
3760 p
, var
->toChars(), p
);
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);
3783 TupleExp::TupleExp(Loc loc
, TupleDeclaration
*tup
)
3784 : Expression(loc
, TOKtuple
, sizeof(TupleExp
))
3786 exps
= new Expressions();
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();
3798 else if (o
->dyncast() == DYNCAST_DSYMBOL
)
3800 Dsymbol
*s
= (Dsymbol
*)o
;
3801 Expression
*e
= new DsymbolExp(loc
, s
);
3804 else if (o
->dyncast() == DYNCAST_TYPE
)
3806 Type
*t
= (Type
*)o
;
3807 Expression
*e
= new TypeExp(loc
, t
);
3812 error("%s is not an expression", o
->toChars());
3817 int TupleExp::equals(Object
*o
)
3822 if (((Expression
*)o
)->op
== TOKtuple
)
3824 TupleExp
*te
= (TupleExp
*)o
;
3825 if (exps
->dim
!= te
->exps
->dim
)
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
))
3839 Expression
*TupleExp::syntaxCopy()
3841 return new TupleExp(loc
, arraySyntaxCopy(exps
));
3844 Expression
*TupleExp::semantic(Scope
*sc
)
3847 printf("+TupleExp::semantic(%s)\n", toChars());
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
);
3858 { error("%s has no value", e
->toChars());
3859 e
->type
= Type::terror
;
3861 exps
->data
[i
] = (void *)e
;
3865 if (0 && exps
->dim
== 1)
3867 return (Expression
*)exps
->data
[0];
3869 type
= new TypeTuple(exps
);
3870 //printf("-TupleExp::semantic(%s)\n", toChars());
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
)
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);
3894 void TupleExp::checkEscape()
3896 for (size_t i
= 0; i
< exps
->dim
; i
++)
3897 { Expression
*e
= (Expression
*)exps
->data
[i
];
3902 /******************************** FuncExp *********************************/
3904 FuncExp::FuncExp(Loc loc
, FuncLiteralDeclaration
*fd
)
3905 : Expression(loc
, TOKfunction
, sizeof(FuncExp
))
3910 Expression
*FuncExp::syntaxCopy()
3912 return new FuncExp(loc
, (FuncLiteralDeclaration
*)fd
->syntaxCopy(NULL
));
3915 Expression
*FuncExp::semantic(Scope
*sc
)
3918 printf("FuncExp::semantic(%s)\n", toChars());
3923 fd
->parent
= sc
->parent
;
3926 if (!fd
->type
->next
)
3927 fd
->type
->next
= Type::terror
;
3936 if (!global
.errors
&& global
.params
.useInline
)
3941 // Type is a "delegate to" or "pointer to" the function literal
3944 type
= new TypeDelegate(fd
->type
);
3945 type
= type
->semantic(loc
, sc
);
3949 type
= fd
->type
->pointerTo();
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
)
3985 printf("DeclarationExp::semantic() %s\n", toChars());
3988 /* This is here to support extern(linkage) declaration,
3989 * where the extern(linkage) winds up being an AttribDeclaration
3992 Dsymbol
*s
= declaration
;
3994 AttribDeclaration
*ad
= declaration
->isAttribDeclaration();
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:
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.
4015 error("declaration %s is already defined", s
->toPrettyChars());
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
)
4027 if (scx
->scopesym
&& scx
->scopesym
->symtab
&&
4028 (s2
= scx
->scopesym
->symtab
->lookup(s
->ident
)) != NULL
&&
4031 error("shadowing declaration %s is deprecated", s
->toPrettyChars());
4037 if (!s
->isVarDeclaration())
4039 declaration
->semantic(sc
);
4040 s
->parent
= sc
->parent
;
4044 declaration
->semantic2(sc
);
4047 declaration
->semantic3(sc
);
4049 if (!global
.errors
&& global
.params
.useInline
)
4050 declaration
->inlineScan();
4058 int DeclarationExp::checkSideEffect(int flag
)
4063 void DeclarationExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
4065 declaration
->toCBuffer(buf
, hgs
);
4069 /************************ TypeidExp ************************************/
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
)
4092 printf("TypeidExp::semantic()\n");
4094 typeidType
= typeidType
->semantic(loc
, sc
);
4095 e
= typeidType
->getTypeInfo(sc
);
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
)
4116 printf("HaltExp::semantic()\n");
4122 int HaltExp::checkSideEffect(int flag
)
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
))
4141 this->tspec
= tspec
;
4145 Expression
*IsExp::syntaxCopy()
4147 return new IsExp(loc
,
4151 tspec
? tspec
->syntaxCopy() : NULL
,
4155 Expression
*IsExp::semantic(Scope
*sc
)
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
;
4164 global
.gag
++; // suppress printing of error messages
4165 targ
= targ
->semantic(loc
, sc
);
4167 unsigned gerrors
= global
.errors
;
4168 global
.errors
= errors_save
;
4170 if (gerrors
) // if any errors happened
4171 { // then condition is false
4174 else if (tok2
!= TOKreserved
)
4179 if (targ
->ty
!= Ttypedef
)
4181 tded
= ((TypeTypedef
*)targ
)->sym
->basetype
;
4185 if (targ
->ty
!= Tstruct
)
4187 if (((TypeStruct
*)targ
)->sym
->isUnionDeclaration())
4193 if (targ
->ty
!= Tstruct
)
4195 if (!((TypeStruct
*)targ
)->sym
->isUnionDeclaration())
4201 if (targ
->ty
!= Tclass
)
4203 if (((TypeClass
*)targ
)->sym
->isInterfaceDeclaration())
4209 if (targ
->ty
!= Tclass
)
4211 if (!((TypeClass
*)targ
)->sym
->isInterfaceDeclaration())
4217 // If class or interface, get the base class and interfaces
4218 if (targ
->ty
!= Tclass
)
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
);
4233 if (targ
->ty
!= Tenum
)
4235 tded
= ((TypeEnum
*)targ
)->sym
->memtype
;
4239 if (targ
->ty
!= Tdelegate
)
4241 tded
= targ
->next
; // the underlying function type
4245 { if (targ
->ty
!= Tfunction
)
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
;
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
);
4265 /* Get the 'return type' for the function,
4266 * delegate, or pointer to function.
4268 if (targ
->ty
== Tfunction
)
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
;
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.
4290 TemplateTypeParameter
tp(loc
, id
, NULL
, NULL
);
4292 TemplateParameters parameters
;
4293 parameters
.setDim(1);
4294 parameters
.data
[0] = (void *)&tp
;
4298 dedtypes
.data
[0] = NULL
;
4300 m
= targ
->deduceType(NULL
, tspec
, ¶meters
, &dedtypes
);
4301 if (m
== MATCHnomatch
||
4302 (m
!= MATCHexact
&& tok
== TOKequal
))
4306 assert(dedtypes
.dim
== 1);
4307 tded
= (Type
*)dedtypes
.data
[0];
4315 /* Declare id as an alias for type targ. Evaluate to TRUE
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
))
4334 { if (targ
->equals(tspec
))
4344 Dsymbol
*s
= new AliasDeclaration(loc
, id
, tded
);
4348 s
->addMember(sc
, sc
->sd
, 1);
4350 return new IntegerExp(1);
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
));
4366 if (tok
== TOKcolon
)
4367 buf
->writestring(" : ");
4369 buf
->writestring(" == ");
4370 tspec
->toCBuffer(buf
, NULL
, hgs
);
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
);
4383 buf
->writeByte(')');
4387 /************************************************************/
4389 UnaExp::UnaExp(Loc loc
, enum TOK op
, int size
, Expression
*e1
)
4390 : Expression(loc
, op
, size
)
4395 Expression
*UnaExp::syntaxCopy()
4398 e
= (UnaExp
*)copy();
4400 e
->e1
= e
->e1
->syntaxCopy();
4404 Expression
*UnaExp::semantic(Scope
*sc
)
4407 printf("UnaExp::semantic('%s')\n", toChars());
4409 e1
= e1
->semantic(sc
);
4411 // error("%s has no value", e1->toChars());
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
)
4430 Expression
*BinExp::syntaxCopy()
4433 e
= (BinExp
*)copy();
4435 e
->e1
= e
->e1
->syntaxCopy();
4436 e
->e2
= e
->e2
->syntaxCopy();
4440 Expression
*BinExp::semantic(Scope
*sc
)
4443 printf("BinExp::semantic('%s')\n", toChars());
4445 e1
= e1
->semantic(sc
);
4448 error("%s has no value", e1
->toChars());
4449 e1
->type
= Type::terror
;
4451 e2
= e2
->semantic(sc
);
4454 error("%s has no value", e2
->toChars());
4455 e2
->type
= Type::terror
;
4461 Expression
*BinExp::semanticp(Scope
*sc
)
4463 BinExp::semantic(sc
);
4464 e1
= resolveProperties(sc
, e1
);
4465 e2
= resolveProperties(sc
, e2
);
4469 /***************************
4470 * Common semantic routine for some xxxAssignExp's.
4473 Expression
*BinExp::commonSemanticAssign(Scope
*sc
)
4478 BinExp::semantic(sc
);
4479 e2
= resolveProperties(sc
, e2
);
4481 e
= op_overload(sc
);
4485 e1
= e1
->modifiableLvalue(sc
, e1
);
4488 if (type
->toBasetype()->ty
== Tbool
)
4490 error("operator not allowed on bool expression %s", toChars());
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);
4504 Expression
*BinExp::commonSemanticAssignIntegral(Scope
*sc
)
4509 BinExp::semantic(sc
);
4510 e2
= resolveProperties(sc
, e2
);
4512 e
= op_overload(sc
);
4516 e1
= e1
->modifiableLvalue(sc
, e1
);
4519 if (type
->toBasetype()->ty
== Tbool
)
4521 e2
= e2
->implicitCastTo(sc
, type
);
4525 e1
->checkIntegral();
4526 e2
->checkIntegral();
4531 int BinExp::checkSideEffect(int flag
)
4533 if (op
== TOKplusplus
||
4534 op
== TOKminusminus
||
4536 op
== TOKconstruct
||
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
)
4587 printf("CompileExp::semantic('%s')\n", toChars());
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
;
4597 StringExp
*se
= (StringExp
*)e1
;
4598 se
= se
->toUTF8(sc
);
4599 Parser
p(sc
->module
, (unsigned char *)se
->string
, se
->len
, 0);
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
)
4627 printf("FileExp::semantic('%s')\n", toChars());
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());
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
);
4645 if (name
!= FileName::name(name
))
4646 { error("use -Jpath switch to provide path for filename %s", name
);
4650 name
= FileName::searchPath(global
.filePath
, name
, 0);
4652 { error("file %s cannot be found, check -Jpath", se
->toChars());
4656 if (global
.params
.verbose
)
4657 printf("file %s\t(%s)\n", se
->string
, name
);
4661 { error("cannot read file %s", f
.toChars());
4667 se
= new StringExp(loc
, f
.buffer
, f
.len
);
4671 return se
->semantic(sc
);
4674 se
= new StringExp(loc
, "");
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
)
4693 Expression
*AssertExp::syntaxCopy()
4695 AssertExp
*ae
= new AssertExp(loc
, e1
->syntaxCopy(),
4696 msg
? msg
->syntaxCopy() : NULL
);
4700 Expression
*AssertExp::semantic(Scope
*sc
)
4703 printf("AssertExp::semantic('%s')\n", toChars());
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();
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
);
4732 int AssertExp::checkSideEffect(int flag
)
4737 void AssertExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
4739 buf
->writestring("assert(");
4740 expToCBuffer(buf
, hgs
, e1
, PREC_assign
);
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
)
4763 printf("DotIdExp::semantic(this = %p, '%s')\n", this, toChars());
4764 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op));
4767 //{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; }
4770 /* Don't do semantic analysis if we'll be converting
4773 if (ident
== Id::stringof
)
4774 { char *s
= e1
->toChars();
4775 e
= new StringExp(loc
, s
, strlen(s
), 'c');
4776 e
= e
->semantic(sc
);
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();
4793 cd
= ad
->isClassDeclaration();
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
);
4809 sd
= ad
->isStructDeclaration();
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
;
4832 e1
= resolveProperties(sc
, 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
);
4844 if (eright
->op
== TOKimport
) // also used for template alias's
4847 ScopeExp
*ie
= (ScopeExp
*)eright
;
4849 s
= ie
->sds
->search(loc
, ident
, 0);
4853 checkDeprecated(sc
, s
);
4855 EnumMember
*em
= s
->isEnumMember();
4859 e
= e
->semantic(sc
);
4863 VarDeclaration
*v
= s
->isVarDeclaration();
4866 //printf("DotIdExp:: Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars());
4869 error("circular reference to '%s'", v
->toChars());
4870 type
= Type::tint32
;
4878 ExpInitializer
*ei
= v
->init
->isExpInitializer();
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
4891 else if (type
->isscalar())
4893 e
= type
->defaultInit();
4901 eleft
= new ThisExp(loc
);
4902 e
= new DotVarExp(loc
, eleft
, v
);
4903 e
= e
->semantic(sc
);
4907 e
= new VarExp(loc
, v
);
4909 { e
= new CommaExp(loc
, eleft
, e
);
4916 FuncDeclaration
*f
= s
->isFuncDeclaration();
4919 //printf("it's a function\n");
4923 eleft
= new ThisExp(loc
);
4924 e
= new DotVarExp(loc
, eleft
, f
);
4925 e
= e
->semantic(sc
);
4929 e
= new VarExp(loc
, f
);
4931 { e
= new CommaExp(loc
, eleft
, e
);
4938 Type
*t
= s
->getType();
4941 return new TypeExp(loc
, t
);
4944 TupleDeclaration
*tup
= s
->isTupleDeclaration();
4948 error("cannot have e.tuple");
4949 e
= new TupleExp(loc
, tup
);
4950 e
= e
->semantic(sc
);
4954 ScopeDsymbol
*sds
= s
->isScopeDsymbol();
4957 //printf("it's a ScopeDsymbol\n");
4958 e
= new ScopeExp(loc
, sds
);
4959 e
= e
->semantic(sc
);
4961 e
= new DotExp(loc
, eleft
, e
);
4965 Import
*imp
= s
->isImport();
4970 ie
= new ScopeExp(loc
, imp
->pkg
);
4971 return ie
->semantic(sc
);
4974 // BUG: handle other cases like in IdentifierExp::semantic()
4976 printf("s = '%s', kind = '%s'\n", s
->toChars(), s
->kind());
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
);
4986 error("undefined identifier %s", toChars());
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
);
5001 e
= e1
->type
->dotExp(sc
, e1
, ident
);
5002 e
= e
->semantic(sc
);
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
)
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");
5043 Expression
*DotVarExp::semantic(Scope
*sc
)
5046 printf("DotVarExp::semantic('%s')\n", toChars());
5050 var
= var
->toAlias()->isDeclaration();
5052 TupleDeclaration
*tup
= var
->isTupleDeclaration();
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());
5070 Expression
*e
= (Expression
*)o
;
5071 if (e
->op
!= TOKdsymbol
)
5072 error("%s is not a member", e
->toChars());
5074 { DsymbolExp
*ve
= (DsymbolExp
*)e
;
5076 e
= new DotVarExp(loc
, e1
, ve
->s
->isDeclaration());
5081 Expression
*e
= new TupleExp(loc
, exps
);
5082 e
= e
->semantic(sc
);
5086 e1
= e1
->semantic(sc
);
5088 if (!type
&& global
.errors
)
5089 { // var is goofed up, just return 0
5090 return new IntegerExp(0);
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());
5105 Expression
*DotVarExp::toLvalue(Scope
*sc
, Expression
*e
)
5107 //printf("DotVarExp::toLvalue(%s)\n", toChars());
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
;
5120 FuncDeclaration
*fd
= NULL
;
5122 fd
= s
->isFuncDeclaration();
5124 ((fd
->isCtorDeclaration() && var
->storage_class
& STCfield
) ||
5125 (fd
->isStaticCtorDeclaration() && !(var
->storage_class
& STCfield
))) &&
5126 fd
->toParent() == var
->toParent() &&
5130 VarDeclaration
*v
= var
->isVarDeclaration();
5133 //printf("setting ctorinit\n");
5138 { s
= s
->toParent2();
5143 const char *p
= var
->isStatic() ? "static " : "";
5144 error("can only initialize %sconst member %s inside %sconstructor",
5145 p
, var
->toChars(), p
);
5154 void DotVarExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
5156 expToCBuffer(buf
, hgs
, e1
, PREC_primary
);
5157 buf
->writeByte('.');
5158 buf
->writestring(var
->toChars());
5161 /************************************************************/
5167 DotTemplateInstanceExp::DotTemplateInstanceExp(Loc loc
, Expression
*e
, TemplateInstance
*ti
)
5168 : UnaExp(loc
, TOKdotti
, sizeof(DotTemplateInstanceExp
), e
)
5170 //printf("DotTemplateInstanceExp()\n");
5174 Expression
*DotTemplateInstanceExp::syntaxCopy()
5176 DotTemplateInstanceExp
*de
= new DotTemplateInstanceExp(loc
,
5178 (TemplateInstance
*)ti
->syntaxCopy(NULL
));
5182 Expression
*DotTemplateInstanceExp::semantic(Scope
*sc
)
5185 TemplateDeclaration
*td
;
5189 Expression
*eleft
= NULL
;
5193 printf("DotTemplateInstanceExp::semantic('%s')\n", toChars());
5197 e1
= e1
->semantic(sc
);
5200 t1
= t1
->toBasetype();
5202 if (e1
->op
== TOKdotexp
)
5203 { DotExp
*de
= (DotExp
*)e1
;
5211 if (eright
->op
== TOKimport
)
5213 s
= ((ScopeExp
*)eright
)->sds
;
5215 else if (e1
->op
== TOKtype
)
5217 s
= t1
->isClassHandle();
5219 { if (t1
->ty
== Tstruct
)
5220 s
= ((TypeStruct
*)t1
)->sym
;
5225 else if (t1
&& (t1
->ty
== Tstruct
|| t1
->ty
== Tclass
))
5227 s
= t1
->toDsymbol(sc
);
5230 else if (t1
&& t1
->ty
== Tpointer
)
5232 t1
= t1
->next
->toBasetype();
5233 if (t1
->ty
!= Tstruct
)
5235 s
= t1
->toDsymbol(sc
);
5241 error("template %s is not a member of %s", ti
->toChars(), e1
->toChars());
5247 s2
= s
->search(loc
, id
, 0);
5249 { error("template identifier %s is not a member of %s %s", id
->toChars(), s
->kind(), s
->ident
->toChars());
5255 td
= s
->isTemplateDeclaration();
5258 error("%s is not a template", id
->toChars());
5270 s
= ti
->inst
->toAlias();
5271 v
= s
->isDeclaration();
5273 { e
= new DotVarExp(loc
, eleft
, v
);
5274 e
= e
->semantic(sc
);
5279 e
= new ScopeExp(loc
, ti
);
5282 e
= new DotExp(loc
, eleft
, e
);
5284 e
= e
->semantic(sc
);
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
)
5306 Expression
*DelegateExp::semantic(Scope
*sc
)
5309 printf("DelegateExp::semantic('%s')\n", toChars());
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
);
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
)
5340 this->type
= s
->getType();
5343 Expression
*DotTypeExp::semantic(Scope
*sc
)
5346 printf("DotTypeExp::semantic('%s')\n", toChars());
5348 UnaExp::semantic(sc
);
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
)
5409 printf("CallExp::semantic() %s\n", toChars());
5412 return this; // semantic() already run
5414 if (arguments
&& arguments
->dim
)
5416 Expression
*earg
= (Expression
*)arguments
->data
[0];
5418 if (earg
->type
) earg
->type
->print();
5422 if (e1
->op
== TOKdelegate
)
5423 { DelegateExp
*de
= (DelegateExp
*)e1
;
5425 e1
= new DotVarExp(de
->loc
, de
->e1
, de
->func
);
5426 return semantic(sc
);
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
);
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()");
5449 Expression
*key
= (Expression
*)arguments
->data
[0];
5450 key
= key
->semantic(sc
);
5451 key
= resolveProperties(sc
, key
);
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
)
5463 arguments
= new Expressions();
5464 arguments
->shift(dotid
->e1
);
5465 e1
= new IdentifierExp(dotid
->loc
, dotid
->ident
);
5473 if (e1
->op
== TOKthis
|| e1
->op
== TOKsuper
)
5475 // semantic() run later for these
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
);
5520 if (e1
->op
== TOKcomma
)
5522 CommaExp
*ce
= (CommaExp
*)e1
;
5525 e1
->type
= ce
->type
;
5528 return ce
->semantic(sc
);
5533 t1
= e1
->type
->toBasetype();
5535 // Check for call operator overload
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
5554 else if (t1
->ty
== Tclass
)
5556 ad
= ((TypeClass
*)t1
)->sym
;
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
);
5567 arrayExpressionSemantic(arguments
, sc
);
5568 preFunctionArguments(loc
, sc
, arguments
);
5570 if (e1
->op
== TOKdotvar
&& t1
->ty
== Tfunction
||
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();
5584 f
= f
->overloadResolve(loc
, arguments
);
5586 ad
= f
->toParent()->isAggregateDeclaration();
5589 { dte
= (DotTemplateExp
*)(e1
);
5590 TemplateDeclaration
*td
= dte
->td
;
5593 // Should fix deduceFunctionTemplate() so it works on NULL argument
5594 arguments
= new Expressions();
5595 f
= td
->deduceFunctionTemplate(sc
, loc
, NULL
, arguments
);
5597 { type
= Type::terror
;
5600 ad
= td
->toParent()->isAggregateDeclaration();
5603 ue
->e1
= getRightThis(loc
, sc
, ad
, ue
->e1
, f
);
5605 checkDeprecated(sc
, f
);
5606 accessCheck(loc
, sc
, ue
->e1
, f
);
5609 VarExp
*ve
= new VarExp(loc
, f
);
5610 e1
= new CommaExp(loc
, ue
->e1
, ve
);
5615 if (e1
->op
== TOKdotvar
)
5618 e1
= new DotVarExp(loc
, dte
->e1
, f
);
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
);
5633 else if (e1
->op
== TOKsuper
)
5635 // Base class constructor call
5636 ClassDeclaration
*cd
= NULL
;
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
;
5648 f
= cd
->baseClass
->ctor
;
5650 { error("no super class constructor for %s", cd
->baseClass
->toChars());
5651 type
= Type::terror
;
5657 if (sc
->callSuper
& (CSXthis
| CSXsuper
))
5658 error("reference to this before super()");
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
);
5674 else if (e1
->op
== TOKthis
)
5676 // same class constructor call
5677 ClassDeclaration
*cd
= NULL
;
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
;
5690 if (sc
->callSuper
& (CSXthis
| CSXsuper
))
5691 error("reference to this before super()");
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
;
5700 f
= f
->overloadResolve(loc
, arguments
);
5701 checkDeprecated(sc
, f
);
5702 e1
= new DotVarExp(e1
->loc
, e1
, f
);
5703 e1
= e1
->semantic(sc
);
5706 // BUG: this should really be done by checking the static
5709 error("cyclic constructor call");
5714 error("function expected before (), not '%s'", e1
->toChars());
5715 type
= Type::terror
;
5718 else if (t1
->ty
!= Tfunction
)
5720 if (t1
->ty
== Tdelegate
)
5722 assert(t1
->next
->ty
== Tfunction
);
5723 tf
= (TypeFunction
*)(t1
->next
);
5726 else if (t1
->ty
== Tpointer
&& t1
->next
->ty
== Tfunction
)
5729 e
= new PtrExp(loc
, e1
);
5734 else if (e1
->op
== TOKtemplate
)
5736 TemplateExp
*te
= (TemplateExp
*)e1
;
5737 f
= te
->td
->deduceFunctionTemplate(sc
, loc
, NULL
, arguments
);
5739 { type
= Type::terror
;
5742 if (f
->needThis() && hasThis(sc
))
5744 // Supply an implicit 'this', as in
5747 e1
= new DotTemplateExp(loc
, (new ThisExp(loc
))->semantic(sc
), te
->td
);
5751 e1
= new VarExp(loc
, f
);
5755 { error("function expected before (), not %s of type %s", e1
->toChars(), e1
->type
->toChars());
5756 type
= Type::terror
;
5760 else if (e1
->op
== TOKvar
)
5762 // Do overload resolution
5763 VarExp
*ve
= (VarExp
*)e1
;
5765 f
= ve
->var
->isFuncDeclaration();
5768 // Look to see if f is really a function template
5769 if (0 && !istemp
&& f
->parent
)
5770 { TemplateInstance
*ti
= f
->parent
->isTemplateInstance();
5773 (ti
->name
== f
->ident
||
5774 ti
->toAlias()->ident
== f
->ident
)
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
);
5791 f
= f
->overloadResolve(loc
, arguments
);
5792 checkDeprecated(sc
, f
);
5794 if (f
->needThis() && hasThis(sc
))
5796 // Supply an implicit 'this', as in
5799 e1
= new DotVarExp(loc
, new ThisExp(loc
), f
);
5803 accessCheck(loc
, sc
, NULL
, f
);
5809 assert(t1
->ty
== Tfunction
);
5810 tf
= (TypeFunction
*)(t1
);
5813 assert(tf
->ty
== Tfunction
);
5817 arguments
= new Expressions();
5818 functionArguments(loc
, sc
, tf
, arguments
);
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
);
5837 int CallExp::checkSideEffect(int flag
)
5842 Expression
*CallExp::toLvalue(Scope
*sc
, Expression
*e
)
5844 if (type
->toBasetype()->ty
== Tstruct
)
5847 return Expression::toLvalue(sc
, e
);
5850 void CallExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
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
)
5870 printf("AddrExp::semantic('%s')\n", toChars());
5874 UnaExp::semantic(sc
);
5875 e1
= e1
->toLvalue(sc
, NULL
);
5878 error("cannot take address of %s", e1
->toChars());
5879 type
= Type::tint32
;
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();
5893 e
= new DelegateExp(loc
, dve
->e1
, f
);
5894 e
= e
->semantic(sc
);
5898 else if (e1
->op
== TOKvar
)
5900 VarExp
*dve
= (VarExp
*)e1
;
5901 FuncDeclaration
*f
= dve
->var
->isFuncDeclaration();
5903 if (f
&& f
->isNested())
5906 e
= new DelegateExp(loc
, e1
, f
);
5907 e
= e
->semantic(sc
);
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
);
5921 /************************************************************/
5923 PtrExp::PtrExp(Loc loc
, Expression
*e
)
5924 : UnaExp(loc
, TOKstar
, sizeof(PtrExp
), e
)
5927 type
= e
->type
->next
;
5930 PtrExp::PtrExp(Loc loc
, Expression
*e
, Type
*t
)
5931 : UnaExp(loc
, TOKstar
, sizeof(PtrExp
), e
)
5936 Expression
*PtrExp::semantic(Scope
*sc
)
5940 printf("PtrExp::semantic('%s')\n", toChars());
5942 UnaExp::semantic(sc
);
5943 e1
= resolveProperties(sc
, e1
);
5947 printf("PtrExp::semantic('%s')\n", toChars());
5948 tb
= e1
->type
->toBasetype();
5956 // Rewrite *p as p[0]
5957 e
= new IndexExp(loc
, e1
, new IntegerExp(0));
5958 return e
->semantic(sc
);
5965 e1
= e1
->castTo(sc
, type
->pointerTo());
5969 error("can only * a pointer, not a '%s'", e1
->type
->toChars());
5970 type
= Type::tint32
;
5977 Expression
*PtrExp::toLvalue(Scope
*sc
, Expression
*e
)
5980 tym
= tybasic(e1
->ET
->Tty
);
5981 if (!(tyscalar(tym
) ||
5983 tym
== TYarray
&& e
->Eoper
== TOKaddr
))
5984 synerr(EM_lvalue
); // lvalue expected
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
)
6006 printf("NegExp::semantic('%s')\n", toChars());
6010 UnaExp::semantic(sc
);
6011 e1
= resolveProperties(sc
, e1
);
6012 e
= op_overload(sc
);
6017 e1
->checkArithmetic();
6023 /************************************************************/
6025 UAddExp::UAddExp(Loc loc
, Expression
*e
)
6026 : UnaExp(loc
, TOKuadd
, sizeof(UAddExp
), e
)
6030 Expression
*UAddExp::semantic(Scope
*sc
)
6034 printf("UAddExp::semantic('%s')\n", toChars());
6037 UnaExp::semantic(sc
);
6038 e1
= resolveProperties(sc
, e1
);
6039 e
= op_overload(sc
);
6043 e1
->checkArithmetic();
6047 /************************************************************/
6049 ComExp::ComExp(Loc loc
, Expression
*e
)
6050 : UnaExp(loc
, TOKtilde
, sizeof(ComExp
), e
)
6054 Expression
*ComExp::semantic(Scope
*sc
)
6059 UnaExp::semantic(sc
);
6060 e1
= resolveProperties(sc
, e1
);
6061 e
= op_overload(sc
);
6066 e1
= e1
->checkIntegral();
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
;
6095 /************************************************************/
6097 BoolExp::BoolExp(Loc loc
, Expression
*e
, Type
*t
)
6098 : UnaExp(loc
, TOKtobool
, sizeof(BoolExp
), e
)
6103 Expression
*BoolExp::semantic(Scope
*sc
)
6105 UnaExp::semantic(sc
);
6106 e1
= resolveProperties(sc
, e1
);
6107 e1
= e1
->checkToBoolean();
6108 type
= Type::tboolean
;
6112 int BoolExp::isBit()
6117 /************************************************************/
6119 DeleteExp::DeleteExp(Loc loc
, Expression
*e
)
6120 : UnaExp(loc
, TOKdelete
, sizeof(DeleteExp
), e
)
6124 Expression
*DeleteExp::semantic(Scope
*sc
)
6128 UnaExp::semantic(sc
);
6129 e1
= resolveProperties(sc
, e1
);
6130 e1
= e1
->toLvalue(sc
, NULL
);
6133 tb
= e1
->type
->toBasetype();
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());
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
;
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
);
6170 if (e1
->op
== TOKindex
)
6172 IndexExp
*ae
= (IndexExp
*)(e1
);
6173 Type
*tb1
= ae
->e1
->type
->toBasetype();
6174 if (tb1
->ty
== Taarray
)
6177 error("cannot delete type %s", e1
->type
->toChars());
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)");
6194 int DeleteExp::checkSideEffect(int flag
)
6199 Expression
*DeleteExp::checkToBoolean()
6201 error("delete does not give a boolean result");
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
)
6219 Expression
*CastExp::syntaxCopy()
6221 return new CastExp(loc
, e1
->syntaxCopy(), to
->syntaxCopy());
6225 Expression
*CastExp::semantic(Scope
*sc
)
6231 printf("CastExp::semantic('%s')\n", toChars());
6234 //static int x; assert(++x < 10);
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
);
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)
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
);
6270 e
= e1
->castTo(sc
, to
);
6274 int CastExp::checkSideEffect(int flag
)
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
);
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();
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
)
6319 Expression
*SliceExp::syntaxCopy()
6321 Expression
*lwr
= NULL
;
6323 lwr
= this->lwr
->syntaxCopy();
6325 Expression
*upr
= NULL
;
6327 upr
= this->upr
->syntaxCopy();
6329 return new SliceExp(loc
, e1
->syntaxCopy(), lwr
, upr
);
6332 Expression
*SliceExp::semantic(Scope
*sc
)
6334 AggregateDeclaration
*ad
;
6335 //FuncDeclaration *fd;
6339 printf("SliceExp::semantic('%s')\n", toChars());
6344 UnaExp::semantic(sc
);
6345 e1
= resolveProperties(sc
, e1
);
6349 Type
*t
= e1
->type
->toBasetype();
6350 if (t
->ty
== Tpointer
)
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
;
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
);
6379 e
= new CallExp(loc
, e
, lwr
, upr
);
6383 e
= new CallExp(loc
, e
);
6385 e
= e
->semantic(sc
);
6390 else if (t
->ty
== Ttuple
)
6395 { error("need upper and lower bound to slice tuple");
6402 if (t
->ty
== Tsarray
|| t
->ty
== Tarray
|| t
->ty
== Ttuple
)
6404 sym
= new ArrayScopeSymbol(this);
6406 sym
->parent
= sc
->scopesym
;
6411 { lwr
= lwr
->semantic(sc
);
6412 lwr
= resolveProperties(sc
, lwr
);
6413 lwr
= lwr
->implicitCastTo(sc
, Type::tsize_t
);
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
)
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();
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
);
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
);
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
);
6466 e
= new TypeExp(e1
->loc
, new TypeTuple(args
));
6468 e
= e
->semantic(sc
);
6472 error("string slice [%"PRIuMAX
" .. %"PRIuMAX
"] is out of bounds", i1
, i2
);
6478 type
= t
->next
->arrayOf();
6487 error("%s cannot be sliced with []", s
);
6488 type
= Type::terror
;
6492 void SliceExp::checkEscape()
6497 Expression
*SliceExp::toLvalue(Scope
*sc
, Expression
*e
)
6502 Expression
*SliceExp::modifiableLvalue(Scope
*sc
, Expression
*e
)
6504 error("slice expression %s is not a modifiable lvalue", toChars());
6508 void SliceExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
6510 expToCBuffer(buf
, hgs
, e1
, precedence
[op
]);
6511 buf
->writeByte('[');
6515 expToCBuffer(buf
, hgs
, lwr
, PREC_assign
);
6517 buf
->writeByte('0');
6518 buf
->writestring("..");
6520 expToCBuffer(buf
, hgs
, upr
, PREC_assign
);
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
)
6538 printf("ArrayLengthExp::semantic('%s')\n", toChars());
6542 UnaExp::semantic(sc
);
6543 e1
= resolveProperties(sc
, e1
);
6545 type
= Type::tsize_t
;
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
)
6566 Expression
*ArrayExp::syntaxCopy()
6568 return new ArrayExp(loc
, e1
->syntaxCopy(), arraySyntaxCopy(arguments
));
6571 Expression
*ArrayExp::semantic(Scope
*sc
)
6576 printf("ArrayExp::semantic('%s')\n", toChars());
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
);
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
);
6605 { error("no [] operator overload for type %s", e1
->type
->toChars());
6612 Expression
*ArrayExp::toLvalue(Scope
*sc
, Expression
*e
)
6614 if (type
&& type
->toBasetype()->ty
== Tvoid
)
6615 error("voids have no value");
6620 void ArrayExp::toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
)
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
)
6639 printf("DotExp::semantic('%s')\n", toChars());
6640 if (type
) printf("\ttype = %s\n", type
->toChars());
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();
6649 { Expression
*e
= new DotTemplateExp(loc
, e1
, td
);
6650 e
= e
->semantic(sc
);
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
)
6670 { BinExp::semanticp(sc
);
6676 void CommaExp::checkEscape()
6681 Expression
*CommaExp::toLvalue(Scope
*sc
, Expression
*e
)
6683 e2
= e2
->toLvalue(sc
, NULL
);
6687 Expression
*CommaExp::modifiableLvalue(Scope
*sc
, Expression
*e
)
6689 e2
= e2
->modifiableLvalue(sc
, e
);
6693 int CommaExp::isBool(int result
)
6695 return e2
->isBool(result
);
6698 int CommaExp::checkSideEffect(int flag
)
6701 return e1
->checkSideEffect(2) || e2
->checkSideEffect(2);
6704 // Don't check e1 until we cast(void) the a,b code generation
6705 return e2
->checkSideEffect(flag
);
6709 /************************** IndexExp **********************************/
6713 IndexExp::IndexExp(Loc loc
, Expression
*e1
, Expression
*e2
)
6714 : BinExp(loc
, TOKindex
, sizeof(IndexExp
), e1
, e2
)
6716 //printf("IndexExp::IndexExp('%s')\n", toChars());
6718 modifiable
= 0; // assume it is an rvalue
6721 Expression
*IndexExp::semantic(Scope
*sc
)
6729 printf("IndexExp::semantic('%s')\n", toChars());
6734 e1
= e1
->semantic(sc
);
6735 assert(e1
->type
); // semantic() should already be run on it
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);
6746 sym
->parent
= sc
->scopesym
;
6750 e2
= e2
->semantic(sc
);
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
)
6765 e2
= e2
->implicitCastTo(sc
, Type::tsize_t
);
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]",
6792 { TypeAArray
*taa
= (TypeAArray
*)t1
;
6794 e2
= e2
->implicitCastTo(sc
, taa
->index
); // type checking
6795 e2
= e2
->implicitCastTo(sc
, taa
->key
); // actual argument type
6802 e2
= e2
->implicitCastTo(sc
, Type::tsize_t
);
6803 e2
= e2
->optimize(WANTvalue
);
6804 uinteger_t index
= e2
->toUInteger();
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
);
6824 if (e1
->op
== TOKtuple
)
6825 e
= (Expression
*)te
->exps
->data
[(size_t)index
];
6827 e
= new TypeExp(e1
->loc
, Argument::getNth(tup
->arguments
, (size_t)index
)->type
);
6831 error("array index [%"PRIuMAX
"] is outside array bounds [0 .. %"PRIuSIZE
"]",
6839 error("%s must be an array or pointer type, not %s",
6840 e1
->toChars(), e1
->type
->toChars());
6841 type
= Type::tint32
;
6847 Expression
*IndexExp::toLvalue(Scope
*sc
, Expression
*e
)
6849 // if (type && type->toBasetype()->ty == Tvoid)
6850 // error("voids have no value");
6854 Expression
*IndexExp::modifiableLvalue(Scope
*sc
, Expression
*e
)
6856 //printf("IndexExp::modifiableLvalue(%s)\n", toChars());
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;
6887 BinExp::semantic(sc
);
6888 e2
= resolveProperties(sc
, e2
);
6890 e
= op_overload(sc
);
6895 e1
= e1
->modifiableLvalue(sc
, e1
);
6898 if (e1
->type
->ty
== Tpointer
)
6899 e
= scaleFactor(sc
);
6901 e2
= e2
->castTo(sc
, e1
->type
);
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
)
6923 Expression
*AssignExp::semantic(Scope
*sc
)
6925 Expression
*e1old
= e1
;
6928 printf("AssignExp::semantic('%s')\n", toChars());
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
)
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
;
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();
6959 e
= new CallExp(loc
, e
, a
);
6960 e
= e
->semantic(sc
);
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
);
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
)
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
;
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();
7014 e
= new CallExp(loc
, e
, a
);
7015 e
= e
->semantic(sc
);
7021 BinExp::semantic(sc
);
7022 e2
= resolveProperties(sc
, e2
);
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
);
7036 { Expressions
*exps
= new Expressions
;
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
);
7050 t1
= e1
->type
->toBasetype();
7052 if (t1
->ty
== Tfunction
)
7053 { // Rewrite f=value to f(value)
7056 e
= new CallExp(loc
, e1
, e2
);
7057 e
= e
->semantic(sc
);
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
);
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
)
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
&&
7093 e2
->implicitConvTo(t1
->nextOf())
7094 // !(t1->nextOf()->equals(e2->type->nextOf()))
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());
7106 e2
= e2
->implicitCastTo(sc
, e1
->type
);
7113 Expression
*AssignExp::checkToBoolean()
7117 // are usually mistakes.
7119 error("'=' does not give a boolean result");
7123 /************************************************************/
7125 AddAssignExp::AddAssignExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7126 : BinExp(loc
, TOKaddass
, sizeof(AddAssignExp
), e1
, e2
)
7130 Expression
*AddAssignExp::semantic(Scope
*sc
)
7136 BinExp::semantic(sc
);
7137 e2
= resolveProperties(sc
, e2
);
7139 e
= op_overload(sc
);
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
)
7160 if (tb1
->ty
== Tpointer
&& tb2
->isintegral())
7161 e
= scaleFactor(sc
);
7162 else if (tb1
->ty
== Tbit
|| tb1
->ty
== Tbool
)
7165 // Need to rethink this
7166 if (e1
->op
!= TOKvar
)
7167 { // Rewrite e1+=e2 to (v=&e1),*v=*v+e2
7175 sprintf(name
, "__name%d", ++idn
);
7176 id
= Lexer::idPool(name
);
7178 v
= new VarDeclaration(loc
, tb1
->pointerTo(), id
, NULL
);
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
);
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
);
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
);
7223 /************************************************************/
7225 MinAssignExp::MinAssignExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7226 : BinExp(loc
, TOKminass
, sizeof(MinAssignExp
), e1
, e2
)
7230 Expression
*MinAssignExp::semantic(Scope
*sc
)
7236 BinExp::semantic(sc
);
7237 e2
= resolveProperties(sc
, e2
);
7239 e
= op_overload(sc
);
7243 e1
= e1
->modifiableLvalue(sc
, e1
);
7246 if (e1
->type
->ty
== Tpointer
&& e2
->type
->isintegral())
7247 e
= scaleFactor(sc
);
7250 e1
= e1
->checkArithmetic();
7251 e2
= e2
->checkArithmetic();
7254 if (type
->isreal() || type
->isimaginary())
7256 assert(e2
->type
->isfloating());
7257 e2
= e2
->castTo(sc
, e1
->type
);
7264 /************************************************************/
7266 CatAssignExp::CatAssignExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7267 : BinExp(loc
, TOKcatass
, sizeof(CatAssignExp
), e1
, e2
)
7271 Expression
*CatAssignExp::semantic(Scope
*sc
)
7274 BinExp::semantic(sc
);
7275 e2
= resolveProperties(sc
, e2
);
7277 e
= op_overload(sc
);
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();
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)
7301 e2
= e2
->castTo(sc
, e1
->type
);
7305 else if ((tb1
->ty
== Tarray
) &&
7306 e2
->implicitConvTo(tb1
->next
)
7309 e2
= e2
->castTo(sc
, tb1
->next
);
7315 error("cannot append type %s to type %s", tb2
->toChars(), tb1
->toChars());
7316 type
= Type::tint32
;
7322 /************************************************************/
7324 MulAssignExp::MulAssignExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7325 : BinExp(loc
, TOKmulass
, sizeof(MulAssignExp
), e1
, e2
)
7329 Expression
*MulAssignExp::semantic(Scope
*sc
)
7332 BinExp::semantic(sc
);
7333 e2
= resolveProperties(sc
, e2
);
7335 e
= op_overload(sc
);
7339 e1
= e1
->modifiableLvalue(sc
, e1
);
7344 e1
->checkArithmetic();
7345 e2
->checkArithmetic();
7346 if (e2
->type
->isfloating())
7354 if (t2
->isimaginary() || t2
->iscomplex())
7356 e2
= e2
->castTo(sc
, t1
);
7359 else if (t1
->isimaginary())
7361 if (t2
->isimaginary() || t2
->iscomplex())
7365 case Timaginary32
: t2
= Type::tfloat32
; break;
7366 case Timaginary64
: t2
= Type::tfloat64
; break;
7367 case Timaginary80
: t2
= Type::tfloat80
; break;
7371 e2
= e2
->castTo(sc
, t2
);
7378 /************************************************************/
7380 DivAssignExp::DivAssignExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7381 : BinExp(loc
, TOKdivass
, sizeof(DivAssignExp
), e1
, e2
)
7385 Expression
*DivAssignExp::semantic(Scope
*sc
)
7388 BinExp::semantic(sc
);
7389 e2
= resolveProperties(sc
, e2
);
7391 e
= op_overload(sc
);
7395 e1
= e1
->modifiableLvalue(sc
, e1
);
7400 e1
->checkArithmetic();
7401 e2
->checkArithmetic();
7402 if (e2
->type
->isimaginary())
7409 // Therefore, the result is 0
7410 e2
= new CommaExp(loc
, e2
, new RealExp(loc
, 0, t1
));
7412 e
= new AssignExp(loc
, e1
, e2
);
7416 else if (t1
->isimaginary())
7421 case Timaginary32
: t2
= Type::tfloat32
; break;
7422 case Timaginary64
: t2
= Type::tfloat64
; break;
7423 case Timaginary80
: t2
= Type::tfloat80
; break;
7427 e2
= e2
->castTo(sc
, t2
);
7428 e
= new AssignExp(loc
, e1
, e2
);
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
)
7458 //printf("ShlAssignExp::semantic()\n");
7459 BinExp::semantic(sc
);
7460 e2
= resolveProperties(sc
, e2
);
7462 e
= op_overload(sc
);
7466 e1
= e1
->modifiableLvalue(sc
, e1
);
7471 e1
->checkIntegral();
7472 e2
= e2
->checkIntegral();
7473 e2
= e2
->castTo(sc
, Type::tshiftcnt
);
7477 /************************************************************/
7479 ShrAssignExp::ShrAssignExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7480 : BinExp(loc
, TOKshrass
, sizeof(ShrAssignExp
), e1
, e2
)
7484 Expression
*ShrAssignExp::semantic(Scope
*sc
)
7487 BinExp::semantic(sc
);
7488 e2
= resolveProperties(sc
, e2
);
7490 e
= op_overload(sc
);
7494 e1
= e1
->modifiableLvalue(sc
, e1
);
7499 e1
->checkIntegral();
7500 e2
= e2
->checkIntegral();
7501 e2
= e2
->castTo(sc
, Type::tshiftcnt
);
7505 /************************************************************/
7507 UshrAssignExp::UshrAssignExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7508 : BinExp(loc
, TOKushrass
, sizeof(UshrAssignExp
), e1
, e2
)
7512 Expression
*UshrAssignExp::semantic(Scope
*sc
)
7515 BinExp::semantic(sc
);
7516 e2
= resolveProperties(sc
, e2
);
7518 e
= op_overload(sc
);
7522 e1
= e1
->modifiableLvalue(sc
, e1
);
7527 e1
->checkIntegral();
7528 e2
= e2
->checkIntegral();
7529 e2
= e2
->castTo(sc
, Type::tshiftcnt
);
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
)
7580 printf("AddExp::semantic('%s')\n", toChars());
7584 BinExp::semanticp(sc
);
7586 e
= op_overload(sc
);
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
)
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();
7613 if ((e1
->type
->isreal() && e2
->type
->isimaginary()) ||
7614 (e1
->type
->isimaginary() && e2
->type
->isreal()))
7616 switch (type
->toBasetype()->ty
)
7620 type
= Type::tcomplex32
;
7625 type
= Type::tcomplex64
;
7630 type
= Type::tcomplex80
;
7644 /************************************************************/
7646 MinExp::MinExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7647 : BinExp(loc
, TOKmin
, sizeof(MinExp
), e1
, e2
)
7651 Expression
*MinExp::semantic(Scope
*sc
)
7657 printf("MinExp::semantic('%s')\n", toChars());
7662 BinExp::semanticp(sc
);
7664 e
= op_overload(sc
);
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
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
;
7686 else if (t2
->isintegral())
7687 e
= scaleFactor(sc
);
7689 { error("incompatible types for -");
7690 return new IntegerExp(0);
7693 else if (t2
->ty
== Tpointer
)
7696 error("can't subtract pointer from %s", e1
->type
->toChars());
7697 return new IntegerExp(0);
7702 t1
= e1
->type
->toBasetype();
7703 t2
= e2
->type
->toBasetype();
7704 if ((t1
->isreal() && t2
->isimaginary()) ||
7705 (t1
->isimaginary() && t2
->isreal()))
7711 type
= Type::tcomplex32
;
7716 type
= Type::tcomplex64
;
7721 type
= Type::tcomplex80
;
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
)
7742 //printf("CatExp::semantic() %s\n", toChars());
7745 BinExp::semanticp(sc
);
7746 e
= op_overload(sc
);
7750 Type
*tb1
= e1
->type
->toBasetype();
7751 Type
*tb2
= e2
->type
->toBasetype();
7754 /* BUG: Should handle things like:
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
);
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
);
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();
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
))
7810 error("Can only concatenate arrays, not (%s ~ %s)",
7811 e1
->type
->toChars(), e2
->type
->toChars());
7812 type
= Type::tint32
;
7815 e
->type
= e
->type
->semantic(loc
, sc
);
7821 /************************************************************/
7823 MulExp::MulExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7824 : BinExp(loc
, TOKmul
, sizeof(MulExp
), e1
, e2
)
7828 Expression
*MulExp::semantic(Scope
*sc
)
7832 printf("MulExp::semantic() %s\n", toChars());
7839 BinExp::semanticp(sc
);
7840 e
= op_overload(sc
);
7845 e1
->checkArithmetic();
7846 e2
->checkArithmetic();
7847 if (type
->isfloating())
7848 { Type
*t1
= e1
->type
;
7849 Type
*t2
= e2
->type
;
7855 else if (t2
->isreal())
7859 else if (t1
->isimaginary())
7861 if (t2
->isimaginary())
7866 case Timaginary32
: type
= Type::tfloat32
; break;
7867 case Timaginary64
: type
= Type::tfloat64
; break;
7868 case Timaginary80
: type
= Type::tfloat80
; break;
7875 e
= new NegExp(loc
, this);
7876 e
= e
->semantic(sc
);
7880 type
= t2
; // t2 is complex
7882 else if (t2
->isimaginary())
7884 type
= t1
; // t1 is complex
7890 /************************************************************/
7892 DivExp::DivExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7893 : BinExp(loc
, TOKdiv
, sizeof(DivExp
), e1
, e2
)
7897 Expression
*DivExp::semantic(Scope
*sc
)
7903 BinExp::semanticp(sc
);
7904 e
= op_overload(sc
);
7909 e1
->checkArithmetic();
7910 e2
->checkArithmetic();
7911 if (type
->isfloating())
7912 { Type
*t1
= e1
->type
;
7913 Type
*t2
= e2
->type
;
7918 if (t2
->isimaginary())
7923 e
= new NegExp(loc
, this);
7924 e
= e
->semantic(sc
);
7928 else if (t2
->isreal())
7932 else if (t1
->isimaginary())
7934 if (t2
->isimaginary())
7938 case Timaginary32
: type
= Type::tfloat32
; break;
7939 case Timaginary64
: type
= Type::tfloat64
; break;
7940 case Timaginary80
: type
= Type::tfloat80
; break;
7945 type
= t2
; // t2 is complex
7947 else if (t2
->isimaginary())
7949 type
= t1
; // t1 is complex
7955 /************************************************************/
7957 ModExp::ModExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7958 : BinExp(loc
, TOKmod
, sizeof(ModExp
), e1
, e2
)
7962 Expression
*ModExp::semantic(Scope
*sc
)
7968 BinExp::semanticp(sc
);
7969 e
= op_overload(sc
);
7974 e1
->checkArithmetic();
7975 e2
->checkArithmetic();
7976 if (type
->isfloating())
7978 if (e2
->type
->iscomplex())
7979 { error("cannot perform modulo complex arithmetic");
7980 return new IntegerExp(0);
7986 /************************************************************/
7988 ShlExp::ShlExp(Loc loc
, Expression
*e1
, Expression
*e2
)
7989 : BinExp(loc
, TOKshl
, sizeof(ShlExp
), e1
, e2
)
7993 Expression
*ShlExp::semantic(Scope
*sc
)
7996 //printf("ShlExp::semantic(), type = %p\n", type);
7998 { BinExp::semanticp(sc
);
7999 e
= op_overload(sc
);
8002 e1
= e1
->checkIntegral();
8003 e2
= e2
->checkIntegral();
8004 e1
= e1
->integralPromotions(sc
);
8005 e2
= e2
->castTo(sc
, Type::tshiftcnt
);
8011 /************************************************************/
8013 ShrExp::ShrExp(Loc loc
, Expression
*e1
, Expression
*e2
)
8014 : BinExp(loc
, TOKshr
, sizeof(ShrExp
), e1
, e2
)
8018 Expression
*ShrExp::semantic(Scope
*sc
)
8022 { BinExp::semanticp(sc
);
8023 e
= op_overload(sc
);
8026 e1
= e1
->checkIntegral();
8027 e2
= e2
->checkIntegral();
8028 e1
= e1
->integralPromotions(sc
);
8029 e2
= e2
->castTo(sc
, Type::tshiftcnt
);
8035 /************************************************************/
8037 UshrExp::UshrExp(Loc loc
, Expression
*e1
, Expression
*e2
)
8038 : BinExp(loc
, TOKushr
, sizeof(UshrExp
), e1
, e2
)
8042 Expression
*UshrExp::semantic(Scope
*sc
)
8046 { BinExp::semanticp(sc
);
8047 e
= op_overload(sc
);
8050 e1
= e1
->checkIntegral();
8051 e2
= e2
->checkIntegral();
8052 e1
= e1
->integralPromotions(sc
);
8053 e2
= e2
->castTo(sc
, Type::tshiftcnt
);
8059 /************************************************************/
8061 AndExp::AndExp(Loc loc
, Expression
*e1
, Expression
*e2
)
8062 : BinExp(loc
, TOKand
, sizeof(AndExp
), e1
, e2
)
8066 Expression
*AndExp::semantic(Scope
*sc
)
8070 { BinExp::semanticp(sc
);
8071 e
= op_overload(sc
);
8074 if (e1
->type
->toBasetype()->ty
== Tbool
&&
8075 e2
->type
->toBasetype()->ty
== Tbool
)
8083 e1
->checkIntegral();
8084 e2
->checkIntegral();
8090 /************************************************************/
8092 OrExp::OrExp(Loc loc
, Expression
*e1
, Expression
*e2
)
8093 : BinExp(loc
, TOKor
, sizeof(OrExp
), e1
, e2
)
8097 Expression
*OrExp::semantic(Scope
*sc
)
8101 { BinExp::semanticp(sc
);
8102 e
= op_overload(sc
);
8105 if (e1
->type
->toBasetype()->ty
== Tbool
&&
8106 e2
->type
->toBasetype()->ty
== Tbool
)
8114 e1
->checkIntegral();
8115 e2
->checkIntegral();
8121 /************************************************************/
8123 XorExp::XorExp(Loc loc
, Expression
*e1
, Expression
*e2
)
8124 : BinExp(loc
, TOKxor
, sizeof(XorExp
), e1
, e2
)
8128 Expression
*XorExp::semantic(Scope
*sc
)
8132 { BinExp::semanticp(sc
);
8133 e
= op_overload(sc
);
8136 if (e1
->type
->toBasetype()->ty
== Tbool
&&
8137 e2
->type
->toBasetype()->ty
== Tbool
)
8145 e1
->checkIntegral();
8146 e2
->checkIntegral();
8153 /************************************************************/
8155 OrOrExp::OrOrExp(Loc loc
, Expression
*e1
, Expression
*e2
)
8156 : BinExp(loc
, TOKoror
, sizeof(OrOrExp
), e1
, e2
)
8160 Expression
*OrOrExp::semantic(Scope
*sc
)
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
)
8190 if (e2
->op
== TOKtype
|| e2
->op
== TOKimport
)
8191 error("%s is not an expression", e2
->toChars());
8195 Expression
*OrOrExp::checkToBoolean()
8197 e2
= e2
->checkToBoolean();
8201 int OrOrExp::isBit()
8206 int OrOrExp::checkSideEffect(int flag
)
8210 return e1
->checkSideEffect(2) || e2
->checkSideEffect(2);
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
)
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
)
8255 if (e2
->op
== TOKtype
|| e2
->op
== TOKimport
)
8256 error("%s is not an expression", e2
->toChars());
8260 Expression
*AndAndExp::checkToBoolean()
8262 e2
= e2
->checkToBoolean();
8266 int AndAndExp::isBit()
8271 int AndAndExp::checkSideEffect(int flag
)
8275 return e1
->checkSideEffect(2) || e2
->checkSideEffect(2);
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
)
8297 BinExp::semanticp(sc
);
8298 e
= op_overload(sc
);
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
;
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();
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
)
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
)
8352 printf("CmpExp::semantic('%s')\n", toChars());
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
);
8368 e
= new CmpExp(op
, loc
, e
, new IntegerExp(loc
, 0, Type::tint32
));
8369 e
= e
->semantic(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());
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());
8392 error("need member function opCmp() for %s %s to compare", t1
->toDsymbol(sc
)->kind(), t1
->toChars());
8396 else if (t1
->iscomplex() || t2
->iscomplex())
8398 error("compare not defined for complex operands");
8399 e
= new IntegerExp(0);
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
)
8426 //printf("EqualExp::semantic('%s')\n", toChars());
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
);
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
);
8466 if (op
== TOKnotequal
)
8468 e
= new NotExp(e
->loc
, e
);
8469 e
= e
->semantic(sc
);
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());
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
);
8499 int EqualExp::isBit()
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
)
8518 BinExp::semanticp(sc
);
8519 type
= Type::tboolean
;
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
);
8530 int IdentityExp::isBit()
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
)
8557 printf("CondExp::semantic('%s')\n", toChars());
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
);
8581 else if (econd
->isBool(FALSE
))
8583 e2
= e2
->semantic(sc
);
8584 e2
= resolveProperties(sc
, e2
);
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
8604 if (t1
->ty
== Tvoid
|| t2
->ty
== Tvoid
)
8611 switch (e1
->type
->toBasetype()->ty
)
8616 e2
= e2
->castTo(sc
, e1
->type
);
8619 switch (e2
->type
->toBasetype()->ty
)
8624 e1
= e1
->castTo(sc
, e2
->type
);
8631 Expression
*CondExp::toLvalue(Scope
*sc
, Expression
*ex
)
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);
8650 Expression
*CondExp::modifiableLvalue(Scope
*sc
, Expression
*e
)
8652 error("conditional expression %s is not a modifiable lvalue", toChars());
8656 void CondExp::checkEscape()
8663 Expression
*CondExp::checkToBoolean()
8665 e1
= e1
->checkToBoolean();
8666 e2
= e2
->checkToBoolean();
8670 int CondExp::checkSideEffect(int flag
)
8674 return econd
->checkSideEffect(2) ||
8675 e1
->checkSideEffect(2) ||
8676 e2
->checkSideEffect(2);
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
);