blob: 53633977e98b383cca8ff10909f3ebc1741f63c8 [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
Ted Kremenek362abcd2009-02-09 20:51:47 +0000189void CallExpr::Destroy(ASTContext& C) {
190 DestroyChildren(C);
191 if (SubExprs) C.Deallocate(SubExprs);
192 this->~CallExpr();
193 C.Deallocate(this);
194}
195
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000196/// setNumArgs - This changes the number of arguments present in this call.
197/// Any orphaned expressions are deleted by this, and any new operands are set
198/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000199void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000200 // No change, just return.
201 if (NumArgs == getNumArgs()) return;
202
203 // If shrinking # arguments, just delete the extras and forgot them.
204 if (NumArgs < getNumArgs()) {
205 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000206 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000207 this->NumArgs = NumArgs;
208 return;
209 }
210
211 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000212 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000213 // Copy over args.
214 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
215 NewSubExprs[i] = SubExprs[i];
216 // Null out new args.
217 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
218 NewSubExprs[i] = 0;
219
Ted Kremenek0c97e042009-02-07 01:47:29 +0000220 delete [] SubExprs;
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000221 SubExprs = NewSubExprs;
222 this->NumArgs = NumArgs;
223}
224
Chris Lattnerc24915f2008-10-06 05:00:53 +0000225/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
226/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000227unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000228 // All simple function calls (e.g. func()) are implicitly cast to pointer to
229 // function. As a result, we try and obtain the DeclRefExpr from the
230 // ImplicitCastExpr.
231 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
232 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000233 return 0;
234
Steve Naroff44aec4c2008-01-31 01:07:12 +0000235 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
236 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000237 return 0;
238
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000239 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
240 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000241 return 0;
242
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000243 if (!FDecl->getIdentifier())
244 return 0;
245
Douglas Gregorb5af7382009-02-14 18:57:46 +0000246 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000247}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000248
Chris Lattnerc24915f2008-10-06 05:00:53 +0000249
Chris Lattner4b009652007-07-25 00:24:17 +0000250/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
251/// corresponds to, e.g. "<<=".
252const char *BinaryOperator::getOpcodeStr(Opcode Op) {
253 switch (Op) {
Douglas Gregor535f3122009-03-12 22:51:37 +0000254 case PtrMemD: return ".*";
255 case PtrMemI: return "->*";
Chris Lattner4b009652007-07-25 00:24:17 +0000256 case Mul: return "*";
257 case Div: return "/";
258 case Rem: return "%";
259 case Add: return "+";
260 case Sub: return "-";
261 case Shl: return "<<";
262 case Shr: return ">>";
263 case LT: return "<";
264 case GT: return ">";
265 case LE: return "<=";
266 case GE: return ">=";
267 case EQ: return "==";
268 case NE: return "!=";
269 case And: return "&";
270 case Xor: return "^";
271 case Or: return "|";
272 case LAnd: return "&&";
273 case LOr: return "||";
274 case Assign: return "=";
275 case MulAssign: return "*=";
276 case DivAssign: return "/=";
277 case RemAssign: return "%=";
278 case AddAssign: return "+=";
279 case SubAssign: return "-=";
280 case ShlAssign: return "<<=";
281 case ShrAssign: return ">>=";
282 case AndAssign: return "&=";
283 case XorAssign: return "^=";
284 case OrAssign: return "|=";
285 case Comma: return ",";
286 }
Douglas Gregor535f3122009-03-12 22:51:37 +0000287
288 return "";
Chris Lattner4b009652007-07-25 00:24:17 +0000289}
290
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000291BinaryOperator::Opcode
292BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
293 switch (OO) {
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000294 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000295 case OO_Plus: return Add;
296 case OO_Minus: return Sub;
297 case OO_Star: return Mul;
298 case OO_Slash: return Div;
299 case OO_Percent: return Rem;
300 case OO_Caret: return Xor;
301 case OO_Amp: return And;
302 case OO_Pipe: return Or;
303 case OO_Equal: return Assign;
304 case OO_Less: return LT;
305 case OO_Greater: return GT;
306 case OO_PlusEqual: return AddAssign;
307 case OO_MinusEqual: return SubAssign;
308 case OO_StarEqual: return MulAssign;
309 case OO_SlashEqual: return DivAssign;
310 case OO_PercentEqual: return RemAssign;
311 case OO_CaretEqual: return XorAssign;
312 case OO_AmpEqual: return AndAssign;
313 case OO_PipeEqual: return OrAssign;
314 case OO_LessLess: return Shl;
315 case OO_GreaterGreater: return Shr;
316 case OO_LessLessEqual: return ShlAssign;
317 case OO_GreaterGreaterEqual: return ShrAssign;
318 case OO_EqualEqual: return EQ;
319 case OO_ExclaimEqual: return NE;
320 case OO_LessEqual: return LE;
321 case OO_GreaterEqual: return GE;
322 case OO_AmpAmp: return LAnd;
323 case OO_PipePipe: return LOr;
324 case OO_Comma: return Comma;
325 case OO_ArrowStar: return PtrMemI;
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000326 }
327}
328
329OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
330 static const OverloadedOperatorKind OverOps[] = {
331 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
332 OO_Star, OO_Slash, OO_Percent,
333 OO_Plus, OO_Minus,
334 OO_LessLess, OO_GreaterGreater,
335 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
336 OO_EqualEqual, OO_ExclaimEqual,
337 OO_Amp,
338 OO_Caret,
339 OO_Pipe,
340 OO_AmpAmp,
341 OO_PipePipe,
342 OO_Equal, OO_StarEqual,
343 OO_SlashEqual, OO_PercentEqual,
344 OO_PlusEqual, OO_MinusEqual,
345 OO_LessLessEqual, OO_GreaterGreaterEqual,
346 OO_AmpEqual, OO_CaretEqual,
347 OO_PipeEqual,
348 OO_Comma
349 };
350 return OverOps[Opc];
351}
352
Anders Carlsson762b7c72007-08-31 04:56:16 +0000353InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000354 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000355 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000356 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000357 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000358 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000359
360 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000361}
Chris Lattner4b009652007-07-25 00:24:17 +0000362
Douglas Gregoree0792c2009-03-20 23:58:33 +0000363void InitListExpr::reserveInits(unsigned NumInits) {
364 if (NumInits > InitExprs.size())
365 InitExprs.reserve(NumInits);
366}
367
Douglas Gregorf603b472009-01-28 21:54:33 +0000368void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000369 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000370 Idx < LastIdx; ++Idx)
Douglas Gregor78e97132009-03-20 23:38:03 +0000371 InitExprs[Idx]->Destroy(Context);
Douglas Gregorf603b472009-01-28 21:54:33 +0000372 InitExprs.resize(NumInits, 0);
373}
374
375Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
376 if (Init >= InitExprs.size()) {
377 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
378 InitExprs.back() = expr;
379 return 0;
380 }
381
382 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
383 InitExprs[Init] = expr;
384 return Result;
385}
386
Steve Naroff6f373332008-09-04 15:31:07 +0000387/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000388///
389const FunctionType *BlockExpr::getFunctionType() const {
390 return getType()->getAsBlockPointerType()->
391 getPointeeType()->getAsFunctionType();
392}
393
Steve Naroff9ac456d2008-10-08 17:01:13 +0000394SourceLocation BlockExpr::getCaretLocation() const {
395 return TheBlock->getCaretLocation();
396}
397const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
398Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
399
400
Chris Lattner4b009652007-07-25 00:24:17 +0000401//===----------------------------------------------------------------------===//
402// Generic Expression Routines
403//===----------------------------------------------------------------------===//
404
Chris Lattnerd2c66552009-02-14 07:37:35 +0000405/// isUnusedResultAWarning - Return true if this immediate expression should
406/// be warned about if the result is unused. If so, fill in Loc and Ranges
407/// with location to warn on and the source range[s] to report with the
408/// warning.
409bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
410 SourceRange &R2) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000411 switch (getStmtClass()) {
412 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000413 Loc = getExprLoc();
414 R1 = getSourceRange();
415 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000416 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000417 return cast<ParenExpr>(this)->getSubExpr()->
418 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000419 case UnaryOperatorClass: {
420 const UnaryOperator *UO = cast<UnaryOperator>(this);
421
422 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000423 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000424 case UnaryOperator::PostInc:
425 case UnaryOperator::PostDec:
426 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000427 case UnaryOperator::PreDec: // ++/--
428 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000429 case UnaryOperator::Deref:
430 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000431 if (getType().isVolatileQualified())
432 return false;
433 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000434 case UnaryOperator::Real:
435 case UnaryOperator::Imag:
436 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000437 if (UO->getSubExpr()->getType().isVolatileQualified())
438 return false;
439 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000440 case UnaryOperator::Extension:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000441 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000442 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000443 Loc = UO->getOperatorLoc();
444 R1 = UO->getSubExpr()->getSourceRange();
445 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000446 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000447 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000448 const BinaryOperator *BO = cast<BinaryOperator>(this);
449 // Consider comma to have side effects if the LHS or RHS does.
450 if (BO->getOpcode() == BinaryOperator::Comma)
451 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
452 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000453
Chris Lattnerd2c66552009-02-14 07:37:35 +0000454 if (BO->isAssignmentOp())
455 return false;
456 Loc = BO->getOperatorLoc();
457 R1 = BO->getLHS()->getSourceRange();
458 R2 = BO->getRHS()->getSourceRange();
459 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000460 }
Chris Lattner06078d22007-08-25 02:00:02 +0000461 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000462 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000463
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000464 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000465 // The condition must be evaluated, but if either the LHS or RHS is a
466 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000467 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump399ad182009-02-27 03:16:57 +0000468 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000469 return true;
470 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000471 }
472
Chris Lattner4b009652007-07-25 00:24:17 +0000473 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000474 // If the base pointer or element is to a volatile pointer/field, accessing
475 // it is a side effect.
476 if (getType().isVolatileQualified())
477 return false;
478 Loc = cast<MemberExpr>(this)->getMemberLoc();
479 R1 = SourceRange(Loc, Loc);
480 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
481 return true;
482
Chris Lattner4b009652007-07-25 00:24:17 +0000483 case ArraySubscriptExprClass:
484 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000485 // it is a side effect.
486 if (getType().isVolatileQualified())
487 return false;
488 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
489 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
490 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
491 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000492
Chris Lattner4b009652007-07-25 00:24:17 +0000493 case CallExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000494 case CXXOperatorCallExprClass: {
495 // If this is a direct call, get the callee.
496 const CallExpr *CE = cast<CallExpr>(this);
497 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
498 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
499 // If the callee has attribute pure, const, or warn_unused_result, warn
500 // about it. void foo() { strlen("bar"); } should warn.
501 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
502 if (FD->getAttr<WarnUnusedResultAttr>() ||
503 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
504 Loc = CE->getCallee()->getLocStart();
505 R1 = CE->getCallee()->getSourceRange();
506
507 if (unsigned NumArgs = CE->getNumArgs())
508 R2 = SourceRange(CE->getArg(0)->getLocStart(),
509 CE->getArg(NumArgs-1)->getLocEnd());
510 return true;
511 }
512 }
513 return false;
514 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000515 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000516 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000517 case StmtExprClass: {
518 // Statement exprs don't logically have side effects themselves, but are
519 // sometimes used in macros in ways that give them a type that is unused.
520 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
521 // however, if the result of the stmt expr is dead, we don't want to emit a
522 // warning.
523 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
524 if (!CS->body_empty())
525 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000526 return E->isUnusedResultAWarning(Loc, R1, R2);
527
528 Loc = cast<StmtExpr>(this)->getLParenLoc();
529 R1 = getSourceRange();
530 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000531 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000532 case CStyleCastExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000533 // If this is a cast to void, check the operand. Otherwise, the result of
534 // the cast is unused.
535 if (getType()->isVoidType())
536 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
537 R1, R2);
538 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
539 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
540 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000541 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000542 // If this is a cast to void, check the operand. Otherwise, the result of
543 // the cast is unused.
544 if (getType()->isVoidType())
Chris Lattnerd2c66552009-02-14 07:37:35 +0000545 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
546 R1, R2);
547 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
548 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
549 return true;
550
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000551 case ImplicitCastExprClass:
552 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000553 return cast<ImplicitCastExpr>(this)
554 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000555
Chris Lattner3e254fb2008-04-08 04:40:51 +0000556 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000557 return cast<CXXDefaultArgExpr>(this)
558 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000559
560 case CXXNewExprClass:
561 // FIXME: In theory, there might be new expressions that don't have side
562 // effects (e.g. a placement new with an uninitialized POD).
563 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000564 return false;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000565 }
Chris Lattner4b009652007-07-25 00:24:17 +0000566}
567
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000568/// DeclCanBeLvalue - Determine whether the given declaration can be
569/// an lvalue. This is a helper routine for isLvalue.
570static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000571 // C++ [temp.param]p6:
572 // A non-type non-reference template-parameter is not an lvalue.
573 if (const NonTypeTemplateParmDecl *NTTParm
574 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
575 return NTTParm->getType()->isReferenceType();
576
Douglas Gregor8acb7272008-12-11 16:49:14 +0000577 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000578 // C++ 3.10p2: An lvalue refers to an object or function.
579 (Ctx.getLangOptions().CPlusPlus &&
580 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
581}
582
Chris Lattner4b009652007-07-25 00:24:17 +0000583/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
584/// incomplete type other than void. Nonarray expressions that can be lvalues:
585/// - name, where name must be a variable
586/// - e[i]
587/// - (e), where e must be an lvalue
588/// - e.name, where e must be an lvalue
589/// - e->name
590/// - *e, the type of e cannot be a function type
591/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000592/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000593/// - reference type [C++ [expr]]
594///
Chris Lattner25168a52008-07-26 21:30:36 +0000595Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000596 // first, check the type (C99 6.3.2.1). Expressions with function
597 // type in C are not lvalues, but they can be lvalues in C++.
598 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000599 return LV_NotObjectType;
600
Steve Naroffec7736d2008-02-10 01:39:04 +0000601 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000602 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000603 return LV_IncompleteVoidType;
604
Sebastian Redlce6fff02009-03-16 23:22:08 +0000605 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
Chris Lattner4b009652007-07-25 00:24:17 +0000606
607 // the type looks fine, now check the expression
608 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000609 case StringLiteralClass: // C99 6.5.1p4
610 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000611 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000612 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
613 // For vectors, make sure base is an lvalue (i.e. not a function call).
614 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000615 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000616 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000617 case DeclRefExprClass:
618 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000619 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
620 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000621 return LV_Valid;
622 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000623 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000624 case BlockDeclRefExprClass: {
625 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000626 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000627 return LV_Valid;
628 break;
629 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000630 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000631 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000632 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
633 NamedDecl *Member = m->getMemberDecl();
634 // C++ [expr.ref]p4:
635 // If E2 is declared to have type "reference to T", then E1.E2
636 // is an lvalue.
637 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
638 if (Value->getType()->isReferenceType())
639 return LV_Valid;
640
641 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor00660582009-03-11 20:22:50 +0000642 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor82d44772008-12-20 23:49:58 +0000643 return LV_Valid;
644
645 // -- If E2 is a non-static data member [...]. If E1 is an
646 // lvalue, then E1.E2 is an lvalue.
647 if (isa<FieldDecl>(Member))
648 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
649
650 // -- If it refers to a static member function [...], then
651 // E1.E2 is an lvalue.
652 // -- Otherwise, if E1.E2 refers to a non-static member
653 // function [...], then E1.E2 is not an lvalue.
654 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
655 return Method->isStatic()? LV_Valid : LV_MemberFunction;
656
657 // -- If E2 is a member enumerator [...], the expression E1.E2
658 // is not an lvalue.
659 if (isa<EnumConstantDecl>(Member))
660 return LV_InvalidExpression;
661
662 // Not an lvalue.
663 return LV_InvalidExpression;
664 }
665
666 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000667 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000668 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000669 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000670 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000671 return LV_Valid; // C99 6.5.3p4
672
673 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000674 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
675 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000676 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000677
678 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
679 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
680 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
681 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000682 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000683 case ImplicitCastExprClass:
684 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
685 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000686 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000687 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000688 case BinaryOperatorClass:
689 case CompoundAssignOperatorClass: {
690 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000691
692 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
693 BinOp->getOpcode() == BinaryOperator::Comma)
694 return BinOp->getRHS()->isLvalue(Ctx);
695
Sebastian Redl95216a62009-02-07 00:15:38 +0000696 // C++ [expr.mptr.oper]p6
697 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
698 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
699 !BinOp->getType()->isFunctionType())
700 return BinOp->getLHS()->isLvalue(Ctx);
701
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000702 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000703 return LV_InvalidExpression;
704
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000705 if (Ctx.getLangOptions().CPlusPlus)
706 // C++ [expr.ass]p1:
707 // The result of an assignment operation [...] is an lvalue.
708 return LV_Valid;
709
710
711 // C99 6.5.16:
712 // An assignment expression [...] is not an lvalue.
713 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000714 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000715 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000716 case CXXOperatorCallExprClass:
717 case CXXMemberCallExprClass: {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000718 // C++0x [expr.call]p10
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000719 // A function call is an lvalue if and only if the result type
Sebastian Redlce6fff02009-03-16 23:22:08 +0000720 // is an lvalue reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000721 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000722 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000723 CalleeType = FnTypePtr->getPointeeType();
724 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
Sebastian Redlce6fff02009-03-16 23:22:08 +0000725 if (FnType->getResultType()->isLValueReferenceType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000726 return LV_Valid;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000727
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000728 break;
729 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000730 case CompoundLiteralExprClass: // C99 6.5.2.5p5
731 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000732 case ChooseExprClass:
733 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000734 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000735 case ExtVectorElementExprClass:
736 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000737 return LV_DuplicateVectorComponents;
738 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000739 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
740 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000741 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
742 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000743 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000744 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000745 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000746 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000747 case VAArgExprClass:
Daniel Dunbar09a82e32009-02-12 09:21:08 +0000748 return LV_NotObjectType;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000749 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000750 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000751 case CXXConditionDeclExprClass:
752 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000753 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000754 case CXXFunctionalCastExprClass:
755 case CXXStaticCastExprClass:
756 case CXXDynamicCastExprClass:
757 case CXXReinterpretCastExprClass:
758 case CXXConstCastExprClass:
759 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redlce6fff02009-03-16 23:22:08 +0000760 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000761 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
762 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000763 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
764 isLValueReferenceType())
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000765 return LV_Valid;
766 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000767 case CXXTypeidExprClass:
768 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
769 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000770 default:
771 break;
772 }
773 return LV_InvalidExpression;
774}
775
776/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
777/// does not have an incomplete type, does not have a const-qualified type, and
778/// if it is a structure or union, does not have any member (including,
779/// recursively, any member or element of all contained aggregates or unions)
780/// with a const-qualified type.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000781Expr::isModifiableLvalueResult
782Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner25168a52008-07-26 21:30:36 +0000783 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000784
785 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000786 case LV_Valid:
787 // C++ 3.10p11: Functions cannot be modified, but pointers to
788 // functions can be modifiable.
789 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
790 return MLV_NotObjectType;
791 break;
792
Chris Lattner4b009652007-07-25 00:24:17 +0000793 case LV_NotObjectType: return MLV_NotObjectType;
794 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000795 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000796 case LV_InvalidExpression:
797 // If the top level is a C-style cast, and the subexpression is a valid
798 // lvalue, then this is probably a use of the old-school "cast as lvalue"
799 // GCC extension. We don't support it, but we want to produce good
800 // diagnostics when it happens so that the user knows why.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000801 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
802 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
803 if (Loc)
804 *Loc = CE->getLParenLoc();
Chris Lattner37fb9402008-11-17 19:51:54 +0000805 return MLV_LValueCast;
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000806 }
807 }
Chris Lattner37fb9402008-11-17 19:51:54 +0000808 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000809 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000810 }
Eli Friedman91571da2009-03-22 23:26:56 +0000811
812 // The following is illegal:
813 // void takeclosure(void (^C)(void));
814 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
815 //
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000816 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman91571da2009-03-22 23:26:56 +0000817 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
818 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
819 return MLV_NotBlockQualified;
820 }
821
Chris Lattnera1923f62008-08-04 07:31:14 +0000822 QualType CT = Ctx.getCanonicalType(getType());
823
824 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000825 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000826 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000827 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000828 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000829 return MLV_IncompleteType;
830
Chris Lattnera1923f62008-08-04 07:31:14 +0000831 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000832 if (r->hasConstFields())
833 return MLV_ConstQualified;
834 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000835
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000836 // Assigning to an 'implicit' property?
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000837 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000838 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
839 if (KVCExpr->getSetterMethod() == 0)
840 return MLV_NoSetterProperty;
841 }
Chris Lattner4b009652007-07-25 00:24:17 +0000842 return MLV_Valid;
843}
844
Ted Kremenek5778d622008-02-27 18:39:48 +0000845/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000846/// duration. This means that the address of this expression is a link-time
847/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000848bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000849 switch (getStmtClass()) {
850 default:
851 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000852 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000853 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000854 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000855 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000856 case CompoundLiteralExprClass:
857 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000858 case DeclRefExprClass:
859 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000860 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
861 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000862 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000863 if (isa<FunctionDecl>(D))
864 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000865 return false;
866 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000867 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000868 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000869 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000870 }
Chris Lattner743ec372007-11-27 21:35:27 +0000871 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000872 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000873 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000874 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000875 case CXXDefaultArgExprClass:
876 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000877 }
878}
879
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000880/// isOBJCGCCandidate - Check if an expression is objc gc'able.
881///
882bool Expr::isOBJCGCCandidate() const {
883 switch (getStmtClass()) {
884 default:
885 return false;
886 case ObjCIvarRefExprClass:
887 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000888 case Expr::UnaryOperatorClass:
889 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000890 case ParenExprClass:
891 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
892 case ImplicitCastExprClass:
893 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
894 case DeclRefExprClass:
895 case QualifiedDeclRefExprClass: {
896 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
897 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
898 return VD->hasGlobalStorage();
899 return false;
900 }
901 case MemberExprClass: {
902 const MemberExpr *M = cast<MemberExpr>(this);
903 return !M->isArrow() && M->getBase()->isOBJCGCCandidate();
904 }
905 case ArraySubscriptExprClass:
906 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
907 }
908}
Ted Kremenek87e30c52008-01-17 16:57:34 +0000909Expr* Expr::IgnoreParens() {
910 Expr* E = this;
911 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
912 E = P->getSubExpr();
913
914 return E;
915}
916
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000917/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
918/// or CastExprs or ImplicitCastExprs, returning their operand.
919Expr *Expr::IgnoreParenCasts() {
920 Expr *E = this;
921 while (true) {
922 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
923 E = P->getSubExpr();
924 else if (CastExpr *P = dyn_cast<CastExpr>(E))
925 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000926 else
927 return E;
928 }
929}
930
Chris Lattnerab0b8b12009-03-13 17:28:01 +0000931/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
932/// value (including ptr->int casts of the same size). Strip off any
933/// ParenExpr or CastExprs, returning their operand.
934Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
935 Expr *E = this;
936 while (true) {
937 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
938 E = P->getSubExpr();
939 continue;
940 }
941
942 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
943 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
944 // ptr<->int casts of the same width. We also ignore all identify casts.
945 Expr *SE = P->getSubExpr();
946
947 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
948 E = SE;
949 continue;
950 }
951
952 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
953 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
954 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
955 E = SE;
956 continue;
957 }
958 }
959
960 return E;
961 }
962}
963
964
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000965/// hasAnyTypeDependentArguments - Determines if any of the expressions
966/// in Exprs is type-dependent.
967bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
968 for (unsigned I = 0; I < NumExprs; ++I)
969 if (Exprs[I]->isTypeDependent())
970 return true;
971
972 return false;
973}
974
975/// hasAnyValueDependentArguments - Determines if any of the expressions
976/// in Exprs is value-dependent.
977bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
978 for (unsigned I = 0; I < NumExprs; ++I)
979 if (Exprs[I]->isValueDependent())
980 return true;
981
982 return false;
983}
984
Eli Friedmandee41122009-01-25 02:32:41 +0000985bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000986 // This function is attempting whether an expression is an initializer
987 // which can be evaluated at compile-time. isEvaluatable handles most
988 // of the cases, but it can't deal with some initializer-specific
989 // expressions, and it can't deal with aggregates; we deal with those here,
990 // and fall back to isEvaluatable for the other cases.
991
Eli Friedman3dc50ed2009-02-20 02:36:22 +0000992 // FIXME: This function assumes the variable being assigned to
993 // isn't a reference type!
994
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000995 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000996 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000997 case StringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +0000998 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000999 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +00001000 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001001 // This handles gcc's extension that allows global initializers like
1002 // "struct x {int x;} x = (struct x) {};".
1003 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +00001004 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +00001005 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +00001006 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001007 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001008 // FIXME: This doesn't deal with fields with reference types correctly.
1009 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1010 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001011 const InitListExpr *Exp = cast<InitListExpr>(this);
1012 unsigned numInits = Exp->getNumInits();
1013 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +00001014 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001015 return false;
1016 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001017 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001018 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001019 case ImplicitValueInitExprClass:
1020 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +00001021 case ParenExprClass: {
1022 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1023 }
1024 case UnaryOperatorClass: {
1025 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1026 if (Exp->getOpcode() == UnaryOperator::Extension)
1027 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1028 break;
1029 }
1030 case CStyleCastExprClass:
1031 // Handle casts with a destination that's a struct or union; this
1032 // deals with both the gcc no-op struct cast extension and the
1033 // cast-to-union extension.
1034 if (getType()->isRecordType())
1035 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1036 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001037 }
1038
Eli Friedman2b0dec52009-01-25 03:12:18 +00001039 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +00001040}
1041
Chris Lattner4b009652007-07-25 00:24:17 +00001042/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +00001043/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001044
1045/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1046/// comma, etc
1047///
Chris Lattner4b009652007-07-25 00:24:17 +00001048/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1049/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1050/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001051
Eli Friedman7beeda62009-02-26 09:29:13 +00001052// CheckICE - This function does the fundamental ICE checking: the returned
1053// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1054// Note that to reduce code duplication, this helper does no evaluation
1055// itself; the caller checks whether the expression is evaluatable, and
1056// in the rare cases where CheckICE actually cares about the evaluated
1057// value, it calls into Evalute.
1058//
1059// Meanings of Val:
1060// 0: This expression is an ICE if it can be evaluated by Evaluate.
1061// 1: This expression is not an ICE, but if it isn't evaluated, it's
1062// a legal subexpression for an ICE. This return value is used to handle
1063// the comma operator in C99 mode.
1064// 2: This expression is not an ICE, and is not a legal subexpression for one.
1065
1066struct ICEDiag {
1067 unsigned Val;
1068 SourceLocation Loc;
1069
1070 public:
1071 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1072 ICEDiag() : Val(0) {}
1073};
1074
1075ICEDiag NoDiag() { return ICEDiag(); }
1076
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001077static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1078 Expr::EvalResult EVResult;
1079 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1080 !EVResult.Val.isInt()) {
1081 return ICEDiag(2, E->getLocStart());
1082 }
1083 return NoDiag();
1084}
1085
Eli Friedman7beeda62009-02-26 09:29:13 +00001086static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson8b842c52009-03-14 00:33:21 +00001087 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001088 if (!E->getType()->isIntegralType()) {
1089 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +00001090 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001091
1092 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001093 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001094 return ICEDiag(2, E->getLocStart());
1095 case Expr::ParenExprClass:
1096 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1097 case Expr::IntegerLiteralClass:
1098 case Expr::CharacterLiteralClass:
1099 case Expr::CXXBoolLiteralExprClass:
1100 case Expr::CXXZeroInitValueExprClass:
1101 case Expr::TypesCompatibleExprClass:
1102 case Expr::UnaryTypeTraitExprClass:
1103 return NoDiag();
1104 case Expr::CallExprClass:
1105 case Expr::CXXOperatorCallExprClass: {
1106 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001107 if (CE->isBuiltinCall(Ctx))
1108 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +00001109 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001110 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001111 case Expr::DeclRefExprClass:
1112 case Expr::QualifiedDeclRefExprClass:
1113 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1114 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001115 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +00001116 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001117 // C++ 7.1.5.1p2
1118 // A variable of non-volatile const-qualified integral or enumeration
1119 // type initialized by an ICE can be used in ICEs.
1120 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +00001121 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001122 if (const Expr *Init = Dcl->getInit())
Eli Friedman7beeda62009-02-26 09:29:13 +00001123 return CheckICE(Init, Ctx);
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001124 }
1125 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001126 return ICEDiag(2, E->getLocStart());
1127 case Expr::UnaryOperatorClass: {
1128 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001129 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001130 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001131 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001132 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +00001133 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +00001134 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +00001135 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +00001136 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001137 case UnaryOperator::Real:
1138 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +00001139 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001140 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001141 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1142 // Evaluate matches the proposed gcc behavior for cases like
1143 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1144 // compliance: we should warn earlier for offsetof expressions with
1145 // array subscripts that aren't ICEs, and if the array subscripts
1146 // are ICEs, the value of the offsetof must be an integer constant.
1147 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001148 }
Chris Lattner4b009652007-07-25 00:24:17 +00001149 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001150 case Expr::SizeOfAlignOfExprClass: {
1151 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1152 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1153 return ICEDiag(2, E->getLocStart());
1154 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +00001155 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001156 case Expr::BinaryOperatorClass: {
1157 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001158 switch (Exp->getOpcode()) {
1159 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001160 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001161 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +00001162 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +00001163 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +00001164 case BinaryOperator::Add:
1165 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001166 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001167 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001168 case BinaryOperator::LT:
1169 case BinaryOperator::GT:
1170 case BinaryOperator::LE:
1171 case BinaryOperator::GE:
1172 case BinaryOperator::EQ:
1173 case BinaryOperator::NE:
1174 case BinaryOperator::And:
1175 case BinaryOperator::Xor:
1176 case BinaryOperator::Or:
1177 case BinaryOperator::Comma: {
1178 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1179 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001180 if (Exp->getOpcode() == BinaryOperator::Div ||
1181 Exp->getOpcode() == BinaryOperator::Rem) {
1182 // Evaluate gives an error for undefined Div/Rem, so make sure
1183 // we don't evaluate one.
1184 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1185 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1186 if (REval == 0)
1187 return ICEDiag(1, E->getLocStart());
1188 if (REval.isSigned() && REval.isAllOnesValue()) {
1189 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1190 if (LEval.isMinSignedValue())
1191 return ICEDiag(1, E->getLocStart());
1192 }
1193 }
1194 }
1195 if (Exp->getOpcode() == BinaryOperator::Comma) {
1196 if (Ctx.getLangOptions().C99) {
1197 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1198 // if it isn't evaluated.
1199 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1200 return ICEDiag(1, E->getLocStart());
1201 } else {
1202 // In both C89 and C++, commas in ICEs are illegal.
1203 return ICEDiag(2, E->getLocStart());
1204 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001205 }
1206 if (LHSResult.Val >= RHSResult.Val)
1207 return LHSResult;
1208 return RHSResult;
1209 }
Chris Lattner4b009652007-07-25 00:24:17 +00001210 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001211 case BinaryOperator::LOr: {
1212 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1213 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1214 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1215 // Rare case where the RHS has a comma "side-effect"; we need
1216 // to actually check the condition to see whether the side
1217 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001218 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001219 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001220 return RHSResult;
1221 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001222 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001223
Eli Friedman7beeda62009-02-26 09:29:13 +00001224 if (LHSResult.Val >= RHSResult.Val)
1225 return LHSResult;
1226 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001227 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001228 }
Chris Lattner4b009652007-07-25 00:24:17 +00001229 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001230 case Expr::ImplicitCastExprClass:
1231 case Expr::CStyleCastExprClass:
1232 case Expr::CXXFunctionalCastExprClass: {
1233 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1234 if (SubExpr->getType()->isIntegralType())
1235 return CheckICE(SubExpr, Ctx);
1236 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1237 return NoDiag();
1238 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001239 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001240 case Expr::ConditionalOperatorClass: {
1241 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001242 // If the condition (ignoring parens) is a __builtin_constant_p call,
1243 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001244 // expression, and it is fully evaluated. This is an important GNU
1245 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001246 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001247 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001248 Expr::EvalResult EVResult;
1249 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1250 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001251 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001252 }
1253 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001254 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001255 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1256 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1257 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1258 if (CondResult.Val == 2)
1259 return CondResult;
1260 if (TrueResult.Val == 2)
1261 return TrueResult;
1262 if (FalseResult.Val == 2)
1263 return FalseResult;
1264 if (CondResult.Val == 1)
1265 return CondResult;
1266 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1267 return NoDiag();
1268 // Rare case where the diagnostics depend on which side is evaluated
1269 // Note that if we get here, CondResult is 0, and at least one of
1270 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001271 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001272 return FalseResult;
1273 }
1274 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001275 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001276 case Expr::CXXDefaultArgExprClass:
1277 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001278 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001279 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001280 }
Chris Lattner4b009652007-07-25 00:24:17 +00001281 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001282}
Chris Lattner4b009652007-07-25 00:24:17 +00001283
Eli Friedman7beeda62009-02-26 09:29:13 +00001284bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1285 SourceLocation *Loc, bool isEvaluated) const {
1286 ICEDiag d = CheckICE(this, Ctx);
1287 if (d.Val != 0) {
1288 if (Loc) *Loc = d.Loc;
1289 return false;
1290 }
1291 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001292 if (!Evaluate(EvalResult, Ctx))
1293 assert(0 && "ICE cannot be evaluated!");
1294 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1295 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001296 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001297 return true;
1298}
1299
Chris Lattner4b009652007-07-25 00:24:17 +00001300/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1301/// integer constant expression with the value zero, or if this is one that is
1302/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001303bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1304{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001305 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001306 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001307 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001308 // Check that it is a cast to void*.
1309 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1310 QualType Pointee = PT->getPointeeType();
1311 if (Pointee.getCVRQualifiers() == 0 &&
1312 Pointee->isVoidType() && // to void*
1313 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001314 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001315 }
Chris Lattner4b009652007-07-25 00:24:17 +00001316 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001317 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1318 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001319 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001320 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1321 // Accept ((void*)0) as a null pointer constant, as many other
1322 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001323 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001324 } else if (const CXXDefaultArgExpr *DefaultArg
1325 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001326 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001327 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001328 } else if (isa<GNUNullExpr>(this)) {
1329 // The GNU __null extension is always a null pointer constant.
1330 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001331 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001332
Steve Naroffa2e53222008-01-14 16:10:57 +00001333 // This expression must be an integer type.
1334 if (!getType()->isIntegerType())
1335 return false;
1336
Chris Lattner4b009652007-07-25 00:24:17 +00001337 // If we have an integer constant expression, we need to *evaluate* it and
1338 // test for the value 0.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001339 // FIXME: We should probably return false if we're compiling in strict mode
1340 // and Diag is not null (this indicates that the value was foldable but not
1341 // an ICE.
1342 EvalResult Result;
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001343 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001344 Result.Val.isInt() && Result.Val.getInt() == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001345}
Steve Naroffc11705f2007-07-28 23:10:27 +00001346
Douglas Gregor81c29152008-10-29 00:13:59 +00001347/// isBitField - Return true if this expression is a bit-field.
1348bool Expr::isBitField() {
1349 Expr *E = this->IgnoreParenCasts();
1350 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001351 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1352 return Field->isBitField();
Douglas Gregor81c29152008-10-29 00:13:59 +00001353 return false;
1354}
1355
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001356/// isArrow - Return true if the base expression is a pointer to vector,
1357/// return false if the base expression is a vector.
1358bool ExtVectorElementExpr::isArrow() const {
1359 return getBase()->getType()->isPointerType();
1360}
1361
Nate Begemanaf6ed502008-04-18 23:10:10 +00001362unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001363 if (const VectorType *VT = getType()->getAsVectorType())
1364 return VT->getNumElements();
1365 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001366}
1367
Nate Begemanc8e51f82008-05-09 06:41:27 +00001368/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001369bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001370 const char *compStr = Accessor.getName();
Chris Lattner58d3fa52008-11-19 07:55:04 +00001371 unsigned length = Accessor.getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001372
1373 // Halving swizzles do not contain duplicate elements.
1374 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1375 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1376 return false;
1377
1378 // Advance past s-char prefix on hex swizzles.
1379 if (*compStr == 's') {
1380 compStr++;
1381 length--;
1382 }
Steve Naroffba67f692007-07-30 03:29:09 +00001383
Chris Lattner58d3fa52008-11-19 07:55:04 +00001384 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001385 const char *s = compStr+i;
1386 for (const char c = *s++; *s; s++)
1387 if (c == *s)
1388 return true;
1389 }
1390 return false;
1391}
Chris Lattner42158e72007-08-02 23:36:59 +00001392
Nate Begemanc8e51f82008-05-09 06:41:27 +00001393/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001394void ExtVectorElementExpr::getEncodedElementAccess(
1395 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner58d3fa52008-11-19 07:55:04 +00001396 const char *compStr = Accessor.getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001397 if (*compStr == 's')
1398 compStr++;
1399
1400 bool isHi = !strcmp(compStr, "hi");
1401 bool isLo = !strcmp(compStr, "lo");
1402 bool isEven = !strcmp(compStr, "even");
1403 bool isOdd = !strcmp(compStr, "odd");
1404
Nate Begemanc8e51f82008-05-09 06:41:27 +00001405 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1406 uint64_t Index;
1407
1408 if (isHi)
1409 Index = e + i;
1410 else if (isLo)
1411 Index = i;
1412 else if (isEven)
1413 Index = 2 * i;
1414 else if (isOdd)
1415 Index = 2 * i + 1;
1416 else
1417 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001418
Nate Begemana1ae7442008-05-13 21:03:02 +00001419 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001420 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001421}
1422
Steve Naroff4ed9d662007-09-27 14:38:14 +00001423// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001424ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001425 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001426 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001427 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001428 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001429 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001430 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001431 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001432 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001433 if (NumArgs) {
1434 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001435 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1436 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001437 LBracloc = LBrac;
1438 RBracloc = RBrac;
1439}
1440
Steve Naroff4ed9d662007-09-27 14:38:14 +00001441// constructor for class messages.
1442// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001443ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001444 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001445 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001446 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001447 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001448 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001449 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001450 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001451 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001452 if (NumArgs) {
1453 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001454 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1455 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001456 LBracloc = LBrac;
1457 RBracloc = RBrac;
1458}
1459
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001460// constructor for class messages.
1461ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1462 QualType retType, ObjCMethodDecl *mproto,
1463 SourceLocation LBrac, SourceLocation RBrac,
1464 Expr **ArgExprs, unsigned nargs)
1465: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1466MethodProto(mproto) {
1467 NumArgs = nargs;
1468 SubExprs = new Stmt*[NumArgs+1];
1469 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1470 if (NumArgs) {
1471 for (unsigned i = 0; i != NumArgs; ++i)
1472 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1473 }
1474 LBracloc = LBrac;
1475 RBracloc = RBrac;
1476}
1477
1478ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1479 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1480 switch (x & Flags) {
1481 default:
1482 assert(false && "Invalid ObjCMessageExpr.");
1483 case IsInstMeth:
1484 return ClassInfo(0, 0);
1485 case IsClsMethDeclUnknown:
1486 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1487 case IsClsMethDeclKnown: {
1488 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1489 return ClassInfo(D, D->getIdentifier());
1490 }
1491 }
1492}
1493
Chris Lattnerf624cd22007-10-25 00:29:32 +00001494bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001495 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001496}
1497
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001498void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1499 // Override default behavior of traversing children. If this has a type
1500 // operand and the type is a variable-length array, the child iteration
1501 // will iterate over the size expression. However, this expression belongs
1502 // to the type, not to this, so we don't want to delete it.
1503 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001504 if (isArgumentType()) {
1505 this->~SizeOfAlignOfExpr();
1506 C.Deallocate(this);
1507 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001508 else
1509 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001510}
1511
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001512//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001513// DesignatedInitExpr
1514//===----------------------------------------------------------------------===//
1515
1516IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1517 assert(Kind == FieldDesignator && "Only valid on a field designator");
1518 if (Field.NameOrField & 0x01)
1519 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1520 else
1521 return getField()->getIdentifier();
1522}
1523
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001524DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1525 const Designator *Designators,
1526 SourceLocation EqualOrColonLoc,
1527 bool GNUSyntax,
1528 unsigned NumSubExprs)
1529 : Expr(DesignatedInitExprClass, Ty),
1530 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1531 NumDesignators(NumDesignators), NumSubExprs(NumSubExprs) {
1532 this->Designators = new Designator[NumDesignators];
1533 for (unsigned I = 0; I != NumDesignators; ++I)
1534 this->Designators[I] = Designators[I];
1535}
1536
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001537DesignatedInitExpr *
1538DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1539 unsigned NumDesignators,
1540 Expr **IndexExprs, unsigned NumIndexExprs,
1541 SourceLocation ColonOrEqualLoc,
1542 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001543 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001544 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001545 DesignatedInitExpr *DIE
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001546 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001547 ColonOrEqualLoc, UsesColonSyntax,
1548 NumIndexExprs + 1);
1549
1550 // Fill in the designators
1551 unsigned ExpectedNumSubExprs = 0;
1552 designators_iterator Desig = DIE->designators_begin();
1553 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001554 if (Designators[Idx].isArrayDesignator())
1555 ++ExpectedNumSubExprs;
1556 else if (Designators[Idx].isArrayRangeDesignator())
1557 ExpectedNumSubExprs += 2;
1558 }
1559 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1560
1561 // Fill in the subexpressions, including the initializer expression.
1562 child_iterator Child = DIE->child_begin();
1563 *Child++ = Init;
1564 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1565 *Child = IndexExprs[Idx];
1566
1567 return DIE;
1568}
1569
1570SourceRange DesignatedInitExpr::getSourceRange() const {
1571 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001572 Designator &First =
1573 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001574 if (First.isFieldDesignator()) {
Douglas Gregor5f34f0e2009-03-28 00:41:23 +00001575 if (GNUSyntax)
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001576 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1577 else
1578 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1579 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001580 StartLoc =
1581 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001582 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1583}
1584
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001585Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1586 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1587 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1588 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001589 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1590 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1591}
1592
1593Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1594 assert(D.Kind == Designator::ArrayRangeDesignator &&
1595 "Requires array range designator");
1596 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1597 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001598 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1599 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1600}
1601
1602Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1603 assert(D.Kind == Designator::ArrayRangeDesignator &&
1604 "Requires array range designator");
1605 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1606 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001607 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1608 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1609}
1610
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001611/// \brief Replaces the designator at index @p Idx with the series
1612/// of designators in [First, Last).
1613void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1614 const Designator *First,
1615 const Designator *Last) {
1616 unsigned NumNewDesignators = Last - First;
1617 if (NumNewDesignators == 0) {
1618 std::copy_backward(Designators + Idx + 1,
1619 Designators + NumDesignators,
1620 Designators + Idx);
1621 --NumNewDesignators;
1622 return;
1623 } else if (NumNewDesignators == 1) {
1624 Designators[Idx] = *First;
1625 return;
1626 }
1627
1628 Designator *NewDesignators
1629 = new Designator[NumDesignators - 1 + NumNewDesignators];
1630 std::copy(Designators, Designators + Idx, NewDesignators);
1631 std::copy(First, Last, NewDesignators + Idx);
1632 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1633 NewDesignators + Idx + NumNewDesignators);
1634 delete [] Designators;
1635 Designators = NewDesignators;
1636 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1637}
1638
1639void DesignatedInitExpr::Destroy(ASTContext &C) {
1640 delete [] Designators;
1641 Expr::Destroy(C);
1642}
1643
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001644//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001645// ExprIterator.
1646//===----------------------------------------------------------------------===//
1647
1648Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1649Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1650Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1651const Expr* ConstExprIterator::operator[](size_t idx) const {
1652 return cast<Expr>(I[idx]);
1653}
1654const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1655const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1656
1657//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001658// Child Iterators for iterating over subexpressions/substatements
1659//===----------------------------------------------------------------------===//
1660
1661// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001662Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1663Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001664
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001665// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001666Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1667Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001668
Steve Naroff6f786252008-06-02 23:03:37 +00001669// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001670Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1671Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001672
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001673// ObjCKVCRefExpr
1674Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1675Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1676
Douglas Gregord8606632008-11-04 14:56:14 +00001677// ObjCSuperExpr
1678Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1679Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1680
Chris Lattner69909292008-08-10 01:53:14 +00001681// PredefinedExpr
1682Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1683Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001684
1685// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001686Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1687Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001688
1689// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001690Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001691Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001692
1693// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001694Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1695Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001696
Chris Lattner1de66eb2007-08-26 03:42:43 +00001697// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001698Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1699Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001700
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001701// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001702Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1703Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001704
1705// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001706Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1707Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001708
1709// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001710Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1711Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001712
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001713// SizeOfAlignOfExpr
1714Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1715 // If this is of a type and the type is a VLA type (and not a typedef), the
1716 // size expression of the VLA needs to be treated as an executable expression.
1717 // Why isn't this weirdness documented better in StmtIterator?
1718 if (isArgumentType()) {
1719 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1720 getArgumentType().getTypePtr()))
1721 return child_iterator(T);
1722 return child_iterator();
1723 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001724 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001725}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001726Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1727 if (isArgumentType())
1728 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001729 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001730}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001731
1732// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001733Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001734 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001735}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001736Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001737 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001738}
1739
1740// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001741Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001742 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001743}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001744Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001745 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001746}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001747
1748// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001749Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1750Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001751
Nate Begemanaf6ed502008-04-18 23:10:10 +00001752// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001753Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1754Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001755
1756// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001757Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1758Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001759
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001760// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001761Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1762Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001763
1764// BinaryOperator
1765Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001766 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001767}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001768Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001769 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001770}
1771
1772// ConditionalOperator
1773Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001774 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001775}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001776Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001777 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001778}
1779
1780// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001781Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1782Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001783
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001784// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001785Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1786Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001787
1788// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001789Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1790 return child_iterator();
1791}
1792
1793Stmt::child_iterator TypesCompatibleExpr::child_end() {
1794 return child_iterator();
1795}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001796
1797// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001798Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1799Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001800
Douglas Gregorad4b3792008-11-29 04:51:27 +00001801// GNUNullExpr
1802Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1803Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1804
Eli Friedmand0e9d092008-05-14 19:38:39 +00001805// ShuffleVectorExpr
1806Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001807 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001808}
1809Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001810 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001811}
1812
Anders Carlsson36760332007-10-15 20:28:48 +00001813// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001814Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1815Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001816
Anders Carlsson762b7c72007-08-31 04:56:16 +00001817// InitListExpr
1818Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001819 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001820}
1821Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001822 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001823}
1824
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001825// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001826Stmt::child_iterator DesignatedInitExpr::child_begin() {
1827 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1828 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001829 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1830}
1831Stmt::child_iterator DesignatedInitExpr::child_end() {
1832 return child_iterator(&*child_begin() + NumSubExprs);
1833}
1834
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001835// ImplicitValueInitExpr
1836Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1837 return child_iterator();
1838}
1839
1840Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1841 return child_iterator();
1842}
1843
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001844// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001845Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00001846 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00001847}
1848Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00001849 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00001850}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001851
1852// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001853Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1854Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001855
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001856// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001857Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1858 return child_iterator();
1859}
1860Stmt::child_iterator ObjCSelectorExpr::child_end() {
1861 return child_iterator();
1862}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001863
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001864// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001865Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1866 return child_iterator();
1867}
1868Stmt::child_iterator ObjCProtocolExpr::child_end() {
1869 return child_iterator();
1870}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001871
Steve Naroffc39ca262007-09-18 23:55:05 +00001872// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001873Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001874 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001875}
1876Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001877 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001878}
1879
Steve Naroff52a81c02008-09-03 18:15:37 +00001880// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001881Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1882Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001883
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001884Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1885Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }