blob: deb76820afe57677fb2061d200d67489f4789316 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ted Kremeneke3a0c142007-08-24 20:21:10 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the subclesses of Expr class declared in ExprCXX.h
10//
11//===----------------------------------------------------------------------===//
12
Eugene Zelenko821f6982017-11-18 01:47:41 +000013#include "clang/AST/ExprCXX.h"
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000014#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Eugene Zelenko821f6982017-11-18 01:47:41 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclAccessPair.h"
18#include "clang/AST/DeclBase.h"
Douglas Gregor993603d2008-11-14 16:09:21 +000019#include "clang/AST/DeclCXX.h"
Eugene Zelenko821f6982017-11-18 01:47:41 +000020#include "clang/AST/DeclarationName.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/LambdaCapture.h"
23#include "clang/AST/NestedNameSpecifier.h"
24#include "clang/AST/TemplateBase.h"
25#include "clang/AST/Type.h"
Douglas Gregor651fe5e2010-02-24 23:40:28 +000026#include "clang/AST/TypeLoc.h"
Eugene Zelenko821f6982017-11-18 01:47:41 +000027#include "clang/Basic/LLVM.h"
28#include "clang/Basic/OperatorKinds.h"
29#include "clang/Basic/SourceLocation.h"
30#include "clang/Basic/Specifiers.h"
31#include "llvm/ADT/ArrayRef.h"
32#include "llvm/Support/Casting.h"
33#include "llvm/Support/ErrorHandling.h"
34#include <cassert>
35#include <cstddef>
36#include <cstring>
37#include <memory>
Ted Kremeneke3a0c142007-08-24 20:21:10 +000038
Eugene Zelenko821f6982017-11-18 01:47:41 +000039using namespace clang;
Douglas Gregor9da64192010-04-26 22:37:10 +000040
Ted Kremeneke3a0c142007-08-24 20:21:10 +000041//===----------------------------------------------------------------------===//
42// Child Iterators for iterating over subexpressions/substatements
43//===----------------------------------------------------------------------===//
44
Richard Smith66094432016-10-20 00:55:15 +000045bool CXXOperatorCallExpr::isInfixBinaryOp() const {
46 // An infix binary operator is any operator with two arguments other than
47 // operator() and operator[]. Note that none of these operators can have
48 // default arguments, so it suffices to check the number of argument
49 // expressions.
50 if (getNumArgs() != 2)
51 return false;
52
53 switch (getOperator()) {
54 case OO_Call: case OO_Subscript:
55 return false;
56 default:
57 return true;
58 }
59}
60
Richard Smith778dc0f2019-10-19 00:04:38 +000061CXXRewrittenBinaryOperator::DecomposedForm
62CXXRewrittenBinaryOperator::getDecomposedForm() const {
63 DecomposedForm Result = {};
64 const Expr *E = getSemanticForm()->IgnoreImplicit();
65
66 // Remove an outer '!' if it exists (only happens for a '!=' rewrite).
67 bool SkippedNot = false;
68 if (auto *NotEq = dyn_cast<UnaryOperator>(E)) {
69 assert(NotEq->getOpcode() == UO_LNot);
70 E = NotEq->getSubExpr()->IgnoreImplicit();
71 SkippedNot = true;
72 }
73
74 // Decompose the outer binary operator.
75 if (auto *BO = dyn_cast<BinaryOperator>(E)) {
76 assert(!SkippedNot || BO->getOpcode() == BO_EQ);
77 Result.Opcode = SkippedNot ? BO_NE : BO->getOpcode();
78 Result.LHS = BO->getLHS();
79 Result.RHS = BO->getRHS();
80 Result.InnerBinOp = BO;
81 } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
Richard Smith974c8b72019-10-19 00:04:43 +000082 assert(!SkippedNot || BO->getOperator() == OO_EqualEqual);
Richard Smith778dc0f2019-10-19 00:04:38 +000083 assert(BO->isInfixBinaryOp());
84 switch (BO->getOperator()) {
85 case OO_Less: Result.Opcode = BO_LT; break;
86 case OO_LessEqual: Result.Opcode = BO_LE; break;
87 case OO_Greater: Result.Opcode = BO_GT; break;
88 case OO_GreaterEqual: Result.Opcode = BO_GE; break;
89 case OO_Spaceship: Result.Opcode = BO_Cmp; break;
90 case OO_EqualEqual: Result.Opcode = SkippedNot ? BO_NE : BO_EQ; break;
91 default: llvm_unreachable("unexpected binop in rewritten operator expr");
92 }
93 Result.LHS = BO->getArg(0);
94 Result.RHS = BO->getArg(1);
95 Result.InnerBinOp = BO;
96 } else {
97 llvm_unreachable("unexpected rewritten operator form");
98 }
99
100 // Put the operands in the right order for == and !=, and canonicalize the
101 // <=> subexpression onto the LHS for all other forms.
102 if (isReversed())
103 std::swap(Result.LHS, Result.RHS);
104
105 // If this isn't a spaceship rewrite, we're done.
106 if (Result.Opcode == BO_EQ || Result.Opcode == BO_NE)
107 return Result;
108
109 // Otherwise, we expect a <=> to now be on the LHS.
Richard Smith75990952019-12-05 18:04:46 -0800110 E = Result.LHS->IgnoreImplicitAsWritten();
Richard Smith778dc0f2019-10-19 00:04:38 +0000111 if (auto *BO = dyn_cast<BinaryOperator>(E)) {
112 assert(BO->getOpcode() == BO_Cmp);
113 Result.LHS = BO->getLHS();
114 Result.RHS = BO->getRHS();
115 Result.InnerBinOp = BO;
116 } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
117 assert(BO->getOperator() == OO_Spaceship);
118 Result.LHS = BO->getArg(0);
119 Result.RHS = BO->getArg(1);
120 Result.InnerBinOp = BO;
121 } else {
122 llvm_unreachable("unexpected rewritten operator form");
123 }
124
125 // Put the comparison operands in the right order.
126 if (isReversed())
127 std::swap(Result.LHS, Result.RHS);
128 return Result;
129}
130
Richard Smithef8bf432012-08-13 20:08:14 +0000131bool CXXTypeidExpr::isPotentiallyEvaluated() const {
132 if (isTypeOperand())
133 return false;
134
135 // C++11 [expr.typeid]p3:
136 // When typeid is applied to an expression other than a glvalue of
137 // polymorphic class type, [...] the expression is an unevaluated operand.
138 const Expr *E = getExprOperand();
139 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
140 if (RD->isPolymorphic() && E->isGLValue())
141 return true;
142
143 return false;
144}
145
David Majnemer143c55e2013-09-27 07:04:31 +0000146QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
Douglas Gregor9da64192010-04-26 22:37:10 +0000147 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +0000148 Qualifiers Quals;
149 return Context.getUnqualifiedArrayType(
150 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Douglas Gregor9da64192010-04-26 22:37:10 +0000151}
152
David Majnemer143c55e2013-09-27 07:04:31 +0000153QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
Francois Pichet9f4f2072010-09-08 12:20:18 +0000154 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +0000155 Qualifiers Quals;
156 return Context.getUnqualifiedArrayType(
157 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Francois Pichet9f4f2072010-09-08 12:20:18 +0000158}
159
Douglas Gregor747eb782010-07-08 06:14:04 +0000160// CXXScalarValueInitExpr
Stephen Kelly724e9e52018-08-09 20:05:03 +0000161SourceLocation CXXScalarValueInitExpr::getBeginLoc() const {
Bruno Ricci030dab42019-01-08 16:08:54 +0000162 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : getRParenLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +0000163}
164
Sebastian Redlbd150f42008-11-21 19:14:01 +0000165// CXXNewExpr
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000166CXXNewExpr::CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
167 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
168 bool UsualArrayDeleteWantsSize,
169 ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
Richard Smithb9fb1212019-05-06 03:47:15 +0000170 Optional<Expr *> ArraySize,
171 InitializationStyle InitializationStyle,
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000172 Expr *Initializer, QualType Ty,
173 TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
174 SourceRange DirectInitRange)
175 : Expr(CXXNewExprClass, Ty, VK_RValue, OK_Ordinary, Ty->isDependentType(),
176 Ty->isDependentType(), Ty->isInstantiationDependentType(),
177 Ty->containsUnexpandedParameterPack()),
178 OperatorNew(OperatorNew), OperatorDelete(OperatorDelete),
179 AllocatedTypeInfo(AllocatedTypeInfo), Range(Range),
180 DirectInitRange(DirectInitRange) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000181
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000182 assert((Initializer != nullptr || InitializationStyle == NoInit) &&
183 "Only NoInit can have no initializer!");
184
185 CXXNewExprBits.IsGlobalNew = IsGlobalNew;
Richard Smithb9fb1212019-05-06 03:47:15 +0000186 CXXNewExprBits.IsArray = ArraySize.hasValue();
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000187 CXXNewExprBits.ShouldPassAlignment = ShouldPassAlignment;
188 CXXNewExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
189 CXXNewExprBits.StoredInitializationStyle =
190 Initializer ? InitializationStyle + 1 : 0;
191 bool IsParenTypeId = TypeIdParens.isValid();
192 CXXNewExprBits.IsParenTypeId = IsParenTypeId;
193 CXXNewExprBits.NumPlacementArgs = PlacementArgs.size();
194
195 if (ArraySize) {
Richard Smithb9fb1212019-05-06 03:47:15 +0000196 if (Expr *SizeExpr = *ArraySize) {
Richard Smithda1b4342019-09-27 01:26:47 +0000197 if (SizeExpr->isValueDependent())
198 ExprBits.ValueDependent = true;
Richard Smithb9fb1212019-05-06 03:47:15 +0000199 if (SizeExpr->isInstantiationDependent())
200 ExprBits.InstantiationDependent = true;
201 if (SizeExpr->containsUnexpandedParameterPack())
202 ExprBits.ContainsUnexpandedParameterPack = true;
203 }
Douglas Gregora6e053e2010-12-15 01:34:56 +0000204
Richard Smithb9fb1212019-05-06 03:47:15 +0000205 getTrailingObjects<Stmt *>()[arraySizeOffset()] = *ArraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000206 }
207
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000208 if (Initializer) {
Richard Smithda1b4342019-09-27 01:26:47 +0000209 if (Initializer->isValueDependent())
210 ExprBits.ValueDependent = true;
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000211 if (Initializer->isInstantiationDependent())
Sebastian Redl6047f072012-02-16 12:22:20 +0000212 ExprBits.InstantiationDependent = true;
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000213 if (Initializer->containsUnexpandedParameterPack())
Sebastian Redl6047f072012-02-16 12:22:20 +0000214 ExprBits.ContainsUnexpandedParameterPack = true;
215
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000216 getTrailingObjects<Stmt *>()[initExprOffset()] = Initializer;
Sebastian Redl6047f072012-02-16 12:22:20 +0000217 }
218
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000219 for (unsigned I = 0; I != PlacementArgs.size(); ++I) {
Richard Smithda1b4342019-09-27 01:26:47 +0000220 if (PlacementArgs[I]->isValueDependent())
221 ExprBits.ValueDependent = true;
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000222 if (PlacementArgs[I]->isInstantiationDependent())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000223 ExprBits.InstantiationDependent = true;
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000224 if (PlacementArgs[I]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000225 ExprBits.ContainsUnexpandedParameterPack = true;
226
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000227 getTrailingObjects<Stmt *>()[placementNewArgsOffset() + I] =
228 PlacementArgs[I];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000229 }
David Blaikie3a0de212012-11-08 22:53:48 +0000230
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000231 if (IsParenTypeId)
232 getTrailingObjects<SourceRange>()[0] = TypeIdParens;
233
David Blaikie3a0de212012-11-08 22:53:48 +0000234 switch (getInitializationStyle()) {
235 case CallInit:
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000236 this->Range.setEnd(DirectInitRange.getEnd());
237 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000238 case ListInit:
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000239 this->Range.setEnd(getInitializer()->getSourceRange().getEnd());
240 break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000241 default:
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000242 if (IsParenTypeId)
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000243 this->Range.setEnd(TypeIdParens.getEnd());
244 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000245 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000246}
247
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000248CXXNewExpr::CXXNewExpr(EmptyShell Empty, bool IsArray,
249 unsigned NumPlacementArgs, bool IsParenTypeId)
250 : Expr(CXXNewExprClass, Empty) {
251 CXXNewExprBits.IsArray = IsArray;
252 CXXNewExprBits.NumPlacementArgs = NumPlacementArgs;
253 CXXNewExprBits.IsParenTypeId = IsParenTypeId;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000254}
255
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000256CXXNewExpr *
257CXXNewExpr::Create(const ASTContext &Ctx, bool IsGlobalNew,
258 FunctionDecl *OperatorNew, FunctionDecl *OperatorDelete,
259 bool ShouldPassAlignment, bool UsualArrayDeleteWantsSize,
260 ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
Richard Smithb9fb1212019-05-06 03:47:15 +0000261 Optional<Expr *> ArraySize,
262 InitializationStyle InitializationStyle, Expr *Initializer,
263 QualType Ty, TypeSourceInfo *AllocatedTypeInfo,
264 SourceRange Range, SourceRange DirectInitRange) {
265 bool IsArray = ArraySize.hasValue();
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000266 bool HasInit = Initializer != nullptr;
267 unsigned NumPlacementArgs = PlacementArgs.size();
268 bool IsParenTypeId = TypeIdParens.isValid();
269 void *Mem =
270 Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
271 IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
272 alignof(CXXNewExpr));
273 return new (Mem)
274 CXXNewExpr(IsGlobalNew, OperatorNew, OperatorDelete, ShouldPassAlignment,
275 UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens,
276 ArraySize, InitializationStyle, Initializer, Ty,
277 AllocatedTypeInfo, Range, DirectInitRange);
278}
279
280CXXNewExpr *CXXNewExpr::CreateEmpty(const ASTContext &Ctx, bool IsArray,
281 bool HasInit, unsigned NumPlacementArgs,
282 bool IsParenTypeId) {
283 void *Mem =
284 Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
285 IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
286 alignof(CXXNewExpr));
287 return new (Mem)
288 CXXNewExpr(EmptyShell(), IsArray, NumPlacementArgs, IsParenTypeId);
289}
290
291bool CXXNewExpr::shouldNullCheckAllocation() const {
292 return getOperatorNew()
293 ->getType()
294 ->castAs<FunctionProtoType>()
295 ->isNothrow() &&
Richard Smith902a0232015-02-14 01:52:20 +0000296 !getOperatorNew()->isReservedGlobalPlacementOperator();
John McCall75f94982011-03-07 03:12:35 +0000297}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000298
Sebastian Redlbd150f42008-11-21 19:14:01 +0000299// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000300QualType CXXDeleteExpr::getDestroyedType() const {
301 const Expr *Arg = getArgument();
Richard Smith5b349582017-10-13 01:55:36 +0000302
303 // For a destroying operator delete, we may have implicitly converted the
304 // pointer type to the type of the parameter of the 'operator delete'
305 // function.
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000306 while (const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
Richard Smith5b349582017-10-13 01:55:36 +0000307 if (ICE->getCastKind() == CK_DerivedToBase ||
308 ICE->getCastKind() == CK_UncheckedDerivedToBase ||
309 ICE->getCastKind() == CK_NoOp) {
310 assert((ICE->getCastKind() == CK_NoOp ||
311 getOperatorDelete()->isDestroyingOperatorDelete()) &&
312 "only a destroying operator delete can have a converted arg");
313 Arg = ICE->getSubExpr();
314 } else
315 break;
316 }
317
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000318 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000319 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000320
321 if (ArgType->isDependentType() && !ArgType->isPointerType())
322 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000323
Simon Pilgrim3459a4c2019-10-07 13:58:05 +0000324 return ArgType->castAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000325}
326
Douglas Gregorad8a3362009-09-04 17:36:40 +0000327// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000328PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000329 : Type(Info) {
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000330 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000331}
332
Craig Toppera31a8822013-08-22 07:09:37 +0000333CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000334 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
Fangrui Song6907ce22018-07-30 19:24:48 +0000335 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
336 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000337 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000338 : Expr(CXXPseudoDestructorExprClass,
David Majnemerced8bdf2015-02-25 17:36:15 +0000339 Context.BoundMemberTy,
John McCalldb40c7f2010-12-14 08:05:40 +0000340 VK_RValue, OK_Ordinary,
341 /*isTypeDependent=*/(Base->isTypeDependent() ||
342 (DestroyedType.getTypeSourceInfo() &&
343 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000344 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000345 (Base->isInstantiationDependent() ||
346 (QualifierLoc &&
347 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
348 (ScopeType &&
349 ScopeType->getType()->isInstantiationDependentType()) ||
350 (DestroyedType.getTypeSourceInfo() &&
351 DestroyedType.getTypeSourceInfo()->getType()
352 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000353 // ContainsUnexpandedParameterPack
354 (Base->containsUnexpandedParameterPack() ||
Fangrui Song6907ce22018-07-30 19:24:48 +0000355 (QualifierLoc &&
Douglas Gregora6ce6082011-02-25 18:19:59 +0000356 QualifierLoc.getNestedNameSpecifier()
357 ->containsUnexpandedParameterPack()) ||
Fangrui Song6907ce22018-07-30 19:24:48 +0000358 (ScopeType &&
Douglas Gregora6e053e2010-12-15 01:34:56 +0000359 ScopeType->getType()->containsUnexpandedParameterPack()) ||
360 (DestroyedType.getTypeSourceInfo() &&
361 DestroyedType.getTypeSourceInfo()->getType()
362 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000363 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000364 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000365 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
Eugene Zelenko821f6982017-11-18 01:47:41 +0000366 DestroyedType(DestroyedType) {}
John McCalldb40c7f2010-12-14 08:05:40 +0000367
Douglas Gregor678f90d2010-02-25 01:56:36 +0000368QualType CXXPseudoDestructorExpr::getDestroyedType() const {
369 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
370 return TInfo->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +0000371
Douglas Gregor678f90d2010-02-25 01:56:36 +0000372 return QualType();
373}
374
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000375SourceLocation CXXPseudoDestructorExpr::getEndLoc() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000376 SourceLocation End = DestroyedType.getLocation();
377 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000378 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000379 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000380}
381
John McCalld14a8642009-11-21 08:51:07 +0000382// UnresolvedLookupExpr
Bruno Riccid7628d92019-01-09 15:43:19 +0000383UnresolvedLookupExpr::UnresolvedLookupExpr(
384 const ASTContext &Context, CXXRecordDecl *NamingClass,
385 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
386 const DeclarationNameInfo &NameInfo, bool RequiresADL, bool Overloaded,
387 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
388 UnresolvedSetIterator End)
389 : OverloadExpr(UnresolvedLookupExprClass, Context, QualifierLoc,
390 TemplateKWLoc, NameInfo, TemplateArgs, Begin, End, false,
391 false, false),
392 NamingClass(NamingClass) {
393 UnresolvedLookupExprBits.RequiresADL = RequiresADL;
394 UnresolvedLookupExprBits.Overloaded = Overloaded;
395}
396
397UnresolvedLookupExpr::UnresolvedLookupExpr(EmptyShell Empty,
398 unsigned NumResults,
399 bool HasTemplateKWAndArgsInfo)
400 : OverloadExpr(UnresolvedLookupExprClass, Empty, NumResults,
401 HasTemplateKWAndArgsInfo) {}
402
403UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
404 const ASTContext &Context, CXXRecordDecl *NamingClass,
405 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
406 bool RequiresADL, bool Overloaded, UnresolvedSetIterator Begin,
407 UnresolvedSetIterator End) {
408 unsigned NumResults = End - Begin;
409 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
410 TemplateArgumentLoc>(NumResults, 0, 0);
411 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
412 return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc,
413 SourceLocation(), NameInfo, RequiresADL,
414 Overloaded, nullptr, Begin, End);
415}
416
417UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
418 const ASTContext &Context, CXXRecordDecl *NamingClass,
419 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
420 const DeclarationNameInfo &NameInfo, bool RequiresADL,
421 const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
422 UnresolvedSetIterator End) {
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000423 assert(Args || TemplateKWLoc.isValid());
Bruno Riccid7628d92019-01-09 15:43:19 +0000424 unsigned NumResults = End - Begin;
425 unsigned NumTemplateArgs = Args ? Args->size() : 0;
426 unsigned Size =
427 totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
428 TemplateArgumentLoc>(NumResults, 1, NumTemplateArgs);
429 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
430 return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc,
431 TemplateKWLoc, NameInfo, RequiresADL,
432 /*Overloaded*/ true, Args, Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000433}
434
Bruno Riccid7628d92019-01-09 15:43:19 +0000435UnresolvedLookupExpr *UnresolvedLookupExpr::CreateEmpty(
436 const ASTContext &Context, unsigned NumResults,
437 bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000438 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
Bruno Riccid7628d92019-01-09 15:43:19 +0000439 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
440 TemplateArgumentLoc>(
441 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
442 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
443 return new (Mem)
444 UnresolvedLookupExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000445}
446
Bruno Riccid7628d92019-01-09 15:43:19 +0000447OverloadExpr::OverloadExpr(StmtClass SC, const ASTContext &Context,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000448 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000449 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000450 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000451 const TemplateArgumentListInfo *TemplateArgs,
Fangrui Song6907ce22018-07-30 19:24:48 +0000452 UnresolvedSetIterator Begin,
Bruno Riccid7628d92019-01-09 15:43:19 +0000453 UnresolvedSetIterator End, bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000454 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000455 bool KnownContainsUnexpandedParameterPack)
Bruno Riccid7628d92019-01-09 15:43:19 +0000456 : Expr(
457 SC, Context.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
458 KnownDependent,
459 (KnownInstantiationDependent || NameInfo.isInstantiationDependent() ||
460 (QualifierLoc &&
Eugene Zelenko821f6982017-11-18 01:47:41 +0000461 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Bruno Riccid7628d92019-01-09 15:43:19 +0000462 (KnownContainsUnexpandedParameterPack ||
463 NameInfo.containsUnexpandedParameterPack() ||
464 (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
465 ->containsUnexpandedParameterPack()))),
466 NameInfo(NameInfo), QualifierLoc(QualifierLoc) {
467 unsigned NumResults = End - Begin;
468 OverloadExprBits.NumResults = NumResults;
469 OverloadExprBits.HasTemplateKWAndArgsInfo =
470 (TemplateArgs != nullptr ) || TemplateKWLoc.isValid();
471
Douglas Gregora6e053e2010-12-15 01:34:56 +0000472 if (NumResults) {
473 // Determine whether this expression is type-dependent.
474 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
475 if ((*I)->getDeclContext()->isDependentContext() ||
476 isa<UnresolvedUsingValueDecl>(*I)) {
477 ExprBits.TypeDependent = true;
478 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000479 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000480 }
481 }
482
Bruno Riccid7628d92019-01-09 15:43:19 +0000483 // Copy the results to the trailing array past UnresolvedLookupExpr
484 // or UnresolvedMemberExpr.
485 DeclAccessPair *Results = getTrailingResults();
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000486 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregora6e053e2010-12-15 01:34:56 +0000487 }
488
489 // If we have explicit template arguments, check for dependent
490 // template arguments and whether they contain any unexpanded pack
491 // expansions.
492 if (TemplateArgs) {
493 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000494 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000495 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +0000496 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
497 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
498 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000499
500 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000501 ExprBits.TypeDependent = true;
502 ExprBits.ValueDependent = true;
503 }
504 if (InstantiationDependent)
505 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000506 if (ContainsUnexpandedParameterPack)
507 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000508 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000509 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000510 }
511
512 if (isTypeDependent())
Bruno Riccid7628d92019-01-09 15:43:19 +0000513 setType(Context.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000514}
515
Bruno Riccid7628d92019-01-09 15:43:19 +0000516OverloadExpr::OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
517 bool HasTemplateKWAndArgsInfo)
518 : Expr(SC, Empty) {
519 OverloadExprBits.NumResults = NumResults;
520 OverloadExprBits.HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
John McCall8c12dc42010-04-22 18:44:12 +0000521}
522
John McCall8cd78132009-11-19 22:55:06 +0000523// DependentScopeDeclRefExpr
Bruno Ricci49ee9642019-01-07 14:27:04 +0000524DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(
525 QualType Ty, NestedNameSpecifierLoc QualifierLoc,
526 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
527 const TemplateArgumentListInfo *Args)
528 : Expr(
529 DependentScopeDeclRefExprClass, Ty, VK_LValue, OK_Ordinary, true,
530 true,
531 (NameInfo.isInstantiationDependent() ||
532 (QualifierLoc &&
533 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
534 (NameInfo.containsUnexpandedParameterPack() ||
535 (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
536 ->containsUnexpandedParameterPack()))),
537 QualifierLoc(QualifierLoc), NameInfo(NameInfo) {
538 DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
539 (Args != nullptr) || TemplateKWLoc.isValid();
Douglas Gregora6e053e2010-12-15 01:34:56 +0000540 if (Args) {
541 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000542 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000543 bool ContainsUnexpandedParameterPack
544 = ExprBits.ContainsUnexpandedParameterPack;
James Y Knighte7d82282015-12-29 18:15:14 +0000545 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
546 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
547 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000548 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000549 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000550 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
551 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000552 }
553}
554
Bruno Ricci49ee9642019-01-07 14:27:04 +0000555DependentScopeDeclRefExpr *DependentScopeDeclRefExpr::Create(
556 const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
557 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
558 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000559 assert(QualifierLoc && "should be created for dependent qualifiers");
James Y Knighte7d82282015-12-29 18:15:14 +0000560 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
561 std::size_t Size =
562 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
563 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
Bruno Ricci49ee9642019-01-07 14:27:04 +0000564 void *Mem = Context.Allocate(Size);
565 return new (Mem) DependentScopeDeclRefExpr(Context.DependentTy, QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000566 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000567}
568
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000569DependentScopeDeclRefExpr *
Bruno Ricci49ee9642019-01-07 14:27:04 +0000570DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &Context,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000571 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000572 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000573 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
574 std::size_t Size =
575 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
576 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Bruno Ricci49ee9642019-01-07 14:27:04 +0000577 void *Mem = Context.Allocate(Size);
578 auto *E = new (Mem) DependentScopeDeclRefExpr(
579 QualType(), NestedNameSpecifierLoc(), SourceLocation(),
580 DeclarationNameInfo(), nullptr);
581 E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
582 HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000583 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000584}
585
Stephen Kelly724e9e52018-08-09 20:05:03 +0000586SourceLocation CXXConstructExpr::getBeginLoc() const {
John McCall701417a2011-02-21 06:23:05 +0000587 if (isa<CXXTemporaryObjectExpr>(this))
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000588 return cast<CXXTemporaryObjectExpr>(this)->getBeginLoc();
Bruno Ricciddb8f6b2018-12-22 14:39:30 +0000589 return getLocation();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000590}
591
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000592SourceLocation CXXConstructExpr::getEndLoc() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000593 if (isa<CXXTemporaryObjectExpr>(this))
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000594 return cast<CXXTemporaryObjectExpr>(this)->getEndLoc();
John McCall701417a2011-02-21 06:23:05 +0000595
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000596 if (ParenOrBraceRange.isValid())
597 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000598
Bruno Ricciddb8f6b2018-12-22 14:39:30 +0000599 SourceLocation End = getLocation();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000600 for (unsigned I = getNumArgs(); I > 0; --I) {
601 const Expr *Arg = getArg(I-1);
602 if (!Arg->isDefaultArgument()) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000603 SourceLocation NewEnd = Arg->getEndLoc();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000604 if (NewEnd.isValid()) {
605 End = NewEnd;
606 break;
607 }
608 }
609 }
610
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000611 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000612}
613
Bruno Riccic5885cf2018-12-21 15:20:32 +0000614CXXOperatorCallExpr::CXXOperatorCallExpr(OverloadedOperatorKind OpKind,
615 Expr *Fn, ArrayRef<Expr *> Args,
616 QualType Ty, ExprValueKind VK,
617 SourceLocation OperatorLoc,
618 FPOptions FPFeatures,
619 ADLCallKind UsesADL)
620 : CallExpr(CXXOperatorCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
Bruno Riccifeb19232018-12-21 16:51:57 +0000621 OperatorLoc, /*MinNumArgs=*/0, UsesADL) {
622 CXXOperatorCallExprBits.OperatorKind = OpKind;
623 CXXOperatorCallExprBits.FPFeatures = FPFeatures.getInt();
Bruno Riccic4ba5462018-12-21 17:54:51 +0000624 assert(
625 (CXXOperatorCallExprBits.OperatorKind == static_cast<unsigned>(OpKind)) &&
626 "OperatorKind overflow!");
Bruno Riccifeb19232018-12-21 16:51:57 +0000627 assert((CXXOperatorCallExprBits.FPFeatures == FPFeatures.getInt()) &&
628 "FPFeatures overflow!");
Bruno Riccic5885cf2018-12-21 15:20:32 +0000629 Range = getSourceRangeImpl();
630}
631
632CXXOperatorCallExpr::CXXOperatorCallExpr(unsigned NumArgs, EmptyShell Empty)
633 : CallExpr(CXXOperatorCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
634
635CXXOperatorCallExpr *CXXOperatorCallExpr::Create(
636 const ASTContext &Ctx, OverloadedOperatorKind OpKind, Expr *Fn,
637 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
638 SourceLocation OperatorLoc, FPOptions FPFeatures, ADLCallKind UsesADL) {
639 // Allocate storage for the trailing objects of CallExpr.
640 unsigned NumArgs = Args.size();
641 unsigned SizeOfTrailingObjects =
642 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
643 void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
644 alignof(CXXOperatorCallExpr));
645 return new (Mem) CXXOperatorCallExpr(OpKind, Fn, Args, Ty, VK, OperatorLoc,
646 FPFeatures, UsesADL);
647}
648
649CXXOperatorCallExpr *CXXOperatorCallExpr::CreateEmpty(const ASTContext &Ctx,
650 unsigned NumArgs,
651 EmptyShell Empty) {
652 // Allocate storage for the trailing objects of CallExpr.
653 unsigned SizeOfTrailingObjects =
654 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
655 void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
656 alignof(CXXOperatorCallExpr));
657 return new (Mem) CXXOperatorCallExpr(NumArgs, Empty);
658}
659
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000660SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000661 OverloadedOperatorKind Kind = getOperator();
662 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
663 if (getNumArgs() == 1)
664 // Prefix operator
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000665 return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000666 else
667 // Postfix operator
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000668 return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000669 } else if (Kind == OO_Arrow) {
670 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000671 } else if (Kind == OO_Call) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000672 return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000673 } else if (Kind == OO_Subscript) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000674 return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000675 } else if (getNumArgs() == 1) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000676 return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000677 } else if (getNumArgs() == 2) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000678 return SourceRange(getArg(0)->getBeginLoc(), getArg(1)->getEndLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000679 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000680 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000681 }
682}
683
Bruno Riccic5885cf2018-12-21 15:20:32 +0000684CXXMemberCallExpr::CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args,
685 QualType Ty, ExprValueKind VK,
686 SourceLocation RP, unsigned MinNumArgs)
687 : CallExpr(CXXMemberCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, RP,
688 MinNumArgs, NotADL) {}
689
690CXXMemberCallExpr::CXXMemberCallExpr(unsigned NumArgs, EmptyShell Empty)
691 : CallExpr(CXXMemberCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
692
693CXXMemberCallExpr *CXXMemberCallExpr::Create(const ASTContext &Ctx, Expr *Fn,
694 ArrayRef<Expr *> Args, QualType Ty,
695 ExprValueKind VK,
696 SourceLocation RP,
697 unsigned MinNumArgs) {
698 // Allocate storage for the trailing objects of CallExpr.
699 unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
700 unsigned SizeOfTrailingObjects =
701 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
702 void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
703 alignof(CXXMemberCallExpr));
704 return new (Mem) CXXMemberCallExpr(Fn, Args, Ty, VK, RP, MinNumArgs);
705}
706
707CXXMemberCallExpr *CXXMemberCallExpr::CreateEmpty(const ASTContext &Ctx,
708 unsigned NumArgs,
709 EmptyShell Empty) {
710 // Allocate storage for the trailing objects of CallExpr.
711 unsigned SizeOfTrailingObjects =
712 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
713 void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
714 alignof(CXXMemberCallExpr));
715 return new (Mem) CXXMemberCallExpr(NumArgs, Empty);
716}
717
Ted Kremenek98a24e32011-03-30 17:41:19 +0000718Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000719 const Expr *Callee = getCallee()->IgnoreParens();
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000720 if (const auto *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000721 return MemExpr->getBase();
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000722 if (const auto *BO = dyn_cast<BinaryOperator>(Callee))
Jordan Rose16fe35e2012-08-03 23:08:39 +0000723 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
724 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000725
726 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000727 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000728}
729
Marco Antognini88559632019-07-22 09:39:13 +0000730QualType CXXMemberCallExpr::getObjectType() const {
731 QualType Ty = getImplicitObjectArgument()->getType();
732 if (Ty->isPointerType())
733 Ty = Ty->getPointeeType();
734 return Ty;
735}
736
Ted Kremenek98a24e32011-03-30 17:41:19 +0000737CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000738 if (const auto *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Ted Kremenek98a24e32011-03-30 17:41:19 +0000739 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
740
741 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000742 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000743}
744
David Blaikiec0f58662012-05-03 16:25:49 +0000745CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000746 Expr* ThisArg = getImplicitObjectArgument();
747 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000748 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000749
750 if (ThisArg->getType()->isAnyPointerType())
751 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
752
753 return ThisArg->getType()->getAsCXXRecordDecl();
754}
755
Douglas Gregore200adc2008-10-27 19:41:14 +0000756//===----------------------------------------------------------------------===//
757// Named casts
758//===----------------------------------------------------------------------===//
759
760/// getCastName - Get the name of the C++ cast being used, e.g.,
761/// "static_cast", "dynamic_cast", "reinterpret_cast", or
762/// "const_cast". The returned pointer must not be freed.
763const char *CXXNamedCastExpr::getCastName() const {
764 switch (getStmtClass()) {
765 case CXXStaticCastExprClass: return "static_cast";
766 case CXXDynamicCastExprClass: return "dynamic_cast";
767 case CXXReinterpretCastExprClass: return "reinterpret_cast";
768 case CXXConstCastExprClass: return "const_cast";
769 default: return "<invalid cast>";
770 }
771}
Douglas Gregordd04d332009-01-16 18:33:17 +0000772
Craig Toppera31a8822013-08-22 07:09:37 +0000773CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000774 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000775 CastKind K, Expr *Op,
776 const CXXCastPath *BasePath,
777 TypeSourceInfo *WrittenTy,
Fangrui Song6907ce22018-07-30 19:24:48 +0000778 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000779 SourceLocation RParenLoc,
780 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000781 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Bruno Ricci49391652019-01-09 16:41:33 +0000782 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000783 auto *E =
784 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
785 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000786 if (PathSize)
787 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
788 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000789 return E;
790}
791
Craig Toppera31a8822013-08-22 07:09:37 +0000792CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000793 unsigned PathSize) {
Bruno Ricci49391652019-01-09 16:41:33 +0000794 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000795 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
796}
797
Craig Toppera31a8822013-08-22 07:09:37 +0000798CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000799 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000800 CastKind K, Expr *Op,
801 const CXXCastPath *BasePath,
802 TypeSourceInfo *WrittenTy,
Fangrui Song6907ce22018-07-30 19:24:48 +0000803 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000804 SourceLocation RParenLoc,
805 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000806 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Bruno Ricci49391652019-01-09 16:41:33 +0000807 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000808 auto *E =
809 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
810 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000811 if (PathSize)
812 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
813 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000814 return E;
815}
816
Craig Toppera31a8822013-08-22 07:09:37 +0000817CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000818 unsigned PathSize) {
Bruno Ricci49391652019-01-09 16:41:33 +0000819 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000820 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
821}
822
Anders Carlsson267c0c92011-04-11 01:43:55 +0000823/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
824/// to always be null. For example:
825///
826/// struct A { };
827/// struct B final : A { };
828/// struct C { };
829///
830/// C *f(B* b) { return dynamic_cast<C*>(b); }
831bool CXXDynamicCastExpr::isAlwaysNull() const
832{
833 QualType SrcType = getSubExpr()->getType();
834 QualType DestType = getType();
835
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000836 if (const auto *SrcPTy = SrcType->getAs<PointerType>()) {
Anders Carlsson267c0c92011-04-11 01:43:55 +0000837 SrcType = SrcPTy->getPointeeType();
838 DestType = DestType->castAs<PointerType>()->getPointeeType();
839 }
840
Alexis Hunt78e2b912012-06-19 23:44:55 +0000841 if (DestType->isVoidType())
842 return false;
843
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000844 const auto *SrcRD =
845 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
Anders Carlsson267c0c92011-04-11 01:43:55 +0000846
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000847 if (!SrcRD->hasAttr<FinalAttr>())
848 return false;
849
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000850 const auto *DestRD =
851 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
Anders Carlsson267c0c92011-04-11 01:43:55 +0000852
853 return !DestRD->isDerivedFrom(SrcRD);
854}
855
John McCallcf142162010-08-07 06:22:56 +0000856CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000857CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
858 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000859 const CXXCastPath *BasePath,
Fangrui Song6907ce22018-07-30 19:24:48 +0000860 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000861 SourceLocation RParenLoc,
862 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000863 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Bruno Ricci49391652019-01-09 16:41:33 +0000864 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000865 auto *E =
866 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
867 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000868 if (PathSize)
869 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
870 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000871 return E;
872}
873
874CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000875CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
Bruno Ricci49391652019-01-09 16:41:33 +0000876 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000877 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
878}
879
Craig Toppera31a8822013-08-22 07:09:37 +0000880CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000881 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000882 TypeSourceInfo *WrittenTy,
Fangrui Song6907ce22018-07-30 19:24:48 +0000883 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000884 SourceLocation RParenLoc,
885 SourceRange AngleBrackets) {
886 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000887}
888
Craig Toppera31a8822013-08-22 07:09:37 +0000889CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000890 return new (C) CXXConstCastExpr(EmptyShell());
891}
892
893CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000894CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000895 TypeSourceInfo *Written, CastKind K, Expr *Op,
896 const CXXCastPath *BasePath,
897 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000898 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Bruno Ricci49391652019-01-09 16:41:33 +0000899 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000900 auto *E =
901 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000902 if (PathSize)
903 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
904 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000905 return E;
906}
907
908CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000909CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
Bruno Ricci49391652019-01-09 16:41:33 +0000910 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000911 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
912}
913
Stephen Kelly724e9e52018-08-09 20:05:03 +0000914SourceLocation CXXFunctionalCastExpr::getBeginLoc() const {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000915 return getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
Eli Friedman89fe0d52013-08-15 22:02:56 +0000916}
917
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000918SourceLocation CXXFunctionalCastExpr::getEndLoc() const {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000919 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getEndLoc();
Eli Friedman89fe0d52013-08-15 22:02:56 +0000920}
921
Bruno Riccic5885cf2018-12-21 15:20:32 +0000922UserDefinedLiteral::UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args,
923 QualType Ty, ExprValueKind VK,
924 SourceLocation LitEndLoc,
925 SourceLocation SuffixLoc)
926 : CallExpr(UserDefinedLiteralClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
927 LitEndLoc, /*MinNumArgs=*/0, NotADL),
928 UDSuffixLoc(SuffixLoc) {}
929
930UserDefinedLiteral::UserDefinedLiteral(unsigned NumArgs, EmptyShell Empty)
931 : CallExpr(UserDefinedLiteralClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
932
933UserDefinedLiteral *UserDefinedLiteral::Create(const ASTContext &Ctx, Expr *Fn,
934 ArrayRef<Expr *> Args,
935 QualType Ty, ExprValueKind VK,
936 SourceLocation LitEndLoc,
937 SourceLocation SuffixLoc) {
938 // Allocate storage for the trailing objects of CallExpr.
939 unsigned NumArgs = Args.size();
940 unsigned SizeOfTrailingObjects =
941 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
942 void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
943 alignof(UserDefinedLiteral));
944 return new (Mem) UserDefinedLiteral(Fn, Args, Ty, VK, LitEndLoc, SuffixLoc);
945}
946
947UserDefinedLiteral *UserDefinedLiteral::CreateEmpty(const ASTContext &Ctx,
948 unsigned NumArgs,
949 EmptyShell Empty) {
950 // Allocate storage for the trailing objects of CallExpr.
951 unsigned SizeOfTrailingObjects =
952 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
953 void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
954 alignof(UserDefinedLiteral));
955 return new (Mem) UserDefinedLiteral(NumArgs, Empty);
956}
957
Richard Smithc67fdd42012-03-07 08:35:16 +0000958UserDefinedLiteral::LiteralOperatorKind
959UserDefinedLiteral::getLiteralOperatorKind() const {
960 if (getNumArgs() == 0)
961 return LOK_Template;
962 if (getNumArgs() == 2)
963 return LOK_String;
964
965 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
966 QualType ParamTy =
967 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
968 if (ParamTy->isPointerType())
969 return LOK_Raw;
970 if (ParamTy->isAnyCharacterType())
971 return LOK_Character;
972 if (ParamTy->isIntegerType())
973 return LOK_Integer;
974 if (ParamTy->isFloatingType())
975 return LOK_Floating;
976
977 llvm_unreachable("unknown kind of literal operator");
978}
979
980Expr *UserDefinedLiteral::getCookedLiteral() {
981#ifndef NDEBUG
982 LiteralOperatorKind LOK = getLiteralOperatorKind();
983 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
984#endif
985 return getArg(0);
986}
987
988const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
989 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
990}
John McCallcf142162010-08-07 06:22:56 +0000991
Bruno Riccifd66eb82018-11-17 13:02:47 +0000992CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &Ctx, SourceLocation Loc,
Eric Fiselier708afb52019-05-16 21:04:15 +0000993 FieldDecl *Field, QualType Ty,
994 DeclContext *UsedContext)
Bruno Riccifd66eb82018-11-17 13:02:47 +0000995 : Expr(CXXDefaultInitExprClass, Ty.getNonLValueExprType(Ctx),
996 Ty->isLValueReferenceType() ? VK_LValue : Ty->isRValueReferenceType()
Richard Smith852c9db2013-04-20 22:23:05 +0000997 ? VK_XValue
998 : VK_RValue,
999 /*FIXME*/ OK_Ordinary, false, false, false, false),
Eric Fiselier708afb52019-05-16 21:04:15 +00001000 Field(Field), UsedContext(UsedContext) {
Bruno Riccifd66eb82018-11-17 13:02:47 +00001001 CXXDefaultInitExprBits.Loc = Loc;
Richard Smith852c9db2013-04-20 22:23:05 +00001002 assert(Field->hasInClassInitializer());
1003}
1004
Craig Toppera31a8822013-08-22 07:09:37 +00001005CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +00001006 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +00001007 return new (C) CXXTemporary(Destructor);
1008}
1009
Craig Toppera31a8822013-08-22 07:09:37 +00001010CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +00001011 CXXTemporary *Temp,
1012 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +00001013 assert((SubExpr->getType()->isRecordType() ||
1014 SubExpr->getType()->isArrayType()) &&
1015 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +00001016
Douglas Gregora6e053e2010-12-15 01:34:56 +00001017 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +00001018}
1019
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001020CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(
1021 CXXConstructorDecl *Cons, QualType Ty, TypeSourceInfo *TSI,
1022 ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
1023 bool HadMultipleCandidates, bool ListInitialization,
1024 bool StdInitListInitialization, bool ZeroInitialization)
1025 : CXXConstructExpr(
1026 CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(),
1027 Cons, /* Elidable=*/false, Args, HadMultipleCandidates,
1028 ListInitialization, StdInitListInitialization, ZeroInitialization,
1029 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
1030 TSI(TSI) {}
1031
1032CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(EmptyShell Empty,
1033 unsigned NumArgs)
1034 : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty, NumArgs) {}
1035
1036CXXTemporaryObjectExpr *CXXTemporaryObjectExpr::Create(
1037 const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
1038 TypeSourceInfo *TSI, ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
1039 bool HadMultipleCandidates, bool ListInitialization,
1040 bool StdInitListInitialization, bool ZeroInitialization) {
1041 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
1042 void *Mem =
1043 Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
1044 alignof(CXXTemporaryObjectExpr));
1045 return new (Mem) CXXTemporaryObjectExpr(
1046 Cons, Ty, TSI, Args, ParenOrBraceRange, HadMultipleCandidates,
1047 ListInitialization, StdInitListInitialization, ZeroInitialization);
1048}
1049
1050CXXTemporaryObjectExpr *
1051CXXTemporaryObjectExpr::CreateEmpty(const ASTContext &Ctx, unsigned NumArgs) {
1052 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
1053 void *Mem =
1054 Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
1055 alignof(CXXTemporaryObjectExpr));
1056 return new (Mem) CXXTemporaryObjectExpr(EmptyShell(), NumArgs);
1057}
Douglas Gregor2b88c112010-09-08 00:15:04 +00001058
Stephen Kelly724e9e52018-08-09 20:05:03 +00001059SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const {
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001060 return getTypeSourceInfo()->getTypeLoc().getBeginLoc();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001061}
1062
Stephen Kelly02a67ba2018-08-09 20:05:47 +00001063SourceLocation CXXTemporaryObjectExpr::getEndLoc() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +00001064 SourceLocation Loc = getParenOrBraceRange().getEnd();
1065 if (Loc.isInvalid() && getNumArgs())
Stephen Kelly1c301dc2018-08-09 21:09:38 +00001066 Loc = getArg(getNumArgs() - 1)->getEndLoc();
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +00001067 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +00001068}
Anders Carlsson6f287832009-04-21 02:22:11 +00001069
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001070CXXConstructExpr *CXXConstructExpr::Create(
1071 const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
1072 CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
1073 bool HadMultipleCandidates, bool ListInitialization,
1074 bool StdInitListInitialization, bool ZeroInitialization,
1075 ConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
1076 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
1077 void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
1078 alignof(CXXConstructExpr));
1079 return new (Mem) CXXConstructExpr(
1080 CXXConstructExprClass, Ty, Loc, Ctor, Elidable, Args,
1081 HadMultipleCandidates, ListInitialization, StdInitListInitialization,
1082 ZeroInitialization, ConstructKind, ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +00001083}
1084
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001085CXXConstructExpr *CXXConstructExpr::CreateEmpty(const ASTContext &Ctx,
1086 unsigned NumArgs) {
1087 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
1088 void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
1089 alignof(CXXConstructExpr));
1090 return new (Mem)
1091 CXXConstructExpr(CXXConstructExprClass, EmptyShell(), NumArgs);
1092}
Fangrui Song6907ce22018-07-30 19:24:48 +00001093
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001094CXXConstructExpr::CXXConstructExpr(
1095 StmtClass SC, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor,
1096 bool Elidable, ArrayRef<Expr *> Args, bool HadMultipleCandidates,
1097 bool ListInitialization, bool StdInitListInitialization,
1098 bool ZeroInitialization, ConstructionKind ConstructKind,
1099 SourceRange ParenOrBraceRange)
1100 : Expr(SC, Ty, VK_RValue, OK_Ordinary, Ty->isDependentType(),
1101 Ty->isDependentType(), Ty->isInstantiationDependentType(),
1102 Ty->containsUnexpandedParameterPack()),
1103 Constructor(Ctor), ParenOrBraceRange(ParenOrBraceRange),
1104 NumArgs(Args.size()) {
1105 CXXConstructExprBits.Elidable = Elidable;
1106 CXXConstructExprBits.HadMultipleCandidates = HadMultipleCandidates;
1107 CXXConstructExprBits.ListInitialization = ListInitialization;
1108 CXXConstructExprBits.StdInitListInitialization = StdInitListInitialization;
1109 CXXConstructExprBits.ZeroInitialization = ZeroInitialization;
1110 CXXConstructExprBits.ConstructionKind = ConstructKind;
1111 CXXConstructExprBits.Loc = Loc;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001112
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001113 Stmt **TrailingArgs = getTrailingArgs();
1114 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1115 assert(Args[I] && "NULL argument in CXXConstructExpr!");
Fangrui Song6907ce22018-07-30 19:24:48 +00001116
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001117 if (Args[I]->isValueDependent())
1118 ExprBits.ValueDependent = true;
1119 if (Args[I]->isInstantiationDependent())
1120 ExprBits.InstantiationDependent = true;
1121 if (Args[I]->containsUnexpandedParameterPack())
1122 ExprBits.ContainsUnexpandedParameterPack = true;
1123
1124 TrailingArgs[I] = Args[I];
Douglas Gregor4f4b1862009-12-16 18:50:27 +00001125 }
Anders Carlsson0781ce72009-04-23 02:32:43 +00001126}
1127
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001128CXXConstructExpr::CXXConstructExpr(StmtClass SC, EmptyShell Empty,
1129 unsigned NumArgs)
1130 : Expr(SC, Empty), NumArgs(NumArgs) {}
1131
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001132LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +00001133 LambdaCaptureKind Kind, VarDecl *Var,
1134 SourceLocation EllipsisLoc)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001135 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001136 unsigned Bits = 0;
1137 if (Implicit)
1138 Bits |= Capture_Implicit;
Fangrui Song6907ce22018-07-30 19:24:48 +00001139
Douglas Gregore31e6062012-02-07 10:09:13 +00001140 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +00001141 case LCK_StarThis:
1142 Bits |= Capture_ByCopy;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +00001143 LLVM_FALLTHROUGH;
Craig Topper36250ad2014-05-12 05:36:57 +00001144 case LCK_This:
1145 assert(!Var && "'this' capture cannot have a variable!");
John Brawn771721c2016-06-15 14:14:51 +00001146 Bits |= Capture_This;
Douglas Gregore31e6062012-02-07 10:09:13 +00001147 break;
1148
1149 case LCK_ByCopy:
1150 Bits |= Capture_ByCopy;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +00001151 LLVM_FALLTHROUGH;
Douglas Gregore31e6062012-02-07 10:09:13 +00001152 case LCK_ByRef:
1153 assert(Var && "capture must have a variable!");
1154 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +00001155 case LCK_VLAType:
1156 assert(!Var && "VLA type capture cannot have a variable!");
Alexey Bataev39c81e22014-08-28 04:28:19 +00001157 break;
Douglas Gregore31e6062012-02-07 10:09:13 +00001158 }
John Brawn771721c2016-06-15 14:14:51 +00001159 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +00001160}
1161
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001162LambdaCaptureKind LambdaCapture::getCaptureKind() const {
John Brawn771721c2016-06-15 14:14:51 +00001163 if (capturesVLAType())
Faisal Validc6b5962016-03-21 09:25:37 +00001164 return LCK_VLAType;
John Brawn771721c2016-06-15 14:14:51 +00001165 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
1166 if (capturesThis())
Faisal Validc6b5962016-03-21 09:25:37 +00001167 return CapByCopy ? LCK_StarThis : LCK_This;
Alexey Bataev39c81e22014-08-28 04:28:19 +00001168 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +00001169}
1170
James Y Knighte00a67e2015-12-31 04:18:25 +00001171LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
Douglas Gregore31e6062012-02-07 10:09:13 +00001172 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +00001173 SourceLocation CaptureDefaultLoc,
James Y Knighte00a67e2015-12-31 04:18:25 +00001174 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
1175 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
Richard Smith2589b9802012-07-25 03:56:55 +00001176 SourceLocation ClosingBrace,
1177 bool ContainsUnexpandedParameterPack)
James Y Knighte00a67e2015-12-31 04:18:25 +00001178 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
1179 T->isDependentType(), T->isDependentType(),
1180 ContainsUnexpandedParameterPack),
1181 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
1182 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
1183 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
1184 ClosingBrace(ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001185 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +00001186 CXXRecordDecl *Class = getLambdaClass();
1187 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Fangrui Song6907ce22018-07-30 19:24:48 +00001188
Douglas Gregorc8a73492012-02-13 15:44:47 +00001189 // FIXME: Propagate "has unexpanded parameter pack" bit.
Fangrui Song6907ce22018-07-30 19:24:48 +00001190
Douglas Gregore5561632012-02-13 17:20:40 +00001191 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +00001192 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +00001193 Data.NumCaptures = NumCaptures;
1194 Data.NumExplicitCaptures = 0;
James Y Knighte00a67e2015-12-31 04:18:25 +00001195 Data.Captures =
1196 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
1197 LambdaCapture *ToCapture = Data.Captures;
Douglas Gregore5561632012-02-13 17:20:40 +00001198 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
1199 if (Captures[I].isExplicit())
1200 ++Data.NumExplicitCaptures;
Fangrui Song6907ce22018-07-30 19:24:48 +00001201
Douglas Gregore5561632012-02-13 17:20:40 +00001202 *ToCapture++ = Captures[I];
1203 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001204
Douglas Gregore5561632012-02-13 17:20:40 +00001205 // Copy initialization expressions for the non-static data members.
1206 Stmt **Stored = getStoredStmts();
1207 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
1208 *Stored++ = CaptureInits[I];
Fangrui Song6907ce22018-07-30 19:24:48 +00001209
Douglas Gregore5561632012-02-13 17:20:40 +00001210 // Copy the body of the lambda.
1211 *Stored++ = getCallOperator()->getBody();
Douglas Gregore31e6062012-02-07 10:09:13 +00001212}
1213
James Y Knighte00a67e2015-12-31 04:18:25 +00001214LambdaExpr *LambdaExpr::Create(
1215 const ASTContext &Context, CXXRecordDecl *Class,
1216 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
1217 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
1218 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
James Y Knighte00a67e2015-12-31 04:18:25 +00001219 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001220 // Determine the type of the expression (i.e., the type of the
1221 // function object we're creating).
1222 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +00001223
Richard Smith30e304e2016-12-14 00:03:17 +00001224 unsigned Size = totalSizeToAlloc<Stmt *>(Captures.size() + 1);
Douglas Gregore5561632012-02-13 17:20:40 +00001225 void *Mem = Context.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +00001226 return new (Mem)
1227 LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
1228 Captures, ExplicitParams, ExplicitResultType, CaptureInits,
1229 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +00001230}
1231
Craig Toppera31a8822013-08-22 07:09:37 +00001232LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
Richard Smith30e304e2016-12-14 00:03:17 +00001233 unsigned NumCaptures) {
1234 unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001235 void *Mem = C.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +00001236 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001237}
1238
James Dennettdd2ffea22015-05-07 18:48:18 +00001239bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1240 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1241 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
1242}
1243
Douglas Gregorc8a73492012-02-13 15:44:47 +00001244LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001245 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001246}
1247
1248LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001249 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001250}
1251
James Dennett1575cb42014-05-27 19:13:04 +00001252LambdaExpr::capture_range LambdaExpr::captures() const {
1253 return capture_range(capture_begin(), capture_end());
1254}
1255
Douglas Gregorc8a73492012-02-13 15:44:47 +00001256LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1257 return capture_begin();
1258}
1259
1260LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1261 struct CXXRecordDecl::LambdaDefinitionData &Data
1262 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001263 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001264}
1265
James Dennett1575cb42014-05-27 19:13:04 +00001266LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1267 return capture_range(explicit_capture_begin(), explicit_capture_end());
1268}
1269
Douglas Gregorc8a73492012-02-13 15:44:47 +00001270LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1271 return explicit_capture_end();
1272}
1273
1274LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1275 return capture_end();
1276}
1277
James Dennett1575cb42014-05-27 19:13:04 +00001278LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1279 return capture_range(implicit_capture_begin(), implicit_capture_end());
1280}
1281
Douglas Gregore31e6062012-02-07 10:09:13 +00001282CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1283 return getType()->getAsCXXRecordDecl();
1284}
1285
1286CXXMethodDecl *LambdaExpr::getCallOperator() const {
1287 CXXRecordDecl *Record = getLambdaClass();
Fangrui Song6907ce22018-07-30 19:24:48 +00001288 return Record->getLambdaCallOperator();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001289}
1290
Erich Keane5c2c60d2019-09-30 19:12:29 +00001291FunctionTemplateDecl *LambdaExpr::getDependentCallOperator() const {
1292 CXXRecordDecl *Record = getLambdaClass();
1293 return Record->getDependentLambdaCallOperator();
1294}
1295
Faisal Vali2b391ab2013-09-26 19:54:12 +00001296TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1297 CXXRecordDecl *Record = getLambdaClass();
1298 return Record->getGenericLambdaTemplateParameterList();
Hamza Sood8205a812019-05-04 10:49:46 +00001299}
Faisal Vali2b391ab2013-09-26 19:54:12 +00001300
Hamza Sood8205a812019-05-04 10:49:46 +00001301ArrayRef<NamedDecl *> LambdaExpr::getExplicitTemplateParameters() const {
1302 const CXXRecordDecl *Record = getLambdaClass();
1303 return Record->getLambdaExplicitTemplateParameters();
Douglas Gregore31e6062012-02-07 10:09:13 +00001304}
1305
Douglas Gregor99ae8062012-02-14 17:54:36 +00001306CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +00001307 // FIXME: this mutation in getBody is bogus. It should be
1308 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1309 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +00001310 if (!getStoredStmts()[NumCaptures])
Eugene Zelenko821f6982017-11-18 01:47:41 +00001311 *const_cast<Stmt **>(&getStoredStmts()[NumCaptures]) =
James Y Knight53c76162015-07-17 18:21:37 +00001312 getCallOperator()->getBody();
1313
James Y Knighte00a67e2015-12-31 04:18:25 +00001314 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001315}
1316
Douglas Gregore31e6062012-02-07 10:09:13 +00001317bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001318 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001319}
1320
John McCall28fc7092011-11-10 05:35:25 +00001321ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001322 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001323 ArrayRef<CleanupObject> objects)
Bill Wendling7c44da22018-10-31 03:48:47 +00001324 : FullExpr(ExprWithCleanupsClass, subexpr) {
Tim Shen4a05bb82016-06-21 20:29:17 +00001325 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
John McCall28fc7092011-11-10 05:35:25 +00001326 ExprWithCleanupsBits.NumObjects = objects.size();
1327 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001328 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001329}
1330
Craig Toppera31a8822013-08-22 07:09:37 +00001331ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001332 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001333 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001334 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001335 alignof(ExprWithCleanups));
Tim Shen4a05bb82016-06-21 20:29:17 +00001336 return new (buffer)
1337 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001338}
1339
John McCall28fc7092011-11-10 05:35:25 +00001340ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
Bill Wendling7c44da22018-10-31 03:48:47 +00001341 : FullExpr(ExprWithCleanupsClass, empty) {
John McCall28fc7092011-11-10 05:35:25 +00001342 ExprWithCleanupsBits.NumObjects = numObjects;
1343}
Chris Lattnercba86142010-05-10 00:25:06 +00001344
Craig Toppera31a8822013-08-22 07:09:37 +00001345ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1346 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001347 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001348 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001349 alignof(ExprWithCleanups));
John McCall28fc7092011-11-10 05:35:25 +00001350 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001351}
1352
Bruno Ricci49ee9642019-01-07 14:27:04 +00001353CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *TSI,
1354 SourceLocation LParenLoc,
1355 ArrayRef<Expr *> Args,
1356 SourceLocation RParenLoc)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001357 : Expr(CXXUnresolvedConstructExprClass,
Bruno Ricci49ee9642019-01-07 14:27:04 +00001358 TSI->getType().getNonReferenceType(),
1359 (TSI->getType()->isLValueReferenceType()
Eugene Zelenko821f6982017-11-18 01:47:41 +00001360 ? VK_LValue
Bruno Ricci49ee9642019-01-07 14:27:04 +00001361 : TSI->getType()->isRValueReferenceType() ? VK_XValue
1362 : VK_RValue),
Eugene Zelenko821f6982017-11-18 01:47:41 +00001363 OK_Ordinary,
Bruno Ricci49ee9642019-01-07 14:27:04 +00001364 TSI->getType()->isDependentType() ||
1365 TSI->getType()->getContainedDeducedType(),
1366 true, true, TSI->getType()->containsUnexpandedParameterPack()),
1367 TSI(TSI), LParenLoc(LParenLoc), RParenLoc(RParenLoc) {
1368 CXXUnresolvedConstructExprBits.NumArgs = Args.size();
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001369 auto **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001370 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001371 if (Args[I]->containsUnexpandedParameterPack())
1372 ExprBits.ContainsUnexpandedParameterPack = true;
1373
1374 StoredArgs[I] = Args[I];
1375 }
Douglas Gregorce934142009-05-20 18:46:25 +00001376}
1377
Bruno Ricci49ee9642019-01-07 14:27:04 +00001378CXXUnresolvedConstructExpr *CXXUnresolvedConstructExpr::Create(
1379 const ASTContext &Context, TypeSourceInfo *TSI, SourceLocation LParenLoc,
1380 ArrayRef<Expr *> Args, SourceLocation RParenLoc) {
1381 void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
1382 return new (Mem) CXXUnresolvedConstructExpr(TSI, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001383}
1384
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001385CXXUnresolvedConstructExpr *
Bruno Ricci49ee9642019-01-07 14:27:04 +00001386CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &Context,
1387 unsigned NumArgs) {
1388 void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
1389 return new (Mem) CXXUnresolvedConstructExpr(EmptyShell(), NumArgs);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001390}
1391
Stephen Kelly724e9e52018-08-09 20:05:03 +00001392SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const {
Bruno Ricci49ee9642019-01-07 14:27:04 +00001393 return TSI->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001394}
1395
James Y Knighte7d82282015-12-29 18:15:14 +00001396CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001397 const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
James Y Knighte7d82282015-12-29 18:15:14 +00001398 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1399 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1400 DeclarationNameInfo MemberNameInfo,
1401 const TemplateArgumentListInfo *TemplateArgs)
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001402 : Expr(CXXDependentScopeMemberExprClass, Ctx.DependentTy, VK_LValue,
James Y Knighte7d82282015-12-29 18:15:14 +00001403 OK_Ordinary, true, true, true,
1404 ((Base && Base->containsUnexpandedParameterPack()) ||
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001405 (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
1406 ->containsUnexpandedParameterPack()) ||
James Y Knighte7d82282015-12-29 18:15:14 +00001407 MemberNameInfo.containsUnexpandedParameterPack())),
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001408 Base(Base), BaseType(BaseType), QualifierLoc(QualifierLoc),
James Y Knighte7d82282015-12-29 18:15:14 +00001409 MemberNameInfo(MemberNameInfo) {
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001410 CXXDependentScopeMemberExprBits.IsArrow = IsArrow;
1411 CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
1412 (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
1413 CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
1414 FirstQualifierFoundInScope != nullptr;
1415 CXXDependentScopeMemberExprBits.OperatorLoc = OperatorLoc;
1416
Douglas Gregora6e053e2010-12-15 01:34:56 +00001417 if (TemplateArgs) {
1418 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001419 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001420 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001421 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1422 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1423 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001424 if (ContainsUnexpandedParameterPack)
1425 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001426 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001427 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1428 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001429 }
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001430
1431 if (hasFirstQualifierFoundInScope())
1432 *getTrailingObjects<NamedDecl *>() = FirstQualifierFoundInScope;
Douglas Gregor308047d2009-09-09 00:23:06 +00001433}
1434
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001435CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1436 EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
1437 bool HasFirstQualifierFoundInScope)
1438 : Expr(CXXDependentScopeMemberExprClass, Empty) {
1439 CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
1440 HasTemplateKWAndArgsInfo;
1441 CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
1442 HasFirstQualifierFoundInScope;
1443}
1444
1445CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::Create(
1446 const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
1447 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1448 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1449 DeclarationNameInfo MemberNameInfo,
1450 const TemplateArgumentListInfo *TemplateArgs) {
1451 bool HasTemplateKWAndArgsInfo =
1452 (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001453 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001454 bool HasFirstQualifierFoundInScope = FirstQualifierFoundInScope != nullptr;
John McCall6b51f282009-11-23 01:53:49 +00001455
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001456 unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
1457 TemplateArgumentLoc, NamedDecl *>(
1458 HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
1459
1460 void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1461 return new (Mem) CXXDependentScopeMemberExpr(
1462 Ctx, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
1463 FirstQualifierFoundInScope, MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001464}
1465
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001466CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::CreateEmpty(
1467 const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
1468 unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope) {
James Y Knighte7d82282015-12-29 18:15:14 +00001469 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001470
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001471 unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
1472 TemplateArgumentLoc, NamedDecl *>(
1473 HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
Fangrui Song6907ce22018-07-30 19:24:48 +00001474
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001475 void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1476 return new (Mem) CXXDependentScopeMemberExpr(
1477 EmptyShell(), HasTemplateKWAndArgsInfo, HasFirstQualifierFoundInScope);
Douglas Gregor0da1d432011-02-28 20:01:57 +00001478}
1479
John McCall0009fcc2011-04-26 20:42:42 +00001480static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1481 UnresolvedSetIterator end) {
1482 do {
1483 NamedDecl *decl = *begin;
1484 if (isa<UnresolvedUsingValueDecl>(decl))
1485 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001486
1487 // Unresolved member expressions should only contain methods and
1488 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001489 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1490 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001491 return false;
1492 } while (++begin != end);
1493
1494 return true;
1495}
1496
Bruno Riccid7628d92019-01-09 15:43:19 +00001497UnresolvedMemberExpr::UnresolvedMemberExpr(
1498 const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
1499 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
1500 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1501 const DeclarationNameInfo &MemberNameInfo,
1502 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1503 UnresolvedSetIterator End)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001504 : OverloadExpr(
Bruno Riccid7628d92019-01-09 15:43:19 +00001505 UnresolvedMemberExprClass, Context, QualifierLoc, TemplateKWLoc,
Eugene Zelenko821f6982017-11-18 01:47:41 +00001506 MemberNameInfo, TemplateArgs, Begin, End,
1507 // Dependent
1508 ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
1509 ((Base && Base->isInstantiationDependent()) ||
1510 BaseType->isInstantiationDependentType()),
1511 // Contains unexpanded parameter pack
1512 ((Base && Base->containsUnexpandedParameterPack()) ||
1513 BaseType->containsUnexpandedParameterPack())),
Bruno Riccid7628d92019-01-09 15:43:19 +00001514 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
1515 UnresolvedMemberExprBits.IsArrow = IsArrow;
1516 UnresolvedMemberExprBits.HasUnresolvedUsing = HasUnresolvedUsing;
1517
John McCall0009fcc2011-04-26 20:42:42 +00001518 // Check whether all of the members are non-static member functions,
1519 // and if so, mark give this bound-member type instead of overload type.
1520 if (hasOnlyNonStaticMemberFunctions(Begin, End))
Bruno Riccid7628d92019-01-09 15:43:19 +00001521 setType(Context.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001522}
1523
Bruno Riccid7628d92019-01-09 15:43:19 +00001524UnresolvedMemberExpr::UnresolvedMemberExpr(EmptyShell Empty,
1525 unsigned NumResults,
1526 bool HasTemplateKWAndArgsInfo)
1527 : OverloadExpr(UnresolvedMemberExprClass, Empty, NumResults,
1528 HasTemplateKWAndArgsInfo) {}
1529
Douglas Gregor0da1d432011-02-28 20:01:57 +00001530bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001531 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001532 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001533
Douglas Gregor25b7e052011-03-02 21:06:53 +00001534 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001535}
1536
James Y Knighte7d82282015-12-29 18:15:14 +00001537UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
Bruno Riccid7628d92019-01-09 15:43:19 +00001538 const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
1539 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
James Y Knighte7d82282015-12-29 18:15:14 +00001540 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1541 const DeclarationNameInfo &MemberNameInfo,
1542 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1543 UnresolvedSetIterator End) {
Bruno Riccid7628d92019-01-09 15:43:19 +00001544 unsigned NumResults = End - Begin;
James Y Knighte7d82282015-12-29 18:15:14 +00001545 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Bruno Riccid7628d92019-01-09 15:43:19 +00001546 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1547 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
1548 TemplateArgumentLoc>(
1549 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
1550 void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
James Y Knighte7d82282015-12-29 18:15:14 +00001551 return new (Mem) UnresolvedMemberExpr(
Bruno Riccid7628d92019-01-09 15:43:19 +00001552 Context, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc,
1553 QualifierLoc, TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001554}
1555
Bruno Riccid7628d92019-01-09 15:43:19 +00001556UnresolvedMemberExpr *UnresolvedMemberExpr::CreateEmpty(
1557 const ASTContext &Context, unsigned NumResults,
1558 bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001559 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
Bruno Riccid7628d92019-01-09 15:43:19 +00001560 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
1561 TemplateArgumentLoc>(
1562 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
1563 void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
1564 return new (Mem)
1565 UnresolvedMemberExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001566}
1567
Bruno Riccid7628d92019-01-09 15:43:19 +00001568CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() {
John McCall58cc69d2010-01-27 01:50:18 +00001569 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1570
1571 // If there was a nested name specifier, it names the naming class.
1572 // It can't be dependent: after all, we were actually able to do the
1573 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001574 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001575 auto *NNS = getQualifier();
1576 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001577 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001578 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001579 Record = T->getAsCXXRecordDecl();
1580 assert(Record && "qualifier in member expression does not name record");
1581 }
John McCall58cc69d2010-01-27 01:50:18 +00001582 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001583 else {
John McCall58cc69d2010-01-27 01:50:18 +00001584 QualType BaseType = getBaseType().getNonReferenceType();
Simon Pilgrim3459a4c2019-10-07 13:58:05 +00001585 if (isArrow())
1586 BaseType = BaseType->castAs<PointerType>()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001587
Douglas Gregor9262f472010-04-27 18:19:34 +00001588 Record = BaseType->getAsCXXRecordDecl();
1589 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001590 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001591
Douglas Gregor9262f472010-04-27 18:19:34 +00001592 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001593}
1594
Richard Smithd784e682015-09-23 21:41:42 +00001595SizeOfPackExpr *
1596SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1597 NamedDecl *Pack, SourceLocation PackLoc,
1598 SourceLocation RParenLoc,
1599 Optional<unsigned> Length,
1600 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001601 void *Storage =
1602 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001603 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1604 PackLoc, RParenLoc, Length, PartialArgs);
1605}
1606
1607SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1608 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001609 void *Storage =
1610 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001611 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1612}
1613
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001614SubstNonTypeTemplateParmPackExpr::
Fangrui Song6907ce22018-07-30 19:24:48 +00001615SubstNonTypeTemplateParmPackExpr(QualType T,
Richard Smithf1f20e62018-02-14 02:07:53 +00001616 ExprValueKind ValueKind,
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001617 NonTypeTemplateParmDecl *Param,
1618 SourceLocation NameLoc,
1619 const TemplateArgument &ArgPack)
Richard Smithf1f20e62018-02-14 02:07:53 +00001620 : Expr(SubstNonTypeTemplateParmPackExprClass, T, ValueKind, OK_Ordinary,
Eugene Zelenko821f6982017-11-18 01:47:41 +00001621 true, true, true, true),
1622 Param(Param), Arguments(ArgPack.pack_begin()),
1623 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) {}
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001624
1625TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001626 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001627}
1628
Richard Smithb2997f52019-05-21 20:10:50 +00001629FunctionParmPackExpr::FunctionParmPackExpr(QualType T, VarDecl *ParamPack,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001630 SourceLocation NameLoc,
1631 unsigned NumParams,
Richard Smithb2997f52019-05-21 20:10:50 +00001632 VarDecl *const *Params)
James Y Knight48fefa32015-09-30 14:04:23 +00001633 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1634 true, true),
1635 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001636 if (Params)
1637 std::uninitialized_copy(Params, Params + NumParams,
Richard Smithb2997f52019-05-21 20:10:50 +00001638 getTrailingObjects<VarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001639}
1640
1641FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001642FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb2997f52019-05-21 20:10:50 +00001643 VarDecl *ParamPack, SourceLocation NameLoc,
1644 ArrayRef<VarDecl *> Params) {
1645 return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(Params.size())))
James Y Knighte00a67e2015-12-31 04:18:25 +00001646 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001647}
1648
1649FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001650FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1651 unsigned NumParams) {
Richard Smithb2997f52019-05-21 20:10:50 +00001652 return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(NumParams)))
James Y Knighte00a67e2015-12-31 04:18:25 +00001653 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001654}
1655
Tykerb0561b32019-11-17 11:41:55 +01001656MaterializeTemporaryExpr::MaterializeTemporaryExpr(
1657 QualType T, Expr *Temporary, bool BoundToLvalueReference,
1658 LifetimeExtendedTemporaryDecl *MTD)
1659 : Expr(MaterializeTemporaryExprClass, T,
1660 BoundToLvalueReference ? VK_LValue : VK_XValue, OK_Ordinary,
1661 Temporary->isTypeDependent(), Temporary->isValueDependent(),
1662 Temporary->isInstantiationDependent(),
1663 Temporary->containsUnexpandedParameterPack()) {
1664 if (MTD) {
1665 State = MTD;
1666 MTD->ExprWithTemporary = Temporary;
1667 return;
1668 }
1669 State = Temporary;
1670}
1671
1672void MaterializeTemporaryExpr::setExtendingDecl(ValueDecl *ExtendedBy,
David Majnemerdaff3702014-05-01 17:50:17 +00001673 unsigned ManglingNumber) {
1674 // We only need extra state if we have to remember more than just the Stmt.
1675 if (!ExtendedBy)
1676 return;
1677
1678 // We may need to allocate extra storage for the mangling number and the
1679 // extended-by ValueDecl.
Tykerb0561b32019-11-17 11:41:55 +01001680 if (!State.is<LifetimeExtendedTemporaryDecl *>())
1681 State = LifetimeExtendedTemporaryDecl::Create(
1682 cast<Expr>(State.get<Stmt *>()), ExtendedBy, ManglingNumber);
David Majnemerdaff3702014-05-01 17:50:17 +00001683
Tykerb0561b32019-11-17 11:41:55 +01001684 auto ES = State.get<LifetimeExtendedTemporaryDecl *>();
David Majnemerdaff3702014-05-01 17:50:17 +00001685 ES->ExtendingDecl = ExtendedBy;
1686 ES->ManglingNumber = ManglingNumber;
1687}
1688
Douglas Gregor29c42f22012-02-24 07:38:34 +00001689TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1690 ArrayRef<TypeSourceInfo *> Args,
1691 SourceLocation RParenLoc,
1692 bool Value)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001693 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1694 /*TypeDependent=*/false,
1695 /*ValueDependent=*/false,
1696 /*InstantiationDependent=*/false,
1697 /*ContainsUnexpandedParameterPack=*/false),
1698 Loc(Loc), RParenLoc(RParenLoc) {
Douglas Gregor29c42f22012-02-24 07:38:34 +00001699 TypeTraitExprBits.Kind = Kind;
1700 TypeTraitExprBits.Value = Value;
1701 TypeTraitExprBits.NumArgs = Args.size();
1702
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001703 auto **ToArgs = getTrailingObjects<TypeSourceInfo *>();
James Y Knighte00a67e2015-12-31 04:18:25 +00001704
Douglas Gregor29c42f22012-02-24 07:38:34 +00001705 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1706 if (Args[I]->getType()->isDependentType())
1707 setValueDependent(true);
1708 if (Args[I]->getType()->isInstantiationDependentType())
1709 setInstantiationDependent(true);
1710 if (Args[I]->getType()->containsUnexpandedParameterPack())
1711 setContainsUnexpandedParameterPack(true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001712
Douglas Gregor29c42f22012-02-24 07:38:34 +00001713 ToArgs[I] = Args[I];
1714 }
1715}
1716
Craig Toppera31a8822013-08-22 07:09:37 +00001717TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Fangrui Song6907ce22018-07-30 19:24:48 +00001718 SourceLocation Loc,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001719 TypeTrait Kind,
1720 ArrayRef<TypeSourceInfo *> Args,
1721 SourceLocation RParenLoc,
1722 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001723 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001724 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1725}
1726
Craig Toppera31a8822013-08-22 07:09:37 +00001727TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001728 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001729 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001730 return new (Mem) TypeTraitExpr(EmptyShell());
1731}
Bruno Riccic5885cf2018-12-21 15:20:32 +00001732
1733CUDAKernelCallExpr::CUDAKernelCallExpr(Expr *Fn, CallExpr *Config,
1734 ArrayRef<Expr *> Args, QualType Ty,
1735 ExprValueKind VK, SourceLocation RP,
1736 unsigned MinNumArgs)
1737 : CallExpr(CUDAKernelCallExprClass, Fn, /*PreArgs=*/Config, Args, Ty, VK,
1738 RP, MinNumArgs, NotADL) {}
1739
1740CUDAKernelCallExpr::CUDAKernelCallExpr(unsigned NumArgs, EmptyShell Empty)
1741 : CallExpr(CUDAKernelCallExprClass, /*NumPreArgs=*/END_PREARG, NumArgs,
1742 Empty) {}
1743
1744CUDAKernelCallExpr *
1745CUDAKernelCallExpr::Create(const ASTContext &Ctx, Expr *Fn, CallExpr *Config,
1746 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
1747 SourceLocation RP, unsigned MinNumArgs) {
1748 // Allocate storage for the trailing objects of CallExpr.
1749 unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
1750 unsigned SizeOfTrailingObjects =
1751 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/END_PREARG, NumArgs);
1752 void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1753 alignof(CUDAKernelCallExpr));
1754 return new (Mem) CUDAKernelCallExpr(Fn, Config, Args, Ty, VK, RP, MinNumArgs);
1755}
1756
1757CUDAKernelCallExpr *CUDAKernelCallExpr::CreateEmpty(const ASTContext &Ctx,
1758 unsigned NumArgs,
1759 EmptyShell Empty) {
1760 // Allocate storage for the trailing objects of CallExpr.
1761 unsigned SizeOfTrailingObjects =
1762 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/END_PREARG, NumArgs);
1763 void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1764 alignof(CUDAKernelCallExpr));
1765 return new (Mem) CUDAKernelCallExpr(NumArgs, Empty);
1766}
Saar Raz5d98ba62019-10-15 15:24:26 +00001767
1768ConceptSpecializationExpr::ConceptSpecializationExpr(ASTContext &C,
1769 NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc,
1770 SourceLocation ConceptNameLoc, NamedDecl *FoundDecl,
1771 ConceptDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten,
Saar Razfdf80e82019-12-06 01:30:21 +02001772 ArrayRef<TemplateArgument> ConvertedArgs,
1773 const ConstraintSatisfaction *Satisfaction)
Saar Raz5d98ba62019-10-15 15:24:26 +00001774 : Expr(ConceptSpecializationExprClass, C.BoolTy, VK_RValue, OK_Ordinary,
1775 /*TypeDependent=*/false,
1776 // All the flags below are set in setTemplateArguments.
Saar Razfdf80e82019-12-06 01:30:21 +02001777 /*ValueDependent=*/!Satisfaction, /*InstantiationDependent=*/false,
Saar Raz5d98ba62019-10-15 15:24:26 +00001778 /*ContainsUnexpandedParameterPacks=*/false),
1779 NestedNameSpec(NNS), TemplateKWLoc(TemplateKWLoc),
1780 ConceptNameLoc(ConceptNameLoc), FoundDecl(FoundDecl),
Saar Razfdf80e82019-12-06 01:30:21 +02001781 NamedConcept(NamedConcept), NumTemplateArgs(ConvertedArgs.size()),
1782 Satisfaction(Satisfaction ?
1783 ASTConstraintSatisfaction::Create(C, *Satisfaction) :
1784 nullptr) {
Saar Raz5d98ba62019-10-15 15:24:26 +00001785 setTemplateArguments(ArgsAsWritten, ConvertedArgs);
1786}
1787
1788ConceptSpecializationExpr::ConceptSpecializationExpr(EmptyShell Empty,
1789 unsigned NumTemplateArgs)
1790 : Expr(ConceptSpecializationExprClass, Empty),
1791 NumTemplateArgs(NumTemplateArgs) { }
1792
1793void ConceptSpecializationExpr::setTemplateArguments(
1794 const ASTTemplateArgumentListInfo *ArgsAsWritten,
1795 ArrayRef<TemplateArgument> Converted) {
1796 assert(Converted.size() == NumTemplateArgs);
1797 assert(!this->ArgsAsWritten && "setTemplateArguments can only be used once");
1798 this->ArgsAsWritten = ArgsAsWritten;
1799 std::uninitialized_copy(Converted.begin(), Converted.end(),
1800 getTrailingObjects<TemplateArgument>());
1801 bool IsInstantiationDependent = false;
1802 bool ContainsUnexpandedParameterPack = false;
1803 for (const TemplateArgumentLoc& LocInfo : ArgsAsWritten->arguments()) {
1804 if (LocInfo.getArgument().isInstantiationDependent())
1805 IsInstantiationDependent = true;
1806 if (LocInfo.getArgument().containsUnexpandedParameterPack())
1807 ContainsUnexpandedParameterPack = true;
1808 if (ContainsUnexpandedParameterPack && IsInstantiationDependent)
1809 break;
1810 }
1811
1812 // Currently guaranteed by the fact concepts can only be at namespace-scope.
1813 assert(!NestedNameSpec ||
1814 (!NestedNameSpec.getNestedNameSpecifier()->isInstantiationDependent() &&
1815 !NestedNameSpec.getNestedNameSpecifier()
1816 ->containsUnexpandedParameterPack()));
1817 setInstantiationDependent(IsInstantiationDependent);
1818 setContainsUnexpandedParameterPack(ContainsUnexpandedParameterPack);
1819 assert((!isValueDependent() || isInstantiationDependent()) &&
1820 "should not be value-dependent");
1821}
1822
1823ConceptSpecializationExpr *
1824ConceptSpecializationExpr::Create(ASTContext &C, NestedNameSpecifierLoc NNS,
1825 SourceLocation TemplateKWLoc,
1826 SourceLocation ConceptNameLoc,
1827 NamedDecl *FoundDecl,
1828 ConceptDecl *NamedConcept,
1829 const ASTTemplateArgumentListInfo *ArgsAsWritten,
1830 ArrayRef<TemplateArgument> ConvertedArgs,
Saar Razfdf80e82019-12-06 01:30:21 +02001831 const ConstraintSatisfaction *Satisfaction) {
Saar Raz5d98ba62019-10-15 15:24:26 +00001832 void *Buffer = C.Allocate(totalSizeToAlloc<TemplateArgument>(
1833 ConvertedArgs.size()));
1834 return new (Buffer) ConceptSpecializationExpr(C, NNS, TemplateKWLoc,
1835 ConceptNameLoc, FoundDecl,
1836 NamedConcept, ArgsAsWritten,
Saar Razfdf80e82019-12-06 01:30:21 +02001837 ConvertedArgs, Satisfaction);
Saar Raz5d98ba62019-10-15 15:24:26 +00001838}
1839
1840ConceptSpecializationExpr *
1841ConceptSpecializationExpr::Create(ASTContext &C, EmptyShell Empty,
1842 unsigned NumTemplateArgs) {
1843 void *Buffer = C.Allocate(totalSizeToAlloc<TemplateArgument>(
1844 NumTemplateArgs));
1845 return new (Buffer) ConceptSpecializationExpr(Empty, NumTemplateArgs);
Saar Raz84423bd2019-10-15 15:49:29 +00001846}