blob: 15fbb24a0d4198d4a9bc42b9ee4b500dcb2b58c8 [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
Anders Carlsson9e933a22007-11-30 22:47:59 +0000299 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000300 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
301 // For vectors, make sure base is an lvalue (i.e. not a function call).
302 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
303 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
304 return LV_Valid;
305 case DeclRefExprClass: // C99 6.5.1p2
306 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
307 return LV_Valid;
308 break;
309 case MemberExprClass: { // C99 6.5.2.3p4
310 const MemberExpr *m = cast<MemberExpr>(this);
311 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
312 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000313 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000314 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000315 return LV_Valid; // C99 6.5.3p4
316
317 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
318 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
319 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000320 break;
321 case ParenExprClass: // C99 6.5.1p5
322 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000323 case OCUVectorElementExprClass:
324 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000325 return LV_DuplicateVectorComponents;
326 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000327 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
328 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000329 default:
330 break;
331 }
332 return LV_InvalidExpression;
333}
334
335/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
336/// does not have an incomplete type, does not have a const-qualified type, and
337/// if it is a structure or union, does not have any member (including,
338/// recursively, any member or element of all contained aggregates or unions)
339/// with a const-qualified type.
340Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
341 isLvalueResult lvalResult = isLvalue();
342
343 switch (lvalResult) {
344 case LV_Valid: break;
345 case LV_NotObjectType: return MLV_NotObjectType;
346 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000347 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000348 case LV_InvalidExpression: return MLV_InvalidExpression;
349 }
350 if (TR.isConstQualified())
351 return MLV_ConstQualified;
352 if (TR->isArrayType())
353 return MLV_ArrayType;
354 if (TR->isIncompleteType())
355 return MLV_IncompleteType;
356
357 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
358 if (r->hasConstFields())
359 return MLV_ConstQualified;
360 }
361 return MLV_Valid;
362}
363
Chris Lattner743ec372007-11-27 21:35:27 +0000364/// hasStaticStorage - Return true if this expression has static storage
365/// duration. This means that the address of this expression is a link-time
366/// constant.
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000367bool Expr::hasStaticStorage() const {
368 switch (getStmtClass()) {
369 default:
370 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000371 case ParenExprClass:
372 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
373 case ImplicitCastExprClass:
374 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000375 case DeclRefExprClass: {
376 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
377 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
378 return VD->hasStaticStorage();
379 return false;
380 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000381 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000382 const MemberExpr *M = cast<MemberExpr>(this);
383 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000384 }
Chris Lattner743ec372007-11-27 21:35:27 +0000385 case ArraySubscriptExprClass:
386 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000387 }
388}
389
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000390bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000391 switch (getStmtClass()) {
392 default:
393 if (Loc) *Loc = getLocStart();
394 return false;
395 case ParenExprClass:
396 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
397 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000398 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000399 case FloatingLiteralClass:
400 case IntegerLiteralClass:
401 case CharacterLiteralClass:
402 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000403 case TypesCompatibleExprClass:
404 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000405 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000406 case CallExprClass: {
407 const CallExpr *CE = cast<CallExpr>(this);
408 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000409 Result.zextOrTrunc(
410 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000411 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000412 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000413 if (Loc) *Loc = getLocStart();
414 return false;
415 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000416 case DeclRefExprClass: {
417 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
418 // Accept address of function.
419 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000420 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000421 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000422 if (isa<VarDecl>(D))
423 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000424 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000425 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000426 case UnaryOperatorClass: {
427 const UnaryOperator *Exp = cast<UnaryOperator>(this);
428
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000429 // C99 6.6p9
430 if (Exp->getOpcode() == UnaryOperator::AddrOf)
431 return Exp->getSubExpr()->hasStaticStorage();
432
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000433 // Get the operand value. If this is sizeof/alignof, do not evalute the
434 // operand. This affects C99 6.6p3.
435 if (!Exp->isSizeOfAlignOfOp() &&
436 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
437 return false;
438
439 switch (Exp->getOpcode()) {
440 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
441 // See C99 6.6p3.
442 default:
443 if (Loc) *Loc = Exp->getOperatorLoc();
444 return false;
445 case UnaryOperator::Extension:
446 return true; // FIXME: this is wrong.
447 case UnaryOperator::SizeOf:
448 case UnaryOperator::AlignOf:
449 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
450 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
451 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000452 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000453 case UnaryOperator::LNot:
454 case UnaryOperator::Plus:
455 case UnaryOperator::Minus:
456 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000457 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000458 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000459 }
460 case SizeOfAlignOfTypeExprClass: {
461 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
462 // alignof always evaluates to a constant.
463 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
464 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000465 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000466 }
467 case BinaryOperatorClass: {
468 const BinaryOperator *Exp = cast<BinaryOperator>(this);
469
470 // The LHS of a constant expr is always evaluated and needed.
471 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
472 return false;
473
474 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
475 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000476 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000477 }
478 case ImplicitCastExprClass:
479 case CastExprClass: {
480 const Expr *SubExpr;
481 SourceLocation CastLoc;
482 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
483 SubExpr = C->getSubExpr();
484 CastLoc = C->getLParenLoc();
485 } else {
486 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
487 CastLoc = getLocStart();
488 }
489 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
490 if (Loc) *Loc = SubExpr->getLocStart();
491 return false;
492 }
Chris Lattner06db6132007-10-18 00:20:32 +0000493 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000494 }
495 case ConditionalOperatorClass: {
496 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000497 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000498 // Handle the GNU extension for missing LHS.
499 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000500 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000501 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000502 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000503 }
504 }
505
506 return true;
507}
508
Chris Lattner4b009652007-07-25 00:24:17 +0000509/// isIntegerConstantExpr - this recursive routine will test if an expression is
510/// an integer constant expression. Note: With the introduction of VLA's in
511/// C99 the result of the sizeof operator is no longer always a constant
512/// expression. The generalization of the wording to include any subexpression
513/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
514/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
515/// "0 || f()" can be treated as a constant expression. In C90 this expression,
516/// occurring in a context requiring a constant, would have been a constraint
517/// violation. FIXME: This routine currently implements C90 semantics.
518/// To properly implement C99 semantics this routine will need to evaluate
519/// expressions involving operators previously mentioned.
520
521/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
522/// comma, etc
523///
524/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000525/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000526///
527/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
528/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
529/// cast+dereference.
530bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
531 SourceLocation *Loc, bool isEvaluated) const {
532 switch (getStmtClass()) {
533 default:
534 if (Loc) *Loc = getLocStart();
535 return false;
536 case ParenExprClass:
537 return cast<ParenExpr>(this)->getSubExpr()->
538 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
539 case IntegerLiteralClass:
540 Result = cast<IntegerLiteral>(this)->getValue();
541 break;
542 case CharacterLiteralClass: {
543 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000544 Result.zextOrTrunc(
545 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000546 Result = CL->getValue();
547 Result.setIsUnsigned(!getType()->isSignedIntegerType());
548 break;
549 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000550 case TypesCompatibleExprClass: {
551 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000552 Result.zextOrTrunc(
553 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000554 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000555 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000556 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000557 case CallExprClass: {
558 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000559 Result.zextOrTrunc(
560 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000561 if (CE->isBuiltinClassifyType(Result))
562 break;
563 if (Loc) *Loc = getLocStart();
564 return false;
565 }
Chris Lattner4b009652007-07-25 00:24:17 +0000566 case DeclRefExprClass:
567 if (const EnumConstantDecl *D =
568 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
569 Result = D->getInitVal();
570 break;
571 }
572 if (Loc) *Loc = getLocStart();
573 return false;
574 case UnaryOperatorClass: {
575 const UnaryOperator *Exp = cast<UnaryOperator>(this);
576
577 // Get the operand value. If this is sizeof/alignof, do not evalute the
578 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000579 if (!Exp->isSizeOfAlignOfOp() &&
580 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000581 return false;
582
583 switch (Exp->getOpcode()) {
584 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
585 // See C99 6.6p3.
586 default:
587 if (Loc) *Loc = Exp->getOperatorLoc();
588 return false;
589 case UnaryOperator::Extension:
590 return true; // FIXME: this is wrong.
591 case UnaryOperator::SizeOf:
592 case UnaryOperator::AlignOf:
593 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
594 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
595 return false;
596
597 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000598 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000599 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
600 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000601
602 // Get information about the size or align.
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000603 if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner4b009652007-07-25 00:24:17 +0000604 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
605 Exp->getOperatorLoc());
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000606 } else {
607 unsigned CharSize = Ctx.Target.getCharWidth(Exp->getOperatorLoc());
608 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
609 Exp->getOperatorLoc()) / CharSize;
610 }
Chris Lattner4b009652007-07-25 00:24:17 +0000611 break;
612 case UnaryOperator::LNot: {
613 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000614 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000615 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
616 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000617 Result = Val;
618 break;
619 }
620 case UnaryOperator::Plus:
621 break;
622 case UnaryOperator::Minus:
623 Result = -Result;
624 break;
625 case UnaryOperator::Not:
626 Result = ~Result;
627 break;
628 }
629 break;
630 }
631 case SizeOfAlignOfTypeExprClass: {
632 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
633 // alignof always evaluates to a constant.
634 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
635 return false;
636
637 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000638 Result.zextOrTrunc(
639 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000640
641 // Get information about the size or align.
642 if (Exp->isSizeOf())
643 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
644 else
645 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
646 break;
647 }
648 case BinaryOperatorClass: {
649 const BinaryOperator *Exp = cast<BinaryOperator>(this);
650
651 // The LHS of a constant expr is always evaluated and needed.
652 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
653 return false;
654
655 llvm::APSInt RHS(Result);
656
657 // The short-circuiting &&/|| operators don't necessarily evaluate their
658 // RHS. Make sure to pass isEvaluated down correctly.
659 if (Exp->isLogicalOp()) {
660 bool RHSEval;
661 if (Exp->getOpcode() == BinaryOperator::LAnd)
662 RHSEval = Result != 0;
663 else {
664 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
665 RHSEval = Result == 0;
666 }
667
668 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
669 isEvaluated & RHSEval))
670 return false;
671 } else {
672 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
673 return false;
674 }
675
676 switch (Exp->getOpcode()) {
677 default:
678 if (Loc) *Loc = getLocStart();
679 return false;
680 case BinaryOperator::Mul:
681 Result *= RHS;
682 break;
683 case BinaryOperator::Div:
684 if (RHS == 0) {
685 if (!isEvaluated) break;
686 if (Loc) *Loc = getLocStart();
687 return false;
688 }
689 Result /= RHS;
690 break;
691 case BinaryOperator::Rem:
692 if (RHS == 0) {
693 if (!isEvaluated) break;
694 if (Loc) *Loc = getLocStart();
695 return false;
696 }
697 Result %= RHS;
698 break;
699 case BinaryOperator::Add: Result += RHS; break;
700 case BinaryOperator::Sub: Result -= RHS; break;
701 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000702 Result <<=
703 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000704 break;
705 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000706 Result >>=
707 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000708 break;
709 case BinaryOperator::LT: Result = Result < RHS; break;
710 case BinaryOperator::GT: Result = Result > RHS; break;
711 case BinaryOperator::LE: Result = Result <= RHS; break;
712 case BinaryOperator::GE: Result = Result >= RHS; break;
713 case BinaryOperator::EQ: Result = Result == RHS; break;
714 case BinaryOperator::NE: Result = Result != RHS; break;
715 case BinaryOperator::And: Result &= RHS; break;
716 case BinaryOperator::Xor: Result ^= RHS; break;
717 case BinaryOperator::Or: Result |= RHS; break;
718 case BinaryOperator::LAnd:
719 Result = Result != 0 && RHS != 0;
720 break;
721 case BinaryOperator::LOr:
722 Result = Result != 0 || RHS != 0;
723 break;
724
725 case BinaryOperator::Comma:
726 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
727 // *except* when they are contained within a subexpression that is not
728 // evaluated". Note that Assignment can never happen due to constraints
729 // on the LHS subexpr, so we don't need to check it here.
730 if (isEvaluated) {
731 if (Loc) *Loc = getLocStart();
732 return false;
733 }
734
735 // The result of the constant expr is the RHS.
736 Result = RHS;
737 return true;
738 }
739
740 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
741 break;
742 }
743 case ImplicitCastExprClass:
744 case CastExprClass: {
745 const Expr *SubExpr;
746 SourceLocation CastLoc;
747 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
748 SubExpr = C->getSubExpr();
749 CastLoc = C->getLParenLoc();
750 } else {
751 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
752 CastLoc = getLocStart();
753 }
754
755 // C99 6.6p6: shall only convert arithmetic types to integer types.
756 if (!SubExpr->getType()->isArithmeticType() ||
757 !getType()->isIntegerType()) {
758 if (Loc) *Loc = SubExpr->getLocStart();
759 return false;
760 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000761
762 uint32_t DestWidth =
763 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
764
Chris Lattner4b009652007-07-25 00:24:17 +0000765 // Handle simple integer->integer casts.
766 if (SubExpr->getType()->isIntegerType()) {
767 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
768 return false;
769
770 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000771 // If the input is signed, do a sign extend, noop, or truncate.
772 if (SubExpr->getType()->isSignedIntegerType())
773 Result.sextOrTrunc(DestWidth);
774 else // If the input is unsigned, do a zero extend, noop, or truncate.
775 Result.zextOrTrunc(DestWidth);
776 break;
777 }
778
779 // Allow floating constants that are the immediate operands of casts or that
780 // are parenthesized.
781 const Expr *Operand = SubExpr;
782 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
783 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000784
785 // If this isn't a floating literal, we can't handle it.
786 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
787 if (!FL) {
788 if (Loc) *Loc = Operand->getLocStart();
789 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000790 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000791
792 // Determine whether we are converting to unsigned or signed.
793 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000794
795 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
796 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000797 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000798 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
799 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000800 Result = llvm::APInt(DestWidth, 4, Space);
801 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000802 }
803 case ConditionalOperatorClass: {
804 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
805
806 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
807 return false;
808
809 const Expr *TrueExp = Exp->getLHS();
810 const Expr *FalseExp = Exp->getRHS();
811 if (Result == 0) std::swap(TrueExp, FalseExp);
812
813 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000814 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000815 return false;
816 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000817 if (TrueExp &&
818 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000819 return false;
820 break;
821 }
822 }
823
824 // Cases that are valid constant exprs fall through to here.
825 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
826 return true;
827}
828
829
830/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
831/// integer constant expression with the value zero, or if this is one that is
832/// cast to void*.
833bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
834 // Strip off a cast to void*, if it exists.
835 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
836 // Check that it is a cast to void*.
837 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
838 QualType Pointee = PT->getPointeeType();
839 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
840 CE->getSubExpr()->getType()->isIntegerType()) // from int.
841 return CE->getSubExpr()->isNullPointerConstant(Ctx);
842 }
Steve Naroff1d6b2472007-08-28 21:20:34 +0000843 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff3d052872007-08-29 00:00:02 +0000844 // Ignore the ImplicitCastExpr type entirely.
845 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000846 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
847 // Accept ((void*)0) as a null pointer constant, as many other
848 // implementations do.
849 return PE->getSubExpr()->isNullPointerConstant(Ctx);
850 }
851
852 // This expression must be an integer type.
853 if (!getType()->isIntegerType())
854 return false;
855
856 // If we have an integer constant expression, we need to *evaluate* it and
857 // test for the value 0.
858 llvm::APSInt Val(32);
859 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
860}
Steve Naroffc11705f2007-07-28 23:10:27 +0000861
Chris Lattnera0d03a72007-08-03 17:31:20 +0000862unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000863 return strlen(Accessor.getName());
864}
865
866
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000867/// getComponentType - Determine whether the components of this access are
868/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000869OCUVectorElementExpr::ElementType
870OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000871 // derive the component type, no need to waste space.
872 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000873
Chris Lattner9096b792007-08-02 22:33:49 +0000874 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
875 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000876
Chris Lattner9096b792007-08-02 22:33:49 +0000877 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000878 "getComponentType(): Illegal accessor");
879 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000880}
Steve Naroffba67f692007-07-30 03:29:09 +0000881
Chris Lattnera0d03a72007-08-03 17:31:20 +0000882/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000883/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000884bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000885 const char *compStr = Accessor.getName();
886 unsigned length = strlen(compStr);
887
888 for (unsigned i = 0; i < length-1; i++) {
889 const char *s = compStr+i;
890 for (const char c = *s++; *s; s++)
891 if (c == *s)
892 return true;
893 }
894 return false;
895}
Chris Lattner42158e72007-08-02 23:36:59 +0000896
897/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000898unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000899 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000900 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +0000901
902 unsigned Result = 0;
903
904 while (length--) {
905 Result <<= 2;
906 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
907 assert(Idx != -1 && "Invalid accessor letter");
908 Result |= Idx;
909 }
910 return Result;
911}
912
Steve Naroff4ed9d662007-09-27 14:38:14 +0000913// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +0000914ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000915 QualType retType, ObjcMethodDecl *mproto,
916 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +0000917 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000918 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
919 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +0000920 NumArgs = nargs;
921 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +0000922 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +0000923 if (NumArgs) {
924 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000925 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
926 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000927 LBracloc = LBrac;
928 RBracloc = RBrac;
929}
930
Steve Naroff4ed9d662007-09-27 14:38:14 +0000931// constructor for class messages.
932// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +0000933ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000934 QualType retType, ObjcMethodDecl *mproto,
935 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +0000936 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000937 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
938 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +0000939 NumArgs = nargs;
940 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +0000941 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +0000942 if (NumArgs) {
943 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000944 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
945 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000946 LBracloc = LBrac;
947 RBracloc = RBrac;
948}
949
Chris Lattnerf624cd22007-10-25 00:29:32 +0000950
951bool ChooseExpr::isConditionTrue(ASTContext &C) const {
952 llvm::APSInt CondVal(32);
953 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
954 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
955 return CondVal != 0;
956}
957
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000958//===----------------------------------------------------------------------===//
959// Child Iterators for iterating over subexpressions/substatements
960//===----------------------------------------------------------------------===//
961
962// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000963Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
964Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000965
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000966// ObjCIvarRefExpr
967Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
968Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
969
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000970// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000971Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
972Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000973
974// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000975Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
976Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000977
978// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000979Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
980Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000981
982// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000983Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
984Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000985
Chris Lattner1de66eb2007-08-26 03:42:43 +0000986// ImaginaryLiteral
987Stmt::child_iterator ImaginaryLiteral::child_begin() {
988 return reinterpret_cast<Stmt**>(&Val);
989}
990Stmt::child_iterator ImaginaryLiteral::child_end() {
991 return reinterpret_cast<Stmt**>(&Val)+1;
992}
993
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000994// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000995Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
996Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000997
998// ParenExpr
999Stmt::child_iterator ParenExpr::child_begin() {
1000 return reinterpret_cast<Stmt**>(&Val);
1001}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001002Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001003 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001004}
1005
1006// UnaryOperator
1007Stmt::child_iterator UnaryOperator::child_begin() {
1008 return reinterpret_cast<Stmt**>(&Val);
1009}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001010Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001011 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001012}
1013
1014// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001015Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
1016 return child_iterator();
1017}
1018Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
1019 return child_iterator();
1020}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001021
1022// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001023Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001024 return reinterpret_cast<Stmt**>(&SubExprs);
1025}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001026Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001027 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001028}
1029
1030// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001031Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001032 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001033}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001034Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001035 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001036}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001037
1038// MemberExpr
1039Stmt::child_iterator MemberExpr::child_begin() {
1040 return reinterpret_cast<Stmt**>(&Base);
1041}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001042Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001043 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001044}
1045
1046// OCUVectorElementExpr
1047Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1048 return reinterpret_cast<Stmt**>(&Base);
1049}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001050Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001051 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001052}
1053
1054// CompoundLiteralExpr
1055Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1056 return reinterpret_cast<Stmt**>(&Init);
1057}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001058Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001059 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001060}
1061
1062// ImplicitCastExpr
1063Stmt::child_iterator ImplicitCastExpr::child_begin() {
1064 return reinterpret_cast<Stmt**>(&Op);
1065}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001066Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001067 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001068}
1069
1070// CastExpr
1071Stmt::child_iterator CastExpr::child_begin() {
1072 return reinterpret_cast<Stmt**>(&Op);
1073}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001074Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001075 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001076}
1077
1078// BinaryOperator
1079Stmt::child_iterator BinaryOperator::child_begin() {
1080 return reinterpret_cast<Stmt**>(&SubExprs);
1081}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001082Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001083 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001084}
1085
1086// ConditionalOperator
1087Stmt::child_iterator ConditionalOperator::child_begin() {
1088 return reinterpret_cast<Stmt**>(&SubExprs);
1089}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001090Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001091 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001092}
1093
1094// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001095Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1096Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001097
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001098// StmtExpr
1099Stmt::child_iterator StmtExpr::child_begin() {
1100 return reinterpret_cast<Stmt**>(&SubStmt);
1101}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001102Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001103 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001104}
1105
1106// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001107Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1108 return child_iterator();
1109}
1110
1111Stmt::child_iterator TypesCompatibleExpr::child_end() {
1112 return child_iterator();
1113}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001114
1115// ChooseExpr
1116Stmt::child_iterator ChooseExpr::child_begin() {
1117 return reinterpret_cast<Stmt**>(&SubExprs);
1118}
1119
1120Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001121 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001122}
1123
Anders Carlsson36760332007-10-15 20:28:48 +00001124// VAArgExpr
1125Stmt::child_iterator VAArgExpr::child_begin() {
1126 return reinterpret_cast<Stmt**>(&Val);
1127}
1128
1129Stmt::child_iterator VAArgExpr::child_end() {
1130 return reinterpret_cast<Stmt**>(&Val)+1;
1131}
1132
Anders Carlsson762b7c72007-08-31 04:56:16 +00001133// InitListExpr
1134Stmt::child_iterator InitListExpr::child_begin() {
1135 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1136}
1137Stmt::child_iterator InitListExpr::child_end() {
1138 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1139}
1140
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001141// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001142Stmt::child_iterator ObjCStringLiteral::child_begin() {
1143 return child_iterator();
1144}
1145Stmt::child_iterator ObjCStringLiteral::child_end() {
1146 return child_iterator();
1147}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001148
1149// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001150Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1151Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001152
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001153// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001154Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1155 return child_iterator();
1156}
1157Stmt::child_iterator ObjCSelectorExpr::child_end() {
1158 return child_iterator();
1159}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001160
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001161// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001162Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1163 return child_iterator();
1164}
1165Stmt::child_iterator ObjCProtocolExpr::child_end() {
1166 return child_iterator();
1167}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001168
Steve Naroffc39ca262007-09-18 23:55:05 +00001169// ObjCMessageExpr
1170Stmt::child_iterator ObjCMessageExpr::child_begin() {
1171 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1172}
1173Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001174 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001175}
1176