blob: e30925970db12dc46551fc34f771bd54a1a3d55f [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar64789f82008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Chris Lattnere0391b22008-06-07 22:13:43 +000029/// getValueAsApproximateDouble - This returns the value as an inaccurate
30/// double. Note that this may cause loss of precision, but is useful for
31/// debugging dumps, etc.
32double FloatingLiteral::getValueAsApproximateDouble() const {
33 llvm::APFloat V = getValue();
Dale Johannesen2461f612008-10-09 23:02:32 +000034 bool ignored;
35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000037 return V.convertToDouble();
38}
39
Chris Lattneraa491192009-02-18 06:40:38 +000040StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
41 unsigned ByteLength, bool Wide,
42 QualType Ty,
43 SourceLocation *Loc, unsigned NumStrs) {
44 // Allocate enough space for the StringLiteral plus an array of locations for
45 // any concatenated string tokens.
46 void *Mem = C.Allocate(sizeof(StringLiteral)+
47 sizeof(SourceLocation)*(NumStrs-1),
48 llvm::alignof<StringLiteral>());
49 StringLiteral *SL = new (Mem) StringLiteral(Ty);
50
Chris Lattner4b009652007-07-25 00:24:17 +000051 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattneraa491192009-02-18 06:40:38 +000052 char *AStrData = new (C, 1) char[ByteLength];
53 memcpy(AStrData, StrData, ByteLength);
54 SL->StrData = AStrData;
55 SL->ByteLength = ByteLength;
56 SL->IsWide = Wide;
57 SL->TokLocs[0] = Loc[0];
58 SL->NumConcatenated = NumStrs;
Chris Lattner4b009652007-07-25 00:24:17 +000059
Chris Lattnerc3144742009-02-18 05:49:11 +000060 if (NumStrs != 1)
Chris Lattneraa491192009-02-18 06:40:38 +000061 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
62 return SL;
Chris Lattnerc3144742009-02-18 05:49:11 +000063}
64
65
Ted Kremenek4f530a92009-02-06 19:55:15 +000066void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +000067 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek32d760b2009-02-09 17:10:09 +000068 this->~StringLiteral();
69 C.Deallocate(this);
Chris Lattner4b009652007-07-25 00:24:17 +000070}
71
Chris Lattner4b009652007-07-25 00:24:17 +000072/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
73/// corresponds to, e.g. "sizeof" or "[pre]++".
74const char *UnaryOperator::getOpcodeStr(Opcode Op) {
75 switch (Op) {
76 default: assert(0 && "Unknown unary operator");
77 case PostInc: return "++";
78 case PostDec: return "--";
79 case PreInc: return "++";
80 case PreDec: return "--";
81 case AddrOf: return "&";
82 case Deref: return "*";
83 case Plus: return "+";
84 case Minus: return "-";
85 case Not: return "~";
86 case LNot: return "!";
87 case Real: return "__real";
88 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +000089 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000090 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000091 }
92}
93
94//===----------------------------------------------------------------------===//
95// Postfix Operators.
96//===----------------------------------------------------------------------===//
97
Ted Kremenek362abcd2009-02-09 20:51:47 +000098CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +000099 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000100 : Expr(SC, t,
101 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000102 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000103 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000104
105 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000106 SubExprs[FN] = fn;
107 for (unsigned i = 0; i != numargs; ++i)
108 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000109
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000110 RParenLoc = rparenloc;
111}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000112
Ted Kremenek362abcd2009-02-09 20:51:47 +0000113CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
114 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000115 : Expr(CallExprClass, t,
116 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000117 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000118 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000119
120 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000121 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000122 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000123 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000124
Chris Lattner4b009652007-07-25 00:24:17 +0000125 RParenLoc = rparenloc;
126}
127
Ted Kremenek362abcd2009-02-09 20:51:47 +0000128void CallExpr::Destroy(ASTContext& C) {
129 DestroyChildren(C);
130 if (SubExprs) C.Deallocate(SubExprs);
131 this->~CallExpr();
132 C.Deallocate(this);
133}
134
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000135/// setNumArgs - This changes the number of arguments present in this call.
136/// Any orphaned expressions are deleted by this, and any new operands are set
137/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000138void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000139 // No change, just return.
140 if (NumArgs == getNumArgs()) return;
141
142 // If shrinking # arguments, just delete the extras and forgot them.
143 if (NumArgs < getNumArgs()) {
144 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000145 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000146 this->NumArgs = NumArgs;
147 return;
148 }
149
150 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000151 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000152 // Copy over args.
153 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
154 NewSubExprs[i] = SubExprs[i];
155 // Null out new args.
156 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
157 NewSubExprs[i] = 0;
158
Ted Kremenek0c97e042009-02-07 01:47:29 +0000159 delete [] SubExprs;
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000160 SubExprs = NewSubExprs;
161 this->NumArgs = NumArgs;
162}
163
Chris Lattnerc24915f2008-10-06 05:00:53 +0000164/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
165/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000166unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000167 // All simple function calls (e.g. func()) are implicitly cast to pointer to
168 // function. As a result, we try and obtain the DeclRefExpr from the
169 // ImplicitCastExpr.
170 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
171 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000172 return 0;
173
Steve Naroff44aec4c2008-01-31 01:07:12 +0000174 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
175 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000176 return 0;
177
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000178 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
179 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000180 return 0;
181
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000182 if (!FDecl->getIdentifier())
183 return 0;
184
Douglas Gregorb5af7382009-02-14 18:57:46 +0000185 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000186}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000187
Chris Lattnerc24915f2008-10-06 05:00:53 +0000188
Chris Lattner4b009652007-07-25 00:24:17 +0000189/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
190/// corresponds to, e.g. "<<=".
191const char *BinaryOperator::getOpcodeStr(Opcode Op) {
192 switch (Op) {
193 default: assert(0 && "Unknown binary operator");
194 case Mul: return "*";
195 case Div: return "/";
196 case Rem: return "%";
197 case Add: return "+";
198 case Sub: return "-";
199 case Shl: return "<<";
200 case Shr: return ">>";
201 case LT: return "<";
202 case GT: return ">";
203 case LE: return "<=";
204 case GE: return ">=";
205 case EQ: return "==";
206 case NE: return "!=";
207 case And: return "&";
208 case Xor: return "^";
209 case Or: return "|";
210 case LAnd: return "&&";
211 case LOr: return "||";
212 case Assign: return "=";
213 case MulAssign: return "*=";
214 case DivAssign: return "/=";
215 case RemAssign: return "%=";
216 case AddAssign: return "+=";
217 case SubAssign: return "-=";
218 case ShlAssign: return "<<=";
219 case ShrAssign: return ">>=";
220 case AndAssign: return "&=";
221 case XorAssign: return "^=";
222 case OrAssign: return "|=";
223 case Comma: return ",";
224 }
225}
226
Anders Carlsson762b7c72007-08-31 04:56:16 +0000227InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000228 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000229 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000230 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000231 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000232 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000233
234 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000235}
Chris Lattner4b009652007-07-25 00:24:17 +0000236
Douglas Gregorf603b472009-01-28 21:54:33 +0000237void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000238 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000239 Idx < LastIdx; ++Idx)
Douglas Gregorf603b472009-01-28 21:54:33 +0000240 delete InitExprs[Idx];
241 InitExprs.resize(NumInits, 0);
242}
243
244Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
245 if (Init >= InitExprs.size()) {
246 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
247 InitExprs.back() = expr;
248 return 0;
249 }
250
251 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
252 InitExprs[Init] = expr;
253 return Result;
254}
255
Steve Naroff6f373332008-09-04 15:31:07 +0000256/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000257///
258const FunctionType *BlockExpr::getFunctionType() const {
259 return getType()->getAsBlockPointerType()->
260 getPointeeType()->getAsFunctionType();
261}
262
Steve Naroff9ac456d2008-10-08 17:01:13 +0000263SourceLocation BlockExpr::getCaretLocation() const {
264 return TheBlock->getCaretLocation();
265}
266const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
267Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
268
269
Chris Lattner4b009652007-07-25 00:24:17 +0000270//===----------------------------------------------------------------------===//
271// Generic Expression Routines
272//===----------------------------------------------------------------------===//
273
Chris Lattnerd2c66552009-02-14 07:37:35 +0000274/// isUnusedResultAWarning - Return true if this immediate expression should
275/// be warned about if the result is unused. If so, fill in Loc and Ranges
276/// with location to warn on and the source range[s] to report with the
277/// warning.
278bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
279 SourceRange &R2) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000280 switch (getStmtClass()) {
281 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000282 Loc = getExprLoc();
283 R1 = getSourceRange();
284 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000285 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000286 return cast<ParenExpr>(this)->getSubExpr()->
287 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000288 case UnaryOperatorClass: {
289 const UnaryOperator *UO = cast<UnaryOperator>(this);
290
291 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000292 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000293 case UnaryOperator::PostInc:
294 case UnaryOperator::PostDec:
295 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000296 case UnaryOperator::PreDec: // ++/--
297 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000298 case UnaryOperator::Deref:
299 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000300 if (getType().isVolatileQualified())
301 return false;
302 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000303 case UnaryOperator::Real:
304 case UnaryOperator::Imag:
305 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000306 if (UO->getSubExpr()->getType().isVolatileQualified())
307 return false;
308 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000309 case UnaryOperator::Extension:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000310 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000311 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000312 Loc = UO->getOperatorLoc();
313 R1 = UO->getSubExpr()->getSourceRange();
314 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000315 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000316 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000317 const BinaryOperator *BO = cast<BinaryOperator>(this);
318 // Consider comma to have side effects if the LHS or RHS does.
319 if (BO->getOpcode() == BinaryOperator::Comma)
320 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
321 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000322
Chris Lattnerd2c66552009-02-14 07:37:35 +0000323 if (BO->isAssignmentOp())
324 return false;
325 Loc = BO->getOperatorLoc();
326 R1 = BO->getLHS()->getSourceRange();
327 R2 = BO->getRHS()->getSourceRange();
328 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000329 }
Chris Lattner06078d22007-08-25 02:00:02 +0000330 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000331 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000332
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000333 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000334 // The condition must be evaluated, but if either the LHS or RHS is a
335 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000336 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump399ad182009-02-27 03:16:57 +0000337 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000338 return true;
339 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000340 }
341
Chris Lattner4b009652007-07-25 00:24:17 +0000342 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000343 // If the base pointer or element is to a volatile pointer/field, accessing
344 // it is a side effect.
345 if (getType().isVolatileQualified())
346 return false;
347 Loc = cast<MemberExpr>(this)->getMemberLoc();
348 R1 = SourceRange(Loc, Loc);
349 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
350 return true;
351
Chris Lattner4b009652007-07-25 00:24:17 +0000352 case ArraySubscriptExprClass:
353 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000354 // it is a side effect.
355 if (getType().isVolatileQualified())
356 return false;
357 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
358 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
359 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
360 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000361
Chris Lattner4b009652007-07-25 00:24:17 +0000362 case CallExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000363 case CXXOperatorCallExprClass: {
364 // If this is a direct call, get the callee.
365 const CallExpr *CE = cast<CallExpr>(this);
366 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
367 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
368 // If the callee has attribute pure, const, or warn_unused_result, warn
369 // about it. void foo() { strlen("bar"); } should warn.
370 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
371 if (FD->getAttr<WarnUnusedResultAttr>() ||
372 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
373 Loc = CE->getCallee()->getLocStart();
374 R1 = CE->getCallee()->getSourceRange();
375
376 if (unsigned NumArgs = CE->getNumArgs())
377 R2 = SourceRange(CE->getArg(0)->getLocStart(),
378 CE->getArg(NumArgs-1)->getLocEnd());
379 return true;
380 }
381 }
382 return false;
383 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000384 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000385 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000386 case StmtExprClass: {
387 // Statement exprs don't logically have side effects themselves, but are
388 // sometimes used in macros in ways that give them a type that is unused.
389 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
390 // however, if the result of the stmt expr is dead, we don't want to emit a
391 // warning.
392 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
393 if (!CS->body_empty())
394 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000395 return E->isUnusedResultAWarning(Loc, R1, R2);
396
397 Loc = cast<StmtExpr>(this)->getLParenLoc();
398 R1 = getSourceRange();
399 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000400 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000401 case CStyleCastExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000402 // If this is a cast to void, check the operand. Otherwise, the result of
403 // the cast is unused.
404 if (getType()->isVoidType())
405 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
406 R1, R2);
407 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
408 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
409 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000410 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000411 // If this is a cast to void, check the operand. Otherwise, the result of
412 // the cast is unused.
413 if (getType()->isVoidType())
Chris Lattnerd2c66552009-02-14 07:37:35 +0000414 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
415 R1, R2);
416 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
417 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
418 return true;
419
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000420 case ImplicitCastExprClass:
421 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000422 return cast<ImplicitCastExpr>(this)
423 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000424
Chris Lattner3e254fb2008-04-08 04:40:51 +0000425 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000426 return cast<CXXDefaultArgExpr>(this)
427 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000428
429 case CXXNewExprClass:
430 // FIXME: In theory, there might be new expressions that don't have side
431 // effects (e.g. a placement new with an uninitialized POD).
432 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000433 return false;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000434 }
Chris Lattner4b009652007-07-25 00:24:17 +0000435}
436
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000437/// DeclCanBeLvalue - Determine whether the given declaration can be
438/// an lvalue. This is a helper routine for isLvalue.
439static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000440 // C++ [temp.param]p6:
441 // A non-type non-reference template-parameter is not an lvalue.
442 if (const NonTypeTemplateParmDecl *NTTParm
443 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
444 return NTTParm->getType()->isReferenceType();
445
Douglas Gregor8acb7272008-12-11 16:49:14 +0000446 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000447 // C++ 3.10p2: An lvalue refers to an object or function.
448 (Ctx.getLangOptions().CPlusPlus &&
449 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
450}
451
Chris Lattner4b009652007-07-25 00:24:17 +0000452/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
453/// incomplete type other than void. Nonarray expressions that can be lvalues:
454/// - name, where name must be a variable
455/// - e[i]
456/// - (e), where e must be an lvalue
457/// - e.name, where e must be an lvalue
458/// - e->name
459/// - *e, the type of e cannot be a function type
460/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000461/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000462/// - reference type [C++ [expr]]
463///
Chris Lattner25168a52008-07-26 21:30:36 +0000464Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000465 // first, check the type (C99 6.3.2.1). Expressions with function
466 // type in C are not lvalues, but they can be lvalues in C++.
467 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000468 return LV_NotObjectType;
469
Steve Naroffec7736d2008-02-10 01:39:04 +0000470 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000471 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000472 return LV_IncompleteVoidType;
473
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000474 /// FIXME: Expressions can't have reference type, so the following
475 /// isn't needed.
Chris Lattner4b009652007-07-25 00:24:17 +0000476 if (TR->isReferenceType()) // C++ [expr]
477 return LV_Valid;
478
479 // the type looks fine, now check the expression
480 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000481 case StringLiteralClass: // C99 6.5.1p4
482 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000483 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000484 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
485 // For vectors, make sure base is an lvalue (i.e. not a function call).
486 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000487 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000488 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000489 case DeclRefExprClass:
490 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000491 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
492 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000493 return LV_Valid;
494 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000495 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000496 case BlockDeclRefExprClass: {
497 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000498 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000499 return LV_Valid;
500 break;
501 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000502 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000503 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000504 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
505 NamedDecl *Member = m->getMemberDecl();
506 // C++ [expr.ref]p4:
507 // If E2 is declared to have type "reference to T", then E1.E2
508 // is an lvalue.
509 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
510 if (Value->getType()->isReferenceType())
511 return LV_Valid;
512
513 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
514 if (isa<CXXClassVarDecl>(Member))
515 return LV_Valid;
516
517 // -- If E2 is a non-static data member [...]. If E1 is an
518 // lvalue, then E1.E2 is an lvalue.
519 if (isa<FieldDecl>(Member))
520 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
521
522 // -- If it refers to a static member function [...], then
523 // E1.E2 is an lvalue.
524 // -- Otherwise, if E1.E2 refers to a non-static member
525 // function [...], then E1.E2 is not an lvalue.
526 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
527 return Method->isStatic()? LV_Valid : LV_MemberFunction;
528
529 // -- If E2 is a member enumerator [...], the expression E1.E2
530 // is not an lvalue.
531 if (isa<EnumConstantDecl>(Member))
532 return LV_InvalidExpression;
533
534 // Not an lvalue.
535 return LV_InvalidExpression;
536 }
537
538 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000539 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000540 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000541 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000542 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000543 return LV_Valid; // C99 6.5.3p4
544
545 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000546 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
547 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000548 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000549
550 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
551 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
552 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
553 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000554 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000555 case ImplicitCastExprClass:
556 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
557 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000558 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000559 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000560 case BinaryOperatorClass:
561 case CompoundAssignOperatorClass: {
562 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000563
564 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
565 BinOp->getOpcode() == BinaryOperator::Comma)
566 return BinOp->getRHS()->isLvalue(Ctx);
567
Sebastian Redl95216a62009-02-07 00:15:38 +0000568 // C++ [expr.mptr.oper]p6
569 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
570 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
571 !BinOp->getType()->isFunctionType())
572 return BinOp->getLHS()->isLvalue(Ctx);
573
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000574 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000575 return LV_InvalidExpression;
576
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000577 if (Ctx.getLangOptions().CPlusPlus)
578 // C++ [expr.ass]p1:
579 // The result of an assignment operation [...] is an lvalue.
580 return LV_Valid;
581
582
583 // C99 6.5.16:
584 // An assignment expression [...] is not an lvalue.
585 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000586 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000587 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000588 case CXXOperatorCallExprClass:
589 case CXXMemberCallExprClass: {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000590 // C++ [expr.call]p10:
591 // A function call is an lvalue if and only if the result type
592 // is a reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000593 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000594 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000595 CalleeType = FnTypePtr->getPointeeType();
596 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
597 if (FnType->getResultType()->isReferenceType())
598 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000599
600 break;
601 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000602 case CompoundLiteralExprClass: // C99 6.5.2.5p5
603 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000604 case ChooseExprClass:
605 // __builtin_choose_expr is an lvalue if the selected operand is.
606 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
607 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
608 else
609 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
610
Nate Begemanaf6ed502008-04-18 23:10:10 +0000611 case ExtVectorElementExprClass:
612 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000613 return LV_DuplicateVectorComponents;
614 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000615 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
616 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000617 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
618 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000619 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000620 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000621 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000622 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000623 case VAArgExprClass:
Daniel Dunbar09a82e32009-02-12 09:21:08 +0000624 return LV_NotObjectType;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000625 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000626 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000627 case CXXConditionDeclExprClass:
628 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000629 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000630 case CXXFunctionalCastExprClass:
631 case CXXStaticCastExprClass:
632 case CXXDynamicCastExprClass:
633 case CXXReinterpretCastExprClass:
634 case CXXConstCastExprClass:
635 // The result of an explicit cast is an lvalue if the type we are
636 // casting to is a reference type. See C++ [expr.cast]p1,
637 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
638 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
639 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
640 return LV_Valid;
641 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000642 case CXXTypeidExprClass:
643 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
644 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000645 default:
646 break;
647 }
648 return LV_InvalidExpression;
649}
650
651/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
652/// does not have an incomplete type, does not have a const-qualified type, and
653/// if it is a structure or union, does not have any member (including,
654/// recursively, any member or element of all contained aggregates or unions)
655/// with a const-qualified type.
Chris Lattner25168a52008-07-26 21:30:36 +0000656Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
657 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000658
659 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000660 case LV_Valid:
661 // C++ 3.10p11: Functions cannot be modified, but pointers to
662 // functions can be modifiable.
663 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
664 return MLV_NotObjectType;
665 break;
666
Chris Lattner4b009652007-07-25 00:24:17 +0000667 case LV_NotObjectType: return MLV_NotObjectType;
668 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000669 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000670 case LV_InvalidExpression:
671 // If the top level is a C-style cast, and the subexpression is a valid
672 // lvalue, then this is probably a use of the old-school "cast as lvalue"
673 // GCC extension. We don't support it, but we want to produce good
674 // diagnostics when it happens so that the user knows why.
675 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
676 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
677 return MLV_LValueCast;
678 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000679 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000680 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000681
682 QualType CT = Ctx.getCanonicalType(getType());
683
684 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000685 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000686 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000687 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000688 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000689 return MLV_IncompleteType;
690
Chris Lattnera1923f62008-08-04 07:31:14 +0000691 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000692 if (r->hasConstFields())
693 return MLV_ConstQualified;
694 }
Steve Naroff076d6cb2008-09-26 14:41:28 +0000695 // The following is illegal:
696 // void takeclosure(void (^C)(void));
697 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
698 //
699 if (getStmtClass() == BlockDeclRefExprClass) {
700 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
701 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
702 return MLV_NotBlockQualified;
703 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000704
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000705 // Assigning to an 'implicit' property?
Fariborz Jahanian48b1a132008-11-25 17:56:43 +0000706 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000707 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
708 if (KVCExpr->getSetterMethod() == 0)
709 return MLV_NoSetterProperty;
710 }
Chris Lattner4b009652007-07-25 00:24:17 +0000711 return MLV_Valid;
712}
713
Ted Kremenek5778d622008-02-27 18:39:48 +0000714/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000715/// duration. This means that the address of this expression is a link-time
716/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000717bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000718 switch (getStmtClass()) {
719 default:
720 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000721 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000722 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000723 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000724 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000725 case CompoundLiteralExprClass:
726 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000727 case DeclRefExprClass:
728 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000729 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
730 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000731 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000732 if (isa<FunctionDecl>(D))
733 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000734 return false;
735 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000736 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000737 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000738 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000739 }
Chris Lattner743ec372007-11-27 21:35:27 +0000740 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000741 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000742 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000743 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000744 case CXXDefaultArgExprClass:
745 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000746 }
747}
748
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000749/// isOBJCGCCandidate - Check if an expression is objc gc'able.
750///
751bool Expr::isOBJCGCCandidate() const {
752 switch (getStmtClass()) {
753 default:
754 return false;
755 case ObjCIvarRefExprClass:
756 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000757 case Expr::UnaryOperatorClass:
758 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000759 case ParenExprClass:
760 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
761 case ImplicitCastExprClass:
762 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
763 case DeclRefExprClass:
764 case QualifiedDeclRefExprClass: {
765 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
766 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
767 return VD->hasGlobalStorage();
768 return false;
769 }
770 case MemberExprClass: {
771 const MemberExpr *M = cast<MemberExpr>(this);
772 return !M->isArrow() && M->getBase()->isOBJCGCCandidate();
773 }
774 case ArraySubscriptExprClass:
775 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
776 }
777}
Ted Kremenek87e30c52008-01-17 16:57:34 +0000778Expr* Expr::IgnoreParens() {
779 Expr* E = this;
780 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
781 E = P->getSubExpr();
782
783 return E;
784}
785
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000786/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
787/// or CastExprs or ImplicitCastExprs, returning their operand.
788Expr *Expr::IgnoreParenCasts() {
789 Expr *E = this;
790 while (true) {
791 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
792 E = P->getSubExpr();
793 else if (CastExpr *P = dyn_cast<CastExpr>(E))
794 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000795 else
796 return E;
797 }
798}
799
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000800/// hasAnyTypeDependentArguments - Determines if any of the expressions
801/// in Exprs is type-dependent.
802bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
803 for (unsigned I = 0; I < NumExprs; ++I)
804 if (Exprs[I]->isTypeDependent())
805 return true;
806
807 return false;
808}
809
810/// hasAnyValueDependentArguments - Determines if any of the expressions
811/// in Exprs is value-dependent.
812bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
813 for (unsigned I = 0; I < NumExprs; ++I)
814 if (Exprs[I]->isValueDependent())
815 return true;
816
817 return false;
818}
819
Eli Friedmandee41122009-01-25 02:32:41 +0000820bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000821 // This function is attempting whether an expression is an initializer
822 // which can be evaluated at compile-time. isEvaluatable handles most
823 // of the cases, but it can't deal with some initializer-specific
824 // expressions, and it can't deal with aggregates; we deal with those here,
825 // and fall back to isEvaluatable for the other cases.
826
Eli Friedman3dc50ed2009-02-20 02:36:22 +0000827 // FIXME: This function assumes the variable being assigned to
828 // isn't a reference type!
829
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000830 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000831 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000832 case StringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +0000833 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000834 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +0000835 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +0000836 // This handles gcc's extension that allows global initializers like
837 // "struct x {int x;} x = (struct x) {};".
838 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +0000839 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +0000840 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +0000841 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000842 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +0000843 // FIXME: This doesn't deal with fields with reference types correctly.
844 // FIXME: This incorrectly allows pointers cast to integers to be assigned
845 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000846 const InitListExpr *Exp = cast<InitListExpr>(this);
847 unsigned numInits = Exp->getNumInits();
848 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +0000849 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000850 return false;
851 }
Eli Friedman2b0dec52009-01-25 03:12:18 +0000852 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000853 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000854 case ImplicitValueInitExprClass:
855 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +0000856 case ParenExprClass: {
857 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
858 }
859 case UnaryOperatorClass: {
860 const UnaryOperator* Exp = cast<UnaryOperator>(this);
861 if (Exp->getOpcode() == UnaryOperator::Extension)
862 return Exp->getSubExpr()->isConstantInitializer(Ctx);
863 break;
864 }
865 case CStyleCastExprClass:
866 // Handle casts with a destination that's a struct or union; this
867 // deals with both the gcc no-op struct cast extension and the
868 // cast-to-union extension.
869 if (getType()->isRecordType())
870 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
871 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000872 }
873
Eli Friedman2b0dec52009-01-25 03:12:18 +0000874 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000875}
876
Chris Lattner4b009652007-07-25 00:24:17 +0000877/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +0000878/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +0000879
880/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
881/// comma, etc
882///
Chris Lattner4b009652007-07-25 00:24:17 +0000883/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
884/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
885/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +0000886
Eli Friedman7beeda62009-02-26 09:29:13 +0000887// CheckICE - This function does the fundamental ICE checking: the returned
888// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
889// Note that to reduce code duplication, this helper does no evaluation
890// itself; the caller checks whether the expression is evaluatable, and
891// in the rare cases where CheckICE actually cares about the evaluated
892// value, it calls into Evalute.
893//
894// Meanings of Val:
895// 0: This expression is an ICE if it can be evaluated by Evaluate.
896// 1: This expression is not an ICE, but if it isn't evaluated, it's
897// a legal subexpression for an ICE. This return value is used to handle
898// the comma operator in C99 mode.
899// 2: This expression is not an ICE, and is not a legal subexpression for one.
900
901struct ICEDiag {
902 unsigned Val;
903 SourceLocation Loc;
904
905 public:
906 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
907 ICEDiag() : Val(0) {}
908};
909
910ICEDiag NoDiag() { return ICEDiag(); }
911
Eli Friedmand65bbbc2009-02-27 04:07:58 +0000912static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
913 Expr::EvalResult EVResult;
914 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
915 !EVResult.Val.isInt()) {
916 return ICEDiag(2, E->getLocStart());
917 }
918 return NoDiag();
919}
920
Eli Friedman7beeda62009-02-26 09:29:13 +0000921static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
922 if (!E->getType()->isIntegralType()) {
923 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +0000924 }
Eli Friedman7beeda62009-02-26 09:29:13 +0000925
926 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000927 default:
Eli Friedman7beeda62009-02-26 09:29:13 +0000928 return ICEDiag(2, E->getLocStart());
929 case Expr::ParenExprClass:
930 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
931 case Expr::IntegerLiteralClass:
932 case Expr::CharacterLiteralClass:
933 case Expr::CXXBoolLiteralExprClass:
934 case Expr::CXXZeroInitValueExprClass:
935 case Expr::TypesCompatibleExprClass:
936 case Expr::UnaryTypeTraitExprClass:
937 return NoDiag();
938 case Expr::CallExprClass:
939 case Expr::CXXOperatorCallExprClass: {
940 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +0000941 if (CE->isBuiltinCall(Ctx))
942 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +0000943 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +0000944 }
Eli Friedman7beeda62009-02-26 09:29:13 +0000945 case Expr::DeclRefExprClass:
946 case Expr::QualifiedDeclRefExprClass:
947 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
948 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000949 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +0000950 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000951 // C++ 7.1.5.1p2
952 // A variable of non-volatile const-qualified integral or enumeration
953 // type initialized by an ICE can be used in ICEs.
954 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +0000955 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000956 if (const Expr *Init = Dcl->getInit())
Eli Friedman7beeda62009-02-26 09:29:13 +0000957 return CheckICE(Init, Ctx);
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000958 }
959 }
Eli Friedman7beeda62009-02-26 09:29:13 +0000960 return ICEDiag(2, E->getLocStart());
961 case Expr::UnaryOperatorClass: {
962 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +0000963 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000964 default:
Eli Friedman7beeda62009-02-26 09:29:13 +0000965 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +0000966 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +0000967 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +0000968 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +0000969 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +0000970 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +0000971 case UnaryOperator::Real:
972 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +0000973 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +0000974 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +0000975 // Note that per C99, offsetof must be an ICE. And AFAIK, using
976 // Evaluate matches the proposed gcc behavior for cases like
977 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
978 // compliance: we should warn earlier for offsetof expressions with
979 // array subscripts that aren't ICEs, and if the array subscripts
980 // are ICEs, the value of the offsetof must be an integer constant.
981 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000982 }
Chris Lattner4b009652007-07-25 00:24:17 +0000983 }
Eli Friedman7beeda62009-02-26 09:29:13 +0000984 case Expr::SizeOfAlignOfExprClass: {
985 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
986 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
987 return ICEDiag(2, E->getLocStart());
988 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +0000989 }
Eli Friedman7beeda62009-02-26 09:29:13 +0000990 case Expr::BinaryOperatorClass: {
991 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +0000992 switch (Exp->getOpcode()) {
993 default:
Eli Friedman7beeda62009-02-26 09:29:13 +0000994 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +0000995 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +0000996 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +0000997 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +0000998 case BinaryOperator::Add:
999 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001000 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001001 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001002 case BinaryOperator::LT:
1003 case BinaryOperator::GT:
1004 case BinaryOperator::LE:
1005 case BinaryOperator::GE:
1006 case BinaryOperator::EQ:
1007 case BinaryOperator::NE:
1008 case BinaryOperator::And:
1009 case BinaryOperator::Xor:
1010 case BinaryOperator::Or:
1011 case BinaryOperator::Comma: {
1012 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1013 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001014 if (Exp->getOpcode() == BinaryOperator::Div ||
1015 Exp->getOpcode() == BinaryOperator::Rem) {
1016 // Evaluate gives an error for undefined Div/Rem, so make sure
1017 // we don't evaluate one.
1018 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1019 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1020 if (REval == 0)
1021 return ICEDiag(1, E->getLocStart());
1022 if (REval.isSigned() && REval.isAllOnesValue()) {
1023 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1024 if (LEval.isMinSignedValue())
1025 return ICEDiag(1, E->getLocStart());
1026 }
1027 }
1028 }
1029 if (Exp->getOpcode() == BinaryOperator::Comma) {
1030 if (Ctx.getLangOptions().C99) {
1031 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1032 // if it isn't evaluated.
1033 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1034 return ICEDiag(1, E->getLocStart());
1035 } else {
1036 // In both C89 and C++, commas in ICEs are illegal.
1037 return ICEDiag(2, E->getLocStart());
1038 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001039 }
1040 if (LHSResult.Val >= RHSResult.Val)
1041 return LHSResult;
1042 return RHSResult;
1043 }
Chris Lattner4b009652007-07-25 00:24:17 +00001044 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001045 case BinaryOperator::LOr: {
1046 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1047 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1048 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1049 // Rare case where the RHS has a comma "side-effect"; we need
1050 // to actually check the condition to see whether the side
1051 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001052 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001053 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001054 return RHSResult;
1055 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001056 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001057
Eli Friedman7beeda62009-02-26 09:29:13 +00001058 if (LHSResult.Val >= RHSResult.Val)
1059 return LHSResult;
1060 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001061 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001062 }
Chris Lattner4b009652007-07-25 00:24:17 +00001063 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001064 case Expr::ImplicitCastExprClass:
1065 case Expr::CStyleCastExprClass:
1066 case Expr::CXXFunctionalCastExprClass: {
1067 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1068 if (SubExpr->getType()->isIntegralType())
1069 return CheckICE(SubExpr, Ctx);
1070 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1071 return NoDiag();
1072 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001073 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001074 case Expr::ConditionalOperatorClass: {
1075 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001076 // If the condition (ignoring parens) is a __builtin_constant_p call,
1077 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001078 // expression, and it is fully evaluated. This is an important GNU
1079 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001080 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001081 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001082 Expr::EvalResult EVResult;
1083 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1084 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001085 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001086 }
1087 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001088 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001089 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1090 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1091 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1092 if (CondResult.Val == 2)
1093 return CondResult;
1094 if (TrueResult.Val == 2)
1095 return TrueResult;
1096 if (FalseResult.Val == 2)
1097 return FalseResult;
1098 if (CondResult.Val == 1)
1099 return CondResult;
1100 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1101 return NoDiag();
1102 // Rare case where the diagnostics depend on which side is evaluated
1103 // Note that if we get here, CondResult is 0, and at least one of
1104 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001105 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001106 return FalseResult;
1107 }
1108 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001109 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001110 case Expr::CXXDefaultArgExprClass:
1111 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001112 case Expr::ChooseExprClass: {
1113 const ChooseExpr *CE = cast<ChooseExpr>(E);
1114 Expr *SubExpr = CE->isConditionTrue(Ctx) ? CE->getLHS() : CE->getRHS();
1115 return CheckICE(SubExpr, Ctx);
1116 }
Chris Lattner4b009652007-07-25 00:24:17 +00001117 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001118}
Chris Lattner4b009652007-07-25 00:24:17 +00001119
Eli Friedman7beeda62009-02-26 09:29:13 +00001120bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1121 SourceLocation *Loc, bool isEvaluated) const {
1122 ICEDiag d = CheckICE(this, Ctx);
1123 if (d.Val != 0) {
1124 if (Loc) *Loc = d.Loc;
1125 return false;
1126 }
1127 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001128 if (!Evaluate(EvalResult, Ctx))
1129 assert(0 && "ICE cannot be evaluated!");
1130 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1131 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001132 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001133 return true;
1134}
1135
Chris Lattner4b009652007-07-25 00:24:17 +00001136/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1137/// integer constant expression with the value zero, or if this is one that is
1138/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001139bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1140{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001141 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001142 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001143 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001144 // Check that it is a cast to void*.
1145 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1146 QualType Pointee = PT->getPointeeType();
1147 if (Pointee.getCVRQualifiers() == 0 &&
1148 Pointee->isVoidType() && // to void*
1149 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001150 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001151 }
Chris Lattner4b009652007-07-25 00:24:17 +00001152 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001153 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1154 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001155 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001156 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1157 // Accept ((void*)0) as a null pointer constant, as many other
1158 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001159 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001160 } else if (const CXXDefaultArgExpr *DefaultArg
1161 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001162 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001163 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001164 } else if (isa<GNUNullExpr>(this)) {
1165 // The GNU __null extension is always a null pointer constant.
1166 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001167 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001168
Steve Naroffa2e53222008-01-14 16:10:57 +00001169 // This expression must be an integer type.
1170 if (!getType()->isIntegerType())
1171 return false;
1172
Chris Lattner4b009652007-07-25 00:24:17 +00001173 // If we have an integer constant expression, we need to *evaluate* it and
1174 // test for the value 0.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001175 // FIXME: We should probably return false if we're compiling in strict mode
1176 // and Diag is not null (this indicates that the value was foldable but not
1177 // an ICE.
1178 EvalResult Result;
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001179 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001180 Result.Val.isInt() && Result.Val.getInt() == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001181}
Steve Naroffc11705f2007-07-28 23:10:27 +00001182
Douglas Gregor81c29152008-10-29 00:13:59 +00001183/// isBitField - Return true if this expression is a bit-field.
1184bool Expr::isBitField() {
1185 Expr *E = this->IgnoreParenCasts();
1186 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001187 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1188 return Field->isBitField();
Douglas Gregor81c29152008-10-29 00:13:59 +00001189 return false;
1190}
1191
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001192/// isArrow - Return true if the base expression is a pointer to vector,
1193/// return false if the base expression is a vector.
1194bool ExtVectorElementExpr::isArrow() const {
1195 return getBase()->getType()->isPointerType();
1196}
1197
Nate Begemanaf6ed502008-04-18 23:10:10 +00001198unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001199 if (const VectorType *VT = getType()->getAsVectorType())
1200 return VT->getNumElements();
1201 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001202}
1203
Nate Begemanc8e51f82008-05-09 06:41:27 +00001204/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001205bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001206 const char *compStr = Accessor.getName();
Chris Lattner58d3fa52008-11-19 07:55:04 +00001207 unsigned length = Accessor.getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001208
1209 // Halving swizzles do not contain duplicate elements.
1210 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1211 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1212 return false;
1213
1214 // Advance past s-char prefix on hex swizzles.
1215 if (*compStr == 's') {
1216 compStr++;
1217 length--;
1218 }
Steve Naroffba67f692007-07-30 03:29:09 +00001219
Chris Lattner58d3fa52008-11-19 07:55:04 +00001220 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001221 const char *s = compStr+i;
1222 for (const char c = *s++; *s; s++)
1223 if (c == *s)
1224 return true;
1225 }
1226 return false;
1227}
Chris Lattner42158e72007-08-02 23:36:59 +00001228
Nate Begemanc8e51f82008-05-09 06:41:27 +00001229/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001230void ExtVectorElementExpr::getEncodedElementAccess(
1231 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner58d3fa52008-11-19 07:55:04 +00001232 const char *compStr = Accessor.getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001233 if (*compStr == 's')
1234 compStr++;
1235
1236 bool isHi = !strcmp(compStr, "hi");
1237 bool isLo = !strcmp(compStr, "lo");
1238 bool isEven = !strcmp(compStr, "even");
1239 bool isOdd = !strcmp(compStr, "odd");
1240
Nate Begemanc8e51f82008-05-09 06:41:27 +00001241 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1242 uint64_t Index;
1243
1244 if (isHi)
1245 Index = e + i;
1246 else if (isLo)
1247 Index = i;
1248 else if (isEven)
1249 Index = 2 * i;
1250 else if (isOdd)
1251 Index = 2 * i + 1;
1252 else
1253 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001254
Nate Begemana1ae7442008-05-13 21:03:02 +00001255 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001256 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001257}
1258
Steve Naroff4ed9d662007-09-27 14:38:14 +00001259// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001260ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001261 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001262 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001263 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001264 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001265 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001266 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001267 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001268 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001269 if (NumArgs) {
1270 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001271 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1272 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001273 LBracloc = LBrac;
1274 RBracloc = RBrac;
1275}
1276
Steve Naroff4ed9d662007-09-27 14:38:14 +00001277// constructor for class messages.
1278// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001279ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001280 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001281 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001282 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001283 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001284 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001285 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001286 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001287 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001288 if (NumArgs) {
1289 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001290 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1291 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001292 LBracloc = LBrac;
1293 RBracloc = RBrac;
1294}
1295
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001296// constructor for class messages.
1297ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1298 QualType retType, ObjCMethodDecl *mproto,
1299 SourceLocation LBrac, SourceLocation RBrac,
1300 Expr **ArgExprs, unsigned nargs)
1301: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1302MethodProto(mproto) {
1303 NumArgs = nargs;
1304 SubExprs = new Stmt*[NumArgs+1];
1305 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1306 if (NumArgs) {
1307 for (unsigned i = 0; i != NumArgs; ++i)
1308 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1309 }
1310 LBracloc = LBrac;
1311 RBracloc = RBrac;
1312}
1313
1314ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1315 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1316 switch (x & Flags) {
1317 default:
1318 assert(false && "Invalid ObjCMessageExpr.");
1319 case IsInstMeth:
1320 return ClassInfo(0, 0);
1321 case IsClsMethDeclUnknown:
1322 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1323 case IsClsMethDeclKnown: {
1324 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1325 return ClassInfo(D, D->getIdentifier());
1326 }
1327 }
1328}
1329
Chris Lattnerf624cd22007-10-25 00:29:32 +00001330bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001331 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001332}
1333
Chris Lattnera5f779dc2008-12-12 05:35:08 +00001334static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
Anders Carlsson52774ad2008-01-29 15:56:48 +00001335 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1336 QualType Ty = ME->getBase()->getType();
1337
1338 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner8cd0e932008-03-05 18:54:05 +00001339 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Douglas Gregor82d44772008-12-20 23:49:58 +00001340 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
1341 // FIXME: This is linear time. And the fact that we're indexing
1342 // into the layout by position in the record means that we're
1343 // either stuck numbering the fields in the AST or we have to keep
1344 // the linear search (yuck and yuck).
1345 unsigned i = 0;
1346 for (RecordDecl::field_iterator Field = RD->field_begin(),
1347 FieldEnd = RD->field_end();
1348 Field != FieldEnd; (void)++Field, ++i) {
1349 if (*Field == FD)
1350 break;
1351 }
1352
1353 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
Anders Carlsson52774ad2008-01-29 15:56:48 +00001354 }
Anders Carlsson52774ad2008-01-29 15:56:48 +00001355 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1356 const Expr *Base = ASE->getBase();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001357
Chris Lattner8cd0e932008-03-05 18:54:05 +00001358 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001359 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001360
1361 return size + evaluateOffsetOf(C, Base);
1362 } else if (isa<CompoundLiteralExpr>(E))
1363 return 0;
1364
1365 assert(0 && "Unknown offsetof subexpression!");
1366 return 0;
1367}
1368
1369int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1370{
1371 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1372
Chris Lattner8cd0e932008-03-05 18:54:05 +00001373 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek2719e982008-06-17 02:43:46 +00001374 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson52774ad2008-01-29 15:56:48 +00001375}
1376
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001377void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1378 // Override default behavior of traversing children. If this has a type
1379 // operand and the type is a variable-length array, the child iteration
1380 // will iterate over the size expression. However, this expression belongs
1381 // to the type, not to this, so we don't want to delete it.
1382 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001383 if (isArgumentType()) {
1384 this->~SizeOfAlignOfExpr();
1385 C.Deallocate(this);
1386 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001387 else
1388 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001389}
1390
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001391//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001392// DesignatedInitExpr
1393//===----------------------------------------------------------------------===//
1394
1395IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1396 assert(Kind == FieldDesignator && "Only valid on a field designator");
1397 if (Field.NameOrField & 0x01)
1398 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1399 else
1400 return getField()->getIdentifier();
1401}
1402
1403DesignatedInitExpr *
1404DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1405 unsigned NumDesignators,
1406 Expr **IndexExprs, unsigned NumIndexExprs,
1407 SourceLocation ColonOrEqualLoc,
1408 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001409 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1410 sizeof(Designator) * NumDesignators +
1411 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001412 DesignatedInitExpr *DIE
1413 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1414 ColonOrEqualLoc, UsesColonSyntax,
1415 NumIndexExprs + 1);
1416
1417 // Fill in the designators
1418 unsigned ExpectedNumSubExprs = 0;
1419 designators_iterator Desig = DIE->designators_begin();
1420 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1421 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1422 if (Designators[Idx].isArrayDesignator())
1423 ++ExpectedNumSubExprs;
1424 else if (Designators[Idx].isArrayRangeDesignator())
1425 ExpectedNumSubExprs += 2;
1426 }
1427 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1428
1429 // Fill in the subexpressions, including the initializer expression.
1430 child_iterator Child = DIE->child_begin();
1431 *Child++ = Init;
1432 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1433 *Child = IndexExprs[Idx];
1434
1435 return DIE;
1436}
1437
1438SourceRange DesignatedInitExpr::getSourceRange() const {
1439 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001440 Designator &First =
1441 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001442 if (First.isFieldDesignator()) {
1443 if (UsesColonSyntax)
1444 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1445 else
1446 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1447 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001448 StartLoc =
1449 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001450 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1451}
1452
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001453DesignatedInitExpr::designators_iterator
1454DesignatedInitExpr::designators_begin() {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001455 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1456 Ptr += sizeof(DesignatedInitExpr);
1457 return static_cast<Designator*>(static_cast<void*>(Ptr));
1458}
1459
1460DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1461 return designators_begin() + NumDesignators;
1462}
1463
1464Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1465 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1466 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1467 Ptr += sizeof(DesignatedInitExpr);
1468 Ptr += sizeof(Designator) * NumDesignators;
1469 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1470 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1471}
1472
1473Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1474 assert(D.Kind == Designator::ArrayRangeDesignator &&
1475 "Requires array range designator");
1476 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1477 Ptr += sizeof(DesignatedInitExpr);
1478 Ptr += sizeof(Designator) * NumDesignators;
1479 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1480 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1481}
1482
1483Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1484 assert(D.Kind == Designator::ArrayRangeDesignator &&
1485 "Requires array range designator");
1486 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1487 Ptr += sizeof(DesignatedInitExpr);
1488 Ptr += sizeof(Designator) * NumDesignators;
1489 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1490 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1491}
1492
1493//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001494// ExprIterator.
1495//===----------------------------------------------------------------------===//
1496
1497Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1498Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1499Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1500const Expr* ConstExprIterator::operator[](size_t idx) const {
1501 return cast<Expr>(I[idx]);
1502}
1503const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1504const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1505
1506//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001507// Child Iterators for iterating over subexpressions/substatements
1508//===----------------------------------------------------------------------===//
1509
1510// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001511Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1512Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001513
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001514// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001515Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1516Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001517
Steve Naroff6f786252008-06-02 23:03:37 +00001518// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001519Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1520Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001521
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001522// ObjCKVCRefExpr
1523Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1524Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1525
Douglas Gregord8606632008-11-04 14:56:14 +00001526// ObjCSuperExpr
1527Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1528Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1529
Chris Lattner69909292008-08-10 01:53:14 +00001530// PredefinedExpr
1531Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1532Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001533
1534// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001535Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1536Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001537
1538// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001539Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001540Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001541
1542// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001543Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1544Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001545
Chris Lattner1de66eb2007-08-26 03:42:43 +00001546// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001547Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1548Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001549
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001550// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001551Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1552Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001553
1554// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001555Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1556Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001557
1558// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001559Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1560Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001561
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001562// SizeOfAlignOfExpr
1563Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1564 // If this is of a type and the type is a VLA type (and not a typedef), the
1565 // size expression of the VLA needs to be treated as an executable expression.
1566 // Why isn't this weirdness documented better in StmtIterator?
1567 if (isArgumentType()) {
1568 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1569 getArgumentType().getTypePtr()))
1570 return child_iterator(T);
1571 return child_iterator();
1572 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001573 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001574}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001575Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1576 if (isArgumentType())
1577 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001578 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001579}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001580
1581// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001582Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001583 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001584}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001585Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001586 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001587}
1588
1589// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001590Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001591 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001592}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001593Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001594 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001595}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001596
1597// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001598Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1599Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001600
Nate Begemanaf6ed502008-04-18 23:10:10 +00001601// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001602Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1603Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001604
1605// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001606Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1607Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001608
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001609// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001610Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1611Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001612
1613// BinaryOperator
1614Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001615 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001616}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001617Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001618 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001619}
1620
1621// ConditionalOperator
1622Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001623 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001624}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001625Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001626 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001627}
1628
1629// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001630Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1631Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001632
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001633// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001634Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1635Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001636
1637// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001638Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1639 return child_iterator();
1640}
1641
1642Stmt::child_iterator TypesCompatibleExpr::child_end() {
1643 return child_iterator();
1644}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001645
1646// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001647Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1648Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001649
Douglas Gregorad4b3792008-11-29 04:51:27 +00001650// GNUNullExpr
1651Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1652Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1653
Eli Friedmand0e9d092008-05-14 19:38:39 +00001654// ShuffleVectorExpr
1655Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001656 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001657}
1658Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001659 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001660}
1661
Anders Carlsson36760332007-10-15 20:28:48 +00001662// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001663Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1664Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001665
Anders Carlsson762b7c72007-08-31 04:56:16 +00001666// InitListExpr
1667Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001668 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001669}
1670Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001671 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001672}
1673
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001674// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001675Stmt::child_iterator DesignatedInitExpr::child_begin() {
1676 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1677 Ptr += sizeof(DesignatedInitExpr);
1678 Ptr += sizeof(Designator) * NumDesignators;
1679 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1680}
1681Stmt::child_iterator DesignatedInitExpr::child_end() {
1682 return child_iterator(&*child_begin() + NumSubExprs);
1683}
1684
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001685// ImplicitValueInitExpr
1686Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1687 return child_iterator();
1688}
1689
1690Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1691 return child_iterator();
1692}
1693
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001694// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001695Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00001696 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00001697}
1698Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00001699 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00001700}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001701
1702// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001703Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1704Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001705
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001706// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001707Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1708 return child_iterator();
1709}
1710Stmt::child_iterator ObjCSelectorExpr::child_end() {
1711 return child_iterator();
1712}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001713
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001714// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001715Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1716 return child_iterator();
1717}
1718Stmt::child_iterator ObjCProtocolExpr::child_end() {
1719 return child_iterator();
1720}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001721
Steve Naroffc39ca262007-09-18 23:55:05 +00001722// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001723Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001724 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001725}
1726Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001727 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001728}
1729
Steve Naroff52a81c02008-09-03 18:15:37 +00001730// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001731Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1732Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001733
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001734Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1735Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }