blob: 1f54d24c471aeca19f69c77b1300fdebbd0fd7e2 [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,
Reid Kleckner78af0702013-08-27 23:08:25 +0000241 Context.getPointerType(Context.getFunctionType(
242 Context.VoidTy, None,
243 FunctionProtoType::ExtProtoInfo(
244 Context.getDefaultCallingConvention(false, true)))),
John McCalldb40c7f2010-12-14 08:05:40 +0000245 VK_RValue, OK_Ordinary,
246 /*isTypeDependent=*/(Base->isTypeDependent() ||
247 (DestroyedType.getTypeSourceInfo() &&
248 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000249 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000250 (Base->isInstantiationDependent() ||
251 (QualifierLoc &&
252 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
253 (ScopeType &&
254 ScopeType->getType()->isInstantiationDependentType()) ||
255 (DestroyedType.getTypeSourceInfo() &&
256 DestroyedType.getTypeSourceInfo()->getType()
257 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000258 // ContainsUnexpandedParameterPack
259 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000260 (QualifierLoc &&
261 QualifierLoc.getNestedNameSpecifier()
262 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000263 (ScopeType &&
264 ScopeType->getType()->containsUnexpandedParameterPack()) ||
265 (DestroyedType.getTypeSourceInfo() &&
266 DestroyedType.getTypeSourceInfo()->getType()
267 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000268 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000269 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000270 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
271 DestroyedType(DestroyedType) { }
272
Douglas Gregor678f90d2010-02-25 01:56:36 +0000273QualType CXXPseudoDestructorExpr::getDestroyedType() const {
274 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
275 return TInfo->getType();
276
277 return QualType();
278}
279
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000280SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000281 SourceLocation End = DestroyedType.getLocation();
282 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000283 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000284 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000285}
286
John McCalld14a8642009-11-21 08:51:07 +0000287// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000288UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000289UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000290 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000291 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000292 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000293 const DeclarationNameInfo &NameInfo,
294 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000295 const TemplateArgumentListInfo *Args,
296 UnresolvedSetIterator Begin,
297 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000298{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000299 assert(Args || TemplateKWLoc.isValid());
300 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000301 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000302 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
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) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000313 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000314 if (HasTemplateKWAndArgsInfo)
315 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000316
Chris Lattner5c0b4052010-10-30 05:14:06 +0000317 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;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000373 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
374 Dependent,
375 InstantiationDependent,
376 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000377
378 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000379 ExprBits.TypeDependent = true;
380 ExprBits.ValueDependent = true;
381 }
382 if (InstantiationDependent)
383 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000384 if (ContainsUnexpandedParameterPack)
385 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000386 } else if (TemplateKWLoc.isValid()) {
387 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000388 }
389
390 if (isTypeDependent())
391 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000392}
393
Craig Toppera31a8822013-08-22 07:09:37 +0000394void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000395 UnresolvedSetIterator Begin,
396 UnresolvedSetIterator End) {
Craig Topper36250ad2014-05-12 05:36:57 +0000397 assert(!Results && "Results already initialized!");
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000398 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000399 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000400 Results = static_cast<DeclAccessPair *>(
401 C.Allocate(sizeof(DeclAccessPair) * NumResults,
402
403 llvm::alignOf<DeclAccessPair>()));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000404 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000405 }
406}
407
John McCall8c12dc42010-04-22 18:44:12 +0000408CXXRecordDecl *OverloadExpr::getNamingClass() const {
409 if (isa<UnresolvedLookupExpr>(this))
410 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
411 else
412 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
413}
414
John McCall8cd78132009-11-19 22:55:06 +0000415// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000416DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000417 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000418 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000419 const DeclarationNameInfo &NameInfo,
420 const TemplateArgumentListInfo *Args)
421 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
422 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000423 (NameInfo.isInstantiationDependent() ||
424 (QualifierLoc &&
425 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000426 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000427 (QualifierLoc &&
428 QualifierLoc.getNestedNameSpecifier()
429 ->containsUnexpandedParameterPack()))),
430 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Craig Topper36250ad2014-05-12 05:36:57 +0000431 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000432{
433 if (Args) {
434 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000435 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000436 bool ContainsUnexpandedParameterPack
437 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000438 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
439 Dependent,
440 InstantiationDependent,
441 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000442 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000443 } else if (TemplateKWLoc.isValid()) {
444 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000445 }
446}
447
John McCalle66edc12009-11-24 19:00:30 +0000448DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000449DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000450 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000451 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000452 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000453 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000454 assert(QualifierLoc && "should be created for dependent qualifiers");
John McCalle66edc12009-11-24 19:00:30 +0000455 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000456 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000457 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
458 else if (TemplateKWLoc.isValid())
459 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000460 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000461 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
462 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000463}
464
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000465DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000466DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000467 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000468 unsigned NumTemplateArgs) {
469 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000470 if (HasTemplateKWAndArgsInfo)
471 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000472 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000473 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000474 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000475 SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000476 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000477 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000478 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000479}
480
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000481SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000482 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000483 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
484 return Loc;
485}
486
487SourceLocation CXXConstructExpr::getLocEnd() const {
488 if (isa<CXXTemporaryObjectExpr>(this))
489 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000490
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000491 if (ParenOrBraceRange.isValid())
492 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000493
494 SourceLocation End = Loc;
495 for (unsigned I = getNumArgs(); I > 0; --I) {
496 const Expr *Arg = getArg(I-1);
497 if (!Arg->isDefaultArgument()) {
498 SourceLocation NewEnd = Arg->getLocEnd();
499 if (NewEnd.isValid()) {
500 End = NewEnd;
501 break;
502 }
503 }
504 }
505
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000506 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000507}
508
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000509SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000510 OverloadedOperatorKind Kind = getOperator();
511 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
512 if (getNumArgs() == 1)
513 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000514 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000515 else
516 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000517 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000518 } else if (Kind == OO_Arrow) {
519 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000520 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000521 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000522 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000523 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000524 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000525 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000526 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000527 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000528 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000529 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000530 }
531}
532
Ted Kremenek98a24e32011-03-30 17:41:19 +0000533Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000534 const Expr *Callee = getCallee()->IgnoreParens();
535 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000536 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000537 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
538 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
539 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000540
541 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000542 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000543}
544
Ted Kremenek98a24e32011-03-30 17:41:19 +0000545CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
546 if (const MemberExpr *MemExpr =
547 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
548 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
549
550 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000551 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000552}
553
554
David Blaikiec0f58662012-05-03 16:25:49 +0000555CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000556 Expr* ThisArg = getImplicitObjectArgument();
557 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000558 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000559
560 if (ThisArg->getType()->isAnyPointerType())
561 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
562
563 return ThisArg->getType()->getAsCXXRecordDecl();
564}
565
Douglas Gregoref986e82009-11-12 15:31:47 +0000566
Douglas Gregore200adc2008-10-27 19:41:14 +0000567//===----------------------------------------------------------------------===//
568// Named casts
569//===----------------------------------------------------------------------===//
570
571/// getCastName - Get the name of the C++ cast being used, e.g.,
572/// "static_cast", "dynamic_cast", "reinterpret_cast", or
573/// "const_cast". The returned pointer must not be freed.
574const char *CXXNamedCastExpr::getCastName() const {
575 switch (getStmtClass()) {
576 case CXXStaticCastExprClass: return "static_cast";
577 case CXXDynamicCastExprClass: return "dynamic_cast";
578 case CXXReinterpretCastExprClass: return "reinterpret_cast";
579 case CXXConstCastExprClass: return "const_cast";
580 default: return "<invalid cast>";
581 }
582}
Douglas Gregordd04d332009-01-16 18:33:17 +0000583
Craig Toppera31a8822013-08-22 07:09:37 +0000584CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000585 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000586 CastKind K, Expr *Op,
587 const CXXCastPath *BasePath,
588 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000589 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000590 SourceLocation RParenLoc,
591 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000592 unsigned PathSize = (BasePath ? BasePath->size() : 0);
593 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
594 + PathSize * sizeof(CXXBaseSpecifier*));
595 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000596 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000597 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000598 if (PathSize) E->setCastPath(*BasePath);
599 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) {
604 void *Buffer =
605 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
606 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
607}
608
Craig Toppera31a8822013-08-22 07:09:37 +0000609CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000610 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000611 CastKind K, Expr *Op,
612 const CXXCastPath *BasePath,
613 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000614 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000615 SourceLocation RParenLoc,
616 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000617 unsigned PathSize = (BasePath ? BasePath->size() : 0);
618 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
619 + PathSize * sizeof(CXXBaseSpecifier*));
620 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000621 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000622 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000623 if (PathSize) E->setCastPath(*BasePath);
624 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) {
629 void *Buffer =
630 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
631 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
632}
633
Anders Carlsson267c0c92011-04-11 01:43:55 +0000634/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
635/// to always be null. For example:
636///
637/// struct A { };
638/// struct B final : A { };
639/// struct C { };
640///
641/// C *f(B* b) { return dynamic_cast<C*>(b); }
642bool CXXDynamicCastExpr::isAlwaysNull() const
643{
644 QualType SrcType = getSubExpr()->getType();
645 QualType DestType = getType();
646
647 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
648 SrcType = SrcPTy->getPointeeType();
649 DestType = DestType->castAs<PointerType>()->getPointeeType();
650 }
651
Alexis Hunt78e2b912012-06-19 23:44:55 +0000652 if (DestType->isVoidType())
653 return false;
654
Anders Carlsson267c0c92011-04-11 01:43:55 +0000655 const CXXRecordDecl *SrcRD =
656 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
657
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000658 if (!SrcRD->hasAttr<FinalAttr>())
659 return false;
660
Anders Carlsson267c0c92011-04-11 01:43:55 +0000661 const CXXRecordDecl *DestRD =
662 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
663
664 return !DestRD->isDerivedFrom(SrcRD);
665}
666
John McCallcf142162010-08-07 06:22:56 +0000667CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000668CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
669 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000670 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000671 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000672 SourceLocation RParenLoc,
673 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000674 unsigned PathSize = (BasePath ? BasePath->size() : 0);
675 void *Buffer =
676 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
677 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000678 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000679 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000680 if (PathSize) E->setCastPath(*BasePath);
681 return E;
682}
683
684CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000685CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000686 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
687 + PathSize * sizeof(CXXBaseSpecifier*));
688 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
689}
690
Craig Toppera31a8822013-08-22 07:09:37 +0000691CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000692 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000693 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000694 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000695 SourceLocation RParenLoc,
696 SourceRange AngleBrackets) {
697 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000698}
699
Craig Toppera31a8822013-08-22 07:09:37 +0000700CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000701 return new (C) CXXConstCastExpr(EmptyShell());
702}
703
704CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000705CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000706 TypeSourceInfo *Written, CastKind K, Expr *Op,
707 const CXXCastPath *BasePath,
708 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000709 unsigned PathSize = (BasePath ? BasePath->size() : 0);
710 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
711 + PathSize * sizeof(CXXBaseSpecifier*));
712 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000713 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
John McCallcf142162010-08-07 06:22:56 +0000714 if (PathSize) E->setCastPath(*BasePath);
715 return E;
716}
717
718CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000719CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000720 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
721 + PathSize * sizeof(CXXBaseSpecifier*));
722 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
723}
724
Eli Friedman89fe0d52013-08-15 22:02:56 +0000725SourceLocation CXXFunctionalCastExpr::getLocStart() const {
726 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
727}
728
729SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
730 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
731}
732
Richard Smithc67fdd42012-03-07 08:35:16 +0000733UserDefinedLiteral::LiteralOperatorKind
734UserDefinedLiteral::getLiteralOperatorKind() const {
735 if (getNumArgs() == 0)
736 return LOK_Template;
737 if (getNumArgs() == 2)
738 return LOK_String;
739
740 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
741 QualType ParamTy =
742 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
743 if (ParamTy->isPointerType())
744 return LOK_Raw;
745 if (ParamTy->isAnyCharacterType())
746 return LOK_Character;
747 if (ParamTy->isIntegerType())
748 return LOK_Integer;
749 if (ParamTy->isFloatingType())
750 return LOK_Floating;
751
752 llvm_unreachable("unknown kind of literal operator");
753}
754
755Expr *UserDefinedLiteral::getCookedLiteral() {
756#ifndef NDEBUG
757 LiteralOperatorKind LOK = getLiteralOperatorKind();
758 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
759#endif
760 return getArg(0);
761}
762
763const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
764 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
765}
John McCallcf142162010-08-07 06:22:56 +0000766
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000767CXXDefaultArgExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000768CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
Douglas Gregor033f6752009-12-23 23:03:06 +0000769 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000770 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000771 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
772 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000773}
774
Craig Toppera31a8822013-08-22 07:09:37 +0000775CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000776 FieldDecl *Field, QualType T)
777 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
778 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
779 ? VK_XValue
780 : VK_RValue,
781 /*FIXME*/ OK_Ordinary, false, false, false, false),
782 Field(Field), Loc(Loc) {
783 assert(Field->hasInClassInitializer());
784}
785
Craig Toppera31a8822013-08-22 07:09:37 +0000786CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000787 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000788 return new (C) CXXTemporary(Destructor);
789}
790
Craig Toppera31a8822013-08-22 07:09:37 +0000791CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000792 CXXTemporary *Temp,
793 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000794 assert((SubExpr->getType()->isRecordType() ||
795 SubExpr->getType()->isArrayType()) &&
796 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000797
Douglas Gregora6e053e2010-12-15 01:34:56 +0000798 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000799}
800
Craig Toppera31a8822013-08-22 07:09:37 +0000801CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000802 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000803 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000804 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000805 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000806 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000807 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000808 bool StdInitListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000809 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000810 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
811 Type->getType().getNonReferenceType(),
812 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000813 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000814 HadMultipleCandidates,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000815 ListInitialization,
816 StdInitListInitialization,
817 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,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000839 bool StdInitListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000840 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000841 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000842 SourceRange ParenOrBraceRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000843 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000844 Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000845 HadMultipleCandidates, ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000846 StdInitListInitialization,
Sebastian Redla9351792012-02-11 23:51:47 +0000847 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000848 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000849}
850
Craig Toppera31a8822013-08-22 07:09:37 +0000851CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
852 QualType T, SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000853 CXXConstructorDecl *D, bool elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000854 ArrayRef<Expr*> args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000855 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000856 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000857 bool StdInitListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000858 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000859 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000860 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000861 : Expr(SC, T, VK_RValue, OK_Ordinary,
862 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000863 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000864 T->containsUnexpandedParameterPack()),
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000865 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
866 NumArgs(args.size()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000867 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000868 ListInitialization(ListInitialization),
Richard Smithf8adcdc2014-07-17 05:12:35 +0000869 StdInitListInitialization(StdInitListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000870 ZeroInitialization(ZeroInitialization),
Craig Topper36250ad2014-05-12 05:36:57 +0000871 ConstructKind(ConstructKind), Args(nullptr)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000872{
873 if (NumArgs) {
Benjamin Kramerc215e762012-08-24 11:54:20 +0000874 Args = new (C) Stmt*[args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000875
Benjamin Kramerc215e762012-08-24 11:54:20 +0000876 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000877 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000878
879 if (args[i]->isValueDependent())
880 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000881 if (args[i]->isInstantiationDependent())
882 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000883 if (args[i]->containsUnexpandedParameterPack())
884 ExprBits.ContainsUnexpandedParameterPack = true;
885
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000886 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000887 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000888 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000889}
890
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000891LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000892 LambdaCaptureKind Kind, VarDecl *Var,
893 SourceLocation EllipsisLoc)
Richard Smithba71c082013-05-16 06:20:58 +0000894 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000895{
896 unsigned Bits = 0;
897 if (Implicit)
898 Bits |= Capture_Implicit;
899
900 switch (Kind) {
Craig Topper36250ad2014-05-12 05:36:57 +0000901 case LCK_This:
902 assert(!Var && "'this' capture cannot have a variable!");
Douglas Gregore31e6062012-02-07 10:09:13 +0000903 break;
904
905 case LCK_ByCopy:
906 Bits |= Capture_ByCopy;
907 // Fall through
908 case LCK_ByRef:
909 assert(Var && "capture must have a variable!");
910 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000911 case LCK_VLAType:
912 assert(!Var && "VLA type capture cannot have a variable!");
913 Bits |= Capture_ByCopy;
914 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000915 }
Richard Smithba71c082013-05-16 06:20:58 +0000916 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000917}
918
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000919LambdaCaptureKind LambdaCapture::getCaptureKind() const {
Richard Smithba71c082013-05-16 06:20:58 +0000920 Decl *D = DeclAndBits.getPointer();
Alexey Bataev39c81e22014-08-28 04:28:19 +0000921 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
Richard Smithba71c082013-05-16 06:20:58 +0000922 if (!D)
Alexey Bataev39c81e22014-08-28 04:28:19 +0000923 return CapByCopy ? LCK_VLAType : LCK_This;
Douglas Gregore31e6062012-02-07 10:09:13 +0000924
Alexey Bataev39c81e22014-08-28 04:28:19 +0000925 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000926}
927
James Dennettddd36ff2013-08-09 23:08:25 +0000928LambdaExpr::LambdaExpr(QualType T,
Douglas Gregore31e6062012-02-07 10:09:13 +0000929 SourceRange IntroducerRange,
930 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000931 SourceLocation CaptureDefaultLoc,
932 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000933 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000934 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000935 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000936 ArrayRef<VarDecl *> ArrayIndexVars,
937 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000938 SourceLocation ClosingBrace,
939 bool ContainsUnexpandedParameterPack)
Douglas Gregore31e6062012-02-07 10:09:13 +0000940 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
941 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith2589b9802012-07-25 03:56:55 +0000942 ContainsUnexpandedParameterPack),
Douglas Gregore31e6062012-02-07 10:09:13 +0000943 IntroducerRange(IntroducerRange),
James Dennettddd36ff2013-08-09 23:08:25 +0000944 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregore5561632012-02-13 17:20:40 +0000945 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000946 CaptureDefault(CaptureDefault),
947 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000948 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000949 ClosingBrace(ClosingBrace)
950{
951 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000952 CXXRecordDecl *Class = getLambdaClass();
953 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000954
955 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000956
957 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000958 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000959 Data.NumCaptures = NumCaptures;
960 Data.NumExplicitCaptures = 0;
961 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
962 Capture *ToCapture = Data.Captures;
963 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
964 if (Captures[I].isExplicit())
965 ++Data.NumExplicitCaptures;
966
967 *ToCapture++ = Captures[I];
968 }
969
970 // Copy initialization expressions for the non-static data members.
971 Stmt **Stored = getStoredStmts();
972 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
973 *Stored++ = CaptureInits[I];
974
975 // Copy the body of the lambda.
976 *Stored++ = getCallOperator()->getBody();
977
978 // Copy the array index variables, if any.
979 HasArrayIndexVars = !ArrayIndexVars.empty();
980 if (HasArrayIndexVars) {
981 assert(ArrayIndexStarts.size() == NumCaptures);
982 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
983 sizeof(VarDecl *) * ArrayIndexVars.size());
984 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
985 sizeof(unsigned) * Captures.size());
986 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000987 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000988}
989
Craig Toppera31a8822013-08-22 07:09:37 +0000990LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
Douglas Gregore31e6062012-02-07 10:09:13 +0000991 CXXRecordDecl *Class,
992 SourceRange IntroducerRange,
993 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000994 SourceLocation CaptureDefaultLoc,
995 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000996 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000997 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000998 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000999 ArrayRef<VarDecl *> ArrayIndexVars,
1000 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001001 SourceLocation ClosingBrace,
1002 bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001003 // Determine the type of the expression (i.e., the type of the
1004 // function object we're creating).
1005 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +00001006
Douglas Gregore5561632012-02-13 17:20:40 +00001007 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smithc7520bf2012-08-21 05:42:49 +00001008 if (!ArrayIndexVars.empty()) {
1009 Size += sizeof(unsigned) * (Captures.size() + 1);
1010 // Realign for following VarDecl array.
Richard Smitha75e1cf2012-08-21 18:18:06 +00001011 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smithc7520bf2012-08-21 05:42:49 +00001012 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
1013 }
Douglas Gregore5561632012-02-13 17:20:40 +00001014 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +00001015 return new (Mem) LambdaExpr(T, IntroducerRange,
1016 CaptureDefault, CaptureDefaultLoc, Captures,
1017 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001018 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001019 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +00001020}
1021
Craig Toppera31a8822013-08-22 07:09:37 +00001022LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1023 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +00001024 unsigned NumArrayIndexVars) {
1025 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
1026 if (NumArrayIndexVars)
1027 Size += sizeof(VarDecl) * NumArrayIndexVars
1028 + sizeof(unsigned) * (NumCaptures + 1);
1029 void *Mem = C.Allocate(Size);
1030 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1031}
1032
Douglas Gregorc8a73492012-02-13 15:44:47 +00001033LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001034 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001035}
1036
1037LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001038 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001039}
1040
James Dennett1575cb42014-05-27 19:13:04 +00001041LambdaExpr::capture_range LambdaExpr::captures() const {
1042 return capture_range(capture_begin(), capture_end());
1043}
1044
Douglas Gregorc8a73492012-02-13 15:44:47 +00001045LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1046 return capture_begin();
1047}
1048
1049LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1050 struct CXXRecordDecl::LambdaDefinitionData &Data
1051 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001052 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001053}
1054
James Dennett1575cb42014-05-27 19:13:04 +00001055LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1056 return capture_range(explicit_capture_begin(), explicit_capture_end());
1057}
1058
Douglas Gregorc8a73492012-02-13 15:44:47 +00001059LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1060 return explicit_capture_end();
1061}
1062
1063LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1064 return capture_end();
1065}
1066
James Dennett1575cb42014-05-27 19:13:04 +00001067LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1068 return capture_range(implicit_capture_begin(), implicit_capture_end());
1069}
1070
Douglas Gregor54fcea62012-02-13 16:35:30 +00001071ArrayRef<VarDecl *>
1072LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001073 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001074
1075 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001076 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1077 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +00001078 VarDecl **IndexVars = getArrayIndexVars();
1079 unsigned *IndexStarts = getArrayIndexStarts();
Craig Topper5fc8fc22014-08-27 06:28:36 +00001080 return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
1081 IndexVars + IndexStarts[Index + 1]);
Douglas Gregor54fcea62012-02-13 16:35:30 +00001082}
1083
Douglas Gregore31e6062012-02-07 10:09:13 +00001084CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1085 return getType()->getAsCXXRecordDecl();
1086}
1087
1088CXXMethodDecl *LambdaExpr::getCallOperator() const {
1089 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001090 return Record->getLambdaCallOperator();
1091}
1092
1093TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1094 CXXRecordDecl *Record = getLambdaClass();
1095 return Record->getGenericLambdaTemplateParameterList();
1096
Douglas Gregore31e6062012-02-07 10:09:13 +00001097}
1098
Douglas Gregor99ae8062012-02-14 17:54:36 +00001099CompoundStmt *LambdaExpr::getBody() const {
1100 if (!getStoredStmts()[NumCaptures])
1101 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1102
1103 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1104}
1105
Douglas Gregore31e6062012-02-07 10:09:13 +00001106bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001107 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001108}
1109
John McCall28fc7092011-11-10 05:35:25 +00001110ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1111 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001112 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001113 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001114 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001115 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001116 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001117 SubExpr(subexpr) {
1118 ExprWithCleanupsBits.NumObjects = objects.size();
1119 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1120 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001121}
1122
Craig Toppera31a8822013-08-22 07:09:37 +00001123ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001124 ArrayRef<CleanupObject> objects) {
1125 size_t size = sizeof(ExprWithCleanups)
1126 + objects.size() * sizeof(CleanupObject);
1127 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1128 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001129}
1130
John McCall28fc7092011-11-10 05:35:25 +00001131ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1132 : Expr(ExprWithCleanupsClass, empty) {
1133 ExprWithCleanupsBits.NumObjects = numObjects;
1134}
Chris Lattnercba86142010-05-10 00:25:06 +00001135
Craig Toppera31a8822013-08-22 07:09:37 +00001136ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1137 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001138 unsigned numObjects) {
1139 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1140 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1141 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001142}
1143
Douglas Gregor2b88c112010-09-08 00:15:04 +00001144CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001145 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001146 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001147 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001148 : Expr(CXXUnresolvedConstructExprClass,
1149 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001150 (Type->getType()->isLValueReferenceType() ? VK_LValue
1151 :Type->getType()->isRValueReferenceType()? VK_XValue
1152 :VK_RValue),
1153 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001154 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001155 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001156 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001157 LParenLoc(LParenLoc),
1158 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001159 NumArgs(Args.size()) {
Douglas Gregorce934142009-05-20 18:46:25 +00001160 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001161 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001162 if (Args[I]->containsUnexpandedParameterPack())
1163 ExprBits.ContainsUnexpandedParameterPack = true;
1164
1165 StoredArgs[I] = Args[I];
1166 }
Douglas Gregorce934142009-05-20 18:46:25 +00001167}
1168
1169CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001170CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001171 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001172 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001173 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001174 SourceLocation RParenLoc) {
1175 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001176 sizeof(Expr *) * Args.size());
1177 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001178}
1179
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001180CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001181CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001182 Stmt::EmptyShell Empty;
1183 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1184 sizeof(Expr *) * NumArgs);
1185 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1186}
1187
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001188SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1189 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001190}
1191
Craig Toppera31a8822013-08-22 07:09:37 +00001192CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001193 Expr *Base, QualType BaseType,
1194 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001195 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001196 NestedNameSpecifierLoc QualifierLoc,
1197 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001198 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001199 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001200 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001201 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001202 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001203 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001204 (QualifierLoc &&
1205 QualifierLoc.getNestedNameSpecifier()
1206 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001207 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001208 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Craig Topper36250ad2014-05-12 05:36:57 +00001209 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1210 TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001211 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001212 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001213 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001214 if (TemplateArgs) {
1215 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001216 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001217 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001218 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1219 Dependent,
1220 InstantiationDependent,
1221 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001222 if (ContainsUnexpandedParameterPack)
1223 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001224 } else if (TemplateKWLoc.isValid()) {
1225 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001226 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001227}
1228
Craig Toppera31a8822013-08-22 07:09:37 +00001229CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001230 Expr *Base, QualType BaseType,
1231 bool IsArrow,
1232 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001233 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001234 NamedDecl *FirstQualifierFoundInScope,
1235 DeclarationNameInfo MemberNameInfo)
1236 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001237 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001238 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001239 (QualifierLoc &&
1240 QualifierLoc.getNestedNameSpecifier()->
1241 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001242 MemberNameInfo.containsUnexpandedParameterPack())),
1243 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001244 HasTemplateKWAndArgsInfo(false),
1245 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001246 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1247 MemberNameInfo(MemberNameInfo) { }
1248
John McCall8cd78132009-11-19 22:55:06 +00001249CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001250CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001251 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001252 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001253 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001254 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001255 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001256 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001257 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001258 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001259 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1260 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001261 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001262 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001263 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001264
Abramo Bagnara7945c982012-01-27 09:46:47 +00001265 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1266 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1267 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001268
Chris Lattner5c0b4052010-10-30 05:14:06 +00001269 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001270 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1271 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001272 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001273 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001274 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001275 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001276}
1277
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001278CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001279CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001280 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001281 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001282 if (!HasTemplateKWAndArgsInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00001283 return new (C) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001284 0, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001285 NestedNameSpecifierLoc(),
1286 nullptr, DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001287
1288 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001289 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001290 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001291 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001292 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001293 0, SourceLocation(),
1294 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001295 SourceLocation(), nullptr,
1296 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001297 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001298 return E;
1299}
1300
Douglas Gregor0da1d432011-02-28 20:01:57 +00001301bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001302 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001303 return true;
1304
Douglas Gregor25b7e052011-03-02 21:06:53 +00001305 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001306}
1307
John McCall0009fcc2011-04-26 20:42:42 +00001308static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1309 UnresolvedSetIterator end) {
1310 do {
1311 NamedDecl *decl = *begin;
1312 if (isa<UnresolvedUsingValueDecl>(decl))
1313 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001314
1315 // Unresolved member expressions should only contain methods and
1316 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001317 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1318 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001319 return false;
1320 } while (++begin != end);
1321
1322 return true;
1323}
1324
Craig Toppera31a8822013-08-22 07:09:37 +00001325UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001326 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001327 Expr *Base, QualType BaseType,
1328 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001329 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001330 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001331 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001332 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001333 const TemplateArgumentListInfo *TemplateArgs,
1334 UnresolvedSetIterator Begin,
1335 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001336 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1337 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001338 // Dependent
1339 ((Base && Base->isTypeDependent()) ||
1340 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001341 ((Base && Base->isInstantiationDependent()) ||
1342 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001343 // Contains unexpanded parameter pack
1344 ((Base && Base->containsUnexpandedParameterPack()) ||
1345 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001346 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1347 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001348
1349 // Check whether all of the members are non-static member functions,
1350 // and if so, mark give this bound-member type instead of overload type.
1351 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1352 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001353}
1354
Douglas Gregor0da1d432011-02-28 20:01:57 +00001355bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001356 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001357 return true;
1358
Douglas Gregor25b7e052011-03-02 21:06:53 +00001359 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001360}
1361
John McCall10eae182009-11-30 22:42:35 +00001362UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001363UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001364 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001365 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001366 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001367 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001368 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001369 const TemplateArgumentListInfo *TemplateArgs,
1370 UnresolvedSetIterator Begin,
1371 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001372 std::size_t size = sizeof(UnresolvedMemberExpr);
1373 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001374 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1375 else if (TemplateKWLoc.isValid())
1376 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001377
Chris Lattner5c0b4052010-10-30 05:14:06 +00001378 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001379 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001380 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001381 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001382 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001383}
1384
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001385UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001386UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1387 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001388 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001389 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001390 if (HasTemplateKWAndArgsInfo)
1391 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001392
Chris Lattner5c0b4052010-10-30 05:14:06 +00001393 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001394 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001395 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001396 return E;
1397}
1398
John McCall58cc69d2010-01-27 01:50:18 +00001399CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1400 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1401
1402 // If there was a nested name specifier, it names the naming class.
1403 // It can't be dependent: after all, we were actually able to do the
1404 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001405 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001406 auto *NNS = getQualifier();
1407 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001408 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001409 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001410 Record = T->getAsCXXRecordDecl();
1411 assert(Record && "qualifier in member expression does not name record");
1412 }
John McCall58cc69d2010-01-27 01:50:18 +00001413 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001414 else {
John McCall58cc69d2010-01-27 01:50:18 +00001415 QualType BaseType = getBaseType().getNonReferenceType();
1416 if (isArrow()) {
1417 const PointerType *PT = BaseType->getAs<PointerType>();
1418 assert(PT && "base of arrow member access is not pointer");
1419 BaseType = PT->getPointeeType();
1420 }
1421
Douglas Gregor9262f472010-04-27 18:19:34 +00001422 Record = BaseType->getAsCXXRecordDecl();
1423 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001424 }
1425
Douglas Gregor9262f472010-04-27 18:19:34 +00001426 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001427}
1428
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001429SubstNonTypeTemplateParmPackExpr::
1430SubstNonTypeTemplateParmPackExpr(QualType T,
1431 NonTypeTemplateParmDecl *Param,
1432 SourceLocation NameLoc,
1433 const TemplateArgument &ArgPack)
1434 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001435 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001436 Param(Param), Arguments(ArgPack.pack_begin()),
1437 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1438
1439TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1440 return TemplateArgument(Arguments, NumArguments);
1441}
1442
Richard Smithb15fe3a2012-09-12 00:56:43 +00001443FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1444 SourceLocation NameLoc,
1445 unsigned NumParams,
1446 Decl * const *Params)
1447 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1448 true, true, true, true),
1449 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1450 if (Params)
1451 std::uninitialized_copy(Params, Params + NumParams,
1452 reinterpret_cast<Decl**>(this+1));
1453}
1454
1455FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001456FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001457 ParmVarDecl *ParamPack, SourceLocation NameLoc,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001458 ArrayRef<Decl *> Params) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001459 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1460 sizeof(ParmVarDecl*) * Params.size()))
1461 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1462}
1463
1464FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001465FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1466 unsigned NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001467 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1468 sizeof(ParmVarDecl*) * NumParams))
Craig Topper36250ad2014-05-12 05:36:57 +00001469 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001470}
1471
David Majnemerdaff3702014-05-01 17:50:17 +00001472void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1473 unsigned ManglingNumber) {
1474 // We only need extra state if we have to remember more than just the Stmt.
1475 if (!ExtendedBy)
1476 return;
1477
1478 // We may need to allocate extra storage for the mangling number and the
1479 // extended-by ValueDecl.
1480 if (!State.is<ExtraState *>()) {
1481 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1482 ES->Temporary = State.get<Stmt *>();
1483 State = ES;
1484 }
1485
1486 auto ES = State.get<ExtraState *>();
1487 ES->ExtendingDecl = ExtendedBy;
1488 ES->ManglingNumber = ManglingNumber;
1489}
1490
Douglas Gregor29c42f22012-02-24 07:38:34 +00001491TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1492 ArrayRef<TypeSourceInfo *> Args,
1493 SourceLocation RParenLoc,
1494 bool Value)
1495 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1496 /*TypeDependent=*/false,
1497 /*ValueDependent=*/false,
1498 /*InstantiationDependent=*/false,
1499 /*ContainsUnexpandedParameterPack=*/false),
1500 Loc(Loc), RParenLoc(RParenLoc)
1501{
1502 TypeTraitExprBits.Kind = Kind;
1503 TypeTraitExprBits.Value = Value;
1504 TypeTraitExprBits.NumArgs = Args.size();
1505
1506 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1507
1508 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1509 if (Args[I]->getType()->isDependentType())
1510 setValueDependent(true);
1511 if (Args[I]->getType()->isInstantiationDependentType())
1512 setInstantiationDependent(true);
1513 if (Args[I]->getType()->containsUnexpandedParameterPack())
1514 setContainsUnexpandedParameterPack(true);
1515
1516 ToArgs[I] = Args[I];
1517 }
1518}
1519
Craig Toppera31a8822013-08-22 07:09:37 +00001520TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001521 SourceLocation Loc,
1522 TypeTrait Kind,
1523 ArrayRef<TypeSourceInfo *> Args,
1524 SourceLocation RParenLoc,
1525 bool Value) {
1526 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1527 void *Mem = C.Allocate(Size);
1528 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1529}
1530
Craig Toppera31a8822013-08-22 07:09:37 +00001531TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001532 unsigned NumArgs) {
1533 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1534 void *Mem = C.Allocate(Size);
1535 return new (Mem) TypeTraitExpr(EmptyShell());
1536}
1537
David Blaikie68e081d2011-12-20 02:48:34 +00001538void ArrayTypeTraitExpr::anchor() { }