blob: 5205702f29730c74362a1f2d00a303e784b602c2 [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();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000456 if (isa<FunctionDecl>(D))
457 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000458 return false;
459 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000460 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000461 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000462 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000463 }
Chris Lattner743ec372007-11-27 21:35:27 +0000464 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000465 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner7e637512008-01-12 08:14:25 +0000466 case PreDefinedExprClass:
467 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000468 }
469}
470
Ted Kremenek87e30c52008-01-17 16:57:34 +0000471Expr* Expr::IgnoreParens() {
472 Expr* E = this;
473 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
474 E = P->getSubExpr();
475
476 return E;
477}
478
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000479/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
480/// or CastExprs or ImplicitCastExprs, returning their operand.
481Expr *Expr::IgnoreParenCasts() {
482 Expr *E = this;
483 while (true) {
484 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
485 E = P->getSubExpr();
486 else if (CastExpr *P = dyn_cast<CastExpr>(E))
487 E = P->getSubExpr();
488 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
489 E = P->getSubExpr();
490 else
491 return E;
492 }
493}
494
495
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000496bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000497 switch (getStmtClass()) {
498 default:
499 if (Loc) *Loc = getLocStart();
500 return false;
501 case ParenExprClass:
502 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
503 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000504 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000505 case FloatingLiteralClass:
506 case IntegerLiteralClass:
507 case CharacterLiteralClass:
508 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000509 case TypesCompatibleExprClass:
510 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000511 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000512 case CallExprClass: {
513 const CallExpr *CE = cast<CallExpr>(this);
514 llvm::APSInt Result(32);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000515 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000516 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000517 return true;
Steve Naroff44aec4c2008-01-31 01:07:12 +0000518 if (CE->isBuiltinConstantExpr())
519 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000520 if (Loc) *Loc = getLocStart();
521 return false;
522 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000523 case DeclRefExprClass: {
524 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
525 // Accept address of function.
526 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000527 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000528 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000529 if (isa<VarDecl>(D))
530 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000531 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000532 }
Steve Narofff91f9722008-01-09 00:05:37 +0000533 case CompoundLiteralExprClass:
534 if (Loc) *Loc = getLocStart();
535 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemanc4e28e42008-01-25 05:34:48 +0000536 // Allow "(vector type){2,4}" since the elements are all constant.
537 return TR->isArrayType() || TR->isVectorType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000538 case UnaryOperatorClass: {
539 const UnaryOperator *Exp = cast<UnaryOperator>(this);
540
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000541 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000542 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
Ted Kremenek5778d622008-02-27 18:39:48 +0000543 if (!Exp->getSubExpr()->hasGlobalStorage()) {
Chris Lattner35b662f2007-12-11 23:11:17 +0000544 if (Loc) *Loc = getLocStart();
545 return false;
546 }
547 return true;
548 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000549
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000550 // Get the operand value. If this is sizeof/alignof, do not evalute the
551 // operand. This affects C99 6.6p3.
Steve Narofff0b23542008-01-10 22:15:12 +0000552 if (!Exp->isSizeOfAlignOfOp() &&
553 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000554 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
555 return false;
556
557 switch (Exp->getOpcode()) {
558 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
559 // See C99 6.6p3.
560 default:
561 if (Loc) *Loc = Exp->getOperatorLoc();
562 return false;
563 case UnaryOperator::Extension:
564 return true; // FIXME: this is wrong.
565 case UnaryOperator::SizeOf:
566 case UnaryOperator::AlignOf:
Steve Narofff0b23542008-01-10 22:15:12 +0000567 case UnaryOperator::OffsetOf:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000568 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000569 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000570 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000571 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000572 }
Chris Lattner06db6132007-10-18 00:20:32 +0000573 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000574 case UnaryOperator::LNot:
575 case UnaryOperator::Plus:
576 case UnaryOperator::Minus:
577 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000578 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000579 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000580 }
581 case SizeOfAlignOfTypeExprClass: {
582 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
583 // alignof always evaluates to a constant.
Chris Lattner20515462008-02-21 05:45:29 +0000584 if (Exp->isSizeOf() && !Exp->getArgumentType()->isVoidType() &&
585 !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000586 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000587 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000588 }
Chris Lattner06db6132007-10-18 00:20:32 +0000589 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000590 }
591 case BinaryOperatorClass: {
592 const BinaryOperator *Exp = cast<BinaryOperator>(this);
593
594 // The LHS of a constant expr is always evaluated and needed.
595 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
596 return false;
597
598 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
599 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000600 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000601 }
602 case ImplicitCastExprClass:
603 case CastExprClass: {
604 const Expr *SubExpr;
605 SourceLocation CastLoc;
606 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
607 SubExpr = C->getSubExpr();
608 CastLoc = C->getLParenLoc();
609 } else {
610 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
611 CastLoc = getLocStart();
612 }
613 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
614 if (Loc) *Loc = SubExpr->getLocStart();
615 return false;
616 }
Chris Lattner06db6132007-10-18 00:20:32 +0000617 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000618 }
619 case ConditionalOperatorClass: {
620 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000621 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000622 // Handle the GNU extension for missing LHS.
623 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000624 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000625 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000626 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000627 }
Steve Narofff0b23542008-01-10 22:15:12 +0000628 case InitListExprClass: {
629 const InitListExpr *Exp = cast<InitListExpr>(this);
630 unsigned numInits = Exp->getNumInits();
631 for (unsigned i = 0; i < numInits; i++) {
632 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
633 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
634 return false;
635 }
636 }
637 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000638 }
Steve Narofff0b23542008-01-10 22:15:12 +0000639 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000640}
641
Chris Lattner4b009652007-07-25 00:24:17 +0000642/// isIntegerConstantExpr - this recursive routine will test if an expression is
643/// an integer constant expression. Note: With the introduction of VLA's in
644/// C99 the result of the sizeof operator is no longer always a constant
645/// expression. The generalization of the wording to include any subexpression
646/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
647/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
648/// "0 || f()" can be treated as a constant expression. In C90 this expression,
649/// occurring in a context requiring a constant, would have been a constraint
650/// violation. FIXME: This routine currently implements C90 semantics.
651/// To properly implement C99 semantics this routine will need to evaluate
652/// expressions involving operators previously mentioned.
653
654/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
655/// comma, etc
656///
657/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000658/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000659///
660/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
661/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
662/// cast+dereference.
663bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
664 SourceLocation *Loc, bool isEvaluated) const {
665 switch (getStmtClass()) {
666 default:
667 if (Loc) *Loc = getLocStart();
668 return false;
669 case ParenExprClass:
670 return cast<ParenExpr>(this)->getSubExpr()->
671 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
672 case IntegerLiteralClass:
673 Result = cast<IntegerLiteral>(this)->getValue();
674 break;
675 case CharacterLiteralClass: {
676 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000677 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
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 Lattner8cd0e932008-03-05 18:54:05 +0000684 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000685 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000686 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000687 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000688 case CallExprClass: {
689 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000690 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000691 if (CE->isBuiltinClassifyType(Result))
692 break;
693 if (Loc) *Loc = getLocStart();
694 return false;
695 }
Chris Lattner4b009652007-07-25 00:24:17 +0000696 case DeclRefExprClass:
697 if (const EnumConstantDecl *D =
698 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
699 Result = D->getInitVal();
700 break;
701 }
702 if (Loc) *Loc = getLocStart();
703 return false;
704 case UnaryOperatorClass: {
705 const UnaryOperator *Exp = cast<UnaryOperator>(this);
706
707 // Get the operand value. If this is sizeof/alignof, do not evalute the
708 // operand. This affects C99 6.6p3.
Anders Carlsson52774ad2008-01-29 15:56:48 +0000709 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner5a9b6242007-08-23 21:42:50 +0000710 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000711 return false;
712
713 switch (Exp->getOpcode()) {
714 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
715 // See C99 6.6p3.
716 default:
717 if (Loc) *Loc = Exp->getOperatorLoc();
718 return false;
719 case UnaryOperator::Extension:
720 return true; // FIXME: this is wrong.
721 case UnaryOperator::SizeOf:
722 case UnaryOperator::AlignOf:
Chris Lattner20515462008-02-21 05:45:29 +0000723 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000724 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000725
726 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
727 if (Exp->getSubExpr()->getType()->isVoidType()) {
728 Result = 1;
729 break;
730 }
731
Chris Lattner4b009652007-07-25 00:24:17 +0000732 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000733 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000734 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000735 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000736 }
Chris Lattner4b009652007-07-25 00:24:17 +0000737
Chris Lattner4b009652007-07-25 00:24:17 +0000738 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000739 if (Exp->getSubExpr()->getType()->isFunctionType()) {
740 // GCC extension: sizeof(function) = 1.
741 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000742 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000743 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson1f86b032008-02-18 07:10:45 +0000744 if (Exp->getOpcode() == UnaryOperator::AlignOf)
Chris Lattner8cd0e932008-03-05 18:54:05 +0000745 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType()) / CharSize;
Anders Carlsson1f86b032008-02-18 07:10:45 +0000746 else
Chris Lattner8cd0e932008-03-05 18:54:05 +0000747 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType()) / CharSize;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000748 }
Chris Lattner4b009652007-07-25 00:24:17 +0000749 break;
750 case UnaryOperator::LNot: {
Chris Lattnerf00fdc02008-01-25 19:16:19 +0000751 bool Val = Result == 0;
Chris Lattner8cd0e932008-03-05 18:54:05 +0000752 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000753 Result = Val;
754 break;
755 }
756 case UnaryOperator::Plus:
757 break;
758 case UnaryOperator::Minus:
759 Result = -Result;
760 break;
761 case UnaryOperator::Not:
762 Result = ~Result;
763 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000764 case UnaryOperator::OffsetOf:
765 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000766 }
767 break;
768 }
769 case SizeOfAlignOfTypeExprClass: {
770 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
Chris Lattner20515462008-02-21 05:45:29 +0000771
772 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000773 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000774
775 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
776 if (Exp->getArgumentType()->isVoidType()) {
777 Result = 1;
778 break;
779 }
780
781 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000782 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000783 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000784 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000785 }
Chris Lattner4b009652007-07-25 00:24:17 +0000786
Chris Lattner4b009652007-07-25 00:24:17 +0000787 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000788 if (Exp->getArgumentType()->isFunctionType()) {
789 // GCC extension: sizeof(function) = 1.
790 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000791 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000792 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000793 if (Exp->isSizeOf())
Chris Lattner8cd0e932008-03-05 18:54:05 +0000794 Result = Ctx.getTypeSize(Exp->getArgumentType()) / CharSize;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000795 else
Chris Lattner8cd0e932008-03-05 18:54:05 +0000796 Result = Ctx.getTypeAlign(Exp->getArgumentType()) / CharSize;
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000797 }
Chris Lattner4b009652007-07-25 00:24:17 +0000798 break;
799 }
800 case BinaryOperatorClass: {
801 const BinaryOperator *Exp = cast<BinaryOperator>(this);
802
803 // The LHS of a constant expr is always evaluated and needed.
804 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
805 return false;
806
807 llvm::APSInt RHS(Result);
808
809 // The short-circuiting &&/|| operators don't necessarily evaluate their
810 // RHS. Make sure to pass isEvaluated down correctly.
811 if (Exp->isLogicalOp()) {
812 bool RHSEval;
813 if (Exp->getOpcode() == BinaryOperator::LAnd)
814 RHSEval = Result != 0;
815 else {
816 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
817 RHSEval = Result == 0;
818 }
819
820 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
821 isEvaluated & RHSEval))
822 return false;
823 } else {
824 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
825 return false;
826 }
827
828 switch (Exp->getOpcode()) {
829 default:
830 if (Loc) *Loc = getLocStart();
831 return false;
832 case BinaryOperator::Mul:
833 Result *= RHS;
834 break;
835 case BinaryOperator::Div:
836 if (RHS == 0) {
837 if (!isEvaluated) break;
838 if (Loc) *Loc = getLocStart();
839 return false;
840 }
841 Result /= RHS;
842 break;
843 case BinaryOperator::Rem:
844 if (RHS == 0) {
845 if (!isEvaluated) break;
846 if (Loc) *Loc = getLocStart();
847 return false;
848 }
849 Result %= RHS;
850 break;
851 case BinaryOperator::Add: Result += RHS; break;
852 case BinaryOperator::Sub: Result -= RHS; break;
853 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000854 Result <<=
855 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000856 break;
857 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000858 Result >>=
859 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000860 break;
861 case BinaryOperator::LT: Result = Result < RHS; break;
862 case BinaryOperator::GT: Result = Result > RHS; break;
863 case BinaryOperator::LE: Result = Result <= RHS; break;
864 case BinaryOperator::GE: Result = Result >= RHS; break;
865 case BinaryOperator::EQ: Result = Result == RHS; break;
866 case BinaryOperator::NE: Result = Result != RHS; break;
867 case BinaryOperator::And: Result &= RHS; break;
868 case BinaryOperator::Xor: Result ^= RHS; break;
869 case BinaryOperator::Or: Result |= RHS; break;
870 case BinaryOperator::LAnd:
871 Result = Result != 0 && RHS != 0;
872 break;
873 case BinaryOperator::LOr:
874 Result = Result != 0 || RHS != 0;
875 break;
876
877 case BinaryOperator::Comma:
878 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
879 // *except* when they are contained within a subexpression that is not
880 // evaluated". Note that Assignment can never happen due to constraints
881 // on the LHS subexpr, so we don't need to check it here.
882 if (isEvaluated) {
883 if (Loc) *Loc = getLocStart();
884 return false;
885 }
886
887 // The result of the constant expr is the RHS.
888 Result = RHS;
889 return true;
890 }
891
892 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
893 break;
894 }
895 case ImplicitCastExprClass:
896 case CastExprClass: {
897 const Expr *SubExpr;
898 SourceLocation CastLoc;
899 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
900 SubExpr = C->getSubExpr();
901 CastLoc = C->getLParenLoc();
902 } else {
903 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
904 CastLoc = getLocStart();
905 }
906
907 // C99 6.6p6: shall only convert arithmetic types to integer types.
908 if (!SubExpr->getType()->isArithmeticType() ||
909 !getType()->isIntegerType()) {
910 if (Loc) *Loc = SubExpr->getLocStart();
911 return false;
912 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000913
Chris Lattner8cd0e932008-03-05 18:54:05 +0000914 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000915
Chris Lattner4b009652007-07-25 00:24:17 +0000916 // Handle simple integer->integer casts.
917 if (SubExpr->getType()->isIntegerType()) {
918 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
919 return false;
920
921 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000922 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +0000923 if (getType()->isBooleanType()) {
924 // Conversion to bool compares against zero.
925 Result = Result != 0;
926 Result.zextOrTrunc(DestWidth);
927 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +0000928 Result.sextOrTrunc(DestWidth);
929 else // If the input is unsigned, do a zero extend, noop, or truncate.
930 Result.zextOrTrunc(DestWidth);
931 break;
932 }
933
934 // Allow floating constants that are the immediate operands of casts or that
935 // are parenthesized.
936 const Expr *Operand = SubExpr;
937 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
938 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000939
940 // If this isn't a floating literal, we can't handle it.
941 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
942 if (!FL) {
943 if (Loc) *Loc = Operand->getLocStart();
944 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000945 }
Chris Lattner000c4102008-01-09 18:59:34 +0000946
947 // If the destination is boolean, compare against zero.
948 if (getType()->isBooleanType()) {
949 Result = !FL->getValue().isZero();
950 Result.zextOrTrunc(DestWidth);
951 break;
952 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000953
954 // Determine whether we are converting to unsigned or signed.
955 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000956
957 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
958 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000959 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000960 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
961 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000962 Result = llvm::APInt(DestWidth, 4, Space);
963 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000964 }
965 case ConditionalOperatorClass: {
966 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
967
968 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
969 return false;
970
971 const Expr *TrueExp = Exp->getLHS();
972 const Expr *FalseExp = Exp->getRHS();
973 if (Result == 0) std::swap(TrueExp, FalseExp);
974
975 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000976 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000977 return false;
978 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000979 if (TrueExp &&
980 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000981 return false;
982 break;
983 }
984 }
985
986 // Cases that are valid constant exprs fall through to here.
987 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
988 return true;
989}
990
Chris Lattner4b009652007-07-25 00:24:17 +0000991/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
992/// integer constant expression with the value zero, or if this is one that is
993/// cast to void*.
994bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffa2e53222008-01-14 16:10:57 +0000995 // Strip off a cast to void*, if it exists.
996 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
997 // Check that it is a cast to void*.
Eli Friedmand899dbe2008-02-13 17:29:58 +0000998 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000999 QualType Pointee = PT->getPointeeType();
Chris Lattner35fef522008-02-20 20:55:12 +00001000 if (Pointee.getCVRQualifiers() == 0 &&
1001 Pointee->isVoidType() && // to void*
Steve Naroffa2e53222008-01-14 16:10:57 +00001002 CE->getSubExpr()->getType()->isIntegerType()) // from int.
1003 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001004 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001005 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1006 // Ignore the ImplicitCastExpr type entirely.
1007 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
1008 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1009 // Accept ((void*)0) as a null pointer constant, as many other
1010 // implementations do.
1011 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Narofff33a9852008-01-14 02:53:34 +00001012 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001013
1014 // This expression must be an integer type.
1015 if (!getType()->isIntegerType())
1016 return false;
1017
Chris Lattner4b009652007-07-25 00:24:17 +00001018 // If we have an integer constant expression, we need to *evaluate* it and
1019 // test for the value 0.
1020 llvm::APSInt Val(32);
Steve Naroffa2e53222008-01-14 16:10:57 +00001021 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001022}
Steve Naroffc11705f2007-07-28 23:10:27 +00001023
Chris Lattnera0d03a72007-08-03 17:31:20 +00001024unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +00001025 return strlen(Accessor.getName());
1026}
1027
1028
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001029/// getComponentType - Determine whether the components of this access are
1030/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001031OCUVectorElementExpr::ElementType
1032OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +00001033 // derive the component type, no need to waste space.
1034 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +00001035
Chris Lattner9096b792007-08-02 22:33:49 +00001036 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
1037 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +00001038
Chris Lattner9096b792007-08-02 22:33:49 +00001039 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +00001040 "getComponentType(): Illegal accessor");
1041 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +00001042}
Steve Naroffba67f692007-07-30 03:29:09 +00001043
Chris Lattnera0d03a72007-08-03 17:31:20 +00001044/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001045/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001046bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001047 const char *compStr = Accessor.getName();
1048 unsigned length = strlen(compStr);
1049
1050 for (unsigned i = 0; i < length-1; i++) {
1051 const char *s = compStr+i;
1052 for (const char c = *s++; *s; s++)
1053 if (c == *s)
1054 return true;
1055 }
1056 return false;
1057}
Chris Lattner42158e72007-08-02 23:36:59 +00001058
1059/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001060unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +00001061 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +00001062 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +00001063
1064 unsigned Result = 0;
1065
1066 while (length--) {
1067 Result <<= 2;
1068 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1069 assert(Idx != -1 && "Invalid accessor letter");
1070 Result |= Idx;
1071 }
1072 return Result;
1073}
1074
Steve Naroff4ed9d662007-09-27 14:38:14 +00001075// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001076ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001077 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001078 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001079 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001080 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1081 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001082 NumArgs = nargs;
1083 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001084 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001085 if (NumArgs) {
1086 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001087 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1088 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001089 LBracloc = LBrac;
1090 RBracloc = RBrac;
1091}
1092
Steve Naroff4ed9d662007-09-27 14:38:14 +00001093// constructor for class messages.
1094// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001095ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001096 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001097 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001098 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001099 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1100 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001101 NumArgs = nargs;
1102 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +00001103 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +00001104 if (NumArgs) {
1105 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001106 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1107 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001108 LBracloc = LBrac;
1109 RBracloc = RBrac;
1110}
1111
Chris Lattnerf624cd22007-10-25 00:29:32 +00001112
1113bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1114 llvm::APSInt CondVal(32);
1115 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1116 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1117 return CondVal != 0;
1118}
1119
Anders Carlsson52774ad2008-01-29 15:56:48 +00001120static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1121{
1122 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1123 QualType Ty = ME->getBase()->getType();
1124
1125 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner8cd0e932008-03-05 18:54:05 +00001126 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001127 FieldDecl *FD = ME->getMemberDecl();
1128
1129 // FIXME: This is linear time.
1130 unsigned i = 0, e = 0;
1131 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1132 if (RD->getMember(i) == FD)
1133 break;
1134 }
1135
1136 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1137 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1138 const Expr *Base = ASE->getBase();
1139 llvm::APSInt Idx(32);
1140 bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1141 assert(ICE && "Array index is not a constant integer!");
1142
Chris Lattner8cd0e932008-03-05 18:54:05 +00001143 int64_t size = C.getTypeSize(ASE->getType());
Anders Carlsson52774ad2008-01-29 15:56:48 +00001144 size *= Idx.getSExtValue();
1145
1146 return size + evaluateOffsetOf(C, Base);
1147 } else if (isa<CompoundLiteralExpr>(E))
1148 return 0;
1149
1150 assert(0 && "Unknown offsetof subexpression!");
1151 return 0;
1152}
1153
1154int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1155{
1156 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1157
Chris Lattner8cd0e932008-03-05 18:54:05 +00001158 unsigned CharSize = C.Target.getCharWidth();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001159 return ::evaluateOffsetOf(C, Val) / CharSize;
1160}
1161
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001162//===----------------------------------------------------------------------===//
1163// Child Iterators for iterating over subexpressions/substatements
1164//===----------------------------------------------------------------------===//
1165
1166// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001167Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1168Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001169
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001170// ObjCIvarRefExpr
1171Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1172Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1173
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001174// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001175Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1176Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001177
1178// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001179Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1180Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001181
1182// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001183Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1184Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001185
1186// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001187Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1188Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001189
Chris Lattner1de66eb2007-08-26 03:42:43 +00001190// ImaginaryLiteral
1191Stmt::child_iterator ImaginaryLiteral::child_begin() {
1192 return reinterpret_cast<Stmt**>(&Val);
1193}
1194Stmt::child_iterator ImaginaryLiteral::child_end() {
1195 return reinterpret_cast<Stmt**>(&Val)+1;
1196}
1197
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001198// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001199Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1200Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001201
1202// ParenExpr
1203Stmt::child_iterator ParenExpr::child_begin() {
1204 return reinterpret_cast<Stmt**>(&Val);
1205}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001206Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001207 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001208}
1209
1210// UnaryOperator
1211Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001212 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001213}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001214Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001215 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001216}
1217
1218// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001219Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001220 // If the type is a VLA type (and not a typedef), the size expression of the
1221 // VLA needs to be treated as an executable expression.
1222 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1223 return child_iterator(T);
1224 else
1225 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001226}
1227Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001228 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001229}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001230
1231// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001232Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001233 return reinterpret_cast<Stmt**>(&SubExprs);
1234}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001235Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001236 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001237}
1238
1239// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001240Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001241 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001242}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001243Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001244 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001245}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001246
1247// MemberExpr
1248Stmt::child_iterator MemberExpr::child_begin() {
1249 return reinterpret_cast<Stmt**>(&Base);
1250}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001251Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001252 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001253}
1254
1255// OCUVectorElementExpr
1256Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1257 return reinterpret_cast<Stmt**>(&Base);
1258}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001259Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001260 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001261}
1262
1263// CompoundLiteralExpr
1264Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1265 return reinterpret_cast<Stmt**>(&Init);
1266}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001267Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001268 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001269}
1270
1271// ImplicitCastExpr
1272Stmt::child_iterator ImplicitCastExpr::child_begin() {
1273 return reinterpret_cast<Stmt**>(&Op);
1274}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001275Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001276 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001277}
1278
1279// CastExpr
1280Stmt::child_iterator CastExpr::child_begin() {
1281 return reinterpret_cast<Stmt**>(&Op);
1282}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001283Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001284 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001285}
1286
1287// BinaryOperator
1288Stmt::child_iterator BinaryOperator::child_begin() {
1289 return reinterpret_cast<Stmt**>(&SubExprs);
1290}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001291Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001292 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001293}
1294
1295// ConditionalOperator
1296Stmt::child_iterator ConditionalOperator::child_begin() {
1297 return reinterpret_cast<Stmt**>(&SubExprs);
1298}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001299Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001300 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001301}
1302
1303// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001304Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1305Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001306
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001307// StmtExpr
1308Stmt::child_iterator StmtExpr::child_begin() {
1309 return reinterpret_cast<Stmt**>(&SubStmt);
1310}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001311Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001312 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001313}
1314
1315// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001316Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1317 return child_iterator();
1318}
1319
1320Stmt::child_iterator TypesCompatibleExpr::child_end() {
1321 return child_iterator();
1322}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001323
1324// ChooseExpr
1325Stmt::child_iterator ChooseExpr::child_begin() {
1326 return reinterpret_cast<Stmt**>(&SubExprs);
1327}
1328
1329Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001330 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001331}
1332
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001333// OverloadExpr
1334Stmt::child_iterator OverloadExpr::child_begin() {
1335 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1336}
1337Stmt::child_iterator OverloadExpr::child_end() {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001338 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001339}
1340
Anders Carlsson36760332007-10-15 20:28:48 +00001341// VAArgExpr
1342Stmt::child_iterator VAArgExpr::child_begin() {
1343 return reinterpret_cast<Stmt**>(&Val);
1344}
1345
1346Stmt::child_iterator VAArgExpr::child_end() {
1347 return reinterpret_cast<Stmt**>(&Val)+1;
1348}
1349
Anders Carlsson762b7c72007-08-31 04:56:16 +00001350// InitListExpr
1351Stmt::child_iterator InitListExpr::child_begin() {
1352 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1353}
1354Stmt::child_iterator InitListExpr::child_end() {
1355 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1356}
1357
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001358// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001359Stmt::child_iterator ObjCStringLiteral::child_begin() {
1360 return child_iterator();
1361}
1362Stmt::child_iterator ObjCStringLiteral::child_end() {
1363 return child_iterator();
1364}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001365
1366// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001367Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1368Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001369
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001370// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001371Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1372 return child_iterator();
1373}
1374Stmt::child_iterator ObjCSelectorExpr::child_end() {
1375 return child_iterator();
1376}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001377
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001378// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001379Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1380 return child_iterator();
1381}
1382Stmt::child_iterator ObjCProtocolExpr::child_end() {
1383 return child_iterator();
1384}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001385
Steve Naroffc39ca262007-09-18 23:55:05 +00001386// ObjCMessageExpr
1387Stmt::child_iterator ObjCMessageExpr::child_begin() {
1388 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1389}
1390Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001391 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001392}
1393