blob: ea983340a293c5659ca1c7d94ef738433f1176ed [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
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000882LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000883 LambdaCaptureKind Kind, VarDecl *Var,
884 SourceLocation EllipsisLoc)
Richard Smithba71c082013-05-16 06:20:58 +0000885 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000886{
887 unsigned Bits = 0;
888 if (Implicit)
889 Bits |= Capture_Implicit;
890
891 switch (Kind) {
Craig Topper36250ad2014-05-12 05:36:57 +0000892 case LCK_This:
893 assert(!Var && "'this' capture cannot have a variable!");
Douglas Gregore31e6062012-02-07 10:09:13 +0000894 break;
895
896 case LCK_ByCopy:
897 Bits |= Capture_ByCopy;
898 // Fall through
899 case LCK_ByRef:
900 assert(Var && "capture must have a variable!");
901 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000902 case LCK_VLAType:
903 assert(!Var && "VLA type capture cannot have a variable!");
904 Bits |= Capture_ByCopy;
905 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000906 }
Richard Smithba71c082013-05-16 06:20:58 +0000907 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000908}
909
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000910LambdaCaptureKind LambdaCapture::getCaptureKind() const {
Richard Smithba71c082013-05-16 06:20:58 +0000911 Decl *D = DeclAndBits.getPointer();
Alexey Bataev39c81e22014-08-28 04:28:19 +0000912 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
Richard Smithba71c082013-05-16 06:20:58 +0000913 if (!D)
Alexey Bataev39c81e22014-08-28 04:28:19 +0000914 return CapByCopy ? LCK_VLAType : LCK_This;
Douglas Gregore31e6062012-02-07 10:09:13 +0000915
Alexey Bataev39c81e22014-08-28 04:28:19 +0000916 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000917}
918
James Y Knighte00a67e2015-12-31 04:18:25 +0000919LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
Douglas Gregore31e6062012-02-07 10:09:13 +0000920 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000921 SourceLocation CaptureDefaultLoc,
James Y Knighte00a67e2015-12-31 04:18:25 +0000922 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
923 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000924 ArrayRef<VarDecl *> ArrayIndexVars,
925 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000926 SourceLocation ClosingBrace,
927 bool ContainsUnexpandedParameterPack)
James Y Knighte00a67e2015-12-31 04:18:25 +0000928 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
929 T->isDependentType(), T->isDependentType(),
930 ContainsUnexpandedParameterPack),
931 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
932 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
933 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
934 ClosingBrace(ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000935 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000936 CXXRecordDecl *Class = getLambdaClass();
937 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000938
939 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000940
941 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000942 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000943 Data.NumCaptures = NumCaptures;
944 Data.NumExplicitCaptures = 0;
James Y Knighte00a67e2015-12-31 04:18:25 +0000945 Data.Captures =
946 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
947 LambdaCapture *ToCapture = Data.Captures;
Douglas Gregore5561632012-02-13 17:20:40 +0000948 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
949 if (Captures[I].isExplicit())
950 ++Data.NumExplicitCaptures;
951
952 *ToCapture++ = Captures[I];
953 }
954
955 // Copy initialization expressions for the non-static data members.
956 Stmt **Stored = getStoredStmts();
957 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
958 *Stored++ = CaptureInits[I];
959
960 // Copy the body of the lambda.
961 *Stored++ = getCallOperator()->getBody();
962
963 // Copy the array index variables, if any.
964 HasArrayIndexVars = !ArrayIndexVars.empty();
965 if (HasArrayIndexVars) {
966 assert(ArrayIndexStarts.size() == NumCaptures);
967 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
968 sizeof(VarDecl *) * ArrayIndexVars.size());
969 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
970 sizeof(unsigned) * Captures.size());
971 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000972 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000973}
974
James Y Knighte00a67e2015-12-31 04:18:25 +0000975LambdaExpr *LambdaExpr::Create(
976 const ASTContext &Context, CXXRecordDecl *Class,
977 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
978 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
979 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
980 ArrayRef<VarDecl *> ArrayIndexVars, ArrayRef<unsigned> ArrayIndexStarts,
981 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000982 // Determine the type of the expression (i.e., the type of the
983 // function object we're creating).
984 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000985
James Y Knighte00a67e2015-12-31 04:18:25 +0000986 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
987 Captures.size() + 1, ArrayIndexVars.empty() ? 0 : Captures.size() + 1,
988 ArrayIndexVars.size());
Douglas Gregore5561632012-02-13 17:20:40 +0000989 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +0000990 return new (Mem) LambdaExpr(T, IntroducerRange,
991 CaptureDefault, CaptureDefaultLoc, Captures,
992 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000993 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000994 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +0000995}
996
Craig Toppera31a8822013-08-22 07:09:37 +0000997LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
998 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +0000999 unsigned NumArrayIndexVars) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001000 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
1001 NumCaptures + 1, NumArrayIndexVars ? NumCaptures + 1 : 0,
1002 NumArrayIndexVars);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001003 void *Mem = C.Allocate(Size);
1004 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1005}
1006
James Dennettdd2ffea22015-05-07 18:48:18 +00001007bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1008 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1009 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
1010}
1011
Douglas Gregorc8a73492012-02-13 15:44:47 +00001012LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001013 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001014}
1015
1016LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001017 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001018}
1019
James Dennett1575cb42014-05-27 19:13:04 +00001020LambdaExpr::capture_range LambdaExpr::captures() const {
1021 return capture_range(capture_begin(), capture_end());
1022}
1023
Douglas Gregorc8a73492012-02-13 15:44:47 +00001024LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1025 return capture_begin();
1026}
1027
1028LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1029 struct CXXRecordDecl::LambdaDefinitionData &Data
1030 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001031 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001032}
1033
James Dennett1575cb42014-05-27 19:13:04 +00001034LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1035 return capture_range(explicit_capture_begin(), explicit_capture_end());
1036}
1037
Douglas Gregorc8a73492012-02-13 15:44:47 +00001038LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1039 return explicit_capture_end();
1040}
1041
1042LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1043 return capture_end();
1044}
1045
James Dennett1575cb42014-05-27 19:13:04 +00001046LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1047 return capture_range(implicit_capture_begin(), implicit_capture_end());
1048}
1049
James Y Knight53c76162015-07-17 18:21:37 +00001050ArrayRef<VarDecl *>
1051LambdaExpr::getCaptureInitIndexVars(const_capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001052 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001053
1054 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001055 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1056 "Capture index out-of-range");
James Y Knight53c76162015-07-17 18:21:37 +00001057 VarDecl *const *IndexVars = getArrayIndexVars();
1058 const unsigned *IndexStarts = getArrayIndexStarts();
Craig Topper5fc8fc22014-08-27 06:28:36 +00001059 return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
1060 IndexVars + IndexStarts[Index + 1]);
Douglas Gregor54fcea62012-02-13 16:35:30 +00001061}
1062
Douglas Gregore31e6062012-02-07 10:09:13 +00001063CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1064 return getType()->getAsCXXRecordDecl();
1065}
1066
1067CXXMethodDecl *LambdaExpr::getCallOperator() const {
1068 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001069 return Record->getLambdaCallOperator();
1070}
1071
1072TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1073 CXXRecordDecl *Record = getLambdaClass();
1074 return Record->getGenericLambdaTemplateParameterList();
1075
Douglas Gregore31e6062012-02-07 10:09:13 +00001076}
1077
Douglas Gregor99ae8062012-02-14 17:54:36 +00001078CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +00001079 // FIXME: this mutation in getBody is bogus. It should be
1080 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1081 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +00001082 if (!getStoredStmts()[NumCaptures])
James Y Knight53c76162015-07-17 18:21:37 +00001083 *const_cast<clang::Stmt **>(&getStoredStmts()[NumCaptures]) =
1084 getCallOperator()->getBody();
1085
James Y Knighte00a67e2015-12-31 04:18:25 +00001086 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001087}
1088
Douglas Gregore31e6062012-02-07 10:09:13 +00001089bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001090 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001091}
1092
John McCall28fc7092011-11-10 05:35:25 +00001093ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1094 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001095 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001096 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001097 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001098 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001099 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001100 SubExpr(subexpr) {
1101 ExprWithCleanupsBits.NumObjects = objects.size();
1102 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001103 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001104}
1105
Craig Toppera31a8822013-08-22 07:09:37 +00001106ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001107 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001108 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
1109 llvm::alignOf<ExprWithCleanups>());
John McCall28fc7092011-11-10 05:35:25 +00001110 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001111}
1112
John McCall28fc7092011-11-10 05:35:25 +00001113ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1114 : Expr(ExprWithCleanupsClass, empty) {
1115 ExprWithCleanupsBits.NumObjects = numObjects;
1116}
Chris Lattnercba86142010-05-10 00:25:06 +00001117
Craig Toppera31a8822013-08-22 07:09:37 +00001118ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1119 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001120 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001121 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
1122 llvm::alignOf<ExprWithCleanups>());
John McCall28fc7092011-11-10 05:35:25 +00001123 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001124}
1125
Douglas Gregor2b88c112010-09-08 00:15:04 +00001126CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001127 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001128 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001129 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001130 : Expr(CXXUnresolvedConstructExprClass,
1131 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001132 (Type->getType()->isLValueReferenceType() ? VK_LValue
1133 :Type->getType()->isRValueReferenceType()? VK_XValue
1134 :VK_RValue),
1135 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001136 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001137 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001138 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001139 LParenLoc(LParenLoc),
1140 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001141 NumArgs(Args.size()) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001142 Expr **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001143 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001144 if (Args[I]->containsUnexpandedParameterPack())
1145 ExprBits.ContainsUnexpandedParameterPack = true;
1146
1147 StoredArgs[I] = Args[I];
1148 }
Douglas Gregorce934142009-05-20 18:46:25 +00001149}
1150
1151CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001152CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001153 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001154 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001155 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001156 SourceLocation RParenLoc) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001157 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
Benjamin Kramerc215e762012-08-24 11:54:20 +00001158 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001159}
1160
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001161CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001162CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001163 Stmt::EmptyShell Empty;
James Y Knighte00a67e2015-12-31 04:18:25 +00001164 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001165 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1166}
1167
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001168SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1169 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001170}
1171
James Y Knighte7d82282015-12-29 18:15:14 +00001172CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1173 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1174 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1175 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1176 DeclarationNameInfo MemberNameInfo,
1177 const TemplateArgumentListInfo *TemplateArgs)
1178 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1179 OK_Ordinary, true, true, true,
1180 ((Base && Base->containsUnexpandedParameterPack()) ||
1181 (QualifierLoc &&
1182 QualifierLoc.getNestedNameSpecifier()
1183 ->containsUnexpandedParameterPack()) ||
1184 MemberNameInfo.containsUnexpandedParameterPack())),
1185 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1186 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1187 TemplateKWLoc.isValid()),
1188 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1189 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1190 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001191 if (TemplateArgs) {
1192 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001193 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001194 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001195 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1196 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1197 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001198 if (ContainsUnexpandedParameterPack)
1199 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001200 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001201 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1202 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001203 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001204}
1205
John McCall8cd78132009-11-19 22:55:06 +00001206CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001207CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001208 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001209 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001210 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001211 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001212 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001213 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001214 const TemplateArgumentListInfo *TemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001215 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001216 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +00001217 std::size_t Size =
1218 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1219 HasTemplateKWAndArgsInfo, NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001220
James Y Knighte7d82282015-12-29 18:15:14 +00001221 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001222 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1223 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001224 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001225 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001226 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001227 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001228}
1229
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001230CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001231CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001232 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001233 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001234 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1235 std::size_t Size =
1236 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1237 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1238 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001239 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001240 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001241 0, SourceLocation(),
1242 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001243 SourceLocation(), nullptr,
1244 DeclarationNameInfo(), nullptr);
James Y Knighte7d82282015-12-29 18:15:14 +00001245 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001246 return E;
1247}
1248
Douglas Gregor0da1d432011-02-28 20:01:57 +00001249bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001250 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001251 return true;
1252
Douglas Gregor25b7e052011-03-02 21:06:53 +00001253 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001254}
1255
John McCall0009fcc2011-04-26 20:42:42 +00001256static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1257 UnresolvedSetIterator end) {
1258 do {
1259 NamedDecl *decl = *begin;
1260 if (isa<UnresolvedUsingValueDecl>(decl))
1261 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001262
1263 // Unresolved member expressions should only contain methods and
1264 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001265 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1266 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001267 return false;
1268 } while (++begin != end);
1269
1270 return true;
1271}
1272
Craig Toppera31a8822013-08-22 07:09:37 +00001273UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001274 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001275 Expr *Base, QualType BaseType,
1276 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001277 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001278 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001279 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001280 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001281 const TemplateArgumentListInfo *TemplateArgs,
1282 UnresolvedSetIterator Begin,
1283 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001284 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1285 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001286 // Dependent
1287 ((Base && Base->isTypeDependent()) ||
1288 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001289 ((Base && Base->isInstantiationDependent()) ||
1290 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001291 // Contains unexpanded parameter pack
1292 ((Base && Base->containsUnexpandedParameterPack()) ||
1293 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001294 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1295 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001296
1297 // Check whether all of the members are non-static member functions,
1298 // and if so, mark give this bound-member type instead of overload type.
1299 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1300 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001301}
1302
Douglas Gregor0da1d432011-02-28 20:01:57 +00001303bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001304 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001305 return true;
1306
Douglas Gregor25b7e052011-03-02 21:06:53 +00001307 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001308}
1309
James Y Knighte7d82282015-12-29 18:15:14 +00001310UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1311 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1312 bool IsArrow, SourceLocation OperatorLoc,
1313 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1314 const DeclarationNameInfo &MemberNameInfo,
1315 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1316 UnresolvedSetIterator End) {
1317 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1318 std::size_t Size =
1319 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1320 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
John McCall10eae182009-11-30 22:42:35 +00001321
James Y Knighte7d82282015-12-29 18:15:14 +00001322 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
1323 return new (Mem) UnresolvedMemberExpr(
1324 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1325 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001326}
1327
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001328UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001329UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1330 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001331 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001332 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1333 std::size_t Size =
1334 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1335 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001336
James Y Knighte7d82282015-12-29 18:15:14 +00001337 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001338 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001339 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001340 return E;
1341}
1342
John McCall58cc69d2010-01-27 01:50:18 +00001343CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1344 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1345
1346 // If there was a nested name specifier, it names the naming class.
1347 // It can't be dependent: after all, we were actually able to do the
1348 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001349 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001350 auto *NNS = getQualifier();
1351 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001352 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001353 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001354 Record = T->getAsCXXRecordDecl();
1355 assert(Record && "qualifier in member expression does not name record");
1356 }
John McCall58cc69d2010-01-27 01:50:18 +00001357 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001358 else {
John McCall58cc69d2010-01-27 01:50:18 +00001359 QualType BaseType = getBaseType().getNonReferenceType();
1360 if (isArrow()) {
1361 const PointerType *PT = BaseType->getAs<PointerType>();
1362 assert(PT && "base of arrow member access is not pointer");
1363 BaseType = PT->getPointeeType();
1364 }
1365
Douglas Gregor9262f472010-04-27 18:19:34 +00001366 Record = BaseType->getAsCXXRecordDecl();
1367 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001368 }
1369
Douglas Gregor9262f472010-04-27 18:19:34 +00001370 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001371}
1372
Richard Smithd784e682015-09-23 21:41:42 +00001373SizeOfPackExpr *
1374SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1375 NamedDecl *Pack, SourceLocation PackLoc,
1376 SourceLocation RParenLoc,
1377 Optional<unsigned> Length,
1378 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001379 void *Storage =
1380 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001381 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1382 PackLoc, RParenLoc, Length, PartialArgs);
1383}
1384
1385SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1386 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001387 void *Storage =
1388 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001389 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1390}
1391
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001392SubstNonTypeTemplateParmPackExpr::
1393SubstNonTypeTemplateParmPackExpr(QualType T,
1394 NonTypeTemplateParmDecl *Param,
1395 SourceLocation NameLoc,
1396 const TemplateArgument &ArgPack)
1397 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001398 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001399 Param(Param), Arguments(ArgPack.pack_begin()),
1400 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1401
1402TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001403 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001404}
1405
Richard Smithb15fe3a2012-09-12 00:56:43 +00001406FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1407 SourceLocation NameLoc,
1408 unsigned NumParams,
James Y Knight48fefa32015-09-30 14:04:23 +00001409 ParmVarDecl *const *Params)
1410 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1411 true, true),
1412 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001413 if (Params)
1414 std::uninitialized_copy(Params, Params + NumParams,
James Y Knighte00a67e2015-12-31 04:18:25 +00001415 getTrailingObjects<ParmVarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001416}
1417
1418FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001419FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001420 ParmVarDecl *ParamPack, SourceLocation NameLoc,
James Y Knight48fefa32015-09-30 14:04:23 +00001421 ArrayRef<ParmVarDecl *> Params) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001422 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1423 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001424}
1425
1426FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001427FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1428 unsigned NumParams) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001429 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1430 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001431}
1432
David Majnemerdaff3702014-05-01 17:50:17 +00001433void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1434 unsigned ManglingNumber) {
1435 // We only need extra state if we have to remember more than just the Stmt.
1436 if (!ExtendedBy)
1437 return;
1438
1439 // We may need to allocate extra storage for the mangling number and the
1440 // extended-by ValueDecl.
1441 if (!State.is<ExtraState *>()) {
1442 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1443 ES->Temporary = State.get<Stmt *>();
1444 State = ES;
1445 }
1446
1447 auto ES = State.get<ExtraState *>();
1448 ES->ExtendingDecl = ExtendedBy;
1449 ES->ManglingNumber = ManglingNumber;
1450}
1451
Douglas Gregor29c42f22012-02-24 07:38:34 +00001452TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1453 ArrayRef<TypeSourceInfo *> Args,
1454 SourceLocation RParenLoc,
1455 bool Value)
1456 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1457 /*TypeDependent=*/false,
1458 /*ValueDependent=*/false,
1459 /*InstantiationDependent=*/false,
1460 /*ContainsUnexpandedParameterPack=*/false),
1461 Loc(Loc), RParenLoc(RParenLoc)
1462{
1463 TypeTraitExprBits.Kind = Kind;
1464 TypeTraitExprBits.Value = Value;
1465 TypeTraitExprBits.NumArgs = Args.size();
1466
James Y Knighte00a67e2015-12-31 04:18:25 +00001467 TypeSourceInfo **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1468
Douglas Gregor29c42f22012-02-24 07:38:34 +00001469 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1470 if (Args[I]->getType()->isDependentType())
1471 setValueDependent(true);
1472 if (Args[I]->getType()->isInstantiationDependentType())
1473 setInstantiationDependent(true);
1474 if (Args[I]->getType()->containsUnexpandedParameterPack())
1475 setContainsUnexpandedParameterPack(true);
1476
1477 ToArgs[I] = Args[I];
1478 }
1479}
1480
Craig Toppera31a8822013-08-22 07:09:37 +00001481TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001482 SourceLocation Loc,
1483 TypeTrait Kind,
1484 ArrayRef<TypeSourceInfo *> Args,
1485 SourceLocation RParenLoc,
1486 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001487 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001488 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1489}
1490
Craig Toppera31a8822013-08-22 07:09:37 +00001491TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001492 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001493 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001494 return new (Mem) TypeTraitExpr(EmptyShell());
1495}
1496
David Blaikie68e081d2011-12-20 02:48:34 +00001497void ArrayTypeTraitExpr::anchor() { }