blob: 904fe5f99c345176297bd7edfae409b1d3a7d7cb [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000016#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Chris Lattnerda8249e2008-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 Johannesenee5a7002008-10-09 23:02:32 +000034 bool ignored;
35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000037 return V.convertToDouble();
38}
39
40
Ted Kremenek6e94ef52009-02-06 19:55:15 +000041StringLiteral::StringLiteral(ASTContext& C, const char *strData,
42 unsigned byteLength, bool Wide, QualType t,
43 SourceLocation firstLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +000044 SourceLocation lastLoc) :
45 Expr(StringLiteralClass, t) {
46 // OPTIMIZE: could allocate this appended to the StringLiteral.
Ted Kremenek6e94ef52009-02-06 19:55:15 +000047 char *AStrData = new (C, 1) char[byteLength];
Reid Spencer5f016e22007-07-11 17:01:13 +000048 memcpy(AStrData, strData, byteLength);
49 StrData = AStrData;
50 ByteLength = byteLength;
51 IsWide = Wide;
52 firstTokLoc = firstLoc;
53 lastTokLoc = lastLoc;
54}
55
Ted Kremenek6e94ef52009-02-06 19:55:15 +000056void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek6e94ef52009-02-06 19:55:15 +000057 this->~StringLiteral();
Ted Kremenek8189cde2009-02-07 01:47:29 +000058 C.Deallocate(const_cast<char*>(StrData));
Reid Spencer5f016e22007-07-11 17:01:13 +000059}
60
61bool UnaryOperator::isPostfix(Opcode Op) {
62 switch (Op) {
63 case PostInc:
64 case PostDec:
65 return true;
66 default:
67 return false;
68 }
69}
70
Ted Kremenek5a56ac32008-07-23 22:18:43 +000071bool UnaryOperator::isPrefix(Opcode Op) {
72 switch (Op) {
73 case PreInc:
74 case PreDec:
75 return true;
76 default:
77 return false;
78 }
79}
80
Reid Spencer5f016e22007-07-11 17:01:13 +000081/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
82/// corresponds to, e.g. "sizeof" or "[pre]++".
83const char *UnaryOperator::getOpcodeStr(Opcode Op) {
84 switch (Op) {
85 default: assert(0 && "Unknown unary operator");
86 case PostInc: return "++";
87 case PostDec: return "--";
88 case PreInc: return "++";
89 case PreDec: return "--";
90 case AddrOf: return "&";
91 case Deref: return "*";
92 case Plus: return "+";
93 case Minus: return "-";
94 case Not: return "~";
95 case LNot: return "!";
96 case Real: return "__real";
97 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +000098 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +000099 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 }
101}
102
103//===----------------------------------------------------------------------===//
104// Postfix Operators.
105//===----------------------------------------------------------------------===//
106
Ted Kremenek8189cde2009-02-07 01:47:29 +0000107CallExpr::CallExpr(StmtClass SC, Expr *fn, Expr **args,
108 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000109 : Expr(SC, t,
110 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
111 fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
112 NumArgs(numargs) {
Douglas Gregorb4609802008-11-14 16:09:21 +0000113 SubExprs = new Stmt*[numargs+1];
114 SubExprs[FN] = fn;
115 for (unsigned i = 0; i != numargs; ++i)
116 SubExprs[i+ARGS_START] = args[i];
117 RParenLoc = rparenloc;
118}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
121 SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000122 : Expr(CallExprClass, t,
123 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
124 fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
125 NumArgs(numargs) {
Ted Kremenek55499762008-06-17 02:43:46 +0000126 SubExprs = new Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000127 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000129 SubExprs[i+ARGS_START] = args[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 RParenLoc = rparenloc;
131}
132
Chris Lattnerd18b3292007-12-28 05:25:02 +0000133/// setNumArgs - This changes the number of arguments present in this call.
134/// Any orphaned expressions are deleted by this, and any new operands are set
135/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000136void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000137 // No change, just return.
138 if (NumArgs == getNumArgs()) return;
139
140 // If shrinking # arguments, just delete the extras and forgot them.
141 if (NumArgs < getNumArgs()) {
142 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000143 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000144 this->NumArgs = NumArgs;
145 return;
146 }
147
148 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek55499762008-06-17 02:43:46 +0000149 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000150 // Copy over args.
151 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
152 NewSubExprs[i] = SubExprs[i];
153 // Null out new args.
154 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
155 NewSubExprs[i] = 0;
156
Ted Kremenek8189cde2009-02-07 01:47:29 +0000157 delete [] SubExprs;
Chris Lattnerd18b3292007-12-28 05:25:02 +0000158 SubExprs = NewSubExprs;
159 this->NumArgs = NumArgs;
160}
161
Chris Lattnercb888962008-10-06 05:00:53 +0000162/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
163/// not, return 0.
164unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000165 // All simple function calls (e.g. func()) are implicitly cast to pointer to
166 // function. As a result, we try and obtain the DeclRefExpr from the
167 // ImplicitCastExpr.
168 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
169 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000170 return 0;
171
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000172 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
173 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000174 return 0;
175
Anders Carlssonbcba2012008-01-31 02:13:57 +0000176 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
177 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000178 return 0;
179
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000180 if (!FDecl->getIdentifier())
181 return 0;
182
Chris Lattnercb888962008-10-06 05:00:53 +0000183 return FDecl->getIdentifier()->getBuiltinID();
184}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000185
Chris Lattnercb888962008-10-06 05:00:53 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
188/// corresponds to, e.g. "<<=".
189const char *BinaryOperator::getOpcodeStr(Opcode Op) {
190 switch (Op) {
191 default: assert(0 && "Unknown binary operator");
192 case Mul: return "*";
193 case Div: return "/";
194 case Rem: return "%";
195 case Add: return "+";
196 case Sub: return "-";
197 case Shl: return "<<";
198 case Shr: return ">>";
199 case LT: return "<";
200 case GT: return ">";
201 case LE: return "<=";
202 case GE: return ">=";
203 case EQ: return "==";
204 case NE: return "!=";
205 case And: return "&";
206 case Xor: return "^";
207 case Or: return "|";
208 case LAnd: return "&&";
209 case LOr: return "||";
210 case Assign: return "=";
211 case MulAssign: return "*=";
212 case DivAssign: return "/=";
213 case RemAssign: return "%=";
214 case AddAssign: return "+=";
215 case SubAssign: return "-=";
216 case ShlAssign: return "<<=";
217 case ShrAssign: return ">>=";
218 case AndAssign: return "&=";
219 case XorAssign: return "^=";
220 case OrAssign: return "|=";
221 case Comma: return ",";
222 }
223}
224
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000225InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000226 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000227 SourceLocation rbraceloc)
Steve Naroffc5ae8992008-05-01 02:04:18 +0000228 : Expr(InitListExprClass, QualType()),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000229 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000230 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000231
232 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000233}
Reid Spencer5f016e22007-07-11 17:01:13 +0000234
Douglas Gregor4c678342009-01-28 21:54:33 +0000235void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
236 for (unsigned Idx = NumInits, LastIdx = InitExprs.size(); Idx < LastIdx; ++Idx)
237 delete InitExprs[Idx];
238 InitExprs.resize(NumInits, 0);
239}
240
241Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
242 if (Init >= InitExprs.size()) {
243 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
244 InitExprs.back() = expr;
245 return 0;
246 }
247
248 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
249 InitExprs[Init] = expr;
250 return Result;
251}
252
Steve Naroffbfdcae62008-09-04 15:31:07 +0000253/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000254///
255const FunctionType *BlockExpr::getFunctionType() const {
256 return getType()->getAsBlockPointerType()->
257 getPointeeType()->getAsFunctionType();
258}
259
Steve Naroff56ee6892008-10-08 17:01:13 +0000260SourceLocation BlockExpr::getCaretLocation() const {
261 return TheBlock->getCaretLocation();
262}
263const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
264Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
265
266
Reid Spencer5f016e22007-07-11 17:01:13 +0000267//===----------------------------------------------------------------------===//
268// Generic Expression Routines
269//===----------------------------------------------------------------------===//
270
271/// hasLocalSideEffect - Return true if this immediate expression has side
272/// effects, not counting any sub-expressions.
273bool Expr::hasLocalSideEffect() const {
274 switch (getStmtClass()) {
275 default:
276 return false;
277 case ParenExprClass:
278 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
279 case UnaryOperatorClass: {
280 const UnaryOperator *UO = cast<UnaryOperator>(this);
281
282 switch (UO->getOpcode()) {
283 default: return false;
284 case UnaryOperator::PostInc:
285 case UnaryOperator::PostDec:
286 case UnaryOperator::PreInc:
287 case UnaryOperator::PreDec:
288 return true; // ++/--
289
290 case UnaryOperator::Deref:
291 // Dereferencing a volatile pointer is a side-effect.
292 return getType().isVolatileQualified();
293 case UnaryOperator::Real:
294 case UnaryOperator::Imag:
295 // accessing a piece of a volatile complex is a side-effect.
296 return UO->getSubExpr()->getType().isVolatileQualified();
297
298 case UnaryOperator::Extension:
299 return UO->getSubExpr()->hasLocalSideEffect();
300 }
301 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000302 case BinaryOperatorClass: {
303 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
304 // Consider comma to have side effects if the LHS and RHS both do.
305 if (BinOp->getOpcode() == BinaryOperator::Comma)
306 return BinOp->getLHS()->hasLocalSideEffect() &&
307 BinOp->getRHS()->hasLocalSideEffect();
308
309 return BinOp->isAssignmentOp();
310 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000311 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000312 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000313
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000314 case ConditionalOperatorClass: {
315 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
316 return Exp->getCond()->hasLocalSideEffect()
317 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
318 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
319 }
320
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 case MemberExprClass:
322 case ArraySubscriptExprClass:
323 // If the base pointer or element is to a volatile pointer/field, accessing
324 // if is a side effect.
325 return getType().isVolatileQualified();
Eli Friedman211f6ad2008-05-27 15:24:04 +0000326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 case CallExprClass:
Douglas Gregorb4609802008-11-14 16:09:21 +0000328 case CXXOperatorCallExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
330 // should warn.
331 return true;
Chris Lattnera9c01022007-09-26 22:06:30 +0000332 case ObjCMessageExprClass:
333 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000334 case StmtExprClass: {
335 // Statement exprs don't logically have side effects themselves, but are
336 // sometimes used in macros in ways that give them a type that is unused.
337 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
338 // however, if the result of the stmt expr is dead, we don't want to emit a
339 // warning.
340 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
341 if (!CS->body_empty())
342 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
343 return E->hasLocalSideEffect();
344 return false;
345 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000346 case CStyleCastExprClass:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000347 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 // If this is a cast to void, check the operand. Otherwise, the result of
349 // the cast is unused.
350 if (getType()->isVoidType())
351 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
352 return false;
Chris Lattner04421082008-04-08 04:40:51 +0000353
Eli Friedman4be1f472008-05-19 21:24:43 +0000354 case ImplicitCastExprClass:
355 // Check the operand, since implicit casts are inserted by Sema
356 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect();
357
Chris Lattner04421082008-04-08 04:40:51 +0000358 case CXXDefaultArgExprClass:
359 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000360
361 case CXXNewExprClass:
362 // FIXME: In theory, there might be new expressions that don't have side
363 // effects (e.g. a placement new with an uninitialized POD).
364 case CXXDeleteExprClass:
365 return true;
366 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000367}
368
Douglas Gregorba7e2102008-10-22 15:04:37 +0000369/// DeclCanBeLvalue - Determine whether the given declaration can be
370/// an lvalue. This is a helper routine for isLvalue.
371static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000372 // C++ [temp.param]p6:
373 // A non-type non-reference template-parameter is not an lvalue.
374 if (const NonTypeTemplateParmDecl *NTTParm
375 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
376 return NTTParm->getType()->isReferenceType();
377
Douglas Gregor44b43212008-12-11 16:49:14 +0000378 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000379 // C++ 3.10p2: An lvalue refers to an object or function.
380 (Ctx.getLangOptions().CPlusPlus &&
381 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
382}
383
Reid Spencer5f016e22007-07-11 17:01:13 +0000384/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
385/// incomplete type other than void. Nonarray expressions that can be lvalues:
386/// - name, where name must be a variable
387/// - e[i]
388/// - (e), where e must be an lvalue
389/// - e.name, where e must be an lvalue
390/// - e->name
391/// - *e, the type of e cannot be a function type
392/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000393/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000394/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000395///
Chris Lattner28be73f2008-07-26 21:30:36 +0000396Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor98cd5992008-10-21 23:43:52 +0000397 // first, check the type (C99 6.3.2.1). Expressions with function
398 // type in C are not lvalues, but they can be lvalues in C++.
399 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 return LV_NotObjectType;
401
Steve Naroffacb818a2008-02-10 01:39:04 +0000402 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000403 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000404 return LV_IncompleteVoidType;
405
Douglas Gregor98cd5992008-10-21 23:43:52 +0000406 /// FIXME: Expressions can't have reference type, so the following
407 /// isn't needed.
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000408 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000409 return LV_Valid;
410
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 // the type looks fine, now check the expression
412 switch (getStmtClass()) {
413 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000414 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
416 // For vectors, make sure base is an lvalue (i.e. not a function call).
417 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000418 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000420 case DeclRefExprClass:
421 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000422 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
423 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 return LV_Valid;
425 break;
Chris Lattner41110242008-06-17 18:05:57 +0000426 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000427 case BlockDeclRefExprClass: {
428 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000429 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000430 return LV_Valid;
431 break;
432 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000433 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000435 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
436 NamedDecl *Member = m->getMemberDecl();
437 // C++ [expr.ref]p4:
438 // If E2 is declared to have type "reference to T", then E1.E2
439 // is an lvalue.
440 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
441 if (Value->getType()->isReferenceType())
442 return LV_Valid;
443
444 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
445 if (isa<CXXClassVarDecl>(Member))
446 return LV_Valid;
447
448 // -- If E2 is a non-static data member [...]. If E1 is an
449 // lvalue, then E1.E2 is an lvalue.
450 if (isa<FieldDecl>(Member))
451 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
452
453 // -- If it refers to a static member function [...], then
454 // E1.E2 is an lvalue.
455 // -- Otherwise, if E1.E2 refers to a non-static member
456 // function [...], then E1.E2 is not an lvalue.
457 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
458 return Method->isStatic()? LV_Valid : LV_MemberFunction;
459
460 // -- If E2 is a member enumerator [...], the expression E1.E2
461 // is not an lvalue.
462 if (isa<EnumConstantDecl>(Member))
463 return LV_InvalidExpression;
464
465 // Not an lvalue.
466 return LV_InvalidExpression;
467 }
468
469 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000470 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000471 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000472 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000474 return LV_Valid; // C99 6.5.3p4
475
476 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000477 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
478 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000479 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000480
481 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
482 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
483 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
484 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000486 case ImplicitCastExprClass:
487 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
488 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000490 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000491 case BinaryOperatorClass:
492 case CompoundAssignOperatorClass: {
493 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000494
495 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
496 BinOp->getOpcode() == BinaryOperator::Comma)
497 return BinOp->getRHS()->isLvalue(Ctx);
498
Sebastian Redl22460502009-02-07 00:15:38 +0000499 // C++ [expr.mptr.oper]p6
500 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
501 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
502 !BinOp->getType()->isFunctionType())
503 return BinOp->getLHS()->isLvalue(Ctx);
504
Douglas Gregorbf3af052008-11-13 20:12:29 +0000505 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000506 return LV_InvalidExpression;
507
Douglas Gregorbf3af052008-11-13 20:12:29 +0000508 if (Ctx.getLangOptions().CPlusPlus)
509 // C++ [expr.ass]p1:
510 // The result of an assignment operation [...] is an lvalue.
511 return LV_Valid;
512
513
514 // C99 6.5.16:
515 // An assignment expression [...] is not an lvalue.
516 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000517 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000518 // FIXME: OverloadExprClass
Douglas Gregorb4609802008-11-14 16:09:21 +0000519 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000520 case CXXOperatorCallExprClass:
521 case CXXMemberCallExprClass: {
Douglas Gregor9d293df2008-10-28 00:22:11 +0000522 // C++ [expr.call]p10:
523 // A function call is an lvalue if and only if the result type
524 // is a reference.
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000525 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor9d293df2008-10-28 00:22:11 +0000526 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000527 CalleeType = FnTypePtr->getPointeeType();
528 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
529 if (FnType->getResultType()->isReferenceType())
530 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000531
532 break;
533 }
Steve Naroffe6386392007-12-05 04:00:10 +0000534 case CompoundLiteralExprClass: // C99 6.5.2.5p5
535 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000536 case ChooseExprClass:
537 // __builtin_choose_expr is an lvalue if the selected operand is.
538 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
539 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
540 else
541 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
542
Nate Begeman213541a2008-04-18 23:10:10 +0000543 case ExtVectorElementExprClass:
544 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000545 return LV_DuplicateVectorComponents;
546 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000547 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
548 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000549 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
550 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000551 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000552 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000553 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000554 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000555 case VAArgExprClass:
556 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +0000557 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000558 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000559 case CXXConditionDeclExprClass:
560 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000561 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000562 case CXXFunctionalCastExprClass:
563 case CXXStaticCastExprClass:
564 case CXXDynamicCastExprClass:
565 case CXXReinterpretCastExprClass:
566 case CXXConstCastExprClass:
567 // The result of an explicit cast is an lvalue if the type we are
568 // casting to is a reference type. See C++ [expr.cast]p1,
569 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
570 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
571 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
572 return LV_Valid;
573 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000574 case CXXTypeidExprClass:
575 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
576 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 default:
578 break;
579 }
580 return LV_InvalidExpression;
581}
582
583/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
584/// does not have an incomplete type, does not have a const-qualified type, and
585/// if it is a structure or union, does not have any member (including,
586/// recursively, any member or element of all contained aggregates or unions)
587/// with a const-qualified type.
Chris Lattner28be73f2008-07-26 21:30:36 +0000588Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
589 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000590
591 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000592 case LV_Valid:
593 // C++ 3.10p11: Functions cannot be modified, but pointers to
594 // functions can be modifiable.
595 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
596 return MLV_NotObjectType;
597 break;
598
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 case LV_NotObjectType: return MLV_NotObjectType;
600 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000601 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000602 case LV_InvalidExpression:
603 // If the top level is a C-style cast, and the subexpression is a valid
604 // lvalue, then this is probably a use of the old-school "cast as lvalue"
605 // GCC extension. We don't support it, but we want to produce good
606 // diagnostics when it happens so that the user knows why.
607 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
608 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
609 return MLV_LValueCast;
610 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000611 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000613
614 QualType CT = Ctx.getCanonicalType(getType());
615
616 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000618 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000620 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 return MLV_IncompleteType;
622
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000623 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 if (r->hasConstFields())
625 return MLV_ConstQualified;
626 }
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000627 // The following is illegal:
628 // void takeclosure(void (^C)(void));
629 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
630 //
631 if (getStmtClass() == BlockDeclRefExprClass) {
632 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
633 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
634 return MLV_NotBlockQualified;
635 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000636
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000637 // Assigning to an 'implicit' property?
Fariborz Jahanian6669db92008-11-25 17:56:43 +0000638 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000639 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
640 if (KVCExpr->getSetterMethod() == 0)
641 return MLV_NoSetterProperty;
642 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 return MLV_Valid;
644}
645
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000646/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000647/// duration. This means that the address of this expression is a link-time
648/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000649bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000650 switch (getStmtClass()) {
651 default:
652 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000653 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000654 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000655 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000656 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000657 case CompoundLiteralExprClass:
658 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000659 case DeclRefExprClass:
660 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000661 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
662 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000663 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000664 if (isa<FunctionDecl>(D))
665 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000666 return false;
667 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000668 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000669 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000670 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000671 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000672 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000673 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000674 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000675 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000676 case CXXDefaultArgExprClass:
677 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000678 }
679}
680
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000681Expr* Expr::IgnoreParens() {
682 Expr* E = this;
683 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
684 E = P->getSubExpr();
685
686 return E;
687}
688
Chris Lattner56f34942008-02-13 01:02:39 +0000689/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
690/// or CastExprs or ImplicitCastExprs, returning their operand.
691Expr *Expr::IgnoreParenCasts() {
692 Expr *E = this;
693 while (true) {
694 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
695 E = P->getSubExpr();
696 else if (CastExpr *P = dyn_cast<CastExpr>(E))
697 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +0000698 else
699 return E;
700 }
701}
702
Douglas Gregor898574e2008-12-05 23:32:09 +0000703/// hasAnyTypeDependentArguments - Determines if any of the expressions
704/// in Exprs is type-dependent.
705bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
706 for (unsigned I = 0; I < NumExprs; ++I)
707 if (Exprs[I]->isTypeDependent())
708 return true;
709
710 return false;
711}
712
713/// hasAnyValueDependentArguments - Determines if any of the expressions
714/// in Exprs is value-dependent.
715bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
716 for (unsigned I = 0; I < NumExprs; ++I)
717 if (Exprs[I]->isValueDependent())
718 return true;
719
720 return false;
721}
722
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000723bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000724 // This function is attempting whether an expression is an initializer
725 // which can be evaluated at compile-time. isEvaluatable handles most
726 // of the cases, but it can't deal with some initializer-specific
727 // expressions, and it can't deal with aggregates; we deal with those here,
728 // and fall back to isEvaluatable for the other cases.
729
Anders Carlssone8a32b82008-11-24 05:23:59 +0000730 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000731 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000732 case StringLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +0000733 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +0000734 case CompoundLiteralExprClass: {
735 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000736 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +0000737 }
Anders Carlssone8a32b82008-11-24 05:23:59 +0000738 case InitListExprClass: {
739 const InitListExpr *Exp = cast<InitListExpr>(this);
740 unsigned numInits = Exp->getNumInits();
741 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000742 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +0000743 return false;
744 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000745 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000746 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000747 case ImplicitValueInitExprClass:
748 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000749 case ParenExprClass: {
750 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
751 }
752 case UnaryOperatorClass: {
753 const UnaryOperator* Exp = cast<UnaryOperator>(this);
754 if (Exp->getOpcode() == UnaryOperator::Extension)
755 return Exp->getSubExpr()->isConstantInitializer(Ctx);
756 break;
757 }
758 case CStyleCastExprClass:
759 // Handle casts with a destination that's a struct or union; this
760 // deals with both the gcc no-op struct cast extension and the
761 // cast-to-union extension.
762 if (getType()->isRecordType())
763 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
764 break;
Eli Friedman32a311e2009-01-25 03:27:40 +0000765 case DesignatedInitExprClass:
Sebastian Redl4e716e02009-01-25 13:34:47 +0000766 return cast<DesignatedInitExpr>(this)->
767 getInit()->isConstantInitializer(Ctx);
Anders Carlssone8a32b82008-11-24 05:23:59 +0000768 }
769
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000770 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +0000771}
772
Reid Spencer5f016e22007-07-11 17:01:13 +0000773/// isIntegerConstantExpr - this recursive routine will test if an expression is
774/// an integer constant expression. Note: With the introduction of VLA's in
775/// C99 the result of the sizeof operator is no longer always a constant
776/// expression. The generalization of the wording to include any subexpression
777/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
778/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopes5f6b6322008-07-08 21:13:06 +0000779/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Reid Spencer5f016e22007-07-11 17:01:13 +0000780/// occurring in a context requiring a constant, would have been a constraint
781/// violation. FIXME: This routine currently implements C90 semantics.
782/// To properly implement C99 semantics this routine will need to evaluate
783/// expressions involving operators previously mentioned.
784
785/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
786/// comma, etc
787///
788/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000789/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000790///
791/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
792/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
793/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000794bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
795 SourceLocation *Loc, bool isEvaluated) const {
Eli Friedmana6afa762008-11-13 06:09:17 +0000796 // Pretest for integral type; some parts of the code crash for types that
797 // can't be sized.
798 if (!getType()->isIntegralType()) {
799 if (Loc) *Loc = getLocStart();
800 return false;
801 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 switch (getStmtClass()) {
803 default:
804 if (Loc) *Loc = getLocStart();
805 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 case ParenExprClass:
807 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000808 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 case IntegerLiteralClass:
810 Result = cast<IntegerLiteral>(this)->getValue();
811 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000812 case CharacterLiteralClass: {
813 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner98be4942008-03-05 18:54:05 +0000814 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000815 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000816 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000818 }
Anders Carlssonb88d45e2008-08-23 21:12:35 +0000819 case CXXBoolLiteralExprClass: {
820 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
821 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
822 Result = BL->getValue();
823 Result.setIsUnsigned(!getType()->isSignedIntegerType());
824 break;
825 }
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000826 case CXXZeroInitValueExprClass:
827 Result.clear();
828 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000829 case TypesCompatibleExprClass: {
830 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner98be4942008-03-05 18:54:05 +0000831 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Daniel Dunbarac620de2008-10-24 08:07:57 +0000832 // Per gcc docs "this built-in function ignores top level
833 // qualifiers". We need to use the canonical version to properly
834 // be able to strip CRV qualifiers from the type.
835 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
836 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
837 Result = Ctx.typesAreCompatible(T0.getUnqualifiedType(),
838 T1.getUnqualifiedType());
Steve Naroff389cecc2007-08-02 00:13:27 +0000839 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000840 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000841 case CallExprClass:
842 case CXXOperatorCallExprClass: {
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000843 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner98be4942008-03-05 18:54:05 +0000844 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattnera4d55d82008-10-06 06:40:35 +0000845
846 // If this is a call to a builtin function, constant fold it otherwise
847 // reject it.
848 if (CE->isBuiltinCall()) {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +0000849 EvalResult EvalResult;
850 if (CE->Evaluate(EvalResult, Ctx)) {
851 assert(!EvalResult.HasSideEffects &&
852 "Foldable builtin call should not have side effects!");
853 Result = EvalResult.Val.getInt();
Chris Lattnera4d55d82008-10-06 06:40:35 +0000854 break; // It is a constant, expand it.
855 }
856 }
857
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000858 if (Loc) *Loc = getLocStart();
859 return false;
860 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 case DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000862 case QualifiedDeclRefExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 if (const EnumConstantDecl *D =
864 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
865 Result = D->getInitVal();
866 break;
867 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +0000868 if (Ctx.getLangOptions().CPlusPlus &&
869 getType().getCVRQualifiers() == QualType::Const) {
870 // C++ 7.1.5.1p2
871 // A variable of non-volatile const-qualified integral or enumeration
872 // type initialized by an ICE can be used in ICEs.
873 if (const VarDecl *Dcl =
874 dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) {
875 if (const Expr *Init = Dcl->getInit())
876 return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
877 }
878 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 if (Loc) *Loc = getLocStart();
880 return false;
881 case UnaryOperatorClass: {
882 const UnaryOperator *Exp = cast<UnaryOperator>(this);
883
Sebastian Redl05189992008-11-11 17:56:53 +0000884 // Get the operand value. If this is offsetof, do not evalute the
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 // operand. This affects C99 6.6p3.
Sebastian Redl05189992008-11-11 17:56:53 +0000886 if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
887 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 return false;
889
890 switch (Exp->getOpcode()) {
891 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
892 // See C99 6.6p3.
893 default:
894 if (Loc) *Loc = Exp->getOperatorLoc();
895 return false;
896 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000897 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 case UnaryOperator::LNot: {
Chris Lattnerbf755382008-01-25 19:16:19 +0000899 bool Val = Result == 0;
Chris Lattner98be4942008-03-05 18:54:05 +0000900 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 Result = Val;
902 break;
903 }
904 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 break;
906 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 Result = -Result;
908 break;
909 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 Result = ~Result;
911 break;
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000912 case UnaryOperator::OffsetOf:
Daniel Dunbaraa1f9f12008-08-28 18:42:20 +0000913 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000914 Result = Exp->evaluateOffsetOf(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 }
916 break;
917 }
Sebastian Redl05189992008-11-11 17:56:53 +0000918 case SizeOfAlignOfExprClass: {
919 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
Chris Lattnera269ebf2008-02-21 05:45:29 +0000920
921 // Return the result in the right width.
Chris Lattner98be4942008-03-05 18:54:05 +0000922 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattnera269ebf2008-02-21 05:45:29 +0000923
Sebastian Redl05189992008-11-11 17:56:53 +0000924 QualType ArgTy = Exp->getTypeOfArgument();
Chris Lattnera269ebf2008-02-21 05:45:29 +0000925 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Sebastian Redl05189992008-11-11 17:56:53 +0000926 if (ArgTy->isVoidType()) {
Chris Lattnera269ebf2008-02-21 05:45:29 +0000927 Result = 1;
928 break;
929 }
930
931 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Sebastian Redl05189992008-11-11 17:56:53 +0000932 if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) {
Chris Lattner65383472007-12-18 07:15:40 +0000933 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000935 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000936
Chris Lattner76e773a2007-07-18 18:38:36 +0000937 // Get information about the size or align.
Sebastian Redl05189992008-11-11 17:56:53 +0000938 if (ArgTy->isFunctionType()) {
Chris Lattnerefdd1572008-01-02 21:54:09 +0000939 // GCC extension: sizeof(function) = 1.
940 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000941 } else {
Chris Lattner98be4942008-03-05 18:54:05 +0000942 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000943 if (Exp->isSizeOf())
Sebastian Redl05189992008-11-11 17:56:53 +0000944 Result = Ctx.getTypeSize(ArgTy) / CharSize;
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000945 else
Sebastian Redl05189992008-11-11 17:56:53 +0000946 Result = Ctx.getTypeAlign(ArgTy) / CharSize;
Ted Kremenek060e4702007-12-17 17:38:43 +0000947 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 break;
949 }
950 case BinaryOperatorClass: {
951 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbare1226d22008-09-22 23:53:24 +0000952 llvm::APSInt LHS, RHS;
953
954 // Initialize result to have correct signedness and width.
955 Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())),
Eli Friedmanb11e7782008-11-13 02:13:11 +0000956 !getType()->isSignedIntegerType());
957
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbare1226d22008-09-22 23:53:24 +0000959 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 return false;
961
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 // The short-circuiting &&/|| operators don't necessarily evaluate their
963 // RHS. Make sure to pass isEvaluated down correctly.
964 if (Exp->isLogicalOp()) {
965 bool RHSEval;
966 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbare1226d22008-09-22 23:53:24 +0000967 RHSEval = LHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 else {
969 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbare1226d22008-09-22 23:53:24 +0000970 RHSEval = LHS == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 }
972
Chris Lattner590b6642007-07-15 23:26:56 +0000973 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 isEvaluated & RHSEval))
975 return false;
976 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000977 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 return false;
979 }
980
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 switch (Exp->getOpcode()) {
982 default:
983 if (Loc) *Loc = getLocStart();
984 return false;
985 case BinaryOperator::Mul:
Daniel Dunbare1226d22008-09-22 23:53:24 +0000986 Result = LHS * RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 break;
988 case BinaryOperator::Div:
989 if (RHS == 0) {
990 if (!isEvaluated) break;
991 if (Loc) *Loc = getLocStart();
992 return false;
993 }
Daniel Dunbare1226d22008-09-22 23:53:24 +0000994 Result = LHS / RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 break;
996 case BinaryOperator::Rem:
997 if (RHS == 0) {
998 if (!isEvaluated) break;
999 if (Loc) *Loc = getLocStart();
1000 return false;
1001 }
Daniel Dunbare1226d22008-09-22 23:53:24 +00001002 Result = LHS % RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 break;
Daniel Dunbare1226d22008-09-22 23:53:24 +00001004 case BinaryOperator::Add: Result = LHS + RHS; break;
1005 case BinaryOperator::Sub: Result = LHS - RHS; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 case BinaryOperator::Shl:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001007 Result = LHS <<
1008 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
1009 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 case BinaryOperator::Shr:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001011 Result = LHS >>
1012 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 break;
Daniel Dunbare1226d22008-09-22 23:53:24 +00001014 case BinaryOperator::LT: Result = LHS < RHS; break;
1015 case BinaryOperator::GT: Result = LHS > RHS; break;
1016 case BinaryOperator::LE: Result = LHS <= RHS; break;
1017 case BinaryOperator::GE: Result = LHS >= RHS; break;
1018 case BinaryOperator::EQ: Result = LHS == RHS; break;
1019 case BinaryOperator::NE: Result = LHS != RHS; break;
1020 case BinaryOperator::And: Result = LHS & RHS; break;
1021 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1022 case BinaryOperator::Or: Result = LHS | RHS; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 case BinaryOperator::LAnd:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001024 Result = LHS != 0 && RHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 break;
1026 case BinaryOperator::LOr:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001027 Result = LHS != 0 || RHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001028 break;
Eli Friedmanb11e7782008-11-13 02:13:11 +00001029
1030 case BinaryOperator::Comma:
1031 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1032 // *except* when they are contained within a subexpression that is not
1033 // evaluated". Note that Assignment can never happen due to constraints
1034 // on the LHS subexpr, so we don't need to check it here.
1035 if (isEvaluated) {
1036 if (Loc) *Loc = getLocStart();
1037 return false;
1038 }
1039
1040 // The result of the constant expr is the RHS.
1041 Result = RHS;
1042 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 }
1044
1045 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
1046 break;
1047 }
Chris Lattner26dc7b32007-07-15 23:54:50 +00001048 case ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001049 case CStyleCastExprClass:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001050 case CXXFunctionalCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001051 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
1052 SourceLocation CastLoc = getLocStart();
Chris Lattner26dc7b32007-07-15 23:54:50 +00001053
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001055 if (!SubExpr->getType()->isArithmeticType() ||
1056 !getType()->isIntegerType()) {
1057 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 return false;
1059 }
Chris Lattner987b15d2007-09-22 19:04:13 +00001060
Chris Lattner98be4942008-03-05 18:54:05 +00001061 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner987b15d2007-09-22 19:04:13 +00001062
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001064 if (SubExpr->getType()->isIntegerType()) {
1065 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +00001067
1068 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001069 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001070 if (getType()->isBooleanType()) {
1071 // Conversion to bool compares against zero.
1072 Result = Result != 0;
1073 Result.zextOrTrunc(DestWidth);
1074 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner26dc7b32007-07-15 23:54:50 +00001075 Result.sextOrTrunc(DestWidth);
1076 else // If the input is unsigned, do a zero extend, noop, or truncate.
1077 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 break;
1079 }
1080
1081 // Allow floating constants that are the immediate operands of casts or that
1082 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001083 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
1085 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +00001086
1087 // If this isn't a floating literal, we can't handle it.
1088 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1089 if (!FL) {
1090 if (Loc) *Loc = Operand->getLocStart();
1091 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 }
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001093
1094 // If the destination is boolean, compare against zero.
1095 if (getType()->isBooleanType()) {
1096 Result = !FL->getValue().isZero();
1097 Result.zextOrTrunc(DestWidth);
1098 break;
1099 }
Chris Lattner987b15d2007-09-22 19:04:13 +00001100
1101 // Determine whether we are converting to unsigned or signed.
1102 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +00001103
1104 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1105 // be called multiple times per AST.
Dale Johannesenee5a7002008-10-09 23:02:32 +00001106 uint64_t Space[4];
1107 bool ignored;
Chris Lattnerccc213f2007-09-26 00:47:26 +00001108 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesenee5a7002008-10-09 23:02:32 +00001109 llvm::APFloat::rmTowardZero,
1110 &ignored);
Chris Lattner987b15d2007-09-22 19:04:13 +00001111 Result = llvm::APInt(DestWidth, 4, Space);
1112 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001113 }
1114 case ConditionalOperatorClass: {
1115 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1116
Chris Lattner28daa532008-12-12 06:55:44 +00001117 const Expr *Cond = Exp->getCond();
1118
1119 if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 return false;
1121
1122 const Expr *TrueExp = Exp->getLHS();
1123 const Expr *FalseExp = Exp->getRHS();
1124 if (Result == 0) std::swap(TrueExp, FalseExp);
1125
Chris Lattner28daa532008-12-12 06:55:44 +00001126 // If the condition (ignoring parens) is a __builtin_constant_p call,
1127 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001128 // expression, and it is fully evaluated. This is an important GNU
1129 // extension. See GCC PR38377 for discussion.
Chris Lattner28daa532008-12-12 06:55:44 +00001130 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
Chris Lattner42b83dd2008-12-12 18:00:51 +00001131 if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) {
1132 EvalResult EVResult;
1133 if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
1134 return false;
1135 assert(EVResult.Val.isInt() && "FP conditional expr not expected");
1136 Result = EVResult.Val.getInt();
1137 if (Loc) *Loc = EVResult.DiagLoc;
1138 return true;
1139 }
Chris Lattner28daa532008-12-12 06:55:44 +00001140
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 // Evaluate the false one first, discard the result.
Anders Carlsson39073232007-11-30 19:04:31 +00001142 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 return false;
1144 // Evalute the true one, capture the result.
Anders Carlsson39073232007-11-30 19:04:31 +00001145 if (TrueExp &&
1146 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 break;
1149 }
Chris Lattner04421082008-04-08 04:40:51 +00001150 case CXXDefaultArgExprClass:
1151 return cast<CXXDefaultArgExpr>(this)
1152 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001153
1154 case UnaryTypeTraitExprClass:
1155 Result = cast<UnaryTypeTraitExpr>(this)->Evaluate();
1156 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 }
1158
1159 // Cases that are valid constant exprs fall through to here.
1160 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1161 return true;
1162}
1163
Reid Spencer5f016e22007-07-11 17:01:13 +00001164/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1165/// integer constant expression with the value zero, or if this is one that is
1166/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001167bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1168{
Sebastian Redl07779722008-10-31 14:43:28 +00001169 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001170 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001171 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001172 // Check that it is a cast to void*.
1173 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1174 QualType Pointee = PT->getPointeeType();
1175 if (Pointee.getCVRQualifiers() == 0 &&
1176 Pointee->isVoidType() && // to void*
1177 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001178 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001179 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001181 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1182 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001183 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001184 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1185 // Accept ((void*)0) as a null pointer constant, as many other
1186 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001187 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001188 } else if (const CXXDefaultArgExpr *DefaultArg
1189 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001190 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001191 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001192 } else if (isa<GNUNullExpr>(this)) {
1193 // The GNU __null extension is always a null pointer constant.
1194 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001195 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001196
Steve Naroffaa58f002008-01-14 16:10:57 +00001197 // This expression must be an integer type.
1198 if (!getType()->isIntegerType())
1199 return false;
1200
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 // If we have an integer constant expression, we need to *evaluate* it and
1202 // test for the value 0.
Anders Carlssond2652772008-12-01 06:28:23 +00001203 // FIXME: We should probably return false if we're compiling in strict mode
1204 // and Diag is not null (this indicates that the value was foldable but not
1205 // an ICE.
1206 EvalResult Result;
Anders Carlssonefa9b382008-12-01 02:13:57 +00001207 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssond2652772008-12-01 06:28:23 +00001208 Result.Val.isInt() && Result.Val.getInt() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001209}
Steve Naroff31a45842007-07-28 23:10:27 +00001210
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001211/// isBitField - Return true if this expression is a bit-field.
1212bool Expr::isBitField() {
1213 Expr *E = this->IgnoreParenCasts();
1214 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001215 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1216 return Field->isBitField();
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001217 return false;
1218}
1219
Nate Begeman213541a2008-04-18 23:10:10 +00001220unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001221 if (const VectorType *VT = getType()->getAsVectorType())
1222 return VT->getNumElements();
1223 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001224}
1225
Nate Begeman8a997642008-05-09 06:41:27 +00001226/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001227bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001228 const char *compStr = Accessor.getName();
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001229 unsigned length = Accessor.getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001230
1231 // Halving swizzles do not contain duplicate elements.
1232 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1233 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1234 return false;
1235
1236 // Advance past s-char prefix on hex swizzles.
1237 if (*compStr == 's') {
1238 compStr++;
1239 length--;
1240 }
Steve Narofffec0b492007-07-30 03:29:09 +00001241
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001242 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001243 const char *s = compStr+i;
1244 for (const char c = *s++; *s; s++)
1245 if (c == *s)
1246 return true;
1247 }
1248 return false;
1249}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001250
Nate Begeman8a997642008-05-09 06:41:27 +00001251/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001252void ExtVectorElementExpr::getEncodedElementAccess(
1253 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001254 const char *compStr = Accessor.getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001255 if (*compStr == 's')
1256 compStr++;
1257
1258 bool isHi = !strcmp(compStr, "hi");
1259 bool isLo = !strcmp(compStr, "lo");
1260 bool isEven = !strcmp(compStr, "even");
1261 bool isOdd = !strcmp(compStr, "odd");
1262
Nate Begeman8a997642008-05-09 06:41:27 +00001263 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1264 uint64_t Index;
1265
1266 if (isHi)
1267 Index = e + i;
1268 else if (isLo)
1269 Index = i;
1270 else if (isEven)
1271 Index = 2 * i;
1272 else if (isOdd)
1273 Index = 2 * i + 1;
1274 else
1275 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001276
Nate Begeman3b8d1162008-05-13 21:03:02 +00001277 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001278 }
Nate Begeman8a997642008-05-09 06:41:27 +00001279}
1280
Steve Naroff68d331a2007-09-27 14:38:14 +00001281// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001282ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001283 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001284 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001285 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001286 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001287 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001288 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001289 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001290 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001291 if (NumArgs) {
1292 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001293 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1294 }
Steve Naroff563477d2007-09-18 23:55:05 +00001295 LBracloc = LBrac;
1296 RBracloc = RBrac;
1297}
1298
Steve Naroff68d331a2007-09-27 14:38:14 +00001299// constructor for class messages.
1300// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001301ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001302 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001303 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001304 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001305 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001306 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001307 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001308 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001309 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001310 if (NumArgs) {
1311 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001312 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1313 }
Steve Naroff563477d2007-09-18 23:55:05 +00001314 LBracloc = LBrac;
1315 RBracloc = RBrac;
1316}
1317
Ted Kremenek4df728e2008-06-24 15:50:53 +00001318// constructor for class messages.
1319ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1320 QualType retType, ObjCMethodDecl *mproto,
1321 SourceLocation LBrac, SourceLocation RBrac,
1322 Expr **ArgExprs, unsigned nargs)
1323: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1324MethodProto(mproto) {
1325 NumArgs = nargs;
1326 SubExprs = new Stmt*[NumArgs+1];
1327 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1328 if (NumArgs) {
1329 for (unsigned i = 0; i != NumArgs; ++i)
1330 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1331 }
1332 LBracloc = LBrac;
1333 RBracloc = RBrac;
1334}
1335
1336ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1337 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1338 switch (x & Flags) {
1339 default:
1340 assert(false && "Invalid ObjCMessageExpr.");
1341 case IsInstMeth:
1342 return ClassInfo(0, 0);
1343 case IsClsMethDeclUnknown:
1344 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1345 case IsClsMethDeclKnown: {
1346 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1347 return ClassInfo(D, D->getIdentifier());
1348 }
1349 }
1350}
1351
Chris Lattner27437ca2007-10-25 00:29:32 +00001352bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001353 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001354}
1355
Chris Lattner670a62c2008-12-12 05:35:08 +00001356static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001357 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1358 QualType Ty = ME->getBase()->getType();
1359
1360 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner98be4942008-03-05 18:54:05 +00001361 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +00001362 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
1363 // FIXME: This is linear time. And the fact that we're indexing
1364 // into the layout by position in the record means that we're
1365 // either stuck numbering the fields in the AST or we have to keep
1366 // the linear search (yuck and yuck).
1367 unsigned i = 0;
1368 for (RecordDecl::field_iterator Field = RD->field_begin(),
1369 FieldEnd = RD->field_end();
1370 Field != FieldEnd; (void)++Field, ++i) {
1371 if (*Field == FD)
1372 break;
1373 }
1374
1375 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001376 }
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001377 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1378 const Expr *Base = ASE->getBase();
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001379
Chris Lattner98be4942008-03-05 18:54:05 +00001380 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001381 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001382
1383 return size + evaluateOffsetOf(C, Base);
1384 } else if (isa<CompoundLiteralExpr>(E))
1385 return 0;
1386
1387 assert(0 && "Unknown offsetof subexpression!");
1388 return 0;
1389}
1390
1391int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1392{
1393 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1394
Chris Lattner98be4942008-03-05 18:54:05 +00001395 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek55499762008-06-17 02:43:46 +00001396 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001397}
1398
Sebastian Redl05189992008-11-11 17:56:53 +00001399void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1400 // Override default behavior of traversing children. If this has a type
1401 // operand and the type is a variable-length array, the child iteration
1402 // will iterate over the size expression. However, this expression belongs
1403 // to the type, not to this, so we don't want to delete it.
1404 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001405 if (isArgumentType()) {
1406 this->~SizeOfAlignOfExpr();
1407 C.Deallocate(this);
1408 }
Sebastian Redl05189992008-11-11 17:56:53 +00001409 else
1410 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001411}
1412
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001413//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001414// DesignatedInitExpr
1415//===----------------------------------------------------------------------===//
1416
1417IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1418 assert(Kind == FieldDesignator && "Only valid on a field designator");
1419 if (Field.NameOrField & 0x01)
1420 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1421 else
1422 return getField()->getIdentifier();
1423}
1424
1425DesignatedInitExpr *
1426DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1427 unsigned NumDesignators,
1428 Expr **IndexExprs, unsigned NumIndexExprs,
1429 SourceLocation ColonOrEqualLoc,
1430 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001431 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1432 sizeof(Designator) * NumDesignators +
1433 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001434 DesignatedInitExpr *DIE
1435 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1436 ColonOrEqualLoc, UsesColonSyntax,
1437 NumIndexExprs + 1);
1438
1439 // Fill in the designators
1440 unsigned ExpectedNumSubExprs = 0;
1441 designators_iterator Desig = DIE->designators_begin();
1442 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1443 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1444 if (Designators[Idx].isArrayDesignator())
1445 ++ExpectedNumSubExprs;
1446 else if (Designators[Idx].isArrayRangeDesignator())
1447 ExpectedNumSubExprs += 2;
1448 }
1449 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1450
1451 // Fill in the subexpressions, including the initializer expression.
1452 child_iterator Child = DIE->child_begin();
1453 *Child++ = Init;
1454 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1455 *Child = IndexExprs[Idx];
1456
1457 return DIE;
1458}
1459
1460SourceRange DesignatedInitExpr::getSourceRange() const {
1461 SourceLocation StartLoc;
1462 Designator &First = *const_cast<DesignatedInitExpr*>(this)->designators_begin();
1463 if (First.isFieldDesignator()) {
1464 if (UsesColonSyntax)
1465 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1466 else
1467 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1468 } else
1469 StartLoc = SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
1470 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1471}
1472
1473DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_begin() {
1474 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1475 Ptr += sizeof(DesignatedInitExpr);
1476 return static_cast<Designator*>(static_cast<void*>(Ptr));
1477}
1478
1479DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1480 return designators_begin() + NumDesignators;
1481}
1482
1483Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1484 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1485 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1486 Ptr += sizeof(DesignatedInitExpr);
1487 Ptr += sizeof(Designator) * NumDesignators;
1488 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1489 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1490}
1491
1492Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1493 assert(D.Kind == Designator::ArrayRangeDesignator &&
1494 "Requires array range designator");
1495 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1496 Ptr += sizeof(DesignatedInitExpr);
1497 Ptr += sizeof(Designator) * NumDesignators;
1498 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1499 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1500}
1501
1502Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1503 assert(D.Kind == Designator::ArrayRangeDesignator &&
1504 "Requires array range designator");
1505 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1506 Ptr += sizeof(DesignatedInitExpr);
1507 Ptr += sizeof(Designator) * NumDesignators;
1508 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1509 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1510}
1511
1512//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001513// ExprIterator.
1514//===----------------------------------------------------------------------===//
1515
1516Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1517Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1518Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1519const Expr* ConstExprIterator::operator[](size_t idx) const {
1520 return cast<Expr>(I[idx]);
1521}
1522const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1523const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1524
1525//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001526// Child Iterators for iterating over subexpressions/substatements
1527//===----------------------------------------------------------------------===//
1528
1529// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001530Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1531Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001532
Steve Naroff7779db42007-11-12 14:29:37 +00001533// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001534Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1535Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001536
Steve Naroffe3e9add2008-06-02 23:03:37 +00001537// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001538Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1539Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001540
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001541// ObjCKVCRefExpr
1542Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1543Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1544
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001545// ObjCSuperExpr
1546Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1547Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1548
Chris Lattnerd9f69102008-08-10 01:53:14 +00001549// PredefinedExpr
1550Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1551Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001552
1553// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001554Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1555Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001556
1557// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001558Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1559Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001560
1561// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001562Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1563Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001564
Chris Lattner5d661452007-08-26 03:42:43 +00001565// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001566Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1567Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001568
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001569// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001570Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1571Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001572
1573// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001574Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1575Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001576
1577// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001578Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1579Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001580
Sebastian Redl05189992008-11-11 17:56:53 +00001581// SizeOfAlignOfExpr
1582Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1583 // If this is of a type and the type is a VLA type (and not a typedef), the
1584 // size expression of the VLA needs to be treated as an executable expression.
1585 // Why isn't this weirdness documented better in StmtIterator?
1586 if (isArgumentType()) {
1587 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1588 getArgumentType().getTypePtr()))
1589 return child_iterator(T);
1590 return child_iterator();
1591 }
Sebastian Redld4575892008-12-03 23:17:54 +00001592 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001593}
Sebastian Redl05189992008-11-11 17:56:53 +00001594Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1595 if (isArgumentType())
1596 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001597 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001598}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001599
1600// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001601Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001602 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001603}
Ted Kremenek1237c672007-08-24 20:06:47 +00001604Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001605 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001606}
1607
1608// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001609Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001610 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001611}
Ted Kremenek1237c672007-08-24 20:06:47 +00001612Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001613 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001614}
Ted Kremenek1237c672007-08-24 20:06:47 +00001615
1616// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001617Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1618Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001619
Nate Begeman213541a2008-04-18 23:10:10 +00001620// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001621Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1622Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001623
1624// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001625Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1626Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001627
Ted Kremenek1237c672007-08-24 20:06:47 +00001628// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001629Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1630Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001631
1632// BinaryOperator
1633Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001634 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001635}
Ted Kremenek1237c672007-08-24 20:06:47 +00001636Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001637 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001638}
1639
1640// ConditionalOperator
1641Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001642 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001643}
Ted Kremenek1237c672007-08-24 20:06:47 +00001644Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001645 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001646}
1647
1648// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001649Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1650Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001651
Ted Kremenek1237c672007-08-24 20:06:47 +00001652// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001653Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1654Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001655
1656// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001657Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1658 return child_iterator();
1659}
1660
1661Stmt::child_iterator TypesCompatibleExpr::child_end() {
1662 return child_iterator();
1663}
Ted Kremenek1237c672007-08-24 20:06:47 +00001664
1665// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001666Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1667Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001668
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001669// GNUNullExpr
1670Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1671Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1672
Nate Begemane2ce1d92008-01-17 17:46:27 +00001673// OverloadExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001674Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; }
1675Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; }
Nate Begemane2ce1d92008-01-17 17:46:27 +00001676
Eli Friedmand38617c2008-05-14 19:38:39 +00001677// ShuffleVectorExpr
1678Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001679 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001680}
1681Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001682 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001683}
1684
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001685// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001686Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1687Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001688
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001689// InitListExpr
1690Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001691 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001692}
1693Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001694 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001695}
1696
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001697// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001698Stmt::child_iterator DesignatedInitExpr::child_begin() {
1699 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1700 Ptr += sizeof(DesignatedInitExpr);
1701 Ptr += sizeof(Designator) * NumDesignators;
1702 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1703}
1704Stmt::child_iterator DesignatedInitExpr::child_end() {
1705 return child_iterator(&*child_begin() + NumSubExprs);
1706}
1707
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001708// ImplicitValueInitExpr
1709Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1710 return child_iterator();
1711}
1712
1713Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1714 return child_iterator();
1715}
1716
Ted Kremenek1237c672007-08-24 20:06:47 +00001717// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001718Stmt::child_iterator ObjCStringLiteral::child_begin() {
1719 return child_iterator();
1720}
1721Stmt::child_iterator ObjCStringLiteral::child_end() {
1722 return child_iterator();
1723}
Ted Kremenek1237c672007-08-24 20:06:47 +00001724
1725// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001726Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1727Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001728
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001729// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001730Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1731 return child_iterator();
1732}
1733Stmt::child_iterator ObjCSelectorExpr::child_end() {
1734 return child_iterator();
1735}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001736
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001737// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001738Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1739 return child_iterator();
1740}
1741Stmt::child_iterator ObjCProtocolExpr::child_end() {
1742 return child_iterator();
1743}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001744
Steve Naroff563477d2007-09-18 23:55:05 +00001745// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00001746Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001747 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00001748}
1749Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001750 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00001751}
1752
Steve Naroff4eb206b2008-09-03 18:15:37 +00001753// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00001754Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1755Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00001756
Ted Kremenek9da13f92008-09-26 23:24:14 +00001757Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1758Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }