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