blob: 1c32d7cd77dd14bd8adb124d24e9170d0eafef81 [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)
257 : Expr(InitListExprClass, QualType())
258 , NumInits(numinits)
259 , LBraceLoc(lbraceloc)
260 , RBraceLoc(rbraceloc)
261{
262 InitExprs = new Expr*[numinits];
263 for (unsigned i = 0; i != numinits; i++)
264 InitExprs[i] = initexprs[i];
265}
Chris Lattner4b009652007-07-25 00:24:17 +0000266
267//===----------------------------------------------------------------------===//
268// Generic Expression Routines
269//===----------------------------------------------------------------------===//
270
271/// hasLocalSideEffect - Return true if this immediate expression has side
272/// effects, not counting any sub-expressions.
273bool Expr::hasLocalSideEffect() const {
274 switch (getStmtClass()) {
275 default:
276 return false;
277 case ParenExprClass:
278 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
279 case UnaryOperatorClass: {
280 const UnaryOperator *UO = cast<UnaryOperator>(this);
281
282 switch (UO->getOpcode()) {
283 default: return false;
284 case UnaryOperator::PostInc:
285 case UnaryOperator::PostDec:
286 case UnaryOperator::PreInc:
287 case UnaryOperator::PreDec:
288 return true; // ++/--
289
290 case UnaryOperator::Deref:
291 // Dereferencing a volatile pointer is a side-effect.
292 return getType().isVolatileQualified();
293 case UnaryOperator::Real:
294 case UnaryOperator::Imag:
295 // accessing a piece of a volatile complex is a side-effect.
296 return UO->getSubExpr()->getType().isVolatileQualified();
297
298 case UnaryOperator::Extension:
299 return UO->getSubExpr()->hasLocalSideEffect();
300 }
301 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000302 case BinaryOperatorClass: {
303 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
304 // Consider comma to have side effects if the LHS and RHS both do.
305 if (BinOp->getOpcode() == BinaryOperator::Comma)
306 return BinOp->getLHS()->hasLocalSideEffect() &&
307 BinOp->getRHS()->hasLocalSideEffect();
308
309 return BinOp->isAssignmentOp();
310 }
Chris Lattner06078d22007-08-25 02:00:02 +0000311 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000312 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000313
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000314 case ConditionalOperatorClass: {
315 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
316 return Exp->getCond()->hasLocalSideEffect()
317 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
318 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
319 }
320
Chris Lattner4b009652007-07-25 00:24:17 +0000321 case MemberExprClass:
322 case ArraySubscriptExprClass:
323 // If the base pointer or element is to a volatile pointer/field, accessing
324 // if is a side effect.
325 return getType().isVolatileQualified();
326
327 case CallExprClass:
328 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
329 // should warn.
330 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000331 case ObjCMessageExprClass:
332 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000333
334 case CastExprClass:
335 // If this is a cast to void, check the operand. Otherwise, the result of
336 // the cast is unused.
337 if (getType()->isVoidType())
338 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
339 return false;
340 }
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;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000396 case OCUVectorElementExprClass:
397 if (cast<OCUVectorElementExpr>(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 Lattner4b009652007-07-25 00:24:17 +0000404 default:
405 break;
406 }
407 return LV_InvalidExpression;
408}
409
410/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
411/// does not have an incomplete type, does not have a const-qualified type, and
412/// if it is a structure or union, does not have any member (including,
413/// recursively, any member or element of all contained aggregates or unions)
414/// with a const-qualified type.
415Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
416 isLvalueResult lvalResult = isLvalue();
417
418 switch (lvalResult) {
419 case LV_Valid: break;
420 case LV_NotObjectType: return MLV_NotObjectType;
421 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000422 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000423 case LV_InvalidExpression: return MLV_InvalidExpression;
424 }
425 if (TR.isConstQualified())
426 return MLV_ConstQualified;
427 if (TR->isArrayType())
428 return MLV_ArrayType;
429 if (TR->isIncompleteType())
430 return MLV_IncompleteType;
431
432 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
433 if (r->hasConstFields())
434 return MLV_ConstQualified;
435 }
436 return MLV_Valid;
437}
438
Ted Kremenek5778d622008-02-27 18:39:48 +0000439/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000440/// duration. This means that the address of this expression is a link-time
441/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000442bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000443 switch (getStmtClass()) {
444 default:
445 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000446 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000447 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000448 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000449 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000450 case CompoundLiteralExprClass:
451 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000452 case DeclRefExprClass: {
453 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
454 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000455 return VD->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000456 return false;
457 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000458 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000459 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000460 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000461 }
Chris Lattner743ec372007-11-27 21:35:27 +0000462 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000463 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner7e637512008-01-12 08:14:25 +0000464 case PreDefinedExprClass:
465 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000466 }
467}
468
Ted Kremenek87e30c52008-01-17 16:57:34 +0000469Expr* Expr::IgnoreParens() {
470 Expr* E = this;
471 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
472 E = P->getSubExpr();
473
474 return E;
475}
476
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000477/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
478/// or CastExprs or ImplicitCastExprs, returning their operand.
479Expr *Expr::IgnoreParenCasts() {
480 Expr *E = this;
481 while (true) {
482 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
483 E = P->getSubExpr();
484 else if (CastExpr *P = dyn_cast<CastExpr>(E))
485 E = P->getSubExpr();
486 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
487 E = P->getSubExpr();
488 else
489 return E;
490 }
491}
492
493
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000494bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000495 switch (getStmtClass()) {
496 default:
497 if (Loc) *Loc = getLocStart();
498 return false;
499 case ParenExprClass:
500 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
501 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000502 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000503 case FloatingLiteralClass:
504 case IntegerLiteralClass:
505 case CharacterLiteralClass:
506 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000507 case TypesCompatibleExprClass:
508 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000509 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000510 case CallExprClass: {
511 const CallExpr *CE = cast<CallExpr>(this);
512 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000513 Result.zextOrTrunc(
514 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000515 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000516 return true;
Steve Naroff44aec4c2008-01-31 01:07:12 +0000517 if (CE->isBuiltinConstantExpr())
518 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000519 if (Loc) *Loc = getLocStart();
520 return false;
521 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000522 case DeclRefExprClass: {
523 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
524 // Accept address of function.
525 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000526 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000527 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000528 if (isa<VarDecl>(D))
529 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000530 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000531 }
Steve Narofff91f9722008-01-09 00:05:37 +0000532 case CompoundLiteralExprClass:
533 if (Loc) *Loc = getLocStart();
534 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemanc4e28e42008-01-25 05:34:48 +0000535 // Allow "(vector type){2,4}" since the elements are all constant.
536 return TR->isArrayType() || TR->isVectorType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000537 case UnaryOperatorClass: {
538 const UnaryOperator *Exp = cast<UnaryOperator>(this);
539
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000540 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000541 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
Ted Kremenek5778d622008-02-27 18:39:48 +0000542 if (!Exp->getSubExpr()->hasGlobalStorage()) {
Chris Lattner35b662f2007-12-11 23:11:17 +0000543 if (Loc) *Loc = getLocStart();
544 return false;
545 }
546 return true;
547 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000548
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000549 // Get the operand value. If this is sizeof/alignof, do not evalute the
550 // operand. This affects C99 6.6p3.
Steve Narofff0b23542008-01-10 22:15:12 +0000551 if (!Exp->isSizeOfAlignOfOp() &&
552 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000553 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
554 return false;
555
556 switch (Exp->getOpcode()) {
557 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
558 // See C99 6.6p3.
559 default:
560 if (Loc) *Loc = Exp->getOperatorLoc();
561 return false;
562 case UnaryOperator::Extension:
563 return true; // FIXME: this is wrong.
564 case UnaryOperator::SizeOf:
565 case UnaryOperator::AlignOf:
Steve Narofff0b23542008-01-10 22:15:12 +0000566 case UnaryOperator::OffsetOf:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000567 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000568 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000569 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000570 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000571 }
Chris Lattner06db6132007-10-18 00:20:32 +0000572 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000573 case UnaryOperator::LNot:
574 case UnaryOperator::Plus:
575 case UnaryOperator::Minus:
576 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000577 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000578 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000579 }
580 case SizeOfAlignOfTypeExprClass: {
581 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
582 // alignof always evaluates to a constant.
Chris Lattner20515462008-02-21 05:45:29 +0000583 if (Exp->isSizeOf() && !Exp->getArgumentType()->isVoidType() &&
584 !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000585 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000586 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000587 }
Chris Lattner06db6132007-10-18 00:20:32 +0000588 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000589 }
590 case BinaryOperatorClass: {
591 const BinaryOperator *Exp = cast<BinaryOperator>(this);
592
593 // The LHS of a constant expr is always evaluated and needed.
594 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
595 return false;
596
597 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
598 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000599 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000600 }
601 case ImplicitCastExprClass:
602 case CastExprClass: {
603 const Expr *SubExpr;
604 SourceLocation CastLoc;
605 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
606 SubExpr = C->getSubExpr();
607 CastLoc = C->getLParenLoc();
608 } else {
609 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
610 CastLoc = getLocStart();
611 }
612 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
613 if (Loc) *Loc = SubExpr->getLocStart();
614 return false;
615 }
Chris Lattner06db6132007-10-18 00:20:32 +0000616 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000617 }
618 case ConditionalOperatorClass: {
619 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000620 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000621 // Handle the GNU extension for missing LHS.
622 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000623 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000624 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000625 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000626 }
Steve Narofff0b23542008-01-10 22:15:12 +0000627 case InitListExprClass: {
628 const InitListExpr *Exp = cast<InitListExpr>(this);
629 unsigned numInits = Exp->getNumInits();
630 for (unsigned i = 0; i < numInits; i++) {
631 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
632 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
633 return false;
634 }
635 }
636 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000637 }
Steve Narofff0b23542008-01-10 22:15:12 +0000638 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000639}
640
Chris Lattner4b009652007-07-25 00:24:17 +0000641/// isIntegerConstantExpr - this recursive routine will test if an expression is
642/// an integer constant expression. Note: With the introduction of VLA's in
643/// C99 the result of the sizeof operator is no longer always a constant
644/// expression. The generalization of the wording to include any subexpression
645/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
646/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
647/// "0 || f()" can be treated as a constant expression. In C90 this expression,
648/// occurring in a context requiring a constant, would have been a constraint
649/// violation. FIXME: This routine currently implements C90 semantics.
650/// To properly implement C99 semantics this routine will need to evaluate
651/// expressions involving operators previously mentioned.
652
653/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
654/// comma, etc
655///
656/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000657/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000658///
659/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
660/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
661/// cast+dereference.
662bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
663 SourceLocation *Loc, bool isEvaluated) const {
664 switch (getStmtClass()) {
665 default:
666 if (Loc) *Loc = getLocStart();
667 return false;
668 case ParenExprClass:
669 return cast<ParenExpr>(this)->getSubExpr()->
670 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
671 case IntegerLiteralClass:
672 Result = cast<IntegerLiteral>(this)->getValue();
673 break;
674 case CharacterLiteralClass: {
675 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000676 Result.zextOrTrunc(
677 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000678 Result = CL->getValue();
679 Result.setIsUnsigned(!getType()->isSignedIntegerType());
680 break;
681 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000682 case TypesCompatibleExprClass: {
683 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000684 Result.zextOrTrunc(
685 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000686 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000687 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000688 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000689 case CallExprClass: {
690 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000691 Result.zextOrTrunc(
692 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000693 if (CE->isBuiltinClassifyType(Result))
694 break;
695 if (Loc) *Loc = getLocStart();
696 return false;
697 }
Chris Lattner4b009652007-07-25 00:24:17 +0000698 case DeclRefExprClass:
699 if (const EnumConstantDecl *D =
700 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
701 Result = D->getInitVal();
702 break;
703 }
704 if (Loc) *Loc = getLocStart();
705 return false;
706 case UnaryOperatorClass: {
707 const UnaryOperator *Exp = cast<UnaryOperator>(this);
708
709 // Get the operand value. If this is sizeof/alignof, do not evalute the
710 // operand. This affects C99 6.6p3.
Anders Carlsson52774ad2008-01-29 15:56:48 +0000711 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner5a9b6242007-08-23 21:42:50 +0000712 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000713 return false;
714
715 switch (Exp->getOpcode()) {
716 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
717 // See C99 6.6p3.
718 default:
719 if (Loc) *Loc = Exp->getOperatorLoc();
720 return false;
721 case UnaryOperator::Extension:
722 return true; // FIXME: this is wrong.
723 case UnaryOperator::SizeOf:
724 case UnaryOperator::AlignOf:
Chris Lattner20515462008-02-21 05:45:29 +0000725 // Return the result in the right width.
726 Result.zextOrTrunc(
727 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
728 Exp->getOperatorLoc())));
729
730 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
731 if (Exp->getSubExpr()->getType()->isVoidType()) {
732 Result = 1;
733 break;
734 }
735
Chris Lattner4b009652007-07-25 00:24:17 +0000736 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000737 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000738 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000739 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000740 }
Chris Lattner4b009652007-07-25 00:24:17 +0000741
Chris Lattner4b009652007-07-25 00:24:17 +0000742 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000743 if (Exp->getSubExpr()->getType()->isFunctionType()) {
744 // GCC extension: sizeof(function) = 1.
745 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000746 } else {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000747 unsigned CharSize =
748 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
749
Anders Carlsson1f86b032008-02-18 07:10:45 +0000750 if (Exp->getOpcode() == UnaryOperator::AlignOf)
751 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
752 Exp->getOperatorLoc()) / CharSize;
753 else
754 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
755 Exp->getOperatorLoc()) / CharSize;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000756 }
Chris Lattner4b009652007-07-25 00:24:17 +0000757 break;
758 case UnaryOperator::LNot: {
Chris Lattnerf00fdc02008-01-25 19:16:19 +0000759 bool Val = Result == 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000760 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000761 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
762 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000763 Result = Val;
764 break;
765 }
766 case UnaryOperator::Plus:
767 break;
768 case UnaryOperator::Minus:
769 Result = -Result;
770 break;
771 case UnaryOperator::Not:
772 Result = ~Result;
773 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000774 case UnaryOperator::OffsetOf:
775 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000776 }
777 break;
778 }
779 case SizeOfAlignOfTypeExprClass: {
780 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
Chris Lattner20515462008-02-21 05:45:29 +0000781
782 // Return the result in the right width.
783 Result.zextOrTrunc(
784 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
785
786 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
787 if (Exp->getArgumentType()->isVoidType()) {
788 Result = 1;
789 break;
790 }
791
792 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000793 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000794 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000795 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000796 }
Chris Lattner4b009652007-07-25 00:24:17 +0000797
Chris Lattner4b009652007-07-25 00:24:17 +0000798 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000799 if (Exp->getArgumentType()->isFunctionType()) {
800 // GCC extension: sizeof(function) = 1.
801 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000802 } else {
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000803 unsigned CharSize =
804 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
805
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000806 if (Exp->isSizeOf())
807 Result = Ctx.getTypeSize(Exp->getArgumentType(),
808 Exp->getOperatorLoc()) / CharSize;
809 else
810 Result = Ctx.getTypeAlign(Exp->getArgumentType(),
811 Exp->getOperatorLoc()) / CharSize;
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000812 }
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000813
Chris Lattner4b009652007-07-25 00:24:17 +0000814 break;
815 }
816 case BinaryOperatorClass: {
817 const BinaryOperator *Exp = cast<BinaryOperator>(this);
818
819 // The LHS of a constant expr is always evaluated and needed.
820 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
821 return false;
822
823 llvm::APSInt RHS(Result);
824
825 // The short-circuiting &&/|| operators don't necessarily evaluate their
826 // RHS. Make sure to pass isEvaluated down correctly.
827 if (Exp->isLogicalOp()) {
828 bool RHSEval;
829 if (Exp->getOpcode() == BinaryOperator::LAnd)
830 RHSEval = Result != 0;
831 else {
832 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
833 RHSEval = Result == 0;
834 }
835
836 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
837 isEvaluated & RHSEval))
838 return false;
839 } else {
840 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
841 return false;
842 }
843
844 switch (Exp->getOpcode()) {
845 default:
846 if (Loc) *Loc = getLocStart();
847 return false;
848 case BinaryOperator::Mul:
849 Result *= RHS;
850 break;
851 case BinaryOperator::Div:
852 if (RHS == 0) {
853 if (!isEvaluated) break;
854 if (Loc) *Loc = getLocStart();
855 return false;
856 }
857 Result /= RHS;
858 break;
859 case BinaryOperator::Rem:
860 if (RHS == 0) {
861 if (!isEvaluated) break;
862 if (Loc) *Loc = getLocStart();
863 return false;
864 }
865 Result %= RHS;
866 break;
867 case BinaryOperator::Add: Result += RHS; break;
868 case BinaryOperator::Sub: Result -= RHS; break;
869 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000870 Result <<=
871 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000872 break;
873 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000874 Result >>=
875 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000876 break;
877 case BinaryOperator::LT: Result = Result < RHS; break;
878 case BinaryOperator::GT: Result = Result > RHS; break;
879 case BinaryOperator::LE: Result = Result <= RHS; break;
880 case BinaryOperator::GE: Result = Result >= RHS; break;
881 case BinaryOperator::EQ: Result = Result == RHS; break;
882 case BinaryOperator::NE: Result = Result != RHS; break;
883 case BinaryOperator::And: Result &= RHS; break;
884 case BinaryOperator::Xor: Result ^= RHS; break;
885 case BinaryOperator::Or: Result |= RHS; break;
886 case BinaryOperator::LAnd:
887 Result = Result != 0 && RHS != 0;
888 break;
889 case BinaryOperator::LOr:
890 Result = Result != 0 || RHS != 0;
891 break;
892
893 case BinaryOperator::Comma:
894 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
895 // *except* when they are contained within a subexpression that is not
896 // evaluated". Note that Assignment can never happen due to constraints
897 // on the LHS subexpr, so we don't need to check it here.
898 if (isEvaluated) {
899 if (Loc) *Loc = getLocStart();
900 return false;
901 }
902
903 // The result of the constant expr is the RHS.
904 Result = RHS;
905 return true;
906 }
907
908 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
909 break;
910 }
911 case ImplicitCastExprClass:
912 case CastExprClass: {
913 const Expr *SubExpr;
914 SourceLocation CastLoc;
915 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
916 SubExpr = C->getSubExpr();
917 CastLoc = C->getLParenLoc();
918 } else {
919 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
920 CastLoc = getLocStart();
921 }
922
923 // C99 6.6p6: shall only convert arithmetic types to integer types.
924 if (!SubExpr->getType()->isArithmeticType() ||
925 !getType()->isIntegerType()) {
926 if (Loc) *Loc = SubExpr->getLocStart();
927 return false;
928 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000929
930 uint32_t DestWidth =
931 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
932
Chris Lattner4b009652007-07-25 00:24:17 +0000933 // Handle simple integer->integer casts.
934 if (SubExpr->getType()->isIntegerType()) {
935 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
936 return false;
937
938 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000939 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +0000940 if (getType()->isBooleanType()) {
941 // Conversion to bool compares against zero.
942 Result = Result != 0;
943 Result.zextOrTrunc(DestWidth);
944 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +0000945 Result.sextOrTrunc(DestWidth);
946 else // If the input is unsigned, do a zero extend, noop, or truncate.
947 Result.zextOrTrunc(DestWidth);
948 break;
949 }
950
951 // Allow floating constants that are the immediate operands of casts or that
952 // are parenthesized.
953 const Expr *Operand = SubExpr;
954 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
955 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000956
957 // If this isn't a floating literal, we can't handle it.
958 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
959 if (!FL) {
960 if (Loc) *Loc = Operand->getLocStart();
961 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000962 }
Chris Lattner000c4102008-01-09 18:59:34 +0000963
964 // If the destination is boolean, compare against zero.
965 if (getType()->isBooleanType()) {
966 Result = !FL->getValue().isZero();
967 Result.zextOrTrunc(DestWidth);
968 break;
969 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000970
971 // Determine whether we are converting to unsigned or signed.
972 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000973
974 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
975 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000976 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000977 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
978 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000979 Result = llvm::APInt(DestWidth, 4, Space);
980 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000981 }
982 case ConditionalOperatorClass: {
983 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
984
985 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
986 return false;
987
988 const Expr *TrueExp = Exp->getLHS();
989 const Expr *FalseExp = Exp->getRHS();
990 if (Result == 0) std::swap(TrueExp, FalseExp);
991
992 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000993 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000994 return false;
995 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000996 if (TrueExp &&
997 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000998 return false;
999 break;
1000 }
1001 }
1002
1003 // Cases that are valid constant exprs fall through to here.
1004 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1005 return true;
1006}
1007
Chris Lattner4b009652007-07-25 00:24:17 +00001008/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1009/// integer constant expression with the value zero, or if this is one that is
1010/// cast to void*.
1011bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffa2e53222008-01-14 16:10:57 +00001012 // Strip off a cast to void*, if it exists.
1013 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
1014 // Check that it is a cast to void*.
Eli Friedmand899dbe2008-02-13 17:29:58 +00001015 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001016 QualType Pointee = PT->getPointeeType();
Chris Lattner35fef522008-02-20 20:55:12 +00001017 if (Pointee.getCVRQualifiers() == 0 &&
1018 Pointee->isVoidType() && // to void*
Steve Naroffa2e53222008-01-14 16:10:57 +00001019 CE->getSubExpr()->getType()->isIntegerType()) // from int.
1020 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001021 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001022 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1023 // Ignore the ImplicitCastExpr type entirely.
1024 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
1025 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1026 // Accept ((void*)0) as a null pointer constant, as many other
1027 // implementations do.
1028 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Narofff33a9852008-01-14 02:53:34 +00001029 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001030
1031 // This expression must be an integer type.
1032 if (!getType()->isIntegerType())
1033 return false;
1034
Chris Lattner4b009652007-07-25 00:24:17 +00001035 // If we have an integer constant expression, we need to *evaluate* it and
1036 // test for the value 0.
1037 llvm::APSInt Val(32);
Steve Naroffa2e53222008-01-14 16:10:57 +00001038 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001039}
Steve Naroffc11705f2007-07-28 23:10:27 +00001040
Chris Lattnera0d03a72007-08-03 17:31:20 +00001041unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +00001042 return strlen(Accessor.getName());
1043}
1044
1045
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001046/// getComponentType - Determine whether the components of this access are
1047/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001048OCUVectorElementExpr::ElementType
1049OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +00001050 // derive the component type, no need to waste space.
1051 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +00001052
Chris Lattner9096b792007-08-02 22:33:49 +00001053 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
1054 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +00001055
Chris Lattner9096b792007-08-02 22:33:49 +00001056 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +00001057 "getComponentType(): Illegal accessor");
1058 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +00001059}
Steve Naroffba67f692007-07-30 03:29:09 +00001060
Chris Lattnera0d03a72007-08-03 17:31:20 +00001061/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001062/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001063bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001064 const char *compStr = Accessor.getName();
1065 unsigned length = strlen(compStr);
1066
1067 for (unsigned i = 0; i < length-1; i++) {
1068 const char *s = compStr+i;
1069 for (const char c = *s++; *s; s++)
1070 if (c == *s)
1071 return true;
1072 }
1073 return false;
1074}
Chris Lattner42158e72007-08-02 23:36:59 +00001075
1076/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001077unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +00001078 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +00001079 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +00001080
1081 unsigned Result = 0;
1082
1083 while (length--) {
1084 Result <<= 2;
1085 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1086 assert(Idx != -1 && "Invalid accessor letter");
1087 Result |= Idx;
1088 }
1089 return Result;
1090}
1091
Steve Naroff4ed9d662007-09-27 14:38:14 +00001092// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001093ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001094 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001095 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001096 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001097 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1098 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001099 NumArgs = nargs;
1100 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001101 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001102 if (NumArgs) {
1103 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001104 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1105 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001106 LBracloc = LBrac;
1107 RBracloc = RBrac;
1108}
1109
Steve Naroff4ed9d662007-09-27 14:38:14 +00001110// constructor for class messages.
1111// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001112ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001113 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001114 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001115 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001116 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1117 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001118 NumArgs = nargs;
1119 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +00001120 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +00001121 if (NumArgs) {
1122 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001123 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1124 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001125 LBracloc = LBrac;
1126 RBracloc = RBrac;
1127}
1128
Chris Lattnerf624cd22007-10-25 00:29:32 +00001129
1130bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1131 llvm::APSInt CondVal(32);
1132 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1133 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1134 return CondVal != 0;
1135}
1136
Anders Carlsson52774ad2008-01-29 15:56:48 +00001137static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1138{
1139 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1140 QualType Ty = ME->getBase()->getType();
1141
1142 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
1143 const ASTRecordLayout &RL = C.getASTRecordLayout(RD, SourceLocation());
1144 FieldDecl *FD = ME->getMemberDecl();
1145
1146 // FIXME: This is linear time.
1147 unsigned i = 0, e = 0;
1148 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1149 if (RD->getMember(i) == FD)
1150 break;
1151 }
1152
1153 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1154 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1155 const Expr *Base = ASE->getBase();
1156 llvm::APSInt Idx(32);
1157 bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1158 assert(ICE && "Array index is not a constant integer!");
1159
1160 int64_t size = C.getTypeSize(ASE->getType(), SourceLocation());
1161 size *= Idx.getSExtValue();
1162
1163 return size + evaluateOffsetOf(C, Base);
1164 } else if (isa<CompoundLiteralExpr>(E))
1165 return 0;
1166
1167 assert(0 && "Unknown offsetof subexpression!");
1168 return 0;
1169}
1170
1171int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1172{
1173 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1174
1175 unsigned CharSize =
1176 C.Target.getCharWidth(C.getFullLoc(getOperatorLoc()));
1177
1178 return ::evaluateOffsetOf(C, Val) / CharSize;
1179}
1180
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001181//===----------------------------------------------------------------------===//
1182// Child Iterators for iterating over subexpressions/substatements
1183//===----------------------------------------------------------------------===//
1184
1185// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001186Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1187Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001188
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001189// ObjCIvarRefExpr
1190Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1191Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1192
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001193// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001194Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1195Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001196
1197// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001198Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1199Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001200
1201// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001202Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1203Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001204
1205// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001206Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1207Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001208
Chris Lattner1de66eb2007-08-26 03:42:43 +00001209// ImaginaryLiteral
1210Stmt::child_iterator ImaginaryLiteral::child_begin() {
1211 return reinterpret_cast<Stmt**>(&Val);
1212}
1213Stmt::child_iterator ImaginaryLiteral::child_end() {
1214 return reinterpret_cast<Stmt**>(&Val)+1;
1215}
1216
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001217// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001218Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1219Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001220
1221// ParenExpr
1222Stmt::child_iterator ParenExpr::child_begin() {
1223 return reinterpret_cast<Stmt**>(&Val);
1224}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001225Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001226 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001227}
1228
1229// UnaryOperator
1230Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001231 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001232}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001233Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001234 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001235}
1236
1237// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001238Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001239 // If the type is a VLA type (and not a typedef), the size expression of the
1240 // VLA needs to be treated as an executable expression.
1241 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1242 return child_iterator(T);
1243 else
1244 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001245}
1246Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001247 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001248}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001249
1250// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001251Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001252 return reinterpret_cast<Stmt**>(&SubExprs);
1253}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001254Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001255 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001256}
1257
1258// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001259Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001260 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001261}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001262Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001263 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001264}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001265
1266// MemberExpr
1267Stmt::child_iterator MemberExpr::child_begin() {
1268 return reinterpret_cast<Stmt**>(&Base);
1269}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001270Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001271 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001272}
1273
1274// OCUVectorElementExpr
1275Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1276 return reinterpret_cast<Stmt**>(&Base);
1277}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001278Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001279 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001280}
1281
1282// CompoundLiteralExpr
1283Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1284 return reinterpret_cast<Stmt**>(&Init);
1285}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001286Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001287 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001288}
1289
1290// ImplicitCastExpr
1291Stmt::child_iterator ImplicitCastExpr::child_begin() {
1292 return reinterpret_cast<Stmt**>(&Op);
1293}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001294Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001295 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001296}
1297
1298// CastExpr
1299Stmt::child_iterator CastExpr::child_begin() {
1300 return reinterpret_cast<Stmt**>(&Op);
1301}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001302Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001303 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001304}
1305
1306// BinaryOperator
1307Stmt::child_iterator BinaryOperator::child_begin() {
1308 return reinterpret_cast<Stmt**>(&SubExprs);
1309}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001310Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001311 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001312}
1313
1314// ConditionalOperator
1315Stmt::child_iterator ConditionalOperator::child_begin() {
1316 return reinterpret_cast<Stmt**>(&SubExprs);
1317}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001318Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001319 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001320}
1321
1322// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001323Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1324Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001325
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001326// StmtExpr
1327Stmt::child_iterator StmtExpr::child_begin() {
1328 return reinterpret_cast<Stmt**>(&SubStmt);
1329}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001330Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001331 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001332}
1333
1334// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001335Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1336 return child_iterator();
1337}
1338
1339Stmt::child_iterator TypesCompatibleExpr::child_end() {
1340 return child_iterator();
1341}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001342
1343// ChooseExpr
1344Stmt::child_iterator ChooseExpr::child_begin() {
1345 return reinterpret_cast<Stmt**>(&SubExprs);
1346}
1347
1348Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001349 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001350}
1351
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001352// OverloadExpr
1353Stmt::child_iterator OverloadExpr::child_begin() {
1354 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1355}
1356Stmt::child_iterator OverloadExpr::child_end() {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001357 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001358}
1359
Anders Carlsson36760332007-10-15 20:28:48 +00001360// VAArgExpr
1361Stmt::child_iterator VAArgExpr::child_begin() {
1362 return reinterpret_cast<Stmt**>(&Val);
1363}
1364
1365Stmt::child_iterator VAArgExpr::child_end() {
1366 return reinterpret_cast<Stmt**>(&Val)+1;
1367}
1368
Anders Carlsson762b7c72007-08-31 04:56:16 +00001369// InitListExpr
1370Stmt::child_iterator InitListExpr::child_begin() {
1371 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1372}
1373Stmt::child_iterator InitListExpr::child_end() {
1374 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1375}
1376
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001377// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001378Stmt::child_iterator ObjCStringLiteral::child_begin() {
1379 return child_iterator();
1380}
1381Stmt::child_iterator ObjCStringLiteral::child_end() {
1382 return child_iterator();
1383}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001384
1385// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001386Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1387Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001388
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001389// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001390Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1391 return child_iterator();
1392}
1393Stmt::child_iterator ObjCSelectorExpr::child_end() {
1394 return child_iterator();
1395}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001396
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001397// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001398Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1399 return child_iterator();
1400}
1401Stmt::child_iterator ObjCProtocolExpr::child_end() {
1402 return child_iterator();
1403}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001404
Steve Naroffc39ca262007-09-18 23:55:05 +00001405// ObjCMessageExpr
1406Stmt::child_iterator ObjCMessageExpr::child_begin() {
1407 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1408}
1409Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001410 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001411}
1412