blob: 031332d7ce63028752ada388fe9ab575155946ee [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
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000435SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000436 OverloadedOperatorKind Kind = getOperator();
437 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
438 if (getNumArgs() == 1)
439 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000440 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000441 else
442 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000443 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000444 } else if (Kind == OO_Arrow) {
445 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000446 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000447 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000448 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000449 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000450 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000451 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000452 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000453 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000454 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000455 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000456 }
457}
458
Ted Kremenek98a24e32011-03-30 17:41:19 +0000459Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000460 const Expr *Callee = getCallee()->IgnoreParens();
461 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000462 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000463 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
464 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
465 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000466
467 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000468 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000469}
470
Ted Kremenek98a24e32011-03-30 17:41:19 +0000471CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
472 if (const MemberExpr *MemExpr =
473 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
474 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
475
476 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000477 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000478}
479
480
David Blaikiec0f58662012-05-03 16:25:49 +0000481CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000482 Expr* ThisArg = getImplicitObjectArgument();
483 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000484 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000485
486 if (ThisArg->getType()->isAnyPointerType())
487 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
488
489 return ThisArg->getType()->getAsCXXRecordDecl();
490}
491
Douglas Gregoref986e82009-11-12 15:31:47 +0000492
Douglas Gregore200adc2008-10-27 19:41:14 +0000493//===----------------------------------------------------------------------===//
494// Named casts
495//===----------------------------------------------------------------------===//
496
497/// getCastName - Get the name of the C++ cast being used, e.g.,
498/// "static_cast", "dynamic_cast", "reinterpret_cast", or
499/// "const_cast". The returned pointer must not be freed.
500const char *CXXNamedCastExpr::getCastName() const {
501 switch (getStmtClass()) {
502 case CXXStaticCastExprClass: return "static_cast";
503 case CXXDynamicCastExprClass: return "dynamic_cast";
504 case CXXReinterpretCastExprClass: return "reinterpret_cast";
505 case CXXConstCastExprClass: return "const_cast";
506 default: return "<invalid cast>";
507 }
508}
Douglas Gregordd04d332009-01-16 18:33:17 +0000509
Craig Toppera31a8822013-08-22 07:09:37 +0000510CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000511 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000512 CastKind K, Expr *Op,
513 const CXXCastPath *BasePath,
514 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000515 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000516 SourceLocation RParenLoc,
517 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000518 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000519 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000520 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000521 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000522 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000523 if (PathSize)
524 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
525 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000526 return E;
527}
528
Craig Toppera31a8822013-08-22 07:09:37 +0000529CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000530 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000531 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000532 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
533}
534
Craig Toppera31a8822013-08-22 07:09:37 +0000535CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000536 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000537 CastKind K, Expr *Op,
538 const CXXCastPath *BasePath,
539 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000540 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000541 SourceLocation RParenLoc,
542 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000543 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000544 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000545 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000546 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000547 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000548 if (PathSize)
549 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
550 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000551 return E;
552}
553
Craig Toppera31a8822013-08-22 07:09:37 +0000554CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000555 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000556 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000557 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
558}
559
Anders Carlsson267c0c92011-04-11 01:43:55 +0000560/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
561/// to always be null. For example:
562///
563/// struct A { };
564/// struct B final : A { };
565/// struct C { };
566///
567/// C *f(B* b) { return dynamic_cast<C*>(b); }
568bool CXXDynamicCastExpr::isAlwaysNull() const
569{
570 QualType SrcType = getSubExpr()->getType();
571 QualType DestType = getType();
572
573 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
574 SrcType = SrcPTy->getPointeeType();
575 DestType = DestType->castAs<PointerType>()->getPointeeType();
576 }
577
Alexis Hunt78e2b912012-06-19 23:44:55 +0000578 if (DestType->isVoidType())
579 return false;
580
Anders Carlsson267c0c92011-04-11 01:43:55 +0000581 const CXXRecordDecl *SrcRD =
582 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
583
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000584 if (!SrcRD->hasAttr<FinalAttr>())
585 return false;
586
Anders Carlsson267c0c92011-04-11 01:43:55 +0000587 const CXXRecordDecl *DestRD =
588 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
589
590 return !DestRD->isDerivedFrom(SrcRD);
591}
592
John McCallcf142162010-08-07 06:22:56 +0000593CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000594CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
595 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000596 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000597 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000598 SourceLocation RParenLoc,
599 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000600 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000601 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000602 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000603 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000604 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000605 if (PathSize)
606 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
607 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000608 return E;
609}
610
611CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000612CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000613 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000614 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
615}
616
Craig Toppera31a8822013-08-22 07:09:37 +0000617CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000618 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000619 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000620 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000621 SourceLocation RParenLoc,
622 SourceRange AngleBrackets) {
623 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000624}
625
Craig Toppera31a8822013-08-22 07:09:37 +0000626CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000627 return new (C) CXXConstCastExpr(EmptyShell());
628}
629
630CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000631CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000632 TypeSourceInfo *Written, CastKind K, Expr *Op,
633 const CXXCastPath *BasePath,
634 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000635 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000636 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000637 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000638 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000639 if (PathSize)
640 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
641 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000642 return E;
643}
644
645CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000646CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000647 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000648 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
649}
650
Eli Friedman89fe0d52013-08-15 22:02:56 +0000651SourceLocation CXXFunctionalCastExpr::getLocStart() const {
652 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
653}
654
655SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
656 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
657}
658
Richard Smithc67fdd42012-03-07 08:35:16 +0000659UserDefinedLiteral::LiteralOperatorKind
660UserDefinedLiteral::getLiteralOperatorKind() const {
661 if (getNumArgs() == 0)
662 return LOK_Template;
663 if (getNumArgs() == 2)
664 return LOK_String;
665
666 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
667 QualType ParamTy =
668 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
669 if (ParamTy->isPointerType())
670 return LOK_Raw;
671 if (ParamTy->isAnyCharacterType())
672 return LOK_Character;
673 if (ParamTy->isIntegerType())
674 return LOK_Integer;
675 if (ParamTy->isFloatingType())
676 return LOK_Floating;
677
678 llvm_unreachable("unknown kind of literal operator");
679}
680
681Expr *UserDefinedLiteral::getCookedLiteral() {
682#ifndef NDEBUG
683 LiteralOperatorKind LOK = getLiteralOperatorKind();
684 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
685#endif
686 return getArg(0);
687}
688
689const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
690 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
691}
John McCallcf142162010-08-07 06:22:56 +0000692
Craig Toppera31a8822013-08-22 07:09:37 +0000693CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000694 FieldDecl *Field, QualType T)
695 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
696 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
697 ? VK_XValue
698 : VK_RValue,
699 /*FIXME*/ OK_Ordinary, false, false, false, false),
700 Field(Field), Loc(Loc) {
701 assert(Field->hasInClassInitializer());
702}
703
Craig Toppera31a8822013-08-22 07:09:37 +0000704CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000705 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000706 return new (C) CXXTemporary(Destructor);
707}
708
Craig Toppera31a8822013-08-22 07:09:37 +0000709CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000710 CXXTemporary *Temp,
711 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000712 assert((SubExpr->getType()->isRecordType() ||
713 SubExpr->getType()->isArrayType()) &&
714 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000715
Douglas Gregora6e053e2010-12-15 01:34:56 +0000716 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000717}
718
Craig Toppera31a8822013-08-22 07:09:37 +0000719CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000720 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000721 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000722 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000723 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000724 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000725 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000726 bool StdInitListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000727 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000728 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
729 Type->getType().getNonReferenceType(),
730 Type->getTypeLoc().getBeginLoc(),
Richard Smithc83bf822016-06-10 00:58:19 +0000731 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000732 HadMultipleCandidates,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000733 ListInitialization,
734 StdInitListInitialization,
735 ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000736 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000737 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000738}
739
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000740SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
741 return Type->getTypeLoc().getBeginLoc();
742}
743
744SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000745 SourceLocation Loc = getParenOrBraceRange().getEnd();
746 if (Loc.isInvalid() && getNumArgs())
747 Loc = getArg(getNumArgs()-1)->getLocEnd();
748 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000749}
Anders Carlsson6f287832009-04-21 02:22:11 +0000750
Craig Toppera31a8822013-08-22 07:09:37 +0000751CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000752 SourceLocation Loc,
Richard Smithc2bebe92016-05-11 20:37:46 +0000753 CXXConstructorDecl *Ctor,
754 bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000755 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000756 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000757 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000758 bool StdInitListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000759 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000760 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000761 SourceRange ParenOrBraceRange) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000762 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000763 Ctor, Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000764 HadMultipleCandidates, ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000765 StdInitListInitialization,
Sebastian Redla9351792012-02-11 23:51:47 +0000766 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000767 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000768}
769
Craig Toppera31a8822013-08-22 07:09:37 +0000770CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
771 QualType T, SourceLocation Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000772 CXXConstructorDecl *Ctor,
Richard Smithc2bebe92016-05-11 20:37:46 +0000773 bool Elidable,
774 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000775 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000776 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000777 bool StdInitListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000778 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000779 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000780 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000781 : Expr(SC, T, VK_RValue, OK_Ordinary,
782 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000783 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000784 T->containsUnexpandedParameterPack()),
Richard Smithc2bebe92016-05-11 20:37:46 +0000785 Constructor(Ctor), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
786 NumArgs(Args.size()),
787 Elidable(Elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000788 ListInitialization(ListInitialization),
Richard Smithf8adcdc2014-07-17 05:12:35 +0000789 StdInitListInitialization(StdInitListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000790 ZeroInitialization(ZeroInitialization),
Craig Topper36250ad2014-05-12 05:36:57 +0000791 ConstructKind(ConstructKind), Args(nullptr)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000792{
793 if (NumArgs) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000794 this->Args = new (C) Stmt*[Args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000795
Richard Smithc2bebe92016-05-11 20:37:46 +0000796 for (unsigned i = 0; i != Args.size(); ++i) {
797 assert(Args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000798
Richard Smithc2bebe92016-05-11 20:37:46 +0000799 if (Args[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000800 ExprBits.ValueDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000801 if (Args[i]->isInstantiationDependent())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000802 ExprBits.InstantiationDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000803 if (Args[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000804 ExprBits.ContainsUnexpandedParameterPack = true;
805
Richard Smithc2bebe92016-05-11 20:37:46 +0000806 this->Args[i] = Args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000807 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000808 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000809}
810
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000811LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000812 LambdaCaptureKind Kind, VarDecl *Var,
813 SourceLocation EllipsisLoc)
John Brawn771721c2016-06-15 14:14:51 +0000814 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000815{
816 unsigned Bits = 0;
817 if (Implicit)
818 Bits |= Capture_Implicit;
819
820 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +0000821 case LCK_StarThis:
822 Bits |= Capture_ByCopy;
823 // Fall through
Craig Topper36250ad2014-05-12 05:36:57 +0000824 case LCK_This:
825 assert(!Var && "'this' capture cannot have a variable!");
John Brawn771721c2016-06-15 14:14:51 +0000826 Bits |= Capture_This;
Douglas Gregore31e6062012-02-07 10:09:13 +0000827 break;
828
829 case LCK_ByCopy:
830 Bits |= Capture_ByCopy;
831 // Fall through
832 case LCK_ByRef:
833 assert(Var && "capture must have a variable!");
834 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000835 case LCK_VLAType:
836 assert(!Var && "VLA type capture cannot have a variable!");
Alexey Bataev39c81e22014-08-28 04:28:19 +0000837 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000838 }
John Brawn771721c2016-06-15 14:14:51 +0000839 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000840}
841
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000842LambdaCaptureKind LambdaCapture::getCaptureKind() const {
John Brawn771721c2016-06-15 14:14:51 +0000843 if (capturesVLAType())
Faisal Validc6b5962016-03-21 09:25:37 +0000844 return LCK_VLAType;
John Brawn771721c2016-06-15 14:14:51 +0000845 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
846 if (capturesThis())
Faisal Validc6b5962016-03-21 09:25:37 +0000847 return CapByCopy ? LCK_StarThis : LCK_This;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000848 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000849}
850
James Y Knighte00a67e2015-12-31 04:18:25 +0000851LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
Douglas Gregore31e6062012-02-07 10:09:13 +0000852 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000853 SourceLocation CaptureDefaultLoc,
James Y Knighte00a67e2015-12-31 04:18:25 +0000854 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
855 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000856 ArrayRef<VarDecl *> ArrayIndexVars,
857 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000858 SourceLocation ClosingBrace,
859 bool ContainsUnexpandedParameterPack)
James Y Knighte00a67e2015-12-31 04:18:25 +0000860 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
861 T->isDependentType(), T->isDependentType(),
862 ContainsUnexpandedParameterPack),
863 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
864 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
865 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
866 ClosingBrace(ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000867 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000868 CXXRecordDecl *Class = getLambdaClass();
869 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000870
871 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000872
873 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000874 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000875 Data.NumCaptures = NumCaptures;
876 Data.NumExplicitCaptures = 0;
James Y Knighte00a67e2015-12-31 04:18:25 +0000877 Data.Captures =
878 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
879 LambdaCapture *ToCapture = Data.Captures;
Douglas Gregore5561632012-02-13 17:20:40 +0000880 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
881 if (Captures[I].isExplicit())
882 ++Data.NumExplicitCaptures;
883
884 *ToCapture++ = Captures[I];
885 }
886
887 // Copy initialization expressions for the non-static data members.
888 Stmt **Stored = getStoredStmts();
889 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
890 *Stored++ = CaptureInits[I];
891
892 // Copy the body of the lambda.
893 *Stored++ = getCallOperator()->getBody();
894
895 // Copy the array index variables, if any.
896 HasArrayIndexVars = !ArrayIndexVars.empty();
897 if (HasArrayIndexVars) {
898 assert(ArrayIndexStarts.size() == NumCaptures);
899 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
900 sizeof(VarDecl *) * ArrayIndexVars.size());
901 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
902 sizeof(unsigned) * Captures.size());
903 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000904 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000905}
906
James Y Knighte00a67e2015-12-31 04:18:25 +0000907LambdaExpr *LambdaExpr::Create(
908 const ASTContext &Context, CXXRecordDecl *Class,
909 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
910 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
911 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
912 ArrayRef<VarDecl *> ArrayIndexVars, ArrayRef<unsigned> ArrayIndexStarts,
913 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000914 // Determine the type of the expression (i.e., the type of the
915 // function object we're creating).
916 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000917
James Y Knighte00a67e2015-12-31 04:18:25 +0000918 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
919 Captures.size() + 1, ArrayIndexVars.empty() ? 0 : Captures.size() + 1,
920 ArrayIndexVars.size());
Douglas Gregore5561632012-02-13 17:20:40 +0000921 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +0000922 return new (Mem) LambdaExpr(T, IntroducerRange,
923 CaptureDefault, CaptureDefaultLoc, Captures,
924 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000925 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000926 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +0000927}
928
Craig Toppera31a8822013-08-22 07:09:37 +0000929LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
930 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +0000931 unsigned NumArrayIndexVars) {
James Y Knighte00a67e2015-12-31 04:18:25 +0000932 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
933 NumCaptures + 1, NumArrayIndexVars ? NumCaptures + 1 : 0,
934 NumArrayIndexVars);
Douglas Gregor99ae8062012-02-14 17:54:36 +0000935 void *Mem = C.Allocate(Size);
936 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
937}
938
James Dennettdd2ffea22015-05-07 18:48:18 +0000939bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
940 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
941 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
942}
943
Douglas Gregorc8a73492012-02-13 15:44:47 +0000944LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000945 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000946}
947
948LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000949 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000950}
951
James Dennett1575cb42014-05-27 19:13:04 +0000952LambdaExpr::capture_range LambdaExpr::captures() const {
953 return capture_range(capture_begin(), capture_end());
954}
955
Douglas Gregorc8a73492012-02-13 15:44:47 +0000956LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
957 return capture_begin();
958}
959
960LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
961 struct CXXRecordDecl::LambdaDefinitionData &Data
962 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000963 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000964}
965
James Dennett1575cb42014-05-27 19:13:04 +0000966LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
967 return capture_range(explicit_capture_begin(), explicit_capture_end());
968}
969
Douglas Gregorc8a73492012-02-13 15:44:47 +0000970LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
971 return explicit_capture_end();
972}
973
974LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
975 return capture_end();
976}
977
James Dennett1575cb42014-05-27 19:13:04 +0000978LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
979 return capture_range(implicit_capture_begin(), implicit_capture_end());
980}
981
James Y Knight53c76162015-07-17 18:21:37 +0000982ArrayRef<VarDecl *>
983LambdaExpr::getCaptureInitIndexVars(const_capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +0000984 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +0000985
986 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +0000987 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
988 "Capture index out-of-range");
James Y Knight53c76162015-07-17 18:21:37 +0000989 VarDecl *const *IndexVars = getArrayIndexVars();
990 const unsigned *IndexStarts = getArrayIndexStarts();
Craig Topper5fc8fc22014-08-27 06:28:36 +0000991 return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
992 IndexVars + IndexStarts[Index + 1]);
Douglas Gregor54fcea62012-02-13 16:35:30 +0000993}
994
Douglas Gregore31e6062012-02-07 10:09:13 +0000995CXXRecordDecl *LambdaExpr::getLambdaClass() const {
996 return getType()->getAsCXXRecordDecl();
997}
998
999CXXMethodDecl *LambdaExpr::getCallOperator() const {
1000 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001001 return Record->getLambdaCallOperator();
1002}
1003
1004TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1005 CXXRecordDecl *Record = getLambdaClass();
1006 return Record->getGenericLambdaTemplateParameterList();
1007
Douglas Gregore31e6062012-02-07 10:09:13 +00001008}
1009
Douglas Gregor99ae8062012-02-14 17:54:36 +00001010CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +00001011 // FIXME: this mutation in getBody is bogus. It should be
1012 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1013 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +00001014 if (!getStoredStmts()[NumCaptures])
James Y Knight53c76162015-07-17 18:21:37 +00001015 *const_cast<clang::Stmt **>(&getStoredStmts()[NumCaptures]) =
1016 getCallOperator()->getBody();
1017
James Y Knighte00a67e2015-12-31 04:18:25 +00001018 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001019}
1020
Douglas Gregore31e6062012-02-07 10:09:13 +00001021bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001022 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001023}
1024
John McCall28fc7092011-11-10 05:35:25 +00001025ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1026 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001027 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001028 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001029 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001030 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001031 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001032 SubExpr(subexpr) {
1033 ExprWithCleanupsBits.NumObjects = objects.size();
1034 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001035 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001036}
1037
Craig Toppera31a8822013-08-22 07:09:37 +00001038ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001039 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001040 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
1041 llvm::alignOf<ExprWithCleanups>());
Tim Shen17b3dee2016-06-09 21:13:39 +00001042 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001043}
1044
John McCall28fc7092011-11-10 05:35:25 +00001045ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1046 : Expr(ExprWithCleanupsClass, empty) {
1047 ExprWithCleanupsBits.NumObjects = numObjects;
1048}
Chris Lattnercba86142010-05-10 00:25:06 +00001049
Craig Toppera31a8822013-08-22 07:09:37 +00001050ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1051 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001052 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001053 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
1054 llvm::alignOf<ExprWithCleanups>());
John McCall28fc7092011-11-10 05:35:25 +00001055 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001056}
1057
Douglas Gregor2b88c112010-09-08 00:15:04 +00001058CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001059 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001060 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001061 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001062 : Expr(CXXUnresolvedConstructExprClass,
1063 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001064 (Type->getType()->isLValueReferenceType() ? VK_LValue
1065 :Type->getType()->isRValueReferenceType()? VK_XValue
1066 :VK_RValue),
1067 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001068 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001069 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001070 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001071 LParenLoc(LParenLoc),
1072 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001073 NumArgs(Args.size()) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001074 Expr **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001075 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001076 if (Args[I]->containsUnexpandedParameterPack())
1077 ExprBits.ContainsUnexpandedParameterPack = true;
1078
1079 StoredArgs[I] = Args[I];
1080 }
Douglas Gregorce934142009-05-20 18:46:25 +00001081}
1082
1083CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001084CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001085 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001086 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001087 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001088 SourceLocation RParenLoc) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001089 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
Benjamin Kramerc215e762012-08-24 11:54:20 +00001090 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001091}
1092
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001093CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001094CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001095 Stmt::EmptyShell Empty;
James Y Knighte00a67e2015-12-31 04:18:25 +00001096 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001097 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1098}
1099
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001100SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1101 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001102}
1103
James Y Knighte7d82282015-12-29 18:15:14 +00001104CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1105 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1106 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1107 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1108 DeclarationNameInfo MemberNameInfo,
1109 const TemplateArgumentListInfo *TemplateArgs)
1110 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1111 OK_Ordinary, true, true, true,
1112 ((Base && Base->containsUnexpandedParameterPack()) ||
1113 (QualifierLoc &&
1114 QualifierLoc.getNestedNameSpecifier()
1115 ->containsUnexpandedParameterPack()) ||
1116 MemberNameInfo.containsUnexpandedParameterPack())),
1117 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1118 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1119 TemplateKWLoc.isValid()),
1120 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1121 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1122 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001123 if (TemplateArgs) {
1124 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001125 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001126 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001127 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1128 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1129 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001130 if (ContainsUnexpandedParameterPack)
1131 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001132 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001133 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1134 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001135 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001136}
1137
John McCall8cd78132009-11-19 22:55:06 +00001138CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001139CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001140 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001141 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001142 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001143 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001144 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001145 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001146 const TemplateArgumentListInfo *TemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001147 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001148 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +00001149 std::size_t Size =
1150 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1151 HasTemplateKWAndArgsInfo, NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001152
James Y Knighte7d82282015-12-29 18:15:14 +00001153 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001154 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1155 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001156 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001157 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001158 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001159 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001160}
1161
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001162CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001163CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001164 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001165 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001166 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1167 std::size_t Size =
1168 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1169 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1170 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001171 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001172 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001173 0, SourceLocation(),
1174 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001175 SourceLocation(), nullptr,
1176 DeclarationNameInfo(), nullptr);
James Y Knighte7d82282015-12-29 18:15:14 +00001177 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001178 return E;
1179}
1180
Douglas Gregor0da1d432011-02-28 20:01:57 +00001181bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001182 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001183 return true;
1184
Douglas Gregor25b7e052011-03-02 21:06:53 +00001185 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001186}
1187
John McCall0009fcc2011-04-26 20:42:42 +00001188static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1189 UnresolvedSetIterator end) {
1190 do {
1191 NamedDecl *decl = *begin;
1192 if (isa<UnresolvedUsingValueDecl>(decl))
1193 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001194
1195 // Unresolved member expressions should only contain methods and
1196 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001197 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1198 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001199 return false;
1200 } while (++begin != end);
1201
1202 return true;
1203}
1204
Craig Toppera31a8822013-08-22 07:09:37 +00001205UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001206 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001207 Expr *Base, QualType BaseType,
1208 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001209 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001210 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001211 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001212 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001213 const TemplateArgumentListInfo *TemplateArgs,
1214 UnresolvedSetIterator Begin,
1215 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001216 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1217 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001218 // Dependent
1219 ((Base && Base->isTypeDependent()) ||
1220 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001221 ((Base && Base->isInstantiationDependent()) ||
1222 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001223 // Contains unexpanded parameter pack
1224 ((Base && Base->containsUnexpandedParameterPack()) ||
1225 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001226 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1227 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001228
1229 // Check whether all of the members are non-static member functions,
1230 // and if so, mark give this bound-member type instead of overload type.
1231 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1232 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001233}
1234
Douglas Gregor0da1d432011-02-28 20:01:57 +00001235bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001236 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001237 return true;
1238
Douglas Gregor25b7e052011-03-02 21:06:53 +00001239 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001240}
1241
James Y Knighte7d82282015-12-29 18:15:14 +00001242UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1243 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1244 bool IsArrow, SourceLocation OperatorLoc,
1245 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1246 const DeclarationNameInfo &MemberNameInfo,
1247 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1248 UnresolvedSetIterator End) {
1249 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1250 std::size_t Size =
1251 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1252 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
John McCall10eae182009-11-30 22:42:35 +00001253
James Y Knighte7d82282015-12-29 18:15:14 +00001254 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
1255 return new (Mem) UnresolvedMemberExpr(
1256 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1257 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001258}
1259
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001260UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001261UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1262 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001263 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001264 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1265 std::size_t Size =
1266 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1267 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001268
James Y Knighte7d82282015-12-29 18:15:14 +00001269 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001270 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001271 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001272 return E;
1273}
1274
John McCall58cc69d2010-01-27 01:50:18 +00001275CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1276 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1277
1278 // If there was a nested name specifier, it names the naming class.
1279 // It can't be dependent: after all, we were actually able to do the
1280 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001281 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001282 auto *NNS = getQualifier();
1283 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001284 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001285 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001286 Record = T->getAsCXXRecordDecl();
1287 assert(Record && "qualifier in member expression does not name record");
1288 }
John McCall58cc69d2010-01-27 01:50:18 +00001289 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001290 else {
John McCall58cc69d2010-01-27 01:50:18 +00001291 QualType BaseType = getBaseType().getNonReferenceType();
1292 if (isArrow()) {
1293 const PointerType *PT = BaseType->getAs<PointerType>();
1294 assert(PT && "base of arrow member access is not pointer");
1295 BaseType = PT->getPointeeType();
1296 }
1297
Douglas Gregor9262f472010-04-27 18:19:34 +00001298 Record = BaseType->getAsCXXRecordDecl();
1299 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001300 }
1301
Douglas Gregor9262f472010-04-27 18:19:34 +00001302 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001303}
1304
Richard Smithd784e682015-09-23 21:41:42 +00001305SizeOfPackExpr *
1306SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1307 NamedDecl *Pack, SourceLocation PackLoc,
1308 SourceLocation RParenLoc,
1309 Optional<unsigned> Length,
1310 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001311 void *Storage =
1312 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001313 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1314 PackLoc, RParenLoc, Length, PartialArgs);
1315}
1316
1317SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1318 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001319 void *Storage =
1320 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001321 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1322}
1323
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001324SubstNonTypeTemplateParmPackExpr::
1325SubstNonTypeTemplateParmPackExpr(QualType T,
1326 NonTypeTemplateParmDecl *Param,
1327 SourceLocation NameLoc,
1328 const TemplateArgument &ArgPack)
1329 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001330 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001331 Param(Param), Arguments(ArgPack.pack_begin()),
1332 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1333
1334TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001335 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001336}
1337
Richard Smithb15fe3a2012-09-12 00:56:43 +00001338FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1339 SourceLocation NameLoc,
1340 unsigned NumParams,
James Y Knight48fefa32015-09-30 14:04:23 +00001341 ParmVarDecl *const *Params)
1342 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1343 true, true),
1344 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001345 if (Params)
1346 std::uninitialized_copy(Params, Params + NumParams,
James Y Knighte00a67e2015-12-31 04:18:25 +00001347 getTrailingObjects<ParmVarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001348}
1349
1350FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001351FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001352 ParmVarDecl *ParamPack, SourceLocation NameLoc,
James Y Knight48fefa32015-09-30 14:04:23 +00001353 ArrayRef<ParmVarDecl *> Params) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001354 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1355 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001356}
1357
1358FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001359FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1360 unsigned NumParams) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001361 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1362 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001363}
1364
David Majnemerdaff3702014-05-01 17:50:17 +00001365void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1366 unsigned ManglingNumber) {
1367 // We only need extra state if we have to remember more than just the Stmt.
1368 if (!ExtendedBy)
1369 return;
1370
1371 // We may need to allocate extra storage for the mangling number and the
1372 // extended-by ValueDecl.
1373 if (!State.is<ExtraState *>()) {
1374 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1375 ES->Temporary = State.get<Stmt *>();
1376 State = ES;
1377 }
1378
1379 auto ES = State.get<ExtraState *>();
1380 ES->ExtendingDecl = ExtendedBy;
1381 ES->ManglingNumber = ManglingNumber;
1382}
1383
Douglas Gregor29c42f22012-02-24 07:38:34 +00001384TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1385 ArrayRef<TypeSourceInfo *> Args,
1386 SourceLocation RParenLoc,
1387 bool Value)
1388 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1389 /*TypeDependent=*/false,
1390 /*ValueDependent=*/false,
1391 /*InstantiationDependent=*/false,
1392 /*ContainsUnexpandedParameterPack=*/false),
1393 Loc(Loc), RParenLoc(RParenLoc)
1394{
1395 TypeTraitExprBits.Kind = Kind;
1396 TypeTraitExprBits.Value = Value;
1397 TypeTraitExprBits.NumArgs = Args.size();
1398
James Y Knighte00a67e2015-12-31 04:18:25 +00001399 TypeSourceInfo **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1400
Douglas Gregor29c42f22012-02-24 07:38:34 +00001401 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1402 if (Args[I]->getType()->isDependentType())
1403 setValueDependent(true);
1404 if (Args[I]->getType()->isInstantiationDependentType())
1405 setInstantiationDependent(true);
1406 if (Args[I]->getType()->containsUnexpandedParameterPack())
1407 setContainsUnexpandedParameterPack(true);
1408
1409 ToArgs[I] = Args[I];
1410 }
1411}
1412
Craig Toppera31a8822013-08-22 07:09:37 +00001413TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001414 SourceLocation Loc,
1415 TypeTrait Kind,
1416 ArrayRef<TypeSourceInfo *> Args,
1417 SourceLocation RParenLoc,
1418 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001419 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001420 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1421}
1422
Craig Toppera31a8822013-08-22 07:09:37 +00001423TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001424 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001425 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001426 return new (Mem) TypeTraitExpr(EmptyShell());
1427}
1428
David Blaikie68e081d2011-12-20 02:48:34 +00001429void ArrayTypeTraitExpr::anchor() { }