blob: 47df95dc71beecba51ea8ebb5a90c41f634c29ee [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
Douglas Gregora2813ce2009-10-23 18:54:35 +000033DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
34 SourceRange QualifierRange,
35 NamedDecl *D, SourceLocation NameLoc,
36 bool HasExplicitTemplateArgumentList,
37 SourceLocation LAngleLoc,
38 const TemplateArgument *ExplicitTemplateArgs,
39 unsigned NumExplicitTemplateArgs,
40 SourceLocation RAngleLoc,
41 QualType T, bool TD, bool VD)
42 : Expr(DeclRefExprClass, T, TD, VD),
43 DecoratedD(D,
44 (Qualifier? HasQualifierFlag : 0) |
45 (HasExplicitTemplateArgumentList?
46 HasExplicitTemplateArgumentListFlag : 0)),
47 Loc(NameLoc) {
48 if (Qualifier) {
49 NameQualifier *NQ = getNameQualifier();
50 NQ->NNS = Qualifier;
51 NQ->Range = QualifierRange;
52 }
53
54 if (HasExplicitTemplateArgumentList) {
55 ExplicitTemplateArgumentList *ETemplateArgs
56 = getExplicitTemplateArgumentList();
57 ETemplateArgs->LAngleLoc = LAngleLoc;
58 ETemplateArgs->RAngleLoc = RAngleLoc;
59 ETemplateArgs->NumTemplateArgs = NumExplicitTemplateArgs;
60
61 TemplateArgument *TemplateArgs = ETemplateArgs->getTemplateArgs();
62 for (unsigned I = 0; I < NumExplicitTemplateArgs; ++I)
63 new (TemplateArgs + I) TemplateArgument(ExplicitTemplateArgs[I]);
64 }
65}
66
67DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
68 NestedNameSpecifier *Qualifier,
69 SourceRange QualifierRange,
70 NamedDecl *D,
71 SourceLocation NameLoc,
72 QualType T, bool TD, bool VD) {
73 return Create(Context, Qualifier, QualifierRange, D, NameLoc,
74 false, SourceLocation(), 0, 0, SourceLocation(),
75 T, TD, VD);
76}
77
78DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
79 NestedNameSpecifier *Qualifier,
80 SourceRange QualifierRange,
81 NamedDecl *D,
82 SourceLocation NameLoc,
83 bool HasExplicitTemplateArgumentList,
84 SourceLocation LAngleLoc,
85 const TemplateArgument *ExplicitTemplateArgs,
86 unsigned NumExplicitTemplateArgs,
87 SourceLocation RAngleLoc,
88 QualType T, bool TD, bool VD) {
89 std::size_t Size = sizeof(DeclRefExpr);
90 if (Qualifier != 0)
91 Size += sizeof(NameQualifier);
92
93 if (HasExplicitTemplateArgumentList)
94 Size += sizeof(ExplicitTemplateArgumentList) +
95 sizeof(TemplateArgument) * NumExplicitTemplateArgs;
96
97 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
98 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
99 HasExplicitTemplateArgumentList,
100 LAngleLoc,
101 ExplicitTemplateArgs,
102 NumExplicitTemplateArgs,
103 RAngleLoc,
104 T, TD, VD);
105}
106
107SourceRange DeclRefExpr::getSourceRange() const {
108 // FIXME: Does not handle multi-token names well, e.g., operator[].
109 SourceRange R(Loc);
110
111 if (hasQualifier())
112 R.setBegin(getQualifierRange().getBegin());
113 if (hasExplicitTemplateArgumentList())
114 R.setEnd(getRAngleLoc());
115 return R;
116}
117
Anders Carlsson3a082d82009-09-08 18:24:21 +0000118// FIXME: Maybe this should use DeclPrinter with a special "print predefined
119// expr" policy instead.
120std::string PredefinedExpr::ComputeName(ASTContext &Context, IdentType IT,
121 const Decl *CurrentDecl) {
122 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
123 if (IT != PrettyFunction)
124 return FD->getNameAsString();
125
126 llvm::SmallString<256> Name;
127 llvm::raw_svector_ostream Out(Name);
128
129 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
130 if (MD->isVirtual())
131 Out << "virtual ";
132 }
133
134 PrintingPolicy Policy(Context.getLangOptions());
135 Policy.SuppressTagKind = true;
136
137 std::string Proto = FD->getQualifiedNameAsString(Policy);
138
John McCall183700f2009-09-21 23:43:11 +0000139 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000140 const FunctionProtoType *FT = 0;
141 if (FD->hasWrittenPrototype())
142 FT = dyn_cast<FunctionProtoType>(AFT);
143
144 Proto += "(";
145 if (FT) {
146 llvm::raw_string_ostream POut(Proto);
147 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
148 if (i) POut << ", ";
149 std::string Param;
150 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
151 POut << Param;
152 }
153
154 if (FT->isVariadic()) {
155 if (FD->getNumParams()) POut << ", ";
156 POut << "...";
157 }
158 }
159 Proto += ")";
160
161 AFT->getResultType().getAsStringInternal(Proto, Policy);
162
163 Out << Proto;
164
165 Out.flush();
166 return Name.str().str();
167 }
168 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
169 llvm::SmallString<256> Name;
170 llvm::raw_svector_ostream Out(Name);
171 Out << (MD->isInstanceMethod() ? '-' : '+');
172 Out << '[';
173 Out << MD->getClassInterface()->getNameAsString();
174 if (const ObjCCategoryImplDecl *CID =
175 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
176 Out << '(';
177 Out << CID->getNameAsString();
178 Out << ')';
179 }
180 Out << ' ';
181 Out << MD->getSelector().getAsString();
182 Out << ']';
183
184 Out.flush();
185 return Name.str().str();
186 }
187 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
188 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
189 return "top level";
190 }
191 return "";
192}
193
Chris Lattnerda8249e2008-06-07 22:13:43 +0000194/// getValueAsApproximateDouble - This returns the value as an inaccurate
195/// double. Note that this may cause loss of precision, but is useful for
196/// debugging dumps, etc.
197double FloatingLiteral::getValueAsApproximateDouble() const {
198 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000199 bool ignored;
200 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
201 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000202 return V.convertToDouble();
203}
204
Chris Lattner2085fd62009-02-18 06:40:38 +0000205StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
206 unsigned ByteLength, bool Wide,
207 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000208 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000209 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000210 // Allocate enough space for the StringLiteral plus an array of locations for
211 // any concatenated string tokens.
212 void *Mem = C.Allocate(sizeof(StringLiteral)+
213 sizeof(SourceLocation)*(NumStrs-1),
214 llvm::alignof<StringLiteral>());
215 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000218 char *AStrData = new (C, 1) char[ByteLength];
219 memcpy(AStrData, StrData, ByteLength);
220 SL->StrData = AStrData;
221 SL->ByteLength = ByteLength;
222 SL->IsWide = Wide;
223 SL->TokLocs[0] = Loc[0];
224 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000225
Chris Lattner726e1682009-02-18 05:49:11 +0000226 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000227 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
228 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000229}
230
Douglas Gregor673ecd62009-04-15 16:35:07 +0000231StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
232 void *Mem = C.Allocate(sizeof(StringLiteral)+
233 sizeof(SourceLocation)*(NumStrs-1),
234 llvm::alignof<StringLiteral>());
235 StringLiteral *SL = new (Mem) StringLiteral(QualType());
236 SL->StrData = 0;
237 SL->ByteLength = 0;
238 SL->NumConcatenated = NumStrs;
239 return SL;
240}
241
Douglas Gregor42602bb2009-08-07 06:08:38 +0000242void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000243 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000244 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000245}
246
Daniel Dunbarb6480232009-09-22 03:27:33 +0000247void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000248 if (StrData)
249 C.Deallocate(const_cast<char*>(StrData));
250
Daniel Dunbarb6480232009-09-22 03:27:33 +0000251 char *AStrData = new (C, 1) char[Str.size()];
252 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000253 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000254 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000255}
256
Reid Spencer5f016e22007-07-11 17:01:13 +0000257/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
258/// corresponds to, e.g. "sizeof" or "[pre]++".
259const char *UnaryOperator::getOpcodeStr(Opcode Op) {
260 switch (Op) {
261 default: assert(0 && "Unknown unary operator");
262 case PostInc: return "++";
263 case PostDec: return "--";
264 case PreInc: return "++";
265 case PreDec: return "--";
266 case AddrOf: return "&";
267 case Deref: return "*";
268 case Plus: return "+";
269 case Minus: return "-";
270 case Not: return "~";
271 case LNot: return "!";
272 case Real: return "__real";
273 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000275 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 }
277}
278
Mike Stump1eb44332009-09-09 15:08:12 +0000279UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000280UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
281 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000282 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000283 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
284 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
285 case OO_Amp: return AddrOf;
286 case OO_Star: return Deref;
287 case OO_Plus: return Plus;
288 case OO_Minus: return Minus;
289 case OO_Tilde: return Not;
290 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000291 }
292}
293
294OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
295 switch (Opc) {
296 case PostInc: case PreInc: return OO_PlusPlus;
297 case PostDec: case PreDec: return OO_MinusMinus;
298 case AddrOf: return OO_Amp;
299 case Deref: return OO_Star;
300 case Plus: return OO_Plus;
301 case Minus: return OO_Minus;
302 case Not: return OO_Tilde;
303 case LNot: return OO_Exclaim;
304 default: return OO_None;
305 }
306}
307
308
Reid Spencer5f016e22007-07-11 17:01:13 +0000309//===----------------------------------------------------------------------===//
310// Postfix Operators.
311//===----------------------------------------------------------------------===//
312
Ted Kremenek668bf912009-02-09 20:51:47 +0000313CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000314 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000315 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000316 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000317 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000318 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenek668bf912009-02-09 20:51:47 +0000320 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000321 SubExprs[FN] = fn;
322 for (unsigned i = 0; i != numargs; ++i)
323 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000324
Douglas Gregorb4609802008-11-14 16:09:21 +0000325 RParenLoc = rparenloc;
326}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000327
Ted Kremenek668bf912009-02-09 20:51:47 +0000328CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
329 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000330 : Expr(CallExprClass, t,
331 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000332 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000333 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000334
335 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000336 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000338 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000339
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 RParenLoc = rparenloc;
341}
342
Mike Stump1eb44332009-09-09 15:08:12 +0000343CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
344 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000345 SubExprs = new (C) Stmt*[1];
346}
347
Douglas Gregor42602bb2009-08-07 06:08:38 +0000348void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000349 DestroyChildren(C);
350 if (SubExprs) C.Deallocate(SubExprs);
351 this->~CallExpr();
352 C.Deallocate(this);
353}
354
Zhongxing Xua0042542009-07-17 07:29:51 +0000355FunctionDecl *CallExpr::getDirectCallee() {
356 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000357 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xua0042542009-07-17 07:29:51 +0000358 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xua0042542009-07-17 07:29:51 +0000359
360 return 0;
361}
362
Chris Lattnerd18b3292007-12-28 05:25:02 +0000363/// setNumArgs - This changes the number of arguments present in this call.
364/// Any orphaned expressions are deleted by this, and any new operands are set
365/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000366void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000367 // No change, just return.
368 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattnerd18b3292007-12-28 05:25:02 +0000370 // If shrinking # arguments, just delete the extras and forgot them.
371 if (NumArgs < getNumArgs()) {
372 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000373 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000374 this->NumArgs = NumArgs;
375 return;
376 }
377
378 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000379 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000380 // Copy over args.
381 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
382 NewSubExprs[i] = SubExprs[i];
383 // Null out new args.
384 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
385 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Douglas Gregor88c9a462009-04-17 21:46:47 +0000387 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000388 SubExprs = NewSubExprs;
389 this->NumArgs = NumArgs;
390}
391
Chris Lattnercb888962008-10-06 05:00:53 +0000392/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
393/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000394unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000395 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000396 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000397 // ImplicitCastExpr.
398 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
399 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000400 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000402 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
403 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000404 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Anders Carlssonbcba2012008-01-31 02:13:57 +0000406 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
407 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000408 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000410 if (!FDecl->getIdentifier())
411 return 0;
412
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000413 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000414}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000415
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000416QualType CallExpr::getCallReturnType() const {
417 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000418 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000419 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000420 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000421 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000422
John McCall183700f2009-09-21 23:43:11 +0000423 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000424 return FnType->getResultType();
425}
Chris Lattnercb888962008-10-06 05:00:53 +0000426
Mike Stump1eb44332009-09-09 15:08:12 +0000427MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
428 SourceRange qualrange, NamedDecl *memberdecl,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000429 SourceLocation l, bool has_explicit,
430 SourceLocation langle,
431 const TemplateArgument *targs, unsigned numtargs,
432 SourceLocation rangle, QualType ty)
Mike Stump1eb44332009-09-09 15:08:12 +0000433 : Expr(MemberExprClass, ty,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000434 base->isTypeDependent() || (qual && qual->isDependent()),
435 base->isValueDependent() || (qual && qual->isDependent())),
436 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000437 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(has_explicit) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000438 // Initialize the qualifier, if any.
439 if (HasQualifier) {
440 NameQualifier *NQ = getMemberQualifier();
441 NQ->NNS = qual;
442 NQ->Range = qualrange;
443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000445 // Initialize the explicit template argument list, if any.
446 if (HasExplicitTemplateArgumentList) {
Mike Stump1eb44332009-09-09 15:08:12 +0000447 ExplicitTemplateArgumentList *ETemplateArgs
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000448 = getExplicitTemplateArgumentList();
449 ETemplateArgs->LAngleLoc = langle;
450 ETemplateArgs->RAngleLoc = rangle;
451 ETemplateArgs->NumTemplateArgs = numtargs;
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000453 TemplateArgument *TemplateArgs = ETemplateArgs->getTemplateArgs();
454 for (unsigned I = 0; I < numtargs; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000455 new (TemplateArgs + I) TemplateArgument(targs[I]);
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000456 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000457}
458
Mike Stump1eb44332009-09-09 15:08:12 +0000459MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
460 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000461 SourceRange qualrange,
Mike Stump1eb44332009-09-09 15:08:12 +0000462 NamedDecl *memberdecl,
463 SourceLocation l,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000464 bool has_explicit,
465 SourceLocation langle,
466 const TemplateArgument *targs,
467 unsigned numtargs,
468 SourceLocation rangle,
469 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000470 std::size_t Size = sizeof(MemberExpr);
471 if (qual != 0)
472 Size += sizeof(NameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000474 if (has_explicit)
Mike Stump1eb44332009-09-09 15:08:12 +0000475 Size += sizeof(ExplicitTemplateArgumentList) +
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000476 sizeof(TemplateArgument) * numtargs;
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000478 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000479 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
480 has_explicit, langle, targs, numtargs, rangle,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000481 ty);
482}
483
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000484const char *CastExpr::getCastKindName() const {
485 switch (getCastKind()) {
486 case CastExpr::CK_Unknown:
487 return "Unknown";
488 case CastExpr::CK_BitCast:
489 return "BitCast";
490 case CastExpr::CK_NoOp:
491 return "NoOp";
492 case CastExpr::CK_DerivedToBase:
493 return "DerivedToBase";
494 case CastExpr::CK_Dynamic:
495 return "Dynamic";
496 case CastExpr::CK_ToUnion:
497 return "ToUnion";
498 case CastExpr::CK_ArrayToPointerDecay:
499 return "ArrayToPointerDecay";
500 case CastExpr::CK_FunctionToPointerDecay:
501 return "FunctionToPointerDecay";
502 case CastExpr::CK_NullToMemberPointer:
503 return "NullToMemberPointer";
504 case CastExpr::CK_BaseToDerivedMemberPointer:
505 return "BaseToDerivedMemberPointer";
506 case CastExpr::CK_UserDefinedConversion:
507 return "UserDefinedConversion";
508 case CastExpr::CK_ConstructorConversion:
509 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000510 case CastExpr::CK_IntegralToPointer:
511 return "IntegralToPointer";
512 case CastExpr::CK_PointerToIntegral:
513 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000514 case CastExpr::CK_ToVoid:
515 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000516 case CastExpr::CK_VectorSplat:
517 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000518 case CastExpr::CK_IntegralCast:
519 return "IntegralCast";
520 case CastExpr::CK_IntegralToFloating:
521 return "IntegralToFloating";
522 case CastExpr::CK_FloatingToIntegral:
523 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000524 case CastExpr::CK_FloatingCast:
525 return "FloatingCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000528 assert(0 && "Unhandled cast kind!");
529 return 0;
530}
531
Reid Spencer5f016e22007-07-11 17:01:13 +0000532/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
533/// corresponds to, e.g. "<<=".
534const char *BinaryOperator::getOpcodeStr(Opcode Op) {
535 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000536 case PtrMemD: return ".*";
537 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 case Mul: return "*";
539 case Div: return "/";
540 case Rem: return "%";
541 case Add: return "+";
542 case Sub: return "-";
543 case Shl: return "<<";
544 case Shr: return ">>";
545 case LT: return "<";
546 case GT: return ">";
547 case LE: return "<=";
548 case GE: return ">=";
549 case EQ: return "==";
550 case NE: return "!=";
551 case And: return "&";
552 case Xor: return "^";
553 case Or: return "|";
554 case LAnd: return "&&";
555 case LOr: return "||";
556 case Assign: return "=";
557 case MulAssign: return "*=";
558 case DivAssign: return "/=";
559 case RemAssign: return "%=";
560 case AddAssign: return "+=";
561 case SubAssign: return "-=";
562 case ShlAssign: return "<<=";
563 case ShrAssign: return ">>=";
564 case AndAssign: return "&=";
565 case XorAssign: return "^=";
566 case OrAssign: return "|=";
567 case Comma: return ",";
568 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000569
570 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000571}
572
Mike Stump1eb44332009-09-09 15:08:12 +0000573BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000574BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
575 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000576 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000577 case OO_Plus: return Add;
578 case OO_Minus: return Sub;
579 case OO_Star: return Mul;
580 case OO_Slash: return Div;
581 case OO_Percent: return Rem;
582 case OO_Caret: return Xor;
583 case OO_Amp: return And;
584 case OO_Pipe: return Or;
585 case OO_Equal: return Assign;
586 case OO_Less: return LT;
587 case OO_Greater: return GT;
588 case OO_PlusEqual: return AddAssign;
589 case OO_MinusEqual: return SubAssign;
590 case OO_StarEqual: return MulAssign;
591 case OO_SlashEqual: return DivAssign;
592 case OO_PercentEqual: return RemAssign;
593 case OO_CaretEqual: return XorAssign;
594 case OO_AmpEqual: return AndAssign;
595 case OO_PipeEqual: return OrAssign;
596 case OO_LessLess: return Shl;
597 case OO_GreaterGreater: return Shr;
598 case OO_LessLessEqual: return ShlAssign;
599 case OO_GreaterGreaterEqual: return ShrAssign;
600 case OO_EqualEqual: return EQ;
601 case OO_ExclaimEqual: return NE;
602 case OO_LessEqual: return LE;
603 case OO_GreaterEqual: return GE;
604 case OO_AmpAmp: return LAnd;
605 case OO_PipePipe: return LOr;
606 case OO_Comma: return Comma;
607 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000608 }
609}
610
611OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
612 static const OverloadedOperatorKind OverOps[] = {
613 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
614 OO_Star, OO_Slash, OO_Percent,
615 OO_Plus, OO_Minus,
616 OO_LessLess, OO_GreaterGreater,
617 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
618 OO_EqualEqual, OO_ExclaimEqual,
619 OO_Amp,
620 OO_Caret,
621 OO_Pipe,
622 OO_AmpAmp,
623 OO_PipePipe,
624 OO_Equal, OO_StarEqual,
625 OO_SlashEqual, OO_PercentEqual,
626 OO_PlusEqual, OO_MinusEqual,
627 OO_LessLessEqual, OO_GreaterGreaterEqual,
628 OO_AmpEqual, OO_CaretEqual,
629 OO_PipeEqual,
630 OO_Comma
631 };
632 return OverOps[Opc];
633}
634
Mike Stump1eb44332009-09-09 15:08:12 +0000635InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000636 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000637 SourceLocation rbraceloc)
Douglas Gregor9ea62762009-05-21 23:17:49 +0000638 : Expr(InitListExprClass, QualType(),
639 hasAnyTypeDependentArguments(initExprs, numInits),
640 hasAnyValueDependentArguments(initExprs, numInits)),
Mike Stump1eb44332009-09-09 15:08:12 +0000641 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000642 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000643
644 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000645}
Reid Spencer5f016e22007-07-11 17:01:13 +0000646
Douglas Gregorfa219202009-03-20 23:58:33 +0000647void InitListExpr::reserveInits(unsigned NumInits) {
648 if (NumInits > InitExprs.size())
649 InitExprs.reserve(NumInits);
650}
651
Douglas Gregor4c678342009-01-28 21:54:33 +0000652void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000653 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000654 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000655 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000656 InitExprs.resize(NumInits, 0);
657}
658
659Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
660 if (Init >= InitExprs.size()) {
661 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
662 InitExprs.back() = expr;
663 return 0;
664 }
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Douglas Gregor4c678342009-01-28 21:54:33 +0000666 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
667 InitExprs[Init] = expr;
668 return Result;
669}
670
Steve Naroffbfdcae62008-09-04 15:31:07 +0000671/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000672///
673const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000674 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000675 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000676}
677
Mike Stump1eb44332009-09-09 15:08:12 +0000678SourceLocation BlockExpr::getCaretLocation() const {
679 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000680}
Mike Stump1eb44332009-09-09 15:08:12 +0000681const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000682 return TheBlock->getBody();
683}
Mike Stump1eb44332009-09-09 15:08:12 +0000684Stmt *BlockExpr::getBody() {
685 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000686}
Steve Naroff56ee6892008-10-08 17:01:13 +0000687
688
Reid Spencer5f016e22007-07-11 17:01:13 +0000689//===----------------------------------------------------------------------===//
690// Generic Expression Routines
691//===----------------------------------------------------------------------===//
692
Chris Lattner026dc962009-02-14 07:37:35 +0000693/// isUnusedResultAWarning - Return true if this immediate expression should
694/// be warned about if the result is unused. If so, fill in Loc and Ranges
695/// with location to warn on and the source range[s] to report with the
696/// warning.
697bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000698 SourceRange &R2) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000699 // Don't warn if the expr is type dependent. The type could end up
700 // instantiating to void.
701 if (isTypeDependent())
702 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 switch (getStmtClass()) {
705 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000706 Loc = getExprLoc();
707 R1 = getSourceRange();
708 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000710 return cast<ParenExpr>(this)->getSubExpr()->
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000711 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 case UnaryOperatorClass: {
713 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000716 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 case UnaryOperator::PostInc:
718 case UnaryOperator::PostDec:
719 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000720 case UnaryOperator::PreDec: // ++/--
721 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 case UnaryOperator::Deref:
723 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000724 if (getType().isVolatileQualified())
725 return false;
726 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 case UnaryOperator::Real:
728 case UnaryOperator::Imag:
729 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000730 if (UO->getSubExpr()->getType().isVolatileQualified())
731 return false;
732 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 case UnaryOperator::Extension:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000734 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 }
Chris Lattner026dc962009-02-14 07:37:35 +0000736 Loc = UO->getOperatorLoc();
737 R1 = UO->getSubExpr()->getSourceRange();
738 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000740 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000741 const BinaryOperator *BO = cast<BinaryOperator>(this);
742 // Consider comma to have side effects if the LHS or RHS does.
743 if (BO->getOpcode() == BinaryOperator::Comma)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000744 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
745 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Chris Lattner026dc962009-02-14 07:37:35 +0000747 if (BO->isAssignmentOp())
748 return false;
749 Loc = BO->getOperatorLoc();
750 R1 = BO->getLHS()->getSourceRange();
751 R2 = BO->getRHS()->getSourceRange();
752 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000753 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000754 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000755 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000756
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000757 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000758 // The condition must be evaluated, but if either the LHS or RHS is a
759 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000760 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000761 if (Exp->getLHS() &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000762 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000763 return true;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000764 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000765 }
766
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000768 // If the base pointer or element is to a volatile pointer/field, accessing
769 // it is a side effect.
770 if (getType().isVolatileQualified())
771 return false;
772 Loc = cast<MemberExpr>(this)->getMemberLoc();
773 R1 = SourceRange(Loc, Loc);
774 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
775 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 case ArraySubscriptExprClass:
778 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000779 // it is a side effect.
780 if (getType().isVolatileQualified())
781 return false;
782 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
783 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
784 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
785 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000786
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000788 case CXXOperatorCallExprClass:
789 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000790 // If this is a direct call, get the callee.
791 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000792 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000793 // If the callee has attribute pure, const, or warn_unused_result, warn
794 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000795 //
796 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
797 // updated to match for QoI.
798 if (FD->getAttr<WarnUnusedResultAttr>() ||
799 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
800 Loc = CE->getCallee()->getLocStart();
801 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000803 if (unsigned NumArgs = CE->getNumArgs())
804 R2 = SourceRange(CE->getArg(0)->getLocStart(),
805 CE->getArg(NumArgs-1)->getLocEnd());
806 return true;
807 }
Chris Lattner026dc962009-02-14 07:37:35 +0000808 }
809 return false;
810 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000811 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000812 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000814 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000815#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000816 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000817 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000818 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000819 Loc = Ref->getLocation();
820 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
821 if (Ref->getBase())
822 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000823#else
824 Loc = getExprLoc();
825 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000826#endif
827 return true;
828 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000829 case StmtExprClass: {
830 // Statement exprs don't logically have side effects themselves, but are
831 // sometimes used in macros in ways that give them a type that is unused.
832 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
833 // however, if the result of the stmt expr is dead, we don't want to emit a
834 // warning.
835 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
836 if (!CS->body_empty())
837 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000838 return E->isUnusedResultAWarning(Loc, R1, R2);
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Chris Lattner026dc962009-02-14 07:37:35 +0000840 Loc = cast<StmtExpr>(this)->getLParenLoc();
841 R1 = getSourceRange();
842 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000843 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000844 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000845 // If this is an explicit cast to void, allow it. People do this when they
846 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000847 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000848 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000849 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
850 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
851 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000852 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 // If this is a cast to void, check the operand. Otherwise, the result of
854 // the cast is unused.
855 if (getType()->isVoidType())
Douglas Gregor68584ed2009-06-18 16:11:24 +0000856 return cast<CastExpr>(this)->getSubExpr()
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000857 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner026dc962009-02-14 07:37:35 +0000858 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
859 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
860 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Eli Friedman4be1f472008-05-19 21:24:43 +0000862 case ImplicitCastExprClass:
863 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000864 return cast<ImplicitCastExpr>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000865 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000866
Chris Lattner04421082008-04-08 04:40:51 +0000867 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000868 return cast<CXXDefaultArgExpr>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000869 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000870
871 case CXXNewExprClass:
872 // FIXME: In theory, there might be new expressions that don't have side
873 // effects (e.g. a placement new with an uninitialized POD).
874 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000875 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +0000876 case CXXBindTemporaryExprClass:
877 return cast<CXXBindTemporaryExpr>(this)
878 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000879 case CXXExprWithTemporariesClass:
880 return cast<CXXExprWithTemporaries>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000881 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000882 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000883}
884
Douglas Gregorba7e2102008-10-22 15:04:37 +0000885/// DeclCanBeLvalue - Determine whether the given declaration can be
886/// an lvalue. This is a helper routine for isLvalue.
887static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000888 // C++ [temp.param]p6:
889 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000890 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +0000891 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
892 return NTTParm->getType()->isReferenceType();
893
Douglas Gregor44b43212008-12-11 16:49:14 +0000894 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000895 // C++ 3.10p2: An lvalue refers to an object or function.
896 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor83314aa2009-07-08 20:55:45 +0000897 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
898 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +0000899}
900
Reid Spencer5f016e22007-07-11 17:01:13 +0000901/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
902/// incomplete type other than void. Nonarray expressions that can be lvalues:
903/// - name, where name must be a variable
904/// - e[i]
905/// - (e), where e must be an lvalue
906/// - e.name, where e must be an lvalue
907/// - e->name
908/// - *e, the type of e cannot be a function type
909/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000910/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000911/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000912///
Chris Lattner28be73f2008-07-26 21:30:36 +0000913Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +0000914 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
915
916 isLvalueResult Res = isLvalueInternal(Ctx);
917 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
918 return Res;
919
Douglas Gregor98cd5992008-10-21 23:43:52 +0000920 // first, check the type (C99 6.3.2.1). Expressions with function
921 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +0000922 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 return LV_NotObjectType;
924
Steve Naroffacb818a2008-02-10 01:39:04 +0000925 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +0000926 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000927 return LV_IncompleteVoidType;
928
Eli Friedman53202852009-05-03 22:36:05 +0000929 return LV_Valid;
930}
Bill Wendling08ad47c2007-07-17 03:52:31 +0000931
Eli Friedman53202852009-05-03 22:36:05 +0000932// Check whether the expression can be sanely treated like an l-value
933Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000935 case StringLiteralClass: // C99 6.5.1p4
936 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000937 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
939 // For vectors, make sure base is an lvalue (i.e. not a function call).
940 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000941 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +0000943 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000944 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
945 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 return LV_Valid;
947 break;
Chris Lattner41110242008-06-17 18:05:57 +0000948 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000949 case BlockDeclRefExprClass: {
950 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000951 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000952 return LV_Valid;
953 break;
954 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000955 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000957 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
958 NamedDecl *Member = m->getMemberDecl();
959 // C++ [expr.ref]p4:
960 // If E2 is declared to have type "reference to T", then E1.E2
961 // is an lvalue.
962 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
963 if (Value->getType()->isReferenceType())
964 return LV_Valid;
965
966 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000967 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000968 return LV_Valid;
969
970 // -- If E2 is a non-static data member [...]. If E1 is an
971 // lvalue, then E1.E2 is an lvalue.
972 if (isa<FieldDecl>(Member))
973 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
974
975 // -- If it refers to a static member function [...], then
976 // E1.E2 is an lvalue.
977 // -- Otherwise, if E1.E2 refers to a non-static member
978 // function [...], then E1.E2 is not an lvalue.
979 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
980 return Method->isStatic()? LV_Valid : LV_MemberFunction;
981
982 // -- If E2 is a member enumerator [...], the expression E1.E2
983 // is not an lvalue.
984 if (isa<EnumConstantDecl>(Member))
985 return LV_InvalidExpression;
986
987 // Not an lvalue.
988 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +0000989 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000990
991 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000992 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000993 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000994 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000996 return LV_Valid; // C99 6.5.3p4
997
998 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000999 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1000 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001001 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001002
1003 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1004 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1005 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1006 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001008 case ImplicitCastExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +00001009 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001010 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001012 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001013 case BinaryOperatorClass:
1014 case CompoundAssignOperatorClass: {
1015 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001016
1017 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1018 BinOp->getOpcode() == BinaryOperator::Comma)
1019 return BinOp->getRHS()->isLvalue(Ctx);
1020
Sebastian Redl22460502009-02-07 00:15:38 +00001021 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001022 // The result of a .* expression is an lvalue only if its first operand is
1023 // an lvalue and its second operand is a pointer to data member.
1024 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001025 !BinOp->getType()->isFunctionType())
1026 return BinOp->getLHS()->isLvalue(Ctx);
1027
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001028 // The result of an ->* expression is an lvalue only if its second operand
1029 // is a pointer to data member.
1030 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1031 !BinOp->getType()->isFunctionType()) {
1032 QualType Ty = BinOp->getRHS()->getType();
1033 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1034 return LV_Valid;
1035 }
1036
Douglas Gregorbf3af052008-11-13 20:12:29 +00001037 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001038 return LV_InvalidExpression;
1039
Douglas Gregorbf3af052008-11-13 20:12:29 +00001040 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001041 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001042 // The result of an assignment operation [...] is an lvalue.
1043 return LV_Valid;
1044
1045
1046 // C99 6.5.16:
1047 // An assignment expression [...] is not an lvalue.
1048 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001049 }
Mike Stump1eb44332009-09-09 15:08:12 +00001050 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001051 case CXXOperatorCallExprClass:
1052 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001053 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001054 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001055 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001056 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1057 if (ReturnType->isLValueReferenceType())
1058 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001059
Douglas Gregor9d293df2008-10-28 00:22:11 +00001060 break;
1061 }
Steve Naroffe6386392007-12-05 04:00:10 +00001062 case CompoundLiteralExprClass: // C99 6.5.2.5p5
1063 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001064 case ChooseExprClass:
1065 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001066 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001067 case ExtVectorElementExprClass:
1068 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001069 return LV_DuplicateVectorComponents;
1070 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001071 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1072 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001073 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1074 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001075 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001076 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001077 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001078 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001079 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001080 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +00001081 case CXXConditionDeclExprClass:
1082 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001083 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001084 case CXXFunctionalCastExprClass:
1085 case CXXStaticCastExprClass:
1086 case CXXDynamicCastExprClass:
1087 case CXXReinterpretCastExprClass:
1088 case CXXConstCastExprClass:
1089 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001090 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001091 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1092 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001093 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1094 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001095 return LV_Valid;
1096 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001097 case CXXTypeidExprClass:
1098 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1099 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001100 case CXXBindTemporaryExprClass:
1101 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1102 isLvalueInternal(Ctx);
Sebastian Redl76458502009-04-17 16:30:52 +00001103 case ConditionalOperatorClass: {
1104 // Complicated handling is only for C++.
1105 if (!Ctx.getLangOptions().CPlusPlus)
1106 return LV_InvalidExpression;
1107
1108 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1109 // everywhere there's an object converted to an rvalue. Also, any other
1110 // casts should be wrapped by ImplicitCastExprs. There's just the special
1111 // case involving throws to work out.
1112 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001113 Expr *True = Cond->getTrueExpr();
1114 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001115 // C++0x 5.16p2
1116 // If either the second or the third operand has type (cv) void, [...]
1117 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001118 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001119 return LV_InvalidExpression;
1120
1121 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001122 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001123 return LV_InvalidExpression;
1124
1125 // That's it.
1126 return LV_Valid;
1127 }
1128
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 default:
1130 break;
1131 }
1132 return LV_InvalidExpression;
1133}
1134
1135/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1136/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001137/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001138/// recursively, any member or element of all contained aggregates or unions)
1139/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001140Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001141Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001142 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001145 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001146 // C++ 3.10p11: Functions cannot be modified, but pointers to
1147 // functions can be modifiable.
1148 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1149 return MLV_NotObjectType;
1150 break;
1151
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 case LV_NotObjectType: return MLV_NotObjectType;
1153 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001154 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001155 case LV_InvalidExpression:
1156 // If the top level is a C-style cast, and the subexpression is a valid
1157 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1158 // GCC extension. We don't support it, but we want to produce good
1159 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001160 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1161 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1162 if (Loc)
1163 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001164 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001165 }
1166 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001167 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001168 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001170
1171 // The following is illegal:
1172 // void takeclosure(void (^C)(void));
1173 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1174 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001175 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001176 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1177 return MLV_NotBlockQualified;
1178 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001179
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001180 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001181 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001182 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1183 if (Expr->getSetterMethod() == 0)
1184 return MLV_NoSetterProperty;
1185 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001186
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001187 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001189 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001191 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001193 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Ted Kremenek6217b802009-07-29 21:53:49 +00001196 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001197 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 return MLV_ConstQualified;
1199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Mike Stump1eb44332009-09-09 15:08:12 +00001201 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001202}
1203
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001204/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001205/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001206bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001207 switch (getStmtClass()) {
1208 default:
1209 return false;
1210 case ObjCIvarRefExprClass:
1211 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001212 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001213 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001214 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001215 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001216 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001217 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001218 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001219 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001220 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001221 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001222 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1223 if (VD->hasGlobalStorage())
1224 return true;
1225 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001226 // dereferencing to a pointer is always a gc'able candidate,
1227 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001228 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001229 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001230 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001231 return false;
1232 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001233 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001234 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001235 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001236 }
1237 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001238 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001239 }
1240}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001241Expr* Expr::IgnoreParens() {
1242 Expr* E = this;
1243 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1244 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001246 return E;
1247}
1248
Chris Lattner56f34942008-02-13 01:02:39 +00001249/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1250/// or CastExprs or ImplicitCastExprs, returning their operand.
1251Expr *Expr::IgnoreParenCasts() {
1252 Expr *E = this;
1253 while (true) {
1254 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1255 E = P->getSubExpr();
1256 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1257 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001258 else
1259 return E;
1260 }
1261}
1262
Chris Lattnerecdd8412009-03-13 17:28:01 +00001263/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1264/// value (including ptr->int casts of the same size). Strip off any
1265/// ParenExpr or CastExprs, returning their operand.
1266Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1267 Expr *E = this;
1268 while (true) {
1269 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1270 E = P->getSubExpr();
1271 continue;
1272 }
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Chris Lattnerecdd8412009-03-13 17:28:01 +00001274 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1275 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1276 // ptr<->int casts of the same width. We also ignore all identify casts.
1277 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Chris Lattnerecdd8412009-03-13 17:28:01 +00001279 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1280 E = SE;
1281 continue;
1282 }
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Chris Lattnerecdd8412009-03-13 17:28:01 +00001284 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1285 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1286 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1287 E = SE;
1288 continue;
1289 }
1290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Chris Lattnerecdd8412009-03-13 17:28:01 +00001292 return E;
1293 }
1294}
1295
1296
Douglas Gregor898574e2008-12-05 23:32:09 +00001297/// hasAnyTypeDependentArguments - Determines if any of the expressions
1298/// in Exprs is type-dependent.
1299bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1300 for (unsigned I = 0; I < NumExprs; ++I)
1301 if (Exprs[I]->isTypeDependent())
1302 return true;
1303
1304 return false;
1305}
1306
1307/// hasAnyValueDependentArguments - Determines if any of the expressions
1308/// in Exprs is value-dependent.
1309bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1310 for (unsigned I = 0; I < NumExprs; ++I)
1311 if (Exprs[I]->isValueDependent())
1312 return true;
1313
1314 return false;
1315}
1316
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001317bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001318 // This function is attempting whether an expression is an initializer
1319 // which can be evaluated at compile-time. isEvaluatable handles most
1320 // of the cases, but it can't deal with some initializer-specific
1321 // expressions, and it can't deal with aggregates; we deal with those here,
1322 // and fall back to isEvaluatable for the other cases.
1323
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001324 // FIXME: This function assumes the variable being assigned to
1325 // isn't a reference type!
1326
Anders Carlssone8a32b82008-11-24 05:23:59 +00001327 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001328 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001329 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001330 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001331 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001332 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001333 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001334 // This handles gcc's extension that allows global initializers like
1335 // "struct x {int x;} x = (struct x) {};".
1336 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001337 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001338 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001339 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001340 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001341 // FIXME: This doesn't deal with fields with reference types correctly.
1342 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1343 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001344 const InitListExpr *Exp = cast<InitListExpr>(this);
1345 unsigned numInits = Exp->getNumInits();
1346 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001347 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001348 return false;
1349 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001350 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001351 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001352 case ImplicitValueInitExprClass:
1353 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001354 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001355 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001356 case UnaryOperatorClass: {
1357 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1358 if (Exp->getOpcode() == UnaryOperator::Extension)
1359 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1360 break;
1361 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001362 case BinaryOperatorClass: {
1363 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1364 // but this handles the common case.
1365 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1366 if (Exp->getOpcode() == BinaryOperator::Sub &&
1367 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1368 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1369 return true;
1370 break;
1371 }
Chris Lattner81045d82009-04-21 05:19:11 +00001372 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001373 case CStyleCastExprClass:
1374 // Handle casts with a destination that's a struct or union; this
1375 // deals with both the gcc no-op struct cast extension and the
1376 // cast-to-union extension.
1377 if (getType()->isRecordType())
1378 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001379
1380 // Integer->integer casts can be handled here, which is important for
1381 // things like (int)(&&x-&&y). Scary but true.
1382 if (getType()->isIntegerType() &&
1383 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1384 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1385
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001386 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001387 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001388 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001389}
1390
Reid Spencer5f016e22007-07-11 17:01:13 +00001391/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001392/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001393
1394/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1395/// comma, etc
1396///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001397/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1398/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1399/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001400
Eli Friedmane28d7192009-02-26 09:29:13 +00001401// CheckICE - This function does the fundamental ICE checking: the returned
1402// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1403// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001404// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001405// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001406// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001407//
1408// Meanings of Val:
1409// 0: This expression is an ICE if it can be evaluated by Evaluate.
1410// 1: This expression is not an ICE, but if it isn't evaluated, it's
1411// a legal subexpression for an ICE. This return value is used to handle
1412// the comma operator in C99 mode.
1413// 2: This expression is not an ICE, and is not a legal subexpression for one.
1414
1415struct ICEDiag {
1416 unsigned Val;
1417 SourceLocation Loc;
1418
1419 public:
1420 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1421 ICEDiag() : Val(0) {}
1422};
1423
1424ICEDiag NoDiag() { return ICEDiag(); }
1425
Eli Friedman60ce9632009-02-27 04:07:58 +00001426static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1427 Expr::EvalResult EVResult;
1428 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1429 !EVResult.Val.isInt()) {
1430 return ICEDiag(2, E->getLocStart());
1431 }
1432 return NoDiag();
1433}
1434
Eli Friedmane28d7192009-02-26 09:29:13 +00001435static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001436 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001437 if (!E->getType()->isIntegralType()) {
1438 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001439 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001440
1441 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001442#define STMT(Node, Base) case Expr::Node##Class:
1443#define EXPR(Node, Base)
1444#include "clang/AST/StmtNodes.def"
1445 case Expr::PredefinedExprClass:
1446 case Expr::FloatingLiteralClass:
1447 case Expr::ImaginaryLiteralClass:
1448 case Expr::StringLiteralClass:
1449 case Expr::ArraySubscriptExprClass:
1450 case Expr::MemberExprClass:
1451 case Expr::CompoundAssignOperatorClass:
1452 case Expr::CompoundLiteralExprClass:
1453 case Expr::ExtVectorElementExprClass:
1454 case Expr::InitListExprClass:
1455 case Expr::DesignatedInitExprClass:
1456 case Expr::ImplicitValueInitExprClass:
1457 case Expr::ParenListExprClass:
1458 case Expr::VAArgExprClass:
1459 case Expr::AddrLabelExprClass:
1460 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001461 case Expr::CXXMemberCallExprClass:
1462 case Expr::CXXDynamicCastExprClass:
1463 case Expr::CXXTypeidExprClass:
1464 case Expr::CXXNullPtrLiteralExprClass:
1465 case Expr::CXXThisExprClass:
1466 case Expr::CXXThrowExprClass:
1467 case Expr::CXXConditionDeclExprClass: // FIXME: is this correct?
1468 case Expr::CXXNewExprClass:
1469 case Expr::CXXDeleteExprClass:
1470 case Expr::CXXPseudoDestructorExprClass:
1471 case Expr::UnresolvedFunctionNameExprClass:
1472 case Expr::UnresolvedDeclRefExprClass:
1473 case Expr::TemplateIdRefExprClass:
1474 case Expr::CXXConstructExprClass:
1475 case Expr::CXXBindTemporaryExprClass:
1476 case Expr::CXXExprWithTemporariesClass:
1477 case Expr::CXXTemporaryObjectExprClass:
1478 case Expr::CXXUnresolvedConstructExprClass:
1479 case Expr::CXXUnresolvedMemberExprClass:
1480 case Expr::ObjCStringLiteralClass:
1481 case Expr::ObjCEncodeExprClass:
1482 case Expr::ObjCMessageExprClass:
1483 case Expr::ObjCSelectorExprClass:
1484 case Expr::ObjCProtocolExprClass:
1485 case Expr::ObjCIvarRefExprClass:
1486 case Expr::ObjCPropertyRefExprClass:
1487 case Expr::ObjCImplicitSetterGetterRefExprClass:
1488 case Expr::ObjCSuperExprClass:
1489 case Expr::ObjCIsaExprClass:
1490 case Expr::ShuffleVectorExprClass:
1491 case Expr::BlockExprClass:
1492 case Expr::BlockDeclRefExprClass:
1493 case Expr::NoStmtClass:
1494 case Expr::ExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001495 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001496
Douglas Gregor043cad22009-09-11 00:18:58 +00001497 case Expr::GNUNullExprClass:
1498 // GCC considers the GNU __null value to be an integral constant expression.
1499 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001500
Eli Friedmane28d7192009-02-26 09:29:13 +00001501 case Expr::ParenExprClass:
1502 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1503 case Expr::IntegerLiteralClass:
1504 case Expr::CharacterLiteralClass:
1505 case Expr::CXXBoolLiteralExprClass:
1506 case Expr::CXXZeroInitValueExprClass:
1507 case Expr::TypesCompatibleExprClass:
1508 case Expr::UnaryTypeTraitExprClass:
1509 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001510 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001511 case Expr::CXXOperatorCallExprClass: {
1512 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001513 if (CE->isBuiltinCall(Ctx))
1514 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001515 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001516 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001517 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001518 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1519 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001520 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001521 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001522 // C++ 7.1.5.1p2
1523 // A variable of non-volatile const-qualified integral or enumeration
1524 // type initialized by an ICE can be used in ICEs.
1525 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001526 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +00001527 if (Dcl->isInitKnownICE()) {
1528 // We have already checked whether this subexpression is an
1529 // integral constant expression.
1530 if (Dcl->isInitICE())
1531 return NoDiag();
1532 else
1533 return ICEDiag(2, E->getLocStart());
1534 }
1535
1536 if (const Expr *Init = Dcl->getInit()) {
1537 ICEDiag Result = CheckICE(Init, Ctx);
1538 // Cache the result of the ICE test.
1539 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1540 return Result;
1541 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001542 }
1543 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001544 return ICEDiag(2, E->getLocStart());
1545 case Expr::UnaryOperatorClass: {
1546 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001547 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001548 case UnaryOperator::PostInc:
1549 case UnaryOperator::PostDec:
1550 case UnaryOperator::PreInc:
1551 case UnaryOperator::PreDec:
1552 case UnaryOperator::AddrOf:
1553 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001554 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001555
Reid Spencer5f016e22007-07-11 17:01:13 +00001556 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001557 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001559 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001560 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001561 case UnaryOperator::Real:
1562 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001563 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001564 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001565 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1566 // Evaluate matches the proposed gcc behavior for cases like
1567 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1568 // compliance: we should warn earlier for offsetof expressions with
1569 // array subscripts that aren't ICEs, and if the array subscripts
1570 // are ICEs, the value of the offsetof must be an integer constant.
1571 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001572 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001574 case Expr::SizeOfAlignOfExprClass: {
1575 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1576 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1577 return ICEDiag(2, E->getLocStart());
1578 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001579 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001580 case Expr::BinaryOperatorClass: {
1581 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001583 case BinaryOperator::PtrMemD:
1584 case BinaryOperator::PtrMemI:
1585 case BinaryOperator::Assign:
1586 case BinaryOperator::MulAssign:
1587 case BinaryOperator::DivAssign:
1588 case BinaryOperator::RemAssign:
1589 case BinaryOperator::AddAssign:
1590 case BinaryOperator::SubAssign:
1591 case BinaryOperator::ShlAssign:
1592 case BinaryOperator::ShrAssign:
1593 case BinaryOperator::AndAssign:
1594 case BinaryOperator::XorAssign:
1595 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001596 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001597
Reid Spencer5f016e22007-07-11 17:01:13 +00001598 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001600 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001601 case BinaryOperator::Add:
1602 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001604 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001605 case BinaryOperator::LT:
1606 case BinaryOperator::GT:
1607 case BinaryOperator::LE:
1608 case BinaryOperator::GE:
1609 case BinaryOperator::EQ:
1610 case BinaryOperator::NE:
1611 case BinaryOperator::And:
1612 case BinaryOperator::Xor:
1613 case BinaryOperator::Or:
1614 case BinaryOperator::Comma: {
1615 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1616 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001617 if (Exp->getOpcode() == BinaryOperator::Div ||
1618 Exp->getOpcode() == BinaryOperator::Rem) {
1619 // Evaluate gives an error for undefined Div/Rem, so make sure
1620 // we don't evaluate one.
1621 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1622 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1623 if (REval == 0)
1624 return ICEDiag(1, E->getLocStart());
1625 if (REval.isSigned() && REval.isAllOnesValue()) {
1626 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1627 if (LEval.isMinSignedValue())
1628 return ICEDiag(1, E->getLocStart());
1629 }
1630 }
1631 }
1632 if (Exp->getOpcode() == BinaryOperator::Comma) {
1633 if (Ctx.getLangOptions().C99) {
1634 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1635 // if it isn't evaluated.
1636 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1637 return ICEDiag(1, E->getLocStart());
1638 } else {
1639 // In both C89 and C++, commas in ICEs are illegal.
1640 return ICEDiag(2, E->getLocStart());
1641 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001642 }
1643 if (LHSResult.Val >= RHSResult.Val)
1644 return LHSResult;
1645 return RHSResult;
1646 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001648 case BinaryOperator::LOr: {
1649 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1650 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1651 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1652 // Rare case where the RHS has a comma "side-effect"; we need
1653 // to actually check the condition to see whether the side
1654 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001655 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001656 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001657 return RHSResult;
1658 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001659 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001660
Eli Friedmane28d7192009-02-26 09:29:13 +00001661 if (LHSResult.Val >= RHSResult.Val)
1662 return LHSResult;
1663 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001664 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001665 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 }
Douglas Gregorf2991242009-09-10 23:31:45 +00001667 case Expr::CastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001668 case Expr::ImplicitCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001669 case Expr::ExplicitCastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001670 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001671 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001672 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001673 case Expr::CXXStaticCastExprClass:
1674 case Expr::CXXReinterpretCastExprClass:
1675 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00001676 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1677 if (SubExpr->getType()->isIntegralType())
1678 return CheckICE(SubExpr, Ctx);
1679 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1680 return NoDiag();
1681 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001683 case Expr::ConditionalOperatorClass: {
1684 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001685 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00001686 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001687 // expression, and it is fully evaluated. This is an important GNU
1688 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001689 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001690 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001691 Expr::EvalResult EVResult;
1692 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1693 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001694 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001695 }
1696 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001697 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001698 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1699 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1700 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1701 if (CondResult.Val == 2)
1702 return CondResult;
1703 if (TrueResult.Val == 2)
1704 return TrueResult;
1705 if (FalseResult.Val == 2)
1706 return FalseResult;
1707 if (CondResult.Val == 1)
1708 return CondResult;
1709 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1710 return NoDiag();
1711 // Rare case where the diagnostics depend on which side is evaluated
1712 // Note that if we get here, CondResult is 0, and at least one of
1713 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001714 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001715 return FalseResult;
1716 }
1717 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001718 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001719 case Expr::CXXDefaultArgExprClass:
1720 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001721 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001722 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001723 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001724 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001725
Douglas Gregorf2991242009-09-10 23:31:45 +00001726 // Silence a GCC warning
1727 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001728}
Reid Spencer5f016e22007-07-11 17:01:13 +00001729
Eli Friedmane28d7192009-02-26 09:29:13 +00001730bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1731 SourceLocation *Loc, bool isEvaluated) const {
1732 ICEDiag d = CheckICE(this, Ctx);
1733 if (d.Val != 0) {
1734 if (Loc) *Loc = d.Loc;
1735 return false;
1736 }
1737 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001738 if (!Evaluate(EvalResult, Ctx))
1739 assert(0 && "ICE cannot be evaluated!");
1740 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1741 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001742 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001743 return true;
1744}
1745
Reid Spencer5f016e22007-07-11 17:01:13 +00001746/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1747/// integer constant expression with the value zero, or if this is one that is
1748/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001749bool Expr::isNullPointerConstant(ASTContext &Ctx,
1750 NullPointerConstantValueDependence NPC) const {
1751 if (isValueDependent()) {
1752 switch (NPC) {
1753 case NPC_NeverValueDependent:
1754 assert(false && "Unexpected value dependent expression!");
1755 // If the unthinkable happens, fall through to the safest alternative.
1756
1757 case NPC_ValueDependentIsNull:
1758 return isTypeDependent() || getType()->isIntegralType();
1759
1760 case NPC_ValueDependentIsNotNull:
1761 return false;
1762 }
1763 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001764
Sebastian Redl07779722008-10-31 14:43:28 +00001765 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001766 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001767 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001768 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001769 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001770 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001771 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001772 Pointee->isVoidType() && // to void*
1773 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001774 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001775 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001776 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001777 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1778 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001779 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00001780 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1781 // Accept ((void*)0) as a null pointer constant, as many other
1782 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00001783 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001784 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001785 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001786 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001787 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001788 } else if (isa<GNUNullExpr>(this)) {
1789 // The GNU __null extension is always a null pointer constant.
1790 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001791 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001792
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001793 // C++0x nullptr_t is always a null pointer constant.
1794 if (getType()->isNullPtrType())
1795 return true;
1796
Steve Naroffaa58f002008-01-14 16:10:57 +00001797 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00001798 if (!getType()->isIntegerType() ||
1799 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00001800 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Reid Spencer5f016e22007-07-11 17:01:13 +00001802 // If we have an integer constant expression, we need to *evaluate* it and
1803 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001804 llvm::APSInt Result;
1805 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001806}
Steve Naroff31a45842007-07-28 23:10:27 +00001807
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001808FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001809 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001810
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001811 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001812 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001813 if (Field->isBitField())
1814 return Field;
1815
1816 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1817 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1818 return BinOp->getLHS()->getBitField();
1819
1820 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001821}
1822
Chris Lattner2140e902009-02-16 22:14:05 +00001823/// isArrow - Return true if the base expression is a pointer to vector,
1824/// return false if the base expression is a vector.
1825bool ExtVectorElementExpr::isArrow() const {
1826 return getBase()->getType()->isPointerType();
1827}
1828
Nate Begeman213541a2008-04-18 23:10:10 +00001829unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00001830 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00001831 return VT->getNumElements();
1832 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001833}
1834
Nate Begeman8a997642008-05-09 06:41:27 +00001835/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001836bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00001837 // FIXME: Refactor this code to an accessor on the AST node which returns the
1838 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001839 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00001840
1841 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00001842 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00001843 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001844
Nate Begeman190d6a22009-01-18 02:01:21 +00001845 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00001846 if (Comp[0] == 's' || Comp[0] == 'S')
1847 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Daniel Dunbar15027422009-10-17 23:53:04 +00001849 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1850 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00001851 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00001852
Steve Narofffec0b492007-07-30 03:29:09 +00001853 return false;
1854}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001855
Nate Begeman8a997642008-05-09 06:41:27 +00001856/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001857void ExtVectorElementExpr::getEncodedElementAccess(
1858 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001859 llvm::StringRef Comp = Accessor->getName();
1860 if (Comp[0] == 's' || Comp[0] == 'S')
1861 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001863 bool isHi = Comp == "hi";
1864 bool isLo = Comp == "lo";
1865 bool isEven = Comp == "even";
1866 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Nate Begeman8a997642008-05-09 06:41:27 +00001868 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1869 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Nate Begeman8a997642008-05-09 06:41:27 +00001871 if (isHi)
1872 Index = e + i;
1873 else if (isLo)
1874 Index = i;
1875 else if (isEven)
1876 Index = 2 * i;
1877 else if (isOdd)
1878 Index = 2 * i + 1;
1879 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001880 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001881
Nate Begeman3b8d1162008-05-13 21:03:02 +00001882 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001883 }
Nate Begeman8a997642008-05-09 06:41:27 +00001884}
1885
Steve Naroff68d331a2007-09-27 14:38:14 +00001886// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001887ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001888 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001889 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001890 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00001891 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001892 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001893 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001894 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001895 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001896 if (NumArgs) {
1897 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001898 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1899 }
Steve Naroff563477d2007-09-18 23:55:05 +00001900 LBracloc = LBrac;
1901 RBracloc = RBrac;
1902}
1903
Mike Stump1eb44332009-09-09 15:08:12 +00001904// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00001905// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001906ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001907 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001908 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001909 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00001910 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001911 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001912 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001913 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001914 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001915 if (NumArgs) {
1916 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001917 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1918 }
Steve Naroff563477d2007-09-18 23:55:05 +00001919 LBracloc = LBrac;
1920 RBracloc = RBrac;
1921}
1922
Mike Stump1eb44332009-09-09 15:08:12 +00001923// constructor for class messages.
Ted Kremenek4df728e2008-06-24 15:50:53 +00001924ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1925 QualType retType, ObjCMethodDecl *mproto,
1926 SourceLocation LBrac, SourceLocation RBrac,
1927 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00001928: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenek4df728e2008-06-24 15:50:53 +00001929MethodProto(mproto) {
1930 NumArgs = nargs;
1931 SubExprs = new Stmt*[NumArgs+1];
1932 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1933 if (NumArgs) {
1934 for (unsigned i = 0; i != NumArgs; ++i)
1935 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1936 }
1937 LBracloc = LBrac;
1938 RBracloc = RBrac;
1939}
1940
1941ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1942 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1943 switch (x & Flags) {
1944 default:
1945 assert(false && "Invalid ObjCMessageExpr.");
1946 case IsInstMeth:
1947 return ClassInfo(0, 0);
1948 case IsClsMethDeclUnknown:
1949 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1950 case IsClsMethDeclKnown: {
1951 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1952 return ClassInfo(D, D->getIdentifier());
1953 }
1954 }
1955}
1956
Chris Lattner0389e6b2009-04-26 00:44:05 +00001957void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1958 if (CI.first == 0 && CI.second == 0)
1959 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1960 else if (CI.first == 0)
1961 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1962 else
1963 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1964}
1965
1966
Chris Lattner27437ca2007-10-25 00:29:32 +00001967bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00001968 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001969}
1970
Nate Begeman888376a2009-08-12 02:28:50 +00001971void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1972 unsigned NumExprs) {
1973 if (SubExprs) C.Deallocate(SubExprs);
1974
1975 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001976 this->NumExprs = NumExprs;
1977 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00001978}
Nate Begeman888376a2009-08-12 02:28:50 +00001979
1980void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
1981 DestroyChildren(C);
1982 if (SubExprs) C.Deallocate(SubExprs);
1983 this->~ShuffleVectorExpr();
1984 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001985}
1986
Douglas Gregor42602bb2009-08-07 06:08:38 +00001987void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00001988 // Override default behavior of traversing children. If this has a type
1989 // operand and the type is a variable-length array, the child iteration
1990 // will iterate over the size expression. However, this expression belongs
1991 // to the type, not to this, so we don't want to delete it.
1992 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001993 if (isArgumentType()) {
1994 this->~SizeOfAlignOfExpr();
1995 C.Deallocate(this);
1996 }
Sebastian Redl05189992008-11-11 17:56:53 +00001997 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00001998 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001999}
2000
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002001//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002002// DesignatedInitExpr
2003//===----------------------------------------------------------------------===//
2004
2005IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2006 assert(Kind == FieldDesignator && "Only valid on a field designator");
2007 if (Field.NameOrField & 0x01)
2008 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2009 else
2010 return getField()->getIdentifier();
2011}
2012
Mike Stump1eb44332009-09-09 15:08:12 +00002013DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002014 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002015 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002016 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002017 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002018 unsigned NumIndexExprs,
2019 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002020 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002021 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002022 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2023 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002024 this->Designators = new Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002025
2026 // Record the initializer itself.
2027 child_iterator Child = child_begin();
2028 *Child++ = Init;
2029
2030 // Copy the designators and their subexpressions, computing
2031 // value-dependence along the way.
2032 unsigned IndexIdx = 0;
2033 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002034 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002035
2036 if (this->Designators[I].isArrayDesignator()) {
2037 // Compute type- and value-dependence.
2038 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002039 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002040 Index->isTypeDependent() || Index->isValueDependent();
2041
2042 // Copy the index expressions into permanent storage.
2043 *Child++ = IndexExprs[IndexIdx++];
2044 } else if (this->Designators[I].isArrayRangeDesignator()) {
2045 // Compute type- and value-dependence.
2046 Expr *Start = IndexExprs[IndexIdx];
2047 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002048 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002049 Start->isTypeDependent() || Start->isValueDependent() ||
2050 End->isTypeDependent() || End->isValueDependent();
2051
2052 // Copy the start/end expressions into permanent storage.
2053 *Child++ = IndexExprs[IndexIdx++];
2054 *Child++ = IndexExprs[IndexIdx++];
2055 }
2056 }
2057
2058 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002059}
2060
Douglas Gregor05c13a32009-01-22 00:58:24 +00002061DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002062DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002063 unsigned NumDesignators,
2064 Expr **IndexExprs, unsigned NumIndexExprs,
2065 SourceLocation ColonOrEqualLoc,
2066 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002067 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002068 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor9ea62762009-05-21 23:17:49 +00002069 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
2070 ColonOrEqualLoc, UsesColonSyntax,
2071 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002072}
2073
Mike Stump1eb44332009-09-09 15:08:12 +00002074DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002075 unsigned NumIndexExprs) {
2076 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2077 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2078 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2079}
2080
Mike Stump1eb44332009-09-09 15:08:12 +00002081void DesignatedInitExpr::setDesignators(const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002082 unsigned NumDesigs) {
2083 if (Designators)
2084 delete [] Designators;
2085
2086 Designators = new Designator[NumDesigs];
2087 NumDesignators = NumDesigs;
2088 for (unsigned I = 0; I != NumDesigs; ++I)
2089 Designators[I] = Desigs[I];
2090}
2091
Douglas Gregor05c13a32009-01-22 00:58:24 +00002092SourceRange DesignatedInitExpr::getSourceRange() const {
2093 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002094 Designator &First =
2095 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002096 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002097 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002098 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2099 else
2100 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2101 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002102 StartLoc =
2103 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002104 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2105}
2106
Douglas Gregor05c13a32009-01-22 00:58:24 +00002107Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2108 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2109 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2110 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002111 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2112 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2113}
2114
2115Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002116 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002117 "Requires array range designator");
2118 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2119 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002120 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2121 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2122}
2123
2124Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002125 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002126 "Requires array range designator");
2127 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2128 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002129 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2130 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2131}
2132
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002133/// \brief Replaces the designator at index @p Idx with the series
2134/// of designators in [First, Last).
Mike Stump1eb44332009-09-09 15:08:12 +00002135void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
2136 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002137 const Designator *Last) {
2138 unsigned NumNewDesignators = Last - First;
2139 if (NumNewDesignators == 0) {
2140 std::copy_backward(Designators + Idx + 1,
2141 Designators + NumDesignators,
2142 Designators + Idx);
2143 --NumNewDesignators;
2144 return;
2145 } else if (NumNewDesignators == 1) {
2146 Designators[Idx] = *First;
2147 return;
2148 }
2149
Mike Stump1eb44332009-09-09 15:08:12 +00002150 Designator *NewDesignators
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002151 = new Designator[NumDesignators - 1 + NumNewDesignators];
2152 std::copy(Designators, Designators + Idx, NewDesignators);
2153 std::copy(First, Last, NewDesignators + Idx);
2154 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2155 NewDesignators + Idx + NumNewDesignators);
2156 delete [] Designators;
2157 Designators = NewDesignators;
2158 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2159}
2160
Douglas Gregor42602bb2009-08-07 06:08:38 +00002161void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002162 delete [] Designators;
Douglas Gregor42602bb2009-08-07 06:08:38 +00002163 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002164}
2165
Mike Stump1eb44332009-09-09 15:08:12 +00002166ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002167 Expr **exprs, unsigned nexprs,
2168 SourceLocation rparenloc)
2169: Expr(ParenListExprClass, QualType(),
2170 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002171 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002172 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002173
Nate Begeman2ef13e52009-08-10 23:49:36 +00002174 Exprs = new (C) Stmt*[nexprs];
2175 for (unsigned i = 0; i != nexprs; ++i)
2176 Exprs[i] = exprs[i];
2177}
2178
2179void ParenListExpr::DoDestroy(ASTContext& C) {
2180 DestroyChildren(C);
2181 if (Exprs) C.Deallocate(Exprs);
2182 this->~ParenListExpr();
2183 C.Deallocate(this);
2184}
2185
Douglas Gregor05c13a32009-01-22 00:58:24 +00002186//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002187// ExprIterator.
2188//===----------------------------------------------------------------------===//
2189
2190Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2191Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2192Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2193const Expr* ConstExprIterator::operator[](size_t idx) const {
2194 return cast<Expr>(I[idx]);
2195}
2196const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2197const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2198
2199//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002200// Child Iterators for iterating over subexpressions/substatements
2201//===----------------------------------------------------------------------===//
2202
2203// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002204Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2205Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002206
Steve Naroff7779db42007-11-12 14:29:37 +00002207// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002208Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2209Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002210
Steve Naroffe3e9add2008-06-02 23:03:37 +00002211// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002212Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2213Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002214
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002215// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002216Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2217 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002218}
Mike Stump1eb44332009-09-09 15:08:12 +00002219Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2220 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002221}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002222
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002223// ObjCSuperExpr
2224Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2225Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2226
Steve Narofff242b1b2009-07-24 17:54:45 +00002227// ObjCIsaExpr
2228Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2229Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2230
Chris Lattnerd9f69102008-08-10 01:53:14 +00002231// PredefinedExpr
2232Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2233Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002234
2235// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002236Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2237Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002238
2239// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002240Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002241Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002242
2243// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002244Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2245Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002246
Chris Lattner5d661452007-08-26 03:42:43 +00002247// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002248Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2249Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002250
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002251// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002252Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2253Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002254
2255// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002256Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2257Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002258
2259// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002260Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2261Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002262
Sebastian Redl05189992008-11-11 17:56:53 +00002263// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002264Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002265 // If this is of a type and the type is a VLA type (and not a typedef), the
2266 // size expression of the VLA needs to be treated as an executable expression.
2267 // Why isn't this weirdness documented better in StmtIterator?
2268 if (isArgumentType()) {
2269 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2270 getArgumentType().getTypePtr()))
2271 return child_iterator(T);
2272 return child_iterator();
2273 }
Sebastian Redld4575892008-12-03 23:17:54 +00002274 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002275}
Sebastian Redl05189992008-11-11 17:56:53 +00002276Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2277 if (isArgumentType())
2278 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002279 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002280}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002281
2282// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002283Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002284 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002285}
Ted Kremenek1237c672007-08-24 20:06:47 +00002286Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002287 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002288}
2289
2290// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002291Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002292 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002293}
Ted Kremenek1237c672007-08-24 20:06:47 +00002294Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002295 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002296}
Ted Kremenek1237c672007-08-24 20:06:47 +00002297
2298// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002299Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2300Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002301
Nate Begeman213541a2008-04-18 23:10:10 +00002302// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002303Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2304Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002305
2306// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002307Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2308Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002309
Ted Kremenek1237c672007-08-24 20:06:47 +00002310// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002311Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2312Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002313
2314// BinaryOperator
2315Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002316 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002317}
Ted Kremenek1237c672007-08-24 20:06:47 +00002318Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002319 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002320}
2321
2322// ConditionalOperator
2323Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002324 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002325}
Ted Kremenek1237c672007-08-24 20:06:47 +00002326Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002327 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002328}
2329
2330// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002331Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2332Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002333
Ted Kremenek1237c672007-08-24 20:06:47 +00002334// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002335Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2336Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002337
2338// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002339Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2340 return child_iterator();
2341}
2342
2343Stmt::child_iterator TypesCompatibleExpr::child_end() {
2344 return child_iterator();
2345}
Ted Kremenek1237c672007-08-24 20:06:47 +00002346
2347// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002348Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2349Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002350
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002351// GNUNullExpr
2352Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2353Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2354
Eli Friedmand38617c2008-05-14 19:38:39 +00002355// ShuffleVectorExpr
2356Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002357 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002358}
2359Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002360 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002361}
2362
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002363// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002364Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2365Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002366
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002367// InitListExpr
2368Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002369 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002370}
2371Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002372 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002373}
2374
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002375// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002376Stmt::child_iterator DesignatedInitExpr::child_begin() {
2377 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2378 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002379 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2380}
2381Stmt::child_iterator DesignatedInitExpr::child_end() {
2382 return child_iterator(&*child_begin() + NumSubExprs);
2383}
2384
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002385// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002386Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2387 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002388}
2389
Mike Stump1eb44332009-09-09 15:08:12 +00002390Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2391 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002392}
2393
Nate Begeman2ef13e52009-08-10 23:49:36 +00002394// ParenListExpr
2395Stmt::child_iterator ParenListExpr::child_begin() {
2396 return &Exprs[0];
2397}
2398Stmt::child_iterator ParenListExpr::child_end() {
2399 return &Exprs[0]+NumExprs;
2400}
2401
Ted Kremenek1237c672007-08-24 20:06:47 +00002402// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002403Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002404 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002405}
2406Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002407 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002408}
Ted Kremenek1237c672007-08-24 20:06:47 +00002409
2410// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002411Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2412Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002413
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002414// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002415Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002416 return child_iterator();
2417}
2418Stmt::child_iterator ObjCSelectorExpr::child_end() {
2419 return child_iterator();
2420}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002421
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002422// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002423Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2424 return child_iterator();
2425}
2426Stmt::child_iterator ObjCProtocolExpr::child_end() {
2427 return child_iterator();
2428}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002429
Steve Naroff563477d2007-09-18 23:55:05 +00002430// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002431Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002432 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002433}
2434Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002435 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002436}
2437
Steve Naroff4eb206b2008-09-03 18:15:37 +00002438// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002439Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2440Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002441
Ted Kremenek9da13f92008-09-26 23:24:14 +00002442Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2443Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }