blob: e2dd64a7036cd89e786d98c9286d2bdc7302aa68 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar64789f82008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Douglas Gregorcc94ab72009-04-15 06:41:24 +000023#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Primary Expressions.
28//===----------------------------------------------------------------------===//
29
Anders Carlsson7f2e7442009-03-15 18:34:13 +000030IntegerLiteral* IntegerLiteral::Clone(ASTContext &C) const {
31 return new (C) IntegerLiteral(Value, getType(), Loc);
32}
33
Chris Lattnere0391b22008-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 Johannesen2461f612008-10-09 23:02:32 +000039 bool ignored;
40 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
41 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000042 return V.convertToDouble();
43}
44
Chris Lattneraa491192009-02-18 06:40:38 +000045StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
46 unsigned ByteLength, bool Wide,
47 QualType Ty,
Anders Carlsson7f2e7442009-03-15 18:34:13 +000048 const SourceLocation *Loc,
49 unsigned NumStrs) {
Chris Lattneraa491192009-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
Chris Lattner4b009652007-07-25 00:24:17 +000057 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattneraa491192009-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;
Chris Lattner4b009652007-07-25 00:24:17 +000065
Chris Lattnerc3144742009-02-18 05:49:11 +000066 if (NumStrs != 1)
Chris Lattneraa491192009-02-18 06:40:38 +000067 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
68 return SL;
Chris Lattnerc3144742009-02-18 05:49:11 +000069}
70
Anders Carlsson7f2e7442009-03-15 18:34:13 +000071StringLiteral* StringLiteral::Clone(ASTContext &C) const {
72 return Create(C, StrData, ByteLength, IsWide, getType(),
73 TokLocs, NumConcatenated);
74}
Chris Lattnerc3144742009-02-18 05:49:11 +000075
Ted Kremenek4f530a92009-02-06 19:55:15 +000076void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +000077 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek32d760b2009-02-09 17:10:09 +000078 this->~StringLiteral();
79 C.Deallocate(this);
Chris Lattner4b009652007-07-25 00:24:17 +000080}
81
Chris Lattner4b009652007-07-25 00:24:17 +000082/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
83/// corresponds to, e.g. "sizeof" or "[pre]++".
84const char *UnaryOperator::getOpcodeStr(Opcode Op) {
85 switch (Op) {
86 default: assert(0 && "Unknown unary operator");
87 case PostInc: return "++";
88 case PostDec: return "--";
89 case PreInc: return "++";
90 case PreDec: return "--";
91 case AddrOf: return "&";
92 case Deref: return "*";
93 case Plus: return "+";
94 case Minus: return "-";
95 case Not: return "~";
96 case LNot: return "!";
97 case Real: return "__real";
98 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +000099 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000100 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +0000101 }
102}
103
Douglas Gregorc78182d2009-03-13 23:49:33 +0000104UnaryOperator::Opcode
105UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
106 switch (OO) {
Douglas Gregorc78182d2009-03-13 23:49:33 +0000107 default: assert(false && "No unary operator for overloaded function");
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000108 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
109 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
110 case OO_Amp: return AddrOf;
111 case OO_Star: return Deref;
112 case OO_Plus: return Plus;
113 case OO_Minus: return Minus;
114 case OO_Tilde: return Not;
115 case OO_Exclaim: return LNot;
Douglas Gregorc78182d2009-03-13 23:49:33 +0000116 }
117}
118
119OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
120 switch (Opc) {
121 case PostInc: case PreInc: return OO_PlusPlus;
122 case PostDec: case PreDec: return OO_MinusMinus;
123 case AddrOf: return OO_Amp;
124 case Deref: return OO_Star;
125 case Plus: return OO_Plus;
126 case Minus: return OO_Minus;
127 case Not: return OO_Tilde;
128 case LNot: return OO_Exclaim;
129 default: return OO_None;
130 }
131}
132
133
Chris Lattner4b009652007-07-25 00:24:17 +0000134//===----------------------------------------------------------------------===//
135// Postfix Operators.
136//===----------------------------------------------------------------------===//
137
Ted Kremenek362abcd2009-02-09 20:51:47 +0000138CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +0000139 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000140 : Expr(SC, t,
141 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000142 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000143 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000144
145 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000146 SubExprs[FN] = fn;
147 for (unsigned i = 0; i != numargs; ++i)
148 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000149
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000150 RParenLoc = rparenloc;
151}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000152
Ted Kremenek362abcd2009-02-09 20:51:47 +0000153CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
154 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000155 : Expr(CallExprClass, t,
156 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000157 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000158 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000159
160 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000161 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000162 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000163 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000164
Chris Lattner4b009652007-07-25 00:24:17 +0000165 RParenLoc = rparenloc;
166}
167
Ted Kremenek362abcd2009-02-09 20:51:47 +0000168void CallExpr::Destroy(ASTContext& C) {
169 DestroyChildren(C);
170 if (SubExprs) C.Deallocate(SubExprs);
171 this->~CallExpr();
172 C.Deallocate(this);
173}
174
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000175/// setNumArgs - This changes the number of arguments present in this call.
176/// Any orphaned expressions are deleted by this, and any new operands are set
177/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000178void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000179 // No change, just return.
180 if (NumArgs == getNumArgs()) return;
181
182 // If shrinking # arguments, just delete the extras and forgot them.
183 if (NumArgs < getNumArgs()) {
184 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000185 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000186 this->NumArgs = NumArgs;
187 return;
188 }
189
190 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000191 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000192 // Copy over args.
193 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
194 NewSubExprs[i] = SubExprs[i];
195 // Null out new args.
196 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
197 NewSubExprs[i] = 0;
198
Ted Kremenek0c97e042009-02-07 01:47:29 +0000199 delete [] SubExprs;
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000200 SubExprs = NewSubExprs;
201 this->NumArgs = NumArgs;
202}
203
Chris Lattnerc24915f2008-10-06 05:00:53 +0000204/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
205/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000206unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000207 // All simple function calls (e.g. func()) are implicitly cast to pointer to
208 // function. As a result, we try and obtain the DeclRefExpr from the
209 // ImplicitCastExpr.
210 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
211 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000212 return 0;
213
Steve Naroff44aec4c2008-01-31 01:07:12 +0000214 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
215 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000216 return 0;
217
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000218 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
219 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000220 return 0;
221
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000222 if (!FDecl->getIdentifier())
223 return 0;
224
Douglas Gregorb5af7382009-02-14 18:57:46 +0000225 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000226}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000227
Chris Lattnerc24915f2008-10-06 05:00:53 +0000228
Chris Lattner4b009652007-07-25 00:24:17 +0000229/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
230/// corresponds to, e.g. "<<=".
231const char *BinaryOperator::getOpcodeStr(Opcode Op) {
232 switch (Op) {
Douglas Gregor535f3122009-03-12 22:51:37 +0000233 case PtrMemD: return ".*";
234 case PtrMemI: return "->*";
Chris Lattner4b009652007-07-25 00:24:17 +0000235 case Mul: return "*";
236 case Div: return "/";
237 case Rem: return "%";
238 case Add: return "+";
239 case Sub: return "-";
240 case Shl: return "<<";
241 case Shr: return ">>";
242 case LT: return "<";
243 case GT: return ">";
244 case LE: return "<=";
245 case GE: return ">=";
246 case EQ: return "==";
247 case NE: return "!=";
248 case And: return "&";
249 case Xor: return "^";
250 case Or: return "|";
251 case LAnd: return "&&";
252 case LOr: return "||";
253 case Assign: return "=";
254 case MulAssign: return "*=";
255 case DivAssign: return "/=";
256 case RemAssign: return "%=";
257 case AddAssign: return "+=";
258 case SubAssign: return "-=";
259 case ShlAssign: return "<<=";
260 case ShrAssign: return ">>=";
261 case AndAssign: return "&=";
262 case XorAssign: return "^=";
263 case OrAssign: return "|=";
264 case Comma: return ",";
265 }
Douglas Gregor535f3122009-03-12 22:51:37 +0000266
267 return "";
Chris Lattner4b009652007-07-25 00:24:17 +0000268}
269
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000270BinaryOperator::Opcode
271BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
272 switch (OO) {
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000273 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000274 case OO_Plus: return Add;
275 case OO_Minus: return Sub;
276 case OO_Star: return Mul;
277 case OO_Slash: return Div;
278 case OO_Percent: return Rem;
279 case OO_Caret: return Xor;
280 case OO_Amp: return And;
281 case OO_Pipe: return Or;
282 case OO_Equal: return Assign;
283 case OO_Less: return LT;
284 case OO_Greater: return GT;
285 case OO_PlusEqual: return AddAssign;
286 case OO_MinusEqual: return SubAssign;
287 case OO_StarEqual: return MulAssign;
288 case OO_SlashEqual: return DivAssign;
289 case OO_PercentEqual: return RemAssign;
290 case OO_CaretEqual: return XorAssign;
291 case OO_AmpEqual: return AndAssign;
292 case OO_PipeEqual: return OrAssign;
293 case OO_LessLess: return Shl;
294 case OO_GreaterGreater: return Shr;
295 case OO_LessLessEqual: return ShlAssign;
296 case OO_GreaterGreaterEqual: return ShrAssign;
297 case OO_EqualEqual: return EQ;
298 case OO_ExclaimEqual: return NE;
299 case OO_LessEqual: return LE;
300 case OO_GreaterEqual: return GE;
301 case OO_AmpAmp: return LAnd;
302 case OO_PipePipe: return LOr;
303 case OO_Comma: return Comma;
304 case OO_ArrowStar: return PtrMemI;
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000305 }
306}
307
308OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
309 static const OverloadedOperatorKind OverOps[] = {
310 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
311 OO_Star, OO_Slash, OO_Percent,
312 OO_Plus, OO_Minus,
313 OO_LessLess, OO_GreaterGreater,
314 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
315 OO_EqualEqual, OO_ExclaimEqual,
316 OO_Amp,
317 OO_Caret,
318 OO_Pipe,
319 OO_AmpAmp,
320 OO_PipePipe,
321 OO_Equal, OO_StarEqual,
322 OO_SlashEqual, OO_PercentEqual,
323 OO_PlusEqual, OO_MinusEqual,
324 OO_LessLessEqual, OO_GreaterGreaterEqual,
325 OO_AmpEqual, OO_CaretEqual,
326 OO_PipeEqual,
327 OO_Comma
328 };
329 return OverOps[Opc];
330}
331
Anders Carlsson762b7c72007-08-31 04:56:16 +0000332InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000333 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000334 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000335 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000336 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000337 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000338
339 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000340}
Chris Lattner4b009652007-07-25 00:24:17 +0000341
Douglas Gregoree0792c2009-03-20 23:58:33 +0000342void InitListExpr::reserveInits(unsigned NumInits) {
343 if (NumInits > InitExprs.size())
344 InitExprs.reserve(NumInits);
345}
346
Douglas Gregorf603b472009-01-28 21:54:33 +0000347void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000348 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000349 Idx < LastIdx; ++Idx)
Douglas Gregor78e97132009-03-20 23:38:03 +0000350 InitExprs[Idx]->Destroy(Context);
Douglas Gregorf603b472009-01-28 21:54:33 +0000351 InitExprs.resize(NumInits, 0);
352}
353
354Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
355 if (Init >= InitExprs.size()) {
356 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
357 InitExprs.back() = expr;
358 return 0;
359 }
360
361 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
362 InitExprs[Init] = expr;
363 return Result;
364}
365
Steve Naroff6f373332008-09-04 15:31:07 +0000366/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000367///
368const FunctionType *BlockExpr::getFunctionType() const {
369 return getType()->getAsBlockPointerType()->
370 getPointeeType()->getAsFunctionType();
371}
372
Steve Naroff9ac456d2008-10-08 17:01:13 +0000373SourceLocation BlockExpr::getCaretLocation() const {
374 return TheBlock->getCaretLocation();
375}
376const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
377Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
378
379
Chris Lattner4b009652007-07-25 00:24:17 +0000380//===----------------------------------------------------------------------===//
381// Generic Expression Routines
382//===----------------------------------------------------------------------===//
383
Chris Lattnerd2c66552009-02-14 07:37:35 +0000384/// isUnusedResultAWarning - Return true if this immediate expression should
385/// be warned about if the result is unused. If so, fill in Loc and Ranges
386/// with location to warn on and the source range[s] to report with the
387/// warning.
388bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
389 SourceRange &R2) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000390 switch (getStmtClass()) {
391 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000392 Loc = getExprLoc();
393 R1 = getSourceRange();
394 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000395 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000396 return cast<ParenExpr>(this)->getSubExpr()->
397 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000398 case UnaryOperatorClass: {
399 const UnaryOperator *UO = cast<UnaryOperator>(this);
400
401 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000402 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000403 case UnaryOperator::PostInc:
404 case UnaryOperator::PostDec:
405 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000406 case UnaryOperator::PreDec: // ++/--
407 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000408 case UnaryOperator::Deref:
409 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000410 if (getType().isVolatileQualified())
411 return false;
412 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000413 case UnaryOperator::Real:
414 case UnaryOperator::Imag:
415 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000416 if (UO->getSubExpr()->getType().isVolatileQualified())
417 return false;
418 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000419 case UnaryOperator::Extension:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000420 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000421 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000422 Loc = UO->getOperatorLoc();
423 R1 = UO->getSubExpr()->getSourceRange();
424 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000425 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000426 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000427 const BinaryOperator *BO = cast<BinaryOperator>(this);
428 // Consider comma to have side effects if the LHS or RHS does.
429 if (BO->getOpcode() == BinaryOperator::Comma)
430 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
431 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000432
Chris Lattnerd2c66552009-02-14 07:37:35 +0000433 if (BO->isAssignmentOp())
434 return false;
435 Loc = BO->getOperatorLoc();
436 R1 = BO->getLHS()->getSourceRange();
437 R2 = BO->getRHS()->getSourceRange();
438 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000439 }
Chris Lattner06078d22007-08-25 02:00:02 +0000440 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000441 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000442
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000443 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000444 // The condition must be evaluated, but if either the LHS or RHS is a
445 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000446 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump399ad182009-02-27 03:16:57 +0000447 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000448 return true;
449 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000450 }
451
Chris Lattner4b009652007-07-25 00:24:17 +0000452 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000453 // If the base pointer or element is to a volatile pointer/field, accessing
454 // it is a side effect.
455 if (getType().isVolatileQualified())
456 return false;
457 Loc = cast<MemberExpr>(this)->getMemberLoc();
458 R1 = SourceRange(Loc, Loc);
459 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
460 return true;
461
Chris Lattner4b009652007-07-25 00:24:17 +0000462 case ArraySubscriptExprClass:
463 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000464 // it is a side effect.
465 if (getType().isVolatileQualified())
466 return false;
467 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
468 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
469 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
470 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000471
Chris Lattner4b009652007-07-25 00:24:17 +0000472 case CallExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000473 case CXXOperatorCallExprClass: {
474 // If this is a direct call, get the callee.
475 const CallExpr *CE = cast<CallExpr>(this);
476 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
477 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
478 // If the callee has attribute pure, const, or warn_unused_result, warn
479 // about it. void foo() { strlen("bar"); } should warn.
480 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
481 if (FD->getAttr<WarnUnusedResultAttr>() ||
482 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
483 Loc = CE->getCallee()->getLocStart();
484 R1 = CE->getCallee()->getSourceRange();
485
486 if (unsigned NumArgs = CE->getNumArgs())
487 R2 = SourceRange(CE->getArg(0)->getLocStart(),
488 CE->getArg(NumArgs-1)->getLocEnd());
489 return true;
490 }
491 }
492 return false;
493 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000494 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000495 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000496 case StmtExprClass: {
497 // Statement exprs don't logically have side effects themselves, but are
498 // sometimes used in macros in ways that give them a type that is unused.
499 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
500 // however, if the result of the stmt expr is dead, we don't want to emit a
501 // warning.
502 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
503 if (!CS->body_empty())
504 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000505 return E->isUnusedResultAWarning(Loc, R1, R2);
506
507 Loc = cast<StmtExpr>(this)->getLParenLoc();
508 R1 = getSourceRange();
509 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000510 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000511 case CStyleCastExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000512 // If this is a cast to void, check the operand. Otherwise, the result of
513 // the cast is unused.
514 if (getType()->isVoidType())
515 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
516 R1, R2);
517 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
518 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
519 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000520 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000521 // If this is a cast to void, check the operand. Otherwise, the result of
522 // the cast is unused.
523 if (getType()->isVoidType())
Chris Lattnerd2c66552009-02-14 07:37:35 +0000524 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
525 R1, R2);
526 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
527 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
528 return true;
529
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000530 case ImplicitCastExprClass:
531 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000532 return cast<ImplicitCastExpr>(this)
533 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000534
Chris Lattner3e254fb2008-04-08 04:40:51 +0000535 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000536 return cast<CXXDefaultArgExpr>(this)
537 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000538
539 case CXXNewExprClass:
540 // FIXME: In theory, there might be new expressions that don't have side
541 // effects (e.g. a placement new with an uninitialized POD).
542 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000543 return false;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000544 }
Chris Lattner4b009652007-07-25 00:24:17 +0000545}
546
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000547/// DeclCanBeLvalue - Determine whether the given declaration can be
548/// an lvalue. This is a helper routine for isLvalue.
549static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000550 // C++ [temp.param]p6:
551 // A non-type non-reference template-parameter is not an lvalue.
552 if (const NonTypeTemplateParmDecl *NTTParm
553 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
554 return NTTParm->getType()->isReferenceType();
555
Douglas Gregor8acb7272008-12-11 16:49:14 +0000556 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000557 // C++ 3.10p2: An lvalue refers to an object or function.
558 (Ctx.getLangOptions().CPlusPlus &&
559 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
560}
561
Chris Lattner4b009652007-07-25 00:24:17 +0000562/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
563/// incomplete type other than void. Nonarray expressions that can be lvalues:
564/// - name, where name must be a variable
565/// - e[i]
566/// - (e), where e must be an lvalue
567/// - e.name, where e must be an lvalue
568/// - e->name
569/// - *e, the type of e cannot be a function type
570/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000571/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000572/// - reference type [C++ [expr]]
573///
Chris Lattner25168a52008-07-26 21:30:36 +0000574Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000575 // first, check the type (C99 6.3.2.1). Expressions with function
576 // type in C are not lvalues, but they can be lvalues in C++.
577 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000578 return LV_NotObjectType;
579
Steve Naroffec7736d2008-02-10 01:39:04 +0000580 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000581 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000582 return LV_IncompleteVoidType;
583
Sebastian Redlce6fff02009-03-16 23:22:08 +0000584 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
Chris Lattner4b009652007-07-25 00:24:17 +0000585
586 // the type looks fine, now check the expression
587 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000588 case StringLiteralClass: // C99 6.5.1p4
589 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000590 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000591 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
592 // For vectors, make sure base is an lvalue (i.e. not a function call).
593 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000594 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000595 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000596 case DeclRefExprClass:
597 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000598 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
599 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000600 return LV_Valid;
601 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000602 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000603 case BlockDeclRefExprClass: {
604 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000605 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000606 return LV_Valid;
607 break;
608 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000609 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000610 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000611 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
612 NamedDecl *Member = m->getMemberDecl();
613 // C++ [expr.ref]p4:
614 // If E2 is declared to have type "reference to T", then E1.E2
615 // is an lvalue.
616 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
617 if (Value->getType()->isReferenceType())
618 return LV_Valid;
619
620 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor00660582009-03-11 20:22:50 +0000621 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor82d44772008-12-20 23:49:58 +0000622 return LV_Valid;
623
624 // -- If E2 is a non-static data member [...]. If E1 is an
625 // lvalue, then E1.E2 is an lvalue.
626 if (isa<FieldDecl>(Member))
627 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
628
629 // -- If it refers to a static member function [...], then
630 // E1.E2 is an lvalue.
631 // -- Otherwise, if E1.E2 refers to a non-static member
632 // function [...], then E1.E2 is not an lvalue.
633 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
634 return Method->isStatic()? LV_Valid : LV_MemberFunction;
635
636 // -- If E2 is a member enumerator [...], the expression E1.E2
637 // is not an lvalue.
638 if (isa<EnumConstantDecl>(Member))
639 return LV_InvalidExpression;
640
641 // Not an lvalue.
642 return LV_InvalidExpression;
643 }
644
645 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000646 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000647 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000648 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000649 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000650 return LV_Valid; // C99 6.5.3p4
651
652 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000653 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
654 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000655 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000656
657 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
658 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
659 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
660 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000661 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000662 case ImplicitCastExprClass:
663 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
664 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000665 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000666 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000667 case BinaryOperatorClass:
668 case CompoundAssignOperatorClass: {
669 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000670
671 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
672 BinOp->getOpcode() == BinaryOperator::Comma)
673 return BinOp->getRHS()->isLvalue(Ctx);
674
Sebastian Redl95216a62009-02-07 00:15:38 +0000675 // C++ [expr.mptr.oper]p6
676 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
677 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
678 !BinOp->getType()->isFunctionType())
679 return BinOp->getLHS()->isLvalue(Ctx);
680
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000681 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000682 return LV_InvalidExpression;
683
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000684 if (Ctx.getLangOptions().CPlusPlus)
685 // C++ [expr.ass]p1:
686 // The result of an assignment operation [...] is an lvalue.
687 return LV_Valid;
688
689
690 // C99 6.5.16:
691 // An assignment expression [...] is not an lvalue.
692 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000693 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000694 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000695 case CXXOperatorCallExprClass:
696 case CXXMemberCallExprClass: {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000697 // C++0x [expr.call]p10
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000698 // A function call is an lvalue if and only if the result type
Sebastian Redlce6fff02009-03-16 23:22:08 +0000699 // is an lvalue reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000700 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000701 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000702 CalleeType = FnTypePtr->getPointeeType();
703 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
Sebastian Redlce6fff02009-03-16 23:22:08 +0000704 if (FnType->getResultType()->isLValueReferenceType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000705 return LV_Valid;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000706
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000707 break;
708 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000709 case CompoundLiteralExprClass: // C99 6.5.2.5p5
710 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000711 case ChooseExprClass:
712 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000713 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000714 case ExtVectorElementExprClass:
715 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000716 return LV_DuplicateVectorComponents;
717 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000718 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
719 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000720 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
721 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000722 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000723 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000724 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000725 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000726 case VAArgExprClass:
Daniel Dunbar09a82e32009-02-12 09:21:08 +0000727 return LV_NotObjectType;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000728 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000729 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000730 case CXXConditionDeclExprClass:
731 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000732 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000733 case CXXFunctionalCastExprClass:
734 case CXXStaticCastExprClass:
735 case CXXDynamicCastExprClass:
736 case CXXReinterpretCastExprClass:
737 case CXXConstCastExprClass:
738 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redlce6fff02009-03-16 23:22:08 +0000739 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000740 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
741 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000742 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
743 isLValueReferenceType())
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000744 return LV_Valid;
745 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000746 case CXXTypeidExprClass:
747 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
748 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000749 default:
750 break;
751 }
752 return LV_InvalidExpression;
753}
754
755/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
756/// does not have an incomplete type, does not have a const-qualified type, and
757/// if it is a structure or union, does not have any member (including,
758/// recursively, any member or element of all contained aggregates or unions)
759/// with a const-qualified type.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000760Expr::isModifiableLvalueResult
761Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner25168a52008-07-26 21:30:36 +0000762 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000763
764 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000765 case LV_Valid:
766 // C++ 3.10p11: Functions cannot be modified, but pointers to
767 // functions can be modifiable.
768 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
769 return MLV_NotObjectType;
770 break;
771
Chris Lattner4b009652007-07-25 00:24:17 +0000772 case LV_NotObjectType: return MLV_NotObjectType;
773 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000774 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000775 case LV_InvalidExpression:
776 // If the top level is a C-style cast, and the subexpression is a valid
777 // lvalue, then this is probably a use of the old-school "cast as lvalue"
778 // GCC extension. We don't support it, but we want to produce good
779 // diagnostics when it happens so that the user knows why.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000780 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
781 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
782 if (Loc)
783 *Loc = CE->getLParenLoc();
Chris Lattner37fb9402008-11-17 19:51:54 +0000784 return MLV_LValueCast;
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000785 }
786 }
Chris Lattner37fb9402008-11-17 19:51:54 +0000787 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000788 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000789 }
Eli Friedman91571da2009-03-22 23:26:56 +0000790
791 // The following is illegal:
792 // void takeclosure(void (^C)(void));
793 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
794 //
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000795 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman91571da2009-03-22 23:26:56 +0000796 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
797 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
798 return MLV_NotBlockQualified;
799 }
800
Chris Lattnera1923f62008-08-04 07:31:14 +0000801 QualType CT = Ctx.getCanonicalType(getType());
802
803 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000804 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000805 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000806 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000807 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000808 return MLV_IncompleteType;
809
Chris Lattnera1923f62008-08-04 07:31:14 +0000810 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000811 if (r->hasConstFields())
812 return MLV_ConstQualified;
813 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000814
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000815 // Assigning to an 'implicit' property?
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000816 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000817 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
818 if (KVCExpr->getSetterMethod() == 0)
819 return MLV_NoSetterProperty;
820 }
Chris Lattner4b009652007-07-25 00:24:17 +0000821 return MLV_Valid;
822}
823
Ted Kremenek5778d622008-02-27 18:39:48 +0000824/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000825/// duration. This means that the address of this expression is a link-time
826/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000827bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000828 switch (getStmtClass()) {
829 default:
830 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000831 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000832 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000833 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000834 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000835 case CompoundLiteralExprClass:
836 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000837 case DeclRefExprClass:
838 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000839 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
840 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000841 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000842 if (isa<FunctionDecl>(D))
843 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000844 return false;
845 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000846 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000847 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000848 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000849 }
Chris Lattner743ec372007-11-27 21:35:27 +0000850 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000851 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000852 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000853 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000854 case CXXDefaultArgExprClass:
855 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000856 }
857}
858
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000859/// isOBJCGCCandidate - Check if an expression is objc gc'able.
860///
861bool Expr::isOBJCGCCandidate() const {
862 switch (getStmtClass()) {
863 default:
864 return false;
865 case ObjCIvarRefExprClass:
866 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000867 case Expr::UnaryOperatorClass:
868 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000869 case ParenExprClass:
870 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
871 case ImplicitCastExprClass:
872 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
873 case DeclRefExprClass:
874 case QualifiedDeclRefExprClass: {
875 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
876 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
877 return VD->hasGlobalStorage();
878 return false;
879 }
880 case MemberExprClass: {
881 const MemberExpr *M = cast<MemberExpr>(this);
882 return !M->isArrow() && M->getBase()->isOBJCGCCandidate();
883 }
884 case ArraySubscriptExprClass:
885 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
886 }
887}
Ted Kremenek87e30c52008-01-17 16:57:34 +0000888Expr* Expr::IgnoreParens() {
889 Expr* E = this;
890 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
891 E = P->getSubExpr();
892
893 return E;
894}
895
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000896/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
897/// or CastExprs or ImplicitCastExprs, returning their operand.
898Expr *Expr::IgnoreParenCasts() {
899 Expr *E = this;
900 while (true) {
901 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
902 E = P->getSubExpr();
903 else if (CastExpr *P = dyn_cast<CastExpr>(E))
904 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000905 else
906 return E;
907 }
908}
909
Chris Lattnerab0b8b12009-03-13 17:28:01 +0000910/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
911/// value (including ptr->int casts of the same size). Strip off any
912/// ParenExpr or CastExprs, returning their operand.
913Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
914 Expr *E = this;
915 while (true) {
916 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
917 E = P->getSubExpr();
918 continue;
919 }
920
921 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
922 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
923 // ptr<->int casts of the same width. We also ignore all identify casts.
924 Expr *SE = P->getSubExpr();
925
926 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
927 E = SE;
928 continue;
929 }
930
931 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
932 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
933 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
934 E = SE;
935 continue;
936 }
937 }
938
939 return E;
940 }
941}
942
943
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000944/// hasAnyTypeDependentArguments - Determines if any of the expressions
945/// in Exprs is type-dependent.
946bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
947 for (unsigned I = 0; I < NumExprs; ++I)
948 if (Exprs[I]->isTypeDependent())
949 return true;
950
951 return false;
952}
953
954/// hasAnyValueDependentArguments - Determines if any of the expressions
955/// in Exprs is value-dependent.
956bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
957 for (unsigned I = 0; I < NumExprs; ++I)
958 if (Exprs[I]->isValueDependent())
959 return true;
960
961 return false;
962}
963
Eli Friedmandee41122009-01-25 02:32:41 +0000964bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000965 // This function is attempting whether an expression is an initializer
966 // which can be evaluated at compile-time. isEvaluatable handles most
967 // of the cases, but it can't deal with some initializer-specific
968 // expressions, and it can't deal with aggregates; we deal with those here,
969 // and fall back to isEvaluatable for the other cases.
970
Eli Friedman3dc50ed2009-02-20 02:36:22 +0000971 // FIXME: This function assumes the variable being assigned to
972 // isn't a reference type!
973
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000974 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000975 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000976 case StringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +0000977 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000978 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +0000979 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +0000980 // This handles gcc's extension that allows global initializers like
981 // "struct x {int x;} x = (struct x) {};".
982 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +0000983 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +0000984 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +0000985 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000986 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +0000987 // FIXME: This doesn't deal with fields with reference types correctly.
988 // FIXME: This incorrectly allows pointers cast to integers to be assigned
989 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000990 const InitListExpr *Exp = cast<InitListExpr>(this);
991 unsigned numInits = Exp->getNumInits();
992 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +0000993 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000994 return false;
995 }
Eli Friedman2b0dec52009-01-25 03:12:18 +0000996 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000997 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000998 case ImplicitValueInitExprClass:
999 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +00001000 case ParenExprClass: {
1001 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1002 }
1003 case UnaryOperatorClass: {
1004 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1005 if (Exp->getOpcode() == UnaryOperator::Extension)
1006 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1007 break;
1008 }
1009 case CStyleCastExprClass:
1010 // Handle casts with a destination that's a struct or union; this
1011 // deals with both the gcc no-op struct cast extension and the
1012 // cast-to-union extension.
1013 if (getType()->isRecordType())
1014 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1015 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001016 }
1017
Eli Friedman2b0dec52009-01-25 03:12:18 +00001018 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +00001019}
1020
Chris Lattner4b009652007-07-25 00:24:17 +00001021/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +00001022/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001023
1024/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1025/// comma, etc
1026///
Chris Lattner4b009652007-07-25 00:24:17 +00001027/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1028/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1029/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001030
Eli Friedman7beeda62009-02-26 09:29:13 +00001031// CheckICE - This function does the fundamental ICE checking: the returned
1032// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1033// Note that to reduce code duplication, this helper does no evaluation
1034// itself; the caller checks whether the expression is evaluatable, and
1035// in the rare cases where CheckICE actually cares about the evaluated
1036// value, it calls into Evalute.
1037//
1038// Meanings of Val:
1039// 0: This expression is an ICE if it can be evaluated by Evaluate.
1040// 1: This expression is not an ICE, but if it isn't evaluated, it's
1041// a legal subexpression for an ICE. This return value is used to handle
1042// the comma operator in C99 mode.
1043// 2: This expression is not an ICE, and is not a legal subexpression for one.
1044
1045struct ICEDiag {
1046 unsigned Val;
1047 SourceLocation Loc;
1048
1049 public:
1050 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1051 ICEDiag() : Val(0) {}
1052};
1053
1054ICEDiag NoDiag() { return ICEDiag(); }
1055
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001056static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1057 Expr::EvalResult EVResult;
1058 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1059 !EVResult.Val.isInt()) {
1060 return ICEDiag(2, E->getLocStart());
1061 }
1062 return NoDiag();
1063}
1064
Eli Friedman7beeda62009-02-26 09:29:13 +00001065static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson8b842c52009-03-14 00:33:21 +00001066 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001067 if (!E->getType()->isIntegralType()) {
1068 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +00001069 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001070
1071 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001072 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001073 return ICEDiag(2, E->getLocStart());
1074 case Expr::ParenExprClass:
1075 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1076 case Expr::IntegerLiteralClass:
1077 case Expr::CharacterLiteralClass:
1078 case Expr::CXXBoolLiteralExprClass:
1079 case Expr::CXXZeroInitValueExprClass:
1080 case Expr::TypesCompatibleExprClass:
1081 case Expr::UnaryTypeTraitExprClass:
1082 return NoDiag();
1083 case Expr::CallExprClass:
1084 case Expr::CXXOperatorCallExprClass: {
1085 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001086 if (CE->isBuiltinCall(Ctx))
1087 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +00001088 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001089 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001090 case Expr::DeclRefExprClass:
1091 case Expr::QualifiedDeclRefExprClass:
1092 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1093 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001094 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +00001095 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001096 // C++ 7.1.5.1p2
1097 // A variable of non-volatile const-qualified integral or enumeration
1098 // type initialized by an ICE can be used in ICEs.
1099 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +00001100 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001101 if (const Expr *Init = Dcl->getInit())
Eli Friedman7beeda62009-02-26 09:29:13 +00001102 return CheckICE(Init, Ctx);
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001103 }
1104 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001105 return ICEDiag(2, E->getLocStart());
1106 case Expr::UnaryOperatorClass: {
1107 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001108 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001109 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001110 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001111 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +00001112 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +00001113 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +00001114 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +00001115 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001116 case UnaryOperator::Real:
1117 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +00001118 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001119 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001120 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1121 // Evaluate matches the proposed gcc behavior for cases like
1122 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1123 // compliance: we should warn earlier for offsetof expressions with
1124 // array subscripts that aren't ICEs, and if the array subscripts
1125 // are ICEs, the value of the offsetof must be an integer constant.
1126 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001127 }
Chris Lattner4b009652007-07-25 00:24:17 +00001128 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001129 case Expr::SizeOfAlignOfExprClass: {
1130 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1131 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1132 return ICEDiag(2, E->getLocStart());
1133 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +00001134 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001135 case Expr::BinaryOperatorClass: {
1136 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001137 switch (Exp->getOpcode()) {
1138 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001139 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001140 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +00001141 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +00001142 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +00001143 case BinaryOperator::Add:
1144 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001145 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001146 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001147 case BinaryOperator::LT:
1148 case BinaryOperator::GT:
1149 case BinaryOperator::LE:
1150 case BinaryOperator::GE:
1151 case BinaryOperator::EQ:
1152 case BinaryOperator::NE:
1153 case BinaryOperator::And:
1154 case BinaryOperator::Xor:
1155 case BinaryOperator::Or:
1156 case BinaryOperator::Comma: {
1157 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1158 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001159 if (Exp->getOpcode() == BinaryOperator::Div ||
1160 Exp->getOpcode() == BinaryOperator::Rem) {
1161 // Evaluate gives an error for undefined Div/Rem, so make sure
1162 // we don't evaluate one.
1163 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1164 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1165 if (REval == 0)
1166 return ICEDiag(1, E->getLocStart());
1167 if (REval.isSigned() && REval.isAllOnesValue()) {
1168 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1169 if (LEval.isMinSignedValue())
1170 return ICEDiag(1, E->getLocStart());
1171 }
1172 }
1173 }
1174 if (Exp->getOpcode() == BinaryOperator::Comma) {
1175 if (Ctx.getLangOptions().C99) {
1176 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1177 // if it isn't evaluated.
1178 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1179 return ICEDiag(1, E->getLocStart());
1180 } else {
1181 // In both C89 and C++, commas in ICEs are illegal.
1182 return ICEDiag(2, E->getLocStart());
1183 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001184 }
1185 if (LHSResult.Val >= RHSResult.Val)
1186 return LHSResult;
1187 return RHSResult;
1188 }
Chris Lattner4b009652007-07-25 00:24:17 +00001189 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001190 case BinaryOperator::LOr: {
1191 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1192 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1193 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1194 // Rare case where the RHS has a comma "side-effect"; we need
1195 // to actually check the condition to see whether the side
1196 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001197 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001198 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001199 return RHSResult;
1200 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001201 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001202
Eli Friedman7beeda62009-02-26 09:29:13 +00001203 if (LHSResult.Val >= RHSResult.Val)
1204 return LHSResult;
1205 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001206 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001207 }
Chris Lattner4b009652007-07-25 00:24:17 +00001208 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001209 case Expr::ImplicitCastExprClass:
1210 case Expr::CStyleCastExprClass:
1211 case Expr::CXXFunctionalCastExprClass: {
1212 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1213 if (SubExpr->getType()->isIntegralType())
1214 return CheckICE(SubExpr, Ctx);
1215 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1216 return NoDiag();
1217 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001218 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001219 case Expr::ConditionalOperatorClass: {
1220 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001221 // If the condition (ignoring parens) is a __builtin_constant_p call,
1222 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001223 // expression, and it is fully evaluated. This is an important GNU
1224 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001225 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001226 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001227 Expr::EvalResult EVResult;
1228 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1229 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001230 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001231 }
1232 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001233 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001234 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1235 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1236 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1237 if (CondResult.Val == 2)
1238 return CondResult;
1239 if (TrueResult.Val == 2)
1240 return TrueResult;
1241 if (FalseResult.Val == 2)
1242 return FalseResult;
1243 if (CondResult.Val == 1)
1244 return CondResult;
1245 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1246 return NoDiag();
1247 // Rare case where the diagnostics depend on which side is evaluated
1248 // Note that if we get here, CondResult is 0, and at least one of
1249 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001250 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001251 return FalseResult;
1252 }
1253 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001254 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001255 case Expr::CXXDefaultArgExprClass:
1256 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001257 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001258 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001259 }
Chris Lattner4b009652007-07-25 00:24:17 +00001260 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001261}
Chris Lattner4b009652007-07-25 00:24:17 +00001262
Eli Friedman7beeda62009-02-26 09:29:13 +00001263bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1264 SourceLocation *Loc, bool isEvaluated) const {
1265 ICEDiag d = CheckICE(this, Ctx);
1266 if (d.Val != 0) {
1267 if (Loc) *Loc = d.Loc;
1268 return false;
1269 }
1270 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001271 if (!Evaluate(EvalResult, Ctx))
1272 assert(0 && "ICE cannot be evaluated!");
1273 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1274 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001275 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001276 return true;
1277}
1278
Chris Lattner4b009652007-07-25 00:24:17 +00001279/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1280/// integer constant expression with the value zero, or if this is one that is
1281/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001282bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1283{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001284 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001285 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001286 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001287 // Check that it is a cast to void*.
1288 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1289 QualType Pointee = PT->getPointeeType();
1290 if (Pointee.getCVRQualifiers() == 0 &&
1291 Pointee->isVoidType() && // to void*
1292 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001293 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001294 }
Chris Lattner4b009652007-07-25 00:24:17 +00001295 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001296 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1297 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001298 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001299 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1300 // Accept ((void*)0) as a null pointer constant, as many other
1301 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001302 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001303 } else if (const CXXDefaultArgExpr *DefaultArg
1304 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001305 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001306 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001307 } else if (isa<GNUNullExpr>(this)) {
1308 // The GNU __null extension is always a null pointer constant.
1309 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001310 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001311
Steve Naroffa2e53222008-01-14 16:10:57 +00001312 // This expression must be an integer type.
1313 if (!getType()->isIntegerType())
1314 return false;
1315
Chris Lattner4b009652007-07-25 00:24:17 +00001316 // If we have an integer constant expression, we need to *evaluate* it and
1317 // test for the value 0.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001318 // FIXME: We should probably return false if we're compiling in strict mode
1319 // and Diag is not null (this indicates that the value was foldable but not
1320 // an ICE.
1321 EvalResult Result;
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001322 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001323 Result.Val.isInt() && Result.Val.getInt() == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001324}
Steve Naroffc11705f2007-07-28 23:10:27 +00001325
Douglas Gregor81c29152008-10-29 00:13:59 +00001326/// isBitField - Return true if this expression is a bit-field.
1327bool Expr::isBitField() {
1328 Expr *E = this->IgnoreParenCasts();
1329 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001330 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1331 return Field->isBitField();
Douglas Gregor81c29152008-10-29 00:13:59 +00001332 return false;
1333}
1334
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001335/// isArrow - Return true if the base expression is a pointer to vector,
1336/// return false if the base expression is a vector.
1337bool ExtVectorElementExpr::isArrow() const {
1338 return getBase()->getType()->isPointerType();
1339}
1340
Nate Begemanaf6ed502008-04-18 23:10:10 +00001341unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001342 if (const VectorType *VT = getType()->getAsVectorType())
1343 return VT->getNumElements();
1344 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001345}
1346
Nate Begemanc8e51f82008-05-09 06:41:27 +00001347/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001348bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001349 const char *compStr = Accessor.getName();
Chris Lattner58d3fa52008-11-19 07:55:04 +00001350 unsigned length = Accessor.getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001351
1352 // Halving swizzles do not contain duplicate elements.
1353 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1354 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1355 return false;
1356
1357 // Advance past s-char prefix on hex swizzles.
1358 if (*compStr == 's') {
1359 compStr++;
1360 length--;
1361 }
Steve Naroffba67f692007-07-30 03:29:09 +00001362
Chris Lattner58d3fa52008-11-19 07:55:04 +00001363 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001364 const char *s = compStr+i;
1365 for (const char c = *s++; *s; s++)
1366 if (c == *s)
1367 return true;
1368 }
1369 return false;
1370}
Chris Lattner42158e72007-08-02 23:36:59 +00001371
Nate Begemanc8e51f82008-05-09 06:41:27 +00001372/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001373void ExtVectorElementExpr::getEncodedElementAccess(
1374 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner58d3fa52008-11-19 07:55:04 +00001375 const char *compStr = Accessor.getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001376 if (*compStr == 's')
1377 compStr++;
1378
1379 bool isHi = !strcmp(compStr, "hi");
1380 bool isLo = !strcmp(compStr, "lo");
1381 bool isEven = !strcmp(compStr, "even");
1382 bool isOdd = !strcmp(compStr, "odd");
1383
Nate Begemanc8e51f82008-05-09 06:41:27 +00001384 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1385 uint64_t Index;
1386
1387 if (isHi)
1388 Index = e + i;
1389 else if (isLo)
1390 Index = i;
1391 else if (isEven)
1392 Index = 2 * i;
1393 else if (isOdd)
1394 Index = 2 * i + 1;
1395 else
1396 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001397
Nate Begemana1ae7442008-05-13 21:03:02 +00001398 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001399 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001400}
1401
Steve Naroff4ed9d662007-09-27 14:38:14 +00001402// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001403ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001404 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001405 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001406 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001407 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001408 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001409 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001410 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001411 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001412 if (NumArgs) {
1413 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001414 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1415 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001416 LBracloc = LBrac;
1417 RBracloc = RBrac;
1418}
1419
Steve Naroff4ed9d662007-09-27 14:38:14 +00001420// constructor for class messages.
1421// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001422ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001423 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001424 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001425 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001426 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001427 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001428 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001429 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001430 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001431 if (NumArgs) {
1432 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001433 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1434 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001435 LBracloc = LBrac;
1436 RBracloc = RBrac;
1437}
1438
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001439// constructor for class messages.
1440ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1441 QualType retType, ObjCMethodDecl *mproto,
1442 SourceLocation LBrac, SourceLocation RBrac,
1443 Expr **ArgExprs, unsigned nargs)
1444: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1445MethodProto(mproto) {
1446 NumArgs = nargs;
1447 SubExprs = new Stmt*[NumArgs+1];
1448 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1449 if (NumArgs) {
1450 for (unsigned i = 0; i != NumArgs; ++i)
1451 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1452 }
1453 LBracloc = LBrac;
1454 RBracloc = RBrac;
1455}
1456
1457ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1458 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1459 switch (x & Flags) {
1460 default:
1461 assert(false && "Invalid ObjCMessageExpr.");
1462 case IsInstMeth:
1463 return ClassInfo(0, 0);
1464 case IsClsMethDeclUnknown:
1465 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1466 case IsClsMethDeclKnown: {
1467 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1468 return ClassInfo(D, D->getIdentifier());
1469 }
1470 }
1471}
1472
Chris Lattnerf624cd22007-10-25 00:29:32 +00001473bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001474 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001475}
1476
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001477void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1478 // Override default behavior of traversing children. If this has a type
1479 // operand and the type is a variable-length array, the child iteration
1480 // will iterate over the size expression. However, this expression belongs
1481 // to the type, not to this, so we don't want to delete it.
1482 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001483 if (isArgumentType()) {
1484 this->~SizeOfAlignOfExpr();
1485 C.Deallocate(this);
1486 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001487 else
1488 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001489}
1490
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001491//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001492// DesignatedInitExpr
1493//===----------------------------------------------------------------------===//
1494
1495IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1496 assert(Kind == FieldDesignator && "Only valid on a field designator");
1497 if (Field.NameOrField & 0x01)
1498 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1499 else
1500 return getField()->getIdentifier();
1501}
1502
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001503DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1504 const Designator *Designators,
1505 SourceLocation EqualOrColonLoc,
1506 bool GNUSyntax,
1507 unsigned NumSubExprs)
1508 : Expr(DesignatedInitExprClass, Ty),
1509 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1510 NumDesignators(NumDesignators), NumSubExprs(NumSubExprs) {
1511 this->Designators = new Designator[NumDesignators];
1512 for (unsigned I = 0; I != NumDesignators; ++I)
1513 this->Designators[I] = Designators[I];
1514}
1515
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001516DesignatedInitExpr *
1517DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1518 unsigned NumDesignators,
1519 Expr **IndexExprs, unsigned NumIndexExprs,
1520 SourceLocation ColonOrEqualLoc,
1521 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001522 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001523 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001524 DesignatedInitExpr *DIE
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001525 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001526 ColonOrEqualLoc, UsesColonSyntax,
1527 NumIndexExprs + 1);
1528
1529 // Fill in the designators
1530 unsigned ExpectedNumSubExprs = 0;
1531 designators_iterator Desig = DIE->designators_begin();
1532 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001533 if (Designators[Idx].isArrayDesignator())
1534 ++ExpectedNumSubExprs;
1535 else if (Designators[Idx].isArrayRangeDesignator())
1536 ExpectedNumSubExprs += 2;
1537 }
1538 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1539
1540 // Fill in the subexpressions, including the initializer expression.
1541 child_iterator Child = DIE->child_begin();
1542 *Child++ = Init;
1543 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1544 *Child = IndexExprs[Idx];
1545
1546 return DIE;
1547}
1548
1549SourceRange DesignatedInitExpr::getSourceRange() const {
1550 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001551 Designator &First =
1552 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001553 if (First.isFieldDesignator()) {
Douglas Gregor5f34f0e2009-03-28 00:41:23 +00001554 if (GNUSyntax)
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001555 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1556 else
1557 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1558 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001559 StartLoc =
1560 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001561 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1562}
1563
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001564Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1565 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1566 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1567 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001568 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1569 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1570}
1571
1572Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1573 assert(D.Kind == Designator::ArrayRangeDesignator &&
1574 "Requires array range designator");
1575 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1576 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001577 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1578 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1579}
1580
1581Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1582 assert(D.Kind == Designator::ArrayRangeDesignator &&
1583 "Requires array range designator");
1584 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1585 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001586 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1587 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1588}
1589
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001590/// \brief Replaces the designator at index @p Idx with the series
1591/// of designators in [First, Last).
1592void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1593 const Designator *First,
1594 const Designator *Last) {
1595 unsigned NumNewDesignators = Last - First;
1596 if (NumNewDesignators == 0) {
1597 std::copy_backward(Designators + Idx + 1,
1598 Designators + NumDesignators,
1599 Designators + Idx);
1600 --NumNewDesignators;
1601 return;
1602 } else if (NumNewDesignators == 1) {
1603 Designators[Idx] = *First;
1604 return;
1605 }
1606
1607 Designator *NewDesignators
1608 = new Designator[NumDesignators - 1 + NumNewDesignators];
1609 std::copy(Designators, Designators + Idx, NewDesignators);
1610 std::copy(First, Last, NewDesignators + Idx);
1611 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1612 NewDesignators + Idx + NumNewDesignators);
1613 delete [] Designators;
1614 Designators = NewDesignators;
1615 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1616}
1617
1618void DesignatedInitExpr::Destroy(ASTContext &C) {
1619 delete [] Designators;
1620 Expr::Destroy(C);
1621}
1622
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001623//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001624// ExprIterator.
1625//===----------------------------------------------------------------------===//
1626
1627Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1628Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1629Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1630const Expr* ConstExprIterator::operator[](size_t idx) const {
1631 return cast<Expr>(I[idx]);
1632}
1633const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1634const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1635
1636//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001637// Child Iterators for iterating over subexpressions/substatements
1638//===----------------------------------------------------------------------===//
1639
1640// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001641Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1642Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001643
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001644// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001645Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1646Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001647
Steve Naroff6f786252008-06-02 23:03:37 +00001648// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001649Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1650Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001651
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001652// ObjCKVCRefExpr
1653Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1654Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1655
Douglas Gregord8606632008-11-04 14:56:14 +00001656// ObjCSuperExpr
1657Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1658Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1659
Chris Lattner69909292008-08-10 01:53:14 +00001660// PredefinedExpr
1661Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1662Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001663
1664// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001665Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1666Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001667
1668// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001669Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001670Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001671
1672// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001673Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1674Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001675
Chris Lattner1de66eb2007-08-26 03:42:43 +00001676// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001677Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1678Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001679
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001680// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001681Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1682Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001683
1684// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001685Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1686Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001687
1688// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001689Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1690Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001691
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001692// SizeOfAlignOfExpr
1693Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1694 // If this is of a type and the type is a VLA type (and not a typedef), the
1695 // size expression of the VLA needs to be treated as an executable expression.
1696 // Why isn't this weirdness documented better in StmtIterator?
1697 if (isArgumentType()) {
1698 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1699 getArgumentType().getTypePtr()))
1700 return child_iterator(T);
1701 return child_iterator();
1702 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001703 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001704}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001705Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1706 if (isArgumentType())
1707 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001708 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001709}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001710
1711// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001712Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001713 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001714}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001715Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001716 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001717}
1718
1719// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001720Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001721 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001722}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001723Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001724 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001725}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001726
1727// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001728Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1729Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001730
Nate Begemanaf6ed502008-04-18 23:10:10 +00001731// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001732Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1733Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001734
1735// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001736Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1737Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001738
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001739// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001740Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1741Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001742
1743// BinaryOperator
1744Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001745 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001746}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001747Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001748 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001749}
1750
1751// ConditionalOperator
1752Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001753 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001754}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001755Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001756 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001757}
1758
1759// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001760Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1761Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001762
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001763// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001764Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1765Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001766
1767// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001768Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1769 return child_iterator();
1770}
1771
1772Stmt::child_iterator TypesCompatibleExpr::child_end() {
1773 return child_iterator();
1774}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001775
1776// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001777Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1778Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001779
Douglas Gregorad4b3792008-11-29 04:51:27 +00001780// GNUNullExpr
1781Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1782Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1783
Eli Friedmand0e9d092008-05-14 19:38:39 +00001784// ShuffleVectorExpr
1785Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001786 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001787}
1788Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001789 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001790}
1791
Anders Carlsson36760332007-10-15 20:28:48 +00001792// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001793Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1794Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001795
Anders Carlsson762b7c72007-08-31 04:56:16 +00001796// InitListExpr
1797Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001798 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001799}
1800Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001801 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001802}
1803
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001804// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001805Stmt::child_iterator DesignatedInitExpr::child_begin() {
1806 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1807 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001808 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1809}
1810Stmt::child_iterator DesignatedInitExpr::child_end() {
1811 return child_iterator(&*child_begin() + NumSubExprs);
1812}
1813
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001814// ImplicitValueInitExpr
1815Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1816 return child_iterator();
1817}
1818
1819Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1820 return child_iterator();
1821}
1822
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001823// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001824Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00001825 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00001826}
1827Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00001828 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00001829}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001830
1831// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001832Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1833Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001834
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001835// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001836Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1837 return child_iterator();
1838}
1839Stmt::child_iterator ObjCSelectorExpr::child_end() {
1840 return child_iterator();
1841}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001842
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001843// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001844Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1845 return child_iterator();
1846}
1847Stmt::child_iterator ObjCProtocolExpr::child_end() {
1848 return child_iterator();
1849}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001850
Steve Naroffc39ca262007-09-18 23:55:05 +00001851// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001852Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001853 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001854}
1855Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001856 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001857}
1858
Steve Naroff52a81c02008-09-03 18:15:37 +00001859// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001860Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1861Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001862
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001863Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1864Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }