blob: 00ff897ffb13b82d3161c1c9193ec7f5d8a4ca4a [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).
361 if (TR->isVoidType() && !TR.getQualifiers())
362 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
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000477bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000478 switch (getStmtClass()) {
479 default:
480 if (Loc) *Loc = getLocStart();
481 return false;
482 case ParenExprClass:
483 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
484 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000485 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000486 case FloatingLiteralClass:
487 case IntegerLiteralClass:
488 case CharacterLiteralClass:
489 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000490 case TypesCompatibleExprClass:
491 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000492 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000493 case CallExprClass: {
494 const CallExpr *CE = cast<CallExpr>(this);
495 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000496 Result.zextOrTrunc(
497 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000498 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000499 return true;
Steve Naroff44aec4c2008-01-31 01:07:12 +0000500 if (CE->isBuiltinConstantExpr())
501 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000502 if (Loc) *Loc = getLocStart();
503 return false;
504 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000505 case DeclRefExprClass: {
506 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
507 // Accept address of function.
508 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000509 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000510 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000511 if (isa<VarDecl>(D))
512 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000513 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000514 }
Steve Narofff91f9722008-01-09 00:05:37 +0000515 case CompoundLiteralExprClass:
516 if (Loc) *Loc = getLocStart();
517 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemanc4e28e42008-01-25 05:34:48 +0000518 // Allow "(vector type){2,4}" since the elements are all constant.
519 return TR->isArrayType() || TR->isVectorType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000520 case UnaryOperatorClass: {
521 const UnaryOperator *Exp = cast<UnaryOperator>(this);
522
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000523 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000524 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
525 if (!Exp->getSubExpr()->hasStaticStorage()) {
526 if (Loc) *Loc = getLocStart();
527 return false;
528 }
529 return true;
530 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000531
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000532 // Get the operand value. If this is sizeof/alignof, do not evalute the
533 // operand. This affects C99 6.6p3.
Steve Narofff0b23542008-01-10 22:15:12 +0000534 if (!Exp->isSizeOfAlignOfOp() &&
535 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000536 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
537 return false;
538
539 switch (Exp->getOpcode()) {
540 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
541 // See C99 6.6p3.
542 default:
543 if (Loc) *Loc = Exp->getOperatorLoc();
544 return false;
545 case UnaryOperator::Extension:
546 return true; // FIXME: this is wrong.
547 case UnaryOperator::SizeOf:
548 case UnaryOperator::AlignOf:
Steve Narofff0b23542008-01-10 22:15:12 +0000549 case UnaryOperator::OffsetOf:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000550 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000551 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
552 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000553 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000554 }
Chris Lattner06db6132007-10-18 00:20:32 +0000555 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000556 case UnaryOperator::LNot:
557 case UnaryOperator::Plus:
558 case UnaryOperator::Minus:
559 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000560 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000561 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000562 }
563 case SizeOfAlignOfTypeExprClass: {
564 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
565 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000566 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
567 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000568 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000569 }
Chris Lattner06db6132007-10-18 00:20:32 +0000570 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000571 }
572 case BinaryOperatorClass: {
573 const BinaryOperator *Exp = cast<BinaryOperator>(this);
574
575 // The LHS of a constant expr is always evaluated and needed.
576 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
577 return false;
578
579 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
580 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000581 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000582 }
583 case ImplicitCastExprClass:
584 case CastExprClass: {
585 const Expr *SubExpr;
586 SourceLocation CastLoc;
587 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
588 SubExpr = C->getSubExpr();
589 CastLoc = C->getLParenLoc();
590 } else {
591 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
592 CastLoc = getLocStart();
593 }
594 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
595 if (Loc) *Loc = SubExpr->getLocStart();
596 return false;
597 }
Chris Lattner06db6132007-10-18 00:20:32 +0000598 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000599 }
600 case ConditionalOperatorClass: {
601 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000602 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000603 // Handle the GNU extension for missing LHS.
604 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000605 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000606 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000607 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000608 }
Steve Narofff0b23542008-01-10 22:15:12 +0000609 case InitListExprClass: {
610 const InitListExpr *Exp = cast<InitListExpr>(this);
611 unsigned numInits = Exp->getNumInits();
612 for (unsigned i = 0; i < numInits; i++) {
613 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
614 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
615 return false;
616 }
617 }
618 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000619 }
Steve Narofff0b23542008-01-10 22:15:12 +0000620 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000621}
622
Chris Lattner4b009652007-07-25 00:24:17 +0000623/// isIntegerConstantExpr - this recursive routine will test if an expression is
624/// an integer constant expression. Note: With the introduction of VLA's in
625/// C99 the result of the sizeof operator is no longer always a constant
626/// expression. The generalization of the wording to include any subexpression
627/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
628/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
629/// "0 || f()" can be treated as a constant expression. In C90 this expression,
630/// occurring in a context requiring a constant, would have been a constraint
631/// violation. FIXME: This routine currently implements C90 semantics.
632/// To properly implement C99 semantics this routine will need to evaluate
633/// expressions involving operators previously mentioned.
634
635/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
636/// comma, etc
637///
638/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000639/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000640///
641/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
642/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
643/// cast+dereference.
644bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
645 SourceLocation *Loc, bool isEvaluated) const {
646 switch (getStmtClass()) {
647 default:
648 if (Loc) *Loc = getLocStart();
649 return false;
650 case ParenExprClass:
651 return cast<ParenExpr>(this)->getSubExpr()->
652 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
653 case IntegerLiteralClass:
654 Result = cast<IntegerLiteral>(this)->getValue();
655 break;
656 case CharacterLiteralClass: {
657 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000658 Result.zextOrTrunc(
659 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000660 Result = CL->getValue();
661 Result.setIsUnsigned(!getType()->isSignedIntegerType());
662 break;
663 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000664 case TypesCompatibleExprClass: {
665 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000666 Result.zextOrTrunc(
667 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000668 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000669 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000670 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000671 case CallExprClass: {
672 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000673 Result.zextOrTrunc(
674 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000675 if (CE->isBuiltinClassifyType(Result))
676 break;
677 if (Loc) *Loc = getLocStart();
678 return false;
679 }
Chris Lattner4b009652007-07-25 00:24:17 +0000680 case DeclRefExprClass:
681 if (const EnumConstantDecl *D =
682 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
683 Result = D->getInitVal();
684 break;
685 }
686 if (Loc) *Loc = getLocStart();
687 return false;
688 case UnaryOperatorClass: {
689 const UnaryOperator *Exp = cast<UnaryOperator>(this);
690
691 // Get the operand value. If this is sizeof/alignof, do not evalute the
692 // operand. This affects C99 6.6p3.
Anders Carlsson52774ad2008-01-29 15:56:48 +0000693 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner5a9b6242007-08-23 21:42:50 +0000694 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000695 return false;
696
697 switch (Exp->getOpcode()) {
698 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
699 // See C99 6.6p3.
700 default:
701 if (Loc) *Loc = Exp->getOperatorLoc();
702 return false;
703 case UnaryOperator::Extension:
704 return true; // FIXME: this is wrong.
705 case UnaryOperator::SizeOf:
706 case UnaryOperator::AlignOf:
707 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000708 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
709 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000710 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000711 }
Chris Lattner4b009652007-07-25 00:24:17 +0000712
713 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000714 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000715 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
716 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000717
718 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000719 if (Exp->getSubExpr()->getType()->isFunctionType()) {
720 // GCC extension: sizeof(function) = 1.
721 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
722 } else if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner4b009652007-07-25 00:24:17 +0000723 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
724 Exp->getOperatorLoc());
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000725 } else {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000726 unsigned CharSize =
727 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
728
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000729 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
730 Exp->getOperatorLoc()) / CharSize;
731 }
Chris Lattner4b009652007-07-25 00:24:17 +0000732 break;
733 case UnaryOperator::LNot: {
Chris Lattnerf00fdc02008-01-25 19:16:19 +0000734 bool Val = Result == 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000735 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000736 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
737 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000738 Result = Val;
739 break;
740 }
741 case UnaryOperator::Plus:
742 break;
743 case UnaryOperator::Minus:
744 Result = -Result;
745 break;
746 case UnaryOperator::Not:
747 Result = ~Result;
748 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000749 case UnaryOperator::OffsetOf:
750 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000751 }
752 break;
753 }
754 case SizeOfAlignOfTypeExprClass: {
755 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
756 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000757 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
758 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000759 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000760 }
Chris Lattner4b009652007-07-25 00:24:17 +0000761
762 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000763 Result.zextOrTrunc(
764 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000765
766 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000767 if (Exp->getArgumentType()->isFunctionType()) {
768 // GCC extension: sizeof(function) = 1.
769 Result = Exp->isSizeOf() ? 1 : 4;
770 } else if (Exp->isSizeOf()) {
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000771 unsigned CharSize =
772 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
773
774 Result = Ctx.getTypeSize(Exp->getArgumentType(),
775 Exp->getOperatorLoc()) / CharSize;
776 }
Chris Lattner4b009652007-07-25 00:24:17 +0000777 else
778 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000779
Chris Lattner4b009652007-07-25 00:24:17 +0000780 break;
781 }
782 case BinaryOperatorClass: {
783 const BinaryOperator *Exp = cast<BinaryOperator>(this);
784
785 // The LHS of a constant expr is always evaluated and needed.
786 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
787 return false;
788
789 llvm::APSInt RHS(Result);
790
791 // The short-circuiting &&/|| operators don't necessarily evaluate their
792 // RHS. Make sure to pass isEvaluated down correctly.
793 if (Exp->isLogicalOp()) {
794 bool RHSEval;
795 if (Exp->getOpcode() == BinaryOperator::LAnd)
796 RHSEval = Result != 0;
797 else {
798 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
799 RHSEval = Result == 0;
800 }
801
802 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
803 isEvaluated & RHSEval))
804 return false;
805 } else {
806 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
807 return false;
808 }
809
810 switch (Exp->getOpcode()) {
811 default:
812 if (Loc) *Loc = getLocStart();
813 return false;
814 case BinaryOperator::Mul:
815 Result *= RHS;
816 break;
817 case BinaryOperator::Div:
818 if (RHS == 0) {
819 if (!isEvaluated) break;
820 if (Loc) *Loc = getLocStart();
821 return false;
822 }
823 Result /= RHS;
824 break;
825 case BinaryOperator::Rem:
826 if (RHS == 0) {
827 if (!isEvaluated) break;
828 if (Loc) *Loc = getLocStart();
829 return false;
830 }
831 Result %= RHS;
832 break;
833 case BinaryOperator::Add: Result += RHS; break;
834 case BinaryOperator::Sub: Result -= RHS; break;
835 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000836 Result <<=
837 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000838 break;
839 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000840 Result >>=
841 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000842 break;
843 case BinaryOperator::LT: Result = Result < RHS; break;
844 case BinaryOperator::GT: Result = Result > RHS; break;
845 case BinaryOperator::LE: Result = Result <= RHS; break;
846 case BinaryOperator::GE: Result = Result >= RHS; break;
847 case BinaryOperator::EQ: Result = Result == RHS; break;
848 case BinaryOperator::NE: Result = Result != RHS; break;
849 case BinaryOperator::And: Result &= RHS; break;
850 case BinaryOperator::Xor: Result ^= RHS; break;
851 case BinaryOperator::Or: Result |= RHS; break;
852 case BinaryOperator::LAnd:
853 Result = Result != 0 && RHS != 0;
854 break;
855 case BinaryOperator::LOr:
856 Result = Result != 0 || RHS != 0;
857 break;
858
859 case BinaryOperator::Comma:
860 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
861 // *except* when they are contained within a subexpression that is not
862 // evaluated". Note that Assignment can never happen due to constraints
863 // on the LHS subexpr, so we don't need to check it here.
864 if (isEvaluated) {
865 if (Loc) *Loc = getLocStart();
866 return false;
867 }
868
869 // The result of the constant expr is the RHS.
870 Result = RHS;
871 return true;
872 }
873
874 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
875 break;
876 }
877 case ImplicitCastExprClass:
878 case CastExprClass: {
879 const Expr *SubExpr;
880 SourceLocation CastLoc;
881 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
882 SubExpr = C->getSubExpr();
883 CastLoc = C->getLParenLoc();
884 } else {
885 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
886 CastLoc = getLocStart();
887 }
888
889 // C99 6.6p6: shall only convert arithmetic types to integer types.
890 if (!SubExpr->getType()->isArithmeticType() ||
891 !getType()->isIntegerType()) {
892 if (Loc) *Loc = SubExpr->getLocStart();
893 return false;
894 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000895
896 uint32_t DestWidth =
897 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
898
Chris Lattner4b009652007-07-25 00:24:17 +0000899 // Handle simple integer->integer casts.
900 if (SubExpr->getType()->isIntegerType()) {
901 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
902 return false;
903
904 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000905 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +0000906 if (getType()->isBooleanType()) {
907 // Conversion to bool compares against zero.
908 Result = Result != 0;
909 Result.zextOrTrunc(DestWidth);
910 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +0000911 Result.sextOrTrunc(DestWidth);
912 else // If the input is unsigned, do a zero extend, noop, or truncate.
913 Result.zextOrTrunc(DestWidth);
914 break;
915 }
916
917 // Allow floating constants that are the immediate operands of casts or that
918 // are parenthesized.
919 const Expr *Operand = SubExpr;
920 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
921 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000922
923 // If this isn't a floating literal, we can't handle it.
924 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
925 if (!FL) {
926 if (Loc) *Loc = Operand->getLocStart();
927 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000928 }
Chris Lattner000c4102008-01-09 18:59:34 +0000929
930 // If the destination is boolean, compare against zero.
931 if (getType()->isBooleanType()) {
932 Result = !FL->getValue().isZero();
933 Result.zextOrTrunc(DestWidth);
934 break;
935 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000936
937 // Determine whether we are converting to unsigned or signed.
938 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000939
940 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
941 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000942 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000943 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
944 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000945 Result = llvm::APInt(DestWidth, 4, Space);
946 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000947 }
948 case ConditionalOperatorClass: {
949 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
950
951 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
952 return false;
953
954 const Expr *TrueExp = Exp->getLHS();
955 const Expr *FalseExp = Exp->getRHS();
956 if (Result == 0) std::swap(TrueExp, FalseExp);
957
958 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000959 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000960 return false;
961 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000962 if (TrueExp &&
963 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000964 return false;
965 break;
966 }
967 }
968
969 // Cases that are valid constant exprs fall through to here.
970 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
971 return true;
972}
973
Chris Lattner4b009652007-07-25 00:24:17 +0000974/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
975/// integer constant expression with the value zero, or if this is one that is
976/// cast to void*.
977bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffa2e53222008-01-14 16:10:57 +0000978 // Strip off a cast to void*, if it exists.
979 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
980 // Check that it is a cast to void*.
Chris Lattner4b009652007-07-25 00:24:17 +0000981 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
982 QualType Pointee = PT->getPointeeType();
Steve Naroffa2e53222008-01-14 16:10:57 +0000983 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
984 CE->getSubExpr()->getType()->isIntegerType()) // from int.
985 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000986 }
Steve Naroffa2e53222008-01-14 16:10:57 +0000987 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
988 // Ignore the ImplicitCastExpr type entirely.
989 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
990 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
991 // Accept ((void*)0) as a null pointer constant, as many other
992 // implementations do.
993 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Narofff33a9852008-01-14 02:53:34 +0000994 }
Steve Naroffa2e53222008-01-14 16:10:57 +0000995
996 // This expression must be an integer type.
997 if (!getType()->isIntegerType())
998 return false;
999
Chris Lattner4b009652007-07-25 00:24:17 +00001000 // If we have an integer constant expression, we need to *evaluate* it and
1001 // test for the value 0.
1002 llvm::APSInt Val(32);
Steve Naroffa2e53222008-01-14 16:10:57 +00001003 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001004}
Steve Naroffc11705f2007-07-28 23:10:27 +00001005
Chris Lattnera0d03a72007-08-03 17:31:20 +00001006unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +00001007 return strlen(Accessor.getName());
1008}
1009
1010
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001011/// getComponentType - Determine whether the components of this access are
1012/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001013OCUVectorElementExpr::ElementType
1014OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +00001015 // derive the component type, no need to waste space.
1016 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +00001017
Chris Lattner9096b792007-08-02 22:33:49 +00001018 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
1019 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +00001020
Chris Lattner9096b792007-08-02 22:33:49 +00001021 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +00001022 "getComponentType(): Illegal accessor");
1023 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +00001024}
Steve Naroffba67f692007-07-30 03:29:09 +00001025
Chris Lattnera0d03a72007-08-03 17:31:20 +00001026/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +00001027/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001028bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001029 const char *compStr = Accessor.getName();
1030 unsigned length = strlen(compStr);
1031
1032 for (unsigned i = 0; i < length-1; i++) {
1033 const char *s = compStr+i;
1034 for (const char c = *s++; *s; s++)
1035 if (c == *s)
1036 return true;
1037 }
1038 return false;
1039}
Chris Lattner42158e72007-08-02 23:36:59 +00001040
1041/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001042unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +00001043 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +00001044 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +00001045
1046 unsigned Result = 0;
1047
1048 while (length--) {
1049 Result <<= 2;
1050 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1051 assert(Idx != -1 && "Invalid accessor letter");
1052 Result |= Idx;
1053 }
1054 return Result;
1055}
1056
Steve Naroff4ed9d662007-09-27 14:38:14 +00001057// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001058ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001059 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001060 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001061 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001062 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1063 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001064 NumArgs = nargs;
1065 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001066 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001067 if (NumArgs) {
1068 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001069 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1070 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001071 LBracloc = LBrac;
1072 RBracloc = RBrac;
1073}
1074
Steve Naroff4ed9d662007-09-27 14:38:14 +00001075// constructor for class messages.
1076// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001077ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001078 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001079 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001080 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001081 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1082 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001083 NumArgs = nargs;
1084 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +00001085 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +00001086 if (NumArgs) {
1087 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001088 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1089 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001090 LBracloc = LBrac;
1091 RBracloc = RBrac;
1092}
1093
Chris Lattnerf624cd22007-10-25 00:29:32 +00001094
1095bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1096 llvm::APSInt CondVal(32);
1097 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1098 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1099 return CondVal != 0;
1100}
1101
Anders Carlsson52774ad2008-01-29 15:56:48 +00001102static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1103{
1104 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1105 QualType Ty = ME->getBase()->getType();
1106
1107 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
1108 const ASTRecordLayout &RL = C.getASTRecordLayout(RD, SourceLocation());
1109 FieldDecl *FD = ME->getMemberDecl();
1110
1111 // FIXME: This is linear time.
1112 unsigned i = 0, e = 0;
1113 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1114 if (RD->getMember(i) == FD)
1115 break;
1116 }
1117
1118 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1119 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1120 const Expr *Base = ASE->getBase();
1121 llvm::APSInt Idx(32);
1122 bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1123 assert(ICE && "Array index is not a constant integer!");
1124
1125 int64_t size = C.getTypeSize(ASE->getType(), SourceLocation());
1126 size *= Idx.getSExtValue();
1127
1128 return size + evaluateOffsetOf(C, Base);
1129 } else if (isa<CompoundLiteralExpr>(E))
1130 return 0;
1131
1132 assert(0 && "Unknown offsetof subexpression!");
1133 return 0;
1134}
1135
1136int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1137{
1138 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1139
1140 unsigned CharSize =
1141 C.Target.getCharWidth(C.getFullLoc(getOperatorLoc()));
1142
1143 return ::evaluateOffsetOf(C, Val) / CharSize;
1144}
1145
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001146//===----------------------------------------------------------------------===//
1147// Child Iterators for iterating over subexpressions/substatements
1148//===----------------------------------------------------------------------===//
1149
1150// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001151Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1152Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001153
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001154// ObjCIvarRefExpr
1155Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1156Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1157
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001158// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001159Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1160Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001161
1162// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001163Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1164Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001165
1166// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001167Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1168Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001169
1170// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001171Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1172Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001173
Chris Lattner1de66eb2007-08-26 03:42:43 +00001174// ImaginaryLiteral
1175Stmt::child_iterator ImaginaryLiteral::child_begin() {
1176 return reinterpret_cast<Stmt**>(&Val);
1177}
1178Stmt::child_iterator ImaginaryLiteral::child_end() {
1179 return reinterpret_cast<Stmt**>(&Val)+1;
1180}
1181
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001182// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001183Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1184Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001185
1186// ParenExpr
1187Stmt::child_iterator ParenExpr::child_begin() {
1188 return reinterpret_cast<Stmt**>(&Val);
1189}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001190Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001191 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001192}
1193
1194// UnaryOperator
1195Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001196 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001197}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001198Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001199 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001200}
1201
1202// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001203Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001204 // If the type is a VLA type (and not a typedef), the size expression of the
1205 // VLA needs to be treated as an executable expression.
1206 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1207 return child_iterator(T);
1208 else
1209 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001210}
1211Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001212 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001213}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001214
1215// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001216Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001217 return reinterpret_cast<Stmt**>(&SubExprs);
1218}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001219Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001220 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001221}
1222
1223// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001224Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001225 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001226}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001227Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001228 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001229}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001230
1231// MemberExpr
1232Stmt::child_iterator MemberExpr::child_begin() {
1233 return reinterpret_cast<Stmt**>(&Base);
1234}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001235Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001236 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001237}
1238
1239// OCUVectorElementExpr
1240Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1241 return reinterpret_cast<Stmt**>(&Base);
1242}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001243Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001244 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001245}
1246
1247// CompoundLiteralExpr
1248Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1249 return reinterpret_cast<Stmt**>(&Init);
1250}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001251Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001252 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001253}
1254
1255// ImplicitCastExpr
1256Stmt::child_iterator ImplicitCastExpr::child_begin() {
1257 return reinterpret_cast<Stmt**>(&Op);
1258}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001259Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001260 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001261}
1262
1263// CastExpr
1264Stmt::child_iterator CastExpr::child_begin() {
1265 return reinterpret_cast<Stmt**>(&Op);
1266}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001267Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001268 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001269}
1270
1271// BinaryOperator
1272Stmt::child_iterator BinaryOperator::child_begin() {
1273 return reinterpret_cast<Stmt**>(&SubExprs);
1274}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001275Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001276 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001277}
1278
1279// ConditionalOperator
1280Stmt::child_iterator ConditionalOperator::child_begin() {
1281 return reinterpret_cast<Stmt**>(&SubExprs);
1282}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001283Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001284 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001285}
1286
1287// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001288Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1289Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001290
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001291// StmtExpr
1292Stmt::child_iterator StmtExpr::child_begin() {
1293 return reinterpret_cast<Stmt**>(&SubStmt);
1294}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001295Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001296 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001297}
1298
1299// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001300Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1301 return child_iterator();
1302}
1303
1304Stmt::child_iterator TypesCompatibleExpr::child_end() {
1305 return child_iterator();
1306}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001307
1308// ChooseExpr
1309Stmt::child_iterator ChooseExpr::child_begin() {
1310 return reinterpret_cast<Stmt**>(&SubExprs);
1311}
1312
1313Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001314 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001315}
1316
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001317// OverloadExpr
1318Stmt::child_iterator OverloadExpr::child_begin() {
1319 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1320}
1321Stmt::child_iterator OverloadExpr::child_end() {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001322 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001323}
1324
Anders Carlsson36760332007-10-15 20:28:48 +00001325// VAArgExpr
1326Stmt::child_iterator VAArgExpr::child_begin() {
1327 return reinterpret_cast<Stmt**>(&Val);
1328}
1329
1330Stmt::child_iterator VAArgExpr::child_end() {
1331 return reinterpret_cast<Stmt**>(&Val)+1;
1332}
1333
Anders Carlsson762b7c72007-08-31 04:56:16 +00001334// InitListExpr
1335Stmt::child_iterator InitListExpr::child_begin() {
1336 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1337}
1338Stmt::child_iterator InitListExpr::child_end() {
1339 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1340}
1341
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001342// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001343Stmt::child_iterator ObjCStringLiteral::child_begin() {
1344 return child_iterator();
1345}
1346Stmt::child_iterator ObjCStringLiteral::child_end() {
1347 return child_iterator();
1348}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001349
1350// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001351Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1352Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001353
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001354// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001355Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1356 return child_iterator();
1357}
1358Stmt::child_iterator ObjCSelectorExpr::child_end() {
1359 return child_iterator();
1360}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001361
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001362// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001363Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1364 return child_iterator();
1365}
1366Stmt::child_iterator ObjCProtocolExpr::child_end() {
1367 return child_iterator();
1368}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001369
Steve Naroffc39ca262007-09-18 23:55:05 +00001370// ObjCMessageExpr
1371Stmt::child_iterator ObjCMessageExpr::child_begin() {
1372 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1373}
1374Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001375 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001376}
1377