blob: 08230bebed18a492d020ba9eb0650e66135c3122 [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 {
John McCall75f94982011-03-07 03:12:35 +0000211 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000212 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000213}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000214
Sebastian Redlbd150f42008-11-21 19:14:01 +0000215// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000216QualType CXXDeleteExpr::getDestroyedType() const {
217 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000218 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000219 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000220
221 if (ArgType->isDependentType() && !ArgType->isPointerType())
222 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000223
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000224 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000225}
226
Douglas Gregorad8a3362009-09-04 17:36:40 +0000227// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000228PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
229 : Type(Info)
230{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000231 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000232}
233
Craig Toppera31a8822013-08-22 07:09:37 +0000234CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000235 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
236 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
237 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
238 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000239 : Expr(CXXPseudoDestructorExprClass,
Reid Kleckner78af0702013-08-27 23:08:25 +0000240 Context.getPointerType(Context.getFunctionType(
241 Context.VoidTy, None,
242 FunctionProtoType::ExtProtoInfo(
243 Context.getDefaultCallingConvention(false, true)))),
John McCalldb40c7f2010-12-14 08:05:40 +0000244 VK_RValue, OK_Ordinary,
245 /*isTypeDependent=*/(Base->isTypeDependent() ||
246 (DestroyedType.getTypeSourceInfo() &&
247 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000248 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000249 (Base->isInstantiationDependent() ||
250 (QualifierLoc &&
251 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
252 (ScopeType &&
253 ScopeType->getType()->isInstantiationDependentType()) ||
254 (DestroyedType.getTypeSourceInfo() &&
255 DestroyedType.getTypeSourceInfo()->getType()
256 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000257 // ContainsUnexpandedParameterPack
258 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000259 (QualifierLoc &&
260 QualifierLoc.getNestedNameSpecifier()
261 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000262 (ScopeType &&
263 ScopeType->getType()->containsUnexpandedParameterPack()) ||
264 (DestroyedType.getTypeSourceInfo() &&
265 DestroyedType.getTypeSourceInfo()->getType()
266 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000267 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000268 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000269 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
270 DestroyedType(DestroyedType) { }
271
Douglas Gregor678f90d2010-02-25 01:56:36 +0000272QualType CXXPseudoDestructorExpr::getDestroyedType() const {
273 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
274 return TInfo->getType();
275
276 return QualType();
277}
278
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000279SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000280 SourceLocation End = DestroyedType.getLocation();
281 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000282 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000283 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000284}
285
John McCalld14a8642009-11-21 08:51:07 +0000286// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000287UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000288UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000289 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000290 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000291 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000292 const DeclarationNameInfo &NameInfo,
293 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000294 const TemplateArgumentListInfo *Args,
295 UnresolvedSetIterator Begin,
296 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000297{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000298 assert(Args || TemplateKWLoc.isValid());
299 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000300 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000301 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000302 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
303 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000304 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000305 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000306}
307
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000308UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000309UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000310 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000311 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000312 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000313 if (HasTemplateKWAndArgsInfo)
314 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000315
Chris Lattner5c0b4052010-10-30 05:14:06 +0000316 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000317 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000318 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000319 return E;
320}
321
Craig Toppera31a8822013-08-22 07:09:37 +0000322OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000323 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000324 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000325 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000326 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000327 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000328 UnresolvedSetIterator End,
329 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000330 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000331 bool KnownContainsUnexpandedParameterPack)
332 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
333 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000334 (KnownInstantiationDependent ||
335 NameInfo.isInstantiationDependent() ||
336 (QualifierLoc &&
337 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000338 (KnownContainsUnexpandedParameterPack ||
339 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000340 (QualifierLoc &&
341 QualifierLoc.getNestedNameSpecifier()
342 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000343 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
Craig Topper36250ad2014-05-12 05:36:57 +0000344 Results(nullptr), NumResults(End - Begin),
345 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
346 TemplateKWLoc.isValid()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000347 NumResults = End - Begin;
348 if (NumResults) {
349 // Determine whether this expression is type-dependent.
350 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
351 if ((*I)->getDeclContext()->isDependentContext() ||
352 isa<UnresolvedUsingValueDecl>(*I)) {
353 ExprBits.TypeDependent = true;
354 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000355 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000356 }
357 }
358
359 Results = static_cast<DeclAccessPair *>(
360 C.Allocate(sizeof(DeclAccessPair) * NumResults,
361 llvm::alignOf<DeclAccessPair>()));
362 memcpy(Results, &*Begin.getIterator(),
363 NumResults * sizeof(DeclAccessPair));
364 }
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>()));
404 memcpy(Results, &*Begin.getIterator(),
405 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000406 }
407}
408
John McCall8c12dc42010-04-22 18:44:12 +0000409CXXRecordDecl *OverloadExpr::getNamingClass() const {
410 if (isa<UnresolvedLookupExpr>(this))
411 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
412 else
413 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
414}
415
John McCall8cd78132009-11-19 22:55:06 +0000416// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000417DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000418 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000419 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000420 const DeclarationNameInfo &NameInfo,
421 const TemplateArgumentListInfo *Args)
422 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
423 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000424 (NameInfo.isInstantiationDependent() ||
425 (QualifierLoc &&
426 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000427 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000428 (QualifierLoc &&
429 QualifierLoc.getNestedNameSpecifier()
430 ->containsUnexpandedParameterPack()))),
431 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Craig Topper36250ad2014-05-12 05:36:57 +0000432 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000433{
434 if (Args) {
435 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000436 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000437 bool ContainsUnexpandedParameterPack
438 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000439 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
440 Dependent,
441 InstantiationDependent,
442 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000443 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000444 } else if (TemplateKWLoc.isValid()) {
445 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000446 }
447}
448
John McCalle66edc12009-11-24 19:00:30 +0000449DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000450DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000451 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000452 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000453 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000454 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000455 assert(QualifierLoc && "should be created for dependent qualifiers");
John McCalle66edc12009-11-24 19:00:30 +0000456 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000457 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000458 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
459 else if (TemplateKWLoc.isValid())
460 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000461 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000462 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
463 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000464}
465
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000466DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000467DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000468 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000469 unsigned NumTemplateArgs) {
470 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000471 if (HasTemplateKWAndArgsInfo)
472 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000473 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000474 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000475 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000476 SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000477 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000478 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000479 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000480}
481
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000482SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000483 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000484 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
485 return Loc;
486}
487
488SourceLocation CXXConstructExpr::getLocEnd() const {
489 if (isa<CXXTemporaryObjectExpr>(this))
490 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000491
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000492 if (ParenOrBraceRange.isValid())
493 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000494
495 SourceLocation End = Loc;
496 for (unsigned I = getNumArgs(); I > 0; --I) {
497 const Expr *Arg = getArg(I-1);
498 if (!Arg->isDefaultArgument()) {
499 SourceLocation NewEnd = Arg->getLocEnd();
500 if (NewEnd.isValid()) {
501 End = NewEnd;
502 break;
503 }
504 }
505 }
506
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000507 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000508}
509
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000510SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000511 OverloadedOperatorKind Kind = getOperator();
512 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
513 if (getNumArgs() == 1)
514 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000515 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000516 else
517 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000518 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000519 } else if (Kind == OO_Arrow) {
520 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000521 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000522 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000523 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000524 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000525 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000526 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000527 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000528 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000529 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000530 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000531 }
532}
533
Ted Kremenek98a24e32011-03-30 17:41:19 +0000534Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000535 const Expr *Callee = getCallee()->IgnoreParens();
536 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000537 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000538 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
539 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
540 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000541
542 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000543 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000544}
545
Ted Kremenek98a24e32011-03-30 17:41:19 +0000546CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
547 if (const MemberExpr *MemExpr =
548 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
549 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
550
551 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000552 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000553}
554
555
David Blaikiec0f58662012-05-03 16:25:49 +0000556CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000557 Expr* ThisArg = getImplicitObjectArgument();
558 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000559 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000560
561 if (ThisArg->getType()->isAnyPointerType())
562 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
563
564 return ThisArg->getType()->getAsCXXRecordDecl();
565}
566
Douglas Gregoref986e82009-11-12 15:31:47 +0000567
Douglas Gregore200adc2008-10-27 19:41:14 +0000568//===----------------------------------------------------------------------===//
569// Named casts
570//===----------------------------------------------------------------------===//
571
572/// getCastName - Get the name of the C++ cast being used, e.g.,
573/// "static_cast", "dynamic_cast", "reinterpret_cast", or
574/// "const_cast". The returned pointer must not be freed.
575const char *CXXNamedCastExpr::getCastName() const {
576 switch (getStmtClass()) {
577 case CXXStaticCastExprClass: return "static_cast";
578 case CXXDynamicCastExprClass: return "dynamic_cast";
579 case CXXReinterpretCastExprClass: return "reinterpret_cast";
580 case CXXConstCastExprClass: return "const_cast";
581 default: return "<invalid cast>";
582 }
583}
Douglas Gregordd04d332009-01-16 18:33:17 +0000584
Craig Toppera31a8822013-08-22 07:09:37 +0000585CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000586 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000587 CastKind K, Expr *Op,
588 const CXXCastPath *BasePath,
589 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000590 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000591 SourceLocation RParenLoc,
592 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000593 unsigned PathSize = (BasePath ? BasePath->size() : 0);
594 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
595 + PathSize * sizeof(CXXBaseSpecifier*));
596 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000597 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000598 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000599 if (PathSize) E->setCastPath(*BasePath);
600 return E;
601}
602
Craig Toppera31a8822013-08-22 07:09:37 +0000603CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000604 unsigned PathSize) {
605 void *Buffer =
606 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
607 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
608}
609
Craig Toppera31a8822013-08-22 07:09:37 +0000610CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000611 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000612 CastKind K, Expr *Op,
613 const CXXCastPath *BasePath,
614 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000615 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000616 SourceLocation RParenLoc,
617 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000618 unsigned PathSize = (BasePath ? BasePath->size() : 0);
619 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
620 + PathSize * sizeof(CXXBaseSpecifier*));
621 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000622 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000623 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000624 if (PathSize) E->setCastPath(*BasePath);
625 return E;
626}
627
Craig Toppera31a8822013-08-22 07:09:37 +0000628CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000629 unsigned PathSize) {
630 void *Buffer =
631 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
632 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
633}
634
Anders Carlsson267c0c92011-04-11 01:43:55 +0000635/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
636/// to always be null. For example:
637///
638/// struct A { };
639/// struct B final : A { };
640/// struct C { };
641///
642/// C *f(B* b) { return dynamic_cast<C*>(b); }
643bool CXXDynamicCastExpr::isAlwaysNull() const
644{
645 QualType SrcType = getSubExpr()->getType();
646 QualType DestType = getType();
647
648 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
649 SrcType = SrcPTy->getPointeeType();
650 DestType = DestType->castAs<PointerType>()->getPointeeType();
651 }
652
Alexis Hunt78e2b912012-06-19 23:44:55 +0000653 if (DestType->isVoidType())
654 return false;
655
Anders Carlsson267c0c92011-04-11 01:43:55 +0000656 const CXXRecordDecl *SrcRD =
657 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
658
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000659 if (!SrcRD->hasAttr<FinalAttr>())
660 return false;
661
Anders Carlsson267c0c92011-04-11 01:43:55 +0000662 const CXXRecordDecl *DestRD =
663 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
664
665 return !DestRD->isDerivedFrom(SrcRD);
666}
667
John McCallcf142162010-08-07 06:22:56 +0000668CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000669CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
670 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000671 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000672 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000673 SourceLocation RParenLoc,
674 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000675 unsigned PathSize = (BasePath ? BasePath->size() : 0);
676 void *Buffer =
677 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
678 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000679 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000680 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000681 if (PathSize) E->setCastPath(*BasePath);
682 return E;
683}
684
685CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000686CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000687 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
688 + PathSize * sizeof(CXXBaseSpecifier*));
689 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
690}
691
Craig Toppera31a8822013-08-22 07:09:37 +0000692CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000693 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000694 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000695 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000696 SourceLocation RParenLoc,
697 SourceRange AngleBrackets) {
698 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000699}
700
Craig Toppera31a8822013-08-22 07:09:37 +0000701CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000702 return new (C) CXXConstCastExpr(EmptyShell());
703}
704
705CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000706CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000707 TypeSourceInfo *Written, CastKind K, Expr *Op,
708 const CXXCastPath *BasePath,
709 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000710 unsigned PathSize = (BasePath ? BasePath->size() : 0);
711 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
712 + PathSize * sizeof(CXXBaseSpecifier*));
713 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000714 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
John McCallcf142162010-08-07 06:22:56 +0000715 if (PathSize) E->setCastPath(*BasePath);
716 return E;
717}
718
719CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000720CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000721 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
722 + PathSize * sizeof(CXXBaseSpecifier*));
723 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
724}
725
Eli Friedman89fe0d52013-08-15 22:02:56 +0000726SourceLocation CXXFunctionalCastExpr::getLocStart() const {
727 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
728}
729
730SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
731 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
732}
733
Richard Smithc67fdd42012-03-07 08:35:16 +0000734UserDefinedLiteral::LiteralOperatorKind
735UserDefinedLiteral::getLiteralOperatorKind() const {
736 if (getNumArgs() == 0)
737 return LOK_Template;
738 if (getNumArgs() == 2)
739 return LOK_String;
740
741 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
742 QualType ParamTy =
743 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
744 if (ParamTy->isPointerType())
745 return LOK_Raw;
746 if (ParamTy->isAnyCharacterType())
747 return LOK_Character;
748 if (ParamTy->isIntegerType())
749 return LOK_Integer;
750 if (ParamTy->isFloatingType())
751 return LOK_Floating;
752
753 llvm_unreachable("unknown kind of literal operator");
754}
755
756Expr *UserDefinedLiteral::getCookedLiteral() {
757#ifndef NDEBUG
758 LiteralOperatorKind LOK = getLiteralOperatorKind();
759 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
760#endif
761 return getArg(0);
762}
763
764const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
765 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
766}
John McCallcf142162010-08-07 06:22:56 +0000767
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000768CXXDefaultArgExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000769CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
Douglas Gregor033f6752009-12-23 23:03:06 +0000770 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000771 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000772 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
773 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000774}
775
Craig Toppera31a8822013-08-22 07:09:37 +0000776CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000777 FieldDecl *Field, QualType T)
778 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
779 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
780 ? VK_XValue
781 : VK_RValue,
782 /*FIXME*/ OK_Ordinary, false, false, false, false),
783 Field(Field), Loc(Loc) {
784 assert(Field->hasInClassInitializer());
785}
786
Craig Toppera31a8822013-08-22 07:09:37 +0000787CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000788 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000789 return new (C) CXXTemporary(Destructor);
790}
791
Craig Toppera31a8822013-08-22 07:09:37 +0000792CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000793 CXXTemporary *Temp,
794 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000795 assert((SubExpr->getType()->isRecordType() ||
796 SubExpr->getType()->isArrayType()) &&
797 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000798
Douglas Gregora6e053e2010-12-15 01:34:56 +0000799 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000800}
801
Craig Toppera31a8822013-08-22 07:09:37 +0000802CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000803 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000804 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000805 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000806 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000807 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000808 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000809 bool StdInitListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000810 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000811 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
812 Type->getType().getNonReferenceType(),
813 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000814 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000815 HadMultipleCandidates,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000816 ListInitialization,
817 StdInitListInitialization,
818 ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000819 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000820 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000821}
822
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000823SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
824 return Type->getTypeLoc().getBeginLoc();
825}
826
827SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000828 SourceLocation Loc = getParenOrBraceRange().getEnd();
829 if (Loc.isInvalid() && getNumArgs())
830 Loc = getArg(getNumArgs()-1)->getLocEnd();
831 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000832}
Anders Carlsson6f287832009-04-21 02:22:11 +0000833
Craig Toppera31a8822013-08-22 07:09:37 +0000834CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000835 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000836 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000837 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000838 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000839 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000840 bool StdInitListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000841 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000842 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000843 SourceRange ParenOrBraceRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000844 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000845 Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000846 HadMultipleCandidates, ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000847 StdInitListInitialization,
Sebastian Redla9351792012-02-11 23:51:47 +0000848 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000849 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000850}
851
Craig Toppera31a8822013-08-22 07:09:37 +0000852CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
853 QualType T, SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000854 CXXConstructorDecl *D, bool elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000855 ArrayRef<Expr*> args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000856 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000857 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000858 bool StdInitListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000859 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000860 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000861 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000862 : Expr(SC, T, VK_RValue, OK_Ordinary,
863 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000864 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000865 T->containsUnexpandedParameterPack()),
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000866 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
867 NumArgs(args.size()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000868 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000869 ListInitialization(ListInitialization),
Richard Smithf8adcdc2014-07-17 05:12:35 +0000870 StdInitListInitialization(StdInitListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000871 ZeroInitialization(ZeroInitialization),
Craig Topper36250ad2014-05-12 05:36:57 +0000872 ConstructKind(ConstructKind), Args(nullptr)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000873{
874 if (NumArgs) {
Benjamin Kramerc215e762012-08-24 11:54:20 +0000875 Args = new (C) Stmt*[args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000876
Benjamin Kramerc215e762012-08-24 11:54:20 +0000877 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000878 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000879
880 if (args[i]->isValueDependent())
881 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000882 if (args[i]->isInstantiationDependent())
883 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000884 if (args[i]->containsUnexpandedParameterPack())
885 ExprBits.ContainsUnexpandedParameterPack = true;
886
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000887 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000888 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000889 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000890}
891
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000892LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000893 LambdaCaptureKind Kind, VarDecl *Var,
894 SourceLocation EllipsisLoc)
Richard Smithba71c082013-05-16 06:20:58 +0000895 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000896{
897 unsigned Bits = 0;
898 if (Implicit)
899 Bits |= Capture_Implicit;
900
901 switch (Kind) {
Craig Topper36250ad2014-05-12 05:36:57 +0000902 case LCK_This:
903 assert(!Var && "'this' capture cannot have a variable!");
Douglas Gregore31e6062012-02-07 10:09:13 +0000904 break;
905
906 case LCK_ByCopy:
907 Bits |= Capture_ByCopy;
908 // Fall through
909 case LCK_ByRef:
910 assert(Var && "capture must have a variable!");
911 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000912 case LCK_VLAType:
913 assert(!Var && "VLA type capture cannot have a variable!");
914 Bits |= Capture_ByCopy;
915 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000916 }
Richard Smithba71c082013-05-16 06:20:58 +0000917 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000918}
919
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000920LambdaCaptureKind LambdaCapture::getCaptureKind() const {
Richard Smithba71c082013-05-16 06:20:58 +0000921 Decl *D = DeclAndBits.getPointer();
Alexey Bataev39c81e22014-08-28 04:28:19 +0000922 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
Richard Smithba71c082013-05-16 06:20:58 +0000923 if (!D)
Alexey Bataev39c81e22014-08-28 04:28:19 +0000924 return CapByCopy ? LCK_VLAType : LCK_This;
Douglas Gregore31e6062012-02-07 10:09:13 +0000925
Alexey Bataev39c81e22014-08-28 04:28:19 +0000926 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000927}
928
James Dennettddd36ff2013-08-09 23:08:25 +0000929LambdaExpr::LambdaExpr(QualType T,
Douglas Gregore31e6062012-02-07 10:09:13 +0000930 SourceRange IntroducerRange,
931 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000932 SourceLocation CaptureDefaultLoc,
933 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000934 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000935 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000936 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000937 ArrayRef<VarDecl *> ArrayIndexVars,
938 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000939 SourceLocation ClosingBrace,
940 bool ContainsUnexpandedParameterPack)
Douglas Gregore31e6062012-02-07 10:09:13 +0000941 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
942 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith2589b9802012-07-25 03:56:55 +0000943 ContainsUnexpandedParameterPack),
Douglas Gregore31e6062012-02-07 10:09:13 +0000944 IntroducerRange(IntroducerRange),
James Dennettddd36ff2013-08-09 23:08:25 +0000945 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregore5561632012-02-13 17:20:40 +0000946 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000947 CaptureDefault(CaptureDefault),
948 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000949 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000950 ClosingBrace(ClosingBrace)
951{
952 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000953 CXXRecordDecl *Class = getLambdaClass();
954 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000955
956 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000957
958 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000959 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000960 Data.NumCaptures = NumCaptures;
961 Data.NumExplicitCaptures = 0;
962 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
963 Capture *ToCapture = Data.Captures;
964 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
965 if (Captures[I].isExplicit())
966 ++Data.NumExplicitCaptures;
967
968 *ToCapture++ = Captures[I];
969 }
970
971 // Copy initialization expressions for the non-static data members.
972 Stmt **Stored = getStoredStmts();
973 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
974 *Stored++ = CaptureInits[I];
975
976 // Copy the body of the lambda.
977 *Stored++ = getCallOperator()->getBody();
978
979 // Copy the array index variables, if any.
980 HasArrayIndexVars = !ArrayIndexVars.empty();
981 if (HasArrayIndexVars) {
982 assert(ArrayIndexStarts.size() == NumCaptures);
983 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
984 sizeof(VarDecl *) * ArrayIndexVars.size());
985 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
986 sizeof(unsigned) * Captures.size());
987 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000988 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000989}
990
Craig Toppera31a8822013-08-22 07:09:37 +0000991LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
Douglas Gregore31e6062012-02-07 10:09:13 +0000992 CXXRecordDecl *Class,
993 SourceRange IntroducerRange,
994 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000995 SourceLocation CaptureDefaultLoc,
996 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000997 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000998 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000999 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +00001000 ArrayRef<VarDecl *> ArrayIndexVars,
1001 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001002 SourceLocation ClosingBrace,
1003 bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001004 // Determine the type of the expression (i.e., the type of the
1005 // function object we're creating).
1006 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +00001007
Douglas Gregore5561632012-02-13 17:20:40 +00001008 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smithc7520bf2012-08-21 05:42:49 +00001009 if (!ArrayIndexVars.empty()) {
1010 Size += sizeof(unsigned) * (Captures.size() + 1);
1011 // Realign for following VarDecl array.
Richard Smitha75e1cf2012-08-21 18:18:06 +00001012 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smithc7520bf2012-08-21 05:42:49 +00001013 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
1014 }
Douglas Gregore5561632012-02-13 17:20:40 +00001015 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +00001016 return new (Mem) LambdaExpr(T, IntroducerRange,
1017 CaptureDefault, CaptureDefaultLoc, Captures,
1018 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001019 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001020 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +00001021}
1022
Craig Toppera31a8822013-08-22 07:09:37 +00001023LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1024 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +00001025 unsigned NumArrayIndexVars) {
1026 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
1027 if (NumArrayIndexVars)
1028 Size += sizeof(VarDecl) * NumArrayIndexVars
1029 + sizeof(unsigned) * (NumCaptures + 1);
1030 void *Mem = C.Allocate(Size);
1031 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1032}
1033
Douglas Gregorc8a73492012-02-13 15:44:47 +00001034LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001035 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001036}
1037
1038LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001039 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001040}
1041
James Dennett1575cb42014-05-27 19:13:04 +00001042LambdaExpr::capture_range LambdaExpr::captures() const {
1043 return capture_range(capture_begin(), capture_end());
1044}
1045
Douglas Gregorc8a73492012-02-13 15:44:47 +00001046LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1047 return capture_begin();
1048}
1049
1050LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1051 struct CXXRecordDecl::LambdaDefinitionData &Data
1052 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001053 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001054}
1055
James Dennett1575cb42014-05-27 19:13:04 +00001056LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1057 return capture_range(explicit_capture_begin(), explicit_capture_end());
1058}
1059
Douglas Gregorc8a73492012-02-13 15:44:47 +00001060LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1061 return explicit_capture_end();
1062}
1063
1064LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1065 return capture_end();
1066}
1067
James Dennett1575cb42014-05-27 19:13:04 +00001068LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1069 return capture_range(implicit_capture_begin(), implicit_capture_end());
1070}
1071
Douglas Gregor54fcea62012-02-13 16:35:30 +00001072ArrayRef<VarDecl *>
1073LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001074 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001075
1076 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001077 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1078 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +00001079 VarDecl **IndexVars = getArrayIndexVars();
1080 unsigned *IndexStarts = getArrayIndexStarts();
Craig Topper5fc8fc22014-08-27 06:28:36 +00001081 return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
1082 IndexVars + IndexStarts[Index + 1]);
Douglas Gregor54fcea62012-02-13 16:35:30 +00001083}
1084
Douglas Gregore31e6062012-02-07 10:09:13 +00001085CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1086 return getType()->getAsCXXRecordDecl();
1087}
1088
1089CXXMethodDecl *LambdaExpr::getCallOperator() const {
1090 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001091 return Record->getLambdaCallOperator();
1092}
1093
1094TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1095 CXXRecordDecl *Record = getLambdaClass();
1096 return Record->getGenericLambdaTemplateParameterList();
1097
Douglas Gregore31e6062012-02-07 10:09:13 +00001098}
1099
Douglas Gregor99ae8062012-02-14 17:54:36 +00001100CompoundStmt *LambdaExpr::getBody() const {
1101 if (!getStoredStmts()[NumCaptures])
1102 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1103
1104 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1105}
1106
Douglas Gregore31e6062012-02-07 10:09:13 +00001107bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001108 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001109}
1110
John McCall28fc7092011-11-10 05:35:25 +00001111ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1112 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001113 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001114 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001115 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001116 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001117 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001118 SubExpr(subexpr) {
1119 ExprWithCleanupsBits.NumObjects = objects.size();
1120 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1121 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001122}
1123
Craig Toppera31a8822013-08-22 07:09:37 +00001124ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001125 ArrayRef<CleanupObject> objects) {
1126 size_t size = sizeof(ExprWithCleanups)
1127 + objects.size() * sizeof(CleanupObject);
1128 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1129 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001130}
1131
John McCall28fc7092011-11-10 05:35:25 +00001132ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1133 : Expr(ExprWithCleanupsClass, empty) {
1134 ExprWithCleanupsBits.NumObjects = numObjects;
1135}
Chris Lattnercba86142010-05-10 00:25:06 +00001136
Craig Toppera31a8822013-08-22 07:09:37 +00001137ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1138 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001139 unsigned numObjects) {
1140 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1141 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1142 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001143}
1144
Douglas Gregor2b88c112010-09-08 00:15:04 +00001145CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001146 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001147 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001148 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001149 : Expr(CXXUnresolvedConstructExprClass,
1150 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001151 (Type->getType()->isLValueReferenceType() ? VK_LValue
1152 :Type->getType()->isRValueReferenceType()? VK_XValue
1153 :VK_RValue),
1154 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001155 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001156 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001157 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001158 LParenLoc(LParenLoc),
1159 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001160 NumArgs(Args.size()) {
Douglas Gregorce934142009-05-20 18:46:25 +00001161 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001162 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001163 if (Args[I]->containsUnexpandedParameterPack())
1164 ExprBits.ContainsUnexpandedParameterPack = true;
1165
1166 StoredArgs[I] = Args[I];
1167 }
Douglas Gregorce934142009-05-20 18:46:25 +00001168}
1169
1170CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001171CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001172 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001173 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001174 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001175 SourceLocation RParenLoc) {
1176 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001177 sizeof(Expr *) * Args.size());
1178 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001179}
1180
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001181CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001182CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001183 Stmt::EmptyShell Empty;
1184 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1185 sizeof(Expr *) * NumArgs);
1186 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1187}
1188
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001189SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1190 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001191}
1192
Craig Toppera31a8822013-08-22 07:09:37 +00001193CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001194 Expr *Base, QualType BaseType,
1195 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001196 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001197 NestedNameSpecifierLoc QualifierLoc,
1198 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001199 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001200 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001201 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001202 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001203 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001204 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001205 (QualifierLoc &&
1206 QualifierLoc.getNestedNameSpecifier()
1207 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001208 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001209 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Craig Topper36250ad2014-05-12 05:36:57 +00001210 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1211 TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001212 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001213 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001214 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001215 if (TemplateArgs) {
1216 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001217 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001218 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001219 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1220 Dependent,
1221 InstantiationDependent,
1222 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001223 if (ContainsUnexpandedParameterPack)
1224 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001225 } else if (TemplateKWLoc.isValid()) {
1226 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001227 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001228}
1229
Craig Toppera31a8822013-08-22 07:09:37 +00001230CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001231 Expr *Base, QualType BaseType,
1232 bool IsArrow,
1233 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001234 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001235 NamedDecl *FirstQualifierFoundInScope,
1236 DeclarationNameInfo MemberNameInfo)
1237 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001238 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001239 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001240 (QualifierLoc &&
1241 QualifierLoc.getNestedNameSpecifier()->
1242 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001243 MemberNameInfo.containsUnexpandedParameterPack())),
1244 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001245 HasTemplateKWAndArgsInfo(false),
1246 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001247 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1248 MemberNameInfo(MemberNameInfo) { }
1249
John McCall8cd78132009-11-19 22:55:06 +00001250CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001251CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001252 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001253 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001254 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001255 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001256 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001257 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001258 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001259 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001260 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1261 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001262 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001263 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001264 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001265
Abramo Bagnara7945c982012-01-27 09:46:47 +00001266 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1267 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1268 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001269
Chris Lattner5c0b4052010-10-30 05:14:06 +00001270 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001271 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1272 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001273 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001274 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001275 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001276 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001277}
1278
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001279CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001280CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001281 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001282 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001283 if (!HasTemplateKWAndArgsInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00001284 return new (C) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001285 0, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001286 NestedNameSpecifierLoc(),
1287 nullptr, DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001288
1289 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001290 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001291 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001292 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001293 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001294 0, SourceLocation(),
1295 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001296 SourceLocation(), nullptr,
1297 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001298 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001299 return E;
1300}
1301
Douglas Gregor0da1d432011-02-28 20:01:57 +00001302bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001303 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001304 return true;
1305
Douglas Gregor25b7e052011-03-02 21:06:53 +00001306 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001307}
1308
John McCall0009fcc2011-04-26 20:42:42 +00001309static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1310 UnresolvedSetIterator end) {
1311 do {
1312 NamedDecl *decl = *begin;
1313 if (isa<UnresolvedUsingValueDecl>(decl))
1314 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001315
1316 // Unresolved member expressions should only contain methods and
1317 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001318 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1319 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001320 return false;
1321 } while (++begin != end);
1322
1323 return true;
1324}
1325
Craig Toppera31a8822013-08-22 07:09:37 +00001326UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001327 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001328 Expr *Base, QualType BaseType,
1329 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001330 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001331 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001332 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001333 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001334 const TemplateArgumentListInfo *TemplateArgs,
1335 UnresolvedSetIterator Begin,
1336 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001337 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1338 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001339 // Dependent
1340 ((Base && Base->isTypeDependent()) ||
1341 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001342 ((Base && Base->isInstantiationDependent()) ||
1343 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001344 // Contains unexpanded parameter pack
1345 ((Base && Base->containsUnexpandedParameterPack()) ||
1346 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001347 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1348 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001349
1350 // Check whether all of the members are non-static member functions,
1351 // and if so, mark give this bound-member type instead of overload type.
1352 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1353 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001354}
1355
Douglas Gregor0da1d432011-02-28 20:01:57 +00001356bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001357 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001358 return true;
1359
Douglas Gregor25b7e052011-03-02 21:06:53 +00001360 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001361}
1362
John McCall10eae182009-11-30 22:42:35 +00001363UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001364UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001365 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001366 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001367 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001368 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001369 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001370 const TemplateArgumentListInfo *TemplateArgs,
1371 UnresolvedSetIterator Begin,
1372 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001373 std::size_t size = sizeof(UnresolvedMemberExpr);
1374 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001375 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1376 else if (TemplateKWLoc.isValid())
1377 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001378
Chris Lattner5c0b4052010-10-30 05:14:06 +00001379 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001380 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001381 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001382 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001383 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001384}
1385
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001386UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001387UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1388 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001389 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001390 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001391 if (HasTemplateKWAndArgsInfo)
1392 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001393
Chris Lattner5c0b4052010-10-30 05:14:06 +00001394 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001395 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001396 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001397 return E;
1398}
1399
John McCall58cc69d2010-01-27 01:50:18 +00001400CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1401 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1402
1403 // If there was a nested name specifier, it names the naming class.
1404 // It can't be dependent: after all, we were actually able to do the
1405 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001406 CXXRecordDecl *Record = nullptr;
John McCall1acbbb52010-02-02 06:20:04 +00001407 if (getQualifier()) {
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() { }