blob: b1b91f78d7a61c7fe4cf67f74529a5169b5ed56b [file] [log] [blame]
Ted Kremeneke3a0c142007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremeneke3a0c142007-08-24 20:21:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the subclesses of Expr class declared in ExprCXX.h
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000014#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Douglas Gregor993603d2008-11-14 16:09:21 +000016#include "clang/AST/DeclCXX.h"
Douglas Gregora727cb92009-06-30 22:34:41 +000017#include "clang/AST/DeclTemplate.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000018#include "clang/AST/ExprCXX.h"
Douglas Gregor651fe5e2010-02-24 23:40:28 +000019#include "clang/AST/TypeLoc.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000020#include "clang/Basic/IdentifierTable.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000021using namespace clang;
22
Douglas Gregor9da64192010-04-26 22:37:10 +000023
Ted Kremeneke3a0c142007-08-24 20:21:10 +000024//===----------------------------------------------------------------------===//
25// Child Iterators for iterating over subexpressions/substatements
26//===----------------------------------------------------------------------===//
27
Richard Smithef8bf432012-08-13 20:08:14 +000028bool CXXTypeidExpr::isPotentiallyEvaluated() const {
29 if (isTypeOperand())
30 return false;
31
32 // C++11 [expr.typeid]p3:
33 // When typeid is applied to an expression other than a glvalue of
34 // polymorphic class type, [...] the expression is an unevaluated operand.
35 const Expr *E = getExprOperand();
36 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
37 if (RD->isPolymorphic() && E->isGLValue())
38 return true;
39
40 return false;
41}
42
David Majnemer143c55e2013-09-27 07:04:31 +000043QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
Douglas Gregor9da64192010-04-26 22:37:10 +000044 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000045 Qualifiers Quals;
46 return Context.getUnqualifiedArrayType(
47 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Douglas Gregor9da64192010-04-26 22:37:10 +000048}
49
David Majnemer143c55e2013-09-27 07:04:31 +000050QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
Francois Pichet9f4f2072010-09-08 12:20:18 +000051 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000052 Qualifiers Quals;
53 return Context.getUnqualifiedArrayType(
54 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Francois Pichet9f4f2072010-09-08 12:20:18 +000055}
56
Nico Webercf4ff5862012-10-11 10:13:44 +000057// static
David Majnemer48b08632014-07-14 23:12:54 +000058const UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT,
59 bool *RDHasMultipleGUIDsPtr) {
Nico Webercf4ff5862012-10-11 10:13:44 +000060 // Optionally remove one level of pointer, reference or array indirection.
61 const Type *Ty = QT.getTypePtr();
62 if (QT->isPointerType() || QT->isReferenceType())
63 Ty = QT->getPointeeType().getTypePtr();
64 else if (QT->isArrayType())
David Majnemer68c880b2013-09-27 07:57:34 +000065 Ty = Ty->getBaseElementTypeUnsafe();
Nico Webercf4ff5862012-10-11 10:13:44 +000066
David Majnemer6d476652014-07-15 04:30:17 +000067 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
David Majnemer59c0ec22013-09-07 06:59:46 +000068 if (!RD)
Craig Topper36250ad2014-05-12 05:36:57 +000069 return nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +000070
David Majnemer6d476652014-07-15 04:30:17 +000071 if (const UuidAttr *Uuid = RD->getMostRecentDecl()->getAttr<UuidAttr>())
72 return Uuid;
David Majnemer37de6112014-07-14 23:40:24 +000073
David Majnemer5d22e7e2013-09-07 07:11:04 +000074 // __uuidof can grab UUIDs from template arguments.
David Majnemer6d476652014-07-15 04:30:17 +000075 if (const ClassTemplateSpecializationDecl *CTSD =
David Majnemer59c0ec22013-09-07 06:59:46 +000076 dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
77 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
David Majnemer48b08632014-07-14 23:12:54 +000078 const UuidAttr *UuidForRD = nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +000079
David Majnemer48b08632014-07-14 23:12:54 +000080 for (const TemplateArgument &TA : TAL.asArray()) {
David Majnemer59c0ec22013-09-07 06:59:46 +000081 bool SeenMultipleGUIDs = false;
82
David Majnemer48b08632014-07-14 23:12:54 +000083 const UuidAttr *UuidForTA = nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +000084 if (TA.getKind() == TemplateArgument::Type)
85 UuidForTA = GetUuidAttrOfType(TA.getAsType(), &SeenMultipleGUIDs);
86 else if (TA.getKind() == TemplateArgument::Declaration)
87 UuidForTA =
88 GetUuidAttrOfType(TA.getAsDecl()->getType(), &SeenMultipleGUIDs);
89
90 // If the template argument has a UUID, there are three cases:
91 // - This is the first UUID seen for this RecordDecl.
David Majnemer37c921e2013-09-07 20:21:47 +000092 // - This is a different UUID than previously seen for this RecordDecl.
93 // - This is the same UUID than previously seen for this RecordDecl.
David Majnemer59c0ec22013-09-07 06:59:46 +000094 if (UuidForTA) {
95 if (!UuidForRD)
96 UuidForRD = UuidForTA;
97 else if (UuidForRD != UuidForTA)
98 SeenMultipleGUIDs = true;
99 }
100
101 // Seeing multiple UUIDs means that we couldn't find a UUID
102 if (SeenMultipleGUIDs) {
103 if (RDHasMultipleGUIDsPtr)
104 *RDHasMultipleGUIDsPtr = true;
Craig Topper36250ad2014-05-12 05:36:57 +0000105 return nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +0000106 }
107 }
108
109 return UuidForRD;
David Majnemer5d22e7e2013-09-07 07:11:04 +0000110 }
111
Craig Topper36250ad2014-05-12 05:36:57 +0000112 return nullptr;
Nico Webercf4ff5862012-10-11 10:13:44 +0000113}
114
David Majnemer8eaab6f2013-08-13 06:32:20 +0000115StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const {
116 StringRef Uuid;
117 if (isTypeOperand())
David Majnemer143c55e2013-09-27 07:04:31 +0000118 Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand(Context))->getGuid();
David Majnemer8eaab6f2013-08-13 06:32:20 +0000119 else {
120 // Special case: __uuidof(0) means an all-zero GUID.
121 Expr *Op = getExprOperand();
122 if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
123 Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
124 else
125 Uuid = "00000000-0000-0000-0000-000000000000";
126 }
127 return Uuid;
128}
129
Douglas Gregor747eb782010-07-08 06:14:04 +0000130// CXXScalarValueInitExpr
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000131SourceLocation CXXScalarValueInitExpr::getLocStart() const {
132 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +0000133}
134
Sebastian Redlbd150f42008-11-21 19:14:01 +0000135// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +0000136CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
137 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +0000138 bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000139 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +0000140 SourceRange typeIdParens, Expr *arraySize,
141 InitializationStyle initializationStyle,
142 Expr *initializer, QualType ty,
143 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +0000144 SourceRange Range, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +0000145 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000146 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000147 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000148 ty->containsUnexpandedParameterPack()),
Craig Topper36250ad2014-05-12 05:36:57 +0000149 SubExprs(nullptr), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
Sebastian Redl6047f072012-02-16 12:22:20 +0000150 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
David Blaikie7b97aef2012-11-07 00:12:38 +0000151 Range(Range), DirectInitRange(directInitRange),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000152 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Craig Topper36250ad2014-05-12 05:36:57 +0000153 assert((initializer != nullptr || initializationStyle == NoInit) &&
Sebastian Redl6047f072012-02-16 12:22:20 +0000154 "Only NoInit can have no initializer.");
155 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Craig Topper36250ad2014-05-12 05:36:57 +0000156 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
157 initializer != nullptr);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000158 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000159 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000160 if (arraySize->isInstantiationDependent())
161 ExprBits.InstantiationDependent = true;
162
Douglas Gregora6e053e2010-12-15 01:34:56 +0000163 if (arraySize->containsUnexpandedParameterPack())
164 ExprBits.ContainsUnexpandedParameterPack = true;
165
Sebastian Redl351bb782008-12-02 14:43:59 +0000166 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000167 }
168
Sebastian Redl6047f072012-02-16 12:22:20 +0000169 if (initializer) {
170 if (initializer->isInstantiationDependent())
171 ExprBits.InstantiationDependent = true;
172
173 if (initializer->containsUnexpandedParameterPack())
174 ExprBits.ContainsUnexpandedParameterPack = true;
175
176 SubExprs[i++] = initializer;
177 }
178
Benjamin Kramerc215e762012-08-24 11:54:20 +0000179 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000180 if (placementArgs[j]->isInstantiationDependent())
181 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000182 if (placementArgs[j]->containsUnexpandedParameterPack())
183 ExprBits.ContainsUnexpandedParameterPack = true;
184
Sebastian Redlbd150f42008-11-21 19:14:01 +0000185 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000186 }
David Blaikie3a0de212012-11-08 22:53:48 +0000187
188 switch (getInitializationStyle()) {
189 case CallInit:
190 this->Range.setEnd(DirectInitRange.getEnd()); break;
191 case ListInit:
192 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000193 default:
194 if (TypeIdParens.isValid())
195 this->Range.setEnd(TypeIdParens.getEnd());
196 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000197 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000198}
199
Craig Toppera31a8822013-08-22 07:09:37 +0000200void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000201 unsigned numPlaceArgs, bool hasInitializer){
Craig Topper36250ad2014-05-12 05:36:57 +0000202 assert(SubExprs == nullptr && "SubExprs already allocated");
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000203 Array = isArray;
204 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000205
206 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000207 SubExprs = new (C) Stmt*[TotalSize];
208}
209
Craig Toppera31a8822013-08-22 07:09:37 +0000210bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
Richard Smith902a0232015-02-14 01:52:20 +0000211 return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow(
212 Ctx) &&
213 !getOperatorNew()->isReservedGlobalPlacementOperator();
John McCall75f94982011-03-07 03:12:35 +0000214}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000215
Sebastian Redlbd150f42008-11-21 19:14:01 +0000216// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000217QualType CXXDeleteExpr::getDestroyedType() const {
218 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000219 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000220 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000221
222 if (ArgType->isDependentType() && !ArgType->isPointerType())
223 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000224
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000225 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000226}
227
Douglas Gregorad8a3362009-09-04 17:36:40 +0000228// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000229PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
230 : Type(Info)
231{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000232 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000233}
234
Craig Toppera31a8822013-08-22 07:09:37 +0000235CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000236 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
237 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
238 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
239 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000240 : Expr(CXXPseudoDestructorExprClass,
David Majnemerced8bdf2015-02-25 17:36:15 +0000241 Context.BoundMemberTy,
John McCalldb40c7f2010-12-14 08:05:40 +0000242 VK_RValue, OK_Ordinary,
243 /*isTypeDependent=*/(Base->isTypeDependent() ||
244 (DestroyedType.getTypeSourceInfo() &&
245 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000246 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000247 (Base->isInstantiationDependent() ||
248 (QualifierLoc &&
249 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
250 (ScopeType &&
251 ScopeType->getType()->isInstantiationDependentType()) ||
252 (DestroyedType.getTypeSourceInfo() &&
253 DestroyedType.getTypeSourceInfo()->getType()
254 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000255 // ContainsUnexpandedParameterPack
256 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000257 (QualifierLoc &&
258 QualifierLoc.getNestedNameSpecifier()
259 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000260 (ScopeType &&
261 ScopeType->getType()->containsUnexpandedParameterPack()) ||
262 (DestroyedType.getTypeSourceInfo() &&
263 DestroyedType.getTypeSourceInfo()->getType()
264 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000265 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000266 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000267 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
268 DestroyedType(DestroyedType) { }
269
Douglas Gregor678f90d2010-02-25 01:56:36 +0000270QualType CXXPseudoDestructorExpr::getDestroyedType() const {
271 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
272 return TInfo->getType();
273
274 return QualType();
275}
276
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000277SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000278 SourceLocation End = DestroyedType.getLocation();
279 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000280 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000281 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000282}
283
John McCalld14a8642009-11-21 08:51:07 +0000284// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000285UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000286UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000287 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000288 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000289 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000290 const DeclarationNameInfo &NameInfo,
291 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000292 const TemplateArgumentListInfo *Args,
293 UnresolvedSetIterator Begin,
294 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000295{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000296 assert(Args || TemplateKWLoc.isValid());
297 unsigned num_args = Args ? Args->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +0000298
299 std::size_t Size =
300 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(1,
301 num_args);
302 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedLookupExpr>());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000303 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
304 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000305 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000306 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000307}
308
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000309UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000310UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000311 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000312 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000313 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
314 std::size_t Size =
315 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
316 HasTemplateKWAndArgsInfo, NumTemplateArgs);
317 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000318 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000319 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000320 return E;
321}
322
Craig Toppera31a8822013-08-22 07:09:37 +0000323OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000324 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000325 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000326 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000327 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000328 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000329 UnresolvedSetIterator End,
330 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000331 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000332 bool KnownContainsUnexpandedParameterPack)
333 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
334 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000335 (KnownInstantiationDependent ||
336 NameInfo.isInstantiationDependent() ||
337 (QualifierLoc &&
338 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000339 (KnownContainsUnexpandedParameterPack ||
340 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000341 (QualifierLoc &&
342 QualifierLoc.getNestedNameSpecifier()
343 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000344 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
Craig Topper36250ad2014-05-12 05:36:57 +0000345 Results(nullptr), NumResults(End - Begin),
346 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
347 TemplateKWLoc.isValid()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000348 NumResults = End - Begin;
349 if (NumResults) {
350 // Determine whether this expression is type-dependent.
351 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
352 if ((*I)->getDeclContext()->isDependentContext() ||
353 isa<UnresolvedUsingValueDecl>(*I)) {
354 ExprBits.TypeDependent = true;
355 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000356 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000357 }
358 }
359
360 Results = static_cast<DeclAccessPair *>(
361 C.Allocate(sizeof(DeclAccessPair) * NumResults,
362 llvm::alignOf<DeclAccessPair>()));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000363 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregora6e053e2010-12-15 01:34:56 +0000364 }
365
366 // If we have explicit template arguments, check for dependent
367 // template arguments and whether they contain any unexpanded pack
368 // expansions.
369 if (TemplateArgs) {
370 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000371 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000372 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +0000373 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
374 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
375 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000376
377 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000378 ExprBits.TypeDependent = true;
379 ExprBits.ValueDependent = true;
380 }
381 if (InstantiationDependent)
382 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000383 if (ContainsUnexpandedParameterPack)
384 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000385 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000386 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000387 }
388
389 if (isTypeDependent())
390 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000391}
392
Craig Toppera31a8822013-08-22 07:09:37 +0000393void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000394 UnresolvedSetIterator Begin,
395 UnresolvedSetIterator End) {
Craig Topper36250ad2014-05-12 05:36:57 +0000396 assert(!Results && "Results already initialized!");
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000397 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000398 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000399 Results = static_cast<DeclAccessPair *>(
400 C.Allocate(sizeof(DeclAccessPair) * NumResults,
401
402 llvm::alignOf<DeclAccessPair>()));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000403 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000404 }
405}
406
John McCall8c12dc42010-04-22 18:44:12 +0000407CXXRecordDecl *OverloadExpr::getNamingClass() const {
408 if (isa<UnresolvedLookupExpr>(this))
409 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
410 else
411 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
412}
413
John McCall8cd78132009-11-19 22:55:06 +0000414// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000415DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000416 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000417 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000418 const DeclarationNameInfo &NameInfo,
419 const TemplateArgumentListInfo *Args)
420 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
421 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000422 (NameInfo.isInstantiationDependent() ||
423 (QualifierLoc &&
424 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000425 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000426 (QualifierLoc &&
427 QualifierLoc.getNestedNameSpecifier()
428 ->containsUnexpandedParameterPack()))),
429 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Craig Topper36250ad2014-05-12 05:36:57 +0000430 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000431{
432 if (Args) {
433 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000434 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000435 bool ContainsUnexpandedParameterPack
436 = ExprBits.ContainsUnexpandedParameterPack;
James Y Knighte7d82282015-12-29 18:15:14 +0000437 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
438 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
439 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000440 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000441 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000442 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
443 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000444 }
445}
446
John McCalle66edc12009-11-24 19:00:30 +0000447DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000448DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000449 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000450 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000451 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000452 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000453 assert(QualifierLoc && "should be created for dependent qualifiers");
James Y Knighte7d82282015-12-29 18:15:14 +0000454 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
455 std::size_t Size =
456 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
457 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
458 void *Mem = C.Allocate(Size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000459 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
460 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000461}
462
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000463DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000464DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000465 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000466 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000467 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
468 std::size_t Size =
469 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
470 HasTemplateKWAndArgsInfo, NumTemplateArgs);
471 void *Mem = C.Allocate(Size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000472 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000473 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000474 SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000475 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000476 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000477 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000478}
479
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000480SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000481 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000482 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
483 return Loc;
484}
485
486SourceLocation CXXConstructExpr::getLocEnd() const {
487 if (isa<CXXTemporaryObjectExpr>(this))
488 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000489
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000490 if (ParenOrBraceRange.isValid())
491 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000492
493 SourceLocation End = Loc;
494 for (unsigned I = getNumArgs(); I > 0; --I) {
495 const Expr *Arg = getArg(I-1);
496 if (!Arg->isDefaultArgument()) {
497 SourceLocation NewEnd = Arg->getLocEnd();
498 if (NewEnd.isValid()) {
499 End = NewEnd;
500 break;
501 }
502 }
503 }
504
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000505 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000506}
507
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000508SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000509 OverloadedOperatorKind Kind = getOperator();
510 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
511 if (getNumArgs() == 1)
512 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000513 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000514 else
515 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000516 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000517 } else if (Kind == OO_Arrow) {
518 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000519 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000520 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000521 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000522 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000523 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000524 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000525 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000526 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000527 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000528 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000529 }
530}
531
Ted Kremenek98a24e32011-03-30 17:41:19 +0000532Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000533 const Expr *Callee = getCallee()->IgnoreParens();
534 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000535 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000536 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
537 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
538 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000539
540 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000541 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000542}
543
Ted Kremenek98a24e32011-03-30 17:41:19 +0000544CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
545 if (const MemberExpr *MemExpr =
546 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
547 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
548
549 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000550 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000551}
552
553
David Blaikiec0f58662012-05-03 16:25:49 +0000554CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000555 Expr* ThisArg = getImplicitObjectArgument();
556 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000557 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000558
559 if (ThisArg->getType()->isAnyPointerType())
560 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
561
562 return ThisArg->getType()->getAsCXXRecordDecl();
563}
564
Douglas Gregoref986e82009-11-12 15:31:47 +0000565
Douglas Gregore200adc2008-10-27 19:41:14 +0000566//===----------------------------------------------------------------------===//
567// Named casts
568//===----------------------------------------------------------------------===//
569
570/// getCastName - Get the name of the C++ cast being used, e.g.,
571/// "static_cast", "dynamic_cast", "reinterpret_cast", or
572/// "const_cast". The returned pointer must not be freed.
573const char *CXXNamedCastExpr::getCastName() const {
574 switch (getStmtClass()) {
575 case CXXStaticCastExprClass: return "static_cast";
576 case CXXDynamicCastExprClass: return "dynamic_cast";
577 case CXXReinterpretCastExprClass: return "reinterpret_cast";
578 case CXXConstCastExprClass: return "const_cast";
579 default: return "<invalid cast>";
580 }
581}
Douglas Gregordd04d332009-01-16 18:33:17 +0000582
Craig Toppera31a8822013-08-22 07:09:37 +0000583CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000584 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000585 CastKind K, Expr *Op,
586 const CXXCastPath *BasePath,
587 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000588 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000589 SourceLocation RParenLoc,
590 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000591 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000592 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000593 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000594 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000595 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000596 if (PathSize)
597 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
598 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000599 return E;
600}
601
Craig Toppera31a8822013-08-22 07:09:37 +0000602CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000603 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000604 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000605 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
606}
607
Craig Toppera31a8822013-08-22 07:09:37 +0000608CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000609 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000610 CastKind K, Expr *Op,
611 const CXXCastPath *BasePath,
612 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000613 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000614 SourceLocation RParenLoc,
615 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000616 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000617 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000618 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000619 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000620 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000621 if (PathSize)
622 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
623 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000624 return E;
625}
626
Craig Toppera31a8822013-08-22 07:09:37 +0000627CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000628 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000629 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000630 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
631}
632
Anders Carlsson267c0c92011-04-11 01:43:55 +0000633/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
634/// to always be null. For example:
635///
636/// struct A { };
637/// struct B final : A { };
638/// struct C { };
639///
640/// C *f(B* b) { return dynamic_cast<C*>(b); }
641bool CXXDynamicCastExpr::isAlwaysNull() const
642{
643 QualType SrcType = getSubExpr()->getType();
644 QualType DestType = getType();
645
646 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
647 SrcType = SrcPTy->getPointeeType();
648 DestType = DestType->castAs<PointerType>()->getPointeeType();
649 }
650
Alexis Hunt78e2b912012-06-19 23:44:55 +0000651 if (DestType->isVoidType())
652 return false;
653
Anders Carlsson267c0c92011-04-11 01:43:55 +0000654 const CXXRecordDecl *SrcRD =
655 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
656
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000657 if (!SrcRD->hasAttr<FinalAttr>())
658 return false;
659
Anders Carlsson267c0c92011-04-11 01:43:55 +0000660 const CXXRecordDecl *DestRD =
661 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
662
663 return !DestRD->isDerivedFrom(SrcRD);
664}
665
John McCallcf142162010-08-07 06:22:56 +0000666CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000667CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
668 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000669 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000670 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000671 SourceLocation RParenLoc,
672 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000673 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000674 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000675 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000676 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000677 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000678 if (PathSize)
679 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
680 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000681 return E;
682}
683
684CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000685CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000686 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000687 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
688}
689
Craig Toppera31a8822013-08-22 07:09:37 +0000690CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000691 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000692 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000693 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000694 SourceLocation RParenLoc,
695 SourceRange AngleBrackets) {
696 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000697}
698
Craig Toppera31a8822013-08-22 07:09:37 +0000699CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000700 return new (C) CXXConstCastExpr(EmptyShell());
701}
702
703CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000704CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000705 TypeSourceInfo *Written, CastKind K, Expr *Op,
706 const CXXCastPath *BasePath,
707 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000708 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000709 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000710 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000711 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000712 if (PathSize)
713 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
714 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000715 return E;
716}
717
718CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000719CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000720 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000721 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
722}
723
Eli Friedman89fe0d52013-08-15 22:02:56 +0000724SourceLocation CXXFunctionalCastExpr::getLocStart() const {
725 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
726}
727
728SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
729 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
730}
731
Richard Smithc67fdd42012-03-07 08:35:16 +0000732UserDefinedLiteral::LiteralOperatorKind
733UserDefinedLiteral::getLiteralOperatorKind() const {
734 if (getNumArgs() == 0)
735 return LOK_Template;
736 if (getNumArgs() == 2)
737 return LOK_String;
738
739 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
740 QualType ParamTy =
741 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
742 if (ParamTy->isPointerType())
743 return LOK_Raw;
744 if (ParamTy->isAnyCharacterType())
745 return LOK_Character;
746 if (ParamTy->isIntegerType())
747 return LOK_Integer;
748 if (ParamTy->isFloatingType())
749 return LOK_Floating;
750
751 llvm_unreachable("unknown kind of literal operator");
752}
753
754Expr *UserDefinedLiteral::getCookedLiteral() {
755#ifndef NDEBUG
756 LiteralOperatorKind LOK = getLiteralOperatorKind();
757 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
758#endif
759 return getArg(0);
760}
761
762const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
763 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
764}
John McCallcf142162010-08-07 06:22:56 +0000765
Craig Toppera31a8822013-08-22 07:09:37 +0000766CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000767 FieldDecl *Field, QualType T)
768 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
769 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
770 ? VK_XValue
771 : VK_RValue,
772 /*FIXME*/ OK_Ordinary, false, false, false, false),
773 Field(Field), Loc(Loc) {
774 assert(Field->hasInClassInitializer());
775}
776
Craig Toppera31a8822013-08-22 07:09:37 +0000777CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000778 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000779 return new (C) CXXTemporary(Destructor);
780}
781
Craig Toppera31a8822013-08-22 07:09:37 +0000782CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000783 CXXTemporary *Temp,
784 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000785 assert((SubExpr->getType()->isRecordType() ||
786 SubExpr->getType()->isArrayType()) &&
787 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000788
Douglas Gregora6e053e2010-12-15 01:34:56 +0000789 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000790}
791
Craig Toppera31a8822013-08-22 07:09:37 +0000792CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000793 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000794 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000795 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000796 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000797 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000798 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000799 bool StdInitListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000800 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000801 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
802 Type->getType().getNonReferenceType(),
803 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000804 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000805 HadMultipleCandidates,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000806 ListInitialization,
807 StdInitListInitialization,
808 ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000809 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000810 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000811}
812
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000813SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
814 return Type->getTypeLoc().getBeginLoc();
815}
816
817SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000818 SourceLocation Loc = getParenOrBraceRange().getEnd();
819 if (Loc.isInvalid() && getNumArgs())
820 Loc = getArg(getNumArgs()-1)->getLocEnd();
821 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000822}
Anders Carlsson6f287832009-04-21 02:22:11 +0000823
Craig Toppera31a8822013-08-22 07:09:37 +0000824CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000825 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000826 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000827 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000828 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000829 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000830 bool StdInitListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000831 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000832 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000833 SourceRange ParenOrBraceRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000834 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000835 Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000836 HadMultipleCandidates, ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000837 StdInitListInitialization,
Sebastian Redla9351792012-02-11 23:51:47 +0000838 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000839 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000840}
841
Craig Toppera31a8822013-08-22 07:09:37 +0000842CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
843 QualType T, SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000844 CXXConstructorDecl *D, bool elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000845 ArrayRef<Expr*> args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000846 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000847 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000848 bool StdInitListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000849 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000850 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000851 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000852 : Expr(SC, T, VK_RValue, OK_Ordinary,
853 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000854 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000855 T->containsUnexpandedParameterPack()),
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000856 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
857 NumArgs(args.size()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000858 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000859 ListInitialization(ListInitialization),
Richard Smithf8adcdc2014-07-17 05:12:35 +0000860 StdInitListInitialization(StdInitListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000861 ZeroInitialization(ZeroInitialization),
Craig Topper36250ad2014-05-12 05:36:57 +0000862 ConstructKind(ConstructKind), Args(nullptr)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000863{
864 if (NumArgs) {
Benjamin Kramerc215e762012-08-24 11:54:20 +0000865 Args = new (C) Stmt*[args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000866
Benjamin Kramerc215e762012-08-24 11:54:20 +0000867 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000868 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000869
870 if (args[i]->isValueDependent())
871 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000872 if (args[i]->isInstantiationDependent())
873 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000874 if (args[i]->containsUnexpandedParameterPack())
875 ExprBits.ContainsUnexpandedParameterPack = true;
876
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000877 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000878 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000879 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000880}
881
Faisal Validc6b5962016-03-21 09:25:37 +0000882LambdaCapture::OpaqueCapturedEntity LambdaCapture::ThisSentinel;
883LambdaCapture::OpaqueCapturedEntity LambdaCapture::VLASentinel;
884
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000885LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000886 LambdaCaptureKind Kind, VarDecl *Var,
887 SourceLocation EllipsisLoc)
Faisal Validc6b5962016-03-21 09:25:37 +0000888 : CapturedEntityAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000889{
890 unsigned Bits = 0;
891 if (Implicit)
892 Bits |= Capture_Implicit;
893
894 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +0000895 case LCK_StarThis:
896 Bits |= Capture_ByCopy;
897 // Fall through
Craig Topper36250ad2014-05-12 05:36:57 +0000898 case LCK_This:
899 assert(!Var && "'this' capture cannot have a variable!");
Faisal Validc6b5962016-03-21 09:25:37 +0000900 CapturedEntityAndBits.setPointer(&ThisSentinel);
Douglas Gregore31e6062012-02-07 10:09:13 +0000901 break;
902
903 case LCK_ByCopy:
904 Bits |= Capture_ByCopy;
905 // Fall through
906 case LCK_ByRef:
907 assert(Var && "capture must have a variable!");
908 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000909 case LCK_VLAType:
910 assert(!Var && "VLA type capture cannot have a variable!");
Faisal Validc6b5962016-03-21 09:25:37 +0000911 CapturedEntityAndBits.setPointer(&VLASentinel);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000912 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000913 }
Faisal Validc6b5962016-03-21 09:25:37 +0000914 CapturedEntityAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000915}
916
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000917LambdaCaptureKind LambdaCapture::getCaptureKind() const {
Faisal Validc6b5962016-03-21 09:25:37 +0000918 void *Ptr = CapturedEntityAndBits.getPointer();
919 if (Ptr == &VLASentinel)
920 return LCK_VLAType;
921 const unsigned Bits = CapturedEntityAndBits.getInt();
922 bool CapByCopy = Bits & Capture_ByCopy;
923 if (Ptr == &ThisSentinel)
924 return CapByCopy ? LCK_StarThis : LCK_This;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000925 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000926}
927
James Y Knighte00a67e2015-12-31 04:18:25 +0000928LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
Douglas Gregore31e6062012-02-07 10:09:13 +0000929 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000930 SourceLocation CaptureDefaultLoc,
James Y Knighte00a67e2015-12-31 04:18:25 +0000931 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
932 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000933 ArrayRef<VarDecl *> ArrayIndexVars,
934 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000935 SourceLocation ClosingBrace,
936 bool ContainsUnexpandedParameterPack)
James Y Knighte00a67e2015-12-31 04:18:25 +0000937 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
938 T->isDependentType(), T->isDependentType(),
939 ContainsUnexpandedParameterPack),
940 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
941 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
942 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
943 ClosingBrace(ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000944 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000945 CXXRecordDecl *Class = getLambdaClass();
946 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000947
948 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000949
950 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000951 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000952 Data.NumCaptures = NumCaptures;
953 Data.NumExplicitCaptures = 0;
James Y Knighte00a67e2015-12-31 04:18:25 +0000954 Data.Captures =
955 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
956 LambdaCapture *ToCapture = Data.Captures;
Douglas Gregore5561632012-02-13 17:20:40 +0000957 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
958 if (Captures[I].isExplicit())
959 ++Data.NumExplicitCaptures;
960
961 *ToCapture++ = Captures[I];
962 }
963
964 // Copy initialization expressions for the non-static data members.
965 Stmt **Stored = getStoredStmts();
966 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
967 *Stored++ = CaptureInits[I];
968
969 // Copy the body of the lambda.
970 *Stored++ = getCallOperator()->getBody();
971
972 // Copy the array index variables, if any.
973 HasArrayIndexVars = !ArrayIndexVars.empty();
974 if (HasArrayIndexVars) {
975 assert(ArrayIndexStarts.size() == NumCaptures);
976 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
977 sizeof(VarDecl *) * ArrayIndexVars.size());
978 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
979 sizeof(unsigned) * Captures.size());
980 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000981 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000982}
983
James Y Knighte00a67e2015-12-31 04:18:25 +0000984LambdaExpr *LambdaExpr::Create(
985 const ASTContext &Context, CXXRecordDecl *Class,
986 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
987 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
988 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
989 ArrayRef<VarDecl *> ArrayIndexVars, ArrayRef<unsigned> ArrayIndexStarts,
990 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000991 // Determine the type of the expression (i.e., the type of the
992 // function object we're creating).
993 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000994
James Y Knighte00a67e2015-12-31 04:18:25 +0000995 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
996 Captures.size() + 1, ArrayIndexVars.empty() ? 0 : Captures.size() + 1,
997 ArrayIndexVars.size());
Douglas Gregore5561632012-02-13 17:20:40 +0000998 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +0000999 return new (Mem) LambdaExpr(T, IntroducerRange,
1000 CaptureDefault, CaptureDefaultLoc, Captures,
1001 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001002 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001003 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +00001004}
1005
Craig Toppera31a8822013-08-22 07:09:37 +00001006LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1007 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +00001008 unsigned NumArrayIndexVars) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001009 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
1010 NumCaptures + 1, NumArrayIndexVars ? NumCaptures + 1 : 0,
1011 NumArrayIndexVars);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001012 void *Mem = C.Allocate(Size);
1013 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1014}
1015
James Dennettdd2ffea22015-05-07 18:48:18 +00001016bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1017 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1018 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
1019}
1020
Douglas Gregorc8a73492012-02-13 15:44:47 +00001021LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001022 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001023}
1024
1025LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001026 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001027}
1028
James Dennett1575cb42014-05-27 19:13:04 +00001029LambdaExpr::capture_range LambdaExpr::captures() const {
1030 return capture_range(capture_begin(), capture_end());
1031}
1032
Douglas Gregorc8a73492012-02-13 15:44:47 +00001033LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1034 return capture_begin();
1035}
1036
1037LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1038 struct CXXRecordDecl::LambdaDefinitionData &Data
1039 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001040 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001041}
1042
James Dennett1575cb42014-05-27 19:13:04 +00001043LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1044 return capture_range(explicit_capture_begin(), explicit_capture_end());
1045}
1046
Douglas Gregorc8a73492012-02-13 15:44:47 +00001047LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1048 return explicit_capture_end();
1049}
1050
1051LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1052 return capture_end();
1053}
1054
James Dennett1575cb42014-05-27 19:13:04 +00001055LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1056 return capture_range(implicit_capture_begin(), implicit_capture_end());
1057}
1058
James Y Knight53c76162015-07-17 18:21:37 +00001059ArrayRef<VarDecl *>
1060LambdaExpr::getCaptureInitIndexVars(const_capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001061 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001062
1063 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001064 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1065 "Capture index out-of-range");
James Y Knight53c76162015-07-17 18:21:37 +00001066 VarDecl *const *IndexVars = getArrayIndexVars();
1067 const unsigned *IndexStarts = getArrayIndexStarts();
Craig Topper5fc8fc22014-08-27 06:28:36 +00001068 return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
1069 IndexVars + IndexStarts[Index + 1]);
Douglas Gregor54fcea62012-02-13 16:35:30 +00001070}
1071
Douglas Gregore31e6062012-02-07 10:09:13 +00001072CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1073 return getType()->getAsCXXRecordDecl();
1074}
1075
1076CXXMethodDecl *LambdaExpr::getCallOperator() const {
1077 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001078 return Record->getLambdaCallOperator();
1079}
1080
1081TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1082 CXXRecordDecl *Record = getLambdaClass();
1083 return Record->getGenericLambdaTemplateParameterList();
1084
Douglas Gregore31e6062012-02-07 10:09:13 +00001085}
1086
Douglas Gregor99ae8062012-02-14 17:54:36 +00001087CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +00001088 // FIXME: this mutation in getBody is bogus. It should be
1089 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1090 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +00001091 if (!getStoredStmts()[NumCaptures])
James Y Knight53c76162015-07-17 18:21:37 +00001092 *const_cast<clang::Stmt **>(&getStoredStmts()[NumCaptures]) =
1093 getCallOperator()->getBody();
1094
James Y Knighte00a67e2015-12-31 04:18:25 +00001095 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001096}
1097
Douglas Gregore31e6062012-02-07 10:09:13 +00001098bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001099 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001100}
1101
John McCall28fc7092011-11-10 05:35:25 +00001102ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1103 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001104 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001105 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001106 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001107 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001108 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001109 SubExpr(subexpr) {
1110 ExprWithCleanupsBits.NumObjects = objects.size();
1111 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001112 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001113}
1114
Craig Toppera31a8822013-08-22 07:09:37 +00001115ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001116 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001117 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
1118 llvm::alignOf<ExprWithCleanups>());
John McCall28fc7092011-11-10 05:35:25 +00001119 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001120}
1121
John McCall28fc7092011-11-10 05:35:25 +00001122ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1123 : Expr(ExprWithCleanupsClass, empty) {
1124 ExprWithCleanupsBits.NumObjects = numObjects;
1125}
Chris Lattnercba86142010-05-10 00:25:06 +00001126
Craig Toppera31a8822013-08-22 07:09:37 +00001127ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1128 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001129 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001130 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
1131 llvm::alignOf<ExprWithCleanups>());
John McCall28fc7092011-11-10 05:35:25 +00001132 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001133}
1134
Douglas Gregor2b88c112010-09-08 00:15:04 +00001135CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001136 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001137 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001138 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001139 : Expr(CXXUnresolvedConstructExprClass,
1140 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001141 (Type->getType()->isLValueReferenceType() ? VK_LValue
1142 :Type->getType()->isRValueReferenceType()? VK_XValue
1143 :VK_RValue),
1144 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001145 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001146 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001147 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001148 LParenLoc(LParenLoc),
1149 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001150 NumArgs(Args.size()) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001151 Expr **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001152 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001153 if (Args[I]->containsUnexpandedParameterPack())
1154 ExprBits.ContainsUnexpandedParameterPack = true;
1155
1156 StoredArgs[I] = Args[I];
1157 }
Douglas Gregorce934142009-05-20 18:46:25 +00001158}
1159
1160CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001161CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001162 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001163 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001164 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001165 SourceLocation RParenLoc) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001166 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
Benjamin Kramerc215e762012-08-24 11:54:20 +00001167 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001168}
1169
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001170CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001171CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001172 Stmt::EmptyShell Empty;
James Y Knighte00a67e2015-12-31 04:18:25 +00001173 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001174 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1175}
1176
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001177SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1178 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001179}
1180
James Y Knighte7d82282015-12-29 18:15:14 +00001181CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1182 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1183 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1184 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1185 DeclarationNameInfo MemberNameInfo,
1186 const TemplateArgumentListInfo *TemplateArgs)
1187 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1188 OK_Ordinary, true, true, true,
1189 ((Base && Base->containsUnexpandedParameterPack()) ||
1190 (QualifierLoc &&
1191 QualifierLoc.getNestedNameSpecifier()
1192 ->containsUnexpandedParameterPack()) ||
1193 MemberNameInfo.containsUnexpandedParameterPack())),
1194 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1195 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1196 TemplateKWLoc.isValid()),
1197 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1198 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1199 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001200 if (TemplateArgs) {
1201 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001202 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001203 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001204 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1205 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1206 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001207 if (ContainsUnexpandedParameterPack)
1208 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001209 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001210 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1211 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001212 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001213}
1214
John McCall8cd78132009-11-19 22:55:06 +00001215CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001216CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001217 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001218 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001219 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001220 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001221 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001222 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001223 const TemplateArgumentListInfo *TemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001224 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001225 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +00001226 std::size_t Size =
1227 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1228 HasTemplateKWAndArgsInfo, NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001229
James Y Knighte7d82282015-12-29 18:15:14 +00001230 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001231 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1232 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001233 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001234 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001235 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001236 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001237}
1238
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001239CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001240CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001241 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001242 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001243 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1244 std::size_t Size =
1245 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1246 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1247 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001248 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001249 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001250 0, SourceLocation(),
1251 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001252 SourceLocation(), nullptr,
1253 DeclarationNameInfo(), nullptr);
James Y Knighte7d82282015-12-29 18:15:14 +00001254 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001255 return E;
1256}
1257
Douglas Gregor0da1d432011-02-28 20:01:57 +00001258bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001259 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001260 return true;
1261
Douglas Gregor25b7e052011-03-02 21:06:53 +00001262 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001263}
1264
John McCall0009fcc2011-04-26 20:42:42 +00001265static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1266 UnresolvedSetIterator end) {
1267 do {
1268 NamedDecl *decl = *begin;
1269 if (isa<UnresolvedUsingValueDecl>(decl))
1270 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001271
1272 // Unresolved member expressions should only contain methods and
1273 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001274 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1275 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001276 return false;
1277 } while (++begin != end);
1278
1279 return true;
1280}
1281
Craig Toppera31a8822013-08-22 07:09:37 +00001282UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001283 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001284 Expr *Base, QualType BaseType,
1285 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001286 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001287 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001288 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001289 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001290 const TemplateArgumentListInfo *TemplateArgs,
1291 UnresolvedSetIterator Begin,
1292 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001293 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1294 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001295 // Dependent
1296 ((Base && Base->isTypeDependent()) ||
1297 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001298 ((Base && Base->isInstantiationDependent()) ||
1299 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001300 // Contains unexpanded parameter pack
1301 ((Base && Base->containsUnexpandedParameterPack()) ||
1302 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001303 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1304 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001305
1306 // Check whether all of the members are non-static member functions,
1307 // and if so, mark give this bound-member type instead of overload type.
1308 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1309 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001310}
1311
Douglas Gregor0da1d432011-02-28 20:01:57 +00001312bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001313 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001314 return true;
1315
Douglas Gregor25b7e052011-03-02 21:06:53 +00001316 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001317}
1318
James Y Knighte7d82282015-12-29 18:15:14 +00001319UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1320 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1321 bool IsArrow, SourceLocation OperatorLoc,
1322 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1323 const DeclarationNameInfo &MemberNameInfo,
1324 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1325 UnresolvedSetIterator End) {
1326 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1327 std::size_t Size =
1328 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1329 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
John McCall10eae182009-11-30 22:42:35 +00001330
James Y Knighte7d82282015-12-29 18:15:14 +00001331 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
1332 return new (Mem) UnresolvedMemberExpr(
1333 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1334 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001335}
1336
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001337UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001338UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1339 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001340 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001341 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1342 std::size_t Size =
1343 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1344 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001345
James Y Knighte7d82282015-12-29 18:15:14 +00001346 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001347 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001348 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001349 return E;
1350}
1351
John McCall58cc69d2010-01-27 01:50:18 +00001352CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1353 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1354
1355 // If there was a nested name specifier, it names the naming class.
1356 // It can't be dependent: after all, we were actually able to do the
1357 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001358 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001359 auto *NNS = getQualifier();
1360 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001361 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001362 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001363 Record = T->getAsCXXRecordDecl();
1364 assert(Record && "qualifier in member expression does not name record");
1365 }
John McCall58cc69d2010-01-27 01:50:18 +00001366 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001367 else {
John McCall58cc69d2010-01-27 01:50:18 +00001368 QualType BaseType = getBaseType().getNonReferenceType();
1369 if (isArrow()) {
1370 const PointerType *PT = BaseType->getAs<PointerType>();
1371 assert(PT && "base of arrow member access is not pointer");
1372 BaseType = PT->getPointeeType();
1373 }
1374
Douglas Gregor9262f472010-04-27 18:19:34 +00001375 Record = BaseType->getAsCXXRecordDecl();
1376 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001377 }
1378
Douglas Gregor9262f472010-04-27 18:19:34 +00001379 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001380}
1381
Richard Smithd784e682015-09-23 21:41:42 +00001382SizeOfPackExpr *
1383SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1384 NamedDecl *Pack, SourceLocation PackLoc,
1385 SourceLocation RParenLoc,
1386 Optional<unsigned> Length,
1387 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001388 void *Storage =
1389 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001390 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1391 PackLoc, RParenLoc, Length, PartialArgs);
1392}
1393
1394SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1395 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001396 void *Storage =
1397 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001398 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1399}
1400
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001401SubstNonTypeTemplateParmPackExpr::
1402SubstNonTypeTemplateParmPackExpr(QualType T,
1403 NonTypeTemplateParmDecl *Param,
1404 SourceLocation NameLoc,
1405 const TemplateArgument &ArgPack)
1406 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001407 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001408 Param(Param), Arguments(ArgPack.pack_begin()),
1409 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1410
1411TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001412 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001413}
1414
Richard Smithb15fe3a2012-09-12 00:56:43 +00001415FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1416 SourceLocation NameLoc,
1417 unsigned NumParams,
James Y Knight48fefa32015-09-30 14:04:23 +00001418 ParmVarDecl *const *Params)
1419 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1420 true, true),
1421 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001422 if (Params)
1423 std::uninitialized_copy(Params, Params + NumParams,
James Y Knighte00a67e2015-12-31 04:18:25 +00001424 getTrailingObjects<ParmVarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001425}
1426
1427FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001428FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001429 ParmVarDecl *ParamPack, SourceLocation NameLoc,
James Y Knight48fefa32015-09-30 14:04:23 +00001430 ArrayRef<ParmVarDecl *> Params) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001431 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1432 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001433}
1434
1435FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001436FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1437 unsigned NumParams) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001438 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1439 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001440}
1441
David Majnemerdaff3702014-05-01 17:50:17 +00001442void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1443 unsigned ManglingNumber) {
1444 // We only need extra state if we have to remember more than just the Stmt.
1445 if (!ExtendedBy)
1446 return;
1447
1448 // We may need to allocate extra storage for the mangling number and the
1449 // extended-by ValueDecl.
1450 if (!State.is<ExtraState *>()) {
1451 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1452 ES->Temporary = State.get<Stmt *>();
1453 State = ES;
1454 }
1455
1456 auto ES = State.get<ExtraState *>();
1457 ES->ExtendingDecl = ExtendedBy;
1458 ES->ManglingNumber = ManglingNumber;
1459}
1460
Douglas Gregor29c42f22012-02-24 07:38:34 +00001461TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1462 ArrayRef<TypeSourceInfo *> Args,
1463 SourceLocation RParenLoc,
1464 bool Value)
1465 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1466 /*TypeDependent=*/false,
1467 /*ValueDependent=*/false,
1468 /*InstantiationDependent=*/false,
1469 /*ContainsUnexpandedParameterPack=*/false),
1470 Loc(Loc), RParenLoc(RParenLoc)
1471{
1472 TypeTraitExprBits.Kind = Kind;
1473 TypeTraitExprBits.Value = Value;
1474 TypeTraitExprBits.NumArgs = Args.size();
1475
James Y Knighte00a67e2015-12-31 04:18:25 +00001476 TypeSourceInfo **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1477
Douglas Gregor29c42f22012-02-24 07:38:34 +00001478 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1479 if (Args[I]->getType()->isDependentType())
1480 setValueDependent(true);
1481 if (Args[I]->getType()->isInstantiationDependentType())
1482 setInstantiationDependent(true);
1483 if (Args[I]->getType()->containsUnexpandedParameterPack())
1484 setContainsUnexpandedParameterPack(true);
1485
1486 ToArgs[I] = Args[I];
1487 }
1488}
1489
Craig Toppera31a8822013-08-22 07:09:37 +00001490TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001491 SourceLocation Loc,
1492 TypeTrait Kind,
1493 ArrayRef<TypeSourceInfo *> Args,
1494 SourceLocation RParenLoc,
1495 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001496 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001497 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1498}
1499
Craig Toppera31a8822013-08-22 07:09:37 +00001500TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001501 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001502 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001503 return new (Mem) TypeTraitExpr(EmptyShell());
1504}
1505
David Blaikie68e081d2011-12-20 02:48:34 +00001506void ArrayTypeTraitExpr::anchor() { }