blob: 0e1eecd7d79caeaf0aaf4c35bc162c96b13c026b [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
81CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
82 SourceLocation rparenloc)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000083 : Expr(CallExprClass, t), NumArgs(numargs) {
84 SubExprs = new Expr*[numargs+1];
85 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +000086 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000087 SubExprs[i+ARGS_START] = args[i];
Reid Spencer5f016e22007-07-11 17:01:13 +000088 RParenLoc = rparenloc;
89}
90
Chris Lattnerd18b3292007-12-28 05:25:02 +000091/// setNumArgs - This changes the number of arguments present in this call.
92/// Any orphaned expressions are deleted by this, and any new operands are set
93/// to null.
94void CallExpr::setNumArgs(unsigned NumArgs) {
95 // No change, just return.
96 if (NumArgs == getNumArgs()) return;
97
98 // If shrinking # arguments, just delete the extras and forgot them.
99 if (NumArgs < getNumArgs()) {
100 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
101 delete getArg(i);
102 this->NumArgs = NumArgs;
103 return;
104 }
105
106 // Otherwise, we are growing the # arguments. New an bigger argument array.
107 Expr **NewSubExprs = new Expr*[NumArgs+1];
108 // Copy over args.
109 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
110 NewSubExprs[i] = SubExprs[i];
111 // Null out new args.
112 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
113 NewSubExprs[i] = 0;
114
115 delete[] SubExprs;
116 SubExprs = NewSubExprs;
117 this->NumArgs = NumArgs;
118}
119
120
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000121bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
122 // The following enum mimics gcc's internal "typeclass.h" file.
123 enum gcc_type_class {
124 no_type_class = -1,
125 void_type_class, integer_type_class, char_type_class,
126 enumeral_type_class, boolean_type_class,
127 pointer_type_class, reference_type_class, offset_type_class,
128 real_type_class, complex_type_class,
129 function_type_class, method_type_class,
130 record_type_class, union_type_class,
131 array_type_class, string_type_class,
132 lang_type_class
133 };
134 Result.setIsSigned(true);
135
136 // All simple function calls (e.g. func()) are implicitly cast to pointer to
137 // function. As a result, we try and obtain the DeclRefExpr from the
138 // ImplicitCastExpr.
139 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
140 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
141 return false;
142 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
143 if (!DRE)
144 return false;
145
146 // We have a DeclRefExpr.
147 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
148 // If no argument was supplied, default to "no_type_class". This isn't
149 // ideal, however it's what gcc does.
150 Result = static_cast<uint64_t>(no_type_class);
151 if (NumArgs >= 1) {
152 QualType argType = getArg(0)->getType();
153
154 if (argType->isVoidType())
155 Result = void_type_class;
156 else if (argType->isEnumeralType())
157 Result = enumeral_type_class;
158 else if (argType->isBooleanType())
159 Result = boolean_type_class;
160 else if (argType->isCharType())
161 Result = string_type_class; // gcc doesn't appear to use char_type_class
162 else if (argType->isIntegerType())
163 Result = integer_type_class;
164 else if (argType->isPointerType())
165 Result = pointer_type_class;
166 else if (argType->isReferenceType())
167 Result = reference_type_class;
168 else if (argType->isRealType())
169 Result = real_type_class;
170 else if (argType->isComplexType())
171 Result = complex_type_class;
172 else if (argType->isFunctionType())
173 Result = function_type_class;
174 else if (argType->isStructureType())
175 Result = record_type_class;
176 else if (argType->isUnionType())
177 Result = union_type_class;
178 else if (argType->isArrayType())
179 Result = array_type_class;
180 else if (argType->isUnionType())
181 Result = union_type_class;
182 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner3ef5bc02007-11-08 17:56:40 +0000183 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000184 }
185 return true;
186 }
187 return false;
188}
189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
191/// corresponds to, e.g. "<<=".
192const char *BinaryOperator::getOpcodeStr(Opcode Op) {
193 switch (Op) {
194 default: assert(0 && "Unknown binary operator");
195 case Mul: return "*";
196 case Div: return "/";
197 case Rem: return "%";
198 case Add: return "+";
199 case Sub: return "-";
200 case Shl: return "<<";
201 case Shr: return ">>";
202 case LT: return "<";
203 case GT: return ">";
204 case LE: return "<=";
205 case GE: return ">=";
206 case EQ: return "==";
207 case NE: return "!=";
208 case And: return "&";
209 case Xor: return "^";
210 case Or: return "|";
211 case LAnd: return "&&";
212 case LOr: return "||";
213 case Assign: return "=";
214 case MulAssign: return "*=";
215 case DivAssign: return "/=";
216 case RemAssign: return "%=";
217 case AddAssign: return "+=";
218 case SubAssign: return "-=";
219 case ShlAssign: return "<<=";
220 case ShrAssign: return ">>=";
221 case AndAssign: return "&=";
222 case XorAssign: return "^=";
223 case OrAssign: return "|=";
224 case Comma: return ",";
225 }
226}
227
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000228InitListExpr::InitListExpr(SourceLocation lbraceloc,
229 Expr **initexprs, unsigned numinits,
230 SourceLocation rbraceloc)
231 : Expr(InitListExprClass, QualType())
232 , NumInits(numinits)
233 , LBraceLoc(lbraceloc)
234 , RBraceLoc(rbraceloc)
235{
236 InitExprs = new Expr*[numinits];
237 for (unsigned i = 0; i != numinits; i++)
238 InitExprs[i] = initexprs[i];
239}
Reid Spencer5f016e22007-07-11 17:01:13 +0000240
241//===----------------------------------------------------------------------===//
242// Generic Expression Routines
243//===----------------------------------------------------------------------===//
244
245/// hasLocalSideEffect - Return true if this immediate expression has side
246/// effects, not counting any sub-expressions.
247bool Expr::hasLocalSideEffect() const {
248 switch (getStmtClass()) {
249 default:
250 return false;
251 case ParenExprClass:
252 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
253 case UnaryOperatorClass: {
254 const UnaryOperator *UO = cast<UnaryOperator>(this);
255
256 switch (UO->getOpcode()) {
257 default: return false;
258 case UnaryOperator::PostInc:
259 case UnaryOperator::PostDec:
260 case UnaryOperator::PreInc:
261 case UnaryOperator::PreDec:
262 return true; // ++/--
263
264 case UnaryOperator::Deref:
265 // Dereferencing a volatile pointer is a side-effect.
266 return getType().isVolatileQualified();
267 case UnaryOperator::Real:
268 case UnaryOperator::Imag:
269 // accessing a piece of a volatile complex is a side-effect.
270 return UO->getSubExpr()->getType().isVolatileQualified();
271
272 case UnaryOperator::Extension:
273 return UO->getSubExpr()->hasLocalSideEffect();
274 }
275 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000276 case BinaryOperatorClass: {
277 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
278 // Consider comma to have side effects if the LHS and RHS both do.
279 if (BinOp->getOpcode() == BinaryOperator::Comma)
280 return BinOp->getLHS()->hasLocalSideEffect() &&
281 BinOp->getRHS()->hasLocalSideEffect();
282
283 return BinOp->isAssignmentOp();
284 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000285 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000286 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000287
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000288 case ConditionalOperatorClass: {
289 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
290 return Exp->getCond()->hasLocalSideEffect()
291 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
292 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
293 }
294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 case MemberExprClass:
296 case ArraySubscriptExprClass:
297 // If the base pointer or element is to a volatile pointer/field, accessing
298 // if is a side effect.
299 return getType().isVolatileQualified();
300
301 case CallExprClass:
302 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
303 // should warn.
304 return true;
Chris Lattnera9c01022007-09-26 22:06:30 +0000305 case ObjCMessageExprClass:
306 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000307
308 case CastExprClass:
309 // If this is a cast to void, check the operand. Otherwise, the result of
310 // the cast is unused.
311 if (getType()->isVoidType())
312 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
313 return false;
314 }
315}
316
317/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
318/// incomplete type other than void. Nonarray expressions that can be lvalues:
319/// - name, where name must be a variable
320/// - e[i]
321/// - (e), where e must be an lvalue
322/// - e.name, where e must be an lvalue
323/// - e->name
324/// - *e, the type of e cannot be a function type
325/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000326/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000327/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000328///
Bill Wendlingca51c972007-07-16 07:07:56 +0000329Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000331 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 return LV_NotObjectType;
333
Steve Naroff731ec572007-07-21 13:32:03 +0000334 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000336
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000337 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000338 return LV_Valid;
339
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 // the type looks fine, now check the expression
341 switch (getStmtClass()) {
342 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000343 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
345 // For vectors, make sure base is an lvalue (i.e. not a function call).
346 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
347 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
348 return LV_Valid;
349 case DeclRefExprClass: // C99 6.5.1p2
350 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
351 return LV_Valid;
352 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000353 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 const MemberExpr *m = cast<MemberExpr>(this);
355 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000356 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000357 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000359 return LV_Valid; // C99 6.5.3p4
360
361 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
362 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
363 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 break;
365 case ParenExprClass: // C99 6.5.1p5
366 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffe6386392007-12-05 04:00:10 +0000367 case CompoundLiteralExprClass: // C99 6.5.2.5p5
368 return LV_Valid;
Chris Lattner6481a572007-08-03 17:31:20 +0000369 case OCUVectorElementExprClass:
370 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000371 return LV_DuplicateVectorComponents;
372 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000373 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
374 return LV_Valid;
Chris Lattnerfa28b302008-01-12 08:14:25 +0000375 case PreDefinedExprClass:
376 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 default:
378 break;
379 }
380 return LV_InvalidExpression;
381}
382
383/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
384/// does not have an incomplete type, does not have a const-qualified type, and
385/// if it is a structure or union, does not have any member (including,
386/// recursively, any member or element of all contained aggregates or unions)
387/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000388Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 isLvalueResult lvalResult = isLvalue();
390
391 switch (lvalResult) {
392 case LV_Valid: break;
393 case LV_NotObjectType: return MLV_NotObjectType;
394 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000395 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 case LV_InvalidExpression: return MLV_InvalidExpression;
397 }
398 if (TR.isConstQualified())
399 return MLV_ConstQualified;
400 if (TR->isArrayType())
401 return MLV_ArrayType;
402 if (TR->isIncompleteType())
403 return MLV_IncompleteType;
404
405 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
406 if (r->hasConstFields())
407 return MLV_ConstQualified;
408 }
409 return MLV_Valid;
410}
411
Chris Lattner4cc62712007-11-27 21:35:27 +0000412/// hasStaticStorage - Return true if this expression has static storage
413/// duration. This means that the address of this expression is a link-time
414/// constant.
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000415bool Expr::hasStaticStorage() const {
416 switch (getStmtClass()) {
417 default:
418 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000419 case ParenExprClass:
420 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
421 case ImplicitCastExprClass:
422 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000423 case CompoundLiteralExprClass:
424 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000425 case DeclRefExprClass: {
426 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
427 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
428 return VD->hasStaticStorage();
429 return false;
430 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000431 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000432 const MemberExpr *M = cast<MemberExpr>(this);
433 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000434 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000435 case ArraySubscriptExprClass:
436 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattnerfa28b302008-01-12 08:14:25 +0000437 case PreDefinedExprClass:
438 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000439 }
440}
441
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000442Expr* Expr::IgnoreParens() {
443 Expr* E = this;
444 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
445 E = P->getSubExpr();
446
447 return E;
448}
449
Steve Naroff38374b02007-09-02 20:30:18 +0000450bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff38374b02007-09-02 20:30:18 +0000451 switch (getStmtClass()) {
452 default:
453 if (Loc) *Loc = getLocStart();
454 return false;
455 case ParenExprClass:
456 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
457 case StringLiteralClass:
Steve Naroff5d37e322007-11-09 15:00:03 +0000458 case ObjCStringLiteralClass:
Steve Naroff38374b02007-09-02 20:30:18 +0000459 case FloatingLiteralClass:
460 case IntegerLiteralClass:
461 case CharacterLiteralClass:
462 case ImaginaryLiteralClass:
Anders Carlsson1a86b332007-10-17 00:52:43 +0000463 case TypesCompatibleExprClass:
464 case CXXBoolLiteralExprClass:
Chris Lattner2777e492007-10-18 00:20:32 +0000465 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000466 case CallExprClass: {
467 const CallExpr *CE = cast<CallExpr>(this);
468 llvm::APSInt Result(32);
Hartmut Kaiser86fd3552007-09-16 21:35:35 +0000469 Result.zextOrTrunc(
470 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff38374b02007-09-02 20:30:18 +0000471 if (CE->isBuiltinClassifyType(Result))
Chris Lattner2777e492007-10-18 00:20:32 +0000472 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000473 if (Loc) *Loc = getLocStart();
474 return false;
475 }
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000476 case DeclRefExprClass: {
477 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
478 // Accept address of function.
479 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner2777e492007-10-18 00:20:32 +0000480 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000481 if (Loc) *Loc = getLocStart();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000482 if (isa<VarDecl>(D))
483 return TR->isArrayType();
Steve Naroff38374b02007-09-02 20:30:18 +0000484 return false;
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000485 }
Steve Naroffb8f13a82008-01-09 00:05:37 +0000486 case CompoundLiteralExprClass:
487 if (Loc) *Loc = getLocStart();
488 // Allow "(int []){2,4}", since the array will be converted to a pointer.
489 return TR->isArrayType();
Steve Naroff38374b02007-09-02 20:30:18 +0000490 case UnaryOperatorClass: {
491 const UnaryOperator *Exp = cast<UnaryOperator>(this);
492
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000493 // C99 6.6p9
Chris Lattner239c15e2007-12-11 23:11:17 +0000494 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
495 if (!Exp->getSubExpr()->hasStaticStorage()) {
496 if (Loc) *Loc = getLocStart();
497 return false;
498 }
499 return true;
500 }
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000501
Steve Naroff38374b02007-09-02 20:30:18 +0000502 // Get the operand value. If this is sizeof/alignof, do not evalute the
503 // operand. This affects C99 6.6p3.
Steve Naroffd0091aa2008-01-10 22:15:12 +0000504 if (!Exp->isSizeOfAlignOfOp() &&
505 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff38374b02007-09-02 20:30:18 +0000506 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
507 return false;
508
509 switch (Exp->getOpcode()) {
510 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
511 // See C99 6.6p3.
512 default:
513 if (Loc) *Loc = Exp->getOperatorLoc();
514 return false;
515 case UnaryOperator::Extension:
516 return true; // FIXME: this is wrong.
517 case UnaryOperator::SizeOf:
518 case UnaryOperator::AlignOf:
Steve Naroffd0091aa2008-01-10 22:15:12 +0000519 case UnaryOperator::OffsetOf:
Steve Naroff38374b02007-09-02 20:30:18 +0000520 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000521 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
522 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000523 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000524 }
Chris Lattner2777e492007-10-18 00:20:32 +0000525 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000526 case UnaryOperator::LNot:
527 case UnaryOperator::Plus:
528 case UnaryOperator::Minus:
529 case UnaryOperator::Not:
Chris Lattner2777e492007-10-18 00:20:32 +0000530 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000531 }
Steve Naroff38374b02007-09-02 20:30:18 +0000532 }
533 case SizeOfAlignOfTypeExprClass: {
534 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
535 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000536 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
537 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000538 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000539 }
Chris Lattner2777e492007-10-18 00:20:32 +0000540 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000541 }
542 case BinaryOperatorClass: {
543 const BinaryOperator *Exp = cast<BinaryOperator>(this);
544
545 // The LHS of a constant expr is always evaluated and needed.
546 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
547 return false;
548
549 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
550 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000551 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000552 }
553 case ImplicitCastExprClass:
554 case CastExprClass: {
555 const Expr *SubExpr;
556 SourceLocation CastLoc;
557 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
558 SubExpr = C->getSubExpr();
559 CastLoc = C->getLParenLoc();
560 } else {
561 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
562 CastLoc = getLocStart();
563 }
564 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
565 if (Loc) *Loc = SubExpr->getLocStart();
566 return false;
567 }
Chris Lattner2777e492007-10-18 00:20:32 +0000568 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000569 }
570 case ConditionalOperatorClass: {
571 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner2777e492007-10-18 00:20:32 +0000572 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson39073232007-11-30 19:04:31 +0000573 // Handle the GNU extension for missing LHS.
574 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner2777e492007-10-18 00:20:32 +0000575 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff38374b02007-09-02 20:30:18 +0000576 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000577 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000578 }
Steve Naroffd0091aa2008-01-10 22:15:12 +0000579 case InitListExprClass: {
580 const InitListExpr *Exp = cast<InitListExpr>(this);
581 unsigned numInits = Exp->getNumInits();
582 for (unsigned i = 0; i < numInits; i++) {
583 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
584 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
585 return false;
586 }
587 }
588 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000589 }
Steve Naroffd0091aa2008-01-10 22:15:12 +0000590 }
Steve Naroff38374b02007-09-02 20:30:18 +0000591}
592
Reid Spencer5f016e22007-07-11 17:01:13 +0000593/// isIntegerConstantExpr - this recursive routine will test if an expression is
594/// an integer constant expression. Note: With the introduction of VLA's in
595/// C99 the result of the sizeof operator is no longer always a constant
596/// expression. The generalization of the wording to include any subexpression
597/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
598/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
599/// "0 || f()" can be treated as a constant expression. In C90 this expression,
600/// occurring in a context requiring a constant, would have been a constraint
601/// violation. FIXME: This routine currently implements C90 semantics.
602/// To properly implement C99 semantics this routine will need to evaluate
603/// expressions involving operators previously mentioned.
604
605/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
606/// comma, etc
607///
608/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000609/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000610///
611/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
612/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
613/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000614bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
615 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 switch (getStmtClass()) {
617 default:
618 if (Loc) *Loc = getLocStart();
619 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 case ParenExprClass:
621 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000622 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 case IntegerLiteralClass:
624 Result = cast<IntegerLiteral>(this)->getValue();
625 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000626 case CharacterLiteralClass: {
627 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000628 Result.zextOrTrunc(
629 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000630 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000631 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000633 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000634 case TypesCompatibleExprClass: {
635 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000636 Result.zextOrTrunc(
637 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffec0550f2007-10-15 20:41:53 +0000638 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff389cecc2007-08-02 00:13:27 +0000639 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000640 }
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000641 case CallExprClass: {
642 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000643 Result.zextOrTrunc(
644 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000645 if (CE->isBuiltinClassifyType(Result))
646 break;
647 if (Loc) *Loc = getLocStart();
648 return false;
649 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 case DeclRefExprClass:
651 if (const EnumConstantDecl *D =
652 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
653 Result = D->getInitVal();
654 break;
655 }
656 if (Loc) *Loc = getLocStart();
657 return false;
658 case UnaryOperatorClass: {
659 const UnaryOperator *Exp = cast<UnaryOperator>(this);
660
661 // Get the operand value. If this is sizeof/alignof, do not evalute the
662 // operand. This affects C99 6.6p3.
Chris Lattner602dafd2007-08-23 21:42:50 +0000663 if (!Exp->isSizeOfAlignOfOp() &&
664 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 return false;
666
667 switch (Exp->getOpcode()) {
668 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
669 // See C99 6.6p3.
670 default:
671 if (Loc) *Loc = Exp->getOperatorLoc();
672 return false;
673 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000674 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 case UnaryOperator::SizeOf:
676 case UnaryOperator::AlignOf:
677 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000678 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
679 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000681 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000682
Chris Lattner76e773a2007-07-18 18:38:36 +0000683 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000684 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000685 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
686 Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000687
688 // Get information about the size or align.
Chris Lattnerefdd1572008-01-02 21:54:09 +0000689 if (Exp->getSubExpr()->getType()->isFunctionType()) {
690 // GCC extension: sizeof(function) = 1.
691 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
692 } else if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner76e773a2007-07-18 18:38:36 +0000693 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
694 Exp->getOperatorLoc());
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000695 } else {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000696 unsigned CharSize =
697 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
698
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000699 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
700 Exp->getOperatorLoc()) / CharSize;
701 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 break;
703 case UnaryOperator::LNot: {
704 bool Val = Result != 0;
Chris Lattner701e5eb2007-09-04 02:45:27 +0000705 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000706 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
707 Exp->getOperatorLoc())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 Result = Val;
709 break;
710 }
711 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 break;
713 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 Result = -Result;
715 break;
716 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 Result = ~Result;
718 break;
719 }
720 break;
721 }
722 case SizeOfAlignOfTypeExprClass: {
723 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
724 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000725 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
726 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000728 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000729
Chris Lattner76e773a2007-07-18 18:38:36 +0000730 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000731 Result.zextOrTrunc(
732 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000733
734 // Get information about the size or align.
Chris Lattnerefdd1572008-01-02 21:54:09 +0000735 if (Exp->getArgumentType()->isFunctionType()) {
736 // GCC extension: sizeof(function) = 1.
737 Result = Exp->isSizeOf() ? 1 : 4;
738 } else if (Exp->isSizeOf()) {
Ted Kremenek060e4702007-12-17 17:38:43 +0000739 unsigned CharSize =
740 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
741
742 Result = Ctx.getTypeSize(Exp->getArgumentType(),
743 Exp->getOperatorLoc()) / CharSize;
744 }
Chris Lattner76e773a2007-07-18 18:38:36 +0000745 else
746 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremenek060e4702007-12-17 17:38:43 +0000747
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 break;
749 }
750 case BinaryOperatorClass: {
751 const BinaryOperator *Exp = cast<BinaryOperator>(this);
752
753 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000754 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 return false;
756
757 llvm::APSInt RHS(Result);
758
759 // The short-circuiting &&/|| operators don't necessarily evaluate their
760 // RHS. Make sure to pass isEvaluated down correctly.
761 if (Exp->isLogicalOp()) {
762 bool RHSEval;
763 if (Exp->getOpcode() == BinaryOperator::LAnd)
764 RHSEval = Result != 0;
765 else {
766 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
767 RHSEval = Result == 0;
768 }
769
Chris Lattner590b6642007-07-15 23:26:56 +0000770 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 isEvaluated & RHSEval))
772 return false;
773 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000774 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 return false;
776 }
777
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 switch (Exp->getOpcode()) {
779 default:
780 if (Loc) *Loc = getLocStart();
781 return false;
782 case BinaryOperator::Mul:
783 Result *= RHS;
784 break;
785 case BinaryOperator::Div:
786 if (RHS == 0) {
787 if (!isEvaluated) break;
788 if (Loc) *Loc = getLocStart();
789 return false;
790 }
791 Result /= RHS;
792 break;
793 case BinaryOperator::Rem:
794 if (RHS == 0) {
795 if (!isEvaluated) break;
796 if (Loc) *Loc = getLocStart();
797 return false;
798 }
799 Result %= RHS;
800 break;
801 case BinaryOperator::Add: Result += RHS; break;
802 case BinaryOperator::Sub: Result -= RHS; break;
803 case BinaryOperator::Shl:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000804 Result <<=
805 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 break;
807 case BinaryOperator::Shr:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000808 Result >>=
809 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 break;
811 case BinaryOperator::LT: Result = Result < RHS; break;
812 case BinaryOperator::GT: Result = Result > RHS; break;
813 case BinaryOperator::LE: Result = Result <= RHS; break;
814 case BinaryOperator::GE: Result = Result >= RHS; break;
815 case BinaryOperator::EQ: Result = Result == RHS; break;
816 case BinaryOperator::NE: Result = Result != RHS; break;
817 case BinaryOperator::And: Result &= RHS; break;
818 case BinaryOperator::Xor: Result ^= RHS; break;
819 case BinaryOperator::Or: Result |= RHS; break;
820 case BinaryOperator::LAnd:
821 Result = Result != 0 && RHS != 0;
822 break;
823 case BinaryOperator::LOr:
824 Result = Result != 0 || RHS != 0;
825 break;
826
827 case BinaryOperator::Comma:
828 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
829 // *except* when they are contained within a subexpression that is not
830 // evaluated". Note that Assignment can never happen due to constraints
831 // on the LHS subexpr, so we don't need to check it here.
832 if (isEvaluated) {
833 if (Loc) *Loc = getLocStart();
834 return false;
835 }
836
837 // The result of the constant expr is the RHS.
838 Result = RHS;
839 return true;
840 }
841
842 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
843 break;
844 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000845 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000847 const Expr *SubExpr;
848 SourceLocation CastLoc;
849 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
850 SubExpr = C->getSubExpr();
851 CastLoc = C->getLParenLoc();
852 } else {
853 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
854 CastLoc = getLocStart();
855 }
856
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000858 if (!SubExpr->getType()->isArithmeticType() ||
859 !getType()->isIntegerType()) {
860 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 return false;
862 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000863
864 uint32_t DestWidth =
865 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
866
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000868 if (SubExpr->getType()->isIntegerType()) {
869 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000871
872 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000873 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattnerc0a356b2008-01-09 18:59:34 +0000874 if (getType()->isBooleanType()) {
875 // Conversion to bool compares against zero.
876 Result = Result != 0;
877 Result.zextOrTrunc(DestWidth);
878 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner26dc7b32007-07-15 23:54:50 +0000879 Result.sextOrTrunc(DestWidth);
880 else // If the input is unsigned, do a zero extend, noop, or truncate.
881 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 break;
883 }
884
885 // Allow floating constants that are the immediate operands of casts or that
886 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000887 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
889 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +0000890
891 // If this isn't a floating literal, we can't handle it.
892 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
893 if (!FL) {
894 if (Loc) *Loc = Operand->getLocStart();
895 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 }
Chris Lattnerc0a356b2008-01-09 18:59:34 +0000897
898 // If the destination is boolean, compare against zero.
899 if (getType()->isBooleanType()) {
900 Result = !FL->getValue().isZero();
901 Result.zextOrTrunc(DestWidth);
902 break;
903 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000904
905 // Determine whether we are converting to unsigned or signed.
906 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +0000907
908 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
909 // be called multiple times per AST.
Chris Lattner987b15d2007-09-22 19:04:13 +0000910 uint64_t Space[4];
Chris Lattnerccc213f2007-09-26 00:47:26 +0000911 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
912 llvm::APFloat::rmTowardZero);
Chris Lattner987b15d2007-09-22 19:04:13 +0000913 Result = llvm::APInt(DestWidth, 4, Space);
914 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 }
916 case ConditionalOperatorClass: {
917 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
918
Chris Lattner590b6642007-07-15 23:26:56 +0000919 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 return false;
921
922 const Expr *TrueExp = Exp->getLHS();
923 const Expr *FalseExp = Exp->getRHS();
924 if (Result == 0) std::swap(TrueExp, FalseExp);
925
926 // Evaluate the false one first, discard the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000927 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 return false;
929 // Evalute the true one, capture the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000930 if (TrueExp &&
931 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 break;
934 }
935 }
936
937 // Cases that are valid constant exprs fall through to here.
938 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
939 return true;
940}
941
Reid Spencer5f016e22007-07-11 17:01:13 +0000942/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
943/// integer constant expression with the value zero, or if this is one that is
944/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000945bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffaa58f002008-01-14 16:10:57 +0000946 // Strip off a cast to void*, if it exists.
947 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
948 // Check that it is a cast to void*.
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
950 QualType Pointee = PT->getPointeeType();
Steve Naroffaa58f002008-01-14 16:10:57 +0000951 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
952 CE->getSubExpr()->getType()->isIntegerType()) // from int.
953 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 }
Steve Naroffaa58f002008-01-14 16:10:57 +0000955 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
956 // Ignore the ImplicitCastExpr type entirely.
957 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
958 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
959 // Accept ((void*)0) as a null pointer constant, as many other
960 // implementations do.
961 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaaffbf72008-01-14 02:53:34 +0000962 }
Steve Naroffaa58f002008-01-14 16:10:57 +0000963
964 // This expression must be an integer type.
965 if (!getType()->isIntegerType())
966 return false;
967
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 // If we have an integer constant expression, we need to *evaluate* it and
969 // test for the value 0.
970 llvm::APSInt Val(32);
Steve Naroffaa58f002008-01-14 16:10:57 +0000971 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000972}
Steve Naroff31a45842007-07-28 23:10:27 +0000973
Chris Lattner6481a572007-08-03 17:31:20 +0000974unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner4d0ac882007-08-03 16:00:20 +0000975 return strlen(Accessor.getName());
976}
977
978
Chris Lattnercb92a112007-08-02 21:47:28 +0000979/// getComponentType - Determine whether the components of this access are
980/// "point" "color" or "texture" elements.
Chris Lattner6481a572007-08-03 17:31:20 +0000981OCUVectorElementExpr::ElementType
982OCUVectorElementExpr::getElementType() const {
Steve Naroff31a45842007-07-28 23:10:27 +0000983 // derive the component type, no need to waste space.
984 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +0000985
Chris Lattner88dca042007-08-02 22:33:49 +0000986 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
987 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +0000988
Chris Lattner88dca042007-08-02 22:33:49 +0000989 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +0000990 "getComponentType(): Illegal accessor");
991 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +0000992}
Steve Narofffec0b492007-07-30 03:29:09 +0000993
Chris Lattner6481a572007-08-03 17:31:20 +0000994/// containsDuplicateElements - Return true if any element access is
Chris Lattnercb92a112007-08-02 21:47:28 +0000995/// repeated.
Chris Lattner6481a572007-08-03 17:31:20 +0000996bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +0000997 const char *compStr = Accessor.getName();
998 unsigned length = strlen(compStr);
999
1000 for (unsigned i = 0; i < length-1; i++) {
1001 const char *s = compStr+i;
1002 for (const char c = *s++; *s; s++)
1003 if (c == *s)
1004 return true;
1005 }
1006 return false;
1007}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001008
1009/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattner6481a572007-08-03 17:31:20 +00001010unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001011 const char *compStr = Accessor.getName();
Chris Lattner6481a572007-08-03 17:31:20 +00001012 unsigned length = getNumElements();
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001013
1014 unsigned Result = 0;
1015
1016 while (length--) {
1017 Result <<= 2;
1018 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1019 assert(Idx != -1 && "Invalid accessor letter");
1020 Result |= Idx;
1021 }
1022 return Result;
1023}
1024
Steve Naroff68d331a2007-09-27 14:38:14 +00001025// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001026ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001027 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001028 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001029 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001030 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1031 MethodProto(mproto), ClassName(0) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001032 NumArgs = nargs;
1033 SubExprs = new Expr*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001034 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001035 if (NumArgs) {
1036 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001037 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1038 }
Steve Naroff563477d2007-09-18 23:55:05 +00001039 LBracloc = LBrac;
1040 RBracloc = RBrac;
1041}
1042
Steve Naroff68d331a2007-09-27 14:38:14 +00001043// constructor for class messages.
1044// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001045ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001046 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001047 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001048 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001049 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1050 MethodProto(mproto), ClassName(clsName) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001051 NumArgs = nargs;
1052 SubExprs = new Expr*[NumArgs+1];
Steve Naroff563477d2007-09-18 23:55:05 +00001053 SubExprs[RECEIVER] = 0;
Steve Naroff49f109c2007-11-15 13:05:42 +00001054 if (NumArgs) {
1055 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001056 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1057 }
Steve Naroff563477d2007-09-18 23:55:05 +00001058 LBracloc = LBrac;
1059 RBracloc = RBrac;
1060}
1061
Chris Lattner27437ca2007-10-25 00:29:32 +00001062
1063bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1064 llvm::APSInt CondVal(32);
1065 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1066 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1067 return CondVal != 0;
1068}
1069
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001070//===----------------------------------------------------------------------===//
1071// Child Iterators for iterating over subexpressions/substatements
1072//===----------------------------------------------------------------------===//
1073
1074// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001075Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1076Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001077
Steve Naroff7779db42007-11-12 14:29:37 +00001078// ObjCIvarRefExpr
1079Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1080Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1081
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001082// PreDefinedExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001083Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1084Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001085
1086// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001087Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1088Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001089
1090// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001091Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1092Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001093
1094// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001095Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1096Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001097
Chris Lattner5d661452007-08-26 03:42:43 +00001098// ImaginaryLiteral
1099Stmt::child_iterator ImaginaryLiteral::child_begin() {
1100 return reinterpret_cast<Stmt**>(&Val);
1101}
1102Stmt::child_iterator ImaginaryLiteral::child_end() {
1103 return reinterpret_cast<Stmt**>(&Val)+1;
1104}
1105
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001106// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001107Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1108Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001109
1110// ParenExpr
1111Stmt::child_iterator ParenExpr::child_begin() {
1112 return reinterpret_cast<Stmt**>(&Val);
1113}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001114Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001115 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001116}
1117
1118// UnaryOperator
1119Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001120 return reinterpret_cast<Stmt**>(&Val);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001121}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001122Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001123 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001124}
1125
1126// SizeOfAlignOfTypeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001127Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001128 // If the type is a VLA type (and not a typedef), the size expression of the
1129 // VLA needs to be treated as an executable expression.
1130 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1131 return child_iterator(T);
1132 else
1133 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001134}
1135Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001136 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001137}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001138
1139// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001140Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001141 return reinterpret_cast<Stmt**>(&SubExprs);
1142}
Ted Kremenek1237c672007-08-24 20:06:47 +00001143Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001144 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001145}
1146
1147// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001148Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001149 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001150}
Ted Kremenek1237c672007-08-24 20:06:47 +00001151Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001152 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001153}
Ted Kremenek1237c672007-08-24 20:06:47 +00001154
1155// MemberExpr
1156Stmt::child_iterator MemberExpr::child_begin() {
1157 return reinterpret_cast<Stmt**>(&Base);
1158}
Ted Kremenek1237c672007-08-24 20:06:47 +00001159Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001160 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001161}
1162
1163// OCUVectorElementExpr
1164Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1165 return reinterpret_cast<Stmt**>(&Base);
1166}
Ted Kremenek1237c672007-08-24 20:06:47 +00001167Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001168 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001169}
1170
1171// CompoundLiteralExpr
1172Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1173 return reinterpret_cast<Stmt**>(&Init);
1174}
Ted Kremenek1237c672007-08-24 20:06:47 +00001175Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001176 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001177}
1178
1179// ImplicitCastExpr
1180Stmt::child_iterator ImplicitCastExpr::child_begin() {
1181 return reinterpret_cast<Stmt**>(&Op);
1182}
Ted Kremenek1237c672007-08-24 20:06:47 +00001183Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001184 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001185}
1186
1187// CastExpr
1188Stmt::child_iterator CastExpr::child_begin() {
1189 return reinterpret_cast<Stmt**>(&Op);
1190}
Ted Kremenek1237c672007-08-24 20:06:47 +00001191Stmt::child_iterator CastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001192 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001193}
1194
1195// BinaryOperator
1196Stmt::child_iterator BinaryOperator::child_begin() {
1197 return reinterpret_cast<Stmt**>(&SubExprs);
1198}
Ted Kremenek1237c672007-08-24 20:06:47 +00001199Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001200 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001201}
1202
1203// ConditionalOperator
1204Stmt::child_iterator ConditionalOperator::child_begin() {
1205 return reinterpret_cast<Stmt**>(&SubExprs);
1206}
Ted Kremenek1237c672007-08-24 20:06:47 +00001207Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001208 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001209}
1210
1211// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001212Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1213Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001214
Ted Kremenek1237c672007-08-24 20:06:47 +00001215// StmtExpr
1216Stmt::child_iterator StmtExpr::child_begin() {
1217 return reinterpret_cast<Stmt**>(&SubStmt);
1218}
Ted Kremenek1237c672007-08-24 20:06:47 +00001219Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001220 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001221}
1222
1223// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001224Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1225 return child_iterator();
1226}
1227
1228Stmt::child_iterator TypesCompatibleExpr::child_end() {
1229 return child_iterator();
1230}
Ted Kremenek1237c672007-08-24 20:06:47 +00001231
1232// ChooseExpr
1233Stmt::child_iterator ChooseExpr::child_begin() {
1234 return reinterpret_cast<Stmt**>(&SubExprs);
1235}
1236
1237Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001238 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001239}
1240
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001241// VAArgExpr
1242Stmt::child_iterator VAArgExpr::child_begin() {
1243 return reinterpret_cast<Stmt**>(&Val);
1244}
1245
1246Stmt::child_iterator VAArgExpr::child_end() {
1247 return reinterpret_cast<Stmt**>(&Val)+1;
1248}
1249
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001250// InitListExpr
1251Stmt::child_iterator InitListExpr::child_begin() {
1252 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1253}
1254Stmt::child_iterator InitListExpr::child_end() {
1255 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1256}
1257
Ted Kremenek1237c672007-08-24 20:06:47 +00001258// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001259Stmt::child_iterator ObjCStringLiteral::child_begin() {
1260 return child_iterator();
1261}
1262Stmt::child_iterator ObjCStringLiteral::child_end() {
1263 return child_iterator();
1264}
Ted Kremenek1237c672007-08-24 20:06:47 +00001265
1266// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001267Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1268Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001269
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001270// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001271Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1272 return child_iterator();
1273}
1274Stmt::child_iterator ObjCSelectorExpr::child_end() {
1275 return child_iterator();
1276}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001277
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001278// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001279Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1280 return child_iterator();
1281}
1282Stmt::child_iterator ObjCProtocolExpr::child_end() {
1283 return child_iterator();
1284}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001285
Steve Naroff563477d2007-09-18 23:55:05 +00001286// ObjCMessageExpr
1287Stmt::child_iterator ObjCMessageExpr::child_begin() {
1288 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1289}
1290Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff68d331a2007-09-27 14:38:14 +00001291 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroff563477d2007-09-18 23:55:05 +00001292}
1293