blob: 5ad014f3e0bde06a6789a8aa9818383c961770d5 [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 Smith66094432016-10-20 00:55:15 +000028bool CXXOperatorCallExpr::isInfixBinaryOp() const {
29 // An infix binary operator is any operator with two arguments other than
30 // operator() and operator[]. Note that none of these operators can have
31 // default arguments, so it suffices to check the number of argument
32 // expressions.
33 if (getNumArgs() != 2)
34 return false;
35
36 switch (getOperator()) {
37 case OO_Call: case OO_Subscript:
38 return false;
39 default:
40 return true;
41 }
42}
43
Richard Smithef8bf432012-08-13 20:08:14 +000044bool CXXTypeidExpr::isPotentiallyEvaluated() const {
45 if (isTypeOperand())
46 return false;
47
48 // C++11 [expr.typeid]p3:
49 // When typeid is applied to an expression other than a glvalue of
50 // polymorphic class type, [...] the expression is an unevaluated operand.
51 const Expr *E = getExprOperand();
52 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
53 if (RD->isPolymorphic() && E->isGLValue())
54 return true;
55
56 return false;
57}
58
David Majnemer143c55e2013-09-27 07:04:31 +000059QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
Douglas Gregor9da64192010-04-26 22:37:10 +000060 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000061 Qualifiers Quals;
62 return Context.getUnqualifiedArrayType(
63 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Douglas Gregor9da64192010-04-26 22:37:10 +000064}
65
David Majnemer143c55e2013-09-27 07:04:31 +000066QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
Francois Pichet9f4f2072010-09-08 12:20:18 +000067 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000068 Qualifiers Quals;
69 return Context.getUnqualifiedArrayType(
70 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Francois Pichet9f4f2072010-09-08 12:20:18 +000071}
72
Douglas Gregor747eb782010-07-08 06:14:04 +000073// CXXScalarValueInitExpr
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +000074SourceLocation CXXScalarValueInitExpr::getLocStart() const {
75 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +000076}
77
Sebastian Redlbd150f42008-11-21 19:14:01 +000078// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +000079CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
80 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Richard Smithb2f0f052016-10-10 18:54:32 +000081 bool PassAlignment, bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +000082 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +000083 SourceRange typeIdParens, Expr *arraySize,
84 InitializationStyle initializationStyle,
85 Expr *initializer, QualType ty,
86 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +000087 SourceRange Range, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +000088 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +000089 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +000090 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +000091 ty->containsUnexpandedParameterPack()),
Craig Topper36250ad2014-05-12 05:36:57 +000092 SubExprs(nullptr), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
Sebastian Redl6047f072012-02-16 12:22:20 +000093 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
David Blaikie7b97aef2012-11-07 00:12:38 +000094 Range(Range), DirectInitRange(directInitRange),
Richard Smithb2f0f052016-10-10 18:54:32 +000095 GlobalNew(globalNew), PassAlignment(PassAlignment),
96 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Craig Topper36250ad2014-05-12 05:36:57 +000097 assert((initializer != nullptr || initializationStyle == NoInit) &&
Sebastian Redl6047f072012-02-16 12:22:20 +000098 "Only NoInit can have no initializer.");
99 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Craig Topper36250ad2014-05-12 05:36:57 +0000100 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
101 initializer != nullptr);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000102 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000103 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000104 if (arraySize->isInstantiationDependent())
105 ExprBits.InstantiationDependent = true;
106
Douglas Gregora6e053e2010-12-15 01:34:56 +0000107 if (arraySize->containsUnexpandedParameterPack())
108 ExprBits.ContainsUnexpandedParameterPack = true;
109
Sebastian Redl351bb782008-12-02 14:43:59 +0000110 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000111 }
112
Sebastian Redl6047f072012-02-16 12:22:20 +0000113 if (initializer) {
114 if (initializer->isInstantiationDependent())
115 ExprBits.InstantiationDependent = true;
116
117 if (initializer->containsUnexpandedParameterPack())
118 ExprBits.ContainsUnexpandedParameterPack = true;
119
120 SubExprs[i++] = initializer;
121 }
122
Benjamin Kramerc215e762012-08-24 11:54:20 +0000123 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000124 if (placementArgs[j]->isInstantiationDependent())
125 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000126 if (placementArgs[j]->containsUnexpandedParameterPack())
127 ExprBits.ContainsUnexpandedParameterPack = true;
128
Sebastian Redlbd150f42008-11-21 19:14:01 +0000129 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000130 }
David Blaikie3a0de212012-11-08 22:53:48 +0000131
132 switch (getInitializationStyle()) {
133 case CallInit:
134 this->Range.setEnd(DirectInitRange.getEnd()); break;
135 case ListInit:
136 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000137 default:
138 if (TypeIdParens.isValid())
139 this->Range.setEnd(TypeIdParens.getEnd());
140 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000141 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000142}
143
Craig Toppera31a8822013-08-22 07:09:37 +0000144void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000145 unsigned numPlaceArgs, bool hasInitializer){
Craig Topper36250ad2014-05-12 05:36:57 +0000146 assert(SubExprs == nullptr && "SubExprs already allocated");
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000147 Array = isArray;
148 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000149
150 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000151 SubExprs = new (C) Stmt*[TotalSize];
152}
153
Craig Toppera31a8822013-08-22 07:09:37 +0000154bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
Richard Smith902a0232015-02-14 01:52:20 +0000155 return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow(
156 Ctx) &&
157 !getOperatorNew()->isReservedGlobalPlacementOperator();
John McCall75f94982011-03-07 03:12:35 +0000158}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000159
Sebastian Redlbd150f42008-11-21 19:14:01 +0000160// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000161QualType CXXDeleteExpr::getDestroyedType() const {
162 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000163 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000164 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000165
166 if (ArgType->isDependentType() && !ArgType->isPointerType())
167 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000168
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000169 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000170}
171
Douglas Gregorad8a3362009-09-04 17:36:40 +0000172// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000173PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
174 : Type(Info)
175{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000176 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000177}
178
Craig Toppera31a8822013-08-22 07:09:37 +0000179CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000180 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
181 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
182 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
183 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000184 : Expr(CXXPseudoDestructorExprClass,
David Majnemerced8bdf2015-02-25 17:36:15 +0000185 Context.BoundMemberTy,
John McCalldb40c7f2010-12-14 08:05:40 +0000186 VK_RValue, OK_Ordinary,
187 /*isTypeDependent=*/(Base->isTypeDependent() ||
188 (DestroyedType.getTypeSourceInfo() &&
189 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000190 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000191 (Base->isInstantiationDependent() ||
192 (QualifierLoc &&
193 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
194 (ScopeType &&
195 ScopeType->getType()->isInstantiationDependentType()) ||
196 (DestroyedType.getTypeSourceInfo() &&
197 DestroyedType.getTypeSourceInfo()->getType()
198 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000199 // ContainsUnexpandedParameterPack
200 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000201 (QualifierLoc &&
202 QualifierLoc.getNestedNameSpecifier()
203 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000204 (ScopeType &&
205 ScopeType->getType()->containsUnexpandedParameterPack()) ||
206 (DestroyedType.getTypeSourceInfo() &&
207 DestroyedType.getTypeSourceInfo()->getType()
208 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000209 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000210 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000211 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
212 DestroyedType(DestroyedType) { }
213
Douglas Gregor678f90d2010-02-25 01:56:36 +0000214QualType CXXPseudoDestructorExpr::getDestroyedType() const {
215 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
216 return TInfo->getType();
217
218 return QualType();
219}
220
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000221SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000222 SourceLocation End = DestroyedType.getLocation();
223 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000224 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000225 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000226}
227
John McCalld14a8642009-11-21 08:51:07 +0000228// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000229UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000230UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000231 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000232 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000233 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000234 const DeclarationNameInfo &NameInfo,
235 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000236 const TemplateArgumentListInfo *Args,
237 UnresolvedSetIterator Begin,
238 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000239{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000240 assert(Args || TemplateKWLoc.isValid());
241 unsigned num_args = Args ? Args->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +0000242
243 std::size_t Size =
244 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(1,
245 num_args);
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000246 void *Mem = C.Allocate(Size, alignof(UnresolvedLookupExpr));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000247 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
248 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000249 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000250 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000251}
252
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000253UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000254UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000255 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000256 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000257 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
258 std::size_t Size =
259 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
260 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000261 void *Mem = C.Allocate(Size, alignof(UnresolvedLookupExpr));
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000262 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000263 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000264 return E;
265}
266
Craig Toppera31a8822013-08-22 07:09:37 +0000267OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000268 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000269 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000270 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000271 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000272 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000273 UnresolvedSetIterator End,
274 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000275 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000276 bool KnownContainsUnexpandedParameterPack)
277 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
278 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000279 (KnownInstantiationDependent ||
280 NameInfo.isInstantiationDependent() ||
281 (QualifierLoc &&
282 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000283 (KnownContainsUnexpandedParameterPack ||
284 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000285 (QualifierLoc &&
286 QualifierLoc.getNestedNameSpecifier()
287 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000288 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
Craig Topper36250ad2014-05-12 05:36:57 +0000289 Results(nullptr), NumResults(End - Begin),
290 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
291 TemplateKWLoc.isValid()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000292 NumResults = End - Begin;
293 if (NumResults) {
294 // Determine whether this expression is type-dependent.
295 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
296 if ((*I)->getDeclContext()->isDependentContext() ||
297 isa<UnresolvedUsingValueDecl>(*I)) {
298 ExprBits.TypeDependent = true;
299 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000300 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000301 }
302 }
303
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000304 Results = static_cast<DeclAccessPair *>(C.Allocate(
305 sizeof(DeclAccessPair) * NumResults, alignof(DeclAccessPair)));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000306 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregora6e053e2010-12-15 01:34:56 +0000307 }
308
309 // If we have explicit template arguments, check for dependent
310 // template arguments and whether they contain any unexpanded pack
311 // expansions.
312 if (TemplateArgs) {
313 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000314 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000315 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +0000316 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
317 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
318 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000319
320 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000321 ExprBits.TypeDependent = true;
322 ExprBits.ValueDependent = true;
323 }
324 if (InstantiationDependent)
325 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000326 if (ContainsUnexpandedParameterPack)
327 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000328 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000329 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000330 }
331
332 if (isTypeDependent())
333 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000334}
335
Craig Toppera31a8822013-08-22 07:09:37 +0000336void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000337 UnresolvedSetIterator Begin,
338 UnresolvedSetIterator End) {
Craig Topper36250ad2014-05-12 05:36:57 +0000339 assert(!Results && "Results already initialized!");
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000340 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000341 if (NumResults) {
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000342 Results = static_cast<DeclAccessPair *>(
343 C.Allocate(sizeof(DeclAccessPair) * NumResults,
344
345 alignof(DeclAccessPair)));
346 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000347 }
348}
349
John McCall8c12dc42010-04-22 18:44:12 +0000350CXXRecordDecl *OverloadExpr::getNamingClass() const {
351 if (isa<UnresolvedLookupExpr>(this))
352 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
353 else
354 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
355}
356
John McCall8cd78132009-11-19 22:55:06 +0000357// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000358DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000359 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000360 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000361 const DeclarationNameInfo &NameInfo,
362 const TemplateArgumentListInfo *Args)
363 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
364 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000365 (NameInfo.isInstantiationDependent() ||
366 (QualifierLoc &&
367 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000368 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000369 (QualifierLoc &&
370 QualifierLoc.getNestedNameSpecifier()
371 ->containsUnexpandedParameterPack()))),
372 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Craig Topper36250ad2014-05-12 05:36:57 +0000373 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000374{
375 if (Args) {
376 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000377 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000378 bool ContainsUnexpandedParameterPack
379 = ExprBits.ContainsUnexpandedParameterPack;
James Y Knighte7d82282015-12-29 18:15:14 +0000380 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
381 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
382 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000383 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000384 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000385 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
386 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000387 }
388}
389
John McCalle66edc12009-11-24 19:00:30 +0000390DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000391DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000392 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000393 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000394 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000395 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000396 assert(QualifierLoc && "should be created for dependent qualifiers");
James Y Knighte7d82282015-12-29 18:15:14 +0000397 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
398 std::size_t Size =
399 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
400 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
401 void *Mem = C.Allocate(Size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000402 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
403 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000404}
405
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000406DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000407DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000408 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000409 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000410 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
411 std::size_t Size =
412 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
413 HasTemplateKWAndArgsInfo, NumTemplateArgs);
414 void *Mem = C.Allocate(Size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000415 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000416 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000417 SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000418 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000419 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000420 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000421}
422
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000423SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000424 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000425 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
426 return Loc;
427}
428
429SourceLocation CXXConstructExpr::getLocEnd() const {
430 if (isa<CXXTemporaryObjectExpr>(this))
431 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000432
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000433 if (ParenOrBraceRange.isValid())
434 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000435
436 SourceLocation End = Loc;
437 for (unsigned I = getNumArgs(); I > 0; --I) {
438 const Expr *Arg = getArg(I-1);
439 if (!Arg->isDefaultArgument()) {
440 SourceLocation NewEnd = Arg->getLocEnd();
441 if (NewEnd.isValid()) {
442 End = NewEnd;
443 break;
444 }
445 }
446 }
447
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000448 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000449}
450
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000451SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000452 OverloadedOperatorKind Kind = getOperator();
453 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
454 if (getNumArgs() == 1)
455 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000456 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000457 else
458 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000459 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000460 } else if (Kind == OO_Arrow) {
461 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000462 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000463 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000464 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000465 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000466 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000467 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000468 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000469 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000470 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000471 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000472 }
473}
474
Ted Kremenek98a24e32011-03-30 17:41:19 +0000475Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000476 const Expr *Callee = getCallee()->IgnoreParens();
477 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000478 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000479 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
480 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
481 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000482
483 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000484 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000485}
486
Ted Kremenek98a24e32011-03-30 17:41:19 +0000487CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
488 if (const MemberExpr *MemExpr =
489 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
490 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
491
492 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000493 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000494}
495
496
David Blaikiec0f58662012-05-03 16:25:49 +0000497CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000498 Expr* ThisArg = getImplicitObjectArgument();
499 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000500 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000501
502 if (ThisArg->getType()->isAnyPointerType())
503 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
504
505 return ThisArg->getType()->getAsCXXRecordDecl();
506}
507
Douglas Gregoref986e82009-11-12 15:31:47 +0000508
Douglas Gregore200adc2008-10-27 19:41:14 +0000509//===----------------------------------------------------------------------===//
510// Named casts
511//===----------------------------------------------------------------------===//
512
513/// getCastName - Get the name of the C++ cast being used, e.g.,
514/// "static_cast", "dynamic_cast", "reinterpret_cast", or
515/// "const_cast". The returned pointer must not be freed.
516const char *CXXNamedCastExpr::getCastName() const {
517 switch (getStmtClass()) {
518 case CXXStaticCastExprClass: return "static_cast";
519 case CXXDynamicCastExprClass: return "dynamic_cast";
520 case CXXReinterpretCastExprClass: return "reinterpret_cast";
521 case CXXConstCastExprClass: return "const_cast";
522 default: return "<invalid cast>";
523 }
524}
Douglas Gregordd04d332009-01-16 18:33:17 +0000525
Craig Toppera31a8822013-08-22 07:09:37 +0000526CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000527 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000528 CastKind K, Expr *Op,
529 const CXXCastPath *BasePath,
530 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000531 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000532 SourceLocation RParenLoc,
533 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000534 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000535 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000536 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000537 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000538 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000539 if (PathSize)
540 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
541 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000542 return E;
543}
544
Craig Toppera31a8822013-08-22 07:09:37 +0000545CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000546 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000547 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000548 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
549}
550
Craig Toppera31a8822013-08-22 07:09:37 +0000551CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000552 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000553 CastKind K, Expr *Op,
554 const CXXCastPath *BasePath,
555 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000556 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000557 SourceLocation RParenLoc,
558 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000559 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000560 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000561 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000562 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000563 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000564 if (PathSize)
565 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
566 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000567 return E;
568}
569
Craig Toppera31a8822013-08-22 07:09:37 +0000570CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000571 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000572 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000573 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
574}
575
Anders Carlsson267c0c92011-04-11 01:43:55 +0000576/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
577/// to always be null. For example:
578///
579/// struct A { };
580/// struct B final : A { };
581/// struct C { };
582///
583/// C *f(B* b) { return dynamic_cast<C*>(b); }
584bool CXXDynamicCastExpr::isAlwaysNull() const
585{
586 QualType SrcType = getSubExpr()->getType();
587 QualType DestType = getType();
588
589 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
590 SrcType = SrcPTy->getPointeeType();
591 DestType = DestType->castAs<PointerType>()->getPointeeType();
592 }
593
Alexis Hunt78e2b912012-06-19 23:44:55 +0000594 if (DestType->isVoidType())
595 return false;
596
Anders Carlsson267c0c92011-04-11 01:43:55 +0000597 const CXXRecordDecl *SrcRD =
598 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
599
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000600 if (!SrcRD->hasAttr<FinalAttr>())
601 return false;
602
Anders Carlsson267c0c92011-04-11 01:43:55 +0000603 const CXXRecordDecl *DestRD =
604 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
605
606 return !DestRD->isDerivedFrom(SrcRD);
607}
608
John McCallcf142162010-08-07 06:22:56 +0000609CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000610CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
611 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000612 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000613 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000614 SourceLocation RParenLoc,
615 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000616 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000617 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000618 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000619 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000620 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000621 if (PathSize)
622 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
623 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000624 return E;
625}
626
627CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000628CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000629 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000630 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
631}
632
Craig Toppera31a8822013-08-22 07:09:37 +0000633CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000634 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000635 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000636 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000637 SourceLocation RParenLoc,
638 SourceRange AngleBrackets) {
639 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000640}
641
Craig Toppera31a8822013-08-22 07:09:37 +0000642CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000643 return new (C) CXXConstCastExpr(EmptyShell());
644}
645
646CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000647CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000648 TypeSourceInfo *Written, CastKind K, Expr *Op,
649 const CXXCastPath *BasePath,
650 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000651 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000652 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000653 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000654 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000655 if (PathSize)
656 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
657 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000658 return E;
659}
660
661CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000662CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000663 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000664 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
665}
666
Eli Friedman89fe0d52013-08-15 22:02:56 +0000667SourceLocation CXXFunctionalCastExpr::getLocStart() const {
668 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
669}
670
671SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
672 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
673}
674
Richard Smithc67fdd42012-03-07 08:35:16 +0000675UserDefinedLiteral::LiteralOperatorKind
676UserDefinedLiteral::getLiteralOperatorKind() const {
677 if (getNumArgs() == 0)
678 return LOK_Template;
679 if (getNumArgs() == 2)
680 return LOK_String;
681
682 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
683 QualType ParamTy =
684 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
685 if (ParamTy->isPointerType())
686 return LOK_Raw;
687 if (ParamTy->isAnyCharacterType())
688 return LOK_Character;
689 if (ParamTy->isIntegerType())
690 return LOK_Integer;
691 if (ParamTy->isFloatingType())
692 return LOK_Floating;
693
694 llvm_unreachable("unknown kind of literal operator");
695}
696
697Expr *UserDefinedLiteral::getCookedLiteral() {
698#ifndef NDEBUG
699 LiteralOperatorKind LOK = getLiteralOperatorKind();
700 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
701#endif
702 return getArg(0);
703}
704
705const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
706 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
707}
John McCallcf142162010-08-07 06:22:56 +0000708
Craig Toppera31a8822013-08-22 07:09:37 +0000709CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000710 FieldDecl *Field, QualType T)
711 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
712 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
713 ? VK_XValue
714 : VK_RValue,
715 /*FIXME*/ OK_Ordinary, false, false, false, false),
716 Field(Field), Loc(Loc) {
717 assert(Field->hasInClassInitializer());
718}
719
Craig Toppera31a8822013-08-22 07:09:37 +0000720CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000721 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000722 return new (C) CXXTemporary(Destructor);
723}
724
Craig Toppera31a8822013-08-22 07:09:37 +0000725CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000726 CXXTemporary *Temp,
727 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000728 assert((SubExpr->getType()->isRecordType() ||
729 SubExpr->getType()->isArrayType()) &&
730 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000731
Douglas Gregora6e053e2010-12-15 01:34:56 +0000732 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000733}
734
Craig Toppera31a8822013-08-22 07:09:37 +0000735CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000736 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000737 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000738 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000739 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000740 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000741 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000742 bool StdInitListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000743 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000744 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
745 Type->getType().getNonReferenceType(),
746 Type->getTypeLoc().getBeginLoc(),
Richard Smithc83bf822016-06-10 00:58:19 +0000747 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000748 HadMultipleCandidates,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000749 ListInitialization,
750 StdInitListInitialization,
751 ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000752 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000753 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000754}
755
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000756SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
757 return Type->getTypeLoc().getBeginLoc();
758}
759
760SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000761 SourceLocation Loc = getParenOrBraceRange().getEnd();
762 if (Loc.isInvalid() && getNumArgs())
763 Loc = getArg(getNumArgs()-1)->getLocEnd();
764 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000765}
Anders Carlsson6f287832009-04-21 02:22:11 +0000766
Craig Toppera31a8822013-08-22 07:09:37 +0000767CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000768 SourceLocation Loc,
Richard Smithc2bebe92016-05-11 20:37:46 +0000769 CXXConstructorDecl *Ctor,
770 bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000771 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000772 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000773 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000774 bool StdInitListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000775 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000776 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000777 SourceRange ParenOrBraceRange) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000778 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000779 Ctor, Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000780 HadMultipleCandidates, ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000781 StdInitListInitialization,
Sebastian Redla9351792012-02-11 23:51:47 +0000782 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000783 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000784}
785
Craig Toppera31a8822013-08-22 07:09:37 +0000786CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
787 QualType T, SourceLocation Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000788 CXXConstructorDecl *Ctor,
Richard Smithc2bebe92016-05-11 20:37:46 +0000789 bool Elidable,
790 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000791 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000792 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000793 bool StdInitListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000794 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000795 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000796 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000797 : Expr(SC, T, VK_RValue, OK_Ordinary,
798 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000799 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000800 T->containsUnexpandedParameterPack()),
Richard Smithc2bebe92016-05-11 20:37:46 +0000801 Constructor(Ctor), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
802 NumArgs(Args.size()),
803 Elidable(Elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000804 ListInitialization(ListInitialization),
Richard Smithf8adcdc2014-07-17 05:12:35 +0000805 StdInitListInitialization(StdInitListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000806 ZeroInitialization(ZeroInitialization),
Craig Topper36250ad2014-05-12 05:36:57 +0000807 ConstructKind(ConstructKind), Args(nullptr)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000808{
809 if (NumArgs) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000810 this->Args = new (C) Stmt*[Args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000811
Richard Smithc2bebe92016-05-11 20:37:46 +0000812 for (unsigned i = 0; i != Args.size(); ++i) {
813 assert(Args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000814
Richard Smithc2bebe92016-05-11 20:37:46 +0000815 if (Args[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000816 ExprBits.ValueDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000817 if (Args[i]->isInstantiationDependent())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000818 ExprBits.InstantiationDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000819 if (Args[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000820 ExprBits.ContainsUnexpandedParameterPack = true;
821
Richard Smithc2bebe92016-05-11 20:37:46 +0000822 this->Args[i] = Args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000823 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000824 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000825}
826
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000827LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000828 LambdaCaptureKind Kind, VarDecl *Var,
829 SourceLocation EllipsisLoc)
John Brawn771721c2016-06-15 14:14:51 +0000830 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000831{
832 unsigned Bits = 0;
833 if (Implicit)
834 Bits |= Capture_Implicit;
835
836 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +0000837 case LCK_StarThis:
838 Bits |= Capture_ByCopy;
839 // Fall through
Craig Topper36250ad2014-05-12 05:36:57 +0000840 case LCK_This:
841 assert(!Var && "'this' capture cannot have a variable!");
John Brawn771721c2016-06-15 14:14:51 +0000842 Bits |= Capture_This;
Douglas Gregore31e6062012-02-07 10:09:13 +0000843 break;
844
845 case LCK_ByCopy:
846 Bits |= Capture_ByCopy;
847 // Fall through
848 case LCK_ByRef:
849 assert(Var && "capture must have a variable!");
850 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000851 case LCK_VLAType:
852 assert(!Var && "VLA type capture cannot have a variable!");
Alexey Bataev39c81e22014-08-28 04:28:19 +0000853 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000854 }
John Brawn771721c2016-06-15 14:14:51 +0000855 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000856}
857
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000858LambdaCaptureKind LambdaCapture::getCaptureKind() const {
John Brawn771721c2016-06-15 14:14:51 +0000859 if (capturesVLAType())
Faisal Validc6b5962016-03-21 09:25:37 +0000860 return LCK_VLAType;
John Brawn771721c2016-06-15 14:14:51 +0000861 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
862 if (capturesThis())
Faisal Validc6b5962016-03-21 09:25:37 +0000863 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,
Tim Shen4a05bb82016-06-21 20:29:17 +00001042 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001043 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001044 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001045 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001046 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001047 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001048 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001049 SubExpr(subexpr) {
Tim Shen4a05bb82016-06-21 20:29:17 +00001050 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
John McCall28fc7092011-11-10 05:35:25 +00001051 ExprWithCleanupsBits.NumObjects = objects.size();
1052 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001053 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001054}
1055
Craig Toppera31a8822013-08-22 07:09:37 +00001056ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001057 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001058 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001059 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001060 alignof(ExprWithCleanups));
Tim Shen4a05bb82016-06-21 20:29:17 +00001061 return new (buffer)
1062 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001063}
1064
John McCall28fc7092011-11-10 05:35:25 +00001065ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1066 : Expr(ExprWithCleanupsClass, empty) {
1067 ExprWithCleanupsBits.NumObjects = numObjects;
1068}
Chris Lattnercba86142010-05-10 00:25:06 +00001069
Craig Toppera31a8822013-08-22 07:09:37 +00001070ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1071 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001072 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001073 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001074 alignof(ExprWithCleanups));
John McCall28fc7092011-11-10 05:35:25 +00001075 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001076}
1077
Douglas Gregor2b88c112010-09-08 00:15:04 +00001078CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001079 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001080 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001081 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001082 : Expr(CXXUnresolvedConstructExprClass,
1083 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001084 (Type->getType()->isLValueReferenceType() ? VK_LValue
1085 :Type->getType()->isRValueReferenceType()? VK_XValue
1086 :VK_RValue),
1087 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001088 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001089 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001090 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001091 LParenLoc(LParenLoc),
1092 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001093 NumArgs(Args.size()) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001094 Expr **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001095 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001096 if (Args[I]->containsUnexpandedParameterPack())
1097 ExprBits.ContainsUnexpandedParameterPack = true;
1098
1099 StoredArgs[I] = Args[I];
1100 }
Douglas Gregorce934142009-05-20 18:46:25 +00001101}
1102
1103CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001104CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001105 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001106 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001107 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001108 SourceLocation RParenLoc) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001109 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
Benjamin Kramerc215e762012-08-24 11:54:20 +00001110 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001111}
1112
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001113CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001114CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001115 Stmt::EmptyShell Empty;
James Y Knighte00a67e2015-12-31 04:18:25 +00001116 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001117 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1118}
1119
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001120SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1121 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001122}
1123
James Y Knighte7d82282015-12-29 18:15:14 +00001124CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1125 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1126 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1127 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1128 DeclarationNameInfo MemberNameInfo,
1129 const TemplateArgumentListInfo *TemplateArgs)
1130 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1131 OK_Ordinary, true, true, true,
1132 ((Base && Base->containsUnexpandedParameterPack()) ||
1133 (QualifierLoc &&
1134 QualifierLoc.getNestedNameSpecifier()
1135 ->containsUnexpandedParameterPack()) ||
1136 MemberNameInfo.containsUnexpandedParameterPack())),
1137 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1138 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1139 TemplateKWLoc.isValid()),
1140 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1141 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1142 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001143 if (TemplateArgs) {
1144 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001145 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001146 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001147 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1148 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1149 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001150 if (ContainsUnexpandedParameterPack)
1151 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001152 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001153 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1154 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001155 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001156}
1157
John McCall8cd78132009-11-19 22:55:06 +00001158CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001159CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001160 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001161 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001162 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001163 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001164 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001165 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001166 const TemplateArgumentListInfo *TemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001167 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001168 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +00001169 std::size_t Size =
1170 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1171 HasTemplateKWAndArgsInfo, NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001172
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001173 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
John McCall2d74de92009-12-01 22:10:20 +00001174 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1175 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001176 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001177 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001178 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001179 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001180}
1181
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001182CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001183CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001184 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001185 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001186 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1187 std::size_t Size =
1188 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1189 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001190 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001191 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001192 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001193 0, SourceLocation(),
1194 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001195 SourceLocation(), nullptr,
1196 DeclarationNameInfo(), nullptr);
James Y Knighte7d82282015-12-29 18:15:14 +00001197 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001198 return E;
1199}
1200
Douglas Gregor0da1d432011-02-28 20:01:57 +00001201bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001202 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001203 return true;
1204
Douglas Gregor25b7e052011-03-02 21:06:53 +00001205 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001206}
1207
John McCall0009fcc2011-04-26 20:42:42 +00001208static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1209 UnresolvedSetIterator end) {
1210 do {
1211 NamedDecl *decl = *begin;
1212 if (isa<UnresolvedUsingValueDecl>(decl))
1213 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001214
1215 // Unresolved member expressions should only contain methods and
1216 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001217 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1218 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001219 return false;
1220 } while (++begin != end);
1221
1222 return true;
1223}
1224
Craig Toppera31a8822013-08-22 07:09:37 +00001225UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001226 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001227 Expr *Base, QualType BaseType,
1228 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001229 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001230 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001231 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001232 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001233 const TemplateArgumentListInfo *TemplateArgs,
1234 UnresolvedSetIterator Begin,
1235 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001236 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1237 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001238 // Dependent
1239 ((Base && Base->isTypeDependent()) ||
1240 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001241 ((Base && Base->isInstantiationDependent()) ||
1242 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001243 // Contains unexpanded parameter pack
1244 ((Base && Base->containsUnexpandedParameterPack()) ||
1245 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001246 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1247 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001248
1249 // Check whether all of the members are non-static member functions,
1250 // and if so, mark give this bound-member type instead of overload type.
1251 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1252 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001253}
1254
Douglas Gregor0da1d432011-02-28 20:01:57 +00001255bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001256 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001257 return true;
1258
Douglas Gregor25b7e052011-03-02 21:06:53 +00001259 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001260}
1261
James Y Knighte7d82282015-12-29 18:15:14 +00001262UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1263 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1264 bool IsArrow, SourceLocation OperatorLoc,
1265 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1266 const DeclarationNameInfo &MemberNameInfo,
1267 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1268 UnresolvedSetIterator End) {
1269 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1270 std::size_t Size =
1271 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1272 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
John McCall10eae182009-11-30 22:42:35 +00001273
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001274 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
James Y Knighte7d82282015-12-29 18:15:14 +00001275 return new (Mem) UnresolvedMemberExpr(
1276 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1277 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001278}
1279
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001280UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001281UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1282 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001283 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001284 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1285 std::size_t Size =
1286 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1287 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001288
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001289 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001290 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001291 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001292 return E;
1293}
1294
John McCall58cc69d2010-01-27 01:50:18 +00001295CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1296 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1297
1298 // If there was a nested name specifier, it names the naming class.
1299 // It can't be dependent: after all, we were actually able to do the
1300 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001301 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001302 auto *NNS = getQualifier();
1303 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001304 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001305 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001306 Record = T->getAsCXXRecordDecl();
1307 assert(Record && "qualifier in member expression does not name record");
1308 }
John McCall58cc69d2010-01-27 01:50:18 +00001309 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001310 else {
John McCall58cc69d2010-01-27 01:50:18 +00001311 QualType BaseType = getBaseType().getNonReferenceType();
1312 if (isArrow()) {
1313 const PointerType *PT = BaseType->getAs<PointerType>();
1314 assert(PT && "base of arrow member access is not pointer");
1315 BaseType = PT->getPointeeType();
1316 }
1317
Douglas Gregor9262f472010-04-27 18:19:34 +00001318 Record = BaseType->getAsCXXRecordDecl();
1319 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001320 }
1321
Douglas Gregor9262f472010-04-27 18:19:34 +00001322 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001323}
1324
Richard Smithd784e682015-09-23 21:41:42 +00001325SizeOfPackExpr *
1326SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1327 NamedDecl *Pack, SourceLocation PackLoc,
1328 SourceLocation RParenLoc,
1329 Optional<unsigned> Length,
1330 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001331 void *Storage =
1332 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001333 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1334 PackLoc, RParenLoc, Length, PartialArgs);
1335}
1336
1337SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1338 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001339 void *Storage =
1340 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001341 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1342}
1343
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001344SubstNonTypeTemplateParmPackExpr::
1345SubstNonTypeTemplateParmPackExpr(QualType T,
1346 NonTypeTemplateParmDecl *Param,
1347 SourceLocation NameLoc,
1348 const TemplateArgument &ArgPack)
1349 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001350 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001351 Param(Param), Arguments(ArgPack.pack_begin()),
1352 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1353
1354TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001355 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001356}
1357
Richard Smithb15fe3a2012-09-12 00:56:43 +00001358FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1359 SourceLocation NameLoc,
1360 unsigned NumParams,
James Y Knight48fefa32015-09-30 14:04:23 +00001361 ParmVarDecl *const *Params)
1362 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1363 true, true),
1364 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001365 if (Params)
1366 std::uninitialized_copy(Params, Params + NumParams,
James Y Knighte00a67e2015-12-31 04:18:25 +00001367 getTrailingObjects<ParmVarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001368}
1369
1370FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001371FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001372 ParmVarDecl *ParamPack, SourceLocation NameLoc,
James Y Knight48fefa32015-09-30 14:04:23 +00001373 ArrayRef<ParmVarDecl *> Params) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001374 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1375 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001376}
1377
1378FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001379FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1380 unsigned NumParams) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001381 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1382 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001383}
1384
David Majnemerdaff3702014-05-01 17:50:17 +00001385void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1386 unsigned ManglingNumber) {
1387 // We only need extra state if we have to remember more than just the Stmt.
1388 if (!ExtendedBy)
1389 return;
1390
1391 // We may need to allocate extra storage for the mangling number and the
1392 // extended-by ValueDecl.
1393 if (!State.is<ExtraState *>()) {
1394 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1395 ES->Temporary = State.get<Stmt *>();
1396 State = ES;
1397 }
1398
1399 auto ES = State.get<ExtraState *>();
1400 ES->ExtendingDecl = ExtendedBy;
1401 ES->ManglingNumber = ManglingNumber;
1402}
1403
Douglas Gregor29c42f22012-02-24 07:38:34 +00001404TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1405 ArrayRef<TypeSourceInfo *> Args,
1406 SourceLocation RParenLoc,
1407 bool Value)
1408 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1409 /*TypeDependent=*/false,
1410 /*ValueDependent=*/false,
1411 /*InstantiationDependent=*/false,
1412 /*ContainsUnexpandedParameterPack=*/false),
1413 Loc(Loc), RParenLoc(RParenLoc)
1414{
1415 TypeTraitExprBits.Kind = Kind;
1416 TypeTraitExprBits.Value = Value;
1417 TypeTraitExprBits.NumArgs = Args.size();
1418
James Y Knighte00a67e2015-12-31 04:18:25 +00001419 TypeSourceInfo **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1420
Douglas Gregor29c42f22012-02-24 07:38:34 +00001421 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1422 if (Args[I]->getType()->isDependentType())
1423 setValueDependent(true);
1424 if (Args[I]->getType()->isInstantiationDependentType())
1425 setInstantiationDependent(true);
1426 if (Args[I]->getType()->containsUnexpandedParameterPack())
1427 setContainsUnexpandedParameterPack(true);
1428
1429 ToArgs[I] = Args[I];
1430 }
1431}
1432
Craig Toppera31a8822013-08-22 07:09:37 +00001433TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001434 SourceLocation Loc,
1435 TypeTrait Kind,
1436 ArrayRef<TypeSourceInfo *> Args,
1437 SourceLocation RParenLoc,
1438 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001439 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001440 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1441}
1442
Craig Toppera31a8822013-08-22 07:09:37 +00001443TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001444 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001445 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001446 return new (Mem) TypeTraitExpr(EmptyShell());
1447}
1448
David Blaikie68e081d2011-12-20 02:48:34 +00001449void ArrayTypeTraitExpr::anchor() { }