blob: f14f5a361bafda0a88235a4bc8882be2b766e3b4 [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
Craig Toppera31a8822013-08-22 07:09:37 +0000752CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000753 FieldDecl *Field, QualType T)
754 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
755 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
756 ? VK_XValue
757 : VK_RValue,
758 /*FIXME*/ OK_Ordinary, false, false, false, false),
759 Field(Field), Loc(Loc) {
760 assert(Field->hasInClassInitializer());
761}
762
Craig Toppera31a8822013-08-22 07:09:37 +0000763CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000764 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000765 return new (C) CXXTemporary(Destructor);
766}
767
Craig Toppera31a8822013-08-22 07:09:37 +0000768CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000769 CXXTemporary *Temp,
770 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000771 assert((SubExpr->getType()->isRecordType() ||
772 SubExpr->getType()->isArrayType()) &&
773 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000774
Douglas Gregora6e053e2010-12-15 01:34:56 +0000775 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000776}
777
Craig Toppera31a8822013-08-22 07:09:37 +0000778CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000779 CXXConstructorDecl *Cons,
Richard Smith60437622017-02-09 19:17:44 +0000780 QualType Type,
781 TypeSourceInfo *TSI,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000782 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000783 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000784 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000785 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000786 bool StdInitListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000787 bool ZeroInitialization)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000788 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, Type,
789 TSI->getTypeLoc().getBeginLoc(), Cons, false, Args,
790 HadMultipleCandidates, ListInitialization,
791 StdInitListInitialization, ZeroInitialization,
792 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
793 Type(TSI) {}
Douglas Gregor2b88c112010-09-08 00:15:04 +0000794
Stephen Kelly724e9e52018-08-09 20:05:03 +0000795SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000796 return Type->getTypeLoc().getBeginLoc();
797}
798
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000799SourceLocation CXXTemporaryObjectExpr::getEndLoc() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000800 SourceLocation Loc = getParenOrBraceRange().getEnd();
801 if (Loc.isInvalid() && getNumArgs())
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000802 Loc = getArg(getNumArgs() - 1)->getEndLoc();
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000803 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000804}
Anders Carlsson6f287832009-04-21 02:22:11 +0000805
Craig Toppera31a8822013-08-22 07:09:37 +0000806CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000807 SourceLocation Loc,
Richard Smithc2bebe92016-05-11 20:37:46 +0000808 CXXConstructorDecl *Ctor,
809 bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000810 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000811 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000812 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000813 bool StdInitListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000814 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000815 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000816 SourceRange ParenOrBraceRange) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000817 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000818 Ctor, Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000819 HadMultipleCandidates, ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000820 StdInitListInitialization,
Sebastian Redla9351792012-02-11 23:51:47 +0000821 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000822 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000823}
824
Craig Toppera31a8822013-08-22 07:09:37 +0000825CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
826 QualType T, SourceLocation Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000827 CXXConstructorDecl *Ctor,
Richard Smithc2bebe92016-05-11 20:37:46 +0000828 bool Elidable,
829 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000830 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000831 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000832 bool StdInitListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000833 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000834 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000835 SourceRange ParenOrBraceRange)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000836 : Expr(SC, T, VK_RValue, OK_Ordinary,
837 T->isDependentType(), T->isDependentType(),
838 T->isInstantiationDependentType(),
839 T->containsUnexpandedParameterPack()),
840 Constructor(Ctor), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
841 NumArgs(Args.size()), Elidable(Elidable),
842 HadMultipleCandidates(HadMultipleCandidates),
843 ListInitialization(ListInitialization),
844 StdInitListInitialization(StdInitListInitialization),
845 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000846 if (NumArgs) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000847 this->Args = new (C) Stmt*[Args.size()];
Fangrui Song6907ce22018-07-30 19:24:48 +0000848
Richard Smithc2bebe92016-05-11 20:37:46 +0000849 for (unsigned i = 0; i != Args.size(); ++i) {
850 assert(Args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000851
Richard Smithc2bebe92016-05-11 20:37:46 +0000852 if (Args[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000853 ExprBits.ValueDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000854 if (Args[i]->isInstantiationDependent())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000855 ExprBits.InstantiationDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000856 if (Args[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000857 ExprBits.ContainsUnexpandedParameterPack = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000858
Richard Smithc2bebe92016-05-11 20:37:46 +0000859 this->Args[i] = Args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000860 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000861 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000862}
863
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000864LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000865 LambdaCaptureKind Kind, VarDecl *Var,
866 SourceLocation EllipsisLoc)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000867 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000868 unsigned Bits = 0;
869 if (Implicit)
870 Bits |= Capture_Implicit;
Fangrui Song6907ce22018-07-30 19:24:48 +0000871
Douglas Gregore31e6062012-02-07 10:09:13 +0000872 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +0000873 case LCK_StarThis:
874 Bits |= Capture_ByCopy;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000875 LLVM_FALLTHROUGH;
Craig Topper36250ad2014-05-12 05:36:57 +0000876 case LCK_This:
877 assert(!Var && "'this' capture cannot have a variable!");
John Brawn771721c2016-06-15 14:14:51 +0000878 Bits |= Capture_This;
Douglas Gregore31e6062012-02-07 10:09:13 +0000879 break;
880
881 case LCK_ByCopy:
882 Bits |= Capture_ByCopy;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000883 LLVM_FALLTHROUGH;
Douglas Gregore31e6062012-02-07 10:09:13 +0000884 case LCK_ByRef:
885 assert(Var && "capture must have a variable!");
886 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000887 case LCK_VLAType:
888 assert(!Var && "VLA type capture cannot have a variable!");
Alexey Bataev39c81e22014-08-28 04:28:19 +0000889 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000890 }
John Brawn771721c2016-06-15 14:14:51 +0000891 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000892}
893
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000894LambdaCaptureKind LambdaCapture::getCaptureKind() const {
John Brawn771721c2016-06-15 14:14:51 +0000895 if (capturesVLAType())
Faisal Validc6b5962016-03-21 09:25:37 +0000896 return LCK_VLAType;
John Brawn771721c2016-06-15 14:14:51 +0000897 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
898 if (capturesThis())
Faisal Validc6b5962016-03-21 09:25:37 +0000899 return CapByCopy ? LCK_StarThis : LCK_This;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000900 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000901}
902
James Y Knighte00a67e2015-12-31 04:18:25 +0000903LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
Douglas Gregore31e6062012-02-07 10:09:13 +0000904 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000905 SourceLocation CaptureDefaultLoc,
James Y Knighte00a67e2015-12-31 04:18:25 +0000906 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
907 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
Richard Smith2589b9802012-07-25 03:56:55 +0000908 SourceLocation ClosingBrace,
909 bool ContainsUnexpandedParameterPack)
James Y Knighte00a67e2015-12-31 04:18:25 +0000910 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
911 T->isDependentType(), T->isDependentType(),
912 ContainsUnexpandedParameterPack),
913 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
914 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
915 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
916 ClosingBrace(ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000917 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000918 CXXRecordDecl *Class = getLambdaClass();
919 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Fangrui Song6907ce22018-07-30 19:24:48 +0000920
Douglas Gregorc8a73492012-02-13 15:44:47 +0000921 // FIXME: Propagate "has unexpanded parameter pack" bit.
Fangrui Song6907ce22018-07-30 19:24:48 +0000922
Douglas Gregore5561632012-02-13 17:20:40 +0000923 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000924 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000925 Data.NumCaptures = NumCaptures;
926 Data.NumExplicitCaptures = 0;
James Y Knighte00a67e2015-12-31 04:18:25 +0000927 Data.Captures =
928 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
929 LambdaCapture *ToCapture = Data.Captures;
Douglas Gregore5561632012-02-13 17:20:40 +0000930 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
931 if (Captures[I].isExplicit())
932 ++Data.NumExplicitCaptures;
Fangrui Song6907ce22018-07-30 19:24:48 +0000933
Douglas Gregore5561632012-02-13 17:20:40 +0000934 *ToCapture++ = Captures[I];
935 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000936
Douglas Gregore5561632012-02-13 17:20:40 +0000937 // Copy initialization expressions for the non-static data members.
938 Stmt **Stored = getStoredStmts();
939 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
940 *Stored++ = CaptureInits[I];
Fangrui Song6907ce22018-07-30 19:24:48 +0000941
Douglas Gregore5561632012-02-13 17:20:40 +0000942 // Copy the body of the lambda.
943 *Stored++ = getCallOperator()->getBody();
Douglas Gregore31e6062012-02-07 10:09:13 +0000944}
945
James Y Knighte00a67e2015-12-31 04:18:25 +0000946LambdaExpr *LambdaExpr::Create(
947 const ASTContext &Context, CXXRecordDecl *Class,
948 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
949 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
950 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
James Y Knighte00a67e2015-12-31 04:18:25 +0000951 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000952 // Determine the type of the expression (i.e., the type of the
953 // function object we're creating).
954 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000955
Richard Smith30e304e2016-12-14 00:03:17 +0000956 unsigned Size = totalSizeToAlloc<Stmt *>(Captures.size() + 1);
Douglas Gregore5561632012-02-13 17:20:40 +0000957 void *Mem = Context.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +0000958 return new (Mem)
959 LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
960 Captures, ExplicitParams, ExplicitResultType, CaptureInits,
961 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +0000962}
963
Craig Toppera31a8822013-08-22 07:09:37 +0000964LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
Richard Smith30e304e2016-12-14 00:03:17 +0000965 unsigned NumCaptures) {
966 unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
Douglas Gregor99ae8062012-02-14 17:54:36 +0000967 void *Mem = C.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +0000968 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +0000969}
970
James Dennettdd2ffea22015-05-07 18:48:18 +0000971bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
972 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
973 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
974}
975
Douglas Gregorc8a73492012-02-13 15:44:47 +0000976LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000977 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000978}
979
980LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000981 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000982}
983
James Dennett1575cb42014-05-27 19:13:04 +0000984LambdaExpr::capture_range LambdaExpr::captures() const {
985 return capture_range(capture_begin(), capture_end());
986}
987
Douglas Gregorc8a73492012-02-13 15:44:47 +0000988LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
989 return capture_begin();
990}
991
992LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
993 struct CXXRecordDecl::LambdaDefinitionData &Data
994 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000995 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000996}
997
James Dennett1575cb42014-05-27 19:13:04 +0000998LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
999 return capture_range(explicit_capture_begin(), explicit_capture_end());
1000}
1001
Douglas Gregorc8a73492012-02-13 15:44:47 +00001002LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1003 return explicit_capture_end();
1004}
1005
1006LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1007 return capture_end();
1008}
1009
James Dennett1575cb42014-05-27 19:13:04 +00001010LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1011 return capture_range(implicit_capture_begin(), implicit_capture_end());
1012}
1013
Douglas Gregore31e6062012-02-07 10:09:13 +00001014CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1015 return getType()->getAsCXXRecordDecl();
1016}
1017
1018CXXMethodDecl *LambdaExpr::getCallOperator() const {
1019 CXXRecordDecl *Record = getLambdaClass();
Fangrui Song6907ce22018-07-30 19:24:48 +00001020 return Record->getLambdaCallOperator();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001021}
1022
1023TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1024 CXXRecordDecl *Record = getLambdaClass();
1025 return Record->getGenericLambdaTemplateParameterList();
1026
Douglas Gregore31e6062012-02-07 10:09:13 +00001027}
1028
Douglas Gregor99ae8062012-02-14 17:54:36 +00001029CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +00001030 // FIXME: this mutation in getBody is bogus. It should be
1031 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1032 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +00001033 if (!getStoredStmts()[NumCaptures])
Eugene Zelenko821f6982017-11-18 01:47:41 +00001034 *const_cast<Stmt **>(&getStoredStmts()[NumCaptures]) =
James Y Knight53c76162015-07-17 18:21:37 +00001035 getCallOperator()->getBody();
1036
James Y Knighte00a67e2015-12-31 04:18:25 +00001037 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001038}
1039
Douglas Gregore31e6062012-02-07 10:09:13 +00001040bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001041 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001042}
1043
John McCall28fc7092011-11-10 05:35:25 +00001044ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001045 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001046 ArrayRef<CleanupObject> objects)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001047 : Expr(ExprWithCleanupsClass, subexpr->getType(),
1048 subexpr->getValueKind(), subexpr->getObjectKind(),
1049 subexpr->isTypeDependent(), subexpr->isValueDependent(),
1050 subexpr->isInstantiationDependent(),
1051 subexpr->containsUnexpandedParameterPack()),
1052 SubExpr(subexpr) {
Tim Shen4a05bb82016-06-21 20:29:17 +00001053 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
John McCall28fc7092011-11-10 05:35:25 +00001054 ExprWithCleanupsBits.NumObjects = objects.size();
1055 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001056 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001057}
1058
Craig Toppera31a8822013-08-22 07:09:37 +00001059ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001060 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001061 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001062 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001063 alignof(ExprWithCleanups));
Tim Shen4a05bb82016-06-21 20:29:17 +00001064 return new (buffer)
1065 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001066}
1067
John McCall28fc7092011-11-10 05:35:25 +00001068ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001069 : Expr(ExprWithCleanupsClass, empty) {
John McCall28fc7092011-11-10 05:35:25 +00001070 ExprWithCleanupsBits.NumObjects = numObjects;
1071}
Chris Lattnercba86142010-05-10 00:25:06 +00001072
Craig Toppera31a8822013-08-22 07:09:37 +00001073ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1074 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001075 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001076 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001077 alignof(ExprWithCleanups));
John McCall28fc7092011-11-10 05:35:25 +00001078 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001079}
1080
Douglas Gregor2b88c112010-09-08 00:15:04 +00001081CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001082 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001083 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001084 SourceLocation RParenLoc)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001085 : Expr(CXXUnresolvedConstructExprClass,
1086 Type->getType().getNonReferenceType(),
1087 (Type->getType()->isLValueReferenceType()
1088 ? VK_LValue
1089 : Type->getType()->isRValueReferenceType() ? VK_XValue
1090 : VK_RValue),
1091 OK_Ordinary,
1092 Type->getType()->isDependentType() ||
1093 Type->getType()->getContainedDeducedType(),
1094 true, true, Type->getType()->containsUnexpandedParameterPack()),
1095 Type(Type), LParenLoc(LParenLoc), RParenLoc(RParenLoc),
1096 NumArgs(Args.size()) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001097 auto **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001098 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001099 if (Args[I]->containsUnexpandedParameterPack())
1100 ExprBits.ContainsUnexpandedParameterPack = true;
1101
1102 StoredArgs[I] = Args[I];
1103 }
Douglas Gregorce934142009-05-20 18:46:25 +00001104}
1105
1106CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001107CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001108 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001109 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001110 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001111 SourceLocation RParenLoc) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001112 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
Benjamin Kramerc215e762012-08-24 11:54:20 +00001113 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001114}
1115
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001116CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001117CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001118 Stmt::EmptyShell Empty;
James Y Knighte00a67e2015-12-31 04:18:25 +00001119 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001120 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1121}
1122
Stephen Kelly724e9e52018-08-09 20:05:03 +00001123SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001124 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001125}
1126
James Y Knighte7d82282015-12-29 18:15:14 +00001127CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1128 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1129 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1130 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1131 DeclarationNameInfo MemberNameInfo,
1132 const TemplateArgumentListInfo *TemplateArgs)
1133 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1134 OK_Ordinary, true, true, true,
1135 ((Base && Base->containsUnexpandedParameterPack()) ||
1136 (QualifierLoc &&
1137 QualifierLoc.getNestedNameSpecifier()
1138 ->containsUnexpandedParameterPack()) ||
1139 MemberNameInfo.containsUnexpandedParameterPack())),
1140 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1141 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1142 TemplateKWLoc.isValid()),
1143 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1144 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1145 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001146 if (TemplateArgs) {
1147 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001148 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001149 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001150 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1151 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1152 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001153 if (ContainsUnexpandedParameterPack)
1154 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001155 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001156 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1157 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001158 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001159}
1160
John McCall8cd78132009-11-19 22:55:06 +00001161CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001162CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001163 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001164 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001165 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001166 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001167 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001168 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001169 const TemplateArgumentListInfo *TemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001170 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001171 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +00001172 std::size_t Size =
1173 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1174 HasTemplateKWAndArgsInfo, NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001175
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001176 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
John McCall2d74de92009-12-01 22:10:20 +00001177 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1178 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001179 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001180 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001181 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001182 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001183}
1184
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001185CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001186CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001187 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001188 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001189 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1190 std::size_t Size =
1191 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1192 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001193 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001194 auto *E =
1195 new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
1196 false, SourceLocation(),
1197 NestedNameSpecifierLoc(),
1198 SourceLocation(), nullptr,
1199 DeclarationNameInfo(), nullptr);
James Y Knighte7d82282015-12-29 18:15:14 +00001200 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001201 return E;
1202}
1203
Douglas Gregor0da1d432011-02-28 20:01:57 +00001204bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001205 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001206 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001207
Douglas Gregor25b7e052011-03-02 21:06:53 +00001208 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001209}
1210
John McCall0009fcc2011-04-26 20:42:42 +00001211static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1212 UnresolvedSetIterator end) {
1213 do {
1214 NamedDecl *decl = *begin;
1215 if (isa<UnresolvedUsingValueDecl>(decl))
1216 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001217
1218 // Unresolved member expressions should only contain methods and
1219 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001220 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1221 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001222 return false;
1223 } while (++begin != end);
1224
1225 return true;
1226}
1227
Craig Toppera31a8822013-08-22 07:09:37 +00001228UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001229 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001230 Expr *Base, QualType BaseType,
1231 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001232 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001233 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001234 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001235 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001236 const TemplateArgumentListInfo *TemplateArgs,
Fangrui Song6907ce22018-07-30 19:24:48 +00001237 UnresolvedSetIterator Begin,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001238 UnresolvedSetIterator End)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001239 : OverloadExpr(
1240 UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1241 MemberNameInfo, TemplateArgs, Begin, End,
1242 // Dependent
1243 ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
1244 ((Base && Base->isInstantiationDependent()) ||
1245 BaseType->isInstantiationDependentType()),
1246 // Contains unexpanded parameter pack
1247 ((Base && Base->containsUnexpandedParameterPack()) ||
1248 BaseType->containsUnexpandedParameterPack())),
1249 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing), Base(Base),
1250 BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001251 // Check whether all of the members are non-static member functions,
1252 // and if so, mark give this bound-member type instead of overload type.
1253 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1254 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001255}
1256
Douglas Gregor0da1d432011-02-28 20:01:57 +00001257bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001258 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001259 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001260
Douglas Gregor25b7e052011-03-02 21:06:53 +00001261 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001262}
1263
James Y Knighte7d82282015-12-29 18:15:14 +00001264UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1265 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1266 bool IsArrow, SourceLocation OperatorLoc,
1267 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1268 const DeclarationNameInfo &MemberNameInfo,
1269 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1270 UnresolvedSetIterator End) {
1271 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1272 std::size_t Size =
1273 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1274 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
John McCall10eae182009-11-30 22:42:35 +00001275
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001276 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
James Y Knighte7d82282015-12-29 18:15:14 +00001277 return new (Mem) UnresolvedMemberExpr(
1278 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1279 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001280}
1281
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001282UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001283UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1284 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001285 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001286 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1287 std::size_t Size =
1288 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1289 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001290
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001291 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001292 auto *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001293 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001294 return E;
1295}
1296
John McCall58cc69d2010-01-27 01:50:18 +00001297CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1298 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1299
1300 // If there was a nested name specifier, it names the naming class.
1301 // It can't be dependent: after all, we were actually able to do the
1302 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001303 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001304 auto *NNS = getQualifier();
1305 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001306 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001307 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001308 Record = T->getAsCXXRecordDecl();
1309 assert(Record && "qualifier in member expression does not name record");
1310 }
John McCall58cc69d2010-01-27 01:50:18 +00001311 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001312 else {
John McCall58cc69d2010-01-27 01:50:18 +00001313 QualType BaseType = getBaseType().getNonReferenceType();
1314 if (isArrow()) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001315 const auto *PT = BaseType->getAs<PointerType>();
John McCall58cc69d2010-01-27 01:50:18 +00001316 assert(PT && "base of arrow member access is not pointer");
1317 BaseType = PT->getPointeeType();
1318 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001319
Douglas Gregor9262f472010-04-27 18:19:34 +00001320 Record = BaseType->getAsCXXRecordDecl();
1321 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001322 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001323
Douglas Gregor9262f472010-04-27 18:19:34 +00001324 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001325}
1326
Richard Smithd784e682015-09-23 21:41:42 +00001327SizeOfPackExpr *
1328SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1329 NamedDecl *Pack, SourceLocation PackLoc,
1330 SourceLocation RParenLoc,
1331 Optional<unsigned> Length,
1332 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001333 void *Storage =
1334 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001335 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1336 PackLoc, RParenLoc, Length, PartialArgs);
1337}
1338
1339SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1340 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001341 void *Storage =
1342 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001343 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1344}
1345
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001346SubstNonTypeTemplateParmPackExpr::
Fangrui Song6907ce22018-07-30 19:24:48 +00001347SubstNonTypeTemplateParmPackExpr(QualType T,
Richard Smithf1f20e62018-02-14 02:07:53 +00001348 ExprValueKind ValueKind,
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001349 NonTypeTemplateParmDecl *Param,
1350 SourceLocation NameLoc,
1351 const TemplateArgument &ArgPack)
Richard Smithf1f20e62018-02-14 02:07:53 +00001352 : Expr(SubstNonTypeTemplateParmPackExprClass, T, ValueKind, OK_Ordinary,
Eugene Zelenko821f6982017-11-18 01:47:41 +00001353 true, true, true, true),
1354 Param(Param), Arguments(ArgPack.pack_begin()),
1355 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) {}
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001356
1357TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001358 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001359}
1360
Richard Smithb15fe3a2012-09-12 00:56:43 +00001361FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1362 SourceLocation NameLoc,
1363 unsigned NumParams,
James Y Knight48fefa32015-09-30 14:04:23 +00001364 ParmVarDecl *const *Params)
1365 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1366 true, true),
1367 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001368 if (Params)
1369 std::uninitialized_copy(Params, Params + NumParams,
James Y Knighte00a67e2015-12-31 04:18:25 +00001370 getTrailingObjects<ParmVarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001371}
1372
1373FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001374FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001375 ParmVarDecl *ParamPack, SourceLocation NameLoc,
James Y Knight48fefa32015-09-30 14:04:23 +00001376 ArrayRef<ParmVarDecl *> Params) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001377 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1378 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001379}
1380
1381FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001382FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1383 unsigned NumParams) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001384 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1385 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001386}
1387
David Majnemerdaff3702014-05-01 17:50:17 +00001388void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1389 unsigned ManglingNumber) {
1390 // We only need extra state if we have to remember more than just the Stmt.
1391 if (!ExtendedBy)
1392 return;
1393
1394 // We may need to allocate extra storage for the mangling number and the
1395 // extended-by ValueDecl.
1396 if (!State.is<ExtraState *>()) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001397 auto *ES = new (ExtendedBy->getASTContext()) ExtraState;
David Majnemerdaff3702014-05-01 17:50:17 +00001398 ES->Temporary = State.get<Stmt *>();
1399 State = ES;
1400 }
1401
1402 auto ES = State.get<ExtraState *>();
1403 ES->ExtendingDecl = ExtendedBy;
1404 ES->ManglingNumber = ManglingNumber;
1405}
1406
Douglas Gregor29c42f22012-02-24 07:38:34 +00001407TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1408 ArrayRef<TypeSourceInfo *> Args,
1409 SourceLocation RParenLoc,
1410 bool Value)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001411 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1412 /*TypeDependent=*/false,
1413 /*ValueDependent=*/false,
1414 /*InstantiationDependent=*/false,
1415 /*ContainsUnexpandedParameterPack=*/false),
1416 Loc(Loc), RParenLoc(RParenLoc) {
Douglas Gregor29c42f22012-02-24 07:38:34 +00001417 TypeTraitExprBits.Kind = Kind;
1418 TypeTraitExprBits.Value = Value;
1419 TypeTraitExprBits.NumArgs = Args.size();
1420
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001421 auto **ToArgs = getTrailingObjects<TypeSourceInfo *>();
James Y Knighte00a67e2015-12-31 04:18:25 +00001422
Douglas Gregor29c42f22012-02-24 07:38:34 +00001423 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1424 if (Args[I]->getType()->isDependentType())
1425 setValueDependent(true);
1426 if (Args[I]->getType()->isInstantiationDependentType())
1427 setInstantiationDependent(true);
1428 if (Args[I]->getType()->containsUnexpandedParameterPack())
1429 setContainsUnexpandedParameterPack(true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001430
Douglas Gregor29c42f22012-02-24 07:38:34 +00001431 ToArgs[I] = Args[I];
1432 }
1433}
1434
Craig Toppera31a8822013-08-22 07:09:37 +00001435TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Fangrui Song6907ce22018-07-30 19:24:48 +00001436 SourceLocation Loc,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001437 TypeTrait Kind,
1438 ArrayRef<TypeSourceInfo *> Args,
1439 SourceLocation RParenLoc,
1440 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001441 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001442 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1443}
1444
Craig Toppera31a8822013-08-22 07:09:37 +00001445TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001446 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001447 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001448 return new (Mem) TypeTraitExpr(EmptyShell());
1449}
1450
Eugene Zelenko821f6982017-11-18 01:47:41 +00001451void ArrayTypeTraitExpr::anchor() {}