blob: 0b1c7994fffbde3b74f28e07417d26f010332eb5 [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"
Steve Naroffc39ca262007-09-18 23:55:05 +000018// is this bad layering? I (snaroff) don't think so. Want Chris to weigh in.
19#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Primary Expressions.
24//===----------------------------------------------------------------------===//
25
26StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
27 bool Wide, QualType t, SourceLocation firstLoc,
28 SourceLocation lastLoc) :
29 Expr(StringLiteralClass, t) {
30 // OPTIMIZE: could allocate this appended to the StringLiteral.
31 char *AStrData = new char[byteLength];
32 memcpy(AStrData, strData, byteLength);
33 StrData = AStrData;
34 ByteLength = byteLength;
35 IsWide = Wide;
36 firstTokLoc = firstLoc;
37 lastTokLoc = lastLoc;
38}
39
40StringLiteral::~StringLiteral() {
41 delete[] StrData;
42}
43
44bool UnaryOperator::isPostfix(Opcode Op) {
45 switch (Op) {
46 case PostInc:
47 case PostDec:
48 return true;
49 default:
50 return false;
51 }
52}
53
54/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
55/// corresponds to, e.g. "sizeof" or "[pre]++".
56const char *UnaryOperator::getOpcodeStr(Opcode Op) {
57 switch (Op) {
58 default: assert(0 && "Unknown unary operator");
59 case PostInc: return "++";
60 case PostDec: return "--";
61 case PreInc: return "++";
62 case PreDec: return "--";
63 case AddrOf: return "&";
64 case Deref: return "*";
65 case Plus: return "+";
66 case Minus: return "-";
67 case Not: return "~";
68 case LNot: return "!";
69 case Real: return "__real";
70 case Imag: return "__imag";
71 case SizeOf: return "sizeof";
72 case AlignOf: return "alignof";
73 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000074 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000075 }
76}
77
78//===----------------------------------------------------------------------===//
79// Postfix Operators.
80//===----------------------------------------------------------------------===//
81
82CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
83 SourceLocation rparenloc)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000084 : Expr(CallExprClass, t), NumArgs(numargs) {
85 SubExprs = new Expr*[numargs+1];
86 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +000087 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000088 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +000089 RParenLoc = rparenloc;
90}
91
Steve Naroff8d3b1702007-08-08 22:15:55 +000092bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
93 // The following enum mimics gcc's internal "typeclass.h" file.
94 enum gcc_type_class {
95 no_type_class = -1,
96 void_type_class, integer_type_class, char_type_class,
97 enumeral_type_class, boolean_type_class,
98 pointer_type_class, reference_type_class, offset_type_class,
99 real_type_class, complex_type_class,
100 function_type_class, method_type_class,
101 record_type_class, union_type_class,
102 array_type_class, string_type_class,
103 lang_type_class
104 };
105 Result.setIsSigned(true);
106
107 // All simple function calls (e.g. func()) are implicitly cast to pointer to
108 // function. As a result, we try and obtain the DeclRefExpr from the
109 // ImplicitCastExpr.
110 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
111 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
112 return false;
113 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
114 if (!DRE)
115 return false;
116
117 // We have a DeclRefExpr.
118 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
119 // If no argument was supplied, default to "no_type_class". This isn't
120 // ideal, however it's what gcc does.
121 Result = static_cast<uint64_t>(no_type_class);
122 if (NumArgs >= 1) {
123 QualType argType = getArg(0)->getType();
124
125 if (argType->isVoidType())
126 Result = void_type_class;
127 else if (argType->isEnumeralType())
128 Result = enumeral_type_class;
129 else if (argType->isBooleanType())
130 Result = boolean_type_class;
131 else if (argType->isCharType())
132 Result = string_type_class; // gcc doesn't appear to use char_type_class
133 else if (argType->isIntegerType())
134 Result = integer_type_class;
135 else if (argType->isPointerType())
136 Result = pointer_type_class;
137 else if (argType->isReferenceType())
138 Result = reference_type_class;
139 else if (argType->isRealType())
140 Result = real_type_class;
141 else if (argType->isComplexType())
142 Result = complex_type_class;
143 else if (argType->isFunctionType())
144 Result = function_type_class;
145 else if (argType->isStructureType())
146 Result = record_type_class;
147 else if (argType->isUnionType())
148 Result = union_type_class;
149 else if (argType->isArrayType())
150 Result = array_type_class;
151 else if (argType->isUnionType())
152 Result = union_type_class;
153 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
154 assert(1 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
155 }
156 return true;
157 }
158 return false;
159}
160
Chris Lattner4b009652007-07-25 00:24:17 +0000161/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
162/// corresponds to, e.g. "<<=".
163const char *BinaryOperator::getOpcodeStr(Opcode Op) {
164 switch (Op) {
165 default: assert(0 && "Unknown binary operator");
166 case Mul: return "*";
167 case Div: return "/";
168 case Rem: return "%";
169 case Add: return "+";
170 case Sub: return "-";
171 case Shl: return "<<";
172 case Shr: return ">>";
173 case LT: return "<";
174 case GT: return ">";
175 case LE: return "<=";
176 case GE: return ">=";
177 case EQ: return "==";
178 case NE: return "!=";
179 case And: return "&";
180 case Xor: return "^";
181 case Or: return "|";
182 case LAnd: return "&&";
183 case LOr: return "||";
184 case Assign: return "=";
185 case MulAssign: return "*=";
186 case DivAssign: return "/=";
187 case RemAssign: return "%=";
188 case AddAssign: return "+=";
189 case SubAssign: return "-=";
190 case ShlAssign: return "<<=";
191 case ShrAssign: return ">>=";
192 case AndAssign: return "&=";
193 case XorAssign: return "^=";
194 case OrAssign: return "|=";
195 case Comma: return ",";
196 }
197}
198
Anders Carlsson762b7c72007-08-31 04:56:16 +0000199InitListExpr::InitListExpr(SourceLocation lbraceloc,
200 Expr **initexprs, unsigned numinits,
201 SourceLocation rbraceloc)
202 : Expr(InitListExprClass, QualType())
203 , NumInits(numinits)
204 , LBraceLoc(lbraceloc)
205 , RBraceLoc(rbraceloc)
206{
207 InitExprs = new Expr*[numinits];
208 for (unsigned i = 0; i != numinits; i++)
209 InitExprs[i] = initexprs[i];
210}
Chris Lattner4b009652007-07-25 00:24:17 +0000211
212//===----------------------------------------------------------------------===//
213// Generic Expression Routines
214//===----------------------------------------------------------------------===//
215
216/// hasLocalSideEffect - Return true if this immediate expression has side
217/// effects, not counting any sub-expressions.
218bool Expr::hasLocalSideEffect() const {
219 switch (getStmtClass()) {
220 default:
221 return false;
222 case ParenExprClass:
223 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
224 case UnaryOperatorClass: {
225 const UnaryOperator *UO = cast<UnaryOperator>(this);
226
227 switch (UO->getOpcode()) {
228 default: return false;
229 case UnaryOperator::PostInc:
230 case UnaryOperator::PostDec:
231 case UnaryOperator::PreInc:
232 case UnaryOperator::PreDec:
233 return true; // ++/--
234
235 case UnaryOperator::Deref:
236 // Dereferencing a volatile pointer is a side-effect.
237 return getType().isVolatileQualified();
238 case UnaryOperator::Real:
239 case UnaryOperator::Imag:
240 // accessing a piece of a volatile complex is a side-effect.
241 return UO->getSubExpr()->getType().isVolatileQualified();
242
243 case UnaryOperator::Extension:
244 return UO->getSubExpr()->hasLocalSideEffect();
245 }
246 }
247 case BinaryOperatorClass:
248 return cast<BinaryOperator>(this)->isAssignmentOp();
Chris Lattner06078d22007-08-25 02:00:02 +0000249 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000250 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000251
252 case MemberExprClass:
253 case ArraySubscriptExprClass:
254 // If the base pointer or element is to a volatile pointer/field, accessing
255 // if is a side effect.
256 return getType().isVolatileQualified();
257
258 case CallExprClass:
259 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
260 // should warn.
261 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000262 case ObjCMessageExprClass:
263 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000264
265 case CastExprClass:
266 // If this is a cast to void, check the operand. Otherwise, the result of
267 // the cast is unused.
268 if (getType()->isVoidType())
269 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
270 return false;
271 }
272}
273
274/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
275/// incomplete type other than void. Nonarray expressions that can be lvalues:
276/// - name, where name must be a variable
277/// - e[i]
278/// - (e), where e must be an lvalue
279/// - e.name, where e must be an lvalue
280/// - e->name
281/// - *e, the type of e cannot be a function type
282/// - string-constant
283/// - reference type [C++ [expr]]
284///
285Expr::isLvalueResult Expr::isLvalue() const {
286 // first, check the type (C99 6.3.2.1)
287 if (TR->isFunctionType()) // from isObjectType()
288 return LV_NotObjectType;
289
290 if (TR->isVoidType())
291 return LV_IncompleteVoidType;
292
293 if (TR->isReferenceType()) // C++ [expr]
294 return LV_Valid;
295
296 // the type looks fine, now check the expression
297 switch (getStmtClass()) {
298 case StringLiteralClass: // C99 6.5.1p4
299 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
300 // For vectors, make sure base is an lvalue (i.e. not a function call).
301 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
302 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
303 return LV_Valid;
304 case DeclRefExprClass: // C99 6.5.1p2
305 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
306 return LV_Valid;
307 break;
308 case MemberExprClass: { // C99 6.5.2.3p4
309 const MemberExpr *m = cast<MemberExpr>(this);
310 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
311 }
312 case UnaryOperatorClass: // C99 6.5.3p4
313 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
314 return LV_Valid;
315 break;
316 case ParenExprClass: // C99 6.5.1p5
317 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000318 case OCUVectorElementExprClass:
319 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000320 return LV_DuplicateVectorComponents;
321 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000322 default:
323 break;
324 }
325 return LV_InvalidExpression;
326}
327
328/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
329/// does not have an incomplete type, does not have a const-qualified type, and
330/// if it is a structure or union, does not have any member (including,
331/// recursively, any member or element of all contained aggregates or unions)
332/// with a const-qualified type.
333Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
334 isLvalueResult lvalResult = isLvalue();
335
336 switch (lvalResult) {
337 case LV_Valid: break;
338 case LV_NotObjectType: return MLV_NotObjectType;
339 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000340 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000341 case LV_InvalidExpression: return MLV_InvalidExpression;
342 }
343 if (TR.isConstQualified())
344 return MLV_ConstQualified;
345 if (TR->isArrayType())
346 return MLV_ArrayType;
347 if (TR->isIncompleteType())
348 return MLV_IncompleteType;
349
350 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
351 if (r->hasConstFields())
352 return MLV_ConstQualified;
353 }
354 return MLV_Valid;
355}
356
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000357bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
358
359 switch (getStmtClass()) {
360 default:
361 if (Loc) *Loc = getLocStart();
362 return false;
363 case ParenExprClass:
364 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
365 case StringLiteralClass:
366 case FloatingLiteralClass:
367 case IntegerLiteralClass:
368 case CharacterLiteralClass:
369 case ImaginaryLiteralClass:
370 case TypesCompatibleExprClass:
371 break;
372 case CallExprClass: {
373 const CallExpr *CE = cast<CallExpr>(this);
374 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000375 Result.zextOrTrunc(
376 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000377 if (CE->isBuiltinClassifyType(Result))
378 break;
379 if (Loc) *Loc = getLocStart();
380 return false;
381 }
382 case DeclRefExprClass:
383 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl()))
384 break;
385 if (Loc) *Loc = getLocStart();
386 return false;
387 case UnaryOperatorClass: {
388 const UnaryOperator *Exp = cast<UnaryOperator>(this);
389
390 // Get the operand value. If this is sizeof/alignof, do not evalute the
391 // operand. This affects C99 6.6p3.
392 if (!Exp->isSizeOfAlignOfOp() &&
393 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
394 return false;
395
396 switch (Exp->getOpcode()) {
397 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
398 // See C99 6.6p3.
399 default:
400 if (Loc) *Loc = Exp->getOperatorLoc();
401 return false;
402 case UnaryOperator::Extension:
403 return true; // FIXME: this is wrong.
404 case UnaryOperator::SizeOf:
405 case UnaryOperator::AlignOf:
406 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
407 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
408 return false;
409 break;
410 case UnaryOperator::LNot:
411 case UnaryOperator::Plus:
412 case UnaryOperator::Minus:
413 case UnaryOperator::Not:
414 break;
415 }
416 break;
417 }
418 case SizeOfAlignOfTypeExprClass: {
419 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
420 // alignof always evaluates to a constant.
421 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
422 return false;
423 break;
424 }
425 case BinaryOperatorClass: {
426 const BinaryOperator *Exp = cast<BinaryOperator>(this);
427
428 // The LHS of a constant expr is always evaluated and needed.
429 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
430 return false;
431
432 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
433 return false;
434
435 break;
436 }
437 case ImplicitCastExprClass:
438 case CastExprClass: {
439 const Expr *SubExpr;
440 SourceLocation CastLoc;
441 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
442 SubExpr = C->getSubExpr();
443 CastLoc = C->getLParenLoc();
444 } else {
445 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
446 CastLoc = getLocStart();
447 }
448 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
449 if (Loc) *Loc = SubExpr->getLocStart();
450 return false;
451 }
452 break;
453 }
454 case ConditionalOperatorClass: {
455 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
456
457 if (!Exp->getCond()->isConstantExpr(Ctx, Loc))
458 return false;
459
460 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
461 return false;
462
463 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
464 return false;
465 break;
466 }
467 }
468
469 return true;
470}
471
Chris Lattner4b009652007-07-25 00:24:17 +0000472/// isIntegerConstantExpr - this recursive routine will test if an expression is
473/// an integer constant expression. Note: With the introduction of VLA's in
474/// C99 the result of the sizeof operator is no longer always a constant
475/// expression. The generalization of the wording to include any subexpression
476/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
477/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
478/// "0 || f()" can be treated as a constant expression. In C90 this expression,
479/// occurring in a context requiring a constant, would have been a constraint
480/// violation. FIXME: This routine currently implements C90 semantics.
481/// To properly implement C99 semantics this routine will need to evaluate
482/// expressions involving operators previously mentioned.
483
484/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
485/// comma, etc
486///
487/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000488/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000489///
490/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
491/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
492/// cast+dereference.
493bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
494 SourceLocation *Loc, bool isEvaluated) const {
495 switch (getStmtClass()) {
496 default:
497 if (Loc) *Loc = getLocStart();
498 return false;
499 case ParenExprClass:
500 return cast<ParenExpr>(this)->getSubExpr()->
501 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
502 case IntegerLiteralClass:
503 Result = cast<IntegerLiteral>(this)->getValue();
504 break;
505 case CharacterLiteralClass: {
506 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000507 Result.zextOrTrunc(
508 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000509 Result = CL->getValue();
510 Result.setIsUnsigned(!getType()->isSignedIntegerType());
511 break;
512 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000513 case TypesCompatibleExprClass: {
514 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000515 Result.zextOrTrunc(
516 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000517 Result = TCE->typesAreCompatible();
Steve Naroff1200b5a2007-08-02 00:13:27 +0000518 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000519 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000520 case CallExprClass: {
521 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000522 Result.zextOrTrunc(
523 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000524 if (CE->isBuiltinClassifyType(Result))
525 break;
526 if (Loc) *Loc = getLocStart();
527 return false;
528 }
Chris Lattner4b009652007-07-25 00:24:17 +0000529 case DeclRefExprClass:
530 if (const EnumConstantDecl *D =
531 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
532 Result = D->getInitVal();
533 break;
534 }
535 if (Loc) *Loc = getLocStart();
536 return false;
537 case UnaryOperatorClass: {
538 const UnaryOperator *Exp = cast<UnaryOperator>(this);
539
540 // Get the operand value. If this is sizeof/alignof, do not evalute the
541 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000542 if (!Exp->isSizeOfAlignOfOp() &&
543 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000544 return false;
545
546 switch (Exp->getOpcode()) {
547 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
548 // See C99 6.6p3.
549 default:
550 if (Loc) *Loc = Exp->getOperatorLoc();
551 return false;
552 case UnaryOperator::Extension:
553 return true; // FIXME: this is wrong.
554 case UnaryOperator::SizeOf:
555 case UnaryOperator::AlignOf:
556 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
557 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
558 return false;
559
560 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000561 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000562 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
563 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000564
565 // Get information about the size or align.
566 if (Exp->getOpcode() == UnaryOperator::SizeOf)
567 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
568 Exp->getOperatorLoc());
569 else
570 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
571 Exp->getOperatorLoc());
572 break;
573 case UnaryOperator::LNot: {
574 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000575 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000576 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
577 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000578 Result = Val;
579 break;
580 }
581 case UnaryOperator::Plus:
582 break;
583 case UnaryOperator::Minus:
584 Result = -Result;
585 break;
586 case UnaryOperator::Not:
587 Result = ~Result;
588 break;
589 }
590 break;
591 }
592 case SizeOfAlignOfTypeExprClass: {
593 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
594 // alignof always evaluates to a constant.
595 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
596 return false;
597
598 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000599 Result.zextOrTrunc(
600 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000601
602 // Get information about the size or align.
603 if (Exp->isSizeOf())
604 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
605 else
606 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
607 break;
608 }
609 case BinaryOperatorClass: {
610 const BinaryOperator *Exp = cast<BinaryOperator>(this);
611
612 // The LHS of a constant expr is always evaluated and needed.
613 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
614 return false;
615
616 llvm::APSInt RHS(Result);
617
618 // The short-circuiting &&/|| operators don't necessarily evaluate their
619 // RHS. Make sure to pass isEvaluated down correctly.
620 if (Exp->isLogicalOp()) {
621 bool RHSEval;
622 if (Exp->getOpcode() == BinaryOperator::LAnd)
623 RHSEval = Result != 0;
624 else {
625 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
626 RHSEval = Result == 0;
627 }
628
629 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
630 isEvaluated & RHSEval))
631 return false;
632 } else {
633 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
634 return false;
635 }
636
637 switch (Exp->getOpcode()) {
638 default:
639 if (Loc) *Loc = getLocStart();
640 return false;
641 case BinaryOperator::Mul:
642 Result *= RHS;
643 break;
644 case BinaryOperator::Div:
645 if (RHS == 0) {
646 if (!isEvaluated) break;
647 if (Loc) *Loc = getLocStart();
648 return false;
649 }
650 Result /= RHS;
651 break;
652 case BinaryOperator::Rem:
653 if (RHS == 0) {
654 if (!isEvaluated) break;
655 if (Loc) *Loc = getLocStart();
656 return false;
657 }
658 Result %= RHS;
659 break;
660 case BinaryOperator::Add: Result += RHS; break;
661 case BinaryOperator::Sub: Result -= RHS; break;
662 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000663 Result <<=
664 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000665 break;
666 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000667 Result >>=
668 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000669 break;
670 case BinaryOperator::LT: Result = Result < RHS; break;
671 case BinaryOperator::GT: Result = Result > RHS; break;
672 case BinaryOperator::LE: Result = Result <= RHS; break;
673 case BinaryOperator::GE: Result = Result >= RHS; break;
674 case BinaryOperator::EQ: Result = Result == RHS; break;
675 case BinaryOperator::NE: Result = Result != RHS; break;
676 case BinaryOperator::And: Result &= RHS; break;
677 case BinaryOperator::Xor: Result ^= RHS; break;
678 case BinaryOperator::Or: Result |= RHS; break;
679 case BinaryOperator::LAnd:
680 Result = Result != 0 && RHS != 0;
681 break;
682 case BinaryOperator::LOr:
683 Result = Result != 0 || RHS != 0;
684 break;
685
686 case BinaryOperator::Comma:
687 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
688 // *except* when they are contained within a subexpression that is not
689 // evaluated". Note that Assignment can never happen due to constraints
690 // on the LHS subexpr, so we don't need to check it here.
691 if (isEvaluated) {
692 if (Loc) *Loc = getLocStart();
693 return false;
694 }
695
696 // The result of the constant expr is the RHS.
697 Result = RHS;
698 return true;
699 }
700
701 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
702 break;
703 }
704 case ImplicitCastExprClass:
705 case CastExprClass: {
706 const Expr *SubExpr;
707 SourceLocation CastLoc;
708 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
709 SubExpr = C->getSubExpr();
710 CastLoc = C->getLParenLoc();
711 } else {
712 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
713 CastLoc = getLocStart();
714 }
715
716 // C99 6.6p6: shall only convert arithmetic types to integer types.
717 if (!SubExpr->getType()->isArithmeticType() ||
718 !getType()->isIntegerType()) {
719 if (Loc) *Loc = SubExpr->getLocStart();
720 return false;
721 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000722
723 uint32_t DestWidth =
724 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
725
Chris Lattner4b009652007-07-25 00:24:17 +0000726 // Handle simple integer->integer casts.
727 if (SubExpr->getType()->isIntegerType()) {
728 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
729 return false;
730
731 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000732 // If the input is signed, do a sign extend, noop, or truncate.
733 if (SubExpr->getType()->isSignedIntegerType())
734 Result.sextOrTrunc(DestWidth);
735 else // If the input is unsigned, do a zero extend, noop, or truncate.
736 Result.zextOrTrunc(DestWidth);
737 break;
738 }
739
740 // Allow floating constants that are the immediate operands of casts or that
741 // are parenthesized.
742 const Expr *Operand = SubExpr;
743 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
744 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000745
746 // If this isn't a floating literal, we can't handle it.
747 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
748 if (!FL) {
749 if (Loc) *Loc = Operand->getLocStart();
750 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000751 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000752
753 // Determine whether we are converting to unsigned or signed.
754 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000755
756 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
757 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000758 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000759 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
760 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000761 Result = llvm::APInt(DestWidth, 4, Space);
762 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000763 }
764 case ConditionalOperatorClass: {
765 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
766
767 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
768 return false;
769
770 const Expr *TrueExp = Exp->getLHS();
771 const Expr *FalseExp = Exp->getRHS();
772 if (Result == 0) std::swap(TrueExp, FalseExp);
773
774 // Evaluate the false one first, discard the result.
775 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
776 return false;
777 // Evalute the true one, capture the result.
778 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
779 return false;
780 break;
781 }
782 }
783
784 // Cases that are valid constant exprs fall through to here.
785 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
786 return true;
787}
788
789
790/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
791/// integer constant expression with the value zero, or if this is one that is
792/// cast to void*.
793bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
794 // Strip off a cast to void*, if it exists.
795 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
796 // Check that it is a cast to void*.
797 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
798 QualType Pointee = PT->getPointeeType();
799 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
800 CE->getSubExpr()->getType()->isIntegerType()) // from int.
801 return CE->getSubExpr()->isNullPointerConstant(Ctx);
802 }
Steve Naroff1d6b2472007-08-28 21:20:34 +0000803 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff3d052872007-08-29 00:00:02 +0000804 // Ignore the ImplicitCastExpr type entirely.
805 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000806 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
807 // Accept ((void*)0) as a null pointer constant, as many other
808 // implementations do.
809 return PE->getSubExpr()->isNullPointerConstant(Ctx);
810 }
811
812 // This expression must be an integer type.
813 if (!getType()->isIntegerType())
814 return false;
815
816 // If we have an integer constant expression, we need to *evaluate* it and
817 // test for the value 0.
818 llvm::APSInt Val(32);
819 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
820}
Steve Naroffc11705f2007-07-28 23:10:27 +0000821
Chris Lattnera0d03a72007-08-03 17:31:20 +0000822unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000823 return strlen(Accessor.getName());
824}
825
826
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000827/// getComponentType - Determine whether the components of this access are
828/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000829OCUVectorElementExpr::ElementType
830OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000831 // derive the component type, no need to waste space.
832 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000833
Chris Lattner9096b792007-08-02 22:33:49 +0000834 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
835 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000836
Chris Lattner9096b792007-08-02 22:33:49 +0000837 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000838 "getComponentType(): Illegal accessor");
839 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000840}
Steve Naroffba67f692007-07-30 03:29:09 +0000841
Chris Lattnera0d03a72007-08-03 17:31:20 +0000842/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000843/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000844bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000845 const char *compStr = Accessor.getName();
846 unsigned length = strlen(compStr);
847
848 for (unsigned i = 0; i < length-1; i++) {
849 const char *s = compStr+i;
850 for (const char c = *s++; *s; s++)
851 if (c == *s)
852 return true;
853 }
854 return false;
855}
Chris Lattner42158e72007-08-02 23:36:59 +0000856
857/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000858unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000859 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000860 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +0000861
862 unsigned Result = 0;
863
864 while (length--) {
865 Result <<= 2;
866 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
867 assert(Idx != -1 && "Invalid accessor letter");
868 Result |= Idx;
869 }
870 return Result;
871}
872
Steve Naroffc39ca262007-09-18 23:55:05 +0000873// constructor for unary messages.
874ObjCMessageExpr::ObjCMessageExpr(
Steve Naroff4bc52ce2007-09-19 16:18:46 +0000875 IdentifierInfo *clsName, IdentifierInfo &methName, QualType retType,
Steve Naroffc39ca262007-09-18 23:55:05 +0000876 SourceLocation LBrac, SourceLocation RBrac)
877 : Expr(ObjCMessageExprClass, retType), Selector(methName) {
878 ClassName = clsName;
879 LBracloc = LBrac;
880 RBracloc = RBrac;
881}
882
883ObjCMessageExpr::ObjCMessageExpr(
Steve Naroff4bc52ce2007-09-19 16:18:46 +0000884 Expr *fn, IdentifierInfo &methName, QualType retType,
Steve Naroffc39ca262007-09-18 23:55:05 +0000885 SourceLocation LBrac, SourceLocation RBrac)
886 : Expr(ObjCMessageExprClass, retType), Selector(methName), ClassName(0) {
887 SubExprs = new Expr*[1];
888 SubExprs[RECEIVER] = fn;
889 LBracloc = LBrac;
890 RBracloc = RBrac;
891}
892
893// constructor for keyword messages.
894ObjCMessageExpr::ObjCMessageExpr(
Steve Naroff4bc52ce2007-09-19 16:18:46 +0000895 Expr *fn, IdentifierInfo &selInfo, ObjcKeywordMessage *keys, unsigned numargs,
Steve Naroffc39ca262007-09-18 23:55:05 +0000896 QualType retType, SourceLocation LBrac, SourceLocation RBrac)
897 : Expr(ObjCMessageExprClass, retType), Selector(selInfo), ClassName(0) {
898 SubExprs = new Expr*[numargs+1];
899 SubExprs[RECEIVER] = fn;
900 for (unsigned i = 0; i != numargs; ++i)
901 SubExprs[i+ARGS_START] = static_cast<Expr *>(keys[i].KeywordExpr);
902 LBracloc = LBrac;
903 RBracloc = RBrac;
904}
905
906ObjCMessageExpr::ObjCMessageExpr(
Steve Naroff4bc52ce2007-09-19 16:18:46 +0000907 IdentifierInfo *clsName, IdentifierInfo &selInfo, ObjcKeywordMessage *keys,
Steve Naroffc39ca262007-09-18 23:55:05 +0000908 unsigned numargs, QualType retType, SourceLocation LBrac, SourceLocation RBrac)
909 : Expr(ObjCMessageExprClass, retType), Selector(selInfo), ClassName(clsName) {
910 SubExprs = new Expr*[numargs+1];
911 SubExprs[RECEIVER] = 0;
912 for (unsigned i = 0; i != numargs; ++i)
913 SubExprs[i+ARGS_START] = static_cast<Expr *>(keys[i].KeywordExpr);
914 LBracloc = LBrac;
915 RBracloc = RBrac;
916}
917
918
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() {
1093 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
1094}
1095