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