blob: 6713fca04571db5ba05acfcc2751747cc5489801 [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,
Richard Smith60437622017-02-09 19:17:44 +0000737 QualType Type,
738 TypeSourceInfo *TSI,
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)
Richard Smith60437622017-02-09 19:17:44 +0000745 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, Type,
746 TSI->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),
Richard Smith60437622017-02-09 19:17:44 +0000753 Type(TSI) {
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,
Richard Smith2589b9802012-07-25 03:56:55 +0000872 SourceLocation ClosingBrace,
873 bool ContainsUnexpandedParameterPack)
James Y Knighte00a67e2015-12-31 04:18:25 +0000874 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
875 T->isDependentType(), T->isDependentType(),
876 ContainsUnexpandedParameterPack),
877 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
878 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
879 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
880 ClosingBrace(ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000881 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000882 CXXRecordDecl *Class = getLambdaClass();
883 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000884
885 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000886
887 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000888 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000889 Data.NumCaptures = NumCaptures;
890 Data.NumExplicitCaptures = 0;
James Y Knighte00a67e2015-12-31 04:18:25 +0000891 Data.Captures =
892 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
893 LambdaCapture *ToCapture = Data.Captures;
Douglas Gregore5561632012-02-13 17:20:40 +0000894 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
895 if (Captures[I].isExplicit())
896 ++Data.NumExplicitCaptures;
897
898 *ToCapture++ = Captures[I];
899 }
900
901 // Copy initialization expressions for the non-static data members.
902 Stmt **Stored = getStoredStmts();
903 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
904 *Stored++ = CaptureInits[I];
905
906 // Copy the body of the lambda.
907 *Stored++ = getCallOperator()->getBody();
Douglas Gregore31e6062012-02-07 10:09:13 +0000908}
909
James Y Knighte00a67e2015-12-31 04:18:25 +0000910LambdaExpr *LambdaExpr::Create(
911 const ASTContext &Context, CXXRecordDecl *Class,
912 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
913 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
914 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
James Y Knighte00a67e2015-12-31 04:18:25 +0000915 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000916 // Determine the type of the expression (i.e., the type of the
917 // function object we're creating).
918 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000919
Richard Smith30e304e2016-12-14 00:03:17 +0000920 unsigned Size = totalSizeToAlloc<Stmt *>(Captures.size() + 1);
Douglas Gregore5561632012-02-13 17:20:40 +0000921 void *Mem = Context.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +0000922 return new (Mem)
923 LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
924 Captures, ExplicitParams, ExplicitResultType, CaptureInits,
925 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +0000926}
927
Craig Toppera31a8822013-08-22 07:09:37 +0000928LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
Richard Smith30e304e2016-12-14 00:03:17 +0000929 unsigned NumCaptures) {
930 unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
Douglas Gregor99ae8062012-02-14 17:54:36 +0000931 void *Mem = C.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +0000932 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +0000933}
934
James Dennettdd2ffea22015-05-07 18:48:18 +0000935bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
936 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
937 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
938}
939
Douglas Gregorc8a73492012-02-13 15:44:47 +0000940LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000941 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000942}
943
944LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000945 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000946}
947
James Dennett1575cb42014-05-27 19:13:04 +0000948LambdaExpr::capture_range LambdaExpr::captures() const {
949 return capture_range(capture_begin(), capture_end());
950}
951
Douglas Gregorc8a73492012-02-13 15:44:47 +0000952LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
953 return capture_begin();
954}
955
956LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
957 struct CXXRecordDecl::LambdaDefinitionData &Data
958 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000959 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000960}
961
James Dennett1575cb42014-05-27 19:13:04 +0000962LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
963 return capture_range(explicit_capture_begin(), explicit_capture_end());
964}
965
Douglas Gregorc8a73492012-02-13 15:44:47 +0000966LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
967 return explicit_capture_end();
968}
969
970LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
971 return capture_end();
972}
973
James Dennett1575cb42014-05-27 19:13:04 +0000974LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
975 return capture_range(implicit_capture_begin(), implicit_capture_end());
976}
977
Douglas Gregore31e6062012-02-07 10:09:13 +0000978CXXRecordDecl *LambdaExpr::getLambdaClass() const {
979 return getType()->getAsCXXRecordDecl();
980}
981
982CXXMethodDecl *LambdaExpr::getCallOperator() const {
983 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +0000984 return Record->getLambdaCallOperator();
985}
986
987TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
988 CXXRecordDecl *Record = getLambdaClass();
989 return Record->getGenericLambdaTemplateParameterList();
990
Douglas Gregore31e6062012-02-07 10:09:13 +0000991}
992
Douglas Gregor99ae8062012-02-14 17:54:36 +0000993CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +0000994 // FIXME: this mutation in getBody is bogus. It should be
995 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
996 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +0000997 if (!getStoredStmts()[NumCaptures])
James Y Knight53c76162015-07-17 18:21:37 +0000998 *const_cast<clang::Stmt **>(&getStoredStmts()[NumCaptures]) =
999 getCallOperator()->getBody();
1000
James Y Knighte00a67e2015-12-31 04:18:25 +00001001 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001002}
1003
Douglas Gregore31e6062012-02-07 10:09:13 +00001004bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001005 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001006}
1007
John McCall28fc7092011-11-10 05:35:25 +00001008ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001009 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001010 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001011 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001012 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001013 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001014 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001015 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001016 SubExpr(subexpr) {
Tim Shen4a05bb82016-06-21 20:29:17 +00001017 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
John McCall28fc7092011-11-10 05:35:25 +00001018 ExprWithCleanupsBits.NumObjects = objects.size();
1019 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001020 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001021}
1022
Craig Toppera31a8822013-08-22 07:09:37 +00001023ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001024 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001025 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001026 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001027 alignof(ExprWithCleanups));
Tim Shen4a05bb82016-06-21 20:29:17 +00001028 return new (buffer)
1029 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001030}
1031
John McCall28fc7092011-11-10 05:35:25 +00001032ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1033 : Expr(ExprWithCleanupsClass, empty) {
1034 ExprWithCleanupsBits.NumObjects = numObjects;
1035}
Chris Lattnercba86142010-05-10 00:25:06 +00001036
Craig Toppera31a8822013-08-22 07:09:37 +00001037ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1038 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001039 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001040 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001041 alignof(ExprWithCleanups));
John McCall28fc7092011-11-10 05:35:25 +00001042 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001043}
1044
Douglas Gregor2b88c112010-09-08 00:15:04 +00001045CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001046 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001047 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001048 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001049 : Expr(CXXUnresolvedConstructExprClass,
1050 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001051 (Type->getType()->isLValueReferenceType() ? VK_LValue
1052 :Type->getType()->isRValueReferenceType()? VK_XValue
1053 :VK_RValue),
1054 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001055 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001056 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001057 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001058 LParenLoc(LParenLoc),
1059 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001060 NumArgs(Args.size()) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001061 Expr **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001062 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001063 if (Args[I]->containsUnexpandedParameterPack())
1064 ExprBits.ContainsUnexpandedParameterPack = true;
1065
1066 StoredArgs[I] = Args[I];
1067 }
Douglas Gregorce934142009-05-20 18:46:25 +00001068}
1069
1070CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001071CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001072 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001073 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001074 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001075 SourceLocation RParenLoc) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001076 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
Benjamin Kramerc215e762012-08-24 11:54:20 +00001077 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001078}
1079
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001080CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001081CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001082 Stmt::EmptyShell Empty;
James Y Knighte00a67e2015-12-31 04:18:25 +00001083 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001084 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1085}
1086
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001087SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1088 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001089}
1090
James Y Knighte7d82282015-12-29 18:15:14 +00001091CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1092 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1093 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1094 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1095 DeclarationNameInfo MemberNameInfo,
1096 const TemplateArgumentListInfo *TemplateArgs)
1097 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1098 OK_Ordinary, true, true, true,
1099 ((Base && Base->containsUnexpandedParameterPack()) ||
1100 (QualifierLoc &&
1101 QualifierLoc.getNestedNameSpecifier()
1102 ->containsUnexpandedParameterPack()) ||
1103 MemberNameInfo.containsUnexpandedParameterPack())),
1104 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1105 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1106 TemplateKWLoc.isValid()),
1107 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1108 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1109 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001110 if (TemplateArgs) {
1111 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001112 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001113 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001114 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1115 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1116 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001117 if (ContainsUnexpandedParameterPack)
1118 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001119 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001120 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1121 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001122 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001123}
1124
John McCall8cd78132009-11-19 22:55:06 +00001125CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001126CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001127 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001128 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001129 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001130 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001131 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001132 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001133 const TemplateArgumentListInfo *TemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001134 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001135 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +00001136 std::size_t Size =
1137 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1138 HasTemplateKWAndArgsInfo, NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001139
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001140 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
John McCall2d74de92009-12-01 22:10:20 +00001141 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1142 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001143 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001144 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001145 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001146 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001147}
1148
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001149CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001150CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001151 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001152 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001153 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1154 std::size_t Size =
1155 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1156 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001157 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001158 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001159 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001160 0, SourceLocation(),
1161 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001162 SourceLocation(), nullptr,
1163 DeclarationNameInfo(), nullptr);
James Y Knighte7d82282015-12-29 18:15:14 +00001164 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001165 return E;
1166}
1167
Douglas Gregor0da1d432011-02-28 20:01:57 +00001168bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001169 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001170 return true;
1171
Douglas Gregor25b7e052011-03-02 21:06:53 +00001172 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001173}
1174
John McCall0009fcc2011-04-26 20:42:42 +00001175static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1176 UnresolvedSetIterator end) {
1177 do {
1178 NamedDecl *decl = *begin;
1179 if (isa<UnresolvedUsingValueDecl>(decl))
1180 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001181
1182 // Unresolved member expressions should only contain methods and
1183 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001184 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1185 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001186 return false;
1187 } while (++begin != end);
1188
1189 return true;
1190}
1191
Craig Toppera31a8822013-08-22 07:09:37 +00001192UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001193 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001194 Expr *Base, QualType BaseType,
1195 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001196 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001197 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001198 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001199 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001200 const TemplateArgumentListInfo *TemplateArgs,
1201 UnresolvedSetIterator Begin,
1202 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001203 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1204 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001205 // Dependent
1206 ((Base && Base->isTypeDependent()) ||
1207 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001208 ((Base && Base->isInstantiationDependent()) ||
1209 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001210 // Contains unexpanded parameter pack
1211 ((Base && Base->containsUnexpandedParameterPack()) ||
1212 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001213 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1214 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001215
1216 // Check whether all of the members are non-static member functions,
1217 // and if so, mark give this bound-member type instead of overload type.
1218 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1219 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001220}
1221
Douglas Gregor0da1d432011-02-28 20:01:57 +00001222bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001223 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001224 return true;
1225
Douglas Gregor25b7e052011-03-02 21:06:53 +00001226 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001227}
1228
James Y Knighte7d82282015-12-29 18:15:14 +00001229UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1230 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1231 bool IsArrow, SourceLocation OperatorLoc,
1232 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1233 const DeclarationNameInfo &MemberNameInfo,
1234 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1235 UnresolvedSetIterator End) {
1236 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1237 std::size_t Size =
1238 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1239 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
John McCall10eae182009-11-30 22:42:35 +00001240
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001241 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
James Y Knighte7d82282015-12-29 18:15:14 +00001242 return new (Mem) UnresolvedMemberExpr(
1243 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1244 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001245}
1246
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001247UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001248UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1249 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001250 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001251 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1252 std::size_t Size =
1253 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1254 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001255
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001256 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001257 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001258 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001259 return E;
1260}
1261
John McCall58cc69d2010-01-27 01:50:18 +00001262CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1263 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1264
1265 // If there was a nested name specifier, it names the naming class.
1266 // It can't be dependent: after all, we were actually able to do the
1267 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001268 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001269 auto *NNS = getQualifier();
1270 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001271 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001272 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001273 Record = T->getAsCXXRecordDecl();
1274 assert(Record && "qualifier in member expression does not name record");
1275 }
John McCall58cc69d2010-01-27 01:50:18 +00001276 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001277 else {
John McCall58cc69d2010-01-27 01:50:18 +00001278 QualType BaseType = getBaseType().getNonReferenceType();
1279 if (isArrow()) {
1280 const PointerType *PT = BaseType->getAs<PointerType>();
1281 assert(PT && "base of arrow member access is not pointer");
1282 BaseType = PT->getPointeeType();
1283 }
1284
Douglas Gregor9262f472010-04-27 18:19:34 +00001285 Record = BaseType->getAsCXXRecordDecl();
1286 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001287 }
1288
Douglas Gregor9262f472010-04-27 18:19:34 +00001289 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001290}
1291
Richard Smithd784e682015-09-23 21:41:42 +00001292SizeOfPackExpr *
1293SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1294 NamedDecl *Pack, SourceLocation PackLoc,
1295 SourceLocation RParenLoc,
1296 Optional<unsigned> Length,
1297 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001298 void *Storage =
1299 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001300 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1301 PackLoc, RParenLoc, Length, PartialArgs);
1302}
1303
1304SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1305 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001306 void *Storage =
1307 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001308 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1309}
1310
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001311SubstNonTypeTemplateParmPackExpr::
1312SubstNonTypeTemplateParmPackExpr(QualType T,
1313 NonTypeTemplateParmDecl *Param,
1314 SourceLocation NameLoc,
1315 const TemplateArgument &ArgPack)
1316 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001317 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001318 Param(Param), Arguments(ArgPack.pack_begin()),
1319 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1320
1321TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001322 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001323}
1324
Richard Smithb15fe3a2012-09-12 00:56:43 +00001325FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1326 SourceLocation NameLoc,
1327 unsigned NumParams,
James Y Knight48fefa32015-09-30 14:04:23 +00001328 ParmVarDecl *const *Params)
1329 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1330 true, true),
1331 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001332 if (Params)
1333 std::uninitialized_copy(Params, Params + NumParams,
James Y Knighte00a67e2015-12-31 04:18:25 +00001334 getTrailingObjects<ParmVarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001335}
1336
1337FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001338FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001339 ParmVarDecl *ParamPack, SourceLocation NameLoc,
James Y Knight48fefa32015-09-30 14:04:23 +00001340 ArrayRef<ParmVarDecl *> Params) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001341 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1342 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001343}
1344
1345FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001346FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1347 unsigned NumParams) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001348 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1349 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001350}
1351
David Majnemerdaff3702014-05-01 17:50:17 +00001352void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1353 unsigned ManglingNumber) {
1354 // We only need extra state if we have to remember more than just the Stmt.
1355 if (!ExtendedBy)
1356 return;
1357
1358 // We may need to allocate extra storage for the mangling number and the
1359 // extended-by ValueDecl.
1360 if (!State.is<ExtraState *>()) {
1361 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1362 ES->Temporary = State.get<Stmt *>();
1363 State = ES;
1364 }
1365
1366 auto ES = State.get<ExtraState *>();
1367 ES->ExtendingDecl = ExtendedBy;
1368 ES->ManglingNumber = ManglingNumber;
1369}
1370
Douglas Gregor29c42f22012-02-24 07:38:34 +00001371TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1372 ArrayRef<TypeSourceInfo *> Args,
1373 SourceLocation RParenLoc,
1374 bool Value)
1375 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1376 /*TypeDependent=*/false,
1377 /*ValueDependent=*/false,
1378 /*InstantiationDependent=*/false,
1379 /*ContainsUnexpandedParameterPack=*/false),
1380 Loc(Loc), RParenLoc(RParenLoc)
1381{
1382 TypeTraitExprBits.Kind = Kind;
1383 TypeTraitExprBits.Value = Value;
1384 TypeTraitExprBits.NumArgs = Args.size();
1385
James Y Knighte00a67e2015-12-31 04:18:25 +00001386 TypeSourceInfo **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1387
Douglas Gregor29c42f22012-02-24 07:38:34 +00001388 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1389 if (Args[I]->getType()->isDependentType())
1390 setValueDependent(true);
1391 if (Args[I]->getType()->isInstantiationDependentType())
1392 setInstantiationDependent(true);
1393 if (Args[I]->getType()->containsUnexpandedParameterPack())
1394 setContainsUnexpandedParameterPack(true);
1395
1396 ToArgs[I] = Args[I];
1397 }
1398}
1399
Craig Toppera31a8822013-08-22 07:09:37 +00001400TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001401 SourceLocation Loc,
1402 TypeTrait Kind,
1403 ArrayRef<TypeSourceInfo *> Args,
1404 SourceLocation RParenLoc,
1405 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001406 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001407 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1408}
1409
Craig Toppera31a8822013-08-22 07:09:37 +00001410TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001411 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001412 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001413 return new (Mem) TypeTraitExpr(EmptyShell());
1414}
1415
David Blaikie68e081d2011-12-20 02:48:34 +00001416void ArrayTypeTraitExpr::anchor() { }