blob: 88c1b236e8ece58bcc27e9b62283ee363654c62c [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
133 // We have a DeclRefExpr.
134 if (strcmp(DRE->getDecl()->getName(),
135 "__builtin___CFStringMakeConstantString") == 0)
136 return true;
137 return false;
138}
Chris Lattnerd18b3292007-12-28 05:25:02 +0000139
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000140bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
141 // The following enum mimics gcc's internal "typeclass.h" file.
142 enum gcc_type_class {
143 no_type_class = -1,
144 void_type_class, integer_type_class, char_type_class,
145 enumeral_type_class, boolean_type_class,
146 pointer_type_class, reference_type_class, offset_type_class,
147 real_type_class, complex_type_class,
148 function_type_class, method_type_class,
149 record_type_class, union_type_class,
150 array_type_class, string_type_class,
151 lang_type_class
152 };
153 Result.setIsSigned(true);
154
155 // All simple function calls (e.g. func()) are implicitly cast to pointer to
156 // function. As a result, we try and obtain the DeclRefExpr from the
157 // ImplicitCastExpr.
158 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
159 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
160 return false;
161 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
162 if (!DRE)
163 return false;
164
165 // We have a DeclRefExpr.
166 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
167 // If no argument was supplied, default to "no_type_class". This isn't
168 // ideal, however it's what gcc does.
169 Result = static_cast<uint64_t>(no_type_class);
170 if (NumArgs >= 1) {
171 QualType argType = getArg(0)->getType();
172
173 if (argType->isVoidType())
174 Result = void_type_class;
175 else if (argType->isEnumeralType())
176 Result = enumeral_type_class;
177 else if (argType->isBooleanType())
178 Result = boolean_type_class;
179 else if (argType->isCharType())
180 Result = string_type_class; // gcc doesn't appear to use char_type_class
181 else if (argType->isIntegerType())
182 Result = integer_type_class;
183 else if (argType->isPointerType())
184 Result = pointer_type_class;
185 else if (argType->isReferenceType())
186 Result = reference_type_class;
187 else if (argType->isRealType())
188 Result = real_type_class;
189 else if (argType->isComplexType())
190 Result = complex_type_class;
191 else if (argType->isFunctionType())
192 Result = function_type_class;
193 else if (argType->isStructureType())
194 Result = record_type_class;
195 else if (argType->isUnionType())
196 Result = union_type_class;
197 else if (argType->isArrayType())
198 Result = array_type_class;
199 else if (argType->isUnionType())
200 Result = union_type_class;
201 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner3ef5bc02007-11-08 17:56:40 +0000202 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000203 }
204 return true;
205 }
206 return false;
207}
208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
210/// corresponds to, e.g. "<<=".
211const char *BinaryOperator::getOpcodeStr(Opcode Op) {
212 switch (Op) {
213 default: assert(0 && "Unknown binary operator");
214 case Mul: return "*";
215 case Div: return "/";
216 case Rem: return "%";
217 case Add: return "+";
218 case Sub: return "-";
219 case Shl: return "<<";
220 case Shr: return ">>";
221 case LT: return "<";
222 case GT: return ">";
223 case LE: return "<=";
224 case GE: return ">=";
225 case EQ: return "==";
226 case NE: return "!=";
227 case And: return "&";
228 case Xor: return "^";
229 case Or: return "|";
230 case LAnd: return "&&";
231 case LOr: return "||";
232 case Assign: return "=";
233 case MulAssign: return "*=";
234 case DivAssign: return "/=";
235 case RemAssign: return "%=";
236 case AddAssign: return "+=";
237 case SubAssign: return "-=";
238 case ShlAssign: return "<<=";
239 case ShrAssign: return ">>=";
240 case AndAssign: return "&=";
241 case XorAssign: return "^=";
242 case OrAssign: return "|=";
243 case Comma: return ",";
244 }
245}
246
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000247InitListExpr::InitListExpr(SourceLocation lbraceloc,
248 Expr **initexprs, unsigned numinits,
249 SourceLocation rbraceloc)
250 : Expr(InitListExprClass, QualType())
251 , NumInits(numinits)
252 , LBraceLoc(lbraceloc)
253 , RBraceLoc(rbraceloc)
254{
255 InitExprs = new Expr*[numinits];
256 for (unsigned i = 0; i != numinits; i++)
257 InitExprs[i] = initexprs[i];
258}
Reid Spencer5f016e22007-07-11 17:01:13 +0000259
260//===----------------------------------------------------------------------===//
261// Generic Expression Routines
262//===----------------------------------------------------------------------===//
263
264/// hasLocalSideEffect - Return true if this immediate expression has side
265/// effects, not counting any sub-expressions.
266bool Expr::hasLocalSideEffect() const {
267 switch (getStmtClass()) {
268 default:
269 return false;
270 case ParenExprClass:
271 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
272 case UnaryOperatorClass: {
273 const UnaryOperator *UO = cast<UnaryOperator>(this);
274
275 switch (UO->getOpcode()) {
276 default: return false;
277 case UnaryOperator::PostInc:
278 case UnaryOperator::PostDec:
279 case UnaryOperator::PreInc:
280 case UnaryOperator::PreDec:
281 return true; // ++/--
282
283 case UnaryOperator::Deref:
284 // Dereferencing a volatile pointer is a side-effect.
285 return getType().isVolatileQualified();
286 case UnaryOperator::Real:
287 case UnaryOperator::Imag:
288 // accessing a piece of a volatile complex is a side-effect.
289 return UO->getSubExpr()->getType().isVolatileQualified();
290
291 case UnaryOperator::Extension:
292 return UO->getSubExpr()->hasLocalSideEffect();
293 }
294 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000295 case BinaryOperatorClass: {
296 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
297 // Consider comma to have side effects if the LHS and RHS both do.
298 if (BinOp->getOpcode() == BinaryOperator::Comma)
299 return BinOp->getLHS()->hasLocalSideEffect() &&
300 BinOp->getRHS()->hasLocalSideEffect();
301
302 return BinOp->isAssignmentOp();
303 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000304 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000305 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000306
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000307 case ConditionalOperatorClass: {
308 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
309 return Exp->getCond()->hasLocalSideEffect()
310 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
311 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
312 }
313
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 case MemberExprClass:
315 case ArraySubscriptExprClass:
316 // If the base pointer or element is to a volatile pointer/field, accessing
317 // if is a side effect.
318 return getType().isVolatileQualified();
319
320 case CallExprClass:
321 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
322 // should warn.
323 return true;
Chris Lattnera9c01022007-09-26 22:06:30 +0000324 case ObjCMessageExprClass:
325 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000326
327 case CastExprClass:
328 // If this is a cast to void, check the operand. Otherwise, the result of
329 // the cast is unused.
330 if (getType()->isVoidType())
331 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
332 return false;
333 }
334}
335
336/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
337/// incomplete type other than void. Nonarray expressions that can be lvalues:
338/// - name, where name must be a variable
339/// - e[i]
340/// - (e), where e must be an lvalue
341/// - e.name, where e must be an lvalue
342/// - e->name
343/// - *e, the type of e cannot be a function type
344/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000345/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000346/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000347///
Bill Wendlingca51c972007-07-16 07:07:56 +0000348Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000350 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 return LV_NotObjectType;
352
Steve Naroff731ec572007-07-21 13:32:03 +0000353 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000355
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000356 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000357 return LV_Valid;
358
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 // the type looks fine, now check the expression
360 switch (getStmtClass()) {
361 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000362 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
364 // For vectors, make sure base is an lvalue (i.e. not a function call).
365 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
366 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
367 return LV_Valid;
368 case DeclRefExprClass: // C99 6.5.1p2
369 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
370 return LV_Valid;
371 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000372 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 const MemberExpr *m = cast<MemberExpr>(this);
374 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000375 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000376 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000378 return LV_Valid; // C99 6.5.3p4
379
380 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
381 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
382 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 break;
384 case ParenExprClass: // C99 6.5.1p5
385 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffe6386392007-12-05 04:00:10 +0000386 case CompoundLiteralExprClass: // C99 6.5.2.5p5
387 return LV_Valid;
Chris Lattner6481a572007-08-03 17:31:20 +0000388 case OCUVectorElementExprClass:
389 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000390 return LV_DuplicateVectorComponents;
391 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000392 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
393 return LV_Valid;
Chris Lattnerfa28b302008-01-12 08:14:25 +0000394 case PreDefinedExprClass:
395 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 default:
397 break;
398 }
399 return LV_InvalidExpression;
400}
401
402/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
403/// does not have an incomplete type, does not have a const-qualified type, and
404/// if it is a structure or union, does not have any member (including,
405/// recursively, any member or element of all contained aggregates or unions)
406/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000407Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 isLvalueResult lvalResult = isLvalue();
409
410 switch (lvalResult) {
411 case LV_Valid: break;
412 case LV_NotObjectType: return MLV_NotObjectType;
413 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000414 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 case LV_InvalidExpression: return MLV_InvalidExpression;
416 }
417 if (TR.isConstQualified())
418 return MLV_ConstQualified;
419 if (TR->isArrayType())
420 return MLV_ArrayType;
421 if (TR->isIncompleteType())
422 return MLV_IncompleteType;
423
424 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
425 if (r->hasConstFields())
426 return MLV_ConstQualified;
427 }
428 return MLV_Valid;
429}
430
Chris Lattner4cc62712007-11-27 21:35:27 +0000431/// hasStaticStorage - Return true if this expression has static storage
432/// duration. This means that the address of this expression is a link-time
433/// constant.
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000434bool Expr::hasStaticStorage() const {
435 switch (getStmtClass()) {
436 default:
437 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000438 case ParenExprClass:
439 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
440 case ImplicitCastExprClass:
441 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000442 case CompoundLiteralExprClass:
443 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000444 case DeclRefExprClass: {
445 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
446 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
447 return VD->hasStaticStorage();
448 return false;
449 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000450 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000451 const MemberExpr *M = cast<MemberExpr>(this);
452 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000453 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000454 case ArraySubscriptExprClass:
455 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattnerfa28b302008-01-12 08:14:25 +0000456 case PreDefinedExprClass:
457 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000458 }
459}
460
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000461Expr* Expr::IgnoreParens() {
462 Expr* E = this;
463 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
464 E = P->getSubExpr();
465
466 return E;
467}
468
Steve Naroff38374b02007-09-02 20:30:18 +0000469bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff38374b02007-09-02 20:30:18 +0000470 switch (getStmtClass()) {
471 default:
472 if (Loc) *Loc = getLocStart();
473 return false;
474 case ParenExprClass:
475 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
476 case StringLiteralClass:
Steve Naroff5d37e322007-11-09 15:00:03 +0000477 case ObjCStringLiteralClass:
Steve Naroff38374b02007-09-02 20:30:18 +0000478 case FloatingLiteralClass:
479 case IntegerLiteralClass:
480 case CharacterLiteralClass:
481 case ImaginaryLiteralClass:
Anders Carlsson1a86b332007-10-17 00:52:43 +0000482 case TypesCompatibleExprClass:
483 case CXXBoolLiteralExprClass:
Chris Lattner2777e492007-10-18 00:20:32 +0000484 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000485 case CallExprClass: {
486 const CallExpr *CE = cast<CallExpr>(this);
487 llvm::APSInt Result(32);
Hartmut Kaiser86fd3552007-09-16 21:35:35 +0000488 Result.zextOrTrunc(
489 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff38374b02007-09-02 20:30:18 +0000490 if (CE->isBuiltinClassifyType(Result))
Chris Lattner2777e492007-10-18 00:20:32 +0000491 return true;
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000492 if (CE->isBuiltinConstantExpr())
493 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000494 if (Loc) *Loc = getLocStart();
495 return false;
496 }
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000497 case DeclRefExprClass: {
498 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
499 // Accept address of function.
500 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner2777e492007-10-18 00:20:32 +0000501 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000502 if (Loc) *Loc = getLocStart();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000503 if (isa<VarDecl>(D))
504 return TR->isArrayType();
Steve Naroff38374b02007-09-02 20:30:18 +0000505 return false;
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000506 }
Steve Naroffb8f13a82008-01-09 00:05:37 +0000507 case CompoundLiteralExprClass:
508 if (Loc) *Loc = getLocStart();
509 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemand47d4f52008-01-25 05:34:48 +0000510 // Allow "(vector type){2,4}" since the elements are all constant.
511 return TR->isArrayType() || TR->isVectorType();
Steve Naroff38374b02007-09-02 20:30:18 +0000512 case UnaryOperatorClass: {
513 const UnaryOperator *Exp = cast<UnaryOperator>(this);
514
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000515 // C99 6.6p9
Chris Lattner239c15e2007-12-11 23:11:17 +0000516 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
517 if (!Exp->getSubExpr()->hasStaticStorage()) {
518 if (Loc) *Loc = getLocStart();
519 return false;
520 }
521 return true;
522 }
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000523
Steve Naroff38374b02007-09-02 20:30:18 +0000524 // Get the operand value. If this is sizeof/alignof, do not evalute the
525 // operand. This affects C99 6.6p3.
Steve Naroffd0091aa2008-01-10 22:15:12 +0000526 if (!Exp->isSizeOfAlignOfOp() &&
527 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff38374b02007-09-02 20:30:18 +0000528 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
529 return false;
530
531 switch (Exp->getOpcode()) {
532 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
533 // See C99 6.6p3.
534 default:
535 if (Loc) *Loc = Exp->getOperatorLoc();
536 return false;
537 case UnaryOperator::Extension:
538 return true; // FIXME: this is wrong.
539 case UnaryOperator::SizeOf:
540 case UnaryOperator::AlignOf:
Steve Naroffd0091aa2008-01-10 22:15:12 +0000541 case UnaryOperator::OffsetOf:
Steve Naroff38374b02007-09-02 20:30:18 +0000542 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000543 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
544 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000545 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000546 }
Chris Lattner2777e492007-10-18 00:20:32 +0000547 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000548 case UnaryOperator::LNot:
549 case UnaryOperator::Plus:
550 case UnaryOperator::Minus:
551 case UnaryOperator::Not:
Chris Lattner2777e492007-10-18 00:20:32 +0000552 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000553 }
Steve Naroff38374b02007-09-02 20:30:18 +0000554 }
555 case SizeOfAlignOfTypeExprClass: {
556 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
557 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000558 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
559 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000560 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000561 }
Chris Lattner2777e492007-10-18 00:20:32 +0000562 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000563 }
564 case BinaryOperatorClass: {
565 const BinaryOperator *Exp = cast<BinaryOperator>(this);
566
567 // The LHS of a constant expr is always evaluated and needed.
568 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
569 return false;
570
571 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
572 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000573 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000574 }
575 case ImplicitCastExprClass:
576 case CastExprClass: {
577 const Expr *SubExpr;
578 SourceLocation CastLoc;
579 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
580 SubExpr = C->getSubExpr();
581 CastLoc = C->getLParenLoc();
582 } else {
583 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
584 CastLoc = getLocStart();
585 }
586 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
587 if (Loc) *Loc = SubExpr->getLocStart();
588 return false;
589 }
Chris Lattner2777e492007-10-18 00:20:32 +0000590 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000591 }
592 case ConditionalOperatorClass: {
593 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner2777e492007-10-18 00:20:32 +0000594 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson39073232007-11-30 19:04:31 +0000595 // Handle the GNU extension for missing LHS.
596 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner2777e492007-10-18 00:20:32 +0000597 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff38374b02007-09-02 20:30:18 +0000598 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000599 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000600 }
Steve Naroffd0091aa2008-01-10 22:15:12 +0000601 case InitListExprClass: {
602 const InitListExpr *Exp = cast<InitListExpr>(this);
603 unsigned numInits = Exp->getNumInits();
604 for (unsigned i = 0; i < numInits; i++) {
605 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
606 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
607 return false;
608 }
609 }
610 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000611 }
Steve Naroffd0091aa2008-01-10 22:15:12 +0000612 }
Steve Naroff38374b02007-09-02 20:30:18 +0000613}
614
Reid Spencer5f016e22007-07-11 17:01:13 +0000615/// isIntegerConstantExpr - this recursive routine will test if an expression is
616/// an integer constant expression. Note: With the introduction of VLA's in
617/// C99 the result of the sizeof operator is no longer always a constant
618/// expression. The generalization of the wording to include any subexpression
619/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
620/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
621/// "0 || f()" can be treated as a constant expression. In C90 this expression,
622/// occurring in a context requiring a constant, would have been a constraint
623/// violation. FIXME: This routine currently implements C90 semantics.
624/// To properly implement C99 semantics this routine will need to evaluate
625/// expressions involving operators previously mentioned.
626
627/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
628/// comma, etc
629///
630/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000631/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000632///
633/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
634/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
635/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000636bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
637 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 switch (getStmtClass()) {
639 default:
640 if (Loc) *Loc = getLocStart();
641 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 case ParenExprClass:
643 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000644 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 case IntegerLiteralClass:
646 Result = cast<IntegerLiteral>(this)->getValue();
647 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000648 case CharacterLiteralClass: {
649 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000650 Result.zextOrTrunc(
651 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000652 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000653 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000655 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000656 case TypesCompatibleExprClass: {
657 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000658 Result.zextOrTrunc(
659 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffec0550f2007-10-15 20:41:53 +0000660 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff389cecc2007-08-02 00:13:27 +0000661 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000662 }
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000663 case CallExprClass: {
664 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000665 Result.zextOrTrunc(
666 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000667 if (CE->isBuiltinClassifyType(Result))
668 break;
669 if (Loc) *Loc = getLocStart();
670 return false;
671 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 case DeclRefExprClass:
673 if (const EnumConstantDecl *D =
674 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
675 Result = D->getInitVal();
676 break;
677 }
678 if (Loc) *Loc = getLocStart();
679 return false;
680 case UnaryOperatorClass: {
681 const UnaryOperator *Exp = cast<UnaryOperator>(this);
682
683 // Get the operand value. If this is sizeof/alignof, do not evalute the
684 // operand. This affects C99 6.6p3.
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000685 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner602dafd2007-08-23 21:42:50 +0000686 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 return false;
688
689 switch (Exp->getOpcode()) {
690 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
691 // See C99 6.6p3.
692 default:
693 if (Loc) *Loc = Exp->getOperatorLoc();
694 return false;
695 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000696 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 case UnaryOperator::SizeOf:
698 case UnaryOperator::AlignOf:
699 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000700 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
701 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000703 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000704
Chris Lattner76e773a2007-07-18 18:38:36 +0000705 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000706 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000707 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
708 Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000709
710 // Get information about the size or align.
Chris Lattnerefdd1572008-01-02 21:54:09 +0000711 if (Exp->getSubExpr()->getType()->isFunctionType()) {
712 // GCC extension: sizeof(function) = 1.
713 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
714 } else if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner76e773a2007-07-18 18:38:36 +0000715 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
716 Exp->getOperatorLoc());
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000717 } else {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000718 unsigned CharSize =
719 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
720
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000721 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
722 Exp->getOperatorLoc()) / CharSize;
723 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 break;
725 case UnaryOperator::LNot: {
Chris Lattnerbf755382008-01-25 19:16:19 +0000726 bool Val = Result == 0;
Chris Lattner701e5eb2007-09-04 02:45:27 +0000727 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000728 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
729 Exp->getOperatorLoc())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 Result = Val;
731 break;
732 }
733 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 break;
735 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 Result = -Result;
737 break;
738 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 Result = ~Result;
740 break;
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000741 case UnaryOperator::OffsetOf:
742 Result = Exp->evaluateOffsetOf(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 }
744 break;
745 }
746 case SizeOfAlignOfTypeExprClass: {
747 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
748 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000749 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
750 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000752 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000753
Chris Lattner76e773a2007-07-18 18:38:36 +0000754 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000755 Result.zextOrTrunc(
756 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000757
758 // Get information about the size or align.
Chris Lattnerefdd1572008-01-02 21:54:09 +0000759 if (Exp->getArgumentType()->isFunctionType()) {
760 // GCC extension: sizeof(function) = 1.
761 Result = Exp->isSizeOf() ? 1 : 4;
762 } else if (Exp->isSizeOf()) {
Ted Kremenek060e4702007-12-17 17:38:43 +0000763 unsigned CharSize =
764 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
765
766 Result = Ctx.getTypeSize(Exp->getArgumentType(),
767 Exp->getOperatorLoc()) / CharSize;
768 }
Chris Lattner76e773a2007-07-18 18:38:36 +0000769 else
770 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremenek060e4702007-12-17 17:38:43 +0000771
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 break;
773 }
774 case BinaryOperatorClass: {
775 const BinaryOperator *Exp = cast<BinaryOperator>(this);
776
777 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000778 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 return false;
780
781 llvm::APSInt RHS(Result);
782
783 // The short-circuiting &&/|| operators don't necessarily evaluate their
784 // RHS. Make sure to pass isEvaluated down correctly.
785 if (Exp->isLogicalOp()) {
786 bool RHSEval;
787 if (Exp->getOpcode() == BinaryOperator::LAnd)
788 RHSEval = Result != 0;
789 else {
790 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
791 RHSEval = Result == 0;
792 }
793
Chris Lattner590b6642007-07-15 23:26:56 +0000794 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 isEvaluated & RHSEval))
796 return false;
797 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000798 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 return false;
800 }
801
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 switch (Exp->getOpcode()) {
803 default:
804 if (Loc) *Loc = getLocStart();
805 return false;
806 case BinaryOperator::Mul:
807 Result *= RHS;
808 break;
809 case BinaryOperator::Div:
810 if (RHS == 0) {
811 if (!isEvaluated) break;
812 if (Loc) *Loc = getLocStart();
813 return false;
814 }
815 Result /= RHS;
816 break;
817 case BinaryOperator::Rem:
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::Add: Result += RHS; break;
826 case BinaryOperator::Sub: Result -= RHS; break;
827 case BinaryOperator::Shl:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000828 Result <<=
829 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 break;
831 case BinaryOperator::Shr:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000832 Result >>=
833 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 break;
835 case BinaryOperator::LT: Result = Result < RHS; break;
836 case BinaryOperator::GT: Result = Result > RHS; break;
837 case BinaryOperator::LE: Result = Result <= RHS; break;
838 case BinaryOperator::GE: Result = Result >= RHS; break;
839 case BinaryOperator::EQ: Result = Result == RHS; break;
840 case BinaryOperator::NE: Result = Result != RHS; break;
841 case BinaryOperator::And: Result &= RHS; break;
842 case BinaryOperator::Xor: Result ^= RHS; break;
843 case BinaryOperator::Or: Result |= RHS; break;
844 case BinaryOperator::LAnd:
845 Result = Result != 0 && RHS != 0;
846 break;
847 case BinaryOperator::LOr:
848 Result = Result != 0 || RHS != 0;
849 break;
850
851 case BinaryOperator::Comma:
852 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
853 // *except* when they are contained within a subexpression that is not
854 // evaluated". Note that Assignment can never happen due to constraints
855 // on the LHS subexpr, so we don't need to check it here.
856 if (isEvaluated) {
857 if (Loc) *Loc = getLocStart();
858 return false;
859 }
860
861 // The result of the constant expr is the RHS.
862 Result = RHS;
863 return true;
864 }
865
866 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
867 break;
868 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000869 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000871 const Expr *SubExpr;
872 SourceLocation CastLoc;
873 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
874 SubExpr = C->getSubExpr();
875 CastLoc = C->getLParenLoc();
876 } else {
877 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
878 CastLoc = getLocStart();
879 }
880
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000882 if (!SubExpr->getType()->isArithmeticType() ||
883 !getType()->isIntegerType()) {
884 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 return false;
886 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000887
888 uint32_t DestWidth =
889 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
890
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000892 if (SubExpr->getType()->isIntegerType()) {
893 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000895
896 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000897 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattnerc0a356b2008-01-09 18:59:34 +0000898 if (getType()->isBooleanType()) {
899 // Conversion to bool compares against zero.
900 Result = Result != 0;
901 Result.zextOrTrunc(DestWidth);
902 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner26dc7b32007-07-15 23:54:50 +0000903 Result.sextOrTrunc(DestWidth);
904 else // If the input is unsigned, do a zero extend, noop, or truncate.
905 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 break;
907 }
908
909 // Allow floating constants that are the immediate operands of casts or that
910 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000911 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
913 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +0000914
915 // If this isn't a floating literal, we can't handle it.
916 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
917 if (!FL) {
918 if (Loc) *Loc = Operand->getLocStart();
919 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 }
Chris Lattnerc0a356b2008-01-09 18:59:34 +0000921
922 // If the destination is boolean, compare against zero.
923 if (getType()->isBooleanType()) {
924 Result = !FL->getValue().isZero();
925 Result.zextOrTrunc(DestWidth);
926 break;
927 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000928
929 // Determine whether we are converting to unsigned or signed.
930 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +0000931
932 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
933 // be called multiple times per AST.
Chris Lattner987b15d2007-09-22 19:04:13 +0000934 uint64_t Space[4];
Chris Lattnerccc213f2007-09-26 00:47:26 +0000935 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
936 llvm::APFloat::rmTowardZero);
Chris Lattner987b15d2007-09-22 19:04:13 +0000937 Result = llvm::APInt(DestWidth, 4, Space);
938 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 }
940 case ConditionalOperatorClass: {
941 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
942
Chris Lattner590b6642007-07-15 23:26:56 +0000943 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 return false;
945
946 const Expr *TrueExp = Exp->getLHS();
947 const Expr *FalseExp = Exp->getRHS();
948 if (Result == 0) std::swap(TrueExp, FalseExp);
949
950 // Evaluate the false one first, discard the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000951 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 return false;
953 // Evalute the true one, capture the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000954 if (TrueExp &&
955 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 break;
958 }
959 }
960
961 // Cases that are valid constant exprs fall through to here.
962 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
963 return true;
964}
965
Reid Spencer5f016e22007-07-11 17:01:13 +0000966/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
967/// integer constant expression with the value zero, or if this is one that is
968/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000969bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffaa58f002008-01-14 16:10:57 +0000970 // Strip off a cast to void*, if it exists.
971 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
972 // Check that it is a cast to void*.
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
974 QualType Pointee = PT->getPointeeType();
Steve Naroffaa58f002008-01-14 16:10:57 +0000975 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
976 CE->getSubExpr()->getType()->isIntegerType()) // from int.
977 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 }
Steve Naroffaa58f002008-01-14 16:10:57 +0000979 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
980 // Ignore the ImplicitCastExpr type entirely.
981 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
982 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
983 // Accept ((void*)0) as a null pointer constant, as many other
984 // implementations do.
985 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaaffbf72008-01-14 02:53:34 +0000986 }
Steve Naroffaa58f002008-01-14 16:10:57 +0000987
988 // This expression must be an integer type.
989 if (!getType()->isIntegerType())
990 return false;
991
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 // If we have an integer constant expression, we need to *evaluate* it and
993 // test for the value 0.
994 llvm::APSInt Val(32);
Steve Naroffaa58f002008-01-14 16:10:57 +0000995 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000996}
Steve Naroff31a45842007-07-28 23:10:27 +0000997
Chris Lattner6481a572007-08-03 17:31:20 +0000998unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner4d0ac882007-08-03 16:00:20 +0000999 return strlen(Accessor.getName());
1000}
1001
1002
Chris Lattnercb92a112007-08-02 21:47:28 +00001003/// getComponentType - Determine whether the components of this access are
1004/// "point" "color" or "texture" elements.
Chris Lattner6481a572007-08-03 17:31:20 +00001005OCUVectorElementExpr::ElementType
1006OCUVectorElementExpr::getElementType() const {
Steve Naroff31a45842007-07-28 23:10:27 +00001007 // derive the component type, no need to waste space.
1008 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +00001009
Chris Lattner88dca042007-08-02 22:33:49 +00001010 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
1011 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +00001012
Chris Lattner88dca042007-08-02 22:33:49 +00001013 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +00001014 "getComponentType(): Illegal accessor");
1015 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +00001016}
Steve Narofffec0b492007-07-30 03:29:09 +00001017
Chris Lattner6481a572007-08-03 17:31:20 +00001018/// containsDuplicateElements - Return true if any element access is
Chris Lattnercb92a112007-08-02 21:47:28 +00001019/// repeated.
Chris Lattner6481a572007-08-03 17:31:20 +00001020bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001021 const char *compStr = Accessor.getName();
1022 unsigned length = strlen(compStr);
1023
1024 for (unsigned i = 0; i < length-1; i++) {
1025 const char *s = compStr+i;
1026 for (const char c = *s++; *s; s++)
1027 if (c == *s)
1028 return true;
1029 }
1030 return false;
1031}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001032
1033/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattner6481a572007-08-03 17:31:20 +00001034unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001035 const char *compStr = Accessor.getName();
Chris Lattner6481a572007-08-03 17:31:20 +00001036 unsigned length = getNumElements();
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001037
1038 unsigned Result = 0;
1039
1040 while (length--) {
1041 Result <<= 2;
1042 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1043 assert(Idx != -1 && "Invalid accessor letter");
1044 Result |= Idx;
1045 }
1046 return Result;
1047}
1048
Steve Naroff68d331a2007-09-27 14:38:14 +00001049// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001050ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001051 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001052 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001053 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001054 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1055 MethodProto(mproto), ClassName(0) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001056 NumArgs = nargs;
1057 SubExprs = new Expr*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001058 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001059 if (NumArgs) {
1060 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001061 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1062 }
Steve Naroff563477d2007-09-18 23:55:05 +00001063 LBracloc = LBrac;
1064 RBracloc = RBrac;
1065}
1066
Steve Naroff68d331a2007-09-27 14:38:14 +00001067// constructor for class messages.
1068// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001069ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001070 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001071 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001072 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001073 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1074 MethodProto(mproto), ClassName(clsName) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001075 NumArgs = nargs;
1076 SubExprs = new Expr*[NumArgs+1];
Steve Naroff563477d2007-09-18 23:55:05 +00001077 SubExprs[RECEIVER] = 0;
Steve Naroff49f109c2007-11-15 13:05:42 +00001078 if (NumArgs) {
1079 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001080 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1081 }
Steve Naroff563477d2007-09-18 23:55:05 +00001082 LBracloc = LBrac;
1083 RBracloc = RBrac;
1084}
1085
Chris Lattner27437ca2007-10-25 00:29:32 +00001086
1087bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1088 llvm::APSInt CondVal(32);
1089 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1090 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1091 return CondVal != 0;
1092}
1093
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001094static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1095{
1096 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1097 QualType Ty = ME->getBase()->getType();
1098
1099 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
1100 const ASTRecordLayout &RL = C.getASTRecordLayout(RD, SourceLocation());
1101 FieldDecl *FD = ME->getMemberDecl();
1102
1103 // FIXME: This is linear time.
1104 unsigned i = 0, e = 0;
1105 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1106 if (RD->getMember(i) == FD)
1107 break;
1108 }
1109
1110 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1111 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1112 const Expr *Base = ASE->getBase();
1113 llvm::APSInt Idx(32);
1114 bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1115 assert(ICE && "Array index is not a constant integer!");
1116
1117 int64_t size = C.getTypeSize(ASE->getType(), SourceLocation());
1118 size *= Idx.getSExtValue();
1119
1120 return size + evaluateOffsetOf(C, Base);
1121 } else if (isa<CompoundLiteralExpr>(E))
1122 return 0;
1123
1124 assert(0 && "Unknown offsetof subexpression!");
1125 return 0;
1126}
1127
1128int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1129{
1130 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1131
1132 unsigned CharSize =
1133 C.Target.getCharWidth(C.getFullLoc(getOperatorLoc()));
1134
1135 return ::evaluateOffsetOf(C, Val) / CharSize;
1136}
1137
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001138//===----------------------------------------------------------------------===//
1139// Child Iterators for iterating over subexpressions/substatements
1140//===----------------------------------------------------------------------===//
1141
1142// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001143Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1144Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001145
Steve Naroff7779db42007-11-12 14:29:37 +00001146// ObjCIvarRefExpr
1147Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1148Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1149
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001150// PreDefinedExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001151Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1152Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001153
1154// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001155Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1156Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001157
1158// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001159Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1160Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001161
1162// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001163Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1164Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001165
Chris Lattner5d661452007-08-26 03:42:43 +00001166// ImaginaryLiteral
1167Stmt::child_iterator ImaginaryLiteral::child_begin() {
1168 return reinterpret_cast<Stmt**>(&Val);
1169}
1170Stmt::child_iterator ImaginaryLiteral::child_end() {
1171 return reinterpret_cast<Stmt**>(&Val)+1;
1172}
1173
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001174// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001175Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1176Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001177
1178// ParenExpr
1179Stmt::child_iterator ParenExpr::child_begin() {
1180 return reinterpret_cast<Stmt**>(&Val);
1181}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001182Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001183 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001184}
1185
1186// UnaryOperator
1187Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001188 return reinterpret_cast<Stmt**>(&Val);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001189}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001190Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001191 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001192}
1193
1194// SizeOfAlignOfTypeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001195Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001196 // If the type is a VLA type (and not a typedef), the size expression of the
1197 // VLA needs to be treated as an executable expression.
1198 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1199 return child_iterator(T);
1200 else
1201 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001202}
1203Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001204 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001205}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001206
1207// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001208Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001209 return reinterpret_cast<Stmt**>(&SubExprs);
1210}
Ted Kremenek1237c672007-08-24 20:06:47 +00001211Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001212 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001213}
1214
1215// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001216Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001217 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001218}
Ted Kremenek1237c672007-08-24 20:06:47 +00001219Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001220 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001221}
Ted Kremenek1237c672007-08-24 20:06:47 +00001222
1223// MemberExpr
1224Stmt::child_iterator MemberExpr::child_begin() {
1225 return reinterpret_cast<Stmt**>(&Base);
1226}
Ted Kremenek1237c672007-08-24 20:06:47 +00001227Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001228 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001229}
1230
1231// OCUVectorElementExpr
1232Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1233 return reinterpret_cast<Stmt**>(&Base);
1234}
Ted Kremenek1237c672007-08-24 20:06:47 +00001235Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001236 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001237}
1238
1239// CompoundLiteralExpr
1240Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1241 return reinterpret_cast<Stmt**>(&Init);
1242}
Ted Kremenek1237c672007-08-24 20:06:47 +00001243Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001244 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001245}
1246
1247// ImplicitCastExpr
1248Stmt::child_iterator ImplicitCastExpr::child_begin() {
1249 return reinterpret_cast<Stmt**>(&Op);
1250}
Ted Kremenek1237c672007-08-24 20:06:47 +00001251Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001252 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001253}
1254
1255// CastExpr
1256Stmt::child_iterator CastExpr::child_begin() {
1257 return reinterpret_cast<Stmt**>(&Op);
1258}
Ted Kremenek1237c672007-08-24 20:06:47 +00001259Stmt::child_iterator CastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001260 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001261}
1262
1263// BinaryOperator
1264Stmt::child_iterator BinaryOperator::child_begin() {
1265 return reinterpret_cast<Stmt**>(&SubExprs);
1266}
Ted Kremenek1237c672007-08-24 20:06:47 +00001267Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001268 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001269}
1270
1271// ConditionalOperator
1272Stmt::child_iterator ConditionalOperator::child_begin() {
1273 return reinterpret_cast<Stmt**>(&SubExprs);
1274}
Ted Kremenek1237c672007-08-24 20:06:47 +00001275Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001276 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001277}
1278
1279// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001280Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1281Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001282
Ted Kremenek1237c672007-08-24 20:06:47 +00001283// StmtExpr
1284Stmt::child_iterator StmtExpr::child_begin() {
1285 return reinterpret_cast<Stmt**>(&SubStmt);
1286}
Ted Kremenek1237c672007-08-24 20:06:47 +00001287Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001288 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001289}
1290
1291// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001292Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1293 return child_iterator();
1294}
1295
1296Stmt::child_iterator TypesCompatibleExpr::child_end() {
1297 return child_iterator();
1298}
Ted Kremenek1237c672007-08-24 20:06:47 +00001299
1300// ChooseExpr
1301Stmt::child_iterator ChooseExpr::child_begin() {
1302 return reinterpret_cast<Stmt**>(&SubExprs);
1303}
1304
1305Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001306 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001307}
1308
Nate Begemane2ce1d92008-01-17 17:46:27 +00001309// OverloadExpr
1310Stmt::child_iterator OverloadExpr::child_begin() {
1311 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1312}
1313Stmt::child_iterator OverloadExpr::child_end() {
Nate Begeman67295d02008-01-30 20:50:20 +00001314 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
Nate Begemane2ce1d92008-01-17 17:46:27 +00001315}
1316
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001317// VAArgExpr
1318Stmt::child_iterator VAArgExpr::child_begin() {
1319 return reinterpret_cast<Stmt**>(&Val);
1320}
1321
1322Stmt::child_iterator VAArgExpr::child_end() {
1323 return reinterpret_cast<Stmt**>(&Val)+1;
1324}
1325
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001326// InitListExpr
1327Stmt::child_iterator InitListExpr::child_begin() {
1328 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1329}
1330Stmt::child_iterator InitListExpr::child_end() {
1331 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1332}
1333
Ted Kremenek1237c672007-08-24 20:06:47 +00001334// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001335Stmt::child_iterator ObjCStringLiteral::child_begin() {
1336 return child_iterator();
1337}
1338Stmt::child_iterator ObjCStringLiteral::child_end() {
1339 return child_iterator();
1340}
Ted Kremenek1237c672007-08-24 20:06:47 +00001341
1342// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001343Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1344Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001345
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001346// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001347Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1348 return child_iterator();
1349}
1350Stmt::child_iterator ObjCSelectorExpr::child_end() {
1351 return child_iterator();
1352}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001353
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001354// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001355Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1356 return child_iterator();
1357}
1358Stmt::child_iterator ObjCProtocolExpr::child_end() {
1359 return child_iterator();
1360}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001361
Steve Naroff563477d2007-09-18 23:55:05 +00001362// ObjCMessageExpr
1363Stmt::child_iterator ObjCMessageExpr::child_begin() {
1364 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1365}
1366Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff68d331a2007-09-27 14:38:14 +00001367 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroff563477d2007-09-18 23:55:05 +00001368}
1369