blob: 0aee702c65d596dce239212f15a884b3632bace1 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/StmtVisitor.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000018#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// Primary Expressions.
23//===----------------------------------------------------------------------===//
24
25StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
26 bool Wide, QualType t, SourceLocation firstLoc,
27 SourceLocation lastLoc) :
28 Expr(StringLiteralClass, t) {
29 // OPTIMIZE: could allocate this appended to the StringLiteral.
30 char *AStrData = new char[byteLength];
31 memcpy(AStrData, strData, byteLength);
32 StrData = AStrData;
33 ByteLength = byteLength;
34 IsWide = Wide;
35 firstTokLoc = firstLoc;
36 lastTokLoc = lastLoc;
37}
38
39StringLiteral::~StringLiteral() {
40 delete[] StrData;
41}
42
43bool UnaryOperator::isPostfix(Opcode Op) {
44 switch (Op) {
45 case PostInc:
46 case PostDec:
47 return true;
48 default:
49 return false;
50 }
51}
52
53/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
54/// corresponds to, e.g. "sizeof" or "[pre]++".
55const char *UnaryOperator::getOpcodeStr(Opcode Op) {
56 switch (Op) {
57 default: assert(0 && "Unknown unary operator");
58 case PostInc: return "++";
59 case PostDec: return "--";
60 case PreInc: return "++";
61 case PreDec: return "--";
62 case AddrOf: return "&";
63 case Deref: return "*";
64 case Plus: return "+";
65 case Minus: return "-";
66 case Not: return "~";
67 case LNot: return "!";
68 case Real: return "__real";
69 case Imag: return "__imag";
70 case SizeOf: return "sizeof";
71 case AlignOf: return "alignof";
72 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000073 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000074 }
75}
76
77//===----------------------------------------------------------------------===//
78// Postfix Operators.
79//===----------------------------------------------------------------------===//
80
81CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
82 SourceLocation rparenloc)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000083 : Expr(CallExprClass, t), NumArgs(numargs) {
84 SubExprs = new Expr*[numargs+1];
85 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +000086 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000087 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +000088 RParenLoc = rparenloc;
89}
90
Steve Naroff8d3b1702007-08-08 22:15:55 +000091bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
92 // The following enum mimics gcc's internal "typeclass.h" file.
93 enum gcc_type_class {
94 no_type_class = -1,
95 void_type_class, integer_type_class, char_type_class,
96 enumeral_type_class, boolean_type_class,
97 pointer_type_class, reference_type_class, offset_type_class,
98 real_type_class, complex_type_class,
99 function_type_class, method_type_class,
100 record_type_class, union_type_class,
101 array_type_class, string_type_class,
102 lang_type_class
103 };
104 Result.setIsSigned(true);
105
106 // All simple function calls (e.g. func()) are implicitly cast to pointer to
107 // function. As a result, we try and obtain the DeclRefExpr from the
108 // ImplicitCastExpr.
109 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
110 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
111 return false;
112 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
113 if (!DRE)
114 return false;
115
116 // We have a DeclRefExpr.
117 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
118 // If no argument was supplied, default to "no_type_class". This isn't
119 // ideal, however it's what gcc does.
120 Result = static_cast<uint64_t>(no_type_class);
121 if (NumArgs >= 1) {
122 QualType argType = getArg(0)->getType();
123
124 if (argType->isVoidType())
125 Result = void_type_class;
126 else if (argType->isEnumeralType())
127 Result = enumeral_type_class;
128 else if (argType->isBooleanType())
129 Result = boolean_type_class;
130 else if (argType->isCharType())
131 Result = string_type_class; // gcc doesn't appear to use char_type_class
132 else if (argType->isIntegerType())
133 Result = integer_type_class;
134 else if (argType->isPointerType())
135 Result = pointer_type_class;
136 else if (argType->isReferenceType())
137 Result = reference_type_class;
138 else if (argType->isRealType())
139 Result = real_type_class;
140 else if (argType->isComplexType())
141 Result = complex_type_class;
142 else if (argType->isFunctionType())
143 Result = function_type_class;
144 else if (argType->isStructureType())
145 Result = record_type_class;
146 else if (argType->isUnionType())
147 Result = union_type_class;
148 else if (argType->isArrayType())
149 Result = array_type_class;
150 else if (argType->isUnionType())
151 Result = union_type_class;
152 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner19b8f1a2007-11-08 17:56:40 +0000153 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff8d3b1702007-08-08 22:15:55 +0000154 }
155 return true;
156 }
157 return false;
158}
159
Chris Lattner4b009652007-07-25 00:24:17 +0000160/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
161/// corresponds to, e.g. "<<=".
162const char *BinaryOperator::getOpcodeStr(Opcode Op) {
163 switch (Op) {
164 default: assert(0 && "Unknown binary operator");
165 case Mul: return "*";
166 case Div: return "/";
167 case Rem: return "%";
168 case Add: return "+";
169 case Sub: return "-";
170 case Shl: return "<<";
171 case Shr: return ">>";
172 case LT: return "<";
173 case GT: return ">";
174 case LE: return "<=";
175 case GE: return ">=";
176 case EQ: return "==";
177 case NE: return "!=";
178 case And: return "&";
179 case Xor: return "^";
180 case Or: return "|";
181 case LAnd: return "&&";
182 case LOr: return "||";
183 case Assign: return "=";
184 case MulAssign: return "*=";
185 case DivAssign: return "/=";
186 case RemAssign: return "%=";
187 case AddAssign: return "+=";
188 case SubAssign: return "-=";
189 case ShlAssign: return "<<=";
190 case ShrAssign: return ">>=";
191 case AndAssign: return "&=";
192 case XorAssign: return "^=";
193 case OrAssign: return "|=";
194 case Comma: return ",";
195 }
196}
197
Anders Carlsson762b7c72007-08-31 04:56:16 +0000198InitListExpr::InitListExpr(SourceLocation lbraceloc,
199 Expr **initexprs, unsigned numinits,
200 SourceLocation rbraceloc)
201 : Expr(InitListExprClass, QualType())
202 , NumInits(numinits)
203 , LBraceLoc(lbraceloc)
204 , RBraceLoc(rbraceloc)
205{
206 InitExprs = new Expr*[numinits];
207 for (unsigned i = 0; i != numinits; i++)
208 InitExprs[i] = initexprs[i];
209}
Chris Lattner4b009652007-07-25 00:24:17 +0000210
211//===----------------------------------------------------------------------===//
212// Generic Expression Routines
213//===----------------------------------------------------------------------===//
214
215/// hasLocalSideEffect - Return true if this immediate expression has side
216/// effects, not counting any sub-expressions.
217bool Expr::hasLocalSideEffect() const {
218 switch (getStmtClass()) {
219 default:
220 return false;
221 case ParenExprClass:
222 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
223 case UnaryOperatorClass: {
224 const UnaryOperator *UO = cast<UnaryOperator>(this);
225
226 switch (UO->getOpcode()) {
227 default: return false;
228 case UnaryOperator::PostInc:
229 case UnaryOperator::PostDec:
230 case UnaryOperator::PreInc:
231 case UnaryOperator::PreDec:
232 return true; // ++/--
233
234 case UnaryOperator::Deref:
235 // Dereferencing a volatile pointer is a side-effect.
236 return getType().isVolatileQualified();
237 case UnaryOperator::Real:
238 case UnaryOperator::Imag:
239 // accessing a piece of a volatile complex is a side-effect.
240 return UO->getSubExpr()->getType().isVolatileQualified();
241
242 case UnaryOperator::Extension:
243 return UO->getSubExpr()->hasLocalSideEffect();
244 }
245 }
246 case BinaryOperatorClass:
247 return cast<BinaryOperator>(this)->isAssignmentOp();
Chris Lattner06078d22007-08-25 02:00:02 +0000248 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000249 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000250
251 case MemberExprClass:
252 case ArraySubscriptExprClass:
253 // If the base pointer or element is to a volatile pointer/field, accessing
254 // if is a side effect.
255 return getType().isVolatileQualified();
256
257 case CallExprClass:
258 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
259 // should warn.
260 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000261 case ObjCMessageExprClass:
262 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000263
264 case CastExprClass:
265 // If this is a cast to void, check the operand. Otherwise, the result of
266 // the cast is unused.
267 if (getType()->isVoidType())
268 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
269 return false;
270 }
271}
272
273/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
274/// incomplete type other than void. Nonarray expressions that can be lvalues:
275/// - name, where name must be a variable
276/// - e[i]
277/// - (e), where e must be an lvalue
278/// - e.name, where e must be an lvalue
279/// - e->name
280/// - *e, the type of e cannot be a function type
281/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000282/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000283/// - reference type [C++ [expr]]
284///
285Expr::isLvalueResult Expr::isLvalue() const {
286 // first, check the type (C99 6.3.2.1)
287 if (TR->isFunctionType()) // from isObjectType()
288 return LV_NotObjectType;
289
290 if (TR->isVoidType())
291 return LV_IncompleteVoidType;
292
293 if (TR->isReferenceType()) // C++ [expr]
294 return LV_Valid;
295
296 // the type looks fine, now check the expression
297 switch (getStmtClass()) {
298 case StringLiteralClass: // C99 6.5.1p4
299 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
300 // For vectors, make sure base is an lvalue (i.e. not a function call).
301 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
302 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
303 return LV_Valid;
304 case DeclRefExprClass: // C99 6.5.1p2
305 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
306 return LV_Valid;
307 break;
308 case MemberExprClass: { // C99 6.5.2.3p4
309 const MemberExpr *m = cast<MemberExpr>(this);
310 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
311 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000312 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000313 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000314 return LV_Valid; // C99 6.5.3p4
315
316 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
317 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
318 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000319 break;
320 case ParenExprClass: // C99 6.5.1p5
321 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000322 case OCUVectorElementExprClass:
323 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000324 return LV_DuplicateVectorComponents;
325 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000326 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
327 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000328 default:
329 break;
330 }
331 return LV_InvalidExpression;
332}
333
334/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
335/// does not have an incomplete type, does not have a const-qualified type, and
336/// if it is a structure or union, does not have any member (including,
337/// recursively, any member or element of all contained aggregates or unions)
338/// with a const-qualified type.
339Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
340 isLvalueResult lvalResult = isLvalue();
341
342 switch (lvalResult) {
343 case LV_Valid: break;
344 case LV_NotObjectType: return MLV_NotObjectType;
345 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000346 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000347 case LV_InvalidExpression: return MLV_InvalidExpression;
348 }
349 if (TR.isConstQualified())
350 return MLV_ConstQualified;
351 if (TR->isArrayType())
352 return MLV_ArrayType;
353 if (TR->isIncompleteType())
354 return MLV_IncompleteType;
355
356 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
357 if (r->hasConstFields())
358 return MLV_ConstQualified;
359 }
360 return MLV_Valid;
361}
362
Chris Lattner743ec372007-11-27 21:35:27 +0000363/// hasStaticStorage - Return true if this expression has static storage
364/// duration. This means that the address of this expression is a link-time
365/// constant.
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000366bool Expr::hasStaticStorage() const {
367 switch (getStmtClass()) {
368 default:
369 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000370 case ParenExprClass:
371 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
372 case ImplicitCastExprClass:
373 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000374 case DeclRefExprClass: {
375 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
376 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
377 return VD->hasStaticStorage();
378 return false;
379 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000380 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000381 const MemberExpr *M = cast<MemberExpr>(this);
382 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000383 }
Chris Lattner743ec372007-11-27 21:35:27 +0000384 case ArraySubscriptExprClass:
385 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000386 }
387}
388
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000389bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000390 switch (getStmtClass()) {
391 default:
392 if (Loc) *Loc = getLocStart();
393 return false;
394 case ParenExprClass:
395 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
396 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000397 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000398 case FloatingLiteralClass:
399 case IntegerLiteralClass:
400 case CharacterLiteralClass:
401 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000402 case TypesCompatibleExprClass:
403 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000404 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000405 case CallExprClass: {
406 const CallExpr *CE = cast<CallExpr>(this);
407 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000408 Result.zextOrTrunc(
409 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000410 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000411 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000412 if (Loc) *Loc = getLocStart();
413 return false;
414 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000415 case DeclRefExprClass: {
416 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
417 // Accept address of function.
418 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000419 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000420 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000421 if (isa<VarDecl>(D))
422 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000423 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000424 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000425 case UnaryOperatorClass: {
426 const UnaryOperator *Exp = cast<UnaryOperator>(this);
427
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000428 // C99 6.6p9
429 if (Exp->getOpcode() == UnaryOperator::AddrOf)
430 return Exp->getSubExpr()->hasStaticStorage();
431
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000432 // Get the operand value. If this is sizeof/alignof, do not evalute the
433 // operand. This affects C99 6.6p3.
434 if (!Exp->isSizeOfAlignOfOp() &&
435 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
436 return false;
437
438 switch (Exp->getOpcode()) {
439 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
440 // See C99 6.6p3.
441 default:
442 if (Loc) *Loc = Exp->getOperatorLoc();
443 return false;
444 case UnaryOperator::Extension:
445 return true; // FIXME: this is wrong.
446 case UnaryOperator::SizeOf:
447 case UnaryOperator::AlignOf:
448 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
449 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
450 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000451 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000452 case UnaryOperator::LNot:
453 case UnaryOperator::Plus:
454 case UnaryOperator::Minus:
455 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000456 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000457 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000458 }
459 case SizeOfAlignOfTypeExprClass: {
460 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
461 // alignof always evaluates to a constant.
462 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
463 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000464 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000465 }
466 case BinaryOperatorClass: {
467 const BinaryOperator *Exp = cast<BinaryOperator>(this);
468
469 // The LHS of a constant expr is always evaluated and needed.
470 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
471 return false;
472
473 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
474 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000475 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000476 }
477 case ImplicitCastExprClass:
478 case CastExprClass: {
479 const Expr *SubExpr;
480 SourceLocation CastLoc;
481 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
482 SubExpr = C->getSubExpr();
483 CastLoc = C->getLParenLoc();
484 } else {
485 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
486 CastLoc = getLocStart();
487 }
488 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
489 if (Loc) *Loc = SubExpr->getLocStart();
490 return false;
491 }
Chris Lattner06db6132007-10-18 00:20:32 +0000492 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000493 }
494 case ConditionalOperatorClass: {
495 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000496 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000497 // Handle the GNU extension for missing LHS.
498 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000499 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000500 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000501 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000502 }
503 }
504
505 return true;
506}
507
Chris Lattner4b009652007-07-25 00:24:17 +0000508/// isIntegerConstantExpr - this recursive routine will test if an expression is
509/// an integer constant expression. Note: With the introduction of VLA's in
510/// C99 the result of the sizeof operator is no longer always a constant
511/// expression. The generalization of the wording to include any subexpression
512/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
513/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
514/// "0 || f()" can be treated as a constant expression. In C90 this expression,
515/// occurring in a context requiring a constant, would have been a constraint
516/// violation. FIXME: This routine currently implements C90 semantics.
517/// To properly implement C99 semantics this routine will need to evaluate
518/// expressions involving operators previously mentioned.
519
520/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
521/// comma, etc
522///
523/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000524/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000525///
526/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
527/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
528/// cast+dereference.
529bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
530 SourceLocation *Loc, bool isEvaluated) const {
531 switch (getStmtClass()) {
532 default:
533 if (Loc) *Loc = getLocStart();
534 return false;
535 case ParenExprClass:
536 return cast<ParenExpr>(this)->getSubExpr()->
537 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
538 case IntegerLiteralClass:
539 Result = cast<IntegerLiteral>(this)->getValue();
540 break;
541 case CharacterLiteralClass: {
542 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000543 Result.zextOrTrunc(
544 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000545 Result = CL->getValue();
546 Result.setIsUnsigned(!getType()->isSignedIntegerType());
547 break;
548 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000549 case TypesCompatibleExprClass: {
550 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000551 Result.zextOrTrunc(
552 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000553 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000554 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000555 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000556 case CallExprClass: {
557 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000558 Result.zextOrTrunc(
559 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000560 if (CE->isBuiltinClassifyType(Result))
561 break;
562 if (Loc) *Loc = getLocStart();
563 return false;
564 }
Chris Lattner4b009652007-07-25 00:24:17 +0000565 case DeclRefExprClass:
566 if (const EnumConstantDecl *D =
567 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
568 Result = D->getInitVal();
569 break;
570 }
571 if (Loc) *Loc = getLocStart();
572 return false;
573 case UnaryOperatorClass: {
574 const UnaryOperator *Exp = cast<UnaryOperator>(this);
575
576 // Get the operand value. If this is sizeof/alignof, do not evalute the
577 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000578 if (!Exp->isSizeOfAlignOfOp() &&
579 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000580 return false;
581
582 switch (Exp->getOpcode()) {
583 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
584 // See C99 6.6p3.
585 default:
586 if (Loc) *Loc = Exp->getOperatorLoc();
587 return false;
588 case UnaryOperator::Extension:
589 return true; // FIXME: this is wrong.
590 case UnaryOperator::SizeOf:
591 case UnaryOperator::AlignOf:
592 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
593 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
594 return false;
595
596 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000597 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000598 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
599 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000600
601 // Get information about the size or align.
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000602 if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner4b009652007-07-25 00:24:17 +0000603 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
604 Exp->getOperatorLoc());
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000605 } else {
606 unsigned CharSize = Ctx.Target.getCharWidth(Exp->getOperatorLoc());
607 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
608 Exp->getOperatorLoc()) / CharSize;
609 }
Chris Lattner4b009652007-07-25 00:24:17 +0000610 break;
611 case UnaryOperator::LNot: {
612 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000613 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000614 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
615 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000616 Result = Val;
617 break;
618 }
619 case UnaryOperator::Plus:
620 break;
621 case UnaryOperator::Minus:
622 Result = -Result;
623 break;
624 case UnaryOperator::Not:
625 Result = ~Result;
626 break;
627 }
628 break;
629 }
630 case SizeOfAlignOfTypeExprClass: {
631 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
632 // alignof always evaluates to a constant.
633 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
634 return false;
635
636 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000637 Result.zextOrTrunc(
638 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000639
640 // Get information about the size or align.
641 if (Exp->isSizeOf())
642 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
643 else
644 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
645 break;
646 }
647 case BinaryOperatorClass: {
648 const BinaryOperator *Exp = cast<BinaryOperator>(this);
649
650 // The LHS of a constant expr is always evaluated and needed.
651 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
652 return false;
653
654 llvm::APSInt RHS(Result);
655
656 // The short-circuiting &&/|| operators don't necessarily evaluate their
657 // RHS. Make sure to pass isEvaluated down correctly.
658 if (Exp->isLogicalOp()) {
659 bool RHSEval;
660 if (Exp->getOpcode() == BinaryOperator::LAnd)
661 RHSEval = Result != 0;
662 else {
663 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
664 RHSEval = Result == 0;
665 }
666
667 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
668 isEvaluated & RHSEval))
669 return false;
670 } else {
671 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
672 return false;
673 }
674
675 switch (Exp->getOpcode()) {
676 default:
677 if (Loc) *Loc = getLocStart();
678 return false;
679 case BinaryOperator::Mul:
680 Result *= RHS;
681 break;
682 case BinaryOperator::Div:
683 if (RHS == 0) {
684 if (!isEvaluated) break;
685 if (Loc) *Loc = getLocStart();
686 return false;
687 }
688 Result /= RHS;
689 break;
690 case BinaryOperator::Rem:
691 if (RHS == 0) {
692 if (!isEvaluated) break;
693 if (Loc) *Loc = getLocStart();
694 return false;
695 }
696 Result %= RHS;
697 break;
698 case BinaryOperator::Add: Result += RHS; break;
699 case BinaryOperator::Sub: Result -= RHS; break;
700 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000701 Result <<=
702 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000703 break;
704 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000705 Result >>=
706 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000707 break;
708 case BinaryOperator::LT: Result = Result < RHS; break;
709 case BinaryOperator::GT: Result = Result > RHS; break;
710 case BinaryOperator::LE: Result = Result <= RHS; break;
711 case BinaryOperator::GE: Result = Result >= RHS; break;
712 case BinaryOperator::EQ: Result = Result == RHS; break;
713 case BinaryOperator::NE: Result = Result != RHS; break;
714 case BinaryOperator::And: Result &= RHS; break;
715 case BinaryOperator::Xor: Result ^= RHS; break;
716 case BinaryOperator::Or: Result |= RHS; break;
717 case BinaryOperator::LAnd:
718 Result = Result != 0 && RHS != 0;
719 break;
720 case BinaryOperator::LOr:
721 Result = Result != 0 || RHS != 0;
722 break;
723
724 case BinaryOperator::Comma:
725 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
726 // *except* when they are contained within a subexpression that is not
727 // evaluated". Note that Assignment can never happen due to constraints
728 // on the LHS subexpr, so we don't need to check it here.
729 if (isEvaluated) {
730 if (Loc) *Loc = getLocStart();
731 return false;
732 }
733
734 // The result of the constant expr is the RHS.
735 Result = RHS;
736 return true;
737 }
738
739 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
740 break;
741 }
742 case ImplicitCastExprClass:
743 case CastExprClass: {
744 const Expr *SubExpr;
745 SourceLocation CastLoc;
746 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
747 SubExpr = C->getSubExpr();
748 CastLoc = C->getLParenLoc();
749 } else {
750 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
751 CastLoc = getLocStart();
752 }
753
754 // C99 6.6p6: shall only convert arithmetic types to integer types.
755 if (!SubExpr->getType()->isArithmeticType() ||
756 !getType()->isIntegerType()) {
757 if (Loc) *Loc = SubExpr->getLocStart();
758 return false;
759 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000760
761 uint32_t DestWidth =
762 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
763
Chris Lattner4b009652007-07-25 00:24:17 +0000764 // Handle simple integer->integer casts.
765 if (SubExpr->getType()->isIntegerType()) {
766 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
767 return false;
768
769 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000770 // If the input is signed, do a sign extend, noop, or truncate.
771 if (SubExpr->getType()->isSignedIntegerType())
772 Result.sextOrTrunc(DestWidth);
773 else // If the input is unsigned, do a zero extend, noop, or truncate.
774 Result.zextOrTrunc(DestWidth);
775 break;
776 }
777
778 // Allow floating constants that are the immediate operands of casts or that
779 // are parenthesized.
780 const Expr *Operand = SubExpr;
781 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
782 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000783
784 // If this isn't a floating literal, we can't handle it.
785 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
786 if (!FL) {
787 if (Loc) *Loc = Operand->getLocStart();
788 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000789 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000790
791 // Determine whether we are converting to unsigned or signed.
792 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000793
794 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
795 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000796 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000797 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
798 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000799 Result = llvm::APInt(DestWidth, 4, Space);
800 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000801 }
802 case ConditionalOperatorClass: {
803 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
804
805 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
806 return false;
807
808 const Expr *TrueExp = Exp->getLHS();
809 const Expr *FalseExp = Exp->getRHS();
810 if (Result == 0) std::swap(TrueExp, FalseExp);
811
812 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000813 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000814 return false;
815 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000816 if (TrueExp &&
817 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000818 return false;
819 break;
820 }
821 }
822
823 // Cases that are valid constant exprs fall through to here.
824 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
825 return true;
826}
827
828
829/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
830/// integer constant expression with the value zero, or if this is one that is
831/// cast to void*.
832bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
833 // Strip off a cast to void*, if it exists.
834 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
835 // Check that it is a cast to void*.
836 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
837 QualType Pointee = PT->getPointeeType();
838 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
839 CE->getSubExpr()->getType()->isIntegerType()) // from int.
840 return CE->getSubExpr()->isNullPointerConstant(Ctx);
841 }
Steve Naroff1d6b2472007-08-28 21:20:34 +0000842 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff3d052872007-08-29 00:00:02 +0000843 // Ignore the ImplicitCastExpr type entirely.
844 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000845 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
846 // Accept ((void*)0) as a null pointer constant, as many other
847 // implementations do.
848 return PE->getSubExpr()->isNullPointerConstant(Ctx);
849 }
850
851 // This expression must be an integer type.
852 if (!getType()->isIntegerType())
853 return false;
854
855 // If we have an integer constant expression, we need to *evaluate* it and
856 // test for the value 0.
857 llvm::APSInt Val(32);
858 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
859}
Steve Naroffc11705f2007-07-28 23:10:27 +0000860
Chris Lattnera0d03a72007-08-03 17:31:20 +0000861unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000862 return strlen(Accessor.getName());
863}
864
865
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000866/// getComponentType - Determine whether the components of this access are
867/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000868OCUVectorElementExpr::ElementType
869OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000870 // derive the component type, no need to waste space.
871 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000872
Chris Lattner9096b792007-08-02 22:33:49 +0000873 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
874 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000875
Chris Lattner9096b792007-08-02 22:33:49 +0000876 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000877 "getComponentType(): Illegal accessor");
878 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000879}
Steve Naroffba67f692007-07-30 03:29:09 +0000880
Chris Lattnera0d03a72007-08-03 17:31:20 +0000881/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000882/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000883bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000884 const char *compStr = Accessor.getName();
885 unsigned length = strlen(compStr);
886
887 for (unsigned i = 0; i < length-1; i++) {
888 const char *s = compStr+i;
889 for (const char c = *s++; *s; s++)
890 if (c == *s)
891 return true;
892 }
893 return false;
894}
Chris Lattner42158e72007-08-02 23:36:59 +0000895
896/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000897unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000898 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000899 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +0000900
901 unsigned Result = 0;
902
903 while (length--) {
904 Result <<= 2;
905 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
906 assert(Idx != -1 && "Invalid accessor letter");
907 Result |= Idx;
908 }
909 return Result;
910}
911
Steve Naroff4ed9d662007-09-27 14:38:14 +0000912// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +0000913ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000914 QualType retType, ObjcMethodDecl *mproto,
915 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +0000916 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000917 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
918 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +0000919 NumArgs = nargs;
920 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +0000921 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +0000922 if (NumArgs) {
923 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000924 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
925 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000926 LBracloc = LBrac;
927 RBracloc = RBrac;
928}
929
Steve Naroff4ed9d662007-09-27 14:38:14 +0000930// constructor for class messages.
931// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +0000932ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000933 QualType retType, ObjcMethodDecl *mproto,
934 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +0000935 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000936 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
937 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +0000938 NumArgs = nargs;
939 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +0000940 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +0000941 if (NumArgs) {
942 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000943 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
944 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000945 LBracloc = LBrac;
946 RBracloc = RBrac;
947}
948
Chris Lattnerf624cd22007-10-25 00:29:32 +0000949
950bool ChooseExpr::isConditionTrue(ASTContext &C) const {
951 llvm::APSInt CondVal(32);
952 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
953 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
954 return CondVal != 0;
955}
956
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000957//===----------------------------------------------------------------------===//
958// Child Iterators for iterating over subexpressions/substatements
959//===----------------------------------------------------------------------===//
960
961// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000962Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
963Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000964
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000965// ObjCIvarRefExpr
966Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
967Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
968
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000969// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000970Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
971Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000972
973// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000974Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
975Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000976
977// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000978Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
979Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000980
981// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000982Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
983Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000984
Chris Lattner1de66eb2007-08-26 03:42:43 +0000985// ImaginaryLiteral
986Stmt::child_iterator ImaginaryLiteral::child_begin() {
987 return reinterpret_cast<Stmt**>(&Val);
988}
989Stmt::child_iterator ImaginaryLiteral::child_end() {
990 return reinterpret_cast<Stmt**>(&Val)+1;
991}
992
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000993// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000994Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
995Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000996
997// ParenExpr
998Stmt::child_iterator ParenExpr::child_begin() {
999 return reinterpret_cast<Stmt**>(&Val);
1000}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001001Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001002 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001003}
1004
1005// UnaryOperator
1006Stmt::child_iterator UnaryOperator::child_begin() {
1007 return reinterpret_cast<Stmt**>(&Val);
1008}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001009Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001010 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001011}
1012
1013// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001014Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
1015 return child_iterator();
1016}
1017Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
1018 return child_iterator();
1019}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001020
1021// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001022Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001023 return reinterpret_cast<Stmt**>(&SubExprs);
1024}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001025Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001026 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001027}
1028
1029// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001030Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001031 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001032}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001033Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001034 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001035}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001036
1037// MemberExpr
1038Stmt::child_iterator MemberExpr::child_begin() {
1039 return reinterpret_cast<Stmt**>(&Base);
1040}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001041Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001042 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001043}
1044
1045// OCUVectorElementExpr
1046Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1047 return reinterpret_cast<Stmt**>(&Base);
1048}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001049Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001050 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001051}
1052
1053// CompoundLiteralExpr
1054Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1055 return reinterpret_cast<Stmt**>(&Init);
1056}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001057Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001058 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001059}
1060
1061// ImplicitCastExpr
1062Stmt::child_iterator ImplicitCastExpr::child_begin() {
1063 return reinterpret_cast<Stmt**>(&Op);
1064}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001065Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001066 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001067}
1068
1069// CastExpr
1070Stmt::child_iterator CastExpr::child_begin() {
1071 return reinterpret_cast<Stmt**>(&Op);
1072}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001073Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001074 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001075}
1076
1077// BinaryOperator
1078Stmt::child_iterator BinaryOperator::child_begin() {
1079 return reinterpret_cast<Stmt**>(&SubExprs);
1080}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001081Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001082 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001083}
1084
1085// ConditionalOperator
1086Stmt::child_iterator ConditionalOperator::child_begin() {
1087 return reinterpret_cast<Stmt**>(&SubExprs);
1088}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001089Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001090 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001091}
1092
1093// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001094Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1095Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001096
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001097// StmtExpr
1098Stmt::child_iterator StmtExpr::child_begin() {
1099 return reinterpret_cast<Stmt**>(&SubStmt);
1100}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001101Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001102 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001103}
1104
1105// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001106Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1107 return child_iterator();
1108}
1109
1110Stmt::child_iterator TypesCompatibleExpr::child_end() {
1111 return child_iterator();
1112}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001113
1114// ChooseExpr
1115Stmt::child_iterator ChooseExpr::child_begin() {
1116 return reinterpret_cast<Stmt**>(&SubExprs);
1117}
1118
1119Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001120 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001121}
1122
Anders Carlsson36760332007-10-15 20:28:48 +00001123// VAArgExpr
1124Stmt::child_iterator VAArgExpr::child_begin() {
1125 return reinterpret_cast<Stmt**>(&Val);
1126}
1127
1128Stmt::child_iterator VAArgExpr::child_end() {
1129 return reinterpret_cast<Stmt**>(&Val)+1;
1130}
1131
Anders Carlsson762b7c72007-08-31 04:56:16 +00001132// InitListExpr
1133Stmt::child_iterator InitListExpr::child_begin() {
1134 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1135}
1136Stmt::child_iterator InitListExpr::child_end() {
1137 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1138}
1139
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001140// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001141Stmt::child_iterator ObjCStringLiteral::child_begin() {
1142 return child_iterator();
1143}
1144Stmt::child_iterator ObjCStringLiteral::child_end() {
1145 return child_iterator();
1146}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001147
1148// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001149Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1150Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001151
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001152// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001153Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1154 return child_iterator();
1155}
1156Stmt::child_iterator ObjCSelectorExpr::child_end() {
1157 return child_iterator();
1158}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001159
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001160// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001161Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1162 return child_iterator();
1163}
1164Stmt::child_iterator ObjCProtocolExpr::child_end() {
1165 return child_iterator();
1166}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001167
Steve Naroffc39ca262007-09-18 23:55:05 +00001168// ObjCMessageExpr
1169Stmt::child_iterator ObjCMessageExpr::child_begin() {
1170 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1171}
1172Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001173 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001174}
1175