blob: 55d1d2bc111504aa3b0cd1cca1e7792e7962cf5e [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"
Anders Carlsson3a082d82009-09-08 18:24:21 +000025#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000026#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// Primary Expressions.
31//===----------------------------------------------------------------------===//
32
Anders Carlsson3a082d82009-09-08 18:24:21 +000033// FIXME: Maybe this should use DeclPrinter with a special "print predefined
34// expr" policy instead.
35std::string PredefinedExpr::ComputeName(ASTContext &Context, IdentType IT,
36 const Decl *CurrentDecl) {
37 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
38 if (IT != PrettyFunction)
39 return FD->getNameAsString();
40
41 llvm::SmallString<256> Name;
42 llvm::raw_svector_ostream Out(Name);
43
44 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
45 if (MD->isVirtual())
46 Out << "virtual ";
47 }
48
49 PrintingPolicy Policy(Context.getLangOptions());
50 Policy.SuppressTagKind = true;
51
52 std::string Proto = FD->getQualifiedNameAsString(Policy);
53
John McCall183700f2009-09-21 23:43:11 +000054 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +000055 const FunctionProtoType *FT = 0;
56 if (FD->hasWrittenPrototype())
57 FT = dyn_cast<FunctionProtoType>(AFT);
58
59 Proto += "(";
60 if (FT) {
61 llvm::raw_string_ostream POut(Proto);
62 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
63 if (i) POut << ", ";
64 std::string Param;
65 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
66 POut << Param;
67 }
68
69 if (FT->isVariadic()) {
70 if (FD->getNumParams()) POut << ", ";
71 POut << "...";
72 }
73 }
74 Proto += ")";
75
76 AFT->getResultType().getAsStringInternal(Proto, Policy);
77
78 Out << Proto;
79
80 Out.flush();
81 return Name.str().str();
82 }
83 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
84 llvm::SmallString<256> Name;
85 llvm::raw_svector_ostream Out(Name);
86 Out << (MD->isInstanceMethod() ? '-' : '+');
87 Out << '[';
88 Out << MD->getClassInterface()->getNameAsString();
89 if (const ObjCCategoryImplDecl *CID =
90 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
91 Out << '(';
92 Out << CID->getNameAsString();
93 Out << ')';
94 }
95 Out << ' ';
96 Out << MD->getSelector().getAsString();
97 Out << ']';
98
99 Out.flush();
100 return Name.str().str();
101 }
102 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
103 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
104 return "top level";
105 }
106 return "";
107}
108
Chris Lattnerda8249e2008-06-07 22:13:43 +0000109/// getValueAsApproximateDouble - This returns the value as an inaccurate
110/// double. Note that this may cause loss of precision, but is useful for
111/// debugging dumps, etc.
112double FloatingLiteral::getValueAsApproximateDouble() const {
113 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000114 bool ignored;
115 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
116 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000117 return V.convertToDouble();
118}
119
Chris Lattner2085fd62009-02-18 06:40:38 +0000120StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
121 unsigned ByteLength, bool Wide,
122 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000123 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000124 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000125 // Allocate enough space for the StringLiteral plus an array of locations for
126 // any concatenated string tokens.
127 void *Mem = C.Allocate(sizeof(StringLiteral)+
128 sizeof(SourceLocation)*(NumStrs-1),
129 llvm::alignof<StringLiteral>());
130 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000133 char *AStrData = new (C, 1) char[ByteLength];
134 memcpy(AStrData, StrData, ByteLength);
135 SL->StrData = AStrData;
136 SL->ByteLength = ByteLength;
137 SL->IsWide = Wide;
138 SL->TokLocs[0] = Loc[0];
139 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000140
Chris Lattner726e1682009-02-18 05:49:11 +0000141 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000142 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
143 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000144}
145
Douglas Gregor673ecd62009-04-15 16:35:07 +0000146StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
147 void *Mem = C.Allocate(sizeof(StringLiteral)+
148 sizeof(SourceLocation)*(NumStrs-1),
149 llvm::alignof<StringLiteral>());
150 StringLiteral *SL = new (Mem) StringLiteral(QualType());
151 SL->StrData = 0;
152 SL->ByteLength = 0;
153 SL->NumConcatenated = NumStrs;
154 return SL;
155}
156
Douglas Gregor42602bb2009-08-07 06:08:38 +0000157void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000158 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000159 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160}
161
Daniel Dunbarb6480232009-09-22 03:27:33 +0000162void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000163 if (StrData)
164 C.Deallocate(const_cast<char*>(StrData));
165
Daniel Dunbarb6480232009-09-22 03:27:33 +0000166 char *AStrData = new (C, 1) char[Str.size()];
167 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000168 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000169 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000170}
171
Reid Spencer5f016e22007-07-11 17:01:13 +0000172/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
173/// corresponds to, e.g. "sizeof" or "[pre]++".
174const char *UnaryOperator::getOpcodeStr(Opcode Op) {
175 switch (Op) {
176 default: assert(0 && "Unknown unary operator");
177 case PostInc: return "++";
178 case PostDec: return "--";
179 case PreInc: return "++";
180 case PreDec: return "--";
181 case AddrOf: return "&";
182 case Deref: return "*";
183 case Plus: return "+";
184 case Minus: return "-";
185 case Not: return "~";
186 case LNot: return "!";
187 case Real: return "__real";
188 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000190 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 }
192}
193
Mike Stump1eb44332009-09-09 15:08:12 +0000194UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000195UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
196 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000197 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000198 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
199 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
200 case OO_Amp: return AddrOf;
201 case OO_Star: return Deref;
202 case OO_Plus: return Plus;
203 case OO_Minus: return Minus;
204 case OO_Tilde: return Not;
205 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000206 }
207}
208
209OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
210 switch (Opc) {
211 case PostInc: case PreInc: return OO_PlusPlus;
212 case PostDec: case PreDec: return OO_MinusMinus;
213 case AddrOf: return OO_Amp;
214 case Deref: return OO_Star;
215 case Plus: return OO_Plus;
216 case Minus: return OO_Minus;
217 case Not: return OO_Tilde;
218 case LNot: return OO_Exclaim;
219 default: return OO_None;
220 }
221}
222
223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224//===----------------------------------------------------------------------===//
225// Postfix Operators.
226//===----------------------------------------------------------------------===//
227
Ted Kremenek668bf912009-02-09 20:51:47 +0000228CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000229 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000230 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000231 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000232 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000233 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenek668bf912009-02-09 20:51:47 +0000235 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000236 SubExprs[FN] = fn;
237 for (unsigned i = 0; i != numargs; ++i)
238 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000239
Douglas Gregorb4609802008-11-14 16:09:21 +0000240 RParenLoc = rparenloc;
241}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000242
Ted Kremenek668bf912009-02-09 20:51:47 +0000243CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
244 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000245 : Expr(CallExprClass, t,
246 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000247 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000248 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000249
250 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000251 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000253 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 RParenLoc = rparenloc;
256}
257
Mike Stump1eb44332009-09-09 15:08:12 +0000258CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
259 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000260 SubExprs = new (C) Stmt*[1];
261}
262
Douglas Gregor42602bb2009-08-07 06:08:38 +0000263void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000264 DestroyChildren(C);
265 if (SubExprs) C.Deallocate(SubExprs);
266 this->~CallExpr();
267 C.Deallocate(this);
268}
269
Zhongxing Xua0042542009-07-17 07:29:51 +0000270FunctionDecl *CallExpr::getDirectCallee() {
271 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000272 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xua0042542009-07-17 07:29:51 +0000273 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xua0042542009-07-17 07:29:51 +0000274
275 return 0;
276}
277
Chris Lattnerd18b3292007-12-28 05:25:02 +0000278/// setNumArgs - This changes the number of arguments present in this call.
279/// Any orphaned expressions are deleted by this, and any new operands are set
280/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000281void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000282 // No change, just return.
283 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Chris Lattnerd18b3292007-12-28 05:25:02 +0000285 // If shrinking # arguments, just delete the extras and forgot them.
286 if (NumArgs < getNumArgs()) {
287 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000288 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000289 this->NumArgs = NumArgs;
290 return;
291 }
292
293 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000294 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000295 // Copy over args.
296 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
297 NewSubExprs[i] = SubExprs[i];
298 // Null out new args.
299 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
300 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Douglas Gregor88c9a462009-04-17 21:46:47 +0000302 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000303 SubExprs = NewSubExprs;
304 this->NumArgs = NumArgs;
305}
306
Chris Lattnercb888962008-10-06 05:00:53 +0000307/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
308/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000309unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000310 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000311 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000312 // ImplicitCastExpr.
313 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
314 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000315 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000317 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
318 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000319 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Anders Carlssonbcba2012008-01-31 02:13:57 +0000321 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
322 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000323 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000325 if (!FDecl->getIdentifier())
326 return 0;
327
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000328 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000329}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000330
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000331QualType CallExpr::getCallReturnType() const {
332 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000333 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000334 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000335 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000336 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000337
John McCall183700f2009-09-21 23:43:11 +0000338 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000339 return FnType->getResultType();
340}
Chris Lattnercb888962008-10-06 05:00:53 +0000341
Mike Stump1eb44332009-09-09 15:08:12 +0000342MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
343 SourceRange qualrange, NamedDecl *memberdecl,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000344 SourceLocation l, bool has_explicit,
345 SourceLocation langle,
346 const TemplateArgument *targs, unsigned numtargs,
347 SourceLocation rangle, QualType ty)
Mike Stump1eb44332009-09-09 15:08:12 +0000348 : Expr(MemberExprClass, ty,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000349 base->isTypeDependent() || (qual && qual->isDependent()),
350 base->isValueDependent() || (qual && qual->isDependent())),
351 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000352 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(has_explicit) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000353 // Initialize the qualifier, if any.
354 if (HasQualifier) {
355 NameQualifier *NQ = getMemberQualifier();
356 NQ->NNS = qual;
357 NQ->Range = qualrange;
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000360 // Initialize the explicit template argument list, if any.
361 if (HasExplicitTemplateArgumentList) {
Mike Stump1eb44332009-09-09 15:08:12 +0000362 ExplicitTemplateArgumentList *ETemplateArgs
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000363 = getExplicitTemplateArgumentList();
364 ETemplateArgs->LAngleLoc = langle;
365 ETemplateArgs->RAngleLoc = rangle;
366 ETemplateArgs->NumTemplateArgs = numtargs;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000368 TemplateArgument *TemplateArgs = ETemplateArgs->getTemplateArgs();
369 for (unsigned I = 0; I < numtargs; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000370 new (TemplateArgs + I) TemplateArgument(targs[I]);
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000371 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000372}
373
Mike Stump1eb44332009-09-09 15:08:12 +0000374MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
375 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000376 SourceRange qualrange,
Mike Stump1eb44332009-09-09 15:08:12 +0000377 NamedDecl *memberdecl,
378 SourceLocation l,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000379 bool has_explicit,
380 SourceLocation langle,
381 const TemplateArgument *targs,
382 unsigned numtargs,
383 SourceLocation rangle,
384 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000385 std::size_t Size = sizeof(MemberExpr);
386 if (qual != 0)
387 Size += sizeof(NameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000389 if (has_explicit)
Mike Stump1eb44332009-09-09 15:08:12 +0000390 Size += sizeof(ExplicitTemplateArgumentList) +
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000391 sizeof(TemplateArgument) * numtargs;
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000393 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000394 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
395 has_explicit, langle, targs, numtargs, rangle,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000396 ty);
397}
398
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000399const char *CastExpr::getCastKindName() const {
400 switch (getCastKind()) {
401 case CastExpr::CK_Unknown:
402 return "Unknown";
403 case CastExpr::CK_BitCast:
404 return "BitCast";
405 case CastExpr::CK_NoOp:
406 return "NoOp";
407 case CastExpr::CK_DerivedToBase:
408 return "DerivedToBase";
409 case CastExpr::CK_Dynamic:
410 return "Dynamic";
411 case CastExpr::CK_ToUnion:
412 return "ToUnion";
413 case CastExpr::CK_ArrayToPointerDecay:
414 return "ArrayToPointerDecay";
415 case CastExpr::CK_FunctionToPointerDecay:
416 return "FunctionToPointerDecay";
417 case CastExpr::CK_NullToMemberPointer:
418 return "NullToMemberPointer";
419 case CastExpr::CK_BaseToDerivedMemberPointer:
420 return "BaseToDerivedMemberPointer";
421 case CastExpr::CK_UserDefinedConversion:
422 return "UserDefinedConversion";
423 case CastExpr::CK_ConstructorConversion:
424 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000425 case CastExpr::CK_IntegralToPointer:
426 return "IntegralToPointer";
427 case CastExpr::CK_PointerToIntegral:
428 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000429 case CastExpr::CK_ToVoid:
430 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000431 case CastExpr::CK_VectorSplat:
432 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000433 case CastExpr::CK_IntegralCast:
434 return "IntegralCast";
435 case CastExpr::CK_IntegralToFloating:
436 return "IntegralToFloating";
437 case CastExpr::CK_FloatingToIntegral:
438 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000439 case CastExpr::CK_FloatingCast:
440 return "FloatingCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000441 }
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000443 assert(0 && "Unhandled cast kind!");
444 return 0;
445}
446
Reid Spencer5f016e22007-07-11 17:01:13 +0000447/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
448/// corresponds to, e.g. "<<=".
449const char *BinaryOperator::getOpcodeStr(Opcode Op) {
450 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000451 case PtrMemD: return ".*";
452 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 case Mul: return "*";
454 case Div: return "/";
455 case Rem: return "%";
456 case Add: return "+";
457 case Sub: return "-";
458 case Shl: return "<<";
459 case Shr: return ">>";
460 case LT: return "<";
461 case GT: return ">";
462 case LE: return "<=";
463 case GE: return ">=";
464 case EQ: return "==";
465 case NE: return "!=";
466 case And: return "&";
467 case Xor: return "^";
468 case Or: return "|";
469 case LAnd: return "&&";
470 case LOr: return "||";
471 case Assign: return "=";
472 case MulAssign: return "*=";
473 case DivAssign: return "/=";
474 case RemAssign: return "%=";
475 case AddAssign: return "+=";
476 case SubAssign: return "-=";
477 case ShlAssign: return "<<=";
478 case ShrAssign: return ">>=";
479 case AndAssign: return "&=";
480 case XorAssign: return "^=";
481 case OrAssign: return "|=";
482 case Comma: return ",";
483 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000484
485 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000486}
487
Mike Stump1eb44332009-09-09 15:08:12 +0000488BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000489BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
490 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000491 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000492 case OO_Plus: return Add;
493 case OO_Minus: return Sub;
494 case OO_Star: return Mul;
495 case OO_Slash: return Div;
496 case OO_Percent: return Rem;
497 case OO_Caret: return Xor;
498 case OO_Amp: return And;
499 case OO_Pipe: return Or;
500 case OO_Equal: return Assign;
501 case OO_Less: return LT;
502 case OO_Greater: return GT;
503 case OO_PlusEqual: return AddAssign;
504 case OO_MinusEqual: return SubAssign;
505 case OO_StarEqual: return MulAssign;
506 case OO_SlashEqual: return DivAssign;
507 case OO_PercentEqual: return RemAssign;
508 case OO_CaretEqual: return XorAssign;
509 case OO_AmpEqual: return AndAssign;
510 case OO_PipeEqual: return OrAssign;
511 case OO_LessLess: return Shl;
512 case OO_GreaterGreater: return Shr;
513 case OO_LessLessEqual: return ShlAssign;
514 case OO_GreaterGreaterEqual: return ShrAssign;
515 case OO_EqualEqual: return EQ;
516 case OO_ExclaimEqual: return NE;
517 case OO_LessEqual: return LE;
518 case OO_GreaterEqual: return GE;
519 case OO_AmpAmp: return LAnd;
520 case OO_PipePipe: return LOr;
521 case OO_Comma: return Comma;
522 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000523 }
524}
525
526OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
527 static const OverloadedOperatorKind OverOps[] = {
528 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
529 OO_Star, OO_Slash, OO_Percent,
530 OO_Plus, OO_Minus,
531 OO_LessLess, OO_GreaterGreater,
532 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
533 OO_EqualEqual, OO_ExclaimEqual,
534 OO_Amp,
535 OO_Caret,
536 OO_Pipe,
537 OO_AmpAmp,
538 OO_PipePipe,
539 OO_Equal, OO_StarEqual,
540 OO_SlashEqual, OO_PercentEqual,
541 OO_PlusEqual, OO_MinusEqual,
542 OO_LessLessEqual, OO_GreaterGreaterEqual,
543 OO_AmpEqual, OO_CaretEqual,
544 OO_PipeEqual,
545 OO_Comma
546 };
547 return OverOps[Opc];
548}
549
Mike Stump1eb44332009-09-09 15:08:12 +0000550InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000551 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000552 SourceLocation rbraceloc)
Douglas Gregor9ea62762009-05-21 23:17:49 +0000553 : Expr(InitListExprClass, QualType(),
554 hasAnyTypeDependentArguments(initExprs, numInits),
555 hasAnyValueDependentArguments(initExprs, numInits)),
Mike Stump1eb44332009-09-09 15:08:12 +0000556 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000557 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000558
559 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000560}
Reid Spencer5f016e22007-07-11 17:01:13 +0000561
Douglas Gregorfa219202009-03-20 23:58:33 +0000562void InitListExpr::reserveInits(unsigned NumInits) {
563 if (NumInits > InitExprs.size())
564 InitExprs.reserve(NumInits);
565}
566
Douglas Gregor4c678342009-01-28 21:54:33 +0000567void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000568 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000569 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000570 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000571 InitExprs.resize(NumInits, 0);
572}
573
574Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
575 if (Init >= InitExprs.size()) {
576 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
577 InitExprs.back() = expr;
578 return 0;
579 }
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Douglas Gregor4c678342009-01-28 21:54:33 +0000581 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
582 InitExprs[Init] = expr;
583 return Result;
584}
585
Steve Naroffbfdcae62008-09-04 15:31:07 +0000586/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000587///
588const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000589 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000590 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000591}
592
Mike Stump1eb44332009-09-09 15:08:12 +0000593SourceLocation BlockExpr::getCaretLocation() const {
594 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000595}
Mike Stump1eb44332009-09-09 15:08:12 +0000596const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000597 return TheBlock->getBody();
598}
Mike Stump1eb44332009-09-09 15:08:12 +0000599Stmt *BlockExpr::getBody() {
600 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000601}
Steve Naroff56ee6892008-10-08 17:01:13 +0000602
603
Reid Spencer5f016e22007-07-11 17:01:13 +0000604//===----------------------------------------------------------------------===//
605// Generic Expression Routines
606//===----------------------------------------------------------------------===//
607
Chris Lattner026dc962009-02-14 07:37:35 +0000608/// isUnusedResultAWarning - Return true if this immediate expression should
609/// be warned about if the result is unused. If so, fill in Loc and Ranges
610/// with location to warn on and the source range[s] to report with the
611/// warning.
612bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000613 SourceRange &R2) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000614 // Don't warn if the expr is type dependent. The type could end up
615 // instantiating to void.
616 if (isTypeDependent())
617 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 switch (getStmtClass()) {
620 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000621 Loc = getExprLoc();
622 R1 = getSourceRange();
623 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000625 return cast<ParenExpr>(this)->getSubExpr()->
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000626 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 case UnaryOperatorClass: {
628 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000631 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 case UnaryOperator::PostInc:
633 case UnaryOperator::PostDec:
634 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000635 case UnaryOperator::PreDec: // ++/--
636 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 case UnaryOperator::Deref:
638 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000639 if (getType().isVolatileQualified())
640 return false;
641 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 case UnaryOperator::Real:
643 case UnaryOperator::Imag:
644 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000645 if (UO->getSubExpr()->getType().isVolatileQualified())
646 return false;
647 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 case UnaryOperator::Extension:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000649 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 }
Chris Lattner026dc962009-02-14 07:37:35 +0000651 Loc = UO->getOperatorLoc();
652 R1 = UO->getSubExpr()->getSourceRange();
653 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000655 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000656 const BinaryOperator *BO = cast<BinaryOperator>(this);
657 // Consider comma to have side effects if the LHS or RHS does.
658 if (BO->getOpcode() == BinaryOperator::Comma)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000659 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
660 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Chris Lattner026dc962009-02-14 07:37:35 +0000662 if (BO->isAssignmentOp())
663 return false;
664 Loc = BO->getOperatorLoc();
665 R1 = BO->getLHS()->getSourceRange();
666 R2 = BO->getRHS()->getSourceRange();
667 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000668 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000669 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000670 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000671
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000672 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000673 // The condition must be evaluated, but if either the LHS or RHS is a
674 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000675 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000676 if (Exp->getLHS() &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000677 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000678 return true;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000679 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000680 }
681
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000683 // If the base pointer or element is to a volatile pointer/field, accessing
684 // it is a side effect.
685 if (getType().isVolatileQualified())
686 return false;
687 Loc = cast<MemberExpr>(this)->getMemberLoc();
688 R1 = SourceRange(Loc, Loc);
689 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
690 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 case ArraySubscriptExprClass:
693 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000694 // it is a side effect.
695 if (getType().isVolatileQualified())
696 return false;
697 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
698 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
699 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
700 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000701
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000703 case CXXOperatorCallExprClass:
704 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000705 // If this is a direct call, get the callee.
706 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000707 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000708 // If the callee has attribute pure, const, or warn_unused_result, warn
709 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000710 //
711 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
712 // updated to match for QoI.
713 if (FD->getAttr<WarnUnusedResultAttr>() ||
714 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
715 Loc = CE->getCallee()->getLocStart();
716 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000718 if (unsigned NumArgs = CE->getNumArgs())
719 R2 = SourceRange(CE->getArg(0)->getLocStart(),
720 CE->getArg(NumArgs-1)->getLocEnd());
721 return true;
722 }
Chris Lattner026dc962009-02-14 07:37:35 +0000723 }
724 return false;
725 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000726 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000727 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000729 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000730#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000731 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000732 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000733 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000734 Loc = Ref->getLocation();
735 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
736 if (Ref->getBase())
737 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000738#else
739 Loc = getExprLoc();
740 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000741#endif
742 return true;
743 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000744 case StmtExprClass: {
745 // Statement exprs don't logically have side effects themselves, but are
746 // sometimes used in macros in ways that give them a type that is unused.
747 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
748 // however, if the result of the stmt expr is dead, we don't want to emit a
749 // warning.
750 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
751 if (!CS->body_empty())
752 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000753 return E->isUnusedResultAWarning(Loc, R1, R2);
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner026dc962009-02-14 07:37:35 +0000755 Loc = cast<StmtExpr>(this)->getLParenLoc();
756 R1 = getSourceRange();
757 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000758 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000759 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000760 // If this is an explicit cast to void, allow it. People do this when they
761 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000762 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000763 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000764 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
765 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
766 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000767 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 // If this is a cast to void, check the operand. Otherwise, the result of
769 // the cast is unused.
770 if (getType()->isVoidType())
Douglas Gregor68584ed2009-06-18 16:11:24 +0000771 return cast<CastExpr>(this)->getSubExpr()
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000772 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner026dc962009-02-14 07:37:35 +0000773 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
774 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
775 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Eli Friedman4be1f472008-05-19 21:24:43 +0000777 case ImplicitCastExprClass:
778 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000779 return cast<ImplicitCastExpr>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000780 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000781
Chris Lattner04421082008-04-08 04:40:51 +0000782 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000783 return cast<CXXDefaultArgExpr>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000784 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000785
786 case CXXNewExprClass:
787 // FIXME: In theory, there might be new expressions that don't have side
788 // effects (e.g. a placement new with an uninitialized POD).
789 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000790 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +0000791 case CXXBindTemporaryExprClass:
792 return cast<CXXBindTemporaryExpr>(this)
793 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000794 case CXXExprWithTemporariesClass:
795 return cast<CXXExprWithTemporaries>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000796 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000797 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000798}
799
Douglas Gregorba7e2102008-10-22 15:04:37 +0000800/// DeclCanBeLvalue - Determine whether the given declaration can be
801/// an lvalue. This is a helper routine for isLvalue.
802static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000803 // C++ [temp.param]p6:
804 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000805 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +0000806 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
807 return NTTParm->getType()->isReferenceType();
808
Douglas Gregor44b43212008-12-11 16:49:14 +0000809 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000810 // C++ 3.10p2: An lvalue refers to an object or function.
811 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor83314aa2009-07-08 20:55:45 +0000812 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
813 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +0000814}
815
Reid Spencer5f016e22007-07-11 17:01:13 +0000816/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
817/// incomplete type other than void. Nonarray expressions that can be lvalues:
818/// - name, where name must be a variable
819/// - e[i]
820/// - (e), where e must be an lvalue
821/// - e.name, where e must be an lvalue
822/// - e->name
823/// - *e, the type of e cannot be a function type
824/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000825/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000826/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000827///
Chris Lattner28be73f2008-07-26 21:30:36 +0000828Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +0000829 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
830
831 isLvalueResult Res = isLvalueInternal(Ctx);
832 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
833 return Res;
834
Douglas Gregor98cd5992008-10-21 23:43:52 +0000835 // first, check the type (C99 6.3.2.1). Expressions with function
836 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +0000837 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 return LV_NotObjectType;
839
Steve Naroffacb818a2008-02-10 01:39:04 +0000840 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +0000841 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000842 return LV_IncompleteVoidType;
843
Eli Friedman53202852009-05-03 22:36:05 +0000844 return LV_Valid;
845}
Bill Wendling08ad47c2007-07-17 03:52:31 +0000846
Eli Friedman53202852009-05-03 22:36:05 +0000847// Check whether the expression can be sanely treated like an l-value
848Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000850 case StringLiteralClass: // C99 6.5.1p4
851 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000852 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
854 // For vectors, make sure base is an lvalue (i.e. not a function call).
855 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000856 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 return LV_Valid;
Mike Stump1eb44332009-09-09 15:08:12 +0000858 case DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000859 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000860 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
861 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 return LV_Valid;
863 break;
Chris Lattner41110242008-06-17 18:05:57 +0000864 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000865 case BlockDeclRefExprClass: {
866 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000867 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000868 return LV_Valid;
869 break;
870 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000871 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000873 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
874 NamedDecl *Member = m->getMemberDecl();
875 // C++ [expr.ref]p4:
876 // If E2 is declared to have type "reference to T", then E1.E2
877 // is an lvalue.
878 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
879 if (Value->getType()->isReferenceType())
880 return LV_Valid;
881
882 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000883 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000884 return LV_Valid;
885
886 // -- If E2 is a non-static data member [...]. If E1 is an
887 // lvalue, then E1.E2 is an lvalue.
888 if (isa<FieldDecl>(Member))
889 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
890
891 // -- If it refers to a static member function [...], then
892 // E1.E2 is an lvalue.
893 // -- Otherwise, if E1.E2 refers to a non-static member
894 // function [...], then E1.E2 is not an lvalue.
895 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
896 return Method->isStatic()? LV_Valid : LV_MemberFunction;
897
898 // -- If E2 is a member enumerator [...], the expression E1.E2
899 // is not an lvalue.
900 if (isa<EnumConstantDecl>(Member))
901 return LV_InvalidExpression;
902
903 // Not an lvalue.
904 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +0000905 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000906
907 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000908 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000909 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000910 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000912 return LV_Valid; // C99 6.5.3p4
913
914 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000915 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
916 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000917 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000918
919 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
920 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
921 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
922 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000924 case ImplicitCastExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000925 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000926 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000928 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000929 case BinaryOperatorClass:
930 case CompoundAssignOperatorClass: {
931 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000932
933 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
934 BinOp->getOpcode() == BinaryOperator::Comma)
935 return BinOp->getRHS()->isLvalue(Ctx);
936
Sebastian Redl22460502009-02-07 00:15:38 +0000937 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +0000938 // The result of a .* expression is an lvalue only if its first operand is
939 // an lvalue and its second operand is a pointer to data member.
940 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +0000941 !BinOp->getType()->isFunctionType())
942 return BinOp->getLHS()->isLvalue(Ctx);
943
Fariborz Jahanian27d4be52009-10-08 18:00:39 +0000944 // The result of an ->* expression is an lvalue only if its second operand
945 // is a pointer to data member.
946 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
947 !BinOp->getType()->isFunctionType()) {
948 QualType Ty = BinOp->getRHS()->getType();
949 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
950 return LV_Valid;
951 }
952
Douglas Gregorbf3af052008-11-13 20:12:29 +0000953 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000954 return LV_InvalidExpression;
955
Douglas Gregorbf3af052008-11-13 20:12:29 +0000956 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000957 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +0000958 // The result of an assignment operation [...] is an lvalue.
959 return LV_Valid;
960
961
962 // C99 6.5.16:
963 // An assignment expression [...] is not an lvalue.
964 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000965 }
Mike Stump1eb44332009-09-09 15:08:12 +0000966 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000967 case CXXOperatorCallExprClass:
968 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000969 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +0000970 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000971 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000972 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
973 if (ReturnType->isLValueReferenceType())
974 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000975
Douglas Gregor9d293df2008-10-28 00:22:11 +0000976 break;
977 }
Steve Naroffe6386392007-12-05 04:00:10 +0000978 case CompoundLiteralExprClass: // C99 6.5.2.5p5
979 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000980 case ChooseExprClass:
981 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000982 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000983 case ExtVectorElementExprClass:
984 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000985 return LV_DuplicateVectorComponents;
986 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000987 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
988 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000989 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
990 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000991 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000992 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000993 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000994 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +0000995 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000996 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000997 case CXXConditionDeclExprClass:
998 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000999 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001000 case CXXFunctionalCastExprClass:
1001 case CXXStaticCastExprClass:
1002 case CXXDynamicCastExprClass:
1003 case CXXReinterpretCastExprClass:
1004 case CXXConstCastExprClass:
1005 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001006 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001007 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1008 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001009 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1010 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001011 return LV_Valid;
1012 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001013 case CXXTypeidExprClass:
1014 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1015 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001016 case CXXBindTemporaryExprClass:
1017 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1018 isLvalueInternal(Ctx);
Sebastian Redl76458502009-04-17 16:30:52 +00001019 case ConditionalOperatorClass: {
1020 // Complicated handling is only for C++.
1021 if (!Ctx.getLangOptions().CPlusPlus)
1022 return LV_InvalidExpression;
1023
1024 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1025 // everywhere there's an object converted to an rvalue. Also, any other
1026 // casts should be wrapped by ImplicitCastExprs. There's just the special
1027 // case involving throws to work out.
1028 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001029 Expr *True = Cond->getTrueExpr();
1030 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001031 // C++0x 5.16p2
1032 // If either the second or the third operand has type (cv) void, [...]
1033 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001034 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001035 return LV_InvalidExpression;
1036
1037 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001038 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001039 return LV_InvalidExpression;
1040
1041 // That's it.
1042 return LV_Valid;
1043 }
1044
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 default:
1046 break;
1047 }
1048 return LV_InvalidExpression;
1049}
1050
1051/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1052/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001053/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001054/// recursively, any member or element of all contained aggregates or unions)
1055/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001056Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001057Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001058 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001061 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001062 // C++ 3.10p11: Functions cannot be modified, but pointers to
1063 // functions can be modifiable.
1064 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1065 return MLV_NotObjectType;
1066 break;
1067
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 case LV_NotObjectType: return MLV_NotObjectType;
1069 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001070 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001071 case LV_InvalidExpression:
1072 // If the top level is a C-style cast, and the subexpression is a valid
1073 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1074 // GCC extension. We don't support it, but we want to produce good
1075 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001076 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1077 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1078 if (Loc)
1079 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001080 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001081 }
1082 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001083 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001084 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001086
1087 // The following is illegal:
1088 // void takeclosure(void (^C)(void));
1089 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1090 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001091 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001092 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1093 return MLV_NotBlockQualified;
1094 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001095
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001096 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001097 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001098 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1099 if (Expr->getSetterMethod() == 0)
1100 return MLV_NoSetterProperty;
1101 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001102
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001103 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001105 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001107 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001109 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Ted Kremenek6217b802009-07-29 21:53:49 +00001112 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001113 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 return MLV_ConstQualified;
1115 }
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Mike Stump1eb44332009-09-09 15:08:12 +00001117 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001118}
1119
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001120/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001121/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001122bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001123 switch (getStmtClass()) {
1124 default:
1125 return false;
1126 case ObjCIvarRefExprClass:
1127 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001128 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001129 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001130 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001131 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001132 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001133 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001134 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001135 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001136 case DeclRefExprClass:
1137 case QualifiedDeclRefExprClass: {
1138 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001139 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1140 if (VD->hasGlobalStorage())
1141 return true;
1142 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001143 // dereferencing to a pointer is always a gc'able candidate,
1144 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001145 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001146 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001147 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001148 return false;
1149 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001150 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001151 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001152 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001153 }
1154 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001155 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001156 }
1157}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001158Expr* Expr::IgnoreParens() {
1159 Expr* E = this;
1160 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1161 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001163 return E;
1164}
1165
Chris Lattner56f34942008-02-13 01:02:39 +00001166/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1167/// or CastExprs or ImplicitCastExprs, returning their operand.
1168Expr *Expr::IgnoreParenCasts() {
1169 Expr *E = this;
1170 while (true) {
1171 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1172 E = P->getSubExpr();
1173 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1174 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001175 else
1176 return E;
1177 }
1178}
1179
Chris Lattnerecdd8412009-03-13 17:28:01 +00001180/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1181/// value (including ptr->int casts of the same size). Strip off any
1182/// ParenExpr or CastExprs, returning their operand.
1183Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1184 Expr *E = this;
1185 while (true) {
1186 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1187 E = P->getSubExpr();
1188 continue;
1189 }
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Chris Lattnerecdd8412009-03-13 17:28:01 +00001191 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1192 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1193 // ptr<->int casts of the same width. We also ignore all identify casts.
1194 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Chris Lattnerecdd8412009-03-13 17:28:01 +00001196 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1197 E = SE;
1198 continue;
1199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Chris Lattnerecdd8412009-03-13 17:28:01 +00001201 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1202 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1203 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1204 E = SE;
1205 continue;
1206 }
1207 }
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Chris Lattnerecdd8412009-03-13 17:28:01 +00001209 return E;
1210 }
1211}
1212
1213
Douglas Gregor898574e2008-12-05 23:32:09 +00001214/// hasAnyTypeDependentArguments - Determines if any of the expressions
1215/// in Exprs is type-dependent.
1216bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1217 for (unsigned I = 0; I < NumExprs; ++I)
1218 if (Exprs[I]->isTypeDependent())
1219 return true;
1220
1221 return false;
1222}
1223
1224/// hasAnyValueDependentArguments - Determines if any of the expressions
1225/// in Exprs is value-dependent.
1226bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1227 for (unsigned I = 0; I < NumExprs; ++I)
1228 if (Exprs[I]->isValueDependent())
1229 return true;
1230
1231 return false;
1232}
1233
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001234bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001235 // This function is attempting whether an expression is an initializer
1236 // which can be evaluated at compile-time. isEvaluatable handles most
1237 // of the cases, but it can't deal with some initializer-specific
1238 // expressions, and it can't deal with aggregates; we deal with those here,
1239 // and fall back to isEvaluatable for the other cases.
1240
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001241 // FIXME: This function assumes the variable being assigned to
1242 // isn't a reference type!
1243
Anders Carlssone8a32b82008-11-24 05:23:59 +00001244 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001245 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001246 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001247 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001248 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001249 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001250 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001251 // This handles gcc's extension that allows global initializers like
1252 // "struct x {int x;} x = (struct x) {};".
1253 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001254 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001255 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001256 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001257 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001258 // FIXME: This doesn't deal with fields with reference types correctly.
1259 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1260 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001261 const InitListExpr *Exp = cast<InitListExpr>(this);
1262 unsigned numInits = Exp->getNumInits();
1263 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001264 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001265 return false;
1266 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001267 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001268 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001269 case ImplicitValueInitExprClass:
1270 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001271 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001272 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001273 case UnaryOperatorClass: {
1274 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1275 if (Exp->getOpcode() == UnaryOperator::Extension)
1276 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1277 break;
1278 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001279 case BinaryOperatorClass: {
1280 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1281 // but this handles the common case.
1282 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1283 if (Exp->getOpcode() == BinaryOperator::Sub &&
1284 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1285 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1286 return true;
1287 break;
1288 }
Chris Lattner81045d82009-04-21 05:19:11 +00001289 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001290 case CStyleCastExprClass:
1291 // Handle casts with a destination that's a struct or union; this
1292 // deals with both the gcc no-op struct cast extension and the
1293 // cast-to-union extension.
1294 if (getType()->isRecordType())
1295 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001296
1297 // Integer->integer casts can be handled here, which is important for
1298 // things like (int)(&&x-&&y). Scary but true.
1299 if (getType()->isIntegerType() &&
1300 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1301 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1302
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001303 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001304 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001305 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001306}
1307
Reid Spencer5f016e22007-07-11 17:01:13 +00001308/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001309/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001310
1311/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1312/// comma, etc
1313///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001314/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1315/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1316/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001317
Eli Friedmane28d7192009-02-26 09:29:13 +00001318// CheckICE - This function does the fundamental ICE checking: the returned
1319// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1320// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001321// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001322// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001323// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001324//
1325// Meanings of Val:
1326// 0: This expression is an ICE if it can be evaluated by Evaluate.
1327// 1: This expression is not an ICE, but if it isn't evaluated, it's
1328// a legal subexpression for an ICE. This return value is used to handle
1329// the comma operator in C99 mode.
1330// 2: This expression is not an ICE, and is not a legal subexpression for one.
1331
1332struct ICEDiag {
1333 unsigned Val;
1334 SourceLocation Loc;
1335
1336 public:
1337 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1338 ICEDiag() : Val(0) {}
1339};
1340
1341ICEDiag NoDiag() { return ICEDiag(); }
1342
Eli Friedman60ce9632009-02-27 04:07:58 +00001343static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1344 Expr::EvalResult EVResult;
1345 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1346 !EVResult.Val.isInt()) {
1347 return ICEDiag(2, E->getLocStart());
1348 }
1349 return NoDiag();
1350}
1351
Eli Friedmane28d7192009-02-26 09:29:13 +00001352static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001353 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001354 if (!E->getType()->isIntegralType()) {
1355 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001356 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001357
1358 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001359#define STMT(Node, Base) case Expr::Node##Class:
1360#define EXPR(Node, Base)
1361#include "clang/AST/StmtNodes.def"
1362 case Expr::PredefinedExprClass:
1363 case Expr::FloatingLiteralClass:
1364 case Expr::ImaginaryLiteralClass:
1365 case Expr::StringLiteralClass:
1366 case Expr::ArraySubscriptExprClass:
1367 case Expr::MemberExprClass:
1368 case Expr::CompoundAssignOperatorClass:
1369 case Expr::CompoundLiteralExprClass:
1370 case Expr::ExtVectorElementExprClass:
1371 case Expr::InitListExprClass:
1372 case Expr::DesignatedInitExprClass:
1373 case Expr::ImplicitValueInitExprClass:
1374 case Expr::ParenListExprClass:
1375 case Expr::VAArgExprClass:
1376 case Expr::AddrLabelExprClass:
1377 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001378 case Expr::CXXMemberCallExprClass:
1379 case Expr::CXXDynamicCastExprClass:
1380 case Expr::CXXTypeidExprClass:
1381 case Expr::CXXNullPtrLiteralExprClass:
1382 case Expr::CXXThisExprClass:
1383 case Expr::CXXThrowExprClass:
1384 case Expr::CXXConditionDeclExprClass: // FIXME: is this correct?
1385 case Expr::CXXNewExprClass:
1386 case Expr::CXXDeleteExprClass:
1387 case Expr::CXXPseudoDestructorExprClass:
1388 case Expr::UnresolvedFunctionNameExprClass:
1389 case Expr::UnresolvedDeclRefExprClass:
1390 case Expr::TemplateIdRefExprClass:
1391 case Expr::CXXConstructExprClass:
1392 case Expr::CXXBindTemporaryExprClass:
1393 case Expr::CXXExprWithTemporariesClass:
1394 case Expr::CXXTemporaryObjectExprClass:
1395 case Expr::CXXUnresolvedConstructExprClass:
1396 case Expr::CXXUnresolvedMemberExprClass:
1397 case Expr::ObjCStringLiteralClass:
1398 case Expr::ObjCEncodeExprClass:
1399 case Expr::ObjCMessageExprClass:
1400 case Expr::ObjCSelectorExprClass:
1401 case Expr::ObjCProtocolExprClass:
1402 case Expr::ObjCIvarRefExprClass:
1403 case Expr::ObjCPropertyRefExprClass:
1404 case Expr::ObjCImplicitSetterGetterRefExprClass:
1405 case Expr::ObjCSuperExprClass:
1406 case Expr::ObjCIsaExprClass:
1407 case Expr::ShuffleVectorExprClass:
1408 case Expr::BlockExprClass:
1409 case Expr::BlockDeclRefExprClass:
1410 case Expr::NoStmtClass:
1411 case Expr::ExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001412 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001413
Douglas Gregor043cad22009-09-11 00:18:58 +00001414 case Expr::GNUNullExprClass:
1415 // GCC considers the GNU __null value to be an integral constant expression.
1416 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001417
Eli Friedmane28d7192009-02-26 09:29:13 +00001418 case Expr::ParenExprClass:
1419 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1420 case Expr::IntegerLiteralClass:
1421 case Expr::CharacterLiteralClass:
1422 case Expr::CXXBoolLiteralExprClass:
1423 case Expr::CXXZeroInitValueExprClass:
1424 case Expr::TypesCompatibleExprClass:
1425 case Expr::UnaryTypeTraitExprClass:
1426 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001427 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001428 case Expr::CXXOperatorCallExprClass: {
1429 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001430 if (CE->isBuiltinCall(Ctx))
1431 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001432 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001433 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001434 case Expr::DeclRefExprClass:
1435 case Expr::QualifiedDeclRefExprClass:
1436 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1437 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001438 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001439 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001440 // C++ 7.1.5.1p2
1441 // A variable of non-volatile const-qualified integral or enumeration
1442 // type initialized by an ICE can be used in ICEs.
1443 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001444 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +00001445 if (Dcl->isInitKnownICE()) {
1446 // We have already checked whether this subexpression is an
1447 // integral constant expression.
1448 if (Dcl->isInitICE())
1449 return NoDiag();
1450 else
1451 return ICEDiag(2, E->getLocStart());
1452 }
1453
1454 if (const Expr *Init = Dcl->getInit()) {
1455 ICEDiag Result = CheckICE(Init, Ctx);
1456 // Cache the result of the ICE test.
1457 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1458 return Result;
1459 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001460 }
1461 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001462 return ICEDiag(2, E->getLocStart());
1463 case Expr::UnaryOperatorClass: {
1464 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001465 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001466 case UnaryOperator::PostInc:
1467 case UnaryOperator::PostDec:
1468 case UnaryOperator::PreInc:
1469 case UnaryOperator::PreDec:
1470 case UnaryOperator::AddrOf:
1471 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001472 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001473
Reid Spencer5f016e22007-07-11 17:01:13 +00001474 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001475 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001478 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001479 case UnaryOperator::Real:
1480 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001481 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001482 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001483 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1484 // Evaluate matches the proposed gcc behavior for cases like
1485 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1486 // compliance: we should warn earlier for offsetof expressions with
1487 // array subscripts that aren't ICEs, and if the array subscripts
1488 // are ICEs, the value of the offsetof must be an integer constant.
1489 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001490 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001492 case Expr::SizeOfAlignOfExprClass: {
1493 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1494 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1495 return ICEDiag(2, E->getLocStart());
1496 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001497 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001498 case Expr::BinaryOperatorClass: {
1499 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001501 case BinaryOperator::PtrMemD:
1502 case BinaryOperator::PtrMemI:
1503 case BinaryOperator::Assign:
1504 case BinaryOperator::MulAssign:
1505 case BinaryOperator::DivAssign:
1506 case BinaryOperator::RemAssign:
1507 case BinaryOperator::AddAssign:
1508 case BinaryOperator::SubAssign:
1509 case BinaryOperator::ShlAssign:
1510 case BinaryOperator::ShrAssign:
1511 case BinaryOperator::AndAssign:
1512 case BinaryOperator::XorAssign:
1513 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001514 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001515
Reid Spencer5f016e22007-07-11 17:01:13 +00001516 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001517 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001518 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001519 case BinaryOperator::Add:
1520 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001522 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001523 case BinaryOperator::LT:
1524 case BinaryOperator::GT:
1525 case BinaryOperator::LE:
1526 case BinaryOperator::GE:
1527 case BinaryOperator::EQ:
1528 case BinaryOperator::NE:
1529 case BinaryOperator::And:
1530 case BinaryOperator::Xor:
1531 case BinaryOperator::Or:
1532 case BinaryOperator::Comma: {
1533 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1534 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001535 if (Exp->getOpcode() == BinaryOperator::Div ||
1536 Exp->getOpcode() == BinaryOperator::Rem) {
1537 // Evaluate gives an error for undefined Div/Rem, so make sure
1538 // we don't evaluate one.
1539 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1540 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1541 if (REval == 0)
1542 return ICEDiag(1, E->getLocStart());
1543 if (REval.isSigned() && REval.isAllOnesValue()) {
1544 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1545 if (LEval.isMinSignedValue())
1546 return ICEDiag(1, E->getLocStart());
1547 }
1548 }
1549 }
1550 if (Exp->getOpcode() == BinaryOperator::Comma) {
1551 if (Ctx.getLangOptions().C99) {
1552 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1553 // if it isn't evaluated.
1554 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1555 return ICEDiag(1, E->getLocStart());
1556 } else {
1557 // In both C89 and C++, commas in ICEs are illegal.
1558 return ICEDiag(2, E->getLocStart());
1559 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001560 }
1561 if (LHSResult.Val >= RHSResult.Val)
1562 return LHSResult;
1563 return RHSResult;
1564 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001566 case BinaryOperator::LOr: {
1567 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1568 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1569 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1570 // Rare case where the RHS has a comma "side-effect"; we need
1571 // to actually check the condition to see whether the side
1572 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001573 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001574 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001575 return RHSResult;
1576 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001577 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001578
Eli Friedmane28d7192009-02-26 09:29:13 +00001579 if (LHSResult.Val >= RHSResult.Val)
1580 return LHSResult;
1581 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001583 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001584 }
Douglas Gregorf2991242009-09-10 23:31:45 +00001585 case Expr::CastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001586 case Expr::ImplicitCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001587 case Expr::ExplicitCastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001588 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001589 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001590 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001591 case Expr::CXXStaticCastExprClass:
1592 case Expr::CXXReinterpretCastExprClass:
1593 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00001594 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1595 if (SubExpr->getType()->isIntegralType())
1596 return CheckICE(SubExpr, Ctx);
1597 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1598 return NoDiag();
1599 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001600 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001601 case Expr::ConditionalOperatorClass: {
1602 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001603 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00001604 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001605 // expression, and it is fully evaluated. This is an important GNU
1606 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001607 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001608 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001609 Expr::EvalResult EVResult;
1610 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1611 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001612 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001613 }
1614 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001615 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001616 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1617 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1618 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1619 if (CondResult.Val == 2)
1620 return CondResult;
1621 if (TrueResult.Val == 2)
1622 return TrueResult;
1623 if (FalseResult.Val == 2)
1624 return FalseResult;
1625 if (CondResult.Val == 1)
1626 return CondResult;
1627 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1628 return NoDiag();
1629 // Rare case where the diagnostics depend on which side is evaluated
1630 // Note that if we get here, CondResult is 0, and at least one of
1631 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001632 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001633 return FalseResult;
1634 }
1635 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001636 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001637 case Expr::CXXDefaultArgExprClass:
1638 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001639 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001640 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001641 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001642 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001643
Douglas Gregorf2991242009-09-10 23:31:45 +00001644 // Silence a GCC warning
1645 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001646}
Reid Spencer5f016e22007-07-11 17:01:13 +00001647
Eli Friedmane28d7192009-02-26 09:29:13 +00001648bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1649 SourceLocation *Loc, bool isEvaluated) const {
1650 ICEDiag d = CheckICE(this, Ctx);
1651 if (d.Val != 0) {
1652 if (Loc) *Loc = d.Loc;
1653 return false;
1654 }
1655 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001656 if (!Evaluate(EvalResult, Ctx))
1657 assert(0 && "ICE cannot be evaluated!");
1658 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1659 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001660 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001661 return true;
1662}
1663
Reid Spencer5f016e22007-07-11 17:01:13 +00001664/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1665/// integer constant expression with the value zero, or if this is one that is
1666/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001667bool Expr::isNullPointerConstant(ASTContext &Ctx,
1668 NullPointerConstantValueDependence NPC) const {
1669 if (isValueDependent()) {
1670 switch (NPC) {
1671 case NPC_NeverValueDependent:
1672 assert(false && "Unexpected value dependent expression!");
1673 // If the unthinkable happens, fall through to the safest alternative.
1674
1675 case NPC_ValueDependentIsNull:
1676 return isTypeDependent() || getType()->isIntegralType();
1677
1678 case NPC_ValueDependentIsNotNull:
1679 return false;
1680 }
1681 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001682
Sebastian Redl07779722008-10-31 14:43:28 +00001683 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001684 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001685 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001686 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001687 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001688 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001689 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001690 Pointee->isVoidType() && // to void*
1691 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001692 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001693 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001694 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001695 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1696 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001697 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00001698 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1699 // Accept ((void*)0) as a null pointer constant, as many other
1700 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00001701 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001702 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001703 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001704 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001705 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001706 } else if (isa<GNUNullExpr>(this)) {
1707 // The GNU __null extension is always a null pointer constant.
1708 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001709 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001710
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001711 // C++0x nullptr_t is always a null pointer constant.
1712 if (getType()->isNullPtrType())
1713 return true;
1714
Steve Naroffaa58f002008-01-14 16:10:57 +00001715 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00001716 if (!getType()->isIntegerType() ||
1717 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00001718 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001719
Reid Spencer5f016e22007-07-11 17:01:13 +00001720 // If we have an integer constant expression, we need to *evaluate* it and
1721 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001722 llvm::APSInt Result;
1723 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001724}
Steve Naroff31a45842007-07-28 23:10:27 +00001725
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001726FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001727 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001728
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001729 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001730 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001731 if (Field->isBitField())
1732 return Field;
1733
1734 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1735 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1736 return BinOp->getLHS()->getBitField();
1737
1738 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001739}
1740
Chris Lattner2140e902009-02-16 22:14:05 +00001741/// isArrow - Return true if the base expression is a pointer to vector,
1742/// return false if the base expression is a vector.
1743bool ExtVectorElementExpr::isArrow() const {
1744 return getBase()->getType()->isPointerType();
1745}
1746
Nate Begeman213541a2008-04-18 23:10:10 +00001747unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00001748 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00001749 return VT->getNumElements();
1750 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001751}
1752
Nate Begeman8a997642008-05-09 06:41:27 +00001753/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001754bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00001755 // FIXME: Refactor this code to an accessor on the AST node which returns the
1756 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar15027422009-10-17 23:53:04 +00001757 llvm::StringRef Comp = Accessor->getNameStr();
Nate Begeman190d6a22009-01-18 02:01:21 +00001758
1759 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00001760 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00001761 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Nate Begeman190d6a22009-01-18 02:01:21 +00001763 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00001764 if (Comp[0] == 's' || Comp[0] == 'S')
1765 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Daniel Dunbar15027422009-10-17 23:53:04 +00001767 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1768 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00001769 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00001770
Steve Narofffec0b492007-07-30 03:29:09 +00001771 return false;
1772}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001773
Nate Begeman8a997642008-05-09 06:41:27 +00001774/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001775void ExtVectorElementExpr::getEncodedElementAccess(
1776 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001777 llvm::StringRef Comp = Accessor->getName();
1778 if (Comp[0] == 's' || Comp[0] == 'S')
1779 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001781 bool isHi = Comp == "hi";
1782 bool isLo = Comp == "lo";
1783 bool isEven = Comp == "even";
1784 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Nate Begeman8a997642008-05-09 06:41:27 +00001786 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1787 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Nate Begeman8a997642008-05-09 06:41:27 +00001789 if (isHi)
1790 Index = e + i;
1791 else if (isLo)
1792 Index = i;
1793 else if (isEven)
1794 Index = 2 * i;
1795 else if (isOdd)
1796 Index = 2 * i + 1;
1797 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001798 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001799
Nate Begeman3b8d1162008-05-13 21:03:02 +00001800 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001801 }
Nate Begeman8a997642008-05-09 06:41:27 +00001802}
1803
Steve Naroff68d331a2007-09-27 14:38:14 +00001804// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001805ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001806 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001807 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001808 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00001809 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001810 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001811 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001812 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001813 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001814 if (NumArgs) {
1815 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001816 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1817 }
Steve Naroff563477d2007-09-18 23:55:05 +00001818 LBracloc = LBrac;
1819 RBracloc = RBrac;
1820}
1821
Mike Stump1eb44332009-09-09 15:08:12 +00001822// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00001823// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001824ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001825 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001826 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001827 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00001828 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001829 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001830 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001831 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001832 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001833 if (NumArgs) {
1834 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001835 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1836 }
Steve Naroff563477d2007-09-18 23:55:05 +00001837 LBracloc = LBrac;
1838 RBracloc = RBrac;
1839}
1840
Mike Stump1eb44332009-09-09 15:08:12 +00001841// constructor for class messages.
Ted Kremenek4df728e2008-06-24 15:50:53 +00001842ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1843 QualType retType, ObjCMethodDecl *mproto,
1844 SourceLocation LBrac, SourceLocation RBrac,
1845 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00001846: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenek4df728e2008-06-24 15:50:53 +00001847MethodProto(mproto) {
1848 NumArgs = nargs;
1849 SubExprs = new Stmt*[NumArgs+1];
1850 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1851 if (NumArgs) {
1852 for (unsigned i = 0; i != NumArgs; ++i)
1853 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1854 }
1855 LBracloc = LBrac;
1856 RBracloc = RBrac;
1857}
1858
1859ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1860 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1861 switch (x & Flags) {
1862 default:
1863 assert(false && "Invalid ObjCMessageExpr.");
1864 case IsInstMeth:
1865 return ClassInfo(0, 0);
1866 case IsClsMethDeclUnknown:
1867 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1868 case IsClsMethDeclKnown: {
1869 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1870 return ClassInfo(D, D->getIdentifier());
1871 }
1872 }
1873}
1874
Chris Lattner0389e6b2009-04-26 00:44:05 +00001875void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1876 if (CI.first == 0 && CI.second == 0)
1877 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1878 else if (CI.first == 0)
1879 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1880 else
1881 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1882}
1883
1884
Chris Lattner27437ca2007-10-25 00:29:32 +00001885bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00001886 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001887}
1888
Nate Begeman888376a2009-08-12 02:28:50 +00001889void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1890 unsigned NumExprs) {
1891 if (SubExprs) C.Deallocate(SubExprs);
1892
1893 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001894 this->NumExprs = NumExprs;
1895 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00001896}
Nate Begeman888376a2009-08-12 02:28:50 +00001897
1898void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
1899 DestroyChildren(C);
1900 if (SubExprs) C.Deallocate(SubExprs);
1901 this->~ShuffleVectorExpr();
1902 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001903}
1904
Douglas Gregor42602bb2009-08-07 06:08:38 +00001905void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00001906 // Override default behavior of traversing children. If this has a type
1907 // operand and the type is a variable-length array, the child iteration
1908 // will iterate over the size expression. However, this expression belongs
1909 // to the type, not to this, so we don't want to delete it.
1910 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001911 if (isArgumentType()) {
1912 this->~SizeOfAlignOfExpr();
1913 C.Deallocate(this);
1914 }
Sebastian Redl05189992008-11-11 17:56:53 +00001915 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00001916 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001917}
1918
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001919//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001920// DesignatedInitExpr
1921//===----------------------------------------------------------------------===//
1922
1923IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1924 assert(Kind == FieldDesignator && "Only valid on a field designator");
1925 if (Field.NameOrField & 0x01)
1926 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1927 else
1928 return getField()->getIdentifier();
1929}
1930
Mike Stump1eb44332009-09-09 15:08:12 +00001931DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001932 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00001933 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001934 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00001935 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00001936 unsigned NumIndexExprs,
1937 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00001938 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00001939 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00001940 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1941 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001942 this->Designators = new Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001943
1944 // Record the initializer itself.
1945 child_iterator Child = child_begin();
1946 *Child++ = Init;
1947
1948 // Copy the designators and their subexpressions, computing
1949 // value-dependence along the way.
1950 unsigned IndexIdx = 0;
1951 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001952 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001953
1954 if (this->Designators[I].isArrayDesignator()) {
1955 // Compute type- and value-dependence.
1956 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00001957 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00001958 Index->isTypeDependent() || Index->isValueDependent();
1959
1960 // Copy the index expressions into permanent storage.
1961 *Child++ = IndexExprs[IndexIdx++];
1962 } else if (this->Designators[I].isArrayRangeDesignator()) {
1963 // Compute type- and value-dependence.
1964 Expr *Start = IndexExprs[IndexIdx];
1965 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00001966 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00001967 Start->isTypeDependent() || Start->isValueDependent() ||
1968 End->isTypeDependent() || End->isValueDependent();
1969
1970 // Copy the start/end expressions into permanent storage.
1971 *Child++ = IndexExprs[IndexIdx++];
1972 *Child++ = IndexExprs[IndexIdx++];
1973 }
1974 }
1975
1976 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001977}
1978
Douglas Gregor05c13a32009-01-22 00:58:24 +00001979DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00001980DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00001981 unsigned NumDesignators,
1982 Expr **IndexExprs, unsigned NumIndexExprs,
1983 SourceLocation ColonOrEqualLoc,
1984 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001985 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001986 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor9ea62762009-05-21 23:17:49 +00001987 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1988 ColonOrEqualLoc, UsesColonSyntax,
1989 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001990}
1991
Mike Stump1eb44332009-09-09 15:08:12 +00001992DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00001993 unsigned NumIndexExprs) {
1994 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1995 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1996 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1997}
1998
Mike Stump1eb44332009-09-09 15:08:12 +00001999void DesignatedInitExpr::setDesignators(const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002000 unsigned NumDesigs) {
2001 if (Designators)
2002 delete [] Designators;
2003
2004 Designators = new Designator[NumDesigs];
2005 NumDesignators = NumDesigs;
2006 for (unsigned I = 0; I != NumDesigs; ++I)
2007 Designators[I] = Desigs[I];
2008}
2009
Douglas Gregor05c13a32009-01-22 00:58:24 +00002010SourceRange DesignatedInitExpr::getSourceRange() const {
2011 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002012 Designator &First =
2013 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002014 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002015 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002016 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2017 else
2018 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2019 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002020 StartLoc =
2021 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002022 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2023}
2024
Douglas Gregor05c13a32009-01-22 00:58:24 +00002025Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2026 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2027 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2028 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002029 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2030 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2031}
2032
2033Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002034 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002035 "Requires array range designator");
2036 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2037 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002038 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2039 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2040}
2041
2042Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002043 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002044 "Requires array range designator");
2045 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2046 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002047 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2048 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2049}
2050
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002051/// \brief Replaces the designator at index @p Idx with the series
2052/// of designators in [First, Last).
Mike Stump1eb44332009-09-09 15:08:12 +00002053void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
2054 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002055 const Designator *Last) {
2056 unsigned NumNewDesignators = Last - First;
2057 if (NumNewDesignators == 0) {
2058 std::copy_backward(Designators + Idx + 1,
2059 Designators + NumDesignators,
2060 Designators + Idx);
2061 --NumNewDesignators;
2062 return;
2063 } else if (NumNewDesignators == 1) {
2064 Designators[Idx] = *First;
2065 return;
2066 }
2067
Mike Stump1eb44332009-09-09 15:08:12 +00002068 Designator *NewDesignators
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002069 = new Designator[NumDesignators - 1 + NumNewDesignators];
2070 std::copy(Designators, Designators + Idx, NewDesignators);
2071 std::copy(First, Last, NewDesignators + Idx);
2072 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2073 NewDesignators + Idx + NumNewDesignators);
2074 delete [] Designators;
2075 Designators = NewDesignators;
2076 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2077}
2078
Douglas Gregor42602bb2009-08-07 06:08:38 +00002079void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002080 delete [] Designators;
Douglas Gregor42602bb2009-08-07 06:08:38 +00002081 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002082}
2083
Mike Stump1eb44332009-09-09 15:08:12 +00002084ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002085 Expr **exprs, unsigned nexprs,
2086 SourceLocation rparenloc)
2087: Expr(ParenListExprClass, QualType(),
2088 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002089 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002090 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002091
Nate Begeman2ef13e52009-08-10 23:49:36 +00002092 Exprs = new (C) Stmt*[nexprs];
2093 for (unsigned i = 0; i != nexprs; ++i)
2094 Exprs[i] = exprs[i];
2095}
2096
2097void ParenListExpr::DoDestroy(ASTContext& C) {
2098 DestroyChildren(C);
2099 if (Exprs) C.Deallocate(Exprs);
2100 this->~ParenListExpr();
2101 C.Deallocate(this);
2102}
2103
Douglas Gregor05c13a32009-01-22 00:58:24 +00002104//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002105// ExprIterator.
2106//===----------------------------------------------------------------------===//
2107
2108Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2109Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2110Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2111const Expr* ConstExprIterator::operator[](size_t idx) const {
2112 return cast<Expr>(I[idx]);
2113}
2114const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2115const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2116
2117//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002118// Child Iterators for iterating over subexpressions/substatements
2119//===----------------------------------------------------------------------===//
2120
2121// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002122Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2123Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002124
Steve Naroff7779db42007-11-12 14:29:37 +00002125// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002126Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2127Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002128
Steve Naroffe3e9add2008-06-02 23:03:37 +00002129// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002130Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2131Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002132
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002133// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002134Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2135 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002136}
Mike Stump1eb44332009-09-09 15:08:12 +00002137Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2138 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002139}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002140
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002141// ObjCSuperExpr
2142Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2143Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2144
Steve Narofff242b1b2009-07-24 17:54:45 +00002145// ObjCIsaExpr
2146Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2147Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2148
Chris Lattnerd9f69102008-08-10 01:53:14 +00002149// PredefinedExpr
2150Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2151Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002152
2153// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002154Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2155Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002156
2157// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002158Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002159Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002160
2161// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002162Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2163Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002164
Chris Lattner5d661452007-08-26 03:42:43 +00002165// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002166Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2167Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002168
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002169// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002170Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2171Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002172
2173// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002174Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2175Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002176
2177// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002178Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2179Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002180
Sebastian Redl05189992008-11-11 17:56:53 +00002181// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002182Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002183 // If this is of a type and the type is a VLA type (and not a typedef), the
2184 // size expression of the VLA needs to be treated as an executable expression.
2185 // Why isn't this weirdness documented better in StmtIterator?
2186 if (isArgumentType()) {
2187 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2188 getArgumentType().getTypePtr()))
2189 return child_iterator(T);
2190 return child_iterator();
2191 }
Sebastian Redld4575892008-12-03 23:17:54 +00002192 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002193}
Sebastian Redl05189992008-11-11 17:56:53 +00002194Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2195 if (isArgumentType())
2196 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002197 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002198}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002199
2200// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002201Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002202 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002203}
Ted Kremenek1237c672007-08-24 20:06:47 +00002204Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002205 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002206}
2207
2208// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002209Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002210 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002211}
Ted Kremenek1237c672007-08-24 20:06:47 +00002212Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002213 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002214}
Ted Kremenek1237c672007-08-24 20:06:47 +00002215
2216// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002217Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2218Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002219
Nate Begeman213541a2008-04-18 23:10:10 +00002220// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002221Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2222Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002223
2224// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002225Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2226Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002227
Ted Kremenek1237c672007-08-24 20:06:47 +00002228// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002229Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2230Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002231
2232// BinaryOperator
2233Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002234 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002235}
Ted Kremenek1237c672007-08-24 20:06:47 +00002236Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002237 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002238}
2239
2240// ConditionalOperator
2241Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002242 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002243}
Ted Kremenek1237c672007-08-24 20:06:47 +00002244Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002245 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002246}
2247
2248// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002249Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2250Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002251
Ted Kremenek1237c672007-08-24 20:06:47 +00002252// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002253Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2254Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002255
2256// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002257Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2258 return child_iterator();
2259}
2260
2261Stmt::child_iterator TypesCompatibleExpr::child_end() {
2262 return child_iterator();
2263}
Ted Kremenek1237c672007-08-24 20:06:47 +00002264
2265// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002266Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2267Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002268
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002269// GNUNullExpr
2270Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2271Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2272
Eli Friedmand38617c2008-05-14 19:38:39 +00002273// ShuffleVectorExpr
2274Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002275 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002276}
2277Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002278 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002279}
2280
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002281// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002282Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2283Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002284
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002285// InitListExpr
2286Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002287 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002288}
2289Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002290 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002291}
2292
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002293// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002294Stmt::child_iterator DesignatedInitExpr::child_begin() {
2295 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2296 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002297 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2298}
2299Stmt::child_iterator DesignatedInitExpr::child_end() {
2300 return child_iterator(&*child_begin() + NumSubExprs);
2301}
2302
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002303// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002304Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2305 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002306}
2307
Mike Stump1eb44332009-09-09 15:08:12 +00002308Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2309 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002310}
2311
Nate Begeman2ef13e52009-08-10 23:49:36 +00002312// ParenListExpr
2313Stmt::child_iterator ParenListExpr::child_begin() {
2314 return &Exprs[0];
2315}
2316Stmt::child_iterator ParenListExpr::child_end() {
2317 return &Exprs[0]+NumExprs;
2318}
2319
Ted Kremenek1237c672007-08-24 20:06:47 +00002320// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002321Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002322 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002323}
2324Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002325 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002326}
Ted Kremenek1237c672007-08-24 20:06:47 +00002327
2328// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002329Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2330Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002331
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002332// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002333Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002334 return child_iterator();
2335}
2336Stmt::child_iterator ObjCSelectorExpr::child_end() {
2337 return child_iterator();
2338}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002339
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002340// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002341Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2342 return child_iterator();
2343}
2344Stmt::child_iterator ObjCProtocolExpr::child_end() {
2345 return child_iterator();
2346}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002347
Steve Naroff563477d2007-09-18 23:55:05 +00002348// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002349Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002350 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002351}
2352Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002353 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002354}
2355
Steve Naroff4eb206b2008-09-03 18:15:37 +00002356// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002357Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2358Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002359
Ted Kremenek9da13f92008-09-26 23:24:14 +00002360Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2361Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }