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/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" 34 #include <cassert> 35 #include <cstddef> 36 #include <cstring> 37 #include <memory> 38 39 using namespace clang; 40 41 //===----------------------------------------------------------------------===// 42 // Child Iterators for iterating over subexpressions/substatements 43 //===----------------------------------------------------------------------===// 44 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 49 // expressions. 50 if (getNumArgs() != 2) 51 return false; 52 53 switch (getOperator()) { 54 case OO_Call: case OO_Subscript: 55 return false; 56 default: 57 return true; 58 } 59 } 60 61 CXXRewrittenBinaryOperator::DecomposedForm 62 CXXRewrittenBinaryOperator::getDecomposedForm() const { 63 DecomposedForm Result = {}; 64 const Expr *E = getSemanticForm()->IgnoreImplicit(); 65 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(); 71 SkippedNot = true; 72 } 73 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"); 92 } 93 Result.LHS = BO->getArg(0); 94 Result.RHS = BO->getArg(1); 95 Result.InnerBinOp = BO; 96 } else { 97 llvm_unreachable("unexpected rewritten operator form"); 98 } 99 100 // Put the operands in the right order for == and !=, and canonicalize the 101 // <=> subexpression onto the LHS for all other forms. 102 if (isReversed()) 103 std::swap(Result.LHS, Result.RHS); 104 105 // If this isn't a spaceship rewrite, we're done. 106 if (Result.Opcode == BO_EQ || Result.Opcode == BO_NE) 107 return Result; 108 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; 121 } else { 122 llvm_unreachable("unexpected rewritten operator form"); 123 } 124 125 // Put the comparison operands in the right order. 126 if (isReversed()) 127 std::swap(Result.LHS, Result.RHS); 128 return Result; 129 } 130 131 bool CXXTypeidExpr::isPotentiallyEvaluated() const { 132 if (isTypeOperand()) 133 return false; 134 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()) 141 return true; 142 143 return false; 144 } 145 146 QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const { 147 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 148 Qualifiers Quals; 149 return Context.getUnqualifiedArrayType( 150 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals); 151 } 152 153 QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const { 154 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)"); 155 Qualifiers Quals; 156 return Context.getUnqualifiedArrayType( 157 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals); 158 } 159 160 // CXXScalarValueInitExpr 161 SourceLocation CXXScalarValueInitExpr::getBeginLoc() const { 162 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : getRParenLoc(); 163 } 164 165 // CXXNewExpr 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) { 181 182 assert((Initializer != nullptr || InitializationStyle == NoInit) && 183 "Only NoInit can have no initializer!"); 184 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(); 194 195 if (ArraySize) { 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; 203 } 204 205 getTrailingObjects<Stmt *>()[arraySizeOffset()] = *ArraySize; 206 } 207 208 if (Initializer) { 209 if (Initializer->isValueDependent()) 210 ExprBits.ValueDependent = true; 211 if (Initializer->isInstantiationDependent()) 212 ExprBits.InstantiationDependent = true; 213 if (Initializer->containsUnexpandedParameterPack()) 214 ExprBits.ContainsUnexpandedParameterPack = true; 215 216 getTrailingObjects<Stmt *>()[initExprOffset()] = Initializer; 217 } 218 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; 226 227 getTrailingObjects<Stmt *>()[placementNewArgsOffset() + I] = 228 PlacementArgs[I]; 229 } 230 231 if (IsParenTypeId) 232 getTrailingObjects<SourceRange>()[0] = TypeIdParens; 233 234 switch (getInitializationStyle()) { 235 case CallInit: 236 this->Range.setEnd(DirectInitRange.getEnd()); 237 break; 238 case ListInit: 239 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); 240 break; 241 default: 242 if (IsParenTypeId) 243 this->Range.setEnd(TypeIdParens.getEnd()); 244 break; 245 } 246 } 247 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; 254 } 255 256 CXXNewExpr * 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(); 269 void *Mem = 270 Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>( 271 IsArray + HasInit + NumPlacementArgs, IsParenTypeId), 272 alignof(CXXNewExpr)); 273 return new (Mem) 274 CXXNewExpr(IsGlobalNew, OperatorNew, OperatorDelete, ShouldPassAlignment, 275 UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens, 276 ArraySize, InitializationStyle, Initializer, Ty, 277 AllocatedTypeInfo, Range, DirectInitRange); 278 } 279 280 CXXNewExpr *CXXNewExpr::CreateEmpty(const ASTContext &Ctx, bool IsArray, 281 bool HasInit, unsigned NumPlacementArgs, 282 bool IsParenTypeId) { 283 void *Mem = 284 Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>( 285 IsArray + HasInit + NumPlacementArgs, IsParenTypeId), 286 alignof(CXXNewExpr)); 287 return new (Mem) 288 CXXNewExpr(EmptyShell(), IsArray, NumPlacementArgs, IsParenTypeId); 289 } 290 291 bool CXXNewExpr::shouldNullCheckAllocation() const { 292 return getOperatorNew() 293 ->getType() 294 ->castAs<FunctionProtoType>() 295 ->isNothrow() && 296 !getOperatorNew()->isReservedGlobalPlacementOperator(); 297 } 298 299 // CXXDeleteExpr 300 QualType CXXDeleteExpr::getDestroyedType() const { 301 const Expr *Arg = getArgument(); 302 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' 305 // function. 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(); 314 } else 315 break; 316 } 317 318 // The type-to-delete may not be a pointer if it's a dependent type. 319 const QualType ArgType = Arg->getType(); 320 321 if (ArgType->isDependentType() && !ArgType->isPointerType()) 322 return QualType(); 323 324 return ArgType->castAs<PointerType>()->getPointeeType(); 325 } 326 327 // CXXPseudoDestructorExpr 328 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info) 329 : Type(Info) { 330 Location = Info->getTypeLoc().getLocalSourceRange().getBegin(); 331 } 332 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() || 346 (QualifierLoc && 347 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) || 348 (ScopeType && 349 ScopeType->getType()->isInstantiationDependentType()) || 350 (DestroyedType.getTypeSourceInfo() && 351 DestroyedType.getTypeSourceInfo()->getType() 352 ->isInstantiationDependentType())), 353 // ContainsUnexpandedParameterPack 354 (Base->containsUnexpandedParameterPack() || 355 (QualifierLoc && 356 QualifierLoc.getNestedNameSpecifier() 357 ->containsUnexpandedParameterPack()) || 358 (ScopeType && 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) {} 367 368 QualType CXXPseudoDestructorExpr::getDestroyedType() const { 369 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo()) 370 return TInfo->getType(); 371 372 return QualType(); 373 } 374 375 SourceLocation CXXPseudoDestructorExpr::getEndLoc() const { 376 SourceLocation End = DestroyedType.getLocation(); 377 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo()) 378 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd(); 379 return End; 380 } 381 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, 391 false, false), 392 NamingClass(NamingClass) { 393 UnresolvedLookupExprBits.RequiresADL = RequiresADL; 394 UnresolvedLookupExprBits.Overloaded = Overloaded; 395 } 396 397 UnresolvedLookupExpr::UnresolvedLookupExpr(EmptyShell Empty, 398 unsigned NumResults, 399 bool HasTemplateKWAndArgsInfo) 400 : OverloadExpr(UnresolvedLookupExprClass, Empty, NumResults, 401 HasTemplateKWAndArgsInfo) {} 402 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); 415 } 416 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; 426 unsigned Size = 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); 433 } 434 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)); 443 return new (Mem) 444 UnresolvedLookupExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo); 445 } 446 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) 456 : Expr( 457 SC, Context.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent, 458 KnownDependent, 459 (KnownInstantiationDependent || NameInfo.isInstantiationDependent() || 460 (QualifierLoc && 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(); 471 472 if (NumResults) { 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; 480 } 481 } 482 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)); 487 } 488 489 // If we have explicit template arguments, check for dependent 490 // template arguments and whether they contain any unexpanded pack 491 // expansions. 492 if (TemplateArgs) { 493 bool Dependent = false; 494 bool InstantiationDependent = false; 495 bool ContainsUnexpandedParameterPack = false; 496 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom( 497 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(), 498 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack); 499 500 if (Dependent) { 501 ExprBits.TypeDependent = true; 502 ExprBits.ValueDependent = true; 503 } 504 if (InstantiationDependent) 505 ExprBits.InstantiationDependent = true; 506 if (ContainsUnexpandedParameterPack) 507 ExprBits.ContainsUnexpandedParameterPack = true; 508 } else if (TemplateKWLoc.isValid()) { 509 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc); 510 } 511 512 if (isTypeDependent()) 513 setType(Context.DependentTy); 514 } 515 516 OverloadExpr::OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults, 517 bool HasTemplateKWAndArgsInfo) 518 : Expr(SC, Empty) { 519 OverloadExprBits.NumResults = NumResults; 520 OverloadExprBits.HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo; 521 } 522 523 // DependentScopeDeclRefExpr 524 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr( 525 QualType Ty, NestedNameSpecifierLoc QualifierLoc, 526 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo, 527 const TemplateArgumentListInfo *Args) 528 : Expr( 529 DependentScopeDeclRefExprClass, Ty, VK_LValue, OK_Ordinary, true, 530 true, 531 (NameInfo.isInstantiationDependent() || 532 (QualifierLoc && 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(); 540 if (Args) { 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( 551 TemplateKWLoc); 552 } 553 } 554 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(); 561 std::size_t Size = 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); 567 } 568 569 DependentScopeDeclRefExpr * 570 DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &Context, 571 bool HasTemplateKWAndArgsInfo, 572 unsigned NumTemplateArgs) { 573 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo); 574 std::size_t Size = 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; 583 return E; 584 } 585 586 SourceLocation CXXConstructExpr::getBeginLoc() const { 587 if (isa<CXXTemporaryObjectExpr>(this)) 588 return cast<CXXTemporaryObjectExpr>(this)->getBeginLoc(); 589 return getLocation(); 590 } 591 592 SourceLocation CXXConstructExpr::getEndLoc() const { 593 if (isa<CXXTemporaryObjectExpr>(this)) 594 return cast<CXXTemporaryObjectExpr>(this)->getEndLoc(); 595 596 if (ParenOrBraceRange.isValid()) 597 return ParenOrBraceRange.getEnd(); 598 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()) { 605 End = NewEnd; 606 break; 607 } 608 } 609 } 610 611 return End; 612 } 613 614 CXXOperatorCallExpr::CXXOperatorCallExpr(OverloadedOperatorKind OpKind, 615 Expr *Fn, ArrayRef<Expr *> Args, 616 QualType Ty, ExprValueKind VK, 617 SourceLocation OperatorLoc, 618 FPOptions FPFeatures, 619 ADLCallKind UsesADL) 620 : CallExpr(CXXOperatorCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, 621 OperatorLoc, /*MinNumArgs=*/0, UsesADL) { 622 CXXOperatorCallExprBits.OperatorKind = OpKind; 623 CXXOperatorCallExprBits.FPFeatures = FPFeatures.getInt(); 624 assert( 625 (CXXOperatorCallExprBits.OperatorKind == static_cast<unsigned>(OpKind)) && 626 "OperatorKind overflow!"); 627 assert((CXXOperatorCallExprBits.FPFeatures == FPFeatures.getInt()) && 628 "FPFeatures overflow!"); 629 Range = getSourceRangeImpl(); 630 } 631 632 CXXOperatorCallExpr::CXXOperatorCallExpr(unsigned NumArgs, EmptyShell Empty) 633 : CallExpr(CXXOperatorCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {} 634 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); 647 } 648 649 CXXOperatorCallExpr *CXXOperatorCallExpr::CreateEmpty(const ASTContext &Ctx, 650 unsigned NumArgs, 651 EmptyShell Empty) { 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); 658 } 659 660 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const { 661 OverloadedOperatorKind Kind = getOperator(); 662 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 663 if (getNumArgs() == 1) 664 // Prefix operator 665 return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc()); 666 else 667 // Postfix operator 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()); 679 } else { 680 return getOperatorLoc(); 681 } 682 } 683 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) {} 689 690 CXXMemberCallExpr::CXXMemberCallExpr(unsigned NumArgs, EmptyShell Empty) 691 : CallExpr(CXXMemberCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {} 692 693 CXXMemberCallExpr *CXXMemberCallExpr::Create(const ASTContext &Ctx, Expr *Fn, 694 ArrayRef<Expr *> Args, QualType Ty, 695 ExprValueKind VK, 696 SourceLocation RP, 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); 705 } 706 707 CXXMemberCallExpr *CXXMemberCallExpr::CreateEmpty(const ASTContext &Ctx, 708 unsigned NumArgs, 709 EmptyShell Empty) { 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); 716 } 717 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) 724 return BO->getLHS(); 725 726 // FIXME: Will eventually need to cope with member pointers. 727 return nullptr; 728 } 729 730 QualType CXXMemberCallExpr::getObjectType() const { 731 QualType Ty = getImplicitObjectArgument()->getType(); 732 if (Ty->isPointerType()) 733 Ty = Ty->getPointeeType(); 734 return Ty; 735 } 736 737 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const { 738 if (const auto *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens())) 739 return cast<CXXMethodDecl>(MemExpr->getMemberDecl()); 740 741 // FIXME: Will eventually need to cope with member pointers. 742 return nullptr; 743 } 744 745 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const { 746 Expr* ThisArg = getImplicitObjectArgument(); 747 if (!ThisArg) 748 return nullptr; 749 750 if (ThisArg->getType()->isAnyPointerType()) 751 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl(); 752 753 return ThisArg->getType()->getAsCXXRecordDecl(); 754 } 755 756 //===----------------------------------------------------------------------===// 757 // Named casts 758 //===----------------------------------------------------------------------===// 759 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>"; 770 } 771 } 772 773 CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T, 774 ExprValueKind VK, 775 CastKind K, Expr *Op, 776 const CXXCastPath *BasePath, 777 TypeSourceInfo *WrittenTy, 778 SourceLocation L, 779 SourceLocation RParenLoc, 780 SourceRange AngleBrackets) { 781 unsigned PathSize = (BasePath ? BasePath->size() : 0); 782 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); 783 auto *E = 784 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, 785 RParenLoc, AngleBrackets); 786 if (PathSize) 787 std::uninitialized_copy_n(BasePath->data(), BasePath->size(), 788 E->getTrailingObjects<CXXBaseSpecifier *>()); 789 return E; 790 } 791 792 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C, 793 unsigned PathSize) { 794 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); 795 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize); 796 } 797 798 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T, 799 ExprValueKind VK, 800 CastKind K, Expr *Op, 801 const CXXCastPath *BasePath, 802 TypeSourceInfo *WrittenTy, 803 SourceLocation L, 804 SourceLocation RParenLoc, 805 SourceRange AngleBrackets) { 806 unsigned PathSize = (BasePath ? BasePath->size() : 0); 807 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); 808 auto *E = 809 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, 810 RParenLoc, AngleBrackets); 811 if (PathSize) 812 std::uninitialized_copy_n(BasePath->data(), BasePath->size(), 813 E->getTrailingObjects<CXXBaseSpecifier *>()); 814 return E; 815 } 816 817 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C, 818 unsigned PathSize) { 819 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); 820 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize); 821 } 822 823 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven 824 /// to always be null. For example: 825 /// 826 /// struct A { }; 827 /// struct B final : A { }; 828 /// struct C { }; 829 /// 830 /// C *f(B* b) { return dynamic_cast<C*>(b); } 831 bool CXXDynamicCastExpr::isAlwaysNull() const 832 { 833 QualType SrcType = getSubExpr()->getType(); 834 QualType DestType = getType(); 835 836 if (const auto *SrcPTy = SrcType->getAs<PointerType>()) { 837 SrcType = SrcPTy->getPointeeType(); 838 DestType = DestType->castAs<PointerType>()->getPointeeType(); 839 } 840 841 if (DestType->isVoidType()) 842 return false; 843 844 const auto *SrcRD = 845 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl()); 846 847 if (!SrcRD->hasAttr<FinalAttr>()) 848 return false; 849 850 const auto *DestRD = 851 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl()); 852 853 return !DestRD->isDerivedFrom(SrcRD); 854 } 855 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)); 865 auto *E = 866 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, 867 RParenLoc, AngleBrackets); 868 if (PathSize) 869 std::uninitialized_copy_n(BasePath->data(), BasePath->size(), 870 E->getTrailingObjects<CXXBaseSpecifier *>()); 871 return E; 872 } 873 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); 878 } 879 880 CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T, 881 ExprValueKind VK, Expr *Op, 882 TypeSourceInfo *WrittenTy, 883 SourceLocation L, 884 SourceLocation RParenLoc, 885 SourceRange AngleBrackets) { 886 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets); 887 } 888 889 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) { 890 return new (C) CXXConstCastExpr(EmptyShell()); 891 } 892 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)); 900 auto *E = 901 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R); 902 if (PathSize) 903 std::uninitialized_copy_n(BasePath->data(), BasePath->size(), 904 E->getTrailingObjects<CXXBaseSpecifier *>()); 905 return E; 906 } 907 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); 912 } 913 914 SourceLocation CXXFunctionalCastExpr::getBeginLoc() const { 915 return getTypeInfoAsWritten()->getTypeLoc().getBeginLoc(); 916 } 917 918 SourceLocation CXXFunctionalCastExpr::getEndLoc() const { 919 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getEndLoc(); 920 } 921 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) {} 929 930 UserDefinedLiteral::UserDefinedLiteral(unsigned NumArgs, EmptyShell Empty) 931 : CallExpr(UserDefinedLiteralClass, /*NumPreArgs=*/0, NumArgs, Empty) {} 932 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); 945 } 946 947 UserDefinedLiteral *UserDefinedLiteral::CreateEmpty(const ASTContext &Ctx, 948 unsigned NumArgs, 949 EmptyShell Empty) { 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); 956 } 957 958 UserDefinedLiteral::LiteralOperatorKind 959 UserDefinedLiteral::getLiteralOperatorKind() const { 960 if (getNumArgs() == 0) 961 return LOK_Template; 962 if (getNumArgs() == 2) 963 return LOK_String; 964 965 assert(getNumArgs() == 1 && "unexpected #args in literal operator call"); 966 QualType ParamTy = 967 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType(); 968 if (ParamTy->isPointerType()) 969 return LOK_Raw; 970 if (ParamTy->isAnyCharacterType()) 971 return LOK_Character; 972 if (ParamTy->isIntegerType()) 973 return LOK_Integer; 974 if (ParamTy->isFloatingType()) 975 return LOK_Floating; 976 977 llvm_unreachable("unknown kind of literal operator"); 978 } 979 980 Expr *UserDefinedLiteral::getCookedLiteral() { 981 #ifndef NDEBUG 982 LiteralOperatorKind LOK = getLiteralOperatorKind(); 983 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal"); 984 #endif 985 return getArg(0); 986 } 987 988 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const { 989 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier(); 990 } 991 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() 997 ? VK_XValue 998 : VK_RValue, 999 /*FIXME*/ OK_Ordinary, false, false, false, false), 1000 Field(Field), UsedContext(UsedContext) { 1001 CXXDefaultInitExprBits.Loc = Loc; 1002 assert(Field->hasInClassInitializer()); 1003 } 1004 1005 CXXTemporary *CXXTemporary::Create(const ASTContext &C, 1006 const CXXDestructorDecl *Destructor) { 1007 return new (C) CXXTemporary(Destructor); 1008 } 1009 1010 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C, 1011 CXXTemporary *Temp, 1012 Expr* SubExpr) { 1013 assert((SubExpr->getType()->isRecordType() || 1014 SubExpr->getType()->isArrayType()) && 1015 "Expression bound to a temporary must have record or array type!"); 1016 1017 return new (C) CXXBindTemporaryExpr(Temp, SubExpr); 1018 } 1019 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) 1025 : CXXConstructExpr( 1026 CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(), 1027 Cons, /* Elidable=*/false, Args, HadMultipleCandidates, 1028 ListInitialization, StdInitListInitialization, ZeroInitialization, 1029 CXXConstructExpr::CK_Complete, ParenOrBraceRange), 1030 TSI(TSI) {} 1031 1032 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(EmptyShell Empty, 1033 unsigned NumArgs) 1034 : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty, NumArgs) {} 1035 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()); 1042 void *Mem = 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); 1048 } 1049 1050 CXXTemporaryObjectExpr * 1051 CXXTemporaryObjectExpr::CreateEmpty(const ASTContext &Ctx, unsigned NumArgs) { 1052 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs); 1053 void *Mem = 1054 Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects, 1055 alignof(CXXTemporaryObjectExpr)); 1056 return new (Mem) CXXTemporaryObjectExpr(EmptyShell(), NumArgs); 1057 } 1058 1059 SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const { 1060 return getTypeSourceInfo()->getTypeLoc().getBeginLoc(); 1061 } 1062 1063 SourceLocation CXXTemporaryObjectExpr::getEndLoc() const { 1064 SourceLocation Loc = getParenOrBraceRange().getEnd(); 1065 if (Loc.isInvalid() && getNumArgs()) 1066 Loc = getArg(getNumArgs() - 1)->getEndLoc(); 1067 return Loc; 1068 } 1069 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); 1083 } 1084 1085 CXXConstructExpr *CXXConstructExpr::CreateEmpty(const ASTContext &Ctx, 1086 unsigned NumArgs) { 1087 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs); 1088 void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects, 1089 alignof(CXXConstructExpr)); 1090 return new (Mem) 1091 CXXConstructExpr(CXXConstructExprClass, EmptyShell(), NumArgs); 1092 } 1093 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; 1112 1113 Stmt **TrailingArgs = getTrailingArgs(); 1114 for (unsigned I = 0, N = Args.size(); I != N; ++I) { 1115 assert(Args[I] && "NULL argument in CXXConstructExpr!"); 1116 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; 1123 1124 TrailingArgs[I] = Args[I]; 1125 } 1126 } 1127 1128 CXXConstructExpr::CXXConstructExpr(StmtClass SC, EmptyShell Empty, 1129 unsigned NumArgs) 1130 : Expr(SC, Empty), NumArgs(NumArgs) {} 1131 1132 LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit, 1133 LambdaCaptureKind Kind, VarDecl *Var, 1134 SourceLocation EllipsisLoc) 1135 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) { 1136 unsigned Bits = 0; 1137 if (Implicit) 1138 Bits |= Capture_Implicit; 1139 1140 switch (Kind) { 1141 case LCK_StarThis: 1142 Bits |= Capture_ByCopy; 1143 LLVM_FALLTHROUGH; 1144 case LCK_This: 1145 assert(!Var && "'this' capture cannot have a variable!"); 1146 Bits |= Capture_This; 1147 break; 1148 1149 case LCK_ByCopy: 1150 Bits |= Capture_ByCopy; 1151 LLVM_FALLTHROUGH; 1152 case LCK_ByRef: 1153 assert(Var && "capture must have a variable!"); 1154 break; 1155 case LCK_VLAType: 1156 assert(!Var && "VLA type capture cannot have a variable!"); 1157 break; 1158 } 1159 DeclAndBits.setInt(Bits); 1160 } 1161 1162 LambdaCaptureKind LambdaCapture::getCaptureKind() const { 1163 if (capturesVLAType()) 1164 return LCK_VLAType; 1165 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy; 1166 if (capturesThis()) 1167 return CapByCopy ? LCK_StarThis : LCK_This; 1168 return CapByCopy ? LCK_ByCopy : LCK_ByRef; 1169 } 1170 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(); 1188 1189 // FIXME: Propagate "has unexpanded parameter pack" bit. 1190 1191 // Copy captures. 1192 const ASTContext &Context = Class->getASTContext(); 1193 Data.NumCaptures = NumCaptures; 1194 Data.NumExplicitCaptures = 0; 1195 Data.Captures = 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; 1201 1202 *ToCapture++ = Captures[I]; 1203 } 1204 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]; 1209 1210 // Copy the body of the lambda. 1211 *Stored++ = getCallOperator()->getBody(); 1212 } 1213 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); 1223 1224 unsigned Size = totalSizeToAlloc<Stmt *>(Captures.size() + 1); 1225 void *Mem = Context.Allocate(Size); 1226 return new (Mem) 1227 LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc, 1228 Captures, ExplicitParams, ExplicitResultType, CaptureInits, 1229 ClosingBrace, ContainsUnexpandedParameterPack); 1230 } 1231 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); 1237 } 1238 1239 bool LambdaExpr::isInitCapture(const LambdaCapture *C) const { 1240 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() && 1241 (getCallOperator() == C->getCapturedVar()->getDeclContext())); 1242 } 1243 1244 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const { 1245 return getLambdaClass()->getLambdaData().Captures; 1246 } 1247 1248 LambdaExpr::capture_iterator LambdaExpr::capture_end() const { 1249 return capture_begin() + NumCaptures; 1250 } 1251 1252 LambdaExpr::capture_range LambdaExpr::captures() const { 1253 return capture_range(capture_begin(), capture_end()); 1254 } 1255 1256 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const { 1257 return capture_begin(); 1258 } 1259 1260 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const { 1261 struct CXXRecordDecl::LambdaDefinitionData &Data 1262 = getLambdaClass()->getLambdaData(); 1263 return Data.Captures + Data.NumExplicitCaptures; 1264 } 1265 1266 LambdaExpr::capture_range LambdaExpr::explicit_captures() const { 1267 return capture_range(explicit_capture_begin(), explicit_capture_end()); 1268 } 1269 1270 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const { 1271 return explicit_capture_end(); 1272 } 1273 1274 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const { 1275 return capture_end(); 1276 } 1277 1278 LambdaExpr::capture_range LambdaExpr::implicit_captures() const { 1279 return capture_range(implicit_capture_begin(), implicit_capture_end()); 1280 } 1281 1282 CXXRecordDecl *LambdaExpr::getLambdaClass() const { 1283 return getType()->getAsCXXRecordDecl(); 1284 } 1285 1286 CXXMethodDecl *LambdaExpr::getCallOperator() const { 1287 CXXRecordDecl *Record = getLambdaClass(); 1288 return Record->getLambdaCallOperator(); 1289 } 1290 1291 FunctionTemplateDecl *LambdaExpr::getDependentCallOperator() const { 1292 CXXRecordDecl *Record = getLambdaClass(); 1293 return Record->getDependentLambdaCallOperator(); 1294 } 1295 1296 TemplateParameterList *LambdaExpr::getTemplateParameterList() const { 1297 CXXRecordDecl *Record = getLambdaClass(); 1298 return Record->getGenericLambdaTemplateParameterList(); 1299 } 1300 1301 ArrayRef<NamedDecl *> LambdaExpr::getExplicitTemplateParameters() const { 1302 const CXXRecordDecl *Record = getLambdaClass(); 1303 return Record->getLambdaExplicitTemplateParameters(); 1304 } 1305 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(); 1313 1314 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]); 1315 } 1316 1317 bool LambdaExpr::isMutable() const { 1318 return !getCallOperator()->isConst(); 1319 } 1320 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]; 1329 } 1330 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)); 1336 return new (buffer) 1337 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects); 1338 } 1339 1340 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects) 1341 : FullExpr(ExprWithCleanupsClass, empty) { 1342 ExprWithCleanupsBits.NumObjects = numObjects; 1343 } 1344 1345 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, 1346 EmptyShell empty, 1347 unsigned numObjects) { 1348 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects), 1349 alignof(ExprWithCleanups)); 1350 return new (buffer) ExprWithCleanups(empty, numObjects); 1351 } 1352 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() 1360 ? VK_LValue 1361 : TSI->getType()->isRValueReferenceType() ? VK_XValue 1362 : VK_RValue), 1363 OK_Ordinary, 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; 1373 1374 StoredArgs[I] = Args[I]; 1375 } 1376 } 1377 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); 1383 } 1384 1385 CXXUnresolvedConstructExpr * 1386 CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &Context, 1387 unsigned NumArgs) { 1388 void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(NumArgs)); 1389 return new (Mem) CXXUnresolvedConstructExpr(EmptyShell(), NumArgs); 1390 } 1391 1392 SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const { 1393 return TSI->getTypeLoc().getBeginLoc(); 1394 } 1395 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; 1416 1417 if (TemplateArgs) { 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( 1428 TemplateKWLoc); 1429 } 1430 1431 if (hasFirstQualifierFoundInScope()) 1432 *getTrailingObjects<NamedDecl *>() = FirstQualifierFoundInScope; 1433 } 1434 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; 1443 } 1444 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; 1455 1456 unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo, 1457 TemplateArgumentLoc, NamedDecl *>( 1458 HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope); 1459 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); 1464 } 1465 1466 CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::CreateEmpty( 1467 const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo, 1468 unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope) { 1469 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo); 1470 1471 unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo, 1472 TemplateArgumentLoc, NamedDecl *>( 1473 HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope); 1474 1475 void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr)); 1476 return new (Mem) CXXDependentScopeMemberExpr( 1477 EmptyShell(), HasTemplateKWAndArgsInfo, HasFirstQualifierFoundInScope); 1478 } 1479 1480 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin, 1481 UnresolvedSetIterator end) { 1482 do { 1483 NamedDecl *decl = *begin; 1484 if (isa<UnresolvedUsingValueDecl>(decl)) 1485 return false; 1486 1487 // Unresolved member expressions should only contain methods and 1488 // method templates. 1489 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction()) 1490 ->isStatic()) 1491 return false; 1492 } while (++begin != end); 1493 1494 return true; 1495 } 1496 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) 1504 : OverloadExpr( 1505 UnresolvedMemberExprClass, Context, QualifierLoc, TemplateKWLoc, 1506 MemberNameInfo, TemplateArgs, Begin, End, 1507 // Dependent 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; 1517 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); 1522 } 1523 1524 UnresolvedMemberExpr::UnresolvedMemberExpr(EmptyShell Empty, 1525 unsigned NumResults, 1526 bool HasTemplateKWAndArgsInfo) 1527 : OverloadExpr(UnresolvedMemberExprClass, Empty, NumResults, 1528 HasTemplateKWAndArgsInfo) {} 1529 1530 bool UnresolvedMemberExpr::isImplicitAccess() const { 1531 if (!Base) 1532 return true; 1533 1534 return cast<Expr>(Base)->isImplicitCXXThis(); 1535 } 1536 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); 1554 } 1555 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)); 1564 return new (Mem) 1565 UnresolvedMemberExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo); 1566 } 1567 1568 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() { 1569 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this. 1570 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 1573 // lookup. 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"); 1581 } 1582 // Otherwise the naming class must have been the base class. 1583 else { 1584 QualType BaseType = getBaseType().getNonReferenceType(); 1585 if (isArrow()) 1586 BaseType = BaseType->castAs<PointerType>()->getPointeeType(); 1587 1588 Record = BaseType->getAsCXXRecordDecl(); 1589 assert(Record && "base of member expression does not name record"); 1590 } 1591 1592 return Record; 1593 } 1594 1595 SizeOfPackExpr * 1596 SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc, 1597 NamedDecl *Pack, SourceLocation PackLoc, 1598 SourceLocation RParenLoc, 1599 Optional<unsigned> Length, 1600 ArrayRef<TemplateArgument> PartialArgs) { 1601 void *Storage = 1602 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size())); 1603 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack, 1604 PackLoc, RParenLoc, Length, PartialArgs); 1605 } 1606 1607 SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context, 1608 unsigned NumPartialArgs) { 1609 void *Storage = 1610 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs)); 1611 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs); 1612 } 1613 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) {} 1624 1625 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const { 1626 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments)); 1627 } 1628 1629 FunctionParmPackExpr::FunctionParmPackExpr(QualType T, VarDecl *ParamPack, 1630 SourceLocation NameLoc, 1631 unsigned NumParams, 1632 VarDecl *const *Params) 1633 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true, 1634 true, true), 1635 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) { 1636 if (Params) 1637 std::uninitialized_copy(Params, Params + NumParams, 1638 getTrailingObjects<VarDecl *>()); 1639 } 1640 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()); 1647 } 1648 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); 1654 } 1655 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. 1659 if (!ExtendedBy) 1660 return; 1661 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 *>(); 1667 State = ES; 1668 } 1669 1670 auto ES = State.get<ExtraState *>(); 1671 ES->ExtendingDecl = ExtendedBy; 1672 ES->ManglingNumber = ManglingNumber; 1673 } 1674 1675 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind, 1676 ArrayRef<TypeSourceInfo *> Args, 1677 SourceLocation RParenLoc, 1678 bool Value) 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(); 1688 1689 auto **ToArgs = getTrailingObjects<TypeSourceInfo *>(); 1690 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); 1698 1699 ToArgs[I] = Args[I]; 1700 } 1701 } 1702 1703 TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T, 1704 SourceLocation Loc, 1705 TypeTrait Kind, 1706 ArrayRef<TypeSourceInfo *> Args, 1707 SourceLocation RParenLoc, 1708 bool Value) { 1709 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size())); 1710 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value); 1711 } 1712 1713 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C, 1714 unsigned NumArgs) { 1715 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs)); 1716 return new (Mem) TypeTraitExpr(EmptyShell()); 1717 } 1718 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) {} 1725 1726 CUDAKernelCallExpr::CUDAKernelCallExpr(unsigned NumArgs, EmptyShell Empty) 1727 : CallExpr(CUDAKernelCallExprClass, /*NumPreArgs=*/END_PREARG, NumArgs, 1728 Empty) {} 1729 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); 1741 } 1742 1743 CUDAKernelCallExpr *CUDAKernelCallExpr::CreateEmpty(const ASTContext &Ctx, 1744 unsigned NumArgs, 1745 EmptyShell Empty) { 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); 1752 } 1753 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()) { 1769 1770 setTemplateArguments(ArgsAsWritten, ConvertedArgs); 1771 } 1772 1773 ConceptSpecializationExpr::ConceptSpecializationExpr(EmptyShell Empty, 1774 unsigned NumTemplateArgs) 1775 : Expr(ConceptSpecializationExprClass, Empty), 1776 NumTemplateArgs(NumTemplateArgs) { } 1777 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) 1794 break; 1795 } 1796 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"); 1806 } 1807 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); 1823 } 1824 1825 ConceptSpecializationExpr * 1826 ConceptSpecializationExpr::Create(ASTContext &C, EmptyShell Empty, 1827 unsigned NumTemplateArgs) { 1828 void *Buffer = C.Allocate(totalSizeToAlloc<TemplateArgument>( 1829 NumTemplateArgs)); 1830 return new (Buffer) ConceptSpecializationExpr(Empty, NumTemplateArgs); 1831 } 1832