blob: 31bf432cf9f9f0e0e7af9c5d84b91bb58cfec711 [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
Nico Webercf4ff5862012-10-11 10:13:44 +000067 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 Majnemer5d22e7e2013-09-07 07:11:04 +000071 // __uuidof can grab UUIDs from template arguments.
David Majnemer59c0ec22013-09-07 06:59:46 +000072 if (ClassTemplateSpecializationDecl *CTSD =
73 dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
74 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
David Majnemer48b08632014-07-14 23:12:54 +000075 const UuidAttr *UuidForRD = nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +000076
David Majnemer48b08632014-07-14 23:12:54 +000077 for (const TemplateArgument &TA : TAL.asArray()) {
David Majnemer59c0ec22013-09-07 06:59:46 +000078 bool SeenMultipleGUIDs = false;
79
David Majnemer48b08632014-07-14 23:12:54 +000080 const UuidAttr *UuidForTA = nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +000081 if (TA.getKind() == TemplateArgument::Type)
82 UuidForTA = GetUuidAttrOfType(TA.getAsType(), &SeenMultipleGUIDs);
83 else if (TA.getKind() == TemplateArgument::Declaration)
84 UuidForTA =
85 GetUuidAttrOfType(TA.getAsDecl()->getType(), &SeenMultipleGUIDs);
86
87 // If the template argument has a UUID, there are three cases:
88 // - This is the first UUID seen for this RecordDecl.
David Majnemer37c921e2013-09-07 20:21:47 +000089 // - This is a different UUID than previously seen for this RecordDecl.
90 // - This is the same UUID than previously seen for this RecordDecl.
David Majnemer59c0ec22013-09-07 06:59:46 +000091 if (UuidForTA) {
92 if (!UuidForRD)
93 UuidForRD = UuidForTA;
94 else if (UuidForRD != UuidForTA)
95 SeenMultipleGUIDs = true;
96 }
97
98 // Seeing multiple UUIDs means that we couldn't find a UUID
99 if (SeenMultipleGUIDs) {
100 if (RDHasMultipleGUIDsPtr)
101 *RDHasMultipleGUIDsPtr = true;
Craig Topper36250ad2014-05-12 05:36:57 +0000102 return nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +0000103 }
104 }
105
106 return UuidForRD;
David Majnemer5d22e7e2013-09-07 07:11:04 +0000107 }
108
David Majnemer48b08632014-07-14 23:12:54 +0000109 // Loop over all record redeclarations looking for a uuid attribute.
110 for (const TagDecl *I : RD->redecls())
111 if (const UuidAttr *Uuid = I->getAttr<UuidAttr>())
David Majnemer5d22e7e2013-09-07 07:11:04 +0000112 return Uuid;
Nico Webercf4ff5862012-10-11 10:13:44 +0000113
Craig Topper36250ad2014-05-12 05:36:57 +0000114 return nullptr;
Nico Webercf4ff5862012-10-11 10:13:44 +0000115}
116
David Majnemer8eaab6f2013-08-13 06:32:20 +0000117StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const {
118 StringRef Uuid;
119 if (isTypeOperand())
David Majnemer143c55e2013-09-27 07:04:31 +0000120 Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand(Context))->getGuid();
David Majnemer8eaab6f2013-08-13 06:32:20 +0000121 else {
122 // Special case: __uuidof(0) means an all-zero GUID.
123 Expr *Op = getExprOperand();
124 if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
125 Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
126 else
127 Uuid = "00000000-0000-0000-0000-000000000000";
128 }
129 return Uuid;
130}
131
Douglas Gregor747eb782010-07-08 06:14:04 +0000132// CXXScalarValueInitExpr
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000133SourceLocation CXXScalarValueInitExpr::getLocStart() const {
134 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +0000135}
136
Sebastian Redlbd150f42008-11-21 19:14:01 +0000137// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +0000138CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
139 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +0000140 bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000141 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +0000142 SourceRange typeIdParens, Expr *arraySize,
143 InitializationStyle initializationStyle,
144 Expr *initializer, QualType ty,
145 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +0000146 SourceRange Range, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +0000147 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000148 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000149 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000150 ty->containsUnexpandedParameterPack()),
Craig Topper36250ad2014-05-12 05:36:57 +0000151 SubExprs(nullptr), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
Sebastian Redl6047f072012-02-16 12:22:20 +0000152 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
David Blaikie7b97aef2012-11-07 00:12:38 +0000153 Range(Range), DirectInitRange(directInitRange),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000154 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Craig Topper36250ad2014-05-12 05:36:57 +0000155 assert((initializer != nullptr || initializationStyle == NoInit) &&
Sebastian Redl6047f072012-02-16 12:22:20 +0000156 "Only NoInit can have no initializer.");
157 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Craig Topper36250ad2014-05-12 05:36:57 +0000158 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
159 initializer != nullptr);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000160 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000161 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000162 if (arraySize->isInstantiationDependent())
163 ExprBits.InstantiationDependent = true;
164
Douglas Gregora6e053e2010-12-15 01:34:56 +0000165 if (arraySize->containsUnexpandedParameterPack())
166 ExprBits.ContainsUnexpandedParameterPack = true;
167
Sebastian Redl351bb782008-12-02 14:43:59 +0000168 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000169 }
170
Sebastian Redl6047f072012-02-16 12:22:20 +0000171 if (initializer) {
172 if (initializer->isInstantiationDependent())
173 ExprBits.InstantiationDependent = true;
174
175 if (initializer->containsUnexpandedParameterPack())
176 ExprBits.ContainsUnexpandedParameterPack = true;
177
178 SubExprs[i++] = initializer;
179 }
180
Benjamin Kramerc215e762012-08-24 11:54:20 +0000181 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000182 if (placementArgs[j]->isInstantiationDependent())
183 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000184 if (placementArgs[j]->containsUnexpandedParameterPack())
185 ExprBits.ContainsUnexpandedParameterPack = true;
186
Sebastian Redlbd150f42008-11-21 19:14:01 +0000187 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000188 }
David Blaikie3a0de212012-11-08 22:53:48 +0000189
190 switch (getInitializationStyle()) {
191 case CallInit:
192 this->Range.setEnd(DirectInitRange.getEnd()); break;
193 case ListInit:
194 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000195 default:
196 if (TypeIdParens.isValid())
197 this->Range.setEnd(TypeIdParens.getEnd());
198 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000199 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000200}
201
Craig Toppera31a8822013-08-22 07:09:37 +0000202void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000203 unsigned numPlaceArgs, bool hasInitializer){
Craig Topper36250ad2014-05-12 05:36:57 +0000204 assert(SubExprs == nullptr && "SubExprs already allocated");
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000205 Array = isArray;
206 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000207
208 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000209 SubExprs = new (C) Stmt*[TotalSize];
210}
211
Craig Toppera31a8822013-08-22 07:09:37 +0000212bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
John McCall75f94982011-03-07 03:12:35 +0000213 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000214 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000215}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000216
Sebastian Redlbd150f42008-11-21 19:14:01 +0000217// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000218QualType CXXDeleteExpr::getDestroyedType() const {
219 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000220 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000221 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000222
223 if (ArgType->isDependentType() && !ArgType->isPointerType())
224 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000225
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000226 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000227}
228
Douglas Gregorad8a3362009-09-04 17:36:40 +0000229// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000230PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
231 : Type(Info)
232{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000233 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000234}
235
Craig Toppera31a8822013-08-22 07:09:37 +0000236CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000237 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
238 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
239 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
240 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000241 : Expr(CXXPseudoDestructorExprClass,
Reid Kleckner78af0702013-08-27 23:08:25 +0000242 Context.getPointerType(Context.getFunctionType(
243 Context.VoidTy, None,
244 FunctionProtoType::ExtProtoInfo(
245 Context.getDefaultCallingConvention(false, true)))),
John McCalldb40c7f2010-12-14 08:05:40 +0000246 VK_RValue, OK_Ordinary,
247 /*isTypeDependent=*/(Base->isTypeDependent() ||
248 (DestroyedType.getTypeSourceInfo() &&
249 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000250 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000251 (Base->isInstantiationDependent() ||
252 (QualifierLoc &&
253 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
254 (ScopeType &&
255 ScopeType->getType()->isInstantiationDependentType()) ||
256 (DestroyedType.getTypeSourceInfo() &&
257 DestroyedType.getTypeSourceInfo()->getType()
258 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000259 // ContainsUnexpandedParameterPack
260 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000261 (QualifierLoc &&
262 QualifierLoc.getNestedNameSpecifier()
263 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000264 (ScopeType &&
265 ScopeType->getType()->containsUnexpandedParameterPack()) ||
266 (DestroyedType.getTypeSourceInfo() &&
267 DestroyedType.getTypeSourceInfo()->getType()
268 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000269 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000270 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000271 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
272 DestroyedType(DestroyedType) { }
273
Douglas Gregor678f90d2010-02-25 01:56:36 +0000274QualType CXXPseudoDestructorExpr::getDestroyedType() const {
275 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
276 return TInfo->getType();
277
278 return QualType();
279}
280
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000281SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000282 SourceLocation End = DestroyedType.getLocation();
283 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000284 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000285 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000286}
287
John McCalld14a8642009-11-21 08:51:07 +0000288// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000289UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000290UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000291 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000292 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000293 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000294 const DeclarationNameInfo &NameInfo,
295 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000296 const TemplateArgumentListInfo *Args,
297 UnresolvedSetIterator Begin,
298 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000299{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000300 assert(Args || TemplateKWLoc.isValid());
301 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000302 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000303 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000304 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
305 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000306 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000307 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000308}
309
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000310UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000311UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000312 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000313 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000314 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000315 if (HasTemplateKWAndArgsInfo)
316 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000317
Chris Lattner5c0b4052010-10-30 05:14:06 +0000318 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000319 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000320 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000321 return E;
322}
323
Craig Toppera31a8822013-08-22 07:09:37 +0000324OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000325 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000326 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000327 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000328 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000329 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000330 UnresolvedSetIterator End,
331 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000332 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000333 bool KnownContainsUnexpandedParameterPack)
334 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
335 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000336 (KnownInstantiationDependent ||
337 NameInfo.isInstantiationDependent() ||
338 (QualifierLoc &&
339 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000340 (KnownContainsUnexpandedParameterPack ||
341 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000342 (QualifierLoc &&
343 QualifierLoc.getNestedNameSpecifier()
344 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000345 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
Craig Topper36250ad2014-05-12 05:36:57 +0000346 Results(nullptr), NumResults(End - Begin),
347 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
348 TemplateKWLoc.isValid()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000349 NumResults = End - Begin;
350 if (NumResults) {
351 // Determine whether this expression is type-dependent.
352 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
353 if ((*I)->getDeclContext()->isDependentContext() ||
354 isa<UnresolvedUsingValueDecl>(*I)) {
355 ExprBits.TypeDependent = true;
356 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000357 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000358 }
359 }
360
361 Results = static_cast<DeclAccessPair *>(
362 C.Allocate(sizeof(DeclAccessPair) * NumResults,
363 llvm::alignOf<DeclAccessPair>()));
364 memcpy(Results, &*Begin.getIterator(),
365 NumResults * sizeof(DeclAccessPair));
366 }
367
368 // If we have explicit template arguments, check for dependent
369 // template arguments and whether they contain any unexpanded pack
370 // expansions.
371 if (TemplateArgs) {
372 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000373 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000374 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000375 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
376 Dependent,
377 InstantiationDependent,
378 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000379
380 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000381 ExprBits.TypeDependent = true;
382 ExprBits.ValueDependent = true;
383 }
384 if (InstantiationDependent)
385 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000386 if (ContainsUnexpandedParameterPack)
387 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000388 } else if (TemplateKWLoc.isValid()) {
389 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000390 }
391
392 if (isTypeDependent())
393 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000394}
395
Craig Toppera31a8822013-08-22 07:09:37 +0000396void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000397 UnresolvedSetIterator Begin,
398 UnresolvedSetIterator End) {
Craig Topper36250ad2014-05-12 05:36:57 +0000399 assert(!Results && "Results already initialized!");
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000400 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000401 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000402 Results = static_cast<DeclAccessPair *>(
403 C.Allocate(sizeof(DeclAccessPair) * NumResults,
404
405 llvm::alignOf<DeclAccessPair>()));
406 memcpy(Results, &*Begin.getIterator(),
407 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000408 }
409}
410
John McCall8c12dc42010-04-22 18:44:12 +0000411CXXRecordDecl *OverloadExpr::getNamingClass() const {
412 if (isa<UnresolvedLookupExpr>(this))
413 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
414 else
415 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
416}
417
John McCall8cd78132009-11-19 22:55:06 +0000418// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000419DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000420 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000421 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000422 const DeclarationNameInfo &NameInfo,
423 const TemplateArgumentListInfo *Args)
424 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
425 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000426 (NameInfo.isInstantiationDependent() ||
427 (QualifierLoc &&
428 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000429 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000430 (QualifierLoc &&
431 QualifierLoc.getNestedNameSpecifier()
432 ->containsUnexpandedParameterPack()))),
433 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Craig Topper36250ad2014-05-12 05:36:57 +0000434 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000435{
436 if (Args) {
437 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000438 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000439 bool ContainsUnexpandedParameterPack
440 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000441 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
442 Dependent,
443 InstantiationDependent,
444 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000445 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000446 } else if (TemplateKWLoc.isValid()) {
447 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000448 }
449}
450
John McCalle66edc12009-11-24 19:00:30 +0000451DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000452DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000453 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000454 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000455 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000456 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000457 assert(QualifierLoc && "should be created for dependent qualifiers");
John McCalle66edc12009-11-24 19:00:30 +0000458 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000459 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000460 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
461 else if (TemplateKWLoc.isValid())
462 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000463 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000464 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
465 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000466}
467
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000468DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000469DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000470 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000471 unsigned NumTemplateArgs) {
472 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000473 if (HasTemplateKWAndArgsInfo)
474 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000475 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000476 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000477 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000478 SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000479 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000480 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000481 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000482}
483
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000484SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000485 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000486 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
487 return Loc;
488}
489
490SourceLocation CXXConstructExpr::getLocEnd() const {
491 if (isa<CXXTemporaryObjectExpr>(this))
492 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000493
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000494 if (ParenOrBraceRange.isValid())
495 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000496
497 SourceLocation End = Loc;
498 for (unsigned I = getNumArgs(); I > 0; --I) {
499 const Expr *Arg = getArg(I-1);
500 if (!Arg->isDefaultArgument()) {
501 SourceLocation NewEnd = Arg->getLocEnd();
502 if (NewEnd.isValid()) {
503 End = NewEnd;
504 break;
505 }
506 }
507 }
508
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000509 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000510}
511
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000512SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000513 OverloadedOperatorKind Kind = getOperator();
514 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
515 if (getNumArgs() == 1)
516 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000517 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000518 else
519 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000520 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000521 } else if (Kind == OO_Arrow) {
522 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000523 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000524 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000525 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000526 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000527 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000528 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000529 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000530 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000531 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000532 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000533 }
534}
535
Ted Kremenek98a24e32011-03-30 17:41:19 +0000536Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000537 const Expr *Callee = getCallee()->IgnoreParens();
538 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000539 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000540 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
541 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
542 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000543
544 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000545 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000546}
547
Ted Kremenek98a24e32011-03-30 17:41:19 +0000548CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
549 if (const MemberExpr *MemExpr =
550 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
551 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
552
553 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000554 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000555}
556
557
David Blaikiec0f58662012-05-03 16:25:49 +0000558CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000559 Expr* ThisArg = getImplicitObjectArgument();
560 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000561 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000562
563 if (ThisArg->getType()->isAnyPointerType())
564 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
565
566 return ThisArg->getType()->getAsCXXRecordDecl();
567}
568
Douglas Gregoref986e82009-11-12 15:31:47 +0000569
Douglas Gregore200adc2008-10-27 19:41:14 +0000570//===----------------------------------------------------------------------===//
571// Named casts
572//===----------------------------------------------------------------------===//
573
574/// getCastName - Get the name of the C++ cast being used, e.g.,
575/// "static_cast", "dynamic_cast", "reinterpret_cast", or
576/// "const_cast". The returned pointer must not be freed.
577const char *CXXNamedCastExpr::getCastName() const {
578 switch (getStmtClass()) {
579 case CXXStaticCastExprClass: return "static_cast";
580 case CXXDynamicCastExprClass: return "dynamic_cast";
581 case CXXReinterpretCastExprClass: return "reinterpret_cast";
582 case CXXConstCastExprClass: return "const_cast";
583 default: return "<invalid cast>";
584 }
585}
Douglas Gregordd04d332009-01-16 18:33:17 +0000586
Craig Toppera31a8822013-08-22 07:09:37 +0000587CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000588 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000589 CastKind K, Expr *Op,
590 const CXXCastPath *BasePath,
591 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000592 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000593 SourceLocation RParenLoc,
594 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000595 unsigned PathSize = (BasePath ? BasePath->size() : 0);
596 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
597 + PathSize * sizeof(CXXBaseSpecifier*));
598 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000599 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000600 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000601 if (PathSize) E->setCastPath(*BasePath);
602 return E;
603}
604
Craig Toppera31a8822013-08-22 07:09:37 +0000605CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000606 unsigned PathSize) {
607 void *Buffer =
608 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
609 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
610}
611
Craig Toppera31a8822013-08-22 07:09:37 +0000612CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000613 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000614 CastKind K, Expr *Op,
615 const CXXCastPath *BasePath,
616 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000617 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000618 SourceLocation RParenLoc,
619 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000620 unsigned PathSize = (BasePath ? BasePath->size() : 0);
621 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
622 + PathSize * sizeof(CXXBaseSpecifier*));
623 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000624 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000625 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000626 if (PathSize) E->setCastPath(*BasePath);
627 return E;
628}
629
Craig Toppera31a8822013-08-22 07:09:37 +0000630CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000631 unsigned PathSize) {
632 void *Buffer =
633 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
634 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
635}
636
Anders Carlsson267c0c92011-04-11 01:43:55 +0000637/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
638/// to always be null. For example:
639///
640/// struct A { };
641/// struct B final : A { };
642/// struct C { };
643///
644/// C *f(B* b) { return dynamic_cast<C*>(b); }
645bool CXXDynamicCastExpr::isAlwaysNull() const
646{
647 QualType SrcType = getSubExpr()->getType();
648 QualType DestType = getType();
649
650 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
651 SrcType = SrcPTy->getPointeeType();
652 DestType = DestType->castAs<PointerType>()->getPointeeType();
653 }
654
Alexis Hunt78e2b912012-06-19 23:44:55 +0000655 if (DestType->isVoidType())
656 return false;
657
Anders Carlsson267c0c92011-04-11 01:43:55 +0000658 const CXXRecordDecl *SrcRD =
659 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
660
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000661 if (!SrcRD->hasAttr<FinalAttr>())
662 return false;
663
Anders Carlsson267c0c92011-04-11 01:43:55 +0000664 const CXXRecordDecl *DestRD =
665 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
666
667 return !DestRD->isDerivedFrom(SrcRD);
668}
669
John McCallcf142162010-08-07 06:22:56 +0000670CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000671CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
672 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000673 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000674 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000675 SourceLocation RParenLoc,
676 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000677 unsigned PathSize = (BasePath ? BasePath->size() : 0);
678 void *Buffer =
679 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
680 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000681 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000682 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000683 if (PathSize) E->setCastPath(*BasePath);
684 return E;
685}
686
687CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000688CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000689 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
690 + PathSize * sizeof(CXXBaseSpecifier*));
691 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
692}
693
Craig Toppera31a8822013-08-22 07:09:37 +0000694CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000695 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000696 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000697 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000698 SourceLocation RParenLoc,
699 SourceRange AngleBrackets) {
700 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000701}
702
Craig Toppera31a8822013-08-22 07:09:37 +0000703CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000704 return new (C) CXXConstCastExpr(EmptyShell());
705}
706
707CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000708CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000709 TypeSourceInfo *Written, CastKind K, Expr *Op,
710 const CXXCastPath *BasePath,
711 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000712 unsigned PathSize = (BasePath ? BasePath->size() : 0);
713 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
714 + PathSize * sizeof(CXXBaseSpecifier*));
715 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000716 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
John McCallcf142162010-08-07 06:22:56 +0000717 if (PathSize) E->setCastPath(*BasePath);
718 return E;
719}
720
721CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000722CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000723 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
724 + PathSize * sizeof(CXXBaseSpecifier*));
725 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
726}
727
Eli Friedman89fe0d52013-08-15 22:02:56 +0000728SourceLocation CXXFunctionalCastExpr::getLocStart() const {
729 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
730}
731
732SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
733 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
734}
735
Richard Smithc67fdd42012-03-07 08:35:16 +0000736UserDefinedLiteral::LiteralOperatorKind
737UserDefinedLiteral::getLiteralOperatorKind() const {
738 if (getNumArgs() == 0)
739 return LOK_Template;
740 if (getNumArgs() == 2)
741 return LOK_String;
742
743 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
744 QualType ParamTy =
745 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
746 if (ParamTy->isPointerType())
747 return LOK_Raw;
748 if (ParamTy->isAnyCharacterType())
749 return LOK_Character;
750 if (ParamTy->isIntegerType())
751 return LOK_Integer;
752 if (ParamTy->isFloatingType())
753 return LOK_Floating;
754
755 llvm_unreachable("unknown kind of literal operator");
756}
757
758Expr *UserDefinedLiteral::getCookedLiteral() {
759#ifndef NDEBUG
760 LiteralOperatorKind LOK = getLiteralOperatorKind();
761 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
762#endif
763 return getArg(0);
764}
765
766const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
767 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
768}
John McCallcf142162010-08-07 06:22:56 +0000769
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000770CXXDefaultArgExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000771CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
Douglas Gregor033f6752009-12-23 23:03:06 +0000772 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000773 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000774 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
775 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000776}
777
Craig Toppera31a8822013-08-22 07:09:37 +0000778CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000779 FieldDecl *Field, QualType T)
780 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
781 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
782 ? VK_XValue
783 : VK_RValue,
784 /*FIXME*/ OK_Ordinary, false, false, false, false),
785 Field(Field), Loc(Loc) {
786 assert(Field->hasInClassInitializer());
787}
788
Craig Toppera31a8822013-08-22 07:09:37 +0000789CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000790 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000791 return new (C) CXXTemporary(Destructor);
792}
793
Craig Toppera31a8822013-08-22 07:09:37 +0000794CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000795 CXXTemporary *Temp,
796 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000797 assert((SubExpr->getType()->isRecordType() ||
798 SubExpr->getType()->isArrayType()) &&
799 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000800
Douglas Gregora6e053e2010-12-15 01:34:56 +0000801 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000802}
803
Craig Toppera31a8822013-08-22 07:09:37 +0000804CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000805 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000806 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000807 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000808 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000809 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000810 bool ListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000811 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000812 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
813 Type->getType().getNonReferenceType(),
814 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000815 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000816 HadMultipleCandidates,
817 ListInitialization, ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000818 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000819 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000820}
821
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000822SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
823 return Type->getTypeLoc().getBeginLoc();
824}
825
826SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000827 SourceLocation Loc = getParenOrBraceRange().getEnd();
828 if (Loc.isInvalid() && getNumArgs())
829 Loc = getArg(getNumArgs()-1)->getLocEnd();
830 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000831}
Anders Carlsson6f287832009-04-21 02:22:11 +0000832
Craig Toppera31a8822013-08-22 07:09:37 +0000833CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000834 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000835 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000836 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000837 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000838 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000839 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000840 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000841 SourceRange ParenOrBraceRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000842 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000843 Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000844 HadMultipleCandidates, ListInitialization,
845 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000846 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000847}
848
Craig Toppera31a8822013-08-22 07:09:37 +0000849CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
850 QualType T, SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000851 CXXConstructorDecl *D, bool elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000852 ArrayRef<Expr*> args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000853 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000854 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000855 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000856 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000857 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000858 : Expr(SC, T, VK_RValue, OK_Ordinary,
859 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000860 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000861 T->containsUnexpandedParameterPack()),
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000862 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
863 NumArgs(args.size()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000864 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000865 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000866 ZeroInitialization(ZeroInitialization),
Craig Topper36250ad2014-05-12 05:36:57 +0000867 ConstructKind(ConstructKind), Args(nullptr)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000868{
869 if (NumArgs) {
Benjamin Kramerc215e762012-08-24 11:54:20 +0000870 Args = new (C) Stmt*[args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000871
Benjamin Kramerc215e762012-08-24 11:54:20 +0000872 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000873 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000874
875 if (args[i]->isValueDependent())
876 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000877 if (args[i]->isInstantiationDependent())
878 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000879 if (args[i]->containsUnexpandedParameterPack())
880 ExprBits.ContainsUnexpandedParameterPack = true;
881
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000882 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000883 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000884 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000885}
886
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000887LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000888 LambdaCaptureKind Kind, VarDecl *Var,
889 SourceLocation EllipsisLoc)
Richard Smithba71c082013-05-16 06:20:58 +0000890 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000891{
892 unsigned Bits = 0;
893 if (Implicit)
894 Bits |= Capture_Implicit;
895
896 switch (Kind) {
Craig Topper36250ad2014-05-12 05:36:57 +0000897 case LCK_This:
898 assert(!Var && "'this' capture cannot have a variable!");
Douglas Gregore31e6062012-02-07 10:09:13 +0000899 break;
900
901 case LCK_ByCopy:
902 Bits |= Capture_ByCopy;
903 // Fall through
904 case LCK_ByRef:
905 assert(Var && "capture must have a variable!");
906 break;
907 }
Richard Smithba71c082013-05-16 06:20:58 +0000908 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000909}
910
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000911LambdaCaptureKind LambdaCapture::getCaptureKind() const {
Richard Smithba71c082013-05-16 06:20:58 +0000912 Decl *D = DeclAndBits.getPointer();
913 if (!D)
Douglas Gregore31e6062012-02-07 10:09:13 +0000914 return LCK_This;
915
Richard Smithba71c082013-05-16 06:20:58 +0000916 return (DeclAndBits.getInt() & Capture_ByCopy) ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000917}
918
James Dennettddd36ff2013-08-09 23:08:25 +0000919LambdaExpr::LambdaExpr(QualType T,
Douglas Gregore31e6062012-02-07 10:09:13 +0000920 SourceRange IntroducerRange,
921 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000922 SourceLocation CaptureDefaultLoc,
923 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000924 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000925 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000926 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000927 ArrayRef<VarDecl *> ArrayIndexVars,
928 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000929 SourceLocation ClosingBrace,
930 bool ContainsUnexpandedParameterPack)
Douglas Gregore31e6062012-02-07 10:09:13 +0000931 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
932 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith2589b9802012-07-25 03:56:55 +0000933 ContainsUnexpandedParameterPack),
Douglas Gregore31e6062012-02-07 10:09:13 +0000934 IntroducerRange(IntroducerRange),
James Dennettddd36ff2013-08-09 23:08:25 +0000935 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregore5561632012-02-13 17:20:40 +0000936 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000937 CaptureDefault(CaptureDefault),
938 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000939 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000940 ClosingBrace(ClosingBrace)
941{
942 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000943 CXXRecordDecl *Class = getLambdaClass();
944 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000945
946 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000947
948 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000949 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000950 Data.NumCaptures = NumCaptures;
951 Data.NumExplicitCaptures = 0;
952 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
953 Capture *ToCapture = Data.Captures;
954 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
955 if (Captures[I].isExplicit())
956 ++Data.NumExplicitCaptures;
957
958 *ToCapture++ = Captures[I];
959 }
960
961 // Copy initialization expressions for the non-static data members.
962 Stmt **Stored = getStoredStmts();
963 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
964 *Stored++ = CaptureInits[I];
965
966 // Copy the body of the lambda.
967 *Stored++ = getCallOperator()->getBody();
968
969 // Copy the array index variables, if any.
970 HasArrayIndexVars = !ArrayIndexVars.empty();
971 if (HasArrayIndexVars) {
972 assert(ArrayIndexStarts.size() == NumCaptures);
973 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
974 sizeof(VarDecl *) * ArrayIndexVars.size());
975 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
976 sizeof(unsigned) * Captures.size());
977 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000978 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000979}
980
Craig Toppera31a8822013-08-22 07:09:37 +0000981LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
Douglas Gregore31e6062012-02-07 10:09:13 +0000982 CXXRecordDecl *Class,
983 SourceRange IntroducerRange,
984 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000985 SourceLocation CaptureDefaultLoc,
986 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000987 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000988 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000989 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000990 ArrayRef<VarDecl *> ArrayIndexVars,
991 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000992 SourceLocation ClosingBrace,
993 bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000994 // Determine the type of the expression (i.e., the type of the
995 // function object we're creating).
996 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000997
Douglas Gregore5561632012-02-13 17:20:40 +0000998 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smithc7520bf2012-08-21 05:42:49 +0000999 if (!ArrayIndexVars.empty()) {
1000 Size += sizeof(unsigned) * (Captures.size() + 1);
1001 // Realign for following VarDecl array.
Richard Smitha75e1cf2012-08-21 18:18:06 +00001002 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smithc7520bf2012-08-21 05:42:49 +00001003 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
1004 }
Douglas Gregore5561632012-02-13 17:20:40 +00001005 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +00001006 return new (Mem) LambdaExpr(T, IntroducerRange,
1007 CaptureDefault, CaptureDefaultLoc, Captures,
1008 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001009 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001010 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +00001011}
1012
Craig Toppera31a8822013-08-22 07:09:37 +00001013LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1014 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +00001015 unsigned NumArrayIndexVars) {
1016 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
1017 if (NumArrayIndexVars)
1018 Size += sizeof(VarDecl) * NumArrayIndexVars
1019 + sizeof(unsigned) * (NumCaptures + 1);
1020 void *Mem = C.Allocate(Size);
1021 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1022}
1023
Douglas Gregorc8a73492012-02-13 15:44:47 +00001024LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001025 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001026}
1027
1028LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001029 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001030}
1031
James Dennett1575cb42014-05-27 19:13:04 +00001032LambdaExpr::capture_range LambdaExpr::captures() const {
1033 return capture_range(capture_begin(), capture_end());
1034}
1035
Douglas Gregorc8a73492012-02-13 15:44:47 +00001036LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1037 return capture_begin();
1038}
1039
1040LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1041 struct CXXRecordDecl::LambdaDefinitionData &Data
1042 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001043 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001044}
1045
James Dennett1575cb42014-05-27 19:13:04 +00001046LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1047 return capture_range(explicit_capture_begin(), explicit_capture_end());
1048}
1049
Douglas Gregorc8a73492012-02-13 15:44:47 +00001050LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1051 return explicit_capture_end();
1052}
1053
1054LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1055 return capture_end();
1056}
1057
James Dennett1575cb42014-05-27 19:13:04 +00001058LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1059 return capture_range(implicit_capture_begin(), implicit_capture_end());
1060}
1061
Douglas Gregor54fcea62012-02-13 16:35:30 +00001062ArrayRef<VarDecl *>
1063LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001064 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001065
1066 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001067 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1068 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +00001069 VarDecl **IndexVars = getArrayIndexVars();
1070 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +00001071 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
1072 IndexVars + IndexStarts[Index + 1]);
1073}
1074
Douglas Gregore31e6062012-02-07 10:09:13 +00001075CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1076 return getType()->getAsCXXRecordDecl();
1077}
1078
1079CXXMethodDecl *LambdaExpr::getCallOperator() const {
1080 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001081 return Record->getLambdaCallOperator();
1082}
1083
1084TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1085 CXXRecordDecl *Record = getLambdaClass();
1086 return Record->getGenericLambdaTemplateParameterList();
1087
Douglas Gregore31e6062012-02-07 10:09:13 +00001088}
1089
Douglas Gregor99ae8062012-02-14 17:54:36 +00001090CompoundStmt *LambdaExpr::getBody() const {
1091 if (!getStoredStmts()[NumCaptures])
1092 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1093
1094 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1095}
1096
Douglas Gregore31e6062012-02-07 10:09:13 +00001097bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001098 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001099}
1100
John McCall28fc7092011-11-10 05:35:25 +00001101ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1102 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001103 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001104 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001105 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001106 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001107 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001108 SubExpr(subexpr) {
1109 ExprWithCleanupsBits.NumObjects = objects.size();
1110 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1111 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001112}
1113
Craig Toppera31a8822013-08-22 07:09:37 +00001114ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001115 ArrayRef<CleanupObject> objects) {
1116 size_t size = sizeof(ExprWithCleanups)
1117 + objects.size() * sizeof(CleanupObject);
1118 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1119 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) {
1130 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1131 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1132 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()) {
Douglas Gregorce934142009-05-20 18:46:25 +00001151 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
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) {
1166 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001167 sizeof(Expr *) * Args.size());
1168 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001169}
1170
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001171CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001172CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001173 Stmt::EmptyShell Empty;
1174 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1175 sizeof(Expr *) * NumArgs);
1176 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1177}
1178
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001179SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1180 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001181}
1182
Craig Toppera31a8822013-08-22 07:09:37 +00001183CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001184 Expr *Base, QualType BaseType,
1185 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001186 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001187 NestedNameSpecifierLoc QualifierLoc,
1188 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001189 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001190 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001191 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001192 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001193 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001194 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001195 (QualifierLoc &&
1196 QualifierLoc.getNestedNameSpecifier()
1197 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001198 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001199 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Craig Topper36250ad2014-05-12 05:36:57 +00001200 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1201 TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001202 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001203 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001204 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001205 if (TemplateArgs) {
1206 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001207 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001208 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001209 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1210 Dependent,
1211 InstantiationDependent,
1212 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001213 if (ContainsUnexpandedParameterPack)
1214 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001215 } else if (TemplateKWLoc.isValid()) {
1216 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001217 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001218}
1219
Craig Toppera31a8822013-08-22 07:09:37 +00001220CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001221 Expr *Base, QualType BaseType,
1222 bool IsArrow,
1223 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001224 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001225 NamedDecl *FirstQualifierFoundInScope,
1226 DeclarationNameInfo MemberNameInfo)
1227 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001228 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001229 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001230 (QualifierLoc &&
1231 QualifierLoc.getNestedNameSpecifier()->
1232 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001233 MemberNameInfo.containsUnexpandedParameterPack())),
1234 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001235 HasTemplateKWAndArgsInfo(false),
1236 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001237 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1238 MemberNameInfo(MemberNameInfo) { }
1239
John McCall8cd78132009-11-19 22:55:06 +00001240CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001241CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001242 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001243 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001244 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001245 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001246 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001247 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001248 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001249 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001250 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1251 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001252 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001253 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001254 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001255
Abramo Bagnara7945c982012-01-27 09:46:47 +00001256 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1257 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1258 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001259
Chris Lattner5c0b4052010-10-30 05:14:06 +00001260 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001261 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1262 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001263 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001264 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001265 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001266 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001267}
1268
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001269CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001270CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001271 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001272 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001273 if (!HasTemplateKWAndArgsInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00001274 return new (C) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001275 0, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001276 NestedNameSpecifierLoc(),
1277 nullptr, DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001278
1279 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001280 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001281 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001282 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001283 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001284 0, SourceLocation(),
1285 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001286 SourceLocation(), nullptr,
1287 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001288 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001289 return E;
1290}
1291
Douglas Gregor0da1d432011-02-28 20:01:57 +00001292bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001293 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001294 return true;
1295
Douglas Gregor25b7e052011-03-02 21:06:53 +00001296 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001297}
1298
John McCall0009fcc2011-04-26 20:42:42 +00001299static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1300 UnresolvedSetIterator end) {
1301 do {
1302 NamedDecl *decl = *begin;
1303 if (isa<UnresolvedUsingValueDecl>(decl))
1304 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001305
1306 // Unresolved member expressions should only contain methods and
1307 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001308 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1309 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001310 return false;
1311 } while (++begin != end);
1312
1313 return true;
1314}
1315
Craig Toppera31a8822013-08-22 07:09:37 +00001316UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001317 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001318 Expr *Base, QualType BaseType,
1319 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001320 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001321 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001322 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001323 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001324 const TemplateArgumentListInfo *TemplateArgs,
1325 UnresolvedSetIterator Begin,
1326 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001327 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1328 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001329 // Dependent
1330 ((Base && Base->isTypeDependent()) ||
1331 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001332 ((Base && Base->isInstantiationDependent()) ||
1333 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001334 // Contains unexpanded parameter pack
1335 ((Base && Base->containsUnexpandedParameterPack()) ||
1336 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001337 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1338 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001339
1340 // Check whether all of the members are non-static member functions,
1341 // and if so, mark give this bound-member type instead of overload type.
1342 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1343 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001344}
1345
Douglas Gregor0da1d432011-02-28 20:01:57 +00001346bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001347 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001348 return true;
1349
Douglas Gregor25b7e052011-03-02 21:06:53 +00001350 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001351}
1352
John McCall10eae182009-11-30 22:42:35 +00001353UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001354UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001355 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001356 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001357 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001358 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001359 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001360 const TemplateArgumentListInfo *TemplateArgs,
1361 UnresolvedSetIterator Begin,
1362 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001363 std::size_t size = sizeof(UnresolvedMemberExpr);
1364 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001365 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1366 else if (TemplateKWLoc.isValid())
1367 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001368
Chris Lattner5c0b4052010-10-30 05:14:06 +00001369 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001370 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001371 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001372 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001373 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001374}
1375
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001376UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001377UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1378 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001379 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001380 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001381 if (HasTemplateKWAndArgsInfo)
1382 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001383
Chris Lattner5c0b4052010-10-30 05:14:06 +00001384 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001385 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001386 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001387 return E;
1388}
1389
John McCall58cc69d2010-01-27 01:50:18 +00001390CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1391 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1392
1393 // If there was a nested name specifier, it names the naming class.
1394 // It can't be dependent: after all, we were actually able to do the
1395 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001396 CXXRecordDecl *Record = nullptr;
John McCall1acbbb52010-02-02 06:20:04 +00001397 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001398 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001399 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001400 Record = T->getAsCXXRecordDecl();
1401 assert(Record && "qualifier in member expression does not name record");
1402 }
John McCall58cc69d2010-01-27 01:50:18 +00001403 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001404 else {
John McCall58cc69d2010-01-27 01:50:18 +00001405 QualType BaseType = getBaseType().getNonReferenceType();
1406 if (isArrow()) {
1407 const PointerType *PT = BaseType->getAs<PointerType>();
1408 assert(PT && "base of arrow member access is not pointer");
1409 BaseType = PT->getPointeeType();
1410 }
1411
Douglas Gregor9262f472010-04-27 18:19:34 +00001412 Record = BaseType->getAsCXXRecordDecl();
1413 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001414 }
1415
Douglas Gregor9262f472010-04-27 18:19:34 +00001416 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001417}
1418
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001419SubstNonTypeTemplateParmPackExpr::
1420SubstNonTypeTemplateParmPackExpr(QualType T,
1421 NonTypeTemplateParmDecl *Param,
1422 SourceLocation NameLoc,
1423 const TemplateArgument &ArgPack)
1424 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001425 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001426 Param(Param), Arguments(ArgPack.pack_begin()),
1427 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1428
1429TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1430 return TemplateArgument(Arguments, NumArguments);
1431}
1432
Richard Smithb15fe3a2012-09-12 00:56:43 +00001433FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1434 SourceLocation NameLoc,
1435 unsigned NumParams,
1436 Decl * const *Params)
1437 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1438 true, true, true, true),
1439 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1440 if (Params)
1441 std::uninitialized_copy(Params, Params + NumParams,
1442 reinterpret_cast<Decl**>(this+1));
1443}
1444
1445FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001446FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001447 ParmVarDecl *ParamPack, SourceLocation NameLoc,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001448 ArrayRef<Decl *> Params) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001449 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1450 sizeof(ParmVarDecl*) * Params.size()))
1451 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1452}
1453
1454FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001455FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1456 unsigned NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001457 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1458 sizeof(ParmVarDecl*) * NumParams))
Craig Topper36250ad2014-05-12 05:36:57 +00001459 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001460}
1461
David Majnemerdaff3702014-05-01 17:50:17 +00001462void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1463 unsigned ManglingNumber) {
1464 // We only need extra state if we have to remember more than just the Stmt.
1465 if (!ExtendedBy)
1466 return;
1467
1468 // We may need to allocate extra storage for the mangling number and the
1469 // extended-by ValueDecl.
1470 if (!State.is<ExtraState *>()) {
1471 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1472 ES->Temporary = State.get<Stmt *>();
1473 State = ES;
1474 }
1475
1476 auto ES = State.get<ExtraState *>();
1477 ES->ExtendingDecl = ExtendedBy;
1478 ES->ManglingNumber = ManglingNumber;
1479}
1480
Douglas Gregor29c42f22012-02-24 07:38:34 +00001481TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1482 ArrayRef<TypeSourceInfo *> Args,
1483 SourceLocation RParenLoc,
1484 bool Value)
1485 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1486 /*TypeDependent=*/false,
1487 /*ValueDependent=*/false,
1488 /*InstantiationDependent=*/false,
1489 /*ContainsUnexpandedParameterPack=*/false),
1490 Loc(Loc), RParenLoc(RParenLoc)
1491{
1492 TypeTraitExprBits.Kind = Kind;
1493 TypeTraitExprBits.Value = Value;
1494 TypeTraitExprBits.NumArgs = Args.size();
1495
1496 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1497
1498 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1499 if (Args[I]->getType()->isDependentType())
1500 setValueDependent(true);
1501 if (Args[I]->getType()->isInstantiationDependentType())
1502 setInstantiationDependent(true);
1503 if (Args[I]->getType()->containsUnexpandedParameterPack())
1504 setContainsUnexpandedParameterPack(true);
1505
1506 ToArgs[I] = Args[I];
1507 }
1508}
1509
Craig Toppera31a8822013-08-22 07:09:37 +00001510TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001511 SourceLocation Loc,
1512 TypeTrait Kind,
1513 ArrayRef<TypeSourceInfo *> Args,
1514 SourceLocation RParenLoc,
1515 bool Value) {
1516 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1517 void *Mem = C.Allocate(Size);
1518 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1519}
1520
Craig Toppera31a8822013-08-22 07:09:37 +00001521TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001522 unsigned NumArgs) {
1523 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1524 void *Mem = C.Allocate(Size);
1525 return new (Mem) TypeTraitExpr(EmptyShell());
1526}
1527
David Blaikie68e081d2011-12-20 02:48:34 +00001528void ArrayTypeTraitExpr::anchor() { }