blob: bd6bd298e9ed364ce7147b1b9dec6ae186c5ad62 [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) {
Douglas Gregor535f3122009-03-12 22:51:37 +0000193 case PtrMemD: return ".*";
194 case PtrMemI: return "->*";
Chris Lattner4b009652007-07-25 00:24:17 +0000195 case Mul: return "*";
196 case Div: return "/";
197 case Rem: return "%";
198 case Add: return "+";
199 case Sub: return "-";
200 case Shl: return "<<";
201 case Shr: return ">>";
202 case LT: return "<";
203 case GT: return ">";
204 case LE: return "<=";
205 case GE: return ">=";
206 case EQ: return "==";
207 case NE: return "!=";
208 case And: return "&";
209 case Xor: return "^";
210 case Or: return "|";
211 case LAnd: return "&&";
212 case LOr: return "||";
213 case Assign: return "=";
214 case MulAssign: return "*=";
215 case DivAssign: return "/=";
216 case RemAssign: return "%=";
217 case AddAssign: return "+=";
218 case SubAssign: return "-=";
219 case ShlAssign: return "<<=";
220 case ShrAssign: return ">>=";
221 case AndAssign: return "&=";
222 case XorAssign: return "^=";
223 case OrAssign: return "|=";
224 case Comma: return ",";
225 }
Douglas Gregor535f3122009-03-12 22:51:37 +0000226
227 return "";
Chris Lattner4b009652007-07-25 00:24:17 +0000228}
229
Anders Carlsson762b7c72007-08-31 04:56:16 +0000230InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000231 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000232 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000233 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000234 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000235 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000236
237 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000238}
Chris Lattner4b009652007-07-25 00:24:17 +0000239
Douglas Gregorf603b472009-01-28 21:54:33 +0000240void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000241 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000242 Idx < LastIdx; ++Idx)
Douglas Gregorf603b472009-01-28 21:54:33 +0000243 delete InitExprs[Idx];
244 InitExprs.resize(NumInits, 0);
245}
246
247Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
248 if (Init >= InitExprs.size()) {
249 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
250 InitExprs.back() = expr;
251 return 0;
252 }
253
254 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
255 InitExprs[Init] = expr;
256 return Result;
257}
258
Steve Naroff6f373332008-09-04 15:31:07 +0000259/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000260///
261const FunctionType *BlockExpr::getFunctionType() const {
262 return getType()->getAsBlockPointerType()->
263 getPointeeType()->getAsFunctionType();
264}
265
Steve Naroff9ac456d2008-10-08 17:01:13 +0000266SourceLocation BlockExpr::getCaretLocation() const {
267 return TheBlock->getCaretLocation();
268}
269const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
270Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
271
272
Chris Lattner4b009652007-07-25 00:24:17 +0000273//===----------------------------------------------------------------------===//
274// Generic Expression Routines
275//===----------------------------------------------------------------------===//
276
Chris Lattnerd2c66552009-02-14 07:37:35 +0000277/// isUnusedResultAWarning - Return true if this immediate expression should
278/// be warned about if the result is unused. If so, fill in Loc and Ranges
279/// with location to warn on and the source range[s] to report with the
280/// warning.
281bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
282 SourceRange &R2) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000283 switch (getStmtClass()) {
284 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000285 Loc = getExprLoc();
286 R1 = getSourceRange();
287 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000288 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000289 return cast<ParenExpr>(this)->getSubExpr()->
290 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000291 case UnaryOperatorClass: {
292 const UnaryOperator *UO = cast<UnaryOperator>(this);
293
294 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000295 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000296 case UnaryOperator::PostInc:
297 case UnaryOperator::PostDec:
298 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000299 case UnaryOperator::PreDec: // ++/--
300 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000301 case UnaryOperator::Deref:
302 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000303 if (getType().isVolatileQualified())
304 return false;
305 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000306 case UnaryOperator::Real:
307 case UnaryOperator::Imag:
308 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000309 if (UO->getSubExpr()->getType().isVolatileQualified())
310 return false;
311 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000312 case UnaryOperator::Extension:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000313 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000314 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000315 Loc = UO->getOperatorLoc();
316 R1 = UO->getSubExpr()->getSourceRange();
317 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000318 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000319 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000320 const BinaryOperator *BO = cast<BinaryOperator>(this);
321 // Consider comma to have side effects if the LHS or RHS does.
322 if (BO->getOpcode() == BinaryOperator::Comma)
323 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
324 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000325
Chris Lattnerd2c66552009-02-14 07:37:35 +0000326 if (BO->isAssignmentOp())
327 return false;
328 Loc = BO->getOperatorLoc();
329 R1 = BO->getLHS()->getSourceRange();
330 R2 = BO->getRHS()->getSourceRange();
331 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000332 }
Chris Lattner06078d22007-08-25 02:00:02 +0000333 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000334 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000335
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000336 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000337 // The condition must be evaluated, but if either the LHS or RHS is a
338 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000339 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump399ad182009-02-27 03:16:57 +0000340 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000341 return true;
342 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000343 }
344
Chris Lattner4b009652007-07-25 00:24:17 +0000345 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000346 // If the base pointer or element is to a volatile pointer/field, accessing
347 // it is a side effect.
348 if (getType().isVolatileQualified())
349 return false;
350 Loc = cast<MemberExpr>(this)->getMemberLoc();
351 R1 = SourceRange(Loc, Loc);
352 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
353 return true;
354
Chris Lattner4b009652007-07-25 00:24:17 +0000355 case ArraySubscriptExprClass:
356 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000357 // it is a side effect.
358 if (getType().isVolatileQualified())
359 return false;
360 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
361 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
362 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
363 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000364
Chris Lattner4b009652007-07-25 00:24:17 +0000365 case CallExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000366 case CXXOperatorCallExprClass: {
367 // If this is a direct call, get the callee.
368 const CallExpr *CE = cast<CallExpr>(this);
369 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
370 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
371 // If the callee has attribute pure, const, or warn_unused_result, warn
372 // about it. void foo() { strlen("bar"); } should warn.
373 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
374 if (FD->getAttr<WarnUnusedResultAttr>() ||
375 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
376 Loc = CE->getCallee()->getLocStart();
377 R1 = CE->getCallee()->getSourceRange();
378
379 if (unsigned NumArgs = CE->getNumArgs())
380 R2 = SourceRange(CE->getArg(0)->getLocStart(),
381 CE->getArg(NumArgs-1)->getLocEnd());
382 return true;
383 }
384 }
385 return false;
386 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000387 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000388 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000389 case StmtExprClass: {
390 // Statement exprs don't logically have side effects themselves, but are
391 // sometimes used in macros in ways that give them a type that is unused.
392 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
393 // however, if the result of the stmt expr is dead, we don't want to emit a
394 // warning.
395 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
396 if (!CS->body_empty())
397 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000398 return E->isUnusedResultAWarning(Loc, R1, R2);
399
400 Loc = cast<StmtExpr>(this)->getLParenLoc();
401 R1 = getSourceRange();
402 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000403 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000404 case CStyleCastExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000405 // If this is a cast to void, check the operand. Otherwise, the result of
406 // the cast is unused.
407 if (getType()->isVoidType())
408 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
409 R1, R2);
410 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
411 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
412 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000413 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000414 // If this is a cast to void, check the operand. Otherwise, the result of
415 // the cast is unused.
416 if (getType()->isVoidType())
Chris Lattnerd2c66552009-02-14 07:37:35 +0000417 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
418 R1, R2);
419 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
420 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
421 return true;
422
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000423 case ImplicitCastExprClass:
424 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000425 return cast<ImplicitCastExpr>(this)
426 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000427
Chris Lattner3e254fb2008-04-08 04:40:51 +0000428 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000429 return cast<CXXDefaultArgExpr>(this)
430 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000431
432 case CXXNewExprClass:
433 // FIXME: In theory, there might be new expressions that don't have side
434 // effects (e.g. a placement new with an uninitialized POD).
435 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000436 return false;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000437 }
Chris Lattner4b009652007-07-25 00:24:17 +0000438}
439
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000440/// DeclCanBeLvalue - Determine whether the given declaration can be
441/// an lvalue. This is a helper routine for isLvalue.
442static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000443 // C++ [temp.param]p6:
444 // A non-type non-reference template-parameter is not an lvalue.
445 if (const NonTypeTemplateParmDecl *NTTParm
446 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
447 return NTTParm->getType()->isReferenceType();
448
Douglas Gregor8acb7272008-12-11 16:49:14 +0000449 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000450 // C++ 3.10p2: An lvalue refers to an object or function.
451 (Ctx.getLangOptions().CPlusPlus &&
452 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
453}
454
Chris Lattner4b009652007-07-25 00:24:17 +0000455/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
456/// incomplete type other than void. Nonarray expressions that can be lvalues:
457/// - name, where name must be a variable
458/// - e[i]
459/// - (e), where e must be an lvalue
460/// - e.name, where e must be an lvalue
461/// - e->name
462/// - *e, the type of e cannot be a function type
463/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000464/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000465/// - reference type [C++ [expr]]
466///
Chris Lattner25168a52008-07-26 21:30:36 +0000467Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000468 // first, check the type (C99 6.3.2.1). Expressions with function
469 // type in C are not lvalues, but they can be lvalues in C++.
470 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000471 return LV_NotObjectType;
472
Steve Naroffec7736d2008-02-10 01:39:04 +0000473 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000474 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000475 return LV_IncompleteVoidType;
476
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000477 /// FIXME: Expressions can't have reference type, so the following
478 /// isn't needed.
Chris Lattner4b009652007-07-25 00:24:17 +0000479 if (TR->isReferenceType()) // C++ [expr]
480 return LV_Valid;
481
482 // the type looks fine, now check the expression
483 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000484 case StringLiteralClass: // C99 6.5.1p4
485 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000486 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000487 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
488 // For vectors, make sure base is an lvalue (i.e. not a function call).
489 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000490 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000491 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000492 case DeclRefExprClass:
493 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000494 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
495 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000496 return LV_Valid;
497 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000498 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000499 case BlockDeclRefExprClass: {
500 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000501 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000502 return LV_Valid;
503 break;
504 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000505 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000506 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000507 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
508 NamedDecl *Member = m->getMemberDecl();
509 // C++ [expr.ref]p4:
510 // If E2 is declared to have type "reference to T", then E1.E2
511 // is an lvalue.
512 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
513 if (Value->getType()->isReferenceType())
514 return LV_Valid;
515
516 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor00660582009-03-11 20:22:50 +0000517 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor82d44772008-12-20 23:49:58 +0000518 return LV_Valid;
519
520 // -- If E2 is a non-static data member [...]. If E1 is an
521 // lvalue, then E1.E2 is an lvalue.
522 if (isa<FieldDecl>(Member))
523 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
524
525 // -- If it refers to a static member function [...], then
526 // E1.E2 is an lvalue.
527 // -- Otherwise, if E1.E2 refers to a non-static member
528 // function [...], then E1.E2 is not an lvalue.
529 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
530 return Method->isStatic()? LV_Valid : LV_MemberFunction;
531
532 // -- If E2 is a member enumerator [...], the expression E1.E2
533 // is not an lvalue.
534 if (isa<EnumConstantDecl>(Member))
535 return LV_InvalidExpression;
536
537 // Not an lvalue.
538 return LV_InvalidExpression;
539 }
540
541 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000542 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000543 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000544 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000545 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000546 return LV_Valid; // C99 6.5.3p4
547
548 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000549 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
550 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000551 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000552
553 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
554 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
555 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
556 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000557 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000558 case ImplicitCastExprClass:
559 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
560 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000561 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000562 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000563 case BinaryOperatorClass:
564 case CompoundAssignOperatorClass: {
565 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000566
567 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
568 BinOp->getOpcode() == BinaryOperator::Comma)
569 return BinOp->getRHS()->isLvalue(Ctx);
570
Sebastian Redl95216a62009-02-07 00:15:38 +0000571 // C++ [expr.mptr.oper]p6
572 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
573 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
574 !BinOp->getType()->isFunctionType())
575 return BinOp->getLHS()->isLvalue(Ctx);
576
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000577 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000578 return LV_InvalidExpression;
579
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000580 if (Ctx.getLangOptions().CPlusPlus)
581 // C++ [expr.ass]p1:
582 // The result of an assignment operation [...] is an lvalue.
583 return LV_Valid;
584
585
586 // C99 6.5.16:
587 // An assignment expression [...] is not an lvalue.
588 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000589 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000590 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000591 case CXXOperatorCallExprClass:
592 case CXXMemberCallExprClass: {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000593 // C++ [expr.call]p10:
594 // A function call is an lvalue if and only if the result type
595 // is a reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000596 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000597 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000598 CalleeType = FnTypePtr->getPointeeType();
599 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
600 if (FnType->getResultType()->isReferenceType())
601 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000602
603 break;
604 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000605 case CompoundLiteralExprClass: // C99 6.5.2.5p5
606 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000607 case ChooseExprClass:
608 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000609 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000610 case ExtVectorElementExprClass:
611 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000612 return LV_DuplicateVectorComponents;
613 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000614 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
615 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000616 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
617 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000618 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000619 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000620 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000621 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000622 case VAArgExprClass:
Daniel Dunbar09a82e32009-02-12 09:21:08 +0000623 return LV_NotObjectType;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000624 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000625 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000626 case CXXConditionDeclExprClass:
627 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000628 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000629 case CXXFunctionalCastExprClass:
630 case CXXStaticCastExprClass:
631 case CXXDynamicCastExprClass:
632 case CXXReinterpretCastExprClass:
633 case CXXConstCastExprClass:
634 // The result of an explicit cast is an lvalue if the type we are
635 // casting to is a reference type. See C++ [expr.cast]p1,
636 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
637 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
638 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
639 return LV_Valid;
640 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000641 case CXXTypeidExprClass:
642 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
643 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000644 default:
645 break;
646 }
647 return LV_InvalidExpression;
648}
649
650/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
651/// does not have an incomplete type, does not have a const-qualified type, and
652/// if it is a structure or union, does not have any member (including,
653/// recursively, any member or element of all contained aggregates or unions)
654/// with a const-qualified type.
Chris Lattner25168a52008-07-26 21:30:36 +0000655Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
656 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000657
658 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000659 case LV_Valid:
660 // C++ 3.10p11: Functions cannot be modified, but pointers to
661 // functions can be modifiable.
662 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
663 return MLV_NotObjectType;
664 break;
665
Chris Lattner4b009652007-07-25 00:24:17 +0000666 case LV_NotObjectType: return MLV_NotObjectType;
667 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000668 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000669 case LV_InvalidExpression:
670 // If the top level is a C-style cast, and the subexpression is a valid
671 // lvalue, then this is probably a use of the old-school "cast as lvalue"
672 // GCC extension. We don't support it, but we want to produce good
673 // diagnostics when it happens so that the user knows why.
674 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
675 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
676 return MLV_LValueCast;
677 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000678 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000679 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000680
681 QualType CT = Ctx.getCanonicalType(getType());
682
683 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000684 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000685 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000686 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000687 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000688 return MLV_IncompleteType;
689
Chris Lattnera1923f62008-08-04 07:31:14 +0000690 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000691 if (r->hasConstFields())
692 return MLV_ConstQualified;
693 }
Steve Naroff076d6cb2008-09-26 14:41:28 +0000694 // The following is illegal:
695 // void takeclosure(void (^C)(void));
696 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
697 //
698 if (getStmtClass() == BlockDeclRefExprClass) {
699 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
700 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
701 return MLV_NotBlockQualified;
702 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000703
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000704 // Assigning to an 'implicit' property?
Fariborz Jahanian48b1a132008-11-25 17:56:43 +0000705 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000706 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
707 if (KVCExpr->getSetterMethod() == 0)
708 return MLV_NoSetterProperty;
709 }
Chris Lattner4b009652007-07-25 00:24:17 +0000710 return MLV_Valid;
711}
712
Ted Kremenek5778d622008-02-27 18:39:48 +0000713/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000714/// duration. This means that the address of this expression is a link-time
715/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000716bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000717 switch (getStmtClass()) {
718 default:
719 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000720 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000721 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000722 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000723 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000724 case CompoundLiteralExprClass:
725 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000726 case DeclRefExprClass:
727 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000728 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
729 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000730 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000731 if (isa<FunctionDecl>(D))
732 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000733 return false;
734 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000735 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000736 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000737 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000738 }
Chris Lattner743ec372007-11-27 21:35:27 +0000739 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000740 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000741 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000742 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000743 case CXXDefaultArgExprClass:
744 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000745 }
746}
747
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000748/// isOBJCGCCandidate - Check if an expression is objc gc'able.
749///
750bool Expr::isOBJCGCCandidate() const {
751 switch (getStmtClass()) {
752 default:
753 return false;
754 case ObjCIvarRefExprClass:
755 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000756 case Expr::UnaryOperatorClass:
757 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000758 case ParenExprClass:
759 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
760 case ImplicitCastExprClass:
761 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
762 case DeclRefExprClass:
763 case QualifiedDeclRefExprClass: {
764 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
765 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
766 return VD->hasGlobalStorage();
767 return false;
768 }
769 case MemberExprClass: {
770 const MemberExpr *M = cast<MemberExpr>(this);
771 return !M->isArrow() && M->getBase()->isOBJCGCCandidate();
772 }
773 case ArraySubscriptExprClass:
774 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
775 }
776}
Ted Kremenek87e30c52008-01-17 16:57:34 +0000777Expr* Expr::IgnoreParens() {
778 Expr* E = this;
779 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
780 E = P->getSubExpr();
781
782 return E;
783}
784
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000785/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
786/// or CastExprs or ImplicitCastExprs, returning their operand.
787Expr *Expr::IgnoreParenCasts() {
788 Expr *E = this;
789 while (true) {
790 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
791 E = P->getSubExpr();
792 else if (CastExpr *P = dyn_cast<CastExpr>(E))
793 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000794 else
795 return E;
796 }
797}
798
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000799/// hasAnyTypeDependentArguments - Determines if any of the expressions
800/// in Exprs is type-dependent.
801bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
802 for (unsigned I = 0; I < NumExprs; ++I)
803 if (Exprs[I]->isTypeDependent())
804 return true;
805
806 return false;
807}
808
809/// hasAnyValueDependentArguments - Determines if any of the expressions
810/// in Exprs is value-dependent.
811bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
812 for (unsigned I = 0; I < NumExprs; ++I)
813 if (Exprs[I]->isValueDependent())
814 return true;
815
816 return false;
817}
818
Eli Friedmandee41122009-01-25 02:32:41 +0000819bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000820 // This function is attempting whether an expression is an initializer
821 // which can be evaluated at compile-time. isEvaluatable handles most
822 // of the cases, but it can't deal with some initializer-specific
823 // expressions, and it can't deal with aggregates; we deal with those here,
824 // and fall back to isEvaluatable for the other cases.
825
Eli Friedman3dc50ed2009-02-20 02:36:22 +0000826 // FIXME: This function assumes the variable being assigned to
827 // isn't a reference type!
828
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000829 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000830 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000831 case StringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +0000832 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000833 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +0000834 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +0000835 // This handles gcc's extension that allows global initializers like
836 // "struct x {int x;} x = (struct x) {};".
837 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +0000838 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +0000839 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +0000840 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000841 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +0000842 // FIXME: This doesn't deal with fields with reference types correctly.
843 // FIXME: This incorrectly allows pointers cast to integers to be assigned
844 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000845 const InitListExpr *Exp = cast<InitListExpr>(this);
846 unsigned numInits = Exp->getNumInits();
847 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +0000848 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000849 return false;
850 }
Eli Friedman2b0dec52009-01-25 03:12:18 +0000851 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000852 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000853 case ImplicitValueInitExprClass:
854 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +0000855 case ParenExprClass: {
856 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
857 }
858 case UnaryOperatorClass: {
859 const UnaryOperator* Exp = cast<UnaryOperator>(this);
860 if (Exp->getOpcode() == UnaryOperator::Extension)
861 return Exp->getSubExpr()->isConstantInitializer(Ctx);
862 break;
863 }
864 case CStyleCastExprClass:
865 // Handle casts with a destination that's a struct or union; this
866 // deals with both the gcc no-op struct cast extension and the
867 // cast-to-union extension.
868 if (getType()->isRecordType())
869 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
870 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000871 }
872
Eli Friedman2b0dec52009-01-25 03:12:18 +0000873 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000874}
875
Chris Lattner4b009652007-07-25 00:24:17 +0000876/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +0000877/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +0000878
879/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
880/// comma, etc
881///
Chris Lattner4b009652007-07-25 00:24:17 +0000882/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
883/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
884/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +0000885
Eli Friedman7beeda62009-02-26 09:29:13 +0000886// CheckICE - This function does the fundamental ICE checking: the returned
887// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
888// Note that to reduce code duplication, this helper does no evaluation
889// itself; the caller checks whether the expression is evaluatable, and
890// in the rare cases where CheckICE actually cares about the evaluated
891// value, it calls into Evalute.
892//
893// Meanings of Val:
894// 0: This expression is an ICE if it can be evaluated by Evaluate.
895// 1: This expression is not an ICE, but if it isn't evaluated, it's
896// a legal subexpression for an ICE. This return value is used to handle
897// the comma operator in C99 mode.
898// 2: This expression is not an ICE, and is not a legal subexpression for one.
899
900struct ICEDiag {
901 unsigned Val;
902 SourceLocation Loc;
903
904 public:
905 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
906 ICEDiag() : Val(0) {}
907};
908
909ICEDiag NoDiag() { return ICEDiag(); }
910
Eli Friedmand65bbbc2009-02-27 04:07:58 +0000911static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
912 Expr::EvalResult EVResult;
913 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
914 !EVResult.Val.isInt()) {
915 return ICEDiag(2, E->getLocStart());
916 }
917 return NoDiag();
918}
919
Eli Friedman7beeda62009-02-26 09:29:13 +0000920static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
921 if (!E->getType()->isIntegralType()) {
922 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +0000923 }
Eli Friedman7beeda62009-02-26 09:29:13 +0000924
925 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000926 default:
Eli Friedman7beeda62009-02-26 09:29:13 +0000927 return ICEDiag(2, E->getLocStart());
928 case Expr::ParenExprClass:
929 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
930 case Expr::IntegerLiteralClass:
931 case Expr::CharacterLiteralClass:
932 case Expr::CXXBoolLiteralExprClass:
933 case Expr::CXXZeroInitValueExprClass:
934 case Expr::TypesCompatibleExprClass:
935 case Expr::UnaryTypeTraitExprClass:
936 return NoDiag();
937 case Expr::CallExprClass:
938 case Expr::CXXOperatorCallExprClass: {
939 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +0000940 if (CE->isBuiltinCall(Ctx))
941 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +0000942 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +0000943 }
Eli Friedman7beeda62009-02-26 09:29:13 +0000944 case Expr::DeclRefExprClass:
945 case Expr::QualifiedDeclRefExprClass:
946 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
947 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000948 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +0000949 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000950 // C++ 7.1.5.1p2
951 // A variable of non-volatile const-qualified integral or enumeration
952 // type initialized by an ICE can be used in ICEs.
953 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +0000954 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000955 if (const Expr *Init = Dcl->getInit())
Eli Friedman7beeda62009-02-26 09:29:13 +0000956 return CheckICE(Init, Ctx);
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000957 }
958 }
Eli Friedman7beeda62009-02-26 09:29:13 +0000959 return ICEDiag(2, E->getLocStart());
960 case Expr::UnaryOperatorClass: {
961 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +0000962 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000963 default:
Eli Friedman7beeda62009-02-26 09:29:13 +0000964 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +0000965 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +0000966 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +0000967 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +0000968 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +0000969 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +0000970 case UnaryOperator::Real:
971 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +0000972 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +0000973 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +0000974 // Note that per C99, offsetof must be an ICE. And AFAIK, using
975 // Evaluate matches the proposed gcc behavior for cases like
976 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
977 // compliance: we should warn earlier for offsetof expressions with
978 // array subscripts that aren't ICEs, and if the array subscripts
979 // are ICEs, the value of the offsetof must be an integer constant.
980 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000981 }
Chris Lattner4b009652007-07-25 00:24:17 +0000982 }
Eli Friedman7beeda62009-02-26 09:29:13 +0000983 case Expr::SizeOfAlignOfExprClass: {
984 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
985 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
986 return ICEDiag(2, E->getLocStart());
987 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +0000988 }
Eli Friedman7beeda62009-02-26 09:29:13 +0000989 case Expr::BinaryOperatorClass: {
990 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +0000991 switch (Exp->getOpcode()) {
992 default:
Eli Friedman7beeda62009-02-26 09:29:13 +0000993 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +0000994 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +0000995 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +0000996 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +0000997 case BinaryOperator::Add:
998 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +0000999 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001000 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001001 case BinaryOperator::LT:
1002 case BinaryOperator::GT:
1003 case BinaryOperator::LE:
1004 case BinaryOperator::GE:
1005 case BinaryOperator::EQ:
1006 case BinaryOperator::NE:
1007 case BinaryOperator::And:
1008 case BinaryOperator::Xor:
1009 case BinaryOperator::Or:
1010 case BinaryOperator::Comma: {
1011 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1012 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001013 if (Exp->getOpcode() == BinaryOperator::Div ||
1014 Exp->getOpcode() == BinaryOperator::Rem) {
1015 // Evaluate gives an error for undefined Div/Rem, so make sure
1016 // we don't evaluate one.
1017 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1018 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1019 if (REval == 0)
1020 return ICEDiag(1, E->getLocStart());
1021 if (REval.isSigned() && REval.isAllOnesValue()) {
1022 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1023 if (LEval.isMinSignedValue())
1024 return ICEDiag(1, E->getLocStart());
1025 }
1026 }
1027 }
1028 if (Exp->getOpcode() == BinaryOperator::Comma) {
1029 if (Ctx.getLangOptions().C99) {
1030 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1031 // if it isn't evaluated.
1032 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1033 return ICEDiag(1, E->getLocStart());
1034 } else {
1035 // In both C89 and C++, commas in ICEs are illegal.
1036 return ICEDiag(2, E->getLocStart());
1037 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001038 }
1039 if (LHSResult.Val >= RHSResult.Val)
1040 return LHSResult;
1041 return RHSResult;
1042 }
Chris Lattner4b009652007-07-25 00:24:17 +00001043 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001044 case BinaryOperator::LOr: {
1045 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1046 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1047 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1048 // Rare case where the RHS has a comma "side-effect"; we need
1049 // to actually check the condition to see whether the side
1050 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001051 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001052 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001053 return RHSResult;
1054 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001055 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001056
Eli Friedman7beeda62009-02-26 09:29:13 +00001057 if (LHSResult.Val >= RHSResult.Val)
1058 return LHSResult;
1059 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001060 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001061 }
Chris Lattner4b009652007-07-25 00:24:17 +00001062 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001063 case Expr::ImplicitCastExprClass:
1064 case Expr::CStyleCastExprClass:
1065 case Expr::CXXFunctionalCastExprClass: {
1066 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1067 if (SubExpr->getType()->isIntegralType())
1068 return CheckICE(SubExpr, Ctx);
1069 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1070 return NoDiag();
1071 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001072 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001073 case Expr::ConditionalOperatorClass: {
1074 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001075 // If the condition (ignoring parens) is a __builtin_constant_p call,
1076 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001077 // expression, and it is fully evaluated. This is an important GNU
1078 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001079 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001080 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001081 Expr::EvalResult EVResult;
1082 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1083 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001084 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001085 }
1086 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001087 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001088 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1089 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1090 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1091 if (CondResult.Val == 2)
1092 return CondResult;
1093 if (TrueResult.Val == 2)
1094 return TrueResult;
1095 if (FalseResult.Val == 2)
1096 return FalseResult;
1097 if (CondResult.Val == 1)
1098 return CondResult;
1099 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1100 return NoDiag();
1101 // Rare case where the diagnostics depend on which side is evaluated
1102 // Note that if we get here, CondResult is 0, and at least one of
1103 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001104 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001105 return FalseResult;
1106 }
1107 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001108 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001109 case Expr::CXXDefaultArgExprClass:
1110 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001111 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001112 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001113 }
Chris Lattner4b009652007-07-25 00:24:17 +00001114 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001115}
Chris Lattner4b009652007-07-25 00:24:17 +00001116
Eli Friedman7beeda62009-02-26 09:29:13 +00001117bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1118 SourceLocation *Loc, bool isEvaluated) const {
1119 ICEDiag d = CheckICE(this, Ctx);
1120 if (d.Val != 0) {
1121 if (Loc) *Loc = d.Loc;
1122 return false;
1123 }
1124 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001125 if (!Evaluate(EvalResult, Ctx))
1126 assert(0 && "ICE cannot be evaluated!");
1127 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1128 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001129 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001130 return true;
1131}
1132
Chris Lattner4b009652007-07-25 00:24:17 +00001133/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1134/// integer constant expression with the value zero, or if this is one that is
1135/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001136bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1137{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001138 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001139 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001140 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001141 // Check that it is a cast to void*.
1142 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1143 QualType Pointee = PT->getPointeeType();
1144 if (Pointee.getCVRQualifiers() == 0 &&
1145 Pointee->isVoidType() && // to void*
1146 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001147 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001148 }
Chris Lattner4b009652007-07-25 00:24:17 +00001149 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001150 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1151 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001152 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001153 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1154 // Accept ((void*)0) as a null pointer constant, as many other
1155 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001156 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001157 } else if (const CXXDefaultArgExpr *DefaultArg
1158 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001159 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001160 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001161 } else if (isa<GNUNullExpr>(this)) {
1162 // The GNU __null extension is always a null pointer constant.
1163 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001164 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001165
Steve Naroffa2e53222008-01-14 16:10:57 +00001166 // This expression must be an integer type.
1167 if (!getType()->isIntegerType())
1168 return false;
1169
Chris Lattner4b009652007-07-25 00:24:17 +00001170 // If we have an integer constant expression, we need to *evaluate* it and
1171 // test for the value 0.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001172 // FIXME: We should probably return false if we're compiling in strict mode
1173 // and Diag is not null (this indicates that the value was foldable but not
1174 // an ICE.
1175 EvalResult Result;
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001176 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001177 Result.Val.isInt() && Result.Val.getInt() == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001178}
Steve Naroffc11705f2007-07-28 23:10:27 +00001179
Douglas Gregor81c29152008-10-29 00:13:59 +00001180/// isBitField - Return true if this expression is a bit-field.
1181bool Expr::isBitField() {
1182 Expr *E = this->IgnoreParenCasts();
1183 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001184 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1185 return Field->isBitField();
Douglas Gregor81c29152008-10-29 00:13:59 +00001186 return false;
1187}
1188
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001189/// isArrow - Return true if the base expression is a pointer to vector,
1190/// return false if the base expression is a vector.
1191bool ExtVectorElementExpr::isArrow() const {
1192 return getBase()->getType()->isPointerType();
1193}
1194
Nate Begemanaf6ed502008-04-18 23:10:10 +00001195unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001196 if (const VectorType *VT = getType()->getAsVectorType())
1197 return VT->getNumElements();
1198 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001199}
1200
Nate Begemanc8e51f82008-05-09 06:41:27 +00001201/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001202bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001203 const char *compStr = Accessor.getName();
Chris Lattner58d3fa52008-11-19 07:55:04 +00001204 unsigned length = Accessor.getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001205
1206 // Halving swizzles do not contain duplicate elements.
1207 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1208 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1209 return false;
1210
1211 // Advance past s-char prefix on hex swizzles.
1212 if (*compStr == 's') {
1213 compStr++;
1214 length--;
1215 }
Steve Naroffba67f692007-07-30 03:29:09 +00001216
Chris Lattner58d3fa52008-11-19 07:55:04 +00001217 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001218 const char *s = compStr+i;
1219 for (const char c = *s++; *s; s++)
1220 if (c == *s)
1221 return true;
1222 }
1223 return false;
1224}
Chris Lattner42158e72007-08-02 23:36:59 +00001225
Nate Begemanc8e51f82008-05-09 06:41:27 +00001226/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001227void ExtVectorElementExpr::getEncodedElementAccess(
1228 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner58d3fa52008-11-19 07:55:04 +00001229 const char *compStr = Accessor.getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001230 if (*compStr == 's')
1231 compStr++;
1232
1233 bool isHi = !strcmp(compStr, "hi");
1234 bool isLo = !strcmp(compStr, "lo");
1235 bool isEven = !strcmp(compStr, "even");
1236 bool isOdd = !strcmp(compStr, "odd");
1237
Nate Begemanc8e51f82008-05-09 06:41:27 +00001238 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1239 uint64_t Index;
1240
1241 if (isHi)
1242 Index = e + i;
1243 else if (isLo)
1244 Index = i;
1245 else if (isEven)
1246 Index = 2 * i;
1247 else if (isOdd)
1248 Index = 2 * i + 1;
1249 else
1250 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001251
Nate Begemana1ae7442008-05-13 21:03:02 +00001252 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001253 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001254}
1255
Steve Naroff4ed9d662007-09-27 14:38:14 +00001256// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001257ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001258 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001259 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001260 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001261 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001262 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001263 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001264 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001265 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001266 if (NumArgs) {
1267 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001268 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1269 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001270 LBracloc = LBrac;
1271 RBracloc = RBrac;
1272}
1273
Steve Naroff4ed9d662007-09-27 14:38:14 +00001274// constructor for class messages.
1275// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001276ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001277 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001278 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001279 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001280 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001281 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001282 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001283 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001284 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001285 if (NumArgs) {
1286 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001287 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1288 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001289 LBracloc = LBrac;
1290 RBracloc = RBrac;
1291}
1292
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001293// constructor for class messages.
1294ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1295 QualType retType, ObjCMethodDecl *mproto,
1296 SourceLocation LBrac, SourceLocation RBrac,
1297 Expr **ArgExprs, unsigned nargs)
1298: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1299MethodProto(mproto) {
1300 NumArgs = nargs;
1301 SubExprs = new Stmt*[NumArgs+1];
1302 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1303 if (NumArgs) {
1304 for (unsigned i = 0; i != NumArgs; ++i)
1305 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1306 }
1307 LBracloc = LBrac;
1308 RBracloc = RBrac;
1309}
1310
1311ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1312 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1313 switch (x & Flags) {
1314 default:
1315 assert(false && "Invalid ObjCMessageExpr.");
1316 case IsInstMeth:
1317 return ClassInfo(0, 0);
1318 case IsClsMethDeclUnknown:
1319 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1320 case IsClsMethDeclKnown: {
1321 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1322 return ClassInfo(D, D->getIdentifier());
1323 }
1324 }
1325}
1326
Chris Lattnerf624cd22007-10-25 00:29:32 +00001327bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001328 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001329}
1330
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001331void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1332 // Override default behavior of traversing children. If this has a type
1333 // operand and the type is a variable-length array, the child iteration
1334 // will iterate over the size expression. However, this expression belongs
1335 // to the type, not to this, so we don't want to delete it.
1336 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001337 if (isArgumentType()) {
1338 this->~SizeOfAlignOfExpr();
1339 C.Deallocate(this);
1340 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001341 else
1342 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001343}
1344
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001345//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001346// DesignatedInitExpr
1347//===----------------------------------------------------------------------===//
1348
1349IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1350 assert(Kind == FieldDesignator && "Only valid on a field designator");
1351 if (Field.NameOrField & 0x01)
1352 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1353 else
1354 return getField()->getIdentifier();
1355}
1356
1357DesignatedInitExpr *
1358DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1359 unsigned NumDesignators,
1360 Expr **IndexExprs, unsigned NumIndexExprs,
1361 SourceLocation ColonOrEqualLoc,
1362 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001363 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1364 sizeof(Designator) * NumDesignators +
1365 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001366 DesignatedInitExpr *DIE
1367 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1368 ColonOrEqualLoc, UsesColonSyntax,
1369 NumIndexExprs + 1);
1370
1371 // Fill in the designators
1372 unsigned ExpectedNumSubExprs = 0;
1373 designators_iterator Desig = DIE->designators_begin();
1374 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1375 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1376 if (Designators[Idx].isArrayDesignator())
1377 ++ExpectedNumSubExprs;
1378 else if (Designators[Idx].isArrayRangeDesignator())
1379 ExpectedNumSubExprs += 2;
1380 }
1381 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1382
1383 // Fill in the subexpressions, including the initializer expression.
1384 child_iterator Child = DIE->child_begin();
1385 *Child++ = Init;
1386 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1387 *Child = IndexExprs[Idx];
1388
1389 return DIE;
1390}
1391
1392SourceRange DesignatedInitExpr::getSourceRange() const {
1393 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001394 Designator &First =
1395 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001396 if (First.isFieldDesignator()) {
1397 if (UsesColonSyntax)
1398 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1399 else
1400 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1401 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001402 StartLoc =
1403 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001404 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1405}
1406
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001407DesignatedInitExpr::designators_iterator
1408DesignatedInitExpr::designators_begin() {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001409 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1410 Ptr += sizeof(DesignatedInitExpr);
1411 return static_cast<Designator*>(static_cast<void*>(Ptr));
1412}
1413
1414DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1415 return designators_begin() + NumDesignators;
1416}
1417
1418Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1419 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1420 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1421 Ptr += sizeof(DesignatedInitExpr);
1422 Ptr += sizeof(Designator) * NumDesignators;
1423 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1424 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1425}
1426
1427Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1428 assert(D.Kind == Designator::ArrayRangeDesignator &&
1429 "Requires array range designator");
1430 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1431 Ptr += sizeof(DesignatedInitExpr);
1432 Ptr += sizeof(Designator) * NumDesignators;
1433 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1434 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1435}
1436
1437Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1438 assert(D.Kind == Designator::ArrayRangeDesignator &&
1439 "Requires array range designator");
1440 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1441 Ptr += sizeof(DesignatedInitExpr);
1442 Ptr += sizeof(Designator) * NumDesignators;
1443 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1444 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1445}
1446
1447//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001448// ExprIterator.
1449//===----------------------------------------------------------------------===//
1450
1451Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1452Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1453Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1454const Expr* ConstExprIterator::operator[](size_t idx) const {
1455 return cast<Expr>(I[idx]);
1456}
1457const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1458const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1459
1460//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001461// Child Iterators for iterating over subexpressions/substatements
1462//===----------------------------------------------------------------------===//
1463
1464// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001465Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1466Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001467
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001468// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001469Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1470Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001471
Steve Naroff6f786252008-06-02 23:03:37 +00001472// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001473Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1474Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001475
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001476// ObjCKVCRefExpr
1477Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1478Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1479
Douglas Gregord8606632008-11-04 14:56:14 +00001480// ObjCSuperExpr
1481Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1482Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1483
Chris Lattner69909292008-08-10 01:53:14 +00001484// PredefinedExpr
1485Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1486Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001487
1488// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001489Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1490Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001491
1492// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001493Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001494Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001495
1496// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001497Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1498Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001499
Chris Lattner1de66eb2007-08-26 03:42:43 +00001500// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001501Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1502Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001503
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001504// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001505Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1506Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001507
1508// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001509Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1510Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001511
1512// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001513Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1514Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001515
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001516// SizeOfAlignOfExpr
1517Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1518 // If this is of a type and the type is a VLA type (and not a typedef), the
1519 // size expression of the VLA needs to be treated as an executable expression.
1520 // Why isn't this weirdness documented better in StmtIterator?
1521 if (isArgumentType()) {
1522 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1523 getArgumentType().getTypePtr()))
1524 return child_iterator(T);
1525 return child_iterator();
1526 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001527 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001528}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001529Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1530 if (isArgumentType())
1531 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001532 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001533}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001534
1535// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001536Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001537 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001538}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001539Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001540 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001541}
1542
1543// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001544Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001545 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001546}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001547Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001548 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001549}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001550
1551// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001552Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1553Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001554
Nate Begemanaf6ed502008-04-18 23:10:10 +00001555// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001556Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1557Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001558
1559// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001560Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1561Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001562
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001563// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001564Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1565Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001566
1567// BinaryOperator
1568Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001569 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001570}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001571Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001572 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001573}
1574
1575// ConditionalOperator
1576Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001577 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001578}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001579Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001580 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001581}
1582
1583// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001584Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1585Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001586
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001587// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001588Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1589Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001590
1591// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001592Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1593 return child_iterator();
1594}
1595
1596Stmt::child_iterator TypesCompatibleExpr::child_end() {
1597 return child_iterator();
1598}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001599
1600// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001601Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1602Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001603
Douglas Gregorad4b3792008-11-29 04:51:27 +00001604// GNUNullExpr
1605Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1606Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1607
Eli Friedmand0e9d092008-05-14 19:38:39 +00001608// ShuffleVectorExpr
1609Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001610 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001611}
1612Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001613 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001614}
1615
Anders Carlsson36760332007-10-15 20:28:48 +00001616// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001617Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1618Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001619
Anders Carlsson762b7c72007-08-31 04:56:16 +00001620// InitListExpr
1621Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001622 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001623}
1624Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001625 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001626}
1627
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001628// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001629Stmt::child_iterator DesignatedInitExpr::child_begin() {
1630 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1631 Ptr += sizeof(DesignatedInitExpr);
1632 Ptr += sizeof(Designator) * NumDesignators;
1633 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1634}
1635Stmt::child_iterator DesignatedInitExpr::child_end() {
1636 return child_iterator(&*child_begin() + NumSubExprs);
1637}
1638
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001639// ImplicitValueInitExpr
1640Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1641 return child_iterator();
1642}
1643
1644Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1645 return child_iterator();
1646}
1647
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001648// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001649Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00001650 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00001651}
1652Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00001653 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00001654}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001655
1656// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001657Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1658Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001659
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001660// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001661Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1662 return child_iterator();
1663}
1664Stmt::child_iterator ObjCSelectorExpr::child_end() {
1665 return child_iterator();
1666}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001667
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001668// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001669Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1670 return child_iterator();
1671}
1672Stmt::child_iterator ObjCProtocolExpr::child_end() {
1673 return child_iterator();
1674}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001675
Steve Naroffc39ca262007-09-18 23:55:05 +00001676// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001677Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001678 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001679}
1680Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001681 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001682}
1683
Steve Naroff52a81c02008-09-03 18:15:37 +00001684// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001685Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1686Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001687
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001688Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1689Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }