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