blob: b21aad20cee7aa01e834e395c821d0fe8ae32125 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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"
Chris Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/StmtVisitor.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// Primary Expressions.
22//===----------------------------------------------------------------------===//
23
24StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
25 bool Wide, QualType t, SourceLocation firstLoc,
26 SourceLocation lastLoc) :
27 Expr(StringLiteralClass, t) {
28 // OPTIMIZE: could allocate this appended to the StringLiteral.
29 char *AStrData = new char[byteLength];
30 memcpy(AStrData, strData, byteLength);
31 StrData = AStrData;
32 ByteLength = byteLength;
33 IsWide = Wide;
34 firstTokLoc = firstLoc;
35 lastTokLoc = lastLoc;
36}
37
38StringLiteral::~StringLiteral() {
39 delete[] StrData;
40}
41
42bool UnaryOperator::isPostfix(Opcode Op) {
43 switch (Op) {
44 case PostInc:
45 case PostDec:
46 return true;
47 default:
48 return false;
49 }
50}
51
52/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
53/// corresponds to, e.g. "sizeof" or "[pre]++".
54const char *UnaryOperator::getOpcodeStr(Opcode Op) {
55 switch (Op) {
56 default: assert(0 && "Unknown unary operator");
57 case PostInc: return "++";
58 case PostDec: return "--";
59 case PreInc: return "++";
60 case PreDec: return "--";
61 case AddrOf: return "&";
62 case Deref: return "*";
63 case Plus: return "+";
64 case Minus: return "-";
65 case Not: return "~";
66 case LNot: return "!";
67 case Real: return "__real";
68 case Imag: return "__imag";
69 case SizeOf: return "sizeof";
70 case AlignOf: return "alignof";
71 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +000072 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +000073 }
74}
75
76//===----------------------------------------------------------------------===//
77// Postfix Operators.
78//===----------------------------------------------------------------------===//
79
80CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
81 SourceLocation rparenloc)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000082 : Expr(CallExprClass, t), NumArgs(numargs) {
83 SubExprs = new Expr*[numargs+1];
84 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +000085 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000086 SubExprs[i+ARGS_START] = args[i];
Reid Spencer5f016e22007-07-11 17:01:13 +000087 RParenLoc = rparenloc;
88}
89
Steve Naroff13b7c5f2007-08-08 22:15:55 +000090bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
91 // The following enum mimics gcc's internal "typeclass.h" file.
92 enum gcc_type_class {
93 no_type_class = -1,
94 void_type_class, integer_type_class, char_type_class,
95 enumeral_type_class, boolean_type_class,
96 pointer_type_class, reference_type_class, offset_type_class,
97 real_type_class, complex_type_class,
98 function_type_class, method_type_class,
99 record_type_class, union_type_class,
100 array_type_class, string_type_class,
101 lang_type_class
102 };
103 Result.setIsSigned(true);
104
105 // All simple function calls (e.g. func()) are implicitly cast to pointer to
106 // function. As a result, we try and obtain the DeclRefExpr from the
107 // ImplicitCastExpr.
108 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
109 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
110 return false;
111 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
112 if (!DRE)
113 return false;
114
115 // We have a DeclRefExpr.
116 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
117 // If no argument was supplied, default to "no_type_class". This isn't
118 // ideal, however it's what gcc does.
119 Result = static_cast<uint64_t>(no_type_class);
120 if (NumArgs >= 1) {
121 QualType argType = getArg(0)->getType();
122
123 if (argType->isVoidType())
124 Result = void_type_class;
125 else if (argType->isEnumeralType())
126 Result = enumeral_type_class;
127 else if (argType->isBooleanType())
128 Result = boolean_type_class;
129 else if (argType->isCharType())
130 Result = string_type_class; // gcc doesn't appear to use char_type_class
131 else if (argType->isIntegerType())
132 Result = integer_type_class;
133 else if (argType->isPointerType())
134 Result = pointer_type_class;
135 else if (argType->isReferenceType())
136 Result = reference_type_class;
137 else if (argType->isRealType())
138 Result = real_type_class;
139 else if (argType->isComplexType())
140 Result = complex_type_class;
141 else if (argType->isFunctionType())
142 Result = function_type_class;
143 else if (argType->isStructureType())
144 Result = record_type_class;
145 else if (argType->isUnionType())
146 Result = union_type_class;
147 else if (argType->isArrayType())
148 Result = array_type_class;
149 else if (argType->isUnionType())
150 Result = union_type_class;
151 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
152 assert(1 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
153 }
154 return true;
155 }
156 return false;
157}
158
Reid Spencer5f016e22007-07-11 17:01:13 +0000159/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
160/// corresponds to, e.g. "<<=".
161const char *BinaryOperator::getOpcodeStr(Opcode Op) {
162 switch (Op) {
163 default: assert(0 && "Unknown binary operator");
164 case Mul: return "*";
165 case Div: return "/";
166 case Rem: return "%";
167 case Add: return "+";
168 case Sub: return "-";
169 case Shl: return "<<";
170 case Shr: return ">>";
171 case LT: return "<";
172 case GT: return ">";
173 case LE: return "<=";
174 case GE: return ">=";
175 case EQ: return "==";
176 case NE: return "!=";
177 case And: return "&";
178 case Xor: return "^";
179 case Or: return "|";
180 case LAnd: return "&&";
181 case LOr: return "||";
182 case Assign: return "=";
183 case MulAssign: return "*=";
184 case DivAssign: return "/=";
185 case RemAssign: return "%=";
186 case AddAssign: return "+=";
187 case SubAssign: return "-=";
188 case ShlAssign: return "<<=";
189 case ShrAssign: return ">>=";
190 case AndAssign: return "&=";
191 case XorAssign: return "^=";
192 case OrAssign: return "|=";
193 case Comma: return ",";
194 }
195}
196
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000197InitListExpr::InitListExpr(SourceLocation lbraceloc,
198 Expr **initexprs, unsigned numinits,
199 SourceLocation rbraceloc)
200 : Expr(InitListExprClass, QualType())
201 , NumInits(numinits)
202 , LBraceLoc(lbraceloc)
203 , RBraceLoc(rbraceloc)
204{
205 InitExprs = new Expr*[numinits];
206 for (unsigned i = 0; i != numinits; i++)
207 InitExprs[i] = initexprs[i];
208}
Reid Spencer5f016e22007-07-11 17:01:13 +0000209
210//===----------------------------------------------------------------------===//
211// Generic Expression Routines
212//===----------------------------------------------------------------------===//
213
214/// hasLocalSideEffect - Return true if this immediate expression has side
215/// effects, not counting any sub-expressions.
216bool Expr::hasLocalSideEffect() const {
217 switch (getStmtClass()) {
218 default:
219 return false;
220 case ParenExprClass:
221 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
222 case UnaryOperatorClass: {
223 const UnaryOperator *UO = cast<UnaryOperator>(this);
224
225 switch (UO->getOpcode()) {
226 default: return false;
227 case UnaryOperator::PostInc:
228 case UnaryOperator::PostDec:
229 case UnaryOperator::PreInc:
230 case UnaryOperator::PreDec:
231 return true; // ++/--
232
233 case UnaryOperator::Deref:
234 // Dereferencing a volatile pointer is a side-effect.
235 return getType().isVolatileQualified();
236 case UnaryOperator::Real:
237 case UnaryOperator::Imag:
238 // accessing a piece of a volatile complex is a side-effect.
239 return UO->getSubExpr()->getType().isVolatileQualified();
240
241 case UnaryOperator::Extension:
242 return UO->getSubExpr()->hasLocalSideEffect();
243 }
244 }
245 case BinaryOperatorClass:
246 return cast<BinaryOperator>(this)->isAssignmentOp();
Chris Lattnereb14fe82007-08-25 02:00:02 +0000247 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000248 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000249
250 case MemberExprClass:
251 case ArraySubscriptExprClass:
252 // If the base pointer or element is to a volatile pointer/field, accessing
253 // if is a side effect.
254 return getType().isVolatileQualified();
255
256 case CallExprClass:
257 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
258 // should warn.
259 return true;
Chris Lattnera9c01022007-09-26 22:06:30 +0000260 case ObjCMessageExprClass:
261 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000262
263 case CastExprClass:
264 // If this is a cast to void, check the operand. Otherwise, the result of
265 // the cast is unused.
266 if (getType()->isVoidType())
267 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
268 return false;
269 }
270}
271
272/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
273/// incomplete type other than void. Nonarray expressions that can be lvalues:
274/// - name, where name must be a variable
275/// - e[i]
276/// - (e), where e must be an lvalue
277/// - e.name, where e must be an lvalue
278/// - e->name
279/// - *e, the type of e cannot be a function type
280/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000281/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000282/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000283///
Bill Wendlingca51c972007-07-16 07:07:56 +0000284Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000286 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 return LV_NotObjectType;
288
Steve Naroff731ec572007-07-21 13:32:03 +0000289 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000291
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000292 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000293 return LV_Valid;
294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // the type looks fine, now check the expression
296 switch (getStmtClass()) {
297 case StringLiteralClass: // C99 6.5.1p4
298 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
299 // For vectors, make sure base is an lvalue (i.e. not a function call).
300 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
301 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
302 return LV_Valid;
303 case DeclRefExprClass: // C99 6.5.1p2
304 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
305 return LV_Valid;
306 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000307 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 const MemberExpr *m = cast<MemberExpr>(this);
309 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000310 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000311 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000313 return LV_Valid; // C99 6.5.3p4
314
315 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
316 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
317 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 break;
319 case ParenExprClass: // C99 6.5.1p5
320 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattner6481a572007-08-03 17:31:20 +0000321 case OCUVectorElementExprClass:
322 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000323 return LV_DuplicateVectorComponents;
324 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 default:
326 break;
327 }
328 return LV_InvalidExpression;
329}
330
331/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
332/// does not have an incomplete type, does not have a const-qualified type, and
333/// if it is a structure or union, does not have any member (including,
334/// recursively, any member or element of all contained aggregates or unions)
335/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000336Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 isLvalueResult lvalResult = isLvalue();
338
339 switch (lvalResult) {
340 case LV_Valid: break;
341 case LV_NotObjectType: return MLV_NotObjectType;
342 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000343 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 case LV_InvalidExpression: return MLV_InvalidExpression;
345 }
346 if (TR.isConstQualified())
347 return MLV_ConstQualified;
348 if (TR->isArrayType())
349 return MLV_ArrayType;
350 if (TR->isIncompleteType())
351 return MLV_IncompleteType;
352
353 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
354 if (r->hasConstFields())
355 return MLV_ConstQualified;
356 }
357 return MLV_Valid;
358}
359
Steve Naroff38374b02007-09-02 20:30:18 +0000360bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff38374b02007-09-02 20:30:18 +0000361 switch (getStmtClass()) {
362 default:
363 if (Loc) *Loc = getLocStart();
364 return false;
365 case ParenExprClass:
366 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
367 case StringLiteralClass:
368 case FloatingLiteralClass:
369 case IntegerLiteralClass:
370 case CharacterLiteralClass:
371 case ImaginaryLiteralClass:
Anders Carlsson1a86b332007-10-17 00:52:43 +0000372 case TypesCompatibleExprClass:
373 case CXXBoolLiteralExprClass:
Chris Lattner2777e492007-10-18 00:20:32 +0000374 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000375 case CallExprClass: {
376 const CallExpr *CE = cast<CallExpr>(this);
377 llvm::APSInt Result(32);
Hartmut Kaiser86fd3552007-09-16 21:35:35 +0000378 Result.zextOrTrunc(
379 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff38374b02007-09-02 20:30:18 +0000380 if (CE->isBuiltinClassifyType(Result))
Chris Lattner2777e492007-10-18 00:20:32 +0000381 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000382 if (Loc) *Loc = getLocStart();
383 return false;
384 }
385 case DeclRefExprClass:
386 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl()))
Chris Lattner2777e492007-10-18 00:20:32 +0000387 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000388 if (Loc) *Loc = getLocStart();
389 return false;
390 case UnaryOperatorClass: {
391 const UnaryOperator *Exp = cast<UnaryOperator>(this);
392
393 // Get the operand value. If this is sizeof/alignof, do not evalute the
394 // operand. This affects C99 6.6p3.
395 if (!Exp->isSizeOfAlignOfOp() &&
396 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
397 return false;
398
399 switch (Exp->getOpcode()) {
400 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
401 // See C99 6.6p3.
402 default:
403 if (Loc) *Loc = Exp->getOperatorLoc();
404 return false;
405 case UnaryOperator::Extension:
406 return true; // FIXME: this is wrong.
407 case UnaryOperator::SizeOf:
408 case UnaryOperator::AlignOf:
409 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
410 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
411 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000412 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000413 case UnaryOperator::LNot:
414 case UnaryOperator::Plus:
415 case UnaryOperator::Minus:
416 case UnaryOperator::Not:
Chris Lattner2777e492007-10-18 00:20:32 +0000417 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000418 }
Steve Naroff38374b02007-09-02 20:30:18 +0000419 }
420 case SizeOfAlignOfTypeExprClass: {
421 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
422 // alignof always evaluates to a constant.
423 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
424 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000425 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000426 }
427 case BinaryOperatorClass: {
428 const BinaryOperator *Exp = cast<BinaryOperator>(this);
429
430 // The LHS of a constant expr is always evaluated and needed.
431 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
432 return false;
433
434 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
435 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000436 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000437 }
438 case ImplicitCastExprClass:
439 case CastExprClass: {
440 const Expr *SubExpr;
441 SourceLocation CastLoc;
442 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
443 SubExpr = C->getSubExpr();
444 CastLoc = C->getLParenLoc();
445 } else {
446 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
447 CastLoc = getLocStart();
448 }
449 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
450 if (Loc) *Loc = SubExpr->getLocStart();
451 return false;
452 }
Chris Lattner2777e492007-10-18 00:20:32 +0000453 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000454 }
455 case ConditionalOperatorClass: {
456 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner2777e492007-10-18 00:20:32 +0000457 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
458 !Exp->getLHS()->isConstantExpr(Ctx, Loc) ||
459 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff38374b02007-09-02 20:30:18 +0000460 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000461 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000462 }
463 }
464
465 return true;
466}
467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468/// isIntegerConstantExpr - this recursive routine will test if an expression is
469/// an integer constant expression. Note: With the introduction of VLA's in
470/// C99 the result of the sizeof operator is no longer always a constant
471/// expression. The generalization of the wording to include any subexpression
472/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
473/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
474/// "0 || f()" can be treated as a constant expression. In C90 this expression,
475/// occurring in a context requiring a constant, would have been a constraint
476/// violation. FIXME: This routine currently implements C90 semantics.
477/// To properly implement C99 semantics this routine will need to evaluate
478/// expressions involving operators previously mentioned.
479
480/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
481/// comma, etc
482///
483/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000484/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000485///
486/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
487/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
488/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000489bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
490 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 switch (getStmtClass()) {
492 default:
493 if (Loc) *Loc = getLocStart();
494 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 case ParenExprClass:
496 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000497 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 case IntegerLiteralClass:
499 Result = cast<IntegerLiteral>(this)->getValue();
500 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000501 case CharacterLiteralClass: {
502 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000503 Result.zextOrTrunc(
504 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000505 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000506 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000507 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000508 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000509 case TypesCompatibleExprClass: {
510 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000511 Result.zextOrTrunc(
512 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffec0550f2007-10-15 20:41:53 +0000513 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff389cecc2007-08-02 00:13:27 +0000514 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000515 }
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000516 case CallExprClass: {
517 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000518 Result.zextOrTrunc(
519 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000520 if (CE->isBuiltinClassifyType(Result))
521 break;
522 if (Loc) *Loc = getLocStart();
523 return false;
524 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 case DeclRefExprClass:
526 if (const EnumConstantDecl *D =
527 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
528 Result = D->getInitVal();
529 break;
530 }
531 if (Loc) *Loc = getLocStart();
532 return false;
533 case UnaryOperatorClass: {
534 const UnaryOperator *Exp = cast<UnaryOperator>(this);
535
536 // Get the operand value. If this is sizeof/alignof, do not evalute the
537 // operand. This affects C99 6.6p3.
Chris Lattner602dafd2007-08-23 21:42:50 +0000538 if (!Exp->isSizeOfAlignOfOp() &&
539 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 return false;
541
542 switch (Exp->getOpcode()) {
543 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
544 // See C99 6.6p3.
545 default:
546 if (Loc) *Loc = Exp->getOperatorLoc();
547 return false;
548 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000549 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 case UnaryOperator::SizeOf:
551 case UnaryOperator::AlignOf:
552 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner590b6642007-07-15 23:26:56 +0000553 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 return false;
555
Chris Lattner76e773a2007-07-18 18:38:36 +0000556 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000557 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000558 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
559 Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000560
561 // Get information about the size or align.
562 if (Exp->getOpcode() == UnaryOperator::SizeOf)
563 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
564 Exp->getOperatorLoc());
565 else
566 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
567 Exp->getOperatorLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 break;
569 case UnaryOperator::LNot: {
570 bool Val = Result != 0;
Chris Lattner701e5eb2007-09-04 02:45:27 +0000571 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000572 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
573 Exp->getOperatorLoc())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 Result = Val;
575 break;
576 }
577 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 break;
579 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 Result = -Result;
581 break;
582 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 Result = ~Result;
584 break;
585 }
586 break;
587 }
588 case SizeOfAlignOfTypeExprClass: {
589 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
590 // alignof always evaluates to a constant.
Chris Lattner590b6642007-07-15 23:26:56 +0000591 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 return false;
593
Chris Lattner76e773a2007-07-18 18:38:36 +0000594 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000595 Result.zextOrTrunc(
596 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000597
598 // Get information about the size or align.
599 if (Exp->isSizeOf())
600 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
601 else
602 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 break;
604 }
605 case BinaryOperatorClass: {
606 const BinaryOperator *Exp = cast<BinaryOperator>(this);
607
608 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000609 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 return false;
611
612 llvm::APSInt RHS(Result);
613
614 // The short-circuiting &&/|| operators don't necessarily evaluate their
615 // RHS. Make sure to pass isEvaluated down correctly.
616 if (Exp->isLogicalOp()) {
617 bool RHSEval;
618 if (Exp->getOpcode() == BinaryOperator::LAnd)
619 RHSEval = Result != 0;
620 else {
621 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
622 RHSEval = Result == 0;
623 }
624
Chris Lattner590b6642007-07-15 23:26:56 +0000625 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 isEvaluated & RHSEval))
627 return false;
628 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000629 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 return false;
631 }
632
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 switch (Exp->getOpcode()) {
634 default:
635 if (Loc) *Loc = getLocStart();
636 return false;
637 case BinaryOperator::Mul:
638 Result *= RHS;
639 break;
640 case BinaryOperator::Div:
641 if (RHS == 0) {
642 if (!isEvaluated) break;
643 if (Loc) *Loc = getLocStart();
644 return false;
645 }
646 Result /= RHS;
647 break;
648 case BinaryOperator::Rem:
649 if (RHS == 0) {
650 if (!isEvaluated) break;
651 if (Loc) *Loc = getLocStart();
652 return false;
653 }
654 Result %= RHS;
655 break;
656 case BinaryOperator::Add: Result += RHS; break;
657 case BinaryOperator::Sub: Result -= RHS; break;
658 case BinaryOperator::Shl:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000659 Result <<=
660 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 break;
662 case BinaryOperator::Shr:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000663 Result >>=
664 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 break;
666 case BinaryOperator::LT: Result = Result < RHS; break;
667 case BinaryOperator::GT: Result = Result > RHS; break;
668 case BinaryOperator::LE: Result = Result <= RHS; break;
669 case BinaryOperator::GE: Result = Result >= RHS; break;
670 case BinaryOperator::EQ: Result = Result == RHS; break;
671 case BinaryOperator::NE: Result = Result != RHS; break;
672 case BinaryOperator::And: Result &= RHS; break;
673 case BinaryOperator::Xor: Result ^= RHS; break;
674 case BinaryOperator::Or: Result |= RHS; break;
675 case BinaryOperator::LAnd:
676 Result = Result != 0 && RHS != 0;
677 break;
678 case BinaryOperator::LOr:
679 Result = Result != 0 || RHS != 0;
680 break;
681
682 case BinaryOperator::Comma:
683 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
684 // *except* when they are contained within a subexpression that is not
685 // evaluated". Note that Assignment can never happen due to constraints
686 // on the LHS subexpr, so we don't need to check it here.
687 if (isEvaluated) {
688 if (Loc) *Loc = getLocStart();
689 return false;
690 }
691
692 // The result of the constant expr is the RHS.
693 Result = RHS;
694 return true;
695 }
696
697 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
698 break;
699 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000700 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000702 const Expr *SubExpr;
703 SourceLocation CastLoc;
704 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
705 SubExpr = C->getSubExpr();
706 CastLoc = C->getLParenLoc();
707 } else {
708 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
709 CastLoc = getLocStart();
710 }
711
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000713 if (!SubExpr->getType()->isArithmeticType() ||
714 !getType()->isIntegerType()) {
715 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 return false;
717 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000718
719 uint32_t DestWidth =
720 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
721
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000723 if (SubExpr->getType()->isIntegerType()) {
724 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000726
727 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000728 // If the input is signed, do a sign extend, noop, or truncate.
729 if (SubExpr->getType()->isSignedIntegerType())
730 Result.sextOrTrunc(DestWidth);
731 else // If the input is unsigned, do a zero extend, noop, or truncate.
732 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 break;
734 }
735
736 // Allow floating constants that are the immediate operands of casts or that
737 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000738 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
740 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +0000741
742 // If this isn't a floating literal, we can't handle it.
743 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
744 if (!FL) {
745 if (Loc) *Loc = Operand->getLocStart();
746 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000748
749 // Determine whether we are converting to unsigned or signed.
750 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +0000751
752 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
753 // be called multiple times per AST.
Chris Lattner987b15d2007-09-22 19:04:13 +0000754 uint64_t Space[4];
Chris Lattnerccc213f2007-09-26 00:47:26 +0000755 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
756 llvm::APFloat::rmTowardZero);
Chris Lattner987b15d2007-09-22 19:04:13 +0000757 Result = llvm::APInt(DestWidth, 4, Space);
758 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 }
760 case ConditionalOperatorClass: {
761 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
762
Chris Lattner590b6642007-07-15 23:26:56 +0000763 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 return false;
765
766 const Expr *TrueExp = Exp->getLHS();
767 const Expr *FalseExp = Exp->getRHS();
768 if (Result == 0) std::swap(TrueExp, FalseExp);
769
770 // Evaluate the false one first, discard the result.
Chris Lattner590b6642007-07-15 23:26:56 +0000771 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 return false;
773 // Evalute the true one, capture the result.
Chris Lattner590b6642007-07-15 23:26:56 +0000774 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 break;
777 }
778 }
779
780 // Cases that are valid constant exprs fall through to here.
781 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
782 return true;
783}
784
785
786/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
787/// integer constant expression with the value zero, or if this is one that is
788/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000789bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 // Strip off a cast to void*, if it exists.
791 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
792 // Check that it is a cast to void*.
793 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
794 QualType Pointee = PT->getPointeeType();
795 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
796 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Chris Lattner590b6642007-07-15 23:26:56 +0000797 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 }
Steve Naroff7269f2d2007-08-28 21:20:34 +0000799 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff19a6ebd2007-08-29 00:00:02 +0000800 // Ignore the ImplicitCastExpr type entirely.
801 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
803 // Accept ((void*)0) as a null pointer constant, as many other
804 // implementations do.
Chris Lattner590b6642007-07-15 23:26:56 +0000805 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 }
807
808 // This expression must be an integer type.
809 if (!getType()->isIntegerType())
810 return false;
811
812 // If we have an integer constant expression, we need to *evaluate* it and
813 // test for the value 0.
814 llvm::APSInt Val(32);
Chris Lattner590b6642007-07-15 23:26:56 +0000815 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000816}
Steve Naroff31a45842007-07-28 23:10:27 +0000817
Chris Lattner6481a572007-08-03 17:31:20 +0000818unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner4d0ac882007-08-03 16:00:20 +0000819 return strlen(Accessor.getName());
820}
821
822
Chris Lattnercb92a112007-08-02 21:47:28 +0000823/// getComponentType - Determine whether the components of this access are
824/// "point" "color" or "texture" elements.
Chris Lattner6481a572007-08-03 17:31:20 +0000825OCUVectorElementExpr::ElementType
826OCUVectorElementExpr::getElementType() const {
Steve Naroff31a45842007-07-28 23:10:27 +0000827 // derive the component type, no need to waste space.
828 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +0000829
Chris Lattner88dca042007-08-02 22:33:49 +0000830 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
831 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +0000832
Chris Lattner88dca042007-08-02 22:33:49 +0000833 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +0000834 "getComponentType(): Illegal accessor");
835 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +0000836}
Steve Narofffec0b492007-07-30 03:29:09 +0000837
Chris Lattner6481a572007-08-03 17:31:20 +0000838/// containsDuplicateElements - Return true if any element access is
Chris Lattnercb92a112007-08-02 21:47:28 +0000839/// repeated.
Chris Lattner6481a572007-08-03 17:31:20 +0000840bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +0000841 const char *compStr = Accessor.getName();
842 unsigned length = strlen(compStr);
843
844 for (unsigned i = 0; i < length-1; i++) {
845 const char *s = compStr+i;
846 for (const char c = *s++; *s; s++)
847 if (c == *s)
848 return true;
849 }
850 return false;
851}
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000852
853/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattner6481a572007-08-03 17:31:20 +0000854unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000855 const char *compStr = Accessor.getName();
Chris Lattner6481a572007-08-03 17:31:20 +0000856 unsigned length = getNumElements();
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000857
858 unsigned Result = 0;
859
860 while (length--) {
861 Result <<= 2;
862 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
863 assert(Idx != -1 && "Invalid accessor letter");
864 Result |= Idx;
865 }
866 return Result;
867}
868
Steve Naroff68d331a2007-09-27 14:38:14 +0000869// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +0000870ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Steve Naroff68d331a2007-09-27 14:38:14 +0000871 QualType retType, SourceLocation LBrac, SourceLocation RBrac,
872 Expr **ArgExprs)
Steve Naroffbcfb06a2007-09-28 22:22:11 +0000873 : Expr(ObjCMessageExprClass, retType), SelName(selInfo), ClassName(0) {
874 unsigned numArgs = selInfo.getNumArgs();
Steve Naroff68d331a2007-09-27 14:38:14 +0000875 SubExprs = new Expr*[numArgs+1];
876 SubExprs[RECEIVER] = receiver;
877 if (numArgs) {
878 for (unsigned i = 0; i != numArgs; ++i)
879 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
880 }
Steve Naroff563477d2007-09-18 23:55:05 +0000881 LBracloc = LBrac;
882 RBracloc = RBrac;
883}
884
Steve Naroff68d331a2007-09-27 14:38:14 +0000885// constructor for class messages.
886// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +0000887ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Steve Naroff68d331a2007-09-27 14:38:14 +0000888 QualType retType, SourceLocation LBrac, SourceLocation RBrac,
889 Expr **ArgExprs)
Steve Naroffbcfb06a2007-09-28 22:22:11 +0000890 : Expr(ObjCMessageExprClass, retType), SelName(selInfo), ClassName(clsName) {
891 unsigned numArgs = selInfo.getNumArgs();
Steve Naroff68d331a2007-09-27 14:38:14 +0000892 SubExprs = new Expr*[numArgs+1];
Steve Naroff563477d2007-09-18 23:55:05 +0000893 SubExprs[RECEIVER] = 0;
Steve Naroff68d331a2007-09-27 14:38:14 +0000894 if (numArgs) {
895 for (unsigned i = 0; i != numArgs; ++i)
896 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
897 }
Steve Naroff563477d2007-09-18 23:55:05 +0000898 LBracloc = LBrac;
899 RBracloc = RBrac;
900}
901
Chris Lattner27437ca2007-10-25 00:29:32 +0000902
903bool ChooseExpr::isConditionTrue(ASTContext &C) const {
904 llvm::APSInt CondVal(32);
905 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
906 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
907 return CondVal != 0;
908}
909
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000910//===----------------------------------------------------------------------===//
911// Child Iterators for iterating over subexpressions/substatements
912//===----------------------------------------------------------------------===//
913
914// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +0000915Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
916Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000917
918// PreDefinedExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +0000919Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
920Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000921
922// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +0000923Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
924Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000925
926// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +0000927Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
928Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000929
930// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +0000931Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
932Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000933
Chris Lattner5d661452007-08-26 03:42:43 +0000934// ImaginaryLiteral
935Stmt::child_iterator ImaginaryLiteral::child_begin() {
936 return reinterpret_cast<Stmt**>(&Val);
937}
938Stmt::child_iterator ImaginaryLiteral::child_end() {
939 return reinterpret_cast<Stmt**>(&Val)+1;
940}
941
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000942// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +0000943Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
944Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000945
946// ParenExpr
947Stmt::child_iterator ParenExpr::child_begin() {
948 return reinterpret_cast<Stmt**>(&Val);
949}
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000950Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000951 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000952}
953
954// UnaryOperator
955Stmt::child_iterator UnaryOperator::child_begin() {
956 return reinterpret_cast<Stmt**>(&Val);
957}
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000958Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000959 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000960}
961
962// SizeOfAlignOfTypeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +0000963Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
964 return child_iterator();
965}
966Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
967 return child_iterator();
968}
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000969
970// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +0000971Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000972 return reinterpret_cast<Stmt**>(&SubExprs);
973}
Ted Kremenek1237c672007-08-24 20:06:47 +0000974Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000975 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000976}
977
978// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +0000979Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek42a29772007-08-27 21:11:44 +0000980 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000981}
Ted Kremenek1237c672007-08-24 20:06:47 +0000982Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek42a29772007-08-27 21:11:44 +0000983 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000984}
Ted Kremenek1237c672007-08-24 20:06:47 +0000985
986// MemberExpr
987Stmt::child_iterator MemberExpr::child_begin() {
988 return reinterpret_cast<Stmt**>(&Base);
989}
Ted Kremenek1237c672007-08-24 20:06:47 +0000990Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000991 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000992}
993
994// OCUVectorElementExpr
995Stmt::child_iterator OCUVectorElementExpr::child_begin() {
996 return reinterpret_cast<Stmt**>(&Base);
997}
Ted Kremenek1237c672007-08-24 20:06:47 +0000998Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000999 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001000}
1001
1002// CompoundLiteralExpr
1003Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1004 return reinterpret_cast<Stmt**>(&Init);
1005}
Ted Kremenek1237c672007-08-24 20:06:47 +00001006Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001007 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001008}
1009
1010// ImplicitCastExpr
1011Stmt::child_iterator ImplicitCastExpr::child_begin() {
1012 return reinterpret_cast<Stmt**>(&Op);
1013}
Ted Kremenek1237c672007-08-24 20:06:47 +00001014Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001015 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001016}
1017
1018// CastExpr
1019Stmt::child_iterator CastExpr::child_begin() {
1020 return reinterpret_cast<Stmt**>(&Op);
1021}
Ted Kremenek1237c672007-08-24 20:06:47 +00001022Stmt::child_iterator CastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001023 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001024}
1025
1026// BinaryOperator
1027Stmt::child_iterator BinaryOperator::child_begin() {
1028 return reinterpret_cast<Stmt**>(&SubExprs);
1029}
Ted Kremenek1237c672007-08-24 20:06:47 +00001030Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001031 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001032}
1033
1034// ConditionalOperator
1035Stmt::child_iterator ConditionalOperator::child_begin() {
1036 return reinterpret_cast<Stmt**>(&SubExprs);
1037}
Ted Kremenek1237c672007-08-24 20:06:47 +00001038Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001039 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001040}
1041
1042// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001043Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1044Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001045
Ted Kremenek1237c672007-08-24 20:06:47 +00001046// StmtExpr
1047Stmt::child_iterator StmtExpr::child_begin() {
1048 return reinterpret_cast<Stmt**>(&SubStmt);
1049}
Ted Kremenek1237c672007-08-24 20:06:47 +00001050Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001051 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001052}
1053
1054// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001055Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1056 return child_iterator();
1057}
1058
1059Stmt::child_iterator TypesCompatibleExpr::child_end() {
1060 return child_iterator();
1061}
Ted Kremenek1237c672007-08-24 20:06:47 +00001062
1063// ChooseExpr
1064Stmt::child_iterator ChooseExpr::child_begin() {
1065 return reinterpret_cast<Stmt**>(&SubExprs);
1066}
1067
1068Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001069 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001070}
1071
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001072// VAArgExpr
1073Stmt::child_iterator VAArgExpr::child_begin() {
1074 return reinterpret_cast<Stmt**>(&Val);
1075}
1076
1077Stmt::child_iterator VAArgExpr::child_end() {
1078 return reinterpret_cast<Stmt**>(&Val)+1;
1079}
1080
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001081// InitListExpr
1082Stmt::child_iterator InitListExpr::child_begin() {
1083 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1084}
1085Stmt::child_iterator InitListExpr::child_end() {
1086 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1087}
1088
Ted Kremenek1237c672007-08-24 20:06:47 +00001089// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001090Stmt::child_iterator ObjCStringLiteral::child_begin() {
1091 return child_iterator();
1092}
1093Stmt::child_iterator ObjCStringLiteral::child_end() {
1094 return child_iterator();
1095}
Ted Kremenek1237c672007-08-24 20:06:47 +00001096
1097// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001098Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1099Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001100
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001101// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001102Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1103 return child_iterator();
1104}
1105Stmt::child_iterator ObjCSelectorExpr::child_end() {
1106 return child_iterator();
1107}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001108
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001109// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001110Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1111 return child_iterator();
1112}
1113Stmt::child_iterator ObjCProtocolExpr::child_end() {
1114 return child_iterator();
1115}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001116
Steve Naroff563477d2007-09-18 23:55:05 +00001117// ObjCMessageExpr
1118Stmt::child_iterator ObjCMessageExpr::child_begin() {
1119 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1120}
1121Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff68d331a2007-09-27 14:38:14 +00001122 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroff563477d2007-09-18 23:55:05 +00001123}
1124