blob: 5ed4ea2ab7692a7dd32e27eb3d6a57a1a9488bb3 [file] [log] [blame]
Eugene Zelenko821f6982017-11-18 01:47:41 +00001//===- ExprCXX.cpp - (C++) Expression AST Node Implementation -------------===//
Ted Kremeneke3a0c142007-08-24 20:21:10 +00002//
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
Eugene Zelenko821f6982017-11-18 01:47:41 +000014#include "clang/AST/ExprCXX.h"
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Eugene Zelenko821f6982017-11-18 01:47:41 +000017#include "clang/AST/Decl.h"
18#include "clang/AST/DeclAccessPair.h"
19#include "clang/AST/DeclBase.h"
Douglas Gregor993603d2008-11-14 16:09:21 +000020#include "clang/AST/DeclCXX.h"
Eugene Zelenko821f6982017-11-18 01:47:41 +000021#include "clang/AST/DeclarationName.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/LambdaCapture.h"
24#include "clang/AST/NestedNameSpecifier.h"
25#include "clang/AST/TemplateBase.h"
26#include "clang/AST/Type.h"
Douglas Gregor651fe5e2010-02-24 23:40:28 +000027#include "clang/AST/TypeLoc.h"
Eugene Zelenko821f6982017-11-18 01:47:41 +000028#include "clang/Basic/LLVM.h"
29#include "clang/Basic/OperatorKinds.h"
30#include "clang/Basic/SourceLocation.h"
31#include "clang/Basic/Specifiers.h"
32#include "llvm/ADT/ArrayRef.h"
33#include "llvm/Support/Casting.h"
34#include "llvm/Support/ErrorHandling.h"
35#include <cassert>
36#include <cstddef>
37#include <cstring>
38#include <memory>
Ted Kremeneke3a0c142007-08-24 20:21:10 +000039
Eugene Zelenko821f6982017-11-18 01:47:41 +000040using namespace clang;
Douglas Gregor9da64192010-04-26 22:37:10 +000041
Ted Kremeneke3a0c142007-08-24 20:21:10 +000042//===----------------------------------------------------------------------===//
43// Child Iterators for iterating over subexpressions/substatements
44//===----------------------------------------------------------------------===//
45
Richard Smith66094432016-10-20 00:55:15 +000046bool CXXOperatorCallExpr::isInfixBinaryOp() const {
47 // An infix binary operator is any operator with two arguments other than
48 // operator() and operator[]. Note that none of these operators can have
49 // default arguments, so it suffices to check the number of argument
50 // expressions.
51 if (getNumArgs() != 2)
52 return false;
53
54 switch (getOperator()) {
55 case OO_Call: case OO_Subscript:
56 return false;
57 default:
58 return true;
59 }
60}
61
Richard Smithef8bf432012-08-13 20:08:14 +000062bool CXXTypeidExpr::isPotentiallyEvaluated() const {
63 if (isTypeOperand())
64 return false;
65
66 // C++11 [expr.typeid]p3:
67 // When typeid is applied to an expression other than a glvalue of
68 // polymorphic class type, [...] the expression is an unevaluated operand.
69 const Expr *E = getExprOperand();
70 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
71 if (RD->isPolymorphic() && E->isGLValue())
72 return true;
73
74 return false;
75}
76
David Majnemer143c55e2013-09-27 07:04:31 +000077QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
Douglas Gregor9da64192010-04-26 22:37:10 +000078 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000079 Qualifiers Quals;
80 return Context.getUnqualifiedArrayType(
81 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Douglas Gregor9da64192010-04-26 22:37:10 +000082}
83
David Majnemer143c55e2013-09-27 07:04:31 +000084QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
Francois Pichet9f4f2072010-09-08 12:20:18 +000085 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000086 Qualifiers Quals;
87 return Context.getUnqualifiedArrayType(
88 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Francois Pichet9f4f2072010-09-08 12:20:18 +000089}
90
Douglas Gregor747eb782010-07-08 06:14:04 +000091// CXXScalarValueInitExpr
Stephen Kelly724e9e52018-08-09 20:05:03 +000092SourceLocation CXXScalarValueInitExpr::getBeginLoc() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +000093 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +000094}
95
Sebastian Redlbd150f42008-11-21 19:14:01 +000096// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +000097CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
98 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Richard Smithb2f0f052016-10-10 18:54:32 +000099 bool PassAlignment, bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000100 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +0000101 SourceRange typeIdParens, Expr *arraySize,
102 InitializationStyle initializationStyle,
103 Expr *initializer, QualType ty,
104 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +0000105 SourceRange Range, SourceRange directInitRange)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000106 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary, ty->isDependentType(),
107 ty->isDependentType(), ty->isInstantiationDependentType(),
108 ty->containsUnexpandedParameterPack()),
109 OperatorNew(operatorNew), OperatorDelete(operatorDelete),
110 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
111 Range(Range), DirectInitRange(directInitRange), GlobalNew(globalNew),
112 PassAlignment(PassAlignment),
113 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Craig Topper36250ad2014-05-12 05:36:57 +0000114 assert((initializer != nullptr || initializationStyle == NoInit) &&
Sebastian Redl6047f072012-02-16 12:22:20 +0000115 "Only NoInit can have no initializer.");
116 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Craig Topper36250ad2014-05-12 05:36:57 +0000117 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
118 initializer != nullptr);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000119 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000120 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000121 if (arraySize->isInstantiationDependent())
122 ExprBits.InstantiationDependent = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000123
Douglas Gregora6e053e2010-12-15 01:34:56 +0000124 if (arraySize->containsUnexpandedParameterPack())
125 ExprBits.ContainsUnexpandedParameterPack = true;
126
Sebastian Redl351bb782008-12-02 14:43:59 +0000127 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000128 }
129
Sebastian Redl6047f072012-02-16 12:22:20 +0000130 if (initializer) {
131 if (initializer->isInstantiationDependent())
132 ExprBits.InstantiationDependent = true;
133
134 if (initializer->containsUnexpandedParameterPack())
135 ExprBits.ContainsUnexpandedParameterPack = true;
136
137 SubExprs[i++] = initializer;
138 }
139
Benjamin Kramerc215e762012-08-24 11:54:20 +0000140 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000141 if (placementArgs[j]->isInstantiationDependent())
142 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000143 if (placementArgs[j]->containsUnexpandedParameterPack())
144 ExprBits.ContainsUnexpandedParameterPack = true;
145
Sebastian Redlbd150f42008-11-21 19:14:01 +0000146 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000147 }
David Blaikie3a0de212012-11-08 22:53:48 +0000148
149 switch (getInitializationStyle()) {
150 case CallInit:
151 this->Range.setEnd(DirectInitRange.getEnd()); break;
152 case ListInit:
153 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000154 default:
155 if (TypeIdParens.isValid())
156 this->Range.setEnd(TypeIdParens.getEnd());
157 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000158 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000159}
160
Craig Toppera31a8822013-08-22 07:09:37 +0000161void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000162 unsigned numPlaceArgs, bool hasInitializer){
Craig Topper36250ad2014-05-12 05:36:57 +0000163 assert(SubExprs == nullptr && "SubExprs already allocated");
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000164 Array = isArray;
165 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000166
167 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000168 SubExprs = new (C) Stmt*[TotalSize];
169}
170
Craig Toppera31a8822013-08-22 07:09:37 +0000171bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
Richard Smitheaf11ad2018-05-03 03:58:32 +0000172 return getOperatorNew()->getType()->castAs<FunctionProtoType>()
173 ->isNothrow() &&
Richard Smith902a0232015-02-14 01:52:20 +0000174 !getOperatorNew()->isReservedGlobalPlacementOperator();
John McCall75f94982011-03-07 03:12:35 +0000175}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000176
Sebastian Redlbd150f42008-11-21 19:14:01 +0000177// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000178QualType CXXDeleteExpr::getDestroyedType() const {
179 const Expr *Arg = getArgument();
Richard Smith5b349582017-10-13 01:55:36 +0000180
181 // For a destroying operator delete, we may have implicitly converted the
182 // pointer type to the type of the parameter of the 'operator delete'
183 // function.
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000184 while (const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
Richard Smith5b349582017-10-13 01:55:36 +0000185 if (ICE->getCastKind() == CK_DerivedToBase ||
186 ICE->getCastKind() == CK_UncheckedDerivedToBase ||
187 ICE->getCastKind() == CK_NoOp) {
188 assert((ICE->getCastKind() == CK_NoOp ||
189 getOperatorDelete()->isDestroyingOperatorDelete()) &&
190 "only a destroying operator delete can have a converted arg");
191 Arg = ICE->getSubExpr();
192 } else
193 break;
194 }
195
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000196 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000197 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000198
199 if (ArgType->isDependentType() && !ArgType->isPointerType())
200 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000201
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000202 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000203}
204
Douglas Gregorad8a3362009-09-04 17:36:40 +0000205// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000206PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000207 : Type(Info) {
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000208 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000209}
210
Craig Toppera31a8822013-08-22 07:09:37 +0000211CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000212 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
Fangrui Song6907ce22018-07-30 19:24:48 +0000213 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
214 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000215 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000216 : Expr(CXXPseudoDestructorExprClass,
David Majnemerced8bdf2015-02-25 17:36:15 +0000217 Context.BoundMemberTy,
John McCalldb40c7f2010-12-14 08:05:40 +0000218 VK_RValue, OK_Ordinary,
219 /*isTypeDependent=*/(Base->isTypeDependent() ||
220 (DestroyedType.getTypeSourceInfo() &&
221 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000222 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000223 (Base->isInstantiationDependent() ||
224 (QualifierLoc &&
225 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
226 (ScopeType &&
227 ScopeType->getType()->isInstantiationDependentType()) ||
228 (DestroyedType.getTypeSourceInfo() &&
229 DestroyedType.getTypeSourceInfo()->getType()
230 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000231 // ContainsUnexpandedParameterPack
232 (Base->containsUnexpandedParameterPack() ||
Fangrui Song6907ce22018-07-30 19:24:48 +0000233 (QualifierLoc &&
Douglas Gregora6ce6082011-02-25 18:19:59 +0000234 QualifierLoc.getNestedNameSpecifier()
235 ->containsUnexpandedParameterPack()) ||
Fangrui Song6907ce22018-07-30 19:24:48 +0000236 (ScopeType &&
Douglas Gregora6e053e2010-12-15 01:34:56 +0000237 ScopeType->getType()->containsUnexpandedParameterPack()) ||
238 (DestroyedType.getTypeSourceInfo() &&
239 DestroyedType.getTypeSourceInfo()->getType()
240 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000241 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000242 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000243 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
Eugene Zelenko821f6982017-11-18 01:47:41 +0000244 DestroyedType(DestroyedType) {}
John McCalldb40c7f2010-12-14 08:05:40 +0000245
Douglas Gregor678f90d2010-02-25 01:56:36 +0000246QualType CXXPseudoDestructorExpr::getDestroyedType() const {
247 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
248 return TInfo->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +0000249
Douglas Gregor678f90d2010-02-25 01:56:36 +0000250 return QualType();
251}
252
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000253SourceLocation CXXPseudoDestructorExpr::getEndLoc() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000254 SourceLocation End = DestroyedType.getLocation();
255 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000256 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000257 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000258}
259
John McCalld14a8642009-11-21 08:51:07 +0000260// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000261UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000262UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000263 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000264 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000265 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000266 const DeclarationNameInfo &NameInfo,
267 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000268 const TemplateArgumentListInfo *Args,
269 UnresolvedSetIterator Begin,
Eugene Zelenko821f6982017-11-18 01:47:41 +0000270 UnresolvedSetIterator End) {
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000271 assert(Args || TemplateKWLoc.isValid());
272 unsigned num_args = Args ? Args->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +0000273
274 std::size_t Size =
275 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(1,
276 num_args);
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000277 void *Mem = C.Allocate(Size, alignof(UnresolvedLookupExpr));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000278 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
279 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000280 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000281 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000282}
283
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000284UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000285UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000286 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000287 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000288 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
289 std::size_t Size =
290 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
291 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000292 void *Mem = C.Allocate(Size, alignof(UnresolvedLookupExpr));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000293 auto *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000294 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000295 return E;
296}
297
Craig Toppera31a8822013-08-22 07:09:37 +0000298OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000299 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000300 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000301 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000302 const TemplateArgumentListInfo *TemplateArgs,
Fangrui Song6907ce22018-07-30 19:24:48 +0000303 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000304 UnresolvedSetIterator End,
305 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000306 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000307 bool KnownContainsUnexpandedParameterPack)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000308 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
309 KnownDependent,
310 (KnownInstantiationDependent ||
311 NameInfo.isInstantiationDependent() ||
312 (QualifierLoc &&
313 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
314 (KnownContainsUnexpandedParameterPack ||
315 NameInfo.containsUnexpandedParameterPack() ||
316 (QualifierLoc &&
317 QualifierLoc.getNestedNameSpecifier()
318 ->containsUnexpandedParameterPack()))),
319 NameInfo(NameInfo), QualifierLoc(QualifierLoc), NumResults(End - Begin),
320 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
321 TemplateKWLoc.isValid()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000322 NumResults = End - Begin;
323 if (NumResults) {
324 // Determine whether this expression is type-dependent.
325 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
326 if ((*I)->getDeclContext()->isDependentContext() ||
327 isa<UnresolvedUsingValueDecl>(*I)) {
328 ExprBits.TypeDependent = true;
329 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000330 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000331 }
332 }
333
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000334 Results = static_cast<DeclAccessPair *>(C.Allocate(
335 sizeof(DeclAccessPair) * NumResults, alignof(DeclAccessPair)));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000336 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregora6e053e2010-12-15 01:34:56 +0000337 }
338
339 // If we have explicit template arguments, check for dependent
340 // template arguments and whether they contain any unexpanded pack
341 // expansions.
342 if (TemplateArgs) {
343 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000344 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000345 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +0000346 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
347 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
348 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000349
350 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000351 ExprBits.TypeDependent = true;
352 ExprBits.ValueDependent = true;
353 }
354 if (InstantiationDependent)
355 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000356 if (ContainsUnexpandedParameterPack)
357 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000358 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000359 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000360 }
361
362 if (isTypeDependent())
363 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000364}
365
Craig Toppera31a8822013-08-22 07:09:37 +0000366void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000367 UnresolvedSetIterator Begin,
368 UnresolvedSetIterator End) {
Craig Topper36250ad2014-05-12 05:36:57 +0000369 assert(!Results && "Results already initialized!");
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000370 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000371 if (NumResults) {
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000372 Results = static_cast<DeclAccessPair *>(
373 C.Allocate(sizeof(DeclAccessPair) * NumResults,
374
375 alignof(DeclAccessPair)));
376 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000377 }
378}
379
John McCall8c12dc42010-04-22 18:44:12 +0000380CXXRecordDecl *OverloadExpr::getNamingClass() const {
381 if (isa<UnresolvedLookupExpr>(this))
382 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
383 else
384 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
385}
386
John McCall8cd78132009-11-19 22:55:06 +0000387// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000388DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000389 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000390 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000391 const DeclarationNameInfo &NameInfo,
392 const TemplateArgumentListInfo *Args)
393 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
394 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000395 (NameInfo.isInstantiationDependent() ||
Fangrui Song6907ce22018-07-30 19:24:48 +0000396 (QualifierLoc &&
Douglas Gregor678d76c2011-07-01 01:22:09 +0000397 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000398 (NameInfo.containsUnexpandedParameterPack() ||
Fangrui Song6907ce22018-07-30 19:24:48 +0000399 (QualifierLoc &&
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000400 QualifierLoc.getNestedNameSpecifier()
401 ->containsUnexpandedParameterPack()))),
Fangrui Song6907ce22018-07-30 19:24:48 +0000402 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Craig Topper36250ad2014-05-12 05:36:57 +0000403 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000404{
405 if (Args) {
406 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000407 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000408 bool ContainsUnexpandedParameterPack
409 = ExprBits.ContainsUnexpandedParameterPack;
James Y Knighte7d82282015-12-29 18:15:14 +0000410 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
411 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
412 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000413 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000414 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000415 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
416 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000417 }
418}
419
John McCalle66edc12009-11-24 19:00:30 +0000420DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000421DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000422 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000423 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000424 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000425 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000426 assert(QualifierLoc && "should be created for dependent qualifiers");
James Y Knighte7d82282015-12-29 18:15:14 +0000427 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
428 std::size_t Size =
429 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
430 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
431 void *Mem = C.Allocate(Size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000432 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
433 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000434}
435
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000436DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000437DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000438 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000439 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000440 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
441 std::size_t Size =
442 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
443 HasTemplateKWAndArgsInfo, NumTemplateArgs);
444 void *Mem = C.Allocate(Size);
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000445 auto *E =
446 new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000447 SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000448 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000449 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000450 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000451}
452
Stephen Kelly724e9e52018-08-09 20:05:03 +0000453SourceLocation CXXConstructExpr::getBeginLoc() const {
John McCall701417a2011-02-21 06:23:05 +0000454 if (isa<CXXTemporaryObjectExpr>(this))
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000455 return cast<CXXTemporaryObjectExpr>(this)->getBeginLoc();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000456 return Loc;
457}
458
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000459SourceLocation CXXConstructExpr::getEndLoc() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000460 if (isa<CXXTemporaryObjectExpr>(this))
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000461 return cast<CXXTemporaryObjectExpr>(this)->getEndLoc();
John McCall701417a2011-02-21 06:23:05 +0000462
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000463 if (ParenOrBraceRange.isValid())
464 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000465
466 SourceLocation End = Loc;
467 for (unsigned I = getNumArgs(); I > 0; --I) {
468 const Expr *Arg = getArg(I-1);
469 if (!Arg->isDefaultArgument()) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000470 SourceLocation NewEnd = Arg->getEndLoc();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000471 if (NewEnd.isValid()) {
472 End = NewEnd;
473 break;
474 }
475 }
476 }
477
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000478 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000479}
480
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000481SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000482 OverloadedOperatorKind Kind = getOperator();
483 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
484 if (getNumArgs() == 1)
485 // Prefix operator
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000486 return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000487 else
488 // Postfix operator
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000489 return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000490 } else if (Kind == OO_Arrow) {
491 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000492 } else if (Kind == OO_Call) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000493 return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000494 } else if (Kind == OO_Subscript) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000495 return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000496 } else if (getNumArgs() == 1) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000497 return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000498 } else if (getNumArgs() == 2) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000499 return SourceRange(getArg(0)->getBeginLoc(), getArg(1)->getEndLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000500 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000501 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000502 }
503}
504
Ted Kremenek98a24e32011-03-30 17:41:19 +0000505Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000506 const Expr *Callee = getCallee()->IgnoreParens();
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000507 if (const auto *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000508 return MemExpr->getBase();
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000509 if (const auto *BO = dyn_cast<BinaryOperator>(Callee))
Jordan Rose16fe35e2012-08-03 23:08:39 +0000510 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
511 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000512
513 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000514 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000515}
516
Ted Kremenek98a24e32011-03-30 17:41:19 +0000517CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000518 if (const auto *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Ted Kremenek98a24e32011-03-30 17:41:19 +0000519 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
520
521 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000522 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000523}
524
David Blaikiec0f58662012-05-03 16:25:49 +0000525CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000526 Expr* ThisArg = getImplicitObjectArgument();
527 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000528 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000529
530 if (ThisArg->getType()->isAnyPointerType())
531 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
532
533 return ThisArg->getType()->getAsCXXRecordDecl();
534}
535
Douglas Gregore200adc2008-10-27 19:41:14 +0000536//===----------------------------------------------------------------------===//
537// Named casts
538//===----------------------------------------------------------------------===//
539
540/// getCastName - Get the name of the C++ cast being used, e.g.,
541/// "static_cast", "dynamic_cast", "reinterpret_cast", or
542/// "const_cast". The returned pointer must not be freed.
543const char *CXXNamedCastExpr::getCastName() const {
544 switch (getStmtClass()) {
545 case CXXStaticCastExprClass: return "static_cast";
546 case CXXDynamicCastExprClass: return "dynamic_cast";
547 case CXXReinterpretCastExprClass: return "reinterpret_cast";
548 case CXXConstCastExprClass: return "const_cast";
549 default: return "<invalid cast>";
550 }
551}
Douglas Gregordd04d332009-01-16 18:33:17 +0000552
Craig Toppera31a8822013-08-22 07:09:37 +0000553CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000554 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000555 CastKind K, Expr *Op,
556 const CXXCastPath *BasePath,
557 TypeSourceInfo *WrittenTy,
Fangrui Song6907ce22018-07-30 19:24:48 +0000558 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000559 SourceLocation RParenLoc,
560 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000561 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Roman Lebedev4aaf1dc2018-08-01 06:06:16 +0000562 void *Buffer =
563 C.Allocate(totalSizeToAlloc<CastExpr::BasePathSizeTy, CXXBaseSpecifier *>(
564 PathSize ? 1 : 0, PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000565 auto *E =
566 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
567 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000568 if (PathSize)
569 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
570 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000571 return E;
572}
573
Craig Toppera31a8822013-08-22 07:09:37 +0000574CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000575 unsigned PathSize) {
Roman Lebedev4aaf1dc2018-08-01 06:06:16 +0000576 void *Buffer =
577 C.Allocate(totalSizeToAlloc<CastExpr::BasePathSizeTy, CXXBaseSpecifier *>(
578 PathSize ? 1 : 0, PathSize));
John McCallcf142162010-08-07 06:22:56 +0000579 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
580}
581
Craig Toppera31a8822013-08-22 07:09:37 +0000582CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000583 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000584 CastKind K, Expr *Op,
585 const CXXCastPath *BasePath,
586 TypeSourceInfo *WrittenTy,
Fangrui Song6907ce22018-07-30 19:24:48 +0000587 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000588 SourceLocation RParenLoc,
589 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000590 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Roman Lebedev4aaf1dc2018-08-01 06:06:16 +0000591 void *Buffer =
592 C.Allocate(totalSizeToAlloc<CastExpr::BasePathSizeTy, CXXBaseSpecifier *>(
593 PathSize ? 1 : 0, PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000594 auto *E =
595 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
596 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000597 if (PathSize)
598 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
599 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000600 return E;
601}
602
Craig Toppera31a8822013-08-22 07:09:37 +0000603CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000604 unsigned PathSize) {
Roman Lebedev4aaf1dc2018-08-01 06:06:16 +0000605 void *Buffer =
606 C.Allocate(totalSizeToAlloc<CastExpr::BasePathSizeTy, CXXBaseSpecifier *>(
607 PathSize ? 1 : 0, PathSize));
John McCallcf142162010-08-07 06:22:56 +0000608 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
609}
610
Anders Carlsson267c0c92011-04-11 01:43:55 +0000611/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
612/// to always be null. For example:
613///
614/// struct A { };
615/// struct B final : A { };
616/// struct C { };
617///
618/// C *f(B* b) { return dynamic_cast<C*>(b); }
619bool CXXDynamicCastExpr::isAlwaysNull() const
620{
621 QualType SrcType = getSubExpr()->getType();
622 QualType DestType = getType();
623
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000624 if (const auto *SrcPTy = SrcType->getAs<PointerType>()) {
Anders Carlsson267c0c92011-04-11 01:43:55 +0000625 SrcType = SrcPTy->getPointeeType();
626 DestType = DestType->castAs<PointerType>()->getPointeeType();
627 }
628
Alexis Hunt78e2b912012-06-19 23:44:55 +0000629 if (DestType->isVoidType())
630 return false;
631
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000632 const auto *SrcRD =
633 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
Anders Carlsson267c0c92011-04-11 01:43:55 +0000634
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000635 if (!SrcRD->hasAttr<FinalAttr>())
636 return false;
637
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000638 const auto *DestRD =
639 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
Anders Carlsson267c0c92011-04-11 01:43:55 +0000640
641 return !DestRD->isDerivedFrom(SrcRD);
642}
643
John McCallcf142162010-08-07 06:22:56 +0000644CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000645CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
646 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000647 const CXXCastPath *BasePath,
Fangrui Song6907ce22018-07-30 19:24:48 +0000648 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000649 SourceLocation RParenLoc,
650 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000651 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Roman Lebedev4aaf1dc2018-08-01 06:06:16 +0000652 void *Buffer =
653 C.Allocate(totalSizeToAlloc<CastExpr::BasePathSizeTy, CXXBaseSpecifier *>(
654 PathSize ? 1 : 0, PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000655 auto *E =
656 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
657 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000658 if (PathSize)
659 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
660 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000661 return E;
662}
663
664CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000665CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
Roman Lebedev4aaf1dc2018-08-01 06:06:16 +0000666 void *Buffer =
667 C.Allocate(totalSizeToAlloc<CastExpr::BasePathSizeTy, CXXBaseSpecifier *>(
668 PathSize ? 1 : 0, PathSize));
John McCallcf142162010-08-07 06:22:56 +0000669 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
670}
671
Craig Toppera31a8822013-08-22 07:09:37 +0000672CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000673 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000674 TypeSourceInfo *WrittenTy,
Fangrui Song6907ce22018-07-30 19:24:48 +0000675 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000676 SourceLocation RParenLoc,
677 SourceRange AngleBrackets) {
678 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000679}
680
Craig Toppera31a8822013-08-22 07:09:37 +0000681CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000682 return new (C) CXXConstCastExpr(EmptyShell());
683}
684
685CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000686CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000687 TypeSourceInfo *Written, CastKind K, Expr *Op,
688 const CXXCastPath *BasePath,
689 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000690 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Roman Lebedev4aaf1dc2018-08-01 06:06:16 +0000691 void *Buffer =
692 C.Allocate(totalSizeToAlloc<CastExpr::BasePathSizeTy, CXXBaseSpecifier *>(
693 PathSize ? 1 : 0, PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000694 auto *E =
695 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000696 if (PathSize)
697 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
698 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000699 return E;
700}
701
702CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000703CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
Roman Lebedev4aaf1dc2018-08-01 06:06:16 +0000704 void *Buffer =
705 C.Allocate(totalSizeToAlloc<CastExpr::BasePathSizeTy, CXXBaseSpecifier *>(
706 PathSize ? 1 : 0, PathSize));
John McCallcf142162010-08-07 06:22:56 +0000707 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
708}
709
Stephen Kelly724e9e52018-08-09 20:05:03 +0000710SourceLocation CXXFunctionalCastExpr::getBeginLoc() const {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000711 return getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
Eli Friedman89fe0d52013-08-15 22:02:56 +0000712}
713
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000714SourceLocation CXXFunctionalCastExpr::getEndLoc() const {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000715 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getEndLoc();
Eli Friedman89fe0d52013-08-15 22:02:56 +0000716}
717
Richard Smithc67fdd42012-03-07 08:35:16 +0000718UserDefinedLiteral::LiteralOperatorKind
719UserDefinedLiteral::getLiteralOperatorKind() const {
720 if (getNumArgs() == 0)
721 return LOK_Template;
722 if (getNumArgs() == 2)
723 return LOK_String;
724
725 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
726 QualType ParamTy =
727 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
728 if (ParamTy->isPointerType())
729 return LOK_Raw;
730 if (ParamTy->isAnyCharacterType())
731 return LOK_Character;
732 if (ParamTy->isIntegerType())
733 return LOK_Integer;
734 if (ParamTy->isFloatingType())
735 return LOK_Floating;
736
737 llvm_unreachable("unknown kind of literal operator");
738}
739
740Expr *UserDefinedLiteral::getCookedLiteral() {
741#ifndef NDEBUG
742 LiteralOperatorKind LOK = getLiteralOperatorKind();
743 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
744#endif
745 return getArg(0);
746}
747
748const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
749 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
750}
John McCallcf142162010-08-07 06:22:56 +0000751
Bruno Riccifd66eb82018-11-17 13:02:47 +0000752CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &Ctx, SourceLocation Loc,
753 FieldDecl *Field, QualType Ty)
754 : Expr(CXXDefaultInitExprClass, Ty.getNonLValueExprType(Ctx),
755 Ty->isLValueReferenceType() ? VK_LValue : Ty->isRValueReferenceType()
Richard Smith852c9db2013-04-20 22:23:05 +0000756 ? VK_XValue
757 : VK_RValue,
758 /*FIXME*/ OK_Ordinary, false, false, false, false),
Bruno Riccifd66eb82018-11-17 13:02:47 +0000759 Field(Field) {
760 CXXDefaultInitExprBits.Loc = Loc;
Richard Smith852c9db2013-04-20 22:23:05 +0000761 assert(Field->hasInClassInitializer());
762}
763
Craig Toppera31a8822013-08-22 07:09:37 +0000764CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000765 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000766 return new (C) CXXTemporary(Destructor);
767}
768
Craig Toppera31a8822013-08-22 07:09:37 +0000769CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000770 CXXTemporary *Temp,
771 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000772 assert((SubExpr->getType()->isRecordType() ||
773 SubExpr->getType()->isArrayType()) &&
774 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000775
Douglas Gregora6e053e2010-12-15 01:34:56 +0000776 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000777}
778
Craig Toppera31a8822013-08-22 07:09:37 +0000779CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000780 CXXConstructorDecl *Cons,
Richard Smith60437622017-02-09 19:17:44 +0000781 QualType Type,
782 TypeSourceInfo *TSI,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000783 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000784 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000785 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000786 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000787 bool StdInitListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000788 bool ZeroInitialization)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000789 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, Type,
790 TSI->getTypeLoc().getBeginLoc(), Cons, false, Args,
791 HadMultipleCandidates, ListInitialization,
792 StdInitListInitialization, ZeroInitialization,
793 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
794 Type(TSI) {}
Douglas Gregor2b88c112010-09-08 00:15:04 +0000795
Stephen Kelly724e9e52018-08-09 20:05:03 +0000796SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000797 return Type->getTypeLoc().getBeginLoc();
798}
799
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000800SourceLocation CXXTemporaryObjectExpr::getEndLoc() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000801 SourceLocation Loc = getParenOrBraceRange().getEnd();
802 if (Loc.isInvalid() && getNumArgs())
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000803 Loc = getArg(getNumArgs() - 1)->getEndLoc();
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000804 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000805}
Anders Carlsson6f287832009-04-21 02:22:11 +0000806
Craig Toppera31a8822013-08-22 07:09:37 +0000807CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000808 SourceLocation Loc,
Richard Smithc2bebe92016-05-11 20:37:46 +0000809 CXXConstructorDecl *Ctor,
810 bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000811 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000812 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000813 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000814 bool StdInitListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000815 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000816 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000817 SourceRange ParenOrBraceRange) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000818 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000819 Ctor, Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000820 HadMultipleCandidates, ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000821 StdInitListInitialization,
Sebastian Redla9351792012-02-11 23:51:47 +0000822 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000823 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000824}
825
Craig Toppera31a8822013-08-22 07:09:37 +0000826CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
827 QualType T, SourceLocation Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000828 CXXConstructorDecl *Ctor,
Richard Smithc2bebe92016-05-11 20:37:46 +0000829 bool Elidable,
830 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000831 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000832 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000833 bool StdInitListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000834 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000835 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000836 SourceRange ParenOrBraceRange)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000837 : Expr(SC, T, VK_RValue, OK_Ordinary,
838 T->isDependentType(), T->isDependentType(),
839 T->isInstantiationDependentType(),
840 T->containsUnexpandedParameterPack()),
841 Constructor(Ctor), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
842 NumArgs(Args.size()), Elidable(Elidable),
843 HadMultipleCandidates(HadMultipleCandidates),
844 ListInitialization(ListInitialization),
845 StdInitListInitialization(StdInitListInitialization),
846 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000847 if (NumArgs) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000848 this->Args = new (C) Stmt*[Args.size()];
Fangrui Song6907ce22018-07-30 19:24:48 +0000849
Richard Smithc2bebe92016-05-11 20:37:46 +0000850 for (unsigned i = 0; i != Args.size(); ++i) {
851 assert(Args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000852
Richard Smithc2bebe92016-05-11 20:37:46 +0000853 if (Args[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000854 ExprBits.ValueDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000855 if (Args[i]->isInstantiationDependent())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000856 ExprBits.InstantiationDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000857 if (Args[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000858 ExprBits.ContainsUnexpandedParameterPack = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000859
Richard Smithc2bebe92016-05-11 20:37:46 +0000860 this->Args[i] = Args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000861 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000862 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000863}
864
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000865LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000866 LambdaCaptureKind Kind, VarDecl *Var,
867 SourceLocation EllipsisLoc)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000868 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000869 unsigned Bits = 0;
870 if (Implicit)
871 Bits |= Capture_Implicit;
Fangrui Song6907ce22018-07-30 19:24:48 +0000872
Douglas Gregore31e6062012-02-07 10:09:13 +0000873 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +0000874 case LCK_StarThis:
875 Bits |= Capture_ByCopy;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000876 LLVM_FALLTHROUGH;
Craig Topper36250ad2014-05-12 05:36:57 +0000877 case LCK_This:
878 assert(!Var && "'this' capture cannot have a variable!");
John Brawn771721c2016-06-15 14:14:51 +0000879 Bits |= Capture_This;
Douglas Gregore31e6062012-02-07 10:09:13 +0000880 break;
881
882 case LCK_ByCopy:
883 Bits |= Capture_ByCopy;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000884 LLVM_FALLTHROUGH;
Douglas Gregore31e6062012-02-07 10:09:13 +0000885 case LCK_ByRef:
886 assert(Var && "capture must have a variable!");
887 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000888 case LCK_VLAType:
889 assert(!Var && "VLA type capture cannot have a variable!");
Alexey Bataev39c81e22014-08-28 04:28:19 +0000890 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000891 }
John Brawn771721c2016-06-15 14:14:51 +0000892 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000893}
894
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000895LambdaCaptureKind LambdaCapture::getCaptureKind() const {
John Brawn771721c2016-06-15 14:14:51 +0000896 if (capturesVLAType())
Faisal Validc6b5962016-03-21 09:25:37 +0000897 return LCK_VLAType;
John Brawn771721c2016-06-15 14:14:51 +0000898 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
899 if (capturesThis())
Faisal Validc6b5962016-03-21 09:25:37 +0000900 return CapByCopy ? LCK_StarThis : LCK_This;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000901 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000902}
903
James Y Knighte00a67e2015-12-31 04:18:25 +0000904LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
Douglas Gregore31e6062012-02-07 10:09:13 +0000905 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000906 SourceLocation CaptureDefaultLoc,
James Y Knighte00a67e2015-12-31 04:18:25 +0000907 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
908 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
Richard Smith2589b9802012-07-25 03:56:55 +0000909 SourceLocation ClosingBrace,
910 bool ContainsUnexpandedParameterPack)
James Y Knighte00a67e2015-12-31 04:18:25 +0000911 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
912 T->isDependentType(), T->isDependentType(),
913 ContainsUnexpandedParameterPack),
914 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
915 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
916 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
917 ClosingBrace(ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000918 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000919 CXXRecordDecl *Class = getLambdaClass();
920 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Fangrui Song6907ce22018-07-30 19:24:48 +0000921
Douglas Gregorc8a73492012-02-13 15:44:47 +0000922 // FIXME: Propagate "has unexpanded parameter pack" bit.
Fangrui Song6907ce22018-07-30 19:24:48 +0000923
Douglas Gregore5561632012-02-13 17:20:40 +0000924 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000925 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000926 Data.NumCaptures = NumCaptures;
927 Data.NumExplicitCaptures = 0;
James Y Knighte00a67e2015-12-31 04:18:25 +0000928 Data.Captures =
929 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
930 LambdaCapture *ToCapture = Data.Captures;
Douglas Gregore5561632012-02-13 17:20:40 +0000931 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
932 if (Captures[I].isExplicit())
933 ++Data.NumExplicitCaptures;
Fangrui Song6907ce22018-07-30 19:24:48 +0000934
Douglas Gregore5561632012-02-13 17:20:40 +0000935 *ToCapture++ = Captures[I];
936 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000937
Douglas Gregore5561632012-02-13 17:20:40 +0000938 // Copy initialization expressions for the non-static data members.
939 Stmt **Stored = getStoredStmts();
940 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
941 *Stored++ = CaptureInits[I];
Fangrui Song6907ce22018-07-30 19:24:48 +0000942
Douglas Gregore5561632012-02-13 17:20:40 +0000943 // Copy the body of the lambda.
944 *Stored++ = getCallOperator()->getBody();
Douglas Gregore31e6062012-02-07 10:09:13 +0000945}
946
James Y Knighte00a67e2015-12-31 04:18:25 +0000947LambdaExpr *LambdaExpr::Create(
948 const ASTContext &Context, CXXRecordDecl *Class,
949 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
950 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
951 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
James Y Knighte00a67e2015-12-31 04:18:25 +0000952 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000953 // Determine the type of the expression (i.e., the type of the
954 // function object we're creating).
955 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000956
Richard Smith30e304e2016-12-14 00:03:17 +0000957 unsigned Size = totalSizeToAlloc<Stmt *>(Captures.size() + 1);
Douglas Gregore5561632012-02-13 17:20:40 +0000958 void *Mem = Context.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +0000959 return new (Mem)
960 LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
961 Captures, ExplicitParams, ExplicitResultType, CaptureInits,
962 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +0000963}
964
Craig Toppera31a8822013-08-22 07:09:37 +0000965LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
Richard Smith30e304e2016-12-14 00:03:17 +0000966 unsigned NumCaptures) {
967 unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
Douglas Gregor99ae8062012-02-14 17:54:36 +0000968 void *Mem = C.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +0000969 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +0000970}
971
James Dennettdd2ffea22015-05-07 18:48:18 +0000972bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
973 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
974 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
975}
976
Douglas Gregorc8a73492012-02-13 15:44:47 +0000977LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000978 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000979}
980
981LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000982 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000983}
984
James Dennett1575cb42014-05-27 19:13:04 +0000985LambdaExpr::capture_range LambdaExpr::captures() const {
986 return capture_range(capture_begin(), capture_end());
987}
988
Douglas Gregorc8a73492012-02-13 15:44:47 +0000989LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
990 return capture_begin();
991}
992
993LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
994 struct CXXRecordDecl::LambdaDefinitionData &Data
995 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000996 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000997}
998
James Dennett1575cb42014-05-27 19:13:04 +0000999LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1000 return capture_range(explicit_capture_begin(), explicit_capture_end());
1001}
1002
Douglas Gregorc8a73492012-02-13 15:44:47 +00001003LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1004 return explicit_capture_end();
1005}
1006
1007LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1008 return capture_end();
1009}
1010
James Dennett1575cb42014-05-27 19:13:04 +00001011LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1012 return capture_range(implicit_capture_begin(), implicit_capture_end());
1013}
1014
Douglas Gregore31e6062012-02-07 10:09:13 +00001015CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1016 return getType()->getAsCXXRecordDecl();
1017}
1018
1019CXXMethodDecl *LambdaExpr::getCallOperator() const {
1020 CXXRecordDecl *Record = getLambdaClass();
Fangrui Song6907ce22018-07-30 19:24:48 +00001021 return Record->getLambdaCallOperator();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001022}
1023
1024TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1025 CXXRecordDecl *Record = getLambdaClass();
1026 return Record->getGenericLambdaTemplateParameterList();
1027
Douglas Gregore31e6062012-02-07 10:09:13 +00001028}
1029
Douglas Gregor99ae8062012-02-14 17:54:36 +00001030CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +00001031 // FIXME: this mutation in getBody is bogus. It should be
1032 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1033 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +00001034 if (!getStoredStmts()[NumCaptures])
Eugene Zelenko821f6982017-11-18 01:47:41 +00001035 *const_cast<Stmt **>(&getStoredStmts()[NumCaptures]) =
James Y Knight53c76162015-07-17 18:21:37 +00001036 getCallOperator()->getBody();
1037
James Y Knighte00a67e2015-12-31 04:18:25 +00001038 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001039}
1040
Douglas Gregore31e6062012-02-07 10:09:13 +00001041bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001042 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001043}
1044
John McCall28fc7092011-11-10 05:35:25 +00001045ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001046 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001047 ArrayRef<CleanupObject> objects)
Bill Wendling7c44da22018-10-31 03:48:47 +00001048 : FullExpr(ExprWithCleanupsClass, subexpr) {
Tim Shen4a05bb82016-06-21 20:29:17 +00001049 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
John McCall28fc7092011-11-10 05:35:25 +00001050 ExprWithCleanupsBits.NumObjects = objects.size();
1051 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001052 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001053}
1054
Craig Toppera31a8822013-08-22 07:09:37 +00001055ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001056 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001057 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001058 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001059 alignof(ExprWithCleanups));
Tim Shen4a05bb82016-06-21 20:29:17 +00001060 return new (buffer)
1061 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001062}
1063
John McCall28fc7092011-11-10 05:35:25 +00001064ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
Bill Wendling7c44da22018-10-31 03:48:47 +00001065 : FullExpr(ExprWithCleanupsClass, empty) {
John McCall28fc7092011-11-10 05:35:25 +00001066 ExprWithCleanupsBits.NumObjects = numObjects;
1067}
Chris Lattnercba86142010-05-10 00:25:06 +00001068
Craig Toppera31a8822013-08-22 07:09:37 +00001069ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1070 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001071 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001072 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001073 alignof(ExprWithCleanups));
John McCall28fc7092011-11-10 05:35:25 +00001074 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001075}
1076
Douglas Gregor2b88c112010-09-08 00:15:04 +00001077CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001078 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001079 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001080 SourceLocation RParenLoc)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001081 : Expr(CXXUnresolvedConstructExprClass,
1082 Type->getType().getNonReferenceType(),
1083 (Type->getType()->isLValueReferenceType()
1084 ? VK_LValue
1085 : Type->getType()->isRValueReferenceType() ? VK_XValue
1086 : VK_RValue),
1087 OK_Ordinary,
1088 Type->getType()->isDependentType() ||
1089 Type->getType()->getContainedDeducedType(),
1090 true, true, Type->getType()->containsUnexpandedParameterPack()),
1091 Type(Type), LParenLoc(LParenLoc), RParenLoc(RParenLoc),
1092 NumArgs(Args.size()) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001093 auto **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001094 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001095 if (Args[I]->containsUnexpandedParameterPack())
1096 ExprBits.ContainsUnexpandedParameterPack = true;
1097
1098 StoredArgs[I] = Args[I];
1099 }
Douglas Gregorce934142009-05-20 18:46:25 +00001100}
1101
1102CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001103CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001104 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001105 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001106 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001107 SourceLocation RParenLoc) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001108 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
Benjamin Kramerc215e762012-08-24 11:54:20 +00001109 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001110}
1111
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001112CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001113CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001114 Stmt::EmptyShell Empty;
James Y Knighte00a67e2015-12-31 04:18:25 +00001115 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001116 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1117}
1118
Stephen Kelly724e9e52018-08-09 20:05:03 +00001119SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001120 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001121}
1122
James Y Knighte7d82282015-12-29 18:15:14 +00001123CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1124 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1125 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1126 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1127 DeclarationNameInfo MemberNameInfo,
1128 const TemplateArgumentListInfo *TemplateArgs)
1129 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1130 OK_Ordinary, true, true, true,
1131 ((Base && Base->containsUnexpandedParameterPack()) ||
1132 (QualifierLoc &&
1133 QualifierLoc.getNestedNameSpecifier()
1134 ->containsUnexpandedParameterPack()) ||
1135 MemberNameInfo.containsUnexpandedParameterPack())),
1136 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1137 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1138 TemplateKWLoc.isValid()),
1139 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1140 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1141 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001142 if (TemplateArgs) {
1143 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001144 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001145 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001146 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1147 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1148 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001149 if (ContainsUnexpandedParameterPack)
1150 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001151 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001152 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1153 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001154 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001155}
1156
John McCall8cd78132009-11-19 22:55:06 +00001157CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001158CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001159 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001160 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001161 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001162 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001163 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001164 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001165 const TemplateArgumentListInfo *TemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001166 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001167 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +00001168 std::size_t Size =
1169 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1170 HasTemplateKWAndArgsInfo, NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001171
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001172 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
John McCall2d74de92009-12-01 22:10:20 +00001173 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1174 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001175 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001176 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001177 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001178 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001179}
1180
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001181CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001182CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001183 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001184 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001185 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1186 std::size_t Size =
1187 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1188 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001189 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001190 auto *E =
1191 new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
1192 false, SourceLocation(),
1193 NestedNameSpecifierLoc(),
1194 SourceLocation(), nullptr,
1195 DeclarationNameInfo(), nullptr);
James Y Knighte7d82282015-12-29 18:15:14 +00001196 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001197 return E;
1198}
1199
Douglas Gregor0da1d432011-02-28 20:01:57 +00001200bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001201 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001202 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001203
Douglas Gregor25b7e052011-03-02 21:06:53 +00001204 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001205}
1206
John McCall0009fcc2011-04-26 20:42:42 +00001207static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1208 UnresolvedSetIterator end) {
1209 do {
1210 NamedDecl *decl = *begin;
1211 if (isa<UnresolvedUsingValueDecl>(decl))
1212 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001213
1214 // Unresolved member expressions should only contain methods and
1215 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001216 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1217 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001218 return false;
1219 } while (++begin != end);
1220
1221 return true;
1222}
1223
Craig Toppera31a8822013-08-22 07:09:37 +00001224UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001225 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001226 Expr *Base, QualType BaseType,
1227 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001228 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001229 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001230 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001231 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001232 const TemplateArgumentListInfo *TemplateArgs,
Fangrui Song6907ce22018-07-30 19:24:48 +00001233 UnresolvedSetIterator Begin,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001234 UnresolvedSetIterator End)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001235 : OverloadExpr(
1236 UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1237 MemberNameInfo, TemplateArgs, Begin, End,
1238 // Dependent
1239 ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
1240 ((Base && Base->isInstantiationDependent()) ||
1241 BaseType->isInstantiationDependentType()),
1242 // Contains unexpanded parameter pack
1243 ((Base && Base->containsUnexpandedParameterPack()) ||
1244 BaseType->containsUnexpandedParameterPack())),
1245 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing), Base(Base),
1246 BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001247 // Check whether all of the members are non-static member functions,
1248 // and if so, mark give this bound-member type instead of overload type.
1249 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1250 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001251}
1252
Douglas Gregor0da1d432011-02-28 20:01:57 +00001253bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001254 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001255 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001256
Douglas Gregor25b7e052011-03-02 21:06:53 +00001257 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001258}
1259
James Y Knighte7d82282015-12-29 18:15:14 +00001260UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1261 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1262 bool IsArrow, SourceLocation OperatorLoc,
1263 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1264 const DeclarationNameInfo &MemberNameInfo,
1265 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1266 UnresolvedSetIterator End) {
1267 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1268 std::size_t Size =
1269 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1270 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
John McCall10eae182009-11-30 22:42:35 +00001271
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001272 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
James Y Knighte7d82282015-12-29 18:15:14 +00001273 return new (Mem) UnresolvedMemberExpr(
1274 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1275 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001276}
1277
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001278UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001279UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1280 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001281 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001282 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1283 std::size_t Size =
1284 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1285 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001286
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001287 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001288 auto *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001289 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001290 return E;
1291}
1292
John McCall58cc69d2010-01-27 01:50:18 +00001293CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1294 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1295
1296 // If there was a nested name specifier, it names the naming class.
1297 // It can't be dependent: after all, we were actually able to do the
1298 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001299 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001300 auto *NNS = getQualifier();
1301 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001302 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001303 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001304 Record = T->getAsCXXRecordDecl();
1305 assert(Record && "qualifier in member expression does not name record");
1306 }
John McCall58cc69d2010-01-27 01:50:18 +00001307 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001308 else {
John McCall58cc69d2010-01-27 01:50:18 +00001309 QualType BaseType = getBaseType().getNonReferenceType();
1310 if (isArrow()) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001311 const auto *PT = BaseType->getAs<PointerType>();
John McCall58cc69d2010-01-27 01:50:18 +00001312 assert(PT && "base of arrow member access is not pointer");
1313 BaseType = PT->getPointeeType();
1314 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001315
Douglas Gregor9262f472010-04-27 18:19:34 +00001316 Record = BaseType->getAsCXXRecordDecl();
1317 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001318 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001319
Douglas Gregor9262f472010-04-27 18:19:34 +00001320 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001321}
1322
Richard Smithd784e682015-09-23 21:41:42 +00001323SizeOfPackExpr *
1324SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1325 NamedDecl *Pack, SourceLocation PackLoc,
1326 SourceLocation RParenLoc,
1327 Optional<unsigned> Length,
1328 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001329 void *Storage =
1330 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001331 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1332 PackLoc, RParenLoc, Length, PartialArgs);
1333}
1334
1335SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1336 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001337 void *Storage =
1338 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001339 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1340}
1341
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001342SubstNonTypeTemplateParmPackExpr::
Fangrui Song6907ce22018-07-30 19:24:48 +00001343SubstNonTypeTemplateParmPackExpr(QualType T,
Richard Smithf1f20e62018-02-14 02:07:53 +00001344 ExprValueKind ValueKind,
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001345 NonTypeTemplateParmDecl *Param,
1346 SourceLocation NameLoc,
1347 const TemplateArgument &ArgPack)
Richard Smithf1f20e62018-02-14 02:07:53 +00001348 : Expr(SubstNonTypeTemplateParmPackExprClass, T, ValueKind, OK_Ordinary,
Eugene Zelenko821f6982017-11-18 01:47:41 +00001349 true, true, true, true),
1350 Param(Param), Arguments(ArgPack.pack_begin()),
1351 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) {}
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001352
1353TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001354 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001355}
1356
Richard Smithb15fe3a2012-09-12 00:56:43 +00001357FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1358 SourceLocation NameLoc,
1359 unsigned NumParams,
James Y Knight48fefa32015-09-30 14:04:23 +00001360 ParmVarDecl *const *Params)
1361 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1362 true, true),
1363 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001364 if (Params)
1365 std::uninitialized_copy(Params, Params + NumParams,
James Y Knighte00a67e2015-12-31 04:18:25 +00001366 getTrailingObjects<ParmVarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001367}
1368
1369FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001370FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001371 ParmVarDecl *ParamPack, SourceLocation NameLoc,
James Y Knight48fefa32015-09-30 14:04:23 +00001372 ArrayRef<ParmVarDecl *> Params) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001373 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1374 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001375}
1376
1377FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001378FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1379 unsigned NumParams) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001380 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1381 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001382}
1383
David Majnemerdaff3702014-05-01 17:50:17 +00001384void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1385 unsigned ManglingNumber) {
1386 // We only need extra state if we have to remember more than just the Stmt.
1387 if (!ExtendedBy)
1388 return;
1389
1390 // We may need to allocate extra storage for the mangling number and the
1391 // extended-by ValueDecl.
1392 if (!State.is<ExtraState *>()) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001393 auto *ES = new (ExtendedBy->getASTContext()) ExtraState;
David Majnemerdaff3702014-05-01 17:50:17 +00001394 ES->Temporary = State.get<Stmt *>();
1395 State = ES;
1396 }
1397
1398 auto ES = State.get<ExtraState *>();
1399 ES->ExtendingDecl = ExtendedBy;
1400 ES->ManglingNumber = ManglingNumber;
1401}
1402
Douglas Gregor29c42f22012-02-24 07:38:34 +00001403TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1404 ArrayRef<TypeSourceInfo *> Args,
1405 SourceLocation RParenLoc,
1406 bool Value)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001407 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1408 /*TypeDependent=*/false,
1409 /*ValueDependent=*/false,
1410 /*InstantiationDependent=*/false,
1411 /*ContainsUnexpandedParameterPack=*/false),
1412 Loc(Loc), RParenLoc(RParenLoc) {
Douglas Gregor29c42f22012-02-24 07:38:34 +00001413 TypeTraitExprBits.Kind = Kind;
1414 TypeTraitExprBits.Value = Value;
1415 TypeTraitExprBits.NumArgs = Args.size();
1416
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001417 auto **ToArgs = getTrailingObjects<TypeSourceInfo *>();
James Y Knighte00a67e2015-12-31 04:18:25 +00001418
Douglas Gregor29c42f22012-02-24 07:38:34 +00001419 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1420 if (Args[I]->getType()->isDependentType())
1421 setValueDependent(true);
1422 if (Args[I]->getType()->isInstantiationDependentType())
1423 setInstantiationDependent(true);
1424 if (Args[I]->getType()->containsUnexpandedParameterPack())
1425 setContainsUnexpandedParameterPack(true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001426
Douglas Gregor29c42f22012-02-24 07:38:34 +00001427 ToArgs[I] = Args[I];
1428 }
1429}
1430
Craig Toppera31a8822013-08-22 07:09:37 +00001431TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Fangrui Song6907ce22018-07-30 19:24:48 +00001432 SourceLocation Loc,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001433 TypeTrait Kind,
1434 ArrayRef<TypeSourceInfo *> Args,
1435 SourceLocation RParenLoc,
1436 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001437 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001438 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1439}
1440
Craig Toppera31a8822013-08-22 07:09:37 +00001441TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001442 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001443 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001444 return new (Mem) TypeTraitExpr(EmptyShell());
1445}