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