blob: 95a6a349f31d333aa00edd8673900ff53f89c7ed [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 Kremenek8189cde2009-02-07 01:47:29 +000057 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +000058 this->~StringLiteral();
59 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +000060}
61
62bool UnaryOperator::isPostfix(Opcode Op) {
63 switch (Op) {
64 case PostInc:
65 case PostDec:
66 return true;
67 default:
68 return false;
69 }
70}
71
Ted Kremenek5a56ac32008-07-23 22:18:43 +000072bool UnaryOperator::isPrefix(Opcode Op) {
73 switch (Op) {
74 case PreInc:
75 case PreDec:
76 return true;
77 default:
78 return false;
79 }
80}
81
Reid Spencer5f016e22007-07-11 17:01:13 +000082/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
83/// corresponds to, e.g. "sizeof" or "[pre]++".
84const char *UnaryOperator::getOpcodeStr(Opcode Op) {
85 switch (Op) {
86 default: assert(0 && "Unknown unary operator");
87 case PostInc: return "++";
88 case PostDec: return "--";
89 case PreInc: return "++";
90 case PreDec: return "--";
91 case AddrOf: return "&";
92 case Deref: return "*";
93 case Plus: return "+";
94 case Minus: return "-";
95 case Not: return "~";
96 case LNot: return "!";
97 case Real: return "__real";
98 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +000099 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000100 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 }
102}
103
104//===----------------------------------------------------------------------===//
105// Postfix Operators.
106//===----------------------------------------------------------------------===//
107
Ted Kremenek668bf912009-02-09 20:51:47 +0000108CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000109 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000110 : Expr(SC, t,
111 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
112 fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
113 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000114
115 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000116 SubExprs[FN] = fn;
117 for (unsigned i = 0; i != numargs; ++i)
118 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000119
Douglas Gregorb4609802008-11-14 16:09:21 +0000120 RParenLoc = rparenloc;
121}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000122
Ted Kremenek668bf912009-02-09 20:51:47 +0000123CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
124 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000125 : Expr(CallExprClass, t,
126 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
127 fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
128 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000129
130 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000131 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000133 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000134
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 RParenLoc = rparenloc;
136}
137
Ted Kremenek668bf912009-02-09 20:51:47 +0000138void CallExpr::Destroy(ASTContext& C) {
139 DestroyChildren(C);
140 if (SubExprs) C.Deallocate(SubExprs);
141 this->~CallExpr();
142 C.Deallocate(this);
143}
144
Chris Lattnerd18b3292007-12-28 05:25:02 +0000145/// setNumArgs - This changes the number of arguments present in this call.
146/// Any orphaned expressions are deleted by this, and any new operands are set
147/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000148void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000149 // No change, just return.
150 if (NumArgs == getNumArgs()) return;
151
152 // If shrinking # arguments, just delete the extras and forgot them.
153 if (NumArgs < getNumArgs()) {
154 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000155 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000156 this->NumArgs = NumArgs;
157 return;
158 }
159
160 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek55499762008-06-17 02:43:46 +0000161 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000162 // Copy over args.
163 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
164 NewSubExprs[i] = SubExprs[i];
165 // Null out new args.
166 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
167 NewSubExprs[i] = 0;
168
Ted Kremenek8189cde2009-02-07 01:47:29 +0000169 delete [] SubExprs;
Chris Lattnerd18b3292007-12-28 05:25:02 +0000170 SubExprs = NewSubExprs;
171 this->NumArgs = NumArgs;
172}
173
Chris Lattnercb888962008-10-06 05:00:53 +0000174/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
175/// not, return 0.
176unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000177 // All simple function calls (e.g. func()) are implicitly cast to pointer to
178 // function. As a result, we try and obtain the DeclRefExpr from the
179 // ImplicitCastExpr.
180 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
181 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000182 return 0;
183
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000184 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
185 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000186 return 0;
187
Anders Carlssonbcba2012008-01-31 02:13:57 +0000188 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
189 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000190 return 0;
191
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000192 if (!FDecl->getIdentifier())
193 return 0;
194
Chris Lattnercb888962008-10-06 05:00:53 +0000195 return FDecl->getIdentifier()->getBuiltinID();
196}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000197
Chris Lattnercb888962008-10-06 05:00:53 +0000198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
200/// corresponds to, e.g. "<<=".
201const char *BinaryOperator::getOpcodeStr(Opcode Op) {
202 switch (Op) {
203 default: assert(0 && "Unknown binary operator");
204 case Mul: return "*";
205 case Div: return "/";
206 case Rem: return "%";
207 case Add: return "+";
208 case Sub: return "-";
209 case Shl: return "<<";
210 case Shr: return ">>";
211 case LT: return "<";
212 case GT: return ">";
213 case LE: return "<=";
214 case GE: return ">=";
215 case EQ: return "==";
216 case NE: return "!=";
217 case And: return "&";
218 case Xor: return "^";
219 case Or: return "|";
220 case LAnd: return "&&";
221 case LOr: return "||";
222 case Assign: return "=";
223 case MulAssign: return "*=";
224 case DivAssign: return "/=";
225 case RemAssign: return "%=";
226 case AddAssign: return "+=";
227 case SubAssign: return "-=";
228 case ShlAssign: return "<<=";
229 case ShrAssign: return ">>=";
230 case AndAssign: return "&=";
231 case XorAssign: return "^=";
232 case OrAssign: return "|=";
233 case Comma: return ",";
234 }
235}
236
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000237InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000238 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000239 SourceLocation rbraceloc)
Steve Naroffc5ae8992008-05-01 02:04:18 +0000240 : Expr(InitListExprClass, QualType()),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000241 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000242 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000243
244 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000245}
Reid Spencer5f016e22007-07-11 17:01:13 +0000246
Douglas Gregor4c678342009-01-28 21:54:33 +0000247void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
248 for (unsigned Idx = NumInits, LastIdx = InitExprs.size(); Idx < LastIdx; ++Idx)
249 delete InitExprs[Idx];
250 InitExprs.resize(NumInits, 0);
251}
252
253Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
254 if (Init >= InitExprs.size()) {
255 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
256 InitExprs.back() = expr;
257 return 0;
258 }
259
260 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
261 InitExprs[Init] = expr;
262 return Result;
263}
264
Steve Naroffbfdcae62008-09-04 15:31:07 +0000265/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000266///
267const FunctionType *BlockExpr::getFunctionType() const {
268 return getType()->getAsBlockPointerType()->
269 getPointeeType()->getAsFunctionType();
270}
271
Steve Naroff56ee6892008-10-08 17:01:13 +0000272SourceLocation BlockExpr::getCaretLocation() const {
273 return TheBlock->getCaretLocation();
274}
275const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
276Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
277
278
Reid Spencer5f016e22007-07-11 17:01:13 +0000279//===----------------------------------------------------------------------===//
280// Generic Expression Routines
281//===----------------------------------------------------------------------===//
282
283/// hasLocalSideEffect - Return true if this immediate expression has side
284/// effects, not counting any sub-expressions.
285bool Expr::hasLocalSideEffect() const {
286 switch (getStmtClass()) {
287 default:
288 return false;
289 case ParenExprClass:
290 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
291 case UnaryOperatorClass: {
292 const UnaryOperator *UO = cast<UnaryOperator>(this);
293
294 switch (UO->getOpcode()) {
295 default: return false;
296 case UnaryOperator::PostInc:
297 case UnaryOperator::PostDec:
298 case UnaryOperator::PreInc:
299 case UnaryOperator::PreDec:
300 return true; // ++/--
301
302 case UnaryOperator::Deref:
303 // Dereferencing a volatile pointer is a side-effect.
304 return getType().isVolatileQualified();
305 case UnaryOperator::Real:
306 case UnaryOperator::Imag:
307 // accessing a piece of a volatile complex is a side-effect.
308 return UO->getSubExpr()->getType().isVolatileQualified();
309
310 case UnaryOperator::Extension:
311 return UO->getSubExpr()->hasLocalSideEffect();
312 }
313 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000314 case BinaryOperatorClass: {
315 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
316 // Consider comma to have side effects if the LHS and RHS both do.
317 if (BinOp->getOpcode() == BinaryOperator::Comma)
318 return BinOp->getLHS()->hasLocalSideEffect() &&
319 BinOp->getRHS()->hasLocalSideEffect();
320
321 return BinOp->isAssignmentOp();
322 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000323 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000324 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000325
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000326 case ConditionalOperatorClass: {
327 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
328 return Exp->getCond()->hasLocalSideEffect()
329 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
330 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
331 }
332
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 case MemberExprClass:
334 case ArraySubscriptExprClass:
335 // If the base pointer or element is to a volatile pointer/field, accessing
336 // if is a side effect.
337 return getType().isVolatileQualified();
Eli Friedman211f6ad2008-05-27 15:24:04 +0000338
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 case CallExprClass:
Douglas Gregorb4609802008-11-14 16:09:21 +0000340 case CXXOperatorCallExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
342 // should warn.
343 return true;
Chris Lattnera9c01022007-09-26 22:06:30 +0000344 case ObjCMessageExprClass:
345 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000346 case StmtExprClass: {
347 // Statement exprs don't logically have side effects themselves, but are
348 // sometimes used in macros in ways that give them a type that is unused.
349 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
350 // however, if the result of the stmt expr is dead, we don't want to emit a
351 // warning.
352 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
353 if (!CS->body_empty())
354 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
355 return E->hasLocalSideEffect();
356 return false;
357 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000358 case CStyleCastExprClass:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000359 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 // If this is a cast to void, check the operand. Otherwise, the result of
361 // the cast is unused.
362 if (getType()->isVoidType())
363 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
364 return false;
Chris Lattner04421082008-04-08 04:40:51 +0000365
Eli Friedman4be1f472008-05-19 21:24:43 +0000366 case ImplicitCastExprClass:
367 // Check the operand, since implicit casts are inserted by Sema
368 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect();
369
Chris Lattner04421082008-04-08 04:40:51 +0000370 case CXXDefaultArgExprClass:
371 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000372
373 case CXXNewExprClass:
374 // FIXME: In theory, there might be new expressions that don't have side
375 // effects (e.g. a placement new with an uninitialized POD).
376 case CXXDeleteExprClass:
377 return true;
378 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000379}
380
Douglas Gregorba7e2102008-10-22 15:04:37 +0000381/// DeclCanBeLvalue - Determine whether the given declaration can be
382/// an lvalue. This is a helper routine for isLvalue.
383static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000384 // C++ [temp.param]p6:
385 // A non-type non-reference template-parameter is not an lvalue.
386 if (const NonTypeTemplateParmDecl *NTTParm
387 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
388 return NTTParm->getType()->isReferenceType();
389
Douglas Gregor44b43212008-12-11 16:49:14 +0000390 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000391 // C++ 3.10p2: An lvalue refers to an object or function.
392 (Ctx.getLangOptions().CPlusPlus &&
393 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
394}
395
Reid Spencer5f016e22007-07-11 17:01:13 +0000396/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
397/// incomplete type other than void. Nonarray expressions that can be lvalues:
398/// - name, where name must be a variable
399/// - e[i]
400/// - (e), where e must be an lvalue
401/// - e.name, where e must be an lvalue
402/// - e->name
403/// - *e, the type of e cannot be a function type
404/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000405/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000406/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000407///
Chris Lattner28be73f2008-07-26 21:30:36 +0000408Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor98cd5992008-10-21 23:43:52 +0000409 // first, check the type (C99 6.3.2.1). Expressions with function
410 // type in C are not lvalues, but they can be lvalues in C++.
411 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 return LV_NotObjectType;
413
Steve Naroffacb818a2008-02-10 01:39:04 +0000414 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000415 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000416 return LV_IncompleteVoidType;
417
Douglas Gregor98cd5992008-10-21 23:43:52 +0000418 /// FIXME: Expressions can't have reference type, so the following
419 /// isn't needed.
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000420 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000421 return LV_Valid;
422
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 // the type looks fine, now check the expression
424 switch (getStmtClass()) {
425 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000426 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
428 // For vectors, make sure base is an lvalue (i.e. not a function call).
429 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000430 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000432 case DeclRefExprClass:
433 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000434 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
435 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 return LV_Valid;
437 break;
Chris Lattner41110242008-06-17 18:05:57 +0000438 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000439 case BlockDeclRefExprClass: {
440 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000441 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000442 return LV_Valid;
443 break;
444 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000445 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000447 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
448 NamedDecl *Member = m->getMemberDecl();
449 // C++ [expr.ref]p4:
450 // If E2 is declared to have type "reference to T", then E1.E2
451 // is an lvalue.
452 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
453 if (Value->getType()->isReferenceType())
454 return LV_Valid;
455
456 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
457 if (isa<CXXClassVarDecl>(Member))
458 return LV_Valid;
459
460 // -- If E2 is a non-static data member [...]. If E1 is an
461 // lvalue, then E1.E2 is an lvalue.
462 if (isa<FieldDecl>(Member))
463 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
464
465 // -- If it refers to a static member function [...], then
466 // E1.E2 is an lvalue.
467 // -- Otherwise, if E1.E2 refers to a non-static member
468 // function [...], then E1.E2 is not an lvalue.
469 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
470 return Method->isStatic()? LV_Valid : LV_MemberFunction;
471
472 // -- If E2 is a member enumerator [...], the expression E1.E2
473 // is not an lvalue.
474 if (isa<EnumConstantDecl>(Member))
475 return LV_InvalidExpression;
476
477 // Not an lvalue.
478 return LV_InvalidExpression;
479 }
480
481 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000482 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000483 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000484 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000486 return LV_Valid; // C99 6.5.3p4
487
488 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000489 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
490 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000491 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000492
493 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
494 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
495 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
496 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000498 case ImplicitCastExprClass:
499 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
500 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000502 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000503 case BinaryOperatorClass:
504 case CompoundAssignOperatorClass: {
505 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000506
507 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
508 BinOp->getOpcode() == BinaryOperator::Comma)
509 return BinOp->getRHS()->isLvalue(Ctx);
510
Sebastian Redl22460502009-02-07 00:15:38 +0000511 // C++ [expr.mptr.oper]p6
512 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
513 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
514 !BinOp->getType()->isFunctionType())
515 return BinOp->getLHS()->isLvalue(Ctx);
516
Douglas Gregorbf3af052008-11-13 20:12:29 +0000517 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000518 return LV_InvalidExpression;
519
Douglas Gregorbf3af052008-11-13 20:12:29 +0000520 if (Ctx.getLangOptions().CPlusPlus)
521 // C++ [expr.ass]p1:
522 // The result of an assignment operation [...] is an lvalue.
523 return LV_Valid;
524
525
526 // C99 6.5.16:
527 // An assignment expression [...] is not an lvalue.
528 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000529 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000530 // FIXME: OverloadExprClass
Douglas Gregorb4609802008-11-14 16:09:21 +0000531 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000532 case CXXOperatorCallExprClass:
533 case CXXMemberCallExprClass: {
Douglas Gregor9d293df2008-10-28 00:22:11 +0000534 // C++ [expr.call]p10:
535 // A function call is an lvalue if and only if the result type
536 // is a reference.
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000537 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor9d293df2008-10-28 00:22:11 +0000538 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000539 CalleeType = FnTypePtr->getPointeeType();
540 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
541 if (FnType->getResultType()->isReferenceType())
542 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000543
544 break;
545 }
Steve Naroffe6386392007-12-05 04:00:10 +0000546 case CompoundLiteralExprClass: // C99 6.5.2.5p5
547 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000548 case ChooseExprClass:
549 // __builtin_choose_expr is an lvalue if the selected operand is.
550 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
551 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
552 else
553 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
554
Nate Begeman213541a2008-04-18 23:10:10 +0000555 case ExtVectorElementExprClass:
556 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000557 return LV_DuplicateVectorComponents;
558 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000559 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
560 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000561 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
562 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000563 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000564 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000565 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000566 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000567 case VAArgExprClass:
568 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +0000569 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000570 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000571 case CXXConditionDeclExprClass:
572 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000573 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000574 case CXXFunctionalCastExprClass:
575 case CXXStaticCastExprClass:
576 case CXXDynamicCastExprClass:
577 case CXXReinterpretCastExprClass:
578 case CXXConstCastExprClass:
579 // The result of an explicit cast is an lvalue if the type we are
580 // casting to is a reference type. See C++ [expr.cast]p1,
581 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
582 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
583 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
584 return LV_Valid;
585 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000586 case CXXTypeidExprClass:
587 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
588 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 default:
590 break;
591 }
592 return LV_InvalidExpression;
593}
594
595/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
596/// does not have an incomplete type, does not have a const-qualified type, and
597/// if it is a structure or union, does not have any member (including,
598/// recursively, any member or element of all contained aggregates or unions)
599/// with a const-qualified type.
Chris Lattner28be73f2008-07-26 21:30:36 +0000600Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
601 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000602
603 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000604 case LV_Valid:
605 // C++ 3.10p11: Functions cannot be modified, but pointers to
606 // functions can be modifiable.
607 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
608 return MLV_NotObjectType;
609 break;
610
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 case LV_NotObjectType: return MLV_NotObjectType;
612 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000613 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000614 case LV_InvalidExpression:
615 // If the top level is a C-style cast, and the subexpression is a valid
616 // lvalue, then this is probably a use of the old-school "cast as lvalue"
617 // GCC extension. We don't support it, but we want to produce good
618 // diagnostics when it happens so that the user knows why.
619 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
620 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
621 return MLV_LValueCast;
622 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000623 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000625
626 QualType CT = Ctx.getCanonicalType(getType());
627
628 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000630 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000632 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 return MLV_IncompleteType;
634
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000635 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 if (r->hasConstFields())
637 return MLV_ConstQualified;
638 }
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000639 // The following is illegal:
640 // void takeclosure(void (^C)(void));
641 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
642 //
643 if (getStmtClass() == BlockDeclRefExprClass) {
644 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
645 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
646 return MLV_NotBlockQualified;
647 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000648
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000649 // Assigning to an 'implicit' property?
Fariborz Jahanian6669db92008-11-25 17:56:43 +0000650 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000651 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
652 if (KVCExpr->getSetterMethod() == 0)
653 return MLV_NoSetterProperty;
654 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 return MLV_Valid;
656}
657
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000658/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000659/// duration. This means that the address of this expression is a link-time
660/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000661bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000662 switch (getStmtClass()) {
663 default:
664 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000665 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000666 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000667 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000668 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000669 case CompoundLiteralExprClass:
670 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000671 case DeclRefExprClass:
672 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000673 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
674 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000675 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000676 if (isa<FunctionDecl>(D))
677 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000678 return false;
679 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000680 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000681 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000682 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000683 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000684 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000685 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000686 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000687 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000688 case CXXDefaultArgExprClass:
689 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000690 }
691}
692
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000693Expr* Expr::IgnoreParens() {
694 Expr* E = this;
695 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
696 E = P->getSubExpr();
697
698 return E;
699}
700
Chris Lattner56f34942008-02-13 01:02:39 +0000701/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
702/// or CastExprs or ImplicitCastExprs, returning their operand.
703Expr *Expr::IgnoreParenCasts() {
704 Expr *E = this;
705 while (true) {
706 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
707 E = P->getSubExpr();
708 else if (CastExpr *P = dyn_cast<CastExpr>(E))
709 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +0000710 else
711 return E;
712 }
713}
714
Douglas Gregor898574e2008-12-05 23:32:09 +0000715/// hasAnyTypeDependentArguments - Determines if any of the expressions
716/// in Exprs is type-dependent.
717bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
718 for (unsigned I = 0; I < NumExprs; ++I)
719 if (Exprs[I]->isTypeDependent())
720 return true;
721
722 return false;
723}
724
725/// hasAnyValueDependentArguments - Determines if any of the expressions
726/// in Exprs is value-dependent.
727bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
728 for (unsigned I = 0; I < NumExprs; ++I)
729 if (Exprs[I]->isValueDependent())
730 return true;
731
732 return false;
733}
734
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000735bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000736 // This function is attempting whether an expression is an initializer
737 // which can be evaluated at compile-time. isEvaluatable handles most
738 // of the cases, but it can't deal with some initializer-specific
739 // expressions, and it can't deal with aggregates; we deal with those here,
740 // and fall back to isEvaluatable for the other cases.
741
Anders Carlssone8a32b82008-11-24 05:23:59 +0000742 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000743 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000744 case StringLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +0000745 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +0000746 case CompoundLiteralExprClass: {
747 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000748 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +0000749 }
Anders Carlssone8a32b82008-11-24 05:23:59 +0000750 case InitListExprClass: {
751 const InitListExpr *Exp = cast<InitListExpr>(this);
752 unsigned numInits = Exp->getNumInits();
753 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000754 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +0000755 return false;
756 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000757 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000758 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000759 case ImplicitValueInitExprClass:
760 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000761 case ParenExprClass: {
762 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
763 }
764 case UnaryOperatorClass: {
765 const UnaryOperator* Exp = cast<UnaryOperator>(this);
766 if (Exp->getOpcode() == UnaryOperator::Extension)
767 return Exp->getSubExpr()->isConstantInitializer(Ctx);
768 break;
769 }
770 case CStyleCastExprClass:
771 // Handle casts with a destination that's a struct or union; this
772 // deals with both the gcc no-op struct cast extension and the
773 // cast-to-union extension.
774 if (getType()->isRecordType())
775 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
776 break;
Eli Friedman32a311e2009-01-25 03:27:40 +0000777 case DesignatedInitExprClass:
Sebastian Redl4e716e02009-01-25 13:34:47 +0000778 return cast<DesignatedInitExpr>(this)->
779 getInit()->isConstantInitializer(Ctx);
Anders Carlssone8a32b82008-11-24 05:23:59 +0000780 }
781
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000782 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +0000783}
784
Reid Spencer5f016e22007-07-11 17:01:13 +0000785/// isIntegerConstantExpr - this recursive routine will test if an expression is
786/// an integer constant expression. Note: With the introduction of VLA's in
787/// C99 the result of the sizeof operator is no longer always a constant
788/// expression. The generalization of the wording to include any subexpression
789/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
790/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopes5f6b6322008-07-08 21:13:06 +0000791/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Reid Spencer5f016e22007-07-11 17:01:13 +0000792/// occurring in a context requiring a constant, would have been a constraint
793/// violation. FIXME: This routine currently implements C90 semantics.
794/// To properly implement C99 semantics this routine will need to evaluate
795/// expressions involving operators previously mentioned.
796
797/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
798/// comma, etc
799///
800/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000801/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000802///
803/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
804/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
805/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000806bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
807 SourceLocation *Loc, bool isEvaluated) const {
Eli Friedmana6afa762008-11-13 06:09:17 +0000808 // Pretest for integral type; some parts of the code crash for types that
809 // can't be sized.
810 if (!getType()->isIntegralType()) {
811 if (Loc) *Loc = getLocStart();
812 return false;
813 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 switch (getStmtClass()) {
815 default:
816 if (Loc) *Loc = getLocStart();
817 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 case ParenExprClass:
819 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000820 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 case IntegerLiteralClass:
822 Result = cast<IntegerLiteral>(this)->getValue();
823 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000824 case CharacterLiteralClass: {
825 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner98be4942008-03-05 18:54:05 +0000826 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000827 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000828 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000830 }
Anders Carlssonb88d45e2008-08-23 21:12:35 +0000831 case CXXBoolLiteralExprClass: {
832 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
833 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
834 Result = BL->getValue();
835 Result.setIsUnsigned(!getType()->isSignedIntegerType());
836 break;
837 }
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000838 case CXXZeroInitValueExprClass:
839 Result.clear();
840 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000841 case TypesCompatibleExprClass: {
842 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner98be4942008-03-05 18:54:05 +0000843 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Daniel Dunbarac620de2008-10-24 08:07:57 +0000844 // Per gcc docs "this built-in function ignores top level
845 // qualifiers". We need to use the canonical version to properly
846 // be able to strip CRV qualifiers from the type.
847 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
848 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
849 Result = Ctx.typesAreCompatible(T0.getUnqualifiedType(),
850 T1.getUnqualifiedType());
Steve Naroff389cecc2007-08-02 00:13:27 +0000851 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000852 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000853 case CallExprClass:
854 case CXXOperatorCallExprClass: {
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000855 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner98be4942008-03-05 18:54:05 +0000856 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattnera4d55d82008-10-06 06:40:35 +0000857
858 // If this is a call to a builtin function, constant fold it otherwise
859 // reject it.
860 if (CE->isBuiltinCall()) {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +0000861 EvalResult EvalResult;
862 if (CE->Evaluate(EvalResult, Ctx)) {
863 assert(!EvalResult.HasSideEffects &&
864 "Foldable builtin call should not have side effects!");
865 Result = EvalResult.Val.getInt();
Chris Lattnera4d55d82008-10-06 06:40:35 +0000866 break; // It is a constant, expand it.
867 }
868 }
869
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000870 if (Loc) *Loc = getLocStart();
871 return false;
872 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 case DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000874 case QualifiedDeclRefExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 if (const EnumConstantDecl *D =
876 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
877 Result = D->getInitVal();
878 break;
879 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +0000880 if (Ctx.getLangOptions().CPlusPlus &&
881 getType().getCVRQualifiers() == QualType::Const) {
882 // C++ 7.1.5.1p2
883 // A variable of non-volatile const-qualified integral or enumeration
884 // type initialized by an ICE can be used in ICEs.
885 if (const VarDecl *Dcl =
886 dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) {
887 if (const Expr *Init = Dcl->getInit())
888 return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
889 }
890 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 if (Loc) *Loc = getLocStart();
892 return false;
893 case UnaryOperatorClass: {
894 const UnaryOperator *Exp = cast<UnaryOperator>(this);
895
Sebastian Redl05189992008-11-11 17:56:53 +0000896 // Get the operand value. If this is offsetof, do not evalute the
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 // operand. This affects C99 6.6p3.
Sebastian Redl05189992008-11-11 17:56:53 +0000898 if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
899 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 return false;
901
902 switch (Exp->getOpcode()) {
903 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
904 // See C99 6.6p3.
905 default:
906 if (Loc) *Loc = Exp->getOperatorLoc();
907 return false;
908 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000909 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 case UnaryOperator::LNot: {
Chris Lattnerbf755382008-01-25 19:16:19 +0000911 bool Val = Result == 0;
Chris Lattner98be4942008-03-05 18:54:05 +0000912 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 Result = Val;
914 break;
915 }
916 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 break;
918 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 Result = -Result;
920 break;
921 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 Result = ~Result;
923 break;
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000924 case UnaryOperator::OffsetOf:
Daniel Dunbaraa1f9f12008-08-28 18:42:20 +0000925 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000926 Result = Exp->evaluateOffsetOf(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 }
928 break;
929 }
Sebastian Redl05189992008-11-11 17:56:53 +0000930 case SizeOfAlignOfExprClass: {
931 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
Chris Lattnera269ebf2008-02-21 05:45:29 +0000932
933 // Return the result in the right width.
Chris Lattner98be4942008-03-05 18:54:05 +0000934 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattnera269ebf2008-02-21 05:45:29 +0000935
Sebastian Redl05189992008-11-11 17:56:53 +0000936 QualType ArgTy = Exp->getTypeOfArgument();
Chris Lattnera269ebf2008-02-21 05:45:29 +0000937 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Sebastian Redl05189992008-11-11 17:56:53 +0000938 if (ArgTy->isVoidType()) {
Chris Lattnera269ebf2008-02-21 05:45:29 +0000939 Result = 1;
940 break;
941 }
942
943 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Sebastian Redl05189992008-11-11 17:56:53 +0000944 if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) {
Chris Lattner65383472007-12-18 07:15:40 +0000945 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000947 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000948
Chris Lattner76e773a2007-07-18 18:38:36 +0000949 // Get information about the size or align.
Sebastian Redl05189992008-11-11 17:56:53 +0000950 if (ArgTy->isFunctionType()) {
Chris Lattnerefdd1572008-01-02 21:54:09 +0000951 // GCC extension: sizeof(function) = 1.
952 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000953 } else {
Chris Lattner98be4942008-03-05 18:54:05 +0000954 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000955 if (Exp->isSizeOf())
Sebastian Redl05189992008-11-11 17:56:53 +0000956 Result = Ctx.getTypeSize(ArgTy) / CharSize;
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000957 else
Sebastian Redl05189992008-11-11 17:56:53 +0000958 Result = Ctx.getTypeAlign(ArgTy) / CharSize;
Ted Kremenek060e4702007-12-17 17:38:43 +0000959 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 break;
961 }
962 case BinaryOperatorClass: {
963 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbare1226d22008-09-22 23:53:24 +0000964 llvm::APSInt LHS, RHS;
965
966 // Initialize result to have correct signedness and width.
967 Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())),
Eli Friedmanb11e7782008-11-13 02:13:11 +0000968 !getType()->isSignedIntegerType());
969
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbare1226d22008-09-22 23:53:24 +0000971 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 return false;
973
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 // The short-circuiting &&/|| operators don't necessarily evaluate their
975 // RHS. Make sure to pass isEvaluated down correctly.
976 if (Exp->isLogicalOp()) {
977 bool RHSEval;
978 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbare1226d22008-09-22 23:53:24 +0000979 RHSEval = LHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 else {
981 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbare1226d22008-09-22 23:53:24 +0000982 RHSEval = LHS == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 }
984
Chris Lattner590b6642007-07-15 23:26:56 +0000985 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 isEvaluated & RHSEval))
987 return false;
988 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000989 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 return false;
991 }
992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 switch (Exp->getOpcode()) {
994 default:
995 if (Loc) *Loc = getLocStart();
996 return false;
997 case BinaryOperator::Mul:
Daniel Dunbare1226d22008-09-22 23:53:24 +0000998 Result = LHS * RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 break;
1000 case BinaryOperator::Div:
1001 if (RHS == 0) {
1002 if (!isEvaluated) break;
1003 if (Loc) *Loc = getLocStart();
1004 return false;
1005 }
Daniel Dunbare1226d22008-09-22 23:53:24 +00001006 Result = LHS / RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 break;
1008 case BinaryOperator::Rem:
1009 if (RHS == 0) {
1010 if (!isEvaluated) break;
1011 if (Loc) *Loc = getLocStart();
1012 return false;
1013 }
Daniel Dunbare1226d22008-09-22 23:53:24 +00001014 Result = LHS % RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 break;
Daniel Dunbare1226d22008-09-22 23:53:24 +00001016 case BinaryOperator::Add: Result = LHS + RHS; break;
1017 case BinaryOperator::Sub: Result = LHS - RHS; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 case BinaryOperator::Shl:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001019 Result = LHS <<
1020 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
1021 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 case BinaryOperator::Shr:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001023 Result = LHS >>
1024 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 break;
Daniel Dunbare1226d22008-09-22 23:53:24 +00001026 case BinaryOperator::LT: Result = LHS < RHS; break;
1027 case BinaryOperator::GT: Result = LHS > RHS; break;
1028 case BinaryOperator::LE: Result = LHS <= RHS; break;
1029 case BinaryOperator::GE: Result = LHS >= RHS; break;
1030 case BinaryOperator::EQ: Result = LHS == RHS; break;
1031 case BinaryOperator::NE: Result = LHS != RHS; break;
1032 case BinaryOperator::And: Result = LHS & RHS; break;
1033 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1034 case BinaryOperator::Or: Result = LHS | RHS; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 case BinaryOperator::LAnd:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001036 Result = LHS != 0 && RHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 break;
1038 case BinaryOperator::LOr:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001039 Result = LHS != 0 || RHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 break;
Eli Friedmanb11e7782008-11-13 02:13:11 +00001041
1042 case BinaryOperator::Comma:
1043 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1044 // *except* when they are contained within a subexpression that is not
1045 // evaluated". Note that Assignment can never happen due to constraints
1046 // on the LHS subexpr, so we don't need to check it here.
1047 if (isEvaluated) {
1048 if (Loc) *Loc = getLocStart();
1049 return false;
1050 }
1051
1052 // The result of the constant expr is the RHS.
1053 Result = RHS;
1054 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 }
1056
1057 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
1058 break;
1059 }
Chris Lattner26dc7b32007-07-15 23:54:50 +00001060 case ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001061 case CStyleCastExprClass:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001062 case CXXFunctionalCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001063 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
1064 SourceLocation CastLoc = getLocStart();
Chris Lattner26dc7b32007-07-15 23:54:50 +00001065
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001067 if (!SubExpr->getType()->isArithmeticType() ||
1068 !getType()->isIntegerType()) {
1069 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 return false;
1071 }
Chris Lattner987b15d2007-09-22 19:04:13 +00001072
Chris Lattner98be4942008-03-05 18:54:05 +00001073 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner987b15d2007-09-22 19:04:13 +00001074
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001076 if (SubExpr->getType()->isIntegerType()) {
1077 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +00001079
1080 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001081 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001082 if (getType()->isBooleanType()) {
1083 // Conversion to bool compares against zero.
1084 Result = Result != 0;
1085 Result.zextOrTrunc(DestWidth);
1086 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner26dc7b32007-07-15 23:54:50 +00001087 Result.sextOrTrunc(DestWidth);
1088 else // If the input is unsigned, do a zero extend, noop, or truncate.
1089 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 break;
1091 }
1092
1093 // Allow floating constants that are the immediate operands of casts or that
1094 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001095 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
1097 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +00001098
1099 // If this isn't a floating literal, we can't handle it.
1100 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1101 if (!FL) {
1102 if (Loc) *Loc = Operand->getLocStart();
1103 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 }
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001105
1106 // If the destination is boolean, compare against zero.
1107 if (getType()->isBooleanType()) {
1108 Result = !FL->getValue().isZero();
1109 Result.zextOrTrunc(DestWidth);
1110 break;
1111 }
Chris Lattner987b15d2007-09-22 19:04:13 +00001112
1113 // Determine whether we are converting to unsigned or signed.
1114 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +00001115
1116 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1117 // be called multiple times per AST.
Dale Johannesenee5a7002008-10-09 23:02:32 +00001118 uint64_t Space[4];
1119 bool ignored;
Chris Lattnerccc213f2007-09-26 00:47:26 +00001120 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesenee5a7002008-10-09 23:02:32 +00001121 llvm::APFloat::rmTowardZero,
1122 &ignored);
Chris Lattner987b15d2007-09-22 19:04:13 +00001123 Result = llvm::APInt(DestWidth, 4, Space);
1124 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 }
1126 case ConditionalOperatorClass: {
1127 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1128
Chris Lattner28daa532008-12-12 06:55:44 +00001129 const Expr *Cond = Exp->getCond();
1130
1131 if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 return false;
1133
1134 const Expr *TrueExp = Exp->getLHS();
1135 const Expr *FalseExp = Exp->getRHS();
1136 if (Result == 0) std::swap(TrueExp, FalseExp);
1137
Chris Lattner28daa532008-12-12 06:55:44 +00001138 // If the condition (ignoring parens) is a __builtin_constant_p call,
1139 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001140 // expression, and it is fully evaluated. This is an important GNU
1141 // extension. See GCC PR38377 for discussion.
Chris Lattner28daa532008-12-12 06:55:44 +00001142 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
Chris Lattner42b83dd2008-12-12 18:00:51 +00001143 if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) {
1144 EvalResult EVResult;
1145 if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
1146 return false;
1147 assert(EVResult.Val.isInt() && "FP conditional expr not expected");
1148 Result = EVResult.Val.getInt();
1149 if (Loc) *Loc = EVResult.DiagLoc;
1150 return true;
1151 }
Chris Lattner28daa532008-12-12 06:55:44 +00001152
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 // Evaluate the false one first, discard the result.
Anders Carlsson39073232007-11-30 19:04:31 +00001154 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 return false;
1156 // Evalute the true one, capture the result.
Anders Carlsson39073232007-11-30 19:04:31 +00001157 if (TrueExp &&
1158 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 break;
1161 }
Chris Lattner04421082008-04-08 04:40:51 +00001162 case CXXDefaultArgExprClass:
1163 return cast<CXXDefaultArgExpr>(this)
1164 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001165
1166 case UnaryTypeTraitExprClass:
1167 Result = cast<UnaryTypeTraitExpr>(this)->Evaluate();
1168 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 }
1170
1171 // Cases that are valid constant exprs fall through to here.
1172 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1173 return true;
1174}
1175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1177/// integer constant expression with the value zero, or if this is one that is
1178/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001179bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1180{
Sebastian Redl07779722008-10-31 14:43:28 +00001181 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001182 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001183 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001184 // Check that it is a cast to void*.
1185 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1186 QualType Pointee = PT->getPointeeType();
1187 if (Pointee.getCVRQualifiers() == 0 &&
1188 Pointee->isVoidType() && // to void*
1189 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001190 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001191 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001193 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1194 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001195 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001196 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1197 // Accept ((void*)0) as a null pointer constant, as many other
1198 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001199 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001200 } else if (const CXXDefaultArgExpr *DefaultArg
1201 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001202 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001203 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001204 } else if (isa<GNUNullExpr>(this)) {
1205 // The GNU __null extension is always a null pointer constant.
1206 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001207 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001208
Steve Naroffaa58f002008-01-14 16:10:57 +00001209 // This expression must be an integer type.
1210 if (!getType()->isIntegerType())
1211 return false;
1212
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 // If we have an integer constant expression, we need to *evaluate* it and
1214 // test for the value 0.
Anders Carlssond2652772008-12-01 06:28:23 +00001215 // FIXME: We should probably return false if we're compiling in strict mode
1216 // and Diag is not null (this indicates that the value was foldable but not
1217 // an ICE.
1218 EvalResult Result;
Anders Carlssonefa9b382008-12-01 02:13:57 +00001219 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssond2652772008-12-01 06:28:23 +00001220 Result.Val.isInt() && Result.Val.getInt() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001221}
Steve Naroff31a45842007-07-28 23:10:27 +00001222
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001223/// isBitField - Return true if this expression is a bit-field.
1224bool Expr::isBitField() {
1225 Expr *E = this->IgnoreParenCasts();
1226 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001227 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1228 return Field->isBitField();
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001229 return false;
1230}
1231
Nate Begeman213541a2008-04-18 23:10:10 +00001232unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001233 if (const VectorType *VT = getType()->getAsVectorType())
1234 return VT->getNumElements();
1235 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001236}
1237
Nate Begeman8a997642008-05-09 06:41:27 +00001238/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001239bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001240 const char *compStr = Accessor.getName();
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001241 unsigned length = Accessor.getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001242
1243 // Halving swizzles do not contain duplicate elements.
1244 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1245 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1246 return false;
1247
1248 // Advance past s-char prefix on hex swizzles.
1249 if (*compStr == 's') {
1250 compStr++;
1251 length--;
1252 }
Steve Narofffec0b492007-07-30 03:29:09 +00001253
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001254 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001255 const char *s = compStr+i;
1256 for (const char c = *s++; *s; s++)
1257 if (c == *s)
1258 return true;
1259 }
1260 return false;
1261}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001262
Nate Begeman8a997642008-05-09 06:41:27 +00001263/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001264void ExtVectorElementExpr::getEncodedElementAccess(
1265 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001266 const char *compStr = Accessor.getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001267 if (*compStr == 's')
1268 compStr++;
1269
1270 bool isHi = !strcmp(compStr, "hi");
1271 bool isLo = !strcmp(compStr, "lo");
1272 bool isEven = !strcmp(compStr, "even");
1273 bool isOdd = !strcmp(compStr, "odd");
1274
Nate Begeman8a997642008-05-09 06:41:27 +00001275 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1276 uint64_t Index;
1277
1278 if (isHi)
1279 Index = e + i;
1280 else if (isLo)
1281 Index = i;
1282 else if (isEven)
1283 Index = 2 * i;
1284 else if (isOdd)
1285 Index = 2 * i + 1;
1286 else
1287 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001288
Nate Begeman3b8d1162008-05-13 21:03:02 +00001289 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001290 }
Nate Begeman8a997642008-05-09 06:41:27 +00001291}
1292
Steve Naroff68d331a2007-09-27 14:38:14 +00001293// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001294ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001295 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001296 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001297 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001298 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001299 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001300 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001301 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001302 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001303 if (NumArgs) {
1304 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001305 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1306 }
Steve Naroff563477d2007-09-18 23:55:05 +00001307 LBracloc = LBrac;
1308 RBracloc = RBrac;
1309}
1310
Steve Naroff68d331a2007-09-27 14:38:14 +00001311// constructor for class messages.
1312// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001313ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001314 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001315 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001316 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001317 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001318 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001319 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001320 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001321 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001322 if (NumArgs) {
1323 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001324 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1325 }
Steve Naroff563477d2007-09-18 23:55:05 +00001326 LBracloc = LBrac;
1327 RBracloc = RBrac;
1328}
1329
Ted Kremenek4df728e2008-06-24 15:50:53 +00001330// constructor for class messages.
1331ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1332 QualType retType, ObjCMethodDecl *mproto,
1333 SourceLocation LBrac, SourceLocation RBrac,
1334 Expr **ArgExprs, unsigned nargs)
1335: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1336MethodProto(mproto) {
1337 NumArgs = nargs;
1338 SubExprs = new Stmt*[NumArgs+1];
1339 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1340 if (NumArgs) {
1341 for (unsigned i = 0; i != NumArgs; ++i)
1342 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1343 }
1344 LBracloc = LBrac;
1345 RBracloc = RBrac;
1346}
1347
1348ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1349 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1350 switch (x & Flags) {
1351 default:
1352 assert(false && "Invalid ObjCMessageExpr.");
1353 case IsInstMeth:
1354 return ClassInfo(0, 0);
1355 case IsClsMethDeclUnknown:
1356 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1357 case IsClsMethDeclKnown: {
1358 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1359 return ClassInfo(D, D->getIdentifier());
1360 }
1361 }
1362}
1363
Chris Lattner27437ca2007-10-25 00:29:32 +00001364bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001365 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001366}
1367
Chris Lattner670a62c2008-12-12 05:35:08 +00001368static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001369 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1370 QualType Ty = ME->getBase()->getType();
1371
1372 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner98be4942008-03-05 18:54:05 +00001373 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +00001374 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
1375 // FIXME: This is linear time. And the fact that we're indexing
1376 // into the layout by position in the record means that we're
1377 // either stuck numbering the fields in the AST or we have to keep
1378 // the linear search (yuck and yuck).
1379 unsigned i = 0;
1380 for (RecordDecl::field_iterator Field = RD->field_begin(),
1381 FieldEnd = RD->field_end();
1382 Field != FieldEnd; (void)++Field, ++i) {
1383 if (*Field == FD)
1384 break;
1385 }
1386
1387 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001388 }
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001389 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1390 const Expr *Base = ASE->getBase();
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001391
Chris Lattner98be4942008-03-05 18:54:05 +00001392 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001393 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001394
1395 return size + evaluateOffsetOf(C, Base);
1396 } else if (isa<CompoundLiteralExpr>(E))
1397 return 0;
1398
1399 assert(0 && "Unknown offsetof subexpression!");
1400 return 0;
1401}
1402
1403int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1404{
1405 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1406
Chris Lattner98be4942008-03-05 18:54:05 +00001407 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek55499762008-06-17 02:43:46 +00001408 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001409}
1410
Sebastian Redl05189992008-11-11 17:56:53 +00001411void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1412 // Override default behavior of traversing children. If this has a type
1413 // operand and the type is a variable-length array, the child iteration
1414 // will iterate over the size expression. However, this expression belongs
1415 // to the type, not to this, so we don't want to delete it.
1416 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001417 if (isArgumentType()) {
1418 this->~SizeOfAlignOfExpr();
1419 C.Deallocate(this);
1420 }
Sebastian Redl05189992008-11-11 17:56:53 +00001421 else
1422 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001423}
1424
Ted Kremenekfb7413f2009-02-09 17:08:14 +00001425void OverloadExpr::Destroy(ASTContext& C) {
1426 DestroyChildren(C);
1427 C.Deallocate(SubExprs);
1428 this->~OverloadExpr();
1429 C.Deallocate(this);
1430}
1431
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001432//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001433// DesignatedInitExpr
1434//===----------------------------------------------------------------------===//
1435
1436IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1437 assert(Kind == FieldDesignator && "Only valid on a field designator");
1438 if (Field.NameOrField & 0x01)
1439 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1440 else
1441 return getField()->getIdentifier();
1442}
1443
1444DesignatedInitExpr *
1445DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1446 unsigned NumDesignators,
1447 Expr **IndexExprs, unsigned NumIndexExprs,
1448 SourceLocation ColonOrEqualLoc,
1449 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001450 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1451 sizeof(Designator) * NumDesignators +
1452 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001453 DesignatedInitExpr *DIE
1454 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1455 ColonOrEqualLoc, UsesColonSyntax,
1456 NumIndexExprs + 1);
1457
1458 // Fill in the designators
1459 unsigned ExpectedNumSubExprs = 0;
1460 designators_iterator Desig = DIE->designators_begin();
1461 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1462 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1463 if (Designators[Idx].isArrayDesignator())
1464 ++ExpectedNumSubExprs;
1465 else if (Designators[Idx].isArrayRangeDesignator())
1466 ExpectedNumSubExprs += 2;
1467 }
1468 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1469
1470 // Fill in the subexpressions, including the initializer expression.
1471 child_iterator Child = DIE->child_begin();
1472 *Child++ = Init;
1473 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1474 *Child = IndexExprs[Idx];
1475
1476 return DIE;
1477}
1478
1479SourceRange DesignatedInitExpr::getSourceRange() const {
1480 SourceLocation StartLoc;
1481 Designator &First = *const_cast<DesignatedInitExpr*>(this)->designators_begin();
1482 if (First.isFieldDesignator()) {
1483 if (UsesColonSyntax)
1484 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1485 else
1486 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1487 } else
1488 StartLoc = SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
1489 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1490}
1491
1492DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_begin() {
1493 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1494 Ptr += sizeof(DesignatedInitExpr);
1495 return static_cast<Designator*>(static_cast<void*>(Ptr));
1496}
1497
1498DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1499 return designators_begin() + NumDesignators;
1500}
1501
1502Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1503 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1504 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1505 Ptr += sizeof(DesignatedInitExpr);
1506 Ptr += sizeof(Designator) * NumDesignators;
1507 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1508 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1509}
1510
1511Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1512 assert(D.Kind == Designator::ArrayRangeDesignator &&
1513 "Requires array range designator");
1514 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1515 Ptr += sizeof(DesignatedInitExpr);
1516 Ptr += sizeof(Designator) * NumDesignators;
1517 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1518 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1519}
1520
1521Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1522 assert(D.Kind == Designator::ArrayRangeDesignator &&
1523 "Requires array range designator");
1524 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1525 Ptr += sizeof(DesignatedInitExpr);
1526 Ptr += sizeof(Designator) * NumDesignators;
1527 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1528 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1529}
1530
1531//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001532// ExprIterator.
1533//===----------------------------------------------------------------------===//
1534
1535Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1536Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1537Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1538const Expr* ConstExprIterator::operator[](size_t idx) const {
1539 return cast<Expr>(I[idx]);
1540}
1541const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1542const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1543
1544//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001545// Child Iterators for iterating over subexpressions/substatements
1546//===----------------------------------------------------------------------===//
1547
1548// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001549Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1550Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001551
Steve Naroff7779db42007-11-12 14:29:37 +00001552// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001553Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1554Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001555
Steve Naroffe3e9add2008-06-02 23:03:37 +00001556// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001557Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1558Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001559
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001560// ObjCKVCRefExpr
1561Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1562Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1563
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001564// ObjCSuperExpr
1565Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1566Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1567
Chris Lattnerd9f69102008-08-10 01:53:14 +00001568// PredefinedExpr
1569Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1570Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001571
1572// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001573Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1574Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001575
1576// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001577Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1578Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001579
1580// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001581Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1582Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001583
Chris Lattner5d661452007-08-26 03:42:43 +00001584// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001585Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1586Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001587
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001588// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001589Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1590Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001591
1592// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001593Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1594Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001595
1596// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001597Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1598Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001599
Sebastian Redl05189992008-11-11 17:56:53 +00001600// SizeOfAlignOfExpr
1601Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1602 // If this is of a type and the type is a VLA type (and not a typedef), the
1603 // size expression of the VLA needs to be treated as an executable expression.
1604 // Why isn't this weirdness documented better in StmtIterator?
1605 if (isArgumentType()) {
1606 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1607 getArgumentType().getTypePtr()))
1608 return child_iterator(T);
1609 return child_iterator();
1610 }
Sebastian Redld4575892008-12-03 23:17:54 +00001611 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001612}
Sebastian Redl05189992008-11-11 17:56:53 +00001613Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1614 if (isArgumentType())
1615 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001616 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001617}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001618
1619// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001620Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001621 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001622}
Ted Kremenek1237c672007-08-24 20:06:47 +00001623Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001624 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001625}
1626
1627// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001628Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001629 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001630}
Ted Kremenek1237c672007-08-24 20:06:47 +00001631Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001632 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001633}
Ted Kremenek1237c672007-08-24 20:06:47 +00001634
1635// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001636Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1637Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001638
Nate Begeman213541a2008-04-18 23:10:10 +00001639// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001640Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1641Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001642
1643// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001644Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1645Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001646
Ted Kremenek1237c672007-08-24 20:06:47 +00001647// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001648Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1649Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001650
1651// BinaryOperator
1652Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001653 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001654}
Ted Kremenek1237c672007-08-24 20:06:47 +00001655Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001656 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001657}
1658
1659// ConditionalOperator
1660Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001661 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001662}
Ted Kremenek1237c672007-08-24 20:06:47 +00001663Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001664 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001665}
1666
1667// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001668Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1669Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001670
Ted Kremenek1237c672007-08-24 20:06:47 +00001671// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001672Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1673Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001674
1675// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001676Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1677 return child_iterator();
1678}
1679
1680Stmt::child_iterator TypesCompatibleExpr::child_end() {
1681 return child_iterator();
1682}
Ted Kremenek1237c672007-08-24 20:06:47 +00001683
1684// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001685Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1686Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001687
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001688// GNUNullExpr
1689Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1690Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1691
Nate Begemane2ce1d92008-01-17 17:46:27 +00001692// OverloadExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001693Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; }
1694Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; }
Nate Begemane2ce1d92008-01-17 17:46:27 +00001695
Eli Friedmand38617c2008-05-14 19:38:39 +00001696// ShuffleVectorExpr
1697Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001698 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001699}
1700Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001701 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001702}
1703
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001704// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001705Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1706Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001707
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001708// InitListExpr
1709Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001710 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001711}
1712Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001713 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001714}
1715
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001716// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001717Stmt::child_iterator DesignatedInitExpr::child_begin() {
1718 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1719 Ptr += sizeof(DesignatedInitExpr);
1720 Ptr += sizeof(Designator) * NumDesignators;
1721 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1722}
1723Stmt::child_iterator DesignatedInitExpr::child_end() {
1724 return child_iterator(&*child_begin() + NumSubExprs);
1725}
1726
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001727// ImplicitValueInitExpr
1728Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1729 return child_iterator();
1730}
1731
1732Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1733 return child_iterator();
1734}
1735
Ted Kremenek1237c672007-08-24 20:06:47 +00001736// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001737Stmt::child_iterator ObjCStringLiteral::child_begin() {
1738 return child_iterator();
1739}
1740Stmt::child_iterator ObjCStringLiteral::child_end() {
1741 return child_iterator();
1742}
Ted Kremenek1237c672007-08-24 20:06:47 +00001743
1744// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001745Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1746Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001747
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001748// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001749Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1750 return child_iterator();
1751}
1752Stmt::child_iterator ObjCSelectorExpr::child_end() {
1753 return child_iterator();
1754}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001755
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001756// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001757Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1758 return child_iterator();
1759}
1760Stmt::child_iterator ObjCProtocolExpr::child_end() {
1761 return child_iterator();
1762}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001763
Steve Naroff563477d2007-09-18 23:55:05 +00001764// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00001765Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001766 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00001767}
1768Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001769 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00001770}
1771
Steve Naroff4eb206b2008-09-03 18:15:37 +00001772// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00001773Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1774Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00001775
Ted Kremenek9da13f92008-09-26 23:24:14 +00001776Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1777Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }