1 //===- ExprCXX.cpp - (C++) Expression AST Node Implementation -------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the subclesses of Expr class declared in ExprCXX.h
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ExprCXX.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclAccessPair.h"
18 #include "clang/AST/DeclBase.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/DeclarationName.h"
21 #include "clang/AST/Expr.h"
22 #include "clang/AST/LambdaCapture.h"
23 #include "clang/AST/NestedNameSpecifier.h"
24 #include "clang/AST/TemplateBase.h"
25 #include "clang/AST/Type.h"
26 #include "clang/AST/TypeLoc.h"
27 #include "clang/Basic/LLVM.h"
28 #include "clang/Basic/OperatorKinds.h"
29 #include "clang/Basic/SourceLocation.h"
30 #include "clang/Basic/Specifiers.h"
31 #include "llvm/ADT/ArrayRef.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/ErrorHandling.h"
39 using namespace clang;
41 //===----------------------------------------------------------------------===//
42 // Child Iterators for iterating over subexpressions/substatements
43 //===----------------------------------------------------------------------===//
45 bool CXXOperatorCallExpr::isInfixBinaryOp() const {
46 // An infix binary operator is any operator with two arguments other than
47 // operator() and operator[]. Note that none of these operators can have
48 // default arguments, so it suffices to check the number of argument
50 if (getNumArgs() != 2)
53 switch (getOperator()) {
54 case OO_Call: case OO_Subscript:
61 CXXRewrittenBinaryOperator::DecomposedForm
62 CXXRewrittenBinaryOperator::getDecomposedForm() const {
63 DecomposedForm Result = {};
64 const Expr *E = getSemanticForm()->IgnoreImplicit();
66 // Remove an outer '!' if it exists (only happens for a '!=' rewrite).
67 bool SkippedNot = false;
68 if (auto *NotEq = dyn_cast<UnaryOperator>(E)) {
69 assert(NotEq->getOpcode() == UO_LNot);
70 E = NotEq->getSubExpr()->IgnoreImplicit();
74 // Decompose the outer binary operator.
75 if (auto *BO = dyn_cast<BinaryOperator>(E)) {
76 assert(!SkippedNot || BO->getOpcode() == BO_EQ);
77 Result.Opcode = SkippedNot ? BO_NE : BO->getOpcode();
78 Result.LHS = BO->getLHS();
79 Result.RHS = BO->getRHS();
80 Result.InnerBinOp = BO;
81 } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
82 assert(!SkippedNot || BO->getOperator() == OO_EqualEqual);
83 assert(BO->isInfixBinaryOp());
84 switch (BO->getOperator()) {
85 case OO_Less: Result.Opcode = BO_LT; break;
86 case OO_LessEqual: Result.Opcode = BO_LE; break;
87 case OO_Greater: Result.Opcode = BO_GT; break;
88 case OO_GreaterEqual: Result.Opcode = BO_GE; break;
89 case OO_Spaceship: Result.Opcode = BO_Cmp; break;
90 case OO_EqualEqual: Result.Opcode = SkippedNot ? BO_NE : BO_EQ; break;
91 default: llvm_unreachable("unexpected binop in rewritten operator expr");
93 Result.LHS = BO->getArg(0);
94 Result.RHS = BO->getArg(1);
95 Result.InnerBinOp = BO;
97 llvm_unreachable("unexpected rewritten operator form");
100 // Put the operands in the right order for == and !=, and canonicalize the
101 // <=> subexpression onto the LHS for all other forms.
103 std::swap(Result.LHS, Result.RHS);
105 // If this isn't a spaceship rewrite, we're done.
106 if (Result.Opcode == BO_EQ || Result.Opcode == BO_NE)
109 // Otherwise, we expect a <=> to now be on the LHS.
110 E = Result.LHS->IgnoreImplicit();
111 if (auto *BO = dyn_cast<BinaryOperator>(E)) {
112 assert(BO->getOpcode() == BO_Cmp);
113 Result.LHS = BO->getLHS();
114 Result.RHS = BO->getRHS();
115 Result.InnerBinOp = BO;
116 } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
117 assert(BO->getOperator() == OO_Spaceship);
118 Result.LHS = BO->getArg(0);
119 Result.RHS = BO->getArg(1);
120 Result.InnerBinOp = BO;
122 llvm_unreachable("unexpected rewritten operator form");
125 // Put the comparison operands in the right order.
127 std::swap(Result.LHS, Result.RHS);
131 bool CXXTypeidExpr::isPotentiallyEvaluated() const {
135 // C++11 [expr.typeid]p3:
136 // When typeid is applied to an expression other than a glvalue of
137 // polymorphic class type, [...] the expression is an unevaluated operand.
138 const Expr *E = getExprOperand();
139 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
140 if (RD->isPolymorphic() && E->isGLValue())
146 QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
147 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
149 return Context.getUnqualifiedArrayType(
150 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
153 QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
154 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
156 return Context.getUnqualifiedArrayType(
157 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
160 // CXXScalarValueInitExpr
161 SourceLocation CXXScalarValueInitExpr::getBeginLoc() const {
162 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : getRParenLoc();
166 CXXNewExpr::CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
167 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
168 bool UsualArrayDeleteWantsSize,
169 ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
170 Optional<Expr *> ArraySize,
171 InitializationStyle InitializationStyle,
172 Expr *Initializer, QualType Ty,
173 TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
174 SourceRange DirectInitRange)
175 : Expr(CXXNewExprClass, Ty, VK_RValue, OK_Ordinary, Ty->isDependentType(),
176 Ty->isDependentType(), Ty->isInstantiationDependentType(),
177 Ty->containsUnexpandedParameterPack()),
178 OperatorNew(OperatorNew), OperatorDelete(OperatorDelete),
179 AllocatedTypeInfo(AllocatedTypeInfo), Range(Range),
180 DirectInitRange(DirectInitRange) {
182 assert((Initializer != nullptr || InitializationStyle == NoInit) &&
183 "Only NoInit can have no initializer!");
185 CXXNewExprBits.IsGlobalNew = IsGlobalNew;
186 CXXNewExprBits.IsArray = ArraySize.hasValue();
187 CXXNewExprBits.ShouldPassAlignment = ShouldPassAlignment;
188 CXXNewExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
189 CXXNewExprBits.StoredInitializationStyle =
190 Initializer ? InitializationStyle + 1 : 0;
191 bool IsParenTypeId = TypeIdParens.isValid();
192 CXXNewExprBits.IsParenTypeId = IsParenTypeId;
193 CXXNewExprBits.NumPlacementArgs = PlacementArgs.size();
196 if (Expr *SizeExpr = *ArraySize) {
197 if (SizeExpr->isValueDependent())
198 ExprBits.ValueDependent = true;
199 if (SizeExpr->isInstantiationDependent())
200 ExprBits.InstantiationDependent = true;
201 if (SizeExpr->containsUnexpandedParameterPack())
202 ExprBits.ContainsUnexpandedParameterPack = true;
205 getTrailingObjects<Stmt *>()[arraySizeOffset()] = *ArraySize;
209 if (Initializer->isValueDependent())
210 ExprBits.ValueDependent = true;
211 if (Initializer->isInstantiationDependent())
212 ExprBits.InstantiationDependent = true;
213 if (Initializer->containsUnexpandedParameterPack())
214 ExprBits.ContainsUnexpandedParameterPack = true;
216 getTrailingObjects<Stmt *>()[initExprOffset()] = Initializer;
219 for (unsigned I = 0; I != PlacementArgs.size(); ++I) {
220 if (PlacementArgs[I]->isValueDependent())
221 ExprBits.ValueDependent = true;
222 if (PlacementArgs[I]->isInstantiationDependent())
223 ExprBits.InstantiationDependent = true;
224 if (PlacementArgs[I]->containsUnexpandedParameterPack())
225 ExprBits.ContainsUnexpandedParameterPack = true;
227 getTrailingObjects<Stmt *>()[placementNewArgsOffset() + I] =
232 getTrailingObjects<SourceRange>()[0] = TypeIdParens;
234 switch (getInitializationStyle()) {
236 this->Range.setEnd(DirectInitRange.getEnd());
239 this->Range.setEnd(getInitializer()->getSourceRange().getEnd());
243 this->Range.setEnd(TypeIdParens.getEnd());
248 CXXNewExpr::CXXNewExpr(EmptyShell Empty, bool IsArray,
249 unsigned NumPlacementArgs, bool IsParenTypeId)
250 : Expr(CXXNewExprClass, Empty) {
251 CXXNewExprBits.IsArray = IsArray;
252 CXXNewExprBits.NumPlacementArgs = NumPlacementArgs;
253 CXXNewExprBits.IsParenTypeId = IsParenTypeId;
257 CXXNewExpr::Create(const ASTContext &Ctx, bool IsGlobalNew,
258 FunctionDecl *OperatorNew, FunctionDecl *OperatorDelete,
259 bool ShouldPassAlignment, bool UsualArrayDeleteWantsSize,
260 ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
261 Optional<Expr *> ArraySize,
262 InitializationStyle InitializationStyle, Expr *Initializer,
263 QualType Ty, TypeSourceInfo *AllocatedTypeInfo,
264 SourceRange Range, SourceRange DirectInitRange) {
265 bool IsArray = ArraySize.hasValue();
266 bool HasInit = Initializer != nullptr;
267 unsigned NumPlacementArgs = PlacementArgs.size();
268 bool IsParenTypeId = TypeIdParens.isValid();
270 Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
271 IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
272 alignof(CXXNewExpr));
274 CXXNewExpr(IsGlobalNew, OperatorNew, OperatorDelete, ShouldPassAlignment,
275 UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens,
276 ArraySize, InitializationStyle, Initializer, Ty,
277 AllocatedTypeInfo, Range, DirectInitRange);
280 CXXNewExpr *CXXNewExpr::CreateEmpty(const ASTContext &Ctx, bool IsArray,
281 bool HasInit, unsigned NumPlacementArgs,
282 bool IsParenTypeId) {
284 Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
285 IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
286 alignof(CXXNewExpr));
288 CXXNewExpr(EmptyShell(), IsArray, NumPlacementArgs, IsParenTypeId);
291 bool CXXNewExpr::shouldNullCheckAllocation() const {
292 return getOperatorNew()
294 ->castAs<FunctionProtoType>()
296 !getOperatorNew()->isReservedGlobalPlacementOperator();
300 QualType CXXDeleteExpr::getDestroyedType() const {
301 const Expr *Arg = getArgument();
303 // For a destroying operator delete, we may have implicitly converted the
304 // pointer type to the type of the parameter of the 'operator delete'
306 while (const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
307 if (ICE->getCastKind() == CK_DerivedToBase ||
308 ICE->getCastKind() == CK_UncheckedDerivedToBase ||
309 ICE->getCastKind() == CK_NoOp) {
310 assert((ICE->getCastKind() == CK_NoOp ||
311 getOperatorDelete()->isDestroyingOperatorDelete()) &&
312 "only a destroying operator delete can have a converted arg");
313 Arg = ICE->getSubExpr();
318 // The type-to-delete may not be a pointer if it's a dependent type.
319 const QualType ArgType = Arg->getType();
321 if (ArgType->isDependentType() && !ArgType->isPointerType())
324 return ArgType->castAs<PointerType>()->getPointeeType();
327 // CXXPseudoDestructorExpr
328 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
330 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
333 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
334 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
335 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
336 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
337 PseudoDestructorTypeStorage DestroyedType)
338 : Expr(CXXPseudoDestructorExprClass,
339 Context.BoundMemberTy,
340 VK_RValue, OK_Ordinary,
341 /*isTypeDependent=*/(Base->isTypeDependent() ||
342 (DestroyedType.getTypeSourceInfo() &&
343 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
344 /*isValueDependent=*/Base->isValueDependent(),
345 (Base->isInstantiationDependent() ||
347 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
349 ScopeType->getType()->isInstantiationDependentType()) ||
350 (DestroyedType.getTypeSourceInfo() &&
351 DestroyedType.getTypeSourceInfo()->getType()
352 ->isInstantiationDependentType())),
353 // ContainsUnexpandedParameterPack
354 (Base->containsUnexpandedParameterPack() ||
356 QualifierLoc.getNestedNameSpecifier()
357 ->containsUnexpandedParameterPack()) ||
359 ScopeType->getType()->containsUnexpandedParameterPack()) ||
360 (DestroyedType.getTypeSourceInfo() &&
361 DestroyedType.getTypeSourceInfo()->getType()
362 ->containsUnexpandedParameterPack()))),
363 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
364 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
365 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
366 DestroyedType(DestroyedType) {}
368 QualType CXXPseudoDestructorExpr::getDestroyedType() const {
369 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
370 return TInfo->getType();
375 SourceLocation CXXPseudoDestructorExpr::getEndLoc() const {
376 SourceLocation End = DestroyedType.getLocation();
377 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
378 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
382 // UnresolvedLookupExpr
383 UnresolvedLookupExpr::UnresolvedLookupExpr(
384 const ASTContext &Context, CXXRecordDecl *NamingClass,
385 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
386 const DeclarationNameInfo &NameInfo, bool RequiresADL, bool Overloaded,
387 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
388 UnresolvedSetIterator End)
389 : OverloadExpr(UnresolvedLookupExprClass, Context, QualifierLoc,
390 TemplateKWLoc, NameInfo, TemplateArgs, Begin, End, false,
392 NamingClass(NamingClass) {
393 UnresolvedLookupExprBits.RequiresADL = RequiresADL;
394 UnresolvedLookupExprBits.Overloaded = Overloaded;
397 UnresolvedLookupExpr::UnresolvedLookupExpr(EmptyShell Empty,
399 bool HasTemplateKWAndArgsInfo)
400 : OverloadExpr(UnresolvedLookupExprClass, Empty, NumResults,
401 HasTemplateKWAndArgsInfo) {}
403 UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
404 const ASTContext &Context, CXXRecordDecl *NamingClass,
405 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
406 bool RequiresADL, bool Overloaded, UnresolvedSetIterator Begin,
407 UnresolvedSetIterator End) {
408 unsigned NumResults = End - Begin;
409 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
410 TemplateArgumentLoc>(NumResults, 0, 0);
411 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
412 return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc,
413 SourceLocation(), NameInfo, RequiresADL,
414 Overloaded, nullptr, Begin, End);
417 UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
418 const ASTContext &Context, CXXRecordDecl *NamingClass,
419 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
420 const DeclarationNameInfo &NameInfo, bool RequiresADL,
421 const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
422 UnresolvedSetIterator End) {
423 assert(Args || TemplateKWLoc.isValid());
424 unsigned NumResults = End - Begin;
425 unsigned NumTemplateArgs = Args ? Args->size() : 0;
427 totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
428 TemplateArgumentLoc>(NumResults, 1, NumTemplateArgs);
429 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
430 return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc,
431 TemplateKWLoc, NameInfo, RequiresADL,
432 /*Overloaded*/ true, Args, Begin, End);
435 UnresolvedLookupExpr *UnresolvedLookupExpr::CreateEmpty(
436 const ASTContext &Context, unsigned NumResults,
437 bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
438 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
439 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
440 TemplateArgumentLoc>(
441 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
442 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
444 UnresolvedLookupExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
447 OverloadExpr::OverloadExpr(StmtClass SC, const ASTContext &Context,
448 NestedNameSpecifierLoc QualifierLoc,
449 SourceLocation TemplateKWLoc,
450 const DeclarationNameInfo &NameInfo,
451 const TemplateArgumentListInfo *TemplateArgs,
452 UnresolvedSetIterator Begin,
453 UnresolvedSetIterator End, bool KnownDependent,
454 bool KnownInstantiationDependent,
455 bool KnownContainsUnexpandedParameterPack)
457 SC, Context.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
459 (KnownInstantiationDependent || NameInfo.isInstantiationDependent() ||
461 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
462 (KnownContainsUnexpandedParameterPack ||
463 NameInfo.containsUnexpandedParameterPack() ||
464 (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
465 ->containsUnexpandedParameterPack()))),
466 NameInfo(NameInfo), QualifierLoc(QualifierLoc) {
467 unsigned NumResults = End - Begin;
468 OverloadExprBits.NumResults = NumResults;
469 OverloadExprBits.HasTemplateKWAndArgsInfo =
470 (TemplateArgs != nullptr ) || TemplateKWLoc.isValid();
473 // Determine whether this expression is type-dependent.
474 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
475 if ((*I)->getDeclContext()->isDependentContext() ||
476 isa<UnresolvedUsingValueDecl>(*I)) {
477 ExprBits.TypeDependent = true;
478 ExprBits.ValueDependent = true;
479 ExprBits.InstantiationDependent = true;
483 // Copy the results to the trailing array past UnresolvedLookupExpr
484 // or UnresolvedMemberExpr.
485 DeclAccessPair *Results = getTrailingResults();
486 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
489 // If we have explicit template arguments, check for dependent
490 // template arguments and whether they contain any unexpanded pack
493 bool Dependent = false;
494 bool InstantiationDependent = false;
495 bool ContainsUnexpandedParameterPack = false;
496 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
497 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
498 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
501 ExprBits.TypeDependent = true;
502 ExprBits.ValueDependent = true;
504 if (InstantiationDependent)
505 ExprBits.InstantiationDependent = true;
506 if (ContainsUnexpandedParameterPack)
507 ExprBits.ContainsUnexpandedParameterPack = true;
508 } else if (TemplateKWLoc.isValid()) {
509 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
512 if (isTypeDependent())
513 setType(Context.DependentTy);
516 OverloadExpr::OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
517 bool HasTemplateKWAndArgsInfo)
519 OverloadExprBits.NumResults = NumResults;
520 OverloadExprBits.HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
523 // DependentScopeDeclRefExpr
524 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(
525 QualType Ty, NestedNameSpecifierLoc QualifierLoc,
526 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
527 const TemplateArgumentListInfo *Args)
529 DependentScopeDeclRefExprClass, Ty, VK_LValue, OK_Ordinary, true,
531 (NameInfo.isInstantiationDependent() ||
533 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
534 (NameInfo.containsUnexpandedParameterPack() ||
535 (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
536 ->containsUnexpandedParameterPack()))),
537 QualifierLoc(QualifierLoc), NameInfo(NameInfo) {
538 DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
539 (Args != nullptr) || TemplateKWLoc.isValid();
541 bool Dependent = true;
542 bool InstantiationDependent = true;
543 bool ContainsUnexpandedParameterPack
544 = ExprBits.ContainsUnexpandedParameterPack;
545 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
546 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
547 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
548 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
549 } else if (TemplateKWLoc.isValid()) {
550 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
555 DependentScopeDeclRefExpr *DependentScopeDeclRefExpr::Create(
556 const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
557 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
558 const TemplateArgumentListInfo *Args) {
559 assert(QualifierLoc && "should be created for dependent qualifiers");
560 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
562 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
563 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
564 void *Mem = Context.Allocate(Size);
565 return new (Mem) DependentScopeDeclRefExpr(Context.DependentTy, QualifierLoc,
566 TemplateKWLoc, NameInfo, Args);
569 DependentScopeDeclRefExpr *
570 DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &Context,
571 bool HasTemplateKWAndArgsInfo,
572 unsigned NumTemplateArgs) {
573 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
575 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
576 HasTemplateKWAndArgsInfo, NumTemplateArgs);
577 void *Mem = Context.Allocate(Size);
578 auto *E = new (Mem) DependentScopeDeclRefExpr(
579 QualType(), NestedNameSpecifierLoc(), SourceLocation(),
580 DeclarationNameInfo(), nullptr);
581 E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
582 HasTemplateKWAndArgsInfo;
586 SourceLocation CXXConstructExpr::getBeginLoc() const {
587 if (isa<CXXTemporaryObjectExpr>(this))
588 return cast<CXXTemporaryObjectExpr>(this)->getBeginLoc();
589 return getLocation();
592 SourceLocation CXXConstructExpr::getEndLoc() const {
593 if (isa<CXXTemporaryObjectExpr>(this))
594 return cast<CXXTemporaryObjectExpr>(this)->getEndLoc();
596 if (ParenOrBraceRange.isValid())
597 return ParenOrBraceRange.getEnd();
599 SourceLocation End = getLocation();
600 for (unsigned I = getNumArgs(); I > 0; --I) {
601 const Expr *Arg = getArg(I-1);
602 if (!Arg->isDefaultArgument()) {
603 SourceLocation NewEnd = Arg->getEndLoc();
604 if (NewEnd.isValid()) {
614 CXXOperatorCallExpr::CXXOperatorCallExpr(OverloadedOperatorKind OpKind,
615 Expr *Fn, ArrayRef<Expr *> Args,
616 QualType Ty, ExprValueKind VK,
617 SourceLocation OperatorLoc,
618 FPOptions FPFeatures,
620 : CallExpr(CXXOperatorCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
621 OperatorLoc, /*MinNumArgs=*/0, UsesADL) {
622 CXXOperatorCallExprBits.OperatorKind = OpKind;
623 CXXOperatorCallExprBits.FPFeatures = FPFeatures.getInt();
625 (CXXOperatorCallExprBits.OperatorKind == static_cast<unsigned>(OpKind)) &&
626 "OperatorKind overflow!");
627 assert((CXXOperatorCallExprBits.FPFeatures == FPFeatures.getInt()) &&
628 "FPFeatures overflow!");
629 Range = getSourceRangeImpl();
632 CXXOperatorCallExpr::CXXOperatorCallExpr(unsigned NumArgs, EmptyShell Empty)
633 : CallExpr(CXXOperatorCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
635 CXXOperatorCallExpr *CXXOperatorCallExpr::Create(
636 const ASTContext &Ctx, OverloadedOperatorKind OpKind, Expr *Fn,
637 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
638 SourceLocation OperatorLoc, FPOptions FPFeatures, ADLCallKind UsesADL) {
639 // Allocate storage for the trailing objects of CallExpr.
640 unsigned NumArgs = Args.size();
641 unsigned SizeOfTrailingObjects =
642 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
643 void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
644 alignof(CXXOperatorCallExpr));
645 return new (Mem) CXXOperatorCallExpr(OpKind, Fn, Args, Ty, VK, OperatorLoc,
646 FPFeatures, UsesADL);
649 CXXOperatorCallExpr *CXXOperatorCallExpr::CreateEmpty(const ASTContext &Ctx,
652 // Allocate storage for the trailing objects of CallExpr.
653 unsigned SizeOfTrailingObjects =
654 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
655 void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
656 alignof(CXXOperatorCallExpr));
657 return new (Mem) CXXOperatorCallExpr(NumArgs, Empty);
660 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
661 OverloadedOperatorKind Kind = getOperator();
662 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
663 if (getNumArgs() == 1)
665 return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
668 return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
669 } else if (Kind == OO_Arrow) {
670 return getArg(0)->getSourceRange();
671 } else if (Kind == OO_Call) {
672 return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
673 } else if (Kind == OO_Subscript) {
674 return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
675 } else if (getNumArgs() == 1) {
676 return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
677 } else if (getNumArgs() == 2) {
678 return SourceRange(getArg(0)->getBeginLoc(), getArg(1)->getEndLoc());
680 return getOperatorLoc();
684 CXXMemberCallExpr::CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args,
685 QualType Ty, ExprValueKind VK,
686 SourceLocation RP, unsigned MinNumArgs)
687 : CallExpr(CXXMemberCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, RP,
688 MinNumArgs, NotADL) {}
690 CXXMemberCallExpr::CXXMemberCallExpr(unsigned NumArgs, EmptyShell Empty)
691 : CallExpr(CXXMemberCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
693 CXXMemberCallExpr *CXXMemberCallExpr::Create(const ASTContext &Ctx, Expr *Fn,
694 ArrayRef<Expr *> Args, QualType Ty,
697 unsigned MinNumArgs) {
698 // Allocate storage for the trailing objects of CallExpr.
699 unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
700 unsigned SizeOfTrailingObjects =
701 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
702 void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
703 alignof(CXXMemberCallExpr));
704 return new (Mem) CXXMemberCallExpr(Fn, Args, Ty, VK, RP, MinNumArgs);
707 CXXMemberCallExpr *CXXMemberCallExpr::CreateEmpty(const ASTContext &Ctx,
710 // Allocate storage for the trailing objects of CallExpr.
711 unsigned SizeOfTrailingObjects =
712 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
713 void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
714 alignof(CXXMemberCallExpr));
715 return new (Mem) CXXMemberCallExpr(NumArgs, Empty);
718 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
719 const Expr *Callee = getCallee()->IgnoreParens();
720 if (const auto *MemExpr = dyn_cast<MemberExpr>(Callee))
721 return MemExpr->getBase();
722 if (const auto *BO = dyn_cast<BinaryOperator>(Callee))
723 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
726 // FIXME: Will eventually need to cope with member pointers.
730 QualType CXXMemberCallExpr::getObjectType() const {
731 QualType Ty = getImplicitObjectArgument()->getType();
732 if (Ty->isPointerType())
733 Ty = Ty->getPointeeType();
737 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
738 if (const auto *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
739 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
741 // FIXME: Will eventually need to cope with member pointers.
745 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
746 Expr* ThisArg = getImplicitObjectArgument();
750 if (ThisArg->getType()->isAnyPointerType())
751 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
753 return ThisArg->getType()->getAsCXXRecordDecl();
756 //===----------------------------------------------------------------------===//
758 //===----------------------------------------------------------------------===//
760 /// getCastName - Get the name of the C++ cast being used, e.g.,
761 /// "static_cast", "dynamic_cast", "reinterpret_cast", or
762 /// "const_cast". The returned pointer must not be freed.
763 const char *CXXNamedCastExpr::getCastName() const {
764 switch (getStmtClass()) {
765 case CXXStaticCastExprClass: return "static_cast";
766 case CXXDynamicCastExprClass: return "dynamic_cast";
767 case CXXReinterpretCastExprClass: return "reinterpret_cast";
768 case CXXConstCastExprClass: return "const_cast";
769 default: return "<invalid cast>";
773 CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
775 CastKind K, Expr *Op,
776 const CXXCastPath *BasePath,
777 TypeSourceInfo *WrittenTy,
779 SourceLocation RParenLoc,
780 SourceRange AngleBrackets) {
781 unsigned PathSize = (BasePath ? BasePath->size() : 0);
782 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
784 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
785 RParenLoc, AngleBrackets);
787 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
788 E->getTrailingObjects<CXXBaseSpecifier *>());
792 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
794 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
795 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
798 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
800 CastKind K, Expr *Op,
801 const CXXCastPath *BasePath,
802 TypeSourceInfo *WrittenTy,
804 SourceLocation RParenLoc,
805 SourceRange AngleBrackets) {
806 unsigned PathSize = (BasePath ? BasePath->size() : 0);
807 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
809 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
810 RParenLoc, AngleBrackets);
812 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
813 E->getTrailingObjects<CXXBaseSpecifier *>());
817 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
819 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
820 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
823 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
824 /// to always be null. For example:
827 /// struct B final : A { };
830 /// C *f(B* b) { return dynamic_cast<C*>(b); }
831 bool CXXDynamicCastExpr::isAlwaysNull() const
833 QualType SrcType = getSubExpr()->getType();
834 QualType DestType = getType();
836 if (const auto *SrcPTy = SrcType->getAs<PointerType>()) {
837 SrcType = SrcPTy->getPointeeType();
838 DestType = DestType->castAs<PointerType>()->getPointeeType();
841 if (DestType->isVoidType())
845 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
847 if (!SrcRD->hasAttr<FinalAttr>())
851 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
853 return !DestRD->isDerivedFrom(SrcRD);
856 CXXReinterpretCastExpr *
857 CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
858 ExprValueKind VK, CastKind K, Expr *Op,
859 const CXXCastPath *BasePath,
860 TypeSourceInfo *WrittenTy, SourceLocation L,
861 SourceLocation RParenLoc,
862 SourceRange AngleBrackets) {
863 unsigned PathSize = (BasePath ? BasePath->size() : 0);
864 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
866 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
867 RParenLoc, AngleBrackets);
869 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
870 E->getTrailingObjects<CXXBaseSpecifier *>());
874 CXXReinterpretCastExpr *
875 CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
876 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
877 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
880 CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
881 ExprValueKind VK, Expr *Op,
882 TypeSourceInfo *WrittenTy,
884 SourceLocation RParenLoc,
885 SourceRange AngleBrackets) {
886 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
889 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
890 return new (C) CXXConstCastExpr(EmptyShell());
893 CXXFunctionalCastExpr *
894 CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
895 TypeSourceInfo *Written, CastKind K, Expr *Op,
896 const CXXCastPath *BasePath,
897 SourceLocation L, SourceLocation R) {
898 unsigned PathSize = (BasePath ? BasePath->size() : 0);
899 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
901 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
903 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
904 E->getTrailingObjects<CXXBaseSpecifier *>());
908 CXXFunctionalCastExpr *
909 CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
910 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
911 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
914 SourceLocation CXXFunctionalCastExpr::getBeginLoc() const {
915 return getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
918 SourceLocation CXXFunctionalCastExpr::getEndLoc() const {
919 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getEndLoc();
922 UserDefinedLiteral::UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args,
923 QualType Ty, ExprValueKind VK,
924 SourceLocation LitEndLoc,
925 SourceLocation SuffixLoc)
926 : CallExpr(UserDefinedLiteralClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
927 LitEndLoc, /*MinNumArgs=*/0, NotADL),
928 UDSuffixLoc(SuffixLoc) {}
930 UserDefinedLiteral::UserDefinedLiteral(unsigned NumArgs, EmptyShell Empty)
931 : CallExpr(UserDefinedLiteralClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
933 UserDefinedLiteral *UserDefinedLiteral::Create(const ASTContext &Ctx, Expr *Fn,
934 ArrayRef<Expr *> Args,
935 QualType Ty, ExprValueKind VK,
936 SourceLocation LitEndLoc,
937 SourceLocation SuffixLoc) {
938 // Allocate storage for the trailing objects of CallExpr.
939 unsigned NumArgs = Args.size();
940 unsigned SizeOfTrailingObjects =
941 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
942 void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
943 alignof(UserDefinedLiteral));
944 return new (Mem) UserDefinedLiteral(Fn, Args, Ty, VK, LitEndLoc, SuffixLoc);
947 UserDefinedLiteral *UserDefinedLiteral::CreateEmpty(const ASTContext &Ctx,
950 // Allocate storage for the trailing objects of CallExpr.
951 unsigned SizeOfTrailingObjects =
952 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
953 void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
954 alignof(UserDefinedLiteral));
955 return new (Mem) UserDefinedLiteral(NumArgs, Empty);
958 UserDefinedLiteral::LiteralOperatorKind
959 UserDefinedLiteral::getLiteralOperatorKind() const {
960 if (getNumArgs() == 0)
962 if (getNumArgs() == 2)
965 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
967 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
968 if (ParamTy->isPointerType())
970 if (ParamTy->isAnyCharacterType())
971 return LOK_Character;
972 if (ParamTy->isIntegerType())
974 if (ParamTy->isFloatingType())
977 llvm_unreachable("unknown kind of literal operator");
980 Expr *UserDefinedLiteral::getCookedLiteral() {
982 LiteralOperatorKind LOK = getLiteralOperatorKind();
983 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
988 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
989 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
992 CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &Ctx, SourceLocation Loc,
993 FieldDecl *Field, QualType Ty,
994 DeclContext *UsedContext)
995 : Expr(CXXDefaultInitExprClass, Ty.getNonLValueExprType(Ctx),
996 Ty->isLValueReferenceType() ? VK_LValue : Ty->isRValueReferenceType()
999 /*FIXME*/ OK_Ordinary, false, false, false, false),
1000 Field(Field), UsedContext(UsedContext) {
1001 CXXDefaultInitExprBits.Loc = Loc;
1002 assert(Field->hasInClassInitializer());
1005 CXXTemporary *CXXTemporary::Create(const ASTContext &C,
1006 const CXXDestructorDecl *Destructor) {
1007 return new (C) CXXTemporary(Destructor);
1010 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
1013 assert((SubExpr->getType()->isRecordType() ||
1014 SubExpr->getType()->isArrayType()) &&
1015 "Expression bound to a temporary must have record or array type!");
1017 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
1020 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(
1021 CXXConstructorDecl *Cons, QualType Ty, TypeSourceInfo *TSI,
1022 ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
1023 bool HadMultipleCandidates, bool ListInitialization,
1024 bool StdInitListInitialization, bool ZeroInitialization)
1026 CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(),
1027 Cons, /* Elidable=*/false, Args, HadMultipleCandidates,
1028 ListInitialization, StdInitListInitialization, ZeroInitialization,
1029 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
1032 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(EmptyShell Empty,
1034 : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty, NumArgs) {}
1036 CXXTemporaryObjectExpr *CXXTemporaryObjectExpr::Create(
1037 const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
1038 TypeSourceInfo *TSI, ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
1039 bool HadMultipleCandidates, bool ListInitialization,
1040 bool StdInitListInitialization, bool ZeroInitialization) {
1041 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
1043 Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
1044 alignof(CXXTemporaryObjectExpr));
1045 return new (Mem) CXXTemporaryObjectExpr(
1046 Cons, Ty, TSI, Args, ParenOrBraceRange, HadMultipleCandidates,
1047 ListInitialization, StdInitListInitialization, ZeroInitialization);
1050 CXXTemporaryObjectExpr *
1051 CXXTemporaryObjectExpr::CreateEmpty(const ASTContext &Ctx, unsigned NumArgs) {
1052 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
1054 Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
1055 alignof(CXXTemporaryObjectExpr));
1056 return new (Mem) CXXTemporaryObjectExpr(EmptyShell(), NumArgs);
1059 SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const {
1060 return getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1063 SourceLocation CXXTemporaryObjectExpr::getEndLoc() const {
1064 SourceLocation Loc = getParenOrBraceRange().getEnd();
1065 if (Loc.isInvalid() && getNumArgs())
1066 Loc = getArg(getNumArgs() - 1)->getEndLoc();
1070 CXXConstructExpr *CXXConstructExpr::Create(
1071 const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
1072 CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
1073 bool HadMultipleCandidates, bool ListInitialization,
1074 bool StdInitListInitialization, bool ZeroInitialization,
1075 ConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
1076 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
1077 void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
1078 alignof(CXXConstructExpr));
1079 return new (Mem) CXXConstructExpr(
1080 CXXConstructExprClass, Ty, Loc, Ctor, Elidable, Args,
1081 HadMultipleCandidates, ListInitialization, StdInitListInitialization,
1082 ZeroInitialization, ConstructKind, ParenOrBraceRange);
1085 CXXConstructExpr *CXXConstructExpr::CreateEmpty(const ASTContext &Ctx,
1087 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
1088 void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
1089 alignof(CXXConstructExpr));
1091 CXXConstructExpr(CXXConstructExprClass, EmptyShell(), NumArgs);
1094 CXXConstructExpr::CXXConstructExpr(
1095 StmtClass SC, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor,
1096 bool Elidable, ArrayRef<Expr *> Args, bool HadMultipleCandidates,
1097 bool ListInitialization, bool StdInitListInitialization,
1098 bool ZeroInitialization, ConstructionKind ConstructKind,
1099 SourceRange ParenOrBraceRange)
1100 : Expr(SC, Ty, VK_RValue, OK_Ordinary, Ty->isDependentType(),
1101 Ty->isDependentType(), Ty->isInstantiationDependentType(),
1102 Ty->containsUnexpandedParameterPack()),
1103 Constructor(Ctor), ParenOrBraceRange(ParenOrBraceRange),
1104 NumArgs(Args.size()) {
1105 CXXConstructExprBits.Elidable = Elidable;
1106 CXXConstructExprBits.HadMultipleCandidates = HadMultipleCandidates;
1107 CXXConstructExprBits.ListInitialization = ListInitialization;
1108 CXXConstructExprBits.StdInitListInitialization = StdInitListInitialization;
1109 CXXConstructExprBits.ZeroInitialization = ZeroInitialization;
1110 CXXConstructExprBits.ConstructionKind = ConstructKind;
1111 CXXConstructExprBits.Loc = Loc;
1113 Stmt **TrailingArgs = getTrailingArgs();
1114 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1115 assert(Args[I] && "NULL argument in CXXConstructExpr!");
1117 if (Args[I]->isValueDependent())
1118 ExprBits.ValueDependent = true;
1119 if (Args[I]->isInstantiationDependent())
1120 ExprBits.InstantiationDependent = true;
1121 if (Args[I]->containsUnexpandedParameterPack())
1122 ExprBits.ContainsUnexpandedParameterPack = true;
1124 TrailingArgs[I] = Args[I];
1128 CXXConstructExpr::CXXConstructExpr(StmtClass SC, EmptyShell Empty,
1130 : Expr(SC, Empty), NumArgs(NumArgs) {}
1132 LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
1133 LambdaCaptureKind Kind, VarDecl *Var,
1134 SourceLocation EllipsisLoc)
1135 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
1138 Bits |= Capture_Implicit;
1142 Bits |= Capture_ByCopy;
1145 assert(!Var && "'this' capture cannot have a variable!");
1146 Bits |= Capture_This;
1150 Bits |= Capture_ByCopy;
1153 assert(Var && "capture must have a variable!");
1156 assert(!Var && "VLA type capture cannot have a variable!");
1159 DeclAndBits.setInt(Bits);
1162 LambdaCaptureKind LambdaCapture::getCaptureKind() const {
1163 if (capturesVLAType())
1165 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
1167 return CapByCopy ? LCK_StarThis : LCK_This;
1168 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
1171 LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
1172 LambdaCaptureDefault CaptureDefault,
1173 SourceLocation CaptureDefaultLoc,
1174 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
1175 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
1176 SourceLocation ClosingBrace,
1177 bool ContainsUnexpandedParameterPack)
1178 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
1179 T->isDependentType(), T->isDependentType(),
1180 ContainsUnexpandedParameterPack),
1181 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
1182 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
1183 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
1184 ClosingBrace(ClosingBrace) {
1185 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
1186 CXXRecordDecl *Class = getLambdaClass();
1187 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
1189 // FIXME: Propagate "has unexpanded parameter pack" bit.
1192 const ASTContext &Context = Class->getASTContext();
1193 Data.NumCaptures = NumCaptures;
1194 Data.NumExplicitCaptures = 0;
1196 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
1197 LambdaCapture *ToCapture = Data.Captures;
1198 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
1199 if (Captures[I].isExplicit())
1200 ++Data.NumExplicitCaptures;
1202 *ToCapture++ = Captures[I];
1205 // Copy initialization expressions for the non-static data members.
1206 Stmt **Stored = getStoredStmts();
1207 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
1208 *Stored++ = CaptureInits[I];
1210 // Copy the body of the lambda.
1211 *Stored++ = getCallOperator()->getBody();
1214 LambdaExpr *LambdaExpr::Create(
1215 const ASTContext &Context, CXXRecordDecl *Class,
1216 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
1217 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
1218 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
1219 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
1220 // Determine the type of the expression (i.e., the type of the
1221 // function object we're creating).
1222 QualType T = Context.getTypeDeclType(Class);
1224 unsigned Size = totalSizeToAlloc<Stmt *>(Captures.size() + 1);
1225 void *Mem = Context.Allocate(Size);
1227 LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
1228 Captures, ExplicitParams, ExplicitResultType, CaptureInits,
1229 ClosingBrace, ContainsUnexpandedParameterPack);
1232 LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1233 unsigned NumCaptures) {
1234 unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
1235 void *Mem = C.Allocate(Size);
1236 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
1239 bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1240 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1241 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
1244 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
1245 return getLambdaClass()->getLambdaData().Captures;
1248 LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
1249 return capture_begin() + NumCaptures;
1252 LambdaExpr::capture_range LambdaExpr::captures() const {
1253 return capture_range(capture_begin(), capture_end());
1256 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1257 return capture_begin();
1260 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1261 struct CXXRecordDecl::LambdaDefinitionData &Data
1262 = getLambdaClass()->getLambdaData();
1263 return Data.Captures + Data.NumExplicitCaptures;
1266 LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1267 return capture_range(explicit_capture_begin(), explicit_capture_end());
1270 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1271 return explicit_capture_end();
1274 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1275 return capture_end();
1278 LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1279 return capture_range(implicit_capture_begin(), implicit_capture_end());
1282 CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1283 return getType()->getAsCXXRecordDecl();
1286 CXXMethodDecl *LambdaExpr::getCallOperator() const {
1287 CXXRecordDecl *Record = getLambdaClass();
1288 return Record->getLambdaCallOperator();
1291 FunctionTemplateDecl *LambdaExpr::getDependentCallOperator() const {
1292 CXXRecordDecl *Record = getLambdaClass();
1293 return Record->getDependentLambdaCallOperator();
1296 TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1297 CXXRecordDecl *Record = getLambdaClass();
1298 return Record->getGenericLambdaTemplateParameterList();
1301 ArrayRef<NamedDecl *> LambdaExpr::getExplicitTemplateParameters() const {
1302 const CXXRecordDecl *Record = getLambdaClass();
1303 return Record->getLambdaExplicitTemplateParameters();
1306 CompoundStmt *LambdaExpr::getBody() const {
1307 // FIXME: this mutation in getBody is bogus. It should be
1308 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1309 // don't understand, that doesn't work.
1310 if (!getStoredStmts()[NumCaptures])
1311 *const_cast<Stmt **>(&getStoredStmts()[NumCaptures]) =
1312 getCallOperator()->getBody();
1314 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1317 bool LambdaExpr::isMutable() const {
1318 return !getCallOperator()->isConst();
1321 ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1322 bool CleanupsHaveSideEffects,
1323 ArrayRef<CleanupObject> objects)
1324 : FullExpr(ExprWithCleanupsClass, subexpr) {
1325 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
1326 ExprWithCleanupsBits.NumObjects = objects.size();
1327 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1328 getTrailingObjects<CleanupObject>()[i] = objects[i];
1331 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
1332 bool CleanupsHaveSideEffects,
1333 ArrayRef<CleanupObject> objects) {
1334 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
1335 alignof(ExprWithCleanups));
1337 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
1340 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1341 : FullExpr(ExprWithCleanupsClass, empty) {
1342 ExprWithCleanupsBits.NumObjects = numObjects;
1345 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1347 unsigned numObjects) {
1348 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
1349 alignof(ExprWithCleanups));
1350 return new (buffer) ExprWithCleanups(empty, numObjects);
1353 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *TSI,
1354 SourceLocation LParenLoc,
1355 ArrayRef<Expr *> Args,
1356 SourceLocation RParenLoc)
1357 : Expr(CXXUnresolvedConstructExprClass,
1358 TSI->getType().getNonReferenceType(),
1359 (TSI->getType()->isLValueReferenceType()
1361 : TSI->getType()->isRValueReferenceType() ? VK_XValue
1364 TSI->getType()->isDependentType() ||
1365 TSI->getType()->getContainedDeducedType(),
1366 true, true, TSI->getType()->containsUnexpandedParameterPack()),
1367 TSI(TSI), LParenLoc(LParenLoc), RParenLoc(RParenLoc) {
1368 CXXUnresolvedConstructExprBits.NumArgs = Args.size();
1369 auto **StoredArgs = getTrailingObjects<Expr *>();
1370 for (unsigned I = 0; I != Args.size(); ++I) {
1371 if (Args[I]->containsUnexpandedParameterPack())
1372 ExprBits.ContainsUnexpandedParameterPack = true;
1374 StoredArgs[I] = Args[I];
1378 CXXUnresolvedConstructExpr *CXXUnresolvedConstructExpr::Create(
1379 const ASTContext &Context, TypeSourceInfo *TSI, SourceLocation LParenLoc,
1380 ArrayRef<Expr *> Args, SourceLocation RParenLoc) {
1381 void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
1382 return new (Mem) CXXUnresolvedConstructExpr(TSI, LParenLoc, Args, RParenLoc);
1385 CXXUnresolvedConstructExpr *
1386 CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &Context,
1388 void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
1389 return new (Mem) CXXUnresolvedConstructExpr(EmptyShell(), NumArgs);
1392 SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const {
1393 return TSI->getTypeLoc().getBeginLoc();
1396 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1397 const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
1398 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1399 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1400 DeclarationNameInfo MemberNameInfo,
1401 const TemplateArgumentListInfo *TemplateArgs)
1402 : Expr(CXXDependentScopeMemberExprClass, Ctx.DependentTy, VK_LValue,
1403 OK_Ordinary, true, true, true,
1404 ((Base && Base->containsUnexpandedParameterPack()) ||
1405 (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
1406 ->containsUnexpandedParameterPack()) ||
1407 MemberNameInfo.containsUnexpandedParameterPack())),
1408 Base(Base), BaseType(BaseType), QualifierLoc(QualifierLoc),
1409 MemberNameInfo(MemberNameInfo) {
1410 CXXDependentScopeMemberExprBits.IsArrow = IsArrow;
1411 CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
1412 (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
1413 CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
1414 FirstQualifierFoundInScope != nullptr;
1415 CXXDependentScopeMemberExprBits.OperatorLoc = OperatorLoc;
1418 bool Dependent = true;
1419 bool InstantiationDependent = true;
1420 bool ContainsUnexpandedParameterPack = false;
1421 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1422 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1423 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
1424 if (ContainsUnexpandedParameterPack)
1425 ExprBits.ContainsUnexpandedParameterPack = true;
1426 } else if (TemplateKWLoc.isValid()) {
1427 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1431 if (hasFirstQualifierFoundInScope())
1432 *getTrailingObjects<NamedDecl *>() = FirstQualifierFoundInScope;
1435 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1436 EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
1437 bool HasFirstQualifierFoundInScope)
1438 : Expr(CXXDependentScopeMemberExprClass, Empty) {
1439 CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
1440 HasTemplateKWAndArgsInfo;
1441 CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
1442 HasFirstQualifierFoundInScope;
1445 CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::Create(
1446 const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
1447 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1448 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1449 DeclarationNameInfo MemberNameInfo,
1450 const TemplateArgumentListInfo *TemplateArgs) {
1451 bool HasTemplateKWAndArgsInfo =
1452 (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
1453 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1454 bool HasFirstQualifierFoundInScope = FirstQualifierFoundInScope != nullptr;
1456 unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
1457 TemplateArgumentLoc, NamedDecl *>(
1458 HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
1460 void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1461 return new (Mem) CXXDependentScopeMemberExpr(
1462 Ctx, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
1463 FirstQualifierFoundInScope, MemberNameInfo, TemplateArgs);
1466 CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::CreateEmpty(
1467 const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
1468 unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope) {
1469 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1471 unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
1472 TemplateArgumentLoc, NamedDecl *>(
1473 HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
1475 void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1476 return new (Mem) CXXDependentScopeMemberExpr(
1477 EmptyShell(), HasTemplateKWAndArgsInfo, HasFirstQualifierFoundInScope);
1480 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1481 UnresolvedSetIterator end) {
1483 NamedDecl *decl = *begin;
1484 if (isa<UnresolvedUsingValueDecl>(decl))
1487 // Unresolved member expressions should only contain methods and
1488 // method templates.
1489 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1492 } while (++begin != end);
1497 UnresolvedMemberExpr::UnresolvedMemberExpr(
1498 const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
1499 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
1500 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1501 const DeclarationNameInfo &MemberNameInfo,
1502 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1503 UnresolvedSetIterator End)
1505 UnresolvedMemberExprClass, Context, QualifierLoc, TemplateKWLoc,
1506 MemberNameInfo, TemplateArgs, Begin, End,
1508 ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
1509 ((Base && Base->isInstantiationDependent()) ||
1510 BaseType->isInstantiationDependentType()),
1511 // Contains unexpanded parameter pack
1512 ((Base && Base->containsUnexpandedParameterPack()) ||
1513 BaseType->containsUnexpandedParameterPack())),
1514 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
1515 UnresolvedMemberExprBits.IsArrow = IsArrow;
1516 UnresolvedMemberExprBits.HasUnresolvedUsing = HasUnresolvedUsing;
1518 // Check whether all of the members are non-static member functions,
1519 // and if so, mark give this bound-member type instead of overload type.
1520 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1521 setType(Context.BoundMemberTy);
1524 UnresolvedMemberExpr::UnresolvedMemberExpr(EmptyShell Empty,
1525 unsigned NumResults,
1526 bool HasTemplateKWAndArgsInfo)
1527 : OverloadExpr(UnresolvedMemberExprClass, Empty, NumResults,
1528 HasTemplateKWAndArgsInfo) {}
1530 bool UnresolvedMemberExpr::isImplicitAccess() const {
1534 return cast<Expr>(Base)->isImplicitCXXThis();
1537 UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1538 const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
1539 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
1540 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1541 const DeclarationNameInfo &MemberNameInfo,
1542 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1543 UnresolvedSetIterator End) {
1544 unsigned NumResults = End - Begin;
1545 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1546 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1547 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
1548 TemplateArgumentLoc>(
1549 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
1550 void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
1551 return new (Mem) UnresolvedMemberExpr(
1552 Context, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc,
1553 QualifierLoc, TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
1556 UnresolvedMemberExpr *UnresolvedMemberExpr::CreateEmpty(
1557 const ASTContext &Context, unsigned NumResults,
1558 bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
1559 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1560 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
1561 TemplateArgumentLoc>(
1562 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
1563 void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
1565 UnresolvedMemberExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
1568 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() {
1569 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1571 // If there was a nested name specifier, it names the naming class.
1572 // It can't be dependent: after all, we were actually able to do the
1574 CXXRecordDecl *Record = nullptr;
1575 auto *NNS = getQualifier();
1576 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
1577 const Type *T = getQualifier()->getAsType();
1578 assert(T && "qualifier in member expression does not name type");
1579 Record = T->getAsCXXRecordDecl();
1580 assert(Record && "qualifier in member expression does not name record");
1582 // Otherwise the naming class must have been the base class.
1584 QualType BaseType = getBaseType().getNonReferenceType();
1586 BaseType = BaseType->castAs<PointerType>()->getPointeeType();
1588 Record = BaseType->getAsCXXRecordDecl();
1589 assert(Record && "base of member expression does not name record");
1596 SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1597 NamedDecl *Pack, SourceLocation PackLoc,
1598 SourceLocation RParenLoc,
1599 Optional<unsigned> Length,
1600 ArrayRef<TemplateArgument> PartialArgs) {
1602 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
1603 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1604 PackLoc, RParenLoc, Length, PartialArgs);
1607 SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1608 unsigned NumPartialArgs) {
1610 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
1611 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1614 SubstNonTypeTemplateParmPackExpr::
1615 SubstNonTypeTemplateParmPackExpr(QualType T,
1616 ExprValueKind ValueKind,
1617 NonTypeTemplateParmDecl *Param,
1618 SourceLocation NameLoc,
1619 const TemplateArgument &ArgPack)
1620 : Expr(SubstNonTypeTemplateParmPackExprClass, T, ValueKind, OK_Ordinary,
1621 true, true, true, true),
1622 Param(Param), Arguments(ArgPack.pack_begin()),
1623 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) {}
1625 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1626 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
1629 FunctionParmPackExpr::FunctionParmPackExpr(QualType T, VarDecl *ParamPack,
1630 SourceLocation NameLoc,
1632 VarDecl *const *Params)
1633 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1635 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1637 std::uninitialized_copy(Params, Params + NumParams,
1638 getTrailingObjects<VarDecl *>());
1641 FunctionParmPackExpr *
1642 FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
1643 VarDecl *ParamPack, SourceLocation NameLoc,
1644 ArrayRef<VarDecl *> Params) {
1645 return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(Params.size())))
1646 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1649 FunctionParmPackExpr *
1650 FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1651 unsigned NumParams) {
1652 return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(NumParams)))
1653 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
1656 void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1657 unsigned ManglingNumber) {
1658 // We only need extra state if we have to remember more than just the Stmt.
1662 // We may need to allocate extra storage for the mangling number and the
1663 // extended-by ValueDecl.
1664 if (!State.is<ExtraState *>()) {
1665 auto *ES = new (ExtendedBy->getASTContext()) ExtraState;
1666 ES->Temporary = State.get<Stmt *>();
1670 auto ES = State.get<ExtraState *>();
1671 ES->ExtendingDecl = ExtendedBy;
1672 ES->ManglingNumber = ManglingNumber;
1675 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1676 ArrayRef<TypeSourceInfo *> Args,
1677 SourceLocation RParenLoc,
1679 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1680 /*TypeDependent=*/false,
1681 /*ValueDependent=*/false,
1682 /*InstantiationDependent=*/false,
1683 /*ContainsUnexpandedParameterPack=*/false),
1684 Loc(Loc), RParenLoc(RParenLoc) {
1685 TypeTraitExprBits.Kind = Kind;
1686 TypeTraitExprBits.Value = Value;
1687 TypeTraitExprBits.NumArgs = Args.size();
1689 auto **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1691 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1692 if (Args[I]->getType()->isDependentType())
1693 setValueDependent(true);
1694 if (Args[I]->getType()->isInstantiationDependentType())
1695 setInstantiationDependent(true);
1696 if (Args[I]->getType()->containsUnexpandedParameterPack())
1697 setContainsUnexpandedParameterPack(true);
1699 ToArgs[I] = Args[I];
1703 TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
1706 ArrayRef<TypeSourceInfo *> Args,
1707 SourceLocation RParenLoc,
1709 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
1710 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1713 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
1715 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
1716 return new (Mem) TypeTraitExpr(EmptyShell());
1719 CUDAKernelCallExpr::CUDAKernelCallExpr(Expr *Fn, CallExpr *Config,
1720 ArrayRef<Expr *> Args, QualType Ty,
1721 ExprValueKind VK, SourceLocation RP,
1722 unsigned MinNumArgs)
1723 : CallExpr(CUDAKernelCallExprClass, Fn, /*PreArgs=*/Config, Args, Ty, VK,
1724 RP, MinNumArgs, NotADL) {}
1726 CUDAKernelCallExpr::CUDAKernelCallExpr(unsigned NumArgs, EmptyShell Empty)
1727 : CallExpr(CUDAKernelCallExprClass, /*NumPreArgs=*/END_PREARG, NumArgs,
1730 CUDAKernelCallExpr *
1731 CUDAKernelCallExpr::Create(const ASTContext &Ctx, Expr *Fn, CallExpr *Config,
1732 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
1733 SourceLocation RP, unsigned MinNumArgs) {
1734 // Allocate storage for the trailing objects of CallExpr.
1735 unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
1736 unsigned SizeOfTrailingObjects =
1737 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/END_PREARG, NumArgs);
1738 void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1739 alignof(CUDAKernelCallExpr));
1740 return new (Mem) CUDAKernelCallExpr(Fn, Config, Args, Ty, VK, RP, MinNumArgs);
1743 CUDAKernelCallExpr *CUDAKernelCallExpr::CreateEmpty(const ASTContext &Ctx,
1746 // Allocate storage for the trailing objects of CallExpr.
1747 unsigned SizeOfTrailingObjects =
1748 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/END_PREARG, NumArgs);
1749 void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1750 alignof(CUDAKernelCallExpr));
1751 return new (Mem) CUDAKernelCallExpr(NumArgs, Empty);
1754 ConceptSpecializationExpr::ConceptSpecializationExpr(ASTContext &C,
1755 NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc,
1756 SourceLocation ConceptNameLoc, NamedDecl *FoundDecl,
1757 ConceptDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten,
1758 ArrayRef<TemplateArgument> ConvertedArgs, Optional<bool> IsSatisfied)
1759 : Expr(ConceptSpecializationExprClass, C.BoolTy, VK_RValue, OK_Ordinary,
1760 /*TypeDependent=*/false,
1761 // All the flags below are set in setTemplateArguments.
1762 /*ValueDependent=*/!IsSatisfied.hasValue(),
1763 /*InstantiationDependent=*/false,
1764 /*ContainsUnexpandedParameterPacks=*/false),
1765 NestedNameSpec(NNS), TemplateKWLoc(TemplateKWLoc),
1766 ConceptNameLoc(ConceptNameLoc), FoundDecl(FoundDecl),
1767 NamedConcept(NamedConcept, IsSatisfied ? *IsSatisfied : true),
1768 NumTemplateArgs(ConvertedArgs.size()) {
1770 setTemplateArguments(ArgsAsWritten, ConvertedArgs);
1773 ConceptSpecializationExpr::ConceptSpecializationExpr(EmptyShell Empty,
1774 unsigned NumTemplateArgs)
1775 : Expr(ConceptSpecializationExprClass, Empty),
1776 NumTemplateArgs(NumTemplateArgs) { }
1778 void ConceptSpecializationExpr::setTemplateArguments(
1779 const ASTTemplateArgumentListInfo *ArgsAsWritten,
1780 ArrayRef<TemplateArgument> Converted) {
1781 assert(Converted.size() == NumTemplateArgs);
1782 assert(!this->ArgsAsWritten && "setTemplateArguments can only be used once");
1783 this->ArgsAsWritten = ArgsAsWritten;
1784 std::uninitialized_copy(Converted.begin(), Converted.end(),
1785 getTrailingObjects<TemplateArgument>());
1786 bool IsInstantiationDependent = false;
1787 bool ContainsUnexpandedParameterPack = false;
1788 for (const TemplateArgumentLoc& LocInfo : ArgsAsWritten->arguments()) {
1789 if (LocInfo.getArgument().isInstantiationDependent())
1790 IsInstantiationDependent = true;
1791 if (LocInfo.getArgument().containsUnexpandedParameterPack())
1792 ContainsUnexpandedParameterPack = true;
1793 if (ContainsUnexpandedParameterPack && IsInstantiationDependent)
1797 // Currently guaranteed by the fact concepts can only be at namespace-scope.
1798 assert(!NestedNameSpec ||
1799 (!NestedNameSpec.getNestedNameSpecifier()->isInstantiationDependent() &&
1800 !NestedNameSpec.getNestedNameSpecifier()
1801 ->containsUnexpandedParameterPack()));
1802 setInstantiationDependent(IsInstantiationDependent);
1803 setContainsUnexpandedParameterPack(ContainsUnexpandedParameterPack);
1804 assert((!isValueDependent() || isInstantiationDependent()) &&
1805 "should not be value-dependent");
1808 ConceptSpecializationExpr *
1809 ConceptSpecializationExpr::Create(ASTContext &C, NestedNameSpecifierLoc NNS,
1810 SourceLocation TemplateKWLoc,
1811 SourceLocation ConceptNameLoc,
1812 NamedDecl *FoundDecl,
1813 ConceptDecl *NamedConcept,
1814 const ASTTemplateArgumentListInfo *ArgsAsWritten,
1815 ArrayRef<TemplateArgument> ConvertedArgs,
1816 Optional<bool> IsSatisfied) {
1817 void *Buffer = C.Allocate(totalSizeToAlloc<TemplateArgument>(
1818 ConvertedArgs.size()));
1819 return new (Buffer) ConceptSpecializationExpr(C, NNS, TemplateKWLoc,
1820 ConceptNameLoc, FoundDecl,
1821 NamedConcept, ArgsAsWritten,
1822 ConvertedArgs, IsSatisfied);
1825 ConceptSpecializationExpr *
1826 ConceptSpecializationExpr::Create(ASTContext &C, EmptyShell Empty,
1827 unsigned NumTemplateArgs) {
1828 void *Buffer = C.Allocate(totalSizeToAlloc<TemplateArgument>(
1830 return new (Buffer) ConceptSpecializationExpr(Empty, NumTemplateArgs);