xref: /freebsd/contrib/llvm-project/clang/lib/AST/ExprCXX.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===- ExprCXX.cpp - (C++) Expression AST Node Implementation -------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the subclesses of Expr class declared in ExprCXX.h
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h"
140b57cec5SDimitry Andric #include "clang/AST/ASTContext.h"
150b57cec5SDimitry Andric #include "clang/AST/Attr.h"
165ffd83dbSDimitry Andric #include "clang/AST/ComputeDependence.h"
170b57cec5SDimitry Andric #include "clang/AST/Decl.h"
180b57cec5SDimitry Andric #include "clang/AST/DeclAccessPair.h"
190b57cec5SDimitry Andric #include "clang/AST/DeclBase.h"
200b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h"
2155e4f9d5SDimitry Andric #include "clang/AST/DeclTemplate.h"
220b57cec5SDimitry Andric #include "clang/AST/DeclarationName.h"
235ffd83dbSDimitry Andric #include "clang/AST/DependenceFlags.h"
240b57cec5SDimitry Andric #include "clang/AST/Expr.h"
250b57cec5SDimitry Andric #include "clang/AST/LambdaCapture.h"
260b57cec5SDimitry Andric #include "clang/AST/NestedNameSpecifier.h"
270b57cec5SDimitry Andric #include "clang/AST/TemplateBase.h"
280b57cec5SDimitry Andric #include "clang/AST/Type.h"
290b57cec5SDimitry Andric #include "clang/AST/TypeLoc.h"
300b57cec5SDimitry Andric #include "clang/Basic/LLVM.h"
310b57cec5SDimitry Andric #include "clang/Basic/OperatorKinds.h"
320b57cec5SDimitry Andric #include "clang/Basic/SourceLocation.h"
330b57cec5SDimitry Andric #include "clang/Basic/Specifiers.h"
340b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
350b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
360b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
370b57cec5SDimitry Andric #include <cassert>
380b57cec5SDimitry Andric #include <cstddef>
390b57cec5SDimitry Andric #include <cstring>
400b57cec5SDimitry Andric #include <memory>
41bdd1243dSDimitry Andric #include <optional>
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric using namespace clang;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
460b57cec5SDimitry Andric //  Child Iterators for iterating over subexpressions/substatements
470b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric bool CXXOperatorCallExpr::isInfixBinaryOp() const {
500b57cec5SDimitry Andric   // An infix binary operator is any operator with two arguments other than
510b57cec5SDimitry Andric   // operator() and operator[]. Note that none of these operators can have
520b57cec5SDimitry Andric   // default arguments, so it suffices to check the number of argument
530b57cec5SDimitry Andric   // expressions.
540b57cec5SDimitry Andric   if (getNumArgs() != 2)
550b57cec5SDimitry Andric     return false;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   switch (getOperator()) {
580b57cec5SDimitry Andric   case OO_Call: case OO_Subscript:
590b57cec5SDimitry Andric     return false;
600b57cec5SDimitry Andric   default:
610b57cec5SDimitry Andric     return true;
620b57cec5SDimitry Andric   }
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric 
65a7dea167SDimitry Andric CXXRewrittenBinaryOperator::DecomposedForm
66a7dea167SDimitry Andric CXXRewrittenBinaryOperator::getDecomposedForm() const {
67a7dea167SDimitry Andric   DecomposedForm Result = {};
68a7dea167SDimitry Andric   const Expr *E = getSemanticForm()->IgnoreImplicit();
69a7dea167SDimitry Andric 
70a7dea167SDimitry Andric   // Remove an outer '!' if it exists (only happens for a '!=' rewrite).
71a7dea167SDimitry Andric   bool SkippedNot = false;
72a7dea167SDimitry Andric   if (auto *NotEq = dyn_cast<UnaryOperator>(E)) {
73a7dea167SDimitry Andric     assert(NotEq->getOpcode() == UO_LNot);
74a7dea167SDimitry Andric     E = NotEq->getSubExpr()->IgnoreImplicit();
75a7dea167SDimitry Andric     SkippedNot = true;
76a7dea167SDimitry Andric   }
77a7dea167SDimitry Andric 
78a7dea167SDimitry Andric   // Decompose the outer binary operator.
79a7dea167SDimitry Andric   if (auto *BO = dyn_cast<BinaryOperator>(E)) {
80a7dea167SDimitry Andric     assert(!SkippedNot || BO->getOpcode() == BO_EQ);
81a7dea167SDimitry Andric     Result.Opcode = SkippedNot ? BO_NE : BO->getOpcode();
82a7dea167SDimitry Andric     Result.LHS = BO->getLHS();
83a7dea167SDimitry Andric     Result.RHS = BO->getRHS();
84a7dea167SDimitry Andric     Result.InnerBinOp = BO;
85a7dea167SDimitry Andric   } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
86a7dea167SDimitry Andric     assert(!SkippedNot || BO->getOperator() == OO_EqualEqual);
87a7dea167SDimitry Andric     assert(BO->isInfixBinaryOp());
88a7dea167SDimitry Andric     switch (BO->getOperator()) {
89a7dea167SDimitry Andric     case OO_Less: Result.Opcode = BO_LT; break;
90a7dea167SDimitry Andric     case OO_LessEqual: Result.Opcode = BO_LE; break;
91a7dea167SDimitry Andric     case OO_Greater: Result.Opcode = BO_GT; break;
92a7dea167SDimitry Andric     case OO_GreaterEqual: Result.Opcode = BO_GE; break;
93a7dea167SDimitry Andric     case OO_Spaceship: Result.Opcode = BO_Cmp; break;
94a7dea167SDimitry Andric     case OO_EqualEqual: Result.Opcode = SkippedNot ? BO_NE : BO_EQ; break;
95a7dea167SDimitry Andric     default: llvm_unreachable("unexpected binop in rewritten operator expr");
96a7dea167SDimitry Andric     }
97a7dea167SDimitry Andric     Result.LHS = BO->getArg(0);
98a7dea167SDimitry Andric     Result.RHS = BO->getArg(1);
99a7dea167SDimitry Andric     Result.InnerBinOp = BO;
100a7dea167SDimitry Andric   } else {
101a7dea167SDimitry Andric     llvm_unreachable("unexpected rewritten operator form");
102a7dea167SDimitry Andric   }
103a7dea167SDimitry Andric 
104a7dea167SDimitry Andric   // Put the operands in the right order for == and !=, and canonicalize the
105a7dea167SDimitry Andric   // <=> subexpression onto the LHS for all other forms.
106a7dea167SDimitry Andric   if (isReversed())
107a7dea167SDimitry Andric     std::swap(Result.LHS, Result.RHS);
108a7dea167SDimitry Andric 
109a7dea167SDimitry Andric   // If this isn't a spaceship rewrite, we're done.
110a7dea167SDimitry Andric   if (Result.Opcode == BO_EQ || Result.Opcode == BO_NE)
111a7dea167SDimitry Andric     return Result;
112a7dea167SDimitry Andric 
113a7dea167SDimitry Andric   // Otherwise, we expect a <=> to now be on the LHS.
114*5f757f3fSDimitry Andric   E = Result.LHS->IgnoreUnlessSpelledInSource();
115a7dea167SDimitry Andric   if (auto *BO = dyn_cast<BinaryOperator>(E)) {
116a7dea167SDimitry Andric     assert(BO->getOpcode() == BO_Cmp);
117a7dea167SDimitry Andric     Result.LHS = BO->getLHS();
118a7dea167SDimitry Andric     Result.RHS = BO->getRHS();
119a7dea167SDimitry Andric     Result.InnerBinOp = BO;
120a7dea167SDimitry Andric   } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
121a7dea167SDimitry Andric     assert(BO->getOperator() == OO_Spaceship);
122a7dea167SDimitry Andric     Result.LHS = BO->getArg(0);
123a7dea167SDimitry Andric     Result.RHS = BO->getArg(1);
124a7dea167SDimitry Andric     Result.InnerBinOp = BO;
125a7dea167SDimitry Andric   } else {
126a7dea167SDimitry Andric     llvm_unreachable("unexpected rewritten operator form");
127a7dea167SDimitry Andric   }
128a7dea167SDimitry Andric 
129a7dea167SDimitry Andric   // Put the comparison operands in the right order.
130a7dea167SDimitry Andric   if (isReversed())
131a7dea167SDimitry Andric     std::swap(Result.LHS, Result.RHS);
132a7dea167SDimitry Andric   return Result;
133a7dea167SDimitry Andric }
134a7dea167SDimitry Andric 
1350b57cec5SDimitry Andric bool CXXTypeidExpr::isPotentiallyEvaluated() const {
1360b57cec5SDimitry Andric   if (isTypeOperand())
1370b57cec5SDimitry Andric     return false;
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   // C++11 [expr.typeid]p3:
1400b57cec5SDimitry Andric   //   When typeid is applied to an expression other than a glvalue of
1410b57cec5SDimitry Andric   //   polymorphic class type, [...] the expression is an unevaluated operand.
1420b57cec5SDimitry Andric   const Expr *E = getExprOperand();
1430b57cec5SDimitry Andric   if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
1440b57cec5SDimitry Andric     if (RD->isPolymorphic() && E->isGLValue())
1450b57cec5SDimitry Andric       return true;
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   return false;
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric 
150e8d8bef9SDimitry Andric bool CXXTypeidExpr::isMostDerived(ASTContext &Context) const {
151e8d8bef9SDimitry Andric   assert(!isTypeOperand() && "Cannot call isMostDerived for typeid(type)");
152e8d8bef9SDimitry Andric   const Expr *E = getExprOperand()->IgnoreParenNoopCasts(Context);
153e8d8bef9SDimitry Andric   if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
154e8d8bef9SDimitry Andric     QualType Ty = DRE->getDecl()->getType();
155e8d8bef9SDimitry Andric     if (!Ty->isPointerType() && !Ty->isReferenceType())
156e8d8bef9SDimitry Andric       return true;
157e8d8bef9SDimitry Andric   }
158e8d8bef9SDimitry Andric 
159e8d8bef9SDimitry Andric   return false;
160e8d8bef9SDimitry Andric }
161e8d8bef9SDimitry Andric 
1620b57cec5SDimitry Andric QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
1630b57cec5SDimitry Andric   assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
1640b57cec5SDimitry Andric   Qualifiers Quals;
1650b57cec5SDimitry Andric   return Context.getUnqualifiedArrayType(
1660b57cec5SDimitry Andric       Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
1670b57cec5SDimitry Andric }
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
1700b57cec5SDimitry Andric   assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
1710b57cec5SDimitry Andric   Qualifiers Quals;
1720b57cec5SDimitry Andric   return Context.getUnqualifiedArrayType(
1730b57cec5SDimitry Andric       Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric // CXXScalarValueInitExpr
1770b57cec5SDimitry Andric SourceLocation CXXScalarValueInitExpr::getBeginLoc() const {
1780b57cec5SDimitry Andric   return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : getRParenLoc();
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric // CXXNewExpr
1820b57cec5SDimitry Andric CXXNewExpr::CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
1830b57cec5SDimitry Andric                        FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
1840b57cec5SDimitry Andric                        bool UsualArrayDeleteWantsSize,
1850b57cec5SDimitry Andric                        ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
186bdd1243dSDimitry Andric                        std::optional<Expr *> ArraySize,
187*5f757f3fSDimitry Andric                        CXXNewInitializationStyle InitializationStyle,
1880b57cec5SDimitry Andric                        Expr *Initializer, QualType Ty,
1890b57cec5SDimitry Andric                        TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
1900b57cec5SDimitry Andric                        SourceRange DirectInitRange)
191fe6060f1SDimitry Andric     : Expr(CXXNewExprClass, Ty, VK_PRValue, OK_Ordinary),
1920b57cec5SDimitry Andric       OperatorNew(OperatorNew), OperatorDelete(OperatorDelete),
1930b57cec5SDimitry Andric       AllocatedTypeInfo(AllocatedTypeInfo), Range(Range),
1940b57cec5SDimitry Andric       DirectInitRange(DirectInitRange) {
1950b57cec5SDimitry Andric 
196*5f757f3fSDimitry Andric   assert((Initializer != nullptr ||
197*5f757f3fSDimitry Andric           InitializationStyle == CXXNewInitializationStyle::None ||
198*5f757f3fSDimitry Andric           InitializationStyle == CXXNewInitializationStyle::Implicit) &&
1990b57cec5SDimitry Andric          "Only NoInit can have no initializer!");
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   CXXNewExprBits.IsGlobalNew = IsGlobalNew;
20281ad6265SDimitry Andric   CXXNewExprBits.IsArray = ArraySize.has_value();
2030b57cec5SDimitry Andric   CXXNewExprBits.ShouldPassAlignment = ShouldPassAlignment;
2040b57cec5SDimitry Andric   CXXNewExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
2050b57cec5SDimitry Andric   CXXNewExprBits.StoredInitializationStyle =
206*5f757f3fSDimitry Andric       llvm::to_underlying(InitializationStyle);
2070b57cec5SDimitry Andric   bool IsParenTypeId = TypeIdParens.isValid();
2080b57cec5SDimitry Andric   CXXNewExprBits.IsParenTypeId = IsParenTypeId;
2090b57cec5SDimitry Andric   CXXNewExprBits.NumPlacementArgs = PlacementArgs.size();
2100b57cec5SDimitry Andric 
2115ffd83dbSDimitry Andric   if (ArraySize)
2120b57cec5SDimitry Andric     getTrailingObjects<Stmt *>()[arraySizeOffset()] = *ArraySize;
2135ffd83dbSDimitry Andric   if (Initializer)
2140b57cec5SDimitry Andric     getTrailingObjects<Stmt *>()[initExprOffset()] = Initializer;
2155ffd83dbSDimitry Andric   for (unsigned I = 0; I != PlacementArgs.size(); ++I)
2160b57cec5SDimitry Andric     getTrailingObjects<Stmt *>()[placementNewArgsOffset() + I] =
2170b57cec5SDimitry Andric         PlacementArgs[I];
2180b57cec5SDimitry Andric   if (IsParenTypeId)
2190b57cec5SDimitry Andric     getTrailingObjects<SourceRange>()[0] = TypeIdParens;
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric   switch (getInitializationStyle()) {
222*5f757f3fSDimitry Andric   case CXXNewInitializationStyle::Call:
2230b57cec5SDimitry Andric     this->Range.setEnd(DirectInitRange.getEnd());
2240b57cec5SDimitry Andric     break;
225*5f757f3fSDimitry Andric   case CXXNewInitializationStyle::List:
2260b57cec5SDimitry Andric     this->Range.setEnd(getInitializer()->getSourceRange().getEnd());
2270b57cec5SDimitry Andric     break;
2280b57cec5SDimitry Andric   default:
2290b57cec5SDimitry Andric     if (IsParenTypeId)
2300b57cec5SDimitry Andric       this->Range.setEnd(TypeIdParens.getEnd());
2310b57cec5SDimitry Andric     break;
2320b57cec5SDimitry Andric   }
2335ffd83dbSDimitry Andric 
2345ffd83dbSDimitry Andric   setDependence(computeDependence(this));
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric CXXNewExpr::CXXNewExpr(EmptyShell Empty, bool IsArray,
2380b57cec5SDimitry Andric                        unsigned NumPlacementArgs, bool IsParenTypeId)
2390b57cec5SDimitry Andric     : Expr(CXXNewExprClass, Empty) {
2400b57cec5SDimitry Andric   CXXNewExprBits.IsArray = IsArray;
2410b57cec5SDimitry Andric   CXXNewExprBits.NumPlacementArgs = NumPlacementArgs;
2420b57cec5SDimitry Andric   CXXNewExprBits.IsParenTypeId = IsParenTypeId;
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
245*5f757f3fSDimitry Andric CXXNewExpr *CXXNewExpr::Create(
246*5f757f3fSDimitry Andric     const ASTContext &Ctx, bool IsGlobalNew, FunctionDecl *OperatorNew,
247*5f757f3fSDimitry Andric     FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
248*5f757f3fSDimitry Andric     bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
249*5f757f3fSDimitry Andric     SourceRange TypeIdParens, std::optional<Expr *> ArraySize,
250*5f757f3fSDimitry Andric     CXXNewInitializationStyle InitializationStyle, Expr *Initializer,
251*5f757f3fSDimitry Andric     QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
252*5f757f3fSDimitry Andric     SourceRange DirectInitRange) {
25381ad6265SDimitry Andric   bool IsArray = ArraySize.has_value();
2540b57cec5SDimitry Andric   bool HasInit = Initializer != nullptr;
2550b57cec5SDimitry Andric   unsigned NumPlacementArgs = PlacementArgs.size();
2560b57cec5SDimitry Andric   bool IsParenTypeId = TypeIdParens.isValid();
2570b57cec5SDimitry Andric   void *Mem =
2580b57cec5SDimitry Andric       Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
2590b57cec5SDimitry Andric                        IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
2600b57cec5SDimitry Andric                    alignof(CXXNewExpr));
2610b57cec5SDimitry Andric   return new (Mem)
2620b57cec5SDimitry Andric       CXXNewExpr(IsGlobalNew, OperatorNew, OperatorDelete, ShouldPassAlignment,
2630b57cec5SDimitry Andric                  UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens,
2640b57cec5SDimitry Andric                  ArraySize, InitializationStyle, Initializer, Ty,
2650b57cec5SDimitry Andric                  AllocatedTypeInfo, Range, DirectInitRange);
2660b57cec5SDimitry Andric }
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric CXXNewExpr *CXXNewExpr::CreateEmpty(const ASTContext &Ctx, bool IsArray,
2690b57cec5SDimitry Andric                                     bool HasInit, unsigned NumPlacementArgs,
2700b57cec5SDimitry Andric                                     bool IsParenTypeId) {
2710b57cec5SDimitry Andric   void *Mem =
2720b57cec5SDimitry Andric       Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
2730b57cec5SDimitry Andric                        IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
2740b57cec5SDimitry Andric                    alignof(CXXNewExpr));
2750b57cec5SDimitry Andric   return new (Mem)
2760b57cec5SDimitry Andric       CXXNewExpr(EmptyShell(), IsArray, NumPlacementArgs, IsParenTypeId);
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric bool CXXNewExpr::shouldNullCheckAllocation() const {
28006c3fb27SDimitry Andric   if (getOperatorNew()->getLangOpts().CheckNew)
28106c3fb27SDimitry Andric     return true;
282fe6060f1SDimitry Andric   return !getOperatorNew()->hasAttr<ReturnsNonNullAttr>() &&
283fe6060f1SDimitry Andric          getOperatorNew()
2840b57cec5SDimitry Andric              ->getType()
2850b57cec5SDimitry Andric              ->castAs<FunctionProtoType>()
2860b57cec5SDimitry Andric              ->isNothrow() &&
2870b57cec5SDimitry Andric          !getOperatorNew()->isReservedGlobalPlacementOperator();
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric // CXXDeleteExpr
2910b57cec5SDimitry Andric QualType CXXDeleteExpr::getDestroyedType() const {
2920b57cec5SDimitry Andric   const Expr *Arg = getArgument();
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric   // For a destroying operator delete, we may have implicitly converted the
2950b57cec5SDimitry Andric   // pointer type to the type of the parameter of the 'operator delete'
2960b57cec5SDimitry Andric   // function.
2970b57cec5SDimitry Andric   while (const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
2980b57cec5SDimitry Andric     if (ICE->getCastKind() == CK_DerivedToBase ||
2990b57cec5SDimitry Andric         ICE->getCastKind() == CK_UncheckedDerivedToBase ||
3000b57cec5SDimitry Andric         ICE->getCastKind() == CK_NoOp) {
3010b57cec5SDimitry Andric       assert((ICE->getCastKind() == CK_NoOp ||
3020b57cec5SDimitry Andric               getOperatorDelete()->isDestroyingOperatorDelete()) &&
3030b57cec5SDimitry Andric              "only a destroying operator delete can have a converted arg");
3040b57cec5SDimitry Andric       Arg = ICE->getSubExpr();
3050b57cec5SDimitry Andric     } else
3060b57cec5SDimitry Andric       break;
3070b57cec5SDimitry Andric   }
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric   // The type-to-delete may not be a pointer if it's a dependent type.
3100b57cec5SDimitry Andric   const QualType ArgType = Arg->getType();
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric   if (ArgType->isDependentType() && !ArgType->isPointerType())
3130b57cec5SDimitry Andric     return QualType();
3140b57cec5SDimitry Andric 
315a7dea167SDimitry Andric   return ArgType->castAs<PointerType>()->getPointeeType();
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric // CXXPseudoDestructorExpr
3190b57cec5SDimitry Andric PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
3200b57cec5SDimitry Andric     : Type(Info) {
321bdd1243dSDimitry Andric   Location = Info->getTypeLoc().getBeginLoc();
3220b57cec5SDimitry Andric }
3230b57cec5SDimitry Andric 
3245ffd83dbSDimitry Andric CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(
3255ffd83dbSDimitry Andric     const ASTContext &Context, Expr *Base, bool isArrow,
3265ffd83dbSDimitry Andric     SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
3275ffd83dbSDimitry Andric     TypeSourceInfo *ScopeType, SourceLocation ColonColonLoc,
3285ffd83dbSDimitry Andric     SourceLocation TildeLoc, PseudoDestructorTypeStorage DestroyedType)
329fe6060f1SDimitry Andric     : Expr(CXXPseudoDestructorExprClass, Context.BoundMemberTy, VK_PRValue,
3305ffd83dbSDimitry Andric            OK_Ordinary),
3310b57cec5SDimitry Andric       Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
3320b57cec5SDimitry Andric       OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
3330b57cec5SDimitry Andric       ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
3345ffd83dbSDimitry Andric       DestroyedType(DestroyedType) {
3355ffd83dbSDimitry Andric   setDependence(computeDependence(this));
3365ffd83dbSDimitry Andric }
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric QualType CXXPseudoDestructorExpr::getDestroyedType() const {
3390b57cec5SDimitry Andric   if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
3400b57cec5SDimitry Andric     return TInfo->getType();
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   return QualType();
3430b57cec5SDimitry Andric }
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric SourceLocation CXXPseudoDestructorExpr::getEndLoc() const {
3460b57cec5SDimitry Andric   SourceLocation End = DestroyedType.getLocation();
3470b57cec5SDimitry Andric   if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
348bdd1243dSDimitry Andric     End = TInfo->getTypeLoc().getSourceRange().getEnd();
3490b57cec5SDimitry Andric   return End;
3500b57cec5SDimitry Andric }
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric // UnresolvedLookupExpr
3530b57cec5SDimitry Andric UnresolvedLookupExpr::UnresolvedLookupExpr(
3540b57cec5SDimitry Andric     const ASTContext &Context, CXXRecordDecl *NamingClass,
3550b57cec5SDimitry Andric     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
3560b57cec5SDimitry Andric     const DeclarationNameInfo &NameInfo, bool RequiresADL, bool Overloaded,
3570b57cec5SDimitry Andric     const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
358*5f757f3fSDimitry Andric     UnresolvedSetIterator End, bool KnownDependent)
3590b57cec5SDimitry Andric     : OverloadExpr(UnresolvedLookupExprClass, Context, QualifierLoc,
360*5f757f3fSDimitry Andric                    TemplateKWLoc, NameInfo, TemplateArgs, Begin, End,
361*5f757f3fSDimitry Andric                    KnownDependent, false, false),
3620b57cec5SDimitry Andric       NamingClass(NamingClass) {
3630b57cec5SDimitry Andric   UnresolvedLookupExprBits.RequiresADL = RequiresADL;
3640b57cec5SDimitry Andric   UnresolvedLookupExprBits.Overloaded = Overloaded;
3650b57cec5SDimitry Andric }
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric UnresolvedLookupExpr::UnresolvedLookupExpr(EmptyShell Empty,
3680b57cec5SDimitry Andric                                            unsigned NumResults,
3690b57cec5SDimitry Andric                                            bool HasTemplateKWAndArgsInfo)
3700b57cec5SDimitry Andric     : OverloadExpr(UnresolvedLookupExprClass, Empty, NumResults,
3710b57cec5SDimitry Andric                    HasTemplateKWAndArgsInfo) {}
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
3740b57cec5SDimitry Andric     const ASTContext &Context, CXXRecordDecl *NamingClass,
3750b57cec5SDimitry Andric     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
3760b57cec5SDimitry Andric     bool RequiresADL, bool Overloaded, UnresolvedSetIterator Begin,
3770b57cec5SDimitry Andric     UnresolvedSetIterator End) {
3780b57cec5SDimitry Andric   unsigned NumResults = End - Begin;
3790b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
3800b57cec5SDimitry Andric                                    TemplateArgumentLoc>(NumResults, 0, 0);
3810b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
3820b57cec5SDimitry Andric   return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc,
3830b57cec5SDimitry Andric                                         SourceLocation(), NameInfo, RequiresADL,
384*5f757f3fSDimitry Andric                                         Overloaded, nullptr, Begin, End, false);
3850b57cec5SDimitry Andric }
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
3880b57cec5SDimitry Andric     const ASTContext &Context, CXXRecordDecl *NamingClass,
3890b57cec5SDimitry Andric     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
3900b57cec5SDimitry Andric     const DeclarationNameInfo &NameInfo, bool RequiresADL,
3910b57cec5SDimitry Andric     const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
392*5f757f3fSDimitry Andric     UnresolvedSetIterator End, bool KnownDependent) {
3930b57cec5SDimitry Andric   assert(Args || TemplateKWLoc.isValid());
3940b57cec5SDimitry Andric   unsigned NumResults = End - Begin;
3950b57cec5SDimitry Andric   unsigned NumTemplateArgs = Args ? Args->size() : 0;
3960b57cec5SDimitry Andric   unsigned Size =
3970b57cec5SDimitry Andric       totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
3980b57cec5SDimitry Andric                        TemplateArgumentLoc>(NumResults, 1, NumTemplateArgs);
3990b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
400*5f757f3fSDimitry Andric   return new (Mem) UnresolvedLookupExpr(
401*5f757f3fSDimitry Andric       Context, NamingClass, QualifierLoc, TemplateKWLoc, NameInfo, RequiresADL,
402*5f757f3fSDimitry Andric       /*Overloaded=*/true, Args, Begin, End, KnownDependent);
4030b57cec5SDimitry Andric }
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric UnresolvedLookupExpr *UnresolvedLookupExpr::CreateEmpty(
4060b57cec5SDimitry Andric     const ASTContext &Context, unsigned NumResults,
4070b57cec5SDimitry Andric     bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
4080b57cec5SDimitry Andric   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
4090b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
4100b57cec5SDimitry Andric                                    TemplateArgumentLoc>(
4110b57cec5SDimitry Andric       NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
4120b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
4130b57cec5SDimitry Andric   return new (Mem)
4140b57cec5SDimitry Andric       UnresolvedLookupExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric OverloadExpr::OverloadExpr(StmtClass SC, const ASTContext &Context,
4180b57cec5SDimitry Andric                            NestedNameSpecifierLoc QualifierLoc,
4190b57cec5SDimitry Andric                            SourceLocation TemplateKWLoc,
4200b57cec5SDimitry Andric                            const DeclarationNameInfo &NameInfo,
4210b57cec5SDimitry Andric                            const TemplateArgumentListInfo *TemplateArgs,
4220b57cec5SDimitry Andric                            UnresolvedSetIterator Begin,
4230b57cec5SDimitry Andric                            UnresolvedSetIterator End, bool KnownDependent,
4240b57cec5SDimitry Andric                            bool KnownInstantiationDependent,
4250b57cec5SDimitry Andric                            bool KnownContainsUnexpandedParameterPack)
4265ffd83dbSDimitry Andric     : Expr(SC, Context.OverloadTy, VK_LValue, OK_Ordinary), NameInfo(NameInfo),
4275ffd83dbSDimitry Andric       QualifierLoc(QualifierLoc) {
4280b57cec5SDimitry Andric   unsigned NumResults = End - Begin;
4290b57cec5SDimitry Andric   OverloadExprBits.NumResults = NumResults;
4300b57cec5SDimitry Andric   OverloadExprBits.HasTemplateKWAndArgsInfo =
4310b57cec5SDimitry Andric       (TemplateArgs != nullptr ) || TemplateKWLoc.isValid();
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric   if (NumResults) {
4340b57cec5SDimitry Andric     // Copy the results to the trailing array past UnresolvedLookupExpr
4350b57cec5SDimitry Andric     // or UnresolvedMemberExpr.
4360b57cec5SDimitry Andric     DeclAccessPair *Results = getTrailingResults();
4370b57cec5SDimitry Andric     memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
4380b57cec5SDimitry Andric   }
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric   if (TemplateArgs) {
4415ffd83dbSDimitry Andric     auto Deps = TemplateArgumentDependence::None;
4420b57cec5SDimitry Andric     getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
4435ffd83dbSDimitry Andric         TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(), Deps);
4440b57cec5SDimitry Andric   } else if (TemplateKWLoc.isValid()) {
4450b57cec5SDimitry Andric     getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
4460b57cec5SDimitry Andric   }
4470b57cec5SDimitry Andric 
4485ffd83dbSDimitry Andric   setDependence(computeDependence(this, KnownDependent,
4495ffd83dbSDimitry Andric                                   KnownInstantiationDependent,
4505ffd83dbSDimitry Andric                                   KnownContainsUnexpandedParameterPack));
4510b57cec5SDimitry Andric   if (isTypeDependent())
4520b57cec5SDimitry Andric     setType(Context.DependentTy);
4530b57cec5SDimitry Andric }
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric OverloadExpr::OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
4560b57cec5SDimitry Andric                            bool HasTemplateKWAndArgsInfo)
4570b57cec5SDimitry Andric     : Expr(SC, Empty) {
4580b57cec5SDimitry Andric   OverloadExprBits.NumResults = NumResults;
4590b57cec5SDimitry Andric   OverloadExprBits.HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
4600b57cec5SDimitry Andric }
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric // DependentScopeDeclRefExpr
4630b57cec5SDimitry Andric DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(
4640b57cec5SDimitry Andric     QualType Ty, NestedNameSpecifierLoc QualifierLoc,
4650b57cec5SDimitry Andric     SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
4660b57cec5SDimitry Andric     const TemplateArgumentListInfo *Args)
4675ffd83dbSDimitry Andric     : Expr(DependentScopeDeclRefExprClass, Ty, VK_LValue, OK_Ordinary),
4680b57cec5SDimitry Andric       QualifierLoc(QualifierLoc), NameInfo(NameInfo) {
4690b57cec5SDimitry Andric   DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
4700b57cec5SDimitry Andric       (Args != nullptr) || TemplateKWLoc.isValid();
4710b57cec5SDimitry Andric   if (Args) {
4725ffd83dbSDimitry Andric     auto Deps = TemplateArgumentDependence::None;
4730b57cec5SDimitry Andric     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
4745ffd83dbSDimitry Andric         TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(), Deps);
4750b57cec5SDimitry Andric   } else if (TemplateKWLoc.isValid()) {
4760b57cec5SDimitry Andric     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
4770b57cec5SDimitry Andric         TemplateKWLoc);
4780b57cec5SDimitry Andric   }
4795ffd83dbSDimitry Andric   setDependence(computeDependence(this));
4800b57cec5SDimitry Andric }
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric DependentScopeDeclRefExpr *DependentScopeDeclRefExpr::Create(
4830b57cec5SDimitry Andric     const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
4840b57cec5SDimitry Andric     SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
4850b57cec5SDimitry Andric     const TemplateArgumentListInfo *Args) {
4860b57cec5SDimitry Andric   assert(QualifierLoc && "should be created for dependent qualifiers");
4870b57cec5SDimitry Andric   bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
4880b57cec5SDimitry Andric   std::size_t Size =
4890b57cec5SDimitry Andric       totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
4900b57cec5SDimitry Andric           HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
4910b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size);
4920b57cec5SDimitry Andric   return new (Mem) DependentScopeDeclRefExpr(Context.DependentTy, QualifierLoc,
4930b57cec5SDimitry Andric                                              TemplateKWLoc, NameInfo, Args);
4940b57cec5SDimitry Andric }
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric DependentScopeDeclRefExpr *
4970b57cec5SDimitry Andric DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &Context,
4980b57cec5SDimitry Andric                                        bool HasTemplateKWAndArgsInfo,
4990b57cec5SDimitry Andric                                        unsigned NumTemplateArgs) {
5000b57cec5SDimitry Andric   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
5010b57cec5SDimitry Andric   std::size_t Size =
5020b57cec5SDimitry Andric       totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
5030b57cec5SDimitry Andric           HasTemplateKWAndArgsInfo, NumTemplateArgs);
5040b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size);
5050b57cec5SDimitry Andric   auto *E = new (Mem) DependentScopeDeclRefExpr(
5060b57cec5SDimitry Andric       QualType(), NestedNameSpecifierLoc(), SourceLocation(),
5070b57cec5SDimitry Andric       DeclarationNameInfo(), nullptr);
5080b57cec5SDimitry Andric   E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
5090b57cec5SDimitry Andric       HasTemplateKWAndArgsInfo;
5100b57cec5SDimitry Andric   return E;
5110b57cec5SDimitry Andric }
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric SourceLocation CXXConstructExpr::getBeginLoc() const {
5140b57cec5SDimitry Andric   if (isa<CXXTemporaryObjectExpr>(this))
5150b57cec5SDimitry Andric     return cast<CXXTemporaryObjectExpr>(this)->getBeginLoc();
5160b57cec5SDimitry Andric   return getLocation();
5170b57cec5SDimitry Andric }
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric SourceLocation CXXConstructExpr::getEndLoc() const {
5200b57cec5SDimitry Andric   if (isa<CXXTemporaryObjectExpr>(this))
5210b57cec5SDimitry Andric     return cast<CXXTemporaryObjectExpr>(this)->getEndLoc();
5220b57cec5SDimitry Andric 
5230b57cec5SDimitry Andric   if (ParenOrBraceRange.isValid())
5240b57cec5SDimitry Andric     return ParenOrBraceRange.getEnd();
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric   SourceLocation End = getLocation();
5270b57cec5SDimitry Andric   for (unsigned I = getNumArgs(); I > 0; --I) {
5280b57cec5SDimitry Andric     const Expr *Arg = getArg(I-1);
5290b57cec5SDimitry Andric     if (!Arg->isDefaultArgument()) {
5300b57cec5SDimitry Andric       SourceLocation NewEnd = Arg->getEndLoc();
5310b57cec5SDimitry Andric       if (NewEnd.isValid()) {
5320b57cec5SDimitry Andric         End = NewEnd;
5330b57cec5SDimitry Andric         break;
5340b57cec5SDimitry Andric       }
5350b57cec5SDimitry Andric     }
5360b57cec5SDimitry Andric   }
5370b57cec5SDimitry Andric 
5380b57cec5SDimitry Andric   return End;
5390b57cec5SDimitry Andric }
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric CXXOperatorCallExpr::CXXOperatorCallExpr(OverloadedOperatorKind OpKind,
5420b57cec5SDimitry Andric                                          Expr *Fn, ArrayRef<Expr *> Args,
5430b57cec5SDimitry Andric                                          QualType Ty, ExprValueKind VK,
5440b57cec5SDimitry Andric                                          SourceLocation OperatorLoc,
5455ffd83dbSDimitry Andric                                          FPOptionsOverride FPFeatures,
5460b57cec5SDimitry Andric                                          ADLCallKind UsesADL)
5470b57cec5SDimitry Andric     : CallExpr(CXXOperatorCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
548e8d8bef9SDimitry Andric                OperatorLoc, FPFeatures, /*MinNumArgs=*/0, UsesADL) {
5490b57cec5SDimitry Andric   CXXOperatorCallExprBits.OperatorKind = OpKind;
5500b57cec5SDimitry Andric   assert(
5510b57cec5SDimitry Andric       (CXXOperatorCallExprBits.OperatorKind == static_cast<unsigned>(OpKind)) &&
5520b57cec5SDimitry Andric       "OperatorKind overflow!");
5530b57cec5SDimitry Andric   Range = getSourceRangeImpl();
5540b57cec5SDimitry Andric }
5550b57cec5SDimitry Andric 
556e8d8bef9SDimitry Andric CXXOperatorCallExpr::CXXOperatorCallExpr(unsigned NumArgs, bool HasFPFeatures,
557e8d8bef9SDimitry Andric                                          EmptyShell Empty)
558e8d8bef9SDimitry Andric     : CallExpr(CXXOperatorCallExprClass, /*NumPreArgs=*/0, NumArgs,
559e8d8bef9SDimitry Andric                HasFPFeatures, Empty) {}
5600b57cec5SDimitry Andric 
5615ffd83dbSDimitry Andric CXXOperatorCallExpr *
5625ffd83dbSDimitry Andric CXXOperatorCallExpr::Create(const ASTContext &Ctx,
5635ffd83dbSDimitry Andric                             OverloadedOperatorKind OpKind, Expr *Fn,
5645ffd83dbSDimitry Andric                             ArrayRef<Expr *> Args, QualType Ty,
5655ffd83dbSDimitry Andric                             ExprValueKind VK, SourceLocation OperatorLoc,
5665ffd83dbSDimitry Andric                             FPOptionsOverride FPFeatures, ADLCallKind UsesADL) {
5670b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
5680b57cec5SDimitry Andric   unsigned NumArgs = Args.size();
569e8d8bef9SDimitry Andric   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
570e8d8bef9SDimitry Andric       /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
5710b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
5720b57cec5SDimitry Andric                            alignof(CXXOperatorCallExpr));
5730b57cec5SDimitry Andric   return new (Mem) CXXOperatorCallExpr(OpKind, Fn, Args, Ty, VK, OperatorLoc,
5740b57cec5SDimitry Andric                                        FPFeatures, UsesADL);
5750b57cec5SDimitry Andric }
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric CXXOperatorCallExpr *CXXOperatorCallExpr::CreateEmpty(const ASTContext &Ctx,
5780b57cec5SDimitry Andric                                                       unsigned NumArgs,
579e8d8bef9SDimitry Andric                                                       bool HasFPFeatures,
5800b57cec5SDimitry Andric                                                       EmptyShell Empty) {
5810b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
5820b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects =
583e8d8bef9SDimitry Andric       CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPFeatures);
5840b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
5850b57cec5SDimitry Andric                            alignof(CXXOperatorCallExpr));
586e8d8bef9SDimitry Andric   return new (Mem) CXXOperatorCallExpr(NumArgs, HasFPFeatures, Empty);
5870b57cec5SDimitry Andric }
5880b57cec5SDimitry Andric 
5890b57cec5SDimitry Andric SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
5900b57cec5SDimitry Andric   OverloadedOperatorKind Kind = getOperator();
5910b57cec5SDimitry Andric   if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
5920b57cec5SDimitry Andric     if (getNumArgs() == 1)
5930b57cec5SDimitry Andric       // Prefix operator
5940b57cec5SDimitry Andric       return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
5950b57cec5SDimitry Andric     else
5960b57cec5SDimitry Andric       // Postfix operator
5970b57cec5SDimitry Andric       return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
5980b57cec5SDimitry Andric   } else if (Kind == OO_Arrow) {
5995ffd83dbSDimitry Andric     return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
6000b57cec5SDimitry Andric   } else if (Kind == OO_Call) {
6010b57cec5SDimitry Andric     return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
6020b57cec5SDimitry Andric   } else if (Kind == OO_Subscript) {
6030b57cec5SDimitry Andric     return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
6040b57cec5SDimitry Andric   } else if (getNumArgs() == 1) {
6050b57cec5SDimitry Andric     return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
6060b57cec5SDimitry Andric   } else if (getNumArgs() == 2) {
6070b57cec5SDimitry Andric     return SourceRange(getArg(0)->getBeginLoc(), getArg(1)->getEndLoc());
6080b57cec5SDimitry Andric   } else {
6090b57cec5SDimitry Andric     return getOperatorLoc();
6100b57cec5SDimitry Andric   }
6110b57cec5SDimitry Andric }
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric CXXMemberCallExpr::CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args,
6140b57cec5SDimitry Andric                                      QualType Ty, ExprValueKind VK,
615e8d8bef9SDimitry Andric                                      SourceLocation RP,
616e8d8bef9SDimitry Andric                                      FPOptionsOverride FPOptions,
617e8d8bef9SDimitry Andric                                      unsigned MinNumArgs)
6180b57cec5SDimitry Andric     : CallExpr(CXXMemberCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, RP,
619e8d8bef9SDimitry Andric                FPOptions, MinNumArgs, NotADL) {}
6200b57cec5SDimitry Andric 
621e8d8bef9SDimitry Andric CXXMemberCallExpr::CXXMemberCallExpr(unsigned NumArgs, bool HasFPFeatures,
622e8d8bef9SDimitry Andric                                      EmptyShell Empty)
623e8d8bef9SDimitry Andric     : CallExpr(CXXMemberCallExprClass, /*NumPreArgs=*/0, NumArgs, HasFPFeatures,
624e8d8bef9SDimitry Andric                Empty) {}
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric CXXMemberCallExpr *CXXMemberCallExpr::Create(const ASTContext &Ctx, Expr *Fn,
6270b57cec5SDimitry Andric                                              ArrayRef<Expr *> Args, QualType Ty,
6280b57cec5SDimitry Andric                                              ExprValueKind VK,
6290b57cec5SDimitry Andric                                              SourceLocation RP,
630e8d8bef9SDimitry Andric                                              FPOptionsOverride FPFeatures,
6310b57cec5SDimitry Andric                                              unsigned MinNumArgs) {
6320b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
6330b57cec5SDimitry Andric   unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
634e8d8bef9SDimitry Andric   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
635e8d8bef9SDimitry Andric       /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
6360b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
6370b57cec5SDimitry Andric                            alignof(CXXMemberCallExpr));
638e8d8bef9SDimitry Andric   return new (Mem)
639e8d8bef9SDimitry Andric       CXXMemberCallExpr(Fn, Args, Ty, VK, RP, FPFeatures, MinNumArgs);
6400b57cec5SDimitry Andric }
6410b57cec5SDimitry Andric 
6420b57cec5SDimitry Andric CXXMemberCallExpr *CXXMemberCallExpr::CreateEmpty(const ASTContext &Ctx,
6430b57cec5SDimitry Andric                                                   unsigned NumArgs,
644e8d8bef9SDimitry Andric                                                   bool HasFPFeatures,
6450b57cec5SDimitry Andric                                                   EmptyShell Empty) {
6460b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
6470b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects =
648e8d8bef9SDimitry Andric       CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPFeatures);
6490b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
6500b57cec5SDimitry Andric                            alignof(CXXMemberCallExpr));
651e8d8bef9SDimitry Andric   return new (Mem) CXXMemberCallExpr(NumArgs, HasFPFeatures, Empty);
6520b57cec5SDimitry Andric }
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
6550b57cec5SDimitry Andric   const Expr *Callee = getCallee()->IgnoreParens();
6560b57cec5SDimitry Andric   if (const auto *MemExpr = dyn_cast<MemberExpr>(Callee))
6570b57cec5SDimitry Andric     return MemExpr->getBase();
6580b57cec5SDimitry Andric   if (const auto *BO = dyn_cast<BinaryOperator>(Callee))
6590b57cec5SDimitry Andric     if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
6600b57cec5SDimitry Andric       return BO->getLHS();
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric   // FIXME: Will eventually need to cope with member pointers.
6630b57cec5SDimitry Andric   return nullptr;
6640b57cec5SDimitry Andric }
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric QualType CXXMemberCallExpr::getObjectType() const {
6670b57cec5SDimitry Andric   QualType Ty = getImplicitObjectArgument()->getType();
6680b57cec5SDimitry Andric   if (Ty->isPointerType())
6690b57cec5SDimitry Andric     Ty = Ty->getPointeeType();
6700b57cec5SDimitry Andric   return Ty;
6710b57cec5SDimitry Andric }
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
6740b57cec5SDimitry Andric   if (const auto *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
6750b57cec5SDimitry Andric     return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric   // FIXME: Will eventually need to cope with member pointers.
678fe6060f1SDimitry Andric   // NOTE: Update makeTailCallIfSwiftAsync on fixing this.
6790b57cec5SDimitry Andric   return nullptr;
6800b57cec5SDimitry Andric }
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
6830b57cec5SDimitry Andric   Expr* ThisArg = getImplicitObjectArgument();
6840b57cec5SDimitry Andric   if (!ThisArg)
6850b57cec5SDimitry Andric     return nullptr;
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric   if (ThisArg->getType()->isAnyPointerType())
6880b57cec5SDimitry Andric     return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric   return ThisArg->getType()->getAsCXXRecordDecl();
6910b57cec5SDimitry Andric }
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6940b57cec5SDimitry Andric //  Named casts
6950b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric /// getCastName - Get the name of the C++ cast being used, e.g.,
6980b57cec5SDimitry Andric /// "static_cast", "dynamic_cast", "reinterpret_cast", or
6990b57cec5SDimitry Andric /// "const_cast". The returned pointer must not be freed.
7000b57cec5SDimitry Andric const char *CXXNamedCastExpr::getCastName() const {
7010b57cec5SDimitry Andric   switch (getStmtClass()) {
7020b57cec5SDimitry Andric   case CXXStaticCastExprClass:      return "static_cast";
7030b57cec5SDimitry Andric   case CXXDynamicCastExprClass:     return "dynamic_cast";
7040b57cec5SDimitry Andric   case CXXReinterpretCastExprClass: return "reinterpret_cast";
7050b57cec5SDimitry Andric   case CXXConstCastExprClass:       return "const_cast";
7065ffd83dbSDimitry Andric   case CXXAddrspaceCastExprClass:   return "addrspace_cast";
7070b57cec5SDimitry Andric   default:                          return "<invalid cast>";
7080b57cec5SDimitry Andric   }
7090b57cec5SDimitry Andric }
7100b57cec5SDimitry Andric 
711e8d8bef9SDimitry Andric CXXStaticCastExpr *
712e8d8bef9SDimitry Andric CXXStaticCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
713e8d8bef9SDimitry Andric                           CastKind K, Expr *Op, const CXXCastPath *BasePath,
714e8d8bef9SDimitry Andric                           TypeSourceInfo *WrittenTy, FPOptionsOverride FPO,
715e8d8bef9SDimitry Andric                           SourceLocation L, SourceLocation RParenLoc,
7160b57cec5SDimitry Andric                           SourceRange AngleBrackets) {
7170b57cec5SDimitry Andric   unsigned PathSize = (BasePath ? BasePath->size() : 0);
718e8d8bef9SDimitry Andric   void *Buffer =
719e8d8bef9SDimitry Andric       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
720e8d8bef9SDimitry Andric           PathSize, FPO.requiresTrailingStorage()));
721e8d8bef9SDimitry Andric   auto *E = new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy,
722e8d8bef9SDimitry Andric                                            FPO, L, RParenLoc, AngleBrackets);
7230b57cec5SDimitry Andric   if (PathSize)
7240b57cec5SDimitry Andric     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
7250b57cec5SDimitry Andric                               E->getTrailingObjects<CXXBaseSpecifier *>());
7260b57cec5SDimitry Andric   return E;
7270b57cec5SDimitry Andric }
7280b57cec5SDimitry Andric 
7290b57cec5SDimitry Andric CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
730e8d8bef9SDimitry Andric                                                   unsigned PathSize,
731e8d8bef9SDimitry Andric                                                   bool HasFPFeatures) {
732e8d8bef9SDimitry Andric   void *Buffer =
733e8d8bef9SDimitry Andric       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
734e8d8bef9SDimitry Andric           PathSize, HasFPFeatures));
735e8d8bef9SDimitry Andric   return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize, HasFPFeatures);
7360b57cec5SDimitry Andric }
7370b57cec5SDimitry Andric 
7380b57cec5SDimitry Andric CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
7390b57cec5SDimitry Andric                                                ExprValueKind VK,
7400b57cec5SDimitry Andric                                                CastKind K, Expr *Op,
7410b57cec5SDimitry Andric                                                const CXXCastPath *BasePath,
7420b57cec5SDimitry Andric                                                TypeSourceInfo *WrittenTy,
7430b57cec5SDimitry Andric                                                SourceLocation L,
7440b57cec5SDimitry Andric                                                SourceLocation RParenLoc,
7450b57cec5SDimitry Andric                                                SourceRange AngleBrackets) {
7460b57cec5SDimitry Andric   unsigned PathSize = (BasePath ? BasePath->size() : 0);
7470b57cec5SDimitry Andric   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
7480b57cec5SDimitry Andric   auto *E =
7490b57cec5SDimitry Andric       new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
7500b57cec5SDimitry Andric                                       RParenLoc, AngleBrackets);
7510b57cec5SDimitry Andric   if (PathSize)
7520b57cec5SDimitry Andric     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
7530b57cec5SDimitry Andric                               E->getTrailingObjects<CXXBaseSpecifier *>());
7540b57cec5SDimitry Andric   return E;
7550b57cec5SDimitry Andric }
7560b57cec5SDimitry Andric 
7570b57cec5SDimitry Andric CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
7580b57cec5SDimitry Andric                                                     unsigned PathSize) {
7590b57cec5SDimitry Andric   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
7600b57cec5SDimitry Andric   return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
7610b57cec5SDimitry Andric }
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
7640b57cec5SDimitry Andric /// to always be null. For example:
7650b57cec5SDimitry Andric ///
7660b57cec5SDimitry Andric /// struct A { };
7670b57cec5SDimitry Andric /// struct B final : A { };
7680b57cec5SDimitry Andric /// struct C { };
7690b57cec5SDimitry Andric ///
7700b57cec5SDimitry Andric /// C *f(B* b) { return dynamic_cast<C*>(b); }
77106c3fb27SDimitry Andric bool CXXDynamicCastExpr::isAlwaysNull() const {
77206c3fb27SDimitry Andric   if (isValueDependent() || getCastKind() != CK_Dynamic)
77306c3fb27SDimitry Andric     return false;
77406c3fb27SDimitry Andric 
7750b57cec5SDimitry Andric   QualType SrcType = getSubExpr()->getType();
7760b57cec5SDimitry Andric   QualType DestType = getType();
7770b57cec5SDimitry Andric 
77806c3fb27SDimitry Andric   if (DestType->isVoidPointerType())
77906c3fb27SDimitry Andric     return false;
78006c3fb27SDimitry Andric 
78106c3fb27SDimitry Andric   if (DestType->isPointerType()) {
78206c3fb27SDimitry Andric     SrcType = SrcType->getPointeeType();
78306c3fb27SDimitry Andric     DestType = DestType->getPointeeType();
7840b57cec5SDimitry Andric   }
7850b57cec5SDimitry Andric 
78606c3fb27SDimitry Andric   const auto *SrcRD = SrcType->getAsCXXRecordDecl();
78706c3fb27SDimitry Andric   const auto *DestRD = DestType->getAsCXXRecordDecl();
78806c3fb27SDimitry Andric   assert(SrcRD && DestRD);
78906c3fb27SDimitry Andric 
79006c3fb27SDimitry Andric   if (SrcRD->isEffectivelyFinal()) {
79106c3fb27SDimitry Andric     assert(!SrcRD->isDerivedFrom(DestRD) &&
79206c3fb27SDimitry Andric            "upcasts should not use CK_Dynamic");
79306c3fb27SDimitry Andric     return true;
79406c3fb27SDimitry Andric   }
79506c3fb27SDimitry Andric 
79606c3fb27SDimitry Andric   if (DestRD->isEffectivelyFinal() && !DestRD->isDerivedFrom(SrcRD))
79706c3fb27SDimitry Andric     return true;
79806c3fb27SDimitry Andric 
7990b57cec5SDimitry Andric   return false;
8000b57cec5SDimitry Andric }
8010b57cec5SDimitry Andric 
8020b57cec5SDimitry Andric CXXReinterpretCastExpr *
8030b57cec5SDimitry Andric CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
8040b57cec5SDimitry Andric                                ExprValueKind VK, CastKind K, Expr *Op,
8050b57cec5SDimitry Andric                                const CXXCastPath *BasePath,
8060b57cec5SDimitry Andric                                TypeSourceInfo *WrittenTy, SourceLocation L,
8070b57cec5SDimitry Andric                                SourceLocation RParenLoc,
8080b57cec5SDimitry Andric                                SourceRange AngleBrackets) {
8090b57cec5SDimitry Andric   unsigned PathSize = (BasePath ? BasePath->size() : 0);
8100b57cec5SDimitry Andric   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
8110b57cec5SDimitry Andric   auto *E =
8120b57cec5SDimitry Andric       new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
8130b57cec5SDimitry Andric                                           RParenLoc, AngleBrackets);
8140b57cec5SDimitry Andric   if (PathSize)
8150b57cec5SDimitry Andric     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
8160b57cec5SDimitry Andric                               E->getTrailingObjects<CXXBaseSpecifier *>());
8170b57cec5SDimitry Andric   return E;
8180b57cec5SDimitry Andric }
8190b57cec5SDimitry Andric 
8200b57cec5SDimitry Andric CXXReinterpretCastExpr *
8210b57cec5SDimitry Andric CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
8220b57cec5SDimitry Andric   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
8230b57cec5SDimitry Andric   return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
8240b57cec5SDimitry Andric }
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
8270b57cec5SDimitry Andric                                            ExprValueKind VK, Expr *Op,
8280b57cec5SDimitry Andric                                            TypeSourceInfo *WrittenTy,
8290b57cec5SDimitry Andric                                            SourceLocation L,
8300b57cec5SDimitry Andric                                            SourceLocation RParenLoc,
8310b57cec5SDimitry Andric                                            SourceRange AngleBrackets) {
8320b57cec5SDimitry Andric   return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
8330b57cec5SDimitry Andric }
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
8360b57cec5SDimitry Andric   return new (C) CXXConstCastExpr(EmptyShell());
8370b57cec5SDimitry Andric }
8380b57cec5SDimitry Andric 
8395ffd83dbSDimitry Andric CXXAddrspaceCastExpr *
8405ffd83dbSDimitry Andric CXXAddrspaceCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
8415ffd83dbSDimitry Andric                              CastKind K, Expr *Op, TypeSourceInfo *WrittenTy,
8425ffd83dbSDimitry Andric                              SourceLocation L, SourceLocation RParenLoc,
8435ffd83dbSDimitry Andric                              SourceRange AngleBrackets) {
8445ffd83dbSDimitry Andric   return new (C) CXXAddrspaceCastExpr(T, VK, K, Op, WrittenTy, L, RParenLoc,
8455ffd83dbSDimitry Andric                                       AngleBrackets);
8465ffd83dbSDimitry Andric }
8475ffd83dbSDimitry Andric 
8485ffd83dbSDimitry Andric CXXAddrspaceCastExpr *CXXAddrspaceCastExpr::CreateEmpty(const ASTContext &C) {
8495ffd83dbSDimitry Andric   return new (C) CXXAddrspaceCastExpr(EmptyShell());
8505ffd83dbSDimitry Andric }
8515ffd83dbSDimitry Andric 
852e8d8bef9SDimitry Andric CXXFunctionalCastExpr *CXXFunctionalCastExpr::Create(
853e8d8bef9SDimitry Andric     const ASTContext &C, QualType T, ExprValueKind VK, TypeSourceInfo *Written,
854e8d8bef9SDimitry Andric     CastKind K, Expr *Op, const CXXCastPath *BasePath, FPOptionsOverride FPO,
8550b57cec5SDimitry Andric     SourceLocation L, SourceLocation R) {
8560b57cec5SDimitry Andric   unsigned PathSize = (BasePath ? BasePath->size() : 0);
857e8d8bef9SDimitry Andric   void *Buffer =
858e8d8bef9SDimitry Andric       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
859e8d8bef9SDimitry Andric           PathSize, FPO.requiresTrailingStorage()));
860e8d8bef9SDimitry Andric   auto *E = new (Buffer)
861e8d8bef9SDimitry Andric       CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, FPO, L, R);
8620b57cec5SDimitry Andric   if (PathSize)
8630b57cec5SDimitry Andric     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
8640b57cec5SDimitry Andric                               E->getTrailingObjects<CXXBaseSpecifier *>());
8650b57cec5SDimitry Andric   return E;
8660b57cec5SDimitry Andric }
8670b57cec5SDimitry Andric 
868e8d8bef9SDimitry Andric CXXFunctionalCastExpr *CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C,
869e8d8bef9SDimitry Andric                                                           unsigned PathSize,
870e8d8bef9SDimitry Andric                                                           bool HasFPFeatures) {
871e8d8bef9SDimitry Andric   void *Buffer =
872e8d8bef9SDimitry Andric       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
873e8d8bef9SDimitry Andric           PathSize, HasFPFeatures));
874e8d8bef9SDimitry Andric   return new (Buffer)
875e8d8bef9SDimitry Andric       CXXFunctionalCastExpr(EmptyShell(), PathSize, HasFPFeatures);
8760b57cec5SDimitry Andric }
8770b57cec5SDimitry Andric 
8780b57cec5SDimitry Andric SourceLocation CXXFunctionalCastExpr::getBeginLoc() const {
8790b57cec5SDimitry Andric   return getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
8800b57cec5SDimitry Andric }
8810b57cec5SDimitry Andric 
8820b57cec5SDimitry Andric SourceLocation CXXFunctionalCastExpr::getEndLoc() const {
8830b57cec5SDimitry Andric   return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getEndLoc();
8840b57cec5SDimitry Andric }
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric UserDefinedLiteral::UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args,
8870b57cec5SDimitry Andric                                        QualType Ty, ExprValueKind VK,
8880b57cec5SDimitry Andric                                        SourceLocation LitEndLoc,
889e8d8bef9SDimitry Andric                                        SourceLocation SuffixLoc,
890e8d8bef9SDimitry Andric                                        FPOptionsOverride FPFeatures)
8910b57cec5SDimitry Andric     : CallExpr(UserDefinedLiteralClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
892e8d8bef9SDimitry Andric                LitEndLoc, FPFeatures, /*MinNumArgs=*/0, NotADL),
8930b57cec5SDimitry Andric       UDSuffixLoc(SuffixLoc) {}
8940b57cec5SDimitry Andric 
895e8d8bef9SDimitry Andric UserDefinedLiteral::UserDefinedLiteral(unsigned NumArgs, bool HasFPFeatures,
896e8d8bef9SDimitry Andric                                        EmptyShell Empty)
897e8d8bef9SDimitry Andric     : CallExpr(UserDefinedLiteralClass, /*NumPreArgs=*/0, NumArgs,
898e8d8bef9SDimitry Andric                HasFPFeatures, Empty) {}
8990b57cec5SDimitry Andric 
9000b57cec5SDimitry Andric UserDefinedLiteral *UserDefinedLiteral::Create(const ASTContext &Ctx, Expr *Fn,
9010b57cec5SDimitry Andric                                                ArrayRef<Expr *> Args,
9020b57cec5SDimitry Andric                                                QualType Ty, ExprValueKind VK,
9030b57cec5SDimitry Andric                                                SourceLocation LitEndLoc,
904e8d8bef9SDimitry Andric                                                SourceLocation SuffixLoc,
905e8d8bef9SDimitry Andric                                                FPOptionsOverride FPFeatures) {
9060b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
9070b57cec5SDimitry Andric   unsigned NumArgs = Args.size();
908e8d8bef9SDimitry Andric   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
909e8d8bef9SDimitry Andric       /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
9100b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
9110b57cec5SDimitry Andric                            alignof(UserDefinedLiteral));
912e8d8bef9SDimitry Andric   return new (Mem)
913e8d8bef9SDimitry Andric       UserDefinedLiteral(Fn, Args, Ty, VK, LitEndLoc, SuffixLoc, FPFeatures);
9140b57cec5SDimitry Andric }
9150b57cec5SDimitry Andric 
9160b57cec5SDimitry Andric UserDefinedLiteral *UserDefinedLiteral::CreateEmpty(const ASTContext &Ctx,
9170b57cec5SDimitry Andric                                                     unsigned NumArgs,
918e8d8bef9SDimitry Andric                                                     bool HasFPOptions,
9190b57cec5SDimitry Andric                                                     EmptyShell Empty) {
9200b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
9210b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects =
922e8d8bef9SDimitry Andric       CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPOptions);
9230b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
9240b57cec5SDimitry Andric                            alignof(UserDefinedLiteral));
925e8d8bef9SDimitry Andric   return new (Mem) UserDefinedLiteral(NumArgs, HasFPOptions, Empty);
9260b57cec5SDimitry Andric }
9270b57cec5SDimitry Andric 
9280b57cec5SDimitry Andric UserDefinedLiteral::LiteralOperatorKind
9290b57cec5SDimitry Andric UserDefinedLiteral::getLiteralOperatorKind() const {
9300b57cec5SDimitry Andric   if (getNumArgs() == 0)
9310b57cec5SDimitry Andric     return LOK_Template;
9320b57cec5SDimitry Andric   if (getNumArgs() == 2)
9330b57cec5SDimitry Andric     return LOK_String;
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric   assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
9360b57cec5SDimitry Andric   QualType ParamTy =
9370b57cec5SDimitry Andric     cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
9380b57cec5SDimitry Andric   if (ParamTy->isPointerType())
9390b57cec5SDimitry Andric     return LOK_Raw;
9400b57cec5SDimitry Andric   if (ParamTy->isAnyCharacterType())
9410b57cec5SDimitry Andric     return LOK_Character;
9420b57cec5SDimitry Andric   if (ParamTy->isIntegerType())
9430b57cec5SDimitry Andric     return LOK_Integer;
9440b57cec5SDimitry Andric   if (ParamTy->isFloatingType())
9450b57cec5SDimitry Andric     return LOK_Floating;
9460b57cec5SDimitry Andric 
9470b57cec5SDimitry Andric   llvm_unreachable("unknown kind of literal operator");
9480b57cec5SDimitry Andric }
9490b57cec5SDimitry Andric 
9500b57cec5SDimitry Andric Expr *UserDefinedLiteral::getCookedLiteral() {
9510b57cec5SDimitry Andric #ifndef NDEBUG
9520b57cec5SDimitry Andric   LiteralOperatorKind LOK = getLiteralOperatorKind();
9530b57cec5SDimitry Andric   assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
9540b57cec5SDimitry Andric #endif
9550b57cec5SDimitry Andric   return getArg(0);
9560b57cec5SDimitry Andric }
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
9590b57cec5SDimitry Andric   return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
9600b57cec5SDimitry Andric }
9610b57cec5SDimitry Andric 
962bdd1243dSDimitry Andric CXXDefaultArgExpr *CXXDefaultArgExpr::CreateEmpty(const ASTContext &C,
963bdd1243dSDimitry Andric                                                   bool HasRewrittenInit) {
964bdd1243dSDimitry Andric   size_t Size = totalSizeToAlloc<Expr *>(HasRewrittenInit);
965bdd1243dSDimitry Andric   auto *Mem = C.Allocate(Size, alignof(CXXDefaultArgExpr));
966bdd1243dSDimitry Andric   return new (Mem) CXXDefaultArgExpr(EmptyShell(), HasRewrittenInit);
967bdd1243dSDimitry Andric }
968bdd1243dSDimitry Andric 
969bdd1243dSDimitry Andric CXXDefaultArgExpr *CXXDefaultArgExpr::Create(const ASTContext &C,
970bdd1243dSDimitry Andric                                              SourceLocation Loc,
971bdd1243dSDimitry Andric                                              ParmVarDecl *Param,
972bdd1243dSDimitry Andric                                              Expr *RewrittenExpr,
973bdd1243dSDimitry Andric                                              DeclContext *UsedContext) {
974bdd1243dSDimitry Andric   size_t Size = totalSizeToAlloc<Expr *>(RewrittenExpr != nullptr);
975bdd1243dSDimitry Andric   auto *Mem = C.Allocate(Size, alignof(CXXDefaultArgExpr));
976bdd1243dSDimitry Andric   return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
977bdd1243dSDimitry Andric                                      RewrittenExpr, UsedContext);
978bdd1243dSDimitry Andric }
979bdd1243dSDimitry Andric 
980bdd1243dSDimitry Andric Expr *CXXDefaultArgExpr::getExpr() {
981bdd1243dSDimitry Andric   return CXXDefaultArgExprBits.HasRewrittenInit ? getAdjustedRewrittenExpr()
982bdd1243dSDimitry Andric                                                 : getParam()->getDefaultArg();
983bdd1243dSDimitry Andric }
984bdd1243dSDimitry Andric 
985bdd1243dSDimitry Andric Expr *CXXDefaultArgExpr::getAdjustedRewrittenExpr() {
986bdd1243dSDimitry Andric   assert(hasRewrittenInit() &&
987bdd1243dSDimitry Andric          "expected this CXXDefaultArgExpr to have a rewritten init.");
988bdd1243dSDimitry Andric   Expr *Init = getRewrittenExpr();
989bdd1243dSDimitry Andric   if (auto *E = dyn_cast_if_present<FullExpr>(Init))
990bdd1243dSDimitry Andric     if (!isa<ConstantExpr>(E))
991bdd1243dSDimitry Andric       return E->getSubExpr();
992bdd1243dSDimitry Andric   return Init;
993bdd1243dSDimitry Andric }
994bdd1243dSDimitry Andric 
9955ffd83dbSDimitry Andric CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &Ctx,
9965ffd83dbSDimitry Andric                                        SourceLocation Loc, FieldDecl *Field,
997bdd1243dSDimitry Andric                                        QualType Ty, DeclContext *UsedContext,
998bdd1243dSDimitry Andric                                        Expr *RewrittenInitExpr)
9990b57cec5SDimitry Andric     : Expr(CXXDefaultInitExprClass, Ty.getNonLValueExprType(Ctx),
1000fe6060f1SDimitry Andric            Ty->isLValueReferenceType()   ? VK_LValue
1001fe6060f1SDimitry Andric            : Ty->isRValueReferenceType() ? VK_XValue
1002fe6060f1SDimitry Andric                                          : VK_PRValue,
10035ffd83dbSDimitry Andric            /*FIXME*/ OK_Ordinary),
10040b57cec5SDimitry Andric       Field(Field), UsedContext(UsedContext) {
10050b57cec5SDimitry Andric   CXXDefaultInitExprBits.Loc = Loc;
1006bdd1243dSDimitry Andric   CXXDefaultInitExprBits.HasRewrittenInit = RewrittenInitExpr != nullptr;
1007bdd1243dSDimitry Andric 
1008bdd1243dSDimitry Andric   if (CXXDefaultInitExprBits.HasRewrittenInit)
1009bdd1243dSDimitry Andric     *getTrailingObjects<Expr *>() = RewrittenInitExpr;
1010bdd1243dSDimitry Andric 
10110b57cec5SDimitry Andric   assert(Field->hasInClassInitializer());
10125ffd83dbSDimitry Andric 
1013e8d8bef9SDimitry Andric   setDependence(computeDependence(this));
10140b57cec5SDimitry Andric }
10150b57cec5SDimitry Andric 
1016bdd1243dSDimitry Andric CXXDefaultInitExpr *CXXDefaultInitExpr::CreateEmpty(const ASTContext &C,
1017bdd1243dSDimitry Andric                                                     bool HasRewrittenInit) {
1018bdd1243dSDimitry Andric   size_t Size = totalSizeToAlloc<Expr *>(HasRewrittenInit);
1019bdd1243dSDimitry Andric   auto *Mem = C.Allocate(Size, alignof(CXXDefaultInitExpr));
1020bdd1243dSDimitry Andric   return new (Mem) CXXDefaultInitExpr(EmptyShell(), HasRewrittenInit);
1021bdd1243dSDimitry Andric }
1022bdd1243dSDimitry Andric 
1023bdd1243dSDimitry Andric CXXDefaultInitExpr *CXXDefaultInitExpr::Create(const ASTContext &Ctx,
1024bdd1243dSDimitry Andric                                                SourceLocation Loc,
1025bdd1243dSDimitry Andric                                                FieldDecl *Field,
1026bdd1243dSDimitry Andric                                                DeclContext *UsedContext,
1027bdd1243dSDimitry Andric                                                Expr *RewrittenInitExpr) {
1028bdd1243dSDimitry Andric 
1029bdd1243dSDimitry Andric   size_t Size = totalSizeToAlloc<Expr *>(RewrittenInitExpr != nullptr);
1030bdd1243dSDimitry Andric   auto *Mem = Ctx.Allocate(Size, alignof(CXXDefaultInitExpr));
1031bdd1243dSDimitry Andric   return new (Mem) CXXDefaultInitExpr(Ctx, Loc, Field, Field->getType(),
1032bdd1243dSDimitry Andric                                       UsedContext, RewrittenInitExpr);
1033bdd1243dSDimitry Andric }
1034bdd1243dSDimitry Andric 
1035bdd1243dSDimitry Andric Expr *CXXDefaultInitExpr::getExpr() {
1036bdd1243dSDimitry Andric   assert(Field->getInClassInitializer() && "initializer hasn't been parsed");
1037bdd1243dSDimitry Andric   if (hasRewrittenInit())
1038bdd1243dSDimitry Andric     return getRewrittenExpr();
1039bdd1243dSDimitry Andric 
1040bdd1243dSDimitry Andric   return Field->getInClassInitializer();
1041bdd1243dSDimitry Andric }
1042bdd1243dSDimitry Andric 
10430b57cec5SDimitry Andric CXXTemporary *CXXTemporary::Create(const ASTContext &C,
10440b57cec5SDimitry Andric                                    const CXXDestructorDecl *Destructor) {
10450b57cec5SDimitry Andric   return new (C) CXXTemporary(Destructor);
10460b57cec5SDimitry Andric }
10470b57cec5SDimitry Andric 
10480b57cec5SDimitry Andric CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
10490b57cec5SDimitry Andric                                                    CXXTemporary *Temp,
10500b57cec5SDimitry Andric                                                    Expr* SubExpr) {
10510b57cec5SDimitry Andric   assert((SubExpr->getType()->isRecordType() ||
10520b57cec5SDimitry Andric           SubExpr->getType()->isArrayType()) &&
10530b57cec5SDimitry Andric          "Expression bound to a temporary must have record or array type!");
10540b57cec5SDimitry Andric 
10550b57cec5SDimitry Andric   return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
10560b57cec5SDimitry Andric }
10570b57cec5SDimitry Andric 
10580b57cec5SDimitry Andric CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(
10590b57cec5SDimitry Andric     CXXConstructorDecl *Cons, QualType Ty, TypeSourceInfo *TSI,
10600b57cec5SDimitry Andric     ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
10610b57cec5SDimitry Andric     bool HadMultipleCandidates, bool ListInitialization,
10620b57cec5SDimitry Andric     bool StdInitListInitialization, bool ZeroInitialization)
10630b57cec5SDimitry Andric     : CXXConstructExpr(
10640b57cec5SDimitry Andric           CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(),
10650b57cec5SDimitry Andric           Cons, /* Elidable=*/false, Args, HadMultipleCandidates,
10660b57cec5SDimitry Andric           ListInitialization, StdInitListInitialization, ZeroInitialization,
1067*5f757f3fSDimitry Andric           CXXConstructionKind::Complete, ParenOrBraceRange),
10683b7f365eSDimitry Andric       TSI(TSI) {
10693b7f365eSDimitry Andric   setDependence(computeDependence(this));
10703b7f365eSDimitry Andric }
10710b57cec5SDimitry Andric 
10720b57cec5SDimitry Andric CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(EmptyShell Empty,
10730b57cec5SDimitry Andric                                                unsigned NumArgs)
10740b57cec5SDimitry Andric     : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty, NumArgs) {}
10750b57cec5SDimitry Andric 
10760b57cec5SDimitry Andric CXXTemporaryObjectExpr *CXXTemporaryObjectExpr::Create(
10770b57cec5SDimitry Andric     const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
10780b57cec5SDimitry Andric     TypeSourceInfo *TSI, ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
10790b57cec5SDimitry Andric     bool HadMultipleCandidates, bool ListInitialization,
10800b57cec5SDimitry Andric     bool StdInitListInitialization, bool ZeroInitialization) {
10810b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
10820b57cec5SDimitry Andric   void *Mem =
10830b57cec5SDimitry Andric       Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
10840b57cec5SDimitry Andric                    alignof(CXXTemporaryObjectExpr));
10850b57cec5SDimitry Andric   return new (Mem) CXXTemporaryObjectExpr(
10860b57cec5SDimitry Andric       Cons, Ty, TSI, Args, ParenOrBraceRange, HadMultipleCandidates,
10870b57cec5SDimitry Andric       ListInitialization, StdInitListInitialization, ZeroInitialization);
10880b57cec5SDimitry Andric }
10890b57cec5SDimitry Andric 
10900b57cec5SDimitry Andric CXXTemporaryObjectExpr *
10910b57cec5SDimitry Andric CXXTemporaryObjectExpr::CreateEmpty(const ASTContext &Ctx, unsigned NumArgs) {
10920b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
10930b57cec5SDimitry Andric   void *Mem =
10940b57cec5SDimitry Andric       Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
10950b57cec5SDimitry Andric                    alignof(CXXTemporaryObjectExpr));
10960b57cec5SDimitry Andric   return new (Mem) CXXTemporaryObjectExpr(EmptyShell(), NumArgs);
10970b57cec5SDimitry Andric }
10980b57cec5SDimitry Andric 
10990b57cec5SDimitry Andric SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const {
11000b57cec5SDimitry Andric   return getTypeSourceInfo()->getTypeLoc().getBeginLoc();
11010b57cec5SDimitry Andric }
11020b57cec5SDimitry Andric 
11030b57cec5SDimitry Andric SourceLocation CXXTemporaryObjectExpr::getEndLoc() const {
11040b57cec5SDimitry Andric   SourceLocation Loc = getParenOrBraceRange().getEnd();
11050b57cec5SDimitry Andric   if (Loc.isInvalid() && getNumArgs())
11060b57cec5SDimitry Andric     Loc = getArg(getNumArgs() - 1)->getEndLoc();
11070b57cec5SDimitry Andric   return Loc;
11080b57cec5SDimitry Andric }
11090b57cec5SDimitry Andric 
11100b57cec5SDimitry Andric CXXConstructExpr *CXXConstructExpr::Create(
11110b57cec5SDimitry Andric     const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
11120b57cec5SDimitry Andric     CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
11130b57cec5SDimitry Andric     bool HadMultipleCandidates, bool ListInitialization,
11140b57cec5SDimitry Andric     bool StdInitListInitialization, bool ZeroInitialization,
1115*5f757f3fSDimitry Andric     CXXConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
11160b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
11170b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
11180b57cec5SDimitry Andric                            alignof(CXXConstructExpr));
11190b57cec5SDimitry Andric   return new (Mem) CXXConstructExpr(
11200b57cec5SDimitry Andric       CXXConstructExprClass, Ty, Loc, Ctor, Elidable, Args,
11210b57cec5SDimitry Andric       HadMultipleCandidates, ListInitialization, StdInitListInitialization,
11220b57cec5SDimitry Andric       ZeroInitialization, ConstructKind, ParenOrBraceRange);
11230b57cec5SDimitry Andric }
11240b57cec5SDimitry Andric 
11250b57cec5SDimitry Andric CXXConstructExpr *CXXConstructExpr::CreateEmpty(const ASTContext &Ctx,
11260b57cec5SDimitry Andric                                                 unsigned NumArgs) {
11270b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
11280b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
11290b57cec5SDimitry Andric                            alignof(CXXConstructExpr));
11300b57cec5SDimitry Andric   return new (Mem)
11310b57cec5SDimitry Andric       CXXConstructExpr(CXXConstructExprClass, EmptyShell(), NumArgs);
11320b57cec5SDimitry Andric }
11330b57cec5SDimitry Andric 
11340b57cec5SDimitry Andric CXXConstructExpr::CXXConstructExpr(
11350b57cec5SDimitry Andric     StmtClass SC, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor,
11360b57cec5SDimitry Andric     bool Elidable, ArrayRef<Expr *> Args, bool HadMultipleCandidates,
11370b57cec5SDimitry Andric     bool ListInitialization, bool StdInitListInitialization,
1138*5f757f3fSDimitry Andric     bool ZeroInitialization, CXXConstructionKind ConstructKind,
11390b57cec5SDimitry Andric     SourceRange ParenOrBraceRange)
1140fe6060f1SDimitry Andric     : Expr(SC, Ty, VK_PRValue, OK_Ordinary), Constructor(Ctor),
11415ffd83dbSDimitry Andric       ParenOrBraceRange(ParenOrBraceRange), NumArgs(Args.size()) {
11420b57cec5SDimitry Andric   CXXConstructExprBits.Elidable = Elidable;
11430b57cec5SDimitry Andric   CXXConstructExprBits.HadMultipleCandidates = HadMultipleCandidates;
11440b57cec5SDimitry Andric   CXXConstructExprBits.ListInitialization = ListInitialization;
11450b57cec5SDimitry Andric   CXXConstructExprBits.StdInitListInitialization = StdInitListInitialization;
11460b57cec5SDimitry Andric   CXXConstructExprBits.ZeroInitialization = ZeroInitialization;
1147*5f757f3fSDimitry Andric   CXXConstructExprBits.ConstructionKind = llvm::to_underlying(ConstructKind);
114806c3fb27SDimitry Andric   CXXConstructExprBits.IsImmediateEscalating = false;
11490b57cec5SDimitry Andric   CXXConstructExprBits.Loc = Loc;
11500b57cec5SDimitry Andric 
11510b57cec5SDimitry Andric   Stmt **TrailingArgs = getTrailingArgs();
11520b57cec5SDimitry Andric   for (unsigned I = 0, N = Args.size(); I != N; ++I) {
11530b57cec5SDimitry Andric     assert(Args[I] && "NULL argument in CXXConstructExpr!");
11540b57cec5SDimitry Andric     TrailingArgs[I] = Args[I];
11550b57cec5SDimitry Andric   }
11565ffd83dbSDimitry Andric 
11573b7f365eSDimitry Andric   // CXXTemporaryObjectExpr does this itself after setting its TypeSourceInfo.
11583b7f365eSDimitry Andric   if (SC == CXXConstructExprClass)
11595ffd83dbSDimitry Andric     setDependence(computeDependence(this));
11600b57cec5SDimitry Andric }
11610b57cec5SDimitry Andric 
11620b57cec5SDimitry Andric CXXConstructExpr::CXXConstructExpr(StmtClass SC, EmptyShell Empty,
11630b57cec5SDimitry Andric                                    unsigned NumArgs)
11640b57cec5SDimitry Andric     : Expr(SC, Empty), NumArgs(NumArgs) {}
11650b57cec5SDimitry Andric 
11660b57cec5SDimitry Andric LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
1167bdd1243dSDimitry Andric                              LambdaCaptureKind Kind, ValueDecl *Var,
11680b57cec5SDimitry Andric                              SourceLocation EllipsisLoc)
11690b57cec5SDimitry Andric     : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
11700b57cec5SDimitry Andric   unsigned Bits = 0;
11710b57cec5SDimitry Andric   if (Implicit)
11720b57cec5SDimitry Andric     Bits |= Capture_Implicit;
11730b57cec5SDimitry Andric 
11740b57cec5SDimitry Andric   switch (Kind) {
11750b57cec5SDimitry Andric   case LCK_StarThis:
11760b57cec5SDimitry Andric     Bits |= Capture_ByCopy;
1177bdd1243dSDimitry Andric     [[fallthrough]];
11780b57cec5SDimitry Andric   case LCK_This:
11790b57cec5SDimitry Andric     assert(!Var && "'this' capture cannot have a variable!");
11800b57cec5SDimitry Andric     Bits |= Capture_This;
11810b57cec5SDimitry Andric     break;
11820b57cec5SDimitry Andric 
11830b57cec5SDimitry Andric   case LCK_ByCopy:
11840b57cec5SDimitry Andric     Bits |= Capture_ByCopy;
1185bdd1243dSDimitry Andric     [[fallthrough]];
11860b57cec5SDimitry Andric   case LCK_ByRef:
11870b57cec5SDimitry Andric     assert(Var && "capture must have a variable!");
11880b57cec5SDimitry Andric     break;
11890b57cec5SDimitry Andric   case LCK_VLAType:
11900b57cec5SDimitry Andric     assert(!Var && "VLA type capture cannot have a variable!");
11910b57cec5SDimitry Andric     break;
11920b57cec5SDimitry Andric   }
11930b57cec5SDimitry Andric   DeclAndBits.setInt(Bits);
11940b57cec5SDimitry Andric }
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric LambdaCaptureKind LambdaCapture::getCaptureKind() const {
11970b57cec5SDimitry Andric   if (capturesVLAType())
11980b57cec5SDimitry Andric     return LCK_VLAType;
11990b57cec5SDimitry Andric   bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
12000b57cec5SDimitry Andric   if (capturesThis())
12010b57cec5SDimitry Andric     return CapByCopy ? LCK_StarThis : LCK_This;
12020b57cec5SDimitry Andric   return CapByCopy ? LCK_ByCopy : LCK_ByRef;
12030b57cec5SDimitry Andric }
12040b57cec5SDimitry Andric 
12050b57cec5SDimitry Andric LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
12060b57cec5SDimitry Andric                        LambdaCaptureDefault CaptureDefault,
12075ffd83dbSDimitry Andric                        SourceLocation CaptureDefaultLoc, bool ExplicitParams,
12080b57cec5SDimitry Andric                        bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
12090b57cec5SDimitry Andric                        SourceLocation ClosingBrace,
12100b57cec5SDimitry Andric                        bool ContainsUnexpandedParameterPack)
1211fe6060f1SDimitry Andric     : Expr(LambdaExprClass, T, VK_PRValue, OK_Ordinary),
12120b57cec5SDimitry Andric       IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
12130b57cec5SDimitry Andric       ClosingBrace(ClosingBrace) {
12145ffd83dbSDimitry Andric   LambdaExprBits.NumCaptures = CaptureInits.size();
12155ffd83dbSDimitry Andric   LambdaExprBits.CaptureDefault = CaptureDefault;
12165ffd83dbSDimitry Andric   LambdaExprBits.ExplicitParams = ExplicitParams;
12175ffd83dbSDimitry Andric   LambdaExprBits.ExplicitResultType = ExplicitResultType;
12185ffd83dbSDimitry Andric 
12190b57cec5SDimitry Andric   CXXRecordDecl *Class = getLambdaClass();
12205ffd83dbSDimitry Andric   (void)Class;
12215ffd83dbSDimitry Andric   assert(capture_size() == Class->capture_size() && "Wrong number of captures");
12225ffd83dbSDimitry Andric   assert(getCaptureDefault() == Class->getLambdaCaptureDefault());
12230b57cec5SDimitry Andric 
12240b57cec5SDimitry Andric   // Copy initialization expressions for the non-static data members.
12250b57cec5SDimitry Andric   Stmt **Stored = getStoredStmts();
12260b57cec5SDimitry Andric   for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
12270b57cec5SDimitry Andric     *Stored++ = CaptureInits[I];
12280b57cec5SDimitry Andric 
12290b57cec5SDimitry Andric   // Copy the body of the lambda.
12300b57cec5SDimitry Andric   *Stored++ = getCallOperator()->getBody();
12315ffd83dbSDimitry Andric 
12325ffd83dbSDimitry Andric   setDependence(computeDependence(this, ContainsUnexpandedParameterPack));
12330b57cec5SDimitry Andric }
12340b57cec5SDimitry Andric 
12355ffd83dbSDimitry Andric LambdaExpr::LambdaExpr(EmptyShell Empty, unsigned NumCaptures)
12365ffd83dbSDimitry Andric     : Expr(LambdaExprClass, Empty) {
12375ffd83dbSDimitry Andric   LambdaExprBits.NumCaptures = NumCaptures;
12385ffd83dbSDimitry Andric 
12395ffd83dbSDimitry Andric   // Initially don't initialize the body of the LambdaExpr. The body will
12405ffd83dbSDimitry Andric   // be lazily deserialized when needed.
12415ffd83dbSDimitry Andric   getStoredStmts()[NumCaptures] = nullptr; // Not one past the end.
12425ffd83dbSDimitry Andric }
12435ffd83dbSDimitry Andric 
12445ffd83dbSDimitry Andric LambdaExpr *LambdaExpr::Create(const ASTContext &Context, CXXRecordDecl *Class,
12455ffd83dbSDimitry Andric                                SourceRange IntroducerRange,
12465ffd83dbSDimitry Andric                                LambdaCaptureDefault CaptureDefault,
12475ffd83dbSDimitry Andric                                SourceLocation CaptureDefaultLoc,
12485ffd83dbSDimitry Andric                                bool ExplicitParams, bool ExplicitResultType,
12495ffd83dbSDimitry Andric                                ArrayRef<Expr *> CaptureInits,
12505ffd83dbSDimitry Andric                                SourceLocation ClosingBrace,
12515ffd83dbSDimitry Andric                                bool ContainsUnexpandedParameterPack) {
12520b57cec5SDimitry Andric   // Determine the type of the expression (i.e., the type of the
12530b57cec5SDimitry Andric   // function object we're creating).
12540b57cec5SDimitry Andric   QualType T = Context.getTypeDeclType(Class);
12550b57cec5SDimitry Andric 
12565ffd83dbSDimitry Andric   unsigned Size = totalSizeToAlloc<Stmt *>(CaptureInits.size() + 1);
12570b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size);
12580b57cec5SDimitry Andric   return new (Mem)
12590b57cec5SDimitry Andric       LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
12605ffd83dbSDimitry Andric                  ExplicitParams, ExplicitResultType, CaptureInits, ClosingBrace,
12615ffd83dbSDimitry Andric                  ContainsUnexpandedParameterPack);
12620b57cec5SDimitry Andric }
12630b57cec5SDimitry Andric 
12640b57cec5SDimitry Andric LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
12650b57cec5SDimitry Andric                                            unsigned NumCaptures) {
12660b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
12670b57cec5SDimitry Andric   void *Mem = C.Allocate(Size);
12680b57cec5SDimitry Andric   return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
12690b57cec5SDimitry Andric }
12700b57cec5SDimitry Andric 
12715ffd83dbSDimitry Andric void LambdaExpr::initBodyIfNeeded() const {
12725ffd83dbSDimitry Andric   if (!getStoredStmts()[capture_size()]) {
12735ffd83dbSDimitry Andric     auto *This = const_cast<LambdaExpr *>(this);
12745ffd83dbSDimitry Andric     This->getStoredStmts()[capture_size()] = getCallOperator()->getBody();
12755ffd83dbSDimitry Andric   }
12765ffd83dbSDimitry Andric }
12775ffd83dbSDimitry Andric 
12785ffd83dbSDimitry Andric Stmt *LambdaExpr::getBody() const {
12795ffd83dbSDimitry Andric   initBodyIfNeeded();
12805ffd83dbSDimitry Andric   return getStoredStmts()[capture_size()];
12815ffd83dbSDimitry Andric }
12825ffd83dbSDimitry Andric 
12835ffd83dbSDimitry Andric const CompoundStmt *LambdaExpr::getCompoundStmtBody() const {
12845ffd83dbSDimitry Andric   Stmt *Body = getBody();
12855ffd83dbSDimitry Andric   if (const auto *CoroBody = dyn_cast<CoroutineBodyStmt>(Body))
12865ffd83dbSDimitry Andric     return cast<CompoundStmt>(CoroBody->getBody());
12875ffd83dbSDimitry Andric   return cast<CompoundStmt>(Body);
12885ffd83dbSDimitry Andric }
12895ffd83dbSDimitry Andric 
12900b57cec5SDimitry Andric bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1291bdd1243dSDimitry Andric   return C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1292bdd1243dSDimitry Andric          getCallOperator() == C->getCapturedVar()->getDeclContext();
12930b57cec5SDimitry Andric }
12940b57cec5SDimitry Andric 
12950b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
1296bdd1243dSDimitry Andric   return getLambdaClass()->captures_begin();
12970b57cec5SDimitry Andric }
12980b57cec5SDimitry Andric 
12990b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
1300bdd1243dSDimitry Andric   return getLambdaClass()->captures_end();
13010b57cec5SDimitry Andric }
13020b57cec5SDimitry Andric 
13030b57cec5SDimitry Andric LambdaExpr::capture_range LambdaExpr::captures() const {
13040b57cec5SDimitry Andric   return capture_range(capture_begin(), capture_end());
13050b57cec5SDimitry Andric }
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
13080b57cec5SDimitry Andric   return capture_begin();
13090b57cec5SDimitry Andric }
13100b57cec5SDimitry Andric 
13110b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1312bdd1243dSDimitry Andric   return capture_begin() +
1313bdd1243dSDimitry Andric          getLambdaClass()->getLambdaData().NumExplicitCaptures;
13140b57cec5SDimitry Andric }
13150b57cec5SDimitry Andric 
13160b57cec5SDimitry Andric LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
13170b57cec5SDimitry Andric   return capture_range(explicit_capture_begin(), explicit_capture_end());
13180b57cec5SDimitry Andric }
13190b57cec5SDimitry Andric 
13200b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
13210b57cec5SDimitry Andric   return explicit_capture_end();
13220b57cec5SDimitry Andric }
13230b57cec5SDimitry Andric 
13240b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
13250b57cec5SDimitry Andric   return capture_end();
13260b57cec5SDimitry Andric }
13270b57cec5SDimitry Andric 
13280b57cec5SDimitry Andric LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
13290b57cec5SDimitry Andric   return capture_range(implicit_capture_begin(), implicit_capture_end());
13300b57cec5SDimitry Andric }
13310b57cec5SDimitry Andric 
13320b57cec5SDimitry Andric CXXRecordDecl *LambdaExpr::getLambdaClass() const {
13330b57cec5SDimitry Andric   return getType()->getAsCXXRecordDecl();
13340b57cec5SDimitry Andric }
13350b57cec5SDimitry Andric 
13360b57cec5SDimitry Andric CXXMethodDecl *LambdaExpr::getCallOperator() const {
13370b57cec5SDimitry Andric   CXXRecordDecl *Record = getLambdaClass();
13380b57cec5SDimitry Andric   return Record->getLambdaCallOperator();
13390b57cec5SDimitry Andric }
13400b57cec5SDimitry Andric 
1341a7dea167SDimitry Andric FunctionTemplateDecl *LambdaExpr::getDependentCallOperator() const {
1342a7dea167SDimitry Andric   CXXRecordDecl *Record = getLambdaClass();
1343a7dea167SDimitry Andric   return Record->getDependentLambdaCallOperator();
1344a7dea167SDimitry Andric }
1345a7dea167SDimitry Andric 
13460b57cec5SDimitry Andric TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
13470b57cec5SDimitry Andric   CXXRecordDecl *Record = getLambdaClass();
13480b57cec5SDimitry Andric   return Record->getGenericLambdaTemplateParameterList();
13490b57cec5SDimitry Andric }
13500b57cec5SDimitry Andric 
13510b57cec5SDimitry Andric ArrayRef<NamedDecl *> LambdaExpr::getExplicitTemplateParameters() const {
13520b57cec5SDimitry Andric   const CXXRecordDecl *Record = getLambdaClass();
13530b57cec5SDimitry Andric   return Record->getLambdaExplicitTemplateParameters();
13540b57cec5SDimitry Andric }
13550b57cec5SDimitry Andric 
1356e8d8bef9SDimitry Andric Expr *LambdaExpr::getTrailingRequiresClause() const {
1357e8d8bef9SDimitry Andric   return getCallOperator()->getTrailingRequiresClause();
1358e8d8bef9SDimitry Andric }
1359e8d8bef9SDimitry Andric 
13605ffd83dbSDimitry Andric bool LambdaExpr::isMutable() const { return !getCallOperator()->isConst(); }
13610b57cec5SDimitry Andric 
13625ffd83dbSDimitry Andric LambdaExpr::child_range LambdaExpr::children() {
13635ffd83dbSDimitry Andric   initBodyIfNeeded();
13645ffd83dbSDimitry Andric   return child_range(getStoredStmts(), getStoredStmts() + capture_size() + 1);
13650b57cec5SDimitry Andric }
13660b57cec5SDimitry Andric 
13675ffd83dbSDimitry Andric LambdaExpr::const_child_range LambdaExpr::children() const {
13685ffd83dbSDimitry Andric   initBodyIfNeeded();
13695ffd83dbSDimitry Andric   return const_child_range(getStoredStmts(),
13705ffd83dbSDimitry Andric                            getStoredStmts() + capture_size() + 1);
13710b57cec5SDimitry Andric }
13720b57cec5SDimitry Andric 
13730b57cec5SDimitry Andric ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
13740b57cec5SDimitry Andric                                    bool CleanupsHaveSideEffects,
13750b57cec5SDimitry Andric                                    ArrayRef<CleanupObject> objects)
13760b57cec5SDimitry Andric     : FullExpr(ExprWithCleanupsClass, subexpr) {
13770b57cec5SDimitry Andric   ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
13780b57cec5SDimitry Andric   ExprWithCleanupsBits.NumObjects = objects.size();
13790b57cec5SDimitry Andric   for (unsigned i = 0, e = objects.size(); i != e; ++i)
13800b57cec5SDimitry Andric     getTrailingObjects<CleanupObject>()[i] = objects[i];
13810b57cec5SDimitry Andric }
13820b57cec5SDimitry Andric 
13830b57cec5SDimitry Andric ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
13840b57cec5SDimitry Andric                                            bool CleanupsHaveSideEffects,
13850b57cec5SDimitry Andric                                            ArrayRef<CleanupObject> objects) {
13860b57cec5SDimitry Andric   void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
13870b57cec5SDimitry Andric                             alignof(ExprWithCleanups));
13880b57cec5SDimitry Andric   return new (buffer)
13890b57cec5SDimitry Andric       ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
13900b57cec5SDimitry Andric }
13910b57cec5SDimitry Andric 
13920b57cec5SDimitry Andric ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
13930b57cec5SDimitry Andric     : FullExpr(ExprWithCleanupsClass, empty) {
13940b57cec5SDimitry Andric   ExprWithCleanupsBits.NumObjects = numObjects;
13950b57cec5SDimitry Andric }
13960b57cec5SDimitry Andric 
13970b57cec5SDimitry Andric ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
13980b57cec5SDimitry Andric                                            EmptyShell empty,
13990b57cec5SDimitry Andric                                            unsigned numObjects) {
14000b57cec5SDimitry Andric   void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
14010b57cec5SDimitry Andric                             alignof(ExprWithCleanups));
14020b57cec5SDimitry Andric   return new (buffer) ExprWithCleanups(empty, numObjects);
14030b57cec5SDimitry Andric }
14040b57cec5SDimitry Andric 
140506c3fb27SDimitry Andric CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(
140606c3fb27SDimitry Andric     QualType T, TypeSourceInfo *TSI, SourceLocation LParenLoc,
140706c3fb27SDimitry Andric     ArrayRef<Expr *> Args, SourceLocation RParenLoc, bool IsListInit)
1408e8d8bef9SDimitry Andric     : Expr(CXXUnresolvedConstructExprClass, T,
1409fe6060f1SDimitry Andric            (TSI->getType()->isLValueReferenceType()   ? VK_LValue
14100b57cec5SDimitry Andric             : TSI->getType()->isRValueReferenceType() ? VK_XValue
1411fe6060f1SDimitry Andric                                                       : VK_PRValue),
14125ffd83dbSDimitry Andric            OK_Ordinary),
141306c3fb27SDimitry Andric       TypeAndInitForm(TSI, IsListInit), LParenLoc(LParenLoc),
141406c3fb27SDimitry Andric       RParenLoc(RParenLoc) {
14150b57cec5SDimitry Andric   CXXUnresolvedConstructExprBits.NumArgs = Args.size();
14160b57cec5SDimitry Andric   auto **StoredArgs = getTrailingObjects<Expr *>();
14175ffd83dbSDimitry Andric   for (unsigned I = 0; I != Args.size(); ++I)
14180b57cec5SDimitry Andric     StoredArgs[I] = Args[I];
14195ffd83dbSDimitry Andric   setDependence(computeDependence(this));
14200b57cec5SDimitry Andric }
14210b57cec5SDimitry Andric 
14220b57cec5SDimitry Andric CXXUnresolvedConstructExpr *CXXUnresolvedConstructExpr::Create(
142306c3fb27SDimitry Andric     const ASTContext &Context, QualType T, TypeSourceInfo *TSI,
142406c3fb27SDimitry Andric     SourceLocation LParenLoc, ArrayRef<Expr *> Args, SourceLocation RParenLoc,
142506c3fb27SDimitry Andric     bool IsListInit) {
14260b57cec5SDimitry Andric   void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
142706c3fb27SDimitry Andric   return new (Mem) CXXUnresolvedConstructExpr(T, TSI, LParenLoc, Args,
142806c3fb27SDimitry Andric                                               RParenLoc, IsListInit);
14290b57cec5SDimitry Andric }
14300b57cec5SDimitry Andric 
14310b57cec5SDimitry Andric CXXUnresolvedConstructExpr *
14320b57cec5SDimitry Andric CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &Context,
14330b57cec5SDimitry Andric                                         unsigned NumArgs) {
14340b57cec5SDimitry Andric   void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
14350b57cec5SDimitry Andric   return new (Mem) CXXUnresolvedConstructExpr(EmptyShell(), NumArgs);
14360b57cec5SDimitry Andric }
14370b57cec5SDimitry Andric 
14380b57cec5SDimitry Andric SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const {
143906c3fb27SDimitry Andric   return TypeAndInitForm.getPointer()->getTypeLoc().getBeginLoc();
14400b57cec5SDimitry Andric }
14410b57cec5SDimitry Andric 
14420b57cec5SDimitry Andric CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
14430b57cec5SDimitry Andric     const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
14440b57cec5SDimitry Andric     SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
14450b57cec5SDimitry Andric     SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
14460b57cec5SDimitry Andric     DeclarationNameInfo MemberNameInfo,
14470b57cec5SDimitry Andric     const TemplateArgumentListInfo *TemplateArgs)
14480b57cec5SDimitry Andric     : Expr(CXXDependentScopeMemberExprClass, Ctx.DependentTy, VK_LValue,
14495ffd83dbSDimitry Andric            OK_Ordinary),
14500b57cec5SDimitry Andric       Base(Base), BaseType(BaseType), QualifierLoc(QualifierLoc),
14510b57cec5SDimitry Andric       MemberNameInfo(MemberNameInfo) {
14520b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.IsArrow = IsArrow;
14530b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
14540b57cec5SDimitry Andric       (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
14550b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
14560b57cec5SDimitry Andric       FirstQualifierFoundInScope != nullptr;
14570b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.OperatorLoc = OperatorLoc;
14580b57cec5SDimitry Andric 
14590b57cec5SDimitry Andric   if (TemplateArgs) {
14605ffd83dbSDimitry Andric     auto Deps = TemplateArgumentDependence::None;
14610b57cec5SDimitry Andric     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
14620b57cec5SDimitry Andric         TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
14635ffd83dbSDimitry Andric         Deps);
14640b57cec5SDimitry Andric   } else if (TemplateKWLoc.isValid()) {
14650b57cec5SDimitry Andric     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
14660b57cec5SDimitry Andric         TemplateKWLoc);
14670b57cec5SDimitry Andric   }
14680b57cec5SDimitry Andric 
14690b57cec5SDimitry Andric   if (hasFirstQualifierFoundInScope())
14700b57cec5SDimitry Andric     *getTrailingObjects<NamedDecl *>() = FirstQualifierFoundInScope;
14715ffd83dbSDimitry Andric   setDependence(computeDependence(this));
14720b57cec5SDimitry Andric }
14730b57cec5SDimitry Andric 
14740b57cec5SDimitry Andric CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
14750b57cec5SDimitry Andric     EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
14760b57cec5SDimitry Andric     bool HasFirstQualifierFoundInScope)
14770b57cec5SDimitry Andric     : Expr(CXXDependentScopeMemberExprClass, Empty) {
14780b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
14790b57cec5SDimitry Andric       HasTemplateKWAndArgsInfo;
14800b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
14810b57cec5SDimitry Andric       HasFirstQualifierFoundInScope;
14820b57cec5SDimitry Andric }
14830b57cec5SDimitry Andric 
14840b57cec5SDimitry Andric CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::Create(
14850b57cec5SDimitry Andric     const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
14860b57cec5SDimitry Andric     SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
14870b57cec5SDimitry Andric     SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
14880b57cec5SDimitry Andric     DeclarationNameInfo MemberNameInfo,
14890b57cec5SDimitry Andric     const TemplateArgumentListInfo *TemplateArgs) {
14900b57cec5SDimitry Andric   bool HasTemplateKWAndArgsInfo =
14910b57cec5SDimitry Andric       (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
14920b57cec5SDimitry Andric   unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
14930b57cec5SDimitry Andric   bool HasFirstQualifierFoundInScope = FirstQualifierFoundInScope != nullptr;
14940b57cec5SDimitry Andric 
14950b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
14960b57cec5SDimitry Andric                                    TemplateArgumentLoc, NamedDecl *>(
14970b57cec5SDimitry Andric       HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
14980b57cec5SDimitry Andric 
14990b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
15000b57cec5SDimitry Andric   return new (Mem) CXXDependentScopeMemberExpr(
15010b57cec5SDimitry Andric       Ctx, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
15020b57cec5SDimitry Andric       FirstQualifierFoundInScope, MemberNameInfo, TemplateArgs);
15030b57cec5SDimitry Andric }
15040b57cec5SDimitry Andric 
15050b57cec5SDimitry Andric CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::CreateEmpty(
15060b57cec5SDimitry Andric     const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
15070b57cec5SDimitry Andric     unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope) {
15080b57cec5SDimitry Andric   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
15090b57cec5SDimitry Andric 
15100b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
15110b57cec5SDimitry Andric                                    TemplateArgumentLoc, NamedDecl *>(
15120b57cec5SDimitry Andric       HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
15130b57cec5SDimitry Andric 
15140b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
15150b57cec5SDimitry Andric   return new (Mem) CXXDependentScopeMemberExpr(
15160b57cec5SDimitry Andric       EmptyShell(), HasTemplateKWAndArgsInfo, HasFirstQualifierFoundInScope);
15170b57cec5SDimitry Andric }
15180b57cec5SDimitry Andric 
1519*5f757f3fSDimitry Andric CXXThisExpr *CXXThisExpr::Create(const ASTContext &Ctx, SourceLocation L,
1520*5f757f3fSDimitry Andric                                  QualType Ty, bool IsImplicit) {
1521*5f757f3fSDimitry Andric   return new (Ctx) CXXThisExpr(L, Ty, IsImplicit,
1522*5f757f3fSDimitry Andric                                Ctx.getLangOpts().HLSL ? VK_LValue : VK_PRValue);
1523*5f757f3fSDimitry Andric }
1524*5f757f3fSDimitry Andric 
1525*5f757f3fSDimitry Andric CXXThisExpr *CXXThisExpr::CreateEmpty(const ASTContext &Ctx) {
1526*5f757f3fSDimitry Andric   return new (Ctx) CXXThisExpr(EmptyShell());
1527*5f757f3fSDimitry Andric }
1528*5f757f3fSDimitry Andric 
15290b57cec5SDimitry Andric static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
15300b57cec5SDimitry Andric                                             UnresolvedSetIterator end) {
15310b57cec5SDimitry Andric   do {
15320b57cec5SDimitry Andric     NamedDecl *decl = *begin;
15330b57cec5SDimitry Andric     if (isa<UnresolvedUsingValueDecl>(decl))
15340b57cec5SDimitry Andric       return false;
15350b57cec5SDimitry Andric 
15360b57cec5SDimitry Andric     // Unresolved member expressions should only contain methods and
15370b57cec5SDimitry Andric     // method templates.
15380b57cec5SDimitry Andric     if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
15390b57cec5SDimitry Andric             ->isStatic())
15400b57cec5SDimitry Andric       return false;
15410b57cec5SDimitry Andric   } while (++begin != end);
15420b57cec5SDimitry Andric 
15430b57cec5SDimitry Andric   return true;
15440b57cec5SDimitry Andric }
15450b57cec5SDimitry Andric 
15460b57cec5SDimitry Andric UnresolvedMemberExpr::UnresolvedMemberExpr(
15470b57cec5SDimitry Andric     const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
15480b57cec5SDimitry Andric     QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
15490b57cec5SDimitry Andric     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
15500b57cec5SDimitry Andric     const DeclarationNameInfo &MemberNameInfo,
15510b57cec5SDimitry Andric     const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
15520b57cec5SDimitry Andric     UnresolvedSetIterator End)
15530b57cec5SDimitry Andric     : OverloadExpr(
15540b57cec5SDimitry Andric           UnresolvedMemberExprClass, Context, QualifierLoc, TemplateKWLoc,
15550b57cec5SDimitry Andric           MemberNameInfo, TemplateArgs, Begin, End,
15560b57cec5SDimitry Andric           // Dependent
15570b57cec5SDimitry Andric           ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
15580b57cec5SDimitry Andric           ((Base && Base->isInstantiationDependent()) ||
15590b57cec5SDimitry Andric            BaseType->isInstantiationDependentType()),
15600b57cec5SDimitry Andric           // Contains unexpanded parameter pack
15610b57cec5SDimitry Andric           ((Base && Base->containsUnexpandedParameterPack()) ||
15620b57cec5SDimitry Andric            BaseType->containsUnexpandedParameterPack())),
15630b57cec5SDimitry Andric       Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
15640b57cec5SDimitry Andric   UnresolvedMemberExprBits.IsArrow = IsArrow;
15650b57cec5SDimitry Andric   UnresolvedMemberExprBits.HasUnresolvedUsing = HasUnresolvedUsing;
15660b57cec5SDimitry Andric 
15670b57cec5SDimitry Andric   // Check whether all of the members are non-static member functions,
15680b57cec5SDimitry Andric   // and if so, mark give this bound-member type instead of overload type.
15690b57cec5SDimitry Andric   if (hasOnlyNonStaticMemberFunctions(Begin, End))
15700b57cec5SDimitry Andric     setType(Context.BoundMemberTy);
15710b57cec5SDimitry Andric }
15720b57cec5SDimitry Andric 
15730b57cec5SDimitry Andric UnresolvedMemberExpr::UnresolvedMemberExpr(EmptyShell Empty,
15740b57cec5SDimitry Andric                                            unsigned NumResults,
15750b57cec5SDimitry Andric                                            bool HasTemplateKWAndArgsInfo)
15760b57cec5SDimitry Andric     : OverloadExpr(UnresolvedMemberExprClass, Empty, NumResults,
15770b57cec5SDimitry Andric                    HasTemplateKWAndArgsInfo) {}
15780b57cec5SDimitry Andric 
15790b57cec5SDimitry Andric bool UnresolvedMemberExpr::isImplicitAccess() const {
15800b57cec5SDimitry Andric   if (!Base)
15810b57cec5SDimitry Andric     return true;
15820b57cec5SDimitry Andric 
15830b57cec5SDimitry Andric   return cast<Expr>(Base)->isImplicitCXXThis();
15840b57cec5SDimitry Andric }
15850b57cec5SDimitry Andric 
15860b57cec5SDimitry Andric UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
15870b57cec5SDimitry Andric     const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
15880b57cec5SDimitry Andric     QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
15890b57cec5SDimitry Andric     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
15900b57cec5SDimitry Andric     const DeclarationNameInfo &MemberNameInfo,
15910b57cec5SDimitry Andric     const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
15920b57cec5SDimitry Andric     UnresolvedSetIterator End) {
15930b57cec5SDimitry Andric   unsigned NumResults = End - Begin;
15940b57cec5SDimitry Andric   bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
15950b57cec5SDimitry Andric   unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
15960b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
15970b57cec5SDimitry Andric                                    TemplateArgumentLoc>(
15980b57cec5SDimitry Andric       NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
15990b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
16000b57cec5SDimitry Andric   return new (Mem) UnresolvedMemberExpr(
16010b57cec5SDimitry Andric       Context, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc,
16020b57cec5SDimitry Andric       QualifierLoc, TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
16030b57cec5SDimitry Andric }
16040b57cec5SDimitry Andric 
16050b57cec5SDimitry Andric UnresolvedMemberExpr *UnresolvedMemberExpr::CreateEmpty(
16060b57cec5SDimitry Andric     const ASTContext &Context, unsigned NumResults,
16070b57cec5SDimitry Andric     bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
16080b57cec5SDimitry Andric   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
16090b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
16100b57cec5SDimitry Andric                                    TemplateArgumentLoc>(
16110b57cec5SDimitry Andric       NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
16120b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
16130b57cec5SDimitry Andric   return new (Mem)
16140b57cec5SDimitry Andric       UnresolvedMemberExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
16150b57cec5SDimitry Andric }
16160b57cec5SDimitry Andric 
16170b57cec5SDimitry Andric CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() {
16180b57cec5SDimitry Andric   // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
16190b57cec5SDimitry Andric 
16200b57cec5SDimitry Andric   // If there was a nested name specifier, it names the naming class.
16210b57cec5SDimitry Andric   // It can't be dependent: after all, we were actually able to do the
16220b57cec5SDimitry Andric   // lookup.
16230b57cec5SDimitry Andric   CXXRecordDecl *Record = nullptr;
16240b57cec5SDimitry Andric   auto *NNS = getQualifier();
16250b57cec5SDimitry Andric   if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
16260b57cec5SDimitry Andric     const Type *T = getQualifier()->getAsType();
16270b57cec5SDimitry Andric     assert(T && "qualifier in member expression does not name type");
16280b57cec5SDimitry Andric     Record = T->getAsCXXRecordDecl();
16290b57cec5SDimitry Andric     assert(Record && "qualifier in member expression does not name record");
16300b57cec5SDimitry Andric   }
16310b57cec5SDimitry Andric   // Otherwise the naming class must have been the base class.
16320b57cec5SDimitry Andric   else {
16330b57cec5SDimitry Andric     QualType BaseType = getBaseType().getNonReferenceType();
1634a7dea167SDimitry Andric     if (isArrow())
1635a7dea167SDimitry Andric       BaseType = BaseType->castAs<PointerType>()->getPointeeType();
16360b57cec5SDimitry Andric 
16370b57cec5SDimitry Andric     Record = BaseType->getAsCXXRecordDecl();
16380b57cec5SDimitry Andric     assert(Record && "base of member expression does not name record");
16390b57cec5SDimitry Andric   }
16400b57cec5SDimitry Andric 
16410b57cec5SDimitry Andric   return Record;
16420b57cec5SDimitry Andric }
16430b57cec5SDimitry Andric 
1644bdd1243dSDimitry Andric SizeOfPackExpr *SizeOfPackExpr::Create(ASTContext &Context,
1645bdd1243dSDimitry Andric                                        SourceLocation OperatorLoc,
16460b57cec5SDimitry Andric                                        NamedDecl *Pack, SourceLocation PackLoc,
16470b57cec5SDimitry Andric                                        SourceLocation RParenLoc,
1648bdd1243dSDimitry Andric                                        std::optional<unsigned> Length,
16490b57cec5SDimitry Andric                                        ArrayRef<TemplateArgument> PartialArgs) {
16500b57cec5SDimitry Andric   void *Storage =
16510b57cec5SDimitry Andric       Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
16520b57cec5SDimitry Andric   return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
16530b57cec5SDimitry Andric                                       PackLoc, RParenLoc, Length, PartialArgs);
16540b57cec5SDimitry Andric }
16550b57cec5SDimitry Andric 
16560b57cec5SDimitry Andric SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
16570b57cec5SDimitry Andric                                                    unsigned NumPartialArgs) {
16580b57cec5SDimitry Andric   void *Storage =
16590b57cec5SDimitry Andric       Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
16600b57cec5SDimitry Andric   return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
16610b57cec5SDimitry Andric }
16620b57cec5SDimitry Andric 
1663bdd1243dSDimitry Andric NonTypeTemplateParmDecl *SubstNonTypeTemplateParmExpr::getParameter() const {
1664bdd1243dSDimitry Andric   return cast<NonTypeTemplateParmDecl>(
1665bdd1243dSDimitry Andric       getReplacedTemplateParameterList(getAssociatedDecl())->asArray()[Index]);
1666bdd1243dSDimitry Andric }
1667bdd1243dSDimitry Andric 
1668e8d8bef9SDimitry Andric QualType SubstNonTypeTemplateParmExpr::getParameterType(
1669e8d8bef9SDimitry Andric     const ASTContext &Context) const {
1670e8d8bef9SDimitry Andric   // Note that, for a class type NTTP, we will have an lvalue of type 'const
1671e8d8bef9SDimitry Andric   // T', so we can't just compute this from the type and value category.
1672e8d8bef9SDimitry Andric   if (isReferenceParameter())
1673e8d8bef9SDimitry Andric     return Context.getLValueReferenceType(getType());
1674e8d8bef9SDimitry Andric   return getType().getUnqualifiedType();
1675e8d8bef9SDimitry Andric }
1676e8d8bef9SDimitry Andric 
16775ffd83dbSDimitry Andric SubstNonTypeTemplateParmPackExpr::SubstNonTypeTemplateParmPackExpr(
1678bdd1243dSDimitry Andric     QualType T, ExprValueKind ValueKind, SourceLocation NameLoc,
1679bdd1243dSDimitry Andric     const TemplateArgument &ArgPack, Decl *AssociatedDecl, unsigned Index)
16805ffd83dbSDimitry Andric     : Expr(SubstNonTypeTemplateParmPackExprClass, T, ValueKind, OK_Ordinary),
1681bdd1243dSDimitry Andric       AssociatedDecl(AssociatedDecl), Arguments(ArgPack.pack_begin()),
1682bdd1243dSDimitry Andric       NumArguments(ArgPack.pack_size()), Index(Index), NameLoc(NameLoc) {
1683bdd1243dSDimitry Andric   assert(AssociatedDecl != nullptr);
16845ffd83dbSDimitry Andric   setDependence(ExprDependence::TypeValueInstantiation |
16855ffd83dbSDimitry Andric                 ExprDependence::UnexpandedPack);
16865ffd83dbSDimitry Andric }
16870b57cec5SDimitry Andric 
1688bdd1243dSDimitry Andric NonTypeTemplateParmDecl *
1689bdd1243dSDimitry Andric SubstNonTypeTemplateParmPackExpr::getParameterPack() const {
1690bdd1243dSDimitry Andric   return cast<NonTypeTemplateParmDecl>(
1691bdd1243dSDimitry Andric       getReplacedTemplateParameterList(getAssociatedDecl())->asArray()[Index]);
1692bdd1243dSDimitry Andric }
1693bdd1243dSDimitry Andric 
16940b57cec5SDimitry Andric TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1695bdd1243dSDimitry Andric   return TemplateArgument(llvm::ArrayRef(Arguments, NumArguments));
16960b57cec5SDimitry Andric }
16970b57cec5SDimitry Andric 
16980b57cec5SDimitry Andric FunctionParmPackExpr::FunctionParmPackExpr(QualType T, VarDecl *ParamPack,
16990b57cec5SDimitry Andric                                            SourceLocation NameLoc,
17000b57cec5SDimitry Andric                                            unsigned NumParams,
17010b57cec5SDimitry Andric                                            VarDecl *const *Params)
17025ffd83dbSDimitry Andric     : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary),
17030b57cec5SDimitry Andric       ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
17040b57cec5SDimitry Andric   if (Params)
17050b57cec5SDimitry Andric     std::uninitialized_copy(Params, Params + NumParams,
17060b57cec5SDimitry Andric                             getTrailingObjects<VarDecl *>());
17075ffd83dbSDimitry Andric   setDependence(ExprDependence::TypeValueInstantiation |
17085ffd83dbSDimitry Andric                 ExprDependence::UnexpandedPack);
17090b57cec5SDimitry Andric }
17100b57cec5SDimitry Andric 
17110b57cec5SDimitry Andric FunctionParmPackExpr *
17120b57cec5SDimitry Andric FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
17130b57cec5SDimitry Andric                              VarDecl *ParamPack, SourceLocation NameLoc,
17140b57cec5SDimitry Andric                              ArrayRef<VarDecl *> Params) {
17150b57cec5SDimitry Andric   return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(Params.size())))
17160b57cec5SDimitry Andric       FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
17170b57cec5SDimitry Andric }
17180b57cec5SDimitry Andric 
17190b57cec5SDimitry Andric FunctionParmPackExpr *
17200b57cec5SDimitry Andric FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
17210b57cec5SDimitry Andric                                   unsigned NumParams) {
17220b57cec5SDimitry Andric   return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(NumParams)))
17230b57cec5SDimitry Andric       FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
17240b57cec5SDimitry Andric }
17250b57cec5SDimitry Andric 
1726480093f4SDimitry Andric MaterializeTemporaryExpr::MaterializeTemporaryExpr(
1727480093f4SDimitry Andric     QualType T, Expr *Temporary, bool BoundToLvalueReference,
1728480093f4SDimitry Andric     LifetimeExtendedTemporaryDecl *MTD)
1729480093f4SDimitry Andric     : Expr(MaterializeTemporaryExprClass, T,
17305ffd83dbSDimitry Andric            BoundToLvalueReference ? VK_LValue : VK_XValue, OK_Ordinary) {
1731480093f4SDimitry Andric   if (MTD) {
1732480093f4SDimitry Andric     State = MTD;
1733480093f4SDimitry Andric     MTD->ExprWithTemporary = Temporary;
1734480093f4SDimitry Andric     return;
1735480093f4SDimitry Andric   }
1736480093f4SDimitry Andric   State = Temporary;
17375ffd83dbSDimitry Andric   setDependence(computeDependence(this));
1738480093f4SDimitry Andric }
1739480093f4SDimitry Andric 
1740480093f4SDimitry Andric void MaterializeTemporaryExpr::setExtendingDecl(ValueDecl *ExtendedBy,
17410b57cec5SDimitry Andric                                                 unsigned ManglingNumber) {
17420b57cec5SDimitry Andric   // We only need extra state if we have to remember more than just the Stmt.
17430b57cec5SDimitry Andric   if (!ExtendedBy)
17440b57cec5SDimitry Andric     return;
17450b57cec5SDimitry Andric 
17460b57cec5SDimitry Andric   // We may need to allocate extra storage for the mangling number and the
17470b57cec5SDimitry Andric   // extended-by ValueDecl.
1748480093f4SDimitry Andric   if (!State.is<LifetimeExtendedTemporaryDecl *>())
1749480093f4SDimitry Andric     State = LifetimeExtendedTemporaryDecl::Create(
1750480093f4SDimitry Andric         cast<Expr>(State.get<Stmt *>()), ExtendedBy, ManglingNumber);
17510b57cec5SDimitry Andric 
1752480093f4SDimitry Andric   auto ES = State.get<LifetimeExtendedTemporaryDecl *>();
17530b57cec5SDimitry Andric   ES->ExtendingDecl = ExtendedBy;
17540b57cec5SDimitry Andric   ES->ManglingNumber = ManglingNumber;
17550b57cec5SDimitry Andric }
17560b57cec5SDimitry Andric 
1757e8d8bef9SDimitry Andric bool MaterializeTemporaryExpr::isUsableInConstantExpressions(
1758e8d8bef9SDimitry Andric     const ASTContext &Context) const {
1759e8d8bef9SDimitry Andric   // C++20 [expr.const]p4:
1760e8d8bef9SDimitry Andric   //   An object or reference is usable in constant expressions if it is [...]
1761e8d8bef9SDimitry Andric   //   a temporary object of non-volatile const-qualified literal type
1762e8d8bef9SDimitry Andric   //   whose lifetime is extended to that of a variable that is usable
1763e8d8bef9SDimitry Andric   //   in constant expressions
1764e8d8bef9SDimitry Andric   auto *VD = dyn_cast_or_null<VarDecl>(getExtendingDecl());
1765e8d8bef9SDimitry Andric   return VD && getType().isConstant(Context) &&
1766e8d8bef9SDimitry Andric          !getType().isVolatileQualified() &&
1767e8d8bef9SDimitry Andric          getType()->isLiteralType(Context) &&
1768e8d8bef9SDimitry Andric          VD->isUsableInConstantExpressions(Context);
1769e8d8bef9SDimitry Andric }
1770e8d8bef9SDimitry Andric 
17710b57cec5SDimitry Andric TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
17720b57cec5SDimitry Andric                              ArrayRef<TypeSourceInfo *> Args,
17735ffd83dbSDimitry Andric                              SourceLocation RParenLoc, bool Value)
1774fe6060f1SDimitry Andric     : Expr(TypeTraitExprClass, T, VK_PRValue, OK_Ordinary), Loc(Loc),
17755ffd83dbSDimitry Andric       RParenLoc(RParenLoc) {
17765ffd83dbSDimitry Andric   assert(Kind <= TT_Last && "invalid enum value!");
17770b57cec5SDimitry Andric   TypeTraitExprBits.Kind = Kind;
17785ffd83dbSDimitry Andric   assert(static_cast<unsigned>(Kind) == TypeTraitExprBits.Kind &&
17795ffd83dbSDimitry Andric          "TypeTraitExprBits.Kind overflow!");
17800b57cec5SDimitry Andric   TypeTraitExprBits.Value = Value;
17810b57cec5SDimitry Andric   TypeTraitExprBits.NumArgs = Args.size();
17825ffd83dbSDimitry Andric   assert(Args.size() == TypeTraitExprBits.NumArgs &&
17835ffd83dbSDimitry Andric          "TypeTraitExprBits.NumArgs overflow!");
17840b57cec5SDimitry Andric 
17850b57cec5SDimitry Andric   auto **ToArgs = getTrailingObjects<TypeSourceInfo *>();
17865ffd83dbSDimitry Andric   for (unsigned I = 0, N = Args.size(); I != N; ++I)
17870b57cec5SDimitry Andric     ToArgs[I] = Args[I];
17885ffd83dbSDimitry Andric 
17895ffd83dbSDimitry Andric   setDependence(computeDependence(this));
17900b57cec5SDimitry Andric }
17910b57cec5SDimitry Andric 
17920b57cec5SDimitry Andric TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
17930b57cec5SDimitry Andric                                      SourceLocation Loc,
17940b57cec5SDimitry Andric                                      TypeTrait Kind,
17950b57cec5SDimitry Andric                                      ArrayRef<TypeSourceInfo *> Args,
17960b57cec5SDimitry Andric                                      SourceLocation RParenLoc,
17970b57cec5SDimitry Andric                                      bool Value) {
17980b57cec5SDimitry Andric   void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
17990b57cec5SDimitry Andric   return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
18000b57cec5SDimitry Andric }
18010b57cec5SDimitry Andric 
18020b57cec5SDimitry Andric TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
18030b57cec5SDimitry Andric                                                  unsigned NumArgs) {
18040b57cec5SDimitry Andric   void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
18050b57cec5SDimitry Andric   return new (Mem) TypeTraitExpr(EmptyShell());
18060b57cec5SDimitry Andric }
18070b57cec5SDimitry Andric 
18080b57cec5SDimitry Andric CUDAKernelCallExpr::CUDAKernelCallExpr(Expr *Fn, CallExpr *Config,
18090b57cec5SDimitry Andric                                        ArrayRef<Expr *> Args, QualType Ty,
18100b57cec5SDimitry Andric                                        ExprValueKind VK, SourceLocation RP,
1811e8d8bef9SDimitry Andric                                        FPOptionsOverride FPFeatures,
18120b57cec5SDimitry Andric                                        unsigned MinNumArgs)
18130b57cec5SDimitry Andric     : CallExpr(CUDAKernelCallExprClass, Fn, /*PreArgs=*/Config, Args, Ty, VK,
1814e8d8bef9SDimitry Andric                RP, FPFeatures, MinNumArgs, NotADL) {}
18150b57cec5SDimitry Andric 
1816e8d8bef9SDimitry Andric CUDAKernelCallExpr::CUDAKernelCallExpr(unsigned NumArgs, bool HasFPFeatures,
1817e8d8bef9SDimitry Andric                                        EmptyShell Empty)
18180b57cec5SDimitry Andric     : CallExpr(CUDAKernelCallExprClass, /*NumPreArgs=*/END_PREARG, NumArgs,
1819e8d8bef9SDimitry Andric                HasFPFeatures, Empty) {}
18200b57cec5SDimitry Andric 
18210b57cec5SDimitry Andric CUDAKernelCallExpr *
18220b57cec5SDimitry Andric CUDAKernelCallExpr::Create(const ASTContext &Ctx, Expr *Fn, CallExpr *Config,
18230b57cec5SDimitry Andric                            ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
1824e8d8bef9SDimitry Andric                            SourceLocation RP, FPOptionsOverride FPFeatures,
1825e8d8bef9SDimitry Andric                            unsigned MinNumArgs) {
18260b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
18270b57cec5SDimitry Andric   unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
1828e8d8bef9SDimitry Andric   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
1829e8d8bef9SDimitry Andric       /*NumPreArgs=*/END_PREARG, NumArgs, FPFeatures.requiresTrailingStorage());
18300b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
18310b57cec5SDimitry Andric                            alignof(CUDAKernelCallExpr));
1832e8d8bef9SDimitry Andric   return new (Mem)
1833e8d8bef9SDimitry Andric       CUDAKernelCallExpr(Fn, Config, Args, Ty, VK, RP, FPFeatures, MinNumArgs);
18340b57cec5SDimitry Andric }
18350b57cec5SDimitry Andric 
18360b57cec5SDimitry Andric CUDAKernelCallExpr *CUDAKernelCallExpr::CreateEmpty(const ASTContext &Ctx,
18370b57cec5SDimitry Andric                                                     unsigned NumArgs,
1838e8d8bef9SDimitry Andric                                                     bool HasFPFeatures,
18390b57cec5SDimitry Andric                                                     EmptyShell Empty) {
18400b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
1841e8d8bef9SDimitry Andric   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
1842e8d8bef9SDimitry Andric       /*NumPreArgs=*/END_PREARG, NumArgs, HasFPFeatures);
18430b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
18440b57cec5SDimitry Andric                            alignof(CUDAKernelCallExpr));
1845e8d8bef9SDimitry Andric   return new (Mem) CUDAKernelCallExpr(NumArgs, HasFPFeatures, Empty);
18460b57cec5SDimitry Andric }
1847bdd1243dSDimitry Andric 
1848bdd1243dSDimitry Andric CXXParenListInitExpr *
1849bdd1243dSDimitry Andric CXXParenListInitExpr::Create(ASTContext &C, ArrayRef<Expr *> Args, QualType T,
1850bdd1243dSDimitry Andric                              unsigned NumUserSpecifiedExprs,
1851bdd1243dSDimitry Andric                              SourceLocation InitLoc, SourceLocation LParenLoc,
1852bdd1243dSDimitry Andric                              SourceLocation RParenLoc) {
1853bdd1243dSDimitry Andric   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
1854bdd1243dSDimitry Andric   return new (Mem) CXXParenListInitExpr(Args, T, NumUserSpecifiedExprs, InitLoc,
1855bdd1243dSDimitry Andric                                         LParenLoc, RParenLoc);
1856bdd1243dSDimitry Andric }
1857bdd1243dSDimitry Andric 
1858bdd1243dSDimitry Andric CXXParenListInitExpr *CXXParenListInitExpr::CreateEmpty(ASTContext &C,
1859bdd1243dSDimitry Andric                                                         unsigned NumExprs,
1860bdd1243dSDimitry Andric                                                         EmptyShell Empty) {
1861bdd1243dSDimitry Andric   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumExprs),
1862bdd1243dSDimitry Andric                          alignof(CXXParenListInitExpr));
1863bdd1243dSDimitry Andric   return new (Mem) CXXParenListInitExpr(Empty, NumExprs);
1864bdd1243dSDimitry Andric }
1865