blob: 90b50c61f989ae40611997d511e79ac45480299c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000027#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// Primary Expressions.
32//===----------------------------------------------------------------------===//
33
Douglas Gregora2813ce2009-10-23 18:54:35 +000034DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
35 SourceRange QualifierRange,
36 NamedDecl *D, SourceLocation NameLoc,
37 bool HasExplicitTemplateArgumentList,
38 SourceLocation LAngleLoc,
John McCall833ca992009-10-29 08:12:44 +000039 const TemplateArgumentLoc *ExplicitTemplateArgs,
Douglas Gregora2813ce2009-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 McCall833ca992009-10-29 08:12:44 +000062 TemplateArgumentLoc *TemplateArgs = ETemplateArgs->getTemplateArgs();
Douglas Gregora2813ce2009-10-23 18:54:35 +000063 for (unsigned I = 0; I < NumExplicitTemplateArgs; ++I)
John McCall833ca992009-10-29 08:12:44 +000064 new (TemplateArgs + I) TemplateArgumentLoc(ExplicitTemplateArgs[I]);
Douglas Gregora2813ce2009-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 McCall833ca992009-10-29 08:12:44 +000086 const TemplateArgumentLoc *ExplicitTemplateArgs,
Douglas Gregora2813ce2009-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 McCall833ca992009-10-29 08:12:44 +000096 sizeof(TemplateArgumentLoc) * NumExplicitTemplateArgs;
Douglas Gregora2813ce2009-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 Carlsson3a082d82009-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 McCall183700f2009-09-21 23:43:11 +0000140 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-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 Lattnerda8249e2008-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 Johannesenee5a7002008-10-09 23:02:32 +0000200 bool ignored;
201 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
202 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000203 return V.convertToDouble();
204}
205
Chris Lattner2085fd62009-02-18 06:40:38 +0000206StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
207 unsigned ByteLength, bool Wide,
208 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000209 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000210 unsigned NumStrs) {
Chris Lattner2085fd62009-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 Stump1eb44332009-09-09 15:08:12 +0000217
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000226
Chris Lattner726e1682009-02-18 05:49:11 +0000227 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000228 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
229 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000230}
231
Douglas Gregor673ecd62009-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 Gregor42602bb2009-08-07 06:08:38 +0000243void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000244 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000245 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000246}
247
Daniel Dunbarb6480232009-09-22 03:27:33 +0000248void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000249 if (StrData)
250 C.Deallocate(const_cast<char*>(StrData));
251
Daniel Dunbarb6480232009-09-22 03:27:33 +0000252 char *AStrData = new (C, 1) char[Str.size()];
253 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000254 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000255 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000256}
257
Reid Spencer5f016e22007-07-11 17:01:13 +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) {
262 default: assert(0 && "Unknown unary operator");
263 case PostInc: return "++";
264 case PostDec: return "--";
265 case PreInc: return "++";
266 case PreDec: return "--";
267 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";
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000276 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 }
278}
279
Mike Stump1eb44332009-09-09 15:08:12 +0000280UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000281UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
282 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000283 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-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 Gregorbc736fc2009-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000310//===----------------------------------------------------------------------===//
311// Postfix Operators.
312//===----------------------------------------------------------------------===//
313
Ted Kremenek668bf912009-02-09 20:51:47 +0000314CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000315 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000316 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000317 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000318 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000319 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenek668bf912009-02-09 20:51:47 +0000321 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-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 Kremenek668bf912009-02-09 20:51:47 +0000325
Douglas Gregorb4609802008-11-14 16:09:21 +0000326 RParenLoc = rparenloc;
327}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000328
Ted Kremenek668bf912009-02-09 20:51:47 +0000329CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
330 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000331 : Expr(CallExprClass, t,
332 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000333 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000334 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000335
336 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000337 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000339 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 RParenLoc = rparenloc;
342}
343
Mike Stump1eb44332009-09-09 15:08:12 +0000344CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
345 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000346 SubExprs = new (C) Stmt*[1];
347}
348
Douglas Gregor42602bb2009-08-07 06:08:38 +0000349void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000350 DestroyChildren(C);
351 if (SubExprs) C.Deallocate(SubExprs);
352 this->~CallExpr();
353 C.Deallocate(this);
354}
355
Zhongxing Xua0042542009-07-17 07:29:51 +0000356FunctionDecl *CallExpr::getDirectCallee() {
357 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000358 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xua0042542009-07-17 07:29:51 +0000359 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xua0042542009-07-17 07:29:51 +0000360
361 return 0;
362}
363
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000367void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000368 // No change, just return.
369 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000374 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-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 Dunbar68a049c2009-07-28 06:29:46 +0000380 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-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 Stump1eb44332009-09-09 15:08:12 +0000387
Douglas Gregor88c9a462009-04-17 21:46:47 +0000388 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000389 SubExprs = NewSubExprs;
390 this->NumArgs = NumArgs;
391}
392
Chris Lattnercb888962008-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 Gregor3c385e52009-02-14 18:57:46 +0000395unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000396 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000397 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-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 Lattnercb888962008-10-06 05:00:53 +0000401 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000403 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
404 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000405 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Anders Carlssonbcba2012008-01-31 02:13:57 +0000407 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
408 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000409 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000411 if (!FDecl->getIdentifier())
412 return 0;
413
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000414 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000415}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000416
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000417QualType CallExpr::getCallReturnType() const {
418 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000419 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000420 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000421 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000422 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000423
John McCall183700f2009-09-21 23:43:11 +0000424 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000425 return FnType->getResultType();
426}
Chris Lattnercb888962008-10-06 05:00:53 +0000427
Mike Stump1eb44332009-09-09 15:08:12 +0000428MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
429 SourceRange qualrange, NamedDecl *memberdecl,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000430 SourceLocation l, bool has_explicit,
431 SourceLocation langle,
John McCall833ca992009-10-29 08:12:44 +0000432 const TemplateArgumentLoc *targs, unsigned numtargs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000433 SourceLocation rangle, QualType ty)
Mike Stump1eb44332009-09-09 15:08:12 +0000434 : Expr(MemberExprClass, ty,
Douglas Gregor83f6faf2009-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 Gregorc4bf26f2009-09-01 00:37:14 +0000438 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(has_explicit) {
Douglas Gregor83f6faf2009-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 Stump1eb44332009-09-09 15:08:12 +0000445
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000446 // Initialize the explicit template argument list, if any.
447 if (HasExplicitTemplateArgumentList) {
Mike Stump1eb44332009-09-09 15:08:12 +0000448 ExplicitTemplateArgumentList *ETemplateArgs
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000449 = getExplicitTemplateArgumentList();
450 ETemplateArgs->LAngleLoc = langle;
451 ETemplateArgs->RAngleLoc = rangle;
452 ETemplateArgs->NumTemplateArgs = numtargs;
Mike Stump1eb44332009-09-09 15:08:12 +0000453
John McCall833ca992009-10-29 08:12:44 +0000454 TemplateArgumentLoc *TemplateArgs = ETemplateArgs->getTemplateArgs();
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000455 for (unsigned I = 0; I < numtargs; ++I)
John McCall833ca992009-10-29 08:12:44 +0000456 new (TemplateArgs + I) TemplateArgumentLoc(targs[I]);
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000457 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000458}
459
Mike Stump1eb44332009-09-09 15:08:12 +0000460MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
461 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000462 SourceRange qualrange,
Mike Stump1eb44332009-09-09 15:08:12 +0000463 NamedDecl *memberdecl,
464 SourceLocation l,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000465 bool has_explicit,
466 SourceLocation langle,
John McCall833ca992009-10-29 08:12:44 +0000467 const TemplateArgumentLoc *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000468 unsigned numtargs,
469 SourceLocation rangle,
470 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000471 std::size_t Size = sizeof(MemberExpr);
472 if (qual != 0)
473 Size += sizeof(NameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000475 if (has_explicit)
Mike Stump1eb44332009-09-09 15:08:12 +0000476 Size += sizeof(ExplicitTemplateArgumentList) +
John McCall833ca992009-10-29 08:12:44 +0000477 sizeof(TemplateArgumentLoc) * numtargs;
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000479 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000480 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
481 has_explicit, langle, targs, numtargs, rangle,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000482 ty);
483}
484
Anders Carlssonf8ec55a2009-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";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000493 case CastExpr::CK_BaseToDerived:
494 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000495 case CastExpr::CK_DerivedToBase:
496 return "DerivedToBase";
497 case CastExpr::CK_Dynamic:
498 return "Dynamic";
499 case CastExpr::CK_ToUnion:
500 return "ToUnion";
501 case CastExpr::CK_ArrayToPointerDecay:
502 return "ArrayToPointerDecay";
503 case CastExpr::CK_FunctionToPointerDecay:
504 return "FunctionToPointerDecay";
505 case CastExpr::CK_NullToMemberPointer:
506 return "NullToMemberPointer";
507 case CastExpr::CK_BaseToDerivedMemberPointer:
508 return "BaseToDerivedMemberPointer";
Anders Carlsson1a31a182009-10-30 00:46:35 +0000509 case CastExpr::CK_DerivedToBaseMemberPointer:
510 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000511 case CastExpr::CK_UserDefinedConversion:
512 return "UserDefinedConversion";
513 case CastExpr::CK_ConstructorConversion:
514 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000515 case CastExpr::CK_IntegralToPointer:
516 return "IntegralToPointer";
517 case CastExpr::CK_PointerToIntegral:
518 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000519 case CastExpr::CK_ToVoid:
520 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000521 case CastExpr::CK_VectorSplat:
522 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000523 case CastExpr::CK_IntegralCast:
524 return "IntegralCast";
525 case CastExpr::CK_IntegralToFloating:
526 return "IntegralToFloating";
527 case CastExpr::CK_FloatingToIntegral:
528 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000529 case CastExpr::CK_FloatingCast:
530 return "FloatingCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000533 assert(0 && "Unhandled cast kind!");
534 return 0;
535}
536
Reid Spencer5f016e22007-07-11 17:01:13 +0000537/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
538/// corresponds to, e.g. "<<=".
539const char *BinaryOperator::getOpcodeStr(Opcode Op) {
540 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000541 case PtrMemD: return ".*";
542 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 case Mul: return "*";
544 case Div: return "/";
545 case Rem: return "%";
546 case Add: return "+";
547 case Sub: return "-";
548 case Shl: return "<<";
549 case Shr: return ">>";
550 case LT: return "<";
551 case GT: return ">";
552 case LE: return "<=";
553 case GE: return ">=";
554 case EQ: return "==";
555 case NE: return "!=";
556 case And: return "&";
557 case Xor: return "^";
558 case Or: return "|";
559 case LAnd: return "&&";
560 case LOr: return "||";
561 case Assign: return "=";
562 case MulAssign: return "*=";
563 case DivAssign: return "/=";
564 case RemAssign: return "%=";
565 case AddAssign: return "+=";
566 case SubAssign: return "-=";
567 case ShlAssign: return "<<=";
568 case ShrAssign: return ">>=";
569 case AndAssign: return "&=";
570 case XorAssign: return "^=";
571 case OrAssign: return "|=";
572 case Comma: return ",";
573 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000574
575 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000576}
577
Mike Stump1eb44332009-09-09 15:08:12 +0000578BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000579BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
580 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000581 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000582 case OO_Plus: return Add;
583 case OO_Minus: return Sub;
584 case OO_Star: return Mul;
585 case OO_Slash: return Div;
586 case OO_Percent: return Rem;
587 case OO_Caret: return Xor;
588 case OO_Amp: return And;
589 case OO_Pipe: return Or;
590 case OO_Equal: return Assign;
591 case OO_Less: return LT;
592 case OO_Greater: return GT;
593 case OO_PlusEqual: return AddAssign;
594 case OO_MinusEqual: return SubAssign;
595 case OO_StarEqual: return MulAssign;
596 case OO_SlashEqual: return DivAssign;
597 case OO_PercentEqual: return RemAssign;
598 case OO_CaretEqual: return XorAssign;
599 case OO_AmpEqual: return AndAssign;
600 case OO_PipeEqual: return OrAssign;
601 case OO_LessLess: return Shl;
602 case OO_GreaterGreater: return Shr;
603 case OO_LessLessEqual: return ShlAssign;
604 case OO_GreaterGreaterEqual: return ShrAssign;
605 case OO_EqualEqual: return EQ;
606 case OO_ExclaimEqual: return NE;
607 case OO_LessEqual: return LE;
608 case OO_GreaterEqual: return GE;
609 case OO_AmpAmp: return LAnd;
610 case OO_PipePipe: return LOr;
611 case OO_Comma: return Comma;
612 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000613 }
614}
615
616OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
617 static const OverloadedOperatorKind OverOps[] = {
618 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
619 OO_Star, OO_Slash, OO_Percent,
620 OO_Plus, OO_Minus,
621 OO_LessLess, OO_GreaterGreater,
622 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
623 OO_EqualEqual, OO_ExclaimEqual,
624 OO_Amp,
625 OO_Caret,
626 OO_Pipe,
627 OO_AmpAmp,
628 OO_PipePipe,
629 OO_Equal, OO_StarEqual,
630 OO_SlashEqual, OO_PercentEqual,
631 OO_PlusEqual, OO_MinusEqual,
632 OO_LessLessEqual, OO_GreaterGreaterEqual,
633 OO_AmpEqual, OO_CaretEqual,
634 OO_PipeEqual,
635 OO_Comma
636 };
637 return OverOps[Opc];
638}
639
Mike Stump1eb44332009-09-09 15:08:12 +0000640InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000641 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000642 SourceLocation rbraceloc)
Douglas Gregor9ea62762009-05-21 23:17:49 +0000643 : Expr(InitListExprClass, QualType(),
644 hasAnyTypeDependentArguments(initExprs, numInits),
645 hasAnyValueDependentArguments(initExprs, numInits)),
Mike Stump1eb44332009-09-09 15:08:12 +0000646 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000647 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000648
649 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000650}
Reid Spencer5f016e22007-07-11 17:01:13 +0000651
Douglas Gregorfa219202009-03-20 23:58:33 +0000652void InitListExpr::reserveInits(unsigned NumInits) {
653 if (NumInits > InitExprs.size())
654 InitExprs.reserve(NumInits);
655}
656
Douglas Gregor4c678342009-01-28 21:54:33 +0000657void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000658 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000659 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000660 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000661 InitExprs.resize(NumInits, 0);
662}
663
664Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
665 if (Init >= InitExprs.size()) {
666 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
667 InitExprs.back() = expr;
668 return 0;
669 }
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Douglas Gregor4c678342009-01-28 21:54:33 +0000671 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
672 InitExprs[Init] = expr;
673 return Result;
674}
675
Steve Naroffbfdcae62008-09-04 15:31:07 +0000676/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000677///
678const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000679 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000680 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000681}
682
Mike Stump1eb44332009-09-09 15:08:12 +0000683SourceLocation BlockExpr::getCaretLocation() const {
684 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000685}
Mike Stump1eb44332009-09-09 15:08:12 +0000686const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000687 return TheBlock->getBody();
688}
Mike Stump1eb44332009-09-09 15:08:12 +0000689Stmt *BlockExpr::getBody() {
690 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000691}
Steve Naroff56ee6892008-10-08 17:01:13 +0000692
693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694//===----------------------------------------------------------------------===//
695// Generic Expression Routines
696//===----------------------------------------------------------------------===//
697
Chris Lattner026dc962009-02-14 07:37:35 +0000698/// isUnusedResultAWarning - Return true if this immediate expression should
699/// be warned about if the result is unused. If so, fill in Loc and Ranges
700/// with location to warn on and the source range[s] to report with the
701/// warning.
702bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000703 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000704 // Don't warn if the expr is type dependent. The type could end up
705 // instantiating to void.
706 if (isTypeDependent())
707 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 switch (getStmtClass()) {
710 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000711 Loc = getExprLoc();
712 R1 = getSourceRange();
713 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000715 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000716 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 case UnaryOperatorClass: {
718 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000721 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 case UnaryOperator::PostInc:
723 case UnaryOperator::PostDec:
724 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000725 case UnaryOperator::PreDec: // ++/--
726 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 case UnaryOperator::Deref:
728 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000729 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000730 return false;
731 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 case UnaryOperator::Real:
733 case UnaryOperator::Imag:
734 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000735 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
736 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000737 return false;
738 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000740 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 }
Chris Lattner026dc962009-02-14 07:37:35 +0000742 Loc = UO->getOperatorLoc();
743 R1 = UO->getSubExpr()->getSourceRange();
744 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000746 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000747 const BinaryOperator *BO = cast<BinaryOperator>(this);
748 // Consider comma to have side effects if the LHS or RHS does.
749 if (BO->getOpcode() == BinaryOperator::Comma)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000750 return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
751 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Chris Lattner026dc962009-02-14 07:37:35 +0000753 if (BO->isAssignmentOp())
754 return false;
755 Loc = BO->getOperatorLoc();
756 R1 = BO->getLHS()->getSourceRange();
757 R2 = BO->getRHS()->getSourceRange();
758 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000759 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000760 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000761 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000762
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000763 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000764 // The condition must be evaluated, but if either the LHS or RHS is a
765 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000766 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000767 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +0000768 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +0000769 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +0000770 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000771 }
772
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000774 // If the base pointer or element is to a volatile pointer/field, accessing
775 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000776 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000777 return false;
778 Loc = cast<MemberExpr>(this)->getMemberLoc();
779 R1 = SourceRange(Loc, Loc);
780 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
781 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 case ArraySubscriptExprClass:
784 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000785 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000786 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000787 return false;
788 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
789 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
790 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
791 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000792
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000794 case CXXOperatorCallExprClass:
795 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000796 // If this is a direct call, get the callee.
797 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000798 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000799 // If the callee has attribute pure, const, or warn_unused_result, warn
800 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000801 //
802 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
803 // updated to match for QoI.
804 if (FD->getAttr<WarnUnusedResultAttr>() ||
805 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
806 Loc = CE->getCallee()->getLocStart();
807 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000809 if (unsigned NumArgs = CE->getNumArgs())
810 R2 = SourceRange(CE->getArg(0)->getLocStart(),
811 CE->getArg(NumArgs-1)->getLocEnd());
812 return true;
813 }
Chris Lattner026dc962009-02-14 07:37:35 +0000814 }
815 return false;
816 }
Anders Carlsson58beed92009-11-17 17:11:23 +0000817
818 case CXXTemporaryObjectExprClass:
819 case CXXConstructExprClass:
820 return false;
821
Chris Lattnera9c01022007-09-26 22:06:30 +0000822 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000823 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000825 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000826#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000827 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000828 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000829 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000830 Loc = Ref->getLocation();
831 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
832 if (Ref->getBase())
833 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000834#else
835 Loc = getExprLoc();
836 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000837#endif
838 return true;
839 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000840 case StmtExprClass: {
841 // Statement exprs don't logically have side effects themselves, but are
842 // sometimes used in macros in ways that give them a type that is unused.
843 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
844 // however, if the result of the stmt expr is dead, we don't want to emit a
845 // warning.
846 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
847 if (!CS->body_empty())
848 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +0000849 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Chris Lattner026dc962009-02-14 07:37:35 +0000851 Loc = cast<StmtExpr>(this)->getLParenLoc();
852 R1 = getSourceRange();
853 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000854 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000855 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000856 // If this is an explicit cast to void, allow it. People do this when they
857 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000858 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000859 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000860 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
861 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
862 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000863 case CXXFunctionalCastExprClass: {
864 const CastExpr *CE = cast<CastExpr>(this);
865
866 // If this is a cast to void or a constructor conversion, check the operand.
867 // Otherwise, the result of the cast is unused.
868 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
869 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000870 return (cast<CastExpr>(this)->getSubExpr()
871 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +0000872 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
873 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
874 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000875 }
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Eli Friedman4be1f472008-05-19 21:24:43 +0000877 case ImplicitCastExprClass:
878 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +0000879 return (cast<ImplicitCastExpr>(this)
880 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +0000881
Chris Lattner04421082008-04-08 04:40:51 +0000882 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000883 return (cast<CXXDefaultArgExpr>(this)
884 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000885
886 case CXXNewExprClass:
887 // FIXME: In theory, there might be new expressions that don't have side
888 // effects (e.g. a placement new with an uninitialized POD).
889 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000890 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +0000891 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000892 return (cast<CXXBindTemporaryExpr>(this)
893 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000894 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000895 return (cast<CXXExprWithTemporaries>(this)
896 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000897 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000898}
899
Douglas Gregorba7e2102008-10-22 15:04:37 +0000900/// DeclCanBeLvalue - Determine whether the given declaration can be
901/// an lvalue. This is a helper routine for isLvalue.
902static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000903 // C++ [temp.param]p6:
904 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000905 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +0000906 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
907 return NTTParm->getType()->isReferenceType();
908
Douglas Gregor44b43212008-12-11 16:49:14 +0000909 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000910 // C++ 3.10p2: An lvalue refers to an object or function.
911 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor83314aa2009-07-08 20:55:45 +0000912 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
913 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +0000914}
915
Reid Spencer5f016e22007-07-11 17:01:13 +0000916/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
917/// incomplete type other than void. Nonarray expressions that can be lvalues:
918/// - name, where name must be a variable
919/// - e[i]
920/// - (e), where e must be an lvalue
921/// - e.name, where e must be an lvalue
922/// - e->name
923/// - *e, the type of e cannot be a function type
924/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000925/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000926/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000927///
Chris Lattner28be73f2008-07-26 21:30:36 +0000928Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +0000929 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
930
931 isLvalueResult Res = isLvalueInternal(Ctx);
932 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
933 return Res;
934
Douglas Gregor98cd5992008-10-21 23:43:52 +0000935 // first, check the type (C99 6.3.2.1). Expressions with function
936 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +0000937 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 return LV_NotObjectType;
939
Steve Naroffacb818a2008-02-10 01:39:04 +0000940 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +0000941 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000942 return LV_IncompleteVoidType;
943
Eli Friedman53202852009-05-03 22:36:05 +0000944 return LV_Valid;
945}
Bill Wendling08ad47c2007-07-17 03:52:31 +0000946
Eli Friedman53202852009-05-03 22:36:05 +0000947// Check whether the expression can be sanely treated like an l-value
948Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000950 case StringLiteralClass: // C99 6.5.1p4
951 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000952 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
954 // For vectors, make sure base is an lvalue (i.e. not a function call).
955 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000956 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +0000958 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000959 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
960 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 return LV_Valid;
962 break;
Chris Lattner41110242008-06-17 18:05:57 +0000963 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000964 case BlockDeclRefExprClass: {
965 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000966 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000967 return LV_Valid;
968 break;
969 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000970 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000972 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
973 NamedDecl *Member = m->getMemberDecl();
974 // C++ [expr.ref]p4:
975 // If E2 is declared to have type "reference to T", then E1.E2
976 // is an lvalue.
977 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
978 if (Value->getType()->isReferenceType())
979 return LV_Valid;
980
981 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000982 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000983 return LV_Valid;
984
985 // -- If E2 is a non-static data member [...]. If E1 is an
986 // lvalue, then E1.E2 is an lvalue.
987 if (isa<FieldDecl>(Member))
988 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
989
990 // -- If it refers to a static member function [...], then
991 // E1.E2 is an lvalue.
992 // -- Otherwise, if E1.E2 refers to a non-static member
993 // function [...], then E1.E2 is not an lvalue.
994 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
995 return Method->isStatic()? LV_Valid : LV_MemberFunction;
996
997 // -- If E2 is a member enumerator [...], the expression E1.E2
998 // is not an lvalue.
999 if (isa<EnumConstantDecl>(Member))
1000 return LV_InvalidExpression;
1001
1002 // Not an lvalue.
1003 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001004 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001005
1006 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +00001007 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001008 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001009 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001011 return LV_Valid; // C99 6.5.3p4
1012
1013 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001014 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1015 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001016 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001017
1018 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1019 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1020 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1021 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001023 case ImplicitCastExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +00001024 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001025 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001027 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001028 case BinaryOperatorClass:
1029 case CompoundAssignOperatorClass: {
1030 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001031
1032 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1033 BinOp->getOpcode() == BinaryOperator::Comma)
1034 return BinOp->getRHS()->isLvalue(Ctx);
1035
Sebastian Redl22460502009-02-07 00:15:38 +00001036 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001037 // The result of a .* expression is an lvalue only if its first operand is
1038 // an lvalue and its second operand is a pointer to data member.
1039 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001040 !BinOp->getType()->isFunctionType())
1041 return BinOp->getLHS()->isLvalue(Ctx);
1042
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001043 // The result of an ->* expression is an lvalue only if its second operand
1044 // is a pointer to data member.
1045 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1046 !BinOp->getType()->isFunctionType()) {
1047 QualType Ty = BinOp->getRHS()->getType();
1048 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1049 return LV_Valid;
1050 }
1051
Douglas Gregorbf3af052008-11-13 20:12:29 +00001052 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001053 return LV_InvalidExpression;
1054
Douglas Gregorbf3af052008-11-13 20:12:29 +00001055 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001056 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001057 // The result of an assignment operation [...] is an lvalue.
1058 return LV_Valid;
1059
1060
1061 // C99 6.5.16:
1062 // An assignment expression [...] is not an lvalue.
1063 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001066 case CXXOperatorCallExprClass:
1067 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001068 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001069 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001070 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001071 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1072 if (ReturnType->isLValueReferenceType())
1073 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001074
Douglas Gregor9d293df2008-10-28 00:22:11 +00001075 break;
1076 }
Steve Naroffe6386392007-12-05 04:00:10 +00001077 case CompoundLiteralExprClass: // C99 6.5.2.5p5
1078 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001079 case ChooseExprClass:
1080 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001081 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001082 case ExtVectorElementExprClass:
1083 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001084 return LV_DuplicateVectorComponents;
1085 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001086 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1087 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001088 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1089 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001090 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001091 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001092 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001093 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001094 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001095 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +00001096 case CXXConditionDeclExprClass:
1097 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001098 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001099 case CXXFunctionalCastExprClass:
1100 case CXXStaticCastExprClass:
1101 case CXXDynamicCastExprClass:
1102 case CXXReinterpretCastExprClass:
1103 case CXXConstCastExprClass:
1104 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001105 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001106 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1107 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001108 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1109 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001110 return LV_Valid;
1111 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001112 case CXXTypeidExprClass:
1113 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1114 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001115 case CXXBindTemporaryExprClass:
1116 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1117 isLvalueInternal(Ctx);
Sebastian Redl76458502009-04-17 16:30:52 +00001118 case ConditionalOperatorClass: {
1119 // Complicated handling is only for C++.
1120 if (!Ctx.getLangOptions().CPlusPlus)
1121 return LV_InvalidExpression;
1122
1123 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1124 // everywhere there's an object converted to an rvalue. Also, any other
1125 // casts should be wrapped by ImplicitCastExprs. There's just the special
1126 // case involving throws to work out.
1127 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001128 Expr *True = Cond->getTrueExpr();
1129 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001130 // C++0x 5.16p2
1131 // If either the second or the third operand has type (cv) void, [...]
1132 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001133 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001134 return LV_InvalidExpression;
1135
1136 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001137 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001138 return LV_InvalidExpression;
1139
1140 // That's it.
1141 return LV_Valid;
1142 }
1143
Douglas Gregor3eefb1c2009-10-24 04:59:53 +00001144 case TemplateIdRefExprClass: {
1145 const TemplateIdRefExpr *TID = cast<TemplateIdRefExpr>(this);
1146 TemplateName Template = TID->getTemplateName();
1147 NamedDecl *ND = Template.getAsTemplateDecl();
1148 if (!ND)
1149 ND = Template.getAsOverloadedFunctionDecl();
1150 if (ND && DeclCanBeLvalue(ND, Ctx))
1151 return LV_Valid;
1152
1153 break;
1154 }
1155
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 default:
1157 break;
1158 }
1159 return LV_InvalidExpression;
1160}
1161
1162/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1163/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001164/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001165/// recursively, any member or element of all contained aggregates or unions)
1166/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001167Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001168Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001169 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001172 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001173 // C++ 3.10p11: Functions cannot be modified, but pointers to
1174 // functions can be modifiable.
1175 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1176 return MLV_NotObjectType;
1177 break;
1178
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 case LV_NotObjectType: return MLV_NotObjectType;
1180 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001181 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001182 case LV_InvalidExpression:
1183 // If the top level is a C-style cast, and the subexpression is a valid
1184 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1185 // GCC extension. We don't support it, but we want to produce good
1186 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001187 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1188 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1189 if (Loc)
1190 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001191 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001192 }
1193 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001194 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001195 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001197
1198 // The following is illegal:
1199 // void takeclosure(void (^C)(void));
1200 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1201 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001202 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001203 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1204 return MLV_NotBlockQualified;
1205 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001206
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001207 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001208 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001209 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1210 if (Expr->getSetterMethod() == 0)
1211 return MLV_NoSetterProperty;
1212 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001213
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001214 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001216 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001218 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001220 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Ted Kremenek6217b802009-07-29 21:53:49 +00001223 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001224 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 return MLV_ConstQualified;
1226 }
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Mike Stump1eb44332009-09-09 15:08:12 +00001228 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001229}
1230
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001231/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001232/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001233bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001234 switch (getStmtClass()) {
1235 default:
1236 return false;
1237 case ObjCIvarRefExprClass:
1238 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001239 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001240 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001241 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001242 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001243 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001244 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001245 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001246 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001247 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001248 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001249 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1250 if (VD->hasGlobalStorage())
1251 return true;
1252 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001253 // dereferencing to a pointer is always a gc'able candidate,
1254 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001255 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001256 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001257 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001258 return false;
1259 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001260 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001261 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001262 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001263 }
1264 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001265 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001266 }
1267}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001268Expr* Expr::IgnoreParens() {
1269 Expr* E = this;
1270 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1271 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001273 return E;
1274}
1275
Chris Lattner56f34942008-02-13 01:02:39 +00001276/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1277/// or CastExprs or ImplicitCastExprs, returning their operand.
1278Expr *Expr::IgnoreParenCasts() {
1279 Expr *E = this;
1280 while (true) {
1281 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1282 E = P->getSubExpr();
1283 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1284 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001285 else
1286 return E;
1287 }
1288}
1289
Chris Lattnerecdd8412009-03-13 17:28:01 +00001290/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1291/// value (including ptr->int casts of the same size). Strip off any
1292/// ParenExpr or CastExprs, returning their operand.
1293Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1294 Expr *E = this;
1295 while (true) {
1296 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1297 E = P->getSubExpr();
1298 continue;
1299 }
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Chris Lattnerecdd8412009-03-13 17:28:01 +00001301 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1302 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1303 // ptr<->int casts of the same width. We also ignore all identify casts.
1304 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001305
Chris Lattnerecdd8412009-03-13 17:28:01 +00001306 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1307 E = SE;
1308 continue;
1309 }
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Chris Lattnerecdd8412009-03-13 17:28:01 +00001311 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1312 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1313 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1314 E = SE;
1315 continue;
1316 }
1317 }
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Chris Lattnerecdd8412009-03-13 17:28:01 +00001319 return E;
1320 }
1321}
1322
1323
Douglas Gregor898574e2008-12-05 23:32:09 +00001324/// hasAnyTypeDependentArguments - Determines if any of the expressions
1325/// in Exprs is type-dependent.
1326bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1327 for (unsigned I = 0; I < NumExprs; ++I)
1328 if (Exprs[I]->isTypeDependent())
1329 return true;
1330
1331 return false;
1332}
1333
1334/// hasAnyValueDependentArguments - Determines if any of the expressions
1335/// in Exprs is value-dependent.
1336bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1337 for (unsigned I = 0; I < NumExprs; ++I)
1338 if (Exprs[I]->isValueDependent())
1339 return true;
1340
1341 return false;
1342}
1343
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001344bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001345 // This function is attempting whether an expression is an initializer
1346 // which can be evaluated at compile-time. isEvaluatable handles most
1347 // of the cases, but it can't deal with some initializer-specific
1348 // expressions, and it can't deal with aggregates; we deal with those here,
1349 // and fall back to isEvaluatable for the other cases.
1350
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001351 // FIXME: This function assumes the variable being assigned to
1352 // isn't a reference type!
1353
Anders Carlssone8a32b82008-11-24 05:23:59 +00001354 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001355 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001356 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001357 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001358 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001359 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001360 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001361 // This handles gcc's extension that allows global initializers like
1362 // "struct x {int x;} x = (struct x) {};".
1363 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001364 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001365 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001366 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001367 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001368 // FIXME: This doesn't deal with fields with reference types correctly.
1369 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1370 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001371 const InitListExpr *Exp = cast<InitListExpr>(this);
1372 unsigned numInits = Exp->getNumInits();
1373 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001374 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001375 return false;
1376 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001377 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001378 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001379 case ImplicitValueInitExprClass:
1380 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001381 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001382 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001383 case UnaryOperatorClass: {
1384 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1385 if (Exp->getOpcode() == UnaryOperator::Extension)
1386 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1387 break;
1388 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001389 case BinaryOperatorClass: {
1390 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1391 // but this handles the common case.
1392 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1393 if (Exp->getOpcode() == BinaryOperator::Sub &&
1394 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1395 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1396 return true;
1397 break;
1398 }
Chris Lattner81045d82009-04-21 05:19:11 +00001399 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001400 case CStyleCastExprClass:
1401 // Handle casts with a destination that's a struct or union; this
1402 // deals with both the gcc no-op struct cast extension and the
1403 // cast-to-union extension.
1404 if (getType()->isRecordType())
1405 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001406
1407 // Integer->integer casts can be handled here, which is important for
1408 // things like (int)(&&x-&&y). Scary but true.
1409 if (getType()->isIntegerType() &&
1410 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1411 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1412
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001413 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001414 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001415 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001416}
1417
Reid Spencer5f016e22007-07-11 17:01:13 +00001418/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001419/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001420
1421/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1422/// comma, etc
1423///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001424/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1425/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1426/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001427
Eli Friedmane28d7192009-02-26 09:29:13 +00001428// CheckICE - This function does the fundamental ICE checking: the returned
1429// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1430// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001431// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001432// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001433// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001434//
1435// Meanings of Val:
1436// 0: This expression is an ICE if it can be evaluated by Evaluate.
1437// 1: This expression is not an ICE, but if it isn't evaluated, it's
1438// a legal subexpression for an ICE. This return value is used to handle
1439// the comma operator in C99 mode.
1440// 2: This expression is not an ICE, and is not a legal subexpression for one.
1441
1442struct ICEDiag {
1443 unsigned Val;
1444 SourceLocation Loc;
1445
1446 public:
1447 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1448 ICEDiag() : Val(0) {}
1449};
1450
1451ICEDiag NoDiag() { return ICEDiag(); }
1452
Eli Friedman60ce9632009-02-27 04:07:58 +00001453static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1454 Expr::EvalResult EVResult;
1455 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1456 !EVResult.Val.isInt()) {
1457 return ICEDiag(2, E->getLocStart());
1458 }
1459 return NoDiag();
1460}
1461
Eli Friedmane28d7192009-02-26 09:29:13 +00001462static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001463 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001464 if (!E->getType()->isIntegralType()) {
1465 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001466 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001467
1468 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001469#define STMT(Node, Base) case Expr::Node##Class:
1470#define EXPR(Node, Base)
1471#include "clang/AST/StmtNodes.def"
1472 case Expr::PredefinedExprClass:
1473 case Expr::FloatingLiteralClass:
1474 case Expr::ImaginaryLiteralClass:
1475 case Expr::StringLiteralClass:
1476 case Expr::ArraySubscriptExprClass:
1477 case Expr::MemberExprClass:
1478 case Expr::CompoundAssignOperatorClass:
1479 case Expr::CompoundLiteralExprClass:
1480 case Expr::ExtVectorElementExprClass:
1481 case Expr::InitListExprClass:
1482 case Expr::DesignatedInitExprClass:
1483 case Expr::ImplicitValueInitExprClass:
1484 case Expr::ParenListExprClass:
1485 case Expr::VAArgExprClass:
1486 case Expr::AddrLabelExprClass:
1487 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001488 case Expr::CXXMemberCallExprClass:
1489 case Expr::CXXDynamicCastExprClass:
1490 case Expr::CXXTypeidExprClass:
1491 case Expr::CXXNullPtrLiteralExprClass:
1492 case Expr::CXXThisExprClass:
1493 case Expr::CXXThrowExprClass:
1494 case Expr::CXXConditionDeclExprClass: // FIXME: is this correct?
1495 case Expr::CXXNewExprClass:
1496 case Expr::CXXDeleteExprClass:
1497 case Expr::CXXPseudoDestructorExprClass:
1498 case Expr::UnresolvedFunctionNameExprClass:
1499 case Expr::UnresolvedDeclRefExprClass:
1500 case Expr::TemplateIdRefExprClass:
1501 case Expr::CXXConstructExprClass:
1502 case Expr::CXXBindTemporaryExprClass:
1503 case Expr::CXXExprWithTemporariesClass:
1504 case Expr::CXXTemporaryObjectExprClass:
1505 case Expr::CXXUnresolvedConstructExprClass:
1506 case Expr::CXXUnresolvedMemberExprClass:
1507 case Expr::ObjCStringLiteralClass:
1508 case Expr::ObjCEncodeExprClass:
1509 case Expr::ObjCMessageExprClass:
1510 case Expr::ObjCSelectorExprClass:
1511 case Expr::ObjCProtocolExprClass:
1512 case Expr::ObjCIvarRefExprClass:
1513 case Expr::ObjCPropertyRefExprClass:
1514 case Expr::ObjCImplicitSetterGetterRefExprClass:
1515 case Expr::ObjCSuperExprClass:
1516 case Expr::ObjCIsaExprClass:
1517 case Expr::ShuffleVectorExprClass:
1518 case Expr::BlockExprClass:
1519 case Expr::BlockDeclRefExprClass:
1520 case Expr::NoStmtClass:
1521 case Expr::ExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001522 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001523
Douglas Gregor043cad22009-09-11 00:18:58 +00001524 case Expr::GNUNullExprClass:
1525 // GCC considers the GNU __null value to be an integral constant expression.
1526 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001527
Eli Friedmane28d7192009-02-26 09:29:13 +00001528 case Expr::ParenExprClass:
1529 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1530 case Expr::IntegerLiteralClass:
1531 case Expr::CharacterLiteralClass:
1532 case Expr::CXXBoolLiteralExprClass:
1533 case Expr::CXXZeroInitValueExprClass:
1534 case Expr::TypesCompatibleExprClass:
1535 case Expr::UnaryTypeTraitExprClass:
1536 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001537 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001538 case Expr::CXXOperatorCallExprClass: {
1539 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001540 if (CE->isBuiltinCall(Ctx))
1541 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001542 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001543 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001544 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001545 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1546 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001547 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001548 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001549 // C++ 7.1.5.1p2
1550 // A variable of non-volatile const-qualified integral or enumeration
1551 // type initialized by an ICE can be used in ICEs.
1552 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001553 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001554 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1555 if (Quals.hasVolatile() || !Quals.hasConst())
1556 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1557
1558 // Look for the definition of this variable, which will actually have
1559 // an initializer.
1560 const VarDecl *Def = 0;
1561 const Expr *Init = Dcl->getDefinition(Def);
1562 if (Init) {
1563 if (Def->isInitKnownICE()) {
1564 // We have already checked whether this subexpression is an
1565 // integral constant expression.
1566 if (Def->isInitICE())
1567 return NoDiag();
1568 else
1569 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1570 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001571
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001572 // C++ [class.static.data]p4:
1573 // If a static data member is of const integral or const
1574 // enumeration type, its declaration in the class definition can
1575 // specify a constant-initializer which shall be an integral
1576 // constant expression (5.19). In that case, the member can appear
1577 // in integral constant expressions.
1578 if (Def->isOutOfLine()) {
1579 Dcl->setInitKnownICE(Ctx, false);
1580 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1581 }
1582
Douglas Gregor78d15832009-05-26 18:54:04 +00001583 ICEDiag Result = CheckICE(Init, Ctx);
1584 // Cache the result of the ICE test.
1585 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1586 return Result;
1587 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001588 }
1589 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001590 return ICEDiag(2, E->getLocStart());
1591 case Expr::UnaryOperatorClass: {
1592 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001593 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001594 case UnaryOperator::PostInc:
1595 case UnaryOperator::PostDec:
1596 case UnaryOperator::PreInc:
1597 case UnaryOperator::PreDec:
1598 case UnaryOperator::AddrOf:
1599 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001600 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001601
Reid Spencer5f016e22007-07-11 17:01:13 +00001602 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001603 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001604 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001605 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001607 case UnaryOperator::Real:
1608 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001609 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001610 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001611 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1612 // Evaluate matches the proposed gcc behavior for cases like
1613 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1614 // compliance: we should warn earlier for offsetof expressions with
1615 // array subscripts that aren't ICEs, and if the array subscripts
1616 // are ICEs, the value of the offsetof must be an integer constant.
1617 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001618 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001620 case Expr::SizeOfAlignOfExprClass: {
1621 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1622 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1623 return ICEDiag(2, E->getLocStart());
1624 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001625 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001626 case Expr::BinaryOperatorClass: {
1627 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001628 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001629 case BinaryOperator::PtrMemD:
1630 case BinaryOperator::PtrMemI:
1631 case BinaryOperator::Assign:
1632 case BinaryOperator::MulAssign:
1633 case BinaryOperator::DivAssign:
1634 case BinaryOperator::RemAssign:
1635 case BinaryOperator::AddAssign:
1636 case BinaryOperator::SubAssign:
1637 case BinaryOperator::ShlAssign:
1638 case BinaryOperator::ShrAssign:
1639 case BinaryOperator::AndAssign:
1640 case BinaryOperator::XorAssign:
1641 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001642 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001643
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001645 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001646 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001647 case BinaryOperator::Add:
1648 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001649 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001650 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001651 case BinaryOperator::LT:
1652 case BinaryOperator::GT:
1653 case BinaryOperator::LE:
1654 case BinaryOperator::GE:
1655 case BinaryOperator::EQ:
1656 case BinaryOperator::NE:
1657 case BinaryOperator::And:
1658 case BinaryOperator::Xor:
1659 case BinaryOperator::Or:
1660 case BinaryOperator::Comma: {
1661 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1662 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001663 if (Exp->getOpcode() == BinaryOperator::Div ||
1664 Exp->getOpcode() == BinaryOperator::Rem) {
1665 // Evaluate gives an error for undefined Div/Rem, so make sure
1666 // we don't evaluate one.
1667 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1668 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1669 if (REval == 0)
1670 return ICEDiag(1, E->getLocStart());
1671 if (REval.isSigned() && REval.isAllOnesValue()) {
1672 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1673 if (LEval.isMinSignedValue())
1674 return ICEDiag(1, E->getLocStart());
1675 }
1676 }
1677 }
1678 if (Exp->getOpcode() == BinaryOperator::Comma) {
1679 if (Ctx.getLangOptions().C99) {
1680 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1681 // if it isn't evaluated.
1682 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1683 return ICEDiag(1, E->getLocStart());
1684 } else {
1685 // In both C89 and C++, commas in ICEs are illegal.
1686 return ICEDiag(2, E->getLocStart());
1687 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001688 }
1689 if (LHSResult.Val >= RHSResult.Val)
1690 return LHSResult;
1691 return RHSResult;
1692 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001693 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001694 case BinaryOperator::LOr: {
1695 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1696 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1697 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1698 // Rare case where the RHS has a comma "side-effect"; we need
1699 // to actually check the condition to see whether the side
1700 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001701 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001702 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001703 return RHSResult;
1704 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001705 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001706
Eli Friedmane28d7192009-02-26 09:29:13 +00001707 if (LHSResult.Val >= RHSResult.Val)
1708 return LHSResult;
1709 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001711 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 }
Douglas Gregorf2991242009-09-10 23:31:45 +00001713 case Expr::CastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001714 case Expr::ImplicitCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001715 case Expr::ExplicitCastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001716 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001717 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001718 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001719 case Expr::CXXStaticCastExprClass:
1720 case Expr::CXXReinterpretCastExprClass:
1721 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00001722 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1723 if (SubExpr->getType()->isIntegralType())
1724 return CheckICE(SubExpr, Ctx);
1725 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1726 return NoDiag();
1727 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001728 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001729 case Expr::ConditionalOperatorClass: {
1730 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001731 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00001732 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001733 // expression, and it is fully evaluated. This is an important GNU
1734 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001735 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001736 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001737 Expr::EvalResult EVResult;
1738 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1739 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001740 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001741 }
1742 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001743 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001744 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1745 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1746 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1747 if (CondResult.Val == 2)
1748 return CondResult;
1749 if (TrueResult.Val == 2)
1750 return TrueResult;
1751 if (FalseResult.Val == 2)
1752 return FalseResult;
1753 if (CondResult.Val == 1)
1754 return CondResult;
1755 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1756 return NoDiag();
1757 // Rare case where the diagnostics depend on which side is evaluated
1758 // Note that if we get here, CondResult is 0, and at least one of
1759 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001760 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001761 return FalseResult;
1762 }
1763 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001764 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001765 case Expr::CXXDefaultArgExprClass:
1766 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001767 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001768 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001769 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001770 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001771
Douglas Gregorf2991242009-09-10 23:31:45 +00001772 // Silence a GCC warning
1773 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001774}
Reid Spencer5f016e22007-07-11 17:01:13 +00001775
Eli Friedmane28d7192009-02-26 09:29:13 +00001776bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1777 SourceLocation *Loc, bool isEvaluated) const {
1778 ICEDiag d = CheckICE(this, Ctx);
1779 if (d.Val != 0) {
1780 if (Loc) *Loc = d.Loc;
1781 return false;
1782 }
1783 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001784 if (!Evaluate(EvalResult, Ctx))
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001785 llvm::llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00001786 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1787 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001788 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001789 return true;
1790}
1791
Reid Spencer5f016e22007-07-11 17:01:13 +00001792/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1793/// integer constant expression with the value zero, or if this is one that is
1794/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001795bool Expr::isNullPointerConstant(ASTContext &Ctx,
1796 NullPointerConstantValueDependence NPC) const {
1797 if (isValueDependent()) {
1798 switch (NPC) {
1799 case NPC_NeverValueDependent:
1800 assert(false && "Unexpected value dependent expression!");
1801 // If the unthinkable happens, fall through to the safest alternative.
1802
1803 case NPC_ValueDependentIsNull:
1804 return isTypeDependent() || getType()->isIntegralType();
1805
1806 case NPC_ValueDependentIsNotNull:
1807 return false;
1808 }
1809 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001810
Sebastian Redl07779722008-10-31 14:43:28 +00001811 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001812 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001813 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001814 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001815 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001816 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001817 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001818 Pointee->isVoidType() && // to void*
1819 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001820 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001821 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001822 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001823 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1824 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001825 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00001826 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1827 // Accept ((void*)0) as a null pointer constant, as many other
1828 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00001829 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001830 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001831 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001832 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001833 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001834 } else if (isa<GNUNullExpr>(this)) {
1835 // The GNU __null extension is always a null pointer constant.
1836 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001837 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001838
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001839 // C++0x nullptr_t is always a null pointer constant.
1840 if (getType()->isNullPtrType())
1841 return true;
1842
Steve Naroffaa58f002008-01-14 16:10:57 +00001843 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00001844 if (!getType()->isIntegerType() ||
1845 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00001846 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Reid Spencer5f016e22007-07-11 17:01:13 +00001848 // If we have an integer constant expression, we need to *evaluate* it and
1849 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001850 llvm::APSInt Result;
1851 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001852}
Steve Naroff31a45842007-07-28 23:10:27 +00001853
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001854FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001855 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001856
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001857 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001858 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001859 if (Field->isBitField())
1860 return Field;
1861
1862 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1863 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1864 return BinOp->getLHS()->getBitField();
1865
1866 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001867}
1868
Chris Lattner2140e902009-02-16 22:14:05 +00001869/// isArrow - Return true if the base expression is a pointer to vector,
1870/// return false if the base expression is a vector.
1871bool ExtVectorElementExpr::isArrow() const {
1872 return getBase()->getType()->isPointerType();
1873}
1874
Nate Begeman213541a2008-04-18 23:10:10 +00001875unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00001876 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00001877 return VT->getNumElements();
1878 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001879}
1880
Nate Begeman8a997642008-05-09 06:41:27 +00001881/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001882bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00001883 // FIXME: Refactor this code to an accessor on the AST node which returns the
1884 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001885 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00001886
1887 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00001888 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00001889 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Nate Begeman190d6a22009-01-18 02:01:21 +00001891 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00001892 if (Comp[0] == 's' || Comp[0] == 'S')
1893 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Daniel Dunbar15027422009-10-17 23:53:04 +00001895 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1896 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00001897 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00001898
Steve Narofffec0b492007-07-30 03:29:09 +00001899 return false;
1900}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001901
Nate Begeman8a997642008-05-09 06:41:27 +00001902/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001903void ExtVectorElementExpr::getEncodedElementAccess(
1904 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001905 llvm::StringRef Comp = Accessor->getName();
1906 if (Comp[0] == 's' || Comp[0] == 'S')
1907 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001909 bool isHi = Comp == "hi";
1910 bool isLo = Comp == "lo";
1911 bool isEven = Comp == "even";
1912 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00001913
Nate Begeman8a997642008-05-09 06:41:27 +00001914 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1915 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Nate Begeman8a997642008-05-09 06:41:27 +00001917 if (isHi)
1918 Index = e + i;
1919 else if (isLo)
1920 Index = i;
1921 else if (isEven)
1922 Index = 2 * i;
1923 else if (isOdd)
1924 Index = 2 * i + 1;
1925 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001926 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001927
Nate Begeman3b8d1162008-05-13 21:03:02 +00001928 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001929 }
Nate Begeman8a997642008-05-09 06:41:27 +00001930}
1931
Steve Naroff68d331a2007-09-27 14:38:14 +00001932// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001933ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001934 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001935 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001936 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00001937 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001938 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001939 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001940 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001941 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001942 if (NumArgs) {
1943 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001944 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1945 }
Steve Naroff563477d2007-09-18 23:55:05 +00001946 LBracloc = LBrac;
1947 RBracloc = RBrac;
1948}
1949
Mike Stump1eb44332009-09-09 15:08:12 +00001950// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00001951// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001952ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001953 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001954 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001955 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00001956 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001957 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001958 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001959 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001960 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001961 if (NumArgs) {
1962 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001963 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1964 }
Steve Naroff563477d2007-09-18 23:55:05 +00001965 LBracloc = LBrac;
1966 RBracloc = RBrac;
1967}
1968
Mike Stump1eb44332009-09-09 15:08:12 +00001969// constructor for class messages.
Ted Kremenek4df728e2008-06-24 15:50:53 +00001970ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1971 QualType retType, ObjCMethodDecl *mproto,
1972 SourceLocation LBrac, SourceLocation RBrac,
1973 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00001974: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenek4df728e2008-06-24 15:50:53 +00001975MethodProto(mproto) {
1976 NumArgs = nargs;
1977 SubExprs = new Stmt*[NumArgs+1];
1978 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1979 if (NumArgs) {
1980 for (unsigned i = 0; i != NumArgs; ++i)
1981 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1982 }
1983 LBracloc = LBrac;
1984 RBracloc = RBrac;
1985}
1986
1987ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1988 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1989 switch (x & Flags) {
1990 default:
1991 assert(false && "Invalid ObjCMessageExpr.");
1992 case IsInstMeth:
1993 return ClassInfo(0, 0);
1994 case IsClsMethDeclUnknown:
1995 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1996 case IsClsMethDeclKnown: {
1997 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1998 return ClassInfo(D, D->getIdentifier());
1999 }
2000 }
2001}
2002
Chris Lattner0389e6b2009-04-26 00:44:05 +00002003void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
2004 if (CI.first == 0 && CI.second == 0)
2005 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
2006 else if (CI.first == 0)
2007 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
2008 else
2009 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
2010}
2011
2012
Chris Lattner27437ca2007-10-25 00:29:32 +00002013bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002014 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002015}
2016
Nate Begeman888376a2009-08-12 02:28:50 +00002017void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2018 unsigned NumExprs) {
2019 if (SubExprs) C.Deallocate(SubExprs);
2020
2021 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002022 this->NumExprs = NumExprs;
2023 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002024}
Nate Begeman888376a2009-08-12 02:28:50 +00002025
2026void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2027 DestroyChildren(C);
2028 if (SubExprs) C.Deallocate(SubExprs);
2029 this->~ShuffleVectorExpr();
2030 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002031}
2032
Douglas Gregor42602bb2009-08-07 06:08:38 +00002033void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002034 // Override default behavior of traversing children. If this has a type
2035 // operand and the type is a variable-length array, the child iteration
2036 // will iterate over the size expression. However, this expression belongs
2037 // to the type, not to this, so we don't want to delete it.
2038 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002039 if (isArgumentType()) {
2040 this->~SizeOfAlignOfExpr();
2041 C.Deallocate(this);
2042 }
Sebastian Redl05189992008-11-11 17:56:53 +00002043 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002044 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002045}
2046
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002047//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002048// DesignatedInitExpr
2049//===----------------------------------------------------------------------===//
2050
2051IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2052 assert(Kind == FieldDesignator && "Only valid on a field designator");
2053 if (Field.NameOrField & 0x01)
2054 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2055 else
2056 return getField()->getIdentifier();
2057}
2058
Mike Stump1eb44332009-09-09 15:08:12 +00002059DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002060 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002061 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002062 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002063 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002064 unsigned NumIndexExprs,
2065 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002066 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002067 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002068 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2069 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002070 this->Designators = new Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002071
2072 // Record the initializer itself.
2073 child_iterator Child = child_begin();
2074 *Child++ = Init;
2075
2076 // Copy the designators and their subexpressions, computing
2077 // value-dependence along the way.
2078 unsigned IndexIdx = 0;
2079 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002080 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002081
2082 if (this->Designators[I].isArrayDesignator()) {
2083 // Compute type- and value-dependence.
2084 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002085 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002086 Index->isTypeDependent() || Index->isValueDependent();
2087
2088 // Copy the index expressions into permanent storage.
2089 *Child++ = IndexExprs[IndexIdx++];
2090 } else if (this->Designators[I].isArrayRangeDesignator()) {
2091 // Compute type- and value-dependence.
2092 Expr *Start = IndexExprs[IndexIdx];
2093 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002094 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002095 Start->isTypeDependent() || Start->isValueDependent() ||
2096 End->isTypeDependent() || End->isValueDependent();
2097
2098 // Copy the start/end expressions into permanent storage.
2099 *Child++ = IndexExprs[IndexIdx++];
2100 *Child++ = IndexExprs[IndexIdx++];
2101 }
2102 }
2103
2104 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002105}
2106
Douglas Gregor05c13a32009-01-22 00:58:24 +00002107DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002108DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002109 unsigned NumDesignators,
2110 Expr **IndexExprs, unsigned NumIndexExprs,
2111 SourceLocation ColonOrEqualLoc,
2112 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002113 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002114 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor9ea62762009-05-21 23:17:49 +00002115 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
2116 ColonOrEqualLoc, UsesColonSyntax,
2117 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002118}
2119
Mike Stump1eb44332009-09-09 15:08:12 +00002120DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002121 unsigned NumIndexExprs) {
2122 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2123 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2124 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2125}
2126
Mike Stump1eb44332009-09-09 15:08:12 +00002127void DesignatedInitExpr::setDesignators(const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002128 unsigned NumDesigs) {
2129 if (Designators)
2130 delete [] Designators;
2131
2132 Designators = new Designator[NumDesigs];
2133 NumDesignators = NumDesigs;
2134 for (unsigned I = 0; I != NumDesigs; ++I)
2135 Designators[I] = Desigs[I];
2136}
2137
Douglas Gregor05c13a32009-01-22 00:58:24 +00002138SourceRange DesignatedInitExpr::getSourceRange() const {
2139 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002140 Designator &First =
2141 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002142 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002143 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002144 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2145 else
2146 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2147 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002148 StartLoc =
2149 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002150 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2151}
2152
Douglas Gregor05c13a32009-01-22 00:58:24 +00002153Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2154 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2155 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2156 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002157 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2158 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2159}
2160
2161Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002162 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002163 "Requires array range designator");
2164 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2165 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002166 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2167 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2168}
2169
2170Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002171 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002172 "Requires array range designator");
2173 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2174 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002175 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2176 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2177}
2178
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002179/// \brief Replaces the designator at index @p Idx with the series
2180/// of designators in [First, Last).
Mike Stump1eb44332009-09-09 15:08:12 +00002181void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
2182 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002183 const Designator *Last) {
2184 unsigned NumNewDesignators = Last - First;
2185 if (NumNewDesignators == 0) {
2186 std::copy_backward(Designators + Idx + 1,
2187 Designators + NumDesignators,
2188 Designators + Idx);
2189 --NumNewDesignators;
2190 return;
2191 } else if (NumNewDesignators == 1) {
2192 Designators[Idx] = *First;
2193 return;
2194 }
2195
Mike Stump1eb44332009-09-09 15:08:12 +00002196 Designator *NewDesignators
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002197 = new Designator[NumDesignators - 1 + NumNewDesignators];
2198 std::copy(Designators, Designators + Idx, NewDesignators);
2199 std::copy(First, Last, NewDesignators + Idx);
2200 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2201 NewDesignators + Idx + NumNewDesignators);
2202 delete [] Designators;
2203 Designators = NewDesignators;
2204 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2205}
2206
Douglas Gregor42602bb2009-08-07 06:08:38 +00002207void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002208 delete [] Designators;
Douglas Gregor42602bb2009-08-07 06:08:38 +00002209 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002210}
2211
Mike Stump1eb44332009-09-09 15:08:12 +00002212ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002213 Expr **exprs, unsigned nexprs,
2214 SourceLocation rparenloc)
2215: Expr(ParenListExprClass, QualType(),
2216 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002217 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002218 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002219
Nate Begeman2ef13e52009-08-10 23:49:36 +00002220 Exprs = new (C) Stmt*[nexprs];
2221 for (unsigned i = 0; i != nexprs; ++i)
2222 Exprs[i] = exprs[i];
2223}
2224
2225void ParenListExpr::DoDestroy(ASTContext& C) {
2226 DestroyChildren(C);
2227 if (Exprs) C.Deallocate(Exprs);
2228 this->~ParenListExpr();
2229 C.Deallocate(this);
2230}
2231
Douglas Gregor05c13a32009-01-22 00:58:24 +00002232//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002233// ExprIterator.
2234//===----------------------------------------------------------------------===//
2235
2236Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2237Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2238Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2239const Expr* ConstExprIterator::operator[](size_t idx) const {
2240 return cast<Expr>(I[idx]);
2241}
2242const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2243const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2244
2245//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002246// Child Iterators for iterating over subexpressions/substatements
2247//===----------------------------------------------------------------------===//
2248
2249// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002250Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2251Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002252
Steve Naroff7779db42007-11-12 14:29:37 +00002253// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002254Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2255Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002256
Steve Naroffe3e9add2008-06-02 23:03:37 +00002257// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002258Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2259Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002260
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002261// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002262Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2263 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002264}
Mike Stump1eb44332009-09-09 15:08:12 +00002265Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2266 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002267}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002268
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002269// ObjCSuperExpr
2270Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2271Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2272
Steve Narofff242b1b2009-07-24 17:54:45 +00002273// ObjCIsaExpr
2274Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2275Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2276
Chris Lattnerd9f69102008-08-10 01:53:14 +00002277// PredefinedExpr
2278Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2279Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002280
2281// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002282Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2283Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002284
2285// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002286Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002287Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002288
2289// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002290Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2291Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002292
Chris Lattner5d661452007-08-26 03:42:43 +00002293// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002294Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2295Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002296
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002297// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002298Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2299Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002300
2301// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002302Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2303Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002304
2305// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002306Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2307Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002308
Sebastian Redl05189992008-11-11 17:56:53 +00002309// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002310Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002311 // If this is of a type and the type is a VLA type (and not a typedef), the
2312 // size expression of the VLA needs to be treated as an executable expression.
2313 // Why isn't this weirdness documented better in StmtIterator?
2314 if (isArgumentType()) {
2315 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2316 getArgumentType().getTypePtr()))
2317 return child_iterator(T);
2318 return child_iterator();
2319 }
Sebastian Redld4575892008-12-03 23:17:54 +00002320 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002321}
Sebastian Redl05189992008-11-11 17:56:53 +00002322Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2323 if (isArgumentType())
2324 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002325 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002326}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002327
2328// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002329Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002330 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002331}
Ted Kremenek1237c672007-08-24 20:06:47 +00002332Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002333 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002334}
2335
2336// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002337Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002338 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002339}
Ted Kremenek1237c672007-08-24 20:06:47 +00002340Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002341 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002342}
Ted Kremenek1237c672007-08-24 20:06:47 +00002343
2344// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002345Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2346Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002347
Nate Begeman213541a2008-04-18 23:10:10 +00002348// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002349Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2350Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002351
2352// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002353Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2354Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002355
Ted Kremenek1237c672007-08-24 20:06:47 +00002356// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002357Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2358Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002359
2360// BinaryOperator
2361Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002362 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002363}
Ted Kremenek1237c672007-08-24 20:06:47 +00002364Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002365 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002366}
2367
2368// ConditionalOperator
2369Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002370 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002371}
Ted Kremenek1237c672007-08-24 20:06:47 +00002372Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002373 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002374}
2375
2376// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002377Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2378Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002379
Ted Kremenek1237c672007-08-24 20:06:47 +00002380// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002381Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2382Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002383
2384// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002385Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2386 return child_iterator();
2387}
2388
2389Stmt::child_iterator TypesCompatibleExpr::child_end() {
2390 return child_iterator();
2391}
Ted Kremenek1237c672007-08-24 20:06:47 +00002392
2393// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002394Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2395Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002396
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002397// GNUNullExpr
2398Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2399Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2400
Eli Friedmand38617c2008-05-14 19:38:39 +00002401// ShuffleVectorExpr
2402Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002403 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002404}
2405Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002406 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002407}
2408
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002409// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002410Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2411Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002412
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002413// InitListExpr
2414Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002415 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002416}
2417Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002418 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002419}
2420
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002421// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002422Stmt::child_iterator DesignatedInitExpr::child_begin() {
2423 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2424 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002425 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2426}
2427Stmt::child_iterator DesignatedInitExpr::child_end() {
2428 return child_iterator(&*child_begin() + NumSubExprs);
2429}
2430
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002431// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002432Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2433 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002434}
2435
Mike Stump1eb44332009-09-09 15:08:12 +00002436Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2437 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002438}
2439
Nate Begeman2ef13e52009-08-10 23:49:36 +00002440// ParenListExpr
2441Stmt::child_iterator ParenListExpr::child_begin() {
2442 return &Exprs[0];
2443}
2444Stmt::child_iterator ParenListExpr::child_end() {
2445 return &Exprs[0]+NumExprs;
2446}
2447
Ted Kremenek1237c672007-08-24 20:06:47 +00002448// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002449Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002450 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002451}
2452Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002453 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002454}
Ted Kremenek1237c672007-08-24 20:06:47 +00002455
2456// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002457Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2458Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002459
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002460// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002461Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002462 return child_iterator();
2463}
2464Stmt::child_iterator ObjCSelectorExpr::child_end() {
2465 return child_iterator();
2466}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002467
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002468// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002469Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2470 return child_iterator();
2471}
2472Stmt::child_iterator ObjCProtocolExpr::child_end() {
2473 return child_iterator();
2474}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002475
Steve Naroff563477d2007-09-18 23:55:05 +00002476// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002477Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002478 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002479}
2480Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002481 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002482}
2483
Steve Naroff4eb206b2008-09-03 18:15:37 +00002484// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002485Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2486Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002487
Ted Kremenek9da13f92008-09-26 23:24:14 +00002488Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2489Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }