blob: 58541af07be3bab87d84c6b0bce60f0929304dd2 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000016#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000019#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000020#include "clang/AST/StmtVisitor.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000021#include "clang/Basic/TargetInfo.h"
Chris Lattner1b926492006-08-23 06:42:10 +000022using namespace clang;
23
Chris Lattner0eedafe2006-08-24 04:56:27 +000024//===----------------------------------------------------------------------===//
25// Primary Expressions.
26//===----------------------------------------------------------------------===//
27
Chris Lattnera0173132008-06-07 22:13:43 +000028/// getValueAsApproximateDouble - This returns the value as an inaccurate
29/// double. Note that this may cause loss of precision, but is useful for
30/// debugging dumps, etc.
31double FloatingLiteral::getValueAsApproximateDouble() const {
32 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +000033 bool ignored;
34 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
35 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +000036 return V.convertToDouble();
37}
38
39
Steve Naroff408451b2007-02-26 22:17:12 +000040StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
Steve Naroff53f07dc2007-05-17 21:49:33 +000041 bool Wide, QualType t, SourceLocation firstLoc,
42 SourceLocation lastLoc) :
Steve Narofff1e53692007-03-23 22:27:02 +000043 Expr(StringLiteralClass, t) {
Steve Naroffdf7855b2007-02-21 23:46:25 +000044 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerd3e98952006-10-06 05:22:26 +000045 char *AStrData = new char[byteLength];
46 memcpy(AStrData, strData, byteLength);
47 StrData = AStrData;
48 ByteLength = byteLength;
Chris Lattner882f7882006-11-04 18:52:07 +000049 IsWide = Wide;
Steve Naroff53f07dc2007-05-17 21:49:33 +000050 firstTokLoc = firstLoc;
51 lastTokLoc = lastLoc;
Chris Lattnerd3e98952006-10-06 05:22:26 +000052}
53
Steve Naroffdf7855b2007-02-21 23:46:25 +000054StringLiteral::~StringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +000055 delete[] StrData;
56}
57
Chris Lattner15768702006-11-05 23:54:51 +000058bool UnaryOperator::isPostfix(Opcode Op) {
59 switch (Op) {
60 case PostInc:
61 case PostDec:
62 return true;
63 default:
64 return false;
65 }
66}
67
Ted Kremenek16923422008-07-23 22:18:43 +000068bool UnaryOperator::isPrefix(Opcode Op) {
69 switch (Op) {
70 case PreInc:
71 case PreDec:
72 return true;
73 default:
74 return false;
75 }
76}
77
Chris Lattner1b926492006-08-23 06:42:10 +000078/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
79/// corresponds to, e.g. "sizeof" or "[pre]++".
80const char *UnaryOperator::getOpcodeStr(Opcode Op) {
81 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +000082 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +000083 case PostInc: return "++";
84 case PostDec: return "--";
85 case PreInc: return "++";
86 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +000087 case AddrOf: return "&";
88 case Deref: return "*";
89 case Plus: return "+";
90 case Minus: return "-";
91 case Not: return "~";
92 case LNot: return "!";
93 case Real: return "__real";
94 case Imag: return "__imag";
Chris Lattner26115ac2006-08-24 06:10:04 +000095 case SizeOf: return "sizeof";
96 case AlignOf: return "alignof";
Chris Lattnerc52b1182006-10-25 05:45:55 +000097 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +000098 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +000099 }
100}
101
Chris Lattner0eedafe2006-08-24 04:56:27 +0000102//===----------------------------------------------------------------------===//
103// Postfix Operators.
104//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000105
Nate Begeman1e36a852008-01-17 17:46:27 +0000106
Steve Naroff509fe022007-05-17 01:16:00 +0000107CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000108 SourceLocation rparenloc)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000109 : Expr(CallExprClass, t), NumArgs(numargs) {
Ted Kremenek08e17112008-06-17 02:43:46 +0000110 SubExprs = new Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000111 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000112 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000113 SubExprs[i+ARGS_START] = args[i];
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000114 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000115}
116
Chris Lattnere4407ed2007-12-28 05:25:02 +0000117/// setNumArgs - This changes the number of arguments present in this call.
118/// Any orphaned expressions are deleted by this, and any new operands are set
119/// to null.
120void CallExpr::setNumArgs(unsigned NumArgs) {
121 // No change, just return.
122 if (NumArgs == getNumArgs()) return;
123
124 // If shrinking # arguments, just delete the extras and forgot them.
125 if (NumArgs < getNumArgs()) {
126 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
127 delete getArg(i);
128 this->NumArgs = NumArgs;
129 return;
130 }
131
132 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek08e17112008-06-17 02:43:46 +0000133 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000134 // Copy over args.
135 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
136 NewSubExprs[i] = SubExprs[i];
137 // Null out new args.
138 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
139 NewSubExprs[i] = 0;
140
141 delete[] SubExprs;
142 SubExprs = NewSubExprs;
143 this->NumArgs = NumArgs;
144}
145
Chris Lattner01ff98a2008-10-06 05:00:53 +0000146/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
147/// not, return 0.
148unsigned CallExpr::isBuiltinCall() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000149 // All simple function calls (e.g. func()) are implicitly cast to pointer to
150 // function. As a result, we try and obtain the DeclRefExpr from the
151 // ImplicitCastExpr.
152 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
153 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000154 return 0;
155
Steve Narofff6e3b3292008-01-31 01:07:12 +0000156 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
157 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000158 return 0;
159
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000160 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
161 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000162 return 0;
163
164 return FDecl->getIdentifier()->getBuiltinID();
165}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000166
Chris Lattner01ff98a2008-10-06 05:00:53 +0000167
Chris Lattner1b926492006-08-23 06:42:10 +0000168/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
169/// corresponds to, e.g. "<<=".
170const char *BinaryOperator::getOpcodeStr(Opcode Op) {
171 switch (Op) {
172 default: assert(0 && "Unknown binary operator");
173 case Mul: return "*";
174 case Div: return "/";
175 case Rem: return "%";
176 case Add: return "+";
177 case Sub: return "-";
178 case Shl: return "<<";
179 case Shr: return ">>";
180 case LT: return "<";
181 case GT: return ">";
182 case LE: return "<=";
183 case GE: return ">=";
184 case EQ: return "==";
185 case NE: return "!=";
186 case And: return "&";
187 case Xor: return "^";
188 case Or: return "|";
189 case LAnd: return "&&";
190 case LOr: return "||";
191 case Assign: return "=";
192 case MulAssign: return "*=";
193 case DivAssign: return "/=";
194 case RemAssign: return "%=";
195 case AddAssign: return "+=";
196 case SubAssign: return "-=";
197 case ShlAssign: return "<<=";
198 case ShrAssign: return ">>=";
199 case AndAssign: return "&=";
200 case XorAssign: return "^=";
201 case OrAssign: return "|=";
202 case Comma: return ",";
203 }
204}
Steve Naroff47500512007-04-19 23:00:49 +0000205
Anders Carlsson4692db02007-08-31 04:56:16 +0000206InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000207 Expr **initExprs, unsigned numInits,
208 SourceLocation rbraceloc, bool hadDesignators)
Steve Naroff20348b52008-05-01 02:04:18 +0000209 : Expr(InitListExprClass, QualType()),
Chris Lattner07d754a2008-10-26 23:43:26 +0000210 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), HadDesignators(hadDesignators) {
211
212 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000213}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000214
Steve Naroff991e99d2008-09-04 15:31:07 +0000215/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000216///
217const FunctionType *BlockExpr::getFunctionType() const {
218 return getType()->getAsBlockPointerType()->
219 getPointeeType()->getAsFunctionType();
220}
221
Steve Naroff415d3d52008-10-08 17:01:13 +0000222SourceLocation BlockExpr::getCaretLocation() const {
223 return TheBlock->getCaretLocation();
224}
225const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
226Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
227
228
Chris Lattner1ec5f562007-06-27 05:38:08 +0000229//===----------------------------------------------------------------------===//
230// Generic Expression Routines
231//===----------------------------------------------------------------------===//
232
233/// hasLocalSideEffect - Return true if this immediate expression has side
234/// effects, not counting any sub-expressions.
235bool Expr::hasLocalSideEffect() const {
236 switch (getStmtClass()) {
237 default:
238 return false;
239 case ParenExprClass:
240 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
241 case UnaryOperatorClass: {
242 const UnaryOperator *UO = cast<UnaryOperator>(this);
243
244 switch (UO->getOpcode()) {
245 default: return false;
246 case UnaryOperator::PostInc:
247 case UnaryOperator::PostDec:
248 case UnaryOperator::PreInc:
249 case UnaryOperator::PreDec:
250 return true; // ++/--
251
Chris Lattnera44d1162007-06-27 05:58:59 +0000252 case UnaryOperator::Deref:
253 // Dereferencing a volatile pointer is a side-effect.
254 return getType().isVolatileQualified();
255 case UnaryOperator::Real:
256 case UnaryOperator::Imag:
257 // accessing a piece of a volatile complex is a side-effect.
258 return UO->getSubExpr()->getType().isVolatileQualified();
259
Chris Lattner1ec5f562007-06-27 05:38:08 +0000260 case UnaryOperator::Extension:
261 return UO->getSubExpr()->hasLocalSideEffect();
262 }
263 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000264 case BinaryOperatorClass: {
265 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
266 // Consider comma to have side effects if the LHS and RHS both do.
267 if (BinOp->getOpcode() == BinaryOperator::Comma)
268 return BinOp->getLHS()->hasLocalSideEffect() &&
269 BinOp->getRHS()->hasLocalSideEffect();
270
271 return BinOp->isAssignmentOp();
272 }
Chris Lattner86928112007-08-25 02:00:02 +0000273 case CompoundAssignOperatorClass:
Chris Lattnerd8c9fc52007-08-25 01:55:00 +0000274 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000275
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000276 case ConditionalOperatorClass: {
277 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
278 return Exp->getCond()->hasLocalSideEffect()
279 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
280 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
281 }
282
Chris Lattnera44d1162007-06-27 05:58:59 +0000283 case MemberExprClass:
Chris Lattner1ec5f562007-06-27 05:38:08 +0000284 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000285 // If the base pointer or element is to a volatile pointer/field, accessing
286 // if is a side effect.
287 return getType().isVolatileQualified();
Eli Friedman824f8c12008-05-27 15:24:04 +0000288
Chris Lattner1ec5f562007-06-27 05:38:08 +0000289 case CallExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000290 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
291 // should warn.
Chris Lattner1ec5f562007-06-27 05:38:08 +0000292 return true;
Chris Lattnere6d9ca52007-09-26 22:06:30 +0000293 case ObjCMessageExprClass:
294 return true;
Chris Lattner944d3062008-07-26 19:51:01 +0000295 case StmtExprClass: {
296 // Statement exprs don't logically have side effects themselves, but are
297 // sometimes used in macros in ways that give them a type that is unused.
298 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
299 // however, if the result of the stmt expr is dead, we don't want to emit a
300 // warning.
301 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
302 if (!CS->body_empty())
303 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
304 return E->hasLocalSideEffect();
305 return false;
306 }
Douglas Gregorf19b2312008-10-28 15:36:24 +0000307 case CStyleCastExprClass:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000308 case CXXFunctionalCastExprClass:
Chris Lattner1ec5f562007-06-27 05:38:08 +0000309 // If this is a cast to void, check the operand. Otherwise, the result of
310 // the cast is unused.
311 if (getType()->isVoidType())
312 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
313 return false;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000314
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000315 case ImplicitCastExprClass:
316 // Check the operand, since implicit casts are inserted by Sema
317 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect();
318
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000319 case CXXDefaultArgExprClass:
320 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect();
Chris Lattner1ec5f562007-06-27 05:38:08 +0000321 }
322}
323
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000324/// DeclCanBeLvalue - Determine whether the given declaration can be
325/// an lvalue. This is a helper routine for isLvalue.
326static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Argyrios Kyrtzidis2147e7d2008-10-22 21:00:29 +0000327 return isa<VarDecl>(Decl) || isa<CXXFieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000328 // C++ 3.10p2: An lvalue refers to an object or function.
329 (Ctx.getLangOptions().CPlusPlus &&
330 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
331}
332
Steve Naroff475cca02007-05-14 17:19:29 +0000333/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
334/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000335/// - name, where name must be a variable
336/// - e[i]
337/// - (e), where e must be an lvalue
338/// - e.name, where e must be an lvalue
339/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000340/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000341/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +0000342/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +0000343/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +0000344///
Chris Lattner67315442008-07-26 21:30:36 +0000345Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor9a657932008-10-21 23:43:52 +0000346 // first, check the type (C99 6.3.2.1). Expressions with function
347 // type in C are not lvalues, but they can be lvalues in C++.
348 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Steve Naroff9358c712007-05-27 23:58:33 +0000349 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +0000350
Steve Naroff1018ea32008-02-10 01:39:04 +0000351 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner67315442008-07-26 21:30:36 +0000352 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +0000353 return LV_IncompleteVoidType;
354
Douglas Gregor9a657932008-10-21 23:43:52 +0000355 /// FIXME: Expressions can't have reference type, so the following
356 /// isn't needed.
Chris Lattner0f4faa12007-07-21 05:33:26 +0000357 if (TR->isReferenceType()) // C++ [expr]
Bill Wendlingdfc81072007-07-17 03:52:31 +0000358 return LV_Valid;
359
Steve Naroff5dd642e2007-05-14 18:14:51 +0000360 // the type looks fine, now check the expression
Steve Naroff47500512007-04-19 23:00:49 +0000361 switch (getStmtClass()) {
Steve Naroff475cca02007-05-14 17:19:29 +0000362 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7a9a38a2007-11-30 22:47:59 +0000363 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +0000364 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +0000365 // For vectors, make sure base is an lvalue (i.e. not a function call).
366 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +0000367 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +0000368 return LV_Valid;
Chris Lattner5696e7b2008-06-17 18:05:57 +0000369 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000370 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
371 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +0000372 return LV_Valid;
373 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +0000374 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +0000375 case BlockDeclRefExprClass: {
376 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +0000377 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +0000378 return LV_Valid;
379 break;
380 }
Anton Korobeynikovb76cda02007-07-12 15:26:50 +0000381 case MemberExprClass: { // C99 6.5.2.3p4
Steve Naroff47500512007-04-19 23:00:49 +0000382 const MemberExpr *m = cast<MemberExpr>(this);
Chris Lattner67315442008-07-26 21:30:36 +0000383 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +0000384 }
Chris Lattner595db862007-10-30 22:53:42 +0000385 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +0000386 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +0000387 return LV_Valid; // C99 6.5.3p4
388
389 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +0000390 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
391 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +0000392 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Steve Naroff9358c712007-05-27 23:58:33 +0000393 break;
Steve Naroff475cca02007-05-14 17:19:29 +0000394 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +0000395 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor6b754842008-10-28 00:22:11 +0000396 case CallExprClass: {
397 // C++ [expr.call]p10:
398 // A function call is an lvalue if and only if the result type
399 // is a reference.
Douglas Gregor8e1cf602008-10-29 00:13:59 +0000400 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor6b754842008-10-28 00:22:11 +0000401 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
402 if (const FunctionType *FnType
403 = FnTypePtr->getPointeeType()->getAsFunctionType())
404 if (FnType->getResultType()->isReferenceType())
405 return LV_Valid;
406
407 break;
408 }
Steve Naroff2644aaf2007-12-05 04:00:10 +0000409 case CompoundLiteralExprClass: // C99 6.5.2.5p5
410 return LV_Valid;
Nate Begemance4d7fc2008-04-18 23:10:10 +0000411 case ExtVectorElementExprClass:
412 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +0000413 return LV_DuplicateVectorComponents;
414 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +0000415 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
416 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +0000417 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
418 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +0000419 case PredefinedExprClass:
420 return (cast<PredefinedExpr>(this)->getIdentType()
421 == PredefinedExpr::CXXThis
Chris Lattner2841fc02008-07-25 23:30:42 +0000422 ? LV_InvalidExpression : LV_Valid);
Douglas Gregor6b754842008-10-28 00:22:11 +0000423 case VAArgExprClass:
424 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000425 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +0000426 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis0fdbd6c2008-09-11 04:22:26 +0000427 case CXXConditionDeclExprClass:
428 return LV_Valid;
Douglas Gregorf19b2312008-10-28 15:36:24 +0000429 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +0000430 case CXXFunctionalCastExprClass:
431 case CXXStaticCastExprClass:
432 case CXXDynamicCastExprClass:
433 case CXXReinterpretCastExprClass:
434 case CXXConstCastExprClass:
435 // The result of an explicit cast is an lvalue if the type we are
436 // casting to is a reference type. See C++ [expr.cast]p1,
437 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
438 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
439 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
440 return LV_Valid;
441 break;
Steve Naroff9358c712007-05-27 23:58:33 +0000442 default:
443 break;
Steve Naroff47500512007-04-19 23:00:49 +0000444 }
Steve Naroff9358c712007-05-27 23:58:33 +0000445 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +0000446}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000447
Steve Naroff475cca02007-05-14 17:19:29 +0000448/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
449/// does not have an incomplete type, does not have a const-qualified type, and
450/// if it is a structure or union, does not have any member (including,
451/// recursively, any member or element of all contained aggregates or unions)
452/// with a const-qualified type.
Chris Lattner67315442008-07-26 21:30:36 +0000453Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
454 isLvalueResult lvalResult = isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +0000455
456 switch (lvalResult) {
Douglas Gregor293a3c62008-10-22 00:03:08 +0000457 case LV_Valid:
458 // C++ 3.10p11: Functions cannot be modified, but pointers to
459 // functions can be modifiable.
460 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
461 return MLV_NotObjectType;
462 break;
463
Chris Lattner1ec5f562007-06-27 05:38:08 +0000464 case LV_NotObjectType: return MLV_NotObjectType;
465 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +0000466 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000467 case LV_InvalidExpression: return MLV_InvalidExpression;
Steve Naroff9358c712007-05-27 23:58:33 +0000468 }
Chris Lattner7adf0762008-08-04 07:31:14 +0000469
470 QualType CT = Ctx.getCanonicalType(getType());
471
472 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +0000473 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +0000474 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +0000475 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +0000476 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +0000477 return MLV_IncompleteType;
478
Chris Lattner7adf0762008-08-04 07:31:14 +0000479 if (const RecordType *r = CT->getAsRecordType()) {
Steve Naroff9358c712007-05-27 23:58:33 +0000480 if (r->hasConstFields())
481 return MLV_ConstQualified;
482 }
Steve Naroffba756cb2008-09-26 14:41:28 +0000483 // The following is illegal:
484 // void takeclosure(void (^C)(void));
485 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
486 //
487 if (getStmtClass() == BlockDeclRefExprClass) {
488 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
489 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
490 return MLV_NotBlockQualified;
491 }
Steve Naroff9358c712007-05-27 23:58:33 +0000492 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +0000493}
494
Ted Kremenek41dd9d02008-02-27 18:39:48 +0000495/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattnerda22eec2007-11-27 21:35:27 +0000496/// duration. This means that the address of this expression is a link-time
497/// constant.
Ted Kremenek41dd9d02008-02-27 18:39:48 +0000498bool Expr::hasGlobalStorage() const {
Chris Lattner2f72c422007-11-13 18:05:45 +0000499 switch (getStmtClass()) {
500 default:
501 return false;
Chris Lattnerda22eec2007-11-27 21:35:27 +0000502 case ParenExprClass:
Ted Kremenek41dd9d02008-02-27 18:39:48 +0000503 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattnerda22eec2007-11-27 21:35:27 +0000504 case ImplicitCastExprClass:
Ted Kremenek41dd9d02008-02-27 18:39:48 +0000505 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffd32419d2008-01-14 18:19:28 +0000506 case CompoundLiteralExprClass:
507 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattner2f72c422007-11-13 18:05:45 +0000508 case DeclRefExprClass: {
509 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
510 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek41dd9d02008-02-27 18:39:48 +0000511 return VD->hasGlobalStorage();
Seo Sanghyeon055c94f2008-04-04 09:45:30 +0000512 if (isa<FunctionDecl>(D))
513 return true;
Chris Lattner2f72c422007-11-13 18:05:45 +0000514 return false;
515 }
Chris Lattnerd0d8d4e2007-11-28 04:30:09 +0000516 case MemberExprClass: {
Chris Lattner2f72c422007-11-13 18:05:45 +0000517 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek41dd9d02008-02-27 18:39:48 +0000518 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerd0d8d4e2007-11-28 04:30:09 +0000519 }
Chris Lattnerda22eec2007-11-27 21:35:27 +0000520 case ArraySubscriptExprClass:
Ted Kremenek41dd9d02008-02-27 18:39:48 +0000521 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner6307f192008-08-10 01:53:14 +0000522 case PredefinedExprClass:
Chris Lattnera81a0272008-01-12 08:14:25 +0000523 return true;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000524 case CXXDefaultArgExprClass:
525 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner2f72c422007-11-13 18:05:45 +0000526 }
527}
528
Ted Kremenekfff70962008-01-17 16:57:34 +0000529Expr* Expr::IgnoreParens() {
530 Expr* E = this;
531 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
532 E = P->getSubExpr();
533
534 return E;
535}
536
Chris Lattnerf2660962008-02-13 01:02:39 +0000537/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
538/// or CastExprs or ImplicitCastExprs, returning their operand.
539Expr *Expr::IgnoreParenCasts() {
540 Expr *E = this;
541 while (true) {
542 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
543 E = P->getSubExpr();
544 else if (CastExpr *P = dyn_cast<CastExpr>(E))
545 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +0000546 else
547 return E;
548 }
549}
550
551
Steve Naroffb03f5942007-09-02 20:30:18 +0000552bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroffb03f5942007-09-02 20:30:18 +0000553 switch (getStmtClass()) {
554 default:
555 if (Loc) *Loc = getLocStart();
556 return false;
557 case ParenExprClass:
558 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
559 case StringLiteralClass:
Steve Naroff8570fde2007-11-09 15:00:03 +0000560 case ObjCStringLiteralClass:
Steve Naroffb03f5942007-09-02 20:30:18 +0000561 case FloatingLiteralClass:
562 case IntegerLiteralClass:
563 case CharacterLiteralClass:
564 case ImaginaryLiteralClass:
Anders Carlssonf94cd1f2007-10-17 00:52:43 +0000565 case TypesCompatibleExprClass:
566 case CXXBoolLiteralExprClass:
Anders Carlsson0fbd4462008-08-23 18:49:32 +0000567 case AddrLabelExprClass:
Chris Lattner9c0f9fe2007-10-18 00:20:32 +0000568 return true;
Steve Naroffb03f5942007-09-02 20:30:18 +0000569 case CallExprClass: {
570 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattnercb136912008-10-06 06:49:02 +0000571
572 // Allow any constant foldable calls to builtins.
573 if (CE->isBuiltinCall() && CE->isEvaluatable(Ctx))
Steve Narofff6e3b3292008-01-31 01:07:12 +0000574 return true;
Chris Lattnercb136912008-10-06 06:49:02 +0000575
Steve Naroffb03f5942007-09-02 20:30:18 +0000576 if (Loc) *Loc = getLocStart();
577 return false;
578 }
Chris Lattnerc3ebf292007-11-01 02:45:17 +0000579 case DeclRefExprClass: {
580 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
581 // Accept address of function.
582 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner9c0f9fe2007-10-18 00:20:32 +0000583 return true;
Steve Naroffb03f5942007-09-02 20:30:18 +0000584 if (Loc) *Loc = getLocStart();
Chris Lattner2f72c422007-11-13 18:05:45 +0000585 if (isa<VarDecl>(D))
586 return TR->isArrayType();
Steve Naroffb03f5942007-09-02 20:30:18 +0000587 return false;
Chris Lattnerc3ebf292007-11-01 02:45:17 +0000588 }
Steve Naroff66a26042008-01-09 00:05:37 +0000589 case CompoundLiteralExprClass:
590 if (Loc) *Loc = getLocStart();
591 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begeman43eec492008-01-25 05:34:48 +0000592 // Allow "(vector type){2,4}" since the elements are all constant.
593 return TR->isArrayType() || TR->isVectorType();
Steve Naroffb03f5942007-09-02 20:30:18 +0000594 case UnaryOperatorClass: {
595 const UnaryOperator *Exp = cast<UnaryOperator>(this);
596
Chris Lattner2f72c422007-11-13 18:05:45 +0000597 // C99 6.6p9
Chris Lattner255e1322007-12-11 23:11:17 +0000598 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
Ted Kremenek41dd9d02008-02-27 18:39:48 +0000599 if (!Exp->getSubExpr()->hasGlobalStorage()) {
Chris Lattner255e1322007-12-11 23:11:17 +0000600 if (Loc) *Loc = getLocStart();
601 return false;
602 }
603 return true;
604 }
Chris Lattner2f72c422007-11-13 18:05:45 +0000605
Steve Naroffb03f5942007-09-02 20:30:18 +0000606 // Get the operand value. If this is sizeof/alignof, do not evalute the
607 // operand. This affects C99 6.6p3.
Steve Naroff98f72032008-01-10 22:15:12 +0000608 if (!Exp->isSizeOfAlignOfOp() &&
609 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroffb03f5942007-09-02 20:30:18 +0000610 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
611 return false;
612
613 switch (Exp->getOpcode()) {
614 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
615 // See C99 6.6p3.
616 default:
617 if (Loc) *Loc = Exp->getOperatorLoc();
618 return false;
619 case UnaryOperator::Extension:
620 return true; // FIXME: this is wrong.
621 case UnaryOperator::SizeOf:
622 case UnaryOperator::AlignOf:
Steve Naroff98f72032008-01-10 22:15:12 +0000623 case UnaryOperator::OffsetOf:
Steve Naroffb03f5942007-09-02 20:30:18 +0000624 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedmana682d392008-02-15 12:20:59 +0000625 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattner20aad332007-12-18 07:15:40 +0000626 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroffb03f5942007-09-02 20:30:18 +0000627 return false;
Chris Lattner20aad332007-12-18 07:15:40 +0000628 }
Chris Lattner9c0f9fe2007-10-18 00:20:32 +0000629 return true;
Steve Naroffb03f5942007-09-02 20:30:18 +0000630 case UnaryOperator::LNot:
631 case UnaryOperator::Plus:
632 case UnaryOperator::Minus:
633 case UnaryOperator::Not:
Chris Lattner9c0f9fe2007-10-18 00:20:32 +0000634 return true;
Steve Naroffb03f5942007-09-02 20:30:18 +0000635 }
Steve Naroffb03f5942007-09-02 20:30:18 +0000636 }
637 case SizeOfAlignOfTypeExprClass: {
638 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
639 // alignof always evaluates to a constant.
Chris Lattner3b418d82008-02-21 05:45:29 +0000640 if (Exp->isSizeOf() && !Exp->getArgumentType()->isVoidType() &&
641 !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattner20aad332007-12-18 07:15:40 +0000642 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroffb03f5942007-09-02 20:30:18 +0000643 return false;
Chris Lattner20aad332007-12-18 07:15:40 +0000644 }
Chris Lattner9c0f9fe2007-10-18 00:20:32 +0000645 return true;
Steve Naroffb03f5942007-09-02 20:30:18 +0000646 }
647 case BinaryOperatorClass: {
648 const BinaryOperator *Exp = cast<BinaryOperator>(this);
649
650 // The LHS of a constant expr is always evaluated and needed.
651 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
652 return false;
653
654 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
655 return false;
Chris Lattner9c0f9fe2007-10-18 00:20:32 +0000656 return true;
Steve Naroffb03f5942007-09-02 20:30:18 +0000657 }
658 case ImplicitCastExprClass:
Douglas Gregorf19b2312008-10-28 15:36:24 +0000659 case CStyleCastExprClass:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000660 case CXXFunctionalCastExprClass: {
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000661 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
662 SourceLocation CastLoc = getLocStart();
Steve Naroffb03f5942007-09-02 20:30:18 +0000663 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
664 if (Loc) *Loc = SubExpr->getLocStart();
665 return false;
666 }
Chris Lattner9c0f9fe2007-10-18 00:20:32 +0000667 return true;
Steve Naroffb03f5942007-09-02 20:30:18 +0000668 }
669 case ConditionalOperatorClass: {
670 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner9c0f9fe2007-10-18 00:20:32 +0000671 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson801c5c72007-11-30 19:04:31 +0000672 // Handle the GNU extension for missing LHS.
673 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner9c0f9fe2007-10-18 00:20:32 +0000674 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroffb03f5942007-09-02 20:30:18 +0000675 return false;
Chris Lattner9c0f9fe2007-10-18 00:20:32 +0000676 return true;
Steve Naroffb03f5942007-09-02 20:30:18 +0000677 }
Steve Naroff98f72032008-01-10 22:15:12 +0000678 case InitListExprClass: {
679 const InitListExpr *Exp = cast<InitListExpr>(this);
680 unsigned numInits = Exp->getNumInits();
681 for (unsigned i = 0; i < numInits; i++) {
682 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
683 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
684 return false;
685 }
686 }
687 return true;
Steve Naroffb03f5942007-09-02 20:30:18 +0000688 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000689 case CXXDefaultArgExprClass:
690 return cast<CXXDefaultArgExpr>(this)->getExpr()->isConstantExpr(Ctx, Loc);
Steve Naroff98f72032008-01-10 22:15:12 +0000691 }
Steve Naroffb03f5942007-09-02 20:30:18 +0000692}
693
Chris Lattner1f4479e2007-06-05 04:15:44 +0000694/// isIntegerConstantExpr - this recursive routine will test if an expression is
695/// an integer constant expression. Note: With the introduction of VLA's in
Steve Narofff8a28c52007-05-15 20:29:32 +0000696/// C99 the result of the sizeof operator is no longer always a constant
697/// expression. The generalization of the wording to include any subexpression
698/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
699/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopese212c3e2008-07-08 21:13:06 +0000700/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Steve Narofff8a28c52007-05-15 20:29:32 +0000701/// occurring in a context requiring a constant, would have been a constraint
702/// violation. FIXME: This routine currently implements C90 semantics.
703/// To properly implement C99 semantics this routine will need to evaluate
704/// expressions involving operators previously mentioned.
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000705
706/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
707/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +0000708///
709/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerf7ab3342007-09-26 00:47:26 +0000710/// permit this. This includes things like (int)1e1000
Chris Lattnerd7372ba2007-07-18 05:21:20 +0000711///
712/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
713/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
714/// cast+dereference.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000715bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
716 SourceLocation *Loc, bool isEvaluated) const {
Steve Naroff8eeeb132007-05-08 21:09:37 +0000717 switch (getStmtClass()) {
Steve Naroff72cada02007-05-18 00:18:52 +0000718 default:
Chris Lattner238cbc52007-06-02 04:48:48 +0000719 if (Loc) *Loc = getLocStart();
Steve Naroff8eeeb132007-05-08 21:09:37 +0000720 return false;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000721 case ParenExprClass:
722 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner0e9d6222007-07-15 23:26:56 +0000723 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000724 case IntegerLiteralClass:
725 Result = cast<IntegerLiteral>(this)->getValue();
726 break;
Chris Lattner5c4664e2007-07-15 23:32:58 +0000727 case CharacterLiteralClass: {
728 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner37e05872008-03-05 18:54:05 +0000729 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner5c4664e2007-07-15 23:32:58 +0000730 Result = CL->getValue();
Chris Lattner7d138432007-07-16 21:04:56 +0000731 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000732 break;
Chris Lattner5c4664e2007-07-15 23:32:58 +0000733 }
Anders Carlsson4f177f82008-08-23 21:12:35 +0000734 case CXXBoolLiteralExprClass: {
735 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
736 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
737 Result = BL->getValue();
738 Result.setIsUnsigned(!getType()->isSignedIntegerType());
739 break;
740 }
Argyrios Kyrtzidisce4528f2008-08-23 19:35:47 +0000741 case CXXZeroInitValueExprClass:
742 Result.clear();
743 break;
Steve Naroffa5645cb2007-08-02 04:09:23 +0000744 case TypesCompatibleExprClass: {
745 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner37e05872008-03-05 18:54:05 +0000746 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Daniel Dunbard7be95d2008-10-24 08:07:57 +0000747 // Per gcc docs "this built-in function ignores top level
748 // qualifiers". We need to use the canonical version to properly
749 // be able to strip CRV qualifiers from the type.
750 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
751 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
752 Result = Ctx.typesAreCompatible(T0.getUnqualifiedType(),
753 T1.getUnqualifiedType());
Steve Naroffb3deb2e2007-08-02 00:13:27 +0000754 break;
Steve Naroffa5645cb2007-08-02 04:09:23 +0000755 }
Steve Naroff12b04472007-08-08 22:15:55 +0000756 case CallExprClass: {
757 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner37e05872008-03-05 18:54:05 +0000758 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner86ee2862008-10-06 06:40:35 +0000759
760 // If this is a call to a builtin function, constant fold it otherwise
761 // reject it.
762 if (CE->isBuiltinCall()) {
763 APValue ResultAP;
764 if (CE->tryEvaluate(ResultAP, Ctx)) {
765 Result = ResultAP.getInt();
766 break; // It is a constant, expand it.
767 }
768 }
769
Steve Naroff12b04472007-08-08 22:15:55 +0000770 if (Loc) *Loc = getLocStart();
771 return false;
772 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000773 case DeclRefExprClass:
774 if (const EnumConstantDecl *D =
775 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
Chris Lattner9fba0282007-06-11 03:47:05 +0000776 Result = D->getInitVal();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000777 break;
778 }
779 if (Loc) *Loc = getLocStart();
780 return false;
781 case UnaryOperatorClass: {
782 const UnaryOperator *Exp = cast<UnaryOperator>(this);
783
784 // Get the operand value. If this is sizeof/alignof, do not evalute the
785 // operand. This affects C99 6.6p3.
Anders Carlssona8dc3e62008-01-29 15:56:48 +0000786 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner95688802007-08-23 21:42:50 +0000787 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000788 return false;
789
790 switch (Exp->getOpcode()) {
791 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
792 // See C99 6.6p3.
793 default:
794 if (Loc) *Loc = Exp->getOperatorLoc();
795 return false;
796 case UnaryOperator::Extension:
Chris Lattner0f0019c2007-07-18 18:38:36 +0000797 return true; // FIXME: this is wrong.
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000798 case UnaryOperator::SizeOf:
799 case UnaryOperator::AlignOf:
Chris Lattner3b418d82008-02-21 05:45:29 +0000800 // Return the result in the right width.
Chris Lattner37e05872008-03-05 18:54:05 +0000801 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner3b418d82008-02-21 05:45:29 +0000802
803 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
804 if (Exp->getSubExpr()->getType()->isVoidType()) {
805 Result = 1;
806 break;
807 }
808
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000809 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedmana682d392008-02-15 12:20:59 +0000810 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattner20aad332007-12-18 07:15:40 +0000811 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000812 return false;
Chris Lattner20aad332007-12-18 07:15:40 +0000813 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000814
Chris Lattner0f0019c2007-07-18 18:38:36 +0000815 // Get information about the size or align.
Chris Lattner7977cca2008-01-02 21:54:09 +0000816 if (Exp->getSubExpr()->getType()->isFunctionType()) {
817 // GCC extension: sizeof(function) = 1.
818 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
Chris Lattnera7944d82007-11-27 18:22:04 +0000819 } else {
Chris Lattner37e05872008-03-05 18:54:05 +0000820 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson9195e082008-02-18 07:10:45 +0000821 if (Exp->getOpcode() == UnaryOperator::AlignOf)
Chris Lattner37e05872008-03-05 18:54:05 +0000822 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType()) / CharSize;
Anders Carlsson9195e082008-02-18 07:10:45 +0000823 else
Chris Lattner37e05872008-03-05 18:54:05 +0000824 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType()) / CharSize;
Chris Lattnera7944d82007-11-27 18:22:04 +0000825 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000826 break;
827 case UnaryOperator::LNot: {
Chris Lattnerde001c182008-01-25 19:16:19 +0000828 bool Val = Result == 0;
Chris Lattner37e05872008-03-05 18:54:05 +0000829 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000830 Result = Val;
831 break;
832 }
833 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000834 break;
835 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000836 Result = -Result;
837 break;
838 case UnaryOperator::Not:
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000839 Result = ~Result;
840 break;
Anders Carlssona8dc3e62008-01-29 15:56:48 +0000841 case UnaryOperator::OffsetOf:
Daniel Dunbar8c861632008-08-28 18:42:20 +0000842 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Anders Carlssona8dc3e62008-01-29 15:56:48 +0000843 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000844 }
845 break;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000846 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000847 case SizeOfAlignOfTypeExprClass: {
848 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
Chris Lattner3b418d82008-02-21 05:45:29 +0000849
850 // Return the result in the right width.
Chris Lattner37e05872008-03-05 18:54:05 +0000851 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner3b418d82008-02-21 05:45:29 +0000852
853 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
854 if (Exp->getArgumentType()->isVoidType()) {
855 Result = 1;
856 break;
857 }
858
859 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Eli Friedmana682d392008-02-15 12:20:59 +0000860 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattner20aad332007-12-18 07:15:40 +0000861 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000862 return false;
Chris Lattner20aad332007-12-18 07:15:40 +0000863 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000864
Chris Lattner0f0019c2007-07-18 18:38:36 +0000865 // Get information about the size or align.
Chris Lattner7977cca2008-01-02 21:54:09 +0000866 if (Exp->getArgumentType()->isFunctionType()) {
867 // GCC extension: sizeof(function) = 1.
868 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson3ea23a42008-02-16 01:20:23 +0000869 } else {
Chris Lattner37e05872008-03-05 18:54:05 +0000870 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson3ea23a42008-02-16 01:20:23 +0000871 if (Exp->isSizeOf())
Chris Lattner37e05872008-03-05 18:54:05 +0000872 Result = Ctx.getTypeSize(Exp->getArgumentType()) / CharSize;
Anders Carlsson3ea23a42008-02-16 01:20:23 +0000873 else
Chris Lattner37e05872008-03-05 18:54:05 +0000874 Result = Ctx.getTypeAlign(Exp->getArgumentType()) / CharSize;
Ted Kremenek05821322007-12-17 17:38:43 +0000875 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000876 break;
877 }
878 case BinaryOperatorClass: {
879 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000880 llvm::APSInt LHS, RHS;
881
882 // Initialize result to have correct signedness and width.
883 Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())),
884 !getType()->isSignedIntegerType());
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000885
886 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000887 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000888 return false;
889
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000890 // The short-circuiting &&/|| operators don't necessarily evaluate their
891 // RHS. Make sure to pass isEvaluated down correctly.
892 if (Exp->isLogicalOp()) {
893 bool RHSEval;
894 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000895 RHSEval = LHS != 0;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000896 else {
897 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000898 RHSEval = LHS == 0;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000899 }
900
Chris Lattner0e9d6222007-07-15 23:26:56 +0000901 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000902 isEvaluated & RHSEval))
903 return false;
904 } else {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000905 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000906 return false;
907 }
908
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000909 switch (Exp->getOpcode()) {
910 default:
911 if (Loc) *Loc = getLocStart();
912 return false;
913 case BinaryOperator::Mul:
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000914 Result = LHS * RHS;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000915 break;
916 case BinaryOperator::Div:
917 if (RHS == 0) {
918 if (!isEvaluated) break;
919 if (Loc) *Loc = getLocStart();
920 return false;
921 }
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000922 Result = LHS / RHS;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000923 break;
924 case BinaryOperator::Rem:
925 if (RHS == 0) {
926 if (!isEvaluated) break;
927 if (Loc) *Loc = getLocStart();
928 return false;
929 }
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000930 Result = LHS % RHS;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000931 break;
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000932 case BinaryOperator::Add: Result = LHS + RHS; break;
933 case BinaryOperator::Sub: Result = LHS - RHS; break;
Chris Lattner901ae1f2007-06-08 21:54:26 +0000934 case BinaryOperator::Shl:
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000935 Result = LHS <<
936 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
937 break;
Chris Lattner901ae1f2007-06-08 21:54:26 +0000938 case BinaryOperator::Shr:
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000939 Result = LHS >>
940 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Chris Lattner901ae1f2007-06-08 21:54:26 +0000941 break;
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000942 case BinaryOperator::LT: Result = LHS < RHS; break;
943 case BinaryOperator::GT: Result = LHS > RHS; break;
944 case BinaryOperator::LE: Result = LHS <= RHS; break;
945 case BinaryOperator::GE: Result = LHS >= RHS; break;
946 case BinaryOperator::EQ: Result = LHS == RHS; break;
947 case BinaryOperator::NE: Result = LHS != RHS; break;
948 case BinaryOperator::And: Result = LHS & RHS; break;
949 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
950 case BinaryOperator::Or: Result = LHS | RHS; break;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000951 case BinaryOperator::LAnd:
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000952 Result = LHS != 0 && RHS != 0;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000953 break;
954 case BinaryOperator::LOr:
Daniel Dunbarc6fdac22008-09-22 23:53:24 +0000955 Result = LHS != 0 || RHS != 0;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000956 break;
957
958 case BinaryOperator::Comma:
959 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
960 // *except* when they are contained within a subexpression that is not
961 // evaluated". Note that Assignment can never happen due to constraints
962 // on the LHS subexpr, so we don't need to check it here.
963 if (isEvaluated) {
964 if (Loc) *Loc = getLocStart();
965 return false;
966 }
967
968 // The result of the constant expr is the RHS.
969 Result = RHS;
970 return true;
971 }
972
973 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
974 break;
975 }
Chris Lattner51aff8b2007-07-15 23:54:50 +0000976 case ImplicitCastExprClass:
Douglas Gregorf19b2312008-10-28 15:36:24 +0000977 case CStyleCastExprClass:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000978 case CXXFunctionalCastExprClass: {
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000979 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
980 SourceLocation CastLoc = getLocStart();
Chris Lattner51aff8b2007-07-15 23:54:50 +0000981
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000982 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner51aff8b2007-07-15 23:54:50 +0000983 if (!SubExpr->getType()->isArithmeticType() ||
984 !getType()->isIntegerType()) {
985 if (Loc) *Loc = SubExpr->getLocStart();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000986 return false;
987 }
Chris Lattner322abe32007-09-22 19:04:13 +0000988
Chris Lattner37e05872008-03-05 18:54:05 +0000989 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner322abe32007-09-22 19:04:13 +0000990
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000991 // Handle simple integer->integer casts.
Chris Lattner51aff8b2007-07-15 23:54:50 +0000992 if (SubExpr->getType()->isIntegerType()) {
993 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000994 return false;
Chris Lattner51aff8b2007-07-15 23:54:50 +0000995
996 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner51aff8b2007-07-15 23:54:50 +0000997 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner119d81a2008-01-09 18:59:34 +0000998 if (getType()->isBooleanType()) {
999 // Conversion to bool compares against zero.
1000 Result = Result != 0;
1001 Result.zextOrTrunc(DestWidth);
1002 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner51aff8b2007-07-15 23:54:50 +00001003 Result.sextOrTrunc(DestWidth);
1004 else // If the input is unsigned, do a zero extend, noop, or truncate.
1005 Result.zextOrTrunc(DestWidth);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001006 break;
1007 }
1008
1009 // Allow floating constants that are the immediate operands of casts or that
1010 // are parenthesized.
Chris Lattner51aff8b2007-07-15 23:54:50 +00001011 const Expr *Operand = SubExpr;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001012 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
1013 Operand = PE->getSubExpr();
Chris Lattner322abe32007-09-22 19:04:13 +00001014
1015 // If this isn't a floating literal, we can't handle it.
1016 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1017 if (!FL) {
1018 if (Loc) *Loc = Operand->getLocStart();
1019 return false;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001020 }
Chris Lattner119d81a2008-01-09 18:59:34 +00001021
1022 // If the destination is boolean, compare against zero.
1023 if (getType()->isBooleanType()) {
1024 Result = !FL->getValue().isZero();
1025 Result.zextOrTrunc(DestWidth);
1026 break;
1027 }
Chris Lattner322abe32007-09-22 19:04:13 +00001028
1029 // Determine whether we are converting to unsigned or signed.
1030 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerf7ab3342007-09-26 00:47:26 +00001031
1032 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1033 // be called multiple times per AST.
Dale Johannesenc48814b2008-10-09 23:02:32 +00001034 uint64_t Space[4];
1035 bool ignored;
Chris Lattnerf7ab3342007-09-26 00:47:26 +00001036 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesenc48814b2008-10-09 23:02:32 +00001037 llvm::APFloat::rmTowardZero,
1038 &ignored);
Chris Lattner322abe32007-09-22 19:04:13 +00001039 Result = llvm::APInt(DestWidth, 4, Space);
1040 break;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001041 }
1042 case ConditionalOperatorClass: {
1043 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1044
Chris Lattner0e9d6222007-07-15 23:26:56 +00001045 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001046 return false;
1047
1048 const Expr *TrueExp = Exp->getLHS();
1049 const Expr *FalseExp = Exp->getRHS();
1050 if (Result == 0) std::swap(TrueExp, FalseExp);
1051
1052 // Evaluate the false one first, discard the result.
Anders Carlsson801c5c72007-11-30 19:04:31 +00001053 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001054 return false;
1055 // Evalute the true one, capture the result.
Anders Carlsson801c5c72007-11-30 19:04:31 +00001056 if (TrueExp &&
1057 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001058 return false;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001059 break;
1060 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001061 case CXXDefaultArgExprClass:
1062 return cast<CXXDefaultArgExpr>(this)
1063 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001064 }
1065
1066 // Cases that are valid constant exprs fall through to here.
1067 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1068 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00001069}
1070
Chris Lattner7eef9192007-05-24 01:23:49 +00001071/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1072/// integer constant expression with the value zero, or if this is one that is
1073/// cast to void*.
Chris Lattner0e9d6222007-07-15 23:26:56 +00001074bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001075 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001076 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001077 if(!Ctx.getLangOptions().CPlusPlus) {
1078 // Check that it is a cast to void*.
1079 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1080 QualType Pointee = PT->getPointeeType();
1081 if (Pointee.getCVRQualifiers() == 0 &&
1082 Pointee->isVoidType() && // to void*
1083 CE->getSubExpr()->getType()->isIntegerType()) // from int.
1084 return CE->getSubExpr()->isNullPointerConstant(Ctx);
1085 }
Steve Naroffada7d422007-05-20 17:54:12 +00001086 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001087 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1088 // Ignore the ImplicitCastExpr type entirely.
1089 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
1090 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1091 // Accept ((void*)0) as a null pointer constant, as many other
1092 // implementations do.
1093 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner58258242008-04-10 02:22:51 +00001094 } else if (const CXXDefaultArgExpr *DefaultArg
1095 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001096 // See through default argument expressions
1097 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Steve Naroff09035312008-01-14 02:53:34 +00001098 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001099
1100 // This expression must be an integer type.
1101 if (!getType()->isIntegerType())
1102 return false;
1103
Chris Lattner1abbd412007-06-08 17:58:43 +00001104 // If we have an integer constant expression, we need to *evaluate* it and
1105 // test for the value 0.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001106 llvm::APSInt Val(32);
Steve Naroff4871fe02008-01-14 16:10:57 +00001107 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001108}
Steve Narofff7a5da12007-07-28 23:10:27 +00001109
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001110/// isBitField - Return true if this expression is a bit-field.
1111bool Expr::isBitField() {
1112 Expr *E = this->IgnoreParenCasts();
1113 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
1114 return MemRef->getMemberDecl()->isBitField();
1115 return false;
1116}
1117
Nate Begemance4d7fc2008-04-18 23:10:10 +00001118unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanf322eab2008-05-09 06:41:27 +00001119 if (const VectorType *VT = getType()->getAsVectorType())
1120 return VT->getNumElements();
1121 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001122}
1123
Nate Begemanf322eab2008-05-09 06:41:27 +00001124/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001125bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroff0d595ca2007-07-30 03:29:09 +00001126 const char *compStr = Accessor.getName();
1127 unsigned length = strlen(compStr);
1128
1129 for (unsigned i = 0; i < length-1; i++) {
1130 const char *s = compStr+i;
1131 for (const char c = *s++; *s; s++)
1132 if (c == *s)
1133 return true;
1134 }
1135 return false;
1136}
Chris Lattner885b4952007-08-02 23:36:59 +00001137
Nate Begemanf322eab2008-05-09 06:41:27 +00001138/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001139void ExtVectorElementExpr::getEncodedElementAccess(
1140 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner885b4952007-08-02 23:36:59 +00001141 const char *compStr = Accessor.getName();
Nate Begemanf322eab2008-05-09 06:41:27 +00001142
1143 bool isHi = !strcmp(compStr, "hi");
1144 bool isLo = !strcmp(compStr, "lo");
1145 bool isEven = !strcmp(compStr, "e");
1146 bool isOdd = !strcmp(compStr, "o");
1147
1148 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1149 uint64_t Index;
1150
1151 if (isHi)
1152 Index = e + i;
1153 else if (isLo)
1154 Index = i;
1155 else if (isEven)
1156 Index = 2 * i;
1157 else if (isOdd)
1158 Index = 2 * i + 1;
1159 else
1160 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001161
Nate Begemand3862152008-05-13 21:03:02 +00001162 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001163 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001164}
1165
Steve Narofff73590d2007-09-27 14:38:14 +00001166// constructor for instance messages.
Steve Naroff80175062007-09-28 22:22:11 +00001167ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001168 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00001169 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001170 Expr **ArgExprs, unsigned nargs)
Steve Naroff66697aa2007-11-03 16:37:59 +00001171 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00001172 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001173 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00001174 SubExprs = new Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00001175 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001176 if (NumArgs) {
1177 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00001178 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1179 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001180 LBracloc = LBrac;
1181 RBracloc = RBrac;
1182}
1183
Steve Narofff73590d2007-09-27 14:38:14 +00001184// constructor for class messages.
1185// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff80175062007-09-28 22:22:11 +00001186ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001187 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00001188 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001189 Expr **ArgExprs, unsigned nargs)
Steve Naroff66697aa2007-11-03 16:37:59 +00001190 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00001191 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001192 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00001193 SubExprs = new Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001194 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001195 if (NumArgs) {
1196 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00001197 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1198 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001199 LBracloc = LBrac;
1200 RBracloc = RBrac;
1201}
1202
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001203// constructor for class messages.
1204ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1205 QualType retType, ObjCMethodDecl *mproto,
1206 SourceLocation LBrac, SourceLocation RBrac,
1207 Expr **ArgExprs, unsigned nargs)
1208: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1209MethodProto(mproto) {
1210 NumArgs = nargs;
1211 SubExprs = new Stmt*[NumArgs+1];
1212 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1213 if (NumArgs) {
1214 for (unsigned i = 0; i != NumArgs; ++i)
1215 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1216 }
1217 LBracloc = LBrac;
1218 RBracloc = RBrac;
1219}
1220
1221ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1222 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1223 switch (x & Flags) {
1224 default:
1225 assert(false && "Invalid ObjCMessageExpr.");
1226 case IsInstMeth:
1227 return ClassInfo(0, 0);
1228 case IsClsMethDeclUnknown:
1229 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1230 case IsClsMethDeclKnown: {
1231 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1232 return ClassInfo(D, D->getIdentifier());
1233 }
1234 }
1235}
1236
Chris Lattner35e564e2007-10-25 00:29:32 +00001237bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7da1b822008-08-13 23:47:13 +00001238 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00001239}
1240
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001241static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1242{
1243 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1244 QualType Ty = ME->getBase()->getType();
1245
1246 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner37e05872008-03-05 18:54:05 +00001247 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001248 FieldDecl *FD = ME->getMemberDecl();
1249
1250 // FIXME: This is linear time.
1251 unsigned i = 0, e = 0;
1252 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1253 if (RD->getMember(i) == FD)
1254 break;
1255 }
1256
1257 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1258 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1259 const Expr *Base = ASE->getBase();
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001260
Chris Lattner37e05872008-03-05 18:54:05 +00001261 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar7da1b822008-08-13 23:47:13 +00001262 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001263
1264 return size + evaluateOffsetOf(C, Base);
1265 } else if (isa<CompoundLiteralExpr>(E))
1266 return 0;
1267
1268 assert(0 && "Unknown offsetof subexpression!");
1269 return 0;
1270}
1271
1272int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1273{
1274 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1275
Chris Lattner37e05872008-03-05 18:54:05 +00001276 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek08e17112008-06-17 02:43:46 +00001277 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001278}
1279
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00001280void SizeOfAlignOfTypeExpr::Destroy(ASTContext& C) {
1281 // Override default behavior of traversing children. We do not want
1282 // to delete the type.
1283}
1284
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001285//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00001286// ExprIterator.
1287//===----------------------------------------------------------------------===//
1288
1289Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1290Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1291Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1292const Expr* ConstExprIterator::operator[](size_t idx) const {
1293 return cast<Expr>(I[idx]);
1294}
1295const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1296const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1297
1298//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001299// Child Iterators for iterating over subexpressions/substatements
1300//===----------------------------------------------------------------------===//
1301
1302// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00001303Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1304Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001305
Steve Naroffe46504b2007-11-12 14:29:37 +00001306// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001307Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1308Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00001309
Steve Naroffebf4cb42008-06-02 23:03:37 +00001310// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001311Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1312Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00001313
Chris Lattner6307f192008-08-10 01:53:14 +00001314// PredefinedExpr
1315Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1316Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001317
1318// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00001319Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1320Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001321
1322// CharacterLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00001323Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1324Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001325
1326// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00001327Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1328Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001329
Chris Lattner1c20a172007-08-26 03:42:43 +00001330// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00001331Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1332Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00001333
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001334// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00001335Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1336Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001337
1338// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001339Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1340Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001341
1342// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00001343Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1344Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001345
1346// SizeOfAlignOfTypeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00001347Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenek85e45f22007-12-14 22:52:23 +00001348 // If the type is a VLA type (and not a typedef), the size expression of the
1349 // VLA needs to be treated as an executable expression.
1350 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1351 return child_iterator(T);
1352 else
Ted Kremenek587a44f2008-10-07 23:35:42 +00001353 return child_iterator();
Ted Kremenek04746ce2007-10-18 23:28:49 +00001354}
1355Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenek587a44f2008-10-07 23:35:42 +00001356 return child_iterator();
Ted Kremenek04746ce2007-10-18 23:28:49 +00001357}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001358
1359// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00001360Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001361 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001362}
Ted Kremenek23702b62007-08-24 20:06:47 +00001363Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001364 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001365}
1366
1367// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00001368Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001369 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001370}
Ted Kremenek23702b62007-08-24 20:06:47 +00001371Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001372 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001373}
Ted Kremenek23702b62007-08-24 20:06:47 +00001374
1375// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001376Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1377Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00001378
Nate Begemance4d7fc2008-04-18 23:10:10 +00001379// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001380Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1381Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00001382
1383// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001384Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1385Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00001386
Ted Kremenek23702b62007-08-24 20:06:47 +00001387// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001388Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1389Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00001390
1391// BinaryOperator
1392Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001393 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00001394}
Ted Kremenek23702b62007-08-24 20:06:47 +00001395Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001396 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00001397}
1398
1399// ConditionalOperator
1400Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001401 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00001402}
Ted Kremenek23702b62007-08-24 20:06:47 +00001403Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001404 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00001405}
1406
1407// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00001408Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1409Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00001410
Ted Kremenek23702b62007-08-24 20:06:47 +00001411// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001412Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1413Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00001414
1415// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00001416Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1417 return child_iterator();
1418}
1419
1420Stmt::child_iterator TypesCompatibleExpr::child_end() {
1421 return child_iterator();
1422}
Ted Kremenek23702b62007-08-24 20:06:47 +00001423
1424// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001425Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1426Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00001427
Nate Begeman1e36a852008-01-17 17:46:27 +00001428// OverloadExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001429Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; }
1430Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; }
Nate Begeman1e36a852008-01-17 17:46:27 +00001431
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001432// ShuffleVectorExpr
1433Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001434 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001435}
1436Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001437 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001438}
1439
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001440// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001441Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1442Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001443
Anders Carlsson4692db02007-08-31 04:56:16 +00001444// InitListExpr
1445Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001446 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00001447}
1448Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001449 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00001450}
1451
Ted Kremenek23702b62007-08-24 20:06:47 +00001452// ObjCStringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00001453Stmt::child_iterator ObjCStringLiteral::child_begin() {
1454 return child_iterator();
1455}
1456Stmt::child_iterator ObjCStringLiteral::child_end() {
1457 return child_iterator();
1458}
Ted Kremenek23702b62007-08-24 20:06:47 +00001459
1460// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00001461Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1462Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00001463
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001464// ObjCSelectorExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00001465Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1466 return child_iterator();
1467}
1468Stmt::child_iterator ObjCSelectorExpr::child_end() {
1469 return child_iterator();
1470}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001471
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001472// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00001473Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1474 return child_iterator();
1475}
1476Stmt::child_iterator ObjCProtocolExpr::child_end() {
1477 return child_iterator();
1478}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001479
Steve Naroffd54978b2007-09-18 23:55:05 +00001480// ObjCMessageExpr
Ted Kremenekb8861a62008-05-01 17:26:20 +00001481Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001482 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00001483}
1484Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00001485 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00001486}
1487
Steve Naroffc540d662008-09-03 18:15:37 +00001488// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00001489Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1490Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00001491
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00001492Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1493Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }