blob: 33b56e52b6eaac2aadec23adfae1adeb41c5bc82 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/StmtVisitor.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000018#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// Primary Expressions.
23//===----------------------------------------------------------------------===//
24
25StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
26 bool Wide, QualType t, SourceLocation firstLoc,
27 SourceLocation lastLoc) :
28 Expr(StringLiteralClass, t) {
29 // OPTIMIZE: could allocate this appended to the StringLiteral.
30 char *AStrData = new char[byteLength];
31 memcpy(AStrData, strData, byteLength);
32 StrData = AStrData;
33 ByteLength = byteLength;
34 IsWide = Wide;
35 firstTokLoc = firstLoc;
36 lastTokLoc = lastLoc;
37}
38
39StringLiteral::~StringLiteral() {
40 delete[] StrData;
41}
42
43bool UnaryOperator::isPostfix(Opcode Op) {
44 switch (Op) {
45 case PostInc:
46 case PostDec:
47 return true;
48 default:
49 return false;
50 }
51}
52
53/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
54/// corresponds to, e.g. "sizeof" or "[pre]++".
55const char *UnaryOperator::getOpcodeStr(Opcode Op) {
56 switch (Op) {
57 default: assert(0 && "Unknown unary operator");
58 case PostInc: return "++";
59 case PostDec: return "--";
60 case PreInc: return "++";
61 case PreDec: return "--";
62 case AddrOf: return "&";
63 case Deref: return "*";
64 case Plus: return "+";
65 case Minus: return "-";
66 case Not: return "~";
67 case LNot: return "!";
68 case Real: return "__real";
69 case Imag: return "__imag";
70 case SizeOf: return "sizeof";
71 case AlignOf: return "alignof";
72 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000073 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000074 }
75}
76
77//===----------------------------------------------------------------------===//
78// Postfix Operators.
79//===----------------------------------------------------------------------===//
80
81CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
82 SourceLocation rparenloc)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000083 : Expr(CallExprClass, t), NumArgs(numargs) {
84 SubExprs = new Expr*[numargs+1];
85 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +000086 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000087 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +000088 RParenLoc = rparenloc;
89}
90
Steve Naroff8d3b1702007-08-08 22:15:55 +000091bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
92 // The following enum mimics gcc's internal "typeclass.h" file.
93 enum gcc_type_class {
94 no_type_class = -1,
95 void_type_class, integer_type_class, char_type_class,
96 enumeral_type_class, boolean_type_class,
97 pointer_type_class, reference_type_class, offset_type_class,
98 real_type_class, complex_type_class,
99 function_type_class, method_type_class,
100 record_type_class, union_type_class,
101 array_type_class, string_type_class,
102 lang_type_class
103 };
104 Result.setIsSigned(true);
105
106 // All simple function calls (e.g. func()) are implicitly cast to pointer to
107 // function. As a result, we try and obtain the DeclRefExpr from the
108 // ImplicitCastExpr.
109 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
110 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
111 return false;
112 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
113 if (!DRE)
114 return false;
115
116 // We have a DeclRefExpr.
117 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
118 // If no argument was supplied, default to "no_type_class". This isn't
119 // ideal, however it's what gcc does.
120 Result = static_cast<uint64_t>(no_type_class);
121 if (NumArgs >= 1) {
122 QualType argType = getArg(0)->getType();
123
124 if (argType->isVoidType())
125 Result = void_type_class;
126 else if (argType->isEnumeralType())
127 Result = enumeral_type_class;
128 else if (argType->isBooleanType())
129 Result = boolean_type_class;
130 else if (argType->isCharType())
131 Result = string_type_class; // gcc doesn't appear to use char_type_class
132 else if (argType->isIntegerType())
133 Result = integer_type_class;
134 else if (argType->isPointerType())
135 Result = pointer_type_class;
136 else if (argType->isReferenceType())
137 Result = reference_type_class;
138 else if (argType->isRealType())
139 Result = real_type_class;
140 else if (argType->isComplexType())
141 Result = complex_type_class;
142 else if (argType->isFunctionType())
143 Result = function_type_class;
144 else if (argType->isStructureType())
145 Result = record_type_class;
146 else if (argType->isUnionType())
147 Result = union_type_class;
148 else if (argType->isArrayType())
149 Result = array_type_class;
150 else if (argType->isUnionType())
151 Result = union_type_class;
152 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner19b8f1a2007-11-08 17:56:40 +0000153 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff8d3b1702007-08-08 22:15:55 +0000154 }
155 return true;
156 }
157 return false;
158}
159
Chris Lattner4b009652007-07-25 00:24:17 +0000160/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
161/// corresponds to, e.g. "<<=".
162const char *BinaryOperator::getOpcodeStr(Opcode Op) {
163 switch (Op) {
164 default: assert(0 && "Unknown binary operator");
165 case Mul: return "*";
166 case Div: return "/";
167 case Rem: return "%";
168 case Add: return "+";
169 case Sub: return "-";
170 case Shl: return "<<";
171 case Shr: return ">>";
172 case LT: return "<";
173 case GT: return ">";
174 case LE: return "<=";
175 case GE: return ">=";
176 case EQ: return "==";
177 case NE: return "!=";
178 case And: return "&";
179 case Xor: return "^";
180 case Or: return "|";
181 case LAnd: return "&&";
182 case LOr: return "||";
183 case Assign: return "=";
184 case MulAssign: return "*=";
185 case DivAssign: return "/=";
186 case RemAssign: return "%=";
187 case AddAssign: return "+=";
188 case SubAssign: return "-=";
189 case ShlAssign: return "<<=";
190 case ShrAssign: return ">>=";
191 case AndAssign: return "&=";
192 case XorAssign: return "^=";
193 case OrAssign: return "|=";
194 case Comma: return ",";
195 }
196}
197
Anders Carlsson762b7c72007-08-31 04:56:16 +0000198InitListExpr::InitListExpr(SourceLocation lbraceloc,
199 Expr **initexprs, unsigned numinits,
200 SourceLocation rbraceloc)
201 : Expr(InitListExprClass, QualType())
202 , NumInits(numinits)
203 , LBraceLoc(lbraceloc)
204 , RBraceLoc(rbraceloc)
205{
206 InitExprs = new Expr*[numinits];
207 for (unsigned i = 0; i != numinits; i++)
208 InitExprs[i] = initexprs[i];
209}
Chris Lattner4b009652007-07-25 00:24:17 +0000210
211//===----------------------------------------------------------------------===//
212// Generic Expression Routines
213//===----------------------------------------------------------------------===//
214
215/// hasLocalSideEffect - Return true if this immediate expression has side
216/// effects, not counting any sub-expressions.
217bool Expr::hasLocalSideEffect() const {
218 switch (getStmtClass()) {
219 default:
220 return false;
221 case ParenExprClass:
222 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
223 case UnaryOperatorClass: {
224 const UnaryOperator *UO = cast<UnaryOperator>(this);
225
226 switch (UO->getOpcode()) {
227 default: return false;
228 case UnaryOperator::PostInc:
229 case UnaryOperator::PostDec:
230 case UnaryOperator::PreInc:
231 case UnaryOperator::PreDec:
232 return true; // ++/--
233
234 case UnaryOperator::Deref:
235 // Dereferencing a volatile pointer is a side-effect.
236 return getType().isVolatileQualified();
237 case UnaryOperator::Real:
238 case UnaryOperator::Imag:
239 // accessing a piece of a volatile complex is a side-effect.
240 return UO->getSubExpr()->getType().isVolatileQualified();
241
242 case UnaryOperator::Extension:
243 return UO->getSubExpr()->hasLocalSideEffect();
244 }
245 }
246 case BinaryOperatorClass:
247 return cast<BinaryOperator>(this)->isAssignmentOp();
Chris Lattner06078d22007-08-25 02:00:02 +0000248 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000249 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000250
251 case MemberExprClass:
252 case ArraySubscriptExprClass:
253 // If the base pointer or element is to a volatile pointer/field, accessing
254 // if is a side effect.
255 return getType().isVolatileQualified();
256
257 case CallExprClass:
258 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
259 // should warn.
260 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000261 case ObjCMessageExprClass:
262 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000263
264 case CastExprClass:
265 // If this is a cast to void, check the operand. Otherwise, the result of
266 // the cast is unused.
267 if (getType()->isVoidType())
268 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
269 return false;
270 }
271}
272
273/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
274/// incomplete type other than void. Nonarray expressions that can be lvalues:
275/// - name, where name must be a variable
276/// - e[i]
277/// - (e), where e must be an lvalue
278/// - e.name, where e must be an lvalue
279/// - e->name
280/// - *e, the type of e cannot be a function type
281/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000282/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000283/// - 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 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000312 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000313 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000314 return LV_Valid; // C99 6.5.3p4
315
316 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
317 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
318 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000319 break;
320 case ParenExprClass: // C99 6.5.1p5
321 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000322 case OCUVectorElementExprClass:
323 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000324 return LV_DuplicateVectorComponents;
325 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000326 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
327 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000328 default:
329 break;
330 }
331 return LV_InvalidExpression;
332}
333
334/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
335/// does not have an incomplete type, does not have a const-qualified type, and
336/// if it is a structure or union, does not have any member (including,
337/// recursively, any member or element of all contained aggregates or unions)
338/// with a const-qualified type.
339Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
340 isLvalueResult lvalResult = isLvalue();
341
342 switch (lvalResult) {
343 case LV_Valid: break;
344 case LV_NotObjectType: return MLV_NotObjectType;
345 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000346 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000347 case LV_InvalidExpression: return MLV_InvalidExpression;
348 }
349 if (TR.isConstQualified())
350 return MLV_ConstQualified;
351 if (TR->isArrayType())
352 return MLV_ArrayType;
353 if (TR->isIncompleteType())
354 return MLV_IncompleteType;
355
356 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
357 if (r->hasConstFields())
358 return MLV_ConstQualified;
359 }
360 return MLV_Valid;
361}
362
Chris Lattner743ec372007-11-27 21:35:27 +0000363/// hasStaticStorage - Return true if this expression has static storage
364/// duration. This means that the address of this expression is a link-time
365/// constant.
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000366bool Expr::hasStaticStorage() const {
367 switch (getStmtClass()) {
368 default:
369 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000370 case ParenExprClass:
371 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
372 case ImplicitCastExprClass:
373 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000374 case DeclRefExprClass: {
375 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
376 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
377 return VD->hasStaticStorage();
378 return false;
379 }
380 case MemberExprClass:
381 const MemberExpr *M = cast<MemberExpr>(this);
382 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000383 case ArraySubscriptExprClass:
384 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000385 }
386}
387
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000388bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000389 switch (getStmtClass()) {
390 default:
391 if (Loc) *Loc = getLocStart();
392 return false;
393 case ParenExprClass:
394 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
395 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000396 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000397 case FloatingLiteralClass:
398 case IntegerLiteralClass:
399 case CharacterLiteralClass:
400 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000401 case TypesCompatibleExprClass:
402 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000403 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000404 case CallExprClass: {
405 const CallExpr *CE = cast<CallExpr>(this);
406 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000407 Result.zextOrTrunc(
408 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000409 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000410 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000411 if (Loc) *Loc = getLocStart();
412 return false;
413 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000414 case DeclRefExprClass: {
415 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
416 // Accept address of function.
417 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000418 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000419 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000420 if (isa<VarDecl>(D))
421 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000422 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000423 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000424 case UnaryOperatorClass: {
425 const UnaryOperator *Exp = cast<UnaryOperator>(this);
426
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000427 // C99 6.6p9
428 if (Exp->getOpcode() == UnaryOperator::AddrOf)
429 return Exp->getSubExpr()->hasStaticStorage();
430
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000431 // Get the operand value. If this is sizeof/alignof, do not evalute the
432 // operand. This affects C99 6.6p3.
433 if (!Exp->isSizeOfAlignOfOp() &&
434 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
435 return false;
436
437 switch (Exp->getOpcode()) {
438 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
439 // See C99 6.6p3.
440 default:
441 if (Loc) *Loc = Exp->getOperatorLoc();
442 return false;
443 case UnaryOperator::Extension:
444 return true; // FIXME: this is wrong.
445 case UnaryOperator::SizeOf:
446 case UnaryOperator::AlignOf:
447 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
448 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
449 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000450 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000451 case UnaryOperator::LNot:
452 case UnaryOperator::Plus:
453 case UnaryOperator::Minus:
454 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000455 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000456 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000457 }
458 case SizeOfAlignOfTypeExprClass: {
459 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
460 // alignof always evaluates to a constant.
461 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
462 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000463 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000464 }
465 case BinaryOperatorClass: {
466 const BinaryOperator *Exp = cast<BinaryOperator>(this);
467
468 // The LHS of a constant expr is always evaluated and needed.
469 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
470 return false;
471
472 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
473 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000474 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000475 }
476 case ImplicitCastExprClass:
477 case CastExprClass: {
478 const Expr *SubExpr;
479 SourceLocation CastLoc;
480 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
481 SubExpr = C->getSubExpr();
482 CastLoc = C->getLParenLoc();
483 } else {
484 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
485 CastLoc = getLocStart();
486 }
487 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
488 if (Loc) *Loc = SubExpr->getLocStart();
489 return false;
490 }
Chris Lattner06db6132007-10-18 00:20:32 +0000491 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000492 }
493 case ConditionalOperatorClass: {
494 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000495 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
496 !Exp->getLHS()->isConstantExpr(Ctx, Loc) ||
497 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000498 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000499 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000500 }
501 }
502
503 return true;
504}
505
Chris Lattner4b009652007-07-25 00:24:17 +0000506/// isIntegerConstantExpr - this recursive routine will test if an expression is
507/// an integer constant expression. Note: With the introduction of VLA's in
508/// C99 the result of the sizeof operator is no longer always a constant
509/// expression. The generalization of the wording to include any subexpression
510/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
511/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
512/// "0 || f()" can be treated as a constant expression. In C90 this expression,
513/// occurring in a context requiring a constant, would have been a constraint
514/// violation. FIXME: This routine currently implements C90 semantics.
515/// To properly implement C99 semantics this routine will need to evaluate
516/// expressions involving operators previously mentioned.
517
518/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
519/// comma, etc
520///
521/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000522/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000523///
524/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
525/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
526/// cast+dereference.
527bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
528 SourceLocation *Loc, bool isEvaluated) const {
529 switch (getStmtClass()) {
530 default:
531 if (Loc) *Loc = getLocStart();
532 return false;
533 case ParenExprClass:
534 return cast<ParenExpr>(this)->getSubExpr()->
535 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
536 case IntegerLiteralClass:
537 Result = cast<IntegerLiteral>(this)->getValue();
538 break;
539 case CharacterLiteralClass: {
540 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000541 Result.zextOrTrunc(
542 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000543 Result = CL->getValue();
544 Result.setIsUnsigned(!getType()->isSignedIntegerType());
545 break;
546 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000547 case TypesCompatibleExprClass: {
548 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000549 Result.zextOrTrunc(
550 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000551 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000552 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000553 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000554 case CallExprClass: {
555 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000556 Result.zextOrTrunc(
557 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000558 if (CE->isBuiltinClassifyType(Result))
559 break;
560 if (Loc) *Loc = getLocStart();
561 return false;
562 }
Chris Lattner4b009652007-07-25 00:24:17 +0000563 case DeclRefExprClass:
564 if (const EnumConstantDecl *D =
565 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
566 Result = D->getInitVal();
567 break;
568 }
569 if (Loc) *Loc = getLocStart();
570 return false;
571 case UnaryOperatorClass: {
572 const UnaryOperator *Exp = cast<UnaryOperator>(this);
573
574 // Get the operand value. If this is sizeof/alignof, do not evalute the
575 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000576 if (!Exp->isSizeOfAlignOfOp() &&
577 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000578 return false;
579
580 switch (Exp->getOpcode()) {
581 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
582 // See C99 6.6p3.
583 default:
584 if (Loc) *Loc = Exp->getOperatorLoc();
585 return false;
586 case UnaryOperator::Extension:
587 return true; // FIXME: this is wrong.
588 case UnaryOperator::SizeOf:
589 case UnaryOperator::AlignOf:
590 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
591 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
592 return false;
593
594 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000595 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000596 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
597 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000598
599 // Get information about the size or align.
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000600 if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner4b009652007-07-25 00:24:17 +0000601 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
602 Exp->getOperatorLoc());
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000603 } else {
604 unsigned CharSize = Ctx.Target.getCharWidth(Exp->getOperatorLoc());
605 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
606 Exp->getOperatorLoc()) / CharSize;
607 }
Chris Lattner4b009652007-07-25 00:24:17 +0000608 break;
609 case UnaryOperator::LNot: {
610 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000611 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000612 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
613 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000614 Result = Val;
615 break;
616 }
617 case UnaryOperator::Plus:
618 break;
619 case UnaryOperator::Minus:
620 Result = -Result;
621 break;
622 case UnaryOperator::Not:
623 Result = ~Result;
624 break;
625 }
626 break;
627 }
628 case SizeOfAlignOfTypeExprClass: {
629 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
630 // alignof always evaluates to a constant.
631 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
632 return false;
633
634 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000635 Result.zextOrTrunc(
636 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000637
638 // Get information about the size or align.
639 if (Exp->isSizeOf())
640 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
641 else
642 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
643 break;
644 }
645 case BinaryOperatorClass: {
646 const BinaryOperator *Exp = cast<BinaryOperator>(this);
647
648 // The LHS of a constant expr is always evaluated and needed.
649 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
650 return false;
651
652 llvm::APSInt RHS(Result);
653
654 // The short-circuiting &&/|| operators don't necessarily evaluate their
655 // RHS. Make sure to pass isEvaluated down correctly.
656 if (Exp->isLogicalOp()) {
657 bool RHSEval;
658 if (Exp->getOpcode() == BinaryOperator::LAnd)
659 RHSEval = Result != 0;
660 else {
661 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
662 RHSEval = Result == 0;
663 }
664
665 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
666 isEvaluated & RHSEval))
667 return false;
668 } else {
669 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
670 return false;
671 }
672
673 switch (Exp->getOpcode()) {
674 default:
675 if (Loc) *Loc = getLocStart();
676 return false;
677 case BinaryOperator::Mul:
678 Result *= RHS;
679 break;
680 case BinaryOperator::Div:
681 if (RHS == 0) {
682 if (!isEvaluated) break;
683 if (Loc) *Loc = getLocStart();
684 return false;
685 }
686 Result /= RHS;
687 break;
688 case BinaryOperator::Rem:
689 if (RHS == 0) {
690 if (!isEvaluated) break;
691 if (Loc) *Loc = getLocStart();
692 return false;
693 }
694 Result %= RHS;
695 break;
696 case BinaryOperator::Add: Result += RHS; break;
697 case BinaryOperator::Sub: Result -= RHS; break;
698 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000699 Result <<=
700 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000701 break;
702 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000703 Result >>=
704 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000705 break;
706 case BinaryOperator::LT: Result = Result < RHS; break;
707 case BinaryOperator::GT: Result = Result > RHS; break;
708 case BinaryOperator::LE: Result = Result <= RHS; break;
709 case BinaryOperator::GE: Result = Result >= RHS; break;
710 case BinaryOperator::EQ: Result = Result == RHS; break;
711 case BinaryOperator::NE: Result = Result != RHS; break;
712 case BinaryOperator::And: Result &= RHS; break;
713 case BinaryOperator::Xor: Result ^= RHS; break;
714 case BinaryOperator::Or: Result |= RHS; break;
715 case BinaryOperator::LAnd:
716 Result = Result != 0 && RHS != 0;
717 break;
718 case BinaryOperator::LOr:
719 Result = Result != 0 || RHS != 0;
720 break;
721
722 case BinaryOperator::Comma:
723 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
724 // *except* when they are contained within a subexpression that is not
725 // evaluated". Note that Assignment can never happen due to constraints
726 // on the LHS subexpr, so we don't need to check it here.
727 if (isEvaluated) {
728 if (Loc) *Loc = getLocStart();
729 return false;
730 }
731
732 // The result of the constant expr is the RHS.
733 Result = RHS;
734 return true;
735 }
736
737 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
738 break;
739 }
740 case ImplicitCastExprClass:
741 case CastExprClass: {
742 const Expr *SubExpr;
743 SourceLocation CastLoc;
744 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
745 SubExpr = C->getSubExpr();
746 CastLoc = C->getLParenLoc();
747 } else {
748 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
749 CastLoc = getLocStart();
750 }
751
752 // C99 6.6p6: shall only convert arithmetic types to integer types.
753 if (!SubExpr->getType()->isArithmeticType() ||
754 !getType()->isIntegerType()) {
755 if (Loc) *Loc = SubExpr->getLocStart();
756 return false;
757 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000758
759 uint32_t DestWidth =
760 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
761
Chris Lattner4b009652007-07-25 00:24:17 +0000762 // Handle simple integer->integer casts.
763 if (SubExpr->getType()->isIntegerType()) {
764 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
765 return false;
766
767 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000768 // If the input is signed, do a sign extend, noop, or truncate.
769 if (SubExpr->getType()->isSignedIntegerType())
770 Result.sextOrTrunc(DestWidth);
771 else // If the input is unsigned, do a zero extend, noop, or truncate.
772 Result.zextOrTrunc(DestWidth);
773 break;
774 }
775
776 // Allow floating constants that are the immediate operands of casts or that
777 // are parenthesized.
778 const Expr *Operand = SubExpr;
779 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
780 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000781
782 // If this isn't a floating literal, we can't handle it.
783 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
784 if (!FL) {
785 if (Loc) *Loc = Operand->getLocStart();
786 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000787 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000788
789 // Determine whether we are converting to unsigned or signed.
790 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000791
792 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
793 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000794 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000795 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
796 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000797 Result = llvm::APInt(DestWidth, 4, Space);
798 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000799 }
800 case ConditionalOperatorClass: {
801 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
802
803 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
804 return false;
805
806 const Expr *TrueExp = Exp->getLHS();
807 const Expr *FalseExp = Exp->getRHS();
808 if (Result == 0) std::swap(TrueExp, FalseExp);
809
810 // Evaluate the false one first, discard the result.
811 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
812 return false;
813 // Evalute the true one, capture the result.
814 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
815 return false;
816 break;
817 }
818 }
819
820 // Cases that are valid constant exprs fall through to here.
821 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
822 return true;
823}
824
825
826/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
827/// integer constant expression with the value zero, or if this is one that is
828/// cast to void*.
829bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
830 // Strip off a cast to void*, if it exists.
831 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
832 // Check that it is a cast to void*.
833 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
834 QualType Pointee = PT->getPointeeType();
835 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
836 CE->getSubExpr()->getType()->isIntegerType()) // from int.
837 return CE->getSubExpr()->isNullPointerConstant(Ctx);
838 }
Steve Naroff1d6b2472007-08-28 21:20:34 +0000839 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff3d052872007-08-29 00:00:02 +0000840 // Ignore the ImplicitCastExpr type entirely.
841 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000842 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
843 // Accept ((void*)0) as a null pointer constant, as many other
844 // implementations do.
845 return PE->getSubExpr()->isNullPointerConstant(Ctx);
846 }
847
848 // This expression must be an integer type.
849 if (!getType()->isIntegerType())
850 return false;
851
852 // If we have an integer constant expression, we need to *evaluate* it and
853 // test for the value 0.
854 llvm::APSInt Val(32);
855 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
856}
Steve Naroffc11705f2007-07-28 23:10:27 +0000857
Chris Lattnera0d03a72007-08-03 17:31:20 +0000858unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000859 return strlen(Accessor.getName());
860}
861
862
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000863/// getComponentType - Determine whether the components of this access are
864/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000865OCUVectorElementExpr::ElementType
866OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000867 // derive the component type, no need to waste space.
868 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000869
Chris Lattner9096b792007-08-02 22:33:49 +0000870 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
871 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000872
Chris Lattner9096b792007-08-02 22:33:49 +0000873 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000874 "getComponentType(): Illegal accessor");
875 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000876}
Steve Naroffba67f692007-07-30 03:29:09 +0000877
Chris Lattnera0d03a72007-08-03 17:31:20 +0000878/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000879/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000880bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000881 const char *compStr = Accessor.getName();
882 unsigned length = strlen(compStr);
883
884 for (unsigned i = 0; i < length-1; i++) {
885 const char *s = compStr+i;
886 for (const char c = *s++; *s; s++)
887 if (c == *s)
888 return true;
889 }
890 return false;
891}
Chris Lattner42158e72007-08-02 23:36:59 +0000892
893/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000894unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000895 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000896 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +0000897
898 unsigned Result = 0;
899
900 while (length--) {
901 Result <<= 2;
902 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
903 assert(Idx != -1 && "Invalid accessor letter");
904 Result |= Idx;
905 }
906 return Result;
907}
908
Steve Naroff4ed9d662007-09-27 14:38:14 +0000909// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +0000910ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000911 QualType retType, ObjcMethodDecl *mproto,
912 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +0000913 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000914 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
915 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +0000916 NumArgs = nargs;
917 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +0000918 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +0000919 if (NumArgs) {
920 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000921 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
922 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000923 LBracloc = LBrac;
924 RBracloc = RBrac;
925}
926
Steve Naroff4ed9d662007-09-27 14:38:14 +0000927// constructor for class messages.
928// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +0000929ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000930 QualType retType, ObjcMethodDecl *mproto,
931 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +0000932 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000933 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
934 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +0000935 NumArgs = nargs;
936 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +0000937 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +0000938 if (NumArgs) {
939 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000940 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
941 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000942 LBracloc = LBrac;
943 RBracloc = RBrac;
944}
945
Chris Lattnerf624cd22007-10-25 00:29:32 +0000946
947bool ChooseExpr::isConditionTrue(ASTContext &C) const {
948 llvm::APSInt CondVal(32);
949 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
950 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
951 return CondVal != 0;
952}
953
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000954//===----------------------------------------------------------------------===//
955// Child Iterators for iterating over subexpressions/substatements
956//===----------------------------------------------------------------------===//
957
958// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000959Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
960Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000961
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000962// ObjCIvarRefExpr
963Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
964Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
965
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000966// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000967Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
968Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000969
970// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000971Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
972Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000973
974// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000975Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
976Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000977
978// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000979Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
980Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000981
Chris Lattner1de66eb2007-08-26 03:42:43 +0000982// ImaginaryLiteral
983Stmt::child_iterator ImaginaryLiteral::child_begin() {
984 return reinterpret_cast<Stmt**>(&Val);
985}
986Stmt::child_iterator ImaginaryLiteral::child_end() {
987 return reinterpret_cast<Stmt**>(&Val)+1;
988}
989
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000990// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000991Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
992Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000993
994// ParenExpr
995Stmt::child_iterator ParenExpr::child_begin() {
996 return reinterpret_cast<Stmt**>(&Val);
997}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000998Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +0000999 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001000}
1001
1002// UnaryOperator
1003Stmt::child_iterator UnaryOperator::child_begin() {
1004 return reinterpret_cast<Stmt**>(&Val);
1005}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001006Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001007 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001008}
1009
1010// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001011Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
1012 return child_iterator();
1013}
1014Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
1015 return child_iterator();
1016}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001017
1018// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001019Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001020 return reinterpret_cast<Stmt**>(&SubExprs);
1021}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001022Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001023 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001024}
1025
1026// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001027Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001028 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001029}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001030Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001031 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001032}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001033
1034// MemberExpr
1035Stmt::child_iterator MemberExpr::child_begin() {
1036 return reinterpret_cast<Stmt**>(&Base);
1037}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001038Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001039 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001040}
1041
1042// OCUVectorElementExpr
1043Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1044 return reinterpret_cast<Stmt**>(&Base);
1045}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001046Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001047 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001048}
1049
1050// CompoundLiteralExpr
1051Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1052 return reinterpret_cast<Stmt**>(&Init);
1053}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001054Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001055 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001056}
1057
1058// ImplicitCastExpr
1059Stmt::child_iterator ImplicitCastExpr::child_begin() {
1060 return reinterpret_cast<Stmt**>(&Op);
1061}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001062Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001063 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001064}
1065
1066// CastExpr
1067Stmt::child_iterator CastExpr::child_begin() {
1068 return reinterpret_cast<Stmt**>(&Op);
1069}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001070Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001071 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001072}
1073
1074// BinaryOperator
1075Stmt::child_iterator BinaryOperator::child_begin() {
1076 return reinterpret_cast<Stmt**>(&SubExprs);
1077}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001078Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001079 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001080}
1081
1082// ConditionalOperator
1083Stmt::child_iterator ConditionalOperator::child_begin() {
1084 return reinterpret_cast<Stmt**>(&SubExprs);
1085}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001086Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001087 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001088}
1089
1090// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001091Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1092Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001093
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001094// StmtExpr
1095Stmt::child_iterator StmtExpr::child_begin() {
1096 return reinterpret_cast<Stmt**>(&SubStmt);
1097}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001098Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001099 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001100}
1101
1102// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001103Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1104 return child_iterator();
1105}
1106
1107Stmt::child_iterator TypesCompatibleExpr::child_end() {
1108 return child_iterator();
1109}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001110
1111// ChooseExpr
1112Stmt::child_iterator ChooseExpr::child_begin() {
1113 return reinterpret_cast<Stmt**>(&SubExprs);
1114}
1115
1116Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001117 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001118}
1119
Anders Carlsson36760332007-10-15 20:28:48 +00001120// VAArgExpr
1121Stmt::child_iterator VAArgExpr::child_begin() {
1122 return reinterpret_cast<Stmt**>(&Val);
1123}
1124
1125Stmt::child_iterator VAArgExpr::child_end() {
1126 return reinterpret_cast<Stmt**>(&Val)+1;
1127}
1128
Anders Carlsson762b7c72007-08-31 04:56:16 +00001129// InitListExpr
1130Stmt::child_iterator InitListExpr::child_begin() {
1131 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1132}
1133Stmt::child_iterator InitListExpr::child_end() {
1134 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1135}
1136
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001137// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001138Stmt::child_iterator ObjCStringLiteral::child_begin() {
1139 return child_iterator();
1140}
1141Stmt::child_iterator ObjCStringLiteral::child_end() {
1142 return child_iterator();
1143}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001144
1145// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001146Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1147Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001148
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001149// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001150Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1151 return child_iterator();
1152}
1153Stmt::child_iterator ObjCSelectorExpr::child_end() {
1154 return child_iterator();
1155}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001156
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001157// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001158Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1159 return child_iterator();
1160}
1161Stmt::child_iterator ObjCProtocolExpr::child_end() {
1162 return child_iterator();
1163}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001164
Steve Naroffc39ca262007-09-18 23:55:05 +00001165// ObjCMessageExpr
1166Stmt::child_iterator ObjCMessageExpr::child_begin() {
1167 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1168}
1169Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001170 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001171}
1172