blob: 312d209e2dbfc4abe8bf9408ddb5b9057903e75f [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"
17#include "clang/Lex/IdentifierTable.h"
18using 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?
152 assert(1 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
153 }
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
281/// - reference type [C++ [expr]]
282///
283Expr::isLvalueResult Expr::isLvalue() const {
284 // first, check the type (C99 6.3.2.1)
285 if (TR->isFunctionType()) // from isObjectType()
286 return LV_NotObjectType;
287
288 if (TR->isVoidType())
289 return LV_IncompleteVoidType;
290
291 if (TR->isReferenceType()) // C++ [expr]
292 return LV_Valid;
293
294 // the type looks fine, now check the expression
295 switch (getStmtClass()) {
296 case StringLiteralClass: // C99 6.5.1p4
297 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
298 // For vectors, make sure base is an lvalue (i.e. not a function call).
299 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
300 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
301 return LV_Valid;
302 case DeclRefExprClass: // C99 6.5.1p2
303 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
304 return LV_Valid;
305 break;
306 case MemberExprClass: { // C99 6.5.2.3p4
307 const MemberExpr *m = cast<MemberExpr>(this);
308 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
309 }
310 case UnaryOperatorClass: // C99 6.5.3p4
311 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
312 return LV_Valid;
313 break;
314 case ParenExprClass: // C99 6.5.1p5
315 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000316 case OCUVectorElementExprClass:
317 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000318 return LV_DuplicateVectorComponents;
319 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000320 default:
321 break;
322 }
323 return LV_InvalidExpression;
324}
325
326/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
327/// does not have an incomplete type, does not have a const-qualified type, and
328/// if it is a structure or union, does not have any member (including,
329/// recursively, any member or element of all contained aggregates or unions)
330/// with a const-qualified type.
331Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
332 isLvalueResult lvalResult = isLvalue();
333
334 switch (lvalResult) {
335 case LV_Valid: break;
336 case LV_NotObjectType: return MLV_NotObjectType;
337 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000338 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000339 case LV_InvalidExpression: return MLV_InvalidExpression;
340 }
341 if (TR.isConstQualified())
342 return MLV_ConstQualified;
343 if (TR->isArrayType())
344 return MLV_ArrayType;
345 if (TR->isIncompleteType())
346 return MLV_IncompleteType;
347
348 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
349 if (r->hasConstFields())
350 return MLV_ConstQualified;
351 }
352 return MLV_Valid;
353}
354
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000355bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
356
357 switch (getStmtClass()) {
358 default:
359 if (Loc) *Loc = getLocStart();
360 return false;
361 case ParenExprClass:
362 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
363 case StringLiteralClass:
364 case FloatingLiteralClass:
365 case IntegerLiteralClass:
366 case CharacterLiteralClass:
367 case ImaginaryLiteralClass:
368 case TypesCompatibleExprClass:
369 break;
370 case CallExprClass: {
371 const CallExpr *CE = cast<CallExpr>(this);
372 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000373 Result.zextOrTrunc(
374 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000375 if (CE->isBuiltinClassifyType(Result))
376 break;
377 if (Loc) *Loc = getLocStart();
378 return false;
379 }
380 case DeclRefExprClass:
381 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl()))
382 break;
383 if (Loc) *Loc = getLocStart();
384 return false;
385 case UnaryOperatorClass: {
386 const UnaryOperator *Exp = cast<UnaryOperator>(this);
387
388 // Get the operand value. If this is sizeof/alignof, do not evalute the
389 // operand. This affects C99 6.6p3.
390 if (!Exp->isSizeOfAlignOfOp() &&
391 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
392 return false;
393
394 switch (Exp->getOpcode()) {
395 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
396 // See C99 6.6p3.
397 default:
398 if (Loc) *Loc = Exp->getOperatorLoc();
399 return false;
400 case UnaryOperator::Extension:
401 return true; // FIXME: this is wrong.
402 case UnaryOperator::SizeOf:
403 case UnaryOperator::AlignOf:
404 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
405 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
406 return false;
407 break;
408 case UnaryOperator::LNot:
409 case UnaryOperator::Plus:
410 case UnaryOperator::Minus:
411 case UnaryOperator::Not:
412 break;
413 }
414 break;
415 }
416 case SizeOfAlignOfTypeExprClass: {
417 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
418 // alignof always evaluates to a constant.
419 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
420 return false;
421 break;
422 }
423 case BinaryOperatorClass: {
424 const BinaryOperator *Exp = cast<BinaryOperator>(this);
425
426 // The LHS of a constant expr is always evaluated and needed.
427 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
428 return false;
429
430 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
431 return false;
432
433 break;
434 }
435 case ImplicitCastExprClass:
436 case CastExprClass: {
437 const Expr *SubExpr;
438 SourceLocation CastLoc;
439 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
440 SubExpr = C->getSubExpr();
441 CastLoc = C->getLParenLoc();
442 } else {
443 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
444 CastLoc = getLocStart();
445 }
446 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
447 if (Loc) *Loc = SubExpr->getLocStart();
448 return false;
449 }
450 break;
451 }
452 case ConditionalOperatorClass: {
453 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
454
455 if (!Exp->getCond()->isConstantExpr(Ctx, Loc))
456 return false;
457
458 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
459 return false;
460
461 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
462 return false;
463 break;
464 }
465 }
466
467 return true;
468}
469
Chris Lattner4b009652007-07-25 00:24:17 +0000470/// isIntegerConstantExpr - this recursive routine will test if an expression is
471/// an integer constant expression. Note: With the introduction of VLA's in
472/// C99 the result of the sizeof operator is no longer always a constant
473/// expression. The generalization of the wording to include any subexpression
474/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
475/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
476/// "0 || f()" can be treated as a constant expression. In C90 this expression,
477/// occurring in a context requiring a constant, would have been a constraint
478/// violation. FIXME: This routine currently implements C90 semantics.
479/// To properly implement C99 semantics this routine will need to evaluate
480/// expressions involving operators previously mentioned.
481
482/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
483/// comma, etc
484///
485/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000486/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000487///
488/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
489/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
490/// cast+dereference.
491bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
492 SourceLocation *Loc, bool isEvaluated) const {
493 switch (getStmtClass()) {
494 default:
495 if (Loc) *Loc = getLocStart();
496 return false;
497 case ParenExprClass:
498 return cast<ParenExpr>(this)->getSubExpr()->
499 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
500 case IntegerLiteralClass:
501 Result = cast<IntegerLiteral>(this)->getValue();
502 break;
503 case CharacterLiteralClass: {
504 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000505 Result.zextOrTrunc(
506 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000507 Result = CL->getValue();
508 Result.setIsUnsigned(!getType()->isSignedIntegerType());
509 break;
510 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000511 case TypesCompatibleExprClass: {
512 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000513 Result.zextOrTrunc(
514 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000515 Result = TCE->typesAreCompatible();
Steve Naroff1200b5a2007-08-02 00:13:27 +0000516 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000517 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000518 case CallExprClass: {
519 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000520 Result.zextOrTrunc(
521 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000522 if (CE->isBuiltinClassifyType(Result))
523 break;
524 if (Loc) *Loc = getLocStart();
525 return false;
526 }
Chris Lattner4b009652007-07-25 00:24:17 +0000527 case DeclRefExprClass:
528 if (const EnumConstantDecl *D =
529 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
530 Result = D->getInitVal();
531 break;
532 }
533 if (Loc) *Loc = getLocStart();
534 return false;
535 case UnaryOperatorClass: {
536 const UnaryOperator *Exp = cast<UnaryOperator>(this);
537
538 // Get the operand value. If this is sizeof/alignof, do not evalute the
539 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000540 if (!Exp->isSizeOfAlignOfOp() &&
541 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000542 return false;
543
544 switch (Exp->getOpcode()) {
545 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
546 // See C99 6.6p3.
547 default:
548 if (Loc) *Loc = Exp->getOperatorLoc();
549 return false;
550 case UnaryOperator::Extension:
551 return true; // FIXME: this is wrong.
552 case UnaryOperator::SizeOf:
553 case UnaryOperator::AlignOf:
554 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
555 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
556 return false;
557
558 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000559 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000560 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
561 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000562
563 // Get information about the size or align.
564 if (Exp->getOpcode() == UnaryOperator::SizeOf)
565 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
566 Exp->getOperatorLoc());
567 else
568 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
569 Exp->getOperatorLoc());
570 break;
571 case UnaryOperator::LNot: {
572 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000573 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000574 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
575 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000576 Result = Val;
577 break;
578 }
579 case UnaryOperator::Plus:
580 break;
581 case UnaryOperator::Minus:
582 Result = -Result;
583 break;
584 case UnaryOperator::Not:
585 Result = ~Result;
586 break;
587 }
588 break;
589 }
590 case SizeOfAlignOfTypeExprClass: {
591 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
592 // alignof always evaluates to a constant.
593 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
594 return false;
595
596 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000597 Result.zextOrTrunc(
598 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000599
600 // Get information about the size or align.
601 if (Exp->isSizeOf())
602 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
603 else
604 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
605 break;
606 }
607 case BinaryOperatorClass: {
608 const BinaryOperator *Exp = cast<BinaryOperator>(this);
609
610 // The LHS of a constant expr is always evaluated and needed.
611 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
612 return false;
613
614 llvm::APSInt RHS(Result);
615
616 // The short-circuiting &&/|| operators don't necessarily evaluate their
617 // RHS. Make sure to pass isEvaluated down correctly.
618 if (Exp->isLogicalOp()) {
619 bool RHSEval;
620 if (Exp->getOpcode() == BinaryOperator::LAnd)
621 RHSEval = Result != 0;
622 else {
623 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
624 RHSEval = Result == 0;
625 }
626
627 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
628 isEvaluated & RHSEval))
629 return false;
630 } else {
631 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
632 return false;
633 }
634
635 switch (Exp->getOpcode()) {
636 default:
637 if (Loc) *Loc = getLocStart();
638 return false;
639 case BinaryOperator::Mul:
640 Result *= RHS;
641 break;
642 case BinaryOperator::Div:
643 if (RHS == 0) {
644 if (!isEvaluated) break;
645 if (Loc) *Loc = getLocStart();
646 return false;
647 }
648 Result /= RHS;
649 break;
650 case BinaryOperator::Rem:
651 if (RHS == 0) {
652 if (!isEvaluated) break;
653 if (Loc) *Loc = getLocStart();
654 return false;
655 }
656 Result %= RHS;
657 break;
658 case BinaryOperator::Add: Result += RHS; break;
659 case BinaryOperator::Sub: Result -= RHS; break;
660 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000661 Result <<=
662 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000663 break;
664 case BinaryOperator::Shr:
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::LT: Result = Result < RHS; break;
669 case BinaryOperator::GT: Result = Result > RHS; break;
670 case BinaryOperator::LE: Result = Result <= RHS; break;
671 case BinaryOperator::GE: Result = Result >= RHS; break;
672 case BinaryOperator::EQ: Result = Result == RHS; break;
673 case BinaryOperator::NE: Result = Result != RHS; break;
674 case BinaryOperator::And: Result &= RHS; break;
675 case BinaryOperator::Xor: Result ^= RHS; break;
676 case BinaryOperator::Or: Result |= RHS; break;
677 case BinaryOperator::LAnd:
678 Result = Result != 0 && RHS != 0;
679 break;
680 case BinaryOperator::LOr:
681 Result = Result != 0 || RHS != 0;
682 break;
683
684 case BinaryOperator::Comma:
685 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
686 // *except* when they are contained within a subexpression that is not
687 // evaluated". Note that Assignment can never happen due to constraints
688 // on the LHS subexpr, so we don't need to check it here.
689 if (isEvaluated) {
690 if (Loc) *Loc = getLocStart();
691 return false;
692 }
693
694 // The result of the constant expr is the RHS.
695 Result = RHS;
696 return true;
697 }
698
699 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
700 break;
701 }
702 case ImplicitCastExprClass:
703 case CastExprClass: {
704 const Expr *SubExpr;
705 SourceLocation CastLoc;
706 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
707 SubExpr = C->getSubExpr();
708 CastLoc = C->getLParenLoc();
709 } else {
710 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
711 CastLoc = getLocStart();
712 }
713
714 // C99 6.6p6: shall only convert arithmetic types to integer types.
715 if (!SubExpr->getType()->isArithmeticType() ||
716 !getType()->isIntegerType()) {
717 if (Loc) *Loc = SubExpr->getLocStart();
718 return false;
719 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000720
721 uint32_t DestWidth =
722 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
723
Chris Lattner4b009652007-07-25 00:24:17 +0000724 // Handle simple integer->integer casts.
725 if (SubExpr->getType()->isIntegerType()) {
726 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
727 return false;
728
729 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000730 // If the input is signed, do a sign extend, noop, or truncate.
731 if (SubExpr->getType()->isSignedIntegerType())
732 Result.sextOrTrunc(DestWidth);
733 else // If the input is unsigned, do a zero extend, noop, or truncate.
734 Result.zextOrTrunc(DestWidth);
735 break;
736 }
737
738 // Allow floating constants that are the immediate operands of casts or that
739 // are parenthesized.
740 const Expr *Operand = SubExpr;
741 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
742 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000743
744 // If this isn't a floating literal, we can't handle it.
745 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
746 if (!FL) {
747 if (Loc) *Loc = Operand->getLocStart();
748 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000749 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000750
751 // Determine whether we are converting to unsigned or signed.
752 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000753
754 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
755 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000756 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000757 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
758 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000759 Result = llvm::APInt(DestWidth, 4, Space);
760 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000761 }
762 case ConditionalOperatorClass: {
763 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
764
765 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
766 return false;
767
768 const Expr *TrueExp = Exp->getLHS();
769 const Expr *FalseExp = Exp->getRHS();
770 if (Result == 0) std::swap(TrueExp, FalseExp);
771
772 // Evaluate the false one first, discard the result.
773 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
774 return false;
775 // Evalute the true one, capture the result.
776 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
777 return false;
778 break;
779 }
780 }
781
782 // Cases that are valid constant exprs fall through to here.
783 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
784 return true;
785}
786
787
788/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
789/// integer constant expression with the value zero, or if this is one that is
790/// cast to void*.
791bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
792 // Strip off a cast to void*, if it exists.
793 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
794 // Check that it is a cast to void*.
795 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
796 QualType Pointee = PT->getPointeeType();
797 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
798 CE->getSubExpr()->getType()->isIntegerType()) // from int.
799 return CE->getSubExpr()->isNullPointerConstant(Ctx);
800 }
Steve Naroff1d6b2472007-08-28 21:20:34 +0000801 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff3d052872007-08-29 00:00:02 +0000802 // Ignore the ImplicitCastExpr type entirely.
803 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000804 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
805 // Accept ((void*)0) as a null pointer constant, as many other
806 // implementations do.
807 return PE->getSubExpr()->isNullPointerConstant(Ctx);
808 }
809
810 // This expression must be an integer type.
811 if (!getType()->isIntegerType())
812 return false;
813
814 // If we have an integer constant expression, we need to *evaluate* it and
815 // test for the value 0.
816 llvm::APSInt Val(32);
817 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
818}
Steve Naroffc11705f2007-07-28 23:10:27 +0000819
Chris Lattnera0d03a72007-08-03 17:31:20 +0000820unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000821 return strlen(Accessor.getName());
822}
823
824
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000825/// getComponentType - Determine whether the components of this access are
826/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000827OCUVectorElementExpr::ElementType
828OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000829 // derive the component type, no need to waste space.
830 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000831
Chris Lattner9096b792007-08-02 22:33:49 +0000832 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
833 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000834
Chris Lattner9096b792007-08-02 22:33:49 +0000835 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000836 "getComponentType(): Illegal accessor");
837 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000838}
Steve Naroffba67f692007-07-30 03:29:09 +0000839
Chris Lattnera0d03a72007-08-03 17:31:20 +0000840/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000841/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000842bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000843 const char *compStr = Accessor.getName();
844 unsigned length = strlen(compStr);
845
846 for (unsigned i = 0; i < length-1; i++) {
847 const char *s = compStr+i;
848 for (const char c = *s++; *s; s++)
849 if (c == *s)
850 return true;
851 }
852 return false;
853}
Chris Lattner42158e72007-08-02 23:36:59 +0000854
855/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000856unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000857 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000858 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +0000859
860 unsigned Result = 0;
861
862 while (length--) {
863 Result <<= 2;
864 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
865 assert(Idx != -1 && "Invalid accessor letter");
866 Result |= Idx;
867 }
868 return Result;
869}
870
Steve Naroff4ed9d662007-09-27 14:38:14 +0000871// constructor for instance messages.
872ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, SelectorInfo *selInfo,
873 QualType retType, SourceLocation LBrac, SourceLocation RBrac,
874 Expr **ArgExprs)
Steve Naroffc39ca262007-09-18 23:55:05 +0000875 : Expr(ObjCMessageExprClass, retType), Selector(selInfo), ClassName(0) {
Steve Naroff4ed9d662007-09-27 14:38:14 +0000876 unsigned numArgs = selInfo->getNumArgs();
877 SubExprs = new Expr*[numArgs+1];
878 SubExprs[RECEIVER] = receiver;
879 if (numArgs) {
880 for (unsigned i = 0; i != numArgs; ++i)
881 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
882 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000883 LBracloc = LBrac;
884 RBracloc = RBrac;
885}
886
Steve Naroff4ed9d662007-09-27 14:38:14 +0000887// constructor for class messages.
888// FIXME: clsName should be typed to ObjCInterfaceType
889ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, SelectorInfo *selInfo,
890 QualType retType, SourceLocation LBrac, SourceLocation RBrac,
891 Expr **ArgExprs)
Steve Naroffc39ca262007-09-18 23:55:05 +0000892 : Expr(ObjCMessageExprClass, retType), Selector(selInfo), ClassName(clsName) {
Steve Naroff4ed9d662007-09-27 14:38:14 +0000893 unsigned numArgs = selInfo->getNumArgs();
894 SubExprs = new Expr*[numArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +0000895 SubExprs[RECEIVER] = 0;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000896 if (numArgs) {
897 for (unsigned i = 0; i != numArgs; ++i)
898 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
899 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000900 LBracloc = LBrac;
901 RBracloc = RBrac;
902}
903
Steve Naroff4ed9d662007-09-27 14:38:14 +0000904// The following 3 methods are defined here (instead of Epxr.h) to avoid
905// importing "IdentifierTable.h" into the header.
906unsigned ObjCMessageExpr::getNumArgs() const { return Selector->getNumArgs(); }
907
908/// getArg - Return the specified argument.
909Expr *ObjCMessageExpr::getArg(unsigned Arg) {
910 assert(Arg < Selector->getNumArgs() && "Arg access out of range!");
911 return SubExprs[Arg+ARGS_START];
912}
913const Expr *ObjCMessageExpr::getArg(unsigned Arg) const {
914 assert(Arg < Selector->getNumArgs() && "Arg access out of range!");
915 return SubExprs[Arg+ARGS_START];
916}
917
Steve Naroffc39ca262007-09-18 23:55:05 +0000918
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000919//===----------------------------------------------------------------------===//
920// Child Iterators for iterating over subexpressions/substatements
921//===----------------------------------------------------------------------===//
922
923// DeclRefExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000924Stmt::child_iterator DeclRefExpr::child_begin() { return NULL; }
925Stmt::child_iterator DeclRefExpr::child_end() { return NULL; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000926
927// PreDefinedExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000928Stmt::child_iterator PreDefinedExpr::child_begin() { return NULL; }
929Stmt::child_iterator PreDefinedExpr::child_end() { return NULL; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000930
931// IntegerLiteral
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000932Stmt::child_iterator IntegerLiteral::child_begin() { return NULL; }
933Stmt::child_iterator IntegerLiteral::child_end() { return NULL; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000934
935// CharacterLiteral
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000936Stmt::child_iterator CharacterLiteral::child_begin() { return NULL; }
937Stmt::child_iterator CharacterLiteral::child_end() { return NULL; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000938
939// FloatingLiteral
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000940Stmt::child_iterator FloatingLiteral::child_begin() { return NULL; }
941Stmt::child_iterator FloatingLiteral::child_end() { return NULL; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000942
Chris Lattner1de66eb2007-08-26 03:42:43 +0000943// ImaginaryLiteral
944Stmt::child_iterator ImaginaryLiteral::child_begin() {
945 return reinterpret_cast<Stmt**>(&Val);
946}
947Stmt::child_iterator ImaginaryLiteral::child_end() {
948 return reinterpret_cast<Stmt**>(&Val)+1;
949}
950
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000951// StringLiteral
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000952Stmt::child_iterator StringLiteral::child_begin() { return NULL; }
953Stmt::child_iterator StringLiteral::child_end() { return NULL; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000954
955// ParenExpr
956Stmt::child_iterator ParenExpr::child_begin() {
957 return reinterpret_cast<Stmt**>(&Val);
958}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000959Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +0000960 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000961}
962
963// UnaryOperator
964Stmt::child_iterator UnaryOperator::child_begin() {
965 return reinterpret_cast<Stmt**>(&Val);
966}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000967Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +0000968 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000969}
970
971// SizeOfAlignOfTypeExpr
Chris Lattner1de66eb2007-08-26 03:42:43 +0000972Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() { return NULL; }
973Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() { return NULL; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000974
975// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000976Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000977 return reinterpret_cast<Stmt**>(&SubExprs);
978}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000979Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +0000980 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000981}
982
983// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000984Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +0000985 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000986}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000987Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +0000988 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000989}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000990
991// MemberExpr
992Stmt::child_iterator MemberExpr::child_begin() {
993 return reinterpret_cast<Stmt**>(&Base);
994}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000995Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +0000996 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +0000997}
998
999// OCUVectorElementExpr
1000Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1001 return reinterpret_cast<Stmt**>(&Base);
1002}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001003Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001004 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001005}
1006
1007// CompoundLiteralExpr
1008Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1009 return reinterpret_cast<Stmt**>(&Init);
1010}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001011Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001012 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001013}
1014
1015// ImplicitCastExpr
1016Stmt::child_iterator ImplicitCastExpr::child_begin() {
1017 return reinterpret_cast<Stmt**>(&Op);
1018}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001019Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001020 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001021}
1022
1023// CastExpr
1024Stmt::child_iterator CastExpr::child_begin() {
1025 return reinterpret_cast<Stmt**>(&Op);
1026}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001027Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001028 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001029}
1030
1031// BinaryOperator
1032Stmt::child_iterator BinaryOperator::child_begin() {
1033 return reinterpret_cast<Stmt**>(&SubExprs);
1034}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001035Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001036 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001037}
1038
1039// ConditionalOperator
1040Stmt::child_iterator ConditionalOperator::child_begin() {
1041 return reinterpret_cast<Stmt**>(&SubExprs);
1042}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001043Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001044 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001045}
1046
1047// AddrLabelExpr
1048Stmt::child_iterator AddrLabelExpr::child_begin() { return NULL; }
1049Stmt::child_iterator AddrLabelExpr::child_end() { return NULL; }
1050
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001051// StmtExpr
1052Stmt::child_iterator StmtExpr::child_begin() {
1053 return reinterpret_cast<Stmt**>(&SubStmt);
1054}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001055Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001056 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001057}
1058
1059// TypesCompatibleExpr
1060Stmt::child_iterator TypesCompatibleExpr::child_begin() { return NULL; }
1061Stmt::child_iterator TypesCompatibleExpr::child_end() { return NULL; }
1062
1063// ChooseExpr
1064Stmt::child_iterator ChooseExpr::child_begin() {
1065 return reinterpret_cast<Stmt**>(&SubExprs);
1066}
1067
1068Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001069 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001070}
1071
Anders Carlsson762b7c72007-08-31 04:56:16 +00001072// InitListExpr
1073Stmt::child_iterator InitListExpr::child_begin() {
1074 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1075}
1076Stmt::child_iterator InitListExpr::child_end() {
1077 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1078}
1079
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001080// ObjCStringLiteral
1081Stmt::child_iterator ObjCStringLiteral::child_begin() { return NULL; }
1082Stmt::child_iterator ObjCStringLiteral::child_end() { return NULL; }
1083
1084// ObjCEncodeExpr
1085Stmt::child_iterator ObjCEncodeExpr::child_begin() { return NULL; }
1086Stmt::child_iterator ObjCEncodeExpr::child_end() { return NULL; }
1087
Steve Naroffc39ca262007-09-18 23:55:05 +00001088// ObjCMessageExpr
1089Stmt::child_iterator ObjCMessageExpr::child_begin() {
1090 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1091}
1092Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001093 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001094}
1095