xref: /llvm-project/clang/lib/AST/ExprCXX.cpp (revision abc8812df02599fc413d9ed77b992f8236ed2af9)
1 //===- ExprCXX.cpp - (C++) Expression AST Node Implementation -------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the subclesses of Expr class declared in ExprCXX.h
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/AST/ExprCXX.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/ComputeDependence.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclAccessPair.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/DeclarationName.h"
23 #include "clang/AST/DependenceFlags.h"
24 #include "clang/AST/Expr.h"
25 #include "clang/AST/LambdaCapture.h"
26 #include "clang/AST/NestedNameSpecifier.h"
27 #include "clang/AST/TemplateBase.h"
28 #include "clang/AST/Type.h"
29 #include "clang/AST/TypeLoc.h"
30 #include "clang/Basic/LLVM.h"
31 #include "clang/Basic/OperatorKinds.h"
32 #include "clang/Basic/SourceLocation.h"
33 #include "clang/Basic/Specifiers.h"
34 #include "llvm/ADT/ArrayRef.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include <cassert>
37 #include <cstddef>
38 #include <cstring>
39 #include <memory>
40 #include <optional>
41 
42 using namespace clang;
43 
44 //===----------------------------------------------------------------------===//
45 //  Child Iterators for iterating over subexpressions/substatements
46 //===----------------------------------------------------------------------===//
47 
48 bool CXXOperatorCallExpr::isInfixBinaryOp() const {
49   // An infix binary operator is any operator with two arguments other than
50   // operator() and operator[]. Note that none of these operators can have
51   // default arguments, so it suffices to check the number of argument
52   // expressions.
53   if (getNumArgs() != 2)
54     return false;
55 
56   switch (getOperator()) {
57   case OO_Call: case OO_Subscript:
58     return false;
59   default:
60     return true;
61   }
62 }
63 
64 CXXRewrittenBinaryOperator::DecomposedForm
65 CXXRewrittenBinaryOperator::getDecomposedForm() const {
66   DecomposedForm Result = {};
67   const Expr *E = getSemanticForm()->IgnoreImplicit();
68 
69   // Remove an outer '!' if it exists (only happens for a '!=' rewrite).
70   bool SkippedNot = false;
71   if (auto *NotEq = dyn_cast<UnaryOperator>(E)) {
72     assert(NotEq->getOpcode() == UO_LNot);
73     E = NotEq->getSubExpr()->IgnoreImplicit();
74     SkippedNot = true;
75   }
76 
77   // Decompose the outer binary operator.
78   if (auto *BO = dyn_cast<BinaryOperator>(E)) {
79     assert(!SkippedNot || BO->getOpcode() == BO_EQ);
80     Result.Opcode = SkippedNot ? BO_NE : BO->getOpcode();
81     Result.LHS = BO->getLHS();
82     Result.RHS = BO->getRHS();
83     Result.InnerBinOp = BO;
84   } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
85     assert(!SkippedNot || BO->getOperator() == OO_EqualEqual);
86     assert(BO->isInfixBinaryOp());
87     switch (BO->getOperator()) {
88     case OO_Less: Result.Opcode = BO_LT; break;
89     case OO_LessEqual: Result.Opcode = BO_LE; break;
90     case OO_Greater: Result.Opcode = BO_GT; break;
91     case OO_GreaterEqual: Result.Opcode = BO_GE; break;
92     case OO_Spaceship: Result.Opcode = BO_Cmp; break;
93     case OO_EqualEqual: Result.Opcode = SkippedNot ? BO_NE : BO_EQ; break;
94     default: llvm_unreachable("unexpected binop in rewritten operator expr");
95     }
96     Result.LHS = BO->getArg(0);
97     Result.RHS = BO->getArg(1);
98     Result.InnerBinOp = BO;
99   } else {
100     llvm_unreachable("unexpected rewritten operator form");
101   }
102 
103   // Put the operands in the right order for == and !=, and canonicalize the
104   // <=> subexpression onto the LHS for all other forms.
105   if (isReversed())
106     std::swap(Result.LHS, Result.RHS);
107 
108   // If this isn't a spaceship rewrite, we're done.
109   if (Result.Opcode == BO_EQ || Result.Opcode == BO_NE)
110     return Result;
111 
112   // Otherwise, we expect a <=> to now be on the LHS.
113   E = Result.LHS->IgnoreUnlessSpelledInSource();
114   if (auto *BO = dyn_cast<BinaryOperator>(E)) {
115     assert(BO->getOpcode() == BO_Cmp);
116     Result.LHS = BO->getLHS();
117     Result.RHS = BO->getRHS();
118     Result.InnerBinOp = BO;
119   } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
120     assert(BO->getOperator() == OO_Spaceship);
121     Result.LHS = BO->getArg(0);
122     Result.RHS = BO->getArg(1);
123     Result.InnerBinOp = BO;
124   } else {
125     llvm_unreachable("unexpected rewritten operator form");
126   }
127 
128   // Put the comparison operands in the right order.
129   if (isReversed())
130     std::swap(Result.LHS, Result.RHS);
131   return Result;
132 }
133 
134 bool CXXTypeidExpr::isPotentiallyEvaluated() const {
135   if (isTypeOperand())
136     return false;
137 
138   // C++11 [expr.typeid]p3:
139   //   When typeid is applied to an expression other than a glvalue of
140   //   polymorphic class type, [...] the expression is an unevaluated operand.
141   const Expr *E = getExprOperand();
142   if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
143     if (RD->isPolymorphic() && E->isGLValue())
144       return true;
145 
146   return false;
147 }
148 
149 bool CXXTypeidExpr::isMostDerived(const ASTContext &Context) const {
150   assert(!isTypeOperand() && "Cannot call isMostDerived for typeid(type)");
151   const Expr *E = getExprOperand()->IgnoreParenNoopCasts(Context);
152   if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
153     QualType Ty = DRE->getDecl()->getType();
154     if (!Ty->isPointerOrReferenceType())
155       return true;
156   }
157 
158   return false;
159 }
160 
161 QualType CXXTypeidExpr::getTypeOperand(const ASTContext &Context) const {
162   assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
163   Qualifiers Quals;
164   return Context.getUnqualifiedArrayType(
165       cast<TypeSourceInfo *>(Operand)->getType().getNonReferenceType(), Quals);
166 }
167 
168 static bool isGLValueFromPointerDeref(const Expr *E) {
169   E = E->IgnoreParens();
170 
171   if (const auto *CE = dyn_cast<CastExpr>(E)) {
172     if (!CE->getSubExpr()->isGLValue())
173       return false;
174     return isGLValueFromPointerDeref(CE->getSubExpr());
175   }
176 
177   if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E))
178     return isGLValueFromPointerDeref(OVE->getSourceExpr());
179 
180   if (const auto *BO = dyn_cast<BinaryOperator>(E))
181     if (BO->getOpcode() == BO_Comma)
182       return isGLValueFromPointerDeref(BO->getRHS());
183 
184   if (const auto *ACO = dyn_cast<AbstractConditionalOperator>(E))
185     return isGLValueFromPointerDeref(ACO->getTrueExpr()) ||
186            isGLValueFromPointerDeref(ACO->getFalseExpr());
187 
188   // C++11 [expr.sub]p1:
189   //   The expression E1[E2] is identical (by definition) to *((E1)+(E2))
190   if (isa<ArraySubscriptExpr>(E))
191     return true;
192 
193   if (const auto *UO = dyn_cast<UnaryOperator>(E))
194     if (UO->getOpcode() == UO_Deref)
195       return true;
196 
197   return false;
198 }
199 
200 bool CXXTypeidExpr::hasNullCheck() const {
201   if (!isPotentiallyEvaluated())
202     return false;
203 
204   // C++ [expr.typeid]p2:
205   //   If the glvalue expression is obtained by applying the unary * operator to
206   //   a pointer and the pointer is a null pointer value, the typeid expression
207   //   throws the std::bad_typeid exception.
208   //
209   // However, this paragraph's intent is not clear.  We choose a very generous
210   // interpretation which implores us to consider comma operators, conditional
211   // operators, parentheses and other such constructs.
212   return isGLValueFromPointerDeref(getExprOperand());
213 }
214 
215 QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
216   assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
217   Qualifiers Quals;
218   return Context.getUnqualifiedArrayType(
219       cast<TypeSourceInfo *>(Operand)->getType().getNonReferenceType(), Quals);
220 }
221 
222 // CXXScalarValueInitExpr
223 SourceLocation CXXScalarValueInitExpr::getBeginLoc() const {
224   return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : getRParenLoc();
225 }
226 
227 // CXXNewExpr
228 CXXNewExpr::CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
229                        FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
230                        bool UsualArrayDeleteWantsSize,
231                        ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
232                        std::optional<Expr *> ArraySize,
233                        CXXNewInitializationStyle InitializationStyle,
234                        Expr *Initializer, QualType Ty,
235                        TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
236                        SourceRange DirectInitRange)
237     : Expr(CXXNewExprClass, Ty, VK_PRValue, OK_Ordinary),
238       OperatorNew(OperatorNew), OperatorDelete(OperatorDelete),
239       AllocatedTypeInfo(AllocatedTypeInfo), Range(Range),
240       DirectInitRange(DirectInitRange) {
241 
242   assert((Initializer != nullptr ||
243           InitializationStyle == CXXNewInitializationStyle::None) &&
244          "Only CXXNewInitializationStyle::None can have no initializer!");
245 
246   CXXNewExprBits.IsGlobalNew = IsGlobalNew;
247   CXXNewExprBits.IsArray = ArraySize.has_value();
248   CXXNewExprBits.ShouldPassAlignment = ShouldPassAlignment;
249   CXXNewExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
250   CXXNewExprBits.HasInitializer = Initializer != nullptr;
251   CXXNewExprBits.StoredInitializationStyle =
252       llvm::to_underlying(InitializationStyle);
253   bool IsParenTypeId = TypeIdParens.isValid();
254   CXXNewExprBits.IsParenTypeId = IsParenTypeId;
255   CXXNewExprBits.NumPlacementArgs = PlacementArgs.size();
256 
257   if (ArraySize)
258     getTrailingObjects<Stmt *>()[arraySizeOffset()] = *ArraySize;
259   if (Initializer)
260     getTrailingObjects<Stmt *>()[initExprOffset()] = Initializer;
261   for (unsigned I = 0; I != PlacementArgs.size(); ++I)
262     getTrailingObjects<Stmt *>()[placementNewArgsOffset() + I] =
263         PlacementArgs[I];
264   if (IsParenTypeId)
265     getTrailingObjects<SourceRange>()[0] = TypeIdParens;
266 
267   switch (getInitializationStyle()) {
268   case CXXNewInitializationStyle::Parens:
269     this->Range.setEnd(DirectInitRange.getEnd());
270     break;
271   case CXXNewInitializationStyle::Braces:
272     this->Range.setEnd(getInitializer()->getSourceRange().getEnd());
273     break;
274   default:
275     if (IsParenTypeId)
276       this->Range.setEnd(TypeIdParens.getEnd());
277     break;
278   }
279 
280   setDependence(computeDependence(this));
281 }
282 
283 CXXNewExpr::CXXNewExpr(EmptyShell Empty, bool IsArray,
284                        unsigned NumPlacementArgs, bool IsParenTypeId)
285     : Expr(CXXNewExprClass, Empty) {
286   CXXNewExprBits.IsArray = IsArray;
287   CXXNewExprBits.NumPlacementArgs = NumPlacementArgs;
288   CXXNewExprBits.IsParenTypeId = IsParenTypeId;
289 }
290 
291 CXXNewExpr *CXXNewExpr::Create(
292     const ASTContext &Ctx, bool IsGlobalNew, FunctionDecl *OperatorNew,
293     FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
294     bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
295     SourceRange TypeIdParens, std::optional<Expr *> ArraySize,
296     CXXNewInitializationStyle InitializationStyle, Expr *Initializer,
297     QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
298     SourceRange DirectInitRange) {
299   bool IsArray = ArraySize.has_value();
300   bool HasInit = Initializer != nullptr;
301   unsigned NumPlacementArgs = PlacementArgs.size();
302   bool IsParenTypeId = TypeIdParens.isValid();
303   void *Mem =
304       Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
305                        IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
306                    alignof(CXXNewExpr));
307   return new (Mem)
308       CXXNewExpr(IsGlobalNew, OperatorNew, OperatorDelete, ShouldPassAlignment,
309                  UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens,
310                  ArraySize, InitializationStyle, Initializer, Ty,
311                  AllocatedTypeInfo, Range, DirectInitRange);
312 }
313 
314 CXXNewExpr *CXXNewExpr::CreateEmpty(const ASTContext &Ctx, bool IsArray,
315                                     bool HasInit, unsigned NumPlacementArgs,
316                                     bool IsParenTypeId) {
317   void *Mem =
318       Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
319                        IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
320                    alignof(CXXNewExpr));
321   return new (Mem)
322       CXXNewExpr(EmptyShell(), IsArray, NumPlacementArgs, IsParenTypeId);
323 }
324 
325 bool CXXNewExpr::shouldNullCheckAllocation() const {
326   if (getOperatorNew()->getLangOpts().CheckNew)
327     return true;
328   return !getOperatorNew()->hasAttr<ReturnsNonNullAttr>() &&
329          getOperatorNew()
330              ->getType()
331              ->castAs<FunctionProtoType>()
332              ->isNothrow() &&
333          !getOperatorNew()->isReservedGlobalPlacementOperator();
334 }
335 
336 // CXXDeleteExpr
337 QualType CXXDeleteExpr::getDestroyedType() const {
338   const Expr *Arg = getArgument();
339 
340   // For a destroying operator delete, we may have implicitly converted the
341   // pointer type to the type of the parameter of the 'operator delete'
342   // function.
343   while (const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
344     if (ICE->getCastKind() == CK_DerivedToBase ||
345         ICE->getCastKind() == CK_UncheckedDerivedToBase ||
346         ICE->getCastKind() == CK_NoOp) {
347       assert((ICE->getCastKind() == CK_NoOp ||
348               getOperatorDelete()->isDestroyingOperatorDelete()) &&
349              "only a destroying operator delete can have a converted arg");
350       Arg = ICE->getSubExpr();
351     } else
352       break;
353   }
354 
355   // The type-to-delete may not be a pointer if it's a dependent type.
356   const QualType ArgType = Arg->getType();
357 
358   if (ArgType->isDependentType() && !ArgType->isPointerType())
359     return QualType();
360 
361   return ArgType->castAs<PointerType>()->getPointeeType();
362 }
363 
364 // CXXPseudoDestructorExpr
365 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
366     : Type(Info) {
367   Location = Info->getTypeLoc().getBeginLoc();
368 }
369 
370 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(
371     const ASTContext &Context, Expr *Base, bool isArrow,
372     SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
373     TypeSourceInfo *ScopeType, SourceLocation ColonColonLoc,
374     SourceLocation TildeLoc, PseudoDestructorTypeStorage DestroyedType)
375     : Expr(CXXPseudoDestructorExprClass, Context.BoundMemberTy, VK_PRValue,
376            OK_Ordinary),
377       Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
378       OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
379       ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
380       DestroyedType(DestroyedType) {
381   setDependence(computeDependence(this));
382 }
383 
384 QualType CXXPseudoDestructorExpr::getDestroyedType() const {
385   if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
386     return TInfo->getType();
387 
388   return QualType();
389 }
390 
391 SourceLocation CXXPseudoDestructorExpr::getEndLoc() const {
392   SourceLocation End = DestroyedType.getLocation();
393   if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
394     End = TInfo->getTypeLoc().getSourceRange().getEnd();
395   return End;
396 }
397 
398 // UnresolvedLookupExpr
399 UnresolvedLookupExpr::UnresolvedLookupExpr(
400     const ASTContext &Context, CXXRecordDecl *NamingClass,
401     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
402     const DeclarationNameInfo &NameInfo, bool RequiresADL,
403     const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
404     UnresolvedSetIterator End, bool KnownDependent,
405     bool KnownInstantiationDependent)
406     : OverloadExpr(UnresolvedLookupExprClass, Context, QualifierLoc,
407                    TemplateKWLoc, NameInfo, TemplateArgs, Begin, End,
408                    KnownDependent, KnownInstantiationDependent, false),
409       NamingClass(NamingClass) {
410   UnresolvedLookupExprBits.RequiresADL = RequiresADL;
411 }
412 
413 UnresolvedLookupExpr::UnresolvedLookupExpr(EmptyShell Empty,
414                                            unsigned NumResults,
415                                            bool HasTemplateKWAndArgsInfo)
416     : OverloadExpr(UnresolvedLookupExprClass, Empty, NumResults,
417                    HasTemplateKWAndArgsInfo) {}
418 
419 UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
420     const ASTContext &Context, CXXRecordDecl *NamingClass,
421     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
422     bool RequiresADL, UnresolvedSetIterator Begin, UnresolvedSetIterator End,
423     bool KnownDependent, bool KnownInstantiationDependent) {
424   unsigned NumResults = End - Begin;
425   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
426                                    TemplateArgumentLoc>(NumResults, 0, 0);
427   void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
428   return new (Mem) UnresolvedLookupExpr(
429       Context, NamingClass, QualifierLoc,
430       /*TemplateKWLoc=*/SourceLocation(), NameInfo, RequiresADL,
431       /*TemplateArgs=*/nullptr, Begin, End, KnownDependent,
432       KnownInstantiationDependent);
433 }
434 
435 UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
436     const ASTContext &Context, CXXRecordDecl *NamingClass,
437     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
438     const DeclarationNameInfo &NameInfo, bool RequiresADL,
439     const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
440     UnresolvedSetIterator End, bool KnownDependent,
441     bool KnownInstantiationDependent) {
442   unsigned NumResults = End - Begin;
443   bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
444   unsigned NumTemplateArgs = Args ? Args->size() : 0;
445   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
446                                    TemplateArgumentLoc>(
447       NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
448   void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
449   return new (Mem) UnresolvedLookupExpr(
450       Context, NamingClass, QualifierLoc, TemplateKWLoc, NameInfo, RequiresADL,
451       Args, Begin, End, KnownDependent, KnownInstantiationDependent);
452 }
453 
454 UnresolvedLookupExpr *UnresolvedLookupExpr::CreateEmpty(
455     const ASTContext &Context, unsigned NumResults,
456     bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
457   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
458   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
459                                    TemplateArgumentLoc>(
460       NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
461   void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
462   return new (Mem)
463       UnresolvedLookupExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
464 }
465 
466 OverloadExpr::OverloadExpr(StmtClass SC, const ASTContext &Context,
467                            NestedNameSpecifierLoc QualifierLoc,
468                            SourceLocation TemplateKWLoc,
469                            const DeclarationNameInfo &NameInfo,
470                            const TemplateArgumentListInfo *TemplateArgs,
471                            UnresolvedSetIterator Begin,
472                            UnresolvedSetIterator End, bool KnownDependent,
473                            bool KnownInstantiationDependent,
474                            bool KnownContainsUnexpandedParameterPack)
475     : Expr(SC, Context.OverloadTy, VK_LValue, OK_Ordinary), NameInfo(NameInfo),
476       QualifierLoc(QualifierLoc) {
477   unsigned NumResults = End - Begin;
478   OverloadExprBits.NumResults = NumResults;
479   OverloadExprBits.HasTemplateKWAndArgsInfo =
480       (TemplateArgs != nullptr ) || TemplateKWLoc.isValid();
481 
482   if (NumResults) {
483     // Copy the results to the trailing array past UnresolvedLookupExpr
484     // or UnresolvedMemberExpr.
485     DeclAccessPair *Results = getTrailingResults();
486     memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
487   }
488 
489   if (TemplateArgs) {
490     auto Deps = TemplateArgumentDependence::None;
491     getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
492         TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(), Deps);
493   } else if (TemplateKWLoc.isValid()) {
494     getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
495   }
496 
497   setDependence(computeDependence(this, KnownDependent,
498                                   KnownInstantiationDependent,
499                                   KnownContainsUnexpandedParameterPack));
500   if (isTypeDependent())
501     setType(Context.DependentTy);
502 }
503 
504 OverloadExpr::OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
505                            bool HasTemplateKWAndArgsInfo)
506     : Expr(SC, Empty) {
507   OverloadExprBits.NumResults = NumResults;
508   OverloadExprBits.HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
509 }
510 
511 // DependentScopeDeclRefExpr
512 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(
513     QualType Ty, NestedNameSpecifierLoc QualifierLoc,
514     SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
515     const TemplateArgumentListInfo *Args)
516     : Expr(DependentScopeDeclRefExprClass, Ty, VK_LValue, OK_Ordinary),
517       QualifierLoc(QualifierLoc), NameInfo(NameInfo) {
518   DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
519       (Args != nullptr) || TemplateKWLoc.isValid();
520   if (Args) {
521     auto Deps = TemplateArgumentDependence::None;
522     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
523         TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(), Deps);
524   } else if (TemplateKWLoc.isValid()) {
525     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
526         TemplateKWLoc);
527   }
528   setDependence(computeDependence(this));
529 }
530 
531 DependentScopeDeclRefExpr *DependentScopeDeclRefExpr::Create(
532     const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
533     SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
534     const TemplateArgumentListInfo *Args) {
535   assert(QualifierLoc && "should be created for dependent qualifiers");
536   bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
537   std::size_t Size =
538       totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
539           HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
540   void *Mem = Context.Allocate(Size);
541   return new (Mem) DependentScopeDeclRefExpr(Context.DependentTy, QualifierLoc,
542                                              TemplateKWLoc, NameInfo, Args);
543 }
544 
545 DependentScopeDeclRefExpr *
546 DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &Context,
547                                        bool HasTemplateKWAndArgsInfo,
548                                        unsigned NumTemplateArgs) {
549   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
550   std::size_t Size =
551       totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
552           HasTemplateKWAndArgsInfo, NumTemplateArgs);
553   void *Mem = Context.Allocate(Size);
554   auto *E = new (Mem) DependentScopeDeclRefExpr(
555       QualType(), NestedNameSpecifierLoc(), SourceLocation(),
556       DeclarationNameInfo(), nullptr);
557   E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
558       HasTemplateKWAndArgsInfo;
559   return E;
560 }
561 
562 SourceLocation CXXConstructExpr::getBeginLoc() const {
563   if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(this))
564     return TOE->getBeginLoc();
565   return getLocation();
566 }
567 
568 SourceLocation CXXConstructExpr::getEndLoc() const {
569   if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(this))
570     return TOE->getEndLoc();
571 
572   if (ParenOrBraceRange.isValid())
573     return ParenOrBraceRange.getEnd();
574 
575   SourceLocation End = getLocation();
576   for (unsigned I = getNumArgs(); I > 0; --I) {
577     const Expr *Arg = getArg(I-1);
578     if (!Arg->isDefaultArgument()) {
579       SourceLocation NewEnd = Arg->getEndLoc();
580       if (NewEnd.isValid()) {
581         End = NewEnd;
582         break;
583       }
584     }
585   }
586 
587   return End;
588 }
589 
590 CXXOperatorCallExpr::CXXOperatorCallExpr(OverloadedOperatorKind OpKind,
591                                          Expr *Fn, ArrayRef<Expr *> Args,
592                                          QualType Ty, ExprValueKind VK,
593                                          SourceLocation OperatorLoc,
594                                          FPOptionsOverride FPFeatures,
595                                          ADLCallKind UsesADL)
596     : CallExpr(CXXOperatorCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
597                OperatorLoc, FPFeatures, /*MinNumArgs=*/0, UsesADL) {
598   CXXOperatorCallExprBits.OperatorKind = OpKind;
599   assert(
600       (CXXOperatorCallExprBits.OperatorKind == static_cast<unsigned>(OpKind)) &&
601       "OperatorKind overflow!");
602   Range = getSourceRangeImpl();
603 }
604 
605 CXXOperatorCallExpr::CXXOperatorCallExpr(unsigned NumArgs, bool HasFPFeatures,
606                                          EmptyShell Empty)
607     : CallExpr(CXXOperatorCallExprClass, /*NumPreArgs=*/0, NumArgs,
608                HasFPFeatures, Empty) {}
609 
610 CXXOperatorCallExpr *
611 CXXOperatorCallExpr::Create(const ASTContext &Ctx,
612                             OverloadedOperatorKind OpKind, Expr *Fn,
613                             ArrayRef<Expr *> Args, QualType Ty,
614                             ExprValueKind VK, SourceLocation OperatorLoc,
615                             FPOptionsOverride FPFeatures, ADLCallKind UsesADL) {
616   // Allocate storage for the trailing objects of CallExpr.
617   unsigned NumArgs = Args.size();
618   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
619       /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
620   void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
621                            alignof(CXXOperatorCallExpr));
622   return new (Mem) CXXOperatorCallExpr(OpKind, Fn, Args, Ty, VK, OperatorLoc,
623                                        FPFeatures, UsesADL);
624 }
625 
626 CXXOperatorCallExpr *CXXOperatorCallExpr::CreateEmpty(const ASTContext &Ctx,
627                                                       unsigned NumArgs,
628                                                       bool HasFPFeatures,
629                                                       EmptyShell Empty) {
630   // Allocate storage for the trailing objects of CallExpr.
631   unsigned SizeOfTrailingObjects =
632       CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPFeatures);
633   void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
634                            alignof(CXXOperatorCallExpr));
635   return new (Mem) CXXOperatorCallExpr(NumArgs, HasFPFeatures, Empty);
636 }
637 
638 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
639   OverloadedOperatorKind Kind = getOperator();
640   if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
641     if (getNumArgs() == 1)
642       // Prefix operator
643       return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
644     else
645       // Postfix operator
646       return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
647   } else if (Kind == OO_Arrow) {
648     return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
649   } else if (Kind == OO_Call) {
650     return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
651   } else if (Kind == OO_Subscript) {
652     return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
653   } else if (getNumArgs() == 1) {
654     return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
655   } else if (getNumArgs() == 2) {
656     return SourceRange(getArg(0)->getBeginLoc(), getArg(1)->getEndLoc());
657   } else {
658     return getOperatorLoc();
659   }
660 }
661 
662 CXXMemberCallExpr::CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args,
663                                      QualType Ty, ExprValueKind VK,
664                                      SourceLocation RP,
665                                      FPOptionsOverride FPOptions,
666                                      unsigned MinNumArgs)
667     : CallExpr(CXXMemberCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, RP,
668                FPOptions, MinNumArgs, NotADL) {}
669 
670 CXXMemberCallExpr::CXXMemberCallExpr(unsigned NumArgs, bool HasFPFeatures,
671                                      EmptyShell Empty)
672     : CallExpr(CXXMemberCallExprClass, /*NumPreArgs=*/0, NumArgs, HasFPFeatures,
673                Empty) {}
674 
675 CXXMemberCallExpr *CXXMemberCallExpr::Create(const ASTContext &Ctx, Expr *Fn,
676                                              ArrayRef<Expr *> Args, QualType Ty,
677                                              ExprValueKind VK,
678                                              SourceLocation RP,
679                                              FPOptionsOverride FPFeatures,
680                                              unsigned MinNumArgs) {
681   // Allocate storage for the trailing objects of CallExpr.
682   unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
683   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
684       /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
685   void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
686                            alignof(CXXMemberCallExpr));
687   return new (Mem)
688       CXXMemberCallExpr(Fn, Args, Ty, VK, RP, FPFeatures, MinNumArgs);
689 }
690 
691 CXXMemberCallExpr *CXXMemberCallExpr::CreateEmpty(const ASTContext &Ctx,
692                                                   unsigned NumArgs,
693                                                   bool HasFPFeatures,
694                                                   EmptyShell Empty) {
695   // Allocate storage for the trailing objects of CallExpr.
696   unsigned SizeOfTrailingObjects =
697       CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPFeatures);
698   void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
699                            alignof(CXXMemberCallExpr));
700   return new (Mem) CXXMemberCallExpr(NumArgs, HasFPFeatures, Empty);
701 }
702 
703 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
704   const Expr *Callee = getCallee()->IgnoreParens();
705   if (const auto *MemExpr = dyn_cast<MemberExpr>(Callee))
706     return MemExpr->getBase();
707   if (const auto *BO = dyn_cast<BinaryOperator>(Callee))
708     if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
709       return BO->getLHS();
710 
711   // FIXME: Will eventually need to cope with member pointers.
712   return nullptr;
713 }
714 
715 QualType CXXMemberCallExpr::getObjectType() const {
716   QualType Ty = getImplicitObjectArgument()->getType();
717   if (Ty->isPointerType())
718     Ty = Ty->getPointeeType();
719   return Ty;
720 }
721 
722 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
723   if (const auto *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
724     return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
725 
726   // FIXME: Will eventually need to cope with member pointers.
727   // NOTE: Update makeTailCallIfSwiftAsync on fixing this.
728   return nullptr;
729 }
730 
731 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
732   Expr* ThisArg = getImplicitObjectArgument();
733   if (!ThisArg)
734     return nullptr;
735 
736   if (ThisArg->getType()->isAnyPointerType())
737     return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
738 
739   return ThisArg->getType()->getAsCXXRecordDecl();
740 }
741 
742 //===----------------------------------------------------------------------===//
743 //  Named casts
744 //===----------------------------------------------------------------------===//
745 
746 /// getCastName - Get the name of the C++ cast being used, e.g.,
747 /// "static_cast", "dynamic_cast", "reinterpret_cast", or
748 /// "const_cast". The returned pointer must not be freed.
749 const char *CXXNamedCastExpr::getCastName() const {
750   switch (getStmtClass()) {
751   case CXXStaticCastExprClass:      return "static_cast";
752   case CXXDynamicCastExprClass:     return "dynamic_cast";
753   case CXXReinterpretCastExprClass: return "reinterpret_cast";
754   case CXXConstCastExprClass:       return "const_cast";
755   case CXXAddrspaceCastExprClass:   return "addrspace_cast";
756   default:                          return "<invalid cast>";
757   }
758 }
759 
760 CXXStaticCastExpr *
761 CXXStaticCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
762                           CastKind K, Expr *Op, const CXXCastPath *BasePath,
763                           TypeSourceInfo *WrittenTy, FPOptionsOverride FPO,
764                           SourceLocation L, SourceLocation RParenLoc,
765                           SourceRange AngleBrackets) {
766   unsigned PathSize = (BasePath ? BasePath->size() : 0);
767   void *Buffer =
768       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
769           PathSize, FPO.requiresTrailingStorage()));
770   auto *E = new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy,
771                                            FPO, L, RParenLoc, AngleBrackets);
772   if (PathSize)
773     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
774                               E->getTrailingObjects<CXXBaseSpecifier *>());
775   return E;
776 }
777 
778 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
779                                                   unsigned PathSize,
780                                                   bool HasFPFeatures) {
781   void *Buffer =
782       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
783           PathSize, HasFPFeatures));
784   return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize, HasFPFeatures);
785 }
786 
787 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
788                                                ExprValueKind VK,
789                                                CastKind K, Expr *Op,
790                                                const CXXCastPath *BasePath,
791                                                TypeSourceInfo *WrittenTy,
792                                                SourceLocation L,
793                                                SourceLocation RParenLoc,
794                                                SourceRange AngleBrackets) {
795   unsigned PathSize = (BasePath ? BasePath->size() : 0);
796   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
797   auto *E =
798       new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
799                                       RParenLoc, AngleBrackets);
800   if (PathSize)
801     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
802                               E->getTrailingObjects<CXXBaseSpecifier *>());
803   return E;
804 }
805 
806 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
807                                                     unsigned PathSize) {
808   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
809   return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
810 }
811 
812 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
813 /// to always be null. For example:
814 ///
815 /// struct A { };
816 /// struct B final : A { };
817 /// struct C { };
818 ///
819 /// C *f(B* b) { return dynamic_cast<C*>(b); }
820 bool CXXDynamicCastExpr::isAlwaysNull() const {
821   if (isValueDependent() || getCastKind() != CK_Dynamic)
822     return false;
823 
824   QualType SrcType = getSubExpr()->getType();
825   QualType DestType = getType();
826 
827   if (DestType->isVoidPointerType())
828     return false;
829 
830   if (DestType->isPointerType()) {
831     SrcType = SrcType->getPointeeType();
832     DestType = DestType->getPointeeType();
833   }
834 
835   const auto *SrcRD = SrcType->getAsCXXRecordDecl();
836   const auto *DestRD = DestType->getAsCXXRecordDecl();
837   assert(SrcRD && DestRD);
838 
839   if (SrcRD->isEffectivelyFinal()) {
840     assert(!SrcRD->isDerivedFrom(DestRD) &&
841            "upcasts should not use CK_Dynamic");
842     return true;
843   }
844 
845   if (DestRD->isEffectivelyFinal() && !DestRD->isDerivedFrom(SrcRD))
846     return true;
847 
848   return false;
849 }
850 
851 CXXReinterpretCastExpr *
852 CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
853                                ExprValueKind VK, CastKind K, Expr *Op,
854                                const CXXCastPath *BasePath,
855                                TypeSourceInfo *WrittenTy, SourceLocation L,
856                                SourceLocation RParenLoc,
857                                SourceRange AngleBrackets) {
858   unsigned PathSize = (BasePath ? BasePath->size() : 0);
859   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
860   auto *E =
861       new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
862                                           RParenLoc, AngleBrackets);
863   if (PathSize)
864     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
865                               E->getTrailingObjects<CXXBaseSpecifier *>());
866   return E;
867 }
868 
869 CXXReinterpretCastExpr *
870 CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
871   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
872   return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
873 }
874 
875 CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
876                                            ExprValueKind VK, Expr *Op,
877                                            TypeSourceInfo *WrittenTy,
878                                            SourceLocation L,
879                                            SourceLocation RParenLoc,
880                                            SourceRange AngleBrackets) {
881   return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
882 }
883 
884 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
885   return new (C) CXXConstCastExpr(EmptyShell());
886 }
887 
888 CXXAddrspaceCastExpr *
889 CXXAddrspaceCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
890                              CastKind K, Expr *Op, TypeSourceInfo *WrittenTy,
891                              SourceLocation L, SourceLocation RParenLoc,
892                              SourceRange AngleBrackets) {
893   return new (C) CXXAddrspaceCastExpr(T, VK, K, Op, WrittenTy, L, RParenLoc,
894                                       AngleBrackets);
895 }
896 
897 CXXAddrspaceCastExpr *CXXAddrspaceCastExpr::CreateEmpty(const ASTContext &C) {
898   return new (C) CXXAddrspaceCastExpr(EmptyShell());
899 }
900 
901 CXXFunctionalCastExpr *CXXFunctionalCastExpr::Create(
902     const ASTContext &C, QualType T, ExprValueKind VK, TypeSourceInfo *Written,
903     CastKind K, Expr *Op, const CXXCastPath *BasePath, FPOptionsOverride FPO,
904     SourceLocation L, SourceLocation R) {
905   unsigned PathSize = (BasePath ? BasePath->size() : 0);
906   void *Buffer =
907       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
908           PathSize, FPO.requiresTrailingStorage()));
909   auto *E = new (Buffer)
910       CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, FPO, L, R);
911   if (PathSize)
912     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
913                               E->getTrailingObjects<CXXBaseSpecifier *>());
914   return E;
915 }
916 
917 CXXFunctionalCastExpr *CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C,
918                                                           unsigned PathSize,
919                                                           bool HasFPFeatures) {
920   void *Buffer =
921       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
922           PathSize, HasFPFeatures));
923   return new (Buffer)
924       CXXFunctionalCastExpr(EmptyShell(), PathSize, HasFPFeatures);
925 }
926 
927 SourceLocation CXXFunctionalCastExpr::getBeginLoc() const {
928   return getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
929 }
930 
931 SourceLocation CXXFunctionalCastExpr::getEndLoc() const {
932   return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getEndLoc();
933 }
934 
935 UserDefinedLiteral::UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args,
936                                        QualType Ty, ExprValueKind VK,
937                                        SourceLocation LitEndLoc,
938                                        SourceLocation SuffixLoc,
939                                        FPOptionsOverride FPFeatures)
940     : CallExpr(UserDefinedLiteralClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
941                LitEndLoc, FPFeatures, /*MinNumArgs=*/0, NotADL),
942       UDSuffixLoc(SuffixLoc) {}
943 
944 UserDefinedLiteral::UserDefinedLiteral(unsigned NumArgs, bool HasFPFeatures,
945                                        EmptyShell Empty)
946     : CallExpr(UserDefinedLiteralClass, /*NumPreArgs=*/0, NumArgs,
947                HasFPFeatures, Empty) {}
948 
949 UserDefinedLiteral *UserDefinedLiteral::Create(const ASTContext &Ctx, Expr *Fn,
950                                                ArrayRef<Expr *> Args,
951                                                QualType Ty, ExprValueKind VK,
952                                                SourceLocation LitEndLoc,
953                                                SourceLocation SuffixLoc,
954                                                FPOptionsOverride FPFeatures) {
955   // Allocate storage for the trailing objects of CallExpr.
956   unsigned NumArgs = Args.size();
957   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
958       /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
959   void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
960                            alignof(UserDefinedLiteral));
961   return new (Mem)
962       UserDefinedLiteral(Fn, Args, Ty, VK, LitEndLoc, SuffixLoc, FPFeatures);
963 }
964 
965 UserDefinedLiteral *UserDefinedLiteral::CreateEmpty(const ASTContext &Ctx,
966                                                     unsigned NumArgs,
967                                                     bool HasFPOptions,
968                                                     EmptyShell Empty) {
969   // Allocate storage for the trailing objects of CallExpr.
970   unsigned SizeOfTrailingObjects =
971       CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPOptions);
972   void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
973                            alignof(UserDefinedLiteral));
974   return new (Mem) UserDefinedLiteral(NumArgs, HasFPOptions, Empty);
975 }
976 
977 UserDefinedLiteral::LiteralOperatorKind
978 UserDefinedLiteral::getLiteralOperatorKind() const {
979   if (getNumArgs() == 0)
980     return LOK_Template;
981   if (getNumArgs() == 2)
982     return LOK_String;
983 
984   assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
985   QualType ParamTy =
986     cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
987   if (ParamTy->isPointerType())
988     return LOK_Raw;
989   if (ParamTy->isAnyCharacterType())
990     return LOK_Character;
991   if (ParamTy->isIntegerType())
992     return LOK_Integer;
993   if (ParamTy->isFloatingType())
994     return LOK_Floating;
995 
996   llvm_unreachable("unknown kind of literal operator");
997 }
998 
999 Expr *UserDefinedLiteral::getCookedLiteral() {
1000 #ifndef NDEBUG
1001   LiteralOperatorKind LOK = getLiteralOperatorKind();
1002   assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
1003 #endif
1004   return getArg(0);
1005 }
1006 
1007 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
1008   return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
1009 }
1010 
1011 CXXDefaultArgExpr *CXXDefaultArgExpr::CreateEmpty(const ASTContext &C,
1012                                                   bool HasRewrittenInit) {
1013   size_t Size = totalSizeToAlloc<Expr *>(HasRewrittenInit);
1014   auto *Mem = C.Allocate(Size, alignof(CXXDefaultArgExpr));
1015   return new (Mem) CXXDefaultArgExpr(EmptyShell(), HasRewrittenInit);
1016 }
1017 
1018 CXXDefaultArgExpr *CXXDefaultArgExpr::Create(const ASTContext &C,
1019                                              SourceLocation Loc,
1020                                              ParmVarDecl *Param,
1021                                              Expr *RewrittenExpr,
1022                                              DeclContext *UsedContext) {
1023   size_t Size = totalSizeToAlloc<Expr *>(RewrittenExpr != nullptr);
1024   auto *Mem = C.Allocate(Size, alignof(CXXDefaultArgExpr));
1025   return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
1026                                      RewrittenExpr, UsedContext);
1027 }
1028 
1029 Expr *CXXDefaultArgExpr::getExpr() {
1030   return CXXDefaultArgExprBits.HasRewrittenInit ? getAdjustedRewrittenExpr()
1031                                                 : getParam()->getDefaultArg();
1032 }
1033 
1034 Expr *CXXDefaultArgExpr::getAdjustedRewrittenExpr() {
1035   assert(hasRewrittenInit() &&
1036          "expected this CXXDefaultArgExpr to have a rewritten init.");
1037   Expr *Init = getRewrittenExpr();
1038   if (auto *E = dyn_cast_if_present<FullExpr>(Init))
1039     if (!isa<ConstantExpr>(E))
1040       return E->getSubExpr();
1041   return Init;
1042 }
1043 
1044 CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &Ctx,
1045                                        SourceLocation Loc, FieldDecl *Field,
1046                                        QualType Ty, DeclContext *UsedContext,
1047                                        Expr *RewrittenInitExpr)
1048     : Expr(CXXDefaultInitExprClass, Ty.getNonLValueExprType(Ctx),
1049            Ty->isLValueReferenceType()   ? VK_LValue
1050            : Ty->isRValueReferenceType() ? VK_XValue
1051                                          : VK_PRValue,
1052            /*FIXME*/ OK_Ordinary),
1053       Field(Field), UsedContext(UsedContext) {
1054   CXXDefaultInitExprBits.Loc = Loc;
1055   CXXDefaultInitExprBits.HasRewrittenInit = RewrittenInitExpr != nullptr;
1056 
1057   if (CXXDefaultInitExprBits.HasRewrittenInit)
1058     *getTrailingObjects<Expr *>() = RewrittenInitExpr;
1059 
1060   assert(Field->hasInClassInitializer());
1061 
1062   setDependence(computeDependence(this));
1063 }
1064 
1065 CXXDefaultInitExpr *CXXDefaultInitExpr::CreateEmpty(const ASTContext &C,
1066                                                     bool HasRewrittenInit) {
1067   size_t Size = totalSizeToAlloc<Expr *>(HasRewrittenInit);
1068   auto *Mem = C.Allocate(Size, alignof(CXXDefaultInitExpr));
1069   return new (Mem) CXXDefaultInitExpr(EmptyShell(), HasRewrittenInit);
1070 }
1071 
1072 CXXDefaultInitExpr *CXXDefaultInitExpr::Create(const ASTContext &Ctx,
1073                                                SourceLocation Loc,
1074                                                FieldDecl *Field,
1075                                                DeclContext *UsedContext,
1076                                                Expr *RewrittenInitExpr) {
1077 
1078   size_t Size = totalSizeToAlloc<Expr *>(RewrittenInitExpr != nullptr);
1079   auto *Mem = Ctx.Allocate(Size, alignof(CXXDefaultInitExpr));
1080   return new (Mem) CXXDefaultInitExpr(Ctx, Loc, Field, Field->getType(),
1081                                       UsedContext, RewrittenInitExpr);
1082 }
1083 
1084 Expr *CXXDefaultInitExpr::getExpr() {
1085   assert(Field->getInClassInitializer() && "initializer hasn't been parsed");
1086   if (hasRewrittenInit())
1087     return getRewrittenExpr();
1088 
1089   return Field->getInClassInitializer();
1090 }
1091 
1092 CXXTemporary *CXXTemporary::Create(const ASTContext &C,
1093                                    const CXXDestructorDecl *Destructor) {
1094   return new (C) CXXTemporary(Destructor);
1095 }
1096 
1097 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
1098                                                    CXXTemporary *Temp,
1099                                                    Expr* SubExpr) {
1100   assert((SubExpr->getType()->isRecordType() ||
1101           SubExpr->getType()->isArrayType()) &&
1102          "Expression bound to a temporary must have record or array type!");
1103 
1104   return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
1105 }
1106 
1107 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(
1108     CXXConstructorDecl *Cons, QualType Ty, TypeSourceInfo *TSI,
1109     ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
1110     bool HadMultipleCandidates, bool ListInitialization,
1111     bool StdInitListInitialization, bool ZeroInitialization)
1112     : CXXConstructExpr(
1113           CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(),
1114           Cons, /* Elidable=*/false, Args, HadMultipleCandidates,
1115           ListInitialization, StdInitListInitialization, ZeroInitialization,
1116           CXXConstructionKind::Complete, ParenOrBraceRange),
1117       TSI(TSI) {
1118   setDependence(computeDependence(this));
1119 }
1120 
1121 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(EmptyShell Empty,
1122                                                unsigned NumArgs)
1123     : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty, NumArgs) {}
1124 
1125 CXXTemporaryObjectExpr *CXXTemporaryObjectExpr::Create(
1126     const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
1127     TypeSourceInfo *TSI, ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
1128     bool HadMultipleCandidates, bool ListInitialization,
1129     bool StdInitListInitialization, bool ZeroInitialization) {
1130   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
1131   void *Mem =
1132       Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
1133                    alignof(CXXTemporaryObjectExpr));
1134   return new (Mem) CXXTemporaryObjectExpr(
1135       Cons, Ty, TSI, Args, ParenOrBraceRange, HadMultipleCandidates,
1136       ListInitialization, StdInitListInitialization, ZeroInitialization);
1137 }
1138 
1139 CXXTemporaryObjectExpr *
1140 CXXTemporaryObjectExpr::CreateEmpty(const ASTContext &Ctx, unsigned NumArgs) {
1141   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
1142   void *Mem =
1143       Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
1144                    alignof(CXXTemporaryObjectExpr));
1145   return new (Mem) CXXTemporaryObjectExpr(EmptyShell(), NumArgs);
1146 }
1147 
1148 SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const {
1149   return getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1150 }
1151 
1152 SourceLocation CXXTemporaryObjectExpr::getEndLoc() const {
1153   SourceLocation Loc = getParenOrBraceRange().getEnd();
1154   if (Loc.isInvalid() && getNumArgs())
1155     Loc = getArg(getNumArgs() - 1)->getEndLoc();
1156   return Loc;
1157 }
1158 
1159 CXXConstructExpr *CXXConstructExpr::Create(
1160     const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
1161     CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
1162     bool HadMultipleCandidates, bool ListInitialization,
1163     bool StdInitListInitialization, bool ZeroInitialization,
1164     CXXConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
1165   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
1166   void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
1167                            alignof(CXXConstructExpr));
1168   return new (Mem) CXXConstructExpr(
1169       CXXConstructExprClass, Ty, Loc, Ctor, Elidable, Args,
1170       HadMultipleCandidates, ListInitialization, StdInitListInitialization,
1171       ZeroInitialization, ConstructKind, ParenOrBraceRange);
1172 }
1173 
1174 CXXConstructExpr *CXXConstructExpr::CreateEmpty(const ASTContext &Ctx,
1175                                                 unsigned NumArgs) {
1176   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
1177   void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
1178                            alignof(CXXConstructExpr));
1179   return new (Mem)
1180       CXXConstructExpr(CXXConstructExprClass, EmptyShell(), NumArgs);
1181 }
1182 
1183 CXXConstructExpr::CXXConstructExpr(
1184     StmtClass SC, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor,
1185     bool Elidable, ArrayRef<Expr *> Args, bool HadMultipleCandidates,
1186     bool ListInitialization, bool StdInitListInitialization,
1187     bool ZeroInitialization, CXXConstructionKind ConstructKind,
1188     SourceRange ParenOrBraceRange)
1189     : Expr(SC, Ty, VK_PRValue, OK_Ordinary), Constructor(Ctor),
1190       ParenOrBraceRange(ParenOrBraceRange), NumArgs(Args.size()) {
1191   CXXConstructExprBits.Elidable = Elidable;
1192   CXXConstructExprBits.HadMultipleCandidates = HadMultipleCandidates;
1193   CXXConstructExprBits.ListInitialization = ListInitialization;
1194   CXXConstructExprBits.StdInitListInitialization = StdInitListInitialization;
1195   CXXConstructExprBits.ZeroInitialization = ZeroInitialization;
1196   CXXConstructExprBits.ConstructionKind = llvm::to_underlying(ConstructKind);
1197   CXXConstructExprBits.IsImmediateEscalating = false;
1198   CXXConstructExprBits.Loc = Loc;
1199 
1200   Stmt **TrailingArgs = getTrailingArgs();
1201   for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1202     assert(Args[I] && "NULL argument in CXXConstructExpr!");
1203     TrailingArgs[I] = Args[I];
1204   }
1205 
1206   // CXXTemporaryObjectExpr does this itself after setting its TypeSourceInfo.
1207   if (SC == CXXConstructExprClass)
1208     setDependence(computeDependence(this));
1209 }
1210 
1211 CXXConstructExpr::CXXConstructExpr(StmtClass SC, EmptyShell Empty,
1212                                    unsigned NumArgs)
1213     : Expr(SC, Empty), NumArgs(NumArgs) {}
1214 
1215 LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
1216                              LambdaCaptureKind Kind, ValueDecl *Var,
1217                              SourceLocation EllipsisLoc)
1218     : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
1219   unsigned Bits = 0;
1220   if (Implicit)
1221     Bits |= Capture_Implicit;
1222 
1223   switch (Kind) {
1224   case LCK_StarThis:
1225     Bits |= Capture_ByCopy;
1226     [[fallthrough]];
1227   case LCK_This:
1228     assert(!Var && "'this' capture cannot have a variable!");
1229     Bits |= Capture_This;
1230     break;
1231 
1232   case LCK_ByCopy:
1233     Bits |= Capture_ByCopy;
1234     [[fallthrough]];
1235   case LCK_ByRef:
1236     assert(Var && "capture must have a variable!");
1237     break;
1238   case LCK_VLAType:
1239     assert(!Var && "VLA type capture cannot have a variable!");
1240     break;
1241   }
1242   DeclAndBits.setInt(Bits);
1243 }
1244 
1245 LambdaCaptureKind LambdaCapture::getCaptureKind() const {
1246   if (capturesVLAType())
1247     return LCK_VLAType;
1248   bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
1249   if (capturesThis())
1250     return CapByCopy ? LCK_StarThis : LCK_This;
1251   return CapByCopy ? LCK_ByCopy : LCK_ByRef;
1252 }
1253 
1254 LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
1255                        LambdaCaptureDefault CaptureDefault,
1256                        SourceLocation CaptureDefaultLoc, bool ExplicitParams,
1257                        bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
1258                        SourceLocation ClosingBrace,
1259                        bool ContainsUnexpandedParameterPack)
1260     : Expr(LambdaExprClass, T, VK_PRValue, OK_Ordinary),
1261       IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
1262       ClosingBrace(ClosingBrace) {
1263   LambdaExprBits.NumCaptures = CaptureInits.size();
1264   LambdaExprBits.CaptureDefault = CaptureDefault;
1265   LambdaExprBits.ExplicitParams = ExplicitParams;
1266   LambdaExprBits.ExplicitResultType = ExplicitResultType;
1267 
1268   CXXRecordDecl *Class = getLambdaClass();
1269   (void)Class;
1270   assert(capture_size() == Class->capture_size() && "Wrong number of captures");
1271   assert(getCaptureDefault() == Class->getLambdaCaptureDefault());
1272 
1273   // Copy initialization expressions for the non-static data members.
1274   Stmt **Stored = getStoredStmts();
1275   for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
1276     *Stored++ = CaptureInits[I];
1277 
1278   // Copy the body of the lambda.
1279   *Stored++ = getCallOperator()->getBody();
1280 
1281   setDependence(computeDependence(this, ContainsUnexpandedParameterPack));
1282 }
1283 
1284 LambdaExpr::LambdaExpr(EmptyShell Empty, unsigned NumCaptures)
1285     : Expr(LambdaExprClass, Empty) {
1286   LambdaExprBits.NumCaptures = NumCaptures;
1287 
1288   // Initially don't initialize the body of the LambdaExpr. The body will
1289   // be lazily deserialized when needed.
1290   getStoredStmts()[NumCaptures] = nullptr; // Not one past the end.
1291 }
1292 
1293 LambdaExpr *LambdaExpr::Create(const ASTContext &Context, CXXRecordDecl *Class,
1294                                SourceRange IntroducerRange,
1295                                LambdaCaptureDefault CaptureDefault,
1296                                SourceLocation CaptureDefaultLoc,
1297                                bool ExplicitParams, bool ExplicitResultType,
1298                                ArrayRef<Expr *> CaptureInits,
1299                                SourceLocation ClosingBrace,
1300                                bool ContainsUnexpandedParameterPack) {
1301   // Determine the type of the expression (i.e., the type of the
1302   // function object we're creating).
1303   QualType T = Context.getTypeDeclType(Class);
1304 
1305   unsigned Size = totalSizeToAlloc<Stmt *>(CaptureInits.size() + 1);
1306   void *Mem = Context.Allocate(Size);
1307   return new (Mem)
1308       LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
1309                  ExplicitParams, ExplicitResultType, CaptureInits, ClosingBrace,
1310                  ContainsUnexpandedParameterPack);
1311 }
1312 
1313 LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1314                                            unsigned NumCaptures) {
1315   unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
1316   void *Mem = C.Allocate(Size);
1317   return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
1318 }
1319 
1320 void LambdaExpr::initBodyIfNeeded() const {
1321   if (!getStoredStmts()[capture_size()]) {
1322     auto *This = const_cast<LambdaExpr *>(this);
1323     This->getStoredStmts()[capture_size()] = getCallOperator()->getBody();
1324   }
1325 }
1326 
1327 Stmt *LambdaExpr::getBody() const {
1328   initBodyIfNeeded();
1329   return getStoredStmts()[capture_size()];
1330 }
1331 
1332 const CompoundStmt *LambdaExpr::getCompoundStmtBody() const {
1333   Stmt *Body = getBody();
1334   if (const auto *CoroBody = dyn_cast<CoroutineBodyStmt>(Body))
1335     return cast<CompoundStmt>(CoroBody->getBody());
1336   return cast<CompoundStmt>(Body);
1337 }
1338 
1339 bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1340   return C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1341          getCallOperator() == C->getCapturedVar()->getDeclContext();
1342 }
1343 
1344 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
1345   return getLambdaClass()->captures_begin();
1346 }
1347 
1348 LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
1349   return getLambdaClass()->captures_end();
1350 }
1351 
1352 LambdaExpr::capture_range LambdaExpr::captures() const {
1353   return capture_range(capture_begin(), capture_end());
1354 }
1355 
1356 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1357   return capture_begin();
1358 }
1359 
1360 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1361   return capture_begin() +
1362          getLambdaClass()->getLambdaData().NumExplicitCaptures;
1363 }
1364 
1365 LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1366   return capture_range(explicit_capture_begin(), explicit_capture_end());
1367 }
1368 
1369 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1370   return explicit_capture_end();
1371 }
1372 
1373 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1374   return capture_end();
1375 }
1376 
1377 LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1378   return capture_range(implicit_capture_begin(), implicit_capture_end());
1379 }
1380 
1381 CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1382   return getType()->getAsCXXRecordDecl();
1383 }
1384 
1385 CXXMethodDecl *LambdaExpr::getCallOperator() const {
1386   CXXRecordDecl *Record = getLambdaClass();
1387   return Record->getLambdaCallOperator();
1388 }
1389 
1390 FunctionTemplateDecl *LambdaExpr::getDependentCallOperator() const {
1391   CXXRecordDecl *Record = getLambdaClass();
1392   return Record->getDependentLambdaCallOperator();
1393 }
1394 
1395 TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1396   CXXRecordDecl *Record = getLambdaClass();
1397   return Record->getGenericLambdaTemplateParameterList();
1398 }
1399 
1400 ArrayRef<NamedDecl *> LambdaExpr::getExplicitTemplateParameters() const {
1401   const CXXRecordDecl *Record = getLambdaClass();
1402   return Record->getLambdaExplicitTemplateParameters();
1403 }
1404 
1405 Expr *LambdaExpr::getTrailingRequiresClause() const {
1406   return getCallOperator()->getTrailingRequiresClause();
1407 }
1408 
1409 bool LambdaExpr::isMutable() const { return !getCallOperator()->isConst(); }
1410 
1411 LambdaExpr::child_range LambdaExpr::children() {
1412   initBodyIfNeeded();
1413   return child_range(getStoredStmts(), getStoredStmts() + capture_size() + 1);
1414 }
1415 
1416 LambdaExpr::const_child_range LambdaExpr::children() const {
1417   initBodyIfNeeded();
1418   return const_child_range(getStoredStmts(),
1419                            getStoredStmts() + capture_size() + 1);
1420 }
1421 
1422 ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1423                                    bool CleanupsHaveSideEffects,
1424                                    ArrayRef<CleanupObject> objects)
1425     : FullExpr(ExprWithCleanupsClass, subexpr) {
1426   ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
1427   ExprWithCleanupsBits.NumObjects = objects.size();
1428   for (unsigned i = 0, e = objects.size(); i != e; ++i)
1429     getTrailingObjects<CleanupObject>()[i] = objects[i];
1430 }
1431 
1432 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
1433                                            bool CleanupsHaveSideEffects,
1434                                            ArrayRef<CleanupObject> objects) {
1435   void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
1436                             alignof(ExprWithCleanups));
1437   return new (buffer)
1438       ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
1439 }
1440 
1441 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1442     : FullExpr(ExprWithCleanupsClass, empty) {
1443   ExprWithCleanupsBits.NumObjects = numObjects;
1444 }
1445 
1446 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1447                                            EmptyShell empty,
1448                                            unsigned numObjects) {
1449   void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
1450                             alignof(ExprWithCleanups));
1451   return new (buffer) ExprWithCleanups(empty, numObjects);
1452 }
1453 
1454 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(
1455     QualType T, TypeSourceInfo *TSI, SourceLocation LParenLoc,
1456     ArrayRef<Expr *> Args, SourceLocation RParenLoc, bool IsListInit)
1457     : Expr(CXXUnresolvedConstructExprClass, T,
1458            (TSI->getType()->isLValueReferenceType()   ? VK_LValue
1459             : TSI->getType()->isRValueReferenceType() ? VK_XValue
1460                                                       : VK_PRValue),
1461            OK_Ordinary),
1462       TypeAndInitForm(TSI, IsListInit), LParenLoc(LParenLoc),
1463       RParenLoc(RParenLoc) {
1464   CXXUnresolvedConstructExprBits.NumArgs = Args.size();
1465   auto **StoredArgs = getTrailingObjects<Expr *>();
1466   for (unsigned I = 0; I != Args.size(); ++I)
1467     StoredArgs[I] = Args[I];
1468   setDependence(computeDependence(this));
1469 }
1470 
1471 CXXUnresolvedConstructExpr *CXXUnresolvedConstructExpr::Create(
1472     const ASTContext &Context, QualType T, TypeSourceInfo *TSI,
1473     SourceLocation LParenLoc, ArrayRef<Expr *> Args, SourceLocation RParenLoc,
1474     bool IsListInit) {
1475   void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
1476   return new (Mem) CXXUnresolvedConstructExpr(T, TSI, LParenLoc, Args,
1477                                               RParenLoc, IsListInit);
1478 }
1479 
1480 CXXUnresolvedConstructExpr *
1481 CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &Context,
1482                                         unsigned NumArgs) {
1483   void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
1484   return new (Mem) CXXUnresolvedConstructExpr(EmptyShell(), NumArgs);
1485 }
1486 
1487 SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const {
1488   return TypeAndInitForm.getPointer()->getTypeLoc().getBeginLoc();
1489 }
1490 
1491 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1492     const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
1493     SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1494     SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1495     DeclarationNameInfo MemberNameInfo,
1496     const TemplateArgumentListInfo *TemplateArgs)
1497     : Expr(CXXDependentScopeMemberExprClass, Ctx.DependentTy, VK_LValue,
1498            OK_Ordinary),
1499       Base(Base), BaseType(BaseType), QualifierLoc(QualifierLoc),
1500       MemberNameInfo(MemberNameInfo) {
1501   CXXDependentScopeMemberExprBits.IsArrow = IsArrow;
1502   CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
1503       (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
1504   CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
1505       FirstQualifierFoundInScope != nullptr;
1506   CXXDependentScopeMemberExprBits.OperatorLoc = OperatorLoc;
1507 
1508   if (TemplateArgs) {
1509     auto Deps = TemplateArgumentDependence::None;
1510     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1511         TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1512         Deps);
1513   } else if (TemplateKWLoc.isValid()) {
1514     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1515         TemplateKWLoc);
1516   }
1517 
1518   if (hasFirstQualifierFoundInScope())
1519     *getTrailingObjects<NamedDecl *>() = FirstQualifierFoundInScope;
1520   setDependence(computeDependence(this));
1521 }
1522 
1523 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1524     EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
1525     bool HasFirstQualifierFoundInScope)
1526     : Expr(CXXDependentScopeMemberExprClass, Empty) {
1527   CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
1528       HasTemplateKWAndArgsInfo;
1529   CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
1530       HasFirstQualifierFoundInScope;
1531 }
1532 
1533 CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::Create(
1534     const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
1535     SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1536     SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1537     DeclarationNameInfo MemberNameInfo,
1538     const TemplateArgumentListInfo *TemplateArgs) {
1539   bool HasTemplateKWAndArgsInfo =
1540       (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
1541   unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1542   bool HasFirstQualifierFoundInScope = FirstQualifierFoundInScope != nullptr;
1543 
1544   unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
1545                                    TemplateArgumentLoc, NamedDecl *>(
1546       HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
1547 
1548   void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1549   return new (Mem) CXXDependentScopeMemberExpr(
1550       Ctx, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
1551       FirstQualifierFoundInScope, MemberNameInfo, TemplateArgs);
1552 }
1553 
1554 CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::CreateEmpty(
1555     const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
1556     unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope) {
1557   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1558 
1559   unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
1560                                    TemplateArgumentLoc, NamedDecl *>(
1561       HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
1562 
1563   void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1564   return new (Mem) CXXDependentScopeMemberExpr(
1565       EmptyShell(), HasTemplateKWAndArgsInfo, HasFirstQualifierFoundInScope);
1566 }
1567 
1568 CXXThisExpr *CXXThisExpr::Create(const ASTContext &Ctx, SourceLocation L,
1569                                  QualType Ty, bool IsImplicit) {
1570   return new (Ctx) CXXThisExpr(L, Ty, IsImplicit,
1571                                Ctx.getLangOpts().HLSL ? VK_LValue : VK_PRValue);
1572 }
1573 
1574 CXXThisExpr *CXXThisExpr::CreateEmpty(const ASTContext &Ctx) {
1575   return new (Ctx) CXXThisExpr(EmptyShell());
1576 }
1577 
1578 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1579                                             UnresolvedSetIterator end) {
1580   do {
1581     NamedDecl *decl = *begin;
1582     if (isa<UnresolvedUsingValueDecl>(decl))
1583       return false;
1584 
1585     // Unresolved member expressions should only contain methods and
1586     // method templates.
1587     if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1588             ->isStatic())
1589       return false;
1590   } while (++begin != end);
1591 
1592   return true;
1593 }
1594 
1595 UnresolvedMemberExpr::UnresolvedMemberExpr(
1596     const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
1597     QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
1598     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1599     const DeclarationNameInfo &MemberNameInfo,
1600     const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1601     UnresolvedSetIterator End)
1602     : OverloadExpr(
1603           UnresolvedMemberExprClass, Context, QualifierLoc, TemplateKWLoc,
1604           MemberNameInfo, TemplateArgs, Begin, End,
1605           // Dependent
1606           ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
1607           ((Base && Base->isInstantiationDependent()) ||
1608            BaseType->isInstantiationDependentType()),
1609           // Contains unexpanded parameter pack
1610           ((Base && Base->containsUnexpandedParameterPack()) ||
1611            BaseType->containsUnexpandedParameterPack())),
1612       Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
1613   UnresolvedMemberExprBits.IsArrow = IsArrow;
1614   UnresolvedMemberExprBits.HasUnresolvedUsing = HasUnresolvedUsing;
1615 
1616   // Check whether all of the members are non-static member functions,
1617   // and if so, mark give this bound-member type instead of overload type.
1618   if (hasOnlyNonStaticMemberFunctions(Begin, End))
1619     setType(Context.BoundMemberTy);
1620 }
1621 
1622 UnresolvedMemberExpr::UnresolvedMemberExpr(EmptyShell Empty,
1623                                            unsigned NumResults,
1624                                            bool HasTemplateKWAndArgsInfo)
1625     : OverloadExpr(UnresolvedMemberExprClass, Empty, NumResults,
1626                    HasTemplateKWAndArgsInfo) {}
1627 
1628 bool UnresolvedMemberExpr::isImplicitAccess() const {
1629   if (!Base)
1630     return true;
1631 
1632   return cast<Expr>(Base)->isImplicitCXXThis();
1633 }
1634 
1635 UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1636     const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
1637     QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
1638     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1639     const DeclarationNameInfo &MemberNameInfo,
1640     const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1641     UnresolvedSetIterator End) {
1642   unsigned NumResults = End - Begin;
1643   bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1644   unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1645   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
1646                                    TemplateArgumentLoc>(
1647       NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
1648   void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
1649   return new (Mem) UnresolvedMemberExpr(
1650       Context, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc,
1651       QualifierLoc, TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
1652 }
1653 
1654 UnresolvedMemberExpr *UnresolvedMemberExpr::CreateEmpty(
1655     const ASTContext &Context, unsigned NumResults,
1656     bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
1657   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1658   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
1659                                    TemplateArgumentLoc>(
1660       NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
1661   void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
1662   return new (Mem)
1663       UnresolvedMemberExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
1664 }
1665 
1666 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() {
1667   // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1668 
1669   // If there was a nested name specifier, it names the naming class.
1670   // It can't be dependent: after all, we were actually able to do the
1671   // lookup.
1672   CXXRecordDecl *Record = nullptr;
1673   auto *NNS = getQualifier();
1674   if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
1675     const Type *T = getQualifier()->getAsType();
1676     assert(T && "qualifier in member expression does not name type");
1677     Record = T->getAsCXXRecordDecl();
1678     assert(Record && "qualifier in member expression does not name record");
1679   }
1680   // Otherwise the naming class must have been the base class.
1681   else {
1682     QualType BaseType = getBaseType().getNonReferenceType();
1683     if (isArrow())
1684       BaseType = BaseType->castAs<PointerType>()->getPointeeType();
1685 
1686     Record = BaseType->getAsCXXRecordDecl();
1687     assert(Record && "base of member expression does not name record");
1688   }
1689 
1690   return Record;
1691 }
1692 
1693 SizeOfPackExpr *SizeOfPackExpr::Create(ASTContext &Context,
1694                                        SourceLocation OperatorLoc,
1695                                        NamedDecl *Pack, SourceLocation PackLoc,
1696                                        SourceLocation RParenLoc,
1697                                        std::optional<unsigned> Length,
1698                                        ArrayRef<TemplateArgument> PartialArgs) {
1699   void *Storage =
1700       Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
1701   return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1702                                       PackLoc, RParenLoc, Length, PartialArgs);
1703 }
1704 
1705 SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1706                                                    unsigned NumPartialArgs) {
1707   void *Storage =
1708       Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
1709   return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1710 }
1711 
1712 NonTypeTemplateParmDecl *SubstNonTypeTemplateParmExpr::getParameter() const {
1713   return cast<NonTypeTemplateParmDecl>(
1714       getReplacedTemplateParameterList(getAssociatedDecl())->asArray()[Index]);
1715 }
1716 
1717 PackIndexingExpr *PackIndexingExpr::Create(
1718     ASTContext &Context, SourceLocation EllipsisLoc, SourceLocation RSquareLoc,
1719     Expr *PackIdExpr, Expr *IndexExpr, std::optional<int64_t> Index,
1720     ArrayRef<Expr *> SubstitutedExprs, bool FullySubstituted) {
1721   QualType Type;
1722   if (Index && FullySubstituted && !SubstitutedExprs.empty())
1723     Type = SubstitutedExprs[*Index]->getType();
1724   else
1725     Type = PackIdExpr->getType();
1726 
1727   void *Storage =
1728       Context.Allocate(totalSizeToAlloc<Expr *>(SubstitutedExprs.size()));
1729   return new (Storage)
1730       PackIndexingExpr(Type, EllipsisLoc, RSquareLoc, PackIdExpr, IndexExpr,
1731                        SubstitutedExprs, FullySubstituted);
1732 }
1733 
1734 NamedDecl *PackIndexingExpr::getPackDecl() const {
1735   if (auto *D = dyn_cast<DeclRefExpr>(getPackIdExpression()); D) {
1736     NamedDecl *ND = dyn_cast<NamedDecl>(D->getDecl());
1737     assert(ND && "exected a named decl");
1738     return ND;
1739   }
1740   assert(false && "invalid declaration kind in pack indexing expression");
1741   return nullptr;
1742 }
1743 
1744 PackIndexingExpr *
1745 PackIndexingExpr::CreateDeserialized(ASTContext &Context,
1746                                      unsigned NumTransformedExprs) {
1747   void *Storage =
1748       Context.Allocate(totalSizeToAlloc<Expr *>(NumTransformedExprs));
1749   return new (Storage) PackIndexingExpr(EmptyShell{});
1750 }
1751 
1752 QualType SubstNonTypeTemplateParmExpr::getParameterType(
1753     const ASTContext &Context) const {
1754   // Note that, for a class type NTTP, we will have an lvalue of type 'const
1755   // T', so we can't just compute this from the type and value category.
1756   if (isReferenceParameter())
1757     return Context.getLValueReferenceType(getType());
1758   return getType().getUnqualifiedType();
1759 }
1760 
1761 SubstNonTypeTemplateParmPackExpr::SubstNonTypeTemplateParmPackExpr(
1762     QualType T, ExprValueKind ValueKind, SourceLocation NameLoc,
1763     const TemplateArgument &ArgPack, Decl *AssociatedDecl, unsigned Index)
1764     : Expr(SubstNonTypeTemplateParmPackExprClass, T, ValueKind, OK_Ordinary),
1765       AssociatedDecl(AssociatedDecl), Arguments(ArgPack.pack_begin()),
1766       NumArguments(ArgPack.pack_size()), Index(Index), NameLoc(NameLoc) {
1767   assert(AssociatedDecl != nullptr);
1768   setDependence(ExprDependence::TypeValueInstantiation |
1769                 ExprDependence::UnexpandedPack);
1770 }
1771 
1772 NonTypeTemplateParmDecl *
1773 SubstNonTypeTemplateParmPackExpr::getParameterPack() const {
1774   return cast<NonTypeTemplateParmDecl>(
1775       getReplacedTemplateParameterList(getAssociatedDecl())->asArray()[Index]);
1776 }
1777 
1778 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1779   return TemplateArgument(llvm::ArrayRef(Arguments, NumArguments));
1780 }
1781 
1782 FunctionParmPackExpr::FunctionParmPackExpr(QualType T, VarDecl *ParamPack,
1783                                            SourceLocation NameLoc,
1784                                            unsigned NumParams,
1785                                            VarDecl *const *Params)
1786     : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary),
1787       ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1788   if (Params)
1789     std::uninitialized_copy(Params, Params + NumParams,
1790                             getTrailingObjects<VarDecl *>());
1791   setDependence(ExprDependence::TypeValueInstantiation |
1792                 ExprDependence::UnexpandedPack);
1793 }
1794 
1795 FunctionParmPackExpr *
1796 FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
1797                              VarDecl *ParamPack, SourceLocation NameLoc,
1798                              ArrayRef<VarDecl *> Params) {
1799   return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(Params.size())))
1800       FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1801 }
1802 
1803 FunctionParmPackExpr *
1804 FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1805                                   unsigned NumParams) {
1806   return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(NumParams)))
1807       FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
1808 }
1809 
1810 MaterializeTemporaryExpr::MaterializeTemporaryExpr(
1811     QualType T, Expr *Temporary, bool BoundToLvalueReference,
1812     LifetimeExtendedTemporaryDecl *MTD)
1813     : Expr(MaterializeTemporaryExprClass, T,
1814            BoundToLvalueReference ? VK_LValue : VK_XValue, OK_Ordinary) {
1815   if (MTD) {
1816     State = MTD;
1817     MTD->ExprWithTemporary = Temporary;
1818     return;
1819   }
1820   State = Temporary;
1821   setDependence(computeDependence(this));
1822 }
1823 
1824 void MaterializeTemporaryExpr::setExtendingDecl(ValueDecl *ExtendedBy,
1825                                                 unsigned ManglingNumber) {
1826   // We only need extra state if we have to remember more than just the Stmt.
1827   if (!ExtendedBy)
1828     return;
1829 
1830   // We may need to allocate extra storage for the mangling number and the
1831   // extended-by ValueDecl.
1832   if (!isa<LifetimeExtendedTemporaryDecl *>(State))
1833     State = LifetimeExtendedTemporaryDecl::Create(
1834         cast<Expr>(cast<Stmt *>(State)), ExtendedBy, ManglingNumber);
1835 
1836   auto ES = cast<LifetimeExtendedTemporaryDecl *>(State);
1837   ES->ExtendingDecl = ExtendedBy;
1838   ES->ManglingNumber = ManglingNumber;
1839 }
1840 
1841 bool MaterializeTemporaryExpr::isUsableInConstantExpressions(
1842     const ASTContext &Context) const {
1843   // C++20 [expr.const]p4:
1844   //   An object or reference is usable in constant expressions if it is [...]
1845   //   a temporary object of non-volatile const-qualified literal type
1846   //   whose lifetime is extended to that of a variable that is usable
1847   //   in constant expressions
1848   auto *VD = dyn_cast_or_null<VarDecl>(getExtendingDecl());
1849   return VD && getType().isConstant(Context) &&
1850          !getType().isVolatileQualified() &&
1851          getType()->isLiteralType(Context) &&
1852          VD->isUsableInConstantExpressions(Context);
1853 }
1854 
1855 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1856                              ArrayRef<TypeSourceInfo *> Args,
1857                              SourceLocation RParenLoc, bool Value)
1858     : Expr(TypeTraitExprClass, T, VK_PRValue, OK_Ordinary), Loc(Loc),
1859       RParenLoc(RParenLoc) {
1860   assert(Kind <= TT_Last && "invalid enum value!");
1861   TypeTraitExprBits.Kind = Kind;
1862   assert(static_cast<unsigned>(Kind) == TypeTraitExprBits.Kind &&
1863          "TypeTraitExprBits.Kind overflow!");
1864   TypeTraitExprBits.Value = Value;
1865   TypeTraitExprBits.NumArgs = Args.size();
1866   assert(Args.size() == TypeTraitExprBits.NumArgs &&
1867          "TypeTraitExprBits.NumArgs overflow!");
1868 
1869   auto **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1870   for (unsigned I = 0, N = Args.size(); I != N; ++I)
1871     ToArgs[I] = Args[I];
1872 
1873   setDependence(computeDependence(this));
1874 }
1875 
1876 TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
1877                                      SourceLocation Loc,
1878                                      TypeTrait Kind,
1879                                      ArrayRef<TypeSourceInfo *> Args,
1880                                      SourceLocation RParenLoc,
1881                                      bool Value) {
1882   void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
1883   return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1884 }
1885 
1886 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
1887                                                  unsigned NumArgs) {
1888   void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
1889   return new (Mem) TypeTraitExpr(EmptyShell());
1890 }
1891 
1892 CUDAKernelCallExpr::CUDAKernelCallExpr(Expr *Fn, CallExpr *Config,
1893                                        ArrayRef<Expr *> Args, QualType Ty,
1894                                        ExprValueKind VK, SourceLocation RP,
1895                                        FPOptionsOverride FPFeatures,
1896                                        unsigned MinNumArgs)
1897     : CallExpr(CUDAKernelCallExprClass, Fn, /*PreArgs=*/Config, Args, Ty, VK,
1898                RP, FPFeatures, MinNumArgs, NotADL) {}
1899 
1900 CUDAKernelCallExpr::CUDAKernelCallExpr(unsigned NumArgs, bool HasFPFeatures,
1901                                        EmptyShell Empty)
1902     : CallExpr(CUDAKernelCallExprClass, /*NumPreArgs=*/END_PREARG, NumArgs,
1903                HasFPFeatures, Empty) {}
1904 
1905 CUDAKernelCallExpr *
1906 CUDAKernelCallExpr::Create(const ASTContext &Ctx, Expr *Fn, CallExpr *Config,
1907                            ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
1908                            SourceLocation RP, FPOptionsOverride FPFeatures,
1909                            unsigned MinNumArgs) {
1910   // Allocate storage for the trailing objects of CallExpr.
1911   unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
1912   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
1913       /*NumPreArgs=*/END_PREARG, NumArgs, FPFeatures.requiresTrailingStorage());
1914   void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1915                            alignof(CUDAKernelCallExpr));
1916   return new (Mem)
1917       CUDAKernelCallExpr(Fn, Config, Args, Ty, VK, RP, FPFeatures, MinNumArgs);
1918 }
1919 
1920 CUDAKernelCallExpr *CUDAKernelCallExpr::CreateEmpty(const ASTContext &Ctx,
1921                                                     unsigned NumArgs,
1922                                                     bool HasFPFeatures,
1923                                                     EmptyShell Empty) {
1924   // Allocate storage for the trailing objects of CallExpr.
1925   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
1926       /*NumPreArgs=*/END_PREARG, NumArgs, HasFPFeatures);
1927   void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1928                            alignof(CUDAKernelCallExpr));
1929   return new (Mem) CUDAKernelCallExpr(NumArgs, HasFPFeatures, Empty);
1930 }
1931 
1932 CXXParenListInitExpr *
1933 CXXParenListInitExpr::Create(ASTContext &C, ArrayRef<Expr *> Args, QualType T,
1934                              unsigned NumUserSpecifiedExprs,
1935                              SourceLocation InitLoc, SourceLocation LParenLoc,
1936                              SourceLocation RParenLoc) {
1937   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
1938   return new (Mem) CXXParenListInitExpr(Args, T, NumUserSpecifiedExprs, InitLoc,
1939                                         LParenLoc, RParenLoc);
1940 }
1941 
1942 CXXParenListInitExpr *CXXParenListInitExpr::CreateEmpty(ASTContext &C,
1943                                                         unsigned NumExprs,
1944                                                         EmptyShell Empty) {
1945   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumExprs),
1946                          alignof(CXXParenListInitExpr));
1947   return new (Mem) CXXParenListInitExpr(Empty, NumExprs);
1948 }
1949 
1950 CXXFoldExpr::CXXFoldExpr(QualType T, UnresolvedLookupExpr *Callee,
1951                                 SourceLocation LParenLoc, Expr *LHS,
1952                                 BinaryOperatorKind Opcode,
1953                                 SourceLocation EllipsisLoc, Expr *RHS,
1954                                 SourceLocation RParenLoc,
1955                                 std::optional<unsigned> NumExpansions)
1956     : Expr(CXXFoldExprClass, T, VK_PRValue, OK_Ordinary), LParenLoc(LParenLoc),
1957       EllipsisLoc(EllipsisLoc), RParenLoc(RParenLoc),
1958       NumExpansions(NumExpansions ? *NumExpansions + 1 : 0), Opcode(Opcode) {
1959   // We rely on asserted invariant to distinguish left and right folds.
1960   assert(((LHS && LHS->containsUnexpandedParameterPack()) !=
1961           (RHS && RHS->containsUnexpandedParameterPack())) &&
1962          "Exactly one of LHS or RHS should contain an unexpanded pack");
1963   SubExprs[SubExpr::Callee] = Callee;
1964   SubExprs[SubExpr::LHS] = LHS;
1965   SubExprs[SubExpr::RHS] = RHS;
1966   setDependence(computeDependence(this));
1967 }
1968 
1969 ResolvedUnexpandedPackExpr::ResolvedUnexpandedPackExpr(SourceLocation BL,
1970                                                        QualType QT,
1971                                                        unsigned NumExprs)
1972     : Expr(ResolvedUnexpandedPackExprClass, QT, VK_PRValue, OK_Ordinary),
1973       BeginLoc(BL), NumExprs(NumExprs) {
1974   // C++ [temp.dep.expr]p3
1975   // An id-expression is type-dependent if it is
1976   //    - associated by name lookup with a pack
1977   setDependence(ExprDependence::TypeValueInstantiation |
1978                 ExprDependence::UnexpandedPack);
1979 }
1980 
1981 ResolvedUnexpandedPackExpr *
1982 ResolvedUnexpandedPackExpr::CreateDeserialized(ASTContext &Ctx,
1983                                                unsigned NumExprs) {
1984   void *Mem = Ctx.Allocate(totalSizeToAlloc<Expr *>(NumExprs),
1985                            alignof(ResolvedUnexpandedPackExpr));
1986   return new (Mem)
1987       ResolvedUnexpandedPackExpr(SourceLocation(), QualType(), NumExprs);
1988 }
1989 
1990 ResolvedUnexpandedPackExpr *
1991 ResolvedUnexpandedPackExpr::Create(ASTContext &Ctx, SourceLocation BL,
1992                                    QualType T, unsigned NumExprs) {
1993   void *Mem = Ctx.Allocate(totalSizeToAlloc<Expr *>(NumExprs),
1994                            alignof(ResolvedUnexpandedPackExpr));
1995   ResolvedUnexpandedPackExpr *New =
1996       new (Mem) ResolvedUnexpandedPackExpr(BL, T, NumExprs);
1997 
1998   auto Exprs = New->getExprs();
1999   std::uninitialized_fill(Exprs.begin(), Exprs.end(), nullptr);
2000 
2001   return New;
2002 }
2003 
2004 ResolvedUnexpandedPackExpr *
2005 ResolvedUnexpandedPackExpr::Create(ASTContext &Ctx, SourceLocation BL,
2006                                    QualType T, ArrayRef<Expr *> Exprs) {
2007   auto *New = Create(Ctx, BL, T, Exprs.size());
2008   std::uninitialized_copy(Exprs.begin(), Exprs.end(), New->getExprs().begin());
2009   return New;
2010 }
2011 
2012 ResolvedUnexpandedPackExpr *ResolvedUnexpandedPackExpr::getFromDecl(Decl *D) {
2013   if (auto *BD = dyn_cast<BindingDecl>(D))
2014     return dyn_cast_if_present<ResolvedUnexpandedPackExpr>(BD->getBinding());
2015   return nullptr;
2016 }
2017