blob: dbfe58c8b3d094fe96dde16325253253f011b3c5 [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
Douglas Gregor747eb782010-07-08 06:14:04 +000057// CXXScalarValueInitExpr
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +000058SourceLocation CXXScalarValueInitExpr::getLocStart() const {
59 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +000060}
61
Sebastian Redlbd150f42008-11-21 19:14:01 +000062// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +000063CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
64 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +000065 bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +000066 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +000067 SourceRange typeIdParens, Expr *arraySize,
68 InitializationStyle initializationStyle,
69 Expr *initializer, QualType ty,
70 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +000071 SourceRange Range, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +000072 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +000073 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +000074 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +000075 ty->containsUnexpandedParameterPack()),
Craig Topper36250ad2014-05-12 05:36:57 +000076 SubExprs(nullptr), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
Sebastian Redl6047f072012-02-16 12:22:20 +000077 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
David Blaikie7b97aef2012-11-07 00:12:38 +000078 Range(Range), DirectInitRange(directInitRange),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +000079 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Craig Topper36250ad2014-05-12 05:36:57 +000080 assert((initializer != nullptr || initializationStyle == NoInit) &&
Sebastian Redl6047f072012-02-16 12:22:20 +000081 "Only NoInit can have no initializer.");
82 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Craig Topper36250ad2014-05-12 05:36:57 +000083 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
84 initializer != nullptr);
Sebastian Redlbd150f42008-11-21 19:14:01 +000085 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +000086 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +000087 if (arraySize->isInstantiationDependent())
88 ExprBits.InstantiationDependent = true;
89
Douglas Gregora6e053e2010-12-15 01:34:56 +000090 if (arraySize->containsUnexpandedParameterPack())
91 ExprBits.ContainsUnexpandedParameterPack = true;
92
Sebastian Redl351bb782008-12-02 14:43:59 +000093 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +000094 }
95
Sebastian Redl6047f072012-02-16 12:22:20 +000096 if (initializer) {
97 if (initializer->isInstantiationDependent())
98 ExprBits.InstantiationDependent = true;
99
100 if (initializer->containsUnexpandedParameterPack())
101 ExprBits.ContainsUnexpandedParameterPack = true;
102
103 SubExprs[i++] = initializer;
104 }
105
Benjamin Kramerc215e762012-08-24 11:54:20 +0000106 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000107 if (placementArgs[j]->isInstantiationDependent())
108 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000109 if (placementArgs[j]->containsUnexpandedParameterPack())
110 ExprBits.ContainsUnexpandedParameterPack = true;
111
Sebastian Redlbd150f42008-11-21 19:14:01 +0000112 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000113 }
David Blaikie3a0de212012-11-08 22:53:48 +0000114
115 switch (getInitializationStyle()) {
116 case CallInit:
117 this->Range.setEnd(DirectInitRange.getEnd()); break;
118 case ListInit:
119 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000120 default:
121 if (TypeIdParens.isValid())
122 this->Range.setEnd(TypeIdParens.getEnd());
123 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000124 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000125}
126
Craig Toppera31a8822013-08-22 07:09:37 +0000127void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000128 unsigned numPlaceArgs, bool hasInitializer){
Craig Topper36250ad2014-05-12 05:36:57 +0000129 assert(SubExprs == nullptr && "SubExprs already allocated");
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000130 Array = isArray;
131 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000132
133 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000134 SubExprs = new (C) Stmt*[TotalSize];
135}
136
Craig Toppera31a8822013-08-22 07:09:37 +0000137bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
Richard Smith902a0232015-02-14 01:52:20 +0000138 return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow(
139 Ctx) &&
140 !getOperatorNew()->isReservedGlobalPlacementOperator();
John McCall75f94982011-03-07 03:12:35 +0000141}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000142
Sebastian Redlbd150f42008-11-21 19:14:01 +0000143// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000144QualType CXXDeleteExpr::getDestroyedType() const {
145 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000146 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000147 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000148
149 if (ArgType->isDependentType() && !ArgType->isPointerType())
150 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000151
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000152 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000153}
154
Douglas Gregorad8a3362009-09-04 17:36:40 +0000155// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000156PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
157 : Type(Info)
158{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000159 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000160}
161
Craig Toppera31a8822013-08-22 07:09:37 +0000162CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000163 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
164 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
165 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
166 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000167 : Expr(CXXPseudoDestructorExprClass,
David Majnemerced8bdf2015-02-25 17:36:15 +0000168 Context.BoundMemberTy,
John McCalldb40c7f2010-12-14 08:05:40 +0000169 VK_RValue, OK_Ordinary,
170 /*isTypeDependent=*/(Base->isTypeDependent() ||
171 (DestroyedType.getTypeSourceInfo() &&
172 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000173 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000174 (Base->isInstantiationDependent() ||
175 (QualifierLoc &&
176 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
177 (ScopeType &&
178 ScopeType->getType()->isInstantiationDependentType()) ||
179 (DestroyedType.getTypeSourceInfo() &&
180 DestroyedType.getTypeSourceInfo()->getType()
181 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000182 // ContainsUnexpandedParameterPack
183 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000184 (QualifierLoc &&
185 QualifierLoc.getNestedNameSpecifier()
186 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000187 (ScopeType &&
188 ScopeType->getType()->containsUnexpandedParameterPack()) ||
189 (DestroyedType.getTypeSourceInfo() &&
190 DestroyedType.getTypeSourceInfo()->getType()
191 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000192 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000193 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000194 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
195 DestroyedType(DestroyedType) { }
196
Douglas Gregor678f90d2010-02-25 01:56:36 +0000197QualType CXXPseudoDestructorExpr::getDestroyedType() const {
198 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
199 return TInfo->getType();
200
201 return QualType();
202}
203
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000204SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000205 SourceLocation End = DestroyedType.getLocation();
206 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000207 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000208 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000209}
210
John McCalld14a8642009-11-21 08:51:07 +0000211// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000212UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000213UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000214 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000215 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000216 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000217 const DeclarationNameInfo &NameInfo,
218 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000219 const TemplateArgumentListInfo *Args,
220 UnresolvedSetIterator Begin,
221 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000222{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000223 assert(Args || TemplateKWLoc.isValid());
224 unsigned num_args = Args ? Args->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +0000225
226 std::size_t Size =
227 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(1,
228 num_args);
229 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedLookupExpr>());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000230 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
231 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000232 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000233 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000234}
235
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000236UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000237UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000238 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000239 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000240 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
241 std::size_t Size =
242 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
243 HasTemplateKWAndArgsInfo, NumTemplateArgs);
244 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000245 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000246 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000247 return E;
248}
249
Craig Toppera31a8822013-08-22 07:09:37 +0000250OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000251 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000252 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000253 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000254 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000255 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000256 UnresolvedSetIterator End,
257 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000258 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000259 bool KnownContainsUnexpandedParameterPack)
260 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
261 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000262 (KnownInstantiationDependent ||
263 NameInfo.isInstantiationDependent() ||
264 (QualifierLoc &&
265 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000266 (KnownContainsUnexpandedParameterPack ||
267 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000268 (QualifierLoc &&
269 QualifierLoc.getNestedNameSpecifier()
270 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000271 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
Craig Topper36250ad2014-05-12 05:36:57 +0000272 Results(nullptr), NumResults(End - Begin),
273 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
274 TemplateKWLoc.isValid()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000275 NumResults = End - Begin;
276 if (NumResults) {
277 // Determine whether this expression is type-dependent.
278 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
279 if ((*I)->getDeclContext()->isDependentContext() ||
280 isa<UnresolvedUsingValueDecl>(*I)) {
281 ExprBits.TypeDependent = true;
282 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000283 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000284 }
285 }
286
287 Results = static_cast<DeclAccessPair *>(
288 C.Allocate(sizeof(DeclAccessPair) * NumResults,
289 llvm::alignOf<DeclAccessPair>()));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000290 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregora6e053e2010-12-15 01:34:56 +0000291 }
292
293 // If we have explicit template arguments, check for dependent
294 // template arguments and whether they contain any unexpanded pack
295 // expansions.
296 if (TemplateArgs) {
297 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000298 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000299 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +0000300 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
301 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
302 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000303
304 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000305 ExprBits.TypeDependent = true;
306 ExprBits.ValueDependent = true;
307 }
308 if (InstantiationDependent)
309 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000310 if (ContainsUnexpandedParameterPack)
311 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000312 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000313 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000314 }
315
316 if (isTypeDependent())
317 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000318}
319
Craig Toppera31a8822013-08-22 07:09:37 +0000320void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000321 UnresolvedSetIterator Begin,
322 UnresolvedSetIterator End) {
Craig Topper36250ad2014-05-12 05:36:57 +0000323 assert(!Results && "Results already initialized!");
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000324 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000325 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000326 Results = static_cast<DeclAccessPair *>(
327 C.Allocate(sizeof(DeclAccessPair) * NumResults,
328
329 llvm::alignOf<DeclAccessPair>()));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000330 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000331 }
332}
333
John McCall8c12dc42010-04-22 18:44:12 +0000334CXXRecordDecl *OverloadExpr::getNamingClass() const {
335 if (isa<UnresolvedLookupExpr>(this))
336 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
337 else
338 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
339}
340
John McCall8cd78132009-11-19 22:55:06 +0000341// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000342DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000343 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000344 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000345 const DeclarationNameInfo &NameInfo,
346 const TemplateArgumentListInfo *Args)
347 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
348 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000349 (NameInfo.isInstantiationDependent() ||
350 (QualifierLoc &&
351 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000352 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000353 (QualifierLoc &&
354 QualifierLoc.getNestedNameSpecifier()
355 ->containsUnexpandedParameterPack()))),
356 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Craig Topper36250ad2014-05-12 05:36:57 +0000357 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000358{
359 if (Args) {
360 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000361 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000362 bool ContainsUnexpandedParameterPack
363 = ExprBits.ContainsUnexpandedParameterPack;
James Y Knighte7d82282015-12-29 18:15:14 +0000364 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
365 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
366 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000367 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000368 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000369 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
370 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000371 }
372}
373
John McCalle66edc12009-11-24 19:00:30 +0000374DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000375DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000376 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000377 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000378 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000379 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000380 assert(QualifierLoc && "should be created for dependent qualifiers");
James Y Knighte7d82282015-12-29 18:15:14 +0000381 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
382 std::size_t Size =
383 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
384 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
385 void *Mem = C.Allocate(Size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000386 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
387 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000388}
389
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000390DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000391DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000392 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000393 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000394 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
395 std::size_t Size =
396 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
397 HasTemplateKWAndArgsInfo, NumTemplateArgs);
398 void *Mem = C.Allocate(Size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000399 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000400 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000401 SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000402 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000403 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000404 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000405}
406
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000407SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000408 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000409 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
410 return Loc;
411}
412
413SourceLocation CXXConstructExpr::getLocEnd() const {
414 if (isa<CXXTemporaryObjectExpr>(this))
415 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000416
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000417 if (ParenOrBraceRange.isValid())
418 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000419
420 SourceLocation End = Loc;
421 for (unsigned I = getNumArgs(); I > 0; --I) {
422 const Expr *Arg = getArg(I-1);
423 if (!Arg->isDefaultArgument()) {
424 SourceLocation NewEnd = Arg->getLocEnd();
425 if (NewEnd.isValid()) {
426 End = NewEnd;
427 break;
428 }
429 }
430 }
431
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000432 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000433}
434
Richard Smithc2bebe92016-05-11 20:37:46 +0000435NamedDecl *CXXConstructExpr::getFoundDecl() const {
436 if (auto *Template = Constructor->getPrimaryTemplate())
437 return Template;
438 return Constructor;
439}
440
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000441SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000442 OverloadedOperatorKind Kind = getOperator();
443 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
444 if (getNumArgs() == 1)
445 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000446 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000447 else
448 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000449 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000450 } else if (Kind == OO_Arrow) {
451 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000452 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000453 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000454 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000455 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000456 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000457 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000458 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000459 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000460 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000461 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000462 }
463}
464
Ted Kremenek98a24e32011-03-30 17:41:19 +0000465Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000466 const Expr *Callee = getCallee()->IgnoreParens();
467 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000468 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000469 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
470 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
471 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000472
473 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000474 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000475}
476
Ted Kremenek98a24e32011-03-30 17:41:19 +0000477CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
478 if (const MemberExpr *MemExpr =
479 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
480 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
481
482 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000483 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000484}
485
486
David Blaikiec0f58662012-05-03 16:25:49 +0000487CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000488 Expr* ThisArg = getImplicitObjectArgument();
489 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000490 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000491
492 if (ThisArg->getType()->isAnyPointerType())
493 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
494
495 return ThisArg->getType()->getAsCXXRecordDecl();
496}
497
Douglas Gregoref986e82009-11-12 15:31:47 +0000498
Douglas Gregore200adc2008-10-27 19:41:14 +0000499//===----------------------------------------------------------------------===//
500// Named casts
501//===----------------------------------------------------------------------===//
502
503/// getCastName - Get the name of the C++ cast being used, e.g.,
504/// "static_cast", "dynamic_cast", "reinterpret_cast", or
505/// "const_cast". The returned pointer must not be freed.
506const char *CXXNamedCastExpr::getCastName() const {
507 switch (getStmtClass()) {
508 case CXXStaticCastExprClass: return "static_cast";
509 case CXXDynamicCastExprClass: return "dynamic_cast";
510 case CXXReinterpretCastExprClass: return "reinterpret_cast";
511 case CXXConstCastExprClass: return "const_cast";
512 default: return "<invalid cast>";
513 }
514}
Douglas Gregordd04d332009-01-16 18:33:17 +0000515
Craig Toppera31a8822013-08-22 07:09:37 +0000516CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000517 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000518 CastKind K, Expr *Op,
519 const CXXCastPath *BasePath,
520 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000521 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000522 SourceLocation RParenLoc,
523 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000524 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000525 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000526 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000527 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000528 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000529 if (PathSize)
530 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
531 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000532 return E;
533}
534
Craig Toppera31a8822013-08-22 07:09:37 +0000535CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000536 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000537 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000538 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
539}
540
Craig Toppera31a8822013-08-22 07:09:37 +0000541CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000542 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000543 CastKind K, Expr *Op,
544 const CXXCastPath *BasePath,
545 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000546 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000547 SourceLocation RParenLoc,
548 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000549 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000550 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000551 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000552 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000553 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000554 if (PathSize)
555 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
556 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000557 return E;
558}
559
Craig Toppera31a8822013-08-22 07:09:37 +0000560CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000561 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000562 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000563 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
564}
565
Anders Carlsson267c0c92011-04-11 01:43:55 +0000566/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
567/// to always be null. For example:
568///
569/// struct A { };
570/// struct B final : A { };
571/// struct C { };
572///
573/// C *f(B* b) { return dynamic_cast<C*>(b); }
574bool CXXDynamicCastExpr::isAlwaysNull() const
575{
576 QualType SrcType = getSubExpr()->getType();
577 QualType DestType = getType();
578
579 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
580 SrcType = SrcPTy->getPointeeType();
581 DestType = DestType->castAs<PointerType>()->getPointeeType();
582 }
583
Alexis Hunt78e2b912012-06-19 23:44:55 +0000584 if (DestType->isVoidType())
585 return false;
586
Anders Carlsson267c0c92011-04-11 01:43:55 +0000587 const CXXRecordDecl *SrcRD =
588 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
589
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000590 if (!SrcRD->hasAttr<FinalAttr>())
591 return false;
592
Anders Carlsson267c0c92011-04-11 01:43:55 +0000593 const CXXRecordDecl *DestRD =
594 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
595
596 return !DestRD->isDerivedFrom(SrcRD);
597}
598
John McCallcf142162010-08-07 06:22:56 +0000599CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000600CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
601 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000602 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000603 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000604 SourceLocation RParenLoc,
605 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000606 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000607 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000608 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000609 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000610 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000611 if (PathSize)
612 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
613 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000614 return E;
615}
616
617CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000618CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000619 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000620 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
621}
622
Craig Toppera31a8822013-08-22 07:09:37 +0000623CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000624 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000625 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000626 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000627 SourceLocation RParenLoc,
628 SourceRange AngleBrackets) {
629 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000630}
631
Craig Toppera31a8822013-08-22 07:09:37 +0000632CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000633 return new (C) CXXConstCastExpr(EmptyShell());
634}
635
636CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000637CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000638 TypeSourceInfo *Written, CastKind K, Expr *Op,
639 const CXXCastPath *BasePath,
640 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000641 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000642 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000643 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000644 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000645 if (PathSize)
646 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
647 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000648 return E;
649}
650
651CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000652CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000653 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000654 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
655}
656
Eli Friedman89fe0d52013-08-15 22:02:56 +0000657SourceLocation CXXFunctionalCastExpr::getLocStart() const {
658 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
659}
660
661SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
662 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
663}
664
Richard Smithc67fdd42012-03-07 08:35:16 +0000665UserDefinedLiteral::LiteralOperatorKind
666UserDefinedLiteral::getLiteralOperatorKind() const {
667 if (getNumArgs() == 0)
668 return LOK_Template;
669 if (getNumArgs() == 2)
670 return LOK_String;
671
672 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
673 QualType ParamTy =
674 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
675 if (ParamTy->isPointerType())
676 return LOK_Raw;
677 if (ParamTy->isAnyCharacterType())
678 return LOK_Character;
679 if (ParamTy->isIntegerType())
680 return LOK_Integer;
681 if (ParamTy->isFloatingType())
682 return LOK_Floating;
683
684 llvm_unreachable("unknown kind of literal operator");
685}
686
687Expr *UserDefinedLiteral::getCookedLiteral() {
688#ifndef NDEBUG
689 LiteralOperatorKind LOK = getLiteralOperatorKind();
690 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
691#endif
692 return getArg(0);
693}
694
695const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
696 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
697}
John McCallcf142162010-08-07 06:22:56 +0000698
Craig Toppera31a8822013-08-22 07:09:37 +0000699CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000700 FieldDecl *Field, QualType T)
701 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
702 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
703 ? VK_XValue
704 : VK_RValue,
705 /*FIXME*/ OK_Ordinary, false, false, false, false),
706 Field(Field), Loc(Loc) {
707 assert(Field->hasInClassInitializer());
708}
709
Craig Toppera31a8822013-08-22 07:09:37 +0000710CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000711 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000712 return new (C) CXXTemporary(Destructor);
713}
714
Craig Toppera31a8822013-08-22 07:09:37 +0000715CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000716 CXXTemporary *Temp,
717 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000718 assert((SubExpr->getType()->isRecordType() ||
719 SubExpr->getType()->isArrayType()) &&
720 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000721
Douglas Gregora6e053e2010-12-15 01:34:56 +0000722 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000723}
724
Craig Toppera31a8822013-08-22 07:09:37 +0000725CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Richard Smithc2bebe92016-05-11 20:37:46 +0000726 NamedDecl *Found,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000727 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000728 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000729 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000730 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000731 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000732 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000733 bool StdInitListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000734 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000735 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
736 Type->getType().getNonReferenceType(),
737 Type->getTypeLoc().getBeginLoc(),
Richard Smithc2bebe92016-05-11 20:37:46 +0000738 Found, Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000739 HadMultipleCandidates,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000740 ListInitialization,
741 StdInitListInitialization,
742 ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000743 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000744 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000745}
746
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000747SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
748 return Type->getTypeLoc().getBeginLoc();
749}
750
751SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000752 SourceLocation Loc = getParenOrBraceRange().getEnd();
753 if (Loc.isInvalid() && getNumArgs())
754 Loc = getArg(getNumArgs()-1)->getLocEnd();
755 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000756}
Anders Carlsson6f287832009-04-21 02:22:11 +0000757
Craig Toppera31a8822013-08-22 07:09:37 +0000758CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000759 SourceLocation Loc,
Richard Smithc2bebe92016-05-11 20:37:46 +0000760 NamedDecl *Found,
761 CXXConstructorDecl *Ctor,
762 bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000763 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000764 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000765 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000766 bool StdInitListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000767 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000768 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000769 SourceRange ParenOrBraceRange) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000770 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc,
771 Found, Ctor, Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000772 HadMultipleCandidates, ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000773 StdInitListInitialization,
Sebastian Redla9351792012-02-11 23:51:47 +0000774 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000775 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000776}
777
Craig Toppera31a8822013-08-22 07:09:37 +0000778CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
779 QualType T, SourceLocation Loc,
Richard Smithc2bebe92016-05-11 20:37:46 +0000780 NamedDecl *Found, CXXConstructorDecl *Ctor,
781 bool Elidable,
782 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000783 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000784 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000785 bool StdInitListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000786 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000787 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000788 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000789 : Expr(SC, T, VK_RValue, OK_Ordinary,
790 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000791 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000792 T->containsUnexpandedParameterPack()),
Richard Smithc2bebe92016-05-11 20:37:46 +0000793 Constructor(Ctor), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
794 NumArgs(Args.size()),
795 Elidable(Elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000796 ListInitialization(ListInitialization),
Richard Smithf8adcdc2014-07-17 05:12:35 +0000797 StdInitListInitialization(StdInitListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000798 ZeroInitialization(ZeroInitialization),
Craig Topper36250ad2014-05-12 05:36:57 +0000799 ConstructKind(ConstructKind), Args(nullptr)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000800{
Richard Smithc2bebe92016-05-11 20:37:46 +0000801 assert(declaresSameEntity(Found, Ctor) ||
802 declaresSameEntity(Found, Ctor->getPrimaryTemplate()));
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000803 if (NumArgs) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000804 this->Args = new (C) Stmt*[Args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000805
Richard Smithc2bebe92016-05-11 20:37:46 +0000806 for (unsigned i = 0; i != Args.size(); ++i) {
807 assert(Args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000808
Richard Smithc2bebe92016-05-11 20:37:46 +0000809 if (Args[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000810 ExprBits.ValueDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000811 if (Args[i]->isInstantiationDependent())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000812 ExprBits.InstantiationDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000813 if (Args[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000814 ExprBits.ContainsUnexpandedParameterPack = true;
815
Richard Smithc2bebe92016-05-11 20:37:46 +0000816 this->Args[i] = Args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000817 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000818 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000819}
820
Faisal Validc6b5962016-03-21 09:25:37 +0000821LambdaCapture::OpaqueCapturedEntity LambdaCapture::ThisSentinel;
822LambdaCapture::OpaqueCapturedEntity LambdaCapture::VLASentinel;
823
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000824LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000825 LambdaCaptureKind Kind, VarDecl *Var,
826 SourceLocation EllipsisLoc)
Faisal Validc6b5962016-03-21 09:25:37 +0000827 : CapturedEntityAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000828{
829 unsigned Bits = 0;
830 if (Implicit)
831 Bits |= Capture_Implicit;
832
833 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +0000834 case LCK_StarThis:
835 Bits |= Capture_ByCopy;
836 // Fall through
Craig Topper36250ad2014-05-12 05:36:57 +0000837 case LCK_This:
838 assert(!Var && "'this' capture cannot have a variable!");
Faisal Validc6b5962016-03-21 09:25:37 +0000839 CapturedEntityAndBits.setPointer(&ThisSentinel);
Douglas Gregore31e6062012-02-07 10:09:13 +0000840 break;
841
842 case LCK_ByCopy:
843 Bits |= Capture_ByCopy;
844 // Fall through
845 case LCK_ByRef:
846 assert(Var && "capture must have a variable!");
847 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000848 case LCK_VLAType:
849 assert(!Var && "VLA type capture cannot have a variable!");
Faisal Validc6b5962016-03-21 09:25:37 +0000850 CapturedEntityAndBits.setPointer(&VLASentinel);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000851 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000852 }
Faisal Validc6b5962016-03-21 09:25:37 +0000853 CapturedEntityAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000854}
855
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000856LambdaCaptureKind LambdaCapture::getCaptureKind() const {
Faisal Validc6b5962016-03-21 09:25:37 +0000857 void *Ptr = CapturedEntityAndBits.getPointer();
858 if (Ptr == &VLASentinel)
859 return LCK_VLAType;
860 const unsigned Bits = CapturedEntityAndBits.getInt();
861 bool CapByCopy = Bits & Capture_ByCopy;
862 if (Ptr == &ThisSentinel)
863 return CapByCopy ? LCK_StarThis : LCK_This;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000864 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000865}
866
James Y Knighte00a67e2015-12-31 04:18:25 +0000867LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
Douglas Gregore31e6062012-02-07 10:09:13 +0000868 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000869 SourceLocation CaptureDefaultLoc,
James Y Knighte00a67e2015-12-31 04:18:25 +0000870 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
871 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000872 ArrayRef<VarDecl *> ArrayIndexVars,
873 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000874 SourceLocation ClosingBrace,
875 bool ContainsUnexpandedParameterPack)
James Y Knighte00a67e2015-12-31 04:18:25 +0000876 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
877 T->isDependentType(), T->isDependentType(),
878 ContainsUnexpandedParameterPack),
879 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
880 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
881 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
882 ClosingBrace(ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000883 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000884 CXXRecordDecl *Class = getLambdaClass();
885 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000886
887 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000888
889 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000890 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000891 Data.NumCaptures = NumCaptures;
892 Data.NumExplicitCaptures = 0;
James Y Knighte00a67e2015-12-31 04:18:25 +0000893 Data.Captures =
894 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
895 LambdaCapture *ToCapture = Data.Captures;
Douglas Gregore5561632012-02-13 17:20:40 +0000896 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
897 if (Captures[I].isExplicit())
898 ++Data.NumExplicitCaptures;
899
900 *ToCapture++ = Captures[I];
901 }
902
903 // Copy initialization expressions for the non-static data members.
904 Stmt **Stored = getStoredStmts();
905 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
906 *Stored++ = CaptureInits[I];
907
908 // Copy the body of the lambda.
909 *Stored++ = getCallOperator()->getBody();
910
911 // Copy the array index variables, if any.
912 HasArrayIndexVars = !ArrayIndexVars.empty();
913 if (HasArrayIndexVars) {
914 assert(ArrayIndexStarts.size() == NumCaptures);
915 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
916 sizeof(VarDecl *) * ArrayIndexVars.size());
917 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
918 sizeof(unsigned) * Captures.size());
919 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000920 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000921}
922
James Y Knighte00a67e2015-12-31 04:18:25 +0000923LambdaExpr *LambdaExpr::Create(
924 const ASTContext &Context, CXXRecordDecl *Class,
925 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
926 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
927 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
928 ArrayRef<VarDecl *> ArrayIndexVars, ArrayRef<unsigned> ArrayIndexStarts,
929 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000930 // Determine the type of the expression (i.e., the type of the
931 // function object we're creating).
932 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000933
James Y Knighte00a67e2015-12-31 04:18:25 +0000934 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
935 Captures.size() + 1, ArrayIndexVars.empty() ? 0 : Captures.size() + 1,
936 ArrayIndexVars.size());
Douglas Gregore5561632012-02-13 17:20:40 +0000937 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +0000938 return new (Mem) LambdaExpr(T, IntroducerRange,
939 CaptureDefault, CaptureDefaultLoc, Captures,
940 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000941 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000942 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +0000943}
944
Craig Toppera31a8822013-08-22 07:09:37 +0000945LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
946 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +0000947 unsigned NumArrayIndexVars) {
James Y Knighte00a67e2015-12-31 04:18:25 +0000948 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
949 NumCaptures + 1, NumArrayIndexVars ? NumCaptures + 1 : 0,
950 NumArrayIndexVars);
Douglas Gregor99ae8062012-02-14 17:54:36 +0000951 void *Mem = C.Allocate(Size);
952 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
953}
954
James Dennettdd2ffea22015-05-07 18:48:18 +0000955bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
956 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
957 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
958}
959
Douglas Gregorc8a73492012-02-13 15:44:47 +0000960LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000961 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000962}
963
964LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000965 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000966}
967
James Dennett1575cb42014-05-27 19:13:04 +0000968LambdaExpr::capture_range LambdaExpr::captures() const {
969 return capture_range(capture_begin(), capture_end());
970}
971
Douglas Gregorc8a73492012-02-13 15:44:47 +0000972LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
973 return capture_begin();
974}
975
976LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
977 struct CXXRecordDecl::LambdaDefinitionData &Data
978 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000979 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000980}
981
James Dennett1575cb42014-05-27 19:13:04 +0000982LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
983 return capture_range(explicit_capture_begin(), explicit_capture_end());
984}
985
Douglas Gregorc8a73492012-02-13 15:44:47 +0000986LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
987 return explicit_capture_end();
988}
989
990LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
991 return capture_end();
992}
993
James Dennett1575cb42014-05-27 19:13:04 +0000994LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
995 return capture_range(implicit_capture_begin(), implicit_capture_end());
996}
997
James Y Knight53c76162015-07-17 18:21:37 +0000998ArrayRef<VarDecl *>
999LambdaExpr::getCaptureInitIndexVars(const_capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001000 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001001
1002 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001003 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1004 "Capture index out-of-range");
James Y Knight53c76162015-07-17 18:21:37 +00001005 VarDecl *const *IndexVars = getArrayIndexVars();
1006 const unsigned *IndexStarts = getArrayIndexStarts();
Craig Topper5fc8fc22014-08-27 06:28:36 +00001007 return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
1008 IndexVars + IndexStarts[Index + 1]);
Douglas Gregor54fcea62012-02-13 16:35:30 +00001009}
1010
Douglas Gregore31e6062012-02-07 10:09:13 +00001011CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1012 return getType()->getAsCXXRecordDecl();
1013}
1014
1015CXXMethodDecl *LambdaExpr::getCallOperator() const {
1016 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001017 return Record->getLambdaCallOperator();
1018}
1019
1020TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1021 CXXRecordDecl *Record = getLambdaClass();
1022 return Record->getGenericLambdaTemplateParameterList();
1023
Douglas Gregore31e6062012-02-07 10:09:13 +00001024}
1025
Douglas Gregor99ae8062012-02-14 17:54:36 +00001026CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +00001027 // FIXME: this mutation in getBody is bogus. It should be
1028 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1029 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +00001030 if (!getStoredStmts()[NumCaptures])
James Y Knight53c76162015-07-17 18:21:37 +00001031 *const_cast<clang::Stmt **>(&getStoredStmts()[NumCaptures]) =
1032 getCallOperator()->getBody();
1033
James Y Knighte00a67e2015-12-31 04:18:25 +00001034 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001035}
1036
Douglas Gregore31e6062012-02-07 10:09:13 +00001037bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001038 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001039}
1040
John McCall28fc7092011-11-10 05:35:25 +00001041ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1042 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001043 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001044 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001045 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001046 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001047 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001048 SubExpr(subexpr) {
1049 ExprWithCleanupsBits.NumObjects = objects.size();
1050 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001051 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001052}
1053
Craig Toppera31a8822013-08-22 07:09:37 +00001054ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001055 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001056 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
1057 llvm::alignOf<ExprWithCleanups>());
Tim Shen17b3dee2016-06-09 21:13:39 +00001058 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001059}
1060
John McCall28fc7092011-11-10 05:35:25 +00001061ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1062 : Expr(ExprWithCleanupsClass, empty) {
1063 ExprWithCleanupsBits.NumObjects = numObjects;
1064}
Chris Lattnercba86142010-05-10 00:25:06 +00001065
Craig Toppera31a8822013-08-22 07:09:37 +00001066ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1067 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001068 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001069 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
1070 llvm::alignOf<ExprWithCleanups>());
John McCall28fc7092011-11-10 05:35:25 +00001071 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001072}
1073
Douglas Gregor2b88c112010-09-08 00:15:04 +00001074CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001075 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001076 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001077 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001078 : Expr(CXXUnresolvedConstructExprClass,
1079 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001080 (Type->getType()->isLValueReferenceType() ? VK_LValue
1081 :Type->getType()->isRValueReferenceType()? VK_XValue
1082 :VK_RValue),
1083 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001084 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001085 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001086 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001087 LParenLoc(LParenLoc),
1088 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001089 NumArgs(Args.size()) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001090 Expr **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001091 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001092 if (Args[I]->containsUnexpandedParameterPack())
1093 ExprBits.ContainsUnexpandedParameterPack = true;
1094
1095 StoredArgs[I] = Args[I];
1096 }
Douglas Gregorce934142009-05-20 18:46:25 +00001097}
1098
1099CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001100CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001101 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001102 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001103 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001104 SourceLocation RParenLoc) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001105 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
Benjamin Kramerc215e762012-08-24 11:54:20 +00001106 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001107}
1108
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001109CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001110CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001111 Stmt::EmptyShell Empty;
James Y Knighte00a67e2015-12-31 04:18:25 +00001112 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001113 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1114}
1115
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001116SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1117 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001118}
1119
James Y Knighte7d82282015-12-29 18:15:14 +00001120CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1121 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1122 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1123 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1124 DeclarationNameInfo MemberNameInfo,
1125 const TemplateArgumentListInfo *TemplateArgs)
1126 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1127 OK_Ordinary, true, true, true,
1128 ((Base && Base->containsUnexpandedParameterPack()) ||
1129 (QualifierLoc &&
1130 QualifierLoc.getNestedNameSpecifier()
1131 ->containsUnexpandedParameterPack()) ||
1132 MemberNameInfo.containsUnexpandedParameterPack())),
1133 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1134 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1135 TemplateKWLoc.isValid()),
1136 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1137 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1138 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001139 if (TemplateArgs) {
1140 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001141 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001142 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001143 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1144 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1145 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001146 if (ContainsUnexpandedParameterPack)
1147 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001148 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001149 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1150 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001151 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001152}
1153
John McCall8cd78132009-11-19 22:55:06 +00001154CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001155CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001156 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001157 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001158 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001159 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001160 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001161 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001162 const TemplateArgumentListInfo *TemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001163 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001164 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +00001165 std::size_t Size =
1166 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1167 HasTemplateKWAndArgsInfo, NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001168
James Y Knighte7d82282015-12-29 18:15:14 +00001169 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001170 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1171 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001172 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001173 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001174 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001175 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001176}
1177
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001178CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001179CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001180 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001181 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001182 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1183 std::size_t Size =
1184 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1185 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1186 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001187 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001188 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001189 0, SourceLocation(),
1190 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001191 SourceLocation(), nullptr,
1192 DeclarationNameInfo(), nullptr);
James Y Knighte7d82282015-12-29 18:15:14 +00001193 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001194 return E;
1195}
1196
Douglas Gregor0da1d432011-02-28 20:01:57 +00001197bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001198 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001199 return true;
1200
Douglas Gregor25b7e052011-03-02 21:06:53 +00001201 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001202}
1203
John McCall0009fcc2011-04-26 20:42:42 +00001204static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1205 UnresolvedSetIterator end) {
1206 do {
1207 NamedDecl *decl = *begin;
1208 if (isa<UnresolvedUsingValueDecl>(decl))
1209 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001210
1211 // Unresolved member expressions should only contain methods and
1212 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001213 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1214 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001215 return false;
1216 } while (++begin != end);
1217
1218 return true;
1219}
1220
Craig Toppera31a8822013-08-22 07:09:37 +00001221UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001222 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001223 Expr *Base, QualType BaseType,
1224 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001225 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001226 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001227 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001228 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001229 const TemplateArgumentListInfo *TemplateArgs,
1230 UnresolvedSetIterator Begin,
1231 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001232 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1233 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001234 // Dependent
1235 ((Base && Base->isTypeDependent()) ||
1236 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001237 ((Base && Base->isInstantiationDependent()) ||
1238 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001239 // Contains unexpanded parameter pack
1240 ((Base && Base->containsUnexpandedParameterPack()) ||
1241 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001242 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1243 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001244
1245 // Check whether all of the members are non-static member functions,
1246 // and if so, mark give this bound-member type instead of overload type.
1247 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1248 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001249}
1250
Douglas Gregor0da1d432011-02-28 20:01:57 +00001251bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001252 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001253 return true;
1254
Douglas Gregor25b7e052011-03-02 21:06:53 +00001255 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001256}
1257
James Y Knighte7d82282015-12-29 18:15:14 +00001258UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1259 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1260 bool IsArrow, SourceLocation OperatorLoc,
1261 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1262 const DeclarationNameInfo &MemberNameInfo,
1263 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1264 UnresolvedSetIterator End) {
1265 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1266 std::size_t Size =
1267 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1268 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
John McCall10eae182009-11-30 22:42:35 +00001269
James Y Knighte7d82282015-12-29 18:15:14 +00001270 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
1271 return new (Mem) UnresolvedMemberExpr(
1272 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1273 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001274}
1275
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001276UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001277UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1278 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001279 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001280 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1281 std::size_t Size =
1282 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1283 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001284
James Y Knighte7d82282015-12-29 18:15:14 +00001285 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001286 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001287 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001288 return E;
1289}
1290
John McCall58cc69d2010-01-27 01:50:18 +00001291CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1292 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1293
1294 // If there was a nested name specifier, it names the naming class.
1295 // It can't be dependent: after all, we were actually able to do the
1296 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001297 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001298 auto *NNS = getQualifier();
1299 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001300 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001301 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001302 Record = T->getAsCXXRecordDecl();
1303 assert(Record && "qualifier in member expression does not name record");
1304 }
John McCall58cc69d2010-01-27 01:50:18 +00001305 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001306 else {
John McCall58cc69d2010-01-27 01:50:18 +00001307 QualType BaseType = getBaseType().getNonReferenceType();
1308 if (isArrow()) {
1309 const PointerType *PT = BaseType->getAs<PointerType>();
1310 assert(PT && "base of arrow member access is not pointer");
1311 BaseType = PT->getPointeeType();
1312 }
1313
Douglas Gregor9262f472010-04-27 18:19:34 +00001314 Record = BaseType->getAsCXXRecordDecl();
1315 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001316 }
1317
Douglas Gregor9262f472010-04-27 18:19:34 +00001318 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001319}
1320
Richard Smithd784e682015-09-23 21:41:42 +00001321SizeOfPackExpr *
1322SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1323 NamedDecl *Pack, SourceLocation PackLoc,
1324 SourceLocation RParenLoc,
1325 Optional<unsigned> Length,
1326 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001327 void *Storage =
1328 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001329 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1330 PackLoc, RParenLoc, Length, PartialArgs);
1331}
1332
1333SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1334 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001335 void *Storage =
1336 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001337 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1338}
1339
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001340SubstNonTypeTemplateParmPackExpr::
1341SubstNonTypeTemplateParmPackExpr(QualType T,
1342 NonTypeTemplateParmDecl *Param,
1343 SourceLocation NameLoc,
1344 const TemplateArgument &ArgPack)
1345 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001346 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001347 Param(Param), Arguments(ArgPack.pack_begin()),
1348 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1349
1350TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001351 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001352}
1353
Richard Smithb15fe3a2012-09-12 00:56:43 +00001354FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1355 SourceLocation NameLoc,
1356 unsigned NumParams,
James Y Knight48fefa32015-09-30 14:04:23 +00001357 ParmVarDecl *const *Params)
1358 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1359 true, true),
1360 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001361 if (Params)
1362 std::uninitialized_copy(Params, Params + NumParams,
James Y Knighte00a67e2015-12-31 04:18:25 +00001363 getTrailingObjects<ParmVarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001364}
1365
1366FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001367FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001368 ParmVarDecl *ParamPack, SourceLocation NameLoc,
James Y Knight48fefa32015-09-30 14:04:23 +00001369 ArrayRef<ParmVarDecl *> Params) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001370 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1371 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001372}
1373
1374FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001375FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1376 unsigned NumParams) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001377 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1378 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001379}
1380
David Majnemerdaff3702014-05-01 17:50:17 +00001381void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1382 unsigned ManglingNumber) {
1383 // We only need extra state if we have to remember more than just the Stmt.
1384 if (!ExtendedBy)
1385 return;
1386
1387 // We may need to allocate extra storage for the mangling number and the
1388 // extended-by ValueDecl.
1389 if (!State.is<ExtraState *>()) {
1390 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1391 ES->Temporary = State.get<Stmt *>();
1392 State = ES;
1393 }
1394
1395 auto ES = State.get<ExtraState *>();
1396 ES->ExtendingDecl = ExtendedBy;
1397 ES->ManglingNumber = ManglingNumber;
1398}
1399
Douglas Gregor29c42f22012-02-24 07:38:34 +00001400TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1401 ArrayRef<TypeSourceInfo *> Args,
1402 SourceLocation RParenLoc,
1403 bool Value)
1404 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1405 /*TypeDependent=*/false,
1406 /*ValueDependent=*/false,
1407 /*InstantiationDependent=*/false,
1408 /*ContainsUnexpandedParameterPack=*/false),
1409 Loc(Loc), RParenLoc(RParenLoc)
1410{
1411 TypeTraitExprBits.Kind = Kind;
1412 TypeTraitExprBits.Value = Value;
1413 TypeTraitExprBits.NumArgs = Args.size();
1414
James Y Knighte00a67e2015-12-31 04:18:25 +00001415 TypeSourceInfo **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1416
Douglas Gregor29c42f22012-02-24 07:38:34 +00001417 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1418 if (Args[I]->getType()->isDependentType())
1419 setValueDependent(true);
1420 if (Args[I]->getType()->isInstantiationDependentType())
1421 setInstantiationDependent(true);
1422 if (Args[I]->getType()->containsUnexpandedParameterPack())
1423 setContainsUnexpandedParameterPack(true);
1424
1425 ToArgs[I] = Args[I];
1426 }
1427}
1428
Craig Toppera31a8822013-08-22 07:09:37 +00001429TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001430 SourceLocation Loc,
1431 TypeTrait Kind,
1432 ArrayRef<TypeSourceInfo *> Args,
1433 SourceLocation RParenLoc,
1434 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001435 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001436 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1437}
1438
Craig Toppera31a8822013-08-22 07:09:37 +00001439TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001440 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001441 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001442 return new (Mem) TypeTraitExpr(EmptyShell());
1443}
1444
David Blaikie68e081d2011-12-20 02:48:34 +00001445void ArrayTypeTraitExpr::anchor() { }