blob: ce13c34012ae875a49aa146602394eaf8597f3c3 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar64789f82008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Chris Lattnere0391b22008-06-07 22:13:43 +000029/// getValueAsApproximateDouble - This returns the value as an inaccurate
30/// double. Note that this may cause loss of precision, but is useful for
31/// debugging dumps, etc.
32double FloatingLiteral::getValueAsApproximateDouble() const {
33 llvm::APFloat V = getValue();
Dale Johannesen2461f612008-10-09 23:02:32 +000034 bool ignored;
35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000037 return V.convertToDouble();
38}
39
40
Chris Lattner4b009652007-07-25 00:24:17 +000041StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
42 bool Wide, QualType t, SourceLocation firstLoc,
43 SourceLocation lastLoc) :
44 Expr(StringLiteralClass, t) {
45 // OPTIMIZE: could allocate this appended to the StringLiteral.
46 char *AStrData = new char[byteLength];
47 memcpy(AStrData, strData, byteLength);
48 StrData = AStrData;
49 ByteLength = byteLength;
50 IsWide = Wide;
51 firstTokLoc = firstLoc;
52 lastTokLoc = lastLoc;
53}
54
55StringLiteral::~StringLiteral() {
56 delete[] StrData;
57}
58
59bool UnaryOperator::isPostfix(Opcode Op) {
60 switch (Op) {
61 case PostInc:
62 case PostDec:
63 return true;
64 default:
65 return false;
66 }
67}
68
Ted Kremenek97318dd2008-07-23 22:18:43 +000069bool UnaryOperator::isPrefix(Opcode Op) {
70 switch (Op) {
71 case PreInc:
72 case PreDec:
73 return true;
74 default:
75 return false;
76 }
77}
78
Chris Lattner4b009652007-07-25 00:24:17 +000079/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
80/// corresponds to, e.g. "sizeof" or "[pre]++".
81const char *UnaryOperator::getOpcodeStr(Opcode Op) {
82 switch (Op) {
83 default: assert(0 && "Unknown unary operator");
84 case PostInc: return "++";
85 case PostDec: return "--";
86 case PreInc: return "++";
87 case PreDec: return "--";
88 case AddrOf: return "&";
89 case Deref: return "*";
90 case Plus: return "+";
91 case Minus: return "-";
92 case Not: return "~";
93 case LNot: return "!";
94 case Real: return "__real";
95 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +000096 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000097 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000098 }
99}
100
101//===----------------------------------------------------------------------===//
102// Postfix Operators.
103//===----------------------------------------------------------------------===//
104
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000105CallExpr::CallExpr(StmtClass SC, Expr *fn, Expr **args, unsigned numargs,
106 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000107 : Expr(SC, t,
108 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
109 fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
110 NumArgs(numargs) {
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000111 SubExprs = new Stmt*[numargs+1];
112 SubExprs[FN] = fn;
113 for (unsigned i = 0; i != numargs; ++i)
114 SubExprs[i+ARGS_START] = args[i];
115 RParenLoc = rparenloc;
116}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000117
Chris Lattner4b009652007-07-25 00:24:17 +0000118CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
119 SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000120 : Expr(CallExprClass, t,
121 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
122 fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
123 NumArgs(numargs) {
Ted Kremenek2719e982008-06-17 02:43:46 +0000124 SubExprs = new Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000125 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000126 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000127 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000128 RParenLoc = rparenloc;
129}
130
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000131/// setNumArgs - This changes the number of arguments present in this call.
132/// Any orphaned expressions are deleted by this, and any new operands are set
133/// to null.
134void CallExpr::setNumArgs(unsigned NumArgs) {
135 // No change, just return.
136 if (NumArgs == getNumArgs()) return;
137
138 // If shrinking # arguments, just delete the extras and forgot them.
139 if (NumArgs < getNumArgs()) {
140 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
141 delete getArg(i);
142 this->NumArgs = NumArgs;
143 return;
144 }
145
146 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000147 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000148 // Copy over args.
149 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
150 NewSubExprs[i] = SubExprs[i];
151 // Null out new args.
152 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
153 NewSubExprs[i] = 0;
154
155 delete[] SubExprs;
156 SubExprs = NewSubExprs;
157 this->NumArgs = NumArgs;
158}
159
Chris Lattnerc24915f2008-10-06 05:00:53 +0000160/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
161/// not, return 0.
162unsigned CallExpr::isBuiltinCall() const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000163 // All simple function calls (e.g. func()) are implicitly cast to pointer to
164 // function. As a result, we try and obtain the DeclRefExpr from the
165 // ImplicitCastExpr.
166 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
167 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000168 return 0;
169
Steve Naroff44aec4c2008-01-31 01:07:12 +0000170 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
171 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000172 return 0;
173
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000174 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
175 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000176 return 0;
177
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000178 if (!FDecl->getIdentifier())
179 return 0;
180
Chris Lattnerc24915f2008-10-06 05:00:53 +0000181 return FDecl->getIdentifier()->getBuiltinID();
182}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000183
Chris Lattnerc24915f2008-10-06 05:00:53 +0000184
Chris Lattner4b009652007-07-25 00:24:17 +0000185/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
186/// corresponds to, e.g. "<<=".
187const char *BinaryOperator::getOpcodeStr(Opcode Op) {
188 switch (Op) {
189 default: assert(0 && "Unknown binary operator");
190 case Mul: return "*";
191 case Div: return "/";
192 case Rem: return "%";
193 case Add: return "+";
194 case Sub: return "-";
195 case Shl: return "<<";
196 case Shr: return ">>";
197 case LT: return "<";
198 case GT: return ">";
199 case LE: return "<=";
200 case GE: return ">=";
201 case EQ: return "==";
202 case NE: return "!=";
203 case And: return "&";
204 case Xor: return "^";
205 case Or: return "|";
206 case LAnd: return "&&";
207 case LOr: return "||";
208 case Assign: return "=";
209 case MulAssign: return "*=";
210 case DivAssign: return "/=";
211 case RemAssign: return "%=";
212 case AddAssign: return "+=";
213 case SubAssign: return "-=";
214 case ShlAssign: return "<<=";
215 case ShrAssign: return ">>=";
216 case AndAssign: return "&=";
217 case XorAssign: return "^=";
218 case OrAssign: return "|=";
219 case Comma: return ",";
220 }
221}
222
Anders Carlsson762b7c72007-08-31 04:56:16 +0000223InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000224 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000225 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000226 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000227 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000228 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000229
230 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000231}
Chris Lattner4b009652007-07-25 00:24:17 +0000232
Douglas Gregorf603b472009-01-28 21:54:33 +0000233void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
234 for (unsigned Idx = NumInits, LastIdx = InitExprs.size(); Idx < LastIdx; ++Idx)
235 delete InitExprs[Idx];
236 InitExprs.resize(NumInits, 0);
237}
238
239Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
240 if (Init >= InitExprs.size()) {
241 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
242 InitExprs.back() = expr;
243 return 0;
244 }
245
246 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
247 InitExprs[Init] = expr;
248 return Result;
249}
250
Steve Naroff6f373332008-09-04 15:31:07 +0000251/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000252///
253const FunctionType *BlockExpr::getFunctionType() const {
254 return getType()->getAsBlockPointerType()->
255 getPointeeType()->getAsFunctionType();
256}
257
Steve Naroff9ac456d2008-10-08 17:01:13 +0000258SourceLocation BlockExpr::getCaretLocation() const {
259 return TheBlock->getCaretLocation();
260}
261const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
262Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
263
264
Chris Lattner4b009652007-07-25 00:24:17 +0000265//===----------------------------------------------------------------------===//
266// Generic Expression Routines
267//===----------------------------------------------------------------------===//
268
269/// hasLocalSideEffect - Return true if this immediate expression has side
270/// effects, not counting any sub-expressions.
271bool Expr::hasLocalSideEffect() const {
272 switch (getStmtClass()) {
273 default:
274 return false;
275 case ParenExprClass:
276 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
277 case UnaryOperatorClass: {
278 const UnaryOperator *UO = cast<UnaryOperator>(this);
279
280 switch (UO->getOpcode()) {
281 default: return false;
282 case UnaryOperator::PostInc:
283 case UnaryOperator::PostDec:
284 case UnaryOperator::PreInc:
285 case UnaryOperator::PreDec:
286 return true; // ++/--
287
288 case UnaryOperator::Deref:
289 // Dereferencing a volatile pointer is a side-effect.
290 return getType().isVolatileQualified();
291 case UnaryOperator::Real:
292 case UnaryOperator::Imag:
293 // accessing a piece of a volatile complex is a side-effect.
294 return UO->getSubExpr()->getType().isVolatileQualified();
295
296 case UnaryOperator::Extension:
297 return UO->getSubExpr()->hasLocalSideEffect();
298 }
299 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000300 case BinaryOperatorClass: {
301 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
302 // Consider comma to have side effects if the LHS and RHS both do.
303 if (BinOp->getOpcode() == BinaryOperator::Comma)
304 return BinOp->getLHS()->hasLocalSideEffect() &&
305 BinOp->getRHS()->hasLocalSideEffect();
306
307 return BinOp->isAssignmentOp();
308 }
Chris Lattner06078d22007-08-25 02:00:02 +0000309 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000310 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000311
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000312 case ConditionalOperatorClass: {
313 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
314 return Exp->getCond()->hasLocalSideEffect()
315 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
316 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
317 }
318
Chris Lattner4b009652007-07-25 00:24:17 +0000319 case MemberExprClass:
320 case ArraySubscriptExprClass:
321 // If the base pointer or element is to a volatile pointer/field, accessing
322 // if is a side effect.
323 return getType().isVolatileQualified();
Eli Friedman21fd0292008-05-27 15:24:04 +0000324
Chris Lattner4b009652007-07-25 00:24:17 +0000325 case CallExprClass:
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000326 case CXXOperatorCallExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000327 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
328 // should warn.
329 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000330 case ObjCMessageExprClass:
331 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000332 case StmtExprClass: {
333 // Statement exprs don't logically have side effects themselves, but are
334 // sometimes used in macros in ways that give them a type that is unused.
335 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
336 // however, if the result of the stmt expr is dead, we don't want to emit a
337 // warning.
338 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
339 if (!CS->body_empty())
340 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
341 return E->hasLocalSideEffect();
342 return false;
343 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000344 case CStyleCastExprClass:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000345 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000346 // If this is a cast to void, check the operand. Otherwise, the result of
347 // the cast is unused.
348 if (getType()->isVoidType())
349 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
350 return false;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000351
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000352 case ImplicitCastExprClass:
353 // Check the operand, since implicit casts are inserted by Sema
354 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect();
355
Chris Lattner3e254fb2008-04-08 04:40:51 +0000356 case CXXDefaultArgExprClass:
357 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000358
359 case CXXNewExprClass:
360 // FIXME: In theory, there might be new expressions that don't have side
361 // effects (e.g. a placement new with an uninitialized POD).
362 case CXXDeleteExprClass:
363 return true;
364 }
Chris Lattner4b009652007-07-25 00:24:17 +0000365}
366
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000367/// DeclCanBeLvalue - Determine whether the given declaration can be
368/// an lvalue. This is a helper routine for isLvalue.
369static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000370 // C++ [temp.param]p6:
371 // A non-type non-reference template-parameter is not an lvalue.
372 if (const NonTypeTemplateParmDecl *NTTParm
373 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
374 return NTTParm->getType()->isReferenceType();
375
Douglas Gregor8acb7272008-12-11 16:49:14 +0000376 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000377 // C++ 3.10p2: An lvalue refers to an object or function.
378 (Ctx.getLangOptions().CPlusPlus &&
379 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
380}
381
Chris Lattner4b009652007-07-25 00:24:17 +0000382/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
383/// incomplete type other than void. Nonarray expressions that can be lvalues:
384/// - name, where name must be a variable
385/// - e[i]
386/// - (e), where e must be an lvalue
387/// - e.name, where e must be an lvalue
388/// - e->name
389/// - *e, the type of e cannot be a function type
390/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000391/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000392/// - reference type [C++ [expr]]
393///
Chris Lattner25168a52008-07-26 21:30:36 +0000394Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000395 // first, check the type (C99 6.3.2.1). Expressions with function
396 // type in C are not lvalues, but they can be lvalues in C++.
397 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000398 return LV_NotObjectType;
399
Steve Naroffec7736d2008-02-10 01:39:04 +0000400 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000401 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000402 return LV_IncompleteVoidType;
403
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000404 /// FIXME: Expressions can't have reference type, so the following
405 /// isn't needed.
Chris Lattner4b009652007-07-25 00:24:17 +0000406 if (TR->isReferenceType()) // C++ [expr]
407 return LV_Valid;
408
409 // the type looks fine, now check the expression
410 switch (getStmtClass()) {
411 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000412 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000413 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
414 // For vectors, make sure base is an lvalue (i.e. not a function call).
415 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000416 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000417 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000418 case DeclRefExprClass:
419 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000420 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
421 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000422 return LV_Valid;
423 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000424 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000425 case BlockDeclRefExprClass: {
426 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000427 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000428 return LV_Valid;
429 break;
430 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000431 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000432 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000433 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
434 NamedDecl *Member = m->getMemberDecl();
435 // C++ [expr.ref]p4:
436 // If E2 is declared to have type "reference to T", then E1.E2
437 // is an lvalue.
438 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
439 if (Value->getType()->isReferenceType())
440 return LV_Valid;
441
442 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
443 if (isa<CXXClassVarDecl>(Member))
444 return LV_Valid;
445
446 // -- If E2 is a non-static data member [...]. If E1 is an
447 // lvalue, then E1.E2 is an lvalue.
448 if (isa<FieldDecl>(Member))
449 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
450
451 // -- If it refers to a static member function [...], then
452 // E1.E2 is an lvalue.
453 // -- Otherwise, if E1.E2 refers to a non-static member
454 // function [...], then E1.E2 is not an lvalue.
455 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
456 return Method->isStatic()? LV_Valid : LV_MemberFunction;
457
458 // -- If E2 is a member enumerator [...], the expression E1.E2
459 // is not an lvalue.
460 if (isa<EnumConstantDecl>(Member))
461 return LV_InvalidExpression;
462
463 // Not an lvalue.
464 return LV_InvalidExpression;
465 }
466
467 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000468 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000469 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000470 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000471 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000472 return LV_Valid; // C99 6.5.3p4
473
474 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000475 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
476 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000477 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000478
479 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
480 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
481 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
482 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000483 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000484 case ImplicitCastExprClass:
485 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
486 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000487 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000488 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000489 case BinaryOperatorClass:
490 case CompoundAssignOperatorClass: {
491 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000492
493 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
494 BinOp->getOpcode() == BinaryOperator::Comma)
495 return BinOp->getRHS()->isLvalue(Ctx);
496
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000497 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000498 return LV_InvalidExpression;
499
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000500 if (Ctx.getLangOptions().CPlusPlus)
501 // C++ [expr.ass]p1:
502 // The result of an assignment operation [...] is an lvalue.
503 return LV_Valid;
504
505
506 // C99 6.5.16:
507 // An assignment expression [...] is not an lvalue.
508 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000509 }
Nate Begemand6d2f772009-01-18 03:20:47 +0000510 // FIXME: OverloadExprClass
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000511 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000512 case CXXOperatorCallExprClass:
513 case CXXMemberCallExprClass: {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000514 // C++ [expr.call]p10:
515 // A function call is an lvalue if and only if the result type
516 // is a reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000517 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000518 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000519 CalleeType = FnTypePtr->getPointeeType();
520 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
521 if (FnType->getResultType()->isReferenceType())
522 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000523
524 break;
525 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000526 case CompoundLiteralExprClass: // C99 6.5.2.5p5
527 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000528 case ChooseExprClass:
529 // __builtin_choose_expr is an lvalue if the selected operand is.
530 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
531 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
532 else
533 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
534
Nate Begemanaf6ed502008-04-18 23:10:10 +0000535 case ExtVectorElementExprClass:
536 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000537 return LV_DuplicateVectorComponents;
538 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000539 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
540 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000541 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
542 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000543 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000544 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000545 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000546 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000547 case VAArgExprClass:
548 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000549 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000550 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000551 case CXXConditionDeclExprClass:
552 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000553 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000554 case CXXFunctionalCastExprClass:
555 case CXXStaticCastExprClass:
556 case CXXDynamicCastExprClass:
557 case CXXReinterpretCastExprClass:
558 case CXXConstCastExprClass:
559 // The result of an explicit cast is an lvalue if the type we are
560 // casting to is a reference type. See C++ [expr.cast]p1,
561 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
562 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
563 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
564 return LV_Valid;
565 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000566 case CXXTypeidExprClass:
567 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
568 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000569 default:
570 break;
571 }
572 return LV_InvalidExpression;
573}
574
575/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
576/// does not have an incomplete type, does not have a const-qualified type, and
577/// if it is a structure or union, does not have any member (including,
578/// recursively, any member or element of all contained aggregates or unions)
579/// with a const-qualified type.
Chris Lattner25168a52008-07-26 21:30:36 +0000580Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
581 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000582
583 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000584 case LV_Valid:
585 // C++ 3.10p11: Functions cannot be modified, but pointers to
586 // functions can be modifiable.
587 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
588 return MLV_NotObjectType;
589 break;
590
Chris Lattner4b009652007-07-25 00:24:17 +0000591 case LV_NotObjectType: return MLV_NotObjectType;
592 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000593 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000594 case LV_InvalidExpression:
595 // If the top level is a C-style cast, and the subexpression is a valid
596 // lvalue, then this is probably a use of the old-school "cast as lvalue"
597 // GCC extension. We don't support it, but we want to produce good
598 // diagnostics when it happens so that the user knows why.
599 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
600 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
601 return MLV_LValueCast;
602 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000603 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000604 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000605
606 QualType CT = Ctx.getCanonicalType(getType());
607
608 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000609 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000610 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000611 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000612 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000613 return MLV_IncompleteType;
614
Chris Lattnera1923f62008-08-04 07:31:14 +0000615 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000616 if (r->hasConstFields())
617 return MLV_ConstQualified;
618 }
Steve Naroff076d6cb2008-09-26 14:41:28 +0000619 // The following is illegal:
620 // void takeclosure(void (^C)(void));
621 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
622 //
623 if (getStmtClass() == BlockDeclRefExprClass) {
624 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
625 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
626 return MLV_NotBlockQualified;
627 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000628
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000629 // Assigning to an 'implicit' property?
Fariborz Jahanian48b1a132008-11-25 17:56:43 +0000630 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000631 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
632 if (KVCExpr->getSetterMethod() == 0)
633 return MLV_NoSetterProperty;
634 }
Chris Lattner4b009652007-07-25 00:24:17 +0000635 return MLV_Valid;
636}
637
Ted Kremenek5778d622008-02-27 18:39:48 +0000638/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000639/// duration. This means that the address of this expression is a link-time
640/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000641bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000642 switch (getStmtClass()) {
643 default:
644 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000645 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000646 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000647 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000648 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000649 case CompoundLiteralExprClass:
650 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000651 case DeclRefExprClass:
652 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000653 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
654 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000655 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000656 if (isa<FunctionDecl>(D))
657 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000658 return false;
659 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000660 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000661 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000662 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000663 }
Chris Lattner743ec372007-11-27 21:35:27 +0000664 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000665 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000666 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000667 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000668 case CXXDefaultArgExprClass:
669 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000670 }
671}
672
Ted Kremenek87e30c52008-01-17 16:57:34 +0000673Expr* Expr::IgnoreParens() {
674 Expr* E = this;
675 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
676 E = P->getSubExpr();
677
678 return E;
679}
680
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000681/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
682/// or CastExprs or ImplicitCastExprs, returning their operand.
683Expr *Expr::IgnoreParenCasts() {
684 Expr *E = this;
685 while (true) {
686 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
687 E = P->getSubExpr();
688 else if (CastExpr *P = dyn_cast<CastExpr>(E))
689 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000690 else
691 return E;
692 }
693}
694
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000695/// hasAnyTypeDependentArguments - Determines if any of the expressions
696/// in Exprs is type-dependent.
697bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
698 for (unsigned I = 0; I < NumExprs; ++I)
699 if (Exprs[I]->isTypeDependent())
700 return true;
701
702 return false;
703}
704
705/// hasAnyValueDependentArguments - Determines if any of the expressions
706/// in Exprs is value-dependent.
707bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
708 for (unsigned I = 0; I < NumExprs; ++I)
709 if (Exprs[I]->isValueDependent())
710 return true;
711
712 return false;
713}
714
Eli Friedmandee41122009-01-25 02:32:41 +0000715bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000716 // This function is attempting whether an expression is an initializer
717 // which can be evaluated at compile-time. isEvaluatable handles most
718 // of the cases, but it can't deal with some initializer-specific
719 // expressions, and it can't deal with aggregates; we deal with those here,
720 // and fall back to isEvaluatable for the other cases.
721
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000722 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000723 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000724 case StringLiteralClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000725 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +0000726 case CompoundLiteralExprClass: {
727 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +0000728 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +0000729 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000730 case InitListExprClass: {
731 const InitListExpr *Exp = cast<InitListExpr>(this);
732 unsigned numInits = Exp->getNumInits();
733 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +0000734 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000735 return false;
736 }
Eli Friedman2b0dec52009-01-25 03:12:18 +0000737 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000738 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000739 case ImplicitValueInitExprClass:
740 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +0000741 case ParenExprClass: {
742 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
743 }
744 case UnaryOperatorClass: {
745 const UnaryOperator* Exp = cast<UnaryOperator>(this);
746 if (Exp->getOpcode() == UnaryOperator::Extension)
747 return Exp->getSubExpr()->isConstantInitializer(Ctx);
748 break;
749 }
750 case CStyleCastExprClass:
751 // Handle casts with a destination that's a struct or union; this
752 // deals with both the gcc no-op struct cast extension and the
753 // cast-to-union extension.
754 if (getType()->isRecordType())
755 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
756 break;
Eli Friedmanc8a00142009-01-25 03:27:40 +0000757 case DesignatedInitExprClass:
Sebastian Redl94889622009-01-25 13:34:47 +0000758 return cast<DesignatedInitExpr>(this)->
759 getInit()->isConstantInitializer(Ctx);
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000760 }
761
Eli Friedman2b0dec52009-01-25 03:12:18 +0000762 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000763}
764
Chris Lattner4b009652007-07-25 00:24:17 +0000765/// isIntegerConstantExpr - this recursive routine will test if an expression is
766/// an integer constant expression. Note: With the introduction of VLA's in
767/// C99 the result of the sizeof operator is no longer always a constant
768/// expression. The generalization of the wording to include any subexpression
769/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
770/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopese1b10a12008-07-08 21:13:06 +0000771/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Chris Lattner4b009652007-07-25 00:24:17 +0000772/// occurring in a context requiring a constant, would have been a constraint
773/// violation. FIXME: This routine currently implements C90 semantics.
774/// To properly implement C99 semantics this routine will need to evaluate
775/// expressions involving operators previously mentioned.
776
777/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
778/// comma, etc
779///
780/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000781/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000782///
783/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
784/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
785/// cast+dereference.
786bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
787 SourceLocation *Loc, bool isEvaluated) const {
Eli Friedman14cc7542008-11-13 06:09:17 +0000788 // Pretest for integral type; some parts of the code crash for types that
789 // can't be sized.
790 if (!getType()->isIntegralType()) {
791 if (Loc) *Loc = getLocStart();
792 return false;
793 }
Chris Lattner4b009652007-07-25 00:24:17 +0000794 switch (getStmtClass()) {
795 default:
796 if (Loc) *Loc = getLocStart();
797 return false;
798 case ParenExprClass:
799 return cast<ParenExpr>(this)->getSubExpr()->
800 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
801 case IntegerLiteralClass:
802 Result = cast<IntegerLiteral>(this)->getValue();
803 break;
804 case CharacterLiteralClass: {
805 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000806 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000807 Result = CL->getValue();
808 Result.setIsUnsigned(!getType()->isSignedIntegerType());
809 break;
810 }
Anders Carlssoned6c2142008-08-23 21:12:35 +0000811 case CXXBoolLiteralExprClass: {
812 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
813 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
814 Result = BL->getValue();
815 Result.setIsUnsigned(!getType()->isSignedIntegerType());
816 break;
817 }
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000818 case CXXZeroInitValueExprClass:
819 Result.clear();
820 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000821 case TypesCompatibleExprClass: {
822 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000823 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Daniel Dunbarda8ebd22008-10-24 08:07:57 +0000824 // Per gcc docs "this built-in function ignores top level
825 // qualifiers". We need to use the canonical version to properly
826 // be able to strip CRV qualifiers from the type.
827 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
828 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
829 Result = Ctx.typesAreCompatible(T0.getUnqualifiedType(),
830 T1.getUnqualifiedType());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000831 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000832 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000833 case CallExprClass:
834 case CXXOperatorCallExprClass: {
Steve Naroff8d3b1702007-08-08 22:15:55 +0000835 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000836 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner1eee9402008-10-06 06:40:35 +0000837
838 // If this is a call to a builtin function, constant fold it otherwise
839 // reject it.
840 if (CE->isBuiltinCall()) {
Anders Carlsson8c3de802008-12-19 20:58:05 +0000841 EvalResult EvalResult;
842 if (CE->Evaluate(EvalResult, Ctx)) {
843 assert(!EvalResult.HasSideEffects &&
844 "Foldable builtin call should not have side effects!");
845 Result = EvalResult.Val.getInt();
Chris Lattner1eee9402008-10-06 06:40:35 +0000846 break; // It is a constant, expand it.
847 }
848 }
849
Steve Naroff8d3b1702007-08-08 22:15:55 +0000850 if (Loc) *Loc = getLocStart();
851 return false;
852 }
Chris Lattner4b009652007-07-25 00:24:17 +0000853 case DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000854 case QualifiedDeclRefExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000855 if (const EnumConstantDecl *D =
856 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
857 Result = D->getInitVal();
858 break;
859 }
860 if (Loc) *Loc = getLocStart();
861 return false;
862 case UnaryOperatorClass: {
863 const UnaryOperator *Exp = cast<UnaryOperator>(this);
864
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000865 // Get the operand value. If this is offsetof, do not evalute the
Chris Lattner4b009652007-07-25 00:24:17 +0000866 // operand. This affects C99 6.6p3.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000867 if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
868 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000869 return false;
870
871 switch (Exp->getOpcode()) {
872 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
873 // See C99 6.6p3.
874 default:
875 if (Loc) *Loc = Exp->getOperatorLoc();
876 return false;
877 case UnaryOperator::Extension:
878 return true; // FIXME: this is wrong.
Chris Lattner4b009652007-07-25 00:24:17 +0000879 case UnaryOperator::LNot: {
Chris Lattnerf00fdc02008-01-25 19:16:19 +0000880 bool Val = Result == 0;
Chris Lattner8cd0e932008-03-05 18:54:05 +0000881 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000882 Result = Val;
883 break;
884 }
885 case UnaryOperator::Plus:
886 break;
887 case UnaryOperator::Minus:
888 Result = -Result;
889 break;
890 case UnaryOperator::Not:
891 Result = ~Result;
892 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000893 case UnaryOperator::OffsetOf:
Daniel Dunbar461d08c2008-08-28 18:42:20 +0000894 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Anders Carlsson52774ad2008-01-29 15:56:48 +0000895 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000896 }
897 break;
898 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000899 case SizeOfAlignOfExprClass: {
900 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
Chris Lattner20515462008-02-21 05:45:29 +0000901
902 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000903 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000904
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000905 QualType ArgTy = Exp->getTypeOfArgument();
Chris Lattner20515462008-02-21 05:45:29 +0000906 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000907 if (ArgTy->isVoidType()) {
Chris Lattner20515462008-02-21 05:45:29 +0000908 Result = 1;
909 break;
910 }
911
912 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000913 if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000914 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000915 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000916 }
Chris Lattner4b009652007-07-25 00:24:17 +0000917
Chris Lattner4b009652007-07-25 00:24:17 +0000918 // Get information about the size or align.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000919 if (ArgTy->isFunctionType()) {
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000920 // GCC extension: sizeof(function) = 1.
921 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000922 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000923 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000924 if (Exp->isSizeOf())
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000925 Result = Ctx.getTypeSize(ArgTy) / CharSize;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000926 else
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000927 Result = Ctx.getTypeAlign(ArgTy) / CharSize;
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000928 }
Chris Lattner4b009652007-07-25 00:24:17 +0000929 break;
930 }
931 case BinaryOperatorClass: {
932 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000933 llvm::APSInt LHS, RHS;
934
935 // Initialize result to have correct signedness and width.
936 Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())),
Eli Friedmanb2935ab2008-11-13 02:13:11 +0000937 !getType()->isSignedIntegerType());
938
Chris Lattner4b009652007-07-25 00:24:17 +0000939 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000940 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000941 return false;
942
Chris Lattner4b009652007-07-25 00:24:17 +0000943 // The short-circuiting &&/|| operators don't necessarily evaluate their
944 // RHS. Make sure to pass isEvaluated down correctly.
945 if (Exp->isLogicalOp()) {
946 bool RHSEval;
947 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000948 RHSEval = LHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000949 else {
950 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000951 RHSEval = LHS == 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000952 }
953
954 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
955 isEvaluated & RHSEval))
956 return false;
957 } else {
958 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
959 return false;
960 }
961
962 switch (Exp->getOpcode()) {
963 default:
964 if (Loc) *Loc = getLocStart();
965 return false;
966 case BinaryOperator::Mul:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000967 Result = LHS * RHS;
Chris Lattner4b009652007-07-25 00:24:17 +0000968 break;
969 case BinaryOperator::Div:
970 if (RHS == 0) {
971 if (!isEvaluated) break;
972 if (Loc) *Loc = getLocStart();
973 return false;
974 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000975 Result = LHS / RHS;
Chris Lattner4b009652007-07-25 00:24:17 +0000976 break;
977 case BinaryOperator::Rem:
978 if (RHS == 0) {
979 if (!isEvaluated) break;
980 if (Loc) *Loc = getLocStart();
981 return false;
982 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000983 Result = LHS % RHS;
Chris Lattner4b009652007-07-25 00:24:17 +0000984 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000985 case BinaryOperator::Add: Result = LHS + RHS; break;
986 case BinaryOperator::Sub: Result = LHS - RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000987 case BinaryOperator::Shl:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000988 Result = LHS <<
989 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
990 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000991 case BinaryOperator::Shr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000992 Result = LHS >>
993 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000994 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000995 case BinaryOperator::LT: Result = LHS < RHS; break;
996 case BinaryOperator::GT: Result = LHS > RHS; break;
997 case BinaryOperator::LE: Result = LHS <= RHS; break;
998 case BinaryOperator::GE: Result = LHS >= RHS; break;
999 case BinaryOperator::EQ: Result = LHS == RHS; break;
1000 case BinaryOperator::NE: Result = LHS != RHS; break;
1001 case BinaryOperator::And: Result = LHS & RHS; break;
1002 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1003 case BinaryOperator::Or: Result = LHS | RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +00001004 case BinaryOperator::LAnd:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001005 Result = LHS != 0 && RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001006 break;
1007 case BinaryOperator::LOr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001008 Result = LHS != 0 || RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001009 break;
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001010
1011 case BinaryOperator::Comma:
1012 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1013 // *except* when they are contained within a subexpression that is not
1014 // evaluated". Note that Assignment can never happen due to constraints
1015 // on the LHS subexpr, so we don't need to check it here.
1016 if (isEvaluated) {
1017 if (Loc) *Loc = getLocStart();
1018 return false;
1019 }
1020
1021 // The result of the constant expr is the RHS.
1022 Result = RHS;
1023 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001024 }
1025
1026 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
1027 break;
1028 }
1029 case ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +00001030 case CStyleCastExprClass:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +00001031 case CXXFunctionalCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001032 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
1033 SourceLocation CastLoc = getLocStart();
Chris Lattner4b009652007-07-25 00:24:17 +00001034
1035 // C99 6.6p6: shall only convert arithmetic types to integer types.
1036 if (!SubExpr->getType()->isArithmeticType() ||
1037 !getType()->isIntegerType()) {
1038 if (Loc) *Loc = SubExpr->getLocStart();
1039 return false;
1040 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001041
Chris Lattner8cd0e932008-03-05 18:54:05 +00001042 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001043
Chris Lattner4b009652007-07-25 00:24:17 +00001044 // Handle simple integer->integer casts.
1045 if (SubExpr->getType()->isIntegerType()) {
1046 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
1047 return false;
1048
1049 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +00001050 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +00001051 if (getType()->isBooleanType()) {
1052 // Conversion to bool compares against zero.
1053 Result = Result != 0;
1054 Result.zextOrTrunc(DestWidth);
1055 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +00001056 Result.sextOrTrunc(DestWidth);
1057 else // If the input is unsigned, do a zero extend, noop, or truncate.
1058 Result.zextOrTrunc(DestWidth);
1059 break;
1060 }
1061
1062 // Allow floating constants that are the immediate operands of casts or that
1063 // are parenthesized.
1064 const Expr *Operand = SubExpr;
1065 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
1066 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001067
1068 // If this isn't a floating literal, we can't handle it.
1069 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1070 if (!FL) {
1071 if (Loc) *Loc = Operand->getLocStart();
1072 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001073 }
Chris Lattner000c4102008-01-09 18:59:34 +00001074
1075 // If the destination is boolean, compare against zero.
1076 if (getType()->isBooleanType()) {
1077 Result = !FL->getValue().isZero();
1078 Result.zextOrTrunc(DestWidth);
1079 break;
1080 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001081
1082 // Determine whether we are converting to unsigned or signed.
1083 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +00001084
1085 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1086 // be called multiple times per AST.
Dale Johannesen2461f612008-10-09 23:02:32 +00001087 uint64_t Space[4];
1088 bool ignored;
Chris Lattner9d020b32007-09-26 00:47:26 +00001089 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesen2461f612008-10-09 23:02:32 +00001090 llvm::APFloat::rmTowardZero,
1091 &ignored);
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001092 Result = llvm::APInt(DestWidth, 4, Space);
1093 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001094 }
1095 case ConditionalOperatorClass: {
1096 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1097
Chris Lattner45e71bf2008-12-12 06:55:44 +00001098 const Expr *Cond = Exp->getCond();
1099
1100 if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001101 return false;
1102
1103 const Expr *TrueExp = Exp->getLHS();
1104 const Expr *FalseExp = Exp->getRHS();
1105 if (Result == 0) std::swap(TrueExp, FalseExp);
1106
Chris Lattner45e71bf2008-12-12 06:55:44 +00001107 // If the condition (ignoring parens) is a __builtin_constant_p call,
1108 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001109 // expression, and it is fully evaluated. This is an important GNU
1110 // extension. See GCC PR38377 for discussion.
Chris Lattner45e71bf2008-12-12 06:55:44 +00001111 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001112 if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) {
1113 EvalResult EVResult;
1114 if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
1115 return false;
1116 assert(EVResult.Val.isInt() && "FP conditional expr not expected");
1117 Result = EVResult.Val.getInt();
1118 if (Loc) *Loc = EVResult.DiagLoc;
1119 return true;
1120 }
Chris Lattner45e71bf2008-12-12 06:55:44 +00001121
Chris Lattner4b009652007-07-25 00:24:17 +00001122 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +00001123 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +00001124 return false;
1125 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +00001126 if (TrueExp &&
1127 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001128 return false;
1129 break;
1130 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001131 case CXXDefaultArgExprClass:
1132 return cast<CXXDefaultArgExpr>(this)
1133 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Sebastian Redl39c0f6f2009-01-05 20:52:13 +00001134
1135 case UnaryTypeTraitExprClass:
1136 Result = cast<UnaryTypeTraitExpr>(this)->Evaluate();
1137 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001138 }
1139
1140 // Cases that are valid constant exprs fall through to here.
1141 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1142 return true;
1143}
1144
Chris Lattner4b009652007-07-25 00:24:17 +00001145/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1146/// integer constant expression with the value zero, or if this is one that is
1147/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001148bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1149{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001150 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001151 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001152 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001153 // Check that it is a cast to void*.
1154 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1155 QualType Pointee = PT->getPointeeType();
1156 if (Pointee.getCVRQualifiers() == 0 &&
1157 Pointee->isVoidType() && // to void*
1158 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001159 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001160 }
Chris Lattner4b009652007-07-25 00:24:17 +00001161 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001162 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1163 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001164 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001165 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1166 // Accept ((void*)0) as a null pointer constant, as many other
1167 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001168 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001169 } else if (const CXXDefaultArgExpr *DefaultArg
1170 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001171 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001172 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001173 } else if (isa<GNUNullExpr>(this)) {
1174 // The GNU __null extension is always a null pointer constant.
1175 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001176 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001177
Steve Naroffa2e53222008-01-14 16:10:57 +00001178 // This expression must be an integer type.
1179 if (!getType()->isIntegerType())
1180 return false;
1181
Chris Lattner4b009652007-07-25 00:24:17 +00001182 // If we have an integer constant expression, we need to *evaluate* it and
1183 // test for the value 0.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001184 // FIXME: We should probably return false if we're compiling in strict mode
1185 // and Diag is not null (this indicates that the value was foldable but not
1186 // an ICE.
1187 EvalResult Result;
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001188 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001189 Result.Val.isInt() && Result.Val.getInt() == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001190}
Steve Naroffc11705f2007-07-28 23:10:27 +00001191
Douglas Gregor81c29152008-10-29 00:13:59 +00001192/// isBitField - Return true if this expression is a bit-field.
1193bool Expr::isBitField() {
1194 Expr *E = this->IgnoreParenCasts();
1195 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001196 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1197 return Field->isBitField();
Douglas Gregor81c29152008-10-29 00:13:59 +00001198 return false;
1199}
1200
Nate Begemanaf6ed502008-04-18 23:10:10 +00001201unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001202 if (const VectorType *VT = getType()->getAsVectorType())
1203 return VT->getNumElements();
1204 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001205}
1206
Nate Begemanc8e51f82008-05-09 06:41:27 +00001207/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001208bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001209 const char *compStr = Accessor.getName();
Chris Lattner58d3fa52008-11-19 07:55:04 +00001210 unsigned length = Accessor.getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001211
1212 // Halving swizzles do not contain duplicate elements.
1213 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1214 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1215 return false;
1216
1217 // Advance past s-char prefix on hex swizzles.
1218 if (*compStr == 's') {
1219 compStr++;
1220 length--;
1221 }
Steve Naroffba67f692007-07-30 03:29:09 +00001222
Chris Lattner58d3fa52008-11-19 07:55:04 +00001223 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001224 const char *s = compStr+i;
1225 for (const char c = *s++; *s; s++)
1226 if (c == *s)
1227 return true;
1228 }
1229 return false;
1230}
Chris Lattner42158e72007-08-02 23:36:59 +00001231
Nate Begemanc8e51f82008-05-09 06:41:27 +00001232/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001233void ExtVectorElementExpr::getEncodedElementAccess(
1234 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner58d3fa52008-11-19 07:55:04 +00001235 const char *compStr = Accessor.getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001236 if (*compStr == 's')
1237 compStr++;
1238
1239 bool isHi = !strcmp(compStr, "hi");
1240 bool isLo = !strcmp(compStr, "lo");
1241 bool isEven = !strcmp(compStr, "even");
1242 bool isOdd = !strcmp(compStr, "odd");
1243
Nate Begemanc8e51f82008-05-09 06:41:27 +00001244 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1245 uint64_t Index;
1246
1247 if (isHi)
1248 Index = e + i;
1249 else if (isLo)
1250 Index = i;
1251 else if (isEven)
1252 Index = 2 * i;
1253 else if (isOdd)
1254 Index = 2 * i + 1;
1255 else
1256 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001257
Nate Begemana1ae7442008-05-13 21:03:02 +00001258 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001259 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001260}
1261
Steve Naroff4ed9d662007-09-27 14:38:14 +00001262// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001263ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001264 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001265 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001266 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001267 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001268 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001269 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001270 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001271 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001272 if (NumArgs) {
1273 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001274 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1275 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001276 LBracloc = LBrac;
1277 RBracloc = RBrac;
1278}
1279
Steve Naroff4ed9d662007-09-27 14:38:14 +00001280// constructor for class messages.
1281// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001282ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001283 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001284 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001285 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001286 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001287 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001288 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001289 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001290 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001291 if (NumArgs) {
1292 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001293 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1294 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001295 LBracloc = LBrac;
1296 RBracloc = RBrac;
1297}
1298
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001299// constructor for class messages.
1300ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1301 QualType retType, ObjCMethodDecl *mproto,
1302 SourceLocation LBrac, SourceLocation RBrac,
1303 Expr **ArgExprs, unsigned nargs)
1304: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1305MethodProto(mproto) {
1306 NumArgs = nargs;
1307 SubExprs = new Stmt*[NumArgs+1];
1308 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1309 if (NumArgs) {
1310 for (unsigned i = 0; i != NumArgs; ++i)
1311 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1312 }
1313 LBracloc = LBrac;
1314 RBracloc = RBrac;
1315}
1316
1317ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1318 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1319 switch (x & Flags) {
1320 default:
1321 assert(false && "Invalid ObjCMessageExpr.");
1322 case IsInstMeth:
1323 return ClassInfo(0, 0);
1324 case IsClsMethDeclUnknown:
1325 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1326 case IsClsMethDeclKnown: {
1327 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1328 return ClassInfo(D, D->getIdentifier());
1329 }
1330 }
1331}
1332
Chris Lattnerf624cd22007-10-25 00:29:32 +00001333bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001334 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001335}
1336
Chris Lattnera5f779dc2008-12-12 05:35:08 +00001337static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
Anders Carlsson52774ad2008-01-29 15:56:48 +00001338 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1339 QualType Ty = ME->getBase()->getType();
1340
1341 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner8cd0e932008-03-05 18:54:05 +00001342 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Douglas Gregor82d44772008-12-20 23:49:58 +00001343 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
1344 // FIXME: This is linear time. And the fact that we're indexing
1345 // into the layout by position in the record means that we're
1346 // either stuck numbering the fields in the AST or we have to keep
1347 // the linear search (yuck and yuck).
1348 unsigned i = 0;
1349 for (RecordDecl::field_iterator Field = RD->field_begin(),
1350 FieldEnd = RD->field_end();
1351 Field != FieldEnd; (void)++Field, ++i) {
1352 if (*Field == FD)
1353 break;
1354 }
1355
1356 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
Anders Carlsson52774ad2008-01-29 15:56:48 +00001357 }
Anders Carlsson52774ad2008-01-29 15:56:48 +00001358 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1359 const Expr *Base = ASE->getBase();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001360
Chris Lattner8cd0e932008-03-05 18:54:05 +00001361 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001362 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001363
1364 return size + evaluateOffsetOf(C, Base);
1365 } else if (isa<CompoundLiteralExpr>(E))
1366 return 0;
1367
1368 assert(0 && "Unknown offsetof subexpression!");
1369 return 0;
1370}
1371
1372int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1373{
1374 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1375
Chris Lattner8cd0e932008-03-05 18:54:05 +00001376 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek2719e982008-06-17 02:43:46 +00001377 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson52774ad2008-01-29 15:56:48 +00001378}
1379
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001380void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1381 // Override default behavior of traversing children. If this has a type
1382 // operand and the type is a variable-length array, the child iteration
1383 // will iterate over the size expression. However, this expression belongs
1384 // to the type, not to this, so we don't want to delete it.
1385 // We still want to delete this expression.
1386 // FIXME: Same as in Stmt::Destroy - will be eventually in ASTContext's
1387 // pool allocator.
1388 if (isArgumentType())
1389 delete this;
1390 else
1391 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001392}
1393
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001394//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001395// DesignatedInitExpr
1396//===----------------------------------------------------------------------===//
1397
1398IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1399 assert(Kind == FieldDesignator && "Only valid on a field designator");
1400 if (Field.NameOrField & 0x01)
1401 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1402 else
1403 return getField()->getIdentifier();
1404}
1405
1406DesignatedInitExpr *
1407DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1408 unsigned NumDesignators,
1409 Expr **IndexExprs, unsigned NumIndexExprs,
1410 SourceLocation ColonOrEqualLoc,
1411 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001412 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1413 sizeof(Designator) * NumDesignators +
1414 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001415 DesignatedInitExpr *DIE
1416 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1417 ColonOrEqualLoc, UsesColonSyntax,
1418 NumIndexExprs + 1);
1419
1420 // Fill in the designators
1421 unsigned ExpectedNumSubExprs = 0;
1422 designators_iterator Desig = DIE->designators_begin();
1423 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1424 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1425 if (Designators[Idx].isArrayDesignator())
1426 ++ExpectedNumSubExprs;
1427 else if (Designators[Idx].isArrayRangeDesignator())
1428 ExpectedNumSubExprs += 2;
1429 }
1430 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1431
1432 // Fill in the subexpressions, including the initializer expression.
1433 child_iterator Child = DIE->child_begin();
1434 *Child++ = Init;
1435 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1436 *Child = IndexExprs[Idx];
1437
1438 return DIE;
1439}
1440
1441SourceRange DesignatedInitExpr::getSourceRange() const {
1442 SourceLocation StartLoc;
1443 Designator &First = *const_cast<DesignatedInitExpr*>(this)->designators_begin();
1444 if (First.isFieldDesignator()) {
1445 if (UsesColonSyntax)
1446 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1447 else
1448 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1449 } else
1450 StartLoc = SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
1451 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1452}
1453
1454DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_begin() {
1455 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1456 Ptr += sizeof(DesignatedInitExpr);
1457 return static_cast<Designator*>(static_cast<void*>(Ptr));
1458}
1459
1460DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1461 return designators_begin() + NumDesignators;
1462}
1463
1464Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1465 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1466 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1467 Ptr += sizeof(DesignatedInitExpr);
1468 Ptr += sizeof(Designator) * NumDesignators;
1469 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1470 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1471}
1472
1473Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1474 assert(D.Kind == Designator::ArrayRangeDesignator &&
1475 "Requires array range designator");
1476 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1477 Ptr += sizeof(DesignatedInitExpr);
1478 Ptr += sizeof(Designator) * NumDesignators;
1479 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1480 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1481}
1482
1483Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1484 assert(D.Kind == Designator::ArrayRangeDesignator &&
1485 "Requires array range designator");
1486 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1487 Ptr += sizeof(DesignatedInitExpr);
1488 Ptr += sizeof(Designator) * NumDesignators;
1489 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1490 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1491}
1492
1493//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001494// ExprIterator.
1495//===----------------------------------------------------------------------===//
1496
1497Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1498Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1499Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1500const Expr* ConstExprIterator::operator[](size_t idx) const {
1501 return cast<Expr>(I[idx]);
1502}
1503const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1504const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1505
1506//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001507// Child Iterators for iterating over subexpressions/substatements
1508//===----------------------------------------------------------------------===//
1509
1510// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001511Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1512Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001513
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001514// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001515Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1516Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001517
Steve Naroff6f786252008-06-02 23:03:37 +00001518// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001519Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1520Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001521
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001522// ObjCKVCRefExpr
1523Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1524Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1525
Douglas Gregord8606632008-11-04 14:56:14 +00001526// ObjCSuperExpr
1527Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1528Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1529
Chris Lattner69909292008-08-10 01:53:14 +00001530// PredefinedExpr
1531Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1532Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001533
1534// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001535Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1536Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001537
1538// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001539Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1540Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001541
1542// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001543Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1544Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001545
Chris Lattner1de66eb2007-08-26 03:42:43 +00001546// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001547Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1548Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001549
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001550// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001551Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1552Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001553
1554// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001555Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1556Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001557
1558// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001559Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1560Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001561
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001562// SizeOfAlignOfExpr
1563Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1564 // If this is of a type and the type is a VLA type (and not a typedef), the
1565 // size expression of the VLA needs to be treated as an executable expression.
1566 // Why isn't this weirdness documented better in StmtIterator?
1567 if (isArgumentType()) {
1568 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1569 getArgumentType().getTypePtr()))
1570 return child_iterator(T);
1571 return child_iterator();
1572 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001573 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001574}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001575Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1576 if (isArgumentType())
1577 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001578 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001579}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001580
1581// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001582Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001583 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001584}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001585Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001586 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001587}
1588
1589// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001590Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001591 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001592}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001593Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001594 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001595}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001596
1597// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001598Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1599Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001600
Nate Begemanaf6ed502008-04-18 23:10:10 +00001601// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001602Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1603Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001604
1605// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001606Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1607Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001608
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001609// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001610Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1611Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001612
1613// BinaryOperator
1614Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001615 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001616}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001617Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001618 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001619}
1620
1621// ConditionalOperator
1622Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001623 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001624}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001625Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001626 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001627}
1628
1629// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001630Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1631Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001632
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001633// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001634Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1635Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001636
1637// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001638Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1639 return child_iterator();
1640}
1641
1642Stmt::child_iterator TypesCompatibleExpr::child_end() {
1643 return child_iterator();
1644}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001645
1646// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001647Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1648Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001649
Douglas Gregorad4b3792008-11-29 04:51:27 +00001650// GNUNullExpr
1651Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1652Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1653
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001654// OverloadExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001655Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; }
1656Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001657
Eli Friedmand0e9d092008-05-14 19:38:39 +00001658// ShuffleVectorExpr
1659Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001660 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001661}
1662Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001663 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001664}
1665
Anders Carlsson36760332007-10-15 20:28:48 +00001666// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001667Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1668Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001669
Anders Carlsson762b7c72007-08-31 04:56:16 +00001670// InitListExpr
1671Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001672 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001673}
1674Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001675 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001676}
1677
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001678// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001679Stmt::child_iterator DesignatedInitExpr::child_begin() {
1680 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1681 Ptr += sizeof(DesignatedInitExpr);
1682 Ptr += sizeof(Designator) * NumDesignators;
1683 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1684}
1685Stmt::child_iterator DesignatedInitExpr::child_end() {
1686 return child_iterator(&*child_begin() + NumSubExprs);
1687}
1688
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001689// ImplicitValueInitExpr
1690Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1691 return child_iterator();
1692}
1693
1694Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1695 return child_iterator();
1696}
1697
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001698// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001699Stmt::child_iterator ObjCStringLiteral::child_begin() {
1700 return child_iterator();
1701}
1702Stmt::child_iterator ObjCStringLiteral::child_end() {
1703 return child_iterator();
1704}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001705
1706// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001707Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1708Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001709
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001710// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001711Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1712 return child_iterator();
1713}
1714Stmt::child_iterator ObjCSelectorExpr::child_end() {
1715 return child_iterator();
1716}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001717
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001718// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001719Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1720 return child_iterator();
1721}
1722Stmt::child_iterator ObjCProtocolExpr::child_end() {
1723 return child_iterator();
1724}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001725
Steve Naroffc39ca262007-09-18 23:55:05 +00001726// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001727Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001728 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001729}
1730Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001731 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001732}
1733
Steve Naroff52a81c02008-09-03 18:15:37 +00001734// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001735Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1736Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001737
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001738Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1739Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }