blob: 8aa5eed80b4e2b1f4f12b95424607bc9becc891a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/StmtVisitor.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000018#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner73d0d4f2007-08-30 17:45:32 +000073 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +000074 }
75}
76
77//===----------------------------------------------------------------------===//
78// Postfix Operators.
79//===----------------------------------------------------------------------===//
80
Nate Begemane2ce1d92008-01-17 17:46:27 +000081
Reid Spencer5f016e22007-07-11 17:01:13 +000082CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
83 SourceLocation rparenloc)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000084 : Expr(CallExprClass, t), NumArgs(numargs) {
85 SubExprs = new Expr*[numargs+1];
86 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +000087 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000088 SubExprs[i+ARGS_START] = args[i];
Reid Spencer5f016e22007-07-11 17:01:13 +000089 RParenLoc = rparenloc;
90}
91
Chris Lattnerd18b3292007-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
121
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000122bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
123 // The following enum mimics gcc's internal "typeclass.h" file.
124 enum gcc_type_class {
125 no_type_class = -1,
126 void_type_class, integer_type_class, char_type_class,
127 enumeral_type_class, boolean_type_class,
128 pointer_type_class, reference_type_class, offset_type_class,
129 real_type_class, complex_type_class,
130 function_type_class, method_type_class,
131 record_type_class, union_type_class,
132 array_type_class, string_type_class,
133 lang_type_class
134 };
135 Result.setIsSigned(true);
136
137 // All simple function calls (e.g. func()) are implicitly cast to pointer to
138 // function. As a result, we try and obtain the DeclRefExpr from the
139 // ImplicitCastExpr.
140 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
141 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
142 return false;
143 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
144 if (!DRE)
145 return false;
146
147 // We have a DeclRefExpr.
148 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
149 // If no argument was supplied, default to "no_type_class". This isn't
150 // ideal, however it's what gcc does.
151 Result = static_cast<uint64_t>(no_type_class);
152 if (NumArgs >= 1) {
153 QualType argType = getArg(0)->getType();
154
155 if (argType->isVoidType())
156 Result = void_type_class;
157 else if (argType->isEnumeralType())
158 Result = enumeral_type_class;
159 else if (argType->isBooleanType())
160 Result = boolean_type_class;
161 else if (argType->isCharType())
162 Result = string_type_class; // gcc doesn't appear to use char_type_class
163 else if (argType->isIntegerType())
164 Result = integer_type_class;
165 else if (argType->isPointerType())
166 Result = pointer_type_class;
167 else if (argType->isReferenceType())
168 Result = reference_type_class;
169 else if (argType->isRealType())
170 Result = real_type_class;
171 else if (argType->isComplexType())
172 Result = complex_type_class;
173 else if (argType->isFunctionType())
174 Result = function_type_class;
175 else if (argType->isStructureType())
176 Result = record_type_class;
177 else if (argType->isUnionType())
178 Result = union_type_class;
179 else if (argType->isArrayType())
180 Result = array_type_class;
181 else if (argType->isUnionType())
182 Result = union_type_class;
183 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner3ef5bc02007-11-08 17:56:40 +0000184 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000185 }
186 return true;
187 }
188 return false;
189}
190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
192/// corresponds to, e.g. "<<=".
193const char *BinaryOperator::getOpcodeStr(Opcode Op) {
194 switch (Op) {
195 default: assert(0 && "Unknown binary operator");
196 case Mul: return "*";
197 case Div: return "/";
198 case Rem: return "%";
199 case Add: return "+";
200 case Sub: return "-";
201 case Shl: return "<<";
202 case Shr: return ">>";
203 case LT: return "<";
204 case GT: return ">";
205 case LE: return "<=";
206 case GE: return ">=";
207 case EQ: return "==";
208 case NE: return "!=";
209 case And: return "&";
210 case Xor: return "^";
211 case Or: return "|";
212 case LAnd: return "&&";
213 case LOr: return "||";
214 case Assign: return "=";
215 case MulAssign: return "*=";
216 case DivAssign: return "/=";
217 case RemAssign: return "%=";
218 case AddAssign: return "+=";
219 case SubAssign: return "-=";
220 case ShlAssign: return "<<=";
221 case ShrAssign: return ">>=";
222 case AndAssign: return "&=";
223 case XorAssign: return "^=";
224 case OrAssign: return "|=";
225 case Comma: return ",";
226 }
227}
228
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000229InitListExpr::InitListExpr(SourceLocation lbraceloc,
230 Expr **initexprs, unsigned numinits,
231 SourceLocation rbraceloc)
232 : Expr(InitListExprClass, QualType())
233 , NumInits(numinits)
234 , LBraceLoc(lbraceloc)
235 , RBraceLoc(rbraceloc)
236{
237 InitExprs = new Expr*[numinits];
238 for (unsigned i = 0; i != numinits; i++)
239 InitExprs[i] = initexprs[i];
240}
Reid Spencer5f016e22007-07-11 17:01:13 +0000241
242//===----------------------------------------------------------------------===//
243// Generic Expression Routines
244//===----------------------------------------------------------------------===//
245
246/// hasLocalSideEffect - Return true if this immediate expression has side
247/// effects, not counting any sub-expressions.
248bool Expr::hasLocalSideEffect() const {
249 switch (getStmtClass()) {
250 default:
251 return false;
252 case ParenExprClass:
253 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
254 case UnaryOperatorClass: {
255 const UnaryOperator *UO = cast<UnaryOperator>(this);
256
257 switch (UO->getOpcode()) {
258 default: return false;
259 case UnaryOperator::PostInc:
260 case UnaryOperator::PostDec:
261 case UnaryOperator::PreInc:
262 case UnaryOperator::PreDec:
263 return true; // ++/--
264
265 case UnaryOperator::Deref:
266 // Dereferencing a volatile pointer is a side-effect.
267 return getType().isVolatileQualified();
268 case UnaryOperator::Real:
269 case UnaryOperator::Imag:
270 // accessing a piece of a volatile complex is a side-effect.
271 return UO->getSubExpr()->getType().isVolatileQualified();
272
273 case UnaryOperator::Extension:
274 return UO->getSubExpr()->hasLocalSideEffect();
275 }
276 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000277 case BinaryOperatorClass: {
278 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
279 // Consider comma to have side effects if the LHS and RHS both do.
280 if (BinOp->getOpcode() == BinaryOperator::Comma)
281 return BinOp->getLHS()->hasLocalSideEffect() &&
282 BinOp->getRHS()->hasLocalSideEffect();
283
284 return BinOp->isAssignmentOp();
285 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000286 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000287 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000288
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000289 case ConditionalOperatorClass: {
290 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
291 return Exp->getCond()->hasLocalSideEffect()
292 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
293 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
294 }
295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 case MemberExprClass:
297 case ArraySubscriptExprClass:
298 // If the base pointer or element is to a volatile pointer/field, accessing
299 // if is a side effect.
300 return getType().isVolatileQualified();
301
302 case CallExprClass:
303 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
304 // should warn.
305 return true;
Chris Lattnera9c01022007-09-26 22:06:30 +0000306 case ObjCMessageExprClass:
307 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000308
309 case CastExprClass:
310 // If this is a cast to void, check the operand. Otherwise, the result of
311 // the cast is unused.
312 if (getType()->isVoidType())
313 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
314 return false;
315 }
316}
317
318/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
319/// incomplete type other than void. Nonarray expressions that can be lvalues:
320/// - name, where name must be a variable
321/// - e[i]
322/// - (e), where e must be an lvalue
323/// - e.name, where e must be an lvalue
324/// - e->name
325/// - *e, the type of e cannot be a function type
326/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000327/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000328/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000329///
Bill Wendlingca51c972007-07-16 07:07:56 +0000330Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000332 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 return LV_NotObjectType;
334
Steve Naroff731ec572007-07-21 13:32:03 +0000335 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000337
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000338 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000339 return LV_Valid;
340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // the type looks fine, now check the expression
342 switch (getStmtClass()) {
343 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000344 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
346 // For vectors, make sure base is an lvalue (i.e. not a function call).
347 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
348 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
349 return LV_Valid;
350 case DeclRefExprClass: // C99 6.5.1p2
351 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
352 return LV_Valid;
353 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000354 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 const MemberExpr *m = cast<MemberExpr>(this);
356 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000357 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000358 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000360 return LV_Valid; // C99 6.5.3p4
361
362 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
363 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
364 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 break;
366 case ParenExprClass: // C99 6.5.1p5
367 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffe6386392007-12-05 04:00:10 +0000368 case CompoundLiteralExprClass: // C99 6.5.2.5p5
369 return LV_Valid;
Chris Lattner6481a572007-08-03 17:31:20 +0000370 case OCUVectorElementExprClass:
371 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000372 return LV_DuplicateVectorComponents;
373 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000374 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
375 return LV_Valid;
Chris Lattnerfa28b302008-01-12 08:14:25 +0000376 case PreDefinedExprClass:
377 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 default:
379 break;
380 }
381 return LV_InvalidExpression;
382}
383
384/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
385/// does not have an incomplete type, does not have a const-qualified type, and
386/// if it is a structure or union, does not have any member (including,
387/// recursively, any member or element of all contained aggregates or unions)
388/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000389Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 isLvalueResult lvalResult = isLvalue();
391
392 switch (lvalResult) {
393 case LV_Valid: break;
394 case LV_NotObjectType: return MLV_NotObjectType;
395 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000396 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 case LV_InvalidExpression: return MLV_InvalidExpression;
398 }
399 if (TR.isConstQualified())
400 return MLV_ConstQualified;
401 if (TR->isArrayType())
402 return MLV_ArrayType;
403 if (TR->isIncompleteType())
404 return MLV_IncompleteType;
405
406 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
407 if (r->hasConstFields())
408 return MLV_ConstQualified;
409 }
410 return MLV_Valid;
411}
412
Chris Lattner4cc62712007-11-27 21:35:27 +0000413/// hasStaticStorage - Return true if this expression has static storage
414/// duration. This means that the address of this expression is a link-time
415/// constant.
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000416bool Expr::hasStaticStorage() const {
417 switch (getStmtClass()) {
418 default:
419 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000420 case ParenExprClass:
421 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
422 case ImplicitCastExprClass:
423 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000424 case CompoundLiteralExprClass:
425 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000426 case DeclRefExprClass: {
427 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
428 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
429 return VD->hasStaticStorage();
430 return false;
431 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000432 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000433 const MemberExpr *M = cast<MemberExpr>(this);
434 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000435 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000436 case ArraySubscriptExprClass:
437 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattnerfa28b302008-01-12 08:14:25 +0000438 case PreDefinedExprClass:
439 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000440 }
441}
442
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000443Expr* Expr::IgnoreParens() {
444 Expr* E = this;
445 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
446 E = P->getSubExpr();
447
448 return E;
449}
450
Steve Naroff38374b02007-09-02 20:30:18 +0000451bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff38374b02007-09-02 20:30:18 +0000452 switch (getStmtClass()) {
453 default:
454 if (Loc) *Loc = getLocStart();
455 return false;
456 case ParenExprClass:
457 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
458 case StringLiteralClass:
Steve Naroff5d37e322007-11-09 15:00:03 +0000459 case ObjCStringLiteralClass:
Steve Naroff38374b02007-09-02 20:30:18 +0000460 case FloatingLiteralClass:
461 case IntegerLiteralClass:
462 case CharacterLiteralClass:
463 case ImaginaryLiteralClass:
Anders Carlsson1a86b332007-10-17 00:52:43 +0000464 case TypesCompatibleExprClass:
465 case CXXBoolLiteralExprClass:
Chris Lattner2777e492007-10-18 00:20:32 +0000466 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000467 case CallExprClass: {
468 const CallExpr *CE = cast<CallExpr>(this);
469 llvm::APSInt Result(32);
Hartmut Kaiser86fd3552007-09-16 21:35:35 +0000470 Result.zextOrTrunc(
471 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff38374b02007-09-02 20:30:18 +0000472 if (CE->isBuiltinClassifyType(Result))
Chris Lattner2777e492007-10-18 00:20:32 +0000473 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000474 if (Loc) *Loc = getLocStart();
475 return false;
476 }
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000477 case DeclRefExprClass: {
478 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
479 // Accept address of function.
480 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner2777e492007-10-18 00:20:32 +0000481 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000482 if (Loc) *Loc = getLocStart();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000483 if (isa<VarDecl>(D))
484 return TR->isArrayType();
Steve Naroff38374b02007-09-02 20:30:18 +0000485 return false;
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000486 }
Steve Naroffb8f13a82008-01-09 00:05:37 +0000487 case CompoundLiteralExprClass:
488 if (Loc) *Loc = getLocStart();
489 // Allow "(int []){2,4}", since the array will be converted to a pointer.
490 return TR->isArrayType();
Steve Naroff38374b02007-09-02 20:30:18 +0000491 case UnaryOperatorClass: {
492 const UnaryOperator *Exp = cast<UnaryOperator>(this);
493
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000494 // C99 6.6p9
Chris Lattner239c15e2007-12-11 23:11:17 +0000495 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
496 if (!Exp->getSubExpr()->hasStaticStorage()) {
497 if (Loc) *Loc = getLocStart();
498 return false;
499 }
500 return true;
501 }
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000502
Steve Naroff38374b02007-09-02 20:30:18 +0000503 // Get the operand value. If this is sizeof/alignof, do not evalute the
504 // operand. This affects C99 6.6p3.
Steve Naroffd0091aa2008-01-10 22:15:12 +0000505 if (!Exp->isSizeOfAlignOfOp() &&
506 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff38374b02007-09-02 20:30:18 +0000507 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
508 return false;
509
510 switch (Exp->getOpcode()) {
511 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
512 // See C99 6.6p3.
513 default:
514 if (Loc) *Loc = Exp->getOperatorLoc();
515 return false;
516 case UnaryOperator::Extension:
517 return true; // FIXME: this is wrong.
518 case UnaryOperator::SizeOf:
519 case UnaryOperator::AlignOf:
Steve Naroffd0091aa2008-01-10 22:15:12 +0000520 case UnaryOperator::OffsetOf:
Steve Naroff38374b02007-09-02 20:30:18 +0000521 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000522 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
523 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000524 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000525 }
Chris Lattner2777e492007-10-18 00:20:32 +0000526 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000527 case UnaryOperator::LNot:
528 case UnaryOperator::Plus:
529 case UnaryOperator::Minus:
530 case UnaryOperator::Not:
Chris Lattner2777e492007-10-18 00:20:32 +0000531 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000532 }
Steve Naroff38374b02007-09-02 20:30:18 +0000533 }
534 case SizeOfAlignOfTypeExprClass: {
535 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
536 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000537 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
538 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000539 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000540 }
Chris Lattner2777e492007-10-18 00:20:32 +0000541 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000542 }
543 case BinaryOperatorClass: {
544 const BinaryOperator *Exp = cast<BinaryOperator>(this);
545
546 // The LHS of a constant expr is always evaluated and needed.
547 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
548 return false;
549
550 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
551 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000552 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000553 }
554 case ImplicitCastExprClass:
555 case CastExprClass: {
556 const Expr *SubExpr;
557 SourceLocation CastLoc;
558 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
559 SubExpr = C->getSubExpr();
560 CastLoc = C->getLParenLoc();
561 } else {
562 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
563 CastLoc = getLocStart();
564 }
565 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
566 if (Loc) *Loc = SubExpr->getLocStart();
567 return false;
568 }
Chris Lattner2777e492007-10-18 00:20:32 +0000569 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000570 }
571 case ConditionalOperatorClass: {
572 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner2777e492007-10-18 00:20:32 +0000573 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson39073232007-11-30 19:04:31 +0000574 // Handle the GNU extension for missing LHS.
575 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner2777e492007-10-18 00:20:32 +0000576 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff38374b02007-09-02 20:30:18 +0000577 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000578 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000579 }
Steve Naroffd0091aa2008-01-10 22:15:12 +0000580 case InitListExprClass: {
581 const InitListExpr *Exp = cast<InitListExpr>(this);
582 unsigned numInits = Exp->getNumInits();
583 for (unsigned i = 0; i < numInits; i++) {
584 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
585 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
586 return false;
587 }
588 }
589 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000590 }
Steve Naroffd0091aa2008-01-10 22:15:12 +0000591 }
Steve Naroff38374b02007-09-02 20:30:18 +0000592}
593
Reid Spencer5f016e22007-07-11 17:01:13 +0000594/// isIntegerConstantExpr - this recursive routine will test if an expression is
595/// an integer constant expression. Note: With the introduction of VLA's in
596/// C99 the result of the sizeof operator is no longer always a constant
597/// expression. The generalization of the wording to include any subexpression
598/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
599/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
600/// "0 || f()" can be treated as a constant expression. In C90 this expression,
601/// occurring in a context requiring a constant, would have been a constraint
602/// violation. FIXME: This routine currently implements C90 semantics.
603/// To properly implement C99 semantics this routine will need to evaluate
604/// expressions involving operators previously mentioned.
605
606/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
607/// comma, etc
608///
609/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000610/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000611///
612/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
613/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
614/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000615bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
616 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 switch (getStmtClass()) {
618 default:
619 if (Loc) *Loc = getLocStart();
620 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 case ParenExprClass:
622 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000623 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 case IntegerLiteralClass:
625 Result = cast<IntegerLiteral>(this)->getValue();
626 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000627 case CharacterLiteralClass: {
628 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000629 Result.zextOrTrunc(
630 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000631 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000632 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000634 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000635 case TypesCompatibleExprClass: {
636 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000637 Result.zextOrTrunc(
638 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffec0550f2007-10-15 20:41:53 +0000639 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff389cecc2007-08-02 00:13:27 +0000640 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000641 }
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000642 case CallExprClass: {
643 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000644 Result.zextOrTrunc(
645 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000646 if (CE->isBuiltinClassifyType(Result))
647 break;
648 if (Loc) *Loc = getLocStart();
649 return false;
650 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 case DeclRefExprClass:
652 if (const EnumConstantDecl *D =
653 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
654 Result = D->getInitVal();
655 break;
656 }
657 if (Loc) *Loc = getLocStart();
658 return false;
659 case UnaryOperatorClass: {
660 const UnaryOperator *Exp = cast<UnaryOperator>(this);
661
662 // Get the operand value. If this is sizeof/alignof, do not evalute the
663 // operand. This affects C99 6.6p3.
Chris Lattner602dafd2007-08-23 21:42:50 +0000664 if (!Exp->isSizeOfAlignOfOp() &&
665 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 return false;
667
668 switch (Exp->getOpcode()) {
669 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
670 // See C99 6.6p3.
671 default:
672 if (Loc) *Loc = Exp->getOperatorLoc();
673 return false;
674 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000675 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 case UnaryOperator::SizeOf:
677 case UnaryOperator::AlignOf:
678 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000679 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
680 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000682 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000683
Chris Lattner76e773a2007-07-18 18:38:36 +0000684 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000685 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000686 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
687 Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000688
689 // Get information about the size or align.
Chris Lattnerefdd1572008-01-02 21:54:09 +0000690 if (Exp->getSubExpr()->getType()->isFunctionType()) {
691 // GCC extension: sizeof(function) = 1.
692 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
693 } else if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner76e773a2007-07-18 18:38:36 +0000694 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
695 Exp->getOperatorLoc());
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000696 } else {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000697 unsigned CharSize =
698 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
699
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000700 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
701 Exp->getOperatorLoc()) / CharSize;
702 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 break;
704 case UnaryOperator::LNot: {
705 bool Val = Result != 0;
Chris Lattner701e5eb2007-09-04 02:45:27 +0000706 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000707 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
708 Exp->getOperatorLoc())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 Result = Val;
710 break;
711 }
712 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 break;
714 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 Result = -Result;
716 break;
717 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 Result = ~Result;
719 break;
720 }
721 break;
722 }
723 case SizeOfAlignOfTypeExprClass: {
724 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
725 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000726 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
727 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000729 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000730
Chris Lattner76e773a2007-07-18 18:38:36 +0000731 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000732 Result.zextOrTrunc(
733 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000734
735 // Get information about the size or align.
Chris Lattnerefdd1572008-01-02 21:54:09 +0000736 if (Exp->getArgumentType()->isFunctionType()) {
737 // GCC extension: sizeof(function) = 1.
738 Result = Exp->isSizeOf() ? 1 : 4;
739 } else if (Exp->isSizeOf()) {
Ted Kremenek060e4702007-12-17 17:38:43 +0000740 unsigned CharSize =
741 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
742
743 Result = Ctx.getTypeSize(Exp->getArgumentType(),
744 Exp->getOperatorLoc()) / CharSize;
745 }
Chris Lattner76e773a2007-07-18 18:38:36 +0000746 else
747 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremenek060e4702007-12-17 17:38:43 +0000748
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 break;
750 }
751 case BinaryOperatorClass: {
752 const BinaryOperator *Exp = cast<BinaryOperator>(this);
753
754 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000755 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 return false;
757
758 llvm::APSInt RHS(Result);
759
760 // The short-circuiting &&/|| operators don't necessarily evaluate their
761 // RHS. Make sure to pass isEvaluated down correctly.
762 if (Exp->isLogicalOp()) {
763 bool RHSEval;
764 if (Exp->getOpcode() == BinaryOperator::LAnd)
765 RHSEval = Result != 0;
766 else {
767 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
768 RHSEval = Result == 0;
769 }
770
Chris Lattner590b6642007-07-15 23:26:56 +0000771 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 isEvaluated & RHSEval))
773 return false;
774 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000775 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 return false;
777 }
778
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 switch (Exp->getOpcode()) {
780 default:
781 if (Loc) *Loc = getLocStart();
782 return false;
783 case BinaryOperator::Mul:
784 Result *= RHS;
785 break;
786 case BinaryOperator::Div:
787 if (RHS == 0) {
788 if (!isEvaluated) break;
789 if (Loc) *Loc = getLocStart();
790 return false;
791 }
792 Result /= RHS;
793 break;
794 case BinaryOperator::Rem:
795 if (RHS == 0) {
796 if (!isEvaluated) break;
797 if (Loc) *Loc = getLocStart();
798 return false;
799 }
800 Result %= RHS;
801 break;
802 case BinaryOperator::Add: Result += RHS; break;
803 case BinaryOperator::Sub: Result -= RHS; break;
804 case BinaryOperator::Shl:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000805 Result <<=
806 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 break;
808 case BinaryOperator::Shr:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000809 Result >>=
810 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 break;
812 case BinaryOperator::LT: Result = Result < RHS; break;
813 case BinaryOperator::GT: Result = Result > RHS; break;
814 case BinaryOperator::LE: Result = Result <= RHS; break;
815 case BinaryOperator::GE: Result = Result >= RHS; break;
816 case BinaryOperator::EQ: Result = Result == RHS; break;
817 case BinaryOperator::NE: Result = Result != RHS; break;
818 case BinaryOperator::And: Result &= RHS; break;
819 case BinaryOperator::Xor: Result ^= RHS; break;
820 case BinaryOperator::Or: Result |= RHS; break;
821 case BinaryOperator::LAnd:
822 Result = Result != 0 && RHS != 0;
823 break;
824 case BinaryOperator::LOr:
825 Result = Result != 0 || RHS != 0;
826 break;
827
828 case BinaryOperator::Comma:
829 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
830 // *except* when they are contained within a subexpression that is not
831 // evaluated". Note that Assignment can never happen due to constraints
832 // on the LHS subexpr, so we don't need to check it here.
833 if (isEvaluated) {
834 if (Loc) *Loc = getLocStart();
835 return false;
836 }
837
838 // The result of the constant expr is the RHS.
839 Result = RHS;
840 return true;
841 }
842
843 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
844 break;
845 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000846 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000848 const Expr *SubExpr;
849 SourceLocation CastLoc;
850 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
851 SubExpr = C->getSubExpr();
852 CastLoc = C->getLParenLoc();
853 } else {
854 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
855 CastLoc = getLocStart();
856 }
857
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000859 if (!SubExpr->getType()->isArithmeticType() ||
860 !getType()->isIntegerType()) {
861 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 return false;
863 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000864
865 uint32_t DestWidth =
866 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
867
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000869 if (SubExpr->getType()->isIntegerType()) {
870 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000872
873 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000874 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattnerc0a356b2008-01-09 18:59:34 +0000875 if (getType()->isBooleanType()) {
876 // Conversion to bool compares against zero.
877 Result = Result != 0;
878 Result.zextOrTrunc(DestWidth);
879 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner26dc7b32007-07-15 23:54:50 +0000880 Result.sextOrTrunc(DestWidth);
881 else // If the input is unsigned, do a zero extend, noop, or truncate.
882 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 break;
884 }
885
886 // Allow floating constants that are the immediate operands of casts or that
887 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000888 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
890 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +0000891
892 // If this isn't a floating literal, we can't handle it.
893 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
894 if (!FL) {
895 if (Loc) *Loc = Operand->getLocStart();
896 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 }
Chris Lattnerc0a356b2008-01-09 18:59:34 +0000898
899 // If the destination is boolean, compare against zero.
900 if (getType()->isBooleanType()) {
901 Result = !FL->getValue().isZero();
902 Result.zextOrTrunc(DestWidth);
903 break;
904 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000905
906 // Determine whether we are converting to unsigned or signed.
907 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +0000908
909 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
910 // be called multiple times per AST.
Chris Lattner987b15d2007-09-22 19:04:13 +0000911 uint64_t Space[4];
Chris Lattnerccc213f2007-09-26 00:47:26 +0000912 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
913 llvm::APFloat::rmTowardZero);
Chris Lattner987b15d2007-09-22 19:04:13 +0000914 Result = llvm::APInt(DestWidth, 4, Space);
915 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 }
917 case ConditionalOperatorClass: {
918 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
919
Chris Lattner590b6642007-07-15 23:26:56 +0000920 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 return false;
922
923 const Expr *TrueExp = Exp->getLHS();
924 const Expr *FalseExp = Exp->getRHS();
925 if (Result == 0) std::swap(TrueExp, FalseExp);
926
927 // Evaluate the false one first, discard the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000928 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 return false;
930 // Evalute the true one, capture the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000931 if (TrueExp &&
932 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 break;
935 }
936 }
937
938 // Cases that are valid constant exprs fall through to here.
939 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
940 return true;
941}
942
Reid Spencer5f016e22007-07-11 17:01:13 +0000943/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
944/// integer constant expression with the value zero, or if this is one that is
945/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000946bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffaa58f002008-01-14 16:10:57 +0000947 // Strip off a cast to void*, if it exists.
948 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
949 // Check that it is a cast to void*.
Reid Spencer5f016e22007-07-11 17:01:13 +0000950 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
951 QualType Pointee = PT->getPointeeType();
Steve Naroffaa58f002008-01-14 16:10:57 +0000952 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
953 CE->getSubExpr()->getType()->isIntegerType()) // from int.
954 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 }
Steve Naroffaa58f002008-01-14 16:10:57 +0000956 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
957 // Ignore the ImplicitCastExpr type entirely.
958 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
959 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
960 // Accept ((void*)0) as a null pointer constant, as many other
961 // implementations do.
962 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaaffbf72008-01-14 02:53:34 +0000963 }
Steve Naroffaa58f002008-01-14 16:10:57 +0000964
965 // This expression must be an integer type.
966 if (!getType()->isIntegerType())
967 return false;
968
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 // If we have an integer constant expression, we need to *evaluate* it and
970 // test for the value 0.
971 llvm::APSInt Val(32);
Steve Naroffaa58f002008-01-14 16:10:57 +0000972 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000973}
Steve Naroff31a45842007-07-28 23:10:27 +0000974
Chris Lattner6481a572007-08-03 17:31:20 +0000975unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner4d0ac882007-08-03 16:00:20 +0000976 return strlen(Accessor.getName());
977}
978
979
Chris Lattnercb92a112007-08-02 21:47:28 +0000980/// getComponentType - Determine whether the components of this access are
981/// "point" "color" or "texture" elements.
Chris Lattner6481a572007-08-03 17:31:20 +0000982OCUVectorElementExpr::ElementType
983OCUVectorElementExpr::getElementType() const {
Steve Naroff31a45842007-07-28 23:10:27 +0000984 // derive the component type, no need to waste space.
985 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +0000986
Chris Lattner88dca042007-08-02 22:33:49 +0000987 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
988 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +0000989
Chris Lattner88dca042007-08-02 22:33:49 +0000990 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +0000991 "getComponentType(): Illegal accessor");
992 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +0000993}
Steve Narofffec0b492007-07-30 03:29:09 +0000994
Chris Lattner6481a572007-08-03 17:31:20 +0000995/// containsDuplicateElements - Return true if any element access is
Chris Lattnercb92a112007-08-02 21:47:28 +0000996/// repeated.
Chris Lattner6481a572007-08-03 17:31:20 +0000997bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +0000998 const char *compStr = Accessor.getName();
999 unsigned length = strlen(compStr);
1000
1001 for (unsigned i = 0; i < length-1; i++) {
1002 const char *s = compStr+i;
1003 for (const char c = *s++; *s; s++)
1004 if (c == *s)
1005 return true;
1006 }
1007 return false;
1008}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001009
1010/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattner6481a572007-08-03 17:31:20 +00001011unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001012 const char *compStr = Accessor.getName();
Chris Lattner6481a572007-08-03 17:31:20 +00001013 unsigned length = getNumElements();
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001014
1015 unsigned Result = 0;
1016
1017 while (length--) {
1018 Result <<= 2;
1019 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1020 assert(Idx != -1 && "Invalid accessor letter");
1021 Result |= Idx;
1022 }
1023 return Result;
1024}
1025
Steve Naroff68d331a2007-09-27 14:38:14 +00001026// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001027ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001028 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001029 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001030 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001031 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1032 MethodProto(mproto), ClassName(0) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001033 NumArgs = nargs;
1034 SubExprs = new Expr*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001035 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001036 if (NumArgs) {
1037 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001038 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1039 }
Steve Naroff563477d2007-09-18 23:55:05 +00001040 LBracloc = LBrac;
1041 RBracloc = RBrac;
1042}
1043
Steve Naroff68d331a2007-09-27 14:38:14 +00001044// constructor for class messages.
1045// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001046ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001047 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001048 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001049 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001050 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1051 MethodProto(mproto), ClassName(clsName) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001052 NumArgs = nargs;
1053 SubExprs = new Expr*[NumArgs+1];
Steve Naroff563477d2007-09-18 23:55:05 +00001054 SubExprs[RECEIVER] = 0;
Steve Naroff49f109c2007-11-15 13:05:42 +00001055 if (NumArgs) {
1056 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001057 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1058 }
Steve Naroff563477d2007-09-18 23:55:05 +00001059 LBracloc = LBrac;
1060 RBracloc = RBrac;
1061}
1062
Chris Lattner27437ca2007-10-25 00:29:32 +00001063
1064bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1065 llvm::APSInt CondVal(32);
1066 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1067 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1068 return CondVal != 0;
1069}
1070
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001071//===----------------------------------------------------------------------===//
1072// Child Iterators for iterating over subexpressions/substatements
1073//===----------------------------------------------------------------------===//
1074
1075// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001076Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1077Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001078
Steve Naroff7779db42007-11-12 14:29:37 +00001079// ObjCIvarRefExpr
1080Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1081Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1082
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001083// PreDefinedExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001084Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1085Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001086
1087// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001088Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1089Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001090
1091// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001092Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1093Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001094
1095// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001096Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1097Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001098
Chris Lattner5d661452007-08-26 03:42:43 +00001099// ImaginaryLiteral
1100Stmt::child_iterator ImaginaryLiteral::child_begin() {
1101 return reinterpret_cast<Stmt**>(&Val);
1102}
1103Stmt::child_iterator ImaginaryLiteral::child_end() {
1104 return reinterpret_cast<Stmt**>(&Val)+1;
1105}
1106
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001107// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001108Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1109Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001110
1111// ParenExpr
1112Stmt::child_iterator ParenExpr::child_begin() {
1113 return reinterpret_cast<Stmt**>(&Val);
1114}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001115Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001116 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001117}
1118
1119// UnaryOperator
1120Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001121 return reinterpret_cast<Stmt**>(&Val);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001122}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001123Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001124 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001125}
1126
1127// SizeOfAlignOfTypeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001128Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001129 // If the type is a VLA type (and not a typedef), the size expression of the
1130 // VLA needs to be treated as an executable expression.
1131 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1132 return child_iterator(T);
1133 else
1134 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001135}
1136Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001137 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001138}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001139
1140// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001141Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001142 return reinterpret_cast<Stmt**>(&SubExprs);
1143}
Ted Kremenek1237c672007-08-24 20:06:47 +00001144Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001145 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001146}
1147
1148// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001149Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001150 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001151}
Ted Kremenek1237c672007-08-24 20:06:47 +00001152Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001153 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001154}
Ted Kremenek1237c672007-08-24 20:06:47 +00001155
1156// MemberExpr
1157Stmt::child_iterator MemberExpr::child_begin() {
1158 return reinterpret_cast<Stmt**>(&Base);
1159}
Ted Kremenek1237c672007-08-24 20:06:47 +00001160Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001161 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001162}
1163
1164// OCUVectorElementExpr
1165Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1166 return reinterpret_cast<Stmt**>(&Base);
1167}
Ted Kremenek1237c672007-08-24 20:06:47 +00001168Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001169 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001170}
1171
1172// CompoundLiteralExpr
1173Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1174 return reinterpret_cast<Stmt**>(&Init);
1175}
Ted Kremenek1237c672007-08-24 20:06:47 +00001176Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001177 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001178}
1179
1180// ImplicitCastExpr
1181Stmt::child_iterator ImplicitCastExpr::child_begin() {
1182 return reinterpret_cast<Stmt**>(&Op);
1183}
Ted Kremenek1237c672007-08-24 20:06:47 +00001184Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001185 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001186}
1187
1188// CastExpr
1189Stmt::child_iterator CastExpr::child_begin() {
1190 return reinterpret_cast<Stmt**>(&Op);
1191}
Ted Kremenek1237c672007-08-24 20:06:47 +00001192Stmt::child_iterator CastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001193 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001194}
1195
1196// BinaryOperator
1197Stmt::child_iterator BinaryOperator::child_begin() {
1198 return reinterpret_cast<Stmt**>(&SubExprs);
1199}
Ted Kremenek1237c672007-08-24 20:06:47 +00001200Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001201 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001202}
1203
1204// ConditionalOperator
1205Stmt::child_iterator ConditionalOperator::child_begin() {
1206 return reinterpret_cast<Stmt**>(&SubExprs);
1207}
Ted Kremenek1237c672007-08-24 20:06:47 +00001208Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001209 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001210}
1211
1212// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001213Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1214Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001215
Ted Kremenek1237c672007-08-24 20:06:47 +00001216// StmtExpr
1217Stmt::child_iterator StmtExpr::child_begin() {
1218 return reinterpret_cast<Stmt**>(&SubStmt);
1219}
Ted Kremenek1237c672007-08-24 20:06:47 +00001220Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001221 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001222}
1223
1224// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001225Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1226 return child_iterator();
1227}
1228
1229Stmt::child_iterator TypesCompatibleExpr::child_end() {
1230 return child_iterator();
1231}
Ted Kremenek1237c672007-08-24 20:06:47 +00001232
1233// ChooseExpr
1234Stmt::child_iterator ChooseExpr::child_begin() {
1235 return reinterpret_cast<Stmt**>(&SubExprs);
1236}
1237
1238Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001239 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001240}
1241
Nate Begemane2ce1d92008-01-17 17:46:27 +00001242// OverloadExpr
1243Stmt::child_iterator OverloadExpr::child_begin() {
1244 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1245}
1246Stmt::child_iterator OverloadExpr::child_end() {
1247 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs]);
1248}
1249
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001250// VAArgExpr
1251Stmt::child_iterator VAArgExpr::child_begin() {
1252 return reinterpret_cast<Stmt**>(&Val);
1253}
1254
1255Stmt::child_iterator VAArgExpr::child_end() {
1256 return reinterpret_cast<Stmt**>(&Val)+1;
1257}
1258
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001259// InitListExpr
1260Stmt::child_iterator InitListExpr::child_begin() {
1261 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1262}
1263Stmt::child_iterator InitListExpr::child_end() {
1264 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1265}
1266
Ted Kremenek1237c672007-08-24 20:06:47 +00001267// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001268Stmt::child_iterator ObjCStringLiteral::child_begin() {
1269 return child_iterator();
1270}
1271Stmt::child_iterator ObjCStringLiteral::child_end() {
1272 return child_iterator();
1273}
Ted Kremenek1237c672007-08-24 20:06:47 +00001274
1275// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001276Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1277Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001278
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001279// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001280Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1281 return child_iterator();
1282}
1283Stmt::child_iterator ObjCSelectorExpr::child_end() {
1284 return child_iterator();
1285}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001286
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001287// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001288Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1289 return child_iterator();
1290}
1291Stmt::child_iterator ObjCProtocolExpr::child_end() {
1292 return child_iterator();
1293}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001294
Steve Naroff563477d2007-09-18 23:55:05 +00001295// ObjCMessageExpr
1296Stmt::child_iterator ObjCMessageExpr::child_begin() {
1297 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1298}
1299Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff68d331a2007-09-27 14:38:14 +00001300 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroff563477d2007-09-18 23:55:05 +00001301}
1302