blob: f979c7aa7b5e1cbefcf97d27c916d93637b10c7b [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
Chris Lattner4b009652007-07-25 00:24:17 +0000360 if (TR->isReferenceType()) // C++ [expr]
361 return LV_Valid;
362
363 // the type looks fine, now check the expression
364 switch (getStmtClass()) {
365 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000366 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000367 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
368 // For vectors, make sure base is an lvalue (i.e. not a function call).
369 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
370 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
371 return LV_Valid;
372 case DeclRefExprClass: // C99 6.5.1p2
373 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
374 return LV_Valid;
375 break;
376 case MemberExprClass: { // C99 6.5.2.3p4
377 const MemberExpr *m = cast<MemberExpr>(this);
378 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
379 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000380 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000381 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000382 return LV_Valid; // C99 6.5.3p4
383
384 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
385 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
386 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000387 break;
388 case ParenExprClass: // C99 6.5.1p5
389 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffc7c66532007-12-05 04:00:10 +0000390 case CompoundLiteralExprClass: // C99 6.5.2.5p5
391 return LV_Valid;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000392 case OCUVectorElementExprClass:
393 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000394 return LV_DuplicateVectorComponents;
395 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000396 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
397 return LV_Valid;
Chris Lattner7e637512008-01-12 08:14:25 +0000398 case PreDefinedExprClass:
399 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000400 default:
401 break;
402 }
403 return LV_InvalidExpression;
404}
405
406/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
407/// does not have an incomplete type, does not have a const-qualified type, and
408/// if it is a structure or union, does not have any member (including,
409/// recursively, any member or element of all contained aggregates or unions)
410/// with a const-qualified type.
411Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
412 isLvalueResult lvalResult = isLvalue();
413
414 switch (lvalResult) {
415 case LV_Valid: break;
416 case LV_NotObjectType: return MLV_NotObjectType;
417 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000418 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000419 case LV_InvalidExpression: return MLV_InvalidExpression;
420 }
421 if (TR.isConstQualified())
422 return MLV_ConstQualified;
423 if (TR->isArrayType())
424 return MLV_ArrayType;
425 if (TR->isIncompleteType())
426 return MLV_IncompleteType;
427
428 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
429 if (r->hasConstFields())
430 return MLV_ConstQualified;
431 }
432 return MLV_Valid;
433}
434
Chris Lattner743ec372007-11-27 21:35:27 +0000435/// hasStaticStorage - Return true if this expression has static storage
436/// duration. This means that the address of this expression is a link-time
437/// constant.
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000438bool Expr::hasStaticStorage() const {
439 switch (getStmtClass()) {
440 default:
441 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000442 case ParenExprClass:
443 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
444 case ImplicitCastExprClass:
445 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000446 case CompoundLiteralExprClass:
447 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000448 case DeclRefExprClass: {
449 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
450 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
451 return VD->hasStaticStorage();
452 return false;
453 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000454 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000455 const MemberExpr *M = cast<MemberExpr>(this);
456 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000457 }
Chris Lattner743ec372007-11-27 21:35:27 +0000458 case ArraySubscriptExprClass:
459 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattner7e637512008-01-12 08:14:25 +0000460 case PreDefinedExprClass:
461 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000462 }
463}
464
Ted Kremenek87e30c52008-01-17 16:57:34 +0000465Expr* Expr::IgnoreParens() {
466 Expr* E = this;
467 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
468 E = P->getSubExpr();
469
470 return E;
471}
472
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000473bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000474 switch (getStmtClass()) {
475 default:
476 if (Loc) *Loc = getLocStart();
477 return false;
478 case ParenExprClass:
479 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
480 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000481 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000482 case FloatingLiteralClass:
483 case IntegerLiteralClass:
484 case CharacterLiteralClass:
485 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000486 case TypesCompatibleExprClass:
487 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000488 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000489 case CallExprClass: {
490 const CallExpr *CE = cast<CallExpr>(this);
491 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000492 Result.zextOrTrunc(
493 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000494 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000495 return true;
Steve Naroff44aec4c2008-01-31 01:07:12 +0000496 if (CE->isBuiltinConstantExpr())
497 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000498 if (Loc) *Loc = getLocStart();
499 return false;
500 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000501 case DeclRefExprClass: {
502 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
503 // Accept address of function.
504 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000505 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000506 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000507 if (isa<VarDecl>(D))
508 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000509 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000510 }
Steve Narofff91f9722008-01-09 00:05:37 +0000511 case CompoundLiteralExprClass:
512 if (Loc) *Loc = getLocStart();
513 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemanc4e28e42008-01-25 05:34:48 +0000514 // Allow "(vector type){2,4}" since the elements are all constant.
515 return TR->isArrayType() || TR->isVectorType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000516 case UnaryOperatorClass: {
517 const UnaryOperator *Exp = cast<UnaryOperator>(this);
518
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000519 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000520 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
521 if (!Exp->getSubExpr()->hasStaticStorage()) {
522 if (Loc) *Loc = getLocStart();
523 return false;
524 }
525 return true;
526 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000527
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000528 // Get the operand value. If this is sizeof/alignof, do not evalute the
529 // operand. This affects C99 6.6p3.
Steve Narofff0b23542008-01-10 22:15:12 +0000530 if (!Exp->isSizeOfAlignOfOp() &&
531 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000532 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
533 return false;
534
535 switch (Exp->getOpcode()) {
536 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
537 // See C99 6.6p3.
538 default:
539 if (Loc) *Loc = Exp->getOperatorLoc();
540 return false;
541 case UnaryOperator::Extension:
542 return true; // FIXME: this is wrong.
543 case UnaryOperator::SizeOf:
544 case UnaryOperator::AlignOf:
Steve Narofff0b23542008-01-10 22:15:12 +0000545 case UnaryOperator::OffsetOf:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000546 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000547 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
548 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000549 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000550 }
Chris Lattner06db6132007-10-18 00:20:32 +0000551 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000552 case UnaryOperator::LNot:
553 case UnaryOperator::Plus:
554 case UnaryOperator::Minus:
555 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000556 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000557 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000558 }
559 case SizeOfAlignOfTypeExprClass: {
560 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
561 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000562 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
563 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000564 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000565 }
Chris Lattner06db6132007-10-18 00:20:32 +0000566 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000567 }
568 case BinaryOperatorClass: {
569 const BinaryOperator *Exp = cast<BinaryOperator>(this);
570
571 // The LHS of a constant expr is always evaluated and needed.
572 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
573 return false;
574
575 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
576 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000577 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000578 }
579 case ImplicitCastExprClass:
580 case CastExprClass: {
581 const Expr *SubExpr;
582 SourceLocation CastLoc;
583 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
584 SubExpr = C->getSubExpr();
585 CastLoc = C->getLParenLoc();
586 } else {
587 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
588 CastLoc = getLocStart();
589 }
590 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
591 if (Loc) *Loc = SubExpr->getLocStart();
592 return false;
593 }
Chris Lattner06db6132007-10-18 00:20:32 +0000594 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000595 }
596 case ConditionalOperatorClass: {
597 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000598 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000599 // Handle the GNU extension for missing LHS.
600 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000601 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000602 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000603 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000604 }
Steve Narofff0b23542008-01-10 22:15:12 +0000605 case InitListExprClass: {
606 const InitListExpr *Exp = cast<InitListExpr>(this);
607 unsigned numInits = Exp->getNumInits();
608 for (unsigned i = 0; i < numInits; i++) {
609 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
610 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
611 return false;
612 }
613 }
614 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000615 }
Steve Narofff0b23542008-01-10 22:15:12 +0000616 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000617}
618
Chris Lattner4b009652007-07-25 00:24:17 +0000619/// isIntegerConstantExpr - this recursive routine will test if an expression is
620/// an integer constant expression. Note: With the introduction of VLA's in
621/// C99 the result of the sizeof operator is no longer always a constant
622/// expression. The generalization of the wording to include any subexpression
623/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
624/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
625/// "0 || f()" can be treated as a constant expression. In C90 this expression,
626/// occurring in a context requiring a constant, would have been a constraint
627/// violation. FIXME: This routine currently implements C90 semantics.
628/// To properly implement C99 semantics this routine will need to evaluate
629/// expressions involving operators previously mentioned.
630
631/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
632/// comma, etc
633///
634/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000635/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000636///
637/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
638/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
639/// cast+dereference.
640bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
641 SourceLocation *Loc, bool isEvaluated) const {
642 switch (getStmtClass()) {
643 default:
644 if (Loc) *Loc = getLocStart();
645 return false;
646 case ParenExprClass:
647 return cast<ParenExpr>(this)->getSubExpr()->
648 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
649 case IntegerLiteralClass:
650 Result = cast<IntegerLiteral>(this)->getValue();
651 break;
652 case CharacterLiteralClass: {
653 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000654 Result.zextOrTrunc(
655 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000656 Result = CL->getValue();
657 Result.setIsUnsigned(!getType()->isSignedIntegerType());
658 break;
659 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000660 case TypesCompatibleExprClass: {
661 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000662 Result.zextOrTrunc(
663 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000664 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000665 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000666 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000667 case CallExprClass: {
668 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000669 Result.zextOrTrunc(
670 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000671 if (CE->isBuiltinClassifyType(Result))
672 break;
673 if (Loc) *Loc = getLocStart();
674 return false;
675 }
Chris Lattner4b009652007-07-25 00:24:17 +0000676 case DeclRefExprClass:
677 if (const EnumConstantDecl *D =
678 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
679 Result = D->getInitVal();
680 break;
681 }
682 if (Loc) *Loc = getLocStart();
683 return false;
684 case UnaryOperatorClass: {
685 const UnaryOperator *Exp = cast<UnaryOperator>(this);
686
687 // Get the operand value. If this is sizeof/alignof, do not evalute the
688 // operand. This affects C99 6.6p3.
Anders Carlsson52774ad2008-01-29 15:56:48 +0000689 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner5a9b6242007-08-23 21:42:50 +0000690 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000691 return false;
692
693 switch (Exp->getOpcode()) {
694 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
695 // See C99 6.6p3.
696 default:
697 if (Loc) *Loc = Exp->getOperatorLoc();
698 return false;
699 case UnaryOperator::Extension:
700 return true; // FIXME: this is wrong.
701 case UnaryOperator::SizeOf:
702 case UnaryOperator::AlignOf:
703 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000704 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
705 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000706 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000707 }
Chris Lattner4b009652007-07-25 00:24:17 +0000708
709 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000710 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000711 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
712 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000713
714 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000715 if (Exp->getSubExpr()->getType()->isFunctionType()) {
716 // GCC extension: sizeof(function) = 1.
717 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
718 } else if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner4b009652007-07-25 00:24:17 +0000719 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
720 Exp->getOperatorLoc());
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000721 } else {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000722 unsigned CharSize =
723 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
724
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000725 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
726 Exp->getOperatorLoc()) / CharSize;
727 }
Chris Lattner4b009652007-07-25 00:24:17 +0000728 break;
729 case UnaryOperator::LNot: {
Chris Lattnerf00fdc02008-01-25 19:16:19 +0000730 bool Val = Result == 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000731 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000732 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
733 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000734 Result = Val;
735 break;
736 }
737 case UnaryOperator::Plus:
738 break;
739 case UnaryOperator::Minus:
740 Result = -Result;
741 break;
742 case UnaryOperator::Not:
743 Result = ~Result;
744 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000745 case UnaryOperator::OffsetOf:
746 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000747 }
748 break;
749 }
750 case SizeOfAlignOfTypeExprClass: {
751 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
752 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000753 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
754 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000755 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000756 }
Chris Lattner4b009652007-07-25 00:24:17 +0000757
758 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000759 Result.zextOrTrunc(
760 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000761
762 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000763 if (Exp->getArgumentType()->isFunctionType()) {
764 // GCC extension: sizeof(function) = 1.
765 Result = Exp->isSizeOf() ? 1 : 4;
766 } else if (Exp->isSizeOf()) {
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000767 unsigned CharSize =
768 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
769
770 Result = Ctx.getTypeSize(Exp->getArgumentType(),
771 Exp->getOperatorLoc()) / CharSize;
772 }
Chris Lattner4b009652007-07-25 00:24:17 +0000773 else
774 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000775
Chris Lattner4b009652007-07-25 00:24:17 +0000776 break;
777 }
778 case BinaryOperatorClass: {
779 const BinaryOperator *Exp = cast<BinaryOperator>(this);
780
781 // The LHS of a constant expr is always evaluated and needed.
782 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
783 return false;
784
785 llvm::APSInt RHS(Result);
786
787 // The short-circuiting &&/|| operators don't necessarily evaluate their
788 // RHS. Make sure to pass isEvaluated down correctly.
789 if (Exp->isLogicalOp()) {
790 bool RHSEval;
791 if (Exp->getOpcode() == BinaryOperator::LAnd)
792 RHSEval = Result != 0;
793 else {
794 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
795 RHSEval = Result == 0;
796 }
797
798 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
799 isEvaluated & RHSEval))
800 return false;
801 } else {
802 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
803 return false;
804 }
805
806 switch (Exp->getOpcode()) {
807 default:
808 if (Loc) *Loc = getLocStart();
809 return false;
810 case BinaryOperator::Mul:
811 Result *= RHS;
812 break;
813 case BinaryOperator::Div:
814 if (RHS == 0) {
815 if (!isEvaluated) break;
816 if (Loc) *Loc = getLocStart();
817 return false;
818 }
819 Result /= RHS;
820 break;
821 case BinaryOperator::Rem:
822 if (RHS == 0) {
823 if (!isEvaluated) break;
824 if (Loc) *Loc = getLocStart();
825 return false;
826 }
827 Result %= RHS;
828 break;
829 case BinaryOperator::Add: Result += RHS; break;
830 case BinaryOperator::Sub: Result -= RHS; break;
831 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000832 Result <<=
833 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000834 break;
835 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000836 Result >>=
837 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000838 break;
839 case BinaryOperator::LT: Result = Result < RHS; break;
840 case BinaryOperator::GT: Result = Result > RHS; break;
841 case BinaryOperator::LE: Result = Result <= RHS; break;
842 case BinaryOperator::GE: Result = Result >= RHS; break;
843 case BinaryOperator::EQ: Result = Result == RHS; break;
844 case BinaryOperator::NE: Result = Result != RHS; break;
845 case BinaryOperator::And: Result &= RHS; break;
846 case BinaryOperator::Xor: Result ^= RHS; break;
847 case BinaryOperator::Or: Result |= RHS; break;
848 case BinaryOperator::LAnd:
849 Result = Result != 0 && RHS != 0;
850 break;
851 case BinaryOperator::LOr:
852 Result = Result != 0 || RHS != 0;
853 break;
854
855 case BinaryOperator::Comma:
856 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
857 // *except* when they are contained within a subexpression that is not
858 // evaluated". Note that Assignment can never happen due to constraints
859 // on the LHS subexpr, so we don't need to check it here.
860 if (isEvaluated) {
861 if (Loc) *Loc = getLocStart();
862 return false;
863 }
864
865 // The result of the constant expr is the RHS.
866 Result = RHS;
867 return true;
868 }
869
870 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
871 break;
872 }
873 case ImplicitCastExprClass:
874 case CastExprClass: {
875 const Expr *SubExpr;
876 SourceLocation CastLoc;
877 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
878 SubExpr = C->getSubExpr();
879 CastLoc = C->getLParenLoc();
880 } else {
881 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
882 CastLoc = getLocStart();
883 }
884
885 // C99 6.6p6: shall only convert arithmetic types to integer types.
886 if (!SubExpr->getType()->isArithmeticType() ||
887 !getType()->isIntegerType()) {
888 if (Loc) *Loc = SubExpr->getLocStart();
889 return false;
890 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000891
892 uint32_t DestWidth =
893 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
894
Chris Lattner4b009652007-07-25 00:24:17 +0000895 // Handle simple integer->integer casts.
896 if (SubExpr->getType()->isIntegerType()) {
897 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
898 return false;
899
900 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000901 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +0000902 if (getType()->isBooleanType()) {
903 // Conversion to bool compares against zero.
904 Result = Result != 0;
905 Result.zextOrTrunc(DestWidth);
906 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +0000907 Result.sextOrTrunc(DestWidth);
908 else // If the input is unsigned, do a zero extend, noop, or truncate.
909 Result.zextOrTrunc(DestWidth);
910 break;
911 }
912
913 // Allow floating constants that are the immediate operands of casts or that
914 // are parenthesized.
915 const Expr *Operand = SubExpr;
916 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
917 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000918
919 // If this isn't a floating literal, we can't handle it.
920 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
921 if (!FL) {
922 if (Loc) *Loc = Operand->getLocStart();
923 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000924 }
Chris Lattner000c4102008-01-09 18:59:34 +0000925
926 // If the destination is boolean, compare against zero.
927 if (getType()->isBooleanType()) {
928 Result = !FL->getValue().isZero();
929 Result.zextOrTrunc(DestWidth);
930 break;
931 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000932
933 // Determine whether we are converting to unsigned or signed.
934 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000935
936 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
937 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000938 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000939 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
940 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000941 Result = llvm::APInt(DestWidth, 4, Space);
942 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000943 }
944 case ConditionalOperatorClass: {
945 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
946
947 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
948 return false;
949
950 const Expr *TrueExp = Exp->getLHS();
951 const Expr *FalseExp = Exp->getRHS();
952 if (Result == 0) std::swap(TrueExp, FalseExp);
953
954 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000955 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000956 return false;
957 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000958 if (TrueExp &&
959 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000960 return false;
961 break;
962 }
963 }
964
965 // Cases that are valid constant exprs fall through to here.
966 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
967 return true;
968}
969
Chris Lattner4b009652007-07-25 00:24:17 +0000970/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
971/// integer constant expression with the value zero, or if this is one that is
972/// cast to void*.
973bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffa2e53222008-01-14 16:10:57 +0000974 // Strip off a cast to void*, if it exists.
975 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
976 // Check that it is a cast to void*.
Chris Lattner4b009652007-07-25 00:24:17 +0000977 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
978 QualType Pointee = PT->getPointeeType();
Steve Naroffa2e53222008-01-14 16:10:57 +0000979 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
980 CE->getSubExpr()->getType()->isIntegerType()) // from int.
981 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000982 }
Steve Naroffa2e53222008-01-14 16:10:57 +0000983 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
984 // Ignore the ImplicitCastExpr type entirely.
985 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
986 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
987 // Accept ((void*)0) as a null pointer constant, as many other
988 // implementations do.
989 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Narofff33a9852008-01-14 02:53:34 +0000990 }
Steve Naroffa2e53222008-01-14 16:10:57 +0000991
992 // This expression must be an integer type.
993 if (!getType()->isIntegerType())
994 return false;
995
Chris Lattner4b009652007-07-25 00:24:17 +0000996 // If we have an integer constant expression, we need to *evaluate* it and
997 // test for the value 0.
998 llvm::APSInt Val(32);
Steve Naroffa2e53222008-01-14 16:10:57 +0000999 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001000}
Steve Naroffc11705f2007-07-28 23:10:27 +00001001
Chris Lattnera0d03a72007-08-03 17:31:20 +00001002unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +00001003 return strlen(Accessor.getName());
1004}
1005
1006
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001007/// getComponentType - Determine whether the components of this access are
1008/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001009OCUVectorElementExpr::ElementType
1010OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +00001011 // derive the component type, no need to waste space.
1012 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +00001013
Chris Lattner9096b792007-08-02 22:33:49 +00001014 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
1015 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +00001016
Chris Lattner9096b792007-08-02 22:33:49 +00001017 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +00001018 "getComponentType(): Illegal accessor");
1019 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +00001020}
Steve Naroffba67f692007-07-30 03:29:09 +00001021
Chris Lattnera0d03a72007-08-03 17:31:20 +00001022/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001023/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001024bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001025 const char *compStr = Accessor.getName();
1026 unsigned length = strlen(compStr);
1027
1028 for (unsigned i = 0; i < length-1; i++) {
1029 const char *s = compStr+i;
1030 for (const char c = *s++; *s; s++)
1031 if (c == *s)
1032 return true;
1033 }
1034 return false;
1035}
Chris Lattner42158e72007-08-02 23:36:59 +00001036
1037/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001038unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +00001039 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +00001040 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +00001041
1042 unsigned Result = 0;
1043
1044 while (length--) {
1045 Result <<= 2;
1046 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1047 assert(Idx != -1 && "Invalid accessor letter");
1048 Result |= Idx;
1049 }
1050 return Result;
1051}
1052
Steve Naroff4ed9d662007-09-27 14:38:14 +00001053// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001054ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001055 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001056 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001057 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001058 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1059 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001060 NumArgs = nargs;
1061 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001062 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001063 if (NumArgs) {
1064 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001065 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1066 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001067 LBracloc = LBrac;
1068 RBracloc = RBrac;
1069}
1070
Steve Naroff4ed9d662007-09-27 14:38:14 +00001071// constructor for class messages.
1072// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001073ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001074 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001075 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001076 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001077 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1078 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001079 NumArgs = nargs;
1080 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +00001081 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +00001082 if (NumArgs) {
1083 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001084 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1085 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001086 LBracloc = LBrac;
1087 RBracloc = RBrac;
1088}
1089
Chris Lattnerf624cd22007-10-25 00:29:32 +00001090
1091bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1092 llvm::APSInt CondVal(32);
1093 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1094 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1095 return CondVal != 0;
1096}
1097
Anders Carlsson52774ad2008-01-29 15:56:48 +00001098static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1099{
1100 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1101 QualType Ty = ME->getBase()->getType();
1102
1103 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
1104 const ASTRecordLayout &RL = C.getASTRecordLayout(RD, SourceLocation());
1105 FieldDecl *FD = ME->getMemberDecl();
1106
1107 // FIXME: This is linear time.
1108 unsigned i = 0, e = 0;
1109 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1110 if (RD->getMember(i) == FD)
1111 break;
1112 }
1113
1114 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1115 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1116 const Expr *Base = ASE->getBase();
1117 llvm::APSInt Idx(32);
1118 bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1119 assert(ICE && "Array index is not a constant integer!");
1120
1121 int64_t size = C.getTypeSize(ASE->getType(), SourceLocation());
1122 size *= Idx.getSExtValue();
1123
1124 return size + evaluateOffsetOf(C, Base);
1125 } else if (isa<CompoundLiteralExpr>(E))
1126 return 0;
1127
1128 assert(0 && "Unknown offsetof subexpression!");
1129 return 0;
1130}
1131
1132int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1133{
1134 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1135
1136 unsigned CharSize =
1137 C.Target.getCharWidth(C.getFullLoc(getOperatorLoc()));
1138
1139 return ::evaluateOffsetOf(C, Val) / CharSize;
1140}
1141
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001142//===----------------------------------------------------------------------===//
1143// Child Iterators for iterating over subexpressions/substatements
1144//===----------------------------------------------------------------------===//
1145
1146// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001147Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1148Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001149
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001150// ObjCIvarRefExpr
1151Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1152Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1153
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001154// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001155Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1156Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001157
1158// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001159Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1160Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001161
1162// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001163Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1164Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001165
1166// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001167Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1168Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001169
Chris Lattner1de66eb2007-08-26 03:42:43 +00001170// ImaginaryLiteral
1171Stmt::child_iterator ImaginaryLiteral::child_begin() {
1172 return reinterpret_cast<Stmt**>(&Val);
1173}
1174Stmt::child_iterator ImaginaryLiteral::child_end() {
1175 return reinterpret_cast<Stmt**>(&Val)+1;
1176}
1177
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001178// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001179Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1180Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001181
1182// ParenExpr
1183Stmt::child_iterator ParenExpr::child_begin() {
1184 return reinterpret_cast<Stmt**>(&Val);
1185}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001186Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001187 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001188}
1189
1190// UnaryOperator
1191Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001192 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001193}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001194Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001195 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001196}
1197
1198// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001199Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001200 // If the type is a VLA type (and not a typedef), the size expression of the
1201 // VLA needs to be treated as an executable expression.
1202 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1203 return child_iterator(T);
1204 else
1205 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001206}
1207Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001208 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001209}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001210
1211// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001212Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001213 return reinterpret_cast<Stmt**>(&SubExprs);
1214}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001215Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001216 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001217}
1218
1219// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001220Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001221 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001222}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001223Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001224 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001225}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001226
1227// MemberExpr
1228Stmt::child_iterator MemberExpr::child_begin() {
1229 return reinterpret_cast<Stmt**>(&Base);
1230}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001231Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001232 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001233}
1234
1235// OCUVectorElementExpr
1236Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1237 return reinterpret_cast<Stmt**>(&Base);
1238}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001239Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001240 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001241}
1242
1243// CompoundLiteralExpr
1244Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1245 return reinterpret_cast<Stmt**>(&Init);
1246}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001247Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001248 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001249}
1250
1251// ImplicitCastExpr
1252Stmt::child_iterator ImplicitCastExpr::child_begin() {
1253 return reinterpret_cast<Stmt**>(&Op);
1254}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001255Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001256 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001257}
1258
1259// CastExpr
1260Stmt::child_iterator CastExpr::child_begin() {
1261 return reinterpret_cast<Stmt**>(&Op);
1262}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001263Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001264 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001265}
1266
1267// BinaryOperator
1268Stmt::child_iterator BinaryOperator::child_begin() {
1269 return reinterpret_cast<Stmt**>(&SubExprs);
1270}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001271Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001272 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001273}
1274
1275// ConditionalOperator
1276Stmt::child_iterator ConditionalOperator::child_begin() {
1277 return reinterpret_cast<Stmt**>(&SubExprs);
1278}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001279Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001280 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001281}
1282
1283// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001284Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1285Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001286
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001287// StmtExpr
1288Stmt::child_iterator StmtExpr::child_begin() {
1289 return reinterpret_cast<Stmt**>(&SubStmt);
1290}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001291Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001292 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001293}
1294
1295// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001296Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1297 return child_iterator();
1298}
1299
1300Stmt::child_iterator TypesCompatibleExpr::child_end() {
1301 return child_iterator();
1302}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001303
1304// ChooseExpr
1305Stmt::child_iterator ChooseExpr::child_begin() {
1306 return reinterpret_cast<Stmt**>(&SubExprs);
1307}
1308
1309Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001310 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001311}
1312
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001313// OverloadExpr
1314Stmt::child_iterator OverloadExpr::child_begin() {
1315 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1316}
1317Stmt::child_iterator OverloadExpr::child_end() {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001318 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001319}
1320
Anders Carlsson36760332007-10-15 20:28:48 +00001321// VAArgExpr
1322Stmt::child_iterator VAArgExpr::child_begin() {
1323 return reinterpret_cast<Stmt**>(&Val);
1324}
1325
1326Stmt::child_iterator VAArgExpr::child_end() {
1327 return reinterpret_cast<Stmt**>(&Val)+1;
1328}
1329
Anders Carlsson762b7c72007-08-31 04:56:16 +00001330// InitListExpr
1331Stmt::child_iterator InitListExpr::child_begin() {
1332 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1333}
1334Stmt::child_iterator InitListExpr::child_end() {
1335 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1336}
1337
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001338// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001339Stmt::child_iterator ObjCStringLiteral::child_begin() {
1340 return child_iterator();
1341}
1342Stmt::child_iterator ObjCStringLiteral::child_end() {
1343 return child_iterator();
1344}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001345
1346// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001347Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1348Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001349
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001350// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001351Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1352 return child_iterator();
1353}
1354Stmt::child_iterator ObjCSelectorExpr::child_end() {
1355 return child_iterator();
1356}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001357
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001358// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001359Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1360 return child_iterator();
1361}
1362Stmt::child_iterator ObjCProtocolExpr::child_end() {
1363 return child_iterator();
1364}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001365
Steve Naroffc39ca262007-09-18 23:55:05 +00001366// ObjCMessageExpr
1367Stmt::child_iterator ObjCMessageExpr::child_begin() {
1368 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1369}
1370Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001371 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001372}
1373