blob: 0d38dd2cd12d867c00aa02fd4cfb915566eecc46 [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
Douglas Gregor88c9a462009-04-17 21:46:47 +0000225 if (SubExprs) C.Deallocate(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}
Douglas Gregor72971342009-04-18 00:02:19 +0000402const Stmt *BlockExpr::getBody() const {
403 return TheBlock->getBody();
404}
405Stmt *BlockExpr::getBody() {
406 return TheBlock->getBody();
407}
Steve Naroff56ee6892008-10-08 17:01:13 +0000408
409
Reid Spencer5f016e22007-07-11 17:01:13 +0000410//===----------------------------------------------------------------------===//
411// Generic Expression Routines
412//===----------------------------------------------------------------------===//
413
Chris Lattner026dc962009-02-14 07:37:35 +0000414/// isUnusedResultAWarning - Return true if this immediate expression should
415/// be warned about if the result is unused. If so, fill in Loc and Ranges
416/// with location to warn on and the source range[s] to report with the
417/// warning.
418bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
419 SourceRange &R2) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 switch (getStmtClass()) {
421 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000422 Loc = getExprLoc();
423 R1 = getSourceRange();
424 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000426 return cast<ParenExpr>(this)->getSubExpr()->
427 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 case UnaryOperatorClass: {
429 const UnaryOperator *UO = cast<UnaryOperator>(this);
430
431 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000432 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 case UnaryOperator::PostInc:
434 case UnaryOperator::PostDec:
435 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000436 case UnaryOperator::PreDec: // ++/--
437 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 case UnaryOperator::Deref:
439 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000440 if (getType().isVolatileQualified())
441 return false;
442 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 case UnaryOperator::Real:
444 case UnaryOperator::Imag:
445 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000446 if (UO->getSubExpr()->getType().isVolatileQualified())
447 return false;
448 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 case UnaryOperator::Extension:
Chris Lattner026dc962009-02-14 07:37:35 +0000450 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 }
Chris Lattner026dc962009-02-14 07:37:35 +0000452 Loc = UO->getOperatorLoc();
453 R1 = UO->getSubExpr()->getSourceRange();
454 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000456 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000457 const BinaryOperator *BO = cast<BinaryOperator>(this);
458 // Consider comma to have side effects if the LHS or RHS does.
459 if (BO->getOpcode() == BinaryOperator::Comma)
460 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
461 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnere7716e62007-12-01 06:07:34 +0000462
Chris Lattner026dc962009-02-14 07:37:35 +0000463 if (BO->isAssignmentOp())
464 return false;
465 Loc = BO->getOperatorLoc();
466 R1 = BO->getLHS()->getSourceRange();
467 R2 = BO->getRHS()->getSourceRange();
468 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000469 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000470 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000471 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000472
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000473 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000474 // The condition must be evaluated, but if either the LHS or RHS is a
475 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000476 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stumpbefbcf42009-02-27 03:16:57 +0000477 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000478 return true;
479 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000480 }
481
Reid Spencer5f016e22007-07-11 17:01:13 +0000482 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000483 // If the base pointer or element is to a volatile pointer/field, accessing
484 // it is a side effect.
485 if (getType().isVolatileQualified())
486 return false;
487 Loc = cast<MemberExpr>(this)->getMemberLoc();
488 R1 = SourceRange(Loc, Loc);
489 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
490 return true;
491
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 case ArraySubscriptExprClass:
493 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000494 // it is a side effect.
495 if (getType().isVolatileQualified())
496 return false;
497 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
498 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
499 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
500 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000501
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000503 case CXXOperatorCallExprClass:
504 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000505 // If this is a direct call, get the callee.
506 const CallExpr *CE = cast<CallExpr>(this);
507 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
508 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
509 // If the callee has attribute pure, const, or warn_unused_result, warn
510 // about it. void foo() { strlen("bar"); } should warn.
511 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
512 if (FD->getAttr<WarnUnusedResultAttr>() ||
513 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
514 Loc = CE->getCallee()->getLocStart();
515 R1 = CE->getCallee()->getSourceRange();
516
517 if (unsigned NumArgs = CE->getNumArgs())
518 R2 = SourceRange(CE->getArg(0)->getLocStart(),
519 CE->getArg(NumArgs-1)->getLocEnd());
520 return true;
521 }
522 }
523 return false;
524 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000525 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000526 return false;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000527 case StmtExprClass: {
528 // Statement exprs don't logically have side effects themselves, but are
529 // sometimes used in macros in ways that give them a type that is unused.
530 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
531 // however, if the result of the stmt expr is dead, we don't want to emit a
532 // warning.
533 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
534 if (!CS->body_empty())
535 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattner026dc962009-02-14 07:37:35 +0000536 return E->isUnusedResultAWarning(Loc, R1, R2);
537
538 Loc = cast<StmtExpr>(this)->getLParenLoc();
539 R1 = getSourceRange();
540 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000541 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000542 case CStyleCastExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000543 // If this is a cast to void, check the operand. Otherwise, the result of
544 // the cast is unused.
545 if (getType()->isVoidType())
546 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
547 R1, R2);
548 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
549 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
550 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000551 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 // If this is a cast to void, check the operand. Otherwise, the result of
553 // the cast is unused.
554 if (getType()->isVoidType())
Chris Lattner026dc962009-02-14 07:37:35 +0000555 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
556 R1, R2);
557 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
558 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
559 return true;
560
Eli Friedman4be1f472008-05-19 21:24:43 +0000561 case ImplicitCastExprClass:
562 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000563 return cast<ImplicitCastExpr>(this)
564 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000565
Chris Lattner04421082008-04-08 04:40:51 +0000566 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000567 return cast<CXXDefaultArgExpr>(this)
568 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000569
570 case CXXNewExprClass:
571 // FIXME: In theory, there might be new expressions that don't have side
572 // effects (e.g. a placement new with an uninitialized POD).
573 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000574 return false;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000575 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000576}
577
Douglas Gregorba7e2102008-10-22 15:04:37 +0000578/// DeclCanBeLvalue - Determine whether the given declaration can be
579/// an lvalue. This is a helper routine for isLvalue.
580static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000581 // C++ [temp.param]p6:
582 // A non-type non-reference template-parameter is not an lvalue.
583 if (const NonTypeTemplateParmDecl *NTTParm
584 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
585 return NTTParm->getType()->isReferenceType();
586
Douglas Gregor44b43212008-12-11 16:49:14 +0000587 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000588 // C++ 3.10p2: An lvalue refers to an object or function.
589 (Ctx.getLangOptions().CPlusPlus &&
590 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
591}
592
Reid Spencer5f016e22007-07-11 17:01:13 +0000593/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
594/// incomplete type other than void. Nonarray expressions that can be lvalues:
595/// - name, where name must be a variable
596/// - e[i]
597/// - (e), where e must be an lvalue
598/// - e.name, where e must be an lvalue
599/// - e->name
600/// - *e, the type of e cannot be a function type
601/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000602/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000603/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000604///
Chris Lattner28be73f2008-07-26 21:30:36 +0000605Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +0000606 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
607
608 isLvalueResult Res = isLvalueInternal(Ctx);
609 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
610 return Res;
611
Douglas Gregor98cd5992008-10-21 23:43:52 +0000612 // first, check the type (C99 6.3.2.1). Expressions with function
613 // type in C are not lvalues, but they can be lvalues in C++.
Eli Friedman53202852009-05-03 22:36:05 +0000614 if (TR->isFunctionType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 return LV_NotObjectType;
616
Steve Naroffacb818a2008-02-10 01:39:04 +0000617 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000618 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000619 return LV_IncompleteVoidType;
620
Eli Friedman53202852009-05-03 22:36:05 +0000621 return LV_Valid;
622}
Bill Wendling08ad47c2007-07-17 03:52:31 +0000623
Eli Friedman53202852009-05-03 22:36:05 +0000624// Check whether the expression can be sanely treated like an l-value
625Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000627 case StringLiteralClass: // C99 6.5.1p4
628 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000629 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
631 // For vectors, make sure base is an lvalue (i.e. not a function call).
632 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000633 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000635 case DeclRefExprClass:
636 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000637 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
638 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 return LV_Valid;
640 break;
Chris Lattner41110242008-06-17 18:05:57 +0000641 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000642 case BlockDeclRefExprClass: {
643 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000644 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000645 return LV_Valid;
646 break;
647 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000648 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000650 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
651 NamedDecl *Member = m->getMemberDecl();
652 // C++ [expr.ref]p4:
653 // If E2 is declared to have type "reference to T", then E1.E2
654 // is an lvalue.
655 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
656 if (Value->getType()->isReferenceType())
657 return LV_Valid;
658
659 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000660 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000661 return LV_Valid;
662
663 // -- If E2 is a non-static data member [...]. If E1 is an
664 // lvalue, then E1.E2 is an lvalue.
665 if (isa<FieldDecl>(Member))
666 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
667
668 // -- If it refers to a static member function [...], then
669 // E1.E2 is an lvalue.
670 // -- Otherwise, if E1.E2 refers to a non-static member
671 // function [...], then E1.E2 is not an lvalue.
672 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
673 return Method->isStatic()? LV_Valid : LV_MemberFunction;
674
675 // -- If E2 is a member enumerator [...], the expression E1.E2
676 // is not an lvalue.
677 if (isa<EnumConstantDecl>(Member))
678 return LV_InvalidExpression;
679
680 // Not an lvalue.
681 return LV_InvalidExpression;
682 }
683
684 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000685 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000686 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000687 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000689 return LV_Valid; // C99 6.5.3p4
690
691 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000692 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
693 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000694 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000695
696 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
697 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
698 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
699 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000701 case ImplicitCastExprClass:
702 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
703 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000705 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000706 case BinaryOperatorClass:
707 case CompoundAssignOperatorClass: {
708 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000709
710 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
711 BinOp->getOpcode() == BinaryOperator::Comma)
712 return BinOp->getRHS()->isLvalue(Ctx);
713
Sebastian Redl22460502009-02-07 00:15:38 +0000714 // C++ [expr.mptr.oper]p6
715 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
716 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
717 !BinOp->getType()->isFunctionType())
718 return BinOp->getLHS()->isLvalue(Ctx);
719
Douglas Gregorbf3af052008-11-13 20:12:29 +0000720 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000721 return LV_InvalidExpression;
722
Douglas Gregorbf3af052008-11-13 20:12:29 +0000723 if (Ctx.getLangOptions().CPlusPlus)
724 // C++ [expr.ass]p1:
725 // The result of an assignment operation [...] is an lvalue.
726 return LV_Valid;
727
728
729 // C99 6.5.16:
730 // An assignment expression [...] is not an lvalue.
731 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000732 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000733 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000734 case CXXOperatorCallExprClass:
735 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000736 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +0000737 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000738 // is an lvalue reference.
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000739 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor9d293df2008-10-28 00:22:11 +0000740 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000741 CalleeType = FnTypePtr->getPointeeType();
742 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000743 if (FnType->getResultType()->isLValueReferenceType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000744 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000745
Douglas Gregor9d293df2008-10-28 00:22:11 +0000746 break;
747 }
Steve Naroffe6386392007-12-05 04:00:10 +0000748 case CompoundLiteralExprClass: // C99 6.5.2.5p5
749 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000750 case ChooseExprClass:
751 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000752 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000753 case ExtVectorElementExprClass:
754 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000755 return LV_DuplicateVectorComponents;
756 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000757 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
758 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000759 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
760 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000761 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000762 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000763 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000764 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +0000765 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000766 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000767 case CXXConditionDeclExprClass:
768 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000769 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000770 case CXXFunctionalCastExprClass:
771 case CXXStaticCastExprClass:
772 case CXXDynamicCastExprClass:
773 case CXXReinterpretCastExprClass:
774 case CXXConstCastExprClass:
775 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000776 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +0000777 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
778 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000779 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
780 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +0000781 return LV_Valid;
782 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000783 case CXXTypeidExprClass:
784 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
785 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +0000786 case ConditionalOperatorClass: {
787 // Complicated handling is only for C++.
788 if (!Ctx.getLangOptions().CPlusPlus)
789 return LV_InvalidExpression;
790
791 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
792 // everywhere there's an object converted to an rvalue. Also, any other
793 // casts should be wrapped by ImplicitCastExprs. There's just the special
794 // case involving throws to work out.
795 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
796 Expr *LHS = Cond->getLHS();
797 Expr *RHS = Cond->getRHS();
798 // C++0x 5.16p2
799 // If either the second or the third operand has type (cv) void, [...]
800 // the result [...] is an rvalue.
801 if (LHS->getType()->isVoidType() || RHS->getType()->isVoidType())
802 return LV_InvalidExpression;
803
804 // Both sides must be lvalues for the result to be an lvalue.
805 if (LHS->isLvalue(Ctx) != LV_Valid || RHS->isLvalue(Ctx) != LV_Valid)
806 return LV_InvalidExpression;
807
808 // That's it.
809 return LV_Valid;
810 }
811
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 default:
813 break;
814 }
815 return LV_InvalidExpression;
816}
817
818/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
819/// does not have an incomplete type, does not have a const-qualified type, and
820/// if it is a structure or union, does not have any member (including,
821/// recursively, any member or element of all contained aggregates or unions)
822/// with a const-qualified type.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000823Expr::isModifiableLvalueResult
824Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +0000825 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000826
827 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000828 case LV_Valid:
829 // C++ 3.10p11: Functions cannot be modified, but pointers to
830 // functions can be modifiable.
831 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
832 return MLV_NotObjectType;
833 break;
834
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 case LV_NotObjectType: return MLV_NotObjectType;
836 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000837 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000838 case LV_InvalidExpression:
839 // If the top level is a C-style cast, and the subexpression is a valid
840 // lvalue, then this is probably a use of the old-school "cast as lvalue"
841 // GCC extension. We don't support it, but we want to produce good
842 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000843 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
844 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
845 if (Loc)
846 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +0000847 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000848 }
849 }
Chris Lattnerca354fa2008-11-17 19:51:54 +0000850 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000851 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000852 }
Eli Friedman04831aa2009-03-22 23:26:56 +0000853
854 // The following is illegal:
855 // void takeclosure(void (^C)(void));
856 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
857 //
Chris Lattner7fd09952009-03-23 17:57:53 +0000858 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +0000859 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
860 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
861 return MLV_NotBlockQualified;
862 }
863
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000864 QualType CT = Ctx.getCanonicalType(getType());
865
866 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000868 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000870 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 return MLV_IncompleteType;
872
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000873 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 if (r->hasConstFields())
875 return MLV_ConstQualified;
876 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000877
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000878 // Assigning to an 'implicit' property?
Chris Lattner7fd09952009-03-23 17:57:53 +0000879 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000880 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
881 if (KVCExpr->getSetterMethod() == 0)
882 return MLV_NoSetterProperty;
883 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 return MLV_Valid;
885}
886
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000887/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000888/// duration. This means that the address of this expression is a link-time
889/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000890bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000891 switch (getStmtClass()) {
892 default:
893 return false;
Steve Naroff3aaa4822009-04-16 19:02:57 +0000894 case BlockExprClass:
895 return true;
Chris Lattner4cc62712007-11-27 21:35:27 +0000896 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000897 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000898 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000899 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000900 case CompoundLiteralExprClass:
901 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000902 case DeclRefExprClass:
903 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000904 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
905 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000906 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000907 if (isa<FunctionDecl>(D))
908 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000909 return false;
910 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000911 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000912 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000913 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000914 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000915 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000916 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000917 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000918 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000919 case CXXDefaultArgExprClass:
920 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000921 }
922}
923
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000924/// isOBJCGCCandidate - Check if an expression is objc gc'able.
925///
926bool Expr::isOBJCGCCandidate() const {
927 switch (getStmtClass()) {
928 default:
929 return false;
930 case ObjCIvarRefExprClass:
931 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000932 case Expr::UnaryOperatorClass:
933 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000934 case ParenExprClass:
935 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
936 case ImplicitCastExprClass:
937 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian06b89122009-05-05 23:28:21 +0000938 case CStyleCastExprClass:
939 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000940 case DeclRefExprClass:
941 case QualifiedDeclRefExprClass: {
942 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
943 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
944 return VD->hasGlobalStorage();
945 return false;
946 }
947 case MemberExprClass: {
948 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian06b89122009-05-05 23:28:21 +0000949 return M->getBase()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000950 }
951 case ArraySubscriptExprClass:
952 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
953 }
954}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000955Expr* Expr::IgnoreParens() {
956 Expr* E = this;
957 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
958 E = P->getSubExpr();
959
960 return E;
961}
962
Chris Lattner56f34942008-02-13 01:02:39 +0000963/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
964/// or CastExprs or ImplicitCastExprs, returning their operand.
965Expr *Expr::IgnoreParenCasts() {
966 Expr *E = this;
967 while (true) {
968 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
969 E = P->getSubExpr();
970 else if (CastExpr *P = dyn_cast<CastExpr>(E))
971 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +0000972 else
973 return E;
974 }
975}
976
Chris Lattnerecdd8412009-03-13 17:28:01 +0000977/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
978/// value (including ptr->int casts of the same size). Strip off any
979/// ParenExpr or CastExprs, returning their operand.
980Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
981 Expr *E = this;
982 while (true) {
983 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
984 E = P->getSubExpr();
985 continue;
986 }
987
988 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
989 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
990 // ptr<->int casts of the same width. We also ignore all identify casts.
991 Expr *SE = P->getSubExpr();
992
993 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
994 E = SE;
995 continue;
996 }
997
998 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
999 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1000 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1001 E = SE;
1002 continue;
1003 }
1004 }
1005
1006 return E;
1007 }
1008}
1009
1010
Douglas Gregor898574e2008-12-05 23:32:09 +00001011/// hasAnyTypeDependentArguments - Determines if any of the expressions
1012/// in Exprs is type-dependent.
1013bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1014 for (unsigned I = 0; I < NumExprs; ++I)
1015 if (Exprs[I]->isTypeDependent())
1016 return true;
1017
1018 return false;
1019}
1020
1021/// hasAnyValueDependentArguments - Determines if any of the expressions
1022/// in Exprs is value-dependent.
1023bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1024 for (unsigned I = 0; I < NumExprs; ++I)
1025 if (Exprs[I]->isValueDependent())
1026 return true;
1027
1028 return false;
1029}
1030
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001031bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001032 // This function is attempting whether an expression is an initializer
1033 // which can be evaluated at compile-time. isEvaluatable handles most
1034 // of the cases, but it can't deal with some initializer-specific
1035 // expressions, and it can't deal with aggregates; we deal with those here,
1036 // and fall back to isEvaluatable for the other cases.
1037
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001038 // FIXME: This function assumes the variable being assigned to
1039 // isn't a reference type!
1040
Anders Carlssone8a32b82008-11-24 05:23:59 +00001041 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001042 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001043 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001044 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001045 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001046 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001047 // This handles gcc's extension that allows global initializers like
1048 // "struct x {int x;} x = (struct x) {};".
1049 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001050 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001051 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001052 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001053 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001054 // FIXME: This doesn't deal with fields with reference types correctly.
1055 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1056 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001057 const InitListExpr *Exp = cast<InitListExpr>(this);
1058 unsigned numInits = Exp->getNumInits();
1059 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001060 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001061 return false;
1062 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001063 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001064 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001065 case ImplicitValueInitExprClass:
1066 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001067 case ParenExprClass: {
1068 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1069 }
1070 case UnaryOperatorClass: {
1071 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1072 if (Exp->getOpcode() == UnaryOperator::Extension)
1073 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1074 break;
1075 }
Chris Lattner81045d82009-04-21 05:19:11 +00001076 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001077 case CStyleCastExprClass:
1078 // Handle casts with a destination that's a struct or union; this
1079 // deals with both the gcc no-op struct cast extension and the
1080 // cast-to-union extension.
1081 if (getType()->isRecordType())
1082 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1083 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001084 }
1085
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001086 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001087}
1088
Reid Spencer5f016e22007-07-11 17:01:13 +00001089/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001090/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001091
1092/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1093/// comma, etc
1094///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001095/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1096/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1097/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001098
Eli Friedmane28d7192009-02-26 09:29:13 +00001099// CheckICE - This function does the fundamental ICE checking: the returned
1100// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1101// Note that to reduce code duplication, this helper does no evaluation
1102// itself; the caller checks whether the expression is evaluatable, and
1103// in the rare cases where CheckICE actually cares about the evaluated
1104// value, it calls into Evalute.
1105//
1106// Meanings of Val:
1107// 0: This expression is an ICE if it can be evaluated by Evaluate.
1108// 1: This expression is not an ICE, but if it isn't evaluated, it's
1109// a legal subexpression for an ICE. This return value is used to handle
1110// the comma operator in C99 mode.
1111// 2: This expression is not an ICE, and is not a legal subexpression for one.
1112
1113struct ICEDiag {
1114 unsigned Val;
1115 SourceLocation Loc;
1116
1117 public:
1118 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1119 ICEDiag() : Val(0) {}
1120};
1121
1122ICEDiag NoDiag() { return ICEDiag(); }
1123
Eli Friedman60ce9632009-02-27 04:07:58 +00001124static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1125 Expr::EvalResult EVResult;
1126 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1127 !EVResult.Val.isInt()) {
1128 return ICEDiag(2, E->getLocStart());
1129 }
1130 return NoDiag();
1131}
1132
Eli Friedmane28d7192009-02-26 09:29:13 +00001133static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001134 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001135 if (!E->getType()->isIntegralType()) {
1136 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001137 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001138
1139 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001141 return ICEDiag(2, E->getLocStart());
1142 case Expr::ParenExprClass:
1143 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1144 case Expr::IntegerLiteralClass:
1145 case Expr::CharacterLiteralClass:
1146 case Expr::CXXBoolLiteralExprClass:
1147 case Expr::CXXZeroInitValueExprClass:
1148 case Expr::TypesCompatibleExprClass:
1149 case Expr::UnaryTypeTraitExprClass:
1150 return NoDiag();
1151 case Expr::CallExprClass:
1152 case Expr::CXXOperatorCallExprClass: {
1153 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001154 if (CE->isBuiltinCall(Ctx))
1155 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001156 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001157 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001158 case Expr::DeclRefExprClass:
1159 case Expr::QualifiedDeclRefExprClass:
1160 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1161 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001162 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedmane28d7192009-02-26 09:29:13 +00001163 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001164 // C++ 7.1.5.1p2
1165 // A variable of non-volatile const-qualified integral or enumeration
1166 // type initialized by an ICE can be used in ICEs.
1167 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001168 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001169 if (const Expr *Init = Dcl->getInit())
Eli Friedmane28d7192009-02-26 09:29:13 +00001170 return CheckICE(Init, Ctx);
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001171 }
1172 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001173 return ICEDiag(2, E->getLocStart());
1174 case Expr::UnaryOperatorClass: {
1175 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 switch (Exp->getOpcode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001178 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001180 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001184 case UnaryOperator::Real:
1185 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001186 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001187 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001188 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1189 // Evaluate matches the proposed gcc behavior for cases like
1190 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1191 // compliance: we should warn earlier for offsetof expressions with
1192 // array subscripts that aren't ICEs, and if the array subscripts
1193 // are ICEs, the value of the offsetof must be an integer constant.
1194 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001197 case Expr::SizeOfAlignOfExprClass: {
1198 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1199 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1200 return ICEDiag(2, E->getLocStart());
1201 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001203 case Expr::BinaryOperatorClass: {
1204 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 switch (Exp->getOpcode()) {
1206 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001207 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001211 case BinaryOperator::Add:
1212 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001215 case BinaryOperator::LT:
1216 case BinaryOperator::GT:
1217 case BinaryOperator::LE:
1218 case BinaryOperator::GE:
1219 case BinaryOperator::EQ:
1220 case BinaryOperator::NE:
1221 case BinaryOperator::And:
1222 case BinaryOperator::Xor:
1223 case BinaryOperator::Or:
1224 case BinaryOperator::Comma: {
1225 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1226 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001227 if (Exp->getOpcode() == BinaryOperator::Div ||
1228 Exp->getOpcode() == BinaryOperator::Rem) {
1229 // Evaluate gives an error for undefined Div/Rem, so make sure
1230 // we don't evaluate one.
1231 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1232 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1233 if (REval == 0)
1234 return ICEDiag(1, E->getLocStart());
1235 if (REval.isSigned() && REval.isAllOnesValue()) {
1236 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1237 if (LEval.isMinSignedValue())
1238 return ICEDiag(1, E->getLocStart());
1239 }
1240 }
1241 }
1242 if (Exp->getOpcode() == BinaryOperator::Comma) {
1243 if (Ctx.getLangOptions().C99) {
1244 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1245 // if it isn't evaluated.
1246 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1247 return ICEDiag(1, E->getLocStart());
1248 } else {
1249 // In both C89 and C++, commas in ICEs are illegal.
1250 return ICEDiag(2, E->getLocStart());
1251 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001252 }
1253 if (LHSResult.Val >= RHSResult.Val)
1254 return LHSResult;
1255 return RHSResult;
1256 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001258 case BinaryOperator::LOr: {
1259 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1260 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1261 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1262 // Rare case where the RHS has a comma "side-effect"; we need
1263 // to actually check the condition to see whether the side
1264 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001265 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001266 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001267 return RHSResult;
1268 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001269 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001270
Eli Friedmane28d7192009-02-26 09:29:13 +00001271 if (LHSResult.Val >= RHSResult.Val)
1272 return LHSResult;
1273 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001275 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001277 case Expr::ImplicitCastExprClass:
1278 case Expr::CStyleCastExprClass:
1279 case Expr::CXXFunctionalCastExprClass: {
1280 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1281 if (SubExpr->getType()->isIntegralType())
1282 return CheckICE(SubExpr, Ctx);
1283 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1284 return NoDiag();
1285 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001287 case Expr::ConditionalOperatorClass: {
1288 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner28daa532008-12-12 06:55:44 +00001289 // If the condition (ignoring parens) is a __builtin_constant_p call,
1290 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001291 // expression, and it is fully evaluated. This is an important GNU
1292 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001293 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001294 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001295 Expr::EvalResult EVResult;
1296 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1297 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001298 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001299 }
1300 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001301 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001302 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1303 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1304 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1305 if (CondResult.Val == 2)
1306 return CondResult;
1307 if (TrueResult.Val == 2)
1308 return TrueResult;
1309 if (FalseResult.Val == 2)
1310 return FalseResult;
1311 if (CondResult.Val == 1)
1312 return CondResult;
1313 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1314 return NoDiag();
1315 // Rare case where the diagnostics depend on which side is evaluated
1316 // Note that if we get here, CondResult is 0, and at least one of
1317 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001318 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001319 return FalseResult;
1320 }
1321 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001323 case Expr::CXXDefaultArgExprClass:
1324 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001325 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001326 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001327 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001329}
Reid Spencer5f016e22007-07-11 17:01:13 +00001330
Eli Friedmane28d7192009-02-26 09:29:13 +00001331bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1332 SourceLocation *Loc, bool isEvaluated) const {
1333 ICEDiag d = CheckICE(this, Ctx);
1334 if (d.Val != 0) {
1335 if (Loc) *Loc = d.Loc;
1336 return false;
1337 }
1338 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001339 if (!Evaluate(EvalResult, Ctx))
1340 assert(0 && "ICE cannot be evaluated!");
1341 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1342 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001343 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 return true;
1345}
1346
Reid Spencer5f016e22007-07-11 17:01:13 +00001347/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1348/// integer constant expression with the value zero, or if this is one that is
1349/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001350bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1351{
Sebastian Redl07779722008-10-31 14:43:28 +00001352 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001353 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001354 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001355 // Check that it is a cast to void*.
1356 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1357 QualType Pointee = PT->getPointeeType();
1358 if (Pointee.getCVRQualifiers() == 0 &&
1359 Pointee->isVoidType() && // to void*
1360 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001361 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001362 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001364 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1365 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001366 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001367 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1368 // Accept ((void*)0) as a null pointer constant, as many other
1369 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001370 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001371 } else if (const CXXDefaultArgExpr *DefaultArg
1372 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001373 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001374 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001375 } else if (isa<GNUNullExpr>(this)) {
1376 // The GNU __null extension is always a null pointer constant.
1377 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001378 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001379
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001380 // C++0x nullptr_t is always a null pointer constant.
1381 if (getType()->isNullPtrType())
1382 return true;
1383
Steve Naroffaa58f002008-01-14 16:10:57 +00001384 // This expression must be an integer type.
1385 if (!getType()->isIntegerType())
1386 return false;
1387
Reid Spencer5f016e22007-07-11 17:01:13 +00001388 // If we have an integer constant expression, we need to *evaluate* it and
1389 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001390 llvm::APSInt Result;
1391 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001392}
Steve Naroff31a45842007-07-28 23:10:27 +00001393
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001394FieldDecl *Expr::getBitField() {
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001395 Expr *E = this->IgnoreParenCasts();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001396
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001397 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001398 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001399 if (Field->isBitField())
1400 return Field;
1401
1402 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1403 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1404 return BinOp->getLHS()->getBitField();
1405
1406 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001407}
1408
Chris Lattner2140e902009-02-16 22:14:05 +00001409/// isArrow - Return true if the base expression is a pointer to vector,
1410/// return false if the base expression is a vector.
1411bool ExtVectorElementExpr::isArrow() const {
1412 return getBase()->getType()->isPointerType();
1413}
1414
Nate Begeman213541a2008-04-18 23:10:10 +00001415unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001416 if (const VectorType *VT = getType()->getAsVectorType())
1417 return VT->getNumElements();
1418 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001419}
1420
Nate Begeman8a997642008-05-09 06:41:27 +00001421/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001422bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001423 const char *compStr = Accessor->getName();
1424 unsigned length = Accessor->getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001425
1426 // Halving swizzles do not contain duplicate elements.
1427 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1428 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1429 return false;
1430
1431 // Advance past s-char prefix on hex swizzles.
1432 if (*compStr == 's') {
1433 compStr++;
1434 length--;
1435 }
Steve Narofffec0b492007-07-30 03:29:09 +00001436
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001437 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001438 const char *s = compStr+i;
1439 for (const char c = *s++; *s; s++)
1440 if (c == *s)
1441 return true;
1442 }
1443 return false;
1444}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001445
Nate Begeman8a997642008-05-09 06:41:27 +00001446/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001447void ExtVectorElementExpr::getEncodedElementAccess(
1448 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001449 const char *compStr = Accessor->getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001450 if (*compStr == 's')
1451 compStr++;
1452
1453 bool isHi = !strcmp(compStr, "hi");
1454 bool isLo = !strcmp(compStr, "lo");
1455 bool isEven = !strcmp(compStr, "even");
1456 bool isOdd = !strcmp(compStr, "odd");
1457
Nate Begeman8a997642008-05-09 06:41:27 +00001458 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1459 uint64_t Index;
1460
1461 if (isHi)
1462 Index = e + i;
1463 else if (isLo)
1464 Index = i;
1465 else if (isEven)
1466 Index = 2 * i;
1467 else if (isOdd)
1468 Index = 2 * i + 1;
1469 else
1470 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001471
Nate Begeman3b8d1162008-05-13 21:03:02 +00001472 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001473 }
Nate Begeman8a997642008-05-09 06:41:27 +00001474}
1475
Steve Naroff68d331a2007-09-27 14:38:14 +00001476// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001477ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001478 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001479 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001480 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001481 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001482 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001483 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001484 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001485 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001486 if (NumArgs) {
1487 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001488 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1489 }
Steve Naroff563477d2007-09-18 23:55:05 +00001490 LBracloc = LBrac;
1491 RBracloc = RBrac;
1492}
1493
Steve Naroff68d331a2007-09-27 14:38:14 +00001494// constructor for class messages.
1495// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001496ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001497 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001498 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001499 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001500 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001501 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001502 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001503 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001504 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001505 if (NumArgs) {
1506 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001507 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1508 }
Steve Naroff563477d2007-09-18 23:55:05 +00001509 LBracloc = LBrac;
1510 RBracloc = RBrac;
1511}
1512
Ted Kremenek4df728e2008-06-24 15:50:53 +00001513// constructor for class messages.
1514ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1515 QualType retType, ObjCMethodDecl *mproto,
1516 SourceLocation LBrac, SourceLocation RBrac,
1517 Expr **ArgExprs, unsigned nargs)
1518: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1519MethodProto(mproto) {
1520 NumArgs = nargs;
1521 SubExprs = new Stmt*[NumArgs+1];
1522 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1523 if (NumArgs) {
1524 for (unsigned i = 0; i != NumArgs; ++i)
1525 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1526 }
1527 LBracloc = LBrac;
1528 RBracloc = RBrac;
1529}
1530
1531ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1532 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1533 switch (x & Flags) {
1534 default:
1535 assert(false && "Invalid ObjCMessageExpr.");
1536 case IsInstMeth:
1537 return ClassInfo(0, 0);
1538 case IsClsMethDeclUnknown:
1539 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1540 case IsClsMethDeclKnown: {
1541 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1542 return ClassInfo(D, D->getIdentifier());
1543 }
1544 }
1545}
1546
Chris Lattner0389e6b2009-04-26 00:44:05 +00001547void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1548 if (CI.first == 0 && CI.second == 0)
1549 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1550 else if (CI.first == 0)
1551 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1552 else
1553 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1554}
1555
1556
Chris Lattner27437ca2007-10-25 00:29:32 +00001557bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00001558 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001559}
1560
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001561void ShuffleVectorExpr::setExprs(Expr ** Exprs, unsigned NumExprs) {
1562 if (NumExprs)
1563 delete [] SubExprs;
1564
1565 SubExprs = new Stmt* [NumExprs];
1566 this->NumExprs = NumExprs;
1567 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
1568}
1569
Sebastian Redl05189992008-11-11 17:56:53 +00001570void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1571 // Override default behavior of traversing children. If this has a type
1572 // operand and the type is a variable-length array, the child iteration
1573 // will iterate over the size expression. However, this expression belongs
1574 // to the type, not to this, so we don't want to delete it.
1575 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001576 if (isArgumentType()) {
1577 this->~SizeOfAlignOfExpr();
1578 C.Deallocate(this);
1579 }
Sebastian Redl05189992008-11-11 17:56:53 +00001580 else
1581 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001582}
1583
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001584//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001585// DesignatedInitExpr
1586//===----------------------------------------------------------------------===//
1587
1588IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1589 assert(Kind == FieldDesignator && "Only valid on a field designator");
1590 if (Field.NameOrField & 0x01)
1591 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1592 else
1593 return getField()->getIdentifier();
1594}
1595
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001596DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1597 const Designator *Designators,
1598 SourceLocation EqualOrColonLoc,
1599 bool GNUSyntax,
1600 unsigned NumSubExprs)
1601 : Expr(DesignatedInitExprClass, Ty),
1602 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1603 NumDesignators(NumDesignators), NumSubExprs(NumSubExprs) {
1604 this->Designators = new Designator[NumDesignators];
1605 for (unsigned I = 0; I != NumDesignators; ++I)
1606 this->Designators[I] = Designators[I];
1607}
1608
Douglas Gregor05c13a32009-01-22 00:58:24 +00001609DesignatedInitExpr *
1610DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1611 unsigned NumDesignators,
1612 Expr **IndexExprs, unsigned NumIndexExprs,
1613 SourceLocation ColonOrEqualLoc,
1614 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001615 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001616 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001617 DesignatedInitExpr *DIE
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001618 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00001619 ColonOrEqualLoc, UsesColonSyntax,
1620 NumIndexExprs + 1);
1621
1622 // Fill in the designators
1623 unsigned ExpectedNumSubExprs = 0;
1624 designators_iterator Desig = DIE->designators_begin();
1625 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001626 if (Designators[Idx].isArrayDesignator())
1627 ++ExpectedNumSubExprs;
1628 else if (Designators[Idx].isArrayRangeDesignator())
1629 ExpectedNumSubExprs += 2;
1630 }
1631 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1632
1633 // Fill in the subexpressions, including the initializer expression.
1634 child_iterator Child = DIE->child_begin();
1635 *Child++ = Init;
1636 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1637 *Child = IndexExprs[Idx];
1638
1639 return DIE;
1640}
1641
Douglas Gregord077d752009-04-16 00:55:48 +00001642DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1643 unsigned NumIndexExprs) {
1644 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1645 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1646 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1647}
1648
1649void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1650 unsigned NumDesigs) {
1651 if (Designators)
1652 delete [] Designators;
1653
1654 Designators = new Designator[NumDesigs];
1655 NumDesignators = NumDesigs;
1656 for (unsigned I = 0; I != NumDesigs; ++I)
1657 Designators[I] = Desigs[I];
1658}
1659
Douglas Gregor05c13a32009-01-22 00:58:24 +00001660SourceRange DesignatedInitExpr::getSourceRange() const {
1661 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001662 Designator &First =
1663 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001664 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001665 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001666 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1667 else
1668 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1669 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001670 StartLoc =
1671 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001672 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1673}
1674
Douglas Gregor05c13a32009-01-22 00:58:24 +00001675Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1676 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1677 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1678 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001679 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1680 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1681}
1682
1683Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1684 assert(D.Kind == Designator::ArrayRangeDesignator &&
1685 "Requires array range designator");
1686 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1687 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001688 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1689 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1690}
1691
1692Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1693 assert(D.Kind == Designator::ArrayRangeDesignator &&
1694 "Requires array range designator");
1695 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1696 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001697 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1698 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1699}
1700
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001701/// \brief Replaces the designator at index @p Idx with the series
1702/// of designators in [First, Last).
1703void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1704 const Designator *First,
1705 const Designator *Last) {
1706 unsigned NumNewDesignators = Last - First;
1707 if (NumNewDesignators == 0) {
1708 std::copy_backward(Designators + Idx + 1,
1709 Designators + NumDesignators,
1710 Designators + Idx);
1711 --NumNewDesignators;
1712 return;
1713 } else if (NumNewDesignators == 1) {
1714 Designators[Idx] = *First;
1715 return;
1716 }
1717
1718 Designator *NewDesignators
1719 = new Designator[NumDesignators - 1 + NumNewDesignators];
1720 std::copy(Designators, Designators + Idx, NewDesignators);
1721 std::copy(First, Last, NewDesignators + Idx);
1722 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1723 NewDesignators + Idx + NumNewDesignators);
1724 delete [] Designators;
1725 Designators = NewDesignators;
1726 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1727}
1728
1729void DesignatedInitExpr::Destroy(ASTContext &C) {
1730 delete [] Designators;
1731 Expr::Destroy(C);
1732}
1733
Douglas Gregor05c13a32009-01-22 00:58:24 +00001734//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001735// ExprIterator.
1736//===----------------------------------------------------------------------===//
1737
1738Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1739Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1740Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1741const Expr* ConstExprIterator::operator[](size_t idx) const {
1742 return cast<Expr>(I[idx]);
1743}
1744const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1745const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1746
1747//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001748// Child Iterators for iterating over subexpressions/substatements
1749//===----------------------------------------------------------------------===//
1750
1751// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001752Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1753Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001754
Steve Naroff7779db42007-11-12 14:29:37 +00001755// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001756Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1757Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001758
Steve Naroffe3e9add2008-06-02 23:03:37 +00001759// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001760Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1761Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001762
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001763// ObjCKVCRefExpr
1764Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1765Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1766
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001767// ObjCSuperExpr
1768Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1769Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1770
Chris Lattnerd9f69102008-08-10 01:53:14 +00001771// PredefinedExpr
1772Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1773Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001774
1775// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001776Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1777Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001778
1779// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001780Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001781Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001782
1783// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001784Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1785Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001786
Chris Lattner5d661452007-08-26 03:42:43 +00001787// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001788Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1789Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001790
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001791// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001792Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1793Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001794
1795// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001796Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1797Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001798
1799// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001800Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1801Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001802
Sebastian Redl05189992008-11-11 17:56:53 +00001803// SizeOfAlignOfExpr
1804Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1805 // If this is of a type and the type is a VLA type (and not a typedef), the
1806 // size expression of the VLA needs to be treated as an executable expression.
1807 // Why isn't this weirdness documented better in StmtIterator?
1808 if (isArgumentType()) {
1809 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1810 getArgumentType().getTypePtr()))
1811 return child_iterator(T);
1812 return child_iterator();
1813 }
Sebastian Redld4575892008-12-03 23:17:54 +00001814 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001815}
Sebastian Redl05189992008-11-11 17:56:53 +00001816Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1817 if (isArgumentType())
1818 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001819 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001820}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001821
1822// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001823Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001824 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001825}
Ted Kremenek1237c672007-08-24 20:06:47 +00001826Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001827 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001828}
1829
1830// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001831Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001832 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001833}
Ted Kremenek1237c672007-08-24 20:06:47 +00001834Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001835 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001836}
Ted Kremenek1237c672007-08-24 20:06:47 +00001837
1838// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001839Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1840Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001841
Nate Begeman213541a2008-04-18 23:10:10 +00001842// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001843Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1844Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001845
1846// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001847Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1848Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001849
Ted Kremenek1237c672007-08-24 20:06:47 +00001850// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001851Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1852Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001853
1854// BinaryOperator
1855Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001856 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001857}
Ted Kremenek1237c672007-08-24 20:06:47 +00001858Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001859 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001860}
1861
1862// ConditionalOperator
1863Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001864 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001865}
Ted Kremenek1237c672007-08-24 20:06:47 +00001866Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001867 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001868}
1869
1870// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001871Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1872Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001873
Ted Kremenek1237c672007-08-24 20:06:47 +00001874// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001875Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1876Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001877
1878// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001879Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1880 return child_iterator();
1881}
1882
1883Stmt::child_iterator TypesCompatibleExpr::child_end() {
1884 return child_iterator();
1885}
Ted Kremenek1237c672007-08-24 20:06:47 +00001886
1887// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001888Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1889Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001890
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001891// GNUNullExpr
1892Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1893Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1894
Eli Friedmand38617c2008-05-14 19:38:39 +00001895// ShuffleVectorExpr
1896Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001897 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001898}
1899Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001900 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001901}
1902
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001903// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001904Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1905Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001906
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001907// InitListExpr
1908Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001909 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001910}
1911Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001912 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001913}
1914
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001915// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001916Stmt::child_iterator DesignatedInitExpr::child_begin() {
1917 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1918 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001919 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1920}
1921Stmt::child_iterator DesignatedInitExpr::child_end() {
1922 return child_iterator(&*child_begin() + NumSubExprs);
1923}
1924
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001925// ImplicitValueInitExpr
1926Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1927 return child_iterator();
1928}
1929
1930Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1931 return child_iterator();
1932}
1933
Ted Kremenek1237c672007-08-24 20:06:47 +00001934// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001935Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001936 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001937}
1938Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001939 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001940}
Ted Kremenek1237c672007-08-24 20:06:47 +00001941
1942// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001943Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1944Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001945
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001946// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001947Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1948 return child_iterator();
1949}
1950Stmt::child_iterator ObjCSelectorExpr::child_end() {
1951 return child_iterator();
1952}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001953
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001954// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001955Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1956 return child_iterator();
1957}
1958Stmt::child_iterator ObjCProtocolExpr::child_end() {
1959 return child_iterator();
1960}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001961
Steve Naroff563477d2007-09-18 23:55:05 +00001962// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00001963Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001964 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00001965}
1966Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001967 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00001968}
1969
Steve Naroff4eb206b2008-09-03 18:15:37 +00001970// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00001971Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1972Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00001973
Ted Kremenek9da13f92008-09-26 23:24:14 +00001974Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1975Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }