blob: acf119ab7974bc584f0855de7fb497700e62bf07 [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
Douglas Gregor596e0932009-04-15 16:35:07 +000071StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
72 void *Mem = C.Allocate(sizeof(StringLiteral)+
73 sizeof(SourceLocation)*(NumStrs-1),
74 llvm::alignof<StringLiteral>());
75 StringLiteral *SL = new (Mem) StringLiteral(QualType());
76 SL->StrData = 0;
77 SL->ByteLength = 0;
78 SL->NumConcatenated = NumStrs;
79 return SL;
80}
81
Anders Carlsson7f2e7442009-03-15 18:34:13 +000082StringLiteral* StringLiteral::Clone(ASTContext &C) const {
83 return Create(C, StrData, ByteLength, IsWide, getType(),
84 TokLocs, NumConcatenated);
85}
Chris Lattnerc3144742009-02-18 05:49:11 +000086
Ted Kremenek4f530a92009-02-06 19:55:15 +000087void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +000088 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek32d760b2009-02-09 17:10:09 +000089 this->~StringLiteral();
90 C.Deallocate(this);
Chris Lattner4b009652007-07-25 00:24:17 +000091}
92
Douglas Gregor596e0932009-04-15 16:35:07 +000093void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
94 if (StrData)
95 C.Deallocate(const_cast<char*>(StrData));
96
97 char *AStrData = new (C, 1) char[Len];
98 memcpy(AStrData, Str, Len);
99 StrData = AStrData;
100 ByteLength = Len;
101}
102
Chris Lattner4b009652007-07-25 00:24:17 +0000103/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
104/// corresponds to, e.g. "sizeof" or "[pre]++".
105const char *UnaryOperator::getOpcodeStr(Opcode Op) {
106 switch (Op) {
107 default: assert(0 && "Unknown unary operator");
108 case PostInc: return "++";
109 case PostDec: return "--";
110 case PreInc: return "++";
111 case PreDec: return "--";
112 case AddrOf: return "&";
113 case Deref: return "*";
114 case Plus: return "+";
115 case Minus: return "-";
116 case Not: return "~";
117 case LNot: return "!";
118 case Real: return "__real";
119 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +0000120 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000121 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +0000122 }
123}
124
Douglas Gregorc78182d2009-03-13 23:49:33 +0000125UnaryOperator::Opcode
126UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
127 switch (OO) {
Douglas Gregorc78182d2009-03-13 23:49:33 +0000128 default: assert(false && "No unary operator for overloaded function");
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000129 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
130 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
131 case OO_Amp: return AddrOf;
132 case OO_Star: return Deref;
133 case OO_Plus: return Plus;
134 case OO_Minus: return Minus;
135 case OO_Tilde: return Not;
136 case OO_Exclaim: return LNot;
Douglas Gregorc78182d2009-03-13 23:49:33 +0000137 }
138}
139
140OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
141 switch (Opc) {
142 case PostInc: case PreInc: return OO_PlusPlus;
143 case PostDec: case PreDec: return OO_MinusMinus;
144 case AddrOf: return OO_Amp;
145 case Deref: return OO_Star;
146 case Plus: return OO_Plus;
147 case Minus: return OO_Minus;
148 case Not: return OO_Tilde;
149 case LNot: return OO_Exclaim;
150 default: return OO_None;
151 }
152}
153
154
Chris Lattner4b009652007-07-25 00:24:17 +0000155//===----------------------------------------------------------------------===//
156// Postfix Operators.
157//===----------------------------------------------------------------------===//
158
Ted Kremenek362abcd2009-02-09 20:51:47 +0000159CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +0000160 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000161 : Expr(SC, t,
162 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000163 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000164 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000165
166 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000167 SubExprs[FN] = fn;
168 for (unsigned i = 0; i != numargs; ++i)
169 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000170
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000171 RParenLoc = rparenloc;
172}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000173
Ted Kremenek362abcd2009-02-09 20:51:47 +0000174CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
175 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000176 : Expr(CallExprClass, t,
177 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000178 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000179 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000180
181 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000182 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000183 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000184 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000185
Chris Lattner4b009652007-07-25 00:24:17 +0000186 RParenLoc = rparenloc;
187}
188
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000189CallExpr::CallExpr(ASTContext &C, EmptyShell Empty)
190 : Expr(CallExprClass, Empty), SubExprs(0), NumArgs(0) {
191 SubExprs = new (C) Stmt*[1];
192}
193
Ted Kremenek362abcd2009-02-09 20:51:47 +0000194void CallExpr::Destroy(ASTContext& C) {
195 DestroyChildren(C);
196 if (SubExprs) C.Deallocate(SubExprs);
197 this->~CallExpr();
198 C.Deallocate(this);
199}
200
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000201/// setNumArgs - This changes the number of arguments present in this call.
202/// Any orphaned expressions are deleted by this, and any new operands are set
203/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000204void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000205 // No change, just return.
206 if (NumArgs == getNumArgs()) return;
207
208 // If shrinking # arguments, just delete the extras and forgot them.
209 if (NumArgs < getNumArgs()) {
210 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000211 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000212 this->NumArgs = NumArgs;
213 return;
214 }
215
216 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000217 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000218 // Copy over args.
219 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
220 NewSubExprs[i] = SubExprs[i];
221 // Null out new args.
222 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
223 NewSubExprs[i] = 0;
224
Douglas Gregorb9f63642009-04-17 21:46:47 +0000225 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000226 SubExprs = NewSubExprs;
227 this->NumArgs = NumArgs;
228}
229
Chris Lattnerc24915f2008-10-06 05:00:53 +0000230/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
231/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000232unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000233 // All simple function calls (e.g. func()) are implicitly cast to pointer to
234 // function. As a result, we try and obtain the DeclRefExpr from the
235 // ImplicitCastExpr.
236 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
237 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000238 return 0;
239
Steve Naroff44aec4c2008-01-31 01:07:12 +0000240 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
241 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000242 return 0;
243
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000244 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
245 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000246 return 0;
247
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000248 if (!FDecl->getIdentifier())
249 return 0;
250
Douglas Gregorb5af7382009-02-14 18:57:46 +0000251 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000252}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000253
Chris Lattnerc24915f2008-10-06 05:00:53 +0000254
Chris Lattner4b009652007-07-25 00:24:17 +0000255/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
256/// corresponds to, e.g. "<<=".
257const char *BinaryOperator::getOpcodeStr(Opcode Op) {
258 switch (Op) {
Douglas Gregor535f3122009-03-12 22:51:37 +0000259 case PtrMemD: return ".*";
260 case PtrMemI: return "->*";
Chris Lattner4b009652007-07-25 00:24:17 +0000261 case Mul: return "*";
262 case Div: return "/";
263 case Rem: return "%";
264 case Add: return "+";
265 case Sub: return "-";
266 case Shl: return "<<";
267 case Shr: return ">>";
268 case LT: return "<";
269 case GT: return ">";
270 case LE: return "<=";
271 case GE: return ">=";
272 case EQ: return "==";
273 case NE: return "!=";
274 case And: return "&";
275 case Xor: return "^";
276 case Or: return "|";
277 case LAnd: return "&&";
278 case LOr: return "||";
279 case Assign: return "=";
280 case MulAssign: return "*=";
281 case DivAssign: return "/=";
282 case RemAssign: return "%=";
283 case AddAssign: return "+=";
284 case SubAssign: return "-=";
285 case ShlAssign: return "<<=";
286 case ShrAssign: return ">>=";
287 case AndAssign: return "&=";
288 case XorAssign: return "^=";
289 case OrAssign: return "|=";
290 case Comma: return ",";
291 }
Douglas Gregor535f3122009-03-12 22:51:37 +0000292
293 return "";
Chris Lattner4b009652007-07-25 00:24:17 +0000294}
295
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000296BinaryOperator::Opcode
297BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
298 switch (OO) {
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000299 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000300 case OO_Plus: return Add;
301 case OO_Minus: return Sub;
302 case OO_Star: return Mul;
303 case OO_Slash: return Div;
304 case OO_Percent: return Rem;
305 case OO_Caret: return Xor;
306 case OO_Amp: return And;
307 case OO_Pipe: return Or;
308 case OO_Equal: return Assign;
309 case OO_Less: return LT;
310 case OO_Greater: return GT;
311 case OO_PlusEqual: return AddAssign;
312 case OO_MinusEqual: return SubAssign;
313 case OO_StarEqual: return MulAssign;
314 case OO_SlashEqual: return DivAssign;
315 case OO_PercentEqual: return RemAssign;
316 case OO_CaretEqual: return XorAssign;
317 case OO_AmpEqual: return AndAssign;
318 case OO_PipeEqual: return OrAssign;
319 case OO_LessLess: return Shl;
320 case OO_GreaterGreater: return Shr;
321 case OO_LessLessEqual: return ShlAssign;
322 case OO_GreaterGreaterEqual: return ShrAssign;
323 case OO_EqualEqual: return EQ;
324 case OO_ExclaimEqual: return NE;
325 case OO_LessEqual: return LE;
326 case OO_GreaterEqual: return GE;
327 case OO_AmpAmp: return LAnd;
328 case OO_PipePipe: return LOr;
329 case OO_Comma: return Comma;
330 case OO_ArrowStar: return PtrMemI;
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000331 }
332}
333
334OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
335 static const OverloadedOperatorKind OverOps[] = {
336 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
337 OO_Star, OO_Slash, OO_Percent,
338 OO_Plus, OO_Minus,
339 OO_LessLess, OO_GreaterGreater,
340 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
341 OO_EqualEqual, OO_ExclaimEqual,
342 OO_Amp,
343 OO_Caret,
344 OO_Pipe,
345 OO_AmpAmp,
346 OO_PipePipe,
347 OO_Equal, OO_StarEqual,
348 OO_SlashEqual, OO_PercentEqual,
349 OO_PlusEqual, OO_MinusEqual,
350 OO_LessLessEqual, OO_GreaterGreaterEqual,
351 OO_AmpEqual, OO_CaretEqual,
352 OO_PipeEqual,
353 OO_Comma
354 };
355 return OverOps[Opc];
356}
357
Anders Carlsson762b7c72007-08-31 04:56:16 +0000358InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000359 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000360 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000361 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000362 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000363 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000364
365 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000366}
Chris Lattner4b009652007-07-25 00:24:17 +0000367
Douglas Gregoree0792c2009-03-20 23:58:33 +0000368void InitListExpr::reserveInits(unsigned NumInits) {
369 if (NumInits > InitExprs.size())
370 InitExprs.reserve(NumInits);
371}
372
Douglas Gregorf603b472009-01-28 21:54:33 +0000373void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000374 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000375 Idx < LastIdx; ++Idx)
Douglas Gregor78e97132009-03-20 23:38:03 +0000376 InitExprs[Idx]->Destroy(Context);
Douglas Gregorf603b472009-01-28 21:54:33 +0000377 InitExprs.resize(NumInits, 0);
378}
379
380Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
381 if (Init >= InitExprs.size()) {
382 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
383 InitExprs.back() = expr;
384 return 0;
385 }
386
387 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
388 InitExprs[Init] = expr;
389 return Result;
390}
391
Steve Naroff6f373332008-09-04 15:31:07 +0000392/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000393///
394const FunctionType *BlockExpr::getFunctionType() const {
395 return getType()->getAsBlockPointerType()->
396 getPointeeType()->getAsFunctionType();
397}
398
Steve Naroff9ac456d2008-10-08 17:01:13 +0000399SourceLocation BlockExpr::getCaretLocation() const {
400 return TheBlock->getCaretLocation();
401}
Douglas Gregore3241e92009-04-18 00:02:19 +0000402const Stmt *BlockExpr::getBody() const {
403 return TheBlock->getBody();
404}
405Stmt *BlockExpr::getBody() {
406 return TheBlock->getBody();
407}
Steve Naroff9ac456d2008-10-08 17:01:13 +0000408
409
Chris Lattner4b009652007-07-25 00:24:17 +0000410//===----------------------------------------------------------------------===//
411// Generic Expression Routines
412//===----------------------------------------------------------------------===//
413
Chris Lattnerd2c66552009-02-14 07:37:35 +0000414/// isUnusedResultAWarning - Return true if this immediate expression should
415/// be warned about if the result is unused. If so, fill in Loc and Ranges
416/// with location to warn on and the source range[s] to report with the
417/// warning.
418bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
419 SourceRange &R2) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000420 switch (getStmtClass()) {
421 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000422 Loc = getExprLoc();
423 R1 = getSourceRange();
424 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000425 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000426 return cast<ParenExpr>(this)->getSubExpr()->
427 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000428 case UnaryOperatorClass: {
429 const UnaryOperator *UO = cast<UnaryOperator>(this);
430
431 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000432 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000433 case UnaryOperator::PostInc:
434 case UnaryOperator::PostDec:
435 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000436 case UnaryOperator::PreDec: // ++/--
437 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000438 case UnaryOperator::Deref:
439 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000440 if (getType().isVolatileQualified())
441 return false;
442 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000443 case UnaryOperator::Real:
444 case UnaryOperator::Imag:
445 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000446 if (UO->getSubExpr()->getType().isVolatileQualified())
447 return false;
448 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000449 case UnaryOperator::Extension:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000450 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000451 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000452 Loc = UO->getOperatorLoc();
453 R1 = UO->getSubExpr()->getSourceRange();
454 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000455 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000456 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000457 const BinaryOperator *BO = cast<BinaryOperator>(this);
458 // Consider comma to have side effects if the LHS or RHS does.
459 if (BO->getOpcode() == BinaryOperator::Comma)
460 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
461 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000462
Chris Lattnerd2c66552009-02-14 07:37:35 +0000463 if (BO->isAssignmentOp())
464 return false;
465 Loc = BO->getOperatorLoc();
466 R1 = BO->getLHS()->getSourceRange();
467 R2 = BO->getRHS()->getSourceRange();
468 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000469 }
Chris Lattner06078d22007-08-25 02:00:02 +0000470 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000471 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000472
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000473 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000474 // The condition must be evaluated, but if either the LHS or RHS is a
475 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000476 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump399ad182009-02-27 03:16:57 +0000477 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000478 return true;
479 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000480 }
481
Chris Lattner4b009652007-07-25 00:24:17 +0000482 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000483 // If the base pointer or element is to a volatile pointer/field, accessing
484 // it is a side effect.
485 if (getType().isVolatileQualified())
486 return false;
487 Loc = cast<MemberExpr>(this)->getMemberLoc();
488 R1 = SourceRange(Loc, Loc);
489 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
490 return true;
491
Chris Lattner4b009652007-07-25 00:24:17 +0000492 case ArraySubscriptExprClass:
493 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000494 // it is a side effect.
495 if (getType().isVolatileQualified())
496 return false;
497 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
498 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
499 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
500 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000501
Chris Lattner4b009652007-07-25 00:24:17 +0000502 case CallExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000503 case CXXOperatorCallExprClass: {
504 // If this is a direct call, get the callee.
505 const CallExpr *CE = cast<CallExpr>(this);
506 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
507 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
508 // If the callee has attribute pure, const, or warn_unused_result, warn
509 // about it. void foo() { strlen("bar"); } should warn.
510 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
511 if (FD->getAttr<WarnUnusedResultAttr>() ||
512 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
513 Loc = CE->getCallee()->getLocStart();
514 R1 = CE->getCallee()->getSourceRange();
515
516 if (unsigned NumArgs = CE->getNumArgs())
517 R2 = SourceRange(CE->getArg(0)->getLocStart(),
518 CE->getArg(NumArgs-1)->getLocEnd());
519 return true;
520 }
521 }
522 return false;
523 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000524 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000525 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000526 case StmtExprClass: {
527 // Statement exprs don't logically have side effects themselves, but are
528 // sometimes used in macros in ways that give them a type that is unused.
529 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
530 // however, if the result of the stmt expr is dead, we don't want to emit a
531 // warning.
532 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
533 if (!CS->body_empty())
534 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000535 return E->isUnusedResultAWarning(Loc, R1, R2);
536
537 Loc = cast<StmtExpr>(this)->getLParenLoc();
538 R1 = getSourceRange();
539 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000540 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000541 case CStyleCastExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000542 // If this is a cast to void, check the operand. Otherwise, the result of
543 // the cast is unused.
544 if (getType()->isVoidType())
545 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
546 R1, R2);
547 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
548 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
549 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000550 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000551 // If this is a cast to void, check the operand. Otherwise, the result of
552 // the cast is unused.
553 if (getType()->isVoidType())
Chris Lattnerd2c66552009-02-14 07:37:35 +0000554 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
555 R1, R2);
556 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
557 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
558 return true;
559
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000560 case ImplicitCastExprClass:
561 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000562 return cast<ImplicitCastExpr>(this)
563 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000564
Chris Lattner3e254fb2008-04-08 04:40:51 +0000565 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000566 return cast<CXXDefaultArgExpr>(this)
567 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000568
569 case CXXNewExprClass:
570 // FIXME: In theory, there might be new expressions that don't have side
571 // effects (e.g. a placement new with an uninitialized POD).
572 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000573 return false;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000574 }
Chris Lattner4b009652007-07-25 00:24:17 +0000575}
576
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000577/// DeclCanBeLvalue - Determine whether the given declaration can be
578/// an lvalue. This is a helper routine for isLvalue.
579static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000580 // C++ [temp.param]p6:
581 // A non-type non-reference template-parameter is not an lvalue.
582 if (const NonTypeTemplateParmDecl *NTTParm
583 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
584 return NTTParm->getType()->isReferenceType();
585
Douglas Gregor8acb7272008-12-11 16:49:14 +0000586 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000587 // C++ 3.10p2: An lvalue refers to an object or function.
588 (Ctx.getLangOptions().CPlusPlus &&
589 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
590}
591
Chris Lattner4b009652007-07-25 00:24:17 +0000592/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
593/// incomplete type other than void. Nonarray expressions that can be lvalues:
594/// - name, where name must be a variable
595/// - e[i]
596/// - (e), where e must be an lvalue
597/// - e.name, where e must be an lvalue
598/// - e->name
599/// - *e, the type of e cannot be a function type
600/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000601/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000602/// - reference type [C++ [expr]]
603///
Chris Lattner25168a52008-07-26 21:30:36 +0000604Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000605 // first, check the type (C99 6.3.2.1). Expressions with function
606 // type in C are not lvalues, but they can be lvalues in C++.
607 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000608 return LV_NotObjectType;
609
Steve Naroffec7736d2008-02-10 01:39:04 +0000610 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000611 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000612 return LV_IncompleteVoidType;
613
Sebastian Redlce6fff02009-03-16 23:22:08 +0000614 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
Chris Lattner4b009652007-07-25 00:24:17 +0000615
616 // the type looks fine, now check the expression
617 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000618 case StringLiteralClass: // C99 6.5.1p4
619 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000620 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000621 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
622 // For vectors, make sure base is an lvalue (i.e. not a function call).
623 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000624 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000625 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000626 case DeclRefExprClass:
627 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000628 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
629 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000630 return LV_Valid;
631 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000632 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000633 case BlockDeclRefExprClass: {
634 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000635 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000636 return LV_Valid;
637 break;
638 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000639 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000640 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000641 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
642 NamedDecl *Member = m->getMemberDecl();
643 // C++ [expr.ref]p4:
644 // If E2 is declared to have type "reference to T", then E1.E2
645 // is an lvalue.
646 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
647 if (Value->getType()->isReferenceType())
648 return LV_Valid;
649
650 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor00660582009-03-11 20:22:50 +0000651 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor82d44772008-12-20 23:49:58 +0000652 return LV_Valid;
653
654 // -- If E2 is a non-static data member [...]. If E1 is an
655 // lvalue, then E1.E2 is an lvalue.
656 if (isa<FieldDecl>(Member))
657 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
658
659 // -- If it refers to a static member function [...], then
660 // E1.E2 is an lvalue.
661 // -- Otherwise, if E1.E2 refers to a non-static member
662 // function [...], then E1.E2 is not an lvalue.
663 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
664 return Method->isStatic()? LV_Valid : LV_MemberFunction;
665
666 // -- If E2 is a member enumerator [...], the expression E1.E2
667 // is not an lvalue.
668 if (isa<EnumConstantDecl>(Member))
669 return LV_InvalidExpression;
670
671 // Not an lvalue.
672 return LV_InvalidExpression;
673 }
674
675 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000676 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000677 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000678 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000679 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000680 return LV_Valid; // C99 6.5.3p4
681
682 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000683 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
684 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000685 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000686
687 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
688 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
689 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
690 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000691 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000692 case ImplicitCastExprClass:
693 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
694 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000695 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000696 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000697 case BinaryOperatorClass:
698 case CompoundAssignOperatorClass: {
699 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000700
701 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
702 BinOp->getOpcode() == BinaryOperator::Comma)
703 return BinOp->getRHS()->isLvalue(Ctx);
704
Sebastian Redl95216a62009-02-07 00:15:38 +0000705 // C++ [expr.mptr.oper]p6
706 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
707 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
708 !BinOp->getType()->isFunctionType())
709 return BinOp->getLHS()->isLvalue(Ctx);
710
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000711 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000712 return LV_InvalidExpression;
713
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000714 if (Ctx.getLangOptions().CPlusPlus)
715 // C++ [expr.ass]p1:
716 // The result of an assignment operation [...] is an lvalue.
717 return LV_Valid;
718
719
720 // C99 6.5.16:
721 // An assignment expression [...] is not an lvalue.
722 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000723 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000724 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000725 case CXXOperatorCallExprClass:
726 case CXXMemberCallExprClass: {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000727 // C++0x [expr.call]p10
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000728 // A function call is an lvalue if and only if the result type
Sebastian Redlce6fff02009-03-16 23:22:08 +0000729 // is an lvalue reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000730 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000731 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000732 CalleeType = FnTypePtr->getPointeeType();
733 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
Sebastian Redlce6fff02009-03-16 23:22:08 +0000734 if (FnType->getResultType()->isLValueReferenceType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000735 return LV_Valid;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000736
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000737 break;
738 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000739 case CompoundLiteralExprClass: // C99 6.5.2.5p5
740 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000741 case ChooseExprClass:
742 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000743 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000744 case ExtVectorElementExprClass:
745 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000746 return LV_DuplicateVectorComponents;
747 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000748 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
749 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000750 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
751 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000752 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000753 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000754 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000755 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000756 case VAArgExprClass:
Daniel Dunbar09a82e32009-02-12 09:21:08 +0000757 return LV_NotObjectType;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000758 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000759 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000760 case CXXConditionDeclExprClass:
761 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000762 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000763 case CXXFunctionalCastExprClass:
764 case CXXStaticCastExprClass:
765 case CXXDynamicCastExprClass:
766 case CXXReinterpretCastExprClass:
767 case CXXConstCastExprClass:
768 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redlce6fff02009-03-16 23:22:08 +0000769 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000770 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
771 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000772 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
773 isLValueReferenceType())
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000774 return LV_Valid;
775 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000776 case CXXTypeidExprClass:
777 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
778 return LV_Valid;
Sebastian Redld3169132009-04-17 16:30:52 +0000779 case ConditionalOperatorClass: {
780 // Complicated handling is only for C++.
781 if (!Ctx.getLangOptions().CPlusPlus)
782 return LV_InvalidExpression;
783
784 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
785 // everywhere there's an object converted to an rvalue. Also, any other
786 // casts should be wrapped by ImplicitCastExprs. There's just the special
787 // case involving throws to work out.
788 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
789 Expr *LHS = Cond->getLHS();
790 Expr *RHS = Cond->getRHS();
791 // C++0x 5.16p2
792 // If either the second or the third operand has type (cv) void, [...]
793 // the result [...] is an rvalue.
794 if (LHS->getType()->isVoidType() || RHS->getType()->isVoidType())
795 return LV_InvalidExpression;
796
797 // Both sides must be lvalues for the result to be an lvalue.
798 if (LHS->isLvalue(Ctx) != LV_Valid || RHS->isLvalue(Ctx) != LV_Valid)
799 return LV_InvalidExpression;
800
801 // That's it.
802 return LV_Valid;
803 }
804
Chris Lattner4b009652007-07-25 00:24:17 +0000805 default:
806 break;
807 }
808 return LV_InvalidExpression;
809}
810
811/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
812/// does not have an incomplete type, does not have a const-qualified type, and
813/// if it is a structure or union, does not have any member (including,
814/// recursively, any member or element of all contained aggregates or unions)
815/// with a const-qualified type.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000816Expr::isModifiableLvalueResult
817Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner25168a52008-07-26 21:30:36 +0000818 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000819
820 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000821 case LV_Valid:
822 // C++ 3.10p11: Functions cannot be modified, but pointers to
823 // functions can be modifiable.
824 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
825 return MLV_NotObjectType;
826 break;
827
Chris Lattner4b009652007-07-25 00:24:17 +0000828 case LV_NotObjectType: return MLV_NotObjectType;
829 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000830 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000831 case LV_InvalidExpression:
832 // If the top level is a C-style cast, and the subexpression is a valid
833 // lvalue, then this is probably a use of the old-school "cast as lvalue"
834 // GCC extension. We don't support it, but we want to produce good
835 // diagnostics when it happens so that the user knows why.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000836 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
837 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
838 if (Loc)
839 *Loc = CE->getLParenLoc();
Chris Lattner37fb9402008-11-17 19:51:54 +0000840 return MLV_LValueCast;
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000841 }
842 }
Chris Lattner37fb9402008-11-17 19:51:54 +0000843 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000844 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000845 }
Eli Friedman91571da2009-03-22 23:26:56 +0000846
847 // The following is illegal:
848 // void takeclosure(void (^C)(void));
849 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
850 //
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000851 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman91571da2009-03-22 23:26:56 +0000852 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
853 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
854 return MLV_NotBlockQualified;
855 }
856
Chris Lattnera1923f62008-08-04 07:31:14 +0000857 QualType CT = Ctx.getCanonicalType(getType());
858
859 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000860 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000861 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000862 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000863 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000864 return MLV_IncompleteType;
865
Chris Lattnera1923f62008-08-04 07:31:14 +0000866 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000867 if (r->hasConstFields())
868 return MLV_ConstQualified;
869 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000870
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000871 // Assigning to an 'implicit' property?
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000872 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000873 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
874 if (KVCExpr->getSetterMethod() == 0)
875 return MLV_NoSetterProperty;
876 }
Chris Lattner4b009652007-07-25 00:24:17 +0000877 return MLV_Valid;
878}
879
Ted Kremenek5778d622008-02-27 18:39:48 +0000880/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000881/// duration. This means that the address of this expression is a link-time
882/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000883bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000884 switch (getStmtClass()) {
885 default:
886 return false;
Steve Naroff7ceff372009-04-16 19:02:57 +0000887 case BlockExprClass:
888 return true;
Chris Lattner743ec372007-11-27 21:35:27 +0000889 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000890 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000891 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000892 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000893 case CompoundLiteralExprClass:
894 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000895 case DeclRefExprClass:
896 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000897 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
898 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000899 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000900 if (isa<FunctionDecl>(D))
901 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000902 return false;
903 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000904 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000905 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000906 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000907 }
Chris Lattner743ec372007-11-27 21:35:27 +0000908 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000909 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000910 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000911 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000912 case CXXDefaultArgExprClass:
913 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000914 }
915}
916
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000917/// isOBJCGCCandidate - Check if an expression is objc gc'able.
918///
919bool Expr::isOBJCGCCandidate() const {
920 switch (getStmtClass()) {
921 default:
922 return false;
923 case ObjCIvarRefExprClass:
924 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000925 case Expr::UnaryOperatorClass:
926 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000927 case ParenExprClass:
928 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
929 case ImplicitCastExprClass:
930 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
931 case DeclRefExprClass:
932 case QualifiedDeclRefExprClass: {
933 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
934 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
935 return VD->hasGlobalStorage();
936 return false;
937 }
938 case MemberExprClass: {
939 const MemberExpr *M = cast<MemberExpr>(this);
940 return !M->isArrow() && M->getBase()->isOBJCGCCandidate();
941 }
942 case ArraySubscriptExprClass:
943 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
944 }
945}
Ted Kremenek87e30c52008-01-17 16:57:34 +0000946Expr* Expr::IgnoreParens() {
947 Expr* E = this;
948 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
949 E = P->getSubExpr();
950
951 return E;
952}
953
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000954/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
955/// or CastExprs or ImplicitCastExprs, returning their operand.
956Expr *Expr::IgnoreParenCasts() {
957 Expr *E = this;
958 while (true) {
959 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
960 E = P->getSubExpr();
961 else if (CastExpr *P = dyn_cast<CastExpr>(E))
962 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000963 else
964 return E;
965 }
966}
967
Chris Lattnerab0b8b12009-03-13 17:28:01 +0000968/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
969/// value (including ptr->int casts of the same size). Strip off any
970/// ParenExpr or CastExprs, returning their operand.
971Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
972 Expr *E = this;
973 while (true) {
974 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
975 E = P->getSubExpr();
976 continue;
977 }
978
979 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
980 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
981 // ptr<->int casts of the same width. We also ignore all identify casts.
982 Expr *SE = P->getSubExpr();
983
984 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
985 E = SE;
986 continue;
987 }
988
989 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
990 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
991 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
992 E = SE;
993 continue;
994 }
995 }
996
997 return E;
998 }
999}
1000
1001
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001002/// hasAnyTypeDependentArguments - Determines if any of the expressions
1003/// in Exprs is type-dependent.
1004bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1005 for (unsigned I = 0; I < NumExprs; ++I)
1006 if (Exprs[I]->isTypeDependent())
1007 return true;
1008
1009 return false;
1010}
1011
1012/// hasAnyValueDependentArguments - Determines if any of the expressions
1013/// in Exprs is value-dependent.
1014bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1015 for (unsigned I = 0; I < NumExprs; ++I)
1016 if (Exprs[I]->isValueDependent())
1017 return true;
1018
1019 return false;
1020}
1021
Eli Friedmandee41122009-01-25 02:32:41 +00001022bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001023 // This function is attempting whether an expression is an initializer
1024 // which can be evaluated at compile-time. isEvaluatable handles most
1025 // of the cases, but it can't deal with some initializer-specific
1026 // expressions, and it can't deal with aggregates; we deal with those here,
1027 // and fall back to isEvaluatable for the other cases.
1028
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001029 // FIXME: This function assumes the variable being assigned to
1030 // isn't a reference type!
1031
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001032 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001033 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001034 case StringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +00001035 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001036 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +00001037 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001038 // This handles gcc's extension that allows global initializers like
1039 // "struct x {int x;} x = (struct x) {};".
1040 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +00001041 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +00001042 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +00001043 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001044 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001045 // FIXME: This doesn't deal with fields with reference types correctly.
1046 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1047 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001048 const InitListExpr *Exp = cast<InitListExpr>(this);
1049 unsigned numInits = Exp->getNumInits();
1050 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +00001051 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001052 return false;
1053 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001054 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001055 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001056 case ImplicitValueInitExprClass:
1057 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +00001058 case ParenExprClass: {
1059 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1060 }
1061 case UnaryOperatorClass: {
1062 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1063 if (Exp->getOpcode() == UnaryOperator::Extension)
1064 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1065 break;
1066 }
Chris Lattner3e0eaf82009-04-21 05:19:11 +00001067 case ImplicitCastExprClass:
Eli Friedman2b0dec52009-01-25 03:12:18 +00001068 case CStyleCastExprClass:
1069 // Handle casts with a destination that's a struct or union; this
1070 // deals with both the gcc no-op struct cast extension and the
1071 // cast-to-union extension.
1072 if (getType()->isRecordType())
1073 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1074 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001075 }
1076
Eli Friedman2b0dec52009-01-25 03:12:18 +00001077 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +00001078}
1079
Chris Lattner4b009652007-07-25 00:24:17 +00001080/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +00001081/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001082
1083/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1084/// comma, etc
1085///
Chris Lattner4b009652007-07-25 00:24:17 +00001086/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1087/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1088/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001089
Eli Friedman7beeda62009-02-26 09:29:13 +00001090// CheckICE - This function does the fundamental ICE checking: the returned
1091// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1092// Note that to reduce code duplication, this helper does no evaluation
1093// itself; the caller checks whether the expression is evaluatable, and
1094// in the rare cases where CheckICE actually cares about the evaluated
1095// value, it calls into Evalute.
1096//
1097// Meanings of Val:
1098// 0: This expression is an ICE if it can be evaluated by Evaluate.
1099// 1: This expression is not an ICE, but if it isn't evaluated, it's
1100// a legal subexpression for an ICE. This return value is used to handle
1101// the comma operator in C99 mode.
1102// 2: This expression is not an ICE, and is not a legal subexpression for one.
1103
1104struct ICEDiag {
1105 unsigned Val;
1106 SourceLocation Loc;
1107
1108 public:
1109 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1110 ICEDiag() : Val(0) {}
1111};
1112
1113ICEDiag NoDiag() { return ICEDiag(); }
1114
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001115static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1116 Expr::EvalResult EVResult;
1117 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1118 !EVResult.Val.isInt()) {
1119 return ICEDiag(2, E->getLocStart());
1120 }
1121 return NoDiag();
1122}
1123
Eli Friedman7beeda62009-02-26 09:29:13 +00001124static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson8b842c52009-03-14 00:33:21 +00001125 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001126 if (!E->getType()->isIntegralType()) {
1127 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +00001128 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001129
1130 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001131 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001132 return ICEDiag(2, E->getLocStart());
1133 case Expr::ParenExprClass:
1134 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1135 case Expr::IntegerLiteralClass:
1136 case Expr::CharacterLiteralClass:
1137 case Expr::CXXBoolLiteralExprClass:
1138 case Expr::CXXZeroInitValueExprClass:
1139 case Expr::TypesCompatibleExprClass:
1140 case Expr::UnaryTypeTraitExprClass:
1141 return NoDiag();
1142 case Expr::CallExprClass:
1143 case Expr::CXXOperatorCallExprClass: {
1144 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001145 if (CE->isBuiltinCall(Ctx))
1146 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +00001147 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001148 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001149 case Expr::DeclRefExprClass:
1150 case Expr::QualifiedDeclRefExprClass:
1151 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1152 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001153 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +00001154 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001155 // C++ 7.1.5.1p2
1156 // A variable of non-volatile const-qualified integral or enumeration
1157 // type initialized by an ICE can be used in ICEs.
1158 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +00001159 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001160 if (const Expr *Init = Dcl->getInit())
Eli Friedman7beeda62009-02-26 09:29:13 +00001161 return CheckICE(Init, Ctx);
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001162 }
1163 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001164 return ICEDiag(2, E->getLocStart());
1165 case Expr::UnaryOperatorClass: {
1166 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001167 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001168 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001169 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001170 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +00001171 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +00001172 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +00001173 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +00001174 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001175 case UnaryOperator::Real:
1176 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +00001177 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001178 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001179 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1180 // Evaluate matches the proposed gcc behavior for cases like
1181 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1182 // compliance: we should warn earlier for offsetof expressions with
1183 // array subscripts that aren't ICEs, and if the array subscripts
1184 // are ICEs, the value of the offsetof must be an integer constant.
1185 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001186 }
Chris Lattner4b009652007-07-25 00:24:17 +00001187 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001188 case Expr::SizeOfAlignOfExprClass: {
1189 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1190 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1191 return ICEDiag(2, E->getLocStart());
1192 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +00001193 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001194 case Expr::BinaryOperatorClass: {
1195 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001196 switch (Exp->getOpcode()) {
1197 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001198 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001199 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +00001200 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +00001201 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +00001202 case BinaryOperator::Add:
1203 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001204 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001205 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001206 case BinaryOperator::LT:
1207 case BinaryOperator::GT:
1208 case BinaryOperator::LE:
1209 case BinaryOperator::GE:
1210 case BinaryOperator::EQ:
1211 case BinaryOperator::NE:
1212 case BinaryOperator::And:
1213 case BinaryOperator::Xor:
1214 case BinaryOperator::Or:
1215 case BinaryOperator::Comma: {
1216 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1217 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001218 if (Exp->getOpcode() == BinaryOperator::Div ||
1219 Exp->getOpcode() == BinaryOperator::Rem) {
1220 // Evaluate gives an error for undefined Div/Rem, so make sure
1221 // we don't evaluate one.
1222 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1223 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1224 if (REval == 0)
1225 return ICEDiag(1, E->getLocStart());
1226 if (REval.isSigned() && REval.isAllOnesValue()) {
1227 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1228 if (LEval.isMinSignedValue())
1229 return ICEDiag(1, E->getLocStart());
1230 }
1231 }
1232 }
1233 if (Exp->getOpcode() == BinaryOperator::Comma) {
1234 if (Ctx.getLangOptions().C99) {
1235 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1236 // if it isn't evaluated.
1237 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1238 return ICEDiag(1, E->getLocStart());
1239 } else {
1240 // In both C89 and C++, commas in ICEs are illegal.
1241 return ICEDiag(2, E->getLocStart());
1242 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001243 }
1244 if (LHSResult.Val >= RHSResult.Val)
1245 return LHSResult;
1246 return RHSResult;
1247 }
Chris Lattner4b009652007-07-25 00:24:17 +00001248 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001249 case BinaryOperator::LOr: {
1250 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1251 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1252 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1253 // Rare case where the RHS has a comma "side-effect"; we need
1254 // to actually check the condition to see whether the side
1255 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001256 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001257 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001258 return RHSResult;
1259 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001260 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001261
Eli Friedman7beeda62009-02-26 09:29:13 +00001262 if (LHSResult.Val >= RHSResult.Val)
1263 return LHSResult;
1264 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001265 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001266 }
Chris Lattner4b009652007-07-25 00:24:17 +00001267 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001268 case Expr::ImplicitCastExprClass:
1269 case Expr::CStyleCastExprClass:
1270 case Expr::CXXFunctionalCastExprClass: {
1271 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1272 if (SubExpr->getType()->isIntegralType())
1273 return CheckICE(SubExpr, Ctx);
1274 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1275 return NoDiag();
1276 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001277 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001278 case Expr::ConditionalOperatorClass: {
1279 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001280 // If the condition (ignoring parens) is a __builtin_constant_p call,
1281 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001282 // expression, and it is fully evaluated. This is an important GNU
1283 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001284 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001285 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001286 Expr::EvalResult EVResult;
1287 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1288 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001289 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001290 }
1291 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001292 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001293 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1294 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1295 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1296 if (CondResult.Val == 2)
1297 return CondResult;
1298 if (TrueResult.Val == 2)
1299 return TrueResult;
1300 if (FalseResult.Val == 2)
1301 return FalseResult;
1302 if (CondResult.Val == 1)
1303 return CondResult;
1304 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1305 return NoDiag();
1306 // Rare case where the diagnostics depend on which side is evaluated
1307 // Note that if we get here, CondResult is 0, and at least one of
1308 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001309 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001310 return FalseResult;
1311 }
1312 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001313 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001314 case Expr::CXXDefaultArgExprClass:
1315 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001316 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001317 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001318 }
Chris Lattner4b009652007-07-25 00:24:17 +00001319 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001320}
Chris Lattner4b009652007-07-25 00:24:17 +00001321
Eli Friedman7beeda62009-02-26 09:29:13 +00001322bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1323 SourceLocation *Loc, bool isEvaluated) const {
1324 ICEDiag d = CheckICE(this, Ctx);
1325 if (d.Val != 0) {
1326 if (Loc) *Loc = d.Loc;
1327 return false;
1328 }
1329 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001330 if (!Evaluate(EvalResult, Ctx))
1331 assert(0 && "ICE cannot be evaluated!");
1332 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1333 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001334 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001335 return true;
1336}
1337
Chris Lattner4b009652007-07-25 00:24:17 +00001338/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1339/// integer constant expression with the value zero, or if this is one that is
1340/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001341bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1342{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001343 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001344 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001345 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001346 // Check that it is a cast to void*.
1347 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1348 QualType Pointee = PT->getPointeeType();
1349 if (Pointee.getCVRQualifiers() == 0 &&
1350 Pointee->isVoidType() && // to void*
1351 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001352 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001353 }
Chris Lattner4b009652007-07-25 00:24:17 +00001354 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001355 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1356 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001357 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001358 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1359 // Accept ((void*)0) as a null pointer constant, as many other
1360 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001361 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001362 } else if (const CXXDefaultArgExpr *DefaultArg
1363 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001364 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001365 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001366 } else if (isa<GNUNullExpr>(this)) {
1367 // The GNU __null extension is always a null pointer constant.
1368 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001369 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001370
Steve Naroffa2e53222008-01-14 16:10:57 +00001371 // This expression must be an integer type.
1372 if (!getType()->isIntegerType())
1373 return false;
1374
Chris Lattner4b009652007-07-25 00:24:17 +00001375 // If we have an integer constant expression, we need to *evaluate* it and
1376 // test for the value 0.
Eli Friedman4b4e14b2009-04-25 22:37:12 +00001377 llvm::APSInt Result;
1378 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001379}
Steve Naroffc11705f2007-07-28 23:10:27 +00001380
Douglas Gregor81c29152008-10-29 00:13:59 +00001381/// isBitField - Return true if this expression is a bit-field.
1382bool Expr::isBitField() {
1383 Expr *E = this->IgnoreParenCasts();
1384 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001385 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1386 return Field->isBitField();
Douglas Gregor81c29152008-10-29 00:13:59 +00001387 return false;
1388}
1389
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001390/// isArrow - Return true if the base expression is a pointer to vector,
1391/// return false if the base expression is a vector.
1392bool ExtVectorElementExpr::isArrow() const {
1393 return getBase()->getType()->isPointerType();
1394}
1395
Nate Begemanaf6ed502008-04-18 23:10:10 +00001396unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001397 if (const VectorType *VT = getType()->getAsVectorType())
1398 return VT->getNumElements();
1399 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001400}
1401
Nate Begemanc8e51f82008-05-09 06:41:27 +00001402/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001403bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001404 const char *compStr = Accessor->getName();
1405 unsigned length = Accessor->getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001406
1407 // Halving swizzles do not contain duplicate elements.
1408 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1409 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1410 return false;
1411
1412 // Advance past s-char prefix on hex swizzles.
1413 if (*compStr == 's') {
1414 compStr++;
1415 length--;
1416 }
Steve Naroffba67f692007-07-30 03:29:09 +00001417
Chris Lattner58d3fa52008-11-19 07:55:04 +00001418 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001419 const char *s = compStr+i;
1420 for (const char c = *s++; *s; s++)
1421 if (c == *s)
1422 return true;
1423 }
1424 return false;
1425}
Chris Lattner42158e72007-08-02 23:36:59 +00001426
Nate Begemanc8e51f82008-05-09 06:41:27 +00001427/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001428void ExtVectorElementExpr::getEncodedElementAccess(
1429 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001430 const char *compStr = Accessor->getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001431 if (*compStr == 's')
1432 compStr++;
1433
1434 bool isHi = !strcmp(compStr, "hi");
1435 bool isLo = !strcmp(compStr, "lo");
1436 bool isEven = !strcmp(compStr, "even");
1437 bool isOdd = !strcmp(compStr, "odd");
1438
Nate Begemanc8e51f82008-05-09 06:41:27 +00001439 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1440 uint64_t Index;
1441
1442 if (isHi)
1443 Index = e + i;
1444 else if (isLo)
1445 Index = i;
1446 else if (isEven)
1447 Index = 2 * i;
1448 else if (isOdd)
1449 Index = 2 * i + 1;
1450 else
1451 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001452
Nate Begemana1ae7442008-05-13 21:03:02 +00001453 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001454 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001455}
1456
Steve Naroff4ed9d662007-09-27 14:38:14 +00001457// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001458ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001459 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001460 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001461 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001462 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001463 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001464 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001465 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001466 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001467 if (NumArgs) {
1468 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001469 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1470 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001471 LBracloc = LBrac;
1472 RBracloc = RBrac;
1473}
1474
Steve Naroff4ed9d662007-09-27 14:38:14 +00001475// constructor for class messages.
1476// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001477ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001478 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001479 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001480 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001481 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001482 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001483 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001484 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001485 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001486 if (NumArgs) {
1487 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001488 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1489 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001490 LBracloc = LBrac;
1491 RBracloc = RBrac;
1492}
1493
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001494// constructor for class messages.
1495ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1496 QualType retType, ObjCMethodDecl *mproto,
1497 SourceLocation LBrac, SourceLocation RBrac,
1498 Expr **ArgExprs, unsigned nargs)
1499: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1500MethodProto(mproto) {
1501 NumArgs = nargs;
1502 SubExprs = new Stmt*[NumArgs+1];
1503 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1504 if (NumArgs) {
1505 for (unsigned i = 0; i != NumArgs; ++i)
1506 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1507 }
1508 LBracloc = LBrac;
1509 RBracloc = RBrac;
1510}
1511
1512ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1513 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1514 switch (x & Flags) {
1515 default:
1516 assert(false && "Invalid ObjCMessageExpr.");
1517 case IsInstMeth:
1518 return ClassInfo(0, 0);
1519 case IsClsMethDeclUnknown:
1520 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1521 case IsClsMethDeclKnown: {
1522 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1523 return ClassInfo(D, D->getIdentifier());
1524 }
1525 }
1526}
1527
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001528void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1529 if (CI.first == 0 && CI.second == 0)
1530 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1531 else if (CI.first == 0)
1532 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1533 else
1534 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1535}
1536
1537
Chris Lattnerf624cd22007-10-25 00:29:32 +00001538bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman5255e7a2009-04-26 19:19:15 +00001539 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001540}
1541
Douglas Gregor725e94b2009-04-16 00:01:45 +00001542void ShuffleVectorExpr::setExprs(Expr ** Exprs, unsigned NumExprs) {
1543 if (NumExprs)
1544 delete [] SubExprs;
1545
1546 SubExprs = new Stmt* [NumExprs];
1547 this->NumExprs = NumExprs;
1548 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
1549}
1550
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001551void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1552 // Override default behavior of traversing children. If this has a type
1553 // operand and the type is a variable-length array, the child iteration
1554 // will iterate over the size expression. However, this expression belongs
1555 // to the type, not to this, so we don't want to delete it.
1556 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001557 if (isArgumentType()) {
1558 this->~SizeOfAlignOfExpr();
1559 C.Deallocate(this);
1560 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001561 else
1562 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001563}
1564
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001565//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001566// DesignatedInitExpr
1567//===----------------------------------------------------------------------===//
1568
1569IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1570 assert(Kind == FieldDesignator && "Only valid on a field designator");
1571 if (Field.NameOrField & 0x01)
1572 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1573 else
1574 return getField()->getIdentifier();
1575}
1576
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001577DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1578 const Designator *Designators,
1579 SourceLocation EqualOrColonLoc,
1580 bool GNUSyntax,
1581 unsigned NumSubExprs)
1582 : Expr(DesignatedInitExprClass, Ty),
1583 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1584 NumDesignators(NumDesignators), NumSubExprs(NumSubExprs) {
1585 this->Designators = new Designator[NumDesignators];
1586 for (unsigned I = 0; I != NumDesignators; ++I)
1587 this->Designators[I] = Designators[I];
1588}
1589
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001590DesignatedInitExpr *
1591DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1592 unsigned NumDesignators,
1593 Expr **IndexExprs, unsigned NumIndexExprs,
1594 SourceLocation ColonOrEqualLoc,
1595 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001596 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001597 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001598 DesignatedInitExpr *DIE
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001599 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001600 ColonOrEqualLoc, UsesColonSyntax,
1601 NumIndexExprs + 1);
1602
1603 // Fill in the designators
1604 unsigned ExpectedNumSubExprs = 0;
1605 designators_iterator Desig = DIE->designators_begin();
1606 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001607 if (Designators[Idx].isArrayDesignator())
1608 ++ExpectedNumSubExprs;
1609 else if (Designators[Idx].isArrayRangeDesignator())
1610 ExpectedNumSubExprs += 2;
1611 }
1612 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1613
1614 // Fill in the subexpressions, including the initializer expression.
1615 child_iterator Child = DIE->child_begin();
1616 *Child++ = Init;
1617 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1618 *Child = IndexExprs[Idx];
1619
1620 return DIE;
1621}
1622
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001623DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1624 unsigned NumIndexExprs) {
1625 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1626 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1627 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1628}
1629
1630void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1631 unsigned NumDesigs) {
1632 if (Designators)
1633 delete [] Designators;
1634
1635 Designators = new Designator[NumDesigs];
1636 NumDesignators = NumDesigs;
1637 for (unsigned I = 0; I != NumDesigs; ++I)
1638 Designators[I] = Desigs[I];
1639}
1640
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001641SourceRange DesignatedInitExpr::getSourceRange() const {
1642 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001643 Designator &First =
1644 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001645 if (First.isFieldDesignator()) {
Douglas Gregor5f34f0e2009-03-28 00:41:23 +00001646 if (GNUSyntax)
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001647 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1648 else
1649 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1650 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001651 StartLoc =
1652 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001653 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1654}
1655
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001656Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1657 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1658 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1659 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001660 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1661 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1662}
1663
1664Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1665 assert(D.Kind == Designator::ArrayRangeDesignator &&
1666 "Requires array range designator");
1667 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1668 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001669 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1670 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1671}
1672
1673Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1674 assert(D.Kind == Designator::ArrayRangeDesignator &&
1675 "Requires array range designator");
1676 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1677 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001678 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1679 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1680}
1681
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001682/// \brief Replaces the designator at index @p Idx with the series
1683/// of designators in [First, Last).
1684void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1685 const Designator *First,
1686 const Designator *Last) {
1687 unsigned NumNewDesignators = Last - First;
1688 if (NumNewDesignators == 0) {
1689 std::copy_backward(Designators + Idx + 1,
1690 Designators + NumDesignators,
1691 Designators + Idx);
1692 --NumNewDesignators;
1693 return;
1694 } else if (NumNewDesignators == 1) {
1695 Designators[Idx] = *First;
1696 return;
1697 }
1698
1699 Designator *NewDesignators
1700 = new Designator[NumDesignators - 1 + NumNewDesignators];
1701 std::copy(Designators, Designators + Idx, NewDesignators);
1702 std::copy(First, Last, NewDesignators + Idx);
1703 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1704 NewDesignators + Idx + NumNewDesignators);
1705 delete [] Designators;
1706 Designators = NewDesignators;
1707 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1708}
1709
1710void DesignatedInitExpr::Destroy(ASTContext &C) {
1711 delete [] Designators;
1712 Expr::Destroy(C);
1713}
1714
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001715//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001716// ExprIterator.
1717//===----------------------------------------------------------------------===//
1718
1719Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1720Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1721Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1722const Expr* ConstExprIterator::operator[](size_t idx) const {
1723 return cast<Expr>(I[idx]);
1724}
1725const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1726const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1727
1728//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001729// Child Iterators for iterating over subexpressions/substatements
1730//===----------------------------------------------------------------------===//
1731
1732// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001733Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1734Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001735
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001736// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001737Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1738Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001739
Steve Naroff6f786252008-06-02 23:03:37 +00001740// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001741Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1742Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001743
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001744// ObjCKVCRefExpr
1745Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1746Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1747
Douglas Gregord8606632008-11-04 14:56:14 +00001748// ObjCSuperExpr
1749Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1750Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1751
Chris Lattner69909292008-08-10 01:53:14 +00001752// PredefinedExpr
1753Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1754Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001755
1756// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001757Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1758Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001759
1760// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001761Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001762Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001763
1764// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001765Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1766Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001767
Chris Lattner1de66eb2007-08-26 03:42:43 +00001768// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001769Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1770Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001771
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001772// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001773Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1774Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001775
1776// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001777Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1778Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001779
1780// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001781Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1782Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001783
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001784// SizeOfAlignOfExpr
1785Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1786 // If this is of a type and the type is a VLA type (and not a typedef), the
1787 // size expression of the VLA needs to be treated as an executable expression.
1788 // Why isn't this weirdness documented better in StmtIterator?
1789 if (isArgumentType()) {
1790 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1791 getArgumentType().getTypePtr()))
1792 return child_iterator(T);
1793 return child_iterator();
1794 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001795 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001796}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001797Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1798 if (isArgumentType())
1799 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001800 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001801}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001802
1803// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001804Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001805 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001806}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001807Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001808 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001809}
1810
1811// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001812Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001813 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001814}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001815Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001816 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001817}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001818
1819// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001820Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1821Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001822
Nate Begemanaf6ed502008-04-18 23:10:10 +00001823// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001824Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1825Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001826
1827// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001828Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1829Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001830
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001831// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001832Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1833Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001834
1835// BinaryOperator
1836Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001837 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001838}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001839Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001840 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001841}
1842
1843// ConditionalOperator
1844Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001845 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001846}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001847Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001848 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001849}
1850
1851// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001852Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1853Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001854
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001855// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001856Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1857Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001858
1859// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001860Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1861 return child_iterator();
1862}
1863
1864Stmt::child_iterator TypesCompatibleExpr::child_end() {
1865 return child_iterator();
1866}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001867
1868// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001869Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1870Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001871
Douglas Gregorad4b3792008-11-29 04:51:27 +00001872// GNUNullExpr
1873Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1874Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1875
Eli Friedmand0e9d092008-05-14 19:38:39 +00001876// ShuffleVectorExpr
1877Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001878 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001879}
1880Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001881 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001882}
1883
Anders Carlsson36760332007-10-15 20:28:48 +00001884// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001885Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1886Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001887
Anders Carlsson762b7c72007-08-31 04:56:16 +00001888// InitListExpr
1889Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001890 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001891}
1892Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001893 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001894}
1895
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001896// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001897Stmt::child_iterator DesignatedInitExpr::child_begin() {
1898 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1899 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001900 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1901}
1902Stmt::child_iterator DesignatedInitExpr::child_end() {
1903 return child_iterator(&*child_begin() + NumSubExprs);
1904}
1905
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001906// ImplicitValueInitExpr
1907Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1908 return child_iterator();
1909}
1910
1911Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1912 return child_iterator();
1913}
1914
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001915// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001916Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00001917 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00001918}
1919Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00001920 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00001921}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001922
1923// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001924Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1925Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001926
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001927// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001928Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1929 return child_iterator();
1930}
1931Stmt::child_iterator ObjCSelectorExpr::child_end() {
1932 return child_iterator();
1933}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001934
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001935// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001936Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1937 return child_iterator();
1938}
1939Stmt::child_iterator ObjCProtocolExpr::child_end() {
1940 return child_iterator();
1941}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001942
Steve Naroffc39ca262007-09-18 23:55:05 +00001943// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001944Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001945 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001946}
1947Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001948 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001949}
1950
Steve Naroff52a81c02008-09-03 18:15:37 +00001951// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001952Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1953Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001954
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001955Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1956Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }