blob: 9612f7ff2afb390fe9281b3d5b92ab502b76f67f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000016#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Chris Lattnerda8249e2008-06-07 22:13:43 +000029/// getValueAsApproximateDouble - This returns the value as an inaccurate
30/// double. Note that this may cause loss of precision, but is useful for
31/// debugging dumps, etc.
32double FloatingLiteral::getValueAsApproximateDouble() const {
33 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000034 bool ignored;
35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000037 return V.convertToDouble();
38}
39
Chris Lattner2085fd62009-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
Reid Spencer5f016e22007-07-11 17:01:13 +000051 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +000059
Chris Lattner726e1682009-02-18 05:49:11 +000060 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +000061 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
62 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +000063}
64
65
Ted Kremenek6e94ef52009-02-06 19:55:15 +000066void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000067 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +000068 this->~StringLiteral();
69 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +000070}
71
Reid Spencer5f016e22007-07-11 17:01:13 +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";
Reid Spencer5f016e22007-07-11 17:01:13 +000089 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +000090 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +000091 }
92}
93
94//===----------------------------------------------------------------------===//
95// Postfix Operators.
96//===----------------------------------------------------------------------===//
97
Ted Kremenek668bf912009-02-09 20:51:47 +000098CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +000099 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000100 : Expr(SC, t,
101 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000102 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000103 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000104
105 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-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 Kremenek668bf912009-02-09 20:51:47 +0000109
Douglas Gregorb4609802008-11-14 16:09:21 +0000110 RParenLoc = rparenloc;
111}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000112
Ted Kremenek668bf912009-02-09 20:51:47 +0000113CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
114 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000115 : Expr(CallExprClass, t,
116 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000117 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000118 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000119
120 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000121 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000123 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 RParenLoc = rparenloc;
126}
127
Ted Kremenek668bf912009-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 Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000138void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000145 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-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 Kremenek55499762008-06-17 02:43:46 +0000151 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000159 delete [] SubExprs;
Chris Lattnerd18b3292007-12-28 05:25:02 +0000160 SubExprs = NewSubExprs;
161 this->NumArgs = NumArgs;
162}
163
Chris Lattnercb888962008-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 Gregor3c385e52009-02-14 18:57:46 +0000166unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-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 Lattnercb888962008-10-06 05:00:53 +0000172 return 0;
173
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000174 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
175 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000176 return 0;
177
Anders Carlssonbcba2012008-01-31 02:13:57 +0000178 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
179 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000180 return 0;
181
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000182 if (!FDecl->getIdentifier())
183 return 0;
184
Douglas Gregor3c385e52009-02-14 18:57:46 +0000185 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000186}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000187
Chris Lattnercb888962008-10-06 05:00:53 +0000188
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregorbaf53482009-03-12 22:51:37 +0000193 case PtrMemD: return ".*";
194 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregorbaf53482009-03-12 22:51:37 +0000226
227 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000228}
229
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000230InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000231 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000232 SourceLocation rbraceloc)
Steve Naroffc5ae8992008-05-01 02:04:18 +0000233 : Expr(InitListExprClass, QualType()),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000234 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000235 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000236
237 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000238}
Reid Spencer5f016e22007-07-11 17:01:13 +0000239
Douglas Gregor4c678342009-01-28 21:54:33 +0000240void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000241 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000242 Idx < LastIdx; ++Idx)
Douglas Gregor4c678342009-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 Naroffbfdcae62008-09-04 15:31:07 +0000259/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000260///
261const FunctionType *BlockExpr::getFunctionType() const {
262 return getType()->getAsBlockPointerType()->
263 getPointeeType()->getAsFunctionType();
264}
265
Steve Naroff56ee6892008-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000273//===----------------------------------------------------------------------===//
274// Generic Expression Routines
275//===----------------------------------------------------------------------===//
276
Chris Lattner026dc962009-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 {
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 switch (getStmtClass()) {
284 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000285 Loc = getExprLoc();
286 R1 = getSourceRange();
287 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000289 return cast<ParenExpr>(this)->getSubExpr()->
290 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 case UnaryOperatorClass: {
292 const UnaryOperator *UO = cast<UnaryOperator>(this);
293
294 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000295 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 case UnaryOperator::PostInc:
297 case UnaryOperator::PostDec:
298 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000299 case UnaryOperator::PreDec: // ++/--
300 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 case UnaryOperator::Deref:
302 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000303 if (getType().isVolatileQualified())
304 return false;
305 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 case UnaryOperator::Real:
307 case UnaryOperator::Imag:
308 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000309 if (UO->getSubExpr()->getType().isVolatileQualified())
310 return false;
311 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 case UnaryOperator::Extension:
Chris Lattner026dc962009-02-14 07:37:35 +0000313 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 }
Chris Lattner026dc962009-02-14 07:37:35 +0000315 Loc = UO->getOperatorLoc();
316 R1 = UO->getSubExpr()->getSourceRange();
317 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000319 case BinaryOperatorClass: {
Chris Lattner026dc962009-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 Lattnere7716e62007-12-01 06:07:34 +0000325
Chris Lattner026dc962009-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 Lattnere7716e62007-12-01 06:07:34 +0000332 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000333 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000334 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000335
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000336 case ConditionalOperatorClass: {
Chris Lattner026dc962009-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 Jahanianab38e4b2007-12-01 19:58:28 +0000339 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stumpbefbcf42009-02-27 03:16:57 +0000340 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000341 return true;
342 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000343 }
344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 case MemberExprClass:
Chris Lattner026dc962009-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 case ArraySubscriptExprClass:
356 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-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 Friedman211f6ad2008-05-27 15:24:04 +0000364
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 case CallExprClass:
Chris Lattner026dc962009-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 Lattnera9c01022007-09-26 22:06:30 +0000387 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000388 return false;
Chris Lattner611b2ec2008-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 Lattner026dc962009-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 Lattner611b2ec2008-07-26 19:51:01 +0000403 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000404 case CStyleCastExprClass:
Chris Lattner026dc962009-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;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000413 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner026dc962009-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 Friedman4be1f472008-05-19 21:24:43 +0000423 case ImplicitCastExprClass:
424 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000425 return cast<ImplicitCastExpr>(this)
426 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000427
Chris Lattner04421082008-04-08 04:40:51 +0000428 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000429 return cast<CXXDefaultArgExpr>(this)
430 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-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 Lattner026dc962009-02-14 07:37:35 +0000436 return false;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000437 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000438}
439
Douglas Gregorba7e2102008-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 Gregor72c3f312008-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 Gregor44b43212008-12-11 16:49:14 +0000449 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner7da36f62007-10-30 22:53:42 +0000464/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000465/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000466///
Chris Lattner28be73f2008-07-26 21:30:36 +0000467Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor98cd5992008-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())
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 return LV_NotObjectType;
472
Steve Naroffacb818a2008-02-10 01:39:04 +0000473 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000474 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000475 return LV_IncompleteVoidType;
476
Douglas Gregor98cd5992008-10-21 23:43:52 +0000477 /// FIXME: Expressions can't have reference type, so the following
478 /// isn't needed.
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000479 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000480 return LV_Valid;
481
Reid Spencer5f016e22007-07-11 17:01:13 +0000482 // the type looks fine, now check the expression
483 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000484 case StringLiteralClass: // C99 6.5.1p4
485 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000486 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner28be73f2008-07-26 21:30:36 +0000490 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000492 case DeclRefExprClass:
493 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000494 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
495 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 return LV_Valid;
497 break;
Chris Lattner41110242008-06-17 18:05:57 +0000498 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000499 case BlockDeclRefExprClass: {
500 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000501 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000502 return LV_Valid;
503 break;
504 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000505 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-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 Gregor2d2e9cf2009-03-11 20:22:50 +0000517 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-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 Lattner28be73f2008-07-26 21:30:36 +0000542 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000543 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000544 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000546 return LV_Valid; // C99 6.5.3p4
547
548 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000549 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
550 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000551 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000558 case ImplicitCastExprClass:
559 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
560 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000562 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000563 case BinaryOperatorClass:
564 case CompoundAssignOperatorClass: {
565 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-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 Redl22460502009-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 Gregorbf3af052008-11-13 20:12:29 +0000577 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000578 return LV_InvalidExpression;
579
Douglas Gregorbf3af052008-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 Gregoreb8f3062008-11-12 17:17:38 +0000589 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000590 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000591 case CXXOperatorCallExprClass:
592 case CXXMemberCallExprClass: {
Douglas Gregor9d293df2008-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 Gregor27c8dc02008-10-29 00:13:59 +0000596 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor9d293df2008-10-28 00:22:11 +0000597 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor88a35142008-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 Gregor9d293df2008-10-28 00:22:11 +0000602
603 break;
604 }
Steve Naroffe6386392007-12-05 04:00:10 +0000605 case CompoundLiteralExprClass: // C99 6.5.2.5p5
606 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000607 case ChooseExprClass:
608 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000609 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000610 case ExtVectorElementExprClass:
611 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000612 return LV_DuplicateVectorComponents;
613 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000614 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
615 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000616 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
617 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000618 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000619 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000620 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000621 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000622 case VAArgExprClass:
Daniel Dunbaradadd8d2009-02-12 09:21:08 +0000623 return LV_NotObjectType;
Chris Lattner04421082008-04-08 04:40:51 +0000624 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000625 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000626 case CXXConditionDeclExprClass:
627 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000628 case CStyleCastExprClass:
Douglas Gregor9d293df2008-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 Redlc42e1182008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner28be73f2008-07-26 21:30:36 +0000655Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
656 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000657
658 switch (lvalResult) {
Douglas Gregorae8d4672008-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 case LV_NotObjectType: return MLV_NotObjectType;
667 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000668 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-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 Gregor86f19402008-12-20 23:49:58 +0000678 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000680
681 QualType CT = Ctx.getCanonicalType(getType());
682
683 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000685 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000687 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 return MLV_IncompleteType;
689
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000690 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 if (r->hasConstFields())
692 return MLV_ConstQualified;
693 }
Steve Naroff4f6a7d72008-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 Jahaniand1fa6442009-01-12 19:55:42 +0000703
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000704 // Assigning to an 'implicit' property?
Fariborz Jahanian6669db92008-11-25 17:56:43 +0000705 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000706 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
707 if (KVCExpr->getSetterMethod() == 0)
708 return MLV_NoSetterProperty;
709 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 return MLV_Valid;
711}
712
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000713/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000714/// duration. This means that the address of this expression is a link-time
715/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000716bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000717 switch (getStmtClass()) {
718 default:
719 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000720 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000721 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000722 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000723 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000724 case CompoundLiteralExprClass:
725 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000726 case DeclRefExprClass:
727 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000728 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
729 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000730 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000731 if (isa<FunctionDecl>(D))
732 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000733 return false;
734 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000735 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000736 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000737 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000738 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000739 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000740 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000741 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000742 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000743 case CXXDefaultArgExprClass:
744 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000745 }
746}
747
Fariborz Jahanian44baa8a2009-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 Jahanian207c5212009-02-23 18:59:50 +0000756 case Expr::UnaryOperatorClass:
757 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-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 Kremenek4e99a5f2008-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 Lattner56f34942008-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 Lattner56f34942008-02-13 01:02:39 +0000794 else
795 return E;
796 }
797}
798
Chris Lattnerecdd8412009-03-13 17:28:01 +0000799/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
800/// value (including ptr->int casts of the same size). Strip off any
801/// ParenExpr or CastExprs, returning their operand.
802Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
803 Expr *E = this;
804 while (true) {
805 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
806 E = P->getSubExpr();
807 continue;
808 }
809
810 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
811 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
812 // ptr<->int casts of the same width. We also ignore all identify casts.
813 Expr *SE = P->getSubExpr();
814
815 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
816 E = SE;
817 continue;
818 }
819
820 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
821 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
822 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
823 E = SE;
824 continue;
825 }
826 }
827
828 return E;
829 }
830}
831
832
Douglas Gregor898574e2008-12-05 23:32:09 +0000833/// hasAnyTypeDependentArguments - Determines if any of the expressions
834/// in Exprs is type-dependent.
835bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
836 for (unsigned I = 0; I < NumExprs; ++I)
837 if (Exprs[I]->isTypeDependent())
838 return true;
839
840 return false;
841}
842
843/// hasAnyValueDependentArguments - Determines if any of the expressions
844/// in Exprs is value-dependent.
845bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
846 for (unsigned I = 0; I < NumExprs; ++I)
847 if (Exprs[I]->isValueDependent())
848 return true;
849
850 return false;
851}
852
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000853bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000854 // This function is attempting whether an expression is an initializer
855 // which can be evaluated at compile-time. isEvaluatable handles most
856 // of the cases, but it can't deal with some initializer-specific
857 // expressions, and it can't deal with aggregates; we deal with those here,
858 // and fall back to isEvaluatable for the other cases.
859
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000860 // FIXME: This function assumes the variable being assigned to
861 // isn't a reference type!
862
Anders Carlssone8a32b82008-11-24 05:23:59 +0000863 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000864 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000865 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000866 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +0000867 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +0000868 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000869 // This handles gcc's extension that allows global initializers like
870 // "struct x {int x;} x = (struct x) {};".
871 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +0000872 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000873 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +0000874 }
Anders Carlssone8a32b82008-11-24 05:23:59 +0000875 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000876 // FIXME: This doesn't deal with fields with reference types correctly.
877 // FIXME: This incorrectly allows pointers cast to integers to be assigned
878 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +0000879 const InitListExpr *Exp = cast<InitListExpr>(this);
880 unsigned numInits = Exp->getNumInits();
881 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000882 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +0000883 return false;
884 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000885 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000886 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000887 case ImplicitValueInitExprClass:
888 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000889 case ParenExprClass: {
890 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
891 }
892 case UnaryOperatorClass: {
893 const UnaryOperator* Exp = cast<UnaryOperator>(this);
894 if (Exp->getOpcode() == UnaryOperator::Extension)
895 return Exp->getSubExpr()->isConstantInitializer(Ctx);
896 break;
897 }
898 case CStyleCastExprClass:
899 // Handle casts with a destination that's a struct or union; this
900 // deals with both the gcc no-op struct cast extension and the
901 // cast-to-union extension.
902 if (getType()->isRecordType())
903 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
904 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000905 }
906
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000907 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +0000908}
909
Reid Spencer5f016e22007-07-11 17:01:13 +0000910/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +0000911/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +0000912
913/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
914/// comma, etc
915///
Chris Lattnerce0afc02007-07-18 05:21:20 +0000916/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
917/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
918/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +0000919
Eli Friedmane28d7192009-02-26 09:29:13 +0000920// CheckICE - This function does the fundamental ICE checking: the returned
921// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
922// Note that to reduce code duplication, this helper does no evaluation
923// itself; the caller checks whether the expression is evaluatable, and
924// in the rare cases where CheckICE actually cares about the evaluated
925// value, it calls into Evalute.
926//
927// Meanings of Val:
928// 0: This expression is an ICE if it can be evaluated by Evaluate.
929// 1: This expression is not an ICE, but if it isn't evaluated, it's
930// a legal subexpression for an ICE. This return value is used to handle
931// the comma operator in C99 mode.
932// 2: This expression is not an ICE, and is not a legal subexpression for one.
933
934struct ICEDiag {
935 unsigned Val;
936 SourceLocation Loc;
937
938 public:
939 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
940 ICEDiag() : Val(0) {}
941};
942
943ICEDiag NoDiag() { return ICEDiag(); }
944
Eli Friedman60ce9632009-02-27 04:07:58 +0000945static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
946 Expr::EvalResult EVResult;
947 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
948 !EVResult.Val.isInt()) {
949 return ICEDiag(2, E->getLocStart());
950 }
951 return NoDiag();
952}
953
Eli Friedmane28d7192009-02-26 09:29:13 +0000954static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
955 if (!E->getType()->isIntegralType()) {
956 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +0000957 }
Eli Friedmane28d7192009-02-26 09:29:13 +0000958
959 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 default:
Eli Friedmane28d7192009-02-26 09:29:13 +0000961 return ICEDiag(2, E->getLocStart());
962 case Expr::ParenExprClass:
963 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
964 case Expr::IntegerLiteralClass:
965 case Expr::CharacterLiteralClass:
966 case Expr::CXXBoolLiteralExprClass:
967 case Expr::CXXZeroInitValueExprClass:
968 case Expr::TypesCompatibleExprClass:
969 case Expr::UnaryTypeTraitExprClass:
970 return NoDiag();
971 case Expr::CallExprClass:
972 case Expr::CXXOperatorCallExprClass: {
973 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +0000974 if (CE->isBuiltinCall(Ctx))
975 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +0000976 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +0000977 }
Eli Friedmane28d7192009-02-26 09:29:13 +0000978 case Expr::DeclRefExprClass:
979 case Expr::QualifiedDeclRefExprClass:
980 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
981 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +0000982 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedmane28d7192009-02-26 09:29:13 +0000983 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +0000984 // C++ 7.1.5.1p2
985 // A variable of non-volatile const-qualified integral or enumeration
986 // type initialized by an ICE can be used in ICEs.
987 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +0000988 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +0000989 if (const Expr *Init = Dcl->getInit())
Eli Friedmane28d7192009-02-26 09:29:13 +0000990 return CheckICE(Init, Ctx);
Sebastian Redl4a4251b2009-02-07 13:06:23 +0000991 }
992 }
Eli Friedmane28d7192009-02-26 09:29:13 +0000993 return ICEDiag(2, E->getLocStart());
994 case Expr::UnaryOperatorClass: {
995 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 switch (Exp->getOpcode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 default:
Eli Friedmane28d7192009-02-26 09:29:13 +0000998 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001000 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001004 case UnaryOperator::Real:
1005 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001006 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001007 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001008 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1009 // Evaluate matches the proposed gcc behavior for cases like
1010 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1011 // compliance: we should warn earlier for offsetof expressions with
1012 // array subscripts that aren't ICEs, and if the array subscripts
1013 // are ICEs, the value of the offsetof must be an integer constant.
1014 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001017 case Expr::SizeOfAlignOfExprClass: {
1018 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1019 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1020 return ICEDiag(2, E->getLocStart());
1021 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001023 case Expr::BinaryOperatorClass: {
1024 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 switch (Exp->getOpcode()) {
1026 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001027 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001028 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001030 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001031 case BinaryOperator::Add:
1032 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001035 case BinaryOperator::LT:
1036 case BinaryOperator::GT:
1037 case BinaryOperator::LE:
1038 case BinaryOperator::GE:
1039 case BinaryOperator::EQ:
1040 case BinaryOperator::NE:
1041 case BinaryOperator::And:
1042 case BinaryOperator::Xor:
1043 case BinaryOperator::Or:
1044 case BinaryOperator::Comma: {
1045 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1046 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001047 if (Exp->getOpcode() == BinaryOperator::Div ||
1048 Exp->getOpcode() == BinaryOperator::Rem) {
1049 // Evaluate gives an error for undefined Div/Rem, so make sure
1050 // we don't evaluate one.
1051 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1052 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1053 if (REval == 0)
1054 return ICEDiag(1, E->getLocStart());
1055 if (REval.isSigned() && REval.isAllOnesValue()) {
1056 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1057 if (LEval.isMinSignedValue())
1058 return ICEDiag(1, E->getLocStart());
1059 }
1060 }
1061 }
1062 if (Exp->getOpcode() == BinaryOperator::Comma) {
1063 if (Ctx.getLangOptions().C99) {
1064 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1065 // if it isn't evaluated.
1066 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1067 return ICEDiag(1, E->getLocStart());
1068 } else {
1069 // In both C89 and C++, commas in ICEs are illegal.
1070 return ICEDiag(2, E->getLocStart());
1071 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001072 }
1073 if (LHSResult.Val >= RHSResult.Val)
1074 return LHSResult;
1075 return RHSResult;
1076 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001078 case BinaryOperator::LOr: {
1079 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1080 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1081 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1082 // Rare case where the RHS has a comma "side-effect"; we need
1083 // to actually check the condition to see whether the side
1084 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001085 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001086 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001087 return RHSResult;
1088 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001089 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001090
Eli Friedmane28d7192009-02-26 09:29:13 +00001091 if (LHSResult.Val >= RHSResult.Val)
1092 return LHSResult;
1093 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001094 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001095 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001097 case Expr::ImplicitCastExprClass:
1098 case Expr::CStyleCastExprClass:
1099 case Expr::CXXFunctionalCastExprClass: {
1100 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1101 if (SubExpr->getType()->isIntegralType())
1102 return CheckICE(SubExpr, Ctx);
1103 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1104 return NoDiag();
1105 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001107 case Expr::ConditionalOperatorClass: {
1108 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner28daa532008-12-12 06:55:44 +00001109 // If the condition (ignoring parens) is a __builtin_constant_p call,
1110 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001111 // expression, and it is fully evaluated. This is an important GNU
1112 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001113 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001114 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001115 Expr::EvalResult EVResult;
1116 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1117 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001118 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001119 }
1120 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001121 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001122 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1123 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1124 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1125 if (CondResult.Val == 2)
1126 return CondResult;
1127 if (TrueResult.Val == 2)
1128 return TrueResult;
1129 if (FalseResult.Val == 2)
1130 return FalseResult;
1131 if (CondResult.Val == 1)
1132 return CondResult;
1133 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1134 return NoDiag();
1135 // Rare case where the diagnostics depend on which side is evaluated
1136 // Note that if we get here, CondResult is 0, and at least one of
1137 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001138 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001139 return FalseResult;
1140 }
1141 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001143 case Expr::CXXDefaultArgExprClass:
1144 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001145 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001146 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001147 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001149}
Reid Spencer5f016e22007-07-11 17:01:13 +00001150
Eli Friedmane28d7192009-02-26 09:29:13 +00001151bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1152 SourceLocation *Loc, bool isEvaluated) const {
1153 ICEDiag d = CheckICE(this, Ctx);
1154 if (d.Val != 0) {
1155 if (Loc) *Loc = d.Loc;
1156 return false;
1157 }
1158 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001159 if (!Evaluate(EvalResult, Ctx))
1160 assert(0 && "ICE cannot be evaluated!");
1161 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1162 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001163 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 return true;
1165}
1166
Reid Spencer5f016e22007-07-11 17:01:13 +00001167/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1168/// integer constant expression with the value zero, or if this is one that is
1169/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001170bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1171{
Sebastian Redl07779722008-10-31 14:43:28 +00001172 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001173 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001174 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001175 // Check that it is a cast to void*.
1176 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1177 QualType Pointee = PT->getPointeeType();
1178 if (Pointee.getCVRQualifiers() == 0 &&
1179 Pointee->isVoidType() && // to void*
1180 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001181 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001182 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001184 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1185 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001186 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001187 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1188 // Accept ((void*)0) as a null pointer constant, as many other
1189 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001190 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001191 } else if (const CXXDefaultArgExpr *DefaultArg
1192 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001193 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001194 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001195 } else if (isa<GNUNullExpr>(this)) {
1196 // The GNU __null extension is always a null pointer constant.
1197 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001198 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001199
Steve Naroffaa58f002008-01-14 16:10:57 +00001200 // This expression must be an integer type.
1201 if (!getType()->isIntegerType())
1202 return false;
1203
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 // If we have an integer constant expression, we need to *evaluate* it and
1205 // test for the value 0.
Anders Carlssond2652772008-12-01 06:28:23 +00001206 // FIXME: We should probably return false if we're compiling in strict mode
1207 // and Diag is not null (this indicates that the value was foldable but not
1208 // an ICE.
1209 EvalResult Result;
Anders Carlssonefa9b382008-12-01 02:13:57 +00001210 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssond2652772008-12-01 06:28:23 +00001211 Result.Val.isInt() && Result.Val.getInt() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001212}
Steve Naroff31a45842007-07-28 23:10:27 +00001213
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001214/// isBitField - Return true if this expression is a bit-field.
1215bool Expr::isBitField() {
1216 Expr *E = this->IgnoreParenCasts();
1217 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001218 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1219 return Field->isBitField();
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001220 return false;
1221}
1222
Chris Lattner2140e902009-02-16 22:14:05 +00001223/// isArrow - Return true if the base expression is a pointer to vector,
1224/// return false if the base expression is a vector.
1225bool ExtVectorElementExpr::isArrow() const {
1226 return getBase()->getType()->isPointerType();
1227}
1228
Nate Begeman213541a2008-04-18 23:10:10 +00001229unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001230 if (const VectorType *VT = getType()->getAsVectorType())
1231 return VT->getNumElements();
1232 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001233}
1234
Nate Begeman8a997642008-05-09 06:41:27 +00001235/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001236bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001237 const char *compStr = Accessor.getName();
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001238 unsigned length = Accessor.getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001239
1240 // Halving swizzles do not contain duplicate elements.
1241 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1242 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1243 return false;
1244
1245 // Advance past s-char prefix on hex swizzles.
1246 if (*compStr == 's') {
1247 compStr++;
1248 length--;
1249 }
Steve Narofffec0b492007-07-30 03:29:09 +00001250
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001251 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001252 const char *s = compStr+i;
1253 for (const char c = *s++; *s; s++)
1254 if (c == *s)
1255 return true;
1256 }
1257 return false;
1258}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001259
Nate Begeman8a997642008-05-09 06:41:27 +00001260/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001261void ExtVectorElementExpr::getEncodedElementAccess(
1262 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001263 const char *compStr = Accessor.getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001264 if (*compStr == 's')
1265 compStr++;
1266
1267 bool isHi = !strcmp(compStr, "hi");
1268 bool isLo = !strcmp(compStr, "lo");
1269 bool isEven = !strcmp(compStr, "even");
1270 bool isOdd = !strcmp(compStr, "odd");
1271
Nate Begeman8a997642008-05-09 06:41:27 +00001272 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1273 uint64_t Index;
1274
1275 if (isHi)
1276 Index = e + i;
1277 else if (isLo)
1278 Index = i;
1279 else if (isEven)
1280 Index = 2 * i;
1281 else if (isOdd)
1282 Index = 2 * i + 1;
1283 else
1284 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001285
Nate Begeman3b8d1162008-05-13 21:03:02 +00001286 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001287 }
Nate Begeman8a997642008-05-09 06:41:27 +00001288}
1289
Steve Naroff68d331a2007-09-27 14:38:14 +00001290// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001291ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001292 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001293 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001294 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001295 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001296 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001297 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001298 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001299 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001300 if (NumArgs) {
1301 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001302 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1303 }
Steve Naroff563477d2007-09-18 23:55:05 +00001304 LBracloc = LBrac;
1305 RBracloc = RBrac;
1306}
1307
Steve Naroff68d331a2007-09-27 14:38:14 +00001308// constructor for class messages.
1309// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001310ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001311 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001312 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001313 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001314 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001315 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001316 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001317 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001318 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001319 if (NumArgs) {
1320 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001321 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1322 }
Steve Naroff563477d2007-09-18 23:55:05 +00001323 LBracloc = LBrac;
1324 RBracloc = RBrac;
1325}
1326
Ted Kremenek4df728e2008-06-24 15:50:53 +00001327// constructor for class messages.
1328ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1329 QualType retType, ObjCMethodDecl *mproto,
1330 SourceLocation LBrac, SourceLocation RBrac,
1331 Expr **ArgExprs, unsigned nargs)
1332: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1333MethodProto(mproto) {
1334 NumArgs = nargs;
1335 SubExprs = new Stmt*[NumArgs+1];
1336 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1337 if (NumArgs) {
1338 for (unsigned i = 0; i != NumArgs; ++i)
1339 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1340 }
1341 LBracloc = LBrac;
1342 RBracloc = RBrac;
1343}
1344
1345ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1346 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1347 switch (x & Flags) {
1348 default:
1349 assert(false && "Invalid ObjCMessageExpr.");
1350 case IsInstMeth:
1351 return ClassInfo(0, 0);
1352 case IsClsMethDeclUnknown:
1353 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1354 case IsClsMethDeclKnown: {
1355 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1356 return ClassInfo(D, D->getIdentifier());
1357 }
1358 }
1359}
1360
Chris Lattner27437ca2007-10-25 00:29:32 +00001361bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001362 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001363}
1364
Sebastian Redl05189992008-11-11 17:56:53 +00001365void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1366 // Override default behavior of traversing children. If this has a type
1367 // operand and the type is a variable-length array, the child iteration
1368 // will iterate over the size expression. However, this expression belongs
1369 // to the type, not to this, so we don't want to delete it.
1370 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001371 if (isArgumentType()) {
1372 this->~SizeOfAlignOfExpr();
1373 C.Deallocate(this);
1374 }
Sebastian Redl05189992008-11-11 17:56:53 +00001375 else
1376 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001377}
1378
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001379//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001380// DesignatedInitExpr
1381//===----------------------------------------------------------------------===//
1382
1383IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1384 assert(Kind == FieldDesignator && "Only valid on a field designator");
1385 if (Field.NameOrField & 0x01)
1386 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1387 else
1388 return getField()->getIdentifier();
1389}
1390
1391DesignatedInitExpr *
1392DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1393 unsigned NumDesignators,
1394 Expr **IndexExprs, unsigned NumIndexExprs,
1395 SourceLocation ColonOrEqualLoc,
1396 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001397 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1398 sizeof(Designator) * NumDesignators +
1399 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001400 DesignatedInitExpr *DIE
1401 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1402 ColonOrEqualLoc, UsesColonSyntax,
1403 NumIndexExprs + 1);
1404
1405 // Fill in the designators
1406 unsigned ExpectedNumSubExprs = 0;
1407 designators_iterator Desig = DIE->designators_begin();
1408 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1409 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1410 if (Designators[Idx].isArrayDesignator())
1411 ++ExpectedNumSubExprs;
1412 else if (Designators[Idx].isArrayRangeDesignator())
1413 ExpectedNumSubExprs += 2;
1414 }
1415 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1416
1417 // Fill in the subexpressions, including the initializer expression.
1418 child_iterator Child = DIE->child_begin();
1419 *Child++ = Init;
1420 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1421 *Child = IndexExprs[Idx];
1422
1423 return DIE;
1424}
1425
1426SourceRange DesignatedInitExpr::getSourceRange() const {
1427 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001428 Designator &First =
1429 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001430 if (First.isFieldDesignator()) {
1431 if (UsesColonSyntax)
1432 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1433 else
1434 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1435 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001436 StartLoc =
1437 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001438 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1439}
1440
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001441DesignatedInitExpr::designators_iterator
1442DesignatedInitExpr::designators_begin() {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001443 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1444 Ptr += sizeof(DesignatedInitExpr);
1445 return static_cast<Designator*>(static_cast<void*>(Ptr));
1446}
1447
1448DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1449 return designators_begin() + NumDesignators;
1450}
1451
1452Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1453 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1454 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1455 Ptr += sizeof(DesignatedInitExpr);
1456 Ptr += sizeof(Designator) * NumDesignators;
1457 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1458 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1459}
1460
1461Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1462 assert(D.Kind == Designator::ArrayRangeDesignator &&
1463 "Requires array range designator");
1464 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1465 Ptr += sizeof(DesignatedInitExpr);
1466 Ptr += sizeof(Designator) * NumDesignators;
1467 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1468 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1469}
1470
1471Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1472 assert(D.Kind == Designator::ArrayRangeDesignator &&
1473 "Requires array range designator");
1474 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1475 Ptr += sizeof(DesignatedInitExpr);
1476 Ptr += sizeof(Designator) * NumDesignators;
1477 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1478 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1479}
1480
1481//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001482// ExprIterator.
1483//===----------------------------------------------------------------------===//
1484
1485Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1486Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1487Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1488const Expr* ConstExprIterator::operator[](size_t idx) const {
1489 return cast<Expr>(I[idx]);
1490}
1491const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1492const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1493
1494//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001495// Child Iterators for iterating over subexpressions/substatements
1496//===----------------------------------------------------------------------===//
1497
1498// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001499Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1500Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001501
Steve Naroff7779db42007-11-12 14:29:37 +00001502// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001503Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1504Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001505
Steve Naroffe3e9add2008-06-02 23:03:37 +00001506// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001507Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1508Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001509
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001510// ObjCKVCRefExpr
1511Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1512Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1513
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001514// ObjCSuperExpr
1515Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1516Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1517
Chris Lattnerd9f69102008-08-10 01:53:14 +00001518// PredefinedExpr
1519Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1520Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001521
1522// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001523Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1524Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001525
1526// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001527Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001528Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001529
1530// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001531Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1532Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001533
Chris Lattner5d661452007-08-26 03:42:43 +00001534// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001535Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1536Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001537
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001538// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001539Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1540Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001541
1542// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001543Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1544Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001545
1546// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001547Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1548Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001549
Sebastian Redl05189992008-11-11 17:56:53 +00001550// SizeOfAlignOfExpr
1551Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1552 // If this is of a type and the type is a VLA type (and not a typedef), the
1553 // size expression of the VLA needs to be treated as an executable expression.
1554 // Why isn't this weirdness documented better in StmtIterator?
1555 if (isArgumentType()) {
1556 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1557 getArgumentType().getTypePtr()))
1558 return child_iterator(T);
1559 return child_iterator();
1560 }
Sebastian Redld4575892008-12-03 23:17:54 +00001561 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001562}
Sebastian Redl05189992008-11-11 17:56:53 +00001563Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1564 if (isArgumentType())
1565 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001566 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001567}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001568
1569// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001570Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001571 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001572}
Ted Kremenek1237c672007-08-24 20:06:47 +00001573Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001574 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001575}
1576
1577// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001578Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001579 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001580}
Ted Kremenek1237c672007-08-24 20:06:47 +00001581Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001582 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001583}
Ted Kremenek1237c672007-08-24 20:06:47 +00001584
1585// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001586Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1587Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001588
Nate Begeman213541a2008-04-18 23:10:10 +00001589// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001590Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1591Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001592
1593// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001594Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1595Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001596
Ted Kremenek1237c672007-08-24 20:06:47 +00001597// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001598Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1599Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001600
1601// BinaryOperator
1602Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001603 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001604}
Ted Kremenek1237c672007-08-24 20:06:47 +00001605Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001606 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001607}
1608
1609// ConditionalOperator
1610Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001611 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001612}
Ted Kremenek1237c672007-08-24 20:06:47 +00001613Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001614 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001615}
1616
1617// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001618Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1619Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001620
Ted Kremenek1237c672007-08-24 20:06:47 +00001621// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001622Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1623Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001624
1625// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001626Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1627 return child_iterator();
1628}
1629
1630Stmt::child_iterator TypesCompatibleExpr::child_end() {
1631 return child_iterator();
1632}
Ted Kremenek1237c672007-08-24 20:06:47 +00001633
1634// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001635Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1636Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001637
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001638// GNUNullExpr
1639Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1640Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1641
Eli Friedmand38617c2008-05-14 19:38:39 +00001642// ShuffleVectorExpr
1643Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001644 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001645}
1646Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001647 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001648}
1649
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001650// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001651Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1652Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001653
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001654// InitListExpr
1655Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001656 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001657}
1658Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001659 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001660}
1661
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001662// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001663Stmt::child_iterator DesignatedInitExpr::child_begin() {
1664 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1665 Ptr += sizeof(DesignatedInitExpr);
1666 Ptr += sizeof(Designator) * NumDesignators;
1667 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1668}
1669Stmt::child_iterator DesignatedInitExpr::child_end() {
1670 return child_iterator(&*child_begin() + NumSubExprs);
1671}
1672
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001673// ImplicitValueInitExpr
1674Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1675 return child_iterator();
1676}
1677
1678Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1679 return child_iterator();
1680}
1681
Ted Kremenek1237c672007-08-24 20:06:47 +00001682// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001683Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001684 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001685}
1686Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001687 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001688}
Ted Kremenek1237c672007-08-24 20:06:47 +00001689
1690// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001691Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1692Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001693
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001694// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001695Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1696 return child_iterator();
1697}
1698Stmt::child_iterator ObjCSelectorExpr::child_end() {
1699 return child_iterator();
1700}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001701
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001702// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001703Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1704 return child_iterator();
1705}
1706Stmt::child_iterator ObjCProtocolExpr::child_end() {
1707 return child_iterator();
1708}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001709
Steve Naroff563477d2007-09-18 23:55:05 +00001710// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00001711Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001712 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00001713}
1714Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001715 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00001716}
1717
Steve Naroff4eb206b2008-09-03 18:15:37 +00001718// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00001719Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1720Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00001721
Ted Kremenek9da13f92008-09-26 23:24:14 +00001722Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1723Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }