blob: 720a13a850ad8cb88ab4f2e44c791c7895f27cab [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);
246 void *Mem = C.Allocate(Size, llvm::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);
261 void *Mem = C.Allocate(Size, llvm::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
304 Results = static_cast<DeclAccessPair *>(
305 C.Allocate(sizeof(DeclAccessPair) * NumResults,
306 llvm::alignOf<DeclAccessPair>()));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000307 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregora6e053e2010-12-15 01:34:56 +0000308 }
309
310 // If we have explicit template arguments, check for dependent
311 // template arguments and whether they contain any unexpanded pack
312 // expansions.
313 if (TemplateArgs) {
314 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000315 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000316 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +0000317 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
318 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
319 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000320
321 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000322 ExprBits.TypeDependent = true;
323 ExprBits.ValueDependent = true;
324 }
325 if (InstantiationDependent)
326 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000327 if (ContainsUnexpandedParameterPack)
328 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000329 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000330 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000331 }
332
333 if (isTypeDependent())
334 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000335}
336
Craig Toppera31a8822013-08-22 07:09:37 +0000337void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000338 UnresolvedSetIterator Begin,
339 UnresolvedSetIterator End) {
Craig Topper36250ad2014-05-12 05:36:57 +0000340 assert(!Results && "Results already initialized!");
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000341 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000342 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000343 Results = static_cast<DeclAccessPair *>(
344 C.Allocate(sizeof(DeclAccessPair) * NumResults,
345
346 llvm::alignOf<DeclAccessPair>()));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000347 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000348 }
349}
350
John McCall8c12dc42010-04-22 18:44:12 +0000351CXXRecordDecl *OverloadExpr::getNamingClass() const {
352 if (isa<UnresolvedLookupExpr>(this))
353 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
354 else
355 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
356}
357
John McCall8cd78132009-11-19 22:55:06 +0000358// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000359DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000360 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000361 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000362 const DeclarationNameInfo &NameInfo,
363 const TemplateArgumentListInfo *Args)
364 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
365 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000366 (NameInfo.isInstantiationDependent() ||
367 (QualifierLoc &&
368 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000369 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000370 (QualifierLoc &&
371 QualifierLoc.getNestedNameSpecifier()
372 ->containsUnexpandedParameterPack()))),
373 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Craig Topper36250ad2014-05-12 05:36:57 +0000374 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000375{
376 if (Args) {
377 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000378 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000379 bool ContainsUnexpandedParameterPack
380 = ExprBits.ContainsUnexpandedParameterPack;
James Y Knighte7d82282015-12-29 18:15:14 +0000381 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
382 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
383 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000384 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000385 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000386 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
387 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000388 }
389}
390
John McCalle66edc12009-11-24 19:00:30 +0000391DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000392DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000393 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000394 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000395 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000396 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000397 assert(QualifierLoc && "should be created for dependent qualifiers");
James Y Knighte7d82282015-12-29 18:15:14 +0000398 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
399 std::size_t Size =
400 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
401 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
402 void *Mem = C.Allocate(Size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000403 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
404 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000405}
406
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000407DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000408DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000409 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000410 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000411 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
412 std::size_t Size =
413 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
414 HasTemplateKWAndArgsInfo, NumTemplateArgs);
415 void *Mem = C.Allocate(Size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000416 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000417 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000418 SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000419 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000420 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000421 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000422}
423
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000424SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000425 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000426 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
427 return Loc;
428}
429
430SourceLocation CXXConstructExpr::getLocEnd() const {
431 if (isa<CXXTemporaryObjectExpr>(this))
432 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000433
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000434 if (ParenOrBraceRange.isValid())
435 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000436
437 SourceLocation End = Loc;
438 for (unsigned I = getNumArgs(); I > 0; --I) {
439 const Expr *Arg = getArg(I-1);
440 if (!Arg->isDefaultArgument()) {
441 SourceLocation NewEnd = Arg->getLocEnd();
442 if (NewEnd.isValid()) {
443 End = NewEnd;
444 break;
445 }
446 }
447 }
448
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000449 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000450}
451
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000452SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000453 OverloadedOperatorKind Kind = getOperator();
454 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
455 if (getNumArgs() == 1)
456 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000457 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000458 else
459 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000460 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000461 } else if (Kind == OO_Arrow) {
462 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000463 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000464 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000465 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000466 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000467 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000468 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000469 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000470 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000471 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000472 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000473 }
474}
475
Ted Kremenek98a24e32011-03-30 17:41:19 +0000476Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000477 const Expr *Callee = getCallee()->IgnoreParens();
478 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000479 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000480 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
481 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
482 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000483
484 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000485 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000486}
487
Ted Kremenek98a24e32011-03-30 17:41:19 +0000488CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
489 if (const MemberExpr *MemExpr =
490 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
491 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
492
493 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000494 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000495}
496
497
David Blaikiec0f58662012-05-03 16:25:49 +0000498CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000499 Expr* ThisArg = getImplicitObjectArgument();
500 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000501 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000502
503 if (ThisArg->getType()->isAnyPointerType())
504 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
505
506 return ThisArg->getType()->getAsCXXRecordDecl();
507}
508
Douglas Gregoref986e82009-11-12 15:31:47 +0000509
Douglas Gregore200adc2008-10-27 19:41:14 +0000510//===----------------------------------------------------------------------===//
511// Named casts
512//===----------------------------------------------------------------------===//
513
514/// getCastName - Get the name of the C++ cast being used, e.g.,
515/// "static_cast", "dynamic_cast", "reinterpret_cast", or
516/// "const_cast". The returned pointer must not be freed.
517const char *CXXNamedCastExpr::getCastName() const {
518 switch (getStmtClass()) {
519 case CXXStaticCastExprClass: return "static_cast";
520 case CXXDynamicCastExprClass: return "dynamic_cast";
521 case CXXReinterpretCastExprClass: return "reinterpret_cast";
522 case CXXConstCastExprClass: return "const_cast";
523 default: return "<invalid cast>";
524 }
525}
Douglas Gregordd04d332009-01-16 18:33:17 +0000526
Craig Toppera31a8822013-08-22 07:09:37 +0000527CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000528 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000529 CastKind K, Expr *Op,
530 const CXXCastPath *BasePath,
531 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000532 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000533 SourceLocation RParenLoc,
534 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000535 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000536 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000537 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000538 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000539 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000540 if (PathSize)
541 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
542 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000543 return E;
544}
545
Craig Toppera31a8822013-08-22 07:09:37 +0000546CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000547 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000548 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000549 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
550}
551
Craig Toppera31a8822013-08-22 07:09:37 +0000552CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000553 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000554 CastKind K, Expr *Op,
555 const CXXCastPath *BasePath,
556 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000557 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000558 SourceLocation RParenLoc,
559 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000560 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000561 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000562 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000563 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000564 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000565 if (PathSize)
566 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
567 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000568 return E;
569}
570
Craig Toppera31a8822013-08-22 07:09:37 +0000571CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000572 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000573 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000574 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
575}
576
Anders Carlsson267c0c92011-04-11 01:43:55 +0000577/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
578/// to always be null. For example:
579///
580/// struct A { };
581/// struct B final : A { };
582/// struct C { };
583///
584/// C *f(B* b) { return dynamic_cast<C*>(b); }
585bool CXXDynamicCastExpr::isAlwaysNull() const
586{
587 QualType SrcType = getSubExpr()->getType();
588 QualType DestType = getType();
589
590 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
591 SrcType = SrcPTy->getPointeeType();
592 DestType = DestType->castAs<PointerType>()->getPointeeType();
593 }
594
Alexis Hunt78e2b912012-06-19 23:44:55 +0000595 if (DestType->isVoidType())
596 return false;
597
Anders Carlsson267c0c92011-04-11 01:43:55 +0000598 const CXXRecordDecl *SrcRD =
599 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
600
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000601 if (!SrcRD->hasAttr<FinalAttr>())
602 return false;
603
Anders Carlsson267c0c92011-04-11 01:43:55 +0000604 const CXXRecordDecl *DestRD =
605 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
606
607 return !DestRD->isDerivedFrom(SrcRD);
608}
609
John McCallcf142162010-08-07 06:22:56 +0000610CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000611CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
612 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000613 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000614 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000615 SourceLocation RParenLoc,
616 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000617 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000618 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000619 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000620 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000621 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000622 if (PathSize)
623 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
624 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000625 return E;
626}
627
628CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000629CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000630 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000631 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
632}
633
Craig Toppera31a8822013-08-22 07:09:37 +0000634CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000635 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000636 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000637 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000638 SourceLocation RParenLoc,
639 SourceRange AngleBrackets) {
640 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000641}
642
Craig Toppera31a8822013-08-22 07:09:37 +0000643CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000644 return new (C) CXXConstCastExpr(EmptyShell());
645}
646
647CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000648CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000649 TypeSourceInfo *Written, CastKind K, Expr *Op,
650 const CXXCastPath *BasePath,
651 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000652 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000653 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000654 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000655 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000656 if (PathSize)
657 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
658 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000659 return E;
660}
661
662CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000663CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000664 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000665 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
666}
667
Eli Friedman89fe0d52013-08-15 22:02:56 +0000668SourceLocation CXXFunctionalCastExpr::getLocStart() const {
669 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
670}
671
672SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
673 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
674}
675
Richard Smithc67fdd42012-03-07 08:35:16 +0000676UserDefinedLiteral::LiteralOperatorKind
677UserDefinedLiteral::getLiteralOperatorKind() const {
678 if (getNumArgs() == 0)
679 return LOK_Template;
680 if (getNumArgs() == 2)
681 return LOK_String;
682
683 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
684 QualType ParamTy =
685 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
686 if (ParamTy->isPointerType())
687 return LOK_Raw;
688 if (ParamTy->isAnyCharacterType())
689 return LOK_Character;
690 if (ParamTy->isIntegerType())
691 return LOK_Integer;
692 if (ParamTy->isFloatingType())
693 return LOK_Floating;
694
695 llvm_unreachable("unknown kind of literal operator");
696}
697
698Expr *UserDefinedLiteral::getCookedLiteral() {
699#ifndef NDEBUG
700 LiteralOperatorKind LOK = getLiteralOperatorKind();
701 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
702#endif
703 return getArg(0);
704}
705
706const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
707 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
708}
John McCallcf142162010-08-07 06:22:56 +0000709
Craig Toppera31a8822013-08-22 07:09:37 +0000710CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000711 FieldDecl *Field, QualType T)
712 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
713 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
714 ? VK_XValue
715 : VK_RValue,
716 /*FIXME*/ OK_Ordinary, false, false, false, false),
717 Field(Field), Loc(Loc) {
718 assert(Field->hasInClassInitializer());
719}
720
Craig Toppera31a8822013-08-22 07:09:37 +0000721CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000722 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000723 return new (C) CXXTemporary(Destructor);
724}
725
Craig Toppera31a8822013-08-22 07:09:37 +0000726CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000727 CXXTemporary *Temp,
728 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000729 assert((SubExpr->getType()->isRecordType() ||
730 SubExpr->getType()->isArrayType()) &&
731 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000732
Douglas Gregora6e053e2010-12-15 01:34:56 +0000733 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000734}
735
Craig Toppera31a8822013-08-22 07:09:37 +0000736CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000737 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000738 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000739 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000740 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000741 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000742 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000743 bool StdInitListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000744 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000745 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
746 Type->getType().getNonReferenceType(),
747 Type->getTypeLoc().getBeginLoc(),
Richard Smithc83bf822016-06-10 00:58:19 +0000748 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000749 HadMultipleCandidates,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000750 ListInitialization,
751 StdInitListInitialization,
752 ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000753 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000754 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000755}
756
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000757SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
758 return Type->getTypeLoc().getBeginLoc();
759}
760
761SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000762 SourceLocation Loc = getParenOrBraceRange().getEnd();
763 if (Loc.isInvalid() && getNumArgs())
764 Loc = getArg(getNumArgs()-1)->getLocEnd();
765 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000766}
Anders Carlsson6f287832009-04-21 02:22:11 +0000767
Craig Toppera31a8822013-08-22 07:09:37 +0000768CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000769 SourceLocation Loc,
Richard Smithc2bebe92016-05-11 20:37:46 +0000770 CXXConstructorDecl *Ctor,
771 bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000772 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000773 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000774 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000775 bool StdInitListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000776 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000777 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000778 SourceRange ParenOrBraceRange) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000779 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000780 Ctor, Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000781 HadMultipleCandidates, ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000782 StdInitListInitialization,
Sebastian Redla9351792012-02-11 23:51:47 +0000783 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000784 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000785}
786
Craig Toppera31a8822013-08-22 07:09:37 +0000787CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
788 QualType T, SourceLocation Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000789 CXXConstructorDecl *Ctor,
Richard Smithc2bebe92016-05-11 20:37:46 +0000790 bool Elidable,
791 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000792 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000793 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000794 bool StdInitListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000795 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000796 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000797 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000798 : Expr(SC, T, VK_RValue, OK_Ordinary,
799 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000800 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000801 T->containsUnexpandedParameterPack()),
Richard Smithc2bebe92016-05-11 20:37:46 +0000802 Constructor(Ctor), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
803 NumArgs(Args.size()),
804 Elidable(Elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000805 ListInitialization(ListInitialization),
Richard Smithf8adcdc2014-07-17 05:12:35 +0000806 StdInitListInitialization(StdInitListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000807 ZeroInitialization(ZeroInitialization),
Craig Topper36250ad2014-05-12 05:36:57 +0000808 ConstructKind(ConstructKind), Args(nullptr)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000809{
810 if (NumArgs) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000811 this->Args = new (C) Stmt*[Args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000812
Richard Smithc2bebe92016-05-11 20:37:46 +0000813 for (unsigned i = 0; i != Args.size(); ++i) {
814 assert(Args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000815
Richard Smithc2bebe92016-05-11 20:37:46 +0000816 if (Args[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000817 ExprBits.ValueDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000818 if (Args[i]->isInstantiationDependent())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000819 ExprBits.InstantiationDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000820 if (Args[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000821 ExprBits.ContainsUnexpandedParameterPack = true;
822
Richard Smithc2bebe92016-05-11 20:37:46 +0000823 this->Args[i] = Args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000824 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000825 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000826}
827
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000828LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000829 LambdaCaptureKind Kind, VarDecl *Var,
830 SourceLocation EllipsisLoc)
John Brawn771721c2016-06-15 14:14:51 +0000831 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000832{
833 unsigned Bits = 0;
834 if (Implicit)
835 Bits |= Capture_Implicit;
836
837 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +0000838 case LCK_StarThis:
839 Bits |= Capture_ByCopy;
840 // Fall through
Craig Topper36250ad2014-05-12 05:36:57 +0000841 case LCK_This:
842 assert(!Var && "'this' capture cannot have a variable!");
John Brawn771721c2016-06-15 14:14:51 +0000843 Bits |= Capture_This;
Douglas Gregore31e6062012-02-07 10:09:13 +0000844 break;
845
846 case LCK_ByCopy:
847 Bits |= Capture_ByCopy;
848 // Fall through
849 case LCK_ByRef:
850 assert(Var && "capture must have a variable!");
851 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000852 case LCK_VLAType:
853 assert(!Var && "VLA type capture cannot have a variable!");
Alexey Bataev39c81e22014-08-28 04:28:19 +0000854 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000855 }
John Brawn771721c2016-06-15 14:14:51 +0000856 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000857}
858
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000859LambdaCaptureKind LambdaCapture::getCaptureKind() const {
John Brawn771721c2016-06-15 14:14:51 +0000860 if (capturesVLAType())
Faisal Validc6b5962016-03-21 09:25:37 +0000861 return LCK_VLAType;
John Brawn771721c2016-06-15 14:14:51 +0000862 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
863 if (capturesThis())
Faisal Validc6b5962016-03-21 09:25:37 +0000864 return CapByCopy ? LCK_StarThis : LCK_This;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000865 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000866}
867
James Y Knighte00a67e2015-12-31 04:18:25 +0000868LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
Douglas Gregore31e6062012-02-07 10:09:13 +0000869 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000870 SourceLocation CaptureDefaultLoc,
James Y Knighte00a67e2015-12-31 04:18:25 +0000871 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
872 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000873 ArrayRef<VarDecl *> ArrayIndexVars,
874 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000875 SourceLocation ClosingBrace,
876 bool ContainsUnexpandedParameterPack)
James Y Knighte00a67e2015-12-31 04:18:25 +0000877 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
878 T->isDependentType(), T->isDependentType(),
879 ContainsUnexpandedParameterPack),
880 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
881 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
882 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
883 ClosingBrace(ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000884 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000885 CXXRecordDecl *Class = getLambdaClass();
886 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000887
888 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000889
890 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000891 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000892 Data.NumCaptures = NumCaptures;
893 Data.NumExplicitCaptures = 0;
James Y Knighte00a67e2015-12-31 04:18:25 +0000894 Data.Captures =
895 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
896 LambdaCapture *ToCapture = Data.Captures;
Douglas Gregore5561632012-02-13 17:20:40 +0000897 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
898 if (Captures[I].isExplicit())
899 ++Data.NumExplicitCaptures;
900
901 *ToCapture++ = Captures[I];
902 }
903
904 // Copy initialization expressions for the non-static data members.
905 Stmt **Stored = getStoredStmts();
906 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
907 *Stored++ = CaptureInits[I];
908
909 // Copy the body of the lambda.
910 *Stored++ = getCallOperator()->getBody();
911
912 // Copy the array index variables, if any.
913 HasArrayIndexVars = !ArrayIndexVars.empty();
914 if (HasArrayIndexVars) {
915 assert(ArrayIndexStarts.size() == NumCaptures);
916 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
917 sizeof(VarDecl *) * ArrayIndexVars.size());
918 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
919 sizeof(unsigned) * Captures.size());
920 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000921 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000922}
923
James Y Knighte00a67e2015-12-31 04:18:25 +0000924LambdaExpr *LambdaExpr::Create(
925 const ASTContext &Context, CXXRecordDecl *Class,
926 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
927 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
928 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
929 ArrayRef<VarDecl *> ArrayIndexVars, ArrayRef<unsigned> ArrayIndexStarts,
930 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000931 // Determine the type of the expression (i.e., the type of the
932 // function object we're creating).
933 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000934
James Y Knighte00a67e2015-12-31 04:18:25 +0000935 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
936 Captures.size() + 1, ArrayIndexVars.empty() ? 0 : Captures.size() + 1,
937 ArrayIndexVars.size());
Douglas Gregore5561632012-02-13 17:20:40 +0000938 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +0000939 return new (Mem) LambdaExpr(T, IntroducerRange,
940 CaptureDefault, CaptureDefaultLoc, Captures,
941 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000942 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000943 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +0000944}
945
Craig Toppera31a8822013-08-22 07:09:37 +0000946LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
947 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +0000948 unsigned NumArrayIndexVars) {
James Y Knighte00a67e2015-12-31 04:18:25 +0000949 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
950 NumCaptures + 1, NumArrayIndexVars ? NumCaptures + 1 : 0,
951 NumArrayIndexVars);
Douglas Gregor99ae8062012-02-14 17:54:36 +0000952 void *Mem = C.Allocate(Size);
953 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
954}
955
James Dennettdd2ffea22015-05-07 18:48:18 +0000956bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
957 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
958 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
959}
960
Douglas Gregorc8a73492012-02-13 15:44:47 +0000961LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000962 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000963}
964
965LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000966 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000967}
968
James Dennett1575cb42014-05-27 19:13:04 +0000969LambdaExpr::capture_range LambdaExpr::captures() const {
970 return capture_range(capture_begin(), capture_end());
971}
972
Douglas Gregorc8a73492012-02-13 15:44:47 +0000973LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
974 return capture_begin();
975}
976
977LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
978 struct CXXRecordDecl::LambdaDefinitionData &Data
979 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000980 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000981}
982
James Dennett1575cb42014-05-27 19:13:04 +0000983LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
984 return capture_range(explicit_capture_begin(), explicit_capture_end());
985}
986
Douglas Gregorc8a73492012-02-13 15:44:47 +0000987LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
988 return explicit_capture_end();
989}
990
991LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
992 return capture_end();
993}
994
James Dennett1575cb42014-05-27 19:13:04 +0000995LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
996 return capture_range(implicit_capture_begin(), implicit_capture_end());
997}
998
James Y Knight53c76162015-07-17 18:21:37 +0000999ArrayRef<VarDecl *>
1000LambdaExpr::getCaptureInitIndexVars(const_capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001001 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001002
1003 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001004 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1005 "Capture index out-of-range");
James Y Knight53c76162015-07-17 18:21:37 +00001006 VarDecl *const *IndexVars = getArrayIndexVars();
1007 const unsigned *IndexStarts = getArrayIndexStarts();
Craig Topper5fc8fc22014-08-27 06:28:36 +00001008 return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
1009 IndexVars + IndexStarts[Index + 1]);
Douglas Gregor54fcea62012-02-13 16:35:30 +00001010}
1011
Douglas Gregore31e6062012-02-07 10:09:13 +00001012CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1013 return getType()->getAsCXXRecordDecl();
1014}
1015
1016CXXMethodDecl *LambdaExpr::getCallOperator() const {
1017 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001018 return Record->getLambdaCallOperator();
1019}
1020
1021TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1022 CXXRecordDecl *Record = getLambdaClass();
1023 return Record->getGenericLambdaTemplateParameterList();
1024
Douglas Gregore31e6062012-02-07 10:09:13 +00001025}
1026
Douglas Gregor99ae8062012-02-14 17:54:36 +00001027CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +00001028 // FIXME: this mutation in getBody is bogus. It should be
1029 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1030 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +00001031 if (!getStoredStmts()[NumCaptures])
James Y Knight53c76162015-07-17 18:21:37 +00001032 *const_cast<clang::Stmt **>(&getStoredStmts()[NumCaptures]) =
1033 getCallOperator()->getBody();
1034
James Y Knighte00a67e2015-12-31 04:18:25 +00001035 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001036}
1037
Douglas Gregore31e6062012-02-07 10:09:13 +00001038bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001039 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001040}
1041
John McCall28fc7092011-11-10 05:35:25 +00001042ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001043 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001044 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001045 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001046 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001047 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001048 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001049 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001050 SubExpr(subexpr) {
Tim Shen4a05bb82016-06-21 20:29:17 +00001051 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
John McCall28fc7092011-11-10 05:35:25 +00001052 ExprWithCleanupsBits.NumObjects = objects.size();
1053 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001054 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001055}
1056
Craig Toppera31a8822013-08-22 07:09:37 +00001057ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001058 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001059 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001060 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
1061 llvm::alignOf<ExprWithCleanups>());
Tim Shen4a05bb82016-06-21 20:29:17 +00001062 return new (buffer)
1063 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001064}
1065
John McCall28fc7092011-11-10 05:35:25 +00001066ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1067 : Expr(ExprWithCleanupsClass, empty) {
1068 ExprWithCleanupsBits.NumObjects = numObjects;
1069}
Chris Lattnercba86142010-05-10 00:25:06 +00001070
Craig Toppera31a8822013-08-22 07:09:37 +00001071ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1072 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001073 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001074 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
1075 llvm::alignOf<ExprWithCleanups>());
John McCall28fc7092011-11-10 05:35:25 +00001076 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001077}
1078
Douglas Gregor2b88c112010-09-08 00:15:04 +00001079CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001080 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001081 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001082 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001083 : Expr(CXXUnresolvedConstructExprClass,
1084 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001085 (Type->getType()->isLValueReferenceType() ? VK_LValue
1086 :Type->getType()->isRValueReferenceType()? VK_XValue
1087 :VK_RValue),
1088 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001089 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001090 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001091 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001092 LParenLoc(LParenLoc),
1093 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001094 NumArgs(Args.size()) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001095 Expr **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001096 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001097 if (Args[I]->containsUnexpandedParameterPack())
1098 ExprBits.ContainsUnexpandedParameterPack = true;
1099
1100 StoredArgs[I] = Args[I];
1101 }
Douglas Gregorce934142009-05-20 18:46:25 +00001102}
1103
1104CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001105CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001106 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001107 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001108 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001109 SourceLocation RParenLoc) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001110 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
Benjamin Kramerc215e762012-08-24 11:54:20 +00001111 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001112}
1113
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001114CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001115CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001116 Stmt::EmptyShell Empty;
James Y Knighte00a67e2015-12-31 04:18:25 +00001117 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001118 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1119}
1120
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001121SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1122 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001123}
1124
James Y Knighte7d82282015-12-29 18:15:14 +00001125CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1126 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1127 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1128 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1129 DeclarationNameInfo MemberNameInfo,
1130 const TemplateArgumentListInfo *TemplateArgs)
1131 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1132 OK_Ordinary, true, true, true,
1133 ((Base && Base->containsUnexpandedParameterPack()) ||
1134 (QualifierLoc &&
1135 QualifierLoc.getNestedNameSpecifier()
1136 ->containsUnexpandedParameterPack()) ||
1137 MemberNameInfo.containsUnexpandedParameterPack())),
1138 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1139 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1140 TemplateKWLoc.isValid()),
1141 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1142 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1143 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001144 if (TemplateArgs) {
1145 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001146 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001147 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001148 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1149 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1150 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001151 if (ContainsUnexpandedParameterPack)
1152 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001153 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001154 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1155 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001156 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001157}
1158
John McCall8cd78132009-11-19 22:55:06 +00001159CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001160CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001161 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001162 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001163 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001164 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001165 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001166 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001167 const TemplateArgumentListInfo *TemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001168 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001169 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +00001170 std::size_t Size =
1171 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1172 HasTemplateKWAndArgsInfo, NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001173
James Y Knighte7d82282015-12-29 18:15:14 +00001174 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001175 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1176 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001177 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001178 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001179 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001180 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001181}
1182
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001183CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001184CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001185 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001186 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001187 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1188 std::size_t Size =
1189 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1190 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1191 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001192 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001193 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001194 0, SourceLocation(),
1195 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001196 SourceLocation(), nullptr,
1197 DeclarationNameInfo(), nullptr);
James Y Knighte7d82282015-12-29 18:15:14 +00001198 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001199 return E;
1200}
1201
Douglas Gregor0da1d432011-02-28 20:01:57 +00001202bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001203 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001204 return true;
1205
Douglas Gregor25b7e052011-03-02 21:06:53 +00001206 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001207}
1208
John McCall0009fcc2011-04-26 20:42:42 +00001209static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1210 UnresolvedSetIterator end) {
1211 do {
1212 NamedDecl *decl = *begin;
1213 if (isa<UnresolvedUsingValueDecl>(decl))
1214 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001215
1216 // Unresolved member expressions should only contain methods and
1217 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001218 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1219 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001220 return false;
1221 } while (++begin != end);
1222
1223 return true;
1224}
1225
Craig Toppera31a8822013-08-22 07:09:37 +00001226UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001227 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001228 Expr *Base, QualType BaseType,
1229 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001230 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001231 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001232 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001233 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001234 const TemplateArgumentListInfo *TemplateArgs,
1235 UnresolvedSetIterator Begin,
1236 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001237 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1238 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001239 // Dependent
1240 ((Base && Base->isTypeDependent()) ||
1241 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001242 ((Base && Base->isInstantiationDependent()) ||
1243 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001244 // Contains unexpanded parameter pack
1245 ((Base && Base->containsUnexpandedParameterPack()) ||
1246 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001247 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1248 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001249
1250 // Check whether all of the members are non-static member functions,
1251 // and if so, mark give this bound-member type instead of overload type.
1252 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1253 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001254}
1255
Douglas Gregor0da1d432011-02-28 20:01:57 +00001256bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001257 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001258 return true;
1259
Douglas Gregor25b7e052011-03-02 21:06:53 +00001260 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001261}
1262
James Y Knighte7d82282015-12-29 18:15:14 +00001263UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1264 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1265 bool IsArrow, SourceLocation OperatorLoc,
1266 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1267 const DeclarationNameInfo &MemberNameInfo,
1268 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1269 UnresolvedSetIterator End) {
1270 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1271 std::size_t Size =
1272 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1273 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
John McCall10eae182009-11-30 22:42:35 +00001274
James Y Knighte7d82282015-12-29 18:15:14 +00001275 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
1276 return new (Mem) UnresolvedMemberExpr(
1277 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1278 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001279}
1280
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001281UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001282UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1283 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001284 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001285 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1286 std::size_t Size =
1287 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1288 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001289
James Y Knighte7d82282015-12-29 18:15:14 +00001290 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001291 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001292 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001293 return E;
1294}
1295
John McCall58cc69d2010-01-27 01:50:18 +00001296CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1297 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1298
1299 // If there was a nested name specifier, it names the naming class.
1300 // It can't be dependent: after all, we were actually able to do the
1301 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001302 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001303 auto *NNS = getQualifier();
1304 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001305 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001306 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001307 Record = T->getAsCXXRecordDecl();
1308 assert(Record && "qualifier in member expression does not name record");
1309 }
John McCall58cc69d2010-01-27 01:50:18 +00001310 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001311 else {
John McCall58cc69d2010-01-27 01:50:18 +00001312 QualType BaseType = getBaseType().getNonReferenceType();
1313 if (isArrow()) {
1314 const PointerType *PT = BaseType->getAs<PointerType>();
1315 assert(PT && "base of arrow member access is not pointer");
1316 BaseType = PT->getPointeeType();
1317 }
1318
Douglas Gregor9262f472010-04-27 18:19:34 +00001319 Record = BaseType->getAsCXXRecordDecl();
1320 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001321 }
1322
Douglas Gregor9262f472010-04-27 18:19:34 +00001323 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001324}
1325
Richard Smithd784e682015-09-23 21:41:42 +00001326SizeOfPackExpr *
1327SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1328 NamedDecl *Pack, SourceLocation PackLoc,
1329 SourceLocation RParenLoc,
1330 Optional<unsigned> Length,
1331 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001332 void *Storage =
1333 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001334 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1335 PackLoc, RParenLoc, Length, PartialArgs);
1336}
1337
1338SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1339 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001340 void *Storage =
1341 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001342 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1343}
1344
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001345SubstNonTypeTemplateParmPackExpr::
1346SubstNonTypeTemplateParmPackExpr(QualType T,
1347 NonTypeTemplateParmDecl *Param,
1348 SourceLocation NameLoc,
1349 const TemplateArgument &ArgPack)
1350 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001351 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001352 Param(Param), Arguments(ArgPack.pack_begin()),
1353 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1354
1355TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001356 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001357}
1358
Richard Smithb15fe3a2012-09-12 00:56:43 +00001359FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1360 SourceLocation NameLoc,
1361 unsigned NumParams,
James Y Knight48fefa32015-09-30 14:04:23 +00001362 ParmVarDecl *const *Params)
1363 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1364 true, true),
1365 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001366 if (Params)
1367 std::uninitialized_copy(Params, Params + NumParams,
James Y Knighte00a67e2015-12-31 04:18:25 +00001368 getTrailingObjects<ParmVarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001369}
1370
1371FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001372FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001373 ParmVarDecl *ParamPack, SourceLocation NameLoc,
James Y Knight48fefa32015-09-30 14:04:23 +00001374 ArrayRef<ParmVarDecl *> Params) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001375 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1376 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001377}
1378
1379FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001380FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1381 unsigned NumParams) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001382 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1383 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001384}
1385
David Majnemerdaff3702014-05-01 17:50:17 +00001386void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1387 unsigned ManglingNumber) {
1388 // We only need extra state if we have to remember more than just the Stmt.
1389 if (!ExtendedBy)
1390 return;
1391
1392 // We may need to allocate extra storage for the mangling number and the
1393 // extended-by ValueDecl.
1394 if (!State.is<ExtraState *>()) {
1395 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1396 ES->Temporary = State.get<Stmt *>();
1397 State = ES;
1398 }
1399
1400 auto ES = State.get<ExtraState *>();
1401 ES->ExtendingDecl = ExtendedBy;
1402 ES->ManglingNumber = ManglingNumber;
1403}
1404
Douglas Gregor29c42f22012-02-24 07:38:34 +00001405TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1406 ArrayRef<TypeSourceInfo *> Args,
1407 SourceLocation RParenLoc,
1408 bool Value)
1409 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1410 /*TypeDependent=*/false,
1411 /*ValueDependent=*/false,
1412 /*InstantiationDependent=*/false,
1413 /*ContainsUnexpandedParameterPack=*/false),
1414 Loc(Loc), RParenLoc(RParenLoc)
1415{
1416 TypeTraitExprBits.Kind = Kind;
1417 TypeTraitExprBits.Value = Value;
1418 TypeTraitExprBits.NumArgs = Args.size();
1419
James Y Knighte00a67e2015-12-31 04:18:25 +00001420 TypeSourceInfo **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1421
Douglas Gregor29c42f22012-02-24 07:38:34 +00001422 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1423 if (Args[I]->getType()->isDependentType())
1424 setValueDependent(true);
1425 if (Args[I]->getType()->isInstantiationDependentType())
1426 setInstantiationDependent(true);
1427 if (Args[I]->getType()->containsUnexpandedParameterPack())
1428 setContainsUnexpandedParameterPack(true);
1429
1430 ToArgs[I] = Args[I];
1431 }
1432}
1433
Craig Toppera31a8822013-08-22 07:09:37 +00001434TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001435 SourceLocation Loc,
1436 TypeTrait Kind,
1437 ArrayRef<TypeSourceInfo *> Args,
1438 SourceLocation RParenLoc,
1439 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001440 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001441 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1442}
1443
Craig Toppera31a8822013-08-22 07:09:37 +00001444TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001445 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001446 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001447 return new (Mem) TypeTraitExpr(EmptyShell());
1448}
1449
David Blaikie68e081d2011-12-20 02:48:34 +00001450void ArrayTypeTraitExpr::anchor() { }