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