blob: 479b651d1fcae7c882fe7b47760b7c4a2e2a5d51 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
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
Nate Begeman9f3bfb72008-01-17 17:46:27 +000081
Chris Lattner4b009652007-07-25 00:24:17 +000082CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
83 SourceLocation rparenloc)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000084 : Expr(CallExprClass, t), NumArgs(numargs) {
85 SubExprs = new Expr*[numargs+1];
86 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +000087 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000088 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +000089 RParenLoc = rparenloc;
90}
91
Chris Lattnerc257c0d2007-12-28 05:25:02 +000092/// setNumArgs - This changes the number of arguments present in this call.
93/// Any orphaned expressions are deleted by this, and any new operands are set
94/// to null.
95void CallExpr::setNumArgs(unsigned NumArgs) {
96 // No change, just return.
97 if (NumArgs == getNumArgs()) return;
98
99 // If shrinking # arguments, just delete the extras and forgot them.
100 if (NumArgs < getNumArgs()) {
101 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
102 delete getArg(i);
103 this->NumArgs = NumArgs;
104 return;
105 }
106
107 // Otherwise, we are growing the # arguments. New an bigger argument array.
108 Expr **NewSubExprs = new Expr*[NumArgs+1];
109 // Copy over args.
110 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
111 NewSubExprs[i] = SubExprs[i];
112 // Null out new args.
113 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
114 NewSubExprs[i] = 0;
115
116 delete[] SubExprs;
117 SubExprs = NewSubExprs;
118 this->NumArgs = NumArgs;
119}
120
Steve Naroff44aec4c2008-01-31 01:07:12 +0000121bool CallExpr::isBuiltinConstantExpr() const {
122 // All simple function calls (e.g. func()) are implicitly cast to pointer to
123 // function. As a result, we try and obtain the DeclRefExpr from the
124 // ImplicitCastExpr.
125 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
126 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
127 return false;
128
129 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
130 if (!DRE)
131 return false;
132
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000133 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
134 if (!FDecl)
135 return false;
136
137 unsigned builtinID = FDecl->getIdentifier()->getBuiltinID();
138 if (!builtinID)
139 return false;
140
141 // We have a builtin that is a constant expression
142 if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString)
Steve Naroff44aec4c2008-01-31 01:07:12 +0000143 return true;
144 return false;
145}
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000146
Steve Naroff8d3b1702007-08-08 22:15:55 +0000147bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
148 // The following enum mimics gcc's internal "typeclass.h" file.
149 enum gcc_type_class {
150 no_type_class = -1,
151 void_type_class, integer_type_class, char_type_class,
152 enumeral_type_class, boolean_type_class,
153 pointer_type_class, reference_type_class, offset_type_class,
154 real_type_class, complex_type_class,
155 function_type_class, method_type_class,
156 record_type_class, union_type_class,
157 array_type_class, string_type_class,
158 lang_type_class
159 };
160 Result.setIsSigned(true);
161
162 // All simple function calls (e.g. func()) are implicitly cast to pointer to
163 // function. As a result, we try and obtain the DeclRefExpr from the
164 // ImplicitCastExpr.
165 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
166 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
167 return false;
168 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
169 if (!DRE)
170 return false;
171
172 // We have a DeclRefExpr.
173 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
174 // If no argument was supplied, default to "no_type_class". This isn't
175 // ideal, however it's what gcc does.
176 Result = static_cast<uint64_t>(no_type_class);
177 if (NumArgs >= 1) {
178 QualType argType = getArg(0)->getType();
179
180 if (argType->isVoidType())
181 Result = void_type_class;
182 else if (argType->isEnumeralType())
183 Result = enumeral_type_class;
184 else if (argType->isBooleanType())
185 Result = boolean_type_class;
186 else if (argType->isCharType())
187 Result = string_type_class; // gcc doesn't appear to use char_type_class
188 else if (argType->isIntegerType())
189 Result = integer_type_class;
190 else if (argType->isPointerType())
191 Result = pointer_type_class;
192 else if (argType->isReferenceType())
193 Result = reference_type_class;
194 else if (argType->isRealType())
195 Result = real_type_class;
196 else if (argType->isComplexType())
197 Result = complex_type_class;
198 else if (argType->isFunctionType())
199 Result = function_type_class;
200 else if (argType->isStructureType())
201 Result = record_type_class;
202 else if (argType->isUnionType())
203 Result = union_type_class;
204 else if (argType->isArrayType())
205 Result = array_type_class;
206 else if (argType->isUnionType())
207 Result = union_type_class;
208 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner19b8f1a2007-11-08 17:56:40 +0000209 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff8d3b1702007-08-08 22:15:55 +0000210 }
211 return true;
212 }
213 return false;
214}
215
Chris Lattner4b009652007-07-25 00:24:17 +0000216/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
217/// corresponds to, e.g. "<<=".
218const char *BinaryOperator::getOpcodeStr(Opcode Op) {
219 switch (Op) {
220 default: assert(0 && "Unknown binary operator");
221 case Mul: return "*";
222 case Div: return "/";
223 case Rem: return "%";
224 case Add: return "+";
225 case Sub: return "-";
226 case Shl: return "<<";
227 case Shr: return ">>";
228 case LT: return "<";
229 case GT: return ">";
230 case LE: return "<=";
231 case GE: return ">=";
232 case EQ: return "==";
233 case NE: return "!=";
234 case And: return "&";
235 case Xor: return "^";
236 case Or: return "|";
237 case LAnd: return "&&";
238 case LOr: return "||";
239 case Assign: return "=";
240 case MulAssign: return "*=";
241 case DivAssign: return "/=";
242 case RemAssign: return "%=";
243 case AddAssign: return "+=";
244 case SubAssign: return "-=";
245 case ShlAssign: return "<<=";
246 case ShrAssign: return ">>=";
247 case AndAssign: return "&=";
248 case XorAssign: return "^=";
249 case OrAssign: return "|=";
250 case Comma: return ",";
251 }
252}
253
Anders Carlsson762b7c72007-08-31 04:56:16 +0000254InitListExpr::InitListExpr(SourceLocation lbraceloc,
255 Expr **initexprs, unsigned numinits,
256 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000257 : Expr(InitListExprClass, QualType()),
258 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc)
Anders Carlsson762b7c72007-08-31 04:56:16 +0000259{
Anders Carlsson762b7c72007-08-31 04:56:16 +0000260 for (unsigned i = 0; i != numinits; i++)
Steve Naroff2e335472008-05-01 02:04:18 +0000261 InitExprs.push_back(initexprs[i]);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000262}
Chris Lattner4b009652007-07-25 00:24:17 +0000263
264//===----------------------------------------------------------------------===//
265// Generic Expression Routines
266//===----------------------------------------------------------------------===//
267
268/// hasLocalSideEffect - Return true if this immediate expression has side
269/// effects, not counting any sub-expressions.
270bool Expr::hasLocalSideEffect() const {
271 switch (getStmtClass()) {
272 default:
273 return false;
274 case ParenExprClass:
275 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
276 case UnaryOperatorClass: {
277 const UnaryOperator *UO = cast<UnaryOperator>(this);
278
279 switch (UO->getOpcode()) {
280 default: return false;
281 case UnaryOperator::PostInc:
282 case UnaryOperator::PostDec:
283 case UnaryOperator::PreInc:
284 case UnaryOperator::PreDec:
285 return true; // ++/--
286
287 case UnaryOperator::Deref:
288 // Dereferencing a volatile pointer is a side-effect.
289 return getType().isVolatileQualified();
290 case UnaryOperator::Real:
291 case UnaryOperator::Imag:
292 // accessing a piece of a volatile complex is a side-effect.
293 return UO->getSubExpr()->getType().isVolatileQualified();
294
295 case UnaryOperator::Extension:
296 return UO->getSubExpr()->hasLocalSideEffect();
297 }
298 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000299 case BinaryOperatorClass: {
300 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
301 // Consider comma to have side effects if the LHS and RHS both do.
302 if (BinOp->getOpcode() == BinaryOperator::Comma)
303 return BinOp->getLHS()->hasLocalSideEffect() &&
304 BinOp->getRHS()->hasLocalSideEffect();
305
306 return BinOp->isAssignmentOp();
307 }
Chris Lattner06078d22007-08-25 02:00:02 +0000308 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000309 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000310
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000311 case ConditionalOperatorClass: {
312 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
313 return Exp->getCond()->hasLocalSideEffect()
314 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
315 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
316 }
317
Chris Lattner4b009652007-07-25 00:24:17 +0000318 case MemberExprClass:
319 case ArraySubscriptExprClass:
320 // If the base pointer or element is to a volatile pointer/field, accessing
321 // if is a side effect.
322 return getType().isVolatileQualified();
323
324 case CallExprClass:
325 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
326 // should warn.
327 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000328 case ObjCMessageExprClass:
329 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000330
331 case CastExprClass:
332 // If this is a cast to void, check the operand. Otherwise, the result of
333 // the cast is unused.
334 if (getType()->isVoidType())
335 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
336 return false;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000337
338 case CXXDefaultArgExprClass:
339 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect();
Chris Lattner4b009652007-07-25 00:24:17 +0000340 }
341}
342
343/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
344/// incomplete type other than void. Nonarray expressions that can be lvalues:
345/// - name, where name must be a variable
346/// - e[i]
347/// - (e), where e must be an lvalue
348/// - e.name, where e must be an lvalue
349/// - e->name
350/// - *e, the type of e cannot be a function type
351/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000352/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000353/// - reference type [C++ [expr]]
354///
355Expr::isLvalueResult Expr::isLvalue() const {
356 // first, check the type (C99 6.3.2.1)
357 if (TR->isFunctionType()) // from isObjectType()
358 return LV_NotObjectType;
359
Steve Naroffec7736d2008-02-10 01:39:04 +0000360 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner35fef522008-02-20 20:55:12 +0000361 if (TR->isVoidType() && !TR.getCanonicalType().getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000362 return LV_IncompleteVoidType;
363
Chris Lattner4b009652007-07-25 00:24:17 +0000364 if (TR->isReferenceType()) // C++ [expr]
365 return LV_Valid;
366
367 // the type looks fine, now check the expression
368 switch (getStmtClass()) {
369 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000370 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000371 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
372 // For vectors, make sure base is an lvalue (i.e. not a function call).
373 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
374 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
375 return LV_Valid;
376 case DeclRefExprClass: // C99 6.5.1p2
377 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
378 return LV_Valid;
379 break;
380 case MemberExprClass: { // C99 6.5.2.3p4
381 const MemberExpr *m = cast<MemberExpr>(this);
382 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
383 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000384 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000385 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000386 return LV_Valid; // C99 6.5.3p4
387
388 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
389 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
390 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000391 break;
392 case ParenExprClass: // C99 6.5.1p5
393 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffc7c66532007-12-05 04:00:10 +0000394 case CompoundLiteralExprClass: // C99 6.5.2.5p5
395 return LV_Valid;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000396 case ExtVectorElementExprClass:
397 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000398 return LV_DuplicateVectorComponents;
399 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000400 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
401 return LV_Valid;
Chris Lattner7e637512008-01-12 08:14:25 +0000402 case PreDefinedExprClass:
403 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000404 case CXXDefaultArgExprClass:
405 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue();
Chris Lattner4b009652007-07-25 00:24:17 +0000406 default:
407 break;
408 }
409 return LV_InvalidExpression;
410}
411
412/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
413/// does not have an incomplete type, does not have a const-qualified type, and
414/// if it is a structure or union, does not have any member (including,
415/// recursively, any member or element of all contained aggregates or unions)
416/// with a const-qualified type.
417Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
418 isLvalueResult lvalResult = isLvalue();
419
420 switch (lvalResult) {
421 case LV_Valid: break;
422 case LV_NotObjectType: return MLV_NotObjectType;
423 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000424 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000425 case LV_InvalidExpression: return MLV_InvalidExpression;
426 }
427 if (TR.isConstQualified())
428 return MLV_ConstQualified;
429 if (TR->isArrayType())
430 return MLV_ArrayType;
431 if (TR->isIncompleteType())
432 return MLV_IncompleteType;
433
434 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
435 if (r->hasConstFields())
436 return MLV_ConstQualified;
437 }
438 return MLV_Valid;
439}
440
Ted Kremenek5778d622008-02-27 18:39:48 +0000441/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000442/// duration. This means that the address of this expression is a link-time
443/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000444bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000445 switch (getStmtClass()) {
446 default:
447 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000448 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000449 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000450 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000451 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000452 case CompoundLiteralExprClass:
453 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000454 case DeclRefExprClass: {
455 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
456 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000457 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000458 if (isa<FunctionDecl>(D))
459 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000460 return false;
461 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000462 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000463 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000464 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000465 }
Chris Lattner743ec372007-11-27 21:35:27 +0000466 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000467 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner7e637512008-01-12 08:14:25 +0000468 case PreDefinedExprClass:
469 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000470 case CXXDefaultArgExprClass:
471 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000472 }
473}
474
Ted Kremenek87e30c52008-01-17 16:57:34 +0000475Expr* Expr::IgnoreParens() {
476 Expr* E = this;
477 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
478 E = P->getSubExpr();
479
480 return E;
481}
482
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000483/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
484/// or CastExprs or ImplicitCastExprs, returning their operand.
485Expr *Expr::IgnoreParenCasts() {
486 Expr *E = this;
487 while (true) {
488 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
489 E = P->getSubExpr();
490 else if (CastExpr *P = dyn_cast<CastExpr>(E))
491 E = P->getSubExpr();
492 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
493 E = P->getSubExpr();
494 else
495 return E;
496 }
497}
498
499
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000500bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000501 switch (getStmtClass()) {
502 default:
503 if (Loc) *Loc = getLocStart();
504 return false;
505 case ParenExprClass:
506 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
507 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000508 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000509 case FloatingLiteralClass:
510 case IntegerLiteralClass:
511 case CharacterLiteralClass:
512 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000513 case TypesCompatibleExprClass:
514 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000515 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000516 case CallExprClass: {
517 const CallExpr *CE = cast<CallExpr>(this);
518 llvm::APSInt Result(32);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000519 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000520 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000521 return true;
Steve Naroff44aec4c2008-01-31 01:07:12 +0000522 if (CE->isBuiltinConstantExpr())
523 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000524 if (Loc) *Loc = getLocStart();
525 return false;
526 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000527 case DeclRefExprClass: {
528 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
529 // Accept address of function.
530 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000531 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000532 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000533 if (isa<VarDecl>(D))
534 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000535 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000536 }
Steve Narofff91f9722008-01-09 00:05:37 +0000537 case CompoundLiteralExprClass:
538 if (Loc) *Loc = getLocStart();
539 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemanc4e28e42008-01-25 05:34:48 +0000540 // Allow "(vector type){2,4}" since the elements are all constant.
541 return TR->isArrayType() || TR->isVectorType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000542 case UnaryOperatorClass: {
543 const UnaryOperator *Exp = cast<UnaryOperator>(this);
544
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000545 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000546 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
Ted Kremenek5778d622008-02-27 18:39:48 +0000547 if (!Exp->getSubExpr()->hasGlobalStorage()) {
Chris Lattner35b662f2007-12-11 23:11:17 +0000548 if (Loc) *Loc = getLocStart();
549 return false;
550 }
551 return true;
552 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000553
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000554 // Get the operand value. If this is sizeof/alignof, do not evalute the
555 // operand. This affects C99 6.6p3.
Steve Narofff0b23542008-01-10 22:15:12 +0000556 if (!Exp->isSizeOfAlignOfOp() &&
557 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000558 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
559 return false;
560
561 switch (Exp->getOpcode()) {
562 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
563 // See C99 6.6p3.
564 default:
565 if (Loc) *Loc = Exp->getOperatorLoc();
566 return false;
567 case UnaryOperator::Extension:
568 return true; // FIXME: this is wrong.
569 case UnaryOperator::SizeOf:
570 case UnaryOperator::AlignOf:
Steve Narofff0b23542008-01-10 22:15:12 +0000571 case UnaryOperator::OffsetOf:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000572 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000573 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000574 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000575 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000576 }
Chris Lattner06db6132007-10-18 00:20:32 +0000577 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000578 case UnaryOperator::LNot:
579 case UnaryOperator::Plus:
580 case UnaryOperator::Minus:
581 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000582 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000583 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000584 }
585 case SizeOfAlignOfTypeExprClass: {
586 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
587 // alignof always evaluates to a constant.
Chris Lattner20515462008-02-21 05:45:29 +0000588 if (Exp->isSizeOf() && !Exp->getArgumentType()->isVoidType() &&
589 !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000590 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000591 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000592 }
Chris Lattner06db6132007-10-18 00:20:32 +0000593 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000594 }
595 case BinaryOperatorClass: {
596 const BinaryOperator *Exp = cast<BinaryOperator>(this);
597
598 // The LHS of a constant expr is always evaluated and needed.
599 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
600 return false;
601
602 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
603 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000604 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000605 }
606 case ImplicitCastExprClass:
607 case CastExprClass: {
608 const Expr *SubExpr;
609 SourceLocation CastLoc;
610 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
611 SubExpr = C->getSubExpr();
612 CastLoc = C->getLParenLoc();
613 } else {
614 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
615 CastLoc = getLocStart();
616 }
617 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
618 if (Loc) *Loc = SubExpr->getLocStart();
619 return false;
620 }
Chris Lattner06db6132007-10-18 00:20:32 +0000621 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000622 }
623 case ConditionalOperatorClass: {
624 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000625 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000626 // Handle the GNU extension for missing LHS.
627 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000628 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000629 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000630 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000631 }
Steve Narofff0b23542008-01-10 22:15:12 +0000632 case InitListExprClass: {
633 const InitListExpr *Exp = cast<InitListExpr>(this);
634 unsigned numInits = Exp->getNumInits();
635 for (unsigned i = 0; i < numInits; i++) {
636 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
637 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
638 return false;
639 }
640 }
641 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000642 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000643 case CXXDefaultArgExprClass:
644 return cast<CXXDefaultArgExpr>(this)->getExpr()->isConstantExpr(Ctx, Loc);
Steve Narofff0b23542008-01-10 22:15:12 +0000645 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000646}
647
Chris Lattner4b009652007-07-25 00:24:17 +0000648/// isIntegerConstantExpr - this recursive routine will test if an expression is
649/// an integer constant expression. Note: With the introduction of VLA's in
650/// C99 the result of the sizeof operator is no longer always a constant
651/// expression. The generalization of the wording to include any subexpression
652/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
653/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
654/// "0 || f()" can be treated as a constant expression. In C90 this expression,
655/// occurring in a context requiring a constant, would have been a constraint
656/// violation. FIXME: This routine currently implements C90 semantics.
657/// To properly implement C99 semantics this routine will need to evaluate
658/// expressions involving operators previously mentioned.
659
660/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
661/// comma, etc
662///
663/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000664/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000665///
666/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
667/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
668/// cast+dereference.
669bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
670 SourceLocation *Loc, bool isEvaluated) const {
671 switch (getStmtClass()) {
672 default:
673 if (Loc) *Loc = getLocStart();
674 return false;
675 case ParenExprClass:
676 return cast<ParenExpr>(this)->getSubExpr()->
677 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
678 case IntegerLiteralClass:
679 Result = cast<IntegerLiteral>(this)->getValue();
680 break;
681 case CharacterLiteralClass: {
682 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000683 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000684 Result = CL->getValue();
685 Result.setIsUnsigned(!getType()->isSignedIntegerType());
686 break;
687 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000688 case TypesCompatibleExprClass: {
689 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000690 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000691 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000692 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000693 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000694 case CallExprClass: {
695 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000696 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000697 if (CE->isBuiltinClassifyType(Result))
698 break;
699 if (Loc) *Loc = getLocStart();
700 return false;
701 }
Chris Lattner4b009652007-07-25 00:24:17 +0000702 case DeclRefExprClass:
703 if (const EnumConstantDecl *D =
704 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
705 Result = D->getInitVal();
706 break;
707 }
708 if (Loc) *Loc = getLocStart();
709 return false;
710 case UnaryOperatorClass: {
711 const UnaryOperator *Exp = cast<UnaryOperator>(this);
712
713 // Get the operand value. If this is sizeof/alignof, do not evalute the
714 // operand. This affects C99 6.6p3.
Anders Carlsson52774ad2008-01-29 15:56:48 +0000715 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner5a9b6242007-08-23 21:42:50 +0000716 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000717 return false;
718
719 switch (Exp->getOpcode()) {
720 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
721 // See C99 6.6p3.
722 default:
723 if (Loc) *Loc = Exp->getOperatorLoc();
724 return false;
725 case UnaryOperator::Extension:
726 return true; // FIXME: this is wrong.
727 case UnaryOperator::SizeOf:
728 case UnaryOperator::AlignOf:
Chris Lattner20515462008-02-21 05:45:29 +0000729 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000730 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000731
732 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
733 if (Exp->getSubExpr()->getType()->isVoidType()) {
734 Result = 1;
735 break;
736 }
737
Chris Lattner4b009652007-07-25 00:24:17 +0000738 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000739 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000740 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000741 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000742 }
Chris Lattner4b009652007-07-25 00:24:17 +0000743
Chris Lattner4b009652007-07-25 00:24:17 +0000744 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000745 if (Exp->getSubExpr()->getType()->isFunctionType()) {
746 // GCC extension: sizeof(function) = 1.
747 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000748 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000749 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson1f86b032008-02-18 07:10:45 +0000750 if (Exp->getOpcode() == UnaryOperator::AlignOf)
Chris Lattner8cd0e932008-03-05 18:54:05 +0000751 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType()) / CharSize;
Anders Carlsson1f86b032008-02-18 07:10:45 +0000752 else
Chris Lattner8cd0e932008-03-05 18:54:05 +0000753 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType()) / CharSize;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000754 }
Chris Lattner4b009652007-07-25 00:24:17 +0000755 break;
756 case UnaryOperator::LNot: {
Chris Lattnerf00fdc02008-01-25 19:16:19 +0000757 bool Val = Result == 0;
Chris Lattner8cd0e932008-03-05 18:54:05 +0000758 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000759 Result = Val;
760 break;
761 }
762 case UnaryOperator::Plus:
763 break;
764 case UnaryOperator::Minus:
765 Result = -Result;
766 break;
767 case UnaryOperator::Not:
768 Result = ~Result;
769 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000770 case UnaryOperator::OffsetOf:
771 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000772 }
773 break;
774 }
775 case SizeOfAlignOfTypeExprClass: {
776 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
Chris Lattner20515462008-02-21 05:45:29 +0000777
778 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000779 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000780
781 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
782 if (Exp->getArgumentType()->isVoidType()) {
783 Result = 1;
784 break;
785 }
786
787 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000788 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000789 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000790 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000791 }
Chris Lattner4b009652007-07-25 00:24:17 +0000792
Chris Lattner4b009652007-07-25 00:24:17 +0000793 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000794 if (Exp->getArgumentType()->isFunctionType()) {
795 // GCC extension: sizeof(function) = 1.
796 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000797 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000798 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000799 if (Exp->isSizeOf())
Chris Lattner8cd0e932008-03-05 18:54:05 +0000800 Result = Ctx.getTypeSize(Exp->getArgumentType()) / CharSize;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000801 else
Chris Lattner8cd0e932008-03-05 18:54:05 +0000802 Result = Ctx.getTypeAlign(Exp->getArgumentType()) / CharSize;
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000803 }
Chris Lattner4b009652007-07-25 00:24:17 +0000804 break;
805 }
806 case BinaryOperatorClass: {
807 const BinaryOperator *Exp = cast<BinaryOperator>(this);
808
809 // The LHS of a constant expr is always evaluated and needed.
810 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
811 return false;
812
813 llvm::APSInt RHS(Result);
814
815 // The short-circuiting &&/|| operators don't necessarily evaluate their
816 // RHS. Make sure to pass isEvaluated down correctly.
817 if (Exp->isLogicalOp()) {
818 bool RHSEval;
819 if (Exp->getOpcode() == BinaryOperator::LAnd)
820 RHSEval = Result != 0;
821 else {
822 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
823 RHSEval = Result == 0;
824 }
825
826 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
827 isEvaluated & RHSEval))
828 return false;
829 } else {
830 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
831 return false;
832 }
833
834 switch (Exp->getOpcode()) {
835 default:
836 if (Loc) *Loc = getLocStart();
837 return false;
838 case BinaryOperator::Mul:
839 Result *= RHS;
840 break;
841 case BinaryOperator::Div:
842 if (RHS == 0) {
843 if (!isEvaluated) break;
844 if (Loc) *Loc = getLocStart();
845 return false;
846 }
847 Result /= RHS;
848 break;
849 case BinaryOperator::Rem:
850 if (RHS == 0) {
851 if (!isEvaluated) break;
852 if (Loc) *Loc = getLocStart();
853 return false;
854 }
855 Result %= RHS;
856 break;
857 case BinaryOperator::Add: Result += RHS; break;
858 case BinaryOperator::Sub: Result -= RHS; break;
859 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000860 Result <<=
861 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000862 break;
863 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000864 Result >>=
865 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000866 break;
867 case BinaryOperator::LT: Result = Result < RHS; break;
868 case BinaryOperator::GT: Result = Result > RHS; break;
869 case BinaryOperator::LE: Result = Result <= RHS; break;
870 case BinaryOperator::GE: Result = Result >= RHS; break;
871 case BinaryOperator::EQ: Result = Result == RHS; break;
872 case BinaryOperator::NE: Result = Result != RHS; break;
873 case BinaryOperator::And: Result &= RHS; break;
874 case BinaryOperator::Xor: Result ^= RHS; break;
875 case BinaryOperator::Or: Result |= RHS; break;
876 case BinaryOperator::LAnd:
877 Result = Result != 0 && RHS != 0;
878 break;
879 case BinaryOperator::LOr:
880 Result = Result != 0 || RHS != 0;
881 break;
882
883 case BinaryOperator::Comma:
884 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
885 // *except* when they are contained within a subexpression that is not
886 // evaluated". Note that Assignment can never happen due to constraints
887 // on the LHS subexpr, so we don't need to check it here.
888 if (isEvaluated) {
889 if (Loc) *Loc = getLocStart();
890 return false;
891 }
892
893 // The result of the constant expr is the RHS.
894 Result = RHS;
895 return true;
896 }
897
898 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
899 break;
900 }
901 case ImplicitCastExprClass:
902 case CastExprClass: {
903 const Expr *SubExpr;
904 SourceLocation CastLoc;
905 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
906 SubExpr = C->getSubExpr();
907 CastLoc = C->getLParenLoc();
908 } else {
909 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
910 CastLoc = getLocStart();
911 }
912
913 // C99 6.6p6: shall only convert arithmetic types to integer types.
914 if (!SubExpr->getType()->isArithmeticType() ||
915 !getType()->isIntegerType()) {
916 if (Loc) *Loc = SubExpr->getLocStart();
917 return false;
918 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000919
Chris Lattner8cd0e932008-03-05 18:54:05 +0000920 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000921
Chris Lattner4b009652007-07-25 00:24:17 +0000922 // Handle simple integer->integer casts.
923 if (SubExpr->getType()->isIntegerType()) {
924 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
925 return false;
926
927 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000928 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +0000929 if (getType()->isBooleanType()) {
930 // Conversion to bool compares against zero.
931 Result = Result != 0;
932 Result.zextOrTrunc(DestWidth);
933 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +0000934 Result.sextOrTrunc(DestWidth);
935 else // If the input is unsigned, do a zero extend, noop, or truncate.
936 Result.zextOrTrunc(DestWidth);
937 break;
938 }
939
940 // Allow floating constants that are the immediate operands of casts or that
941 // are parenthesized.
942 const Expr *Operand = SubExpr;
943 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
944 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000945
946 // If this isn't a floating literal, we can't handle it.
947 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
948 if (!FL) {
949 if (Loc) *Loc = Operand->getLocStart();
950 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000951 }
Chris Lattner000c4102008-01-09 18:59:34 +0000952
953 // If the destination is boolean, compare against zero.
954 if (getType()->isBooleanType()) {
955 Result = !FL->getValue().isZero();
956 Result.zextOrTrunc(DestWidth);
957 break;
958 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000959
960 // Determine whether we are converting to unsigned or signed.
961 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000962
963 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
964 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000965 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000966 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
967 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000968 Result = llvm::APInt(DestWidth, 4, Space);
969 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000970 }
971 case ConditionalOperatorClass: {
972 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
973
974 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
975 return false;
976
977 const Expr *TrueExp = Exp->getLHS();
978 const Expr *FalseExp = Exp->getRHS();
979 if (Result == 0) std::swap(TrueExp, FalseExp);
980
981 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000982 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000983 return false;
984 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000985 if (TrueExp &&
986 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000987 return false;
988 break;
989 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000990 case CXXDefaultArgExprClass:
991 return cast<CXXDefaultArgExpr>(this)
992 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Chris Lattner4b009652007-07-25 00:24:17 +0000993 }
994
995 // Cases that are valid constant exprs fall through to here.
996 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
997 return true;
998}
999
Chris Lattner4b009652007-07-25 00:24:17 +00001000/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1001/// integer constant expression with the value zero, or if this is one that is
1002/// cast to void*.
1003bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffa2e53222008-01-14 16:10:57 +00001004 // Strip off a cast to void*, if it exists.
1005 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
1006 // Check that it is a cast to void*.
Eli Friedmand899dbe2008-02-13 17:29:58 +00001007 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001008 QualType Pointee = PT->getPointeeType();
Chris Lattner35fef522008-02-20 20:55:12 +00001009 if (Pointee.getCVRQualifiers() == 0 &&
1010 Pointee->isVoidType() && // to void*
Steve Naroffa2e53222008-01-14 16:10:57 +00001011 CE->getSubExpr()->getType()->isIntegerType()) // from int.
1012 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001013 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001014 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1015 // Ignore the ImplicitCastExpr type entirely.
1016 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
1017 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1018 // Accept ((void*)0) as a null pointer constant, as many other
1019 // implementations do.
1020 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001021 } else if (const CXXDefaultArgExpr *DefaultArg
1022 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001023 // See through default argument expressions
1024 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Steve Narofff33a9852008-01-14 02:53:34 +00001025 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001026
1027 // This expression must be an integer type.
1028 if (!getType()->isIntegerType())
1029 return false;
1030
Chris Lattner4b009652007-07-25 00:24:17 +00001031 // If we have an integer constant expression, we need to *evaluate* it and
1032 // test for the value 0.
1033 llvm::APSInt Val(32);
Steve Naroffa2e53222008-01-14 16:10:57 +00001034 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001035}
Steve Naroffc11705f2007-07-28 23:10:27 +00001036
Nate Begemanaf6ed502008-04-18 23:10:10 +00001037unsigned ExtVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +00001038 return strlen(Accessor.getName());
1039}
1040
1041
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001042/// getComponentType - Determine whether the components of this access are
1043/// "point" "color" or "texture" elements.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001044ExtVectorElementExpr::ElementType
1045ExtVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +00001046 // derive the component type, no need to waste space.
1047 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +00001048
Nate Begemanaf6ed502008-04-18 23:10:10 +00001049 if (ExtVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
1050 if (ExtVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +00001051
Nate Begemanaf6ed502008-04-18 23:10:10 +00001052 assert(ExtVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +00001053 "getComponentType(): Illegal accessor");
1054 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +00001055}
Steve Naroffba67f692007-07-30 03:29:09 +00001056
Chris Lattnera0d03a72007-08-03 17:31:20 +00001057/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001058/// repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001059bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001060 const char *compStr = Accessor.getName();
1061 unsigned length = strlen(compStr);
1062
1063 for (unsigned i = 0; i < length-1; i++) {
1064 const char *s = compStr+i;
1065 for (const char c = *s++; *s; s++)
1066 if (c == *s)
1067 return true;
1068 }
1069 return false;
1070}
Chris Lattner42158e72007-08-02 23:36:59 +00001071
1072/// getEncodedElementAccess - We encode fields with two bits per component.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001073unsigned ExtVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +00001074 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +00001075 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +00001076
1077 unsigned Result = 0;
1078
1079 while (length--) {
1080 Result <<= 2;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001081 int Idx = ExtVectorType::getAccessorIdx(compStr[length]);
Chris Lattner42158e72007-08-02 23:36:59 +00001082 assert(Idx != -1 && "Invalid accessor letter");
1083 Result |= Idx;
1084 }
1085 return Result;
1086}
1087
Steve Naroff4ed9d662007-09-27 14:38:14 +00001088// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001089ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001090 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001091 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001092 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001093 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001094 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001095 NumArgs = nargs;
1096 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001097 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001098 if (NumArgs) {
1099 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001100 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1101 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001102 LBracloc = LBrac;
1103 RBracloc = RBrac;
1104}
1105
Steve Naroff4ed9d662007-09-27 14:38:14 +00001106// constructor for class messages.
1107// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001108ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001109 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001110 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001111 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001112 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001113 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001114 NumArgs = nargs;
1115 SubExprs = new Expr*[NumArgs+1];
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001116 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | 0x1);
Steve Naroff9f176d12007-11-15 13:05:42 +00001117 if (NumArgs) {
1118 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001119 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1120 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001121 LBracloc = LBrac;
1122 RBracloc = RBrac;
1123}
1124
Chris Lattnerf624cd22007-10-25 00:29:32 +00001125bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1126 llvm::APSInt CondVal(32);
1127 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1128 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1129 return CondVal != 0;
1130}
1131
Anders Carlsson52774ad2008-01-29 15:56:48 +00001132static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1133{
1134 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1135 QualType Ty = ME->getBase()->getType();
1136
1137 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner8cd0e932008-03-05 18:54:05 +00001138 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001139 FieldDecl *FD = ME->getMemberDecl();
1140
1141 // FIXME: This is linear time.
1142 unsigned i = 0, e = 0;
1143 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1144 if (RD->getMember(i) == FD)
1145 break;
1146 }
1147
1148 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1149 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1150 const Expr *Base = ASE->getBase();
1151 llvm::APSInt Idx(32);
1152 bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1153 assert(ICE && "Array index is not a constant integer!");
1154
Chris Lattner8cd0e932008-03-05 18:54:05 +00001155 int64_t size = C.getTypeSize(ASE->getType());
Anders Carlsson52774ad2008-01-29 15:56:48 +00001156 size *= Idx.getSExtValue();
1157
1158 return size + evaluateOffsetOf(C, Base);
1159 } else if (isa<CompoundLiteralExpr>(E))
1160 return 0;
1161
1162 assert(0 && "Unknown offsetof subexpression!");
1163 return 0;
1164}
1165
1166int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1167{
1168 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1169
Chris Lattner8cd0e932008-03-05 18:54:05 +00001170 unsigned CharSize = C.Target.getCharWidth();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001171 return ::evaluateOffsetOf(C, Val) / CharSize;
1172}
1173
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001174//===----------------------------------------------------------------------===//
1175// Child Iterators for iterating over subexpressions/substatements
1176//===----------------------------------------------------------------------===//
1177
1178// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001179Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1180Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001181
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001182// ObjCIvarRefExpr
1183Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1184Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1185
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001186// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001187Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1188Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001189
1190// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001191Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1192Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001193
1194// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001195Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1196Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001197
1198// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001199Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1200Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001201
Chris Lattner1de66eb2007-08-26 03:42:43 +00001202// ImaginaryLiteral
1203Stmt::child_iterator ImaginaryLiteral::child_begin() {
1204 return reinterpret_cast<Stmt**>(&Val);
1205}
1206Stmt::child_iterator ImaginaryLiteral::child_end() {
1207 return reinterpret_cast<Stmt**>(&Val)+1;
1208}
1209
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001210// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001211Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1212Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001213
1214// ParenExpr
1215Stmt::child_iterator ParenExpr::child_begin() {
1216 return reinterpret_cast<Stmt**>(&Val);
1217}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001218Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001219 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001220}
1221
1222// UnaryOperator
1223Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001224 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001225}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001226Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001227 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001228}
1229
1230// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001231Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001232 // If the type is a VLA type (and not a typedef), the size expression of the
1233 // VLA needs to be treated as an executable expression.
1234 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1235 return child_iterator(T);
1236 else
1237 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001238}
1239Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001240 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001241}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001242
1243// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001244Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001245 return reinterpret_cast<Stmt**>(&SubExprs);
1246}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001247Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001248 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001249}
1250
1251// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001252Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001253 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001254}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001255Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001256 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001257}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001258
1259// MemberExpr
1260Stmt::child_iterator MemberExpr::child_begin() {
1261 return reinterpret_cast<Stmt**>(&Base);
1262}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001263Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001264 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001265}
1266
Nate Begemanaf6ed502008-04-18 23:10:10 +00001267// ExtVectorElementExpr
1268Stmt::child_iterator ExtVectorElementExpr::child_begin() {
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001269 return reinterpret_cast<Stmt**>(&Base);
1270}
Nate Begemanaf6ed502008-04-18 23:10:10 +00001271Stmt::child_iterator ExtVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001272 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001273}
1274
1275// CompoundLiteralExpr
1276Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1277 return reinterpret_cast<Stmt**>(&Init);
1278}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001279Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001280 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001281}
1282
1283// ImplicitCastExpr
1284Stmt::child_iterator ImplicitCastExpr::child_begin() {
1285 return reinterpret_cast<Stmt**>(&Op);
1286}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001287Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001288 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001289}
1290
1291// CastExpr
1292Stmt::child_iterator CastExpr::child_begin() {
1293 return reinterpret_cast<Stmt**>(&Op);
1294}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001295Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001296 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001297}
1298
1299// BinaryOperator
1300Stmt::child_iterator BinaryOperator::child_begin() {
1301 return reinterpret_cast<Stmt**>(&SubExprs);
1302}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001303Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001304 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001305}
1306
1307// ConditionalOperator
1308Stmt::child_iterator ConditionalOperator::child_begin() {
1309 return reinterpret_cast<Stmt**>(&SubExprs);
1310}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001311Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001312 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001313}
1314
1315// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001316Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1317Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001318
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001319// StmtExpr
1320Stmt::child_iterator StmtExpr::child_begin() {
1321 return reinterpret_cast<Stmt**>(&SubStmt);
1322}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001323Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001324 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001325}
1326
1327// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001328Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1329 return child_iterator();
1330}
1331
1332Stmt::child_iterator TypesCompatibleExpr::child_end() {
1333 return child_iterator();
1334}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001335
1336// ChooseExpr
1337Stmt::child_iterator ChooseExpr::child_begin() {
1338 return reinterpret_cast<Stmt**>(&SubExprs);
1339}
1340
1341Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001342 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001343}
1344
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001345// OverloadExpr
1346Stmt::child_iterator OverloadExpr::child_begin() {
1347 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1348}
1349Stmt::child_iterator OverloadExpr::child_end() {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001350 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001351}
1352
Anders Carlsson36760332007-10-15 20:28:48 +00001353// VAArgExpr
1354Stmt::child_iterator VAArgExpr::child_begin() {
1355 return reinterpret_cast<Stmt**>(&Val);
1356}
1357
1358Stmt::child_iterator VAArgExpr::child_end() {
1359 return reinterpret_cast<Stmt**>(&Val)+1;
1360}
1361
Anders Carlsson762b7c72007-08-31 04:56:16 +00001362// InitListExpr
1363Stmt::child_iterator InitListExpr::child_begin() {
1364 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1365}
1366Stmt::child_iterator InitListExpr::child_end() {
Steve Naroff2e335472008-05-01 02:04:18 +00001367 return reinterpret_cast<Stmt**>(&InitExprs[getNumInits()]);
Anders Carlsson762b7c72007-08-31 04:56:16 +00001368}
1369
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001370// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001371Stmt::child_iterator ObjCStringLiteral::child_begin() {
1372 return child_iterator();
1373}
1374Stmt::child_iterator ObjCStringLiteral::child_end() {
1375 return child_iterator();
1376}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001377
1378// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001379Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1380Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001381
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001382// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001383Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1384 return child_iterator();
1385}
1386Stmt::child_iterator ObjCSelectorExpr::child_end() {
1387 return child_iterator();
1388}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001389
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001390// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001391Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1392 return child_iterator();
1393}
1394Stmt::child_iterator ObjCProtocolExpr::child_end() {
1395 return child_iterator();
1396}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001397
Steve Naroffc39ca262007-09-18 23:55:05 +00001398// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001399Stmt::child_iterator ObjCMessageExpr::child_begin() {
1400 return reinterpret_cast<Stmt**>(&SubExprs[ getReceiver() ? 0 : ARGS_START ]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001401}
1402Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001403 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001404}
1405