blob: a8ea752a4a7ea6c4363dac4b6411d0dee98f7975 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000027#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000028using namespace clang;
29
Chris Lattner0eedafe2006-08-24 04:56:27 +000030//===----------------------------------------------------------------------===//
31// Primary Expressions.
32//===----------------------------------------------------------------------===//
33
Douglas Gregor4bd90e52009-10-23 18:54:35 +000034DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
35 SourceRange QualifierRange,
36 NamedDecl *D, SourceLocation NameLoc,
37 bool HasExplicitTemplateArgumentList,
38 SourceLocation LAngleLoc,
John McCall0ad16662009-10-29 08:12:44 +000039 const TemplateArgumentLoc *ExplicitTemplateArgs,
Douglas Gregor4bd90e52009-10-23 18:54:35 +000040 unsigned NumExplicitTemplateArgs,
41 SourceLocation RAngleLoc,
42 QualType T, bool TD, bool VD)
43 : Expr(DeclRefExprClass, T, TD, VD),
44 DecoratedD(D,
45 (Qualifier? HasQualifierFlag : 0) |
46 (HasExplicitTemplateArgumentList?
47 HasExplicitTemplateArgumentListFlag : 0)),
48 Loc(NameLoc) {
49 if (Qualifier) {
50 NameQualifier *NQ = getNameQualifier();
51 NQ->NNS = Qualifier;
52 NQ->Range = QualifierRange;
53 }
54
55 if (HasExplicitTemplateArgumentList) {
56 ExplicitTemplateArgumentList *ETemplateArgs
57 = getExplicitTemplateArgumentList();
58 ETemplateArgs->LAngleLoc = LAngleLoc;
59 ETemplateArgs->RAngleLoc = RAngleLoc;
60 ETemplateArgs->NumTemplateArgs = NumExplicitTemplateArgs;
61
John McCall0ad16662009-10-29 08:12:44 +000062 TemplateArgumentLoc *TemplateArgs = ETemplateArgs->getTemplateArgs();
Douglas Gregor4bd90e52009-10-23 18:54:35 +000063 for (unsigned I = 0; I < NumExplicitTemplateArgs; ++I)
John McCall0ad16662009-10-29 08:12:44 +000064 new (TemplateArgs + I) TemplateArgumentLoc(ExplicitTemplateArgs[I]);
Douglas Gregor4bd90e52009-10-23 18:54:35 +000065 }
66}
67
68DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
69 NestedNameSpecifier *Qualifier,
70 SourceRange QualifierRange,
71 NamedDecl *D,
72 SourceLocation NameLoc,
73 QualType T, bool TD, bool VD) {
74 return Create(Context, Qualifier, QualifierRange, D, NameLoc,
75 false, SourceLocation(), 0, 0, SourceLocation(),
76 T, TD, VD);
77}
78
79DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
80 NestedNameSpecifier *Qualifier,
81 SourceRange QualifierRange,
82 NamedDecl *D,
83 SourceLocation NameLoc,
84 bool HasExplicitTemplateArgumentList,
85 SourceLocation LAngleLoc,
John McCall0ad16662009-10-29 08:12:44 +000086 const TemplateArgumentLoc *ExplicitTemplateArgs,
Douglas Gregor4bd90e52009-10-23 18:54:35 +000087 unsigned NumExplicitTemplateArgs,
88 SourceLocation RAngleLoc,
89 QualType T, bool TD, bool VD) {
90 std::size_t Size = sizeof(DeclRefExpr);
91 if (Qualifier != 0)
92 Size += sizeof(NameQualifier);
93
94 if (HasExplicitTemplateArgumentList)
95 Size += sizeof(ExplicitTemplateArgumentList) +
John McCall0ad16662009-10-29 08:12:44 +000096 sizeof(TemplateArgumentLoc) * NumExplicitTemplateArgs;
Douglas Gregor4bd90e52009-10-23 18:54:35 +000097
98 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
99 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
100 HasExplicitTemplateArgumentList,
101 LAngleLoc,
102 ExplicitTemplateArgs,
103 NumExplicitTemplateArgs,
104 RAngleLoc,
105 T, TD, VD);
106}
107
108SourceRange DeclRefExpr::getSourceRange() const {
109 // FIXME: Does not handle multi-token names well, e.g., operator[].
110 SourceRange R(Loc);
111
112 if (hasQualifier())
113 R.setBegin(getQualifierRange().getBegin());
114 if (hasExplicitTemplateArgumentList())
115 R.setEnd(getRAngleLoc());
116 return R;
117}
118
Anders Carlsson2fb08242009-09-08 18:24:21 +0000119// FIXME: Maybe this should use DeclPrinter with a special "print predefined
120// expr" policy instead.
121std::string PredefinedExpr::ComputeName(ASTContext &Context, IdentType IT,
122 const Decl *CurrentDecl) {
123 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
124 if (IT != PrettyFunction)
125 return FD->getNameAsString();
126
127 llvm::SmallString<256> Name;
128 llvm::raw_svector_ostream Out(Name);
129
130 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
131 if (MD->isVirtual())
132 Out << "virtual ";
133 }
134
135 PrintingPolicy Policy(Context.getLangOptions());
136 Policy.SuppressTagKind = true;
137
138 std::string Proto = FD->getQualifiedNameAsString(Policy);
139
John McCall9dd450b2009-09-21 23:43:11 +0000140 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000141 const FunctionProtoType *FT = 0;
142 if (FD->hasWrittenPrototype())
143 FT = dyn_cast<FunctionProtoType>(AFT);
144
145 Proto += "(";
146 if (FT) {
147 llvm::raw_string_ostream POut(Proto);
148 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
149 if (i) POut << ", ";
150 std::string Param;
151 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
152 POut << Param;
153 }
154
155 if (FT->isVariadic()) {
156 if (FD->getNumParams()) POut << ", ";
157 POut << "...";
158 }
159 }
160 Proto += ")";
161
162 AFT->getResultType().getAsStringInternal(Proto, Policy);
163
164 Out << Proto;
165
166 Out.flush();
167 return Name.str().str();
168 }
169 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
170 llvm::SmallString<256> Name;
171 llvm::raw_svector_ostream Out(Name);
172 Out << (MD->isInstanceMethod() ? '-' : '+');
173 Out << '[';
174 Out << MD->getClassInterface()->getNameAsString();
175 if (const ObjCCategoryImplDecl *CID =
176 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
177 Out << '(';
178 Out << CID->getNameAsString();
179 Out << ')';
180 }
181 Out << ' ';
182 Out << MD->getSelector().getAsString();
183 Out << ']';
184
185 Out.flush();
186 return Name.str().str();
187 }
188 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
189 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
190 return "top level";
191 }
192 return "";
193}
194
Chris Lattnera0173132008-06-07 22:13:43 +0000195/// getValueAsApproximateDouble - This returns the value as an inaccurate
196/// double. Note that this may cause loss of precision, but is useful for
197/// debugging dumps, etc.
198double FloatingLiteral::getValueAsApproximateDouble() const {
199 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000200 bool ignored;
201 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
202 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000203 return V.convertToDouble();
204}
205
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000206StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
207 unsigned ByteLength, bool Wide,
208 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000209 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000210 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000211 // Allocate enough space for the StringLiteral plus an array of locations for
212 // any concatenated string tokens.
213 void *Mem = C.Allocate(sizeof(StringLiteral)+
214 sizeof(SourceLocation)*(NumStrs-1),
215 llvm::alignof<StringLiteral>());
216 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000217
Steve Naroffdf7855b2007-02-21 23:46:25 +0000218 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000219 char *AStrData = new (C, 1) char[ByteLength];
220 memcpy(AStrData, StrData, ByteLength);
221 SL->StrData = AStrData;
222 SL->ByteLength = ByteLength;
223 SL->IsWide = Wide;
224 SL->TokLocs[0] = Loc[0];
225 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000226
Chris Lattner630970d2009-02-18 05:49:11 +0000227 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000228 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
229 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000230}
231
Douglas Gregor958dfc92009-04-15 16:35:07 +0000232StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
233 void *Mem = C.Allocate(sizeof(StringLiteral)+
234 sizeof(SourceLocation)*(NumStrs-1),
235 llvm::alignof<StringLiteral>());
236 StringLiteral *SL = new (Mem) StringLiteral(QualType());
237 SL->StrData = 0;
238 SL->ByteLength = 0;
239 SL->NumConcatenated = NumStrs;
240 return SL;
241}
242
Douglas Gregore26a2852009-08-07 06:08:38 +0000243void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000244 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000245 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000246}
247
Daniel Dunbar36217882009-09-22 03:27:33 +0000248void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000249 if (StrData)
250 C.Deallocate(const_cast<char*>(StrData));
251
Daniel Dunbar36217882009-09-22 03:27:33 +0000252 char *AStrData = new (C, 1) char[Str.size()];
253 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000254 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000255 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000256}
257
Chris Lattner1b926492006-08-23 06:42:10 +0000258/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
259/// corresponds to, e.g. "sizeof" or "[pre]++".
260const char *UnaryOperator::getOpcodeStr(Opcode Op) {
261 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000262 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000263 case PostInc: return "++";
264 case PostDec: return "--";
265 case PreInc: return "++";
266 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000267 case AddrOf: return "&";
268 case Deref: return "*";
269 case Plus: return "+";
270 case Minus: return "-";
271 case Not: return "~";
272 case LNot: return "!";
273 case Real: return "__real";
274 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000275 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000276 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000277 }
278}
279
Mike Stump11289f42009-09-09 15:08:12 +0000280UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000281UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
282 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000283 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000284 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
285 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
286 case OO_Amp: return AddrOf;
287 case OO_Star: return Deref;
288 case OO_Plus: return Plus;
289 case OO_Minus: return Minus;
290 case OO_Tilde: return Not;
291 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000292 }
293}
294
295OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
296 switch (Opc) {
297 case PostInc: case PreInc: return OO_PlusPlus;
298 case PostDec: case PreDec: return OO_MinusMinus;
299 case AddrOf: return OO_Amp;
300 case Deref: return OO_Star;
301 case Plus: return OO_Plus;
302 case Minus: return OO_Minus;
303 case Not: return OO_Tilde;
304 case LNot: return OO_Exclaim;
305 default: return OO_None;
306 }
307}
308
309
Chris Lattner0eedafe2006-08-24 04:56:27 +0000310//===----------------------------------------------------------------------===//
311// Postfix Operators.
312//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000313
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000314CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000315 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000316 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000317 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000318 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000319 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000320
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000321 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000322 SubExprs[FN] = fn;
323 for (unsigned i = 0; i != numargs; ++i)
324 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000325
Douglas Gregor993603d2008-11-14 16:09:21 +0000326 RParenLoc = rparenloc;
327}
Nate Begeman1e36a852008-01-17 17:46:27 +0000328
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000329CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
330 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000331 : Expr(CallExprClass, t,
332 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000333 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000334 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000335
336 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000337 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000338 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000339 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000340
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000341 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000342}
343
Mike Stump11289f42009-09-09 15:08:12 +0000344CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
345 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000346 SubExprs = new (C) Stmt*[1];
347}
348
Douglas Gregore26a2852009-08-07 06:08:38 +0000349void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000350 DestroyChildren(C);
351 if (SubExprs) C.Deallocate(SubExprs);
352 this->~CallExpr();
353 C.Deallocate(this);
354}
355
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000356FunctionDecl *CallExpr::getDirectCallee() {
357 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000358 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000359 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000360
361 return 0;
362}
363
Chris Lattnere4407ed2007-12-28 05:25:02 +0000364/// setNumArgs - This changes the number of arguments present in this call.
365/// Any orphaned expressions are deleted by this, and any new operands are set
366/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000367void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000368 // No change, just return.
369 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000370
Chris Lattnere4407ed2007-12-28 05:25:02 +0000371 // If shrinking # arguments, just delete the extras and forgot them.
372 if (NumArgs < getNumArgs()) {
373 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek5a201952009-02-07 01:47:29 +0000374 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000375 this->NumArgs = NumArgs;
376 return;
377 }
378
379 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000380 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000381 // Copy over args.
382 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
383 NewSubExprs[i] = SubExprs[i];
384 // Null out new args.
385 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
386 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000387
Douglas Gregorba6e5572009-04-17 21:46:47 +0000388 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000389 SubExprs = NewSubExprs;
390 this->NumArgs = NumArgs;
391}
392
Chris Lattner01ff98a2008-10-06 05:00:53 +0000393/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
394/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000395unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000396 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000397 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000398 // ImplicitCastExpr.
399 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
400 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000401 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000402
Steve Narofff6e3b3292008-01-31 01:07:12 +0000403 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
404 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000405 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000406
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000407 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
408 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000409 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000410
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000411 if (!FDecl->getIdentifier())
412 return 0;
413
Douglas Gregor15fc9562009-09-12 00:22:50 +0000414 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000415}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000416
Anders Carlsson00a27592009-05-26 04:57:27 +0000417QualType CallExpr::getCallReturnType() const {
418 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000419 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000420 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000421 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000422 CalleeType = BPT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000423
John McCall9dd450b2009-09-21 23:43:11 +0000424 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000425 return FnType->getResultType();
426}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000427
Mike Stump11289f42009-09-09 15:08:12 +0000428MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
429 SourceRange qualrange, NamedDecl *memberdecl,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000430 SourceLocation l, bool has_explicit,
431 SourceLocation langle,
John McCall0ad16662009-10-29 08:12:44 +0000432 const TemplateArgumentLoc *targs, unsigned numtargs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000433 SourceLocation rangle, QualType ty)
Mike Stump11289f42009-09-09 15:08:12 +0000434 : Expr(MemberExprClass, ty,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000435 base->isTypeDependent() || (qual && qual->isDependent()),
436 base->isValueDependent() || (qual && qual->isDependent())),
437 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000438 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(has_explicit) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000439 // Initialize the qualifier, if any.
440 if (HasQualifier) {
441 NameQualifier *NQ = getMemberQualifier();
442 NQ->NNS = qual;
443 NQ->Range = qualrange;
444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000446 // Initialize the explicit template argument list, if any.
447 if (HasExplicitTemplateArgumentList) {
Mike Stump11289f42009-09-09 15:08:12 +0000448 ExplicitTemplateArgumentList *ETemplateArgs
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000449 = getExplicitTemplateArgumentList();
450 ETemplateArgs->LAngleLoc = langle;
451 ETemplateArgs->RAngleLoc = rangle;
452 ETemplateArgs->NumTemplateArgs = numtargs;
Mike Stump11289f42009-09-09 15:08:12 +0000453
John McCall0ad16662009-10-29 08:12:44 +0000454 TemplateArgumentLoc *TemplateArgs = ETemplateArgs->getTemplateArgs();
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000455 for (unsigned I = 0; I < numtargs; ++I)
John McCall0ad16662009-10-29 08:12:44 +0000456 new (TemplateArgs + I) TemplateArgumentLoc(targs[I]);
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000457 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000458}
459
Mike Stump11289f42009-09-09 15:08:12 +0000460MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
461 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000462 SourceRange qualrange,
Mike Stump11289f42009-09-09 15:08:12 +0000463 NamedDecl *memberdecl,
464 SourceLocation l,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000465 bool has_explicit,
466 SourceLocation langle,
John McCall0ad16662009-10-29 08:12:44 +0000467 const TemplateArgumentLoc *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000468 unsigned numtargs,
469 SourceLocation rangle,
470 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000471 std::size_t Size = sizeof(MemberExpr);
472 if (qual != 0)
473 Size += sizeof(NameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000474
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000475 if (has_explicit)
Mike Stump11289f42009-09-09 15:08:12 +0000476 Size += sizeof(ExplicitTemplateArgumentList) +
John McCall0ad16662009-10-29 08:12:44 +0000477 sizeof(TemplateArgumentLoc) * numtargs;
Mike Stump11289f42009-09-09 15:08:12 +0000478
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000479 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000480 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
481 has_explicit, langle, targs, numtargs, rangle,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000482 ty);
483}
484
Anders Carlsson496335e2009-09-03 00:59:21 +0000485const char *CastExpr::getCastKindName() const {
486 switch (getCastKind()) {
487 case CastExpr::CK_Unknown:
488 return "Unknown";
489 case CastExpr::CK_BitCast:
490 return "BitCast";
491 case CastExpr::CK_NoOp:
492 return "NoOp";
493 case CastExpr::CK_DerivedToBase:
494 return "DerivedToBase";
495 case CastExpr::CK_Dynamic:
496 return "Dynamic";
497 case CastExpr::CK_ToUnion:
498 return "ToUnion";
499 case CastExpr::CK_ArrayToPointerDecay:
500 return "ArrayToPointerDecay";
501 case CastExpr::CK_FunctionToPointerDecay:
502 return "FunctionToPointerDecay";
503 case CastExpr::CK_NullToMemberPointer:
504 return "NullToMemberPointer";
505 case CastExpr::CK_BaseToDerivedMemberPointer:
506 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000507 case CastExpr::CK_DerivedToBaseMemberPointer:
508 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000509 case CastExpr::CK_UserDefinedConversion:
510 return "UserDefinedConversion";
511 case CastExpr::CK_ConstructorConversion:
512 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000513 case CastExpr::CK_IntegralToPointer:
514 return "IntegralToPointer";
515 case CastExpr::CK_PointerToIntegral:
516 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000517 case CastExpr::CK_ToVoid:
518 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000519 case CastExpr::CK_VectorSplat:
520 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000521 case CastExpr::CK_IntegralCast:
522 return "IntegralCast";
523 case CastExpr::CK_IntegralToFloating:
524 return "IntegralToFloating";
525 case CastExpr::CK_FloatingToIntegral:
526 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000527 case CastExpr::CK_FloatingCast:
528 return "FloatingCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000529 }
Mike Stump11289f42009-09-09 15:08:12 +0000530
Anders Carlsson496335e2009-09-03 00:59:21 +0000531 assert(0 && "Unhandled cast kind!");
532 return 0;
533}
534
Chris Lattner1b926492006-08-23 06:42:10 +0000535/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
536/// corresponds to, e.g. "<<=".
537const char *BinaryOperator::getOpcodeStr(Opcode Op) {
538 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000539 case PtrMemD: return ".*";
540 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000541 case Mul: return "*";
542 case Div: return "/";
543 case Rem: return "%";
544 case Add: return "+";
545 case Sub: return "-";
546 case Shl: return "<<";
547 case Shr: return ">>";
548 case LT: return "<";
549 case GT: return ">";
550 case LE: return "<=";
551 case GE: return ">=";
552 case EQ: return "==";
553 case NE: return "!=";
554 case And: return "&";
555 case Xor: return "^";
556 case Or: return "|";
557 case LAnd: return "&&";
558 case LOr: return "||";
559 case Assign: return "=";
560 case MulAssign: return "*=";
561 case DivAssign: return "/=";
562 case RemAssign: return "%=";
563 case AddAssign: return "+=";
564 case SubAssign: return "-=";
565 case ShlAssign: return "<<=";
566 case ShrAssign: return ">>=";
567 case AndAssign: return "&=";
568 case XorAssign: return "^=";
569 case OrAssign: return "|=";
570 case Comma: return ",";
571 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000572
573 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000574}
Steve Naroff47500512007-04-19 23:00:49 +0000575
Mike Stump11289f42009-09-09 15:08:12 +0000576BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000577BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
578 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000579 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000580 case OO_Plus: return Add;
581 case OO_Minus: return Sub;
582 case OO_Star: return Mul;
583 case OO_Slash: return Div;
584 case OO_Percent: return Rem;
585 case OO_Caret: return Xor;
586 case OO_Amp: return And;
587 case OO_Pipe: return Or;
588 case OO_Equal: return Assign;
589 case OO_Less: return LT;
590 case OO_Greater: return GT;
591 case OO_PlusEqual: return AddAssign;
592 case OO_MinusEqual: return SubAssign;
593 case OO_StarEqual: return MulAssign;
594 case OO_SlashEqual: return DivAssign;
595 case OO_PercentEqual: return RemAssign;
596 case OO_CaretEqual: return XorAssign;
597 case OO_AmpEqual: return AndAssign;
598 case OO_PipeEqual: return OrAssign;
599 case OO_LessLess: return Shl;
600 case OO_GreaterGreater: return Shr;
601 case OO_LessLessEqual: return ShlAssign;
602 case OO_GreaterGreaterEqual: return ShrAssign;
603 case OO_EqualEqual: return EQ;
604 case OO_ExclaimEqual: return NE;
605 case OO_LessEqual: return LE;
606 case OO_GreaterEqual: return GE;
607 case OO_AmpAmp: return LAnd;
608 case OO_PipePipe: return LOr;
609 case OO_Comma: return Comma;
610 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000611 }
612}
613
614OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
615 static const OverloadedOperatorKind OverOps[] = {
616 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
617 OO_Star, OO_Slash, OO_Percent,
618 OO_Plus, OO_Minus,
619 OO_LessLess, OO_GreaterGreater,
620 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
621 OO_EqualEqual, OO_ExclaimEqual,
622 OO_Amp,
623 OO_Caret,
624 OO_Pipe,
625 OO_AmpAmp,
626 OO_PipePipe,
627 OO_Equal, OO_StarEqual,
628 OO_SlashEqual, OO_PercentEqual,
629 OO_PlusEqual, OO_MinusEqual,
630 OO_LessLessEqual, OO_GreaterGreaterEqual,
631 OO_AmpEqual, OO_CaretEqual,
632 OO_PipeEqual,
633 OO_Comma
634 };
635 return OverOps[Opc];
636}
637
Mike Stump11289f42009-09-09 15:08:12 +0000638InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000639 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000640 SourceLocation rbraceloc)
Douglas Gregorca1aeec2009-05-21 23:17:49 +0000641 : Expr(InitListExprClass, QualType(),
642 hasAnyTypeDependentArguments(initExprs, numInits),
643 hasAnyValueDependentArguments(initExprs, numInits)),
Mike Stump11289f42009-09-09 15:08:12 +0000644 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregorbf7207a2009-01-29 19:42:23 +0000645 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner07d754a2008-10-26 23:43:26 +0000646
647 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000648}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000649
Douglas Gregor6d00c992009-03-20 23:58:33 +0000650void InitListExpr::reserveInits(unsigned NumInits) {
651 if (NumInits > InitExprs.size())
652 InitExprs.reserve(NumInits);
653}
654
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000655void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattner8ba22472009-02-16 22:33:34 +0000656 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar45a2a202009-02-16 22:42:44 +0000657 Idx < LastIdx; ++Idx)
Douglas Gregor52a47e92009-03-20 23:38:03 +0000658 InitExprs[Idx]->Destroy(Context);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000659 InitExprs.resize(NumInits, 0);
660}
661
662Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
663 if (Init >= InitExprs.size()) {
664 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
665 InitExprs.back() = expr;
666 return 0;
667 }
Mike Stump11289f42009-09-09 15:08:12 +0000668
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000669 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
670 InitExprs[Init] = expr;
671 return Result;
672}
673
Steve Naroff991e99d2008-09-04 15:31:07 +0000674/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000675///
676const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000677 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000678 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000679}
680
Mike Stump11289f42009-09-09 15:08:12 +0000681SourceLocation BlockExpr::getCaretLocation() const {
682 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000683}
Mike Stump11289f42009-09-09 15:08:12 +0000684const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000685 return TheBlock->getBody();
686}
Mike Stump11289f42009-09-09 15:08:12 +0000687Stmt *BlockExpr::getBody() {
688 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000689}
Steve Naroff415d3d52008-10-08 17:01:13 +0000690
691
Chris Lattner1ec5f562007-06-27 05:38:08 +0000692//===----------------------------------------------------------------------===//
693// Generic Expression Routines
694//===----------------------------------------------------------------------===//
695
Chris Lattner237f2752009-02-14 07:37:35 +0000696/// isUnusedResultAWarning - Return true if this immediate expression should
697/// be warned about if the result is unused. If so, fill in Loc and Ranges
698/// with location to warn on and the source range[s] to report with the
699/// warning.
700bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000701 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000702 // Don't warn if the expr is type dependent. The type could end up
703 // instantiating to void.
704 if (isTypeDependent())
705 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000706
Chris Lattner1ec5f562007-06-27 05:38:08 +0000707 switch (getStmtClass()) {
708 default:
Chris Lattner237f2752009-02-14 07:37:35 +0000709 Loc = getExprLoc();
710 R1 = getSourceRange();
711 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000712 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000713 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000714 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000715 case UnaryOperatorClass: {
716 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000717
Chris Lattner1ec5f562007-06-27 05:38:08 +0000718 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000719 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000720 case UnaryOperator::PostInc:
721 case UnaryOperator::PostDec:
722 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000723 case UnaryOperator::PreDec: // ++/--
724 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000725 case UnaryOperator::Deref:
726 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000727 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000728 return false;
729 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000730 case UnaryOperator::Real:
731 case UnaryOperator::Imag:
732 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000733 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
734 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000735 return false;
736 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000737 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000738 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000739 }
Chris Lattner237f2752009-02-14 07:37:35 +0000740 Loc = UO->getOperatorLoc();
741 R1 = UO->getSubExpr()->getSourceRange();
742 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000743 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000744 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000745 const BinaryOperator *BO = cast<BinaryOperator>(this);
746 // Consider comma to have side effects if the LHS or RHS does.
747 if (BO->getOpcode() == BinaryOperator::Comma)
Mike Stump53f9ded2009-11-03 23:25:48 +0000748 return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
749 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattner237f2752009-02-14 07:37:35 +0000751 if (BO->isAssignmentOp())
752 return false;
753 Loc = BO->getOperatorLoc();
754 R1 = BO->getLHS()->getSourceRange();
755 R2 = BO->getRHS()->getSourceRange();
756 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000757 }
Chris Lattner86928112007-08-25 02:00:02 +0000758 case CompoundAssignOperatorClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000759 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000760
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000761 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000762 // The condition must be evaluated, but if either the LHS or RHS is a
763 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000764 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000765 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +0000766 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +0000767 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +0000768 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000769 }
770
Chris Lattnera44d1162007-06-27 05:58:59 +0000771 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000772 // If the base pointer or element is to a volatile pointer/field, accessing
773 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000774 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000775 return false;
776 Loc = cast<MemberExpr>(this)->getMemberLoc();
777 R1 = SourceRange(Loc, Loc);
778 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
779 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000780
Chris Lattner1ec5f562007-06-27 05:38:08 +0000781 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000782 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +0000783 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000784 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000785 return false;
786 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
787 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
788 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
789 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +0000790
Chris Lattner1ec5f562007-06-27 05:38:08 +0000791 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +0000792 case CXXOperatorCallExprClass:
793 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000794 // If this is a direct call, get the callee.
795 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner1a6babf2009-10-13 04:53:48 +0000796 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000797 // If the callee has attribute pure, const, or warn_unused_result, warn
798 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +0000799 //
800 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
801 // updated to match for QoI.
802 if (FD->getAttr<WarnUnusedResultAttr>() ||
803 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
804 Loc = CE->getCallee()->getLocStart();
805 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000806
Chris Lattner1a6babf2009-10-13 04:53:48 +0000807 if (unsigned NumArgs = CE->getNumArgs())
808 R2 = SourceRange(CE->getArg(0)->getLocStart(),
809 CE->getArg(NumArgs-1)->getLocEnd());
810 return true;
811 }
Chris Lattner237f2752009-02-14 07:37:35 +0000812 }
813 return false;
814 }
Chris Lattnere6d9ca52007-09-26 22:06:30 +0000815 case ObjCMessageExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000816 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000817
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000818 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000819#if 0
Mike Stump11289f42009-09-09 15:08:12 +0000820 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000821 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000822 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +0000823 Loc = Ref->getLocation();
824 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
825 if (Ref->getBase())
826 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +0000827#else
828 Loc = getExprLoc();
829 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000830#endif
831 return true;
832 }
Chris Lattner944d3062008-07-26 19:51:01 +0000833 case StmtExprClass: {
834 // Statement exprs don't logically have side effects themselves, but are
835 // sometimes used in macros in ways that give them a type that is unused.
836 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
837 // however, if the result of the stmt expr is dead, we don't want to emit a
838 // warning.
839 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
840 if (!CS->body_empty())
841 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +0000842 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +0000843
Chris Lattner237f2752009-02-14 07:37:35 +0000844 Loc = cast<StmtExpr>(this)->getLParenLoc();
845 R1 = getSourceRange();
846 return true;
Chris Lattner944d3062008-07-26 19:51:01 +0000847 }
Douglas Gregorf19b2312008-10-28 15:36:24 +0000848 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +0000849 // If this is an explicit cast to void, allow it. People do this when they
850 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +0000851 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +0000852 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000853 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
854 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
855 return true;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000856 case CXXFunctionalCastExprClass:
Chris Lattner1ec5f562007-06-27 05:38:08 +0000857 // If this is a cast to void, check the operand. Otherwise, the result of
858 // the cast is unused.
859 if (getType()->isVoidType())
Mike Stump53f9ded2009-11-03 23:25:48 +0000860 return (cast<CastExpr>(this)->getSubExpr()
861 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +0000862 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
863 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
864 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000865
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000866 case ImplicitCastExprClass:
867 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +0000868 return (cast<ImplicitCastExpr>(this)
869 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000870
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000871 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000872 return (cast<CXXDefaultArgExpr>(this)
873 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +0000874
875 case CXXNewExprClass:
876 // FIXME: In theory, there might be new expressions that don't have side
877 // effects (e.g. a placement new with an uninitialized POD).
878 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000879 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +0000880 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000881 return (cast<CXXBindTemporaryExpr>(this)
882 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +0000883 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000884 return (cast<CXXExprWithTemporaries>(this)
885 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +0000886 }
Chris Lattner1ec5f562007-06-27 05:38:08 +0000887}
888
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000889/// DeclCanBeLvalue - Determine whether the given declaration can be
890/// an lvalue. This is a helper routine for isLvalue.
891static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor5101c242008-12-05 18:15:24 +0000892 // C++ [temp.param]p6:
893 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +0000894 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor5101c242008-12-05 18:15:24 +0000895 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
896 return NTTParm->getType()->isReferenceType();
897
Douglas Gregor91f84212008-12-11 16:49:14 +0000898 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000899 // C++ 3.10p2: An lvalue refers to an object or function.
900 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor9b146582009-07-08 20:55:45 +0000901 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
902 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000903}
904
Steve Naroff475cca02007-05-14 17:19:29 +0000905/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
906/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000907/// - name, where name must be a variable
908/// - e[i]
909/// - (e), where e must be an lvalue
910/// - e.name, where e must be an lvalue
911/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000912/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000913/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +0000914/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +0000915/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +0000916///
Chris Lattner67315442008-07-26 21:30:36 +0000917Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmanb8c4fd82009-05-03 22:36:05 +0000918 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
919
920 isLvalueResult Res = isLvalueInternal(Ctx);
921 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
922 return Res;
923
Douglas Gregor9a657932008-10-21 23:43:52 +0000924 // first, check the type (C99 6.3.2.1). Expressions with function
925 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor9b146582009-07-08 20:55:45 +0000926 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Steve Naroff9358c712007-05-27 23:58:33 +0000927 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +0000928
Steve Naroff1018ea32008-02-10 01:39:04 +0000929 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall8ccfcb52009-09-24 19:53:00 +0000930 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +0000931 return LV_IncompleteVoidType;
932
Eli Friedmanb8c4fd82009-05-03 22:36:05 +0000933 return LV_Valid;
934}
Bill Wendlingdfc81072007-07-17 03:52:31 +0000935
Eli Friedmanb8c4fd82009-05-03 22:36:05 +0000936// Check whether the expression can be sanely treated like an l-value
937Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Steve Naroff47500512007-04-19 23:00:49 +0000938 switch (getStmtClass()) {
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000939 case StringLiteralClass: // C99 6.5.1p4
940 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7a9a38a2007-11-30 22:47:59 +0000941 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +0000942 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +0000943 // For vectors, make sure base is an lvalue (i.e. not a function call).
944 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +0000945 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +0000946 return LV_Valid;
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000947 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000948 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
949 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +0000950 return LV_Valid;
951 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +0000952 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +0000953 case BlockDeclRefExprClass: {
954 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +0000955 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +0000956 return LV_Valid;
957 break;
958 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000959 case MemberExprClass: {
Steve Naroff47500512007-04-19 23:00:49 +0000960 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000961 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
962 NamedDecl *Member = m->getMemberDecl();
963 // C++ [expr.ref]p4:
964 // If E2 is declared to have type "reference to T", then E1.E2
965 // is an lvalue.
966 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
967 if (Value->getType()->isReferenceType())
968 return LV_Valid;
969
970 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor212cab32009-03-11 20:22:50 +0000971 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000972 return LV_Valid;
973
974 // -- If E2 is a non-static data member [...]. If E1 is an
975 // lvalue, then E1.E2 is an lvalue.
976 if (isa<FieldDecl>(Member))
977 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
978
979 // -- If it refers to a static member function [...], then
980 // E1.E2 is an lvalue.
981 // -- Otherwise, if E1.E2 refers to a non-static member
982 // function [...], then E1.E2 is not an lvalue.
983 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
984 return Method->isStatic()? LV_Valid : LV_MemberFunction;
985
986 // -- If E2 is a member enumerator [...], the expression E1.E2
987 // is not an lvalue.
988 if (isa<EnumConstantDecl>(Member))
989 return LV_InvalidExpression;
990
991 // Not an lvalue.
992 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +0000993 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000994
995 // C99 6.5.2.3p4
Chris Lattner67315442008-07-26 21:30:36 +0000996 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +0000997 }
Chris Lattner595db862007-10-30 22:53:42 +0000998 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +0000999 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +00001000 return LV_Valid; // C99 6.5.3p4
1001
1002 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +00001003 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1004 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +00001005 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +00001006
1007 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1008 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1009 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1010 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +00001011 break;
Douglas Gregora11693b2008-11-12 17:17:38 +00001012 case ImplicitCastExprClass:
Mike Stump11289f42009-09-09 15:08:12 +00001013 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregora11693b2008-11-12 17:17:38 +00001014 : LV_InvalidExpression;
Steve Naroff475cca02007-05-14 17:19:29 +00001015 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +00001016 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +00001017 case BinaryOperatorClass:
1018 case CompoundAssignOperatorClass: {
1019 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +00001020
1021 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1022 BinOp->getOpcode() == BinaryOperator::Comma)
1023 return BinOp->getRHS()->isLvalue(Ctx);
1024
Sebastian Redl112a97662009-02-07 00:15:38 +00001025 // C++ [expr.mptr.oper]p6
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001026 // The result of a .* expression is an lvalue only if its first operand is
1027 // an lvalue and its second operand is a pointer to data member.
1028 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl112a97662009-02-07 00:15:38 +00001029 !BinOp->getType()->isFunctionType())
1030 return BinOp->getLHS()->isLvalue(Ctx);
1031
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001032 // The result of an ->* expression is an lvalue only if its second operand
1033 // is a pointer to data member.
1034 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1035 !BinOp->getType()->isFunctionType()) {
1036 QualType Ty = BinOp->getRHS()->getType();
1037 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1038 return LV_Valid;
1039 }
1040
Douglas Gregor58e008d2008-11-13 20:12:29 +00001041 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +00001042 return LV_InvalidExpression;
1043
Douglas Gregor58e008d2008-11-13 20:12:29 +00001044 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +00001045 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +00001046 // The result of an assignment operation [...] is an lvalue.
1047 return LV_Valid;
1048
1049
1050 // C99 6.5.16:
1051 // An assignment expression [...] is not an lvalue.
1052 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +00001053 }
Mike Stump11289f42009-09-09 15:08:12 +00001054 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001055 case CXXOperatorCallExprClass:
1056 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001057 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +00001058 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001059 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +00001060 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1061 if (ReturnType->isLValueReferenceType())
1062 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001063
Douglas Gregor6b754842008-10-28 00:22:11 +00001064 break;
1065 }
Steve Naroff2644aaf2007-12-05 04:00:10 +00001066 case CompoundLiteralExprClass: // C99 6.5.2.5p5
1067 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +00001068 case ChooseExprClass:
1069 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001070 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +00001071 case ExtVectorElementExprClass:
1072 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +00001073 return LV_DuplicateVectorComponents;
1074 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +00001075 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1076 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +00001077 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1078 return LV_Valid;
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001079 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +00001080 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +00001081 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +00001082 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001083 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +00001084 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis0fdbd6c2008-09-11 04:22:26 +00001085 case CXXConditionDeclExprClass:
1086 return LV_Valid;
Douglas Gregorf19b2312008-10-28 15:36:24 +00001087 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +00001088 case CXXFunctionalCastExprClass:
1089 case CXXStaticCastExprClass:
1090 case CXXDynamicCastExprClass:
1091 case CXXReinterpretCastExprClass:
1092 case CXXConstCastExprClass:
1093 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001094 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +00001095 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1096 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001097 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1098 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +00001099 return LV_Valid;
1100 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001101 case CXXTypeidExprClass:
1102 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1103 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +00001104 case CXXBindTemporaryExprClass:
1105 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1106 isLvalueInternal(Ctx);
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001107 case ConditionalOperatorClass: {
1108 // Complicated handling is only for C++.
1109 if (!Ctx.getLangOptions().CPlusPlus)
1110 return LV_InvalidExpression;
1111
1112 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1113 // everywhere there's an object converted to an rvalue. Also, any other
1114 // casts should be wrapped by ImplicitCastExprs. There's just the special
1115 // case involving throws to work out.
1116 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001117 Expr *True = Cond->getTrueExpr();
1118 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001119 // C++0x 5.16p2
1120 // If either the second or the third operand has type (cv) void, [...]
1121 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001122 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001123 return LV_InvalidExpression;
1124
1125 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001126 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001127 return LV_InvalidExpression;
1128
1129 // That's it.
1130 return LV_Valid;
1131 }
1132
Douglas Gregord3319842009-10-24 04:59:53 +00001133 case TemplateIdRefExprClass: {
1134 const TemplateIdRefExpr *TID = cast<TemplateIdRefExpr>(this);
1135 TemplateName Template = TID->getTemplateName();
1136 NamedDecl *ND = Template.getAsTemplateDecl();
1137 if (!ND)
1138 ND = Template.getAsOverloadedFunctionDecl();
1139 if (ND && DeclCanBeLvalue(ND, Ctx))
1140 return LV_Valid;
1141
1142 break;
1143 }
1144
Steve Naroff9358c712007-05-27 23:58:33 +00001145 default:
1146 break;
Steve Naroff47500512007-04-19 23:00:49 +00001147 }
Steve Naroff9358c712007-05-27 23:58:33 +00001148 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001149}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001150
Steve Naroff475cca02007-05-14 17:19:29 +00001151/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1152/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001153/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001154/// recursively, any member or element of all contained aggregates or unions)
1155/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001156Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001157Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001158 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001159
Steve Naroff9358c712007-05-27 23:58:33 +00001160 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001161 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001162 // C++ 3.10p11: Functions cannot be modified, but pointers to
1163 // functions can be modifiable.
1164 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1165 return MLV_NotObjectType;
1166 break;
1167
Chris Lattner1ec5f562007-06-27 05:38:08 +00001168 case LV_NotObjectType: return MLV_NotObjectType;
1169 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001170 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001171 case LV_InvalidExpression:
1172 // If the top level is a C-style cast, and the subexpression is a valid
1173 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1174 // GCC extension. We don't support it, but we want to produce good
1175 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001176 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1177 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1178 if (Loc)
1179 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001180 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001181 }
1182 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001183 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001184 case LV_MemberFunction: return MLV_MemberFunction;
Steve Naroff9358c712007-05-27 23:58:33 +00001185 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001186
1187 // The following is illegal:
1188 // void takeclosure(void (^C)(void));
1189 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1190 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001191 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001192 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1193 return MLV_NotBlockQualified;
1194 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001195
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001196 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001197 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001198 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1199 if (Expr->getSetterMethod() == 0)
1200 return MLV_NoSetterProperty;
1201 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001202
Chris Lattner7adf0762008-08-04 07:31:14 +00001203 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001204
Chris Lattner7adf0762008-08-04 07:31:14 +00001205 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001206 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001207 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001208 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001209 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001210 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001211
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001212 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001213 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001214 return MLV_ConstQualified;
1215 }
Mike Stump11289f42009-09-09 15:08:12 +00001216
Mike Stump11289f42009-09-09 15:08:12 +00001217 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001218}
1219
Fariborz Jahanian07735332009-02-22 18:40:18 +00001220/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001221/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001222bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001223 switch (getStmtClass()) {
1224 default:
1225 return false;
1226 case ObjCIvarRefExprClass:
1227 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001228 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001229 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001230 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001231 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001232 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001233 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001234 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001235 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001236 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001237 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001238 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1239 if (VD->hasGlobalStorage())
1240 return true;
1241 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001242 // dereferencing to a pointer is always a gc'able candidate,
1243 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001244 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001245 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001246 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001247 return false;
1248 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001249 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001250 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001251 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001252 }
1253 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001254 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001255 }
1256}
Ted Kremenekfff70962008-01-17 16:57:34 +00001257Expr* Expr::IgnoreParens() {
1258 Expr* E = this;
1259 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1260 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001261
Ted Kremenekfff70962008-01-17 16:57:34 +00001262 return E;
1263}
1264
Chris Lattnerf2660962008-02-13 01:02:39 +00001265/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1266/// or CastExprs or ImplicitCastExprs, returning their operand.
1267Expr *Expr::IgnoreParenCasts() {
1268 Expr *E = this;
1269 while (true) {
1270 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1271 E = P->getSubExpr();
1272 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1273 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001274 else
1275 return E;
1276 }
1277}
1278
Chris Lattneref26c772009-03-13 17:28:01 +00001279/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1280/// value (including ptr->int casts of the same size). Strip off any
1281/// ParenExpr or CastExprs, returning their operand.
1282Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1283 Expr *E = this;
1284 while (true) {
1285 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1286 E = P->getSubExpr();
1287 continue;
1288 }
Mike Stump11289f42009-09-09 15:08:12 +00001289
Chris Lattneref26c772009-03-13 17:28:01 +00001290 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1291 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1292 // ptr<->int casts of the same width. We also ignore all identify casts.
1293 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001294
Chris Lattneref26c772009-03-13 17:28:01 +00001295 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1296 E = SE;
1297 continue;
1298 }
Mike Stump11289f42009-09-09 15:08:12 +00001299
Chris Lattneref26c772009-03-13 17:28:01 +00001300 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1301 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1302 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1303 E = SE;
1304 continue;
1305 }
1306 }
Mike Stump11289f42009-09-09 15:08:12 +00001307
Chris Lattneref26c772009-03-13 17:28:01 +00001308 return E;
1309 }
1310}
1311
1312
Douglas Gregor4619e432008-12-05 23:32:09 +00001313/// hasAnyTypeDependentArguments - Determines if any of the expressions
1314/// in Exprs is type-dependent.
1315bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1316 for (unsigned I = 0; I < NumExprs; ++I)
1317 if (Exprs[I]->isTypeDependent())
1318 return true;
1319
1320 return false;
1321}
1322
1323/// hasAnyValueDependentArguments - Determines if any of the expressions
1324/// in Exprs is value-dependent.
1325bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1326 for (unsigned I = 0; I < NumExprs; ++I)
1327 if (Exprs[I]->isValueDependent())
1328 return true;
1329
1330 return false;
1331}
1332
Eli Friedman7139af42009-01-25 02:32:41 +00001333bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001334 // This function is attempting whether an expression is an initializer
1335 // which can be evaluated at compile-time. isEvaluatable handles most
1336 // of the cases, but it can't deal with some initializer-specific
1337 // expressions, and it can't deal with aggregates; we deal with those here,
1338 // and fall back to isEvaluatable for the other cases.
1339
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001340 // FIXME: This function assumes the variable being assigned to
1341 // isn't a reference type!
1342
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001343 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001344 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001345 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001346 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001347 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001348 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001349 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001350 // This handles gcc's extension that allows global initializers like
1351 // "struct x {int x;} x = (struct x) {};".
1352 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001353 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001354 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001355 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001356 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001357 // FIXME: This doesn't deal with fields with reference types correctly.
1358 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1359 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001360 const InitListExpr *Exp = cast<InitListExpr>(this);
1361 unsigned numInits = Exp->getNumInits();
1362 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001363 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001364 return false;
1365 }
Eli Friedman384da272009-01-25 03:12:18 +00001366 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001367 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001368 case ImplicitValueInitExprClass:
1369 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001370 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001371 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001372 case UnaryOperatorClass: {
1373 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1374 if (Exp->getOpcode() == UnaryOperator::Extension)
1375 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1376 break;
1377 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001378 case BinaryOperatorClass: {
1379 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1380 // but this handles the common case.
1381 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1382 if (Exp->getOpcode() == BinaryOperator::Sub &&
1383 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1384 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1385 return true;
1386 break;
1387 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001388 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001389 case CStyleCastExprClass:
1390 // Handle casts with a destination that's a struct or union; this
1391 // deals with both the gcc no-op struct cast extension and the
1392 // cast-to-union extension.
1393 if (getType()->isRecordType())
1394 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001395
1396 // Integer->integer casts can be handled here, which is important for
1397 // things like (int)(&&x-&&y). Scary but true.
1398 if (getType()->isIntegerType() &&
1399 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1400 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1401
Eli Friedman384da272009-01-25 03:12:18 +00001402 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001403 }
Eli Friedman384da272009-01-25 03:12:18 +00001404 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001405}
1406
Chris Lattner1f4479e2007-06-05 04:15:44 +00001407/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman98c56a42009-02-26 09:29:13 +00001408/// an integer constant expression.
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001409
1410/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1411/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +00001412///
Chris Lattnerd7372ba2007-07-18 05:21:20 +00001413/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1414/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1415/// cast+dereference.
Daniel Dunbar4750e632009-02-18 00:47:45 +00001416
Eli Friedman98c56a42009-02-26 09:29:13 +00001417// CheckICE - This function does the fundamental ICE checking: the returned
1418// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1419// Note that to reduce code duplication, this helper does no evaluation
Mike Stump11289f42009-09-09 15:08:12 +00001420// itself; the caller checks whether the expression is evaluatable, and
Eli Friedman98c56a42009-02-26 09:29:13 +00001421// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump11289f42009-09-09 15:08:12 +00001422// value, it calls into Evalute.
Eli Friedman98c56a42009-02-26 09:29:13 +00001423//
1424// Meanings of Val:
1425// 0: This expression is an ICE if it can be evaluated by Evaluate.
1426// 1: This expression is not an ICE, but if it isn't evaluated, it's
1427// a legal subexpression for an ICE. This return value is used to handle
1428// the comma operator in C99 mode.
1429// 2: This expression is not an ICE, and is not a legal subexpression for one.
1430
1431struct ICEDiag {
1432 unsigned Val;
1433 SourceLocation Loc;
1434
1435 public:
1436 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1437 ICEDiag() : Val(0) {}
1438};
1439
1440ICEDiag NoDiag() { return ICEDiag(); }
1441
Eli Friedman90afd3d2009-02-27 04:07:58 +00001442static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1443 Expr::EvalResult EVResult;
1444 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1445 !EVResult.Val.isInt()) {
1446 return ICEDiag(2, E->getLocStart());
1447 }
1448 return NoDiag();
1449}
1450
Eli Friedman98c56a42009-02-26 09:29:13 +00001451static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson54b26982009-03-14 00:33:21 +00001452 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001453 if (!E->getType()->isIntegralType()) {
1454 return ICEDiag(2, E->getLocStart());
Eli Friedman5a332ea2008-11-13 06:09:17 +00001455 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001456
1457 switch (E->getStmtClass()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001458#define STMT(Node, Base) case Expr::Node##Class:
1459#define EXPR(Node, Base)
1460#include "clang/AST/StmtNodes.def"
1461 case Expr::PredefinedExprClass:
1462 case Expr::FloatingLiteralClass:
1463 case Expr::ImaginaryLiteralClass:
1464 case Expr::StringLiteralClass:
1465 case Expr::ArraySubscriptExprClass:
1466 case Expr::MemberExprClass:
1467 case Expr::CompoundAssignOperatorClass:
1468 case Expr::CompoundLiteralExprClass:
1469 case Expr::ExtVectorElementExprClass:
1470 case Expr::InitListExprClass:
1471 case Expr::DesignatedInitExprClass:
1472 case Expr::ImplicitValueInitExprClass:
1473 case Expr::ParenListExprClass:
1474 case Expr::VAArgExprClass:
1475 case Expr::AddrLabelExprClass:
1476 case Expr::StmtExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001477 case Expr::CXXMemberCallExprClass:
1478 case Expr::CXXDynamicCastExprClass:
1479 case Expr::CXXTypeidExprClass:
1480 case Expr::CXXNullPtrLiteralExprClass:
1481 case Expr::CXXThisExprClass:
1482 case Expr::CXXThrowExprClass:
1483 case Expr::CXXConditionDeclExprClass: // FIXME: is this correct?
1484 case Expr::CXXNewExprClass:
1485 case Expr::CXXDeleteExprClass:
1486 case Expr::CXXPseudoDestructorExprClass:
1487 case Expr::UnresolvedFunctionNameExprClass:
1488 case Expr::UnresolvedDeclRefExprClass:
1489 case Expr::TemplateIdRefExprClass:
1490 case Expr::CXXConstructExprClass:
1491 case Expr::CXXBindTemporaryExprClass:
1492 case Expr::CXXExprWithTemporariesClass:
1493 case Expr::CXXTemporaryObjectExprClass:
1494 case Expr::CXXUnresolvedConstructExprClass:
1495 case Expr::CXXUnresolvedMemberExprClass:
1496 case Expr::ObjCStringLiteralClass:
1497 case Expr::ObjCEncodeExprClass:
1498 case Expr::ObjCMessageExprClass:
1499 case Expr::ObjCSelectorExprClass:
1500 case Expr::ObjCProtocolExprClass:
1501 case Expr::ObjCIvarRefExprClass:
1502 case Expr::ObjCPropertyRefExprClass:
1503 case Expr::ObjCImplicitSetterGetterRefExprClass:
1504 case Expr::ObjCSuperExprClass:
1505 case Expr::ObjCIsaExprClass:
1506 case Expr::ShuffleVectorExprClass:
1507 case Expr::BlockExprClass:
1508 case Expr::BlockDeclRefExprClass:
1509 case Expr::NoStmtClass:
1510 case Expr::ExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001511 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001512
Douglas Gregor73341c42009-09-11 00:18:58 +00001513 case Expr::GNUNullExprClass:
1514 // GCC considers the GNU __null value to be an integral constant expression.
1515 return NoDiag();
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001516
Eli Friedman98c56a42009-02-26 09:29:13 +00001517 case Expr::ParenExprClass:
1518 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1519 case Expr::IntegerLiteralClass:
1520 case Expr::CharacterLiteralClass:
1521 case Expr::CXXBoolLiteralExprClass:
1522 case Expr::CXXZeroInitValueExprClass:
1523 case Expr::TypesCompatibleExprClass:
1524 case Expr::UnaryTypeTraitExprClass:
1525 return NoDiag();
Mike Stump11289f42009-09-09 15:08:12 +00001526 case Expr::CallExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001527 case Expr::CXXOperatorCallExprClass: {
1528 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001529 if (CE->isBuiltinCall(Ctx))
1530 return CheckEvalInICE(E, Ctx);
Eli Friedman98c56a42009-02-26 09:29:13 +00001531 return ICEDiag(2, E->getLocStart());
Chris Lattner5c4664e2007-07-15 23:32:58 +00001532 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001533 case Expr::DeclRefExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001534 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1535 return NoDiag();
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001536 if (Ctx.getLangOptions().CPlusPlus &&
John McCall8ccfcb52009-09-24 19:53:00 +00001537 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001538 // C++ 7.1.5.1p2
1539 // A variable of non-volatile const-qualified integral or enumeration
1540 // type initialized by an ICE can be used in ICEs.
1541 if (const VarDecl *Dcl =
Eli Friedman98c56a42009-02-26 09:29:13 +00001542 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001543 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1544 if (Quals.hasVolatile() || !Quals.hasConst())
1545 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1546
1547 // Look for the definition of this variable, which will actually have
1548 // an initializer.
1549 const VarDecl *Def = 0;
1550 const Expr *Init = Dcl->getDefinition(Def);
1551 if (Init) {
1552 if (Def->isInitKnownICE()) {
1553 // We have already checked whether this subexpression is an
1554 // integral constant expression.
1555 if (Def->isInitICE())
1556 return NoDiag();
1557 else
1558 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1559 }
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001560
Douglas Gregor0840cc02009-11-01 20:32:48 +00001561 // C++ [class.static.data]p4:
1562 // If a static data member is of const integral or const
1563 // enumeration type, its declaration in the class definition can
1564 // specify a constant-initializer which shall be an integral
1565 // constant expression (5.19). In that case, the member can appear
1566 // in integral constant expressions.
1567 if (Def->isOutOfLine()) {
1568 Dcl->setInitKnownICE(Ctx, false);
1569 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1570 }
1571
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001572 ICEDiag Result = CheckICE(Init, Ctx);
1573 // Cache the result of the ICE test.
1574 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1575 return Result;
1576 }
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001577 }
1578 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001579 return ICEDiag(2, E->getLocStart());
1580 case Expr::UnaryOperatorClass: {
1581 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001582 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001583 case UnaryOperator::PostInc:
1584 case UnaryOperator::PostDec:
1585 case UnaryOperator::PreInc:
1586 case UnaryOperator::PreDec:
1587 case UnaryOperator::AddrOf:
1588 case UnaryOperator::Deref:
Eli Friedman98c56a42009-02-26 09:29:13 +00001589 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001590
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001591 case UnaryOperator::Extension:
Eli Friedman98c56a42009-02-26 09:29:13 +00001592 case UnaryOperator::LNot:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001593 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001594 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001595 case UnaryOperator::Not:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001596 case UnaryOperator::Real:
1597 case UnaryOperator::Imag:
Eli Friedman98c56a42009-02-26 09:29:13 +00001598 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001599 case UnaryOperator::OffsetOf:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001600 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1601 // Evaluate matches the proposed gcc behavior for cases like
1602 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1603 // compliance: we should warn earlier for offsetof expressions with
1604 // array subscripts that aren't ICEs, and if the array subscripts
1605 // are ICEs, the value of the offsetof must be an integer constant.
1606 return CheckEvalInICE(E, Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001607 }
Steve Naroff8eeeb132007-05-08 21:09:37 +00001608 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001609 case Expr::SizeOfAlignOfExprClass: {
1610 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1611 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1612 return ICEDiag(2, E->getLocStart());
1613 return NoDiag();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001614 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001615 case Expr::BinaryOperatorClass: {
1616 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001617 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001618 case BinaryOperator::PtrMemD:
1619 case BinaryOperator::PtrMemI:
1620 case BinaryOperator::Assign:
1621 case BinaryOperator::MulAssign:
1622 case BinaryOperator::DivAssign:
1623 case BinaryOperator::RemAssign:
1624 case BinaryOperator::AddAssign:
1625 case BinaryOperator::SubAssign:
1626 case BinaryOperator::ShlAssign:
1627 case BinaryOperator::ShrAssign:
1628 case BinaryOperator::AndAssign:
1629 case BinaryOperator::XorAssign:
1630 case BinaryOperator::OrAssign:
Eli Friedman98c56a42009-02-26 09:29:13 +00001631 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001632
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001633 case BinaryOperator::Mul:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001634 case BinaryOperator::Div:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001635 case BinaryOperator::Rem:
Eli Friedman98c56a42009-02-26 09:29:13 +00001636 case BinaryOperator::Add:
1637 case BinaryOperator::Sub:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001638 case BinaryOperator::Shl:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001639 case BinaryOperator::Shr:
Eli Friedman98c56a42009-02-26 09:29:13 +00001640 case BinaryOperator::LT:
1641 case BinaryOperator::GT:
1642 case BinaryOperator::LE:
1643 case BinaryOperator::GE:
1644 case BinaryOperator::EQ:
1645 case BinaryOperator::NE:
1646 case BinaryOperator::And:
1647 case BinaryOperator::Xor:
1648 case BinaryOperator::Or:
1649 case BinaryOperator::Comma: {
1650 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1651 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001652 if (Exp->getOpcode() == BinaryOperator::Div ||
1653 Exp->getOpcode() == BinaryOperator::Rem) {
1654 // Evaluate gives an error for undefined Div/Rem, so make sure
1655 // we don't evaluate one.
1656 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1657 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1658 if (REval == 0)
1659 return ICEDiag(1, E->getLocStart());
1660 if (REval.isSigned() && REval.isAllOnesValue()) {
1661 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1662 if (LEval.isMinSignedValue())
1663 return ICEDiag(1, E->getLocStart());
1664 }
1665 }
1666 }
1667 if (Exp->getOpcode() == BinaryOperator::Comma) {
1668 if (Ctx.getLangOptions().C99) {
1669 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1670 // if it isn't evaluated.
1671 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1672 return ICEDiag(1, E->getLocStart());
1673 } else {
1674 // In both C89 and C++, commas in ICEs are illegal.
1675 return ICEDiag(2, E->getLocStart());
1676 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001677 }
1678 if (LHSResult.Val >= RHSResult.Val)
1679 return LHSResult;
1680 return RHSResult;
1681 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001682 case BinaryOperator::LAnd:
Eli Friedman98c56a42009-02-26 09:29:13 +00001683 case BinaryOperator::LOr: {
1684 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1685 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1686 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1687 // Rare case where the RHS has a comma "side-effect"; we need
1688 // to actually check the condition to see whether the side
1689 // with the comma is evaluated.
Eli Friedman98c56a42009-02-26 09:29:13 +00001690 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman90afd3d2009-02-27 04:07:58 +00001691 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman98c56a42009-02-26 09:29:13 +00001692 return RHSResult;
1693 return NoDiag();
Eli Friedman8553a982008-11-13 02:13:11 +00001694 }
Eli Friedman90afd3d2009-02-27 04:07:58 +00001695
Eli Friedman98c56a42009-02-26 09:29:13 +00001696 if (LHSResult.Val >= RHSResult.Val)
1697 return LHSResult;
1698 return RHSResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001699 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001700 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001701 }
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001702 case Expr::CastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001703 case Expr::ImplicitCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001704 case Expr::ExplicitCastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001705 case Expr::CStyleCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001706 case Expr::CXXFunctionalCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001707 case Expr::CXXNamedCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001708 case Expr::CXXStaticCastExprClass:
1709 case Expr::CXXReinterpretCastExprClass:
1710 case Expr::CXXConstCastExprClass: {
Eli Friedman98c56a42009-02-26 09:29:13 +00001711 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1712 if (SubExpr->getType()->isIntegralType())
1713 return CheckICE(SubExpr, Ctx);
1714 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1715 return NoDiag();
1716 return ICEDiag(2, E->getLocStart());
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001717 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001718 case Expr::ConditionalOperatorClass: {
1719 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00001720 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner85b25bc2008-12-12 06:55:44 +00001721 // then only the true side is actually considered in an integer constant
Chris Lattner04397352008-12-12 18:00:51 +00001722 // expression, and it is fully evaluated. This is an important GNU
1723 // extension. See GCC PR38377 for discussion.
Eli Friedman98c56a42009-02-26 09:29:13 +00001724 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregore711f702009-02-14 18:57:46 +00001725 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001726 Expr::EvalResult EVResult;
1727 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1728 !EVResult.Val.isInt()) {
Eli Friedman90afd3d2009-02-27 04:07:58 +00001729 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001730 }
1731 return NoDiag();
Chris Lattner04397352008-12-12 18:00:51 +00001732 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001733 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1734 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1735 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1736 if (CondResult.Val == 2)
1737 return CondResult;
1738 if (TrueResult.Val == 2)
1739 return TrueResult;
1740 if (FalseResult.Val == 2)
1741 return FalseResult;
1742 if (CondResult.Val == 1)
1743 return CondResult;
1744 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1745 return NoDiag();
1746 // Rare case where the diagnostics depend on which side is evaluated
1747 // Note that if we get here, CondResult is 0, and at least one of
1748 // TrueResult and FalseResult is non-zero.
Eli Friedman90afd3d2009-02-27 04:07:58 +00001749 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001750 return FalseResult;
1751 }
1752 return TrueResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001753 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001754 case Expr::CXXDefaultArgExprClass:
1755 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001756 case Expr::ChooseExprClass: {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001757 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001758 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001759 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001760
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001761 // Silence a GCC warning
1762 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001763}
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001764
Eli Friedman98c56a42009-02-26 09:29:13 +00001765bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1766 SourceLocation *Loc, bool isEvaluated) const {
1767 ICEDiag d = CheckICE(this, Ctx);
1768 if (d.Val != 0) {
1769 if (Loc) *Loc = d.Loc;
1770 return false;
1771 }
1772 EvalResult EvalResult;
Eli Friedman90afd3d2009-02-27 04:07:58 +00001773 if (!Evaluate(EvalResult, Ctx))
Douglas Gregor0840cc02009-11-01 20:32:48 +00001774 llvm::llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman90afd3d2009-02-27 04:07:58 +00001775 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1776 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001777 Result = EvalResult.Val.getInt();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001778 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00001779}
1780
Chris Lattner7eef9192007-05-24 01:23:49 +00001781/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1782/// integer constant expression with the value zero, or if this is one that is
1783/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001784bool Expr::isNullPointerConstant(ASTContext &Ctx,
1785 NullPointerConstantValueDependence NPC) const {
1786 if (isValueDependent()) {
1787 switch (NPC) {
1788 case NPC_NeverValueDependent:
1789 assert(false && "Unexpected value dependent expression!");
1790 // If the unthinkable happens, fall through to the safest alternative.
1791
1792 case NPC_ValueDependentIsNull:
1793 return isTypeDependent() || getType()->isIntegralType();
1794
1795 case NPC_ValueDependentIsNotNull:
1796 return false;
1797 }
1798 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001799
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001800 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001801 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001802 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001803 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001804 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001805 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001806 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001807 Pointee->isVoidType() && // to void*
1808 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001809 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001810 }
Steve Naroffada7d422007-05-20 17:54:12 +00001811 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001812 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1813 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001814 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001815 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1816 // Accept ((void*)0) as a null pointer constant, as many other
1817 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001818 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001819 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001820 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001821 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001822 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001823 } else if (isa<GNUNullExpr>(this)) {
1824 // The GNU __null extension is always a null pointer constant.
1825 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001826 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001827
Sebastian Redl576fd422009-05-10 18:38:11 +00001828 // C++0x nullptr_t is always a null pointer constant.
1829 if (getType()->isNullPtrType())
1830 return true;
1831
Steve Naroff4871fe02008-01-14 16:10:57 +00001832 // This expression must be an integer type.
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001833 if (!getType()->isIntegerType() ||
1834 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001835 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001836
Chris Lattner1abbd412007-06-08 17:58:43 +00001837 // If we have an integer constant expression, we need to *evaluate* it and
1838 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001839 llvm::APSInt Result;
1840 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001841}
Steve Narofff7a5da12007-07-28 23:10:27 +00001842
Douglas Gregor71235ec2009-05-02 02:18:30 +00001843FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001844 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001845
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001846 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001847 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001848 if (Field->isBitField())
1849 return Field;
1850
1851 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1852 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1853 return BinOp->getLHS()->getBitField();
1854
1855 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001856}
1857
Chris Lattnerb8211f62009-02-16 22:14:05 +00001858/// isArrow - Return true if the base expression is a pointer to vector,
1859/// return false if the base expression is a vector.
1860bool ExtVectorElementExpr::isArrow() const {
1861 return getBase()->getType()->isPointerType();
1862}
1863
Nate Begemance4d7fc2008-04-18 23:10:10 +00001864unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001865 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001866 return VT->getNumElements();
1867 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001868}
1869
Nate Begemanf322eab2008-05-09 06:41:27 +00001870/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001871bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001872 // FIXME: Refactor this code to an accessor on the AST node which returns the
1873 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001874 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001875
1876 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001877 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001878 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001879
Nate Begeman7e5185b2009-01-18 02:01:21 +00001880 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001881 if (Comp[0] == 's' || Comp[0] == 'S')
1882 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001883
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001884 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1885 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001886 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001887
Steve Naroff0d595ca2007-07-30 03:29:09 +00001888 return false;
1889}
Chris Lattner885b4952007-08-02 23:36:59 +00001890
Nate Begemanf322eab2008-05-09 06:41:27 +00001891/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001892void ExtVectorElementExpr::getEncodedElementAccess(
1893 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001894 llvm::StringRef Comp = Accessor->getName();
1895 if (Comp[0] == 's' || Comp[0] == 'S')
1896 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001897
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001898 bool isHi = Comp == "hi";
1899 bool isLo = Comp == "lo";
1900 bool isEven = Comp == "even";
1901 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001902
Nate Begemanf322eab2008-05-09 06:41:27 +00001903 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1904 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001905
Nate Begemanf322eab2008-05-09 06:41:27 +00001906 if (isHi)
1907 Index = e + i;
1908 else if (isLo)
1909 Index = i;
1910 else if (isEven)
1911 Index = 2 * i;
1912 else if (isOdd)
1913 Index = 2 * i + 1;
1914 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001915 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001916
Nate Begemand3862152008-05-13 21:03:02 +00001917 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001918 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001919}
1920
Steve Narofff73590d2007-09-27 14:38:14 +00001921// constructor for instance messages.
Steve Naroff80175062007-09-28 22:22:11 +00001922ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001923 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00001924 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001925 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00001926 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00001927 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001928 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00001929 SubExprs = new Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00001930 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001931 if (NumArgs) {
1932 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00001933 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1934 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001935 LBracloc = LBrac;
1936 RBracloc = RBrac;
1937}
1938
Mike Stump11289f42009-09-09 15:08:12 +00001939// constructor for class messages.
Steve Narofff73590d2007-09-27 14:38:14 +00001940// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff80175062007-09-28 22:22:11 +00001941ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001942 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00001943 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001944 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00001945 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00001946 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001947 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00001948 SubExprs = new Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001949 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001950 if (NumArgs) {
1951 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00001952 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1953 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001954 LBracloc = LBrac;
1955 RBracloc = RBrac;
1956}
1957
Mike Stump11289f42009-09-09 15:08:12 +00001958// constructor for class messages.
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001959ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1960 QualType retType, ObjCMethodDecl *mproto,
1961 SourceLocation LBrac, SourceLocation RBrac,
1962 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00001963: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001964MethodProto(mproto) {
1965 NumArgs = nargs;
1966 SubExprs = new Stmt*[NumArgs+1];
1967 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1968 if (NumArgs) {
1969 for (unsigned i = 0; i != NumArgs; ++i)
1970 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1971 }
1972 LBracloc = LBrac;
1973 RBracloc = RBrac;
1974}
1975
1976ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1977 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1978 switch (x & Flags) {
1979 default:
1980 assert(false && "Invalid ObjCMessageExpr.");
1981 case IsInstMeth:
1982 return ClassInfo(0, 0);
1983 case IsClsMethDeclUnknown:
1984 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1985 case IsClsMethDeclKnown: {
1986 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1987 return ClassInfo(D, D->getIdentifier());
1988 }
1989 }
1990}
1991
Chris Lattner7ec71da2009-04-26 00:44:05 +00001992void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1993 if (CI.first == 0 && CI.second == 0)
1994 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1995 else if (CI.first == 0)
1996 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1997 else
1998 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1999}
2000
2001
Chris Lattner35e564e2007-10-25 00:29:32 +00002002bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002003 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002004}
2005
Nate Begeman48745922009-08-12 02:28:50 +00002006void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2007 unsigned NumExprs) {
2008 if (SubExprs) C.Deallocate(SubExprs);
2009
2010 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002011 this->NumExprs = NumExprs;
2012 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002013}
Nate Begeman48745922009-08-12 02:28:50 +00002014
2015void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2016 DestroyChildren(C);
2017 if (SubExprs) C.Deallocate(SubExprs);
2018 this->~ShuffleVectorExpr();
2019 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002020}
2021
Douglas Gregore26a2852009-08-07 06:08:38 +00002022void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002023 // Override default behavior of traversing children. If this has a type
2024 // operand and the type is a variable-length array, the child iteration
2025 // will iterate over the size expression. However, this expression belongs
2026 // to the type, not to this, so we don't want to delete it.
2027 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002028 if (isArgumentType()) {
2029 this->~SizeOfAlignOfExpr();
2030 C.Deallocate(this);
2031 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002032 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002033 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002034}
2035
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002036//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002037// DesignatedInitExpr
2038//===----------------------------------------------------------------------===//
2039
2040IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2041 assert(Kind == FieldDesignator && "Only valid on a field designator");
2042 if (Field.NameOrField & 0x01)
2043 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2044 else
2045 return getField()->getIdentifier();
2046}
2047
Mike Stump11289f42009-09-09 15:08:12 +00002048DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002049 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002050 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002051 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002052 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002053 unsigned NumIndexExprs,
2054 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002055 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002056 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002057 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2058 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002059 this->Designators = new Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002060
2061 // Record the initializer itself.
2062 child_iterator Child = child_begin();
2063 *Child++ = Init;
2064
2065 // Copy the designators and their subexpressions, computing
2066 // value-dependence along the way.
2067 unsigned IndexIdx = 0;
2068 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002069 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002070
2071 if (this->Designators[I].isArrayDesignator()) {
2072 // Compute type- and value-dependence.
2073 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002074 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002075 Index->isTypeDependent() || Index->isValueDependent();
2076
2077 // Copy the index expressions into permanent storage.
2078 *Child++ = IndexExprs[IndexIdx++];
2079 } else if (this->Designators[I].isArrayRangeDesignator()) {
2080 // Compute type- and value-dependence.
2081 Expr *Start = IndexExprs[IndexIdx];
2082 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002083 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002084 Start->isTypeDependent() || Start->isValueDependent() ||
2085 End->isTypeDependent() || End->isValueDependent();
2086
2087 // Copy the start/end expressions into permanent storage.
2088 *Child++ = IndexExprs[IndexIdx++];
2089 *Child++ = IndexExprs[IndexIdx++];
2090 }
2091 }
2092
2093 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002094}
2095
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002096DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002097DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002098 unsigned NumDesignators,
2099 Expr **IndexExprs, unsigned NumIndexExprs,
2100 SourceLocation ColonOrEqualLoc,
2101 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002102 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002103 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002104 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
2105 ColonOrEqualLoc, UsesColonSyntax,
2106 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002107}
2108
Mike Stump11289f42009-09-09 15:08:12 +00002109DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002110 unsigned NumIndexExprs) {
2111 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2112 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2113 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2114}
2115
Mike Stump11289f42009-09-09 15:08:12 +00002116void DesignatedInitExpr::setDesignators(const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002117 unsigned NumDesigs) {
2118 if (Designators)
2119 delete [] Designators;
2120
2121 Designators = new Designator[NumDesigs];
2122 NumDesignators = NumDesigs;
2123 for (unsigned I = 0; I != NumDesigs; ++I)
2124 Designators[I] = Desigs[I];
2125}
2126
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002127SourceRange DesignatedInitExpr::getSourceRange() const {
2128 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002129 Designator &First =
2130 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002131 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002132 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002133 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2134 else
2135 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2136 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002137 StartLoc =
2138 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002139 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2140}
2141
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002142Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2143 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2144 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2145 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002146 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2147 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2148}
2149
2150Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002151 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002152 "Requires array range designator");
2153 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2154 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002155 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2156 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2157}
2158
2159Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002160 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002161 "Requires array range designator");
2162 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2163 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002164 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2165 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2166}
2167
Douglas Gregord5846a12009-04-15 06:41:24 +00002168/// \brief Replaces the designator at index @p Idx with the series
2169/// of designators in [First, Last).
Mike Stump11289f42009-09-09 15:08:12 +00002170void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
2171 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002172 const Designator *Last) {
2173 unsigned NumNewDesignators = Last - First;
2174 if (NumNewDesignators == 0) {
2175 std::copy_backward(Designators + Idx + 1,
2176 Designators + NumDesignators,
2177 Designators + Idx);
2178 --NumNewDesignators;
2179 return;
2180 } else if (NumNewDesignators == 1) {
2181 Designators[Idx] = *First;
2182 return;
2183 }
2184
Mike Stump11289f42009-09-09 15:08:12 +00002185 Designator *NewDesignators
Douglas Gregord5846a12009-04-15 06:41:24 +00002186 = new Designator[NumDesignators - 1 + NumNewDesignators];
2187 std::copy(Designators, Designators + Idx, NewDesignators);
2188 std::copy(First, Last, NewDesignators + Idx);
2189 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2190 NewDesignators + Idx + NumNewDesignators);
2191 delete [] Designators;
2192 Designators = NewDesignators;
2193 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2194}
2195
Douglas Gregore26a2852009-08-07 06:08:38 +00002196void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002197 delete [] Designators;
Douglas Gregore26a2852009-08-07 06:08:38 +00002198 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002199}
2200
Mike Stump11289f42009-09-09 15:08:12 +00002201ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002202 Expr **exprs, unsigned nexprs,
2203 SourceLocation rparenloc)
2204: Expr(ParenListExprClass, QualType(),
2205 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002206 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002207 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002208
Nate Begeman5ec4b312009-08-10 23:49:36 +00002209 Exprs = new (C) Stmt*[nexprs];
2210 for (unsigned i = 0; i != nexprs; ++i)
2211 Exprs[i] = exprs[i];
2212}
2213
2214void ParenListExpr::DoDestroy(ASTContext& C) {
2215 DestroyChildren(C);
2216 if (Exprs) C.Deallocate(Exprs);
2217 this->~ParenListExpr();
2218 C.Deallocate(this);
2219}
2220
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002221//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002222// ExprIterator.
2223//===----------------------------------------------------------------------===//
2224
2225Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2226Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2227Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2228const Expr* ConstExprIterator::operator[](size_t idx) const {
2229 return cast<Expr>(I[idx]);
2230}
2231const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2232const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2233
2234//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002235// Child Iterators for iterating over subexpressions/substatements
2236//===----------------------------------------------------------------------===//
2237
2238// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002239Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2240Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002241
Steve Naroffe46504b2007-11-12 14:29:37 +00002242// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002243Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2244Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002245
Steve Naroffebf4cb42008-06-02 23:03:37 +00002246// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002247Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2248Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002249
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002250// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002251Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2252 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002253}
Mike Stump11289f42009-09-09 15:08:12 +00002254Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2255 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002256}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002257
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002258// ObjCSuperExpr
2259Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2260Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2261
Steve Naroffe87026a2009-07-24 17:54:45 +00002262// ObjCIsaExpr
2263Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2264Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2265
Chris Lattner6307f192008-08-10 01:53:14 +00002266// PredefinedExpr
2267Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2268Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002269
2270// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002271Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2272Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002273
2274// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002275Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002276Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002277
2278// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002279Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2280Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002281
Chris Lattner1c20a172007-08-26 03:42:43 +00002282// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002283Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2284Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002285
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002286// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002287Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2288Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002289
2290// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002291Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2292Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002293
2294// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002295Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2296Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002297
Sebastian Redl6f282892008-11-11 17:56:53 +00002298// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002299Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002300 // If this is of a type and the type is a VLA type (and not a typedef), the
2301 // size expression of the VLA needs to be treated as an executable expression.
2302 // Why isn't this weirdness documented better in StmtIterator?
2303 if (isArgumentType()) {
2304 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2305 getArgumentType().getTypePtr()))
2306 return child_iterator(T);
2307 return child_iterator();
2308 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002309 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002310}
Sebastian Redl6f282892008-11-11 17:56:53 +00002311Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2312 if (isArgumentType())
2313 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002314 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002315}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002316
2317// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002318Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002319 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002320}
Ted Kremenek23702b62007-08-24 20:06:47 +00002321Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002322 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002323}
2324
2325// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002326Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002327 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002328}
Ted Kremenek23702b62007-08-24 20:06:47 +00002329Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002330 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002331}
Ted Kremenek23702b62007-08-24 20:06:47 +00002332
2333// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002334Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2335Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002336
Nate Begemance4d7fc2008-04-18 23:10:10 +00002337// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002338Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2339Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002340
2341// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002342Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2343Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002344
Ted Kremenek23702b62007-08-24 20:06:47 +00002345// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002346Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2347Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002348
2349// BinaryOperator
2350Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002351 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002352}
Ted Kremenek23702b62007-08-24 20:06:47 +00002353Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002354 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002355}
2356
2357// ConditionalOperator
2358Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002359 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002360}
Ted Kremenek23702b62007-08-24 20:06:47 +00002361Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002362 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002363}
2364
2365// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002366Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2367Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002368
Ted Kremenek23702b62007-08-24 20:06:47 +00002369// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002370Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2371Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002372
2373// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002374Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2375 return child_iterator();
2376}
2377
2378Stmt::child_iterator TypesCompatibleExpr::child_end() {
2379 return child_iterator();
2380}
Ted Kremenek23702b62007-08-24 20:06:47 +00002381
2382// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002383Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2384Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002385
Douglas Gregor3be4b122008-11-29 04:51:27 +00002386// GNUNullExpr
2387Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2388Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2389
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002390// ShuffleVectorExpr
2391Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002392 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002393}
2394Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002395 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002396}
2397
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002398// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002399Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2400Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002401
Anders Carlsson4692db02007-08-31 04:56:16 +00002402// InitListExpr
2403Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002404 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002405}
2406Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002407 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002408}
2409
Douglas Gregor0202cb42009-01-29 17:44:32 +00002410// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002411Stmt::child_iterator DesignatedInitExpr::child_begin() {
2412 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2413 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002414 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2415}
2416Stmt::child_iterator DesignatedInitExpr::child_end() {
2417 return child_iterator(&*child_begin() + NumSubExprs);
2418}
2419
Douglas Gregor0202cb42009-01-29 17:44:32 +00002420// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002421Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2422 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002423}
2424
Mike Stump11289f42009-09-09 15:08:12 +00002425Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2426 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002427}
2428
Nate Begeman5ec4b312009-08-10 23:49:36 +00002429// ParenListExpr
2430Stmt::child_iterator ParenListExpr::child_begin() {
2431 return &Exprs[0];
2432}
2433Stmt::child_iterator ParenListExpr::child_end() {
2434 return &Exprs[0]+NumExprs;
2435}
2436
Ted Kremenek23702b62007-08-24 20:06:47 +00002437// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002438Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002439 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002440}
2441Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002442 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002443}
Ted Kremenek23702b62007-08-24 20:06:47 +00002444
2445// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002446Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2447Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002448
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002449// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002450Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002451 return child_iterator();
2452}
2453Stmt::child_iterator ObjCSelectorExpr::child_end() {
2454 return child_iterator();
2455}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002456
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002457// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002458Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2459 return child_iterator();
2460}
2461Stmt::child_iterator ObjCProtocolExpr::child_end() {
2462 return child_iterator();
2463}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002464
Steve Naroffd54978b2007-09-18 23:55:05 +00002465// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002466Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002467 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00002468}
2469Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002470 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002471}
2472
Steve Naroffc540d662008-09-03 18:15:37 +00002473// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002474Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2475Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002476
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002477Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2478Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }