blob: f02e817f0659dadc461ca9e2b5801b4a80177ef4 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/StmtVisitor.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000018#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner73d0d4f2007-08-30 17:45:32 +000073 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +000074 }
75}
76
77//===----------------------------------------------------------------------===//
78// Postfix Operators.
79//===----------------------------------------------------------------------===//
80
Nate Begemane2ce1d92008-01-17 17:46:27 +000081
Reid Spencer5f016e22007-07-11 17:01:13 +000082CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
83 SourceLocation rparenloc)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000084 : Expr(CallExprClass, t), NumArgs(numargs) {
85 SubExprs = new Expr*[numargs+1];
86 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +000087 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000088 SubExprs[i+ARGS_START] = args[i];
Reid Spencer5f016e22007-07-11 17:01:13 +000089 RParenLoc = rparenloc;
90}
91
Chris Lattnerd18b3292007-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 Naroffc4f8e8b2008-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 Carlssonbcba2012008-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 Naroffc4f8e8b2008-01-31 01:07:12 +0000143 return true;
144 return false;
145}
Chris Lattnerd18b3292007-12-28 05:25:02 +0000146
Steve Naroff13b7c5f2007-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 Lattner3ef5bc02007-11-08 17:56:40 +0000209 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000210 }
211 return true;
212 }
213 return false;
214}
215
Reid Spencer5f016e22007-07-11 17:01:13 +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 Carlsson66b5a8a2007-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}
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnere7716e62007-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 Lattnereb14fe82007-08-25 02:00:02 +0000311 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000312 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000313
Fariborz Jahanianab38e4b2007-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnera9c01022007-09-26 22:06:30 +0000331 case ObjCMessageExprClass:
332 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner7da36f62007-10-30 22:53:42 +0000352/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000353/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000354///
Bill Wendlingca51c972007-07-16 07:07:56 +0000355Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000357 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 return LV_NotObjectType;
359
Steve Naroff731ec572007-07-21 13:32:03 +0000360 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000362
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000363 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000364 return LV_Valid;
365
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 // the type looks fine, now check the expression
367 switch (getStmtClass()) {
368 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000369 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
371 // For vectors, make sure base is an lvalue (i.e. not a function call).
372 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
373 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
374 return LV_Valid;
375 case DeclRefExprClass: // C99 6.5.1p2
376 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
377 return LV_Valid;
378 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000379 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 const MemberExpr *m = cast<MemberExpr>(this);
381 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000382 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000383 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000385 return LV_Valid; // C99 6.5.3p4
386
387 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
388 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
389 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 break;
391 case ParenExprClass: // C99 6.5.1p5
392 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffe6386392007-12-05 04:00:10 +0000393 case CompoundLiteralExprClass: // C99 6.5.2.5p5
394 return LV_Valid;
Chris Lattner6481a572007-08-03 17:31:20 +0000395 case OCUVectorElementExprClass:
396 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000397 return LV_DuplicateVectorComponents;
398 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000399 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
400 return LV_Valid;
Chris Lattnerfa28b302008-01-12 08:14:25 +0000401 case PreDefinedExprClass:
402 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 default:
404 break;
405 }
406 return LV_InvalidExpression;
407}
408
409/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
410/// does not have an incomplete type, does not have a const-qualified type, and
411/// if it is a structure or union, does not have any member (including,
412/// recursively, any member or element of all contained aggregates or unions)
413/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000414Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 isLvalueResult lvalResult = isLvalue();
416
417 switch (lvalResult) {
418 case LV_Valid: break;
419 case LV_NotObjectType: return MLV_NotObjectType;
420 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000421 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 case LV_InvalidExpression: return MLV_InvalidExpression;
423 }
424 if (TR.isConstQualified())
425 return MLV_ConstQualified;
426 if (TR->isArrayType())
427 return MLV_ArrayType;
428 if (TR->isIncompleteType())
429 return MLV_IncompleteType;
430
431 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
432 if (r->hasConstFields())
433 return MLV_ConstQualified;
434 }
435 return MLV_Valid;
436}
437
Chris Lattner4cc62712007-11-27 21:35:27 +0000438/// hasStaticStorage - Return true if this expression has static storage
439/// duration. This means that the address of this expression is a link-time
440/// constant.
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000441bool Expr::hasStaticStorage() const {
442 switch (getStmtClass()) {
443 default:
444 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000445 case ParenExprClass:
446 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
447 case ImplicitCastExprClass:
448 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000449 case CompoundLiteralExprClass:
450 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000451 case DeclRefExprClass: {
452 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
453 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
454 return VD->hasStaticStorage();
455 return false;
456 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000457 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000458 const MemberExpr *M = cast<MemberExpr>(this);
459 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000460 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000461 case ArraySubscriptExprClass:
462 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattnerfa28b302008-01-12 08:14:25 +0000463 case PreDefinedExprClass:
464 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000465 }
466}
467
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000468Expr* Expr::IgnoreParens() {
469 Expr* E = this;
470 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
471 E = P->getSubExpr();
472
473 return E;
474}
475
Steve Naroff38374b02007-09-02 20:30:18 +0000476bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff38374b02007-09-02 20:30:18 +0000477 switch (getStmtClass()) {
478 default:
479 if (Loc) *Loc = getLocStart();
480 return false;
481 case ParenExprClass:
482 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
483 case StringLiteralClass:
Steve Naroff5d37e322007-11-09 15:00:03 +0000484 case ObjCStringLiteralClass:
Steve Naroff38374b02007-09-02 20:30:18 +0000485 case FloatingLiteralClass:
486 case IntegerLiteralClass:
487 case CharacterLiteralClass:
488 case ImaginaryLiteralClass:
Anders Carlsson1a86b332007-10-17 00:52:43 +0000489 case TypesCompatibleExprClass:
490 case CXXBoolLiteralExprClass:
Chris Lattner2777e492007-10-18 00:20:32 +0000491 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000492 case CallExprClass: {
493 const CallExpr *CE = cast<CallExpr>(this);
494 llvm::APSInt Result(32);
Hartmut Kaiser86fd3552007-09-16 21:35:35 +0000495 Result.zextOrTrunc(
496 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff38374b02007-09-02 20:30:18 +0000497 if (CE->isBuiltinClassifyType(Result))
Chris Lattner2777e492007-10-18 00:20:32 +0000498 return true;
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000499 if (CE->isBuiltinConstantExpr())
500 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000501 if (Loc) *Loc = getLocStart();
502 return false;
503 }
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000504 case DeclRefExprClass: {
505 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
506 // Accept address of function.
507 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner2777e492007-10-18 00:20:32 +0000508 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000509 if (Loc) *Loc = getLocStart();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000510 if (isa<VarDecl>(D))
511 return TR->isArrayType();
Steve Naroff38374b02007-09-02 20:30:18 +0000512 return false;
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000513 }
Steve Naroffb8f13a82008-01-09 00:05:37 +0000514 case CompoundLiteralExprClass:
515 if (Loc) *Loc = getLocStart();
516 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemand47d4f52008-01-25 05:34:48 +0000517 // Allow "(vector type){2,4}" since the elements are all constant.
518 return TR->isArrayType() || TR->isVectorType();
Steve Naroff38374b02007-09-02 20:30:18 +0000519 case UnaryOperatorClass: {
520 const UnaryOperator *Exp = cast<UnaryOperator>(this);
521
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000522 // C99 6.6p9
Chris Lattner239c15e2007-12-11 23:11:17 +0000523 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
524 if (!Exp->getSubExpr()->hasStaticStorage()) {
525 if (Loc) *Loc = getLocStart();
526 return false;
527 }
528 return true;
529 }
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000530
Steve Naroff38374b02007-09-02 20:30:18 +0000531 // Get the operand value. If this is sizeof/alignof, do not evalute the
532 // operand. This affects C99 6.6p3.
Steve Naroffd0091aa2008-01-10 22:15:12 +0000533 if (!Exp->isSizeOfAlignOfOp() &&
534 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff38374b02007-09-02 20:30:18 +0000535 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
536 return false;
537
538 switch (Exp->getOpcode()) {
539 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
540 // See C99 6.6p3.
541 default:
542 if (Loc) *Loc = Exp->getOperatorLoc();
543 return false;
544 case UnaryOperator::Extension:
545 return true; // FIXME: this is wrong.
546 case UnaryOperator::SizeOf:
547 case UnaryOperator::AlignOf:
Steve Naroffd0091aa2008-01-10 22:15:12 +0000548 case UnaryOperator::OffsetOf:
Steve Naroff38374b02007-09-02 20:30:18 +0000549 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000550 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
551 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000552 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000553 }
Chris Lattner2777e492007-10-18 00:20:32 +0000554 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000555 case UnaryOperator::LNot:
556 case UnaryOperator::Plus:
557 case UnaryOperator::Minus:
558 case UnaryOperator::Not:
Chris Lattner2777e492007-10-18 00:20:32 +0000559 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000560 }
Steve Naroff38374b02007-09-02 20:30:18 +0000561 }
562 case SizeOfAlignOfTypeExprClass: {
563 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
564 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000565 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
566 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000567 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000568 }
Chris Lattner2777e492007-10-18 00:20:32 +0000569 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000570 }
571 case BinaryOperatorClass: {
572 const BinaryOperator *Exp = cast<BinaryOperator>(this);
573
574 // The LHS of a constant expr is always evaluated and needed.
575 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
576 return false;
577
578 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
579 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000580 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000581 }
582 case ImplicitCastExprClass:
583 case CastExprClass: {
584 const Expr *SubExpr;
585 SourceLocation CastLoc;
586 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
587 SubExpr = C->getSubExpr();
588 CastLoc = C->getLParenLoc();
589 } else {
590 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
591 CastLoc = getLocStart();
592 }
593 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
594 if (Loc) *Loc = SubExpr->getLocStart();
595 return false;
596 }
Chris Lattner2777e492007-10-18 00:20:32 +0000597 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000598 }
599 case ConditionalOperatorClass: {
600 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner2777e492007-10-18 00:20:32 +0000601 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson39073232007-11-30 19:04:31 +0000602 // Handle the GNU extension for missing LHS.
603 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner2777e492007-10-18 00:20:32 +0000604 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff38374b02007-09-02 20:30:18 +0000605 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000606 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000607 }
Steve Naroffd0091aa2008-01-10 22:15:12 +0000608 case InitListExprClass: {
609 const InitListExpr *Exp = cast<InitListExpr>(this);
610 unsigned numInits = Exp->getNumInits();
611 for (unsigned i = 0; i < numInits; i++) {
612 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
613 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
614 return false;
615 }
616 }
617 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000618 }
Steve Naroffd0091aa2008-01-10 22:15:12 +0000619 }
Steve Naroff38374b02007-09-02 20:30:18 +0000620}
621
Reid Spencer5f016e22007-07-11 17:01:13 +0000622/// isIntegerConstantExpr - this recursive routine will test if an expression is
623/// an integer constant expression. Note: With the introduction of VLA's in
624/// C99 the result of the sizeof operator is no longer always a constant
625/// expression. The generalization of the wording to include any subexpression
626/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
627/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
628/// "0 || f()" can be treated as a constant expression. In C90 this expression,
629/// occurring in a context requiring a constant, would have been a constraint
630/// violation. FIXME: This routine currently implements C90 semantics.
631/// To properly implement C99 semantics this routine will need to evaluate
632/// expressions involving operators previously mentioned.
633
634/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
635/// comma, etc
636///
637/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000638/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000639///
640/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
641/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
642/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000643bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
644 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 switch (getStmtClass()) {
646 default:
647 if (Loc) *Loc = getLocStart();
648 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 case ParenExprClass:
650 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000651 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 case IntegerLiteralClass:
653 Result = cast<IntegerLiteral>(this)->getValue();
654 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000655 case CharacterLiteralClass: {
656 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000657 Result.zextOrTrunc(
658 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000659 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000660 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000662 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000663 case TypesCompatibleExprClass: {
664 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000665 Result.zextOrTrunc(
666 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffec0550f2007-10-15 20:41:53 +0000667 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff389cecc2007-08-02 00:13:27 +0000668 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000669 }
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000670 case CallExprClass: {
671 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000672 Result.zextOrTrunc(
673 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000674 if (CE->isBuiltinClassifyType(Result))
675 break;
676 if (Loc) *Loc = getLocStart();
677 return false;
678 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 case DeclRefExprClass:
680 if (const EnumConstantDecl *D =
681 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
682 Result = D->getInitVal();
683 break;
684 }
685 if (Loc) *Loc = getLocStart();
686 return false;
687 case UnaryOperatorClass: {
688 const UnaryOperator *Exp = cast<UnaryOperator>(this);
689
690 // Get the operand value. If this is sizeof/alignof, do not evalute the
691 // operand. This affects C99 6.6p3.
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000692 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner602dafd2007-08-23 21:42:50 +0000693 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 return false;
695
696 switch (Exp->getOpcode()) {
697 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
698 // See C99 6.6p3.
699 default:
700 if (Loc) *Loc = Exp->getOperatorLoc();
701 return false;
702 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000703 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 case UnaryOperator::SizeOf:
705 case UnaryOperator::AlignOf:
706 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000707 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
708 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000710 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000711
Chris Lattner76e773a2007-07-18 18:38:36 +0000712 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000713 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000714 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
715 Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000716
717 // Get information about the size or align.
Chris Lattnerefdd1572008-01-02 21:54:09 +0000718 if (Exp->getSubExpr()->getType()->isFunctionType()) {
719 // GCC extension: sizeof(function) = 1.
720 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
721 } else if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner76e773a2007-07-18 18:38:36 +0000722 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
723 Exp->getOperatorLoc());
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000724 } else {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000725 unsigned CharSize =
726 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
727
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000728 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
729 Exp->getOperatorLoc()) / CharSize;
730 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 break;
732 case UnaryOperator::LNot: {
Chris Lattnerbf755382008-01-25 19:16:19 +0000733 bool Val = Result == 0;
Chris Lattner701e5eb2007-09-04 02:45:27 +0000734 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000735 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
736 Exp->getOperatorLoc())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 Result = Val;
738 break;
739 }
740 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 break;
742 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 Result = -Result;
744 break;
745 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 Result = ~Result;
747 break;
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000748 case UnaryOperator::OffsetOf:
749 Result = Exp->evaluateOffsetOf(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 }
751 break;
752 }
753 case SizeOfAlignOfTypeExprClass: {
754 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
755 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000756 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
757 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000759 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000760
Chris Lattner76e773a2007-07-18 18:38:36 +0000761 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000762 Result.zextOrTrunc(
763 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000764
765 // Get information about the size or align.
Chris Lattnerefdd1572008-01-02 21:54:09 +0000766 if (Exp->getArgumentType()->isFunctionType()) {
767 // GCC extension: sizeof(function) = 1.
768 Result = Exp->isSizeOf() ? 1 : 4;
769 } else if (Exp->isSizeOf()) {
Ted Kremenek060e4702007-12-17 17:38:43 +0000770 unsigned CharSize =
771 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
772
773 Result = Ctx.getTypeSize(Exp->getArgumentType(),
774 Exp->getOperatorLoc()) / CharSize;
775 }
Chris Lattner76e773a2007-07-18 18:38:36 +0000776 else
777 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremenek060e4702007-12-17 17:38:43 +0000778
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 break;
780 }
781 case BinaryOperatorClass: {
782 const BinaryOperator *Exp = cast<BinaryOperator>(this);
783
784 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000785 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000786 return false;
787
788 llvm::APSInt RHS(Result);
789
790 // The short-circuiting &&/|| operators don't necessarily evaluate their
791 // RHS. Make sure to pass isEvaluated down correctly.
792 if (Exp->isLogicalOp()) {
793 bool RHSEval;
794 if (Exp->getOpcode() == BinaryOperator::LAnd)
795 RHSEval = Result != 0;
796 else {
797 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
798 RHSEval = Result == 0;
799 }
800
Chris Lattner590b6642007-07-15 23:26:56 +0000801 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 isEvaluated & RHSEval))
803 return false;
804 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000805 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 return false;
807 }
808
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 switch (Exp->getOpcode()) {
810 default:
811 if (Loc) *Loc = getLocStart();
812 return false;
813 case BinaryOperator::Mul:
814 Result *= RHS;
815 break;
816 case BinaryOperator::Div:
817 if (RHS == 0) {
818 if (!isEvaluated) break;
819 if (Loc) *Loc = getLocStart();
820 return false;
821 }
822 Result /= RHS;
823 break;
824 case BinaryOperator::Rem:
825 if (RHS == 0) {
826 if (!isEvaluated) break;
827 if (Loc) *Loc = getLocStart();
828 return false;
829 }
830 Result %= RHS;
831 break;
832 case BinaryOperator::Add: Result += RHS; break;
833 case BinaryOperator::Sub: Result -= RHS; break;
834 case BinaryOperator::Shl:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000835 Result <<=
836 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 break;
838 case BinaryOperator::Shr:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000839 Result >>=
840 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 break;
842 case BinaryOperator::LT: Result = Result < RHS; break;
843 case BinaryOperator::GT: Result = Result > RHS; break;
844 case BinaryOperator::LE: Result = Result <= RHS; break;
845 case BinaryOperator::GE: Result = Result >= RHS; break;
846 case BinaryOperator::EQ: Result = Result == RHS; break;
847 case BinaryOperator::NE: Result = Result != RHS; break;
848 case BinaryOperator::And: Result &= RHS; break;
849 case BinaryOperator::Xor: Result ^= RHS; break;
850 case BinaryOperator::Or: Result |= RHS; break;
851 case BinaryOperator::LAnd:
852 Result = Result != 0 && RHS != 0;
853 break;
854 case BinaryOperator::LOr:
855 Result = Result != 0 || RHS != 0;
856 break;
857
858 case BinaryOperator::Comma:
859 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
860 // *except* when they are contained within a subexpression that is not
861 // evaluated". Note that Assignment can never happen due to constraints
862 // on the LHS subexpr, so we don't need to check it here.
863 if (isEvaluated) {
864 if (Loc) *Loc = getLocStart();
865 return false;
866 }
867
868 // The result of the constant expr is the RHS.
869 Result = RHS;
870 return true;
871 }
872
873 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
874 break;
875 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000876 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000878 const Expr *SubExpr;
879 SourceLocation CastLoc;
880 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
881 SubExpr = C->getSubExpr();
882 CastLoc = C->getLParenLoc();
883 } else {
884 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
885 CastLoc = getLocStart();
886 }
887
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000889 if (!SubExpr->getType()->isArithmeticType() ||
890 !getType()->isIntegerType()) {
891 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 return false;
893 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000894
895 uint32_t DestWidth =
896 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
897
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000899 if (SubExpr->getType()->isIntegerType()) {
900 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000902
903 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000904 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattnerc0a356b2008-01-09 18:59:34 +0000905 if (getType()->isBooleanType()) {
906 // Conversion to bool compares against zero.
907 Result = Result != 0;
908 Result.zextOrTrunc(DestWidth);
909 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner26dc7b32007-07-15 23:54:50 +0000910 Result.sextOrTrunc(DestWidth);
911 else // If the input is unsigned, do a zero extend, noop, or truncate.
912 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 break;
914 }
915
916 // Allow floating constants that are the immediate operands of casts or that
917 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000918 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
920 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +0000921
922 // If this isn't a floating literal, we can't handle it.
923 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
924 if (!FL) {
925 if (Loc) *Loc = Operand->getLocStart();
926 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 }
Chris Lattnerc0a356b2008-01-09 18:59:34 +0000928
929 // If the destination is boolean, compare against zero.
930 if (getType()->isBooleanType()) {
931 Result = !FL->getValue().isZero();
932 Result.zextOrTrunc(DestWidth);
933 break;
934 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000935
936 // Determine whether we are converting to unsigned or signed.
937 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +0000938
939 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
940 // be called multiple times per AST.
Chris Lattner987b15d2007-09-22 19:04:13 +0000941 uint64_t Space[4];
Chris Lattnerccc213f2007-09-26 00:47:26 +0000942 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
943 llvm::APFloat::rmTowardZero);
Chris Lattner987b15d2007-09-22 19:04:13 +0000944 Result = llvm::APInt(DestWidth, 4, Space);
945 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 }
947 case ConditionalOperatorClass: {
948 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
949
Chris Lattner590b6642007-07-15 23:26:56 +0000950 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 return false;
952
953 const Expr *TrueExp = Exp->getLHS();
954 const Expr *FalseExp = Exp->getRHS();
955 if (Result == 0) std::swap(TrueExp, FalseExp);
956
957 // Evaluate the false one first, discard the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000958 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 return false;
960 // Evalute the true one, capture the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000961 if (TrueExp &&
962 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 break;
965 }
966 }
967
968 // Cases that are valid constant exprs fall through to here.
969 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
970 return true;
971}
972
Reid Spencer5f016e22007-07-11 17:01:13 +0000973/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
974/// integer constant expression with the value zero, or if this is one that is
975/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000976bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffaa58f002008-01-14 16:10:57 +0000977 // Strip off a cast to void*, if it exists.
978 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
979 // Check that it is a cast to void*.
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
981 QualType Pointee = PT->getPointeeType();
Steve Naroffaa58f002008-01-14 16:10:57 +0000982 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
983 CE->getSubExpr()->getType()->isIntegerType()) // from int.
984 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 }
Steve Naroffaa58f002008-01-14 16:10:57 +0000986 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
987 // Ignore the ImplicitCastExpr type entirely.
988 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
989 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
990 // Accept ((void*)0) as a null pointer constant, as many other
991 // implementations do.
992 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaaffbf72008-01-14 02:53:34 +0000993 }
Steve Naroffaa58f002008-01-14 16:10:57 +0000994
995 // This expression must be an integer type.
996 if (!getType()->isIntegerType())
997 return false;
998
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 // If we have an integer constant expression, we need to *evaluate* it and
1000 // test for the value 0.
1001 llvm::APSInt Val(32);
Steve Naroffaa58f002008-01-14 16:10:57 +00001002 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001003}
Steve Naroff31a45842007-07-28 23:10:27 +00001004
Chris Lattner6481a572007-08-03 17:31:20 +00001005unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner4d0ac882007-08-03 16:00:20 +00001006 return strlen(Accessor.getName());
1007}
1008
1009
Chris Lattnercb92a112007-08-02 21:47:28 +00001010/// getComponentType - Determine whether the components of this access are
1011/// "point" "color" or "texture" elements.
Chris Lattner6481a572007-08-03 17:31:20 +00001012OCUVectorElementExpr::ElementType
1013OCUVectorElementExpr::getElementType() const {
Steve Naroff31a45842007-07-28 23:10:27 +00001014 // derive the component type, no need to waste space.
1015 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +00001016
Chris Lattner88dca042007-08-02 22:33:49 +00001017 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
1018 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +00001019
Chris Lattner88dca042007-08-02 22:33:49 +00001020 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +00001021 "getComponentType(): Illegal accessor");
1022 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +00001023}
Steve Narofffec0b492007-07-30 03:29:09 +00001024
Chris Lattner6481a572007-08-03 17:31:20 +00001025/// containsDuplicateElements - Return true if any element access is
Chris Lattnercb92a112007-08-02 21:47:28 +00001026/// repeated.
Chris Lattner6481a572007-08-03 17:31:20 +00001027bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001028 const char *compStr = Accessor.getName();
1029 unsigned length = strlen(compStr);
1030
1031 for (unsigned i = 0; i < length-1; i++) {
1032 const char *s = compStr+i;
1033 for (const char c = *s++; *s; s++)
1034 if (c == *s)
1035 return true;
1036 }
1037 return false;
1038}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001039
1040/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattner6481a572007-08-03 17:31:20 +00001041unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001042 const char *compStr = Accessor.getName();
Chris Lattner6481a572007-08-03 17:31:20 +00001043 unsigned length = getNumElements();
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001044
1045 unsigned Result = 0;
1046
1047 while (length--) {
1048 Result <<= 2;
1049 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1050 assert(Idx != -1 && "Invalid accessor letter");
1051 Result |= Idx;
1052 }
1053 return Result;
1054}
1055
Steve Naroff68d331a2007-09-27 14:38:14 +00001056// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001057ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001058 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001059 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001060 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001061 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1062 MethodProto(mproto), ClassName(0) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001063 NumArgs = nargs;
1064 SubExprs = new Expr*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001065 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001066 if (NumArgs) {
1067 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001068 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1069 }
Steve Naroff563477d2007-09-18 23:55:05 +00001070 LBracloc = LBrac;
1071 RBracloc = RBrac;
1072}
1073
Steve Naroff68d331a2007-09-27 14:38:14 +00001074// constructor for class messages.
1075// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001076ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001077 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001078 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001079 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001080 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1081 MethodProto(mproto), ClassName(clsName) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001082 NumArgs = nargs;
1083 SubExprs = new Expr*[NumArgs+1];
Steve Naroff563477d2007-09-18 23:55:05 +00001084 SubExprs[RECEIVER] = 0;
Steve Naroff49f109c2007-11-15 13:05:42 +00001085 if (NumArgs) {
1086 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001087 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1088 }
Steve Naroff563477d2007-09-18 23:55:05 +00001089 LBracloc = LBrac;
1090 RBracloc = RBrac;
1091}
1092
Chris Lattner27437ca2007-10-25 00:29:32 +00001093
1094bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1095 llvm::APSInt CondVal(32);
1096 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1097 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1098 return CondVal != 0;
1099}
1100
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001101static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1102{
1103 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1104 QualType Ty = ME->getBase()->getType();
1105
1106 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
1107 const ASTRecordLayout &RL = C.getASTRecordLayout(RD, SourceLocation());
1108 FieldDecl *FD = ME->getMemberDecl();
1109
1110 // FIXME: This is linear time.
1111 unsigned i = 0, e = 0;
1112 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1113 if (RD->getMember(i) == FD)
1114 break;
1115 }
1116
1117 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1118 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1119 const Expr *Base = ASE->getBase();
1120 llvm::APSInt Idx(32);
1121 bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1122 assert(ICE && "Array index is not a constant integer!");
1123
1124 int64_t size = C.getTypeSize(ASE->getType(), SourceLocation());
1125 size *= Idx.getSExtValue();
1126
1127 return size + evaluateOffsetOf(C, Base);
1128 } else if (isa<CompoundLiteralExpr>(E))
1129 return 0;
1130
1131 assert(0 && "Unknown offsetof subexpression!");
1132 return 0;
1133}
1134
1135int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1136{
1137 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1138
1139 unsigned CharSize =
1140 C.Target.getCharWidth(C.getFullLoc(getOperatorLoc()));
1141
1142 return ::evaluateOffsetOf(C, Val) / CharSize;
1143}
1144
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001145//===----------------------------------------------------------------------===//
1146// Child Iterators for iterating over subexpressions/substatements
1147//===----------------------------------------------------------------------===//
1148
1149// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001150Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1151Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001152
Steve Naroff7779db42007-11-12 14:29:37 +00001153// ObjCIvarRefExpr
1154Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1155Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1156
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001157// PreDefinedExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001158Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1159Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001160
1161// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001162Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1163Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001164
1165// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001166Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1167Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001168
1169// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001170Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1171Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001172
Chris Lattner5d661452007-08-26 03:42:43 +00001173// ImaginaryLiteral
1174Stmt::child_iterator ImaginaryLiteral::child_begin() {
1175 return reinterpret_cast<Stmt**>(&Val);
1176}
1177Stmt::child_iterator ImaginaryLiteral::child_end() {
1178 return reinterpret_cast<Stmt**>(&Val)+1;
1179}
1180
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001181// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001182Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1183Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001184
1185// ParenExpr
1186Stmt::child_iterator ParenExpr::child_begin() {
1187 return reinterpret_cast<Stmt**>(&Val);
1188}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001189Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001190 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001191}
1192
1193// UnaryOperator
1194Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001195 return reinterpret_cast<Stmt**>(&Val);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001196}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001197Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001198 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001199}
1200
1201// SizeOfAlignOfTypeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001202Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001203 // If the type is a VLA type (and not a typedef), the size expression of the
1204 // VLA needs to be treated as an executable expression.
1205 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1206 return child_iterator(T);
1207 else
1208 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001209}
1210Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001211 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001212}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001213
1214// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001215Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001216 return reinterpret_cast<Stmt**>(&SubExprs);
1217}
Ted Kremenek1237c672007-08-24 20:06:47 +00001218Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001219 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001220}
1221
1222// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001223Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001224 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001225}
Ted Kremenek1237c672007-08-24 20:06:47 +00001226Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001227 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001228}
Ted Kremenek1237c672007-08-24 20:06:47 +00001229
1230// MemberExpr
1231Stmt::child_iterator MemberExpr::child_begin() {
1232 return reinterpret_cast<Stmt**>(&Base);
1233}
Ted Kremenek1237c672007-08-24 20:06:47 +00001234Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001235 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001236}
1237
1238// OCUVectorElementExpr
1239Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1240 return reinterpret_cast<Stmt**>(&Base);
1241}
Ted Kremenek1237c672007-08-24 20:06:47 +00001242Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001243 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001244}
1245
1246// CompoundLiteralExpr
1247Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1248 return reinterpret_cast<Stmt**>(&Init);
1249}
Ted Kremenek1237c672007-08-24 20:06:47 +00001250Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001251 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001252}
1253
1254// ImplicitCastExpr
1255Stmt::child_iterator ImplicitCastExpr::child_begin() {
1256 return reinterpret_cast<Stmt**>(&Op);
1257}
Ted Kremenek1237c672007-08-24 20:06:47 +00001258Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001259 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001260}
1261
1262// CastExpr
1263Stmt::child_iterator CastExpr::child_begin() {
1264 return reinterpret_cast<Stmt**>(&Op);
1265}
Ted Kremenek1237c672007-08-24 20:06:47 +00001266Stmt::child_iterator CastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001267 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001268}
1269
1270// BinaryOperator
1271Stmt::child_iterator BinaryOperator::child_begin() {
1272 return reinterpret_cast<Stmt**>(&SubExprs);
1273}
Ted Kremenek1237c672007-08-24 20:06:47 +00001274Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001275 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001276}
1277
1278// ConditionalOperator
1279Stmt::child_iterator ConditionalOperator::child_begin() {
1280 return reinterpret_cast<Stmt**>(&SubExprs);
1281}
Ted Kremenek1237c672007-08-24 20:06:47 +00001282Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001283 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001284}
1285
1286// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001287Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1288Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001289
Ted Kremenek1237c672007-08-24 20:06:47 +00001290// StmtExpr
1291Stmt::child_iterator StmtExpr::child_begin() {
1292 return reinterpret_cast<Stmt**>(&SubStmt);
1293}
Ted Kremenek1237c672007-08-24 20:06:47 +00001294Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001295 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001296}
1297
1298// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001299Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1300 return child_iterator();
1301}
1302
1303Stmt::child_iterator TypesCompatibleExpr::child_end() {
1304 return child_iterator();
1305}
Ted Kremenek1237c672007-08-24 20:06:47 +00001306
1307// ChooseExpr
1308Stmt::child_iterator ChooseExpr::child_begin() {
1309 return reinterpret_cast<Stmt**>(&SubExprs);
1310}
1311
1312Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001313 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001314}
1315
Nate Begemane2ce1d92008-01-17 17:46:27 +00001316// OverloadExpr
1317Stmt::child_iterator OverloadExpr::child_begin() {
1318 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1319}
1320Stmt::child_iterator OverloadExpr::child_end() {
Nate Begeman67295d02008-01-30 20:50:20 +00001321 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
Nate Begemane2ce1d92008-01-17 17:46:27 +00001322}
1323
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001324// VAArgExpr
1325Stmt::child_iterator VAArgExpr::child_begin() {
1326 return reinterpret_cast<Stmt**>(&Val);
1327}
1328
1329Stmt::child_iterator VAArgExpr::child_end() {
1330 return reinterpret_cast<Stmt**>(&Val)+1;
1331}
1332
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001333// InitListExpr
1334Stmt::child_iterator InitListExpr::child_begin() {
1335 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1336}
1337Stmt::child_iterator InitListExpr::child_end() {
1338 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1339}
1340
Ted Kremenek1237c672007-08-24 20:06:47 +00001341// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001342Stmt::child_iterator ObjCStringLiteral::child_begin() {
1343 return child_iterator();
1344}
1345Stmt::child_iterator ObjCStringLiteral::child_end() {
1346 return child_iterator();
1347}
Ted Kremenek1237c672007-08-24 20:06:47 +00001348
1349// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001350Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1351Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001352
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001353// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001354Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1355 return child_iterator();
1356}
1357Stmt::child_iterator ObjCSelectorExpr::child_end() {
1358 return child_iterator();
1359}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001360
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001361// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001362Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1363 return child_iterator();
1364}
1365Stmt::child_iterator ObjCProtocolExpr::child_end() {
1366 return child_iterator();
1367}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001368
Steve Naroff563477d2007-09-18 23:55:05 +00001369// ObjCMessageExpr
1370Stmt::child_iterator ObjCMessageExpr::child_begin() {
1371 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1372}
1373Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff68d331a2007-09-27 14:38:14 +00001374 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroff563477d2007-09-18 23:55:05 +00001375}
1376