blob: 4505feca77e830d0018dce3aac7dd737606ca405 [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
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000362bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000363 switch (getStmtClass()) {
364 default:
365 if (Loc) *Loc = getLocStart();
366 return false;
367 case ParenExprClass:
368 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
369 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000370 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000371 case FloatingLiteralClass:
372 case IntegerLiteralClass:
373 case CharacterLiteralClass:
374 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000375 case TypesCompatibleExprClass:
376 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000377 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000378 case CallExprClass: {
379 const CallExpr *CE = cast<CallExpr>(this);
380 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000381 Result.zextOrTrunc(
382 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000383 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000384 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000385 if (Loc) *Loc = getLocStart();
386 return false;
387 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000388 case DeclRefExprClass: {
389 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
390 // Accept address of function.
391 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000392 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000393 if (Loc) *Loc = getLocStart();
394 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000395 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000396 case UnaryOperatorClass: {
397 const UnaryOperator *Exp = cast<UnaryOperator>(this);
398
399 // Get the operand value. If this is sizeof/alignof, do not evalute the
400 // operand. This affects C99 6.6p3.
401 if (!Exp->isSizeOfAlignOfOp() &&
402 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
403 return false;
404
405 switch (Exp->getOpcode()) {
406 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
407 // See C99 6.6p3.
408 default:
409 if (Loc) *Loc = Exp->getOperatorLoc();
410 return false;
411 case UnaryOperator::Extension:
412 return true; // FIXME: this is wrong.
413 case UnaryOperator::SizeOf:
414 case UnaryOperator::AlignOf:
415 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
416 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
417 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000418 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000419 case UnaryOperator::LNot:
420 case UnaryOperator::Plus:
421 case UnaryOperator::Minus:
422 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000423 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000424 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000425 }
426 case SizeOfAlignOfTypeExprClass: {
427 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
428 // alignof always evaluates to a constant.
429 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
430 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000431 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000432 }
433 case BinaryOperatorClass: {
434 const BinaryOperator *Exp = cast<BinaryOperator>(this);
435
436 // The LHS of a constant expr is always evaluated and needed.
437 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
438 return false;
439
440 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
441 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000442 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000443 }
444 case ImplicitCastExprClass:
445 case CastExprClass: {
446 const Expr *SubExpr;
447 SourceLocation CastLoc;
448 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
449 SubExpr = C->getSubExpr();
450 CastLoc = C->getLParenLoc();
451 } else {
452 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
453 CastLoc = getLocStart();
454 }
455 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
456 if (Loc) *Loc = SubExpr->getLocStart();
457 return false;
458 }
Chris Lattner06db6132007-10-18 00:20:32 +0000459 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000460 }
461 case ConditionalOperatorClass: {
462 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000463 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
464 !Exp->getLHS()->isConstantExpr(Ctx, Loc) ||
465 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000466 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000467 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000468 }
469 }
470
471 return true;
472}
473
Chris Lattner4b009652007-07-25 00:24:17 +0000474/// isIntegerConstantExpr - this recursive routine will test if an expression is
475/// an integer constant expression. Note: With the introduction of VLA's in
476/// C99 the result of the sizeof operator is no longer always a constant
477/// expression. The generalization of the wording to include any subexpression
478/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
479/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
480/// "0 || f()" can be treated as a constant expression. In C90 this expression,
481/// occurring in a context requiring a constant, would have been a constraint
482/// violation. FIXME: This routine currently implements C90 semantics.
483/// To properly implement C99 semantics this routine will need to evaluate
484/// expressions involving operators previously mentioned.
485
486/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
487/// comma, etc
488///
489/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000490/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000491///
492/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
493/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
494/// cast+dereference.
495bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
496 SourceLocation *Loc, bool isEvaluated) const {
497 switch (getStmtClass()) {
498 default:
499 if (Loc) *Loc = getLocStart();
500 return false;
501 case ParenExprClass:
502 return cast<ParenExpr>(this)->getSubExpr()->
503 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
504 case IntegerLiteralClass:
505 Result = cast<IntegerLiteral>(this)->getValue();
506 break;
507 case CharacterLiteralClass: {
508 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000509 Result.zextOrTrunc(
510 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000511 Result = CL->getValue();
512 Result.setIsUnsigned(!getType()->isSignedIntegerType());
513 break;
514 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000515 case TypesCompatibleExprClass: {
516 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000517 Result.zextOrTrunc(
518 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000519 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000520 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000521 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000522 case CallExprClass: {
523 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000524 Result.zextOrTrunc(
525 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000526 if (CE->isBuiltinClassifyType(Result))
527 break;
528 if (Loc) *Loc = getLocStart();
529 return false;
530 }
Chris Lattner4b009652007-07-25 00:24:17 +0000531 case DeclRefExprClass:
532 if (const EnumConstantDecl *D =
533 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
534 Result = D->getInitVal();
535 break;
536 }
537 if (Loc) *Loc = getLocStart();
538 return false;
539 case UnaryOperatorClass: {
540 const UnaryOperator *Exp = cast<UnaryOperator>(this);
541
542 // Get the operand value. If this is sizeof/alignof, do not evalute the
543 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000544 if (!Exp->isSizeOfAlignOfOp() &&
545 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000546 return false;
547
548 switch (Exp->getOpcode()) {
549 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
550 // See C99 6.6p3.
551 default:
552 if (Loc) *Loc = Exp->getOperatorLoc();
553 return false;
554 case UnaryOperator::Extension:
555 return true; // FIXME: this is wrong.
556 case UnaryOperator::SizeOf:
557 case UnaryOperator::AlignOf:
558 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
559 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
560 return false;
561
562 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000563 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000564 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
565 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000566
567 // Get information about the size or align.
568 if (Exp->getOpcode() == UnaryOperator::SizeOf)
569 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
570 Exp->getOperatorLoc());
571 else
572 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
573 Exp->getOperatorLoc());
574 break;
575 case UnaryOperator::LNot: {
576 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000577 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000578 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
579 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000580 Result = Val;
581 break;
582 }
583 case UnaryOperator::Plus:
584 break;
585 case UnaryOperator::Minus:
586 Result = -Result;
587 break;
588 case UnaryOperator::Not:
589 Result = ~Result;
590 break;
591 }
592 break;
593 }
594 case SizeOfAlignOfTypeExprClass: {
595 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
596 // alignof always evaluates to a constant.
597 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
598 return false;
599
600 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000601 Result.zextOrTrunc(
602 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000603
604 // Get information about the size or align.
605 if (Exp->isSizeOf())
606 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
607 else
608 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
609 break;
610 }
611 case BinaryOperatorClass: {
612 const BinaryOperator *Exp = cast<BinaryOperator>(this);
613
614 // The LHS of a constant expr is always evaluated and needed.
615 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
616 return false;
617
618 llvm::APSInt RHS(Result);
619
620 // The short-circuiting &&/|| operators don't necessarily evaluate their
621 // RHS. Make sure to pass isEvaluated down correctly.
622 if (Exp->isLogicalOp()) {
623 bool RHSEval;
624 if (Exp->getOpcode() == BinaryOperator::LAnd)
625 RHSEval = Result != 0;
626 else {
627 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
628 RHSEval = Result == 0;
629 }
630
631 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
632 isEvaluated & RHSEval))
633 return false;
634 } else {
635 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
636 return false;
637 }
638
639 switch (Exp->getOpcode()) {
640 default:
641 if (Loc) *Loc = getLocStart();
642 return false;
643 case BinaryOperator::Mul:
644 Result *= RHS;
645 break;
646 case BinaryOperator::Div:
647 if (RHS == 0) {
648 if (!isEvaluated) break;
649 if (Loc) *Loc = getLocStart();
650 return false;
651 }
652 Result /= RHS;
653 break;
654 case BinaryOperator::Rem:
655 if (RHS == 0) {
656 if (!isEvaluated) break;
657 if (Loc) *Loc = getLocStart();
658 return false;
659 }
660 Result %= RHS;
661 break;
662 case BinaryOperator::Add: Result += RHS; break;
663 case BinaryOperator::Sub: Result -= RHS; break;
664 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000665 Result <<=
666 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000667 break;
668 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000669 Result >>=
670 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000671 break;
672 case BinaryOperator::LT: Result = Result < RHS; break;
673 case BinaryOperator::GT: Result = Result > RHS; break;
674 case BinaryOperator::LE: Result = Result <= RHS; break;
675 case BinaryOperator::GE: Result = Result >= RHS; break;
676 case BinaryOperator::EQ: Result = Result == RHS; break;
677 case BinaryOperator::NE: Result = Result != RHS; break;
678 case BinaryOperator::And: Result &= RHS; break;
679 case BinaryOperator::Xor: Result ^= RHS; break;
680 case BinaryOperator::Or: Result |= RHS; break;
681 case BinaryOperator::LAnd:
682 Result = Result != 0 && RHS != 0;
683 break;
684 case BinaryOperator::LOr:
685 Result = Result != 0 || RHS != 0;
686 break;
687
688 case BinaryOperator::Comma:
689 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
690 // *except* when they are contained within a subexpression that is not
691 // evaluated". Note that Assignment can never happen due to constraints
692 // on the LHS subexpr, so we don't need to check it here.
693 if (isEvaluated) {
694 if (Loc) *Loc = getLocStart();
695 return false;
696 }
697
698 // The result of the constant expr is the RHS.
699 Result = RHS;
700 return true;
701 }
702
703 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
704 break;
705 }
706 case ImplicitCastExprClass:
707 case CastExprClass: {
708 const Expr *SubExpr;
709 SourceLocation CastLoc;
710 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
711 SubExpr = C->getSubExpr();
712 CastLoc = C->getLParenLoc();
713 } else {
714 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
715 CastLoc = getLocStart();
716 }
717
718 // C99 6.6p6: shall only convert arithmetic types to integer types.
719 if (!SubExpr->getType()->isArithmeticType() ||
720 !getType()->isIntegerType()) {
721 if (Loc) *Loc = SubExpr->getLocStart();
722 return false;
723 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000724
725 uint32_t DestWidth =
726 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
727
Chris Lattner4b009652007-07-25 00:24:17 +0000728 // Handle simple integer->integer casts.
729 if (SubExpr->getType()->isIntegerType()) {
730 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
731 return false;
732
733 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000734 // If the input is signed, do a sign extend, noop, or truncate.
735 if (SubExpr->getType()->isSignedIntegerType())
736 Result.sextOrTrunc(DestWidth);
737 else // If the input is unsigned, do a zero extend, noop, or truncate.
738 Result.zextOrTrunc(DestWidth);
739 break;
740 }
741
742 // Allow floating constants that are the immediate operands of casts or that
743 // are parenthesized.
744 const Expr *Operand = SubExpr;
745 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
746 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000747
748 // If this isn't a floating literal, we can't handle it.
749 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
750 if (!FL) {
751 if (Loc) *Loc = Operand->getLocStart();
752 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000753 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000754
755 // Determine whether we are converting to unsigned or signed.
756 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000757
758 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
759 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000760 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000761 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
762 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000763 Result = llvm::APInt(DestWidth, 4, Space);
764 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000765 }
766 case ConditionalOperatorClass: {
767 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
768
769 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
770 return false;
771
772 const Expr *TrueExp = Exp->getLHS();
773 const Expr *FalseExp = Exp->getRHS();
774 if (Result == 0) std::swap(TrueExp, FalseExp);
775
776 // Evaluate the false one first, discard the result.
777 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
778 return false;
779 // Evalute the true one, capture the result.
780 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
781 return false;
782 break;
783 }
784 }
785
786 // Cases that are valid constant exprs fall through to here.
787 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
788 return true;
789}
790
791
792/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
793/// integer constant expression with the value zero, or if this is one that is
794/// cast to void*.
795bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
796 // Strip off a cast to void*, if it exists.
797 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
798 // Check that it is a cast to void*.
799 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
800 QualType Pointee = PT->getPointeeType();
801 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
802 CE->getSubExpr()->getType()->isIntegerType()) // from int.
803 return CE->getSubExpr()->isNullPointerConstant(Ctx);
804 }
Steve Naroff1d6b2472007-08-28 21:20:34 +0000805 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff3d052872007-08-29 00:00:02 +0000806 // Ignore the ImplicitCastExpr type entirely.
807 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000808 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
809 // Accept ((void*)0) as a null pointer constant, as many other
810 // implementations do.
811 return PE->getSubExpr()->isNullPointerConstant(Ctx);
812 }
813
814 // This expression must be an integer type.
815 if (!getType()->isIntegerType())
816 return false;
817
818 // If we have an integer constant expression, we need to *evaluate* it and
819 // test for the value 0.
820 llvm::APSInt Val(32);
821 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
822}
Steve Naroffc11705f2007-07-28 23:10:27 +0000823
Chris Lattnera0d03a72007-08-03 17:31:20 +0000824unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000825 return strlen(Accessor.getName());
826}
827
828
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000829/// getComponentType - Determine whether the components of this access are
830/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000831OCUVectorElementExpr::ElementType
832OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000833 // derive the component type, no need to waste space.
834 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000835
Chris Lattner9096b792007-08-02 22:33:49 +0000836 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
837 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000838
Chris Lattner9096b792007-08-02 22:33:49 +0000839 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000840 "getComponentType(): Illegal accessor");
841 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000842}
Steve Naroffba67f692007-07-30 03:29:09 +0000843
Chris Lattnera0d03a72007-08-03 17:31:20 +0000844/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000845/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000846bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000847 const char *compStr = Accessor.getName();
848 unsigned length = strlen(compStr);
849
850 for (unsigned i = 0; i < length-1; i++) {
851 const char *s = compStr+i;
852 for (const char c = *s++; *s; s++)
853 if (c == *s)
854 return true;
855 }
856 return false;
857}
Chris Lattner42158e72007-08-02 23:36:59 +0000858
859/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000860unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000861 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000862 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +0000863
864 unsigned Result = 0;
865
866 while (length--) {
867 Result <<= 2;
868 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
869 assert(Idx != -1 && "Invalid accessor letter");
870 Result |= Idx;
871 }
872 return Result;
873}
874
Steve Naroff4ed9d662007-09-27 14:38:14 +0000875// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +0000876ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000877 QualType retType, ObjcMethodDecl *mproto,
878 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000879 Expr **ArgExprs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000880 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
881 MethodProto(mproto), ClassName(0) {
Steve Naroff6cb1d362007-09-28 22:22:11 +0000882 unsigned numArgs = selInfo.getNumArgs();
Steve Naroff4ed9d662007-09-27 14:38:14 +0000883 SubExprs = new Expr*[numArgs+1];
884 SubExprs[RECEIVER] = receiver;
885 if (numArgs) {
886 for (unsigned i = 0; i != numArgs; ++i)
887 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
888 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000889 LBracloc = LBrac;
890 RBracloc = RBrac;
891}
892
Steve Naroff4ed9d662007-09-27 14:38:14 +0000893// constructor for class messages.
894// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +0000895ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000896 QualType retType, ObjcMethodDecl *mproto,
897 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000898 Expr **ArgExprs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000899 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
900 MethodProto(mproto), ClassName(clsName) {
Steve Naroff6cb1d362007-09-28 22:22:11 +0000901 unsigned numArgs = selInfo.getNumArgs();
Steve Naroff4ed9d662007-09-27 14:38:14 +0000902 SubExprs = new Expr*[numArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +0000903 SubExprs[RECEIVER] = 0;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000904 if (numArgs) {
905 for (unsigned i = 0; i != numArgs; ++i)
906 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
907 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000908 LBracloc = LBrac;
909 RBracloc = RBrac;
910}
911
Chris Lattnerf624cd22007-10-25 00:29:32 +0000912
913bool ChooseExpr::isConditionTrue(ASTContext &C) const {
914 llvm::APSInt CondVal(32);
915 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
916 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
917 return CondVal != 0;
918}
919
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000920//===----------------------------------------------------------------------===//
921// Child Iterators for iterating over subexpressions/substatements
922//===----------------------------------------------------------------------===//
923
924// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000925Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
926Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000927
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000928// ObjCIvarRefExpr
929Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
930Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
931
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000932// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000933Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
934Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000935
936// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000937Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
938Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000939
940// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000941Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
942Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000943
944// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000945Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
946Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000947
Chris Lattner1de66eb2007-08-26 03:42:43 +0000948// ImaginaryLiteral
949Stmt::child_iterator ImaginaryLiteral::child_begin() {
950 return reinterpret_cast<Stmt**>(&Val);
951}
952Stmt::child_iterator ImaginaryLiteral::child_end() {
953 return reinterpret_cast<Stmt**>(&Val)+1;
954}
955
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000956// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000957Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
958Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000959
960// ParenExpr
961Stmt::child_iterator ParenExpr::child_begin() {
962 return reinterpret_cast<Stmt**>(&Val);
963}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000964Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +0000965 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000966}
967
968// UnaryOperator
969Stmt::child_iterator UnaryOperator::child_begin() {
970 return reinterpret_cast<Stmt**>(&Val);
971}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000972Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +0000973 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000974}
975
976// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000977Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
978 return child_iterator();
979}
980Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
981 return child_iterator();
982}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000983
984// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000985Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000986 return reinterpret_cast<Stmt**>(&SubExprs);
987}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000988Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +0000989 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000990}
991
992// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000993Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +0000994 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000995}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000996Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +0000997 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000998}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000999
1000// MemberExpr
1001Stmt::child_iterator MemberExpr::child_begin() {
1002 return reinterpret_cast<Stmt**>(&Base);
1003}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001004Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001005 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001006}
1007
1008// OCUVectorElementExpr
1009Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1010 return reinterpret_cast<Stmt**>(&Base);
1011}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001012Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001013 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001014}
1015
1016// CompoundLiteralExpr
1017Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1018 return reinterpret_cast<Stmt**>(&Init);
1019}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001020Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001021 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001022}
1023
1024// ImplicitCastExpr
1025Stmt::child_iterator ImplicitCastExpr::child_begin() {
1026 return reinterpret_cast<Stmt**>(&Op);
1027}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001028Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001029 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001030}
1031
1032// CastExpr
1033Stmt::child_iterator CastExpr::child_begin() {
1034 return reinterpret_cast<Stmt**>(&Op);
1035}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001036Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001037 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001038}
1039
1040// BinaryOperator
1041Stmt::child_iterator BinaryOperator::child_begin() {
1042 return reinterpret_cast<Stmt**>(&SubExprs);
1043}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001044Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001045 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001046}
1047
1048// ConditionalOperator
1049Stmt::child_iterator ConditionalOperator::child_begin() {
1050 return reinterpret_cast<Stmt**>(&SubExprs);
1051}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001052Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001053 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001054}
1055
1056// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001057Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1058Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001059
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001060// StmtExpr
1061Stmt::child_iterator StmtExpr::child_begin() {
1062 return reinterpret_cast<Stmt**>(&SubStmt);
1063}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001064Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001065 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001066}
1067
1068// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001069Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1070 return child_iterator();
1071}
1072
1073Stmt::child_iterator TypesCompatibleExpr::child_end() {
1074 return child_iterator();
1075}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001076
1077// ChooseExpr
1078Stmt::child_iterator ChooseExpr::child_begin() {
1079 return reinterpret_cast<Stmt**>(&SubExprs);
1080}
1081
1082Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001083 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001084}
1085
Anders Carlsson36760332007-10-15 20:28:48 +00001086// VAArgExpr
1087Stmt::child_iterator VAArgExpr::child_begin() {
1088 return reinterpret_cast<Stmt**>(&Val);
1089}
1090
1091Stmt::child_iterator VAArgExpr::child_end() {
1092 return reinterpret_cast<Stmt**>(&Val)+1;
1093}
1094
Anders Carlsson762b7c72007-08-31 04:56:16 +00001095// InitListExpr
1096Stmt::child_iterator InitListExpr::child_begin() {
1097 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1098}
1099Stmt::child_iterator InitListExpr::child_end() {
1100 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1101}
1102
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001103// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001104Stmt::child_iterator ObjCStringLiteral::child_begin() {
1105 return child_iterator();
1106}
1107Stmt::child_iterator ObjCStringLiteral::child_end() {
1108 return child_iterator();
1109}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001110
1111// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001112Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1113Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001114
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001115// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001116Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1117 return child_iterator();
1118}
1119Stmt::child_iterator ObjCSelectorExpr::child_end() {
1120 return child_iterator();
1121}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001122
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001123// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001124Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1125 return child_iterator();
1126}
1127Stmt::child_iterator ObjCProtocolExpr::child_end() {
1128 return child_iterator();
1129}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001130
Steve Naroffc39ca262007-09-18 23:55:05 +00001131// ObjCMessageExpr
1132Stmt::child_iterator ObjCMessageExpr::child_begin() {
1133 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1134}
1135Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001136 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001137}
1138