blob: 27dfa78273bafebeef1152b78b1c937ded47d136 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
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
Nate Begeman9f3bfb72008-01-17 17:46:27 +000081
Chris Lattner4b009652007-07-25 00:24:17 +000082CallExpr::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
Chris Lattnerc257c0d2007-12-28 05:25:02 +000092/// setNumArgs - This changes the number of arguments present in this call.
93/// Any orphaned expressions are deleted by this, and any new operands are set
94/// to null.
95void CallExpr::setNumArgs(unsigned NumArgs) {
96 // No change, just return.
97 if (NumArgs == getNumArgs()) return;
98
99 // If shrinking # arguments, just delete the extras and forgot them.
100 if (NumArgs < getNumArgs()) {
101 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
102 delete getArg(i);
103 this->NumArgs = NumArgs;
104 return;
105 }
106
107 // Otherwise, we are growing the # arguments. New an bigger argument array.
108 Expr **NewSubExprs = new Expr*[NumArgs+1];
109 // Copy over args.
110 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
111 NewSubExprs[i] = SubExprs[i];
112 // Null out new args.
113 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
114 NewSubExprs[i] = 0;
115
116 delete[] SubExprs;
117 SubExprs = NewSubExprs;
118 this->NumArgs = NumArgs;
119}
120
Steve Naroff44aec4c2008-01-31 01:07:12 +0000121bool CallExpr::isBuiltinConstantExpr() const {
122 // All simple function calls (e.g. func()) are implicitly cast to pointer to
123 // function. As a result, we try and obtain the DeclRefExpr from the
124 // ImplicitCastExpr.
125 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
126 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
127 return false;
128
129 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
130 if (!DRE)
131 return false;
132
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000133 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
134 if (!FDecl)
135 return false;
136
137 unsigned builtinID = FDecl->getIdentifier()->getBuiltinID();
138 if (!builtinID)
139 return false;
140
141 // We have a builtin that is a constant expression
142 if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString)
Steve Naroff44aec4c2008-01-31 01:07:12 +0000143 return true;
144 return false;
145}
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000146
Steve Naroff8d3b1702007-08-08 22:15:55 +0000147bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
148 // The following enum mimics gcc's internal "typeclass.h" file.
149 enum gcc_type_class {
150 no_type_class = -1,
151 void_type_class, integer_type_class, char_type_class,
152 enumeral_type_class, boolean_type_class,
153 pointer_type_class, reference_type_class, offset_type_class,
154 real_type_class, complex_type_class,
155 function_type_class, method_type_class,
156 record_type_class, union_type_class,
157 array_type_class, string_type_class,
158 lang_type_class
159 };
160 Result.setIsSigned(true);
161
162 // All simple function calls (e.g. func()) are implicitly cast to pointer to
163 // function. As a result, we try and obtain the DeclRefExpr from the
164 // ImplicitCastExpr.
165 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
166 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
167 return false;
168 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
169 if (!DRE)
170 return false;
171
172 // We have a DeclRefExpr.
173 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
174 // If no argument was supplied, default to "no_type_class". This isn't
175 // ideal, however it's what gcc does.
176 Result = static_cast<uint64_t>(no_type_class);
177 if (NumArgs >= 1) {
178 QualType argType = getArg(0)->getType();
179
180 if (argType->isVoidType())
181 Result = void_type_class;
182 else if (argType->isEnumeralType())
183 Result = enumeral_type_class;
184 else if (argType->isBooleanType())
185 Result = boolean_type_class;
186 else if (argType->isCharType())
187 Result = string_type_class; // gcc doesn't appear to use char_type_class
188 else if (argType->isIntegerType())
189 Result = integer_type_class;
190 else if (argType->isPointerType())
191 Result = pointer_type_class;
192 else if (argType->isReferenceType())
193 Result = reference_type_class;
194 else if (argType->isRealType())
195 Result = real_type_class;
196 else if (argType->isComplexType())
197 Result = complex_type_class;
198 else if (argType->isFunctionType())
199 Result = function_type_class;
200 else if (argType->isStructureType())
201 Result = record_type_class;
202 else if (argType->isUnionType())
203 Result = union_type_class;
204 else if (argType->isArrayType())
205 Result = array_type_class;
206 else if (argType->isUnionType())
207 Result = union_type_class;
208 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner19b8f1a2007-11-08 17:56:40 +0000209 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff8d3b1702007-08-08 22:15:55 +0000210 }
211 return true;
212 }
213 return false;
214}
215
Chris Lattner4b009652007-07-25 00:24:17 +0000216/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
217/// corresponds to, e.g. "<<=".
218const char *BinaryOperator::getOpcodeStr(Opcode Op) {
219 switch (Op) {
220 default: assert(0 && "Unknown binary operator");
221 case Mul: return "*";
222 case Div: return "/";
223 case Rem: return "%";
224 case Add: return "+";
225 case Sub: return "-";
226 case Shl: return "<<";
227 case Shr: return ">>";
228 case LT: return "<";
229 case GT: return ">";
230 case LE: return "<=";
231 case GE: return ">=";
232 case EQ: return "==";
233 case NE: return "!=";
234 case And: return "&";
235 case Xor: return "^";
236 case Or: return "|";
237 case LAnd: return "&&";
238 case LOr: return "||";
239 case Assign: return "=";
240 case MulAssign: return "*=";
241 case DivAssign: return "/=";
242 case RemAssign: return "%=";
243 case AddAssign: return "+=";
244 case SubAssign: return "-=";
245 case ShlAssign: return "<<=";
246 case ShrAssign: return ">>=";
247 case AndAssign: return "&=";
248 case XorAssign: return "^=";
249 case OrAssign: return "|=";
250 case Comma: return ",";
251 }
252}
253
Anders Carlsson762b7c72007-08-31 04:56:16 +0000254InitListExpr::InitListExpr(SourceLocation lbraceloc,
255 Expr **initexprs, unsigned numinits,
256 SourceLocation rbraceloc)
257 : Expr(InitListExprClass, QualType())
258 , NumInits(numinits)
259 , LBraceLoc(lbraceloc)
260 , RBraceLoc(rbraceloc)
261{
262 InitExprs = new Expr*[numinits];
263 for (unsigned i = 0; i != numinits; i++)
264 InitExprs[i] = initexprs[i];
265}
Chris Lattner4b009652007-07-25 00:24:17 +0000266
267//===----------------------------------------------------------------------===//
268// Generic Expression Routines
269//===----------------------------------------------------------------------===//
270
271/// hasLocalSideEffect - Return true if this immediate expression has side
272/// effects, not counting any sub-expressions.
273bool Expr::hasLocalSideEffect() const {
274 switch (getStmtClass()) {
275 default:
276 return false;
277 case ParenExprClass:
278 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
279 case UnaryOperatorClass: {
280 const UnaryOperator *UO = cast<UnaryOperator>(this);
281
282 switch (UO->getOpcode()) {
283 default: return false;
284 case UnaryOperator::PostInc:
285 case UnaryOperator::PostDec:
286 case UnaryOperator::PreInc:
287 case UnaryOperator::PreDec:
288 return true; // ++/--
289
290 case UnaryOperator::Deref:
291 // Dereferencing a volatile pointer is a side-effect.
292 return getType().isVolatileQualified();
293 case UnaryOperator::Real:
294 case UnaryOperator::Imag:
295 // accessing a piece of a volatile complex is a side-effect.
296 return UO->getSubExpr()->getType().isVolatileQualified();
297
298 case UnaryOperator::Extension:
299 return UO->getSubExpr()->hasLocalSideEffect();
300 }
301 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000302 case BinaryOperatorClass: {
303 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
304 // Consider comma to have side effects if the LHS and RHS both do.
305 if (BinOp->getOpcode() == BinaryOperator::Comma)
306 return BinOp->getLHS()->hasLocalSideEffect() &&
307 BinOp->getRHS()->hasLocalSideEffect();
308
309 return BinOp->isAssignmentOp();
310 }
Chris Lattner06078d22007-08-25 02:00:02 +0000311 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000312 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000313
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000314 case ConditionalOperatorClass: {
315 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
316 return Exp->getCond()->hasLocalSideEffect()
317 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
318 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
319 }
320
Chris Lattner4b009652007-07-25 00:24:17 +0000321 case MemberExprClass:
322 case ArraySubscriptExprClass:
323 // If the base pointer or element is to a volatile pointer/field, accessing
324 // if is a side effect.
325 return getType().isVolatileQualified();
326
327 case CallExprClass:
328 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
329 // should warn.
330 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000331 case ObjCMessageExprClass:
332 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000333
334 case CastExprClass:
335 // If this is a cast to void, check the operand. Otherwise, the result of
336 // the cast is unused.
337 if (getType()->isVoidType())
338 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
339 return false;
340 }
341}
342
343/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
344/// incomplete type other than void. Nonarray expressions that can be lvalues:
345/// - name, where name must be a variable
346/// - e[i]
347/// - (e), where e must be an lvalue
348/// - e.name, where e must be an lvalue
349/// - e->name
350/// - *e, the type of e cannot be a function type
351/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000352/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000353/// - reference type [C++ [expr]]
354///
355Expr::isLvalueResult Expr::isLvalue() const {
356 // first, check the type (C99 6.3.2.1)
357 if (TR->isFunctionType()) // from isObjectType()
358 return LV_NotObjectType;
359
Steve Naroffec7736d2008-02-10 01:39:04 +0000360 // Allow qualified void which is an incomplete type other than void (yuck).
Steve Naroff74a77812008-02-18 15:14:59 +0000361 if (TR->isVoidType() && !TR.getCanonicalType().getQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000362 return LV_IncompleteVoidType;
363
Chris Lattner4b009652007-07-25 00:24:17 +0000364 if (TR->isReferenceType()) // C++ [expr]
365 return LV_Valid;
366
367 // the type looks fine, now check the expression
368 switch (getStmtClass()) {
369 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000370 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000371 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
372 // For vectors, make sure base is an lvalue (i.e. not a function call).
373 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
374 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
375 return LV_Valid;
376 case DeclRefExprClass: // C99 6.5.1p2
377 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
378 return LV_Valid;
379 break;
380 case MemberExprClass: { // C99 6.5.2.3p4
381 const MemberExpr *m = cast<MemberExpr>(this);
382 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
383 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000384 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000385 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000386 return LV_Valid; // C99 6.5.3p4
387
388 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
389 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
390 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000391 break;
392 case ParenExprClass: // C99 6.5.1p5
393 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffc7c66532007-12-05 04:00:10 +0000394 case CompoundLiteralExprClass: // C99 6.5.2.5p5
395 return LV_Valid;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000396 case OCUVectorElementExprClass:
397 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000398 return LV_DuplicateVectorComponents;
399 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000400 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
401 return LV_Valid;
Chris Lattner7e637512008-01-12 08:14:25 +0000402 case PreDefinedExprClass:
403 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000404 default:
405 break;
406 }
407 return LV_InvalidExpression;
408}
409
410/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
411/// does not have an incomplete type, does not have a const-qualified type, and
412/// if it is a structure or union, does not have any member (including,
413/// recursively, any member or element of all contained aggregates or unions)
414/// with a const-qualified type.
415Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
416 isLvalueResult lvalResult = isLvalue();
417
418 switch (lvalResult) {
419 case LV_Valid: break;
420 case LV_NotObjectType: return MLV_NotObjectType;
421 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000422 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000423 case LV_InvalidExpression: return MLV_InvalidExpression;
424 }
425 if (TR.isConstQualified())
426 return MLV_ConstQualified;
427 if (TR->isArrayType())
428 return MLV_ArrayType;
429 if (TR->isIncompleteType())
430 return MLV_IncompleteType;
431
432 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
433 if (r->hasConstFields())
434 return MLV_ConstQualified;
435 }
436 return MLV_Valid;
437}
438
Chris Lattner743ec372007-11-27 21:35:27 +0000439/// hasStaticStorage - Return true if this expression has static storage
440/// duration. This means that the address of this expression is a link-time
441/// constant.
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000442bool Expr::hasStaticStorage() const {
443 switch (getStmtClass()) {
444 default:
445 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000446 case ParenExprClass:
447 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
448 case ImplicitCastExprClass:
449 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000450 case CompoundLiteralExprClass:
451 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000452 case DeclRefExprClass: {
453 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
454 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
455 return VD->hasStaticStorage();
456 return false;
457 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000458 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000459 const MemberExpr *M = cast<MemberExpr>(this);
460 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000461 }
Chris Lattner743ec372007-11-27 21:35:27 +0000462 case ArraySubscriptExprClass:
463 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattner7e637512008-01-12 08:14:25 +0000464 case PreDefinedExprClass:
465 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000466 }
467}
468
Ted Kremenek87e30c52008-01-17 16:57:34 +0000469Expr* Expr::IgnoreParens() {
470 Expr* E = this;
471 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
472 E = P->getSubExpr();
473
474 return E;
475}
476
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000477/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
478/// or CastExprs or ImplicitCastExprs, returning their operand.
479Expr *Expr::IgnoreParenCasts() {
480 Expr *E = this;
481 while (true) {
482 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
483 E = P->getSubExpr();
484 else if (CastExpr *P = dyn_cast<CastExpr>(E))
485 E = P->getSubExpr();
486 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
487 E = P->getSubExpr();
488 else
489 return E;
490 }
491}
492
493
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000494bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000495 switch (getStmtClass()) {
496 default:
497 if (Loc) *Loc = getLocStart();
498 return false;
499 case ParenExprClass:
500 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
501 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000502 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000503 case FloatingLiteralClass:
504 case IntegerLiteralClass:
505 case CharacterLiteralClass:
506 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000507 case TypesCompatibleExprClass:
508 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000509 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000510 case CallExprClass: {
511 const CallExpr *CE = cast<CallExpr>(this);
512 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000513 Result.zextOrTrunc(
514 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000515 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000516 return true;
Steve Naroff44aec4c2008-01-31 01:07:12 +0000517 if (CE->isBuiltinConstantExpr())
518 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000519 if (Loc) *Loc = getLocStart();
520 return false;
521 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000522 case DeclRefExprClass: {
523 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
524 // Accept address of function.
525 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000526 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000527 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000528 if (isa<VarDecl>(D))
529 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000530 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000531 }
Steve Narofff91f9722008-01-09 00:05:37 +0000532 case CompoundLiteralExprClass:
533 if (Loc) *Loc = getLocStart();
534 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemanc4e28e42008-01-25 05:34:48 +0000535 // Allow "(vector type){2,4}" since the elements are all constant.
536 return TR->isArrayType() || TR->isVectorType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000537 case UnaryOperatorClass: {
538 const UnaryOperator *Exp = cast<UnaryOperator>(this);
539
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000540 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000541 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
542 if (!Exp->getSubExpr()->hasStaticStorage()) {
543 if (Loc) *Loc = getLocStart();
544 return false;
545 }
546 return true;
547 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000548
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000549 // Get the operand value. If this is sizeof/alignof, do not evalute the
550 // operand. This affects C99 6.6p3.
Steve Narofff0b23542008-01-10 22:15:12 +0000551 if (!Exp->isSizeOfAlignOfOp() &&
552 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000553 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
554 return false;
555
556 switch (Exp->getOpcode()) {
557 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
558 // See C99 6.6p3.
559 default:
560 if (Loc) *Loc = Exp->getOperatorLoc();
561 return false;
562 case UnaryOperator::Extension:
563 return true; // FIXME: this is wrong.
564 case UnaryOperator::SizeOf:
565 case UnaryOperator::AlignOf:
Steve Narofff0b23542008-01-10 22:15:12 +0000566 case UnaryOperator::OffsetOf:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000567 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000568 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000569 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000570 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000571 }
Chris Lattner06db6132007-10-18 00:20:32 +0000572 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000573 case UnaryOperator::LNot:
574 case UnaryOperator::Plus:
575 case UnaryOperator::Minus:
576 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000577 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000578 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000579 }
580 case SizeOfAlignOfTypeExprClass: {
581 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
582 // alignof always evaluates to a constant.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000583 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000584 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000585 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000586 }
Chris Lattner06db6132007-10-18 00:20:32 +0000587 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000588 }
589 case BinaryOperatorClass: {
590 const BinaryOperator *Exp = cast<BinaryOperator>(this);
591
592 // The LHS of a constant expr is always evaluated and needed.
593 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
594 return false;
595
596 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
597 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000598 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000599 }
600 case ImplicitCastExprClass:
601 case CastExprClass: {
602 const Expr *SubExpr;
603 SourceLocation CastLoc;
604 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
605 SubExpr = C->getSubExpr();
606 CastLoc = C->getLParenLoc();
607 } else {
608 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
609 CastLoc = getLocStart();
610 }
611 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
612 if (Loc) *Loc = SubExpr->getLocStart();
613 return false;
614 }
Chris Lattner06db6132007-10-18 00:20:32 +0000615 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000616 }
617 case ConditionalOperatorClass: {
618 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000619 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000620 // Handle the GNU extension for missing LHS.
621 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000622 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000623 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000624 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000625 }
Steve Narofff0b23542008-01-10 22:15:12 +0000626 case InitListExprClass: {
627 const InitListExpr *Exp = cast<InitListExpr>(this);
628 unsigned numInits = Exp->getNumInits();
629 for (unsigned i = 0; i < numInits; i++) {
630 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
631 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
632 return false;
633 }
634 }
635 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000636 }
Steve Narofff0b23542008-01-10 22:15:12 +0000637 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000638}
639
Chris Lattner4b009652007-07-25 00:24:17 +0000640/// isIntegerConstantExpr - this recursive routine will test if an expression is
641/// an integer constant expression. Note: With the introduction of VLA's in
642/// C99 the result of the sizeof operator is no longer always a constant
643/// expression. The generalization of the wording to include any subexpression
644/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
645/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
646/// "0 || f()" can be treated as a constant expression. In C90 this expression,
647/// occurring in a context requiring a constant, would have been a constraint
648/// violation. FIXME: This routine currently implements C90 semantics.
649/// To properly implement C99 semantics this routine will need to evaluate
650/// expressions involving operators previously mentioned.
651
652/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
653/// comma, etc
654///
655/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000656/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000657///
658/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
659/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
660/// cast+dereference.
661bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
662 SourceLocation *Loc, bool isEvaluated) const {
663 switch (getStmtClass()) {
664 default:
665 if (Loc) *Loc = getLocStart();
666 return false;
667 case ParenExprClass:
668 return cast<ParenExpr>(this)->getSubExpr()->
669 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
670 case IntegerLiteralClass:
671 Result = cast<IntegerLiteral>(this)->getValue();
672 break;
673 case CharacterLiteralClass: {
674 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000675 Result.zextOrTrunc(
676 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000677 Result = CL->getValue();
678 Result.setIsUnsigned(!getType()->isSignedIntegerType());
679 break;
680 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000681 case TypesCompatibleExprClass: {
682 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000683 Result.zextOrTrunc(
684 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000685 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000686 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000687 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000688 case CallExprClass: {
689 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000690 Result.zextOrTrunc(
691 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000692 if (CE->isBuiltinClassifyType(Result))
693 break;
694 if (Loc) *Loc = getLocStart();
695 return false;
696 }
Chris Lattner4b009652007-07-25 00:24:17 +0000697 case DeclRefExprClass:
698 if (const EnumConstantDecl *D =
699 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
700 Result = D->getInitVal();
701 break;
702 }
703 if (Loc) *Loc = getLocStart();
704 return false;
705 case UnaryOperatorClass: {
706 const UnaryOperator *Exp = cast<UnaryOperator>(this);
707
708 // Get the operand value. If this is sizeof/alignof, do not evalute the
709 // operand. This affects C99 6.6p3.
Anders Carlsson52774ad2008-01-29 15:56:48 +0000710 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner5a9b6242007-08-23 21:42:50 +0000711 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000712 return false;
713
714 switch (Exp->getOpcode()) {
715 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
716 // See C99 6.6p3.
717 default:
718 if (Loc) *Loc = Exp->getOperatorLoc();
719 return false;
720 case UnaryOperator::Extension:
721 return true; // FIXME: this is wrong.
722 case UnaryOperator::SizeOf:
723 case UnaryOperator::AlignOf:
724 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000725 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000726 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000727 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000728 }
Chris Lattner4b009652007-07-25 00:24:17 +0000729
730 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000731 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000732 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
733 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000734
735 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000736 if (Exp->getSubExpr()->getType()->isFunctionType()) {
737 // GCC extension: sizeof(function) = 1.
738 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000739 } else {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000740 unsigned CharSize =
741 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
742
Anders Carlsson1f86b032008-02-18 07:10:45 +0000743 if (Exp->getOpcode() == UnaryOperator::AlignOf)
744 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
745 Exp->getOperatorLoc()) / CharSize;
746 else
747 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
748 Exp->getOperatorLoc()) / CharSize;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000749 }
Chris Lattner4b009652007-07-25 00:24:17 +0000750 break;
751 case UnaryOperator::LNot: {
Chris Lattnerf00fdc02008-01-25 19:16:19 +0000752 bool Val = Result == 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000753 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000754 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
755 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000756 Result = Val;
757 break;
758 }
759 case UnaryOperator::Plus:
760 break;
761 case UnaryOperator::Minus:
762 Result = -Result;
763 break;
764 case UnaryOperator::Not:
765 Result = ~Result;
766 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000767 case UnaryOperator::OffsetOf:
768 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000769 }
770 break;
771 }
772 case SizeOfAlignOfTypeExprClass: {
773 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
774 // alignof always evaluates to a constant.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000775 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000776 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000777 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000778 }
Chris Lattner4b009652007-07-25 00:24:17 +0000779
780 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000781 Result.zextOrTrunc(
782 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000783
784 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000785 if (Exp->getArgumentType()->isFunctionType()) {
786 // GCC extension: sizeof(function) = 1.
787 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000788 } else {
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000789 unsigned CharSize =
790 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
791
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000792 if (Exp->isSizeOf())
793 Result = Ctx.getTypeSize(Exp->getArgumentType(),
794 Exp->getOperatorLoc()) / CharSize;
795 else
796 Result = Ctx.getTypeAlign(Exp->getArgumentType(),
797 Exp->getOperatorLoc()) / CharSize;
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000798 }
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000799
Chris Lattner4b009652007-07-25 00:24:17 +0000800 break;
801 }
802 case BinaryOperatorClass: {
803 const BinaryOperator *Exp = cast<BinaryOperator>(this);
804
805 // The LHS of a constant expr is always evaluated and needed.
806 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
807 return false;
808
809 llvm::APSInt RHS(Result);
810
811 // The short-circuiting &&/|| operators don't necessarily evaluate their
812 // RHS. Make sure to pass isEvaluated down correctly.
813 if (Exp->isLogicalOp()) {
814 bool RHSEval;
815 if (Exp->getOpcode() == BinaryOperator::LAnd)
816 RHSEval = Result != 0;
817 else {
818 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
819 RHSEval = Result == 0;
820 }
821
822 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
823 isEvaluated & RHSEval))
824 return false;
825 } else {
826 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
827 return false;
828 }
829
830 switch (Exp->getOpcode()) {
831 default:
832 if (Loc) *Loc = getLocStart();
833 return false;
834 case BinaryOperator::Mul:
835 Result *= RHS;
836 break;
837 case BinaryOperator::Div:
838 if (RHS == 0) {
839 if (!isEvaluated) break;
840 if (Loc) *Loc = getLocStart();
841 return false;
842 }
843 Result /= RHS;
844 break;
845 case BinaryOperator::Rem:
846 if (RHS == 0) {
847 if (!isEvaluated) break;
848 if (Loc) *Loc = getLocStart();
849 return false;
850 }
851 Result %= RHS;
852 break;
853 case BinaryOperator::Add: Result += RHS; break;
854 case BinaryOperator::Sub: Result -= RHS; break;
855 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000856 Result <<=
857 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000858 break;
859 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000860 Result >>=
861 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000862 break;
863 case BinaryOperator::LT: Result = Result < RHS; break;
864 case BinaryOperator::GT: Result = Result > RHS; break;
865 case BinaryOperator::LE: Result = Result <= RHS; break;
866 case BinaryOperator::GE: Result = Result >= RHS; break;
867 case BinaryOperator::EQ: Result = Result == RHS; break;
868 case BinaryOperator::NE: Result = Result != RHS; break;
869 case BinaryOperator::And: Result &= RHS; break;
870 case BinaryOperator::Xor: Result ^= RHS; break;
871 case BinaryOperator::Or: Result |= RHS; break;
872 case BinaryOperator::LAnd:
873 Result = Result != 0 && RHS != 0;
874 break;
875 case BinaryOperator::LOr:
876 Result = Result != 0 || RHS != 0;
877 break;
878
879 case BinaryOperator::Comma:
880 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
881 // *except* when they are contained within a subexpression that is not
882 // evaluated". Note that Assignment can never happen due to constraints
883 // on the LHS subexpr, so we don't need to check it here.
884 if (isEvaluated) {
885 if (Loc) *Loc = getLocStart();
886 return false;
887 }
888
889 // The result of the constant expr is the RHS.
890 Result = RHS;
891 return true;
892 }
893
894 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
895 break;
896 }
897 case ImplicitCastExprClass:
898 case CastExprClass: {
899 const Expr *SubExpr;
900 SourceLocation CastLoc;
901 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
902 SubExpr = C->getSubExpr();
903 CastLoc = C->getLParenLoc();
904 } else {
905 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
906 CastLoc = getLocStart();
907 }
908
909 // C99 6.6p6: shall only convert arithmetic types to integer types.
910 if (!SubExpr->getType()->isArithmeticType() ||
911 !getType()->isIntegerType()) {
912 if (Loc) *Loc = SubExpr->getLocStart();
913 return false;
914 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000915
916 uint32_t DestWidth =
917 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
918
Chris Lattner4b009652007-07-25 00:24:17 +0000919 // Handle simple integer->integer casts.
920 if (SubExpr->getType()->isIntegerType()) {
921 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
922 return false;
923
924 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000925 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +0000926 if (getType()->isBooleanType()) {
927 // Conversion to bool compares against zero.
928 Result = Result != 0;
929 Result.zextOrTrunc(DestWidth);
930 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +0000931 Result.sextOrTrunc(DestWidth);
932 else // If the input is unsigned, do a zero extend, noop, or truncate.
933 Result.zextOrTrunc(DestWidth);
934 break;
935 }
936
937 // Allow floating constants that are the immediate operands of casts or that
938 // are parenthesized.
939 const Expr *Operand = SubExpr;
940 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
941 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000942
943 // If this isn't a floating literal, we can't handle it.
944 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
945 if (!FL) {
946 if (Loc) *Loc = Operand->getLocStart();
947 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000948 }
Chris Lattner000c4102008-01-09 18:59:34 +0000949
950 // If the destination is boolean, compare against zero.
951 if (getType()->isBooleanType()) {
952 Result = !FL->getValue().isZero();
953 Result.zextOrTrunc(DestWidth);
954 break;
955 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000956
957 // Determine whether we are converting to unsigned or signed.
958 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000959
960 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
961 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000962 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000963 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
964 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000965 Result = llvm::APInt(DestWidth, 4, Space);
966 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000967 }
968 case ConditionalOperatorClass: {
969 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
970
971 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
972 return false;
973
974 const Expr *TrueExp = Exp->getLHS();
975 const Expr *FalseExp = Exp->getRHS();
976 if (Result == 0) std::swap(TrueExp, FalseExp);
977
978 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000979 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000980 return false;
981 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000982 if (TrueExp &&
983 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000984 return false;
985 break;
986 }
987 }
988
989 // Cases that are valid constant exprs fall through to here.
990 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
991 return true;
992}
993
Chris Lattner4b009652007-07-25 00:24:17 +0000994/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
995/// integer constant expression with the value zero, or if this is one that is
996/// cast to void*.
997bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffa2e53222008-01-14 16:10:57 +0000998 // Strip off a cast to void*, if it exists.
999 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
1000 // Check that it is a cast to void*.
Eli Friedmand899dbe2008-02-13 17:29:58 +00001001 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001002 QualType Pointee = PT->getPointeeType();
Steve Naroffa2e53222008-01-14 16:10:57 +00001003 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
1004 CE->getSubExpr()->getType()->isIntegerType()) // from int.
1005 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001006 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001007 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1008 // Ignore the ImplicitCastExpr type entirely.
1009 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
1010 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1011 // Accept ((void*)0) as a null pointer constant, as many other
1012 // implementations do.
1013 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Narofff33a9852008-01-14 02:53:34 +00001014 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001015
1016 // This expression must be an integer type.
1017 if (!getType()->isIntegerType())
1018 return false;
1019
Chris Lattner4b009652007-07-25 00:24:17 +00001020 // If we have an integer constant expression, we need to *evaluate* it and
1021 // test for the value 0.
1022 llvm::APSInt Val(32);
Steve Naroffa2e53222008-01-14 16:10:57 +00001023 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001024}
Steve Naroffc11705f2007-07-28 23:10:27 +00001025
Chris Lattnera0d03a72007-08-03 17:31:20 +00001026unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +00001027 return strlen(Accessor.getName());
1028}
1029
1030
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001031/// getComponentType - Determine whether the components of this access are
1032/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001033OCUVectorElementExpr::ElementType
1034OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +00001035 // derive the component type, no need to waste space.
1036 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +00001037
Chris Lattner9096b792007-08-02 22:33:49 +00001038 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
1039 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +00001040
Chris Lattner9096b792007-08-02 22:33:49 +00001041 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +00001042 "getComponentType(): Illegal accessor");
1043 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +00001044}
Steve Naroffba67f692007-07-30 03:29:09 +00001045
Chris Lattnera0d03a72007-08-03 17:31:20 +00001046/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001047/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001048bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001049 const char *compStr = Accessor.getName();
1050 unsigned length = strlen(compStr);
1051
1052 for (unsigned i = 0; i < length-1; i++) {
1053 const char *s = compStr+i;
1054 for (const char c = *s++; *s; s++)
1055 if (c == *s)
1056 return true;
1057 }
1058 return false;
1059}
Chris Lattner42158e72007-08-02 23:36:59 +00001060
1061/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001062unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +00001063 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +00001064 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +00001065
1066 unsigned Result = 0;
1067
1068 while (length--) {
1069 Result <<= 2;
1070 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1071 assert(Idx != -1 && "Invalid accessor letter");
1072 Result |= Idx;
1073 }
1074 return Result;
1075}
1076
Steve Naroff4ed9d662007-09-27 14:38:14 +00001077// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001078ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001079 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001080 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001081 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001082 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1083 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001084 NumArgs = nargs;
1085 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001086 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001087 if (NumArgs) {
1088 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001089 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1090 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001091 LBracloc = LBrac;
1092 RBracloc = RBrac;
1093}
1094
Steve Naroff4ed9d662007-09-27 14:38:14 +00001095// constructor for class messages.
1096// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001097ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001098 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001099 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001100 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001101 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1102 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001103 NumArgs = nargs;
1104 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +00001105 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +00001106 if (NumArgs) {
1107 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001108 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1109 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001110 LBracloc = LBrac;
1111 RBracloc = RBrac;
1112}
1113
Chris Lattnerf624cd22007-10-25 00:29:32 +00001114
1115bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1116 llvm::APSInt CondVal(32);
1117 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1118 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1119 return CondVal != 0;
1120}
1121
Anders Carlsson52774ad2008-01-29 15:56:48 +00001122static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1123{
1124 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1125 QualType Ty = ME->getBase()->getType();
1126
1127 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
1128 const ASTRecordLayout &RL = C.getASTRecordLayout(RD, SourceLocation());
1129 FieldDecl *FD = ME->getMemberDecl();
1130
1131 // FIXME: This is linear time.
1132 unsigned i = 0, e = 0;
1133 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1134 if (RD->getMember(i) == FD)
1135 break;
1136 }
1137
1138 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1139 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1140 const Expr *Base = ASE->getBase();
1141 llvm::APSInt Idx(32);
1142 bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1143 assert(ICE && "Array index is not a constant integer!");
1144
1145 int64_t size = C.getTypeSize(ASE->getType(), SourceLocation());
1146 size *= Idx.getSExtValue();
1147
1148 return size + evaluateOffsetOf(C, Base);
1149 } else if (isa<CompoundLiteralExpr>(E))
1150 return 0;
1151
1152 assert(0 && "Unknown offsetof subexpression!");
1153 return 0;
1154}
1155
1156int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1157{
1158 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1159
1160 unsigned CharSize =
1161 C.Target.getCharWidth(C.getFullLoc(getOperatorLoc()));
1162
1163 return ::evaluateOffsetOf(C, Val) / CharSize;
1164}
1165
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001166//===----------------------------------------------------------------------===//
1167// Child Iterators for iterating over subexpressions/substatements
1168//===----------------------------------------------------------------------===//
1169
1170// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001171Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1172Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001173
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001174// ObjCIvarRefExpr
1175Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1176Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1177
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001178// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001179Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1180Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001181
1182// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001183Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1184Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001185
1186// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001187Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1188Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001189
1190// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001191Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1192Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001193
Chris Lattner1de66eb2007-08-26 03:42:43 +00001194// ImaginaryLiteral
1195Stmt::child_iterator ImaginaryLiteral::child_begin() {
1196 return reinterpret_cast<Stmt**>(&Val);
1197}
1198Stmt::child_iterator ImaginaryLiteral::child_end() {
1199 return reinterpret_cast<Stmt**>(&Val)+1;
1200}
1201
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001202// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001203Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1204Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001205
1206// ParenExpr
1207Stmt::child_iterator ParenExpr::child_begin() {
1208 return reinterpret_cast<Stmt**>(&Val);
1209}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001210Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001211 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001212}
1213
1214// UnaryOperator
1215Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001216 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001217}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001218Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001219 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001220}
1221
1222// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001223Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001224 // If the type is a VLA type (and not a typedef), the size expression of the
1225 // VLA needs to be treated as an executable expression.
1226 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1227 return child_iterator(T);
1228 else
1229 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001230}
1231Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001232 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001233}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001234
1235// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001236Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001237 return reinterpret_cast<Stmt**>(&SubExprs);
1238}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001239Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001240 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001241}
1242
1243// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001244Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001245 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001246}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001247Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001248 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001249}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001250
1251// MemberExpr
1252Stmt::child_iterator MemberExpr::child_begin() {
1253 return reinterpret_cast<Stmt**>(&Base);
1254}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001255Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001256 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001257}
1258
1259// OCUVectorElementExpr
1260Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1261 return reinterpret_cast<Stmt**>(&Base);
1262}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001263Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001264 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001265}
1266
1267// CompoundLiteralExpr
1268Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1269 return reinterpret_cast<Stmt**>(&Init);
1270}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001271Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001272 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001273}
1274
1275// ImplicitCastExpr
1276Stmt::child_iterator ImplicitCastExpr::child_begin() {
1277 return reinterpret_cast<Stmt**>(&Op);
1278}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001279Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001280 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001281}
1282
1283// CastExpr
1284Stmt::child_iterator CastExpr::child_begin() {
1285 return reinterpret_cast<Stmt**>(&Op);
1286}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001287Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001288 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001289}
1290
1291// BinaryOperator
1292Stmt::child_iterator BinaryOperator::child_begin() {
1293 return reinterpret_cast<Stmt**>(&SubExprs);
1294}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001295Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001296 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001297}
1298
1299// ConditionalOperator
1300Stmt::child_iterator ConditionalOperator::child_begin() {
1301 return reinterpret_cast<Stmt**>(&SubExprs);
1302}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001303Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001304 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001305}
1306
1307// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001308Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1309Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001310
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001311// StmtExpr
1312Stmt::child_iterator StmtExpr::child_begin() {
1313 return reinterpret_cast<Stmt**>(&SubStmt);
1314}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001315Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001316 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001317}
1318
1319// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001320Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1321 return child_iterator();
1322}
1323
1324Stmt::child_iterator TypesCompatibleExpr::child_end() {
1325 return child_iterator();
1326}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001327
1328// ChooseExpr
1329Stmt::child_iterator ChooseExpr::child_begin() {
1330 return reinterpret_cast<Stmt**>(&SubExprs);
1331}
1332
1333Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001334 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001335}
1336
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001337// OverloadExpr
1338Stmt::child_iterator OverloadExpr::child_begin() {
1339 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1340}
1341Stmt::child_iterator OverloadExpr::child_end() {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001342 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001343}
1344
Anders Carlsson36760332007-10-15 20:28:48 +00001345// VAArgExpr
1346Stmt::child_iterator VAArgExpr::child_begin() {
1347 return reinterpret_cast<Stmt**>(&Val);
1348}
1349
1350Stmt::child_iterator VAArgExpr::child_end() {
1351 return reinterpret_cast<Stmt**>(&Val)+1;
1352}
1353
Anders Carlsson762b7c72007-08-31 04:56:16 +00001354// InitListExpr
1355Stmt::child_iterator InitListExpr::child_begin() {
1356 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1357}
1358Stmt::child_iterator InitListExpr::child_end() {
1359 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1360}
1361
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001362// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001363Stmt::child_iterator ObjCStringLiteral::child_begin() {
1364 return child_iterator();
1365}
1366Stmt::child_iterator ObjCStringLiteral::child_end() {
1367 return child_iterator();
1368}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001369
1370// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001371Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1372Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001373
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001374// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001375Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1376 return child_iterator();
1377}
1378Stmt::child_iterator ObjCSelectorExpr::child_end() {
1379 return child_iterator();
1380}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001381
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001382// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001383Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1384 return child_iterator();
1385}
1386Stmt::child_iterator ObjCProtocolExpr::child_end() {
1387 return child_iterator();
1388}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001389
Steve Naroffc39ca262007-09-18 23:55:05 +00001390// ObjCMessageExpr
1391Stmt::child_iterator ObjCMessageExpr::child_begin() {
1392 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1393}
1394Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001395 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001396}
1397