blob: 2a93f71429ec569046ab091707bf98edf8e20c17 [file] [log] [blame]
Ted Kremeneke3a0c142007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremeneke3a0c142007-08-24 20:21:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the subclesses of Expr class declared in ExprCXX.h
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000014#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Douglas Gregor993603d2008-11-14 16:09:21 +000016#include "clang/AST/DeclCXX.h"
Douglas Gregora727cb92009-06-30 22:34:41 +000017#include "clang/AST/DeclTemplate.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000018#include "clang/AST/ExprCXX.h"
Douglas Gregor651fe5e2010-02-24 23:40:28 +000019#include "clang/AST/TypeLoc.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000020#include "clang/Basic/IdentifierTable.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000021using namespace clang;
22
Douglas Gregor9da64192010-04-26 22:37:10 +000023
Ted Kremeneke3a0c142007-08-24 20:21:10 +000024//===----------------------------------------------------------------------===//
25// Child Iterators for iterating over subexpressions/substatements
26//===----------------------------------------------------------------------===//
27
Richard Smithef8bf432012-08-13 20:08:14 +000028bool CXXTypeidExpr::isPotentiallyEvaluated() const {
29 if (isTypeOperand())
30 return false;
31
32 // C++11 [expr.typeid]p3:
33 // When typeid is applied to an expression other than a glvalue of
34 // polymorphic class type, [...] the expression is an unevaluated operand.
35 const Expr *E = getExprOperand();
36 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
37 if (RD->isPolymorphic() && E->isGLValue())
38 return true;
39
40 return false;
41}
42
David Majnemer143c55e2013-09-27 07:04:31 +000043QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
Douglas Gregor9da64192010-04-26 22:37:10 +000044 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000045 Qualifiers Quals;
46 return Context.getUnqualifiedArrayType(
47 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Douglas Gregor9da64192010-04-26 22:37:10 +000048}
49
David Majnemer143c55e2013-09-27 07:04:31 +000050QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
Francois Pichet9f4f2072010-09-08 12:20:18 +000051 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000052 Qualifiers Quals;
53 return Context.getUnqualifiedArrayType(
54 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Francois Pichet9f4f2072010-09-08 12:20:18 +000055}
56
Nico Webercf4ff5862012-10-11 10:13:44 +000057// static
David Majnemer48b08632014-07-14 23:12:54 +000058const UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT,
59 bool *RDHasMultipleGUIDsPtr) {
Nico Webercf4ff5862012-10-11 10:13:44 +000060 // Optionally remove one level of pointer, reference or array indirection.
61 const Type *Ty = QT.getTypePtr();
62 if (QT->isPointerType() || QT->isReferenceType())
63 Ty = QT->getPointeeType().getTypePtr();
64 else if (QT->isArrayType())
David Majnemer68c880b2013-09-27 07:57:34 +000065 Ty = Ty->getBaseElementTypeUnsafe();
Nico Webercf4ff5862012-10-11 10:13:44 +000066
David Majnemer6d476652014-07-15 04:30:17 +000067 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
David Majnemer59c0ec22013-09-07 06:59:46 +000068 if (!RD)
Craig Topper36250ad2014-05-12 05:36:57 +000069 return nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +000070
David Majnemer6d476652014-07-15 04:30:17 +000071 if (const UuidAttr *Uuid = RD->getMostRecentDecl()->getAttr<UuidAttr>())
72 return Uuid;
David Majnemer37de6112014-07-14 23:40:24 +000073
David Majnemer5d22e7e2013-09-07 07:11:04 +000074 // __uuidof can grab UUIDs from template arguments.
David Majnemer6d476652014-07-15 04:30:17 +000075 if (const ClassTemplateSpecializationDecl *CTSD =
David Majnemer59c0ec22013-09-07 06:59:46 +000076 dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
77 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
David Majnemer48b08632014-07-14 23:12:54 +000078 const UuidAttr *UuidForRD = nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +000079
David Majnemer48b08632014-07-14 23:12:54 +000080 for (const TemplateArgument &TA : TAL.asArray()) {
David Majnemer59c0ec22013-09-07 06:59:46 +000081 bool SeenMultipleGUIDs = false;
82
David Majnemer48b08632014-07-14 23:12:54 +000083 const UuidAttr *UuidForTA = nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +000084 if (TA.getKind() == TemplateArgument::Type)
85 UuidForTA = GetUuidAttrOfType(TA.getAsType(), &SeenMultipleGUIDs);
86 else if (TA.getKind() == TemplateArgument::Declaration)
87 UuidForTA =
88 GetUuidAttrOfType(TA.getAsDecl()->getType(), &SeenMultipleGUIDs);
89
90 // If the template argument has a UUID, there are three cases:
91 // - This is the first UUID seen for this RecordDecl.
David Majnemer37c921e2013-09-07 20:21:47 +000092 // - This is a different UUID than previously seen for this RecordDecl.
93 // - This is the same UUID than previously seen for this RecordDecl.
David Majnemer59c0ec22013-09-07 06:59:46 +000094 if (UuidForTA) {
95 if (!UuidForRD)
96 UuidForRD = UuidForTA;
97 else if (UuidForRD != UuidForTA)
98 SeenMultipleGUIDs = true;
99 }
100
101 // Seeing multiple UUIDs means that we couldn't find a UUID
102 if (SeenMultipleGUIDs) {
103 if (RDHasMultipleGUIDsPtr)
104 *RDHasMultipleGUIDsPtr = true;
Craig Topper36250ad2014-05-12 05:36:57 +0000105 return nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +0000106 }
107 }
108
109 return UuidForRD;
David Majnemer5d22e7e2013-09-07 07:11:04 +0000110 }
111
Craig Topper36250ad2014-05-12 05:36:57 +0000112 return nullptr;
Nico Webercf4ff5862012-10-11 10:13:44 +0000113}
114
David Majnemer8eaab6f2013-08-13 06:32:20 +0000115StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const {
116 StringRef Uuid;
117 if (isTypeOperand())
David Majnemer143c55e2013-09-27 07:04:31 +0000118 Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand(Context))->getGuid();
David Majnemer8eaab6f2013-08-13 06:32:20 +0000119 else {
120 // Special case: __uuidof(0) means an all-zero GUID.
121 Expr *Op = getExprOperand();
122 if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
123 Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
124 else
125 Uuid = "00000000-0000-0000-0000-000000000000";
126 }
127 return Uuid;
128}
129
Douglas Gregor747eb782010-07-08 06:14:04 +0000130// CXXScalarValueInitExpr
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000131SourceLocation CXXScalarValueInitExpr::getLocStart() const {
132 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +0000133}
134
Sebastian Redlbd150f42008-11-21 19:14:01 +0000135// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +0000136CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
137 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +0000138 bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000139 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +0000140 SourceRange typeIdParens, Expr *arraySize,
141 InitializationStyle initializationStyle,
142 Expr *initializer, QualType ty,
143 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +0000144 SourceRange Range, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +0000145 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000146 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000147 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000148 ty->containsUnexpandedParameterPack()),
Craig Topper36250ad2014-05-12 05:36:57 +0000149 SubExprs(nullptr), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
Sebastian Redl6047f072012-02-16 12:22:20 +0000150 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
David Blaikie7b97aef2012-11-07 00:12:38 +0000151 Range(Range), DirectInitRange(directInitRange),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000152 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Craig Topper36250ad2014-05-12 05:36:57 +0000153 assert((initializer != nullptr || initializationStyle == NoInit) &&
Sebastian Redl6047f072012-02-16 12:22:20 +0000154 "Only NoInit can have no initializer.");
155 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Craig Topper36250ad2014-05-12 05:36:57 +0000156 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
157 initializer != nullptr);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000158 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000159 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000160 if (arraySize->isInstantiationDependent())
161 ExprBits.InstantiationDependent = true;
162
Douglas Gregora6e053e2010-12-15 01:34:56 +0000163 if (arraySize->containsUnexpandedParameterPack())
164 ExprBits.ContainsUnexpandedParameterPack = true;
165
Sebastian Redl351bb782008-12-02 14:43:59 +0000166 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000167 }
168
Sebastian Redl6047f072012-02-16 12:22:20 +0000169 if (initializer) {
170 if (initializer->isInstantiationDependent())
171 ExprBits.InstantiationDependent = true;
172
173 if (initializer->containsUnexpandedParameterPack())
174 ExprBits.ContainsUnexpandedParameterPack = true;
175
176 SubExprs[i++] = initializer;
177 }
178
Benjamin Kramerc215e762012-08-24 11:54:20 +0000179 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000180 if (placementArgs[j]->isInstantiationDependent())
181 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000182 if (placementArgs[j]->containsUnexpandedParameterPack())
183 ExprBits.ContainsUnexpandedParameterPack = true;
184
Sebastian Redlbd150f42008-11-21 19:14:01 +0000185 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000186 }
David Blaikie3a0de212012-11-08 22:53:48 +0000187
188 switch (getInitializationStyle()) {
189 case CallInit:
190 this->Range.setEnd(DirectInitRange.getEnd()); break;
191 case ListInit:
192 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000193 default:
194 if (TypeIdParens.isValid())
195 this->Range.setEnd(TypeIdParens.getEnd());
196 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000197 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000198}
199
Craig Toppera31a8822013-08-22 07:09:37 +0000200void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000201 unsigned numPlaceArgs, bool hasInitializer){
Craig Topper36250ad2014-05-12 05:36:57 +0000202 assert(SubExprs == nullptr && "SubExprs already allocated");
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000203 Array = isArray;
204 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000205
206 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000207 SubExprs = new (C) Stmt*[TotalSize];
208}
209
Craig Toppera31a8822013-08-22 07:09:37 +0000210bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
Richard Smith902a0232015-02-14 01:52:20 +0000211 return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow(
212 Ctx) &&
213 !getOperatorNew()->isReservedGlobalPlacementOperator();
John McCall75f94982011-03-07 03:12:35 +0000214}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000215
Sebastian Redlbd150f42008-11-21 19:14:01 +0000216// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000217QualType CXXDeleteExpr::getDestroyedType() const {
218 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000219 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000220 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000221
222 if (ArgType->isDependentType() && !ArgType->isPointerType())
223 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000224
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000225 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000226}
227
Douglas Gregorad8a3362009-09-04 17:36:40 +0000228// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000229PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
230 : Type(Info)
231{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000232 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000233}
234
Craig Toppera31a8822013-08-22 07:09:37 +0000235CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000236 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
237 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
238 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
239 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000240 : Expr(CXXPseudoDestructorExprClass,
David Majnemerced8bdf2015-02-25 17:36:15 +0000241 Context.BoundMemberTy,
John McCalldb40c7f2010-12-14 08:05:40 +0000242 VK_RValue, OK_Ordinary,
243 /*isTypeDependent=*/(Base->isTypeDependent() ||
244 (DestroyedType.getTypeSourceInfo() &&
245 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000246 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000247 (Base->isInstantiationDependent() ||
248 (QualifierLoc &&
249 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
250 (ScopeType &&
251 ScopeType->getType()->isInstantiationDependentType()) ||
252 (DestroyedType.getTypeSourceInfo() &&
253 DestroyedType.getTypeSourceInfo()->getType()
254 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000255 // ContainsUnexpandedParameterPack
256 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000257 (QualifierLoc &&
258 QualifierLoc.getNestedNameSpecifier()
259 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000260 (ScopeType &&
261 ScopeType->getType()->containsUnexpandedParameterPack()) ||
262 (DestroyedType.getTypeSourceInfo() &&
263 DestroyedType.getTypeSourceInfo()->getType()
264 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000265 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000266 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000267 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
268 DestroyedType(DestroyedType) { }
269
Douglas Gregor678f90d2010-02-25 01:56:36 +0000270QualType CXXPseudoDestructorExpr::getDestroyedType() const {
271 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
272 return TInfo->getType();
273
274 return QualType();
275}
276
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000277SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000278 SourceLocation End = DestroyedType.getLocation();
279 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000280 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000281 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000282}
283
John McCalld14a8642009-11-21 08:51:07 +0000284// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000285UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000286UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000287 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000288 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000289 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000290 const DeclarationNameInfo &NameInfo,
291 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000292 const TemplateArgumentListInfo *Args,
293 UnresolvedSetIterator Begin,
294 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000295{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000296 assert(Args || TemplateKWLoc.isValid());
297 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000298 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000299 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000300 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
301 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000302 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000303 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000304}
305
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000306UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000307UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000308 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000309 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000310 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000311 if (HasTemplateKWAndArgsInfo)
312 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000313
Chris Lattner5c0b4052010-10-30 05:14:06 +0000314 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000315 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000316 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000317 return E;
318}
319
Craig Toppera31a8822013-08-22 07:09:37 +0000320OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000321 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000322 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000323 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000324 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000325 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000326 UnresolvedSetIterator End,
327 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000328 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000329 bool KnownContainsUnexpandedParameterPack)
330 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
331 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000332 (KnownInstantiationDependent ||
333 NameInfo.isInstantiationDependent() ||
334 (QualifierLoc &&
335 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000336 (KnownContainsUnexpandedParameterPack ||
337 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000338 (QualifierLoc &&
339 QualifierLoc.getNestedNameSpecifier()
340 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000341 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
Craig Topper36250ad2014-05-12 05:36:57 +0000342 Results(nullptr), NumResults(End - Begin),
343 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
344 TemplateKWLoc.isValid()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000345 NumResults = End - Begin;
346 if (NumResults) {
347 // Determine whether this expression is type-dependent.
348 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
349 if ((*I)->getDeclContext()->isDependentContext() ||
350 isa<UnresolvedUsingValueDecl>(*I)) {
351 ExprBits.TypeDependent = true;
352 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000353 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000354 }
355 }
356
357 Results = static_cast<DeclAccessPair *>(
358 C.Allocate(sizeof(DeclAccessPair) * NumResults,
359 llvm::alignOf<DeclAccessPair>()));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000360 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregora6e053e2010-12-15 01:34:56 +0000361 }
362
363 // If we have explicit template arguments, check for dependent
364 // template arguments and whether they contain any unexpanded pack
365 // expansions.
366 if (TemplateArgs) {
367 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000368 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000369 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000370 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
371 Dependent,
372 InstantiationDependent,
373 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000374
375 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000376 ExprBits.TypeDependent = true;
377 ExprBits.ValueDependent = true;
378 }
379 if (InstantiationDependent)
380 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000381 if (ContainsUnexpandedParameterPack)
382 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000383 } else if (TemplateKWLoc.isValid()) {
384 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000385 }
386
387 if (isTypeDependent())
388 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000389}
390
Craig Toppera31a8822013-08-22 07:09:37 +0000391void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000392 UnresolvedSetIterator Begin,
393 UnresolvedSetIterator End) {
Craig Topper36250ad2014-05-12 05:36:57 +0000394 assert(!Results && "Results already initialized!");
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000395 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000396 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000397 Results = static_cast<DeclAccessPair *>(
398 C.Allocate(sizeof(DeclAccessPair) * NumResults,
399
400 llvm::alignOf<DeclAccessPair>()));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000401 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000402 }
403}
404
John McCall8c12dc42010-04-22 18:44:12 +0000405CXXRecordDecl *OverloadExpr::getNamingClass() const {
406 if (isa<UnresolvedLookupExpr>(this))
407 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
408 else
409 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
410}
411
John McCall8cd78132009-11-19 22:55:06 +0000412// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000413DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000414 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000415 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000416 const DeclarationNameInfo &NameInfo,
417 const TemplateArgumentListInfo *Args)
418 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
419 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000420 (NameInfo.isInstantiationDependent() ||
421 (QualifierLoc &&
422 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000423 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000424 (QualifierLoc &&
425 QualifierLoc.getNestedNameSpecifier()
426 ->containsUnexpandedParameterPack()))),
427 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Craig Topper36250ad2014-05-12 05:36:57 +0000428 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000429{
430 if (Args) {
431 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000432 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000433 bool ContainsUnexpandedParameterPack
434 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000435 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
436 Dependent,
437 InstantiationDependent,
438 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000439 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000440 } else if (TemplateKWLoc.isValid()) {
441 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000442 }
443}
444
John McCalle66edc12009-11-24 19:00:30 +0000445DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000446DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000447 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000448 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000449 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000450 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000451 assert(QualifierLoc && "should be created for dependent qualifiers");
John McCalle66edc12009-11-24 19:00:30 +0000452 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000453 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000454 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
455 else if (TemplateKWLoc.isValid())
456 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000457 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000458 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
459 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000460}
461
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000462DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000463DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000464 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000465 unsigned NumTemplateArgs) {
466 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000467 if (HasTemplateKWAndArgsInfo)
468 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000469 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000470 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000471 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000472 SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000473 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000474 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000475 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000476}
477
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000478SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000479 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000480 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
481 return Loc;
482}
483
484SourceLocation CXXConstructExpr::getLocEnd() const {
485 if (isa<CXXTemporaryObjectExpr>(this))
486 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000487
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000488 if (ParenOrBraceRange.isValid())
489 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000490
491 SourceLocation End = Loc;
492 for (unsigned I = getNumArgs(); I > 0; --I) {
493 const Expr *Arg = getArg(I-1);
494 if (!Arg->isDefaultArgument()) {
495 SourceLocation NewEnd = Arg->getLocEnd();
496 if (NewEnd.isValid()) {
497 End = NewEnd;
498 break;
499 }
500 }
501 }
502
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000503 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000504}
505
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000506SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000507 OverloadedOperatorKind Kind = getOperator();
508 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
509 if (getNumArgs() == 1)
510 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000511 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000512 else
513 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000514 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000515 } else if (Kind == OO_Arrow) {
516 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000517 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000518 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000519 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000520 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000521 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000522 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000523 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000524 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000525 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000526 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000527 }
528}
529
Ted Kremenek98a24e32011-03-30 17:41:19 +0000530Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000531 const Expr *Callee = getCallee()->IgnoreParens();
532 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000533 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000534 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
535 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
536 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000537
538 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000539 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000540}
541
Ted Kremenek98a24e32011-03-30 17:41:19 +0000542CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
543 if (const MemberExpr *MemExpr =
544 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
545 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
546
547 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000548 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000549}
550
551
David Blaikiec0f58662012-05-03 16:25:49 +0000552CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000553 Expr* ThisArg = getImplicitObjectArgument();
554 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000555 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000556
557 if (ThisArg->getType()->isAnyPointerType())
558 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
559
560 return ThisArg->getType()->getAsCXXRecordDecl();
561}
562
Douglas Gregoref986e82009-11-12 15:31:47 +0000563
Douglas Gregore200adc2008-10-27 19:41:14 +0000564//===----------------------------------------------------------------------===//
565// Named casts
566//===----------------------------------------------------------------------===//
567
568/// getCastName - Get the name of the C++ cast being used, e.g.,
569/// "static_cast", "dynamic_cast", "reinterpret_cast", or
570/// "const_cast". The returned pointer must not be freed.
571const char *CXXNamedCastExpr::getCastName() const {
572 switch (getStmtClass()) {
573 case CXXStaticCastExprClass: return "static_cast";
574 case CXXDynamicCastExprClass: return "dynamic_cast";
575 case CXXReinterpretCastExprClass: return "reinterpret_cast";
576 case CXXConstCastExprClass: return "const_cast";
577 default: return "<invalid cast>";
578 }
579}
Douglas Gregordd04d332009-01-16 18:33:17 +0000580
Craig Toppera31a8822013-08-22 07:09:37 +0000581CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000582 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000583 CastKind K, Expr *Op,
584 const CXXCastPath *BasePath,
585 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000586 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000587 SourceLocation RParenLoc,
588 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000589 unsigned PathSize = (BasePath ? BasePath->size() : 0);
590 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
591 + PathSize * sizeof(CXXBaseSpecifier*));
592 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000593 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000594 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000595 if (PathSize) E->setCastPath(*BasePath);
596 return E;
597}
598
Craig Toppera31a8822013-08-22 07:09:37 +0000599CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000600 unsigned PathSize) {
601 void *Buffer =
602 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
603 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
604}
605
Craig Toppera31a8822013-08-22 07:09:37 +0000606CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000607 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000608 CastKind K, Expr *Op,
609 const CXXCastPath *BasePath,
610 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000611 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000612 SourceLocation RParenLoc,
613 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000614 unsigned PathSize = (BasePath ? BasePath->size() : 0);
615 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
616 + PathSize * sizeof(CXXBaseSpecifier*));
617 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000618 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000619 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000620 if (PathSize) E->setCastPath(*BasePath);
621 return E;
622}
623
Craig Toppera31a8822013-08-22 07:09:37 +0000624CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000625 unsigned PathSize) {
626 void *Buffer =
627 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
628 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
629}
630
Anders Carlsson267c0c92011-04-11 01:43:55 +0000631/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
632/// to always be null. For example:
633///
634/// struct A { };
635/// struct B final : A { };
636/// struct C { };
637///
638/// C *f(B* b) { return dynamic_cast<C*>(b); }
639bool CXXDynamicCastExpr::isAlwaysNull() const
640{
641 QualType SrcType = getSubExpr()->getType();
642 QualType DestType = getType();
643
644 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
645 SrcType = SrcPTy->getPointeeType();
646 DestType = DestType->castAs<PointerType>()->getPointeeType();
647 }
648
Alexis Hunt78e2b912012-06-19 23:44:55 +0000649 if (DestType->isVoidType())
650 return false;
651
Anders Carlsson267c0c92011-04-11 01:43:55 +0000652 const CXXRecordDecl *SrcRD =
653 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
654
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000655 if (!SrcRD->hasAttr<FinalAttr>())
656 return false;
657
Anders Carlsson267c0c92011-04-11 01:43:55 +0000658 const CXXRecordDecl *DestRD =
659 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
660
661 return !DestRD->isDerivedFrom(SrcRD);
662}
663
John McCallcf142162010-08-07 06:22:56 +0000664CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000665CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
666 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000667 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000668 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000669 SourceLocation RParenLoc,
670 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000671 unsigned PathSize = (BasePath ? BasePath->size() : 0);
672 void *Buffer =
673 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
674 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000675 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000676 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000677 if (PathSize) E->setCastPath(*BasePath);
678 return E;
679}
680
681CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000682CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000683 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
684 + PathSize * sizeof(CXXBaseSpecifier*));
685 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
686}
687
Craig Toppera31a8822013-08-22 07:09:37 +0000688CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000689 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000690 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000691 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000692 SourceLocation RParenLoc,
693 SourceRange AngleBrackets) {
694 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000695}
696
Craig Toppera31a8822013-08-22 07:09:37 +0000697CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000698 return new (C) CXXConstCastExpr(EmptyShell());
699}
700
701CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000702CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000703 TypeSourceInfo *Written, CastKind K, Expr *Op,
704 const CXXCastPath *BasePath,
705 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000706 unsigned PathSize = (BasePath ? BasePath->size() : 0);
707 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
708 + PathSize * sizeof(CXXBaseSpecifier*));
709 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000710 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
John McCallcf142162010-08-07 06:22:56 +0000711 if (PathSize) E->setCastPath(*BasePath);
712 return E;
713}
714
715CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000716CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000717 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
718 + PathSize * sizeof(CXXBaseSpecifier*));
719 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
720}
721
Eli Friedman89fe0d52013-08-15 22:02:56 +0000722SourceLocation CXXFunctionalCastExpr::getLocStart() const {
723 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
724}
725
726SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
727 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
728}
729
Richard Smithc67fdd42012-03-07 08:35:16 +0000730UserDefinedLiteral::LiteralOperatorKind
731UserDefinedLiteral::getLiteralOperatorKind() const {
732 if (getNumArgs() == 0)
733 return LOK_Template;
734 if (getNumArgs() == 2)
735 return LOK_String;
736
737 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
738 QualType ParamTy =
739 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
740 if (ParamTy->isPointerType())
741 return LOK_Raw;
742 if (ParamTy->isAnyCharacterType())
743 return LOK_Character;
744 if (ParamTy->isIntegerType())
745 return LOK_Integer;
746 if (ParamTy->isFloatingType())
747 return LOK_Floating;
748
749 llvm_unreachable("unknown kind of literal operator");
750}
751
752Expr *UserDefinedLiteral::getCookedLiteral() {
753#ifndef NDEBUG
754 LiteralOperatorKind LOK = getLiteralOperatorKind();
755 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
756#endif
757 return getArg(0);
758}
759
760const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
761 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
762}
John McCallcf142162010-08-07 06:22:56 +0000763
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000764CXXDefaultArgExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000765CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
Douglas Gregor033f6752009-12-23 23:03:06 +0000766 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000767 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000768 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
769 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000770}
771
Craig Toppera31a8822013-08-22 07:09:37 +0000772CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000773 FieldDecl *Field, QualType T)
774 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
775 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
776 ? VK_XValue
777 : VK_RValue,
778 /*FIXME*/ OK_Ordinary, false, false, false, false),
779 Field(Field), Loc(Loc) {
780 assert(Field->hasInClassInitializer());
781}
782
Craig Toppera31a8822013-08-22 07:09:37 +0000783CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000784 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000785 return new (C) CXXTemporary(Destructor);
786}
787
Craig Toppera31a8822013-08-22 07:09:37 +0000788CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000789 CXXTemporary *Temp,
790 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000791 assert((SubExpr->getType()->isRecordType() ||
792 SubExpr->getType()->isArrayType()) &&
793 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000794
Douglas Gregora6e053e2010-12-15 01:34:56 +0000795 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000796}
797
Craig Toppera31a8822013-08-22 07:09:37 +0000798CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000799 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000800 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000801 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000802 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000803 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000804 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000805 bool StdInitListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000806 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000807 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
808 Type->getType().getNonReferenceType(),
809 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000810 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000811 HadMultipleCandidates,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000812 ListInitialization,
813 StdInitListInitialization,
814 ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000815 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000816 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000817}
818
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000819SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
820 return Type->getTypeLoc().getBeginLoc();
821}
822
823SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000824 SourceLocation Loc = getParenOrBraceRange().getEnd();
825 if (Loc.isInvalid() && getNumArgs())
826 Loc = getArg(getNumArgs()-1)->getLocEnd();
827 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000828}
Anders Carlsson6f287832009-04-21 02:22:11 +0000829
Craig Toppera31a8822013-08-22 07:09:37 +0000830CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000831 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000832 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000833 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000834 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000835 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000836 bool StdInitListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000837 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000838 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000839 SourceRange ParenOrBraceRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000840 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000841 Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000842 HadMultipleCandidates, ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000843 StdInitListInitialization,
Sebastian Redla9351792012-02-11 23:51:47 +0000844 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000845 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000846}
847
Craig Toppera31a8822013-08-22 07:09:37 +0000848CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
849 QualType T, SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000850 CXXConstructorDecl *D, bool elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000851 ArrayRef<Expr*> args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000852 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000853 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000854 bool StdInitListInitialization,
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),
Richard Smithf8adcdc2014-07-17 05:12:35 +0000866 StdInitListInitialization(StdInitListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000867 ZeroInitialization(ZeroInitialization),
Craig Topper36250ad2014-05-12 05:36:57 +0000868 ConstructKind(ConstructKind), Args(nullptr)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000869{
870 if (NumArgs) {
Benjamin Kramerc215e762012-08-24 11:54:20 +0000871 Args = new (C) Stmt*[args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000872
Benjamin Kramerc215e762012-08-24 11:54:20 +0000873 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000874 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000875
876 if (args[i]->isValueDependent())
877 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000878 if (args[i]->isInstantiationDependent())
879 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000880 if (args[i]->containsUnexpandedParameterPack())
881 ExprBits.ContainsUnexpandedParameterPack = true;
882
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000883 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000884 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000885 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000886}
887
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000888LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000889 LambdaCaptureKind Kind, VarDecl *Var,
890 SourceLocation EllipsisLoc)
Richard Smithba71c082013-05-16 06:20:58 +0000891 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000892{
893 unsigned Bits = 0;
894 if (Implicit)
895 Bits |= Capture_Implicit;
896
897 switch (Kind) {
Craig Topper36250ad2014-05-12 05:36:57 +0000898 case LCK_This:
899 assert(!Var && "'this' capture cannot have a variable!");
Douglas Gregore31e6062012-02-07 10:09:13 +0000900 break;
901
902 case LCK_ByCopy:
903 Bits |= Capture_ByCopy;
904 // Fall through
905 case LCK_ByRef:
906 assert(Var && "capture must have a variable!");
907 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000908 case LCK_VLAType:
909 assert(!Var && "VLA type capture cannot have a variable!");
910 Bits |= Capture_ByCopy;
911 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000912 }
Richard Smithba71c082013-05-16 06:20:58 +0000913 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000914}
915
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000916LambdaCaptureKind LambdaCapture::getCaptureKind() const {
Richard Smithba71c082013-05-16 06:20:58 +0000917 Decl *D = DeclAndBits.getPointer();
Alexey Bataev39c81e22014-08-28 04:28:19 +0000918 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
Richard Smithba71c082013-05-16 06:20:58 +0000919 if (!D)
Alexey Bataev39c81e22014-08-28 04:28:19 +0000920 return CapByCopy ? LCK_VLAType : LCK_This;
Douglas Gregore31e6062012-02-07 10:09:13 +0000921
Alexey Bataev39c81e22014-08-28 04:28:19 +0000922 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000923}
924
James Dennettddd36ff2013-08-09 23:08:25 +0000925LambdaExpr::LambdaExpr(QualType T,
Douglas Gregore31e6062012-02-07 10:09:13 +0000926 SourceRange IntroducerRange,
927 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000928 SourceLocation CaptureDefaultLoc,
929 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000930 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000931 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000932 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000933 ArrayRef<VarDecl *> ArrayIndexVars,
934 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000935 SourceLocation ClosingBrace,
936 bool ContainsUnexpandedParameterPack)
Douglas Gregore31e6062012-02-07 10:09:13 +0000937 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
938 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith2589b9802012-07-25 03:56:55 +0000939 ContainsUnexpandedParameterPack),
Douglas Gregore31e6062012-02-07 10:09:13 +0000940 IntroducerRange(IntroducerRange),
James Dennettddd36ff2013-08-09 23:08:25 +0000941 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregore5561632012-02-13 17:20:40 +0000942 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000943 CaptureDefault(CaptureDefault),
944 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000945 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000946 ClosingBrace(ClosingBrace)
947{
948 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000949 CXXRecordDecl *Class = getLambdaClass();
950 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000951
952 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000953
954 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000955 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000956 Data.NumCaptures = NumCaptures;
957 Data.NumExplicitCaptures = 0;
958 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
959 Capture *ToCapture = Data.Captures;
960 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
961 if (Captures[I].isExplicit())
962 ++Data.NumExplicitCaptures;
963
964 *ToCapture++ = Captures[I];
965 }
966
967 // Copy initialization expressions for the non-static data members.
968 Stmt **Stored = getStoredStmts();
969 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
970 *Stored++ = CaptureInits[I];
971
972 // Copy the body of the lambda.
973 *Stored++ = getCallOperator()->getBody();
974
975 // Copy the array index variables, if any.
976 HasArrayIndexVars = !ArrayIndexVars.empty();
977 if (HasArrayIndexVars) {
978 assert(ArrayIndexStarts.size() == NumCaptures);
979 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
980 sizeof(VarDecl *) * ArrayIndexVars.size());
981 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
982 sizeof(unsigned) * Captures.size());
983 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000984 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000985}
986
Craig Toppera31a8822013-08-22 07:09:37 +0000987LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
Douglas Gregore31e6062012-02-07 10:09:13 +0000988 CXXRecordDecl *Class,
989 SourceRange IntroducerRange,
990 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000991 SourceLocation CaptureDefaultLoc,
992 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000993 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000994 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000995 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000996 ArrayRef<VarDecl *> ArrayIndexVars,
997 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000998 SourceLocation ClosingBrace,
999 bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001000 // Determine the type of the expression (i.e., the type of the
1001 // function object we're creating).
1002 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +00001003
Douglas Gregore5561632012-02-13 17:20:40 +00001004 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smithc7520bf2012-08-21 05:42:49 +00001005 if (!ArrayIndexVars.empty()) {
1006 Size += sizeof(unsigned) * (Captures.size() + 1);
1007 // Realign for following VarDecl array.
Richard Smitha75e1cf2012-08-21 18:18:06 +00001008 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smithc7520bf2012-08-21 05:42:49 +00001009 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
1010 }
Douglas Gregore5561632012-02-13 17:20:40 +00001011 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +00001012 return new (Mem) LambdaExpr(T, IntroducerRange,
1013 CaptureDefault, CaptureDefaultLoc, Captures,
1014 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001015 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001016 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +00001017}
1018
Craig Toppera31a8822013-08-22 07:09:37 +00001019LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1020 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +00001021 unsigned NumArrayIndexVars) {
1022 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
1023 if (NumArrayIndexVars)
1024 Size += sizeof(VarDecl) * NumArrayIndexVars
1025 + sizeof(unsigned) * (NumCaptures + 1);
1026 void *Mem = C.Allocate(Size);
1027 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1028}
1029
James Dennettdd2ffea22015-05-07 18:48:18 +00001030bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1031 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1032 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
1033}
1034
Douglas Gregorc8a73492012-02-13 15:44:47 +00001035LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001036 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001037}
1038
1039LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001040 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001041}
1042
James Dennett1575cb42014-05-27 19:13:04 +00001043LambdaExpr::capture_range LambdaExpr::captures() const {
1044 return capture_range(capture_begin(), capture_end());
1045}
1046
Douglas Gregorc8a73492012-02-13 15:44:47 +00001047LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1048 return capture_begin();
1049}
1050
1051LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1052 struct CXXRecordDecl::LambdaDefinitionData &Data
1053 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001054 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001055}
1056
James Dennett1575cb42014-05-27 19:13:04 +00001057LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1058 return capture_range(explicit_capture_begin(), explicit_capture_end());
1059}
1060
Douglas Gregorc8a73492012-02-13 15:44:47 +00001061LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1062 return explicit_capture_end();
1063}
1064
1065LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1066 return capture_end();
1067}
1068
James Dennett1575cb42014-05-27 19:13:04 +00001069LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1070 return capture_range(implicit_capture_begin(), implicit_capture_end());
1071}
1072
James Y Knight53c76162015-07-17 18:21:37 +00001073ArrayRef<VarDecl *>
1074LambdaExpr::getCaptureInitIndexVars(const_capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001075 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001076
1077 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001078 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1079 "Capture index out-of-range");
James Y Knight53c76162015-07-17 18:21:37 +00001080 VarDecl *const *IndexVars = getArrayIndexVars();
1081 const unsigned *IndexStarts = getArrayIndexStarts();
Craig Topper5fc8fc22014-08-27 06:28:36 +00001082 return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
1083 IndexVars + IndexStarts[Index + 1]);
Douglas Gregor54fcea62012-02-13 16:35:30 +00001084}
1085
Douglas Gregore31e6062012-02-07 10:09:13 +00001086CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1087 return getType()->getAsCXXRecordDecl();
1088}
1089
1090CXXMethodDecl *LambdaExpr::getCallOperator() const {
1091 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001092 return Record->getLambdaCallOperator();
1093}
1094
1095TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1096 CXXRecordDecl *Record = getLambdaClass();
1097 return Record->getGenericLambdaTemplateParameterList();
1098
Douglas Gregore31e6062012-02-07 10:09:13 +00001099}
1100
Douglas Gregor99ae8062012-02-14 17:54:36 +00001101CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +00001102 // FIXME: this mutation in getBody is bogus. It should be
1103 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1104 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +00001105 if (!getStoredStmts()[NumCaptures])
James Y Knight53c76162015-07-17 18:21:37 +00001106 *const_cast<clang::Stmt **>(&getStoredStmts()[NumCaptures]) =
1107 getCallOperator()->getBody();
1108
Douglas Gregor99ae8062012-02-14 17:54:36 +00001109 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1110}
1111
Douglas Gregore31e6062012-02-07 10:09:13 +00001112bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001113 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001114}
1115
John McCall28fc7092011-11-10 05:35:25 +00001116ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1117 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001118 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001119 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001120 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001121 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001122 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001123 SubExpr(subexpr) {
1124 ExprWithCleanupsBits.NumObjects = objects.size();
1125 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1126 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001127}
1128
Craig Toppera31a8822013-08-22 07:09:37 +00001129ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001130 ArrayRef<CleanupObject> objects) {
1131 size_t size = sizeof(ExprWithCleanups)
1132 + objects.size() * sizeof(CleanupObject);
1133 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1134 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001135}
1136
John McCall28fc7092011-11-10 05:35:25 +00001137ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1138 : Expr(ExprWithCleanupsClass, empty) {
1139 ExprWithCleanupsBits.NumObjects = numObjects;
1140}
Chris Lattnercba86142010-05-10 00:25:06 +00001141
Craig Toppera31a8822013-08-22 07:09:37 +00001142ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1143 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001144 unsigned numObjects) {
1145 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1146 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1147 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001148}
1149
Douglas Gregor2b88c112010-09-08 00:15:04 +00001150CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001151 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001152 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001153 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001154 : Expr(CXXUnresolvedConstructExprClass,
1155 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001156 (Type->getType()->isLValueReferenceType() ? VK_LValue
1157 :Type->getType()->isRValueReferenceType()? VK_XValue
1158 :VK_RValue),
1159 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001160 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001161 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001162 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001163 LParenLoc(LParenLoc),
1164 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001165 NumArgs(Args.size()) {
Douglas Gregorce934142009-05-20 18:46:25 +00001166 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001167 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001168 if (Args[I]->containsUnexpandedParameterPack())
1169 ExprBits.ContainsUnexpandedParameterPack = true;
1170
1171 StoredArgs[I] = Args[I];
1172 }
Douglas Gregorce934142009-05-20 18:46:25 +00001173}
1174
1175CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001176CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001177 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001178 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001179 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001180 SourceLocation RParenLoc) {
1181 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001182 sizeof(Expr *) * Args.size());
1183 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001184}
1185
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001186CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001187CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001188 Stmt::EmptyShell Empty;
1189 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1190 sizeof(Expr *) * NumArgs);
1191 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1192}
1193
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001194SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1195 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001196}
1197
Craig Toppera31a8822013-08-22 07:09:37 +00001198CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001199 Expr *Base, QualType BaseType,
1200 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001201 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001202 NestedNameSpecifierLoc QualifierLoc,
1203 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001204 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001205 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001206 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001207 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001208 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001209 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001210 (QualifierLoc &&
1211 QualifierLoc.getNestedNameSpecifier()
1212 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001213 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001214 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Craig Topper36250ad2014-05-12 05:36:57 +00001215 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1216 TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001217 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001218 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001219 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001220 if (TemplateArgs) {
1221 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001222 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001223 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001224 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1225 Dependent,
1226 InstantiationDependent,
1227 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001228 if (ContainsUnexpandedParameterPack)
1229 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001230 } else if (TemplateKWLoc.isValid()) {
1231 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001232 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001233}
1234
Craig Toppera31a8822013-08-22 07:09:37 +00001235CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001236 Expr *Base, QualType BaseType,
1237 bool IsArrow,
1238 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001239 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001240 NamedDecl *FirstQualifierFoundInScope,
1241 DeclarationNameInfo MemberNameInfo)
1242 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001243 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001244 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001245 (QualifierLoc &&
1246 QualifierLoc.getNestedNameSpecifier()->
1247 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001248 MemberNameInfo.containsUnexpandedParameterPack())),
1249 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001250 HasTemplateKWAndArgsInfo(false),
1251 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001252 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1253 MemberNameInfo(MemberNameInfo) { }
1254
John McCall8cd78132009-11-19 22:55:06 +00001255CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001256CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001257 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001258 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001259 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001260 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001261 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001262 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001263 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001264 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001265 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1266 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001267 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001268 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001269 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001270
Abramo Bagnara7945c982012-01-27 09:46:47 +00001271 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1272 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1273 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001274
Chris Lattner5c0b4052010-10-30 05:14:06 +00001275 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001276 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1277 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001278 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001279 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001280 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001281 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001282}
1283
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001284CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001285CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001286 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001287 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001288 if (!HasTemplateKWAndArgsInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00001289 return new (C) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001290 0, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001291 NestedNameSpecifierLoc(),
1292 nullptr, DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001293
1294 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001295 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001296 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001297 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001298 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001299 0, SourceLocation(),
1300 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001301 SourceLocation(), nullptr,
1302 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001303 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001304 return E;
1305}
1306
Douglas Gregor0da1d432011-02-28 20:01:57 +00001307bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001308 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001309 return true;
1310
Douglas Gregor25b7e052011-03-02 21:06:53 +00001311 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001312}
1313
John McCall0009fcc2011-04-26 20:42:42 +00001314static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1315 UnresolvedSetIterator end) {
1316 do {
1317 NamedDecl *decl = *begin;
1318 if (isa<UnresolvedUsingValueDecl>(decl))
1319 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001320
1321 // Unresolved member expressions should only contain methods and
1322 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001323 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1324 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001325 return false;
1326 } while (++begin != end);
1327
1328 return true;
1329}
1330
Craig Toppera31a8822013-08-22 07:09:37 +00001331UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001332 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001333 Expr *Base, QualType BaseType,
1334 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001335 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001336 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001337 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001338 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001339 const TemplateArgumentListInfo *TemplateArgs,
1340 UnresolvedSetIterator Begin,
1341 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001342 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1343 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001344 // Dependent
1345 ((Base && Base->isTypeDependent()) ||
1346 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001347 ((Base && Base->isInstantiationDependent()) ||
1348 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001349 // Contains unexpanded parameter pack
1350 ((Base && Base->containsUnexpandedParameterPack()) ||
1351 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001352 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1353 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001354
1355 // Check whether all of the members are non-static member functions,
1356 // and if so, mark give this bound-member type instead of overload type.
1357 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1358 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001359}
1360
Douglas Gregor0da1d432011-02-28 20:01:57 +00001361bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001362 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001363 return true;
1364
Douglas Gregor25b7e052011-03-02 21:06:53 +00001365 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001366}
1367
John McCall10eae182009-11-30 22:42:35 +00001368UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001369UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001370 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001371 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001372 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001373 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001374 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001375 const TemplateArgumentListInfo *TemplateArgs,
1376 UnresolvedSetIterator Begin,
1377 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001378 std::size_t size = sizeof(UnresolvedMemberExpr);
1379 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001380 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1381 else if (TemplateKWLoc.isValid())
1382 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001383
Chris Lattner5c0b4052010-10-30 05:14:06 +00001384 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001385 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001386 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001387 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001388 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001389}
1390
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001391UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001392UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1393 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001394 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001395 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001396 if (HasTemplateKWAndArgsInfo)
1397 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001398
Chris Lattner5c0b4052010-10-30 05:14:06 +00001399 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001400 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001401 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001402 return E;
1403}
1404
John McCall58cc69d2010-01-27 01:50:18 +00001405CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1406 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1407
1408 // If there was a nested name specifier, it names the naming class.
1409 // It can't be dependent: after all, we were actually able to do the
1410 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001411 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001412 auto *NNS = getQualifier();
1413 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001414 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001415 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001416 Record = T->getAsCXXRecordDecl();
1417 assert(Record && "qualifier in member expression does not name record");
1418 }
John McCall58cc69d2010-01-27 01:50:18 +00001419 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001420 else {
John McCall58cc69d2010-01-27 01:50:18 +00001421 QualType BaseType = getBaseType().getNonReferenceType();
1422 if (isArrow()) {
1423 const PointerType *PT = BaseType->getAs<PointerType>();
1424 assert(PT && "base of arrow member access is not pointer");
1425 BaseType = PT->getPointeeType();
1426 }
1427
Douglas Gregor9262f472010-04-27 18:19:34 +00001428 Record = BaseType->getAsCXXRecordDecl();
1429 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001430 }
1431
Douglas Gregor9262f472010-04-27 18:19:34 +00001432 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001433}
1434
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001435SubstNonTypeTemplateParmPackExpr::
1436SubstNonTypeTemplateParmPackExpr(QualType T,
1437 NonTypeTemplateParmDecl *Param,
1438 SourceLocation NameLoc,
1439 const TemplateArgument &ArgPack)
1440 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001441 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001442 Param(Param), Arguments(ArgPack.pack_begin()),
1443 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1444
1445TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1446 return TemplateArgument(Arguments, NumArguments);
1447}
1448
Richard Smithb15fe3a2012-09-12 00:56:43 +00001449FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1450 SourceLocation NameLoc,
1451 unsigned NumParams,
1452 Decl * const *Params)
1453 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1454 true, true, true, true),
1455 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1456 if (Params)
1457 std::uninitialized_copy(Params, Params + NumParams,
1458 reinterpret_cast<Decl**>(this+1));
1459}
1460
1461FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001462FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001463 ParmVarDecl *ParamPack, SourceLocation NameLoc,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001464 ArrayRef<Decl *> Params) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001465 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1466 sizeof(ParmVarDecl*) * Params.size()))
1467 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1468}
1469
1470FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001471FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1472 unsigned NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001473 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1474 sizeof(ParmVarDecl*) * NumParams))
Craig Topper36250ad2014-05-12 05:36:57 +00001475 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001476}
1477
David Majnemerdaff3702014-05-01 17:50:17 +00001478void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1479 unsigned ManglingNumber) {
1480 // We only need extra state if we have to remember more than just the Stmt.
1481 if (!ExtendedBy)
1482 return;
1483
1484 // We may need to allocate extra storage for the mangling number and the
1485 // extended-by ValueDecl.
1486 if (!State.is<ExtraState *>()) {
1487 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1488 ES->Temporary = State.get<Stmt *>();
1489 State = ES;
1490 }
1491
1492 auto ES = State.get<ExtraState *>();
1493 ES->ExtendingDecl = ExtendedBy;
1494 ES->ManglingNumber = ManglingNumber;
1495}
1496
Douglas Gregor29c42f22012-02-24 07:38:34 +00001497TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1498 ArrayRef<TypeSourceInfo *> Args,
1499 SourceLocation RParenLoc,
1500 bool Value)
1501 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1502 /*TypeDependent=*/false,
1503 /*ValueDependent=*/false,
1504 /*InstantiationDependent=*/false,
1505 /*ContainsUnexpandedParameterPack=*/false),
1506 Loc(Loc), RParenLoc(RParenLoc)
1507{
1508 TypeTraitExprBits.Kind = Kind;
1509 TypeTraitExprBits.Value = Value;
1510 TypeTraitExprBits.NumArgs = Args.size();
1511
1512 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1513
1514 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1515 if (Args[I]->getType()->isDependentType())
1516 setValueDependent(true);
1517 if (Args[I]->getType()->isInstantiationDependentType())
1518 setInstantiationDependent(true);
1519 if (Args[I]->getType()->containsUnexpandedParameterPack())
1520 setContainsUnexpandedParameterPack(true);
1521
1522 ToArgs[I] = Args[I];
1523 }
1524}
1525
Craig Toppera31a8822013-08-22 07:09:37 +00001526TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001527 SourceLocation Loc,
1528 TypeTrait Kind,
1529 ArrayRef<TypeSourceInfo *> Args,
1530 SourceLocation RParenLoc,
1531 bool Value) {
1532 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1533 void *Mem = C.Allocate(Size);
1534 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1535}
1536
Craig Toppera31a8822013-08-22 07:09:37 +00001537TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001538 unsigned NumArgs) {
1539 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1540 void *Mem = C.Allocate(Size);
1541 return new (Mem) TypeTraitExpr(EmptyShell());
1542}
1543
David Blaikie68e081d2011-12-20 02:48:34 +00001544void ArrayTypeTraitExpr::anchor() { }