blob: 99e07c4b8ab11a993808a2926b78a6652ab53cc9 [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"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000023#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Primary Expressions.
28//===----------------------------------------------------------------------===//
29
Anders Carlssona135fb42009-03-15 18:34:13 +000030IntegerLiteral* IntegerLiteral::Clone(ASTContext &C) const {
31 return new (C) IntegerLiteral(Value, getType(), Loc);
32}
33
Chris Lattnerda8249e2008-06-07 22:13:43 +000034/// getValueAsApproximateDouble - This returns the value as an inaccurate
35/// double. Note that this may cause loss of precision, but is useful for
36/// debugging dumps, etc.
37double FloatingLiteral::getValueAsApproximateDouble() const {
38 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000039 bool ignored;
40 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
41 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000042 return V.convertToDouble();
43}
44
Chris Lattner2085fd62009-02-18 06:40:38 +000045StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
46 unsigned ByteLength, bool Wide,
47 QualType Ty,
Anders Carlssona135fb42009-03-15 18:34:13 +000048 const SourceLocation *Loc,
49 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +000050 // Allocate enough space for the StringLiteral plus an array of locations for
51 // any concatenated string tokens.
52 void *Mem = C.Allocate(sizeof(StringLiteral)+
53 sizeof(SourceLocation)*(NumStrs-1),
54 llvm::alignof<StringLiteral>());
55 StringLiteral *SL = new (Mem) StringLiteral(Ty);
56
Reid Spencer5f016e22007-07-11 17:01:13 +000057 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +000058 char *AStrData = new (C, 1) char[ByteLength];
59 memcpy(AStrData, StrData, ByteLength);
60 SL->StrData = AStrData;
61 SL->ByteLength = ByteLength;
62 SL->IsWide = Wide;
63 SL->TokLocs[0] = Loc[0];
64 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +000065
Chris Lattner726e1682009-02-18 05:49:11 +000066 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +000067 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
68 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +000069}
70
Douglas Gregor673ecd62009-04-15 16:35:07 +000071StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
72 void *Mem = C.Allocate(sizeof(StringLiteral)+
73 sizeof(SourceLocation)*(NumStrs-1),
74 llvm::alignof<StringLiteral>());
75 StringLiteral *SL = new (Mem) StringLiteral(QualType());
76 SL->StrData = 0;
77 SL->ByteLength = 0;
78 SL->NumConcatenated = NumStrs;
79 return SL;
80}
81
Anders Carlssona135fb42009-03-15 18:34:13 +000082StringLiteral* StringLiteral::Clone(ASTContext &C) const {
83 return Create(C, StrData, ByteLength, IsWide, getType(),
84 TokLocs, NumConcatenated);
85}
Chris Lattner726e1682009-02-18 05:49:11 +000086
Ted Kremenek6e94ef52009-02-06 19:55:15 +000087void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000088 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +000089 this->~StringLiteral();
90 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +000091}
92
Douglas Gregor673ecd62009-04-15 16:35:07 +000093void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
94 if (StrData)
95 C.Deallocate(const_cast<char*>(StrData));
96
97 char *AStrData = new (C, 1) char[Len];
98 memcpy(AStrData, Str, Len);
99 StrData = AStrData;
100 ByteLength = Len;
101}
102
Reid Spencer5f016e22007-07-11 17:01:13 +0000103/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
104/// corresponds to, e.g. "sizeof" or "[pre]++".
105const char *UnaryOperator::getOpcodeStr(Opcode Op) {
106 switch (Op) {
107 default: assert(0 && "Unknown unary operator");
108 case PostInc: return "++";
109 case PostDec: return "--";
110 case PreInc: return "++";
111 case PreDec: return "--";
112 case AddrOf: return "&";
113 case Deref: return "*";
114 case Plus: return "+";
115 case Minus: return "-";
116 case Not: return "~";
117 case LNot: return "!";
118 case Real: return "__real";
119 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000121 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
123}
124
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000125UnaryOperator::Opcode
126UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
127 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000128 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000129 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
130 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
131 case OO_Amp: return AddrOf;
132 case OO_Star: return Deref;
133 case OO_Plus: return Plus;
134 case OO_Minus: return Minus;
135 case OO_Tilde: return Not;
136 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000137 }
138}
139
140OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
141 switch (Opc) {
142 case PostInc: case PreInc: return OO_PlusPlus;
143 case PostDec: case PreDec: return OO_MinusMinus;
144 case AddrOf: return OO_Amp;
145 case Deref: return OO_Star;
146 case Plus: return OO_Plus;
147 case Minus: return OO_Minus;
148 case Not: return OO_Tilde;
149 case LNot: return OO_Exclaim;
150 default: return OO_None;
151 }
152}
153
154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155//===----------------------------------------------------------------------===//
156// Postfix Operators.
157//===----------------------------------------------------------------------===//
158
Ted Kremenek668bf912009-02-09 20:51:47 +0000159CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000160 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000161 : Expr(SC, t,
162 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000163 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000164 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000165
166 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000167 SubExprs[FN] = fn;
168 for (unsigned i = 0; i != numargs; ++i)
169 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000170
Douglas Gregorb4609802008-11-14 16:09:21 +0000171 RParenLoc = rparenloc;
172}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000173
Ted Kremenek668bf912009-02-09 20:51:47 +0000174CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
175 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000176 : Expr(CallExprClass, t,
177 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000178 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000179 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000180
181 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000182 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000184 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000185
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 RParenLoc = rparenloc;
187}
188
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000189CallExpr::CallExpr(ASTContext &C, EmptyShell Empty)
190 : Expr(CallExprClass, Empty), SubExprs(0), NumArgs(0) {
191 SubExprs = new (C) Stmt*[1];
192}
193
Ted Kremenek668bf912009-02-09 20:51:47 +0000194void CallExpr::Destroy(ASTContext& C) {
195 DestroyChildren(C);
196 if (SubExprs) C.Deallocate(SubExprs);
197 this->~CallExpr();
198 C.Deallocate(this);
199}
200
Chris Lattnerd18b3292007-12-28 05:25:02 +0000201/// setNumArgs - This changes the number of arguments present in this call.
202/// Any orphaned expressions are deleted by this, and any new operands are set
203/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000204void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000205 // No change, just return.
206 if (NumArgs == getNumArgs()) return;
207
208 // If shrinking # arguments, just delete the extras and forgot them.
209 if (NumArgs < getNumArgs()) {
210 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000211 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000212 this->NumArgs = NumArgs;
213 return;
214 }
215
216 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek55499762008-06-17 02:43:46 +0000217 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000218 // Copy over args.
219 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
220 NewSubExprs[i] = SubExprs[i];
221 // Null out new args.
222 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
223 NewSubExprs[i] = 0;
224
Ted Kremenek8189cde2009-02-07 01:47:29 +0000225 delete [] SubExprs;
Chris Lattnerd18b3292007-12-28 05:25:02 +0000226 SubExprs = NewSubExprs;
227 this->NumArgs = NumArgs;
228}
229
Chris Lattnercb888962008-10-06 05:00:53 +0000230/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
231/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000232unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000233 // All simple function calls (e.g. func()) are implicitly cast to pointer to
234 // function. As a result, we try and obtain the DeclRefExpr from the
235 // ImplicitCastExpr.
236 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
237 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000238 return 0;
239
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000240 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
241 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000242 return 0;
243
Anders Carlssonbcba2012008-01-31 02:13:57 +0000244 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
245 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000246 return 0;
247
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000248 if (!FDecl->getIdentifier())
249 return 0;
250
Douglas Gregor3c385e52009-02-14 18:57:46 +0000251 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000252}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000253
Chris Lattnercb888962008-10-06 05:00:53 +0000254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
256/// corresponds to, e.g. "<<=".
257const char *BinaryOperator::getOpcodeStr(Opcode Op) {
258 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000259 case PtrMemD: return ".*";
260 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 case Mul: return "*";
262 case Div: return "/";
263 case Rem: return "%";
264 case Add: return "+";
265 case Sub: return "-";
266 case Shl: return "<<";
267 case Shr: return ">>";
268 case LT: return "<";
269 case GT: return ">";
270 case LE: return "<=";
271 case GE: return ">=";
272 case EQ: return "==";
273 case NE: return "!=";
274 case And: return "&";
275 case Xor: return "^";
276 case Or: return "|";
277 case LAnd: return "&&";
278 case LOr: return "||";
279 case Assign: return "=";
280 case MulAssign: return "*=";
281 case DivAssign: return "/=";
282 case RemAssign: return "%=";
283 case AddAssign: return "+=";
284 case SubAssign: return "-=";
285 case ShlAssign: return "<<=";
286 case ShrAssign: return ">>=";
287 case AndAssign: return "&=";
288 case XorAssign: return "^=";
289 case OrAssign: return "|=";
290 case Comma: return ",";
291 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000292
293 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000294}
295
Douglas Gregor063daf62009-03-13 18:40:31 +0000296BinaryOperator::Opcode
297BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
298 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000299 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000300 case OO_Plus: return Add;
301 case OO_Minus: return Sub;
302 case OO_Star: return Mul;
303 case OO_Slash: return Div;
304 case OO_Percent: return Rem;
305 case OO_Caret: return Xor;
306 case OO_Amp: return And;
307 case OO_Pipe: return Or;
308 case OO_Equal: return Assign;
309 case OO_Less: return LT;
310 case OO_Greater: return GT;
311 case OO_PlusEqual: return AddAssign;
312 case OO_MinusEqual: return SubAssign;
313 case OO_StarEqual: return MulAssign;
314 case OO_SlashEqual: return DivAssign;
315 case OO_PercentEqual: return RemAssign;
316 case OO_CaretEqual: return XorAssign;
317 case OO_AmpEqual: return AndAssign;
318 case OO_PipeEqual: return OrAssign;
319 case OO_LessLess: return Shl;
320 case OO_GreaterGreater: return Shr;
321 case OO_LessLessEqual: return ShlAssign;
322 case OO_GreaterGreaterEqual: return ShrAssign;
323 case OO_EqualEqual: return EQ;
324 case OO_ExclaimEqual: return NE;
325 case OO_LessEqual: return LE;
326 case OO_GreaterEqual: return GE;
327 case OO_AmpAmp: return LAnd;
328 case OO_PipePipe: return LOr;
329 case OO_Comma: return Comma;
330 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000331 }
332}
333
334OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
335 static const OverloadedOperatorKind OverOps[] = {
336 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
337 OO_Star, OO_Slash, OO_Percent,
338 OO_Plus, OO_Minus,
339 OO_LessLess, OO_GreaterGreater,
340 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
341 OO_EqualEqual, OO_ExclaimEqual,
342 OO_Amp,
343 OO_Caret,
344 OO_Pipe,
345 OO_AmpAmp,
346 OO_PipePipe,
347 OO_Equal, OO_StarEqual,
348 OO_SlashEqual, OO_PercentEqual,
349 OO_PlusEqual, OO_MinusEqual,
350 OO_LessLessEqual, OO_GreaterGreaterEqual,
351 OO_AmpEqual, OO_CaretEqual,
352 OO_PipeEqual,
353 OO_Comma
354 };
355 return OverOps[Opc];
356}
357
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000358InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000359 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000360 SourceLocation rbraceloc)
Steve Naroffc5ae8992008-05-01 02:04:18 +0000361 : Expr(InitListExprClass, QualType()),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000362 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000363 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000364
365 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000366}
Reid Spencer5f016e22007-07-11 17:01:13 +0000367
Douglas Gregorfa219202009-03-20 23:58:33 +0000368void InitListExpr::reserveInits(unsigned NumInits) {
369 if (NumInits > InitExprs.size())
370 InitExprs.reserve(NumInits);
371}
372
Douglas Gregor4c678342009-01-28 21:54:33 +0000373void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000374 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000375 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000376 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000377 InitExprs.resize(NumInits, 0);
378}
379
380Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
381 if (Init >= InitExprs.size()) {
382 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
383 InitExprs.back() = expr;
384 return 0;
385 }
386
387 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
388 InitExprs[Init] = expr;
389 return Result;
390}
391
Steve Naroffbfdcae62008-09-04 15:31:07 +0000392/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000393///
394const FunctionType *BlockExpr::getFunctionType() const {
395 return getType()->getAsBlockPointerType()->
396 getPointeeType()->getAsFunctionType();
397}
398
Steve Naroff56ee6892008-10-08 17:01:13 +0000399SourceLocation BlockExpr::getCaretLocation() const {
400 return TheBlock->getCaretLocation();
401}
402const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
403Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
404
405
Reid Spencer5f016e22007-07-11 17:01:13 +0000406//===----------------------------------------------------------------------===//
407// Generic Expression Routines
408//===----------------------------------------------------------------------===//
409
Chris Lattner026dc962009-02-14 07:37:35 +0000410/// isUnusedResultAWarning - Return true if this immediate expression should
411/// be warned about if the result is unused. If so, fill in Loc and Ranges
412/// with location to warn on and the source range[s] to report with the
413/// warning.
414bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
415 SourceRange &R2) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 switch (getStmtClass()) {
417 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000418 Loc = getExprLoc();
419 R1 = getSourceRange();
420 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000422 return cast<ParenExpr>(this)->getSubExpr()->
423 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 case UnaryOperatorClass: {
425 const UnaryOperator *UO = cast<UnaryOperator>(this);
426
427 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000428 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 case UnaryOperator::PostInc:
430 case UnaryOperator::PostDec:
431 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000432 case UnaryOperator::PreDec: // ++/--
433 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 case UnaryOperator::Deref:
435 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000436 if (getType().isVolatileQualified())
437 return false;
438 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 case UnaryOperator::Real:
440 case UnaryOperator::Imag:
441 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000442 if (UO->getSubExpr()->getType().isVolatileQualified())
443 return false;
444 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 case UnaryOperator::Extension:
Chris Lattner026dc962009-02-14 07:37:35 +0000446 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 }
Chris Lattner026dc962009-02-14 07:37:35 +0000448 Loc = UO->getOperatorLoc();
449 R1 = UO->getSubExpr()->getSourceRange();
450 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000452 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000453 const BinaryOperator *BO = cast<BinaryOperator>(this);
454 // Consider comma to have side effects if the LHS or RHS does.
455 if (BO->getOpcode() == BinaryOperator::Comma)
456 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
457 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnere7716e62007-12-01 06:07:34 +0000458
Chris Lattner026dc962009-02-14 07:37:35 +0000459 if (BO->isAssignmentOp())
460 return false;
461 Loc = BO->getOperatorLoc();
462 R1 = BO->getLHS()->getSourceRange();
463 R2 = BO->getRHS()->getSourceRange();
464 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000465 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000466 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000467 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000468
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000469 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000470 // The condition must be evaluated, but if either the LHS or RHS is a
471 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000472 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stumpbefbcf42009-02-27 03:16:57 +0000473 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000474 return true;
475 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000476 }
477
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000479 // If the base pointer or element is to a volatile pointer/field, accessing
480 // it is a side effect.
481 if (getType().isVolatileQualified())
482 return false;
483 Loc = cast<MemberExpr>(this)->getMemberLoc();
484 R1 = SourceRange(Loc, Loc);
485 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
486 return true;
487
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 case ArraySubscriptExprClass:
489 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000490 // it is a side effect.
491 if (getType().isVolatileQualified())
492 return false;
493 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
494 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
495 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
496 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000497
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 case CallExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000499 case CXXOperatorCallExprClass: {
500 // If this is a direct call, get the callee.
501 const CallExpr *CE = cast<CallExpr>(this);
502 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
503 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
504 // If the callee has attribute pure, const, or warn_unused_result, warn
505 // about it. void foo() { strlen("bar"); } should warn.
506 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
507 if (FD->getAttr<WarnUnusedResultAttr>() ||
508 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
509 Loc = CE->getCallee()->getLocStart();
510 R1 = CE->getCallee()->getSourceRange();
511
512 if (unsigned NumArgs = CE->getNumArgs())
513 R2 = SourceRange(CE->getArg(0)->getLocStart(),
514 CE->getArg(NumArgs-1)->getLocEnd());
515 return true;
516 }
517 }
518 return false;
519 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000520 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000521 return false;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000522 case StmtExprClass: {
523 // Statement exprs don't logically have side effects themselves, but are
524 // sometimes used in macros in ways that give them a type that is unused.
525 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
526 // however, if the result of the stmt expr is dead, we don't want to emit a
527 // warning.
528 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
529 if (!CS->body_empty())
530 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattner026dc962009-02-14 07:37:35 +0000531 return E->isUnusedResultAWarning(Loc, R1, R2);
532
533 Loc = cast<StmtExpr>(this)->getLParenLoc();
534 R1 = getSourceRange();
535 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000536 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000537 case CStyleCastExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000538 // If this is a cast to void, check the operand. Otherwise, the result of
539 // the cast is unused.
540 if (getType()->isVoidType())
541 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
542 R1, R2);
543 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
544 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
545 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000546 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 // If this is a cast to void, check the operand. Otherwise, the result of
548 // the cast is unused.
549 if (getType()->isVoidType())
Chris Lattner026dc962009-02-14 07:37:35 +0000550 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
551 R1, R2);
552 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
553 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
554 return true;
555
Eli Friedman4be1f472008-05-19 21:24:43 +0000556 case ImplicitCastExprClass:
557 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000558 return cast<ImplicitCastExpr>(this)
559 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000560
Chris Lattner04421082008-04-08 04:40:51 +0000561 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000562 return cast<CXXDefaultArgExpr>(this)
563 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000564
565 case CXXNewExprClass:
566 // FIXME: In theory, there might be new expressions that don't have side
567 // effects (e.g. a placement new with an uninitialized POD).
568 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000569 return false;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000570 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000571}
572
Douglas Gregorba7e2102008-10-22 15:04:37 +0000573/// DeclCanBeLvalue - Determine whether the given declaration can be
574/// an lvalue. This is a helper routine for isLvalue.
575static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000576 // C++ [temp.param]p6:
577 // A non-type non-reference template-parameter is not an lvalue.
578 if (const NonTypeTemplateParmDecl *NTTParm
579 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
580 return NTTParm->getType()->isReferenceType();
581
Douglas Gregor44b43212008-12-11 16:49:14 +0000582 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000583 // C++ 3.10p2: An lvalue refers to an object or function.
584 (Ctx.getLangOptions().CPlusPlus &&
585 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
586}
587
Reid Spencer5f016e22007-07-11 17:01:13 +0000588/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
589/// incomplete type other than void. Nonarray expressions that can be lvalues:
590/// - name, where name must be a variable
591/// - e[i]
592/// - (e), where e must be an lvalue
593/// - e.name, where e must be an lvalue
594/// - e->name
595/// - *e, the type of e cannot be a function type
596/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000597/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000598/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000599///
Chris Lattner28be73f2008-07-26 21:30:36 +0000600Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor98cd5992008-10-21 23:43:52 +0000601 // first, check the type (C99 6.3.2.1). Expressions with function
602 // type in C are not lvalues, but they can be lvalues in C++.
603 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 return LV_NotObjectType;
605
Steve Naroffacb818a2008-02-10 01:39:04 +0000606 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000607 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000608 return LV_IncompleteVoidType;
609
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000610 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
Bill Wendling08ad47c2007-07-17 03:52:31 +0000611
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 // the type looks fine, now check the expression
613 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000614 case StringLiteralClass: // C99 6.5.1p4
615 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000616 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
618 // For vectors, make sure base is an lvalue (i.e. not a function call).
619 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000620 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000622 case DeclRefExprClass:
623 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000624 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
625 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 return LV_Valid;
627 break;
Chris Lattner41110242008-06-17 18:05:57 +0000628 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000629 case BlockDeclRefExprClass: {
630 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000631 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000632 return LV_Valid;
633 break;
634 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000635 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000637 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
638 NamedDecl *Member = m->getMemberDecl();
639 // C++ [expr.ref]p4:
640 // If E2 is declared to have type "reference to T", then E1.E2
641 // is an lvalue.
642 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
643 if (Value->getType()->isReferenceType())
644 return LV_Valid;
645
646 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000647 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000648 return LV_Valid;
649
650 // -- If E2 is a non-static data member [...]. If E1 is an
651 // lvalue, then E1.E2 is an lvalue.
652 if (isa<FieldDecl>(Member))
653 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
654
655 // -- If it refers to a static member function [...], then
656 // E1.E2 is an lvalue.
657 // -- Otherwise, if E1.E2 refers to a non-static member
658 // function [...], then E1.E2 is not an lvalue.
659 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
660 return Method->isStatic()? LV_Valid : LV_MemberFunction;
661
662 // -- If E2 is a member enumerator [...], the expression E1.E2
663 // is not an lvalue.
664 if (isa<EnumConstantDecl>(Member))
665 return LV_InvalidExpression;
666
667 // Not an lvalue.
668 return LV_InvalidExpression;
669 }
670
671 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000672 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000673 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000674 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000676 return LV_Valid; // C99 6.5.3p4
677
678 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000679 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
680 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000681 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000682
683 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
684 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
685 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
686 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000688 case ImplicitCastExprClass:
689 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
690 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000692 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000693 case BinaryOperatorClass:
694 case CompoundAssignOperatorClass: {
695 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000696
697 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
698 BinOp->getOpcode() == BinaryOperator::Comma)
699 return BinOp->getRHS()->isLvalue(Ctx);
700
Sebastian Redl22460502009-02-07 00:15:38 +0000701 // C++ [expr.mptr.oper]p6
702 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
703 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
704 !BinOp->getType()->isFunctionType())
705 return BinOp->getLHS()->isLvalue(Ctx);
706
Douglas Gregorbf3af052008-11-13 20:12:29 +0000707 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000708 return LV_InvalidExpression;
709
Douglas Gregorbf3af052008-11-13 20:12:29 +0000710 if (Ctx.getLangOptions().CPlusPlus)
711 // C++ [expr.ass]p1:
712 // The result of an assignment operation [...] is an lvalue.
713 return LV_Valid;
714
715
716 // C99 6.5.16:
717 // An assignment expression [...] is not an lvalue.
718 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000719 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000720 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000721 case CXXOperatorCallExprClass:
722 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000723 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +0000724 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000725 // is an lvalue reference.
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000726 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor9d293df2008-10-28 00:22:11 +0000727 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000728 CalleeType = FnTypePtr->getPointeeType();
729 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000730 if (FnType->getResultType()->isLValueReferenceType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000731 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000732
Douglas Gregor9d293df2008-10-28 00:22:11 +0000733 break;
734 }
Steve Naroffe6386392007-12-05 04:00:10 +0000735 case CompoundLiteralExprClass: // C99 6.5.2.5p5
736 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000737 case ChooseExprClass:
738 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000739 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000740 case ExtVectorElementExprClass:
741 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000742 return LV_DuplicateVectorComponents;
743 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000744 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
745 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000746 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
747 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000748 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000749 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000750 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000751 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000752 case VAArgExprClass:
Daniel Dunbaradadd8d2009-02-12 09:21:08 +0000753 return LV_NotObjectType;
Chris Lattner04421082008-04-08 04:40:51 +0000754 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000755 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000756 case CXXConditionDeclExprClass:
757 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000758 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000759 case CXXFunctionalCastExprClass:
760 case CXXStaticCastExprClass:
761 case CXXDynamicCastExprClass:
762 case CXXReinterpretCastExprClass:
763 case CXXConstCastExprClass:
764 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000765 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +0000766 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
767 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000768 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
769 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +0000770 return LV_Valid;
771 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000772 case CXXTypeidExprClass:
773 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
774 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 default:
776 break;
777 }
778 return LV_InvalidExpression;
779}
780
781/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
782/// does not have an incomplete type, does not have a const-qualified type, and
783/// if it is a structure or union, does not have any member (including,
784/// recursively, any member or element of all contained aggregates or unions)
785/// with a const-qualified type.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000786Expr::isModifiableLvalueResult
787Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +0000788 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000789
790 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000791 case LV_Valid:
792 // C++ 3.10p11: Functions cannot be modified, but pointers to
793 // functions can be modifiable.
794 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
795 return MLV_NotObjectType;
796 break;
797
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 case LV_NotObjectType: return MLV_NotObjectType;
799 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000800 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000801 case LV_InvalidExpression:
802 // If the top level is a C-style cast, and the subexpression is a valid
803 // lvalue, then this is probably a use of the old-school "cast as lvalue"
804 // GCC extension. We don't support it, but we want to produce good
805 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000806 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
807 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
808 if (Loc)
809 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +0000810 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000811 }
812 }
Chris Lattnerca354fa2008-11-17 19:51:54 +0000813 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000814 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 }
Eli Friedman04831aa2009-03-22 23:26:56 +0000816
817 // The following is illegal:
818 // void takeclosure(void (^C)(void));
819 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
820 //
Chris Lattner7fd09952009-03-23 17:57:53 +0000821 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +0000822 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
823 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
824 return MLV_NotBlockQualified;
825 }
826
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000827 QualType CT = Ctx.getCanonicalType(getType());
828
829 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000831 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000833 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 return MLV_IncompleteType;
835
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000836 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 if (r->hasConstFields())
838 return MLV_ConstQualified;
839 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000840
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000841 // Assigning to an 'implicit' property?
Chris Lattner7fd09952009-03-23 17:57:53 +0000842 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000843 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
844 if (KVCExpr->getSetterMethod() == 0)
845 return MLV_NoSetterProperty;
846 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 return MLV_Valid;
848}
849
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000850/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000851/// duration. This means that the address of this expression is a link-time
852/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000853bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000854 switch (getStmtClass()) {
855 default:
856 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000857 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000858 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000859 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000860 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000861 case CompoundLiteralExprClass:
862 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000863 case DeclRefExprClass:
864 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000865 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
866 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000867 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000868 if (isa<FunctionDecl>(D))
869 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000870 return false;
871 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000872 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000873 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000874 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000875 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000876 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000877 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000878 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000879 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000880 case CXXDefaultArgExprClass:
881 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000882 }
883}
884
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000885/// isOBJCGCCandidate - Check if an expression is objc gc'able.
886///
887bool Expr::isOBJCGCCandidate() const {
888 switch (getStmtClass()) {
889 default:
890 return false;
891 case ObjCIvarRefExprClass:
892 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000893 case Expr::UnaryOperatorClass:
894 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000895 case ParenExprClass:
896 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
897 case ImplicitCastExprClass:
898 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
899 case DeclRefExprClass:
900 case QualifiedDeclRefExprClass: {
901 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
902 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
903 return VD->hasGlobalStorage();
904 return false;
905 }
906 case MemberExprClass: {
907 const MemberExpr *M = cast<MemberExpr>(this);
908 return !M->isArrow() && M->getBase()->isOBJCGCCandidate();
909 }
910 case ArraySubscriptExprClass:
911 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
912 }
913}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000914Expr* Expr::IgnoreParens() {
915 Expr* E = this;
916 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
917 E = P->getSubExpr();
918
919 return E;
920}
921
Chris Lattner56f34942008-02-13 01:02:39 +0000922/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
923/// or CastExprs or ImplicitCastExprs, returning their operand.
924Expr *Expr::IgnoreParenCasts() {
925 Expr *E = this;
926 while (true) {
927 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
928 E = P->getSubExpr();
929 else if (CastExpr *P = dyn_cast<CastExpr>(E))
930 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +0000931 else
932 return E;
933 }
934}
935
Chris Lattnerecdd8412009-03-13 17:28:01 +0000936/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
937/// value (including ptr->int casts of the same size). Strip off any
938/// ParenExpr or CastExprs, returning their operand.
939Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
940 Expr *E = this;
941 while (true) {
942 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
943 E = P->getSubExpr();
944 continue;
945 }
946
947 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
948 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
949 // ptr<->int casts of the same width. We also ignore all identify casts.
950 Expr *SE = P->getSubExpr();
951
952 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
953 E = SE;
954 continue;
955 }
956
957 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
958 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
959 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
960 E = SE;
961 continue;
962 }
963 }
964
965 return E;
966 }
967}
968
969
Douglas Gregor898574e2008-12-05 23:32:09 +0000970/// hasAnyTypeDependentArguments - Determines if any of the expressions
971/// in Exprs is type-dependent.
972bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
973 for (unsigned I = 0; I < NumExprs; ++I)
974 if (Exprs[I]->isTypeDependent())
975 return true;
976
977 return false;
978}
979
980/// hasAnyValueDependentArguments - Determines if any of the expressions
981/// in Exprs is value-dependent.
982bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
983 for (unsigned I = 0; I < NumExprs; ++I)
984 if (Exprs[I]->isValueDependent())
985 return true;
986
987 return false;
988}
989
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000990bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000991 // This function is attempting whether an expression is an initializer
992 // which can be evaluated at compile-time. isEvaluatable handles most
993 // of the cases, but it can't deal with some initializer-specific
994 // expressions, and it can't deal with aggregates; we deal with those here,
995 // and fall back to isEvaluatable for the other cases.
996
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000997 // FIXME: This function assumes the variable being assigned to
998 // isn't a reference type!
999
Anders Carlssone8a32b82008-11-24 05:23:59 +00001000 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001001 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001002 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001003 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001004 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001005 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001006 // This handles gcc's extension that allows global initializers like
1007 // "struct x {int x;} x = (struct x) {};".
1008 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001009 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001010 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001011 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001012 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001013 // FIXME: This doesn't deal with fields with reference types correctly.
1014 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1015 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001016 const InitListExpr *Exp = cast<InitListExpr>(this);
1017 unsigned numInits = Exp->getNumInits();
1018 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001019 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001020 return false;
1021 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001022 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001023 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001024 case ImplicitValueInitExprClass:
1025 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001026 case ParenExprClass: {
1027 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1028 }
1029 case UnaryOperatorClass: {
1030 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1031 if (Exp->getOpcode() == UnaryOperator::Extension)
1032 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1033 break;
1034 }
1035 case CStyleCastExprClass:
1036 // Handle casts with a destination that's a struct or union; this
1037 // deals with both the gcc no-op struct cast extension and the
1038 // cast-to-union extension.
1039 if (getType()->isRecordType())
1040 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1041 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001042 }
1043
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001044 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001045}
1046
Reid Spencer5f016e22007-07-11 17:01:13 +00001047/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001048/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001049
1050/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1051/// comma, etc
1052///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001053/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1054/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1055/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001056
Eli Friedmane28d7192009-02-26 09:29:13 +00001057// CheckICE - This function does the fundamental ICE checking: the returned
1058// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1059// Note that to reduce code duplication, this helper does no evaluation
1060// itself; the caller checks whether the expression is evaluatable, and
1061// in the rare cases where CheckICE actually cares about the evaluated
1062// value, it calls into Evalute.
1063//
1064// Meanings of Val:
1065// 0: This expression is an ICE if it can be evaluated by Evaluate.
1066// 1: This expression is not an ICE, but if it isn't evaluated, it's
1067// a legal subexpression for an ICE. This return value is used to handle
1068// the comma operator in C99 mode.
1069// 2: This expression is not an ICE, and is not a legal subexpression for one.
1070
1071struct ICEDiag {
1072 unsigned Val;
1073 SourceLocation Loc;
1074
1075 public:
1076 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1077 ICEDiag() : Val(0) {}
1078};
1079
1080ICEDiag NoDiag() { return ICEDiag(); }
1081
Eli Friedman60ce9632009-02-27 04:07:58 +00001082static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1083 Expr::EvalResult EVResult;
1084 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1085 !EVResult.Val.isInt()) {
1086 return ICEDiag(2, E->getLocStart());
1087 }
1088 return NoDiag();
1089}
1090
Eli Friedmane28d7192009-02-26 09:29:13 +00001091static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001092 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001093 if (!E->getType()->isIntegralType()) {
1094 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001095 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001096
1097 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001099 return ICEDiag(2, E->getLocStart());
1100 case Expr::ParenExprClass:
1101 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1102 case Expr::IntegerLiteralClass:
1103 case Expr::CharacterLiteralClass:
1104 case Expr::CXXBoolLiteralExprClass:
1105 case Expr::CXXZeroInitValueExprClass:
1106 case Expr::TypesCompatibleExprClass:
1107 case Expr::UnaryTypeTraitExprClass:
1108 return NoDiag();
1109 case Expr::CallExprClass:
1110 case Expr::CXXOperatorCallExprClass: {
1111 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001112 if (CE->isBuiltinCall(Ctx))
1113 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001114 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001115 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001116 case Expr::DeclRefExprClass:
1117 case Expr::QualifiedDeclRefExprClass:
1118 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1119 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001120 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedmane28d7192009-02-26 09:29:13 +00001121 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001122 // C++ 7.1.5.1p2
1123 // A variable of non-volatile const-qualified integral or enumeration
1124 // type initialized by an ICE can be used in ICEs.
1125 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001126 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001127 if (const Expr *Init = Dcl->getInit())
Eli Friedmane28d7192009-02-26 09:29:13 +00001128 return CheckICE(Init, Ctx);
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001129 }
1130 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001131 return ICEDiag(2, E->getLocStart());
1132 case Expr::UnaryOperatorClass: {
1133 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 switch (Exp->getOpcode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001136 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001138 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001142 case UnaryOperator::Real:
1143 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001144 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001145 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001146 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1147 // Evaluate matches the proposed gcc behavior for cases like
1148 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1149 // compliance: we should warn earlier for offsetof expressions with
1150 // array subscripts that aren't ICEs, and if the array subscripts
1151 // are ICEs, the value of the offsetof must be an integer constant.
1152 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001155 case Expr::SizeOfAlignOfExprClass: {
1156 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1157 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1158 return ICEDiag(2, E->getLocStart());
1159 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001161 case Expr::BinaryOperatorClass: {
1162 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 switch (Exp->getOpcode()) {
1164 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001165 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001169 case BinaryOperator::Add:
1170 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001173 case BinaryOperator::LT:
1174 case BinaryOperator::GT:
1175 case BinaryOperator::LE:
1176 case BinaryOperator::GE:
1177 case BinaryOperator::EQ:
1178 case BinaryOperator::NE:
1179 case BinaryOperator::And:
1180 case BinaryOperator::Xor:
1181 case BinaryOperator::Or:
1182 case BinaryOperator::Comma: {
1183 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1184 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001185 if (Exp->getOpcode() == BinaryOperator::Div ||
1186 Exp->getOpcode() == BinaryOperator::Rem) {
1187 // Evaluate gives an error for undefined Div/Rem, so make sure
1188 // we don't evaluate one.
1189 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1190 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1191 if (REval == 0)
1192 return ICEDiag(1, E->getLocStart());
1193 if (REval.isSigned() && REval.isAllOnesValue()) {
1194 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1195 if (LEval.isMinSignedValue())
1196 return ICEDiag(1, E->getLocStart());
1197 }
1198 }
1199 }
1200 if (Exp->getOpcode() == BinaryOperator::Comma) {
1201 if (Ctx.getLangOptions().C99) {
1202 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1203 // if it isn't evaluated.
1204 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1205 return ICEDiag(1, E->getLocStart());
1206 } else {
1207 // In both C89 and C++, commas in ICEs are illegal.
1208 return ICEDiag(2, E->getLocStart());
1209 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001210 }
1211 if (LHSResult.Val >= RHSResult.Val)
1212 return LHSResult;
1213 return RHSResult;
1214 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001216 case BinaryOperator::LOr: {
1217 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1218 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1219 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1220 // Rare case where the RHS has a comma "side-effect"; we need
1221 // to actually check the condition to see whether the side
1222 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001223 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001224 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001225 return RHSResult;
1226 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001227 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001228
Eli Friedmane28d7192009-02-26 09:29:13 +00001229 if (LHSResult.Val >= RHSResult.Val)
1230 return LHSResult;
1231 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001233 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001235 case Expr::ImplicitCastExprClass:
1236 case Expr::CStyleCastExprClass:
1237 case Expr::CXXFunctionalCastExprClass: {
1238 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1239 if (SubExpr->getType()->isIntegralType())
1240 return CheckICE(SubExpr, Ctx);
1241 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1242 return NoDiag();
1243 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001245 case Expr::ConditionalOperatorClass: {
1246 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner28daa532008-12-12 06:55:44 +00001247 // If the condition (ignoring parens) is a __builtin_constant_p call,
1248 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001249 // expression, and it is fully evaluated. This is an important GNU
1250 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001251 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001252 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001253 Expr::EvalResult EVResult;
1254 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1255 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001256 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001257 }
1258 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001259 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001260 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1261 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1262 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1263 if (CondResult.Val == 2)
1264 return CondResult;
1265 if (TrueResult.Val == 2)
1266 return TrueResult;
1267 if (FalseResult.Val == 2)
1268 return FalseResult;
1269 if (CondResult.Val == 1)
1270 return CondResult;
1271 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1272 return NoDiag();
1273 // Rare case where the diagnostics depend on which side is evaluated
1274 // Note that if we get here, CondResult is 0, and at least one of
1275 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001276 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001277 return FalseResult;
1278 }
1279 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001281 case Expr::CXXDefaultArgExprClass:
1282 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001283 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001284 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001285 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001287}
Reid Spencer5f016e22007-07-11 17:01:13 +00001288
Eli Friedmane28d7192009-02-26 09:29:13 +00001289bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1290 SourceLocation *Loc, bool isEvaluated) const {
1291 ICEDiag d = CheckICE(this, Ctx);
1292 if (d.Val != 0) {
1293 if (Loc) *Loc = d.Loc;
1294 return false;
1295 }
1296 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001297 if (!Evaluate(EvalResult, Ctx))
1298 assert(0 && "ICE cannot be evaluated!");
1299 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1300 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001301 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 return true;
1303}
1304
Reid Spencer5f016e22007-07-11 17:01:13 +00001305/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1306/// integer constant expression with the value zero, or if this is one that is
1307/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001308bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1309{
Sebastian Redl07779722008-10-31 14:43:28 +00001310 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001311 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001312 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001313 // Check that it is a cast to void*.
1314 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1315 QualType Pointee = PT->getPointeeType();
1316 if (Pointee.getCVRQualifiers() == 0 &&
1317 Pointee->isVoidType() && // to void*
1318 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001319 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001320 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001321 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001322 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1323 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001324 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001325 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1326 // Accept ((void*)0) as a null pointer constant, as many other
1327 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001328 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001329 } else if (const CXXDefaultArgExpr *DefaultArg
1330 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001331 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001332 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001333 } else if (isa<GNUNullExpr>(this)) {
1334 // The GNU __null extension is always a null pointer constant.
1335 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001336 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001337
Steve Naroffaa58f002008-01-14 16:10:57 +00001338 // This expression must be an integer type.
1339 if (!getType()->isIntegerType())
1340 return false;
1341
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 // If we have an integer constant expression, we need to *evaluate* it and
1343 // test for the value 0.
Anders Carlssond2652772008-12-01 06:28:23 +00001344 // FIXME: We should probably return false if we're compiling in strict mode
1345 // and Diag is not null (this indicates that the value was foldable but not
1346 // an ICE.
1347 EvalResult Result;
Anders Carlssonefa9b382008-12-01 02:13:57 +00001348 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssond2652772008-12-01 06:28:23 +00001349 Result.Val.isInt() && Result.Val.getInt() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001350}
Steve Naroff31a45842007-07-28 23:10:27 +00001351
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001352/// isBitField - Return true if this expression is a bit-field.
1353bool Expr::isBitField() {
1354 Expr *E = this->IgnoreParenCasts();
1355 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001356 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1357 return Field->isBitField();
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001358 return false;
1359}
1360
Chris Lattner2140e902009-02-16 22:14:05 +00001361/// isArrow - Return true if the base expression is a pointer to vector,
1362/// return false if the base expression is a vector.
1363bool ExtVectorElementExpr::isArrow() const {
1364 return getBase()->getType()->isPointerType();
1365}
1366
Nate Begeman213541a2008-04-18 23:10:10 +00001367unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001368 if (const VectorType *VT = getType()->getAsVectorType())
1369 return VT->getNumElements();
1370 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001371}
1372
Nate Begeman8a997642008-05-09 06:41:27 +00001373/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001374bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001375 const char *compStr = Accessor.getName();
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001376 unsigned length = Accessor.getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001377
1378 // Halving swizzles do not contain duplicate elements.
1379 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1380 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1381 return false;
1382
1383 // Advance past s-char prefix on hex swizzles.
1384 if (*compStr == 's') {
1385 compStr++;
1386 length--;
1387 }
Steve Narofffec0b492007-07-30 03:29:09 +00001388
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001389 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001390 const char *s = compStr+i;
1391 for (const char c = *s++; *s; s++)
1392 if (c == *s)
1393 return true;
1394 }
1395 return false;
1396}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001397
Nate Begeman8a997642008-05-09 06:41:27 +00001398/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001399void ExtVectorElementExpr::getEncodedElementAccess(
1400 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001401 const char *compStr = Accessor.getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001402 if (*compStr == 's')
1403 compStr++;
1404
1405 bool isHi = !strcmp(compStr, "hi");
1406 bool isLo = !strcmp(compStr, "lo");
1407 bool isEven = !strcmp(compStr, "even");
1408 bool isOdd = !strcmp(compStr, "odd");
1409
Nate Begeman8a997642008-05-09 06:41:27 +00001410 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1411 uint64_t Index;
1412
1413 if (isHi)
1414 Index = e + i;
1415 else if (isLo)
1416 Index = i;
1417 else if (isEven)
1418 Index = 2 * i;
1419 else if (isOdd)
1420 Index = 2 * i + 1;
1421 else
1422 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001423
Nate Begeman3b8d1162008-05-13 21:03:02 +00001424 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001425 }
Nate Begeman8a997642008-05-09 06:41:27 +00001426}
1427
Steve Naroff68d331a2007-09-27 14:38:14 +00001428// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001429ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001430 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001431 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001432 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001433 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001434 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001435 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001436 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001437 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001438 if (NumArgs) {
1439 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001440 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1441 }
Steve Naroff563477d2007-09-18 23:55:05 +00001442 LBracloc = LBrac;
1443 RBracloc = RBrac;
1444}
1445
Steve Naroff68d331a2007-09-27 14:38:14 +00001446// constructor for class messages.
1447// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001448ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001449 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001450 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001451 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001452 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001453 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001454 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001455 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001456 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001457 if (NumArgs) {
1458 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001459 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1460 }
Steve Naroff563477d2007-09-18 23:55:05 +00001461 LBracloc = LBrac;
1462 RBracloc = RBrac;
1463}
1464
Ted Kremenek4df728e2008-06-24 15:50:53 +00001465// constructor for class messages.
1466ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1467 QualType retType, ObjCMethodDecl *mproto,
1468 SourceLocation LBrac, SourceLocation RBrac,
1469 Expr **ArgExprs, unsigned nargs)
1470: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1471MethodProto(mproto) {
1472 NumArgs = nargs;
1473 SubExprs = new Stmt*[NumArgs+1];
1474 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1475 if (NumArgs) {
1476 for (unsigned i = 0; i != NumArgs; ++i)
1477 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1478 }
1479 LBracloc = LBrac;
1480 RBracloc = RBrac;
1481}
1482
1483ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1484 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1485 switch (x & Flags) {
1486 default:
1487 assert(false && "Invalid ObjCMessageExpr.");
1488 case IsInstMeth:
1489 return ClassInfo(0, 0);
1490 case IsClsMethDeclUnknown:
1491 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1492 case IsClsMethDeclKnown: {
1493 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1494 return ClassInfo(D, D->getIdentifier());
1495 }
1496 }
1497}
1498
Chris Lattner27437ca2007-10-25 00:29:32 +00001499bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001500 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001501}
1502
Sebastian Redl05189992008-11-11 17:56:53 +00001503void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1504 // Override default behavior of traversing children. If this has a type
1505 // operand and the type is a variable-length array, the child iteration
1506 // will iterate over the size expression. However, this expression belongs
1507 // to the type, not to this, so we don't want to delete it.
1508 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001509 if (isArgumentType()) {
1510 this->~SizeOfAlignOfExpr();
1511 C.Deallocate(this);
1512 }
Sebastian Redl05189992008-11-11 17:56:53 +00001513 else
1514 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001515}
1516
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001517//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001518// DesignatedInitExpr
1519//===----------------------------------------------------------------------===//
1520
1521IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1522 assert(Kind == FieldDesignator && "Only valid on a field designator");
1523 if (Field.NameOrField & 0x01)
1524 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1525 else
1526 return getField()->getIdentifier();
1527}
1528
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001529DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1530 const Designator *Designators,
1531 SourceLocation EqualOrColonLoc,
1532 bool GNUSyntax,
1533 unsigned NumSubExprs)
1534 : Expr(DesignatedInitExprClass, Ty),
1535 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1536 NumDesignators(NumDesignators), NumSubExprs(NumSubExprs) {
1537 this->Designators = new Designator[NumDesignators];
1538 for (unsigned I = 0; I != NumDesignators; ++I)
1539 this->Designators[I] = Designators[I];
1540}
1541
Douglas Gregor05c13a32009-01-22 00:58:24 +00001542DesignatedInitExpr *
1543DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1544 unsigned NumDesignators,
1545 Expr **IndexExprs, unsigned NumIndexExprs,
1546 SourceLocation ColonOrEqualLoc,
1547 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001548 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001549 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001550 DesignatedInitExpr *DIE
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001551 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00001552 ColonOrEqualLoc, UsesColonSyntax,
1553 NumIndexExprs + 1);
1554
1555 // Fill in the designators
1556 unsigned ExpectedNumSubExprs = 0;
1557 designators_iterator Desig = DIE->designators_begin();
1558 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001559 if (Designators[Idx].isArrayDesignator())
1560 ++ExpectedNumSubExprs;
1561 else if (Designators[Idx].isArrayRangeDesignator())
1562 ExpectedNumSubExprs += 2;
1563 }
1564 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1565
1566 // Fill in the subexpressions, including the initializer expression.
1567 child_iterator Child = DIE->child_begin();
1568 *Child++ = Init;
1569 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1570 *Child = IndexExprs[Idx];
1571
1572 return DIE;
1573}
1574
1575SourceRange DesignatedInitExpr::getSourceRange() const {
1576 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001577 Designator &First =
1578 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001579 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001580 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001581 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1582 else
1583 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1584 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001585 StartLoc =
1586 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001587 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1588}
1589
Douglas Gregor05c13a32009-01-22 00:58:24 +00001590Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1591 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1592 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1593 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001594 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1595 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1596}
1597
1598Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1599 assert(D.Kind == Designator::ArrayRangeDesignator &&
1600 "Requires array range designator");
1601 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1602 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001603 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1604 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1605}
1606
1607Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1608 assert(D.Kind == Designator::ArrayRangeDesignator &&
1609 "Requires array range designator");
1610 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1611 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001612 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1613 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1614}
1615
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001616/// \brief Replaces the designator at index @p Idx with the series
1617/// of designators in [First, Last).
1618void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1619 const Designator *First,
1620 const Designator *Last) {
1621 unsigned NumNewDesignators = Last - First;
1622 if (NumNewDesignators == 0) {
1623 std::copy_backward(Designators + Idx + 1,
1624 Designators + NumDesignators,
1625 Designators + Idx);
1626 --NumNewDesignators;
1627 return;
1628 } else if (NumNewDesignators == 1) {
1629 Designators[Idx] = *First;
1630 return;
1631 }
1632
1633 Designator *NewDesignators
1634 = new Designator[NumDesignators - 1 + NumNewDesignators];
1635 std::copy(Designators, Designators + Idx, NewDesignators);
1636 std::copy(First, Last, NewDesignators + Idx);
1637 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1638 NewDesignators + Idx + NumNewDesignators);
1639 delete [] Designators;
1640 Designators = NewDesignators;
1641 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1642}
1643
1644void DesignatedInitExpr::Destroy(ASTContext &C) {
1645 delete [] Designators;
1646 Expr::Destroy(C);
1647}
1648
Douglas Gregor05c13a32009-01-22 00:58:24 +00001649//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001650// ExprIterator.
1651//===----------------------------------------------------------------------===//
1652
1653Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1654Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1655Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1656const Expr* ConstExprIterator::operator[](size_t idx) const {
1657 return cast<Expr>(I[idx]);
1658}
1659const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1660const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1661
1662//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001663// Child Iterators for iterating over subexpressions/substatements
1664//===----------------------------------------------------------------------===//
1665
1666// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001667Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1668Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001669
Steve Naroff7779db42007-11-12 14:29:37 +00001670// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001671Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1672Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001673
Steve Naroffe3e9add2008-06-02 23:03:37 +00001674// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001675Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1676Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001677
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001678// ObjCKVCRefExpr
1679Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1680Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1681
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001682// ObjCSuperExpr
1683Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1684Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1685
Chris Lattnerd9f69102008-08-10 01:53:14 +00001686// PredefinedExpr
1687Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1688Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001689
1690// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001691Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1692Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001693
1694// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001695Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001696Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001697
1698// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001699Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1700Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001701
Chris Lattner5d661452007-08-26 03:42:43 +00001702// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001703Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1704Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001705
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001706// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001707Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1708Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001709
1710// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001711Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1712Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001713
1714// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001715Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1716Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001717
Sebastian Redl05189992008-11-11 17:56:53 +00001718// SizeOfAlignOfExpr
1719Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1720 // If this is of a type and the type is a VLA type (and not a typedef), the
1721 // size expression of the VLA needs to be treated as an executable expression.
1722 // Why isn't this weirdness documented better in StmtIterator?
1723 if (isArgumentType()) {
1724 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1725 getArgumentType().getTypePtr()))
1726 return child_iterator(T);
1727 return child_iterator();
1728 }
Sebastian Redld4575892008-12-03 23:17:54 +00001729 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001730}
Sebastian Redl05189992008-11-11 17:56:53 +00001731Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1732 if (isArgumentType())
1733 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001734 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001735}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001736
1737// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001738Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001739 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001740}
Ted Kremenek1237c672007-08-24 20:06:47 +00001741Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001742 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001743}
1744
1745// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001746Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001747 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001748}
Ted Kremenek1237c672007-08-24 20:06:47 +00001749Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001750 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001751}
Ted Kremenek1237c672007-08-24 20:06:47 +00001752
1753// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001754Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1755Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001756
Nate Begeman213541a2008-04-18 23:10:10 +00001757// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001758Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1759Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001760
1761// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001762Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1763Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001764
Ted Kremenek1237c672007-08-24 20:06:47 +00001765// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001766Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1767Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001768
1769// BinaryOperator
1770Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001771 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001772}
Ted Kremenek1237c672007-08-24 20:06:47 +00001773Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001774 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001775}
1776
1777// ConditionalOperator
1778Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001779 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001780}
Ted Kremenek1237c672007-08-24 20:06:47 +00001781Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001782 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001783}
1784
1785// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001786Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1787Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001788
Ted Kremenek1237c672007-08-24 20:06:47 +00001789// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001790Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1791Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001792
1793// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001794Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1795 return child_iterator();
1796}
1797
1798Stmt::child_iterator TypesCompatibleExpr::child_end() {
1799 return child_iterator();
1800}
Ted Kremenek1237c672007-08-24 20:06:47 +00001801
1802// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001803Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1804Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001805
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001806// GNUNullExpr
1807Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1808Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1809
Eli Friedmand38617c2008-05-14 19:38:39 +00001810// ShuffleVectorExpr
1811Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001812 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001813}
1814Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001815 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001816}
1817
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001818// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001819Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1820Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001821
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001822// InitListExpr
1823Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001824 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001825}
1826Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001827 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001828}
1829
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001830// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001831Stmt::child_iterator DesignatedInitExpr::child_begin() {
1832 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1833 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001834 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1835}
1836Stmt::child_iterator DesignatedInitExpr::child_end() {
1837 return child_iterator(&*child_begin() + NumSubExprs);
1838}
1839
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001840// ImplicitValueInitExpr
1841Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1842 return child_iterator();
1843}
1844
1845Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1846 return child_iterator();
1847}
1848
Ted Kremenek1237c672007-08-24 20:06:47 +00001849// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001850Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001851 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001852}
1853Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001854 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001855}
Ted Kremenek1237c672007-08-24 20:06:47 +00001856
1857// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001858Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1859Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001860
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001861// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001862Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1863 return child_iterator();
1864}
1865Stmt::child_iterator ObjCSelectorExpr::child_end() {
1866 return child_iterator();
1867}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001868
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001869// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001870Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1871 return child_iterator();
1872}
1873Stmt::child_iterator ObjCProtocolExpr::child_end() {
1874 return child_iterator();
1875}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001876
Steve Naroff563477d2007-09-18 23:55:05 +00001877// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00001878Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001879 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00001880}
1881Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001882 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00001883}
1884
Steve Naroff4eb206b2008-09-03 18:15:37 +00001885// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00001886Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1887Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00001888
Ted Kremenek9da13f92008-09-26 23:24:14 +00001889Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1890Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }