xref: /freebsd/contrib/llvm-project/clang/lib/AST/ExprCXX.cpp (revision 62987288060ff68c817b7056815aa9fb8ba8ecd7)
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 
isInfixBinaryOp() const490b57cec5SDimitry 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
getDecomposedForm() const66a7dea167SDimitry 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.
1145f757f3fSDimitry 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 
isPotentiallyEvaluated() const1350b57cec5SDimitry 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 
isMostDerived(ASTContext & Context) const150e8d8bef9SDimitry 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 
getTypeOperand(ASTContext & Context) const1620b57cec5SDimitry 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 
isGLValueFromPointerDeref(const Expr * E)1690fca6ea1SDimitry Andric static bool isGLValueFromPointerDeref(const Expr *E) {
1700fca6ea1SDimitry Andric   E = E->IgnoreParens();
1710fca6ea1SDimitry Andric 
1720fca6ea1SDimitry Andric   if (const auto *CE = dyn_cast<CastExpr>(E)) {
1730fca6ea1SDimitry Andric     if (!CE->getSubExpr()->isGLValue())
1740fca6ea1SDimitry Andric       return false;
1750fca6ea1SDimitry Andric     return isGLValueFromPointerDeref(CE->getSubExpr());
1760fca6ea1SDimitry Andric   }
1770fca6ea1SDimitry Andric 
1780fca6ea1SDimitry Andric   if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E))
1790fca6ea1SDimitry Andric     return isGLValueFromPointerDeref(OVE->getSourceExpr());
1800fca6ea1SDimitry Andric 
1810fca6ea1SDimitry Andric   if (const auto *BO = dyn_cast<BinaryOperator>(E))
1820fca6ea1SDimitry Andric     if (BO->getOpcode() == BO_Comma)
1830fca6ea1SDimitry Andric       return isGLValueFromPointerDeref(BO->getRHS());
1840fca6ea1SDimitry Andric 
1850fca6ea1SDimitry Andric   if (const auto *ACO = dyn_cast<AbstractConditionalOperator>(E))
1860fca6ea1SDimitry Andric     return isGLValueFromPointerDeref(ACO->getTrueExpr()) ||
1870fca6ea1SDimitry Andric            isGLValueFromPointerDeref(ACO->getFalseExpr());
1880fca6ea1SDimitry Andric 
1890fca6ea1SDimitry Andric   // C++11 [expr.sub]p1:
1900fca6ea1SDimitry Andric   //   The expression E1[E2] is identical (by definition) to *((E1)+(E2))
1910fca6ea1SDimitry Andric   if (isa<ArraySubscriptExpr>(E))
1920fca6ea1SDimitry Andric     return true;
1930fca6ea1SDimitry Andric 
1940fca6ea1SDimitry Andric   if (const auto *UO = dyn_cast<UnaryOperator>(E))
1950fca6ea1SDimitry Andric     if (UO->getOpcode() == UO_Deref)
1960fca6ea1SDimitry Andric       return true;
1970fca6ea1SDimitry Andric 
1980fca6ea1SDimitry Andric   return false;
1990fca6ea1SDimitry Andric }
2000fca6ea1SDimitry Andric 
hasNullCheck() const2010fca6ea1SDimitry Andric bool CXXTypeidExpr::hasNullCheck() const {
2020fca6ea1SDimitry Andric   if (!isPotentiallyEvaluated())
2030fca6ea1SDimitry Andric     return false;
2040fca6ea1SDimitry Andric 
2050fca6ea1SDimitry Andric   // C++ [expr.typeid]p2:
2060fca6ea1SDimitry Andric   //   If the glvalue expression is obtained by applying the unary * operator to
2070fca6ea1SDimitry Andric   //   a pointer and the pointer is a null pointer value, the typeid expression
2080fca6ea1SDimitry Andric   //   throws the std::bad_typeid exception.
2090fca6ea1SDimitry Andric   //
2100fca6ea1SDimitry Andric   // However, this paragraph's intent is not clear.  We choose a very generous
2110fca6ea1SDimitry Andric   // interpretation which implores us to consider comma operators, conditional
2120fca6ea1SDimitry Andric   // operators, parentheses and other such constructs.
2130fca6ea1SDimitry Andric   return isGLValueFromPointerDeref(getExprOperand());
2140fca6ea1SDimitry Andric }
2150fca6ea1SDimitry Andric 
getTypeOperand(ASTContext & Context) const2160b57cec5SDimitry Andric QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
2170b57cec5SDimitry Andric   assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
2180b57cec5SDimitry Andric   Qualifiers Quals;
2190b57cec5SDimitry Andric   return Context.getUnqualifiedArrayType(
2200b57cec5SDimitry Andric       Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric // CXXScalarValueInitExpr
getBeginLoc() const2240b57cec5SDimitry Andric SourceLocation CXXScalarValueInitExpr::getBeginLoc() const {
2250b57cec5SDimitry Andric   return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : getRParenLoc();
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric // CXXNewExpr
CXXNewExpr(bool IsGlobalNew,FunctionDecl * OperatorNew,FunctionDecl * OperatorDelete,bool ShouldPassAlignment,bool UsualArrayDeleteWantsSize,ArrayRef<Expr * > PlacementArgs,SourceRange TypeIdParens,std::optional<Expr * > ArraySize,CXXNewInitializationStyle InitializationStyle,Expr * Initializer,QualType Ty,TypeSourceInfo * AllocatedTypeInfo,SourceRange Range,SourceRange DirectInitRange)2290b57cec5SDimitry Andric CXXNewExpr::CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
2300b57cec5SDimitry Andric                        FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
2310b57cec5SDimitry Andric                        bool UsualArrayDeleteWantsSize,
2320b57cec5SDimitry Andric                        ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
233bdd1243dSDimitry Andric                        std::optional<Expr *> ArraySize,
2345f757f3fSDimitry Andric                        CXXNewInitializationStyle InitializationStyle,
2350b57cec5SDimitry Andric                        Expr *Initializer, QualType Ty,
2360b57cec5SDimitry Andric                        TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
2370b57cec5SDimitry Andric                        SourceRange DirectInitRange)
238fe6060f1SDimitry Andric     : Expr(CXXNewExprClass, Ty, VK_PRValue, OK_Ordinary),
2390b57cec5SDimitry Andric       OperatorNew(OperatorNew), OperatorDelete(OperatorDelete),
2400b57cec5SDimitry Andric       AllocatedTypeInfo(AllocatedTypeInfo), Range(Range),
2410b57cec5SDimitry Andric       DirectInitRange(DirectInitRange) {
2420b57cec5SDimitry Andric 
2435f757f3fSDimitry Andric   assert((Initializer != nullptr ||
2447a6dacacSDimitry Andric           InitializationStyle == CXXNewInitializationStyle::None) &&
2457a6dacacSDimitry Andric          "Only CXXNewInitializationStyle::None can have no initializer!");
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   CXXNewExprBits.IsGlobalNew = IsGlobalNew;
24881ad6265SDimitry Andric   CXXNewExprBits.IsArray = ArraySize.has_value();
2490b57cec5SDimitry Andric   CXXNewExprBits.ShouldPassAlignment = ShouldPassAlignment;
2500b57cec5SDimitry Andric   CXXNewExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
2517a6dacacSDimitry Andric   CXXNewExprBits.HasInitializer = Initializer != nullptr;
2520b57cec5SDimitry Andric   CXXNewExprBits.StoredInitializationStyle =
2535f757f3fSDimitry Andric       llvm::to_underlying(InitializationStyle);
2540b57cec5SDimitry Andric   bool IsParenTypeId = TypeIdParens.isValid();
2550b57cec5SDimitry Andric   CXXNewExprBits.IsParenTypeId = IsParenTypeId;
2560b57cec5SDimitry Andric   CXXNewExprBits.NumPlacementArgs = PlacementArgs.size();
2570b57cec5SDimitry Andric 
2585ffd83dbSDimitry Andric   if (ArraySize)
2590b57cec5SDimitry Andric     getTrailingObjects<Stmt *>()[arraySizeOffset()] = *ArraySize;
2605ffd83dbSDimitry Andric   if (Initializer)
2610b57cec5SDimitry Andric     getTrailingObjects<Stmt *>()[initExprOffset()] = Initializer;
2625ffd83dbSDimitry Andric   for (unsigned I = 0; I != PlacementArgs.size(); ++I)
2630b57cec5SDimitry Andric     getTrailingObjects<Stmt *>()[placementNewArgsOffset() + I] =
2640b57cec5SDimitry Andric         PlacementArgs[I];
2650b57cec5SDimitry Andric   if (IsParenTypeId)
2660b57cec5SDimitry Andric     getTrailingObjects<SourceRange>()[0] = TypeIdParens;
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric   switch (getInitializationStyle()) {
2697a6dacacSDimitry Andric   case CXXNewInitializationStyle::Parens:
2700b57cec5SDimitry Andric     this->Range.setEnd(DirectInitRange.getEnd());
2710b57cec5SDimitry Andric     break;
2727a6dacacSDimitry Andric   case CXXNewInitializationStyle::Braces:
2730b57cec5SDimitry Andric     this->Range.setEnd(getInitializer()->getSourceRange().getEnd());
2740b57cec5SDimitry Andric     break;
2750b57cec5SDimitry Andric   default:
2760b57cec5SDimitry Andric     if (IsParenTypeId)
2770b57cec5SDimitry Andric       this->Range.setEnd(TypeIdParens.getEnd());
2780b57cec5SDimitry Andric     break;
2790b57cec5SDimitry Andric   }
2805ffd83dbSDimitry Andric 
2815ffd83dbSDimitry Andric   setDependence(computeDependence(this));
2820b57cec5SDimitry Andric }
2830b57cec5SDimitry Andric 
CXXNewExpr(EmptyShell Empty,bool IsArray,unsigned NumPlacementArgs,bool IsParenTypeId)2840b57cec5SDimitry Andric CXXNewExpr::CXXNewExpr(EmptyShell Empty, bool IsArray,
2850b57cec5SDimitry Andric                        unsigned NumPlacementArgs, bool IsParenTypeId)
2860b57cec5SDimitry Andric     : Expr(CXXNewExprClass, Empty) {
2870b57cec5SDimitry Andric   CXXNewExprBits.IsArray = IsArray;
2880b57cec5SDimitry Andric   CXXNewExprBits.NumPlacementArgs = NumPlacementArgs;
2890b57cec5SDimitry Andric   CXXNewExprBits.IsParenTypeId = IsParenTypeId;
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric 
Create(const ASTContext & Ctx,bool IsGlobalNew,FunctionDecl * OperatorNew,FunctionDecl * OperatorDelete,bool ShouldPassAlignment,bool UsualArrayDeleteWantsSize,ArrayRef<Expr * > PlacementArgs,SourceRange TypeIdParens,std::optional<Expr * > ArraySize,CXXNewInitializationStyle InitializationStyle,Expr * Initializer,QualType Ty,TypeSourceInfo * AllocatedTypeInfo,SourceRange Range,SourceRange DirectInitRange)2925f757f3fSDimitry Andric CXXNewExpr *CXXNewExpr::Create(
2935f757f3fSDimitry Andric     const ASTContext &Ctx, bool IsGlobalNew, FunctionDecl *OperatorNew,
2945f757f3fSDimitry Andric     FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
2955f757f3fSDimitry Andric     bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs,
2965f757f3fSDimitry Andric     SourceRange TypeIdParens, std::optional<Expr *> ArraySize,
2975f757f3fSDimitry Andric     CXXNewInitializationStyle InitializationStyle, Expr *Initializer,
2985f757f3fSDimitry Andric     QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
2995f757f3fSDimitry Andric     SourceRange DirectInitRange) {
30081ad6265SDimitry Andric   bool IsArray = ArraySize.has_value();
3010b57cec5SDimitry Andric   bool HasInit = Initializer != nullptr;
3020b57cec5SDimitry Andric   unsigned NumPlacementArgs = PlacementArgs.size();
3030b57cec5SDimitry Andric   bool IsParenTypeId = TypeIdParens.isValid();
3040b57cec5SDimitry Andric   void *Mem =
3050b57cec5SDimitry Andric       Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
3060b57cec5SDimitry Andric                        IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
3070b57cec5SDimitry Andric                    alignof(CXXNewExpr));
3080b57cec5SDimitry Andric   return new (Mem)
3090b57cec5SDimitry Andric       CXXNewExpr(IsGlobalNew, OperatorNew, OperatorDelete, ShouldPassAlignment,
3100b57cec5SDimitry Andric                  UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens,
3110b57cec5SDimitry Andric                  ArraySize, InitializationStyle, Initializer, Ty,
3120b57cec5SDimitry Andric                  AllocatedTypeInfo, Range, DirectInitRange);
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & Ctx,bool IsArray,bool HasInit,unsigned NumPlacementArgs,bool IsParenTypeId)3150b57cec5SDimitry Andric CXXNewExpr *CXXNewExpr::CreateEmpty(const ASTContext &Ctx, bool IsArray,
3160b57cec5SDimitry Andric                                     bool HasInit, unsigned NumPlacementArgs,
3170b57cec5SDimitry Andric                                     bool IsParenTypeId) {
3180b57cec5SDimitry Andric   void *Mem =
3190b57cec5SDimitry Andric       Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
3200b57cec5SDimitry Andric                        IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
3210b57cec5SDimitry Andric                    alignof(CXXNewExpr));
3220b57cec5SDimitry Andric   return new (Mem)
3230b57cec5SDimitry Andric       CXXNewExpr(EmptyShell(), IsArray, NumPlacementArgs, IsParenTypeId);
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric 
shouldNullCheckAllocation() const3260b57cec5SDimitry Andric bool CXXNewExpr::shouldNullCheckAllocation() const {
32706c3fb27SDimitry Andric   if (getOperatorNew()->getLangOpts().CheckNew)
32806c3fb27SDimitry Andric     return true;
329fe6060f1SDimitry Andric   return !getOperatorNew()->hasAttr<ReturnsNonNullAttr>() &&
330fe6060f1SDimitry Andric          getOperatorNew()
3310b57cec5SDimitry Andric              ->getType()
3320b57cec5SDimitry Andric              ->castAs<FunctionProtoType>()
3330b57cec5SDimitry Andric              ->isNothrow() &&
3340b57cec5SDimitry Andric          !getOperatorNew()->isReservedGlobalPlacementOperator();
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric // CXXDeleteExpr
getDestroyedType() const3380b57cec5SDimitry Andric QualType CXXDeleteExpr::getDestroyedType() const {
3390b57cec5SDimitry Andric   const Expr *Arg = getArgument();
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric   // For a destroying operator delete, we may have implicitly converted the
3420b57cec5SDimitry Andric   // pointer type to the type of the parameter of the 'operator delete'
3430b57cec5SDimitry Andric   // function.
3440b57cec5SDimitry Andric   while (const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
3450b57cec5SDimitry Andric     if (ICE->getCastKind() == CK_DerivedToBase ||
3460b57cec5SDimitry Andric         ICE->getCastKind() == CK_UncheckedDerivedToBase ||
3470b57cec5SDimitry Andric         ICE->getCastKind() == CK_NoOp) {
3480b57cec5SDimitry Andric       assert((ICE->getCastKind() == CK_NoOp ||
3490b57cec5SDimitry Andric               getOperatorDelete()->isDestroyingOperatorDelete()) &&
3500b57cec5SDimitry Andric              "only a destroying operator delete can have a converted arg");
3510b57cec5SDimitry Andric       Arg = ICE->getSubExpr();
3520b57cec5SDimitry Andric     } else
3530b57cec5SDimitry Andric       break;
3540b57cec5SDimitry Andric   }
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   // The type-to-delete may not be a pointer if it's a dependent type.
3570b57cec5SDimitry Andric   const QualType ArgType = Arg->getType();
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric   if (ArgType->isDependentType() && !ArgType->isPointerType())
3600b57cec5SDimitry Andric     return QualType();
3610b57cec5SDimitry Andric 
362a7dea167SDimitry Andric   return ArgType->castAs<PointerType>()->getPointeeType();
3630b57cec5SDimitry Andric }
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric // CXXPseudoDestructorExpr
PseudoDestructorTypeStorage(TypeSourceInfo * Info)3660b57cec5SDimitry Andric PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
3670b57cec5SDimitry Andric     : Type(Info) {
368bdd1243dSDimitry Andric   Location = Info->getTypeLoc().getBeginLoc();
3690b57cec5SDimitry Andric }
3700b57cec5SDimitry Andric 
CXXPseudoDestructorExpr(const ASTContext & Context,Expr * Base,bool isArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,TypeSourceInfo * ScopeType,SourceLocation ColonColonLoc,SourceLocation TildeLoc,PseudoDestructorTypeStorage DestroyedType)3715ffd83dbSDimitry Andric CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(
3725ffd83dbSDimitry Andric     const ASTContext &Context, Expr *Base, bool isArrow,
3735ffd83dbSDimitry Andric     SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
3745ffd83dbSDimitry Andric     TypeSourceInfo *ScopeType, SourceLocation ColonColonLoc,
3755ffd83dbSDimitry Andric     SourceLocation TildeLoc, PseudoDestructorTypeStorage DestroyedType)
376fe6060f1SDimitry Andric     : Expr(CXXPseudoDestructorExprClass, Context.BoundMemberTy, VK_PRValue,
3775ffd83dbSDimitry Andric            OK_Ordinary),
3780b57cec5SDimitry Andric       Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
3790b57cec5SDimitry Andric       OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
3800b57cec5SDimitry Andric       ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
3815ffd83dbSDimitry Andric       DestroyedType(DestroyedType) {
3825ffd83dbSDimitry Andric   setDependence(computeDependence(this));
3835ffd83dbSDimitry Andric }
3840b57cec5SDimitry Andric 
getDestroyedType() const3850b57cec5SDimitry Andric QualType CXXPseudoDestructorExpr::getDestroyedType() const {
3860b57cec5SDimitry Andric   if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
3870b57cec5SDimitry Andric     return TInfo->getType();
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   return QualType();
3900b57cec5SDimitry Andric }
3910b57cec5SDimitry Andric 
getEndLoc() const3920b57cec5SDimitry Andric SourceLocation CXXPseudoDestructorExpr::getEndLoc() const {
3930b57cec5SDimitry Andric   SourceLocation End = DestroyedType.getLocation();
3940b57cec5SDimitry Andric   if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
395bdd1243dSDimitry Andric     End = TInfo->getTypeLoc().getSourceRange().getEnd();
3960b57cec5SDimitry Andric   return End;
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric // UnresolvedLookupExpr
UnresolvedLookupExpr(const ASTContext & Context,CXXRecordDecl * NamingClass,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,bool RequiresADL,const TemplateArgumentListInfo * TemplateArgs,UnresolvedSetIterator Begin,UnresolvedSetIterator End,bool KnownDependent,bool KnownInstantiationDependent)4000b57cec5SDimitry Andric UnresolvedLookupExpr::UnresolvedLookupExpr(
4010b57cec5SDimitry Andric     const ASTContext &Context, CXXRecordDecl *NamingClass,
4020b57cec5SDimitry Andric     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
4030fca6ea1SDimitry Andric     const DeclarationNameInfo &NameInfo, bool RequiresADL,
4040b57cec5SDimitry Andric     const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
405*62987288SDimitry Andric     UnresolvedSetIterator End, bool KnownDependent,
406*62987288SDimitry Andric     bool KnownInstantiationDependent)
4070b57cec5SDimitry Andric     : OverloadExpr(UnresolvedLookupExprClass, Context, QualifierLoc,
4085f757f3fSDimitry Andric                    TemplateKWLoc, NameInfo, TemplateArgs, Begin, End,
409*62987288SDimitry Andric                    KnownDependent, KnownInstantiationDependent, false),
4100b57cec5SDimitry Andric       NamingClass(NamingClass) {
4110b57cec5SDimitry Andric   UnresolvedLookupExprBits.RequiresADL = RequiresADL;
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric 
UnresolvedLookupExpr(EmptyShell Empty,unsigned NumResults,bool HasTemplateKWAndArgsInfo)4140b57cec5SDimitry Andric UnresolvedLookupExpr::UnresolvedLookupExpr(EmptyShell Empty,
4150b57cec5SDimitry Andric                                            unsigned NumResults,
4160b57cec5SDimitry Andric                                            bool HasTemplateKWAndArgsInfo)
4170b57cec5SDimitry Andric     : OverloadExpr(UnresolvedLookupExprClass, Empty, NumResults,
4180b57cec5SDimitry Andric                    HasTemplateKWAndArgsInfo) {}
4190b57cec5SDimitry Andric 
Create(const ASTContext & Context,CXXRecordDecl * NamingClass,NestedNameSpecifierLoc QualifierLoc,const DeclarationNameInfo & NameInfo,bool RequiresADL,UnresolvedSetIterator Begin,UnresolvedSetIterator End,bool KnownDependent,bool KnownInstantiationDependent)4200b57cec5SDimitry Andric UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
4210b57cec5SDimitry Andric     const ASTContext &Context, CXXRecordDecl *NamingClass,
4220b57cec5SDimitry Andric     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
4230fca6ea1SDimitry Andric     bool RequiresADL, UnresolvedSetIterator Begin, UnresolvedSetIterator End,
424*62987288SDimitry Andric     bool KnownDependent, bool KnownInstantiationDependent) {
4250b57cec5SDimitry Andric   unsigned NumResults = End - Begin;
4260b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
4270b57cec5SDimitry Andric                                    TemplateArgumentLoc>(NumResults, 0, 0);
4280b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
4290fca6ea1SDimitry Andric   return new (Mem) UnresolvedLookupExpr(
4300fca6ea1SDimitry Andric       Context, NamingClass, QualifierLoc,
4310fca6ea1SDimitry Andric       /*TemplateKWLoc=*/SourceLocation(), NameInfo, RequiresADL,
432*62987288SDimitry Andric       /*TemplateArgs=*/nullptr, Begin, End, KnownDependent,
433*62987288SDimitry Andric       KnownInstantiationDependent);
4340b57cec5SDimitry Andric }
4350b57cec5SDimitry Andric 
Create(const ASTContext & Context,CXXRecordDecl * NamingClass,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,bool RequiresADL,const TemplateArgumentListInfo * Args,UnresolvedSetIterator Begin,UnresolvedSetIterator End,bool KnownDependent,bool KnownInstantiationDependent)4360b57cec5SDimitry Andric UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
4370b57cec5SDimitry Andric     const ASTContext &Context, CXXRecordDecl *NamingClass,
4380b57cec5SDimitry Andric     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
4390b57cec5SDimitry Andric     const DeclarationNameInfo &NameInfo, bool RequiresADL,
4400b57cec5SDimitry Andric     const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
441*62987288SDimitry Andric     UnresolvedSetIterator End, bool KnownDependent,
442*62987288SDimitry Andric     bool KnownInstantiationDependent) {
4430b57cec5SDimitry Andric   unsigned NumResults = End - Begin;
4440fca6ea1SDimitry Andric   bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
4450b57cec5SDimitry Andric   unsigned NumTemplateArgs = Args ? Args->size() : 0;
4460fca6ea1SDimitry Andric   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
4470fca6ea1SDimitry Andric                                    TemplateArgumentLoc>(
4480fca6ea1SDimitry Andric       NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
4490b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
450*62987288SDimitry Andric   return new (Mem) UnresolvedLookupExpr(
451*62987288SDimitry Andric       Context, NamingClass, QualifierLoc, TemplateKWLoc, NameInfo, RequiresADL,
452*62987288SDimitry Andric       Args, Begin, End, KnownDependent, KnownInstantiationDependent);
4530b57cec5SDimitry Andric }
4540b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & Context,unsigned NumResults,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs)4550b57cec5SDimitry Andric UnresolvedLookupExpr *UnresolvedLookupExpr::CreateEmpty(
4560b57cec5SDimitry Andric     const ASTContext &Context, unsigned NumResults,
4570b57cec5SDimitry Andric     bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
4580b57cec5SDimitry Andric   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
4590b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
4600b57cec5SDimitry Andric                                    TemplateArgumentLoc>(
4610b57cec5SDimitry Andric       NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
4620b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
4630b57cec5SDimitry Andric   return new (Mem)
4640b57cec5SDimitry Andric       UnresolvedLookupExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
4650b57cec5SDimitry Andric }
4660b57cec5SDimitry Andric 
OverloadExpr(StmtClass SC,const ASTContext & Context,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * TemplateArgs,UnresolvedSetIterator Begin,UnresolvedSetIterator End,bool KnownDependent,bool KnownInstantiationDependent,bool KnownContainsUnexpandedParameterPack)4670b57cec5SDimitry Andric OverloadExpr::OverloadExpr(StmtClass SC, const ASTContext &Context,
4680b57cec5SDimitry Andric                            NestedNameSpecifierLoc QualifierLoc,
4690b57cec5SDimitry Andric                            SourceLocation TemplateKWLoc,
4700b57cec5SDimitry Andric                            const DeclarationNameInfo &NameInfo,
4710b57cec5SDimitry Andric                            const TemplateArgumentListInfo *TemplateArgs,
4720b57cec5SDimitry Andric                            UnresolvedSetIterator Begin,
4730b57cec5SDimitry Andric                            UnresolvedSetIterator End, bool KnownDependent,
4740b57cec5SDimitry Andric                            bool KnownInstantiationDependent,
4750b57cec5SDimitry Andric                            bool KnownContainsUnexpandedParameterPack)
4765ffd83dbSDimitry Andric     : Expr(SC, Context.OverloadTy, VK_LValue, OK_Ordinary), NameInfo(NameInfo),
4775ffd83dbSDimitry Andric       QualifierLoc(QualifierLoc) {
4780b57cec5SDimitry Andric   unsigned NumResults = End - Begin;
4790b57cec5SDimitry Andric   OverloadExprBits.NumResults = NumResults;
4800b57cec5SDimitry Andric   OverloadExprBits.HasTemplateKWAndArgsInfo =
4810b57cec5SDimitry Andric       (TemplateArgs != nullptr ) || TemplateKWLoc.isValid();
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric   if (NumResults) {
4840b57cec5SDimitry Andric     // Copy the results to the trailing array past UnresolvedLookupExpr
4850b57cec5SDimitry Andric     // or UnresolvedMemberExpr.
4860b57cec5SDimitry Andric     DeclAccessPair *Results = getTrailingResults();
4870b57cec5SDimitry Andric     memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
4880b57cec5SDimitry Andric   }
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   if (TemplateArgs) {
4915ffd83dbSDimitry Andric     auto Deps = TemplateArgumentDependence::None;
4920b57cec5SDimitry Andric     getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
4935ffd83dbSDimitry Andric         TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(), Deps);
4940b57cec5SDimitry Andric   } else if (TemplateKWLoc.isValid()) {
4950b57cec5SDimitry Andric     getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
4960b57cec5SDimitry Andric   }
4970b57cec5SDimitry Andric 
4985ffd83dbSDimitry Andric   setDependence(computeDependence(this, KnownDependent,
4995ffd83dbSDimitry Andric                                   KnownInstantiationDependent,
5005ffd83dbSDimitry Andric                                   KnownContainsUnexpandedParameterPack));
5010b57cec5SDimitry Andric   if (isTypeDependent())
5020b57cec5SDimitry Andric     setType(Context.DependentTy);
5030b57cec5SDimitry Andric }
5040b57cec5SDimitry Andric 
OverloadExpr(StmtClass SC,EmptyShell Empty,unsigned NumResults,bool HasTemplateKWAndArgsInfo)5050b57cec5SDimitry Andric OverloadExpr::OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
5060b57cec5SDimitry Andric                            bool HasTemplateKWAndArgsInfo)
5070b57cec5SDimitry Andric     : Expr(SC, Empty) {
5080b57cec5SDimitry Andric   OverloadExprBits.NumResults = NumResults;
5090b57cec5SDimitry Andric   OverloadExprBits.HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric // DependentScopeDeclRefExpr
DependentScopeDeclRefExpr(QualType Ty,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * Args)5130b57cec5SDimitry Andric DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(
5140b57cec5SDimitry Andric     QualType Ty, NestedNameSpecifierLoc QualifierLoc,
5150b57cec5SDimitry Andric     SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
5160b57cec5SDimitry Andric     const TemplateArgumentListInfo *Args)
5175ffd83dbSDimitry Andric     : Expr(DependentScopeDeclRefExprClass, Ty, VK_LValue, OK_Ordinary),
5180b57cec5SDimitry Andric       QualifierLoc(QualifierLoc), NameInfo(NameInfo) {
5190b57cec5SDimitry Andric   DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
5200b57cec5SDimitry Andric       (Args != nullptr) || TemplateKWLoc.isValid();
5210b57cec5SDimitry Andric   if (Args) {
5225ffd83dbSDimitry Andric     auto Deps = TemplateArgumentDependence::None;
5230b57cec5SDimitry Andric     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
5245ffd83dbSDimitry Andric         TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(), Deps);
5250b57cec5SDimitry Andric   } else if (TemplateKWLoc.isValid()) {
5260b57cec5SDimitry Andric     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
5270b57cec5SDimitry Andric         TemplateKWLoc);
5280b57cec5SDimitry Andric   }
5295ffd83dbSDimitry Andric   setDependence(computeDependence(this));
5300b57cec5SDimitry Andric }
5310b57cec5SDimitry Andric 
Create(const ASTContext & Context,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * Args)5320b57cec5SDimitry Andric DependentScopeDeclRefExpr *DependentScopeDeclRefExpr::Create(
5330b57cec5SDimitry Andric     const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
5340b57cec5SDimitry Andric     SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
5350b57cec5SDimitry Andric     const TemplateArgumentListInfo *Args) {
5360b57cec5SDimitry Andric   assert(QualifierLoc && "should be created for dependent qualifiers");
5370b57cec5SDimitry Andric   bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
5380b57cec5SDimitry Andric   std::size_t Size =
5390b57cec5SDimitry Andric       totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
5400b57cec5SDimitry Andric           HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
5410b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size);
5420b57cec5SDimitry Andric   return new (Mem) DependentScopeDeclRefExpr(Context.DependentTy, QualifierLoc,
5430b57cec5SDimitry Andric                                              TemplateKWLoc, NameInfo, Args);
5440b57cec5SDimitry Andric }
5450b57cec5SDimitry Andric 
5460b57cec5SDimitry Andric DependentScopeDeclRefExpr *
CreateEmpty(const ASTContext & Context,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs)5470b57cec5SDimitry Andric DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &Context,
5480b57cec5SDimitry Andric                                        bool HasTemplateKWAndArgsInfo,
5490b57cec5SDimitry Andric                                        unsigned NumTemplateArgs) {
5500b57cec5SDimitry Andric   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
5510b57cec5SDimitry Andric   std::size_t Size =
5520b57cec5SDimitry Andric       totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
5530b57cec5SDimitry Andric           HasTemplateKWAndArgsInfo, NumTemplateArgs);
5540b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size);
5550b57cec5SDimitry Andric   auto *E = new (Mem) DependentScopeDeclRefExpr(
5560b57cec5SDimitry Andric       QualType(), NestedNameSpecifierLoc(), SourceLocation(),
5570b57cec5SDimitry Andric       DeclarationNameInfo(), nullptr);
5580b57cec5SDimitry Andric   E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
5590b57cec5SDimitry Andric       HasTemplateKWAndArgsInfo;
5600b57cec5SDimitry Andric   return E;
5610b57cec5SDimitry Andric }
5620b57cec5SDimitry Andric 
getBeginLoc() const5630b57cec5SDimitry Andric SourceLocation CXXConstructExpr::getBeginLoc() const {
5640fca6ea1SDimitry Andric   if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(this))
5650fca6ea1SDimitry Andric     return TOE->getBeginLoc();
5660b57cec5SDimitry Andric   return getLocation();
5670b57cec5SDimitry Andric }
5680b57cec5SDimitry Andric 
getEndLoc() const5690b57cec5SDimitry Andric SourceLocation CXXConstructExpr::getEndLoc() const {
5700fca6ea1SDimitry Andric   if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(this))
5710fca6ea1SDimitry Andric     return TOE->getEndLoc();
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   if (ParenOrBraceRange.isValid())
5740b57cec5SDimitry Andric     return ParenOrBraceRange.getEnd();
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric   SourceLocation End = getLocation();
5770b57cec5SDimitry Andric   for (unsigned I = getNumArgs(); I > 0; --I) {
5780b57cec5SDimitry Andric     const Expr *Arg = getArg(I-1);
5790b57cec5SDimitry Andric     if (!Arg->isDefaultArgument()) {
5800b57cec5SDimitry Andric       SourceLocation NewEnd = Arg->getEndLoc();
5810b57cec5SDimitry Andric       if (NewEnd.isValid()) {
5820b57cec5SDimitry Andric         End = NewEnd;
5830b57cec5SDimitry Andric         break;
5840b57cec5SDimitry Andric       }
5850b57cec5SDimitry Andric     }
5860b57cec5SDimitry Andric   }
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric   return End;
5890b57cec5SDimitry Andric }
5900b57cec5SDimitry Andric 
CXXOperatorCallExpr(OverloadedOperatorKind OpKind,Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation OperatorLoc,FPOptionsOverride FPFeatures,ADLCallKind UsesADL)5910b57cec5SDimitry Andric CXXOperatorCallExpr::CXXOperatorCallExpr(OverloadedOperatorKind OpKind,
5920b57cec5SDimitry Andric                                          Expr *Fn, ArrayRef<Expr *> Args,
5930b57cec5SDimitry Andric                                          QualType Ty, ExprValueKind VK,
5940b57cec5SDimitry Andric                                          SourceLocation OperatorLoc,
5955ffd83dbSDimitry Andric                                          FPOptionsOverride FPFeatures,
5960b57cec5SDimitry Andric                                          ADLCallKind UsesADL)
5970b57cec5SDimitry Andric     : CallExpr(CXXOperatorCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
598e8d8bef9SDimitry Andric                OperatorLoc, FPFeatures, /*MinNumArgs=*/0, UsesADL) {
5990b57cec5SDimitry Andric   CXXOperatorCallExprBits.OperatorKind = OpKind;
6000b57cec5SDimitry Andric   assert(
6010b57cec5SDimitry Andric       (CXXOperatorCallExprBits.OperatorKind == static_cast<unsigned>(OpKind)) &&
6020b57cec5SDimitry Andric       "OperatorKind overflow!");
6030b57cec5SDimitry Andric   Range = getSourceRangeImpl();
6040b57cec5SDimitry Andric }
6050b57cec5SDimitry Andric 
CXXOperatorCallExpr(unsigned NumArgs,bool HasFPFeatures,EmptyShell Empty)606e8d8bef9SDimitry Andric CXXOperatorCallExpr::CXXOperatorCallExpr(unsigned NumArgs, bool HasFPFeatures,
607e8d8bef9SDimitry Andric                                          EmptyShell Empty)
608e8d8bef9SDimitry Andric     : CallExpr(CXXOperatorCallExprClass, /*NumPreArgs=*/0, NumArgs,
609e8d8bef9SDimitry Andric                HasFPFeatures, Empty) {}
6100b57cec5SDimitry Andric 
6115ffd83dbSDimitry Andric CXXOperatorCallExpr *
Create(const ASTContext & Ctx,OverloadedOperatorKind OpKind,Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation OperatorLoc,FPOptionsOverride FPFeatures,ADLCallKind UsesADL)6125ffd83dbSDimitry Andric CXXOperatorCallExpr::Create(const ASTContext &Ctx,
6135ffd83dbSDimitry Andric                             OverloadedOperatorKind OpKind, Expr *Fn,
6145ffd83dbSDimitry Andric                             ArrayRef<Expr *> Args, QualType Ty,
6155ffd83dbSDimitry Andric                             ExprValueKind VK, SourceLocation OperatorLoc,
6165ffd83dbSDimitry Andric                             FPOptionsOverride FPFeatures, ADLCallKind UsesADL) {
6170b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
6180b57cec5SDimitry Andric   unsigned NumArgs = Args.size();
619e8d8bef9SDimitry Andric   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
620e8d8bef9SDimitry Andric       /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
6210b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
6220b57cec5SDimitry Andric                            alignof(CXXOperatorCallExpr));
6230b57cec5SDimitry Andric   return new (Mem) CXXOperatorCallExpr(OpKind, Fn, Args, Ty, VK, OperatorLoc,
6240b57cec5SDimitry Andric                                        FPFeatures, UsesADL);
6250b57cec5SDimitry Andric }
6260b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs,bool HasFPFeatures,EmptyShell Empty)6270b57cec5SDimitry Andric CXXOperatorCallExpr *CXXOperatorCallExpr::CreateEmpty(const ASTContext &Ctx,
6280b57cec5SDimitry Andric                                                       unsigned NumArgs,
629e8d8bef9SDimitry Andric                                                       bool HasFPFeatures,
6300b57cec5SDimitry Andric                                                       EmptyShell Empty) {
6310b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
6320b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects =
633e8d8bef9SDimitry Andric       CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPFeatures);
6340b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
6350b57cec5SDimitry Andric                            alignof(CXXOperatorCallExpr));
636e8d8bef9SDimitry Andric   return new (Mem) CXXOperatorCallExpr(NumArgs, HasFPFeatures, Empty);
6370b57cec5SDimitry Andric }
6380b57cec5SDimitry Andric 
getSourceRangeImpl() const6390b57cec5SDimitry Andric SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
6400b57cec5SDimitry Andric   OverloadedOperatorKind Kind = getOperator();
6410b57cec5SDimitry Andric   if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
6420b57cec5SDimitry Andric     if (getNumArgs() == 1)
6430b57cec5SDimitry Andric       // Prefix operator
6440b57cec5SDimitry Andric       return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
6450b57cec5SDimitry Andric     else
6460b57cec5SDimitry Andric       // Postfix operator
6470b57cec5SDimitry Andric       return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
6480b57cec5SDimitry Andric   } else if (Kind == OO_Arrow) {
6495ffd83dbSDimitry Andric     return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
6500b57cec5SDimitry Andric   } else if (Kind == OO_Call) {
6510b57cec5SDimitry Andric     return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
6520b57cec5SDimitry Andric   } else if (Kind == OO_Subscript) {
6530b57cec5SDimitry Andric     return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
6540b57cec5SDimitry Andric   } else if (getNumArgs() == 1) {
6550b57cec5SDimitry Andric     return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
6560b57cec5SDimitry Andric   } else if (getNumArgs() == 2) {
6570b57cec5SDimitry Andric     return SourceRange(getArg(0)->getBeginLoc(), getArg(1)->getEndLoc());
6580b57cec5SDimitry Andric   } else {
6590b57cec5SDimitry Andric     return getOperatorLoc();
6600b57cec5SDimitry Andric   }
6610b57cec5SDimitry Andric }
6620b57cec5SDimitry Andric 
CXXMemberCallExpr(Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation RP,FPOptionsOverride FPOptions,unsigned MinNumArgs)6630b57cec5SDimitry Andric CXXMemberCallExpr::CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args,
6640b57cec5SDimitry Andric                                      QualType Ty, ExprValueKind VK,
665e8d8bef9SDimitry Andric                                      SourceLocation RP,
666e8d8bef9SDimitry Andric                                      FPOptionsOverride FPOptions,
667e8d8bef9SDimitry Andric                                      unsigned MinNumArgs)
6680b57cec5SDimitry Andric     : CallExpr(CXXMemberCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, RP,
669e8d8bef9SDimitry Andric                FPOptions, MinNumArgs, NotADL) {}
6700b57cec5SDimitry Andric 
CXXMemberCallExpr(unsigned NumArgs,bool HasFPFeatures,EmptyShell Empty)671e8d8bef9SDimitry Andric CXXMemberCallExpr::CXXMemberCallExpr(unsigned NumArgs, bool HasFPFeatures,
672e8d8bef9SDimitry Andric                                      EmptyShell Empty)
673e8d8bef9SDimitry Andric     : CallExpr(CXXMemberCallExprClass, /*NumPreArgs=*/0, NumArgs, HasFPFeatures,
674e8d8bef9SDimitry Andric                Empty) {}
6750b57cec5SDimitry Andric 
Create(const ASTContext & Ctx,Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation RP,FPOptionsOverride FPFeatures,unsigned MinNumArgs)6760b57cec5SDimitry Andric CXXMemberCallExpr *CXXMemberCallExpr::Create(const ASTContext &Ctx, Expr *Fn,
6770b57cec5SDimitry Andric                                              ArrayRef<Expr *> Args, QualType Ty,
6780b57cec5SDimitry Andric                                              ExprValueKind VK,
6790b57cec5SDimitry Andric                                              SourceLocation RP,
680e8d8bef9SDimitry Andric                                              FPOptionsOverride FPFeatures,
6810b57cec5SDimitry Andric                                              unsigned MinNumArgs) {
6820b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
6830b57cec5SDimitry Andric   unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
684e8d8bef9SDimitry Andric   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
685e8d8bef9SDimitry Andric       /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
6860b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
6870b57cec5SDimitry Andric                            alignof(CXXMemberCallExpr));
688e8d8bef9SDimitry Andric   return new (Mem)
689e8d8bef9SDimitry Andric       CXXMemberCallExpr(Fn, Args, Ty, VK, RP, FPFeatures, MinNumArgs);
6900b57cec5SDimitry Andric }
6910b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs,bool HasFPFeatures,EmptyShell Empty)6920b57cec5SDimitry Andric CXXMemberCallExpr *CXXMemberCallExpr::CreateEmpty(const ASTContext &Ctx,
6930b57cec5SDimitry Andric                                                   unsigned NumArgs,
694e8d8bef9SDimitry Andric                                                   bool HasFPFeatures,
6950b57cec5SDimitry Andric                                                   EmptyShell Empty) {
6960b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
6970b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects =
698e8d8bef9SDimitry Andric       CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPFeatures);
6990b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
7000b57cec5SDimitry Andric                            alignof(CXXMemberCallExpr));
701e8d8bef9SDimitry Andric   return new (Mem) CXXMemberCallExpr(NumArgs, HasFPFeatures, Empty);
7020b57cec5SDimitry Andric }
7030b57cec5SDimitry Andric 
getImplicitObjectArgument() const7040b57cec5SDimitry Andric Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
7050b57cec5SDimitry Andric   const Expr *Callee = getCallee()->IgnoreParens();
7060b57cec5SDimitry Andric   if (const auto *MemExpr = dyn_cast<MemberExpr>(Callee))
7070b57cec5SDimitry Andric     return MemExpr->getBase();
7080b57cec5SDimitry Andric   if (const auto *BO = dyn_cast<BinaryOperator>(Callee))
7090b57cec5SDimitry Andric     if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
7100b57cec5SDimitry Andric       return BO->getLHS();
7110b57cec5SDimitry Andric 
7120b57cec5SDimitry Andric   // FIXME: Will eventually need to cope with member pointers.
7130b57cec5SDimitry Andric   return nullptr;
7140b57cec5SDimitry Andric }
7150b57cec5SDimitry Andric 
getObjectType() const7160b57cec5SDimitry Andric QualType CXXMemberCallExpr::getObjectType() const {
7170b57cec5SDimitry Andric   QualType Ty = getImplicitObjectArgument()->getType();
7180b57cec5SDimitry Andric   if (Ty->isPointerType())
7190b57cec5SDimitry Andric     Ty = Ty->getPointeeType();
7200b57cec5SDimitry Andric   return Ty;
7210b57cec5SDimitry Andric }
7220b57cec5SDimitry Andric 
getMethodDecl() const7230b57cec5SDimitry Andric CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
7240b57cec5SDimitry Andric   if (const auto *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
7250b57cec5SDimitry Andric     return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
7260b57cec5SDimitry Andric 
7270b57cec5SDimitry Andric   // FIXME: Will eventually need to cope with member pointers.
728fe6060f1SDimitry Andric   // NOTE: Update makeTailCallIfSwiftAsync on fixing this.
7290b57cec5SDimitry Andric   return nullptr;
7300b57cec5SDimitry Andric }
7310b57cec5SDimitry Andric 
getRecordDecl() const7320b57cec5SDimitry Andric CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
7330b57cec5SDimitry Andric   Expr* ThisArg = getImplicitObjectArgument();
7340b57cec5SDimitry Andric   if (!ThisArg)
7350b57cec5SDimitry Andric     return nullptr;
7360b57cec5SDimitry Andric 
7370b57cec5SDimitry Andric   if (ThisArg->getType()->isAnyPointerType())
7380b57cec5SDimitry Andric     return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric   return ThisArg->getType()->getAsCXXRecordDecl();
7410b57cec5SDimitry Andric }
7420b57cec5SDimitry Andric 
7430b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
7440b57cec5SDimitry Andric //  Named casts
7450b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric /// getCastName - Get the name of the C++ cast being used, e.g.,
7480b57cec5SDimitry Andric /// "static_cast", "dynamic_cast", "reinterpret_cast", or
7490b57cec5SDimitry Andric /// "const_cast". The returned pointer must not be freed.
getCastName() const7500b57cec5SDimitry Andric const char *CXXNamedCastExpr::getCastName() const {
7510b57cec5SDimitry Andric   switch (getStmtClass()) {
7520b57cec5SDimitry Andric   case CXXStaticCastExprClass:      return "static_cast";
7530b57cec5SDimitry Andric   case CXXDynamicCastExprClass:     return "dynamic_cast";
7540b57cec5SDimitry Andric   case CXXReinterpretCastExprClass: return "reinterpret_cast";
7550b57cec5SDimitry Andric   case CXXConstCastExprClass:       return "const_cast";
7565ffd83dbSDimitry Andric   case CXXAddrspaceCastExprClass:   return "addrspace_cast";
7570b57cec5SDimitry Andric   default:                          return "<invalid cast>";
7580b57cec5SDimitry Andric   }
7590b57cec5SDimitry Andric }
7600b57cec5SDimitry Andric 
761e8d8bef9SDimitry Andric CXXStaticCastExpr *
Create(const ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,const CXXCastPath * BasePath,TypeSourceInfo * WrittenTy,FPOptionsOverride FPO,SourceLocation L,SourceLocation RParenLoc,SourceRange AngleBrackets)762e8d8bef9SDimitry Andric CXXStaticCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
763e8d8bef9SDimitry Andric                           CastKind K, Expr *Op, const CXXCastPath *BasePath,
764e8d8bef9SDimitry Andric                           TypeSourceInfo *WrittenTy, FPOptionsOverride FPO,
765e8d8bef9SDimitry Andric                           SourceLocation L, SourceLocation RParenLoc,
7660b57cec5SDimitry Andric                           SourceRange AngleBrackets) {
7670b57cec5SDimitry Andric   unsigned PathSize = (BasePath ? BasePath->size() : 0);
768e8d8bef9SDimitry Andric   void *Buffer =
769e8d8bef9SDimitry Andric       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
770e8d8bef9SDimitry Andric           PathSize, FPO.requiresTrailingStorage()));
771e8d8bef9SDimitry Andric   auto *E = new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy,
772e8d8bef9SDimitry Andric                                            FPO, L, RParenLoc, AngleBrackets);
7730b57cec5SDimitry Andric   if (PathSize)
7740b57cec5SDimitry Andric     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
7750b57cec5SDimitry Andric                               E->getTrailingObjects<CXXBaseSpecifier *>());
7760b57cec5SDimitry Andric   return E;
7770b57cec5SDimitry Andric }
7780b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & C,unsigned PathSize,bool HasFPFeatures)7790b57cec5SDimitry Andric CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
780e8d8bef9SDimitry Andric                                                   unsigned PathSize,
781e8d8bef9SDimitry Andric                                                   bool HasFPFeatures) {
782e8d8bef9SDimitry Andric   void *Buffer =
783e8d8bef9SDimitry Andric       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
784e8d8bef9SDimitry Andric           PathSize, HasFPFeatures));
785e8d8bef9SDimitry Andric   return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize, HasFPFeatures);
7860b57cec5SDimitry Andric }
7870b57cec5SDimitry Andric 
Create(const ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,const CXXCastPath * BasePath,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc,SourceRange AngleBrackets)7880b57cec5SDimitry Andric CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
7890b57cec5SDimitry Andric                                                ExprValueKind VK,
7900b57cec5SDimitry Andric                                                CastKind K, Expr *Op,
7910b57cec5SDimitry Andric                                                const CXXCastPath *BasePath,
7920b57cec5SDimitry Andric                                                TypeSourceInfo *WrittenTy,
7930b57cec5SDimitry Andric                                                SourceLocation L,
7940b57cec5SDimitry Andric                                                SourceLocation RParenLoc,
7950b57cec5SDimitry Andric                                                SourceRange AngleBrackets) {
7960b57cec5SDimitry Andric   unsigned PathSize = (BasePath ? BasePath->size() : 0);
7970b57cec5SDimitry Andric   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
7980b57cec5SDimitry Andric   auto *E =
7990b57cec5SDimitry Andric       new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
8000b57cec5SDimitry Andric                                       RParenLoc, AngleBrackets);
8010b57cec5SDimitry Andric   if (PathSize)
8020b57cec5SDimitry Andric     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
8030b57cec5SDimitry Andric                               E->getTrailingObjects<CXXBaseSpecifier *>());
8040b57cec5SDimitry Andric   return E;
8050b57cec5SDimitry Andric }
8060b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & C,unsigned PathSize)8070b57cec5SDimitry Andric CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
8080b57cec5SDimitry Andric                                                     unsigned PathSize) {
8090b57cec5SDimitry Andric   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
8100b57cec5SDimitry Andric   return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
8110b57cec5SDimitry Andric }
8120b57cec5SDimitry Andric 
8130b57cec5SDimitry Andric /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
8140b57cec5SDimitry Andric /// to always be null. For example:
8150b57cec5SDimitry Andric ///
8160b57cec5SDimitry Andric /// struct A { };
8170b57cec5SDimitry Andric /// struct B final : A { };
8180b57cec5SDimitry Andric /// struct C { };
8190b57cec5SDimitry Andric ///
8200b57cec5SDimitry Andric /// C *f(B* b) { return dynamic_cast<C*>(b); }
isAlwaysNull() const82106c3fb27SDimitry Andric bool CXXDynamicCastExpr::isAlwaysNull() const {
82206c3fb27SDimitry Andric   if (isValueDependent() || getCastKind() != CK_Dynamic)
82306c3fb27SDimitry Andric     return false;
82406c3fb27SDimitry Andric 
8250b57cec5SDimitry Andric   QualType SrcType = getSubExpr()->getType();
8260b57cec5SDimitry Andric   QualType DestType = getType();
8270b57cec5SDimitry Andric 
82806c3fb27SDimitry Andric   if (DestType->isVoidPointerType())
82906c3fb27SDimitry Andric     return false;
83006c3fb27SDimitry Andric 
83106c3fb27SDimitry Andric   if (DestType->isPointerType()) {
83206c3fb27SDimitry Andric     SrcType = SrcType->getPointeeType();
83306c3fb27SDimitry Andric     DestType = DestType->getPointeeType();
8340b57cec5SDimitry Andric   }
8350b57cec5SDimitry Andric 
83606c3fb27SDimitry Andric   const auto *SrcRD = SrcType->getAsCXXRecordDecl();
83706c3fb27SDimitry Andric   const auto *DestRD = DestType->getAsCXXRecordDecl();
83806c3fb27SDimitry Andric   assert(SrcRD && DestRD);
83906c3fb27SDimitry Andric 
84006c3fb27SDimitry Andric   if (SrcRD->isEffectivelyFinal()) {
84106c3fb27SDimitry Andric     assert(!SrcRD->isDerivedFrom(DestRD) &&
84206c3fb27SDimitry Andric            "upcasts should not use CK_Dynamic");
84306c3fb27SDimitry Andric     return true;
84406c3fb27SDimitry Andric   }
84506c3fb27SDimitry Andric 
84606c3fb27SDimitry Andric   if (DestRD->isEffectivelyFinal() && !DestRD->isDerivedFrom(SrcRD))
84706c3fb27SDimitry Andric     return true;
84806c3fb27SDimitry Andric 
8490b57cec5SDimitry Andric   return false;
8500b57cec5SDimitry Andric }
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric CXXReinterpretCastExpr *
Create(const ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,const CXXCastPath * BasePath,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc,SourceRange AngleBrackets)8530b57cec5SDimitry Andric CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
8540b57cec5SDimitry Andric                                ExprValueKind VK, CastKind K, Expr *Op,
8550b57cec5SDimitry Andric                                const CXXCastPath *BasePath,
8560b57cec5SDimitry Andric                                TypeSourceInfo *WrittenTy, SourceLocation L,
8570b57cec5SDimitry Andric                                SourceLocation RParenLoc,
8580b57cec5SDimitry Andric                                SourceRange AngleBrackets) {
8590b57cec5SDimitry Andric   unsigned PathSize = (BasePath ? BasePath->size() : 0);
8600b57cec5SDimitry Andric   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
8610b57cec5SDimitry Andric   auto *E =
8620b57cec5SDimitry Andric       new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
8630b57cec5SDimitry Andric                                           RParenLoc, AngleBrackets);
8640b57cec5SDimitry Andric   if (PathSize)
8650b57cec5SDimitry Andric     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
8660b57cec5SDimitry Andric                               E->getTrailingObjects<CXXBaseSpecifier *>());
8670b57cec5SDimitry Andric   return E;
8680b57cec5SDimitry Andric }
8690b57cec5SDimitry Andric 
8700b57cec5SDimitry Andric CXXReinterpretCastExpr *
CreateEmpty(const ASTContext & C,unsigned PathSize)8710b57cec5SDimitry Andric CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
8720b57cec5SDimitry Andric   void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
8730b57cec5SDimitry Andric   return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
8740b57cec5SDimitry Andric }
8750b57cec5SDimitry Andric 
Create(const ASTContext & C,QualType T,ExprValueKind VK,Expr * Op,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc,SourceRange AngleBrackets)8760b57cec5SDimitry Andric CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
8770b57cec5SDimitry Andric                                            ExprValueKind VK, Expr *Op,
8780b57cec5SDimitry Andric                                            TypeSourceInfo *WrittenTy,
8790b57cec5SDimitry Andric                                            SourceLocation L,
8800b57cec5SDimitry Andric                                            SourceLocation RParenLoc,
8810b57cec5SDimitry Andric                                            SourceRange AngleBrackets) {
8820b57cec5SDimitry Andric   return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
8830b57cec5SDimitry Andric }
8840b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & C)8850b57cec5SDimitry Andric CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
8860b57cec5SDimitry Andric   return new (C) CXXConstCastExpr(EmptyShell());
8870b57cec5SDimitry Andric }
8880b57cec5SDimitry Andric 
8895ffd83dbSDimitry Andric CXXAddrspaceCastExpr *
Create(const ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc,SourceRange AngleBrackets)8905ffd83dbSDimitry Andric CXXAddrspaceCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
8915ffd83dbSDimitry Andric                              CastKind K, Expr *Op, TypeSourceInfo *WrittenTy,
8925ffd83dbSDimitry Andric                              SourceLocation L, SourceLocation RParenLoc,
8935ffd83dbSDimitry Andric                              SourceRange AngleBrackets) {
8945ffd83dbSDimitry Andric   return new (C) CXXAddrspaceCastExpr(T, VK, K, Op, WrittenTy, L, RParenLoc,
8955ffd83dbSDimitry Andric                                       AngleBrackets);
8965ffd83dbSDimitry Andric }
8975ffd83dbSDimitry Andric 
CreateEmpty(const ASTContext & C)8985ffd83dbSDimitry Andric CXXAddrspaceCastExpr *CXXAddrspaceCastExpr::CreateEmpty(const ASTContext &C) {
8995ffd83dbSDimitry Andric   return new (C) CXXAddrspaceCastExpr(EmptyShell());
9005ffd83dbSDimitry Andric }
9015ffd83dbSDimitry Andric 
Create(const ASTContext & C,QualType T,ExprValueKind VK,TypeSourceInfo * Written,CastKind K,Expr * Op,const CXXCastPath * BasePath,FPOptionsOverride FPO,SourceLocation L,SourceLocation R)902e8d8bef9SDimitry Andric CXXFunctionalCastExpr *CXXFunctionalCastExpr::Create(
903e8d8bef9SDimitry Andric     const ASTContext &C, QualType T, ExprValueKind VK, TypeSourceInfo *Written,
904e8d8bef9SDimitry Andric     CastKind K, Expr *Op, const CXXCastPath *BasePath, FPOptionsOverride FPO,
9050b57cec5SDimitry Andric     SourceLocation L, SourceLocation R) {
9060b57cec5SDimitry Andric   unsigned PathSize = (BasePath ? BasePath->size() : 0);
907e8d8bef9SDimitry Andric   void *Buffer =
908e8d8bef9SDimitry Andric       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
909e8d8bef9SDimitry Andric           PathSize, FPO.requiresTrailingStorage()));
910e8d8bef9SDimitry Andric   auto *E = new (Buffer)
911e8d8bef9SDimitry Andric       CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, FPO, L, R);
9120b57cec5SDimitry Andric   if (PathSize)
9130b57cec5SDimitry Andric     std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
9140b57cec5SDimitry Andric                               E->getTrailingObjects<CXXBaseSpecifier *>());
9150b57cec5SDimitry Andric   return E;
9160b57cec5SDimitry Andric }
9170b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & C,unsigned PathSize,bool HasFPFeatures)918e8d8bef9SDimitry Andric CXXFunctionalCastExpr *CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C,
919e8d8bef9SDimitry Andric                                                           unsigned PathSize,
920e8d8bef9SDimitry Andric                                                           bool HasFPFeatures) {
921e8d8bef9SDimitry Andric   void *Buffer =
922e8d8bef9SDimitry Andric       C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>(
923e8d8bef9SDimitry Andric           PathSize, HasFPFeatures));
924e8d8bef9SDimitry Andric   return new (Buffer)
925e8d8bef9SDimitry Andric       CXXFunctionalCastExpr(EmptyShell(), PathSize, HasFPFeatures);
9260b57cec5SDimitry Andric }
9270b57cec5SDimitry Andric 
getBeginLoc() const9280b57cec5SDimitry Andric SourceLocation CXXFunctionalCastExpr::getBeginLoc() const {
9290b57cec5SDimitry Andric   return getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
9300b57cec5SDimitry Andric }
9310b57cec5SDimitry Andric 
getEndLoc() const9320b57cec5SDimitry Andric SourceLocation CXXFunctionalCastExpr::getEndLoc() const {
9330b57cec5SDimitry Andric   return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getEndLoc();
9340b57cec5SDimitry Andric }
9350b57cec5SDimitry Andric 
UserDefinedLiteral(Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation LitEndLoc,SourceLocation SuffixLoc,FPOptionsOverride FPFeatures)9360b57cec5SDimitry Andric UserDefinedLiteral::UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args,
9370b57cec5SDimitry Andric                                        QualType Ty, ExprValueKind VK,
9380b57cec5SDimitry Andric                                        SourceLocation LitEndLoc,
939e8d8bef9SDimitry Andric                                        SourceLocation SuffixLoc,
940e8d8bef9SDimitry Andric                                        FPOptionsOverride FPFeatures)
9410b57cec5SDimitry Andric     : CallExpr(UserDefinedLiteralClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
942e8d8bef9SDimitry Andric                LitEndLoc, FPFeatures, /*MinNumArgs=*/0, NotADL),
9430b57cec5SDimitry Andric       UDSuffixLoc(SuffixLoc) {}
9440b57cec5SDimitry Andric 
UserDefinedLiteral(unsigned NumArgs,bool HasFPFeatures,EmptyShell Empty)945e8d8bef9SDimitry Andric UserDefinedLiteral::UserDefinedLiteral(unsigned NumArgs, bool HasFPFeatures,
946e8d8bef9SDimitry Andric                                        EmptyShell Empty)
947e8d8bef9SDimitry Andric     : CallExpr(UserDefinedLiteralClass, /*NumPreArgs=*/0, NumArgs,
948e8d8bef9SDimitry Andric                HasFPFeatures, Empty) {}
9490b57cec5SDimitry Andric 
Create(const ASTContext & Ctx,Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation LitEndLoc,SourceLocation SuffixLoc,FPOptionsOverride FPFeatures)9500b57cec5SDimitry Andric UserDefinedLiteral *UserDefinedLiteral::Create(const ASTContext &Ctx, Expr *Fn,
9510b57cec5SDimitry Andric                                                ArrayRef<Expr *> Args,
9520b57cec5SDimitry Andric                                                QualType Ty, ExprValueKind VK,
9530b57cec5SDimitry Andric                                                SourceLocation LitEndLoc,
954e8d8bef9SDimitry Andric                                                SourceLocation SuffixLoc,
955e8d8bef9SDimitry Andric                                                FPOptionsOverride FPFeatures) {
9560b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
9570b57cec5SDimitry Andric   unsigned NumArgs = Args.size();
958e8d8bef9SDimitry Andric   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
959e8d8bef9SDimitry Andric       /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage());
9600b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
9610b57cec5SDimitry Andric                            alignof(UserDefinedLiteral));
962e8d8bef9SDimitry Andric   return new (Mem)
963e8d8bef9SDimitry Andric       UserDefinedLiteral(Fn, Args, Ty, VK, LitEndLoc, SuffixLoc, FPFeatures);
9640b57cec5SDimitry Andric }
9650b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs,bool HasFPOptions,EmptyShell Empty)9660b57cec5SDimitry Andric UserDefinedLiteral *UserDefinedLiteral::CreateEmpty(const ASTContext &Ctx,
9670b57cec5SDimitry Andric                                                     unsigned NumArgs,
968e8d8bef9SDimitry Andric                                                     bool HasFPOptions,
9690b57cec5SDimitry Andric                                                     EmptyShell Empty) {
9700b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
9710b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects =
972e8d8bef9SDimitry Andric       CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPOptions);
9730b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
9740b57cec5SDimitry Andric                            alignof(UserDefinedLiteral));
975e8d8bef9SDimitry Andric   return new (Mem) UserDefinedLiteral(NumArgs, HasFPOptions, Empty);
9760b57cec5SDimitry Andric }
9770b57cec5SDimitry Andric 
9780b57cec5SDimitry Andric UserDefinedLiteral::LiteralOperatorKind
getLiteralOperatorKind() const9790b57cec5SDimitry Andric UserDefinedLiteral::getLiteralOperatorKind() const {
9800b57cec5SDimitry Andric   if (getNumArgs() == 0)
9810b57cec5SDimitry Andric     return LOK_Template;
9820b57cec5SDimitry Andric   if (getNumArgs() == 2)
9830b57cec5SDimitry Andric     return LOK_String;
9840b57cec5SDimitry Andric 
9850b57cec5SDimitry Andric   assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
9860b57cec5SDimitry Andric   QualType ParamTy =
9870b57cec5SDimitry Andric     cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
9880b57cec5SDimitry Andric   if (ParamTy->isPointerType())
9890b57cec5SDimitry Andric     return LOK_Raw;
9900b57cec5SDimitry Andric   if (ParamTy->isAnyCharacterType())
9910b57cec5SDimitry Andric     return LOK_Character;
9920b57cec5SDimitry Andric   if (ParamTy->isIntegerType())
9930b57cec5SDimitry Andric     return LOK_Integer;
9940b57cec5SDimitry Andric   if (ParamTy->isFloatingType())
9950b57cec5SDimitry Andric     return LOK_Floating;
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric   llvm_unreachable("unknown kind of literal operator");
9980b57cec5SDimitry Andric }
9990b57cec5SDimitry Andric 
getCookedLiteral()10000b57cec5SDimitry Andric Expr *UserDefinedLiteral::getCookedLiteral() {
10010b57cec5SDimitry Andric #ifndef NDEBUG
10020b57cec5SDimitry Andric   LiteralOperatorKind LOK = getLiteralOperatorKind();
10030b57cec5SDimitry Andric   assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
10040b57cec5SDimitry Andric #endif
10050b57cec5SDimitry Andric   return getArg(0);
10060b57cec5SDimitry Andric }
10070b57cec5SDimitry Andric 
getUDSuffix() const10080b57cec5SDimitry Andric const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
10090b57cec5SDimitry Andric   return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
10100b57cec5SDimitry Andric }
10110b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & C,bool HasRewrittenInit)1012bdd1243dSDimitry Andric CXXDefaultArgExpr *CXXDefaultArgExpr::CreateEmpty(const ASTContext &C,
1013bdd1243dSDimitry Andric                                                   bool HasRewrittenInit) {
1014bdd1243dSDimitry Andric   size_t Size = totalSizeToAlloc<Expr *>(HasRewrittenInit);
1015bdd1243dSDimitry Andric   auto *Mem = C.Allocate(Size, alignof(CXXDefaultArgExpr));
1016bdd1243dSDimitry Andric   return new (Mem) CXXDefaultArgExpr(EmptyShell(), HasRewrittenInit);
1017bdd1243dSDimitry Andric }
1018bdd1243dSDimitry Andric 
Create(const ASTContext & C,SourceLocation Loc,ParmVarDecl * Param,Expr * RewrittenExpr,DeclContext * UsedContext)1019bdd1243dSDimitry Andric CXXDefaultArgExpr *CXXDefaultArgExpr::Create(const ASTContext &C,
1020bdd1243dSDimitry Andric                                              SourceLocation Loc,
1021bdd1243dSDimitry Andric                                              ParmVarDecl *Param,
1022bdd1243dSDimitry Andric                                              Expr *RewrittenExpr,
1023bdd1243dSDimitry Andric                                              DeclContext *UsedContext) {
1024bdd1243dSDimitry Andric   size_t Size = totalSizeToAlloc<Expr *>(RewrittenExpr != nullptr);
1025bdd1243dSDimitry Andric   auto *Mem = C.Allocate(Size, alignof(CXXDefaultArgExpr));
1026bdd1243dSDimitry Andric   return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
1027bdd1243dSDimitry Andric                                      RewrittenExpr, UsedContext);
1028bdd1243dSDimitry Andric }
1029bdd1243dSDimitry Andric 
getExpr()1030bdd1243dSDimitry Andric Expr *CXXDefaultArgExpr::getExpr() {
1031bdd1243dSDimitry Andric   return CXXDefaultArgExprBits.HasRewrittenInit ? getAdjustedRewrittenExpr()
1032bdd1243dSDimitry Andric                                                 : getParam()->getDefaultArg();
1033bdd1243dSDimitry Andric }
1034bdd1243dSDimitry Andric 
getAdjustedRewrittenExpr()1035bdd1243dSDimitry Andric Expr *CXXDefaultArgExpr::getAdjustedRewrittenExpr() {
1036bdd1243dSDimitry Andric   assert(hasRewrittenInit() &&
1037bdd1243dSDimitry Andric          "expected this CXXDefaultArgExpr to have a rewritten init.");
1038bdd1243dSDimitry Andric   Expr *Init = getRewrittenExpr();
1039bdd1243dSDimitry Andric   if (auto *E = dyn_cast_if_present<FullExpr>(Init))
1040bdd1243dSDimitry Andric     if (!isa<ConstantExpr>(E))
1041bdd1243dSDimitry Andric       return E->getSubExpr();
1042bdd1243dSDimitry Andric   return Init;
1043bdd1243dSDimitry Andric }
1044bdd1243dSDimitry Andric 
CXXDefaultInitExpr(const ASTContext & Ctx,SourceLocation Loc,FieldDecl * Field,QualType Ty,DeclContext * UsedContext,Expr * RewrittenInitExpr)10455ffd83dbSDimitry Andric CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &Ctx,
10465ffd83dbSDimitry Andric                                        SourceLocation Loc, FieldDecl *Field,
1047bdd1243dSDimitry Andric                                        QualType Ty, DeclContext *UsedContext,
1048bdd1243dSDimitry Andric                                        Expr *RewrittenInitExpr)
10490b57cec5SDimitry Andric     : Expr(CXXDefaultInitExprClass, Ty.getNonLValueExprType(Ctx),
1050fe6060f1SDimitry Andric            Ty->isLValueReferenceType()   ? VK_LValue
1051fe6060f1SDimitry Andric            : Ty->isRValueReferenceType() ? VK_XValue
1052fe6060f1SDimitry Andric                                          : VK_PRValue,
10535ffd83dbSDimitry Andric            /*FIXME*/ OK_Ordinary),
10540b57cec5SDimitry Andric       Field(Field), UsedContext(UsedContext) {
10550b57cec5SDimitry Andric   CXXDefaultInitExprBits.Loc = Loc;
1056bdd1243dSDimitry Andric   CXXDefaultInitExprBits.HasRewrittenInit = RewrittenInitExpr != nullptr;
1057bdd1243dSDimitry Andric 
1058bdd1243dSDimitry Andric   if (CXXDefaultInitExprBits.HasRewrittenInit)
1059bdd1243dSDimitry Andric     *getTrailingObjects<Expr *>() = RewrittenInitExpr;
1060bdd1243dSDimitry Andric 
10610b57cec5SDimitry Andric   assert(Field->hasInClassInitializer());
10625ffd83dbSDimitry Andric 
1063e8d8bef9SDimitry Andric   setDependence(computeDependence(this));
10640b57cec5SDimitry Andric }
10650b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & C,bool HasRewrittenInit)1066bdd1243dSDimitry Andric CXXDefaultInitExpr *CXXDefaultInitExpr::CreateEmpty(const ASTContext &C,
1067bdd1243dSDimitry Andric                                                     bool HasRewrittenInit) {
1068bdd1243dSDimitry Andric   size_t Size = totalSizeToAlloc<Expr *>(HasRewrittenInit);
1069bdd1243dSDimitry Andric   auto *Mem = C.Allocate(Size, alignof(CXXDefaultInitExpr));
1070bdd1243dSDimitry Andric   return new (Mem) CXXDefaultInitExpr(EmptyShell(), HasRewrittenInit);
1071bdd1243dSDimitry Andric }
1072bdd1243dSDimitry Andric 
Create(const ASTContext & Ctx,SourceLocation Loc,FieldDecl * Field,DeclContext * UsedContext,Expr * RewrittenInitExpr)1073bdd1243dSDimitry Andric CXXDefaultInitExpr *CXXDefaultInitExpr::Create(const ASTContext &Ctx,
1074bdd1243dSDimitry Andric                                                SourceLocation Loc,
1075bdd1243dSDimitry Andric                                                FieldDecl *Field,
1076bdd1243dSDimitry Andric                                                DeclContext *UsedContext,
1077bdd1243dSDimitry Andric                                                Expr *RewrittenInitExpr) {
1078bdd1243dSDimitry Andric 
1079bdd1243dSDimitry Andric   size_t Size = totalSizeToAlloc<Expr *>(RewrittenInitExpr != nullptr);
1080bdd1243dSDimitry Andric   auto *Mem = Ctx.Allocate(Size, alignof(CXXDefaultInitExpr));
1081bdd1243dSDimitry Andric   return new (Mem) CXXDefaultInitExpr(Ctx, Loc, Field, Field->getType(),
1082bdd1243dSDimitry Andric                                       UsedContext, RewrittenInitExpr);
1083bdd1243dSDimitry Andric }
1084bdd1243dSDimitry Andric 
getExpr()1085bdd1243dSDimitry Andric Expr *CXXDefaultInitExpr::getExpr() {
1086bdd1243dSDimitry Andric   assert(Field->getInClassInitializer() && "initializer hasn't been parsed");
1087bdd1243dSDimitry Andric   if (hasRewrittenInit())
1088bdd1243dSDimitry Andric     return getRewrittenExpr();
1089bdd1243dSDimitry Andric 
1090bdd1243dSDimitry Andric   return Field->getInClassInitializer();
1091bdd1243dSDimitry Andric }
1092bdd1243dSDimitry Andric 
Create(const ASTContext & C,const CXXDestructorDecl * Destructor)10930b57cec5SDimitry Andric CXXTemporary *CXXTemporary::Create(const ASTContext &C,
10940b57cec5SDimitry Andric                                    const CXXDestructorDecl *Destructor) {
10950b57cec5SDimitry Andric   return new (C) CXXTemporary(Destructor);
10960b57cec5SDimitry Andric }
10970b57cec5SDimitry Andric 
Create(const ASTContext & C,CXXTemporary * Temp,Expr * SubExpr)10980b57cec5SDimitry Andric CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
10990b57cec5SDimitry Andric                                                    CXXTemporary *Temp,
11000b57cec5SDimitry Andric                                                    Expr* SubExpr) {
11010b57cec5SDimitry Andric   assert((SubExpr->getType()->isRecordType() ||
11020b57cec5SDimitry Andric           SubExpr->getType()->isArrayType()) &&
11030b57cec5SDimitry Andric          "Expression bound to a temporary must have record or array type!");
11040b57cec5SDimitry Andric 
11050b57cec5SDimitry Andric   return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
11060b57cec5SDimitry Andric }
11070b57cec5SDimitry Andric 
CXXTemporaryObjectExpr(CXXConstructorDecl * Cons,QualType Ty,TypeSourceInfo * TSI,ArrayRef<Expr * > Args,SourceRange ParenOrBraceRange,bool HadMultipleCandidates,bool ListInitialization,bool StdInitListInitialization,bool ZeroInitialization)11080b57cec5SDimitry Andric CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(
11090b57cec5SDimitry Andric     CXXConstructorDecl *Cons, QualType Ty, TypeSourceInfo *TSI,
11100b57cec5SDimitry Andric     ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
11110b57cec5SDimitry Andric     bool HadMultipleCandidates, bool ListInitialization,
11120b57cec5SDimitry Andric     bool StdInitListInitialization, bool ZeroInitialization)
11130b57cec5SDimitry Andric     : CXXConstructExpr(
11140b57cec5SDimitry Andric           CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(),
11150b57cec5SDimitry Andric           Cons, /* Elidable=*/false, Args, HadMultipleCandidates,
11160b57cec5SDimitry Andric           ListInitialization, StdInitListInitialization, ZeroInitialization,
11175f757f3fSDimitry Andric           CXXConstructionKind::Complete, ParenOrBraceRange),
11183b7f365eSDimitry Andric       TSI(TSI) {
11193b7f365eSDimitry Andric   setDependence(computeDependence(this));
11203b7f365eSDimitry Andric }
11210b57cec5SDimitry Andric 
CXXTemporaryObjectExpr(EmptyShell Empty,unsigned NumArgs)11220b57cec5SDimitry Andric CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(EmptyShell Empty,
11230b57cec5SDimitry Andric                                                unsigned NumArgs)
11240b57cec5SDimitry Andric     : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty, NumArgs) {}
11250b57cec5SDimitry Andric 
Create(const ASTContext & Ctx,CXXConstructorDecl * Cons,QualType Ty,TypeSourceInfo * TSI,ArrayRef<Expr * > Args,SourceRange ParenOrBraceRange,bool HadMultipleCandidates,bool ListInitialization,bool StdInitListInitialization,bool ZeroInitialization)11260b57cec5SDimitry Andric CXXTemporaryObjectExpr *CXXTemporaryObjectExpr::Create(
11270b57cec5SDimitry Andric     const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
11280b57cec5SDimitry Andric     TypeSourceInfo *TSI, ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
11290b57cec5SDimitry Andric     bool HadMultipleCandidates, bool ListInitialization,
11300b57cec5SDimitry Andric     bool StdInitListInitialization, bool ZeroInitialization) {
11310b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
11320b57cec5SDimitry Andric   void *Mem =
11330b57cec5SDimitry Andric       Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
11340b57cec5SDimitry Andric                    alignof(CXXTemporaryObjectExpr));
11350b57cec5SDimitry Andric   return new (Mem) CXXTemporaryObjectExpr(
11360b57cec5SDimitry Andric       Cons, Ty, TSI, Args, ParenOrBraceRange, HadMultipleCandidates,
11370b57cec5SDimitry Andric       ListInitialization, StdInitListInitialization, ZeroInitialization);
11380b57cec5SDimitry Andric }
11390b57cec5SDimitry Andric 
11400b57cec5SDimitry Andric CXXTemporaryObjectExpr *
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs)11410b57cec5SDimitry Andric CXXTemporaryObjectExpr::CreateEmpty(const ASTContext &Ctx, unsigned NumArgs) {
11420b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
11430b57cec5SDimitry Andric   void *Mem =
11440b57cec5SDimitry Andric       Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
11450b57cec5SDimitry Andric                    alignof(CXXTemporaryObjectExpr));
11460b57cec5SDimitry Andric   return new (Mem) CXXTemporaryObjectExpr(EmptyShell(), NumArgs);
11470b57cec5SDimitry Andric }
11480b57cec5SDimitry Andric 
getBeginLoc() const11490b57cec5SDimitry Andric SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const {
11500b57cec5SDimitry Andric   return getTypeSourceInfo()->getTypeLoc().getBeginLoc();
11510b57cec5SDimitry Andric }
11520b57cec5SDimitry Andric 
getEndLoc() const11530b57cec5SDimitry Andric SourceLocation CXXTemporaryObjectExpr::getEndLoc() const {
11540b57cec5SDimitry Andric   SourceLocation Loc = getParenOrBraceRange().getEnd();
11550b57cec5SDimitry Andric   if (Loc.isInvalid() && getNumArgs())
11560b57cec5SDimitry Andric     Loc = getArg(getNumArgs() - 1)->getEndLoc();
11570b57cec5SDimitry Andric   return Loc;
11580b57cec5SDimitry Andric }
11590b57cec5SDimitry Andric 
Create(const ASTContext & Ctx,QualType Ty,SourceLocation Loc,CXXConstructorDecl * Ctor,bool Elidable,ArrayRef<Expr * > Args,bool HadMultipleCandidates,bool ListInitialization,bool StdInitListInitialization,bool ZeroInitialization,CXXConstructionKind ConstructKind,SourceRange ParenOrBraceRange)11600b57cec5SDimitry Andric CXXConstructExpr *CXXConstructExpr::Create(
11610b57cec5SDimitry Andric     const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
11620b57cec5SDimitry Andric     CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
11630b57cec5SDimitry Andric     bool HadMultipleCandidates, bool ListInitialization,
11640b57cec5SDimitry Andric     bool StdInitListInitialization, bool ZeroInitialization,
11655f757f3fSDimitry Andric     CXXConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
11660b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
11670b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
11680b57cec5SDimitry Andric                            alignof(CXXConstructExpr));
11690b57cec5SDimitry Andric   return new (Mem) CXXConstructExpr(
11700b57cec5SDimitry Andric       CXXConstructExprClass, Ty, Loc, Ctor, Elidable, Args,
11710b57cec5SDimitry Andric       HadMultipleCandidates, ListInitialization, StdInitListInitialization,
11720b57cec5SDimitry Andric       ZeroInitialization, ConstructKind, ParenOrBraceRange);
11730b57cec5SDimitry Andric }
11740b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs)11750b57cec5SDimitry Andric CXXConstructExpr *CXXConstructExpr::CreateEmpty(const ASTContext &Ctx,
11760b57cec5SDimitry Andric                                                 unsigned NumArgs) {
11770b57cec5SDimitry Andric   unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
11780b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
11790b57cec5SDimitry Andric                            alignof(CXXConstructExpr));
11800b57cec5SDimitry Andric   return new (Mem)
11810b57cec5SDimitry Andric       CXXConstructExpr(CXXConstructExprClass, EmptyShell(), NumArgs);
11820b57cec5SDimitry Andric }
11830b57cec5SDimitry Andric 
CXXConstructExpr(StmtClass SC,QualType Ty,SourceLocation Loc,CXXConstructorDecl * Ctor,bool Elidable,ArrayRef<Expr * > Args,bool HadMultipleCandidates,bool ListInitialization,bool StdInitListInitialization,bool ZeroInitialization,CXXConstructionKind ConstructKind,SourceRange ParenOrBraceRange)11840b57cec5SDimitry Andric CXXConstructExpr::CXXConstructExpr(
11850b57cec5SDimitry Andric     StmtClass SC, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor,
11860b57cec5SDimitry Andric     bool Elidable, ArrayRef<Expr *> Args, bool HadMultipleCandidates,
11870b57cec5SDimitry Andric     bool ListInitialization, bool StdInitListInitialization,
11885f757f3fSDimitry Andric     bool ZeroInitialization, CXXConstructionKind ConstructKind,
11890b57cec5SDimitry Andric     SourceRange ParenOrBraceRange)
1190fe6060f1SDimitry Andric     : Expr(SC, Ty, VK_PRValue, OK_Ordinary), Constructor(Ctor),
11915ffd83dbSDimitry Andric       ParenOrBraceRange(ParenOrBraceRange), NumArgs(Args.size()) {
11920b57cec5SDimitry Andric   CXXConstructExprBits.Elidable = Elidable;
11930b57cec5SDimitry Andric   CXXConstructExprBits.HadMultipleCandidates = HadMultipleCandidates;
11940b57cec5SDimitry Andric   CXXConstructExprBits.ListInitialization = ListInitialization;
11950b57cec5SDimitry Andric   CXXConstructExprBits.StdInitListInitialization = StdInitListInitialization;
11960b57cec5SDimitry Andric   CXXConstructExprBits.ZeroInitialization = ZeroInitialization;
11975f757f3fSDimitry Andric   CXXConstructExprBits.ConstructionKind = llvm::to_underlying(ConstructKind);
119806c3fb27SDimitry Andric   CXXConstructExprBits.IsImmediateEscalating = false;
11990b57cec5SDimitry Andric   CXXConstructExprBits.Loc = Loc;
12000b57cec5SDimitry Andric 
12010b57cec5SDimitry Andric   Stmt **TrailingArgs = getTrailingArgs();
12020b57cec5SDimitry Andric   for (unsigned I = 0, N = Args.size(); I != N; ++I) {
12030b57cec5SDimitry Andric     assert(Args[I] && "NULL argument in CXXConstructExpr!");
12040b57cec5SDimitry Andric     TrailingArgs[I] = Args[I];
12050b57cec5SDimitry Andric   }
12065ffd83dbSDimitry Andric 
12073b7f365eSDimitry Andric   // CXXTemporaryObjectExpr does this itself after setting its TypeSourceInfo.
12083b7f365eSDimitry Andric   if (SC == CXXConstructExprClass)
12095ffd83dbSDimitry Andric     setDependence(computeDependence(this));
12100b57cec5SDimitry Andric }
12110b57cec5SDimitry Andric 
CXXConstructExpr(StmtClass SC,EmptyShell Empty,unsigned NumArgs)12120b57cec5SDimitry Andric CXXConstructExpr::CXXConstructExpr(StmtClass SC, EmptyShell Empty,
12130b57cec5SDimitry Andric                                    unsigned NumArgs)
12140b57cec5SDimitry Andric     : Expr(SC, Empty), NumArgs(NumArgs) {}
12150b57cec5SDimitry Andric 
LambdaCapture(SourceLocation Loc,bool Implicit,LambdaCaptureKind Kind,ValueDecl * Var,SourceLocation EllipsisLoc)12160b57cec5SDimitry Andric LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
1217bdd1243dSDimitry Andric                              LambdaCaptureKind Kind, ValueDecl *Var,
12180b57cec5SDimitry Andric                              SourceLocation EllipsisLoc)
12190b57cec5SDimitry Andric     : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
12200b57cec5SDimitry Andric   unsigned Bits = 0;
12210b57cec5SDimitry Andric   if (Implicit)
12220b57cec5SDimitry Andric     Bits |= Capture_Implicit;
12230b57cec5SDimitry Andric 
12240b57cec5SDimitry Andric   switch (Kind) {
12250b57cec5SDimitry Andric   case LCK_StarThis:
12260b57cec5SDimitry Andric     Bits |= Capture_ByCopy;
1227bdd1243dSDimitry Andric     [[fallthrough]];
12280b57cec5SDimitry Andric   case LCK_This:
12290b57cec5SDimitry Andric     assert(!Var && "'this' capture cannot have a variable!");
12300b57cec5SDimitry Andric     Bits |= Capture_This;
12310b57cec5SDimitry Andric     break;
12320b57cec5SDimitry Andric 
12330b57cec5SDimitry Andric   case LCK_ByCopy:
12340b57cec5SDimitry Andric     Bits |= Capture_ByCopy;
1235bdd1243dSDimitry Andric     [[fallthrough]];
12360b57cec5SDimitry Andric   case LCK_ByRef:
12370b57cec5SDimitry Andric     assert(Var && "capture must have a variable!");
12380b57cec5SDimitry Andric     break;
12390b57cec5SDimitry Andric   case LCK_VLAType:
12400b57cec5SDimitry Andric     assert(!Var && "VLA type capture cannot have a variable!");
12410b57cec5SDimitry Andric     break;
12420b57cec5SDimitry Andric   }
12430b57cec5SDimitry Andric   DeclAndBits.setInt(Bits);
12440b57cec5SDimitry Andric }
12450b57cec5SDimitry Andric 
getCaptureKind() const12460b57cec5SDimitry Andric LambdaCaptureKind LambdaCapture::getCaptureKind() const {
12470b57cec5SDimitry Andric   if (capturesVLAType())
12480b57cec5SDimitry Andric     return LCK_VLAType;
12490b57cec5SDimitry Andric   bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
12500b57cec5SDimitry Andric   if (capturesThis())
12510b57cec5SDimitry Andric     return CapByCopy ? LCK_StarThis : LCK_This;
12520b57cec5SDimitry Andric   return CapByCopy ? LCK_ByCopy : LCK_ByRef;
12530b57cec5SDimitry Andric }
12540b57cec5SDimitry Andric 
LambdaExpr(QualType T,SourceRange IntroducerRange,LambdaCaptureDefault CaptureDefault,SourceLocation CaptureDefaultLoc,bool ExplicitParams,bool ExplicitResultType,ArrayRef<Expr * > CaptureInits,SourceLocation ClosingBrace,bool ContainsUnexpandedParameterPack)12550b57cec5SDimitry Andric LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
12560b57cec5SDimitry Andric                        LambdaCaptureDefault CaptureDefault,
12575ffd83dbSDimitry Andric                        SourceLocation CaptureDefaultLoc, bool ExplicitParams,
12580b57cec5SDimitry Andric                        bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
12590b57cec5SDimitry Andric                        SourceLocation ClosingBrace,
12600b57cec5SDimitry Andric                        bool ContainsUnexpandedParameterPack)
1261fe6060f1SDimitry Andric     : Expr(LambdaExprClass, T, VK_PRValue, OK_Ordinary),
12620b57cec5SDimitry Andric       IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
12630b57cec5SDimitry Andric       ClosingBrace(ClosingBrace) {
12645ffd83dbSDimitry Andric   LambdaExprBits.NumCaptures = CaptureInits.size();
12655ffd83dbSDimitry Andric   LambdaExprBits.CaptureDefault = CaptureDefault;
12665ffd83dbSDimitry Andric   LambdaExprBits.ExplicitParams = ExplicitParams;
12675ffd83dbSDimitry Andric   LambdaExprBits.ExplicitResultType = ExplicitResultType;
12685ffd83dbSDimitry Andric 
12690b57cec5SDimitry Andric   CXXRecordDecl *Class = getLambdaClass();
12705ffd83dbSDimitry Andric   (void)Class;
12715ffd83dbSDimitry Andric   assert(capture_size() == Class->capture_size() && "Wrong number of captures");
12725ffd83dbSDimitry Andric   assert(getCaptureDefault() == Class->getLambdaCaptureDefault());
12730b57cec5SDimitry Andric 
12740b57cec5SDimitry Andric   // Copy initialization expressions for the non-static data members.
12750b57cec5SDimitry Andric   Stmt **Stored = getStoredStmts();
12760b57cec5SDimitry Andric   for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
12770b57cec5SDimitry Andric     *Stored++ = CaptureInits[I];
12780b57cec5SDimitry Andric 
12790b57cec5SDimitry Andric   // Copy the body of the lambda.
12800b57cec5SDimitry Andric   *Stored++ = getCallOperator()->getBody();
12815ffd83dbSDimitry Andric 
12825ffd83dbSDimitry Andric   setDependence(computeDependence(this, ContainsUnexpandedParameterPack));
12830b57cec5SDimitry Andric }
12840b57cec5SDimitry Andric 
LambdaExpr(EmptyShell Empty,unsigned NumCaptures)12855ffd83dbSDimitry Andric LambdaExpr::LambdaExpr(EmptyShell Empty, unsigned NumCaptures)
12865ffd83dbSDimitry Andric     : Expr(LambdaExprClass, Empty) {
12875ffd83dbSDimitry Andric   LambdaExprBits.NumCaptures = NumCaptures;
12885ffd83dbSDimitry Andric 
12895ffd83dbSDimitry Andric   // Initially don't initialize the body of the LambdaExpr. The body will
12905ffd83dbSDimitry Andric   // be lazily deserialized when needed.
12915ffd83dbSDimitry Andric   getStoredStmts()[NumCaptures] = nullptr; // Not one past the end.
12925ffd83dbSDimitry Andric }
12935ffd83dbSDimitry Andric 
Create(const ASTContext & Context,CXXRecordDecl * Class,SourceRange IntroducerRange,LambdaCaptureDefault CaptureDefault,SourceLocation CaptureDefaultLoc,bool ExplicitParams,bool ExplicitResultType,ArrayRef<Expr * > CaptureInits,SourceLocation ClosingBrace,bool ContainsUnexpandedParameterPack)12945ffd83dbSDimitry Andric LambdaExpr *LambdaExpr::Create(const ASTContext &Context, CXXRecordDecl *Class,
12955ffd83dbSDimitry Andric                                SourceRange IntroducerRange,
12965ffd83dbSDimitry Andric                                LambdaCaptureDefault CaptureDefault,
12975ffd83dbSDimitry Andric                                SourceLocation CaptureDefaultLoc,
12985ffd83dbSDimitry Andric                                bool ExplicitParams, bool ExplicitResultType,
12995ffd83dbSDimitry Andric                                ArrayRef<Expr *> CaptureInits,
13005ffd83dbSDimitry Andric                                SourceLocation ClosingBrace,
13015ffd83dbSDimitry Andric                                bool ContainsUnexpandedParameterPack) {
13020b57cec5SDimitry Andric   // Determine the type of the expression (i.e., the type of the
13030b57cec5SDimitry Andric   // function object we're creating).
13040b57cec5SDimitry Andric   QualType T = Context.getTypeDeclType(Class);
13050b57cec5SDimitry Andric 
13065ffd83dbSDimitry Andric   unsigned Size = totalSizeToAlloc<Stmt *>(CaptureInits.size() + 1);
13070b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size);
13080b57cec5SDimitry Andric   return new (Mem)
13090b57cec5SDimitry Andric       LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
13105ffd83dbSDimitry Andric                  ExplicitParams, ExplicitResultType, CaptureInits, ClosingBrace,
13115ffd83dbSDimitry Andric                  ContainsUnexpandedParameterPack);
13120b57cec5SDimitry Andric }
13130b57cec5SDimitry Andric 
CreateDeserialized(const ASTContext & C,unsigned NumCaptures)13140b57cec5SDimitry Andric LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
13150b57cec5SDimitry Andric                                            unsigned NumCaptures) {
13160b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
13170b57cec5SDimitry Andric   void *Mem = C.Allocate(Size);
13180b57cec5SDimitry Andric   return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
13190b57cec5SDimitry Andric }
13200b57cec5SDimitry Andric 
initBodyIfNeeded() const13215ffd83dbSDimitry Andric void LambdaExpr::initBodyIfNeeded() const {
13225ffd83dbSDimitry Andric   if (!getStoredStmts()[capture_size()]) {
13235ffd83dbSDimitry Andric     auto *This = const_cast<LambdaExpr *>(this);
13245ffd83dbSDimitry Andric     This->getStoredStmts()[capture_size()] = getCallOperator()->getBody();
13255ffd83dbSDimitry Andric   }
13265ffd83dbSDimitry Andric }
13275ffd83dbSDimitry Andric 
getBody() const13285ffd83dbSDimitry Andric Stmt *LambdaExpr::getBody() const {
13295ffd83dbSDimitry Andric   initBodyIfNeeded();
13305ffd83dbSDimitry Andric   return getStoredStmts()[capture_size()];
13315ffd83dbSDimitry Andric }
13325ffd83dbSDimitry Andric 
getCompoundStmtBody() const13335ffd83dbSDimitry Andric const CompoundStmt *LambdaExpr::getCompoundStmtBody() const {
13345ffd83dbSDimitry Andric   Stmt *Body = getBody();
13355ffd83dbSDimitry Andric   if (const auto *CoroBody = dyn_cast<CoroutineBodyStmt>(Body))
13365ffd83dbSDimitry Andric     return cast<CompoundStmt>(CoroBody->getBody());
13375ffd83dbSDimitry Andric   return cast<CompoundStmt>(Body);
13385ffd83dbSDimitry Andric }
13395ffd83dbSDimitry Andric 
isInitCapture(const LambdaCapture * C) const13400b57cec5SDimitry Andric bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1341bdd1243dSDimitry Andric   return C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1342bdd1243dSDimitry Andric          getCallOperator() == C->getCapturedVar()->getDeclContext();
13430b57cec5SDimitry Andric }
13440b57cec5SDimitry Andric 
capture_begin() const13450b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
1346bdd1243dSDimitry Andric   return getLambdaClass()->captures_begin();
13470b57cec5SDimitry Andric }
13480b57cec5SDimitry Andric 
capture_end() const13490b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
1350bdd1243dSDimitry Andric   return getLambdaClass()->captures_end();
13510b57cec5SDimitry Andric }
13520b57cec5SDimitry Andric 
captures() const13530b57cec5SDimitry Andric LambdaExpr::capture_range LambdaExpr::captures() const {
13540b57cec5SDimitry Andric   return capture_range(capture_begin(), capture_end());
13550b57cec5SDimitry Andric }
13560b57cec5SDimitry Andric 
explicit_capture_begin() const13570b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
13580b57cec5SDimitry Andric   return capture_begin();
13590b57cec5SDimitry Andric }
13600b57cec5SDimitry Andric 
explicit_capture_end() const13610b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1362bdd1243dSDimitry Andric   return capture_begin() +
1363bdd1243dSDimitry Andric          getLambdaClass()->getLambdaData().NumExplicitCaptures;
13640b57cec5SDimitry Andric }
13650b57cec5SDimitry Andric 
explicit_captures() const13660b57cec5SDimitry Andric LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
13670b57cec5SDimitry Andric   return capture_range(explicit_capture_begin(), explicit_capture_end());
13680b57cec5SDimitry Andric }
13690b57cec5SDimitry Andric 
implicit_capture_begin() const13700b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
13710b57cec5SDimitry Andric   return explicit_capture_end();
13720b57cec5SDimitry Andric }
13730b57cec5SDimitry Andric 
implicit_capture_end() const13740b57cec5SDimitry Andric LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
13750b57cec5SDimitry Andric   return capture_end();
13760b57cec5SDimitry Andric }
13770b57cec5SDimitry Andric 
implicit_captures() const13780b57cec5SDimitry Andric LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
13790b57cec5SDimitry Andric   return capture_range(implicit_capture_begin(), implicit_capture_end());
13800b57cec5SDimitry Andric }
13810b57cec5SDimitry Andric 
getLambdaClass() const13820b57cec5SDimitry Andric CXXRecordDecl *LambdaExpr::getLambdaClass() const {
13830b57cec5SDimitry Andric   return getType()->getAsCXXRecordDecl();
13840b57cec5SDimitry Andric }
13850b57cec5SDimitry Andric 
getCallOperator() const13860b57cec5SDimitry Andric CXXMethodDecl *LambdaExpr::getCallOperator() const {
13870b57cec5SDimitry Andric   CXXRecordDecl *Record = getLambdaClass();
13880b57cec5SDimitry Andric   return Record->getLambdaCallOperator();
13890b57cec5SDimitry Andric }
13900b57cec5SDimitry Andric 
getDependentCallOperator() const1391a7dea167SDimitry Andric FunctionTemplateDecl *LambdaExpr::getDependentCallOperator() const {
1392a7dea167SDimitry Andric   CXXRecordDecl *Record = getLambdaClass();
1393a7dea167SDimitry Andric   return Record->getDependentLambdaCallOperator();
1394a7dea167SDimitry Andric }
1395a7dea167SDimitry Andric 
getTemplateParameterList() const13960b57cec5SDimitry Andric TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
13970b57cec5SDimitry Andric   CXXRecordDecl *Record = getLambdaClass();
13980b57cec5SDimitry Andric   return Record->getGenericLambdaTemplateParameterList();
13990b57cec5SDimitry Andric }
14000b57cec5SDimitry Andric 
getExplicitTemplateParameters() const14010b57cec5SDimitry Andric ArrayRef<NamedDecl *> LambdaExpr::getExplicitTemplateParameters() const {
14020b57cec5SDimitry Andric   const CXXRecordDecl *Record = getLambdaClass();
14030b57cec5SDimitry Andric   return Record->getLambdaExplicitTemplateParameters();
14040b57cec5SDimitry Andric }
14050b57cec5SDimitry Andric 
getTrailingRequiresClause() const1406e8d8bef9SDimitry Andric Expr *LambdaExpr::getTrailingRequiresClause() const {
1407e8d8bef9SDimitry Andric   return getCallOperator()->getTrailingRequiresClause();
1408e8d8bef9SDimitry Andric }
1409e8d8bef9SDimitry Andric 
isMutable() const14105ffd83dbSDimitry Andric bool LambdaExpr::isMutable() const { return !getCallOperator()->isConst(); }
14110b57cec5SDimitry Andric 
children()14125ffd83dbSDimitry Andric LambdaExpr::child_range LambdaExpr::children() {
14135ffd83dbSDimitry Andric   initBodyIfNeeded();
14145ffd83dbSDimitry Andric   return child_range(getStoredStmts(), getStoredStmts() + capture_size() + 1);
14150b57cec5SDimitry Andric }
14160b57cec5SDimitry Andric 
children() const14175ffd83dbSDimitry Andric LambdaExpr::const_child_range LambdaExpr::children() const {
14185ffd83dbSDimitry Andric   initBodyIfNeeded();
14195ffd83dbSDimitry Andric   return const_child_range(getStoredStmts(),
14205ffd83dbSDimitry Andric                            getStoredStmts() + capture_size() + 1);
14210b57cec5SDimitry Andric }
14220b57cec5SDimitry Andric 
ExprWithCleanups(Expr * subexpr,bool CleanupsHaveSideEffects,ArrayRef<CleanupObject> objects)14230b57cec5SDimitry Andric ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
14240b57cec5SDimitry Andric                                    bool CleanupsHaveSideEffects,
14250b57cec5SDimitry Andric                                    ArrayRef<CleanupObject> objects)
14260b57cec5SDimitry Andric     : FullExpr(ExprWithCleanupsClass, subexpr) {
14270b57cec5SDimitry Andric   ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
14280b57cec5SDimitry Andric   ExprWithCleanupsBits.NumObjects = objects.size();
14290b57cec5SDimitry Andric   for (unsigned i = 0, e = objects.size(); i != e; ++i)
14300b57cec5SDimitry Andric     getTrailingObjects<CleanupObject>()[i] = objects[i];
14310b57cec5SDimitry Andric }
14320b57cec5SDimitry Andric 
Create(const ASTContext & C,Expr * subexpr,bool CleanupsHaveSideEffects,ArrayRef<CleanupObject> objects)14330b57cec5SDimitry Andric ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
14340b57cec5SDimitry Andric                                            bool CleanupsHaveSideEffects,
14350b57cec5SDimitry Andric                                            ArrayRef<CleanupObject> objects) {
14360b57cec5SDimitry Andric   void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
14370b57cec5SDimitry Andric                             alignof(ExprWithCleanups));
14380b57cec5SDimitry Andric   return new (buffer)
14390b57cec5SDimitry Andric       ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
14400b57cec5SDimitry Andric }
14410b57cec5SDimitry Andric 
ExprWithCleanups(EmptyShell empty,unsigned numObjects)14420b57cec5SDimitry Andric ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
14430b57cec5SDimitry Andric     : FullExpr(ExprWithCleanupsClass, empty) {
14440b57cec5SDimitry Andric   ExprWithCleanupsBits.NumObjects = numObjects;
14450b57cec5SDimitry Andric }
14460b57cec5SDimitry Andric 
Create(const ASTContext & C,EmptyShell empty,unsigned numObjects)14470b57cec5SDimitry Andric ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
14480b57cec5SDimitry Andric                                            EmptyShell empty,
14490b57cec5SDimitry Andric                                            unsigned numObjects) {
14500b57cec5SDimitry Andric   void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
14510b57cec5SDimitry Andric                             alignof(ExprWithCleanups));
14520b57cec5SDimitry Andric   return new (buffer) ExprWithCleanups(empty, numObjects);
14530b57cec5SDimitry Andric }
14540b57cec5SDimitry Andric 
CXXUnresolvedConstructExpr(QualType T,TypeSourceInfo * TSI,SourceLocation LParenLoc,ArrayRef<Expr * > Args,SourceLocation RParenLoc,bool IsListInit)145506c3fb27SDimitry Andric CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(
145606c3fb27SDimitry Andric     QualType T, TypeSourceInfo *TSI, SourceLocation LParenLoc,
145706c3fb27SDimitry Andric     ArrayRef<Expr *> Args, SourceLocation RParenLoc, bool IsListInit)
1458e8d8bef9SDimitry Andric     : Expr(CXXUnresolvedConstructExprClass, T,
1459fe6060f1SDimitry Andric            (TSI->getType()->isLValueReferenceType()   ? VK_LValue
14600b57cec5SDimitry Andric             : TSI->getType()->isRValueReferenceType() ? VK_XValue
1461fe6060f1SDimitry Andric                                                       : VK_PRValue),
14625ffd83dbSDimitry Andric            OK_Ordinary),
146306c3fb27SDimitry Andric       TypeAndInitForm(TSI, IsListInit), LParenLoc(LParenLoc),
146406c3fb27SDimitry Andric       RParenLoc(RParenLoc) {
14650b57cec5SDimitry Andric   CXXUnresolvedConstructExprBits.NumArgs = Args.size();
14660b57cec5SDimitry Andric   auto **StoredArgs = getTrailingObjects<Expr *>();
14675ffd83dbSDimitry Andric   for (unsigned I = 0; I != Args.size(); ++I)
14680b57cec5SDimitry Andric     StoredArgs[I] = Args[I];
14695ffd83dbSDimitry Andric   setDependence(computeDependence(this));
14700b57cec5SDimitry Andric }
14710b57cec5SDimitry Andric 
Create(const ASTContext & Context,QualType T,TypeSourceInfo * TSI,SourceLocation LParenLoc,ArrayRef<Expr * > Args,SourceLocation RParenLoc,bool IsListInit)14720b57cec5SDimitry Andric CXXUnresolvedConstructExpr *CXXUnresolvedConstructExpr::Create(
147306c3fb27SDimitry Andric     const ASTContext &Context, QualType T, TypeSourceInfo *TSI,
147406c3fb27SDimitry Andric     SourceLocation LParenLoc, ArrayRef<Expr *> Args, SourceLocation RParenLoc,
147506c3fb27SDimitry Andric     bool IsListInit) {
14760b57cec5SDimitry Andric   void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
147706c3fb27SDimitry Andric   return new (Mem) CXXUnresolvedConstructExpr(T, TSI, LParenLoc, Args,
147806c3fb27SDimitry Andric                                               RParenLoc, IsListInit);
14790b57cec5SDimitry Andric }
14800b57cec5SDimitry Andric 
14810b57cec5SDimitry Andric CXXUnresolvedConstructExpr *
CreateEmpty(const ASTContext & Context,unsigned NumArgs)14820b57cec5SDimitry Andric CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &Context,
14830b57cec5SDimitry Andric                                         unsigned NumArgs) {
14840b57cec5SDimitry Andric   void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
14850b57cec5SDimitry Andric   return new (Mem) CXXUnresolvedConstructExpr(EmptyShell(), NumArgs);
14860b57cec5SDimitry Andric }
14870b57cec5SDimitry Andric 
getBeginLoc() const14880b57cec5SDimitry Andric SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const {
148906c3fb27SDimitry Andric   return TypeAndInitForm.getPointer()->getTypeLoc().getBeginLoc();
14900b57cec5SDimitry Andric }
14910b57cec5SDimitry Andric 
CXXDependentScopeMemberExpr(const ASTContext & Ctx,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,NamedDecl * FirstQualifierFoundInScope,DeclarationNameInfo MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs)14920b57cec5SDimitry Andric CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
14930b57cec5SDimitry Andric     const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
14940b57cec5SDimitry Andric     SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
14950b57cec5SDimitry Andric     SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
14960b57cec5SDimitry Andric     DeclarationNameInfo MemberNameInfo,
14970b57cec5SDimitry Andric     const TemplateArgumentListInfo *TemplateArgs)
14980b57cec5SDimitry Andric     : Expr(CXXDependentScopeMemberExprClass, Ctx.DependentTy, VK_LValue,
14995ffd83dbSDimitry Andric            OK_Ordinary),
15000b57cec5SDimitry Andric       Base(Base), BaseType(BaseType), QualifierLoc(QualifierLoc),
15010b57cec5SDimitry Andric       MemberNameInfo(MemberNameInfo) {
15020b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.IsArrow = IsArrow;
15030b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
15040b57cec5SDimitry Andric       (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
15050b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
15060b57cec5SDimitry Andric       FirstQualifierFoundInScope != nullptr;
15070b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.OperatorLoc = OperatorLoc;
15080b57cec5SDimitry Andric 
15090b57cec5SDimitry Andric   if (TemplateArgs) {
15105ffd83dbSDimitry Andric     auto Deps = TemplateArgumentDependence::None;
15110b57cec5SDimitry Andric     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
15120b57cec5SDimitry Andric         TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
15135ffd83dbSDimitry Andric         Deps);
15140b57cec5SDimitry Andric   } else if (TemplateKWLoc.isValid()) {
15150b57cec5SDimitry Andric     getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
15160b57cec5SDimitry Andric         TemplateKWLoc);
15170b57cec5SDimitry Andric   }
15180b57cec5SDimitry Andric 
15190b57cec5SDimitry Andric   if (hasFirstQualifierFoundInScope())
15200b57cec5SDimitry Andric     *getTrailingObjects<NamedDecl *>() = FirstQualifierFoundInScope;
15215ffd83dbSDimitry Andric   setDependence(computeDependence(this));
15220b57cec5SDimitry Andric }
15230b57cec5SDimitry Andric 
CXXDependentScopeMemberExpr(EmptyShell Empty,bool HasTemplateKWAndArgsInfo,bool HasFirstQualifierFoundInScope)15240b57cec5SDimitry Andric CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
15250b57cec5SDimitry Andric     EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
15260b57cec5SDimitry Andric     bool HasFirstQualifierFoundInScope)
15270b57cec5SDimitry Andric     : Expr(CXXDependentScopeMemberExprClass, Empty) {
15280b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
15290b57cec5SDimitry Andric       HasTemplateKWAndArgsInfo;
15300b57cec5SDimitry Andric   CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
15310b57cec5SDimitry Andric       HasFirstQualifierFoundInScope;
15320b57cec5SDimitry Andric }
15330b57cec5SDimitry Andric 
Create(const ASTContext & Ctx,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,NamedDecl * FirstQualifierFoundInScope,DeclarationNameInfo MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs)15340b57cec5SDimitry Andric CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::Create(
15350b57cec5SDimitry Andric     const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
15360b57cec5SDimitry Andric     SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
15370b57cec5SDimitry Andric     SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
15380b57cec5SDimitry Andric     DeclarationNameInfo MemberNameInfo,
15390b57cec5SDimitry Andric     const TemplateArgumentListInfo *TemplateArgs) {
15400b57cec5SDimitry Andric   bool HasTemplateKWAndArgsInfo =
15410b57cec5SDimitry Andric       (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
15420b57cec5SDimitry Andric   unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
15430b57cec5SDimitry Andric   bool HasFirstQualifierFoundInScope = FirstQualifierFoundInScope != nullptr;
15440b57cec5SDimitry Andric 
15450b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
15460b57cec5SDimitry Andric                                    TemplateArgumentLoc, NamedDecl *>(
15470b57cec5SDimitry Andric       HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
15480b57cec5SDimitry Andric 
15490b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
15500b57cec5SDimitry Andric   return new (Mem) CXXDependentScopeMemberExpr(
15510b57cec5SDimitry Andric       Ctx, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
15520b57cec5SDimitry Andric       FirstQualifierFoundInScope, MemberNameInfo, TemplateArgs);
15530b57cec5SDimitry Andric }
15540b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & Ctx,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs,bool HasFirstQualifierFoundInScope)15550b57cec5SDimitry Andric CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::CreateEmpty(
15560b57cec5SDimitry Andric     const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
15570b57cec5SDimitry Andric     unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope) {
15580b57cec5SDimitry Andric   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
15590b57cec5SDimitry Andric 
15600b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
15610b57cec5SDimitry Andric                                    TemplateArgumentLoc, NamedDecl *>(
15620b57cec5SDimitry Andric       HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
15630b57cec5SDimitry Andric 
15640b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
15650b57cec5SDimitry Andric   return new (Mem) CXXDependentScopeMemberExpr(
15660b57cec5SDimitry Andric       EmptyShell(), HasTemplateKWAndArgsInfo, HasFirstQualifierFoundInScope);
15670b57cec5SDimitry Andric }
15680b57cec5SDimitry Andric 
Create(const ASTContext & Ctx,SourceLocation L,QualType Ty,bool IsImplicit)15695f757f3fSDimitry Andric CXXThisExpr *CXXThisExpr::Create(const ASTContext &Ctx, SourceLocation L,
15705f757f3fSDimitry Andric                                  QualType Ty, bool IsImplicit) {
15715f757f3fSDimitry Andric   return new (Ctx) CXXThisExpr(L, Ty, IsImplicit,
15725f757f3fSDimitry Andric                                Ctx.getLangOpts().HLSL ? VK_LValue : VK_PRValue);
15735f757f3fSDimitry Andric }
15745f757f3fSDimitry Andric 
CreateEmpty(const ASTContext & Ctx)15755f757f3fSDimitry Andric CXXThisExpr *CXXThisExpr::CreateEmpty(const ASTContext &Ctx) {
15765f757f3fSDimitry Andric   return new (Ctx) CXXThisExpr(EmptyShell());
15775f757f3fSDimitry Andric }
15785f757f3fSDimitry Andric 
hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,UnresolvedSetIterator end)15790b57cec5SDimitry Andric static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
15800b57cec5SDimitry Andric                                             UnresolvedSetIterator end) {
15810b57cec5SDimitry Andric   do {
15820b57cec5SDimitry Andric     NamedDecl *decl = *begin;
15830b57cec5SDimitry Andric     if (isa<UnresolvedUsingValueDecl>(decl))
15840b57cec5SDimitry Andric       return false;
15850b57cec5SDimitry Andric 
15860b57cec5SDimitry Andric     // Unresolved member expressions should only contain methods and
15870b57cec5SDimitry Andric     // method templates.
15880b57cec5SDimitry Andric     if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
15890b57cec5SDimitry Andric             ->isStatic())
15900b57cec5SDimitry Andric       return false;
15910b57cec5SDimitry Andric   } while (++begin != end);
15920b57cec5SDimitry Andric 
15930b57cec5SDimitry Andric   return true;
15940b57cec5SDimitry Andric }
15950b57cec5SDimitry Andric 
UnresolvedMemberExpr(const ASTContext & Context,bool HasUnresolvedUsing,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs,UnresolvedSetIterator Begin,UnresolvedSetIterator End)15960b57cec5SDimitry Andric UnresolvedMemberExpr::UnresolvedMemberExpr(
15970b57cec5SDimitry Andric     const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
15980b57cec5SDimitry Andric     QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
15990b57cec5SDimitry Andric     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
16000b57cec5SDimitry Andric     const DeclarationNameInfo &MemberNameInfo,
16010b57cec5SDimitry Andric     const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
16020b57cec5SDimitry Andric     UnresolvedSetIterator End)
16030b57cec5SDimitry Andric     : OverloadExpr(
16040b57cec5SDimitry Andric           UnresolvedMemberExprClass, Context, QualifierLoc, TemplateKWLoc,
16050b57cec5SDimitry Andric           MemberNameInfo, TemplateArgs, Begin, End,
16060b57cec5SDimitry Andric           // Dependent
16070b57cec5SDimitry Andric           ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
16080b57cec5SDimitry Andric           ((Base && Base->isInstantiationDependent()) ||
16090b57cec5SDimitry Andric            BaseType->isInstantiationDependentType()),
16100b57cec5SDimitry Andric           // Contains unexpanded parameter pack
16110b57cec5SDimitry Andric           ((Base && Base->containsUnexpandedParameterPack()) ||
16120b57cec5SDimitry Andric            BaseType->containsUnexpandedParameterPack())),
16130b57cec5SDimitry Andric       Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
16140b57cec5SDimitry Andric   UnresolvedMemberExprBits.IsArrow = IsArrow;
16150b57cec5SDimitry Andric   UnresolvedMemberExprBits.HasUnresolvedUsing = HasUnresolvedUsing;
16160b57cec5SDimitry Andric 
16170b57cec5SDimitry Andric   // Check whether all of the members are non-static member functions,
16180b57cec5SDimitry Andric   // and if so, mark give this bound-member type instead of overload type.
16190b57cec5SDimitry Andric   if (hasOnlyNonStaticMemberFunctions(Begin, End))
16200b57cec5SDimitry Andric     setType(Context.BoundMemberTy);
16210b57cec5SDimitry Andric }
16220b57cec5SDimitry Andric 
UnresolvedMemberExpr(EmptyShell Empty,unsigned NumResults,bool HasTemplateKWAndArgsInfo)16230b57cec5SDimitry Andric UnresolvedMemberExpr::UnresolvedMemberExpr(EmptyShell Empty,
16240b57cec5SDimitry Andric                                            unsigned NumResults,
16250b57cec5SDimitry Andric                                            bool HasTemplateKWAndArgsInfo)
16260b57cec5SDimitry Andric     : OverloadExpr(UnresolvedMemberExprClass, Empty, NumResults,
16270b57cec5SDimitry Andric                    HasTemplateKWAndArgsInfo) {}
16280b57cec5SDimitry Andric 
isImplicitAccess() const16290b57cec5SDimitry Andric bool UnresolvedMemberExpr::isImplicitAccess() const {
16300b57cec5SDimitry Andric   if (!Base)
16310b57cec5SDimitry Andric     return true;
16320b57cec5SDimitry Andric 
16330b57cec5SDimitry Andric   return cast<Expr>(Base)->isImplicitCXXThis();
16340b57cec5SDimitry Andric }
16350b57cec5SDimitry Andric 
Create(const ASTContext & Context,bool HasUnresolvedUsing,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs,UnresolvedSetIterator Begin,UnresolvedSetIterator End)16360b57cec5SDimitry Andric UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
16370b57cec5SDimitry Andric     const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
16380b57cec5SDimitry Andric     QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
16390b57cec5SDimitry Andric     NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
16400b57cec5SDimitry Andric     const DeclarationNameInfo &MemberNameInfo,
16410b57cec5SDimitry Andric     const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
16420b57cec5SDimitry Andric     UnresolvedSetIterator End) {
16430b57cec5SDimitry Andric   unsigned NumResults = End - Begin;
16440b57cec5SDimitry Andric   bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
16450b57cec5SDimitry Andric   unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
16460b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
16470b57cec5SDimitry Andric                                    TemplateArgumentLoc>(
16480b57cec5SDimitry Andric       NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
16490b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
16500b57cec5SDimitry Andric   return new (Mem) UnresolvedMemberExpr(
16510b57cec5SDimitry Andric       Context, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc,
16520b57cec5SDimitry Andric       QualifierLoc, TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
16530b57cec5SDimitry Andric }
16540b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & Context,unsigned NumResults,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs)16550b57cec5SDimitry Andric UnresolvedMemberExpr *UnresolvedMemberExpr::CreateEmpty(
16560b57cec5SDimitry Andric     const ASTContext &Context, unsigned NumResults,
16570b57cec5SDimitry Andric     bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
16580b57cec5SDimitry Andric   assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
16590b57cec5SDimitry Andric   unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
16600b57cec5SDimitry Andric                                    TemplateArgumentLoc>(
16610b57cec5SDimitry Andric       NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
16620b57cec5SDimitry Andric   void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
16630b57cec5SDimitry Andric   return new (Mem)
16640b57cec5SDimitry Andric       UnresolvedMemberExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
16650b57cec5SDimitry Andric }
16660b57cec5SDimitry Andric 
getNamingClass()16670b57cec5SDimitry Andric CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() {
16680b57cec5SDimitry Andric   // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
16690b57cec5SDimitry Andric 
16700b57cec5SDimitry Andric   // If there was a nested name specifier, it names the naming class.
16710b57cec5SDimitry Andric   // It can't be dependent: after all, we were actually able to do the
16720b57cec5SDimitry Andric   // lookup.
16730b57cec5SDimitry Andric   CXXRecordDecl *Record = nullptr;
16740b57cec5SDimitry Andric   auto *NNS = getQualifier();
16750b57cec5SDimitry Andric   if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
16760b57cec5SDimitry Andric     const Type *T = getQualifier()->getAsType();
16770b57cec5SDimitry Andric     assert(T && "qualifier in member expression does not name type");
16780b57cec5SDimitry Andric     Record = T->getAsCXXRecordDecl();
16790b57cec5SDimitry Andric     assert(Record && "qualifier in member expression does not name record");
16800b57cec5SDimitry Andric   }
16810b57cec5SDimitry Andric   // Otherwise the naming class must have been the base class.
16820b57cec5SDimitry Andric   else {
16830b57cec5SDimitry Andric     QualType BaseType = getBaseType().getNonReferenceType();
1684a7dea167SDimitry Andric     if (isArrow())
1685a7dea167SDimitry Andric       BaseType = BaseType->castAs<PointerType>()->getPointeeType();
16860b57cec5SDimitry Andric 
16870b57cec5SDimitry Andric     Record = BaseType->getAsCXXRecordDecl();
16880b57cec5SDimitry Andric     assert(Record && "base of member expression does not name record");
16890b57cec5SDimitry Andric   }
16900b57cec5SDimitry Andric 
16910b57cec5SDimitry Andric   return Record;
16920b57cec5SDimitry Andric }
16930b57cec5SDimitry Andric 
Create(ASTContext & Context,SourceLocation OperatorLoc,NamedDecl * Pack,SourceLocation PackLoc,SourceLocation RParenLoc,std::optional<unsigned> Length,ArrayRef<TemplateArgument> PartialArgs)1694bdd1243dSDimitry Andric SizeOfPackExpr *SizeOfPackExpr::Create(ASTContext &Context,
1695bdd1243dSDimitry Andric                                        SourceLocation OperatorLoc,
16960b57cec5SDimitry Andric                                        NamedDecl *Pack, SourceLocation PackLoc,
16970b57cec5SDimitry Andric                                        SourceLocation RParenLoc,
1698bdd1243dSDimitry Andric                                        std::optional<unsigned> Length,
16990b57cec5SDimitry Andric                                        ArrayRef<TemplateArgument> PartialArgs) {
17000b57cec5SDimitry Andric   void *Storage =
17010b57cec5SDimitry Andric       Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
17020b57cec5SDimitry Andric   return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
17030b57cec5SDimitry Andric                                       PackLoc, RParenLoc, Length, PartialArgs);
17040b57cec5SDimitry Andric }
17050b57cec5SDimitry Andric 
CreateDeserialized(ASTContext & Context,unsigned NumPartialArgs)17060b57cec5SDimitry Andric SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
17070b57cec5SDimitry Andric                                                    unsigned NumPartialArgs) {
17080b57cec5SDimitry Andric   void *Storage =
17090b57cec5SDimitry Andric       Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
17100b57cec5SDimitry Andric   return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
17110b57cec5SDimitry Andric }
17120b57cec5SDimitry Andric 
getParameter() const1713bdd1243dSDimitry Andric NonTypeTemplateParmDecl *SubstNonTypeTemplateParmExpr::getParameter() const {
1714bdd1243dSDimitry Andric   return cast<NonTypeTemplateParmDecl>(
1715bdd1243dSDimitry Andric       getReplacedTemplateParameterList(getAssociatedDecl())->asArray()[Index]);
1716bdd1243dSDimitry Andric }
1717bdd1243dSDimitry Andric 
Create(ASTContext & Context,SourceLocation EllipsisLoc,SourceLocation RSquareLoc,Expr * PackIdExpr,Expr * IndexExpr,std::optional<int64_t> Index,ArrayRef<Expr * > SubstitutedExprs,bool ExpandedToEmptyPack)17180fca6ea1SDimitry Andric PackIndexingExpr *PackIndexingExpr::Create(
17190fca6ea1SDimitry Andric     ASTContext &Context, SourceLocation EllipsisLoc, SourceLocation RSquareLoc,
17200fca6ea1SDimitry Andric     Expr *PackIdExpr, Expr *IndexExpr, std::optional<int64_t> Index,
17210fca6ea1SDimitry Andric     ArrayRef<Expr *> SubstitutedExprs, bool ExpandedToEmptyPack) {
17220fca6ea1SDimitry Andric   QualType Type;
17230fca6ea1SDimitry Andric   if (Index && !SubstitutedExprs.empty())
17240fca6ea1SDimitry Andric     Type = SubstitutedExprs[*Index]->getType();
17250fca6ea1SDimitry Andric   else
17260fca6ea1SDimitry Andric     Type = Context.DependentTy;
17270fca6ea1SDimitry Andric 
17280fca6ea1SDimitry Andric   void *Storage =
17290fca6ea1SDimitry Andric       Context.Allocate(totalSizeToAlloc<Expr *>(SubstitutedExprs.size()));
17300fca6ea1SDimitry Andric   return new (Storage)
17310fca6ea1SDimitry Andric       PackIndexingExpr(Type, EllipsisLoc, RSquareLoc, PackIdExpr, IndexExpr,
17320fca6ea1SDimitry Andric                        SubstitutedExprs, ExpandedToEmptyPack);
17330fca6ea1SDimitry Andric }
17340fca6ea1SDimitry Andric 
getPackDecl() const17350fca6ea1SDimitry Andric NamedDecl *PackIndexingExpr::getPackDecl() const {
17360fca6ea1SDimitry Andric   if (auto *D = dyn_cast<DeclRefExpr>(getPackIdExpression()); D) {
17370fca6ea1SDimitry Andric     NamedDecl *ND = dyn_cast<NamedDecl>(D->getDecl());
17380fca6ea1SDimitry Andric     assert(ND && "exected a named decl");
17390fca6ea1SDimitry Andric     return ND;
17400fca6ea1SDimitry Andric   }
17410fca6ea1SDimitry Andric   assert(false && "invalid declaration kind in pack indexing expression");
17420fca6ea1SDimitry Andric   return nullptr;
17430fca6ea1SDimitry Andric }
17440fca6ea1SDimitry Andric 
17450fca6ea1SDimitry Andric PackIndexingExpr *
CreateDeserialized(ASTContext & Context,unsigned NumTransformedExprs)17460fca6ea1SDimitry Andric PackIndexingExpr::CreateDeserialized(ASTContext &Context,
17470fca6ea1SDimitry Andric                                      unsigned NumTransformedExprs) {
17480fca6ea1SDimitry Andric   void *Storage =
17490fca6ea1SDimitry Andric       Context.Allocate(totalSizeToAlloc<Expr *>(NumTransformedExprs));
17500fca6ea1SDimitry Andric   return new (Storage) PackIndexingExpr(EmptyShell{});
17510fca6ea1SDimitry Andric }
17520fca6ea1SDimitry Andric 
getParameterType(const ASTContext & Context) const1753e8d8bef9SDimitry Andric QualType SubstNonTypeTemplateParmExpr::getParameterType(
1754e8d8bef9SDimitry Andric     const ASTContext &Context) const {
1755e8d8bef9SDimitry Andric   // Note that, for a class type NTTP, we will have an lvalue of type 'const
1756e8d8bef9SDimitry Andric   // T', so we can't just compute this from the type and value category.
1757e8d8bef9SDimitry Andric   if (isReferenceParameter())
1758e8d8bef9SDimitry Andric     return Context.getLValueReferenceType(getType());
1759e8d8bef9SDimitry Andric   return getType().getUnqualifiedType();
1760e8d8bef9SDimitry Andric }
1761e8d8bef9SDimitry Andric 
SubstNonTypeTemplateParmPackExpr(QualType T,ExprValueKind ValueKind,SourceLocation NameLoc,const TemplateArgument & ArgPack,Decl * AssociatedDecl,unsigned Index)17625ffd83dbSDimitry Andric SubstNonTypeTemplateParmPackExpr::SubstNonTypeTemplateParmPackExpr(
1763bdd1243dSDimitry Andric     QualType T, ExprValueKind ValueKind, SourceLocation NameLoc,
1764bdd1243dSDimitry Andric     const TemplateArgument &ArgPack, Decl *AssociatedDecl, unsigned Index)
17655ffd83dbSDimitry Andric     : Expr(SubstNonTypeTemplateParmPackExprClass, T, ValueKind, OK_Ordinary),
1766bdd1243dSDimitry Andric       AssociatedDecl(AssociatedDecl), Arguments(ArgPack.pack_begin()),
1767bdd1243dSDimitry Andric       NumArguments(ArgPack.pack_size()), Index(Index), NameLoc(NameLoc) {
1768bdd1243dSDimitry Andric   assert(AssociatedDecl != nullptr);
17695ffd83dbSDimitry Andric   setDependence(ExprDependence::TypeValueInstantiation |
17705ffd83dbSDimitry Andric                 ExprDependence::UnexpandedPack);
17715ffd83dbSDimitry Andric }
17720b57cec5SDimitry Andric 
1773bdd1243dSDimitry Andric NonTypeTemplateParmDecl *
getParameterPack() const1774bdd1243dSDimitry Andric SubstNonTypeTemplateParmPackExpr::getParameterPack() const {
1775bdd1243dSDimitry Andric   return cast<NonTypeTemplateParmDecl>(
1776bdd1243dSDimitry Andric       getReplacedTemplateParameterList(getAssociatedDecl())->asArray()[Index]);
1777bdd1243dSDimitry Andric }
1778bdd1243dSDimitry Andric 
getArgumentPack() const17790b57cec5SDimitry Andric TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1780bdd1243dSDimitry Andric   return TemplateArgument(llvm::ArrayRef(Arguments, NumArguments));
17810b57cec5SDimitry Andric }
17820b57cec5SDimitry Andric 
FunctionParmPackExpr(QualType T,VarDecl * ParamPack,SourceLocation NameLoc,unsigned NumParams,VarDecl * const * Params)17830b57cec5SDimitry Andric FunctionParmPackExpr::FunctionParmPackExpr(QualType T, VarDecl *ParamPack,
17840b57cec5SDimitry Andric                                            SourceLocation NameLoc,
17850b57cec5SDimitry Andric                                            unsigned NumParams,
17860b57cec5SDimitry Andric                                            VarDecl *const *Params)
17875ffd83dbSDimitry Andric     : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary),
17880b57cec5SDimitry Andric       ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
17890b57cec5SDimitry Andric   if (Params)
17900b57cec5SDimitry Andric     std::uninitialized_copy(Params, Params + NumParams,
17910b57cec5SDimitry Andric                             getTrailingObjects<VarDecl *>());
17925ffd83dbSDimitry Andric   setDependence(ExprDependence::TypeValueInstantiation |
17935ffd83dbSDimitry Andric                 ExprDependence::UnexpandedPack);
17940b57cec5SDimitry Andric }
17950b57cec5SDimitry Andric 
17960b57cec5SDimitry Andric FunctionParmPackExpr *
Create(const ASTContext & Context,QualType T,VarDecl * ParamPack,SourceLocation NameLoc,ArrayRef<VarDecl * > Params)17970b57cec5SDimitry Andric FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
17980b57cec5SDimitry Andric                              VarDecl *ParamPack, SourceLocation NameLoc,
17990b57cec5SDimitry Andric                              ArrayRef<VarDecl *> Params) {
18000b57cec5SDimitry Andric   return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(Params.size())))
18010b57cec5SDimitry Andric       FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
18020b57cec5SDimitry Andric }
18030b57cec5SDimitry Andric 
18040b57cec5SDimitry Andric FunctionParmPackExpr *
CreateEmpty(const ASTContext & Context,unsigned NumParams)18050b57cec5SDimitry Andric FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
18060b57cec5SDimitry Andric                                   unsigned NumParams) {
18070b57cec5SDimitry Andric   return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(NumParams)))
18080b57cec5SDimitry Andric       FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
18090b57cec5SDimitry Andric }
18100b57cec5SDimitry Andric 
MaterializeTemporaryExpr(QualType T,Expr * Temporary,bool BoundToLvalueReference,LifetimeExtendedTemporaryDecl * MTD)1811480093f4SDimitry Andric MaterializeTemporaryExpr::MaterializeTemporaryExpr(
1812480093f4SDimitry Andric     QualType T, Expr *Temporary, bool BoundToLvalueReference,
1813480093f4SDimitry Andric     LifetimeExtendedTemporaryDecl *MTD)
1814480093f4SDimitry Andric     : Expr(MaterializeTemporaryExprClass, T,
18155ffd83dbSDimitry Andric            BoundToLvalueReference ? VK_LValue : VK_XValue, OK_Ordinary) {
1816480093f4SDimitry Andric   if (MTD) {
1817480093f4SDimitry Andric     State = MTD;
1818480093f4SDimitry Andric     MTD->ExprWithTemporary = Temporary;
1819480093f4SDimitry Andric     return;
1820480093f4SDimitry Andric   }
1821480093f4SDimitry Andric   State = Temporary;
18225ffd83dbSDimitry Andric   setDependence(computeDependence(this));
1823480093f4SDimitry Andric }
1824480093f4SDimitry Andric 
setExtendingDecl(ValueDecl * ExtendedBy,unsigned ManglingNumber)1825480093f4SDimitry Andric void MaterializeTemporaryExpr::setExtendingDecl(ValueDecl *ExtendedBy,
18260b57cec5SDimitry Andric                                                 unsigned ManglingNumber) {
18270b57cec5SDimitry Andric   // We only need extra state if we have to remember more than just the Stmt.
18280b57cec5SDimitry Andric   if (!ExtendedBy)
18290b57cec5SDimitry Andric     return;
18300b57cec5SDimitry Andric 
18310b57cec5SDimitry Andric   // We may need to allocate extra storage for the mangling number and the
18320b57cec5SDimitry Andric   // extended-by ValueDecl.
1833480093f4SDimitry Andric   if (!State.is<LifetimeExtendedTemporaryDecl *>())
1834480093f4SDimitry Andric     State = LifetimeExtendedTemporaryDecl::Create(
1835480093f4SDimitry Andric         cast<Expr>(State.get<Stmt *>()), ExtendedBy, ManglingNumber);
18360b57cec5SDimitry Andric 
1837480093f4SDimitry Andric   auto ES = State.get<LifetimeExtendedTemporaryDecl *>();
18380b57cec5SDimitry Andric   ES->ExtendingDecl = ExtendedBy;
18390b57cec5SDimitry Andric   ES->ManglingNumber = ManglingNumber;
18400b57cec5SDimitry Andric }
18410b57cec5SDimitry Andric 
isUsableInConstantExpressions(const ASTContext & Context) const1842e8d8bef9SDimitry Andric bool MaterializeTemporaryExpr::isUsableInConstantExpressions(
1843e8d8bef9SDimitry Andric     const ASTContext &Context) const {
1844e8d8bef9SDimitry Andric   // C++20 [expr.const]p4:
1845e8d8bef9SDimitry Andric   //   An object or reference is usable in constant expressions if it is [...]
1846e8d8bef9SDimitry Andric   //   a temporary object of non-volatile const-qualified literal type
1847e8d8bef9SDimitry Andric   //   whose lifetime is extended to that of a variable that is usable
1848e8d8bef9SDimitry Andric   //   in constant expressions
1849e8d8bef9SDimitry Andric   auto *VD = dyn_cast_or_null<VarDecl>(getExtendingDecl());
1850e8d8bef9SDimitry Andric   return VD && getType().isConstant(Context) &&
1851e8d8bef9SDimitry Andric          !getType().isVolatileQualified() &&
1852e8d8bef9SDimitry Andric          getType()->isLiteralType(Context) &&
1853e8d8bef9SDimitry Andric          VD->isUsableInConstantExpressions(Context);
1854e8d8bef9SDimitry Andric }
1855e8d8bef9SDimitry Andric 
TypeTraitExpr(QualType T,SourceLocation Loc,TypeTrait Kind,ArrayRef<TypeSourceInfo * > Args,SourceLocation RParenLoc,bool Value)18560b57cec5SDimitry Andric TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
18570b57cec5SDimitry Andric                              ArrayRef<TypeSourceInfo *> Args,
18585ffd83dbSDimitry Andric                              SourceLocation RParenLoc, bool Value)
1859fe6060f1SDimitry Andric     : Expr(TypeTraitExprClass, T, VK_PRValue, OK_Ordinary), Loc(Loc),
18605ffd83dbSDimitry Andric       RParenLoc(RParenLoc) {
18615ffd83dbSDimitry Andric   assert(Kind <= TT_Last && "invalid enum value!");
18620b57cec5SDimitry Andric   TypeTraitExprBits.Kind = Kind;
18635ffd83dbSDimitry Andric   assert(static_cast<unsigned>(Kind) == TypeTraitExprBits.Kind &&
18645ffd83dbSDimitry Andric          "TypeTraitExprBits.Kind overflow!");
18650b57cec5SDimitry Andric   TypeTraitExprBits.Value = Value;
18660b57cec5SDimitry Andric   TypeTraitExprBits.NumArgs = Args.size();
18675ffd83dbSDimitry Andric   assert(Args.size() == TypeTraitExprBits.NumArgs &&
18685ffd83dbSDimitry Andric          "TypeTraitExprBits.NumArgs overflow!");
18690b57cec5SDimitry Andric 
18700b57cec5SDimitry Andric   auto **ToArgs = getTrailingObjects<TypeSourceInfo *>();
18715ffd83dbSDimitry Andric   for (unsigned I = 0, N = Args.size(); I != N; ++I)
18720b57cec5SDimitry Andric     ToArgs[I] = Args[I];
18735ffd83dbSDimitry Andric 
18745ffd83dbSDimitry Andric   setDependence(computeDependence(this));
18750b57cec5SDimitry Andric }
18760b57cec5SDimitry Andric 
Create(const ASTContext & C,QualType T,SourceLocation Loc,TypeTrait Kind,ArrayRef<TypeSourceInfo * > Args,SourceLocation RParenLoc,bool Value)18770b57cec5SDimitry Andric TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
18780b57cec5SDimitry Andric                                      SourceLocation Loc,
18790b57cec5SDimitry Andric                                      TypeTrait Kind,
18800b57cec5SDimitry Andric                                      ArrayRef<TypeSourceInfo *> Args,
18810b57cec5SDimitry Andric                                      SourceLocation RParenLoc,
18820b57cec5SDimitry Andric                                      bool Value) {
18830b57cec5SDimitry Andric   void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
18840b57cec5SDimitry Andric   return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
18850b57cec5SDimitry Andric }
18860b57cec5SDimitry Andric 
CreateDeserialized(const ASTContext & C,unsigned NumArgs)18870b57cec5SDimitry Andric TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
18880b57cec5SDimitry Andric                                                  unsigned NumArgs) {
18890b57cec5SDimitry Andric   void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
18900b57cec5SDimitry Andric   return new (Mem) TypeTraitExpr(EmptyShell());
18910b57cec5SDimitry Andric }
18920b57cec5SDimitry Andric 
CUDAKernelCallExpr(Expr * Fn,CallExpr * Config,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation RP,FPOptionsOverride FPFeatures,unsigned MinNumArgs)18930b57cec5SDimitry Andric CUDAKernelCallExpr::CUDAKernelCallExpr(Expr *Fn, CallExpr *Config,
18940b57cec5SDimitry Andric                                        ArrayRef<Expr *> Args, QualType Ty,
18950b57cec5SDimitry Andric                                        ExprValueKind VK, SourceLocation RP,
1896e8d8bef9SDimitry Andric                                        FPOptionsOverride FPFeatures,
18970b57cec5SDimitry Andric                                        unsigned MinNumArgs)
18980b57cec5SDimitry Andric     : CallExpr(CUDAKernelCallExprClass, Fn, /*PreArgs=*/Config, Args, Ty, VK,
1899e8d8bef9SDimitry Andric                RP, FPFeatures, MinNumArgs, NotADL) {}
19000b57cec5SDimitry Andric 
CUDAKernelCallExpr(unsigned NumArgs,bool HasFPFeatures,EmptyShell Empty)1901e8d8bef9SDimitry Andric CUDAKernelCallExpr::CUDAKernelCallExpr(unsigned NumArgs, bool HasFPFeatures,
1902e8d8bef9SDimitry Andric                                        EmptyShell Empty)
19030b57cec5SDimitry Andric     : CallExpr(CUDAKernelCallExprClass, /*NumPreArgs=*/END_PREARG, NumArgs,
1904e8d8bef9SDimitry Andric                HasFPFeatures, Empty) {}
19050b57cec5SDimitry Andric 
19060b57cec5SDimitry Andric CUDAKernelCallExpr *
Create(const ASTContext & Ctx,Expr * Fn,CallExpr * Config,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation RP,FPOptionsOverride FPFeatures,unsigned MinNumArgs)19070b57cec5SDimitry Andric CUDAKernelCallExpr::Create(const ASTContext &Ctx, Expr *Fn, CallExpr *Config,
19080b57cec5SDimitry Andric                            ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
1909e8d8bef9SDimitry Andric                            SourceLocation RP, FPOptionsOverride FPFeatures,
1910e8d8bef9SDimitry Andric                            unsigned MinNumArgs) {
19110b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
19120b57cec5SDimitry Andric   unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
1913e8d8bef9SDimitry Andric   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
1914e8d8bef9SDimitry Andric       /*NumPreArgs=*/END_PREARG, NumArgs, FPFeatures.requiresTrailingStorage());
19150b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
19160b57cec5SDimitry Andric                            alignof(CUDAKernelCallExpr));
1917e8d8bef9SDimitry Andric   return new (Mem)
1918e8d8bef9SDimitry Andric       CUDAKernelCallExpr(Fn, Config, Args, Ty, VK, RP, FPFeatures, MinNumArgs);
19190b57cec5SDimitry Andric }
19200b57cec5SDimitry Andric 
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs,bool HasFPFeatures,EmptyShell Empty)19210b57cec5SDimitry Andric CUDAKernelCallExpr *CUDAKernelCallExpr::CreateEmpty(const ASTContext &Ctx,
19220b57cec5SDimitry Andric                                                     unsigned NumArgs,
1923e8d8bef9SDimitry Andric                                                     bool HasFPFeatures,
19240b57cec5SDimitry Andric                                                     EmptyShell Empty) {
19250b57cec5SDimitry Andric   // Allocate storage for the trailing objects of CallExpr.
1926e8d8bef9SDimitry Andric   unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects(
1927e8d8bef9SDimitry Andric       /*NumPreArgs=*/END_PREARG, NumArgs, HasFPFeatures);
19280b57cec5SDimitry Andric   void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
19290b57cec5SDimitry Andric                            alignof(CUDAKernelCallExpr));
1930e8d8bef9SDimitry Andric   return new (Mem) CUDAKernelCallExpr(NumArgs, HasFPFeatures, Empty);
19310b57cec5SDimitry Andric }
1932bdd1243dSDimitry Andric 
1933bdd1243dSDimitry Andric CXXParenListInitExpr *
Create(ASTContext & C,ArrayRef<Expr * > Args,QualType T,unsigned NumUserSpecifiedExprs,SourceLocation InitLoc,SourceLocation LParenLoc,SourceLocation RParenLoc)1934bdd1243dSDimitry Andric CXXParenListInitExpr::Create(ASTContext &C, ArrayRef<Expr *> Args, QualType T,
1935bdd1243dSDimitry Andric                              unsigned NumUserSpecifiedExprs,
1936bdd1243dSDimitry Andric                              SourceLocation InitLoc, SourceLocation LParenLoc,
1937bdd1243dSDimitry Andric                              SourceLocation RParenLoc) {
1938bdd1243dSDimitry Andric   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
1939bdd1243dSDimitry Andric   return new (Mem) CXXParenListInitExpr(Args, T, NumUserSpecifiedExprs, InitLoc,
1940bdd1243dSDimitry Andric                                         LParenLoc, RParenLoc);
1941bdd1243dSDimitry Andric }
1942bdd1243dSDimitry Andric 
CreateEmpty(ASTContext & C,unsigned NumExprs,EmptyShell Empty)1943bdd1243dSDimitry Andric CXXParenListInitExpr *CXXParenListInitExpr::CreateEmpty(ASTContext &C,
1944bdd1243dSDimitry Andric                                                         unsigned NumExprs,
1945bdd1243dSDimitry Andric                                                         EmptyShell Empty) {
1946bdd1243dSDimitry Andric   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumExprs),
1947bdd1243dSDimitry Andric                          alignof(CXXParenListInitExpr));
1948bdd1243dSDimitry Andric   return new (Mem) CXXParenListInitExpr(Empty, NumExprs);
1949bdd1243dSDimitry Andric }
1950