blob: c257b3b8e6640d74968aa006cb067802b91da510 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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"
Chris Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/StmtVisitor.h"
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 Lattner73d0d4f2007-08-30 17:45:32 +000072 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +000073 }
74}
75
76//===----------------------------------------------------------------------===//
77// Postfix Operators.
78//===----------------------------------------------------------------------===//
79
80CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
81 SourceLocation rparenloc)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000082 : Expr(CallExprClass, t), NumArgs(numargs) {
83 SubExprs = new Expr*[numargs+1];
84 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +000085 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000086 SubExprs[i+ARGS_START] = args[i];
Reid Spencer5f016e22007-07-11 17:01:13 +000087 RParenLoc = rparenloc;
88}
89
Steve Naroff13b7c5f2007-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Carlsson66b5a8a2007-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}
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnereb14fe82007-08-25 02:00:02 +0000247 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000248 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +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;
260
261 case CastExprClass:
262 // If this is a cast to void, check the operand. Otherwise, the result of
263 // the cast is unused.
264 if (getType()->isVoidType())
265 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
266 return false;
267 }
268}
269
270/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
271/// incomplete type other than void. Nonarray expressions that can be lvalues:
272/// - name, where name must be a variable
273/// - e[i]
274/// - (e), where e must be an lvalue
275/// - e.name, where e must be an lvalue
276/// - e->name
277/// - *e, the type of e cannot be a function type
278/// - string-constant
Bill Wendling08ad47c2007-07-17 03:52:31 +0000279/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000280///
Bill Wendlingca51c972007-07-16 07:07:56 +0000281Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000283 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 return LV_NotObjectType;
285
Steve Naroff731ec572007-07-21 13:32:03 +0000286 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000288
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000289 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000290 return LV_Valid;
291
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 // the type looks fine, now check the expression
293 switch (getStmtClass()) {
294 case StringLiteralClass: // C99 6.5.1p4
295 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
296 // For vectors, make sure base is an lvalue (i.e. not a function call).
297 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
298 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
299 return LV_Valid;
300 case DeclRefExprClass: // C99 6.5.1p2
301 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
302 return LV_Valid;
303 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000304 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 const MemberExpr *m = cast<MemberExpr>(this);
306 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000307 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 case UnaryOperatorClass: // C99 6.5.3p4
309 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
310 return LV_Valid;
311 break;
312 case ParenExprClass: // C99 6.5.1p5
313 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattner6481a572007-08-03 17:31:20 +0000314 case OCUVectorElementExprClass:
315 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000316 return LV_DuplicateVectorComponents;
317 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 default:
319 break;
320 }
321 return LV_InvalidExpression;
322}
323
324/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
325/// does not have an incomplete type, does not have a const-qualified type, and
326/// if it is a structure or union, does not have any member (including,
327/// recursively, any member or element of all contained aggregates or unions)
328/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000329Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 isLvalueResult lvalResult = isLvalue();
331
332 switch (lvalResult) {
333 case LV_Valid: break;
334 case LV_NotObjectType: return MLV_NotObjectType;
335 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000336 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 case LV_InvalidExpression: return MLV_InvalidExpression;
338 }
339 if (TR.isConstQualified())
340 return MLV_ConstQualified;
341 if (TR->isArrayType())
342 return MLV_ArrayType;
343 if (TR->isIncompleteType())
344 return MLV_IncompleteType;
345
346 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
347 if (r->hasConstFields())
348 return MLV_ConstQualified;
349 }
350 return MLV_Valid;
351}
352
Steve Naroff38374b02007-09-02 20:30:18 +0000353bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
354
355 switch (getStmtClass()) {
356 default:
357 if (Loc) *Loc = getLocStart();
358 return false;
359 case ParenExprClass:
360 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
361 case StringLiteralClass:
362 case FloatingLiteralClass:
363 case IntegerLiteralClass:
364 case CharacterLiteralClass:
365 case ImaginaryLiteralClass:
366 case TypesCompatibleExprClass:
367 break;
368 case CallExprClass: {
369 const CallExpr *CE = cast<CallExpr>(this);
370 llvm::APSInt Result(32);
371 Result.zextOrTrunc(Ctx.getTypeSize(getType(), CE->getLocStart()));
372 if (CE->isBuiltinClassifyType(Result))
373 break;
374 if (Loc) *Loc = getLocStart();
375 return false;
376 }
377 case DeclRefExprClass:
378 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl()))
379 break;
380 if (Loc) *Loc = getLocStart();
381 return false;
382 case UnaryOperatorClass: {
383 const UnaryOperator *Exp = cast<UnaryOperator>(this);
384
385 // Get the operand value. If this is sizeof/alignof, do not evalute the
386 // operand. This affects C99 6.6p3.
387 if (!Exp->isSizeOfAlignOfOp() &&
388 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
389 return false;
390
391 switch (Exp->getOpcode()) {
392 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
393 // See C99 6.6p3.
394 default:
395 if (Loc) *Loc = Exp->getOperatorLoc();
396 return false;
397 case UnaryOperator::Extension:
398 return true; // FIXME: this is wrong.
399 case UnaryOperator::SizeOf:
400 case UnaryOperator::AlignOf:
401 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
402 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
403 return false;
404 break;
405 case UnaryOperator::LNot:
406 case UnaryOperator::Plus:
407 case UnaryOperator::Minus:
408 case UnaryOperator::Not:
409 break;
410 }
411 break;
412 }
413 case SizeOfAlignOfTypeExprClass: {
414 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
415 // alignof always evaluates to a constant.
416 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
417 return false;
418 break;
419 }
420 case BinaryOperatorClass: {
421 const BinaryOperator *Exp = cast<BinaryOperator>(this);
422
423 // The LHS of a constant expr is always evaluated and needed.
424 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
425 return false;
426
427 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
428 return false;
429
430 break;
431 }
432 case ImplicitCastExprClass:
433 case CastExprClass: {
434 const Expr *SubExpr;
435 SourceLocation CastLoc;
436 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
437 SubExpr = C->getSubExpr();
438 CastLoc = C->getLParenLoc();
439 } else {
440 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
441 CastLoc = getLocStart();
442 }
443 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
444 if (Loc) *Loc = SubExpr->getLocStart();
445 return false;
446 }
447 break;
448 }
449 case ConditionalOperatorClass: {
450 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
451
452 if (!Exp->getCond()->isConstantExpr(Ctx, Loc))
453 return false;
454
455 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
456 return false;
457
458 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
459 return false;
460 break;
461 }
462 }
463
464 return true;
465}
466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467/// isIntegerConstantExpr - this recursive routine will test if an expression is
468/// an integer constant expression. Note: With the introduction of VLA's in
469/// C99 the result of the sizeof operator is no longer always a constant
470/// expression. The generalization of the wording to include any subexpression
471/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
472/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
473/// "0 || f()" can be treated as a constant expression. In C90 this expression,
474/// occurring in a context requiring a constant, would have been a constraint
475/// violation. FIXME: This routine currently implements C90 semantics.
476/// To properly implement C99 semantics this routine will need to evaluate
477/// expressions involving operators previously mentioned.
478
479/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
480/// comma, etc
481///
482/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
483/// permit this.
Chris Lattnerce0afc02007-07-18 05:21:20 +0000484///
485/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
486/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
487/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000488bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
489 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 switch (getStmtClass()) {
491 default:
492 if (Loc) *Loc = getLocStart();
493 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 case ParenExprClass:
495 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000496 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 case IntegerLiteralClass:
498 Result = cast<IntegerLiteral>(this)->getValue();
499 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000500 case CharacterLiteralClass: {
501 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
502 Result.zextOrTrunc(Ctx.getTypeSize(getType(), CL->getLoc()));
503 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000504 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000506 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000507 case TypesCompatibleExprClass: {
508 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
509 Result.zextOrTrunc(Ctx.getTypeSize(getType(), TCE->getLocStart()));
510 Result = TCE->typesAreCompatible();
Steve Naroff389cecc2007-08-02 00:13:27 +0000511 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000512 }
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000513 case CallExprClass: {
514 const CallExpr *CE = cast<CallExpr>(this);
515 Result.zextOrTrunc(Ctx.getTypeSize(getType(), CE->getLocStart()));
516 if (CE->isBuiltinClassifyType(Result))
517 break;
518 if (Loc) *Loc = getLocStart();
519 return false;
520 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 case DeclRefExprClass:
522 if (const EnumConstantDecl *D =
523 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
524 Result = D->getInitVal();
525 break;
526 }
527 if (Loc) *Loc = getLocStart();
528 return false;
529 case UnaryOperatorClass: {
530 const UnaryOperator *Exp = cast<UnaryOperator>(this);
531
532 // Get the operand value. If this is sizeof/alignof, do not evalute the
533 // operand. This affects C99 6.6p3.
Chris Lattner602dafd2007-08-23 21:42:50 +0000534 if (!Exp->isSizeOfAlignOfOp() &&
535 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 return false;
537
538 switch (Exp->getOpcode()) {
539 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
540 // See C99 6.6p3.
541 default:
542 if (Loc) *Loc = Exp->getOperatorLoc();
543 return false;
544 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000545 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 case UnaryOperator::SizeOf:
547 case UnaryOperator::AlignOf:
548 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner590b6642007-07-15 23:26:56 +0000549 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 return false;
551
Chris Lattner76e773a2007-07-18 18:38:36 +0000552 // Return the result in the right width.
553 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
554
555 // Get information about the size or align.
556 if (Exp->getOpcode() == UnaryOperator::SizeOf)
557 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
558 Exp->getOperatorLoc());
559 else
560 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
561 Exp->getOperatorLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 break;
563 case UnaryOperator::LNot: {
564 bool Val = Result != 0;
Chris Lattner76e773a2007-07-18 18:38:36 +0000565 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 Result = Val;
567 break;
568 }
569 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 break;
571 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 Result = -Result;
573 break;
574 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 Result = ~Result;
576 break;
577 }
578 break;
579 }
580 case SizeOfAlignOfTypeExprClass: {
581 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
582 // alignof always evaluates to a constant.
Chris Lattner590b6642007-07-15 23:26:56 +0000583 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 return false;
585
Chris Lattner76e773a2007-07-18 18:38:36 +0000586 // Return the result in the right width.
587 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
588
589 // Get information about the size or align.
590 if (Exp->isSizeOf())
591 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
592 else
593 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 break;
595 }
596 case BinaryOperatorClass: {
597 const BinaryOperator *Exp = cast<BinaryOperator>(this);
598
599 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000600 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 return false;
602
603 llvm::APSInt RHS(Result);
604
605 // The short-circuiting &&/|| operators don't necessarily evaluate their
606 // RHS. Make sure to pass isEvaluated down correctly.
607 if (Exp->isLogicalOp()) {
608 bool RHSEval;
609 if (Exp->getOpcode() == BinaryOperator::LAnd)
610 RHSEval = Result != 0;
611 else {
612 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
613 RHSEval = Result == 0;
614 }
615
Chris Lattner590b6642007-07-15 23:26:56 +0000616 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 isEvaluated & RHSEval))
618 return false;
619 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000620 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 return false;
622 }
623
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 switch (Exp->getOpcode()) {
625 default:
626 if (Loc) *Loc = getLocStart();
627 return false;
628 case BinaryOperator::Mul:
629 Result *= RHS;
630 break;
631 case BinaryOperator::Div:
632 if (RHS == 0) {
633 if (!isEvaluated) break;
634 if (Loc) *Loc = getLocStart();
635 return false;
636 }
637 Result /= RHS;
638 break;
639 case BinaryOperator::Rem:
640 if (RHS == 0) {
641 if (!isEvaluated) break;
642 if (Loc) *Loc = getLocStart();
643 return false;
644 }
645 Result %= RHS;
646 break;
647 case BinaryOperator::Add: Result += RHS; break;
648 case BinaryOperator::Sub: Result -= RHS; break;
649 case BinaryOperator::Shl:
650 Result <<= RHS.getLimitedValue(Result.getBitWidth()-1);
651 break;
652 case BinaryOperator::Shr:
653 Result >>= RHS.getLimitedValue(Result.getBitWidth()-1);
654 break;
655 case BinaryOperator::LT: Result = Result < RHS; break;
656 case BinaryOperator::GT: Result = Result > RHS; break;
657 case BinaryOperator::LE: Result = Result <= RHS; break;
658 case BinaryOperator::GE: Result = Result >= RHS; break;
659 case BinaryOperator::EQ: Result = Result == RHS; break;
660 case BinaryOperator::NE: Result = Result != RHS; break;
661 case BinaryOperator::And: Result &= RHS; break;
662 case BinaryOperator::Xor: Result ^= RHS; break;
663 case BinaryOperator::Or: Result |= RHS; break;
664 case BinaryOperator::LAnd:
665 Result = Result != 0 && RHS != 0;
666 break;
667 case BinaryOperator::LOr:
668 Result = Result != 0 || RHS != 0;
669 break;
670
671 case BinaryOperator::Comma:
672 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
673 // *except* when they are contained within a subexpression that is not
674 // evaluated". Note that Assignment can never happen due to constraints
675 // on the LHS subexpr, so we don't need to check it here.
676 if (isEvaluated) {
677 if (Loc) *Loc = getLocStart();
678 return false;
679 }
680
681 // The result of the constant expr is the RHS.
682 Result = RHS;
683 return true;
684 }
685
686 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
687 break;
688 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000689 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000691 const Expr *SubExpr;
692 SourceLocation CastLoc;
693 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
694 SubExpr = C->getSubExpr();
695 CastLoc = C->getLParenLoc();
696 } else {
697 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
698 CastLoc = getLocStart();
699 }
700
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000702 if (!SubExpr->getType()->isArithmeticType() ||
703 !getType()->isIntegerType()) {
704 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 return false;
706 }
707
708 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000709 if (SubExpr->getType()->isIntegerType()) {
710 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000712
713 // Figure out if this is a truncate, extend or noop cast.
714 unsigned DestWidth = Ctx.getTypeSize(getType(), CastLoc);
715
716 // If the input is signed, do a sign extend, noop, or truncate.
717 if (SubExpr->getType()->isSignedIntegerType())
718 Result.sextOrTrunc(DestWidth);
719 else // If the input is unsigned, do a zero extend, noop, or truncate.
720 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 break;
722 }
723
724 // Allow floating constants that are the immediate operands of casts or that
725 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000726 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
728 Operand = PE->getSubExpr();
729
730 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) {
731 // FIXME: Evaluate this correctly!
732 Result = (int)FL->getValue();
733 break;
734 }
735 if (Loc) *Loc = Operand->getLocStart();
736 return false;
737 }
738 case ConditionalOperatorClass: {
739 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
740
Chris Lattner590b6642007-07-15 23:26:56 +0000741 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 return false;
743
744 const Expr *TrueExp = Exp->getLHS();
745 const Expr *FalseExp = Exp->getRHS();
746 if (Result == 0) std::swap(TrueExp, FalseExp);
747
748 // Evaluate the false one first, discard the result.
Chris Lattner590b6642007-07-15 23:26:56 +0000749 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 return false;
751 // Evalute the true one, capture the result.
Chris Lattner590b6642007-07-15 23:26:56 +0000752 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 break;
755 }
756 }
757
758 // Cases that are valid constant exprs fall through to here.
759 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
760 return true;
761}
762
763
764/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
765/// integer constant expression with the value zero, or if this is one that is
766/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000767bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 // Strip off a cast to void*, if it exists.
769 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
770 // Check that it is a cast to void*.
771 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
772 QualType Pointee = PT->getPointeeType();
773 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
774 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Chris Lattner590b6642007-07-15 23:26:56 +0000775 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 }
Steve Naroff7269f2d2007-08-28 21:20:34 +0000777 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff19a6ebd2007-08-29 00:00:02 +0000778 // Ignore the ImplicitCastExpr type entirely.
779 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
781 // Accept ((void*)0) as a null pointer constant, as many other
782 // implementations do.
Chris Lattner590b6642007-07-15 23:26:56 +0000783 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 }
785
786 // This expression must be an integer type.
787 if (!getType()->isIntegerType())
788 return false;
789
790 // If we have an integer constant expression, we need to *evaluate* it and
791 // test for the value 0.
792 llvm::APSInt Val(32);
Chris Lattner590b6642007-07-15 23:26:56 +0000793 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000794}
Steve Naroff31a45842007-07-28 23:10:27 +0000795
Chris Lattner6481a572007-08-03 17:31:20 +0000796unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner4d0ac882007-08-03 16:00:20 +0000797 return strlen(Accessor.getName());
798}
799
800
Chris Lattnercb92a112007-08-02 21:47:28 +0000801/// getComponentType - Determine whether the components of this access are
802/// "point" "color" or "texture" elements.
Chris Lattner6481a572007-08-03 17:31:20 +0000803OCUVectorElementExpr::ElementType
804OCUVectorElementExpr::getElementType() const {
Steve Naroff31a45842007-07-28 23:10:27 +0000805 // derive the component type, no need to waste space.
806 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +0000807
Chris Lattner88dca042007-08-02 22:33:49 +0000808 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
809 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +0000810
Chris Lattner88dca042007-08-02 22:33:49 +0000811 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +0000812 "getComponentType(): Illegal accessor");
813 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +0000814}
Steve Narofffec0b492007-07-30 03:29:09 +0000815
Chris Lattner6481a572007-08-03 17:31:20 +0000816/// containsDuplicateElements - Return true if any element access is
Chris Lattnercb92a112007-08-02 21:47:28 +0000817/// repeated.
Chris Lattner6481a572007-08-03 17:31:20 +0000818bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +0000819 const char *compStr = Accessor.getName();
820 unsigned length = strlen(compStr);
821
822 for (unsigned i = 0; i < length-1; i++) {
823 const char *s = compStr+i;
824 for (const char c = *s++; *s; s++)
825 if (c == *s)
826 return true;
827 }
828 return false;
829}
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000830
831/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattner6481a572007-08-03 17:31:20 +0000832unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000833 const char *compStr = Accessor.getName();
Chris Lattner6481a572007-08-03 17:31:20 +0000834 unsigned length = getNumElements();
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000835
836 unsigned Result = 0;
837
838 while (length--) {
839 Result <<= 2;
840 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
841 assert(Idx != -1 && "Invalid accessor letter");
842 Result |= Idx;
843 }
844 return Result;
845}
846
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000847//===----------------------------------------------------------------------===//
848// Child Iterators for iterating over subexpressions/substatements
849//===----------------------------------------------------------------------===//
850
851// DeclRefExpr
Ted Kremenek1237c672007-08-24 20:06:47 +0000852Stmt::child_iterator DeclRefExpr::child_begin() { return NULL; }
853Stmt::child_iterator DeclRefExpr::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000854
855// PreDefinedExpr
Ted Kremenek1237c672007-08-24 20:06:47 +0000856Stmt::child_iterator PreDefinedExpr::child_begin() { return NULL; }
857Stmt::child_iterator PreDefinedExpr::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000858
859// IntegerLiteral
Ted Kremenek1237c672007-08-24 20:06:47 +0000860Stmt::child_iterator IntegerLiteral::child_begin() { return NULL; }
861Stmt::child_iterator IntegerLiteral::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000862
863// CharacterLiteral
Ted Kremenek1237c672007-08-24 20:06:47 +0000864Stmt::child_iterator CharacterLiteral::child_begin() { return NULL; }
865Stmt::child_iterator CharacterLiteral::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000866
867// FloatingLiteral
Ted Kremenek1237c672007-08-24 20:06:47 +0000868Stmt::child_iterator FloatingLiteral::child_begin() { return NULL; }
869Stmt::child_iterator FloatingLiteral::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000870
Chris Lattner5d661452007-08-26 03:42:43 +0000871// ImaginaryLiteral
872Stmt::child_iterator ImaginaryLiteral::child_begin() {
873 return reinterpret_cast<Stmt**>(&Val);
874}
875Stmt::child_iterator ImaginaryLiteral::child_end() {
876 return reinterpret_cast<Stmt**>(&Val)+1;
877}
878
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000879// StringLiteral
Ted Kremenek1237c672007-08-24 20:06:47 +0000880Stmt::child_iterator StringLiteral::child_begin() { return NULL; }
881Stmt::child_iterator StringLiteral::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000882
883// ParenExpr
884Stmt::child_iterator ParenExpr::child_begin() {
885 return reinterpret_cast<Stmt**>(&Val);
886}
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000887Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000888 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000889}
890
891// UnaryOperator
892Stmt::child_iterator UnaryOperator::child_begin() {
893 return reinterpret_cast<Stmt**>(&Val);
894}
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000895Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000896 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000897}
898
899// SizeOfAlignOfTypeExpr
Chris Lattner5d661452007-08-26 03:42:43 +0000900Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() { return NULL; }
901Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000902
903// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +0000904Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000905 return reinterpret_cast<Stmt**>(&SubExprs);
906}
Ted Kremenek1237c672007-08-24 20:06:47 +0000907Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000908 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000909}
910
911// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +0000912Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek42a29772007-08-27 21:11:44 +0000913 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000914}
Ted Kremenek1237c672007-08-24 20:06:47 +0000915Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek42a29772007-08-27 21:11:44 +0000916 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000917}
Ted Kremenek1237c672007-08-24 20:06:47 +0000918
919// MemberExpr
920Stmt::child_iterator MemberExpr::child_begin() {
921 return reinterpret_cast<Stmt**>(&Base);
922}
Ted Kremenek1237c672007-08-24 20:06:47 +0000923Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000924 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000925}
926
927// OCUVectorElementExpr
928Stmt::child_iterator OCUVectorElementExpr::child_begin() {
929 return reinterpret_cast<Stmt**>(&Base);
930}
Ted Kremenek1237c672007-08-24 20:06:47 +0000931Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000932 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000933}
934
935// CompoundLiteralExpr
936Stmt::child_iterator CompoundLiteralExpr::child_begin() {
937 return reinterpret_cast<Stmt**>(&Init);
938}
Ted Kremenek1237c672007-08-24 20:06:47 +0000939Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000940 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000941}
942
943// ImplicitCastExpr
944Stmt::child_iterator ImplicitCastExpr::child_begin() {
945 return reinterpret_cast<Stmt**>(&Op);
946}
Ted Kremenek1237c672007-08-24 20:06:47 +0000947Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000948 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000949}
950
951// CastExpr
952Stmt::child_iterator CastExpr::child_begin() {
953 return reinterpret_cast<Stmt**>(&Op);
954}
Ted Kremenek1237c672007-08-24 20:06:47 +0000955Stmt::child_iterator CastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000956 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000957}
958
959// BinaryOperator
960Stmt::child_iterator BinaryOperator::child_begin() {
961 return reinterpret_cast<Stmt**>(&SubExprs);
962}
Ted Kremenek1237c672007-08-24 20:06:47 +0000963Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000964 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +0000965}
966
967// ConditionalOperator
968Stmt::child_iterator ConditionalOperator::child_begin() {
969 return reinterpret_cast<Stmt**>(&SubExprs);
970}
Ted Kremenek1237c672007-08-24 20:06:47 +0000971Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000972 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +0000973}
974
975// AddrLabelExpr
976Stmt::child_iterator AddrLabelExpr::child_begin() { return NULL; }
977Stmt::child_iterator AddrLabelExpr::child_end() { return NULL; }
978
Ted Kremenek1237c672007-08-24 20:06:47 +0000979// StmtExpr
980Stmt::child_iterator StmtExpr::child_begin() {
981 return reinterpret_cast<Stmt**>(&SubStmt);
982}
Ted Kremenek1237c672007-08-24 20:06:47 +0000983Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000984 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000985}
986
987// TypesCompatibleExpr
988Stmt::child_iterator TypesCompatibleExpr::child_begin() { return NULL; }
989Stmt::child_iterator TypesCompatibleExpr::child_end() { return NULL; }
990
991// ChooseExpr
992Stmt::child_iterator ChooseExpr::child_begin() {
993 return reinterpret_cast<Stmt**>(&SubExprs);
994}
995
996Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000997 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +0000998}
999
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001000// InitListExpr
1001Stmt::child_iterator InitListExpr::child_begin() {
1002 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1003}
1004Stmt::child_iterator InitListExpr::child_end() {
1005 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1006}
1007
Ted Kremenek1237c672007-08-24 20:06:47 +00001008// ObjCStringLiteral
1009Stmt::child_iterator ObjCStringLiteral::child_begin() { return NULL; }
1010Stmt::child_iterator ObjCStringLiteral::child_end() { return NULL; }
1011
1012// ObjCEncodeExpr
1013Stmt::child_iterator ObjCEncodeExpr::child_begin() { return NULL; }
1014Stmt::child_iterator ObjCEncodeExpr::child_end() { return NULL; }
1015