blob: 10f0895695eb34e578dc1fe176690af08b5260bc [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/StmtVisitor.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// Primary Expressions.
22//===----------------------------------------------------------------------===//
23
24StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
25 bool Wide, QualType t, SourceLocation firstLoc,
26 SourceLocation lastLoc) :
27 Expr(StringLiteralClass, t) {
28 // OPTIMIZE: could allocate this appended to the StringLiteral.
29 char *AStrData = new char[byteLength];
30 memcpy(AStrData, strData, byteLength);
31 StrData = AStrData;
32 ByteLength = byteLength;
33 IsWide = Wide;
34 firstTokLoc = firstLoc;
35 lastTokLoc = lastLoc;
36}
37
38StringLiteral::~StringLiteral() {
39 delete[] StrData;
40}
41
42bool UnaryOperator::isPostfix(Opcode Op) {
43 switch (Op) {
44 case PostInc:
45 case PostDec:
46 return true;
47 default:
48 return false;
49 }
50}
51
52/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
53/// corresponds to, e.g. "sizeof" or "[pre]++".
54const char *UnaryOperator::getOpcodeStr(Opcode Op) {
55 switch (Op) {
56 default: assert(0 && "Unknown unary operator");
57 case PostInc: return "++";
58 case PostDec: return "--";
59 case PreInc: return "++";
60 case PreDec: return "--";
61 case AddrOf: return "&";
62 case Deref: return "*";
63 case Plus: return "+";
64 case Minus: return "-";
65 case Not: return "~";
66 case LNot: return "!";
67 case Real: return "__real";
68 case Imag: return "__imag";
69 case SizeOf: return "sizeof";
70 case AlignOf: return "alignof";
71 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000072 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000073 }
74}
75
76//===----------------------------------------------------------------------===//
77// Postfix Operators.
78//===----------------------------------------------------------------------===//
79
80CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
81 SourceLocation rparenloc)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000082 : Expr(CallExprClass, t), NumArgs(numargs) {
83 SubExprs = new Expr*[numargs+1];
84 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +000085 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000086 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +000087 RParenLoc = rparenloc;
88}
89
Steve Naroff8d3b1702007-08-08 22:15:55 +000090bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
91 // The following enum mimics gcc's internal "typeclass.h" file.
92 enum gcc_type_class {
93 no_type_class = -1,
94 void_type_class, integer_type_class, char_type_class,
95 enumeral_type_class, boolean_type_class,
96 pointer_type_class, reference_type_class, offset_type_class,
97 real_type_class, complex_type_class,
98 function_type_class, method_type_class,
99 record_type_class, union_type_class,
100 array_type_class, string_type_class,
101 lang_type_class
102 };
103 Result.setIsSigned(true);
104
105 // All simple function calls (e.g. func()) are implicitly cast to pointer to
106 // function. As a result, we try and obtain the DeclRefExpr from the
107 // ImplicitCastExpr.
108 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
109 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
110 return false;
111 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
112 if (!DRE)
113 return false;
114
115 // We have a DeclRefExpr.
116 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
117 // If no argument was supplied, default to "no_type_class". This isn't
118 // ideal, however it's what gcc does.
119 Result = static_cast<uint64_t>(no_type_class);
120 if (NumArgs >= 1) {
121 QualType argType = getArg(0)->getType();
122
123 if (argType->isVoidType())
124 Result = void_type_class;
125 else if (argType->isEnumeralType())
126 Result = enumeral_type_class;
127 else if (argType->isBooleanType())
128 Result = boolean_type_class;
129 else if (argType->isCharType())
130 Result = string_type_class; // gcc doesn't appear to use char_type_class
131 else if (argType->isIntegerType())
132 Result = integer_type_class;
133 else if (argType->isPointerType())
134 Result = pointer_type_class;
135 else if (argType->isReferenceType())
136 Result = reference_type_class;
137 else if (argType->isRealType())
138 Result = real_type_class;
139 else if (argType->isComplexType())
140 Result = complex_type_class;
141 else if (argType->isFunctionType())
142 Result = function_type_class;
143 else if (argType->isStructureType())
144 Result = record_type_class;
145 else if (argType->isUnionType())
146 Result = union_type_class;
147 else if (argType->isArrayType())
148 Result = array_type_class;
149 else if (argType->isUnionType())
150 Result = union_type_class;
151 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner19b8f1a2007-11-08 17:56:40 +0000152 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff8d3b1702007-08-08 22:15:55 +0000153 }
154 return true;
155 }
156 return false;
157}
158
Chris Lattner4b009652007-07-25 00:24:17 +0000159/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
160/// corresponds to, e.g. "<<=".
161const char *BinaryOperator::getOpcodeStr(Opcode Op) {
162 switch (Op) {
163 default: assert(0 && "Unknown binary operator");
164 case Mul: return "*";
165 case Div: return "/";
166 case Rem: return "%";
167 case Add: return "+";
168 case Sub: return "-";
169 case Shl: return "<<";
170 case Shr: return ">>";
171 case LT: return "<";
172 case GT: return ">";
173 case LE: return "<=";
174 case GE: return ">=";
175 case EQ: return "==";
176 case NE: return "!=";
177 case And: return "&";
178 case Xor: return "^";
179 case Or: return "|";
180 case LAnd: return "&&";
181 case LOr: return "||";
182 case Assign: return "=";
183 case MulAssign: return "*=";
184 case DivAssign: return "/=";
185 case RemAssign: return "%=";
186 case AddAssign: return "+=";
187 case SubAssign: return "-=";
188 case ShlAssign: return "<<=";
189 case ShrAssign: return ">>=";
190 case AndAssign: return "&=";
191 case XorAssign: return "^=";
192 case OrAssign: return "|=";
193 case Comma: return ",";
194 }
195}
196
Anders Carlsson762b7c72007-08-31 04:56:16 +0000197InitListExpr::InitListExpr(SourceLocation lbraceloc,
198 Expr **initexprs, unsigned numinits,
199 SourceLocation rbraceloc)
200 : Expr(InitListExprClass, QualType())
201 , NumInits(numinits)
202 , LBraceLoc(lbraceloc)
203 , RBraceLoc(rbraceloc)
204{
205 InitExprs = new Expr*[numinits];
206 for (unsigned i = 0; i != numinits; i++)
207 InitExprs[i] = initexprs[i];
208}
Chris Lattner4b009652007-07-25 00:24:17 +0000209
210//===----------------------------------------------------------------------===//
211// Generic Expression Routines
212//===----------------------------------------------------------------------===//
213
214/// hasLocalSideEffect - Return true if this immediate expression has side
215/// effects, not counting any sub-expressions.
216bool Expr::hasLocalSideEffect() const {
217 switch (getStmtClass()) {
218 default:
219 return false;
220 case ParenExprClass:
221 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
222 case UnaryOperatorClass: {
223 const UnaryOperator *UO = cast<UnaryOperator>(this);
224
225 switch (UO->getOpcode()) {
226 default: return false;
227 case UnaryOperator::PostInc:
228 case UnaryOperator::PostDec:
229 case UnaryOperator::PreInc:
230 case UnaryOperator::PreDec:
231 return true; // ++/--
232
233 case UnaryOperator::Deref:
234 // Dereferencing a volatile pointer is a side-effect.
235 return getType().isVolatileQualified();
236 case UnaryOperator::Real:
237 case UnaryOperator::Imag:
238 // accessing a piece of a volatile complex is a side-effect.
239 return UO->getSubExpr()->getType().isVolatileQualified();
240
241 case UnaryOperator::Extension:
242 return UO->getSubExpr()->hasLocalSideEffect();
243 }
244 }
245 case BinaryOperatorClass:
246 return cast<BinaryOperator>(this)->isAssignmentOp();
Chris Lattner06078d22007-08-25 02:00:02 +0000247 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000248 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000249
250 case MemberExprClass:
251 case ArraySubscriptExprClass:
252 // If the base pointer or element is to a volatile pointer/field, accessing
253 // if is a side effect.
254 return getType().isVolatileQualified();
255
256 case CallExprClass:
257 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
258 // should warn.
259 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000260 case ObjCMessageExprClass:
261 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000262
263 case CastExprClass:
264 // If this is a cast to void, check the operand. Otherwise, the result of
265 // the cast is unused.
266 if (getType()->isVoidType())
267 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
268 return false;
269 }
270}
271
272/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
273/// incomplete type other than void. Nonarray expressions that can be lvalues:
274/// - name, where name must be a variable
275/// - e[i]
276/// - (e), where e must be an lvalue
277/// - e.name, where e must be an lvalue
278/// - e->name
279/// - *e, the type of e cannot be a function type
280/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000281/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000282/// - reference type [C++ [expr]]
283///
284Expr::isLvalueResult Expr::isLvalue() const {
285 // first, check the type (C99 6.3.2.1)
286 if (TR->isFunctionType()) // from isObjectType()
287 return LV_NotObjectType;
288
289 if (TR->isVoidType())
290 return LV_IncompleteVoidType;
291
292 if (TR->isReferenceType()) // C++ [expr]
293 return LV_Valid;
294
295 // the type looks fine, now check the expression
296 switch (getStmtClass()) {
297 case StringLiteralClass: // C99 6.5.1p4
298 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
299 // For vectors, make sure base is an lvalue (i.e. not a function call).
300 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
301 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
302 return LV_Valid;
303 case DeclRefExprClass: // C99 6.5.1p2
304 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
305 return LV_Valid;
306 break;
307 case MemberExprClass: { // C99 6.5.2.3p4
308 const MemberExpr *m = cast<MemberExpr>(this);
309 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
310 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000311 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000312 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000313 return LV_Valid; // C99 6.5.3p4
314
315 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
316 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
317 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000318 break;
319 case ParenExprClass: // C99 6.5.1p5
320 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000321 case OCUVectorElementExprClass:
322 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000323 return LV_DuplicateVectorComponents;
324 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000325 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
326 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000327 default:
328 break;
329 }
330 return LV_InvalidExpression;
331}
332
333/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
334/// does not have an incomplete type, does not have a const-qualified type, and
335/// if it is a structure or union, does not have any member (including,
336/// recursively, any member or element of all contained aggregates or unions)
337/// with a const-qualified type.
338Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
339 isLvalueResult lvalResult = isLvalue();
340
341 switch (lvalResult) {
342 case LV_Valid: break;
343 case LV_NotObjectType: return MLV_NotObjectType;
344 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000345 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000346 case LV_InvalidExpression: return MLV_InvalidExpression;
347 }
348 if (TR.isConstQualified())
349 return MLV_ConstQualified;
350 if (TR->isArrayType())
351 return MLV_ArrayType;
352 if (TR->isIncompleteType())
353 return MLV_IncompleteType;
354
355 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
356 if (r->hasConstFields())
357 return MLV_ConstQualified;
358 }
359 return MLV_Valid;
360}
361
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000362bool Expr::hasStaticStorage() const {
363 switch (getStmtClass()) {
364 default:
365 return false;
366 case DeclRefExprClass: {
367 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
368 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
369 return VD->hasStaticStorage();
370 return false;
371 }
372 case MemberExprClass:
373 const MemberExpr *M = cast<MemberExpr>(this);
374 return !M->isArrow() && M->getBase()->hasStaticStorage();
375 }
376}
377
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000378bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000379 switch (getStmtClass()) {
380 default:
381 if (Loc) *Loc = getLocStart();
382 return false;
383 case ParenExprClass:
384 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
385 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000386 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000387 case FloatingLiteralClass:
388 case IntegerLiteralClass:
389 case CharacterLiteralClass:
390 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000391 case TypesCompatibleExprClass:
392 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000393 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000394 case CallExprClass: {
395 const CallExpr *CE = cast<CallExpr>(this);
396 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000397 Result.zextOrTrunc(
398 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000399 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000400 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000401 if (Loc) *Loc = getLocStart();
402 return false;
403 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000404 case DeclRefExprClass: {
405 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
406 // Accept address of function.
407 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000408 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000409 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000410 if (isa<VarDecl>(D))
411 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000412 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000413 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000414 case UnaryOperatorClass: {
415 const UnaryOperator *Exp = cast<UnaryOperator>(this);
416
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000417 // C99 6.6p9
418 if (Exp->getOpcode() == UnaryOperator::AddrOf)
419 return Exp->getSubExpr()->hasStaticStorage();
420
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000421 // Get the operand value. If this is sizeof/alignof, do not evalute the
422 // operand. This affects C99 6.6p3.
423 if (!Exp->isSizeOfAlignOfOp() &&
424 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
425 return false;
426
427 switch (Exp->getOpcode()) {
428 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
429 // See C99 6.6p3.
430 default:
431 if (Loc) *Loc = Exp->getOperatorLoc();
432 return false;
433 case UnaryOperator::Extension:
434 return true; // FIXME: this is wrong.
435 case UnaryOperator::SizeOf:
436 case UnaryOperator::AlignOf:
437 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
438 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
439 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000440 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000441 case UnaryOperator::LNot:
442 case UnaryOperator::Plus:
443 case UnaryOperator::Minus:
444 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000445 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000446 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000447 }
448 case SizeOfAlignOfTypeExprClass: {
449 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
450 // alignof always evaluates to a constant.
451 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
452 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000453 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000454 }
455 case BinaryOperatorClass: {
456 const BinaryOperator *Exp = cast<BinaryOperator>(this);
457
458 // The LHS of a constant expr is always evaluated and needed.
459 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
460 return false;
461
462 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
463 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000464 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000465 }
466 case ImplicitCastExprClass:
467 case CastExprClass: {
468 const Expr *SubExpr;
469 SourceLocation CastLoc;
470 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
471 SubExpr = C->getSubExpr();
472 CastLoc = C->getLParenLoc();
473 } else {
474 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
475 CastLoc = getLocStart();
476 }
477 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
478 if (Loc) *Loc = SubExpr->getLocStart();
479 return false;
480 }
Chris Lattner06db6132007-10-18 00:20:32 +0000481 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000482 }
483 case ConditionalOperatorClass: {
484 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000485 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
486 !Exp->getLHS()->isConstantExpr(Ctx, Loc) ||
487 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000488 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000489 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000490 }
491 }
492
493 return true;
494}
495
Chris Lattner4b009652007-07-25 00:24:17 +0000496/// isIntegerConstantExpr - this recursive routine will test if an expression is
497/// an integer constant expression. Note: With the introduction of VLA's in
498/// C99 the result of the sizeof operator is no longer always a constant
499/// expression. The generalization of the wording to include any subexpression
500/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
501/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
502/// "0 || f()" can be treated as a constant expression. In C90 this expression,
503/// occurring in a context requiring a constant, would have been a constraint
504/// violation. FIXME: This routine currently implements C90 semantics.
505/// To properly implement C99 semantics this routine will need to evaluate
506/// expressions involving operators previously mentioned.
507
508/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
509/// comma, etc
510///
511/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000512/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000513///
514/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
515/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
516/// cast+dereference.
517bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
518 SourceLocation *Loc, bool isEvaluated) const {
519 switch (getStmtClass()) {
520 default:
521 if (Loc) *Loc = getLocStart();
522 return false;
523 case ParenExprClass:
524 return cast<ParenExpr>(this)->getSubExpr()->
525 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
526 case IntegerLiteralClass:
527 Result = cast<IntegerLiteral>(this)->getValue();
528 break;
529 case CharacterLiteralClass: {
530 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000531 Result.zextOrTrunc(
532 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000533 Result = CL->getValue();
534 Result.setIsUnsigned(!getType()->isSignedIntegerType());
535 break;
536 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000537 case TypesCompatibleExprClass: {
538 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000539 Result.zextOrTrunc(
540 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000541 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000542 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000543 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000544 case CallExprClass: {
545 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000546 Result.zextOrTrunc(
547 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000548 if (CE->isBuiltinClassifyType(Result))
549 break;
550 if (Loc) *Loc = getLocStart();
551 return false;
552 }
Chris Lattner4b009652007-07-25 00:24:17 +0000553 case DeclRefExprClass:
554 if (const EnumConstantDecl *D =
555 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
556 Result = D->getInitVal();
557 break;
558 }
559 if (Loc) *Loc = getLocStart();
560 return false;
561 case UnaryOperatorClass: {
562 const UnaryOperator *Exp = cast<UnaryOperator>(this);
563
564 // Get the operand value. If this is sizeof/alignof, do not evalute the
565 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000566 if (!Exp->isSizeOfAlignOfOp() &&
567 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000568 return false;
569
570 switch (Exp->getOpcode()) {
571 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
572 // See C99 6.6p3.
573 default:
574 if (Loc) *Loc = Exp->getOperatorLoc();
575 return false;
576 case UnaryOperator::Extension:
577 return true; // FIXME: this is wrong.
578 case UnaryOperator::SizeOf:
579 case UnaryOperator::AlignOf:
580 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
581 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
582 return false;
583
584 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000585 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000586 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
587 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000588
589 // Get information about the size or align.
590 if (Exp->getOpcode() == UnaryOperator::SizeOf)
591 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
592 Exp->getOperatorLoc());
593 else
594 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
595 Exp->getOperatorLoc());
596 break;
597 case UnaryOperator::LNot: {
598 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000599 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000600 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
601 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000602 Result = Val;
603 break;
604 }
605 case UnaryOperator::Plus:
606 break;
607 case UnaryOperator::Minus:
608 Result = -Result;
609 break;
610 case UnaryOperator::Not:
611 Result = ~Result;
612 break;
613 }
614 break;
615 }
616 case SizeOfAlignOfTypeExprClass: {
617 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
618 // alignof always evaluates to a constant.
619 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
620 return false;
621
622 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000623 Result.zextOrTrunc(
624 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000625
626 // Get information about the size or align.
627 if (Exp->isSizeOf())
628 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
629 else
630 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
631 break;
632 }
633 case BinaryOperatorClass: {
634 const BinaryOperator *Exp = cast<BinaryOperator>(this);
635
636 // The LHS of a constant expr is always evaluated and needed.
637 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
638 return false;
639
640 llvm::APSInt RHS(Result);
641
642 // The short-circuiting &&/|| operators don't necessarily evaluate their
643 // RHS. Make sure to pass isEvaluated down correctly.
644 if (Exp->isLogicalOp()) {
645 bool RHSEval;
646 if (Exp->getOpcode() == BinaryOperator::LAnd)
647 RHSEval = Result != 0;
648 else {
649 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
650 RHSEval = Result == 0;
651 }
652
653 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
654 isEvaluated & RHSEval))
655 return false;
656 } else {
657 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
658 return false;
659 }
660
661 switch (Exp->getOpcode()) {
662 default:
663 if (Loc) *Loc = getLocStart();
664 return false;
665 case BinaryOperator::Mul:
666 Result *= RHS;
667 break;
668 case BinaryOperator::Div:
669 if (RHS == 0) {
670 if (!isEvaluated) break;
671 if (Loc) *Loc = getLocStart();
672 return false;
673 }
674 Result /= RHS;
675 break;
676 case BinaryOperator::Rem:
677 if (RHS == 0) {
678 if (!isEvaluated) break;
679 if (Loc) *Loc = getLocStart();
680 return false;
681 }
682 Result %= RHS;
683 break;
684 case BinaryOperator::Add: Result += RHS; break;
685 case BinaryOperator::Sub: Result -= RHS; break;
686 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000687 Result <<=
688 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000689 break;
690 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000691 Result >>=
692 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000693 break;
694 case BinaryOperator::LT: Result = Result < RHS; break;
695 case BinaryOperator::GT: Result = Result > RHS; break;
696 case BinaryOperator::LE: Result = Result <= RHS; break;
697 case BinaryOperator::GE: Result = Result >= RHS; break;
698 case BinaryOperator::EQ: Result = Result == RHS; break;
699 case BinaryOperator::NE: Result = Result != RHS; break;
700 case BinaryOperator::And: Result &= RHS; break;
701 case BinaryOperator::Xor: Result ^= RHS; break;
702 case BinaryOperator::Or: Result |= RHS; break;
703 case BinaryOperator::LAnd:
704 Result = Result != 0 && RHS != 0;
705 break;
706 case BinaryOperator::LOr:
707 Result = Result != 0 || RHS != 0;
708 break;
709
710 case BinaryOperator::Comma:
711 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
712 // *except* when they are contained within a subexpression that is not
713 // evaluated". Note that Assignment can never happen due to constraints
714 // on the LHS subexpr, so we don't need to check it here.
715 if (isEvaluated) {
716 if (Loc) *Loc = getLocStart();
717 return false;
718 }
719
720 // The result of the constant expr is the RHS.
721 Result = RHS;
722 return true;
723 }
724
725 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
726 break;
727 }
728 case ImplicitCastExprClass:
729 case CastExprClass: {
730 const Expr *SubExpr;
731 SourceLocation CastLoc;
732 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
733 SubExpr = C->getSubExpr();
734 CastLoc = C->getLParenLoc();
735 } else {
736 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
737 CastLoc = getLocStart();
738 }
739
740 // C99 6.6p6: shall only convert arithmetic types to integer types.
741 if (!SubExpr->getType()->isArithmeticType() ||
742 !getType()->isIntegerType()) {
743 if (Loc) *Loc = SubExpr->getLocStart();
744 return false;
745 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000746
747 uint32_t DestWidth =
748 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
749
Chris Lattner4b009652007-07-25 00:24:17 +0000750 // Handle simple integer->integer casts.
751 if (SubExpr->getType()->isIntegerType()) {
752 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
753 return false;
754
755 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000756 // If the input is signed, do a sign extend, noop, or truncate.
757 if (SubExpr->getType()->isSignedIntegerType())
758 Result.sextOrTrunc(DestWidth);
759 else // If the input is unsigned, do a zero extend, noop, or truncate.
760 Result.zextOrTrunc(DestWidth);
761 break;
762 }
763
764 // Allow floating constants that are the immediate operands of casts or that
765 // are parenthesized.
766 const Expr *Operand = SubExpr;
767 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
768 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000769
770 // If this isn't a floating literal, we can't handle it.
771 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
772 if (!FL) {
773 if (Loc) *Loc = Operand->getLocStart();
774 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000775 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000776
777 // Determine whether we are converting to unsigned or signed.
778 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000779
780 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
781 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000782 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000783 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
784 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000785 Result = llvm::APInt(DestWidth, 4, Space);
786 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000787 }
788 case ConditionalOperatorClass: {
789 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
790
791 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
792 return false;
793
794 const Expr *TrueExp = Exp->getLHS();
795 const Expr *FalseExp = Exp->getRHS();
796 if (Result == 0) std::swap(TrueExp, FalseExp);
797
798 // Evaluate the false one first, discard the result.
799 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
800 return false;
801 // Evalute the true one, capture the result.
802 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
803 return false;
804 break;
805 }
806 }
807
808 // Cases that are valid constant exprs fall through to here.
809 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
810 return true;
811}
812
813
814/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
815/// integer constant expression with the value zero, or if this is one that is
816/// cast to void*.
817bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
818 // Strip off a cast to void*, if it exists.
819 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
820 // Check that it is a cast to void*.
821 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
822 QualType Pointee = PT->getPointeeType();
823 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
824 CE->getSubExpr()->getType()->isIntegerType()) // from int.
825 return CE->getSubExpr()->isNullPointerConstant(Ctx);
826 }
Steve Naroff1d6b2472007-08-28 21:20:34 +0000827 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff3d052872007-08-29 00:00:02 +0000828 // Ignore the ImplicitCastExpr type entirely.
829 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000830 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
831 // Accept ((void*)0) as a null pointer constant, as many other
832 // implementations do.
833 return PE->getSubExpr()->isNullPointerConstant(Ctx);
834 }
835
836 // This expression must be an integer type.
837 if (!getType()->isIntegerType())
838 return false;
839
840 // If we have an integer constant expression, we need to *evaluate* it and
841 // test for the value 0.
842 llvm::APSInt Val(32);
843 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
844}
Steve Naroffc11705f2007-07-28 23:10:27 +0000845
Chris Lattnera0d03a72007-08-03 17:31:20 +0000846unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000847 return strlen(Accessor.getName());
848}
849
850
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000851/// getComponentType - Determine whether the components of this access are
852/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000853OCUVectorElementExpr::ElementType
854OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000855 // derive the component type, no need to waste space.
856 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000857
Chris Lattner9096b792007-08-02 22:33:49 +0000858 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
859 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000860
Chris Lattner9096b792007-08-02 22:33:49 +0000861 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000862 "getComponentType(): Illegal accessor");
863 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000864}
Steve Naroffba67f692007-07-30 03:29:09 +0000865
Chris Lattnera0d03a72007-08-03 17:31:20 +0000866/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000867/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000868bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000869 const char *compStr = Accessor.getName();
870 unsigned length = strlen(compStr);
871
872 for (unsigned i = 0; i < length-1; i++) {
873 const char *s = compStr+i;
874 for (const char c = *s++; *s; s++)
875 if (c == *s)
876 return true;
877 }
878 return false;
879}
Chris Lattner42158e72007-08-02 23:36:59 +0000880
881/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000882unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000883 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000884 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +0000885
886 unsigned Result = 0;
887
888 while (length--) {
889 Result <<= 2;
890 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
891 assert(Idx != -1 && "Invalid accessor letter");
892 Result |= Idx;
893 }
894 return Result;
895}
896
Steve Naroff4ed9d662007-09-27 14:38:14 +0000897// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +0000898ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000899 QualType retType, ObjcMethodDecl *mproto,
900 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000901 Expr **ArgExprs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000902 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
903 MethodProto(mproto), ClassName(0) {
Steve Naroff6cb1d362007-09-28 22:22:11 +0000904 unsigned numArgs = selInfo.getNumArgs();
Steve Naroff4ed9d662007-09-27 14:38:14 +0000905 SubExprs = new Expr*[numArgs+1];
906 SubExprs[RECEIVER] = receiver;
907 if (numArgs) {
908 for (unsigned i = 0; i != numArgs; ++i)
909 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
910 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000911 LBracloc = LBrac;
912 RBracloc = RBrac;
913}
914
Steve Naroff4ed9d662007-09-27 14:38:14 +0000915// constructor for class messages.
916// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +0000917ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000918 QualType retType, ObjcMethodDecl *mproto,
919 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000920 Expr **ArgExprs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000921 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
922 MethodProto(mproto), ClassName(clsName) {
Steve Naroff6cb1d362007-09-28 22:22:11 +0000923 unsigned numArgs = selInfo.getNumArgs();
Steve Naroff4ed9d662007-09-27 14:38:14 +0000924 SubExprs = new Expr*[numArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +0000925 SubExprs[RECEIVER] = 0;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000926 if (numArgs) {
927 for (unsigned i = 0; i != numArgs; ++i)
928 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
929 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000930 LBracloc = LBrac;
931 RBracloc = RBrac;
932}
933
Chris Lattnerf624cd22007-10-25 00:29:32 +0000934
935bool ChooseExpr::isConditionTrue(ASTContext &C) const {
936 llvm::APSInt CondVal(32);
937 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
938 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
939 return CondVal != 0;
940}
941
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000942//===----------------------------------------------------------------------===//
943// Child Iterators for iterating over subexpressions/substatements
944//===----------------------------------------------------------------------===//
945
946// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000947Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
948Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000949
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000950// ObjCIvarRefExpr
951Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
952Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
953
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000954// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000955Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
956Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000957
958// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000959Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
960Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000961
962// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000963Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
964Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000965
966// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000967Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
968Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000969
Chris Lattner1de66eb2007-08-26 03:42:43 +0000970// ImaginaryLiteral
971Stmt::child_iterator ImaginaryLiteral::child_begin() {
972 return reinterpret_cast<Stmt**>(&Val);
973}
974Stmt::child_iterator ImaginaryLiteral::child_end() {
975 return reinterpret_cast<Stmt**>(&Val)+1;
976}
977
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000978// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000979Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
980Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000981
982// ParenExpr
983Stmt::child_iterator ParenExpr::child_begin() {
984 return reinterpret_cast<Stmt**>(&Val);
985}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000986Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +0000987 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000988}
989
990// UnaryOperator
991Stmt::child_iterator UnaryOperator::child_begin() {
992 return reinterpret_cast<Stmt**>(&Val);
993}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000994Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +0000995 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000996}
997
998// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000999Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
1000 return child_iterator();
1001}
1002Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
1003 return child_iterator();
1004}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001005
1006// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001007Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001008 return reinterpret_cast<Stmt**>(&SubExprs);
1009}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001010Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001011 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001012}
1013
1014// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001015Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001016 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001017}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001018Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001019 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001020}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001021
1022// MemberExpr
1023Stmt::child_iterator MemberExpr::child_begin() {
1024 return reinterpret_cast<Stmt**>(&Base);
1025}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001026Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001027 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001028}
1029
1030// OCUVectorElementExpr
1031Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1032 return reinterpret_cast<Stmt**>(&Base);
1033}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001034Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001035 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001036}
1037
1038// CompoundLiteralExpr
1039Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1040 return reinterpret_cast<Stmt**>(&Init);
1041}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001042Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001043 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001044}
1045
1046// ImplicitCastExpr
1047Stmt::child_iterator ImplicitCastExpr::child_begin() {
1048 return reinterpret_cast<Stmt**>(&Op);
1049}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001050Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001051 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001052}
1053
1054// CastExpr
1055Stmt::child_iterator CastExpr::child_begin() {
1056 return reinterpret_cast<Stmt**>(&Op);
1057}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001058Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001059 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001060}
1061
1062// BinaryOperator
1063Stmt::child_iterator BinaryOperator::child_begin() {
1064 return reinterpret_cast<Stmt**>(&SubExprs);
1065}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001066Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001067 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001068}
1069
1070// ConditionalOperator
1071Stmt::child_iterator ConditionalOperator::child_begin() {
1072 return reinterpret_cast<Stmt**>(&SubExprs);
1073}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001074Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001075 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001076}
1077
1078// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001079Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1080Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001081
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001082// StmtExpr
1083Stmt::child_iterator StmtExpr::child_begin() {
1084 return reinterpret_cast<Stmt**>(&SubStmt);
1085}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001086Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001087 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001088}
1089
1090// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001091Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1092 return child_iterator();
1093}
1094
1095Stmt::child_iterator TypesCompatibleExpr::child_end() {
1096 return child_iterator();
1097}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001098
1099// ChooseExpr
1100Stmt::child_iterator ChooseExpr::child_begin() {
1101 return reinterpret_cast<Stmt**>(&SubExprs);
1102}
1103
1104Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001105 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001106}
1107
Anders Carlsson36760332007-10-15 20:28:48 +00001108// VAArgExpr
1109Stmt::child_iterator VAArgExpr::child_begin() {
1110 return reinterpret_cast<Stmt**>(&Val);
1111}
1112
1113Stmt::child_iterator VAArgExpr::child_end() {
1114 return reinterpret_cast<Stmt**>(&Val)+1;
1115}
1116
Anders Carlsson762b7c72007-08-31 04:56:16 +00001117// InitListExpr
1118Stmt::child_iterator InitListExpr::child_begin() {
1119 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1120}
1121Stmt::child_iterator InitListExpr::child_end() {
1122 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1123}
1124
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001125// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001126Stmt::child_iterator ObjCStringLiteral::child_begin() {
1127 return child_iterator();
1128}
1129Stmt::child_iterator ObjCStringLiteral::child_end() {
1130 return child_iterator();
1131}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001132
1133// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001134Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1135Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001136
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001137// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001138Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1139 return child_iterator();
1140}
1141Stmt::child_iterator ObjCSelectorExpr::child_end() {
1142 return child_iterator();
1143}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001144
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001145// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001146Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1147 return child_iterator();
1148}
1149Stmt::child_iterator ObjCProtocolExpr::child_end() {
1150 return child_iterator();
1151}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001152
Steve Naroffc39ca262007-09-18 23:55:05 +00001153// ObjCMessageExpr
1154Stmt::child_iterator ObjCMessageExpr::child_begin() {
1155 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1156}
1157Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001158 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001159}
1160