1 //===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the subclesses of Expr class declared in ExprCXX.h 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/ASTContext.h" 15 #include "clang/AST/Attr.h" 16 #include "clang/AST/DeclCXX.h" 17 #include "clang/AST/DeclTemplate.h" 18 #include "clang/AST/ExprCXX.h" 19 #include "clang/AST/TypeLoc.h" 20 #include "clang/Basic/IdentifierTable.h" 21 using namespace clang; 22 23 24 //===----------------------------------------------------------------------===// 25 // Child Iterators for iterating over subexpressions/substatements 26 //===----------------------------------------------------------------------===// 27 28 bool CXXTypeidExpr::isPotentiallyEvaluated() const { 29 if (isTypeOperand()) 30 return false; 31 32 // C++11 [expr.typeid]p3: 33 // When typeid is applied to an expression other than a glvalue of 34 // polymorphic class type, [...] the expression is an unevaluated operand. 35 const Expr *E = getExprOperand(); 36 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl()) 37 if (RD->isPolymorphic() && E->isGLValue()) 38 return true; 39 40 return false; 41 } 42 43 QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const { 44 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 45 Qualifiers Quals; 46 return Context.getUnqualifiedArrayType( 47 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals); 48 } 49 50 QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const { 51 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)"); 52 Qualifiers Quals; 53 return Context.getUnqualifiedArrayType( 54 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals); 55 } 56 57 // static 58 const UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT, 59 bool *RDHasMultipleGUIDsPtr) { 60 // Optionally remove one level of pointer, reference or array indirection. 61 const Type *Ty = QT.getTypePtr(); 62 if (QT->isPointerType() || QT->isReferenceType()) 63 Ty = QT->getPointeeType().getTypePtr(); 64 else if (QT->isArrayType()) 65 Ty = Ty->getBaseElementTypeUnsafe(); 66 67 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); 68 if (!RD) 69 return nullptr; 70 71 if (const UuidAttr *Uuid = RD->getMostRecentDecl()->getAttr<UuidAttr>()) 72 return Uuid; 73 74 // __uuidof can grab UUIDs from template arguments. 75 if (const ClassTemplateSpecializationDecl *CTSD = 76 dyn_cast<ClassTemplateSpecializationDecl>(RD)) { 77 const TemplateArgumentList &TAL = CTSD->getTemplateArgs(); 78 const UuidAttr *UuidForRD = nullptr; 79 80 for (const TemplateArgument &TA : TAL.asArray()) { 81 bool SeenMultipleGUIDs = false; 82 83 const UuidAttr *UuidForTA = nullptr; 84 if (TA.getKind() == TemplateArgument::Type) 85 UuidForTA = GetUuidAttrOfType(TA.getAsType(), &SeenMultipleGUIDs); 86 else if (TA.getKind() == TemplateArgument::Declaration) 87 UuidForTA = 88 GetUuidAttrOfType(TA.getAsDecl()->getType(), &SeenMultipleGUIDs); 89 90 // If the template argument has a UUID, there are three cases: 91 // - This is the first UUID seen for this RecordDecl. 92 // - This is a different UUID than previously seen for this RecordDecl. 93 // - This is the same UUID than previously seen for this RecordDecl. 94 if (UuidForTA) { 95 if (!UuidForRD) 96 UuidForRD = UuidForTA; 97 else if (UuidForRD != UuidForTA) 98 SeenMultipleGUIDs = true; 99 } 100 101 // Seeing multiple UUIDs means that we couldn't find a UUID 102 if (SeenMultipleGUIDs) { 103 if (RDHasMultipleGUIDsPtr) 104 *RDHasMultipleGUIDsPtr = true; 105 return nullptr; 106 } 107 } 108 109 return UuidForRD; 110 } 111 112 return nullptr; 113 } 114 115 StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const { 116 StringRef Uuid; 117 if (isTypeOperand()) 118 Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand(Context))->getGuid(); 119 else { 120 // Special case: __uuidof(0) means an all-zero GUID. 121 Expr *Op = getExprOperand(); 122 if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) 123 Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid(); 124 else 125 Uuid = "00000000-0000-0000-0000-000000000000"; 126 } 127 return Uuid; 128 } 129 130 // CXXScalarValueInitExpr 131 SourceLocation CXXScalarValueInitExpr::getLocStart() const { 132 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc; 133 } 134 135 // CXXNewExpr 136 CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew, 137 FunctionDecl *operatorNew, FunctionDecl *operatorDelete, 138 bool usualArrayDeleteWantsSize, 139 ArrayRef<Expr*> placementArgs, 140 SourceRange typeIdParens, Expr *arraySize, 141 InitializationStyle initializationStyle, 142 Expr *initializer, QualType ty, 143 TypeSourceInfo *allocatedTypeInfo, 144 SourceRange Range, SourceRange directInitRange) 145 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary, 146 ty->isDependentType(), ty->isDependentType(), 147 ty->isInstantiationDependentType(), 148 ty->containsUnexpandedParameterPack()), 149 SubExprs(nullptr), OperatorNew(operatorNew), OperatorDelete(operatorDelete), 150 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens), 151 Range(Range), DirectInitRange(directInitRange), 152 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) { 153 assert((initializer != nullptr || initializationStyle == NoInit) && 154 "Only NoInit can have no initializer."); 155 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0; 156 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(), 157 initializer != nullptr); 158 unsigned i = 0; 159 if (Array) { 160 if (arraySize->isInstantiationDependent()) 161 ExprBits.InstantiationDependent = true; 162 163 if (arraySize->containsUnexpandedParameterPack()) 164 ExprBits.ContainsUnexpandedParameterPack = true; 165 166 SubExprs[i++] = arraySize; 167 } 168 169 if (initializer) { 170 if (initializer->isInstantiationDependent()) 171 ExprBits.InstantiationDependent = true; 172 173 if (initializer->containsUnexpandedParameterPack()) 174 ExprBits.ContainsUnexpandedParameterPack = true; 175 176 SubExprs[i++] = initializer; 177 } 178 179 for (unsigned j = 0; j != placementArgs.size(); ++j) { 180 if (placementArgs[j]->isInstantiationDependent()) 181 ExprBits.InstantiationDependent = true; 182 if (placementArgs[j]->containsUnexpandedParameterPack()) 183 ExprBits.ContainsUnexpandedParameterPack = true; 184 185 SubExprs[i++] = placementArgs[j]; 186 } 187 188 switch (getInitializationStyle()) { 189 case CallInit: 190 this->Range.setEnd(DirectInitRange.getEnd()); break; 191 case ListInit: 192 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break; 193 default: 194 if (TypeIdParens.isValid()) 195 this->Range.setEnd(TypeIdParens.getEnd()); 196 break; 197 } 198 } 199 200 void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray, 201 unsigned numPlaceArgs, bool hasInitializer){ 202 assert(SubExprs == nullptr && "SubExprs already allocated"); 203 Array = isArray; 204 NumPlacementArgs = numPlaceArgs; 205 206 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs; 207 SubExprs = new (C) Stmt*[TotalSize]; 208 } 209 210 bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const { 211 return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow( 212 Ctx) && 213 !getOperatorNew()->isReservedGlobalPlacementOperator(); 214 } 215 216 // CXXDeleteExpr 217 QualType CXXDeleteExpr::getDestroyedType() const { 218 const Expr *Arg = getArgument(); 219 // The type-to-delete may not be a pointer if it's a dependent type. 220 const QualType ArgType = Arg->getType(); 221 222 if (ArgType->isDependentType() && !ArgType->isPointerType()) 223 return QualType(); 224 225 return ArgType->getAs<PointerType>()->getPointeeType(); 226 } 227 228 // CXXPseudoDestructorExpr 229 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info) 230 : Type(Info) 231 { 232 Location = Info->getTypeLoc().getLocalSourceRange().getBegin(); 233 } 234 235 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context, 236 Expr *Base, bool isArrow, SourceLocation OperatorLoc, 237 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType, 238 SourceLocation ColonColonLoc, SourceLocation TildeLoc, 239 PseudoDestructorTypeStorage DestroyedType) 240 : Expr(CXXPseudoDestructorExprClass, 241 Context.getPointerType(Context.getFunctionType( 242 Context.VoidTy, None, 243 FunctionProtoType::ExtProtoInfo( 244 Context.getDefaultCallingConvention(false, true)))), 245 VK_RValue, OK_Ordinary, 246 /*isTypeDependent=*/(Base->isTypeDependent() || 247 (DestroyedType.getTypeSourceInfo() && 248 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())), 249 /*isValueDependent=*/Base->isValueDependent(), 250 (Base->isInstantiationDependent() || 251 (QualifierLoc && 252 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) || 253 (ScopeType && 254 ScopeType->getType()->isInstantiationDependentType()) || 255 (DestroyedType.getTypeSourceInfo() && 256 DestroyedType.getTypeSourceInfo()->getType() 257 ->isInstantiationDependentType())), 258 // ContainsUnexpandedParameterPack 259 (Base->containsUnexpandedParameterPack() || 260 (QualifierLoc && 261 QualifierLoc.getNestedNameSpecifier() 262 ->containsUnexpandedParameterPack()) || 263 (ScopeType && 264 ScopeType->getType()->containsUnexpandedParameterPack()) || 265 (DestroyedType.getTypeSourceInfo() && 266 DestroyedType.getTypeSourceInfo()->getType() 267 ->containsUnexpandedParameterPack()))), 268 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow), 269 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc), 270 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc), 271 DestroyedType(DestroyedType) { } 272 273 QualType CXXPseudoDestructorExpr::getDestroyedType() const { 274 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo()) 275 return TInfo->getType(); 276 277 return QualType(); 278 } 279 280 SourceLocation CXXPseudoDestructorExpr::getLocEnd() const { 281 SourceLocation End = DestroyedType.getLocation(); 282 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo()) 283 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd(); 284 return End; 285 } 286 287 // UnresolvedLookupExpr 288 UnresolvedLookupExpr * 289 UnresolvedLookupExpr::Create(const ASTContext &C, 290 CXXRecordDecl *NamingClass, 291 NestedNameSpecifierLoc QualifierLoc, 292 SourceLocation TemplateKWLoc, 293 const DeclarationNameInfo &NameInfo, 294 bool ADL, 295 const TemplateArgumentListInfo *Args, 296 UnresolvedSetIterator Begin, 297 UnresolvedSetIterator End) 298 { 299 assert(Args || TemplateKWLoc.isValid()); 300 unsigned num_args = Args ? Args->size() : 0; 301 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) + 302 ASTTemplateKWAndArgsInfo::sizeFor(num_args)); 303 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc, 304 TemplateKWLoc, NameInfo, 305 ADL, /*Overload*/ true, Args, 306 Begin, End); 307 } 308 309 UnresolvedLookupExpr * 310 UnresolvedLookupExpr::CreateEmpty(const ASTContext &C, 311 bool HasTemplateKWAndArgsInfo, 312 unsigned NumTemplateArgs) { 313 std::size_t size = sizeof(UnresolvedLookupExpr); 314 if (HasTemplateKWAndArgsInfo) 315 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs); 316 317 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>()); 318 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell()); 319 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo; 320 return E; 321 } 322 323 OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C, 324 NestedNameSpecifierLoc QualifierLoc, 325 SourceLocation TemplateKWLoc, 326 const DeclarationNameInfo &NameInfo, 327 const TemplateArgumentListInfo *TemplateArgs, 328 UnresolvedSetIterator Begin, 329 UnresolvedSetIterator End, 330 bool KnownDependent, 331 bool KnownInstantiationDependent, 332 bool KnownContainsUnexpandedParameterPack) 333 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent, 334 KnownDependent, 335 (KnownInstantiationDependent || 336 NameInfo.isInstantiationDependent() || 337 (QualifierLoc && 338 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())), 339 (KnownContainsUnexpandedParameterPack || 340 NameInfo.containsUnexpandedParameterPack() || 341 (QualifierLoc && 342 QualifierLoc.getNestedNameSpecifier() 343 ->containsUnexpandedParameterPack()))), 344 NameInfo(NameInfo), QualifierLoc(QualifierLoc), 345 Results(nullptr), NumResults(End - Begin), 346 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr || 347 TemplateKWLoc.isValid()) { 348 NumResults = End - Begin; 349 if (NumResults) { 350 // Determine whether this expression is type-dependent. 351 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) { 352 if ((*I)->getDeclContext()->isDependentContext() || 353 isa<UnresolvedUsingValueDecl>(*I)) { 354 ExprBits.TypeDependent = true; 355 ExprBits.ValueDependent = true; 356 ExprBits.InstantiationDependent = true; 357 } 358 } 359 360 Results = static_cast<DeclAccessPair *>( 361 C.Allocate(sizeof(DeclAccessPair) * NumResults, 362 llvm::alignOf<DeclAccessPair>())); 363 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair)); 364 } 365 366 // If we have explicit template arguments, check for dependent 367 // template arguments and whether they contain any unexpanded pack 368 // expansions. 369 if (TemplateArgs) { 370 bool Dependent = false; 371 bool InstantiationDependent = false; 372 bool ContainsUnexpandedParameterPack = false; 373 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs, 374 Dependent, 375 InstantiationDependent, 376 ContainsUnexpandedParameterPack); 377 378 if (Dependent) { 379 ExprBits.TypeDependent = true; 380 ExprBits.ValueDependent = true; 381 } 382 if (InstantiationDependent) 383 ExprBits.InstantiationDependent = true; 384 if (ContainsUnexpandedParameterPack) 385 ExprBits.ContainsUnexpandedParameterPack = true; 386 } else if (TemplateKWLoc.isValid()) { 387 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc); 388 } 389 390 if (isTypeDependent()) 391 setType(C.DependentTy); 392 } 393 394 void OverloadExpr::initializeResults(const ASTContext &C, 395 UnresolvedSetIterator Begin, 396 UnresolvedSetIterator End) { 397 assert(!Results && "Results already initialized!"); 398 NumResults = End - Begin; 399 if (NumResults) { 400 Results = static_cast<DeclAccessPair *>( 401 C.Allocate(sizeof(DeclAccessPair) * NumResults, 402 403 llvm::alignOf<DeclAccessPair>())); 404 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair)); 405 } 406 } 407 408 CXXRecordDecl *OverloadExpr::getNamingClass() const { 409 if (isa<UnresolvedLookupExpr>(this)) 410 return cast<UnresolvedLookupExpr>(this)->getNamingClass(); 411 else 412 return cast<UnresolvedMemberExpr>(this)->getNamingClass(); 413 } 414 415 // DependentScopeDeclRefExpr 416 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T, 417 NestedNameSpecifierLoc QualifierLoc, 418 SourceLocation TemplateKWLoc, 419 const DeclarationNameInfo &NameInfo, 420 const TemplateArgumentListInfo *Args) 421 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary, 422 true, true, 423 (NameInfo.isInstantiationDependent() || 424 (QualifierLoc && 425 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())), 426 (NameInfo.containsUnexpandedParameterPack() || 427 (QualifierLoc && 428 QualifierLoc.getNestedNameSpecifier() 429 ->containsUnexpandedParameterPack()))), 430 QualifierLoc(QualifierLoc), NameInfo(NameInfo), 431 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid()) 432 { 433 if (Args) { 434 bool Dependent = true; 435 bool InstantiationDependent = true; 436 bool ContainsUnexpandedParameterPack 437 = ExprBits.ContainsUnexpandedParameterPack; 438 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args, 439 Dependent, 440 InstantiationDependent, 441 ContainsUnexpandedParameterPack); 442 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack; 443 } else if (TemplateKWLoc.isValid()) { 444 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc); 445 } 446 } 447 448 DependentScopeDeclRefExpr * 449 DependentScopeDeclRefExpr::Create(const ASTContext &C, 450 NestedNameSpecifierLoc QualifierLoc, 451 SourceLocation TemplateKWLoc, 452 const DeclarationNameInfo &NameInfo, 453 const TemplateArgumentListInfo *Args) { 454 assert(QualifierLoc && "should be created for dependent qualifiers"); 455 std::size_t size = sizeof(DependentScopeDeclRefExpr); 456 if (Args) 457 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size()); 458 else if (TemplateKWLoc.isValid()) 459 size += ASTTemplateKWAndArgsInfo::sizeFor(0); 460 void *Mem = C.Allocate(size); 461 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc, 462 TemplateKWLoc, NameInfo, Args); 463 } 464 465 DependentScopeDeclRefExpr * 466 DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C, 467 bool HasTemplateKWAndArgsInfo, 468 unsigned NumTemplateArgs) { 469 std::size_t size = sizeof(DependentScopeDeclRefExpr); 470 if (HasTemplateKWAndArgsInfo) 471 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs); 472 void *Mem = C.Allocate(size); 473 DependentScopeDeclRefExpr *E 474 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(), 475 SourceLocation(), 476 DeclarationNameInfo(), nullptr); 477 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo; 478 return E; 479 } 480 481 SourceLocation CXXConstructExpr::getLocStart() const { 482 if (isa<CXXTemporaryObjectExpr>(this)) 483 return cast<CXXTemporaryObjectExpr>(this)->getLocStart(); 484 return Loc; 485 } 486 487 SourceLocation CXXConstructExpr::getLocEnd() const { 488 if (isa<CXXTemporaryObjectExpr>(this)) 489 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd(); 490 491 if (ParenOrBraceRange.isValid()) 492 return ParenOrBraceRange.getEnd(); 493 494 SourceLocation End = Loc; 495 for (unsigned I = getNumArgs(); I > 0; --I) { 496 const Expr *Arg = getArg(I-1); 497 if (!Arg->isDefaultArgument()) { 498 SourceLocation NewEnd = Arg->getLocEnd(); 499 if (NewEnd.isValid()) { 500 End = NewEnd; 501 break; 502 } 503 } 504 } 505 506 return End; 507 } 508 509 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const { 510 OverloadedOperatorKind Kind = getOperator(); 511 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 512 if (getNumArgs() == 1) 513 // Prefix operator 514 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd()); 515 else 516 // Postfix operator 517 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc()); 518 } else if (Kind == OO_Arrow) { 519 return getArg(0)->getSourceRange(); 520 } else if (Kind == OO_Call) { 521 return SourceRange(getArg(0)->getLocStart(), getRParenLoc()); 522 } else if (Kind == OO_Subscript) { 523 return SourceRange(getArg(0)->getLocStart(), getRParenLoc()); 524 } else if (getNumArgs() == 1) { 525 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd()); 526 } else if (getNumArgs() == 2) { 527 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd()); 528 } else { 529 return getOperatorLoc(); 530 } 531 } 532 533 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const { 534 const Expr *Callee = getCallee()->IgnoreParens(); 535 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee)) 536 return MemExpr->getBase(); 537 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee)) 538 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI) 539 return BO->getLHS(); 540 541 // FIXME: Will eventually need to cope with member pointers. 542 return nullptr; 543 } 544 545 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const { 546 if (const MemberExpr *MemExpr = 547 dyn_cast<MemberExpr>(getCallee()->IgnoreParens())) 548 return cast<CXXMethodDecl>(MemExpr->getMemberDecl()); 549 550 // FIXME: Will eventually need to cope with member pointers. 551 return nullptr; 552 } 553 554 555 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const { 556 Expr* ThisArg = getImplicitObjectArgument(); 557 if (!ThisArg) 558 return nullptr; 559 560 if (ThisArg->getType()->isAnyPointerType()) 561 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl(); 562 563 return ThisArg->getType()->getAsCXXRecordDecl(); 564 } 565 566 567 //===----------------------------------------------------------------------===// 568 // Named casts 569 //===----------------------------------------------------------------------===// 570 571 /// getCastName - Get the name of the C++ cast being used, e.g., 572 /// "static_cast", "dynamic_cast", "reinterpret_cast", or 573 /// "const_cast". The returned pointer must not be freed. 574 const char *CXXNamedCastExpr::getCastName() const { 575 switch (getStmtClass()) { 576 case CXXStaticCastExprClass: return "static_cast"; 577 case CXXDynamicCastExprClass: return "dynamic_cast"; 578 case CXXReinterpretCastExprClass: return "reinterpret_cast"; 579 case CXXConstCastExprClass: return "const_cast"; 580 default: return "<invalid cast>"; 581 } 582 } 583 584 CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T, 585 ExprValueKind VK, 586 CastKind K, Expr *Op, 587 const CXXCastPath *BasePath, 588 TypeSourceInfo *WrittenTy, 589 SourceLocation L, 590 SourceLocation RParenLoc, 591 SourceRange AngleBrackets) { 592 unsigned PathSize = (BasePath ? BasePath->size() : 0); 593 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr) 594 + PathSize * sizeof(CXXBaseSpecifier*)); 595 CXXStaticCastExpr *E = 596 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, 597 RParenLoc, AngleBrackets); 598 if (PathSize) E->setCastPath(*BasePath); 599 return E; 600 } 601 602 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C, 603 unsigned PathSize) { 604 void *Buffer = 605 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); 606 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize); 607 } 608 609 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T, 610 ExprValueKind VK, 611 CastKind K, Expr *Op, 612 const CXXCastPath *BasePath, 613 TypeSourceInfo *WrittenTy, 614 SourceLocation L, 615 SourceLocation RParenLoc, 616 SourceRange AngleBrackets) { 617 unsigned PathSize = (BasePath ? BasePath->size() : 0); 618 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr) 619 + PathSize * sizeof(CXXBaseSpecifier*)); 620 CXXDynamicCastExpr *E = 621 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, 622 RParenLoc, AngleBrackets); 623 if (PathSize) E->setCastPath(*BasePath); 624 return E; 625 } 626 627 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C, 628 unsigned PathSize) { 629 void *Buffer = 630 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); 631 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize); 632 } 633 634 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven 635 /// to always be null. For example: 636 /// 637 /// struct A { }; 638 /// struct B final : A { }; 639 /// struct C { }; 640 /// 641 /// C *f(B* b) { return dynamic_cast<C*>(b); } 642 bool CXXDynamicCastExpr::isAlwaysNull() const 643 { 644 QualType SrcType = getSubExpr()->getType(); 645 QualType DestType = getType(); 646 647 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) { 648 SrcType = SrcPTy->getPointeeType(); 649 DestType = DestType->castAs<PointerType>()->getPointeeType(); 650 } 651 652 if (DestType->isVoidType()) 653 return false; 654 655 const CXXRecordDecl *SrcRD = 656 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl()); 657 658 if (!SrcRD->hasAttr<FinalAttr>()) 659 return false; 660 661 const CXXRecordDecl *DestRD = 662 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl()); 663 664 return !DestRD->isDerivedFrom(SrcRD); 665 } 666 667 CXXReinterpretCastExpr * 668 CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T, 669 ExprValueKind VK, CastKind K, Expr *Op, 670 const CXXCastPath *BasePath, 671 TypeSourceInfo *WrittenTy, SourceLocation L, 672 SourceLocation RParenLoc, 673 SourceRange AngleBrackets) { 674 unsigned PathSize = (BasePath ? BasePath->size() : 0); 675 void *Buffer = 676 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); 677 CXXReinterpretCastExpr *E = 678 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, 679 RParenLoc, AngleBrackets); 680 if (PathSize) E->setCastPath(*BasePath); 681 return E; 682 } 683 684 CXXReinterpretCastExpr * 685 CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) { 686 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr) 687 + PathSize * sizeof(CXXBaseSpecifier*)); 688 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize); 689 } 690 691 CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T, 692 ExprValueKind VK, Expr *Op, 693 TypeSourceInfo *WrittenTy, 694 SourceLocation L, 695 SourceLocation RParenLoc, 696 SourceRange AngleBrackets) { 697 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets); 698 } 699 700 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) { 701 return new (C) CXXConstCastExpr(EmptyShell()); 702 } 703 704 CXXFunctionalCastExpr * 705 CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK, 706 TypeSourceInfo *Written, CastKind K, Expr *Op, 707 const CXXCastPath *BasePath, 708 SourceLocation L, SourceLocation R) { 709 unsigned PathSize = (BasePath ? BasePath->size() : 0); 710 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr) 711 + PathSize * sizeof(CXXBaseSpecifier*)); 712 CXXFunctionalCastExpr *E = 713 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R); 714 if (PathSize) E->setCastPath(*BasePath); 715 return E; 716 } 717 718 CXXFunctionalCastExpr * 719 CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) { 720 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr) 721 + PathSize * sizeof(CXXBaseSpecifier*)); 722 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize); 723 } 724 725 SourceLocation CXXFunctionalCastExpr::getLocStart() const { 726 return getTypeInfoAsWritten()->getTypeLoc().getLocStart(); 727 } 728 729 SourceLocation CXXFunctionalCastExpr::getLocEnd() const { 730 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd(); 731 } 732 733 UserDefinedLiteral::LiteralOperatorKind 734 UserDefinedLiteral::getLiteralOperatorKind() const { 735 if (getNumArgs() == 0) 736 return LOK_Template; 737 if (getNumArgs() == 2) 738 return LOK_String; 739 740 assert(getNumArgs() == 1 && "unexpected #args in literal operator call"); 741 QualType ParamTy = 742 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType(); 743 if (ParamTy->isPointerType()) 744 return LOK_Raw; 745 if (ParamTy->isAnyCharacterType()) 746 return LOK_Character; 747 if (ParamTy->isIntegerType()) 748 return LOK_Integer; 749 if (ParamTy->isFloatingType()) 750 return LOK_Floating; 751 752 llvm_unreachable("unknown kind of literal operator"); 753 } 754 755 Expr *UserDefinedLiteral::getCookedLiteral() { 756 #ifndef NDEBUG 757 LiteralOperatorKind LOK = getLiteralOperatorKind(); 758 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal"); 759 #endif 760 return getArg(0); 761 } 762 763 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const { 764 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier(); 765 } 766 767 CXXDefaultArgExpr * 768 CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc, 769 ParmVarDecl *Param, Expr *SubExpr) { 770 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *)); 771 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param, 772 SubExpr); 773 } 774 775 CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc, 776 FieldDecl *Field, QualType T) 777 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C), 778 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType() 779 ? VK_XValue 780 : VK_RValue, 781 /*FIXME*/ OK_Ordinary, false, false, false, false), 782 Field(Field), Loc(Loc) { 783 assert(Field->hasInClassInitializer()); 784 } 785 786 CXXTemporary *CXXTemporary::Create(const ASTContext &C, 787 const CXXDestructorDecl *Destructor) { 788 return new (C) CXXTemporary(Destructor); 789 } 790 791 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C, 792 CXXTemporary *Temp, 793 Expr* SubExpr) { 794 assert((SubExpr->getType()->isRecordType() || 795 SubExpr->getType()->isArrayType()) && 796 "Expression bound to a temporary must have record or array type!"); 797 798 return new (C) CXXBindTemporaryExpr(Temp, SubExpr); 799 } 800 801 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C, 802 CXXConstructorDecl *Cons, 803 TypeSourceInfo *Type, 804 ArrayRef<Expr*> Args, 805 SourceRange ParenOrBraceRange, 806 bool HadMultipleCandidates, 807 bool ListInitialization, 808 bool StdInitListInitialization, 809 bool ZeroInitialization) 810 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, 811 Type->getType().getNonReferenceType(), 812 Type->getTypeLoc().getBeginLoc(), 813 Cons, false, Args, 814 HadMultipleCandidates, 815 ListInitialization, 816 StdInitListInitialization, 817 ZeroInitialization, 818 CXXConstructExpr::CK_Complete, ParenOrBraceRange), 819 Type(Type) { 820 } 821 822 SourceLocation CXXTemporaryObjectExpr::getLocStart() const { 823 return Type->getTypeLoc().getBeginLoc(); 824 } 825 826 SourceLocation CXXTemporaryObjectExpr::getLocEnd() const { 827 SourceLocation Loc = getParenOrBraceRange().getEnd(); 828 if (Loc.isInvalid() && getNumArgs()) 829 Loc = getArg(getNumArgs()-1)->getLocEnd(); 830 return Loc; 831 } 832 833 CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T, 834 SourceLocation Loc, 835 CXXConstructorDecl *D, bool Elidable, 836 ArrayRef<Expr*> Args, 837 bool HadMultipleCandidates, 838 bool ListInitialization, 839 bool StdInitListInitialization, 840 bool ZeroInitialization, 841 ConstructionKind ConstructKind, 842 SourceRange ParenOrBraceRange) { 843 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D, 844 Elidable, Args, 845 HadMultipleCandidates, ListInitialization, 846 StdInitListInitialization, 847 ZeroInitialization, ConstructKind, 848 ParenOrBraceRange); 849 } 850 851 CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC, 852 QualType T, SourceLocation Loc, 853 CXXConstructorDecl *D, bool elidable, 854 ArrayRef<Expr*> args, 855 bool HadMultipleCandidates, 856 bool ListInitialization, 857 bool StdInitListInitialization, 858 bool ZeroInitialization, 859 ConstructionKind ConstructKind, 860 SourceRange ParenOrBraceRange) 861 : Expr(SC, T, VK_RValue, OK_Ordinary, 862 T->isDependentType(), T->isDependentType(), 863 T->isInstantiationDependentType(), 864 T->containsUnexpandedParameterPack()), 865 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange), 866 NumArgs(args.size()), 867 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates), 868 ListInitialization(ListInitialization), 869 StdInitListInitialization(StdInitListInitialization), 870 ZeroInitialization(ZeroInitialization), 871 ConstructKind(ConstructKind), Args(nullptr) 872 { 873 if (NumArgs) { 874 Args = new (C) Stmt*[args.size()]; 875 876 for (unsigned i = 0; i != args.size(); ++i) { 877 assert(args[i] && "NULL argument in CXXConstructExpr"); 878 879 if (args[i]->isValueDependent()) 880 ExprBits.ValueDependent = true; 881 if (args[i]->isInstantiationDependent()) 882 ExprBits.InstantiationDependent = true; 883 if (args[i]->containsUnexpandedParameterPack()) 884 ExprBits.ContainsUnexpandedParameterPack = true; 885 886 Args[i] = args[i]; 887 } 888 } 889 } 890 891 LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit, 892 LambdaCaptureKind Kind, VarDecl *Var, 893 SourceLocation EllipsisLoc) 894 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) 895 { 896 unsigned Bits = 0; 897 if (Implicit) 898 Bits |= Capture_Implicit; 899 900 switch (Kind) { 901 case LCK_This: 902 assert(!Var && "'this' capture cannot have a variable!"); 903 break; 904 905 case LCK_ByCopy: 906 Bits |= Capture_ByCopy; 907 // Fall through 908 case LCK_ByRef: 909 assert(Var && "capture must have a variable!"); 910 break; 911 case LCK_VLAType: 912 assert(!Var && "VLA type capture cannot have a variable!"); 913 Bits |= Capture_ByCopy; 914 break; 915 } 916 DeclAndBits.setInt(Bits); 917 } 918 919 LambdaCaptureKind LambdaCapture::getCaptureKind() const { 920 Decl *D = DeclAndBits.getPointer(); 921 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy; 922 if (!D) 923 return CapByCopy ? LCK_VLAType : LCK_This; 924 925 return CapByCopy ? LCK_ByCopy : LCK_ByRef; 926 } 927 928 LambdaExpr::LambdaExpr(QualType T, 929 SourceRange IntroducerRange, 930 LambdaCaptureDefault CaptureDefault, 931 SourceLocation CaptureDefaultLoc, 932 ArrayRef<Capture> Captures, 933 bool ExplicitParams, 934 bool ExplicitResultType, 935 ArrayRef<Expr *> CaptureInits, 936 ArrayRef<VarDecl *> ArrayIndexVars, 937 ArrayRef<unsigned> ArrayIndexStarts, 938 SourceLocation ClosingBrace, 939 bool ContainsUnexpandedParameterPack) 940 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, 941 T->isDependentType(), T->isDependentType(), T->isDependentType(), 942 ContainsUnexpandedParameterPack), 943 IntroducerRange(IntroducerRange), 944 CaptureDefaultLoc(CaptureDefaultLoc), 945 NumCaptures(Captures.size()), 946 CaptureDefault(CaptureDefault), 947 ExplicitParams(ExplicitParams), 948 ExplicitResultType(ExplicitResultType), 949 ClosingBrace(ClosingBrace) 950 { 951 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments"); 952 CXXRecordDecl *Class = getLambdaClass(); 953 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData(); 954 955 // FIXME: Propagate "has unexpanded parameter pack" bit. 956 957 // Copy captures. 958 const ASTContext &Context = Class->getASTContext(); 959 Data.NumCaptures = NumCaptures; 960 Data.NumExplicitCaptures = 0; 961 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures); 962 Capture *ToCapture = Data.Captures; 963 for (unsigned I = 0, N = Captures.size(); I != N; ++I) { 964 if (Captures[I].isExplicit()) 965 ++Data.NumExplicitCaptures; 966 967 *ToCapture++ = Captures[I]; 968 } 969 970 // Copy initialization expressions for the non-static data members. 971 Stmt **Stored = getStoredStmts(); 972 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I) 973 *Stored++ = CaptureInits[I]; 974 975 // Copy the body of the lambda. 976 *Stored++ = getCallOperator()->getBody(); 977 978 // Copy the array index variables, if any. 979 HasArrayIndexVars = !ArrayIndexVars.empty(); 980 if (HasArrayIndexVars) { 981 assert(ArrayIndexStarts.size() == NumCaptures); 982 memcpy(getArrayIndexVars(), ArrayIndexVars.data(), 983 sizeof(VarDecl *) * ArrayIndexVars.size()); 984 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(), 985 sizeof(unsigned) * Captures.size()); 986 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size(); 987 } 988 } 989 990 LambdaExpr *LambdaExpr::Create(const ASTContext &Context, 991 CXXRecordDecl *Class, 992 SourceRange IntroducerRange, 993 LambdaCaptureDefault CaptureDefault, 994 SourceLocation CaptureDefaultLoc, 995 ArrayRef<Capture> Captures, 996 bool ExplicitParams, 997 bool ExplicitResultType, 998 ArrayRef<Expr *> CaptureInits, 999 ArrayRef<VarDecl *> ArrayIndexVars, 1000 ArrayRef<unsigned> ArrayIndexStarts, 1001 SourceLocation ClosingBrace, 1002 bool ContainsUnexpandedParameterPack) { 1003 // Determine the type of the expression (i.e., the type of the 1004 // function object we're creating). 1005 QualType T = Context.getTypeDeclType(Class); 1006 1007 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1); 1008 if (!ArrayIndexVars.empty()) { 1009 Size += sizeof(unsigned) * (Captures.size() + 1); 1010 // Realign for following VarDecl array. 1011 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>()); 1012 Size += sizeof(VarDecl *) * ArrayIndexVars.size(); 1013 } 1014 void *Mem = Context.Allocate(Size); 1015 return new (Mem) LambdaExpr(T, IntroducerRange, 1016 CaptureDefault, CaptureDefaultLoc, Captures, 1017 ExplicitParams, ExplicitResultType, 1018 CaptureInits, ArrayIndexVars, ArrayIndexStarts, 1019 ClosingBrace, ContainsUnexpandedParameterPack); 1020 } 1021 1022 LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C, 1023 unsigned NumCaptures, 1024 unsigned NumArrayIndexVars) { 1025 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1); 1026 if (NumArrayIndexVars) 1027 Size += sizeof(VarDecl) * NumArrayIndexVars 1028 + sizeof(unsigned) * (NumCaptures + 1); 1029 void *Mem = C.Allocate(Size); 1030 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0); 1031 } 1032 1033 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const { 1034 return getLambdaClass()->getLambdaData().Captures; 1035 } 1036 1037 LambdaExpr::capture_iterator LambdaExpr::capture_end() const { 1038 return capture_begin() + NumCaptures; 1039 } 1040 1041 LambdaExpr::capture_range LambdaExpr::captures() const { 1042 return capture_range(capture_begin(), capture_end()); 1043 } 1044 1045 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const { 1046 return capture_begin(); 1047 } 1048 1049 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const { 1050 struct CXXRecordDecl::LambdaDefinitionData &Data 1051 = getLambdaClass()->getLambdaData(); 1052 return Data.Captures + Data.NumExplicitCaptures; 1053 } 1054 1055 LambdaExpr::capture_range LambdaExpr::explicit_captures() const { 1056 return capture_range(explicit_capture_begin(), explicit_capture_end()); 1057 } 1058 1059 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const { 1060 return explicit_capture_end(); 1061 } 1062 1063 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const { 1064 return capture_end(); 1065 } 1066 1067 LambdaExpr::capture_range LambdaExpr::implicit_captures() const { 1068 return capture_range(implicit_capture_begin(), implicit_capture_end()); 1069 } 1070 1071 ArrayRef<VarDecl *> 1072 LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const { 1073 assert(HasArrayIndexVars && "No array index-var data?"); 1074 1075 unsigned Index = Iter - capture_init_begin(); 1076 assert(Index < getLambdaClass()->getLambdaData().NumCaptures && 1077 "Capture index out-of-range"); 1078 VarDecl **IndexVars = getArrayIndexVars(); 1079 unsigned *IndexStarts = getArrayIndexStarts(); 1080 return llvm::makeArrayRef(IndexVars + IndexStarts[Index], 1081 IndexVars + IndexStarts[Index + 1]); 1082 } 1083 1084 CXXRecordDecl *LambdaExpr::getLambdaClass() const { 1085 return getType()->getAsCXXRecordDecl(); 1086 } 1087 1088 CXXMethodDecl *LambdaExpr::getCallOperator() const { 1089 CXXRecordDecl *Record = getLambdaClass(); 1090 return Record->getLambdaCallOperator(); 1091 } 1092 1093 TemplateParameterList *LambdaExpr::getTemplateParameterList() const { 1094 CXXRecordDecl *Record = getLambdaClass(); 1095 return Record->getGenericLambdaTemplateParameterList(); 1096 1097 } 1098 1099 CompoundStmt *LambdaExpr::getBody() const { 1100 if (!getStoredStmts()[NumCaptures]) 1101 getStoredStmts()[NumCaptures] = getCallOperator()->getBody(); 1102 1103 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]); 1104 } 1105 1106 bool LambdaExpr::isMutable() const { 1107 return !getCallOperator()->isConst(); 1108 } 1109 1110 ExprWithCleanups::ExprWithCleanups(Expr *subexpr, 1111 ArrayRef<CleanupObject> objects) 1112 : Expr(ExprWithCleanupsClass, subexpr->getType(), 1113 subexpr->getValueKind(), subexpr->getObjectKind(), 1114 subexpr->isTypeDependent(), subexpr->isValueDependent(), 1115 subexpr->isInstantiationDependent(), 1116 subexpr->containsUnexpandedParameterPack()), 1117 SubExpr(subexpr) { 1118 ExprWithCleanupsBits.NumObjects = objects.size(); 1119 for (unsigned i = 0, e = objects.size(); i != e; ++i) 1120 getObjectsBuffer()[i] = objects[i]; 1121 } 1122 1123 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr, 1124 ArrayRef<CleanupObject> objects) { 1125 size_t size = sizeof(ExprWithCleanups) 1126 + objects.size() * sizeof(CleanupObject); 1127 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>()); 1128 return new (buffer) ExprWithCleanups(subexpr, objects); 1129 } 1130 1131 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects) 1132 : Expr(ExprWithCleanupsClass, empty) { 1133 ExprWithCleanupsBits.NumObjects = numObjects; 1134 } 1135 1136 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, 1137 EmptyShell empty, 1138 unsigned numObjects) { 1139 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject); 1140 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>()); 1141 return new (buffer) ExprWithCleanups(empty, numObjects); 1142 } 1143 1144 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type, 1145 SourceLocation LParenLoc, 1146 ArrayRef<Expr*> Args, 1147 SourceLocation RParenLoc) 1148 : Expr(CXXUnresolvedConstructExprClass, 1149 Type->getType().getNonReferenceType(), 1150 (Type->getType()->isLValueReferenceType() ? VK_LValue 1151 :Type->getType()->isRValueReferenceType()? VK_XValue 1152 :VK_RValue), 1153 OK_Ordinary, 1154 Type->getType()->isDependentType(), true, true, 1155 Type->getType()->containsUnexpandedParameterPack()), 1156 Type(Type), 1157 LParenLoc(LParenLoc), 1158 RParenLoc(RParenLoc), 1159 NumArgs(Args.size()) { 1160 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1); 1161 for (unsigned I = 0; I != Args.size(); ++I) { 1162 if (Args[I]->containsUnexpandedParameterPack()) 1163 ExprBits.ContainsUnexpandedParameterPack = true; 1164 1165 StoredArgs[I] = Args[I]; 1166 } 1167 } 1168 1169 CXXUnresolvedConstructExpr * 1170 CXXUnresolvedConstructExpr::Create(const ASTContext &C, 1171 TypeSourceInfo *Type, 1172 SourceLocation LParenLoc, 1173 ArrayRef<Expr*> Args, 1174 SourceLocation RParenLoc) { 1175 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) + 1176 sizeof(Expr *) * Args.size()); 1177 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc); 1178 } 1179 1180 CXXUnresolvedConstructExpr * 1181 CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) { 1182 Stmt::EmptyShell Empty; 1183 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) + 1184 sizeof(Expr *) * NumArgs); 1185 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs); 1186 } 1187 1188 SourceLocation CXXUnresolvedConstructExpr::getLocStart() const { 1189 return Type->getTypeLoc().getBeginLoc(); 1190 } 1191 1192 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C, 1193 Expr *Base, QualType BaseType, 1194 bool IsArrow, 1195 SourceLocation OperatorLoc, 1196 NestedNameSpecifierLoc QualifierLoc, 1197 SourceLocation TemplateKWLoc, 1198 NamedDecl *FirstQualifierFoundInScope, 1199 DeclarationNameInfo MemberNameInfo, 1200 const TemplateArgumentListInfo *TemplateArgs) 1201 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, 1202 VK_LValue, OK_Ordinary, true, true, true, 1203 ((Base && Base->containsUnexpandedParameterPack()) || 1204 (QualifierLoc && 1205 QualifierLoc.getNestedNameSpecifier() 1206 ->containsUnexpandedParameterPack()) || 1207 MemberNameInfo.containsUnexpandedParameterPack())), 1208 Base(Base), BaseType(BaseType), IsArrow(IsArrow), 1209 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr || 1210 TemplateKWLoc.isValid()), 1211 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc), 1212 FirstQualifierFoundInScope(FirstQualifierFoundInScope), 1213 MemberNameInfo(MemberNameInfo) { 1214 if (TemplateArgs) { 1215 bool Dependent = true; 1216 bool InstantiationDependent = true; 1217 bool ContainsUnexpandedParameterPack = false; 1218 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs, 1219 Dependent, 1220 InstantiationDependent, 1221 ContainsUnexpandedParameterPack); 1222 if (ContainsUnexpandedParameterPack) 1223 ExprBits.ContainsUnexpandedParameterPack = true; 1224 } else if (TemplateKWLoc.isValid()) { 1225 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc); 1226 } 1227 } 1228 1229 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C, 1230 Expr *Base, QualType BaseType, 1231 bool IsArrow, 1232 SourceLocation OperatorLoc, 1233 NestedNameSpecifierLoc QualifierLoc, 1234 NamedDecl *FirstQualifierFoundInScope, 1235 DeclarationNameInfo MemberNameInfo) 1236 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, 1237 VK_LValue, OK_Ordinary, true, true, true, 1238 ((Base && Base->containsUnexpandedParameterPack()) || 1239 (QualifierLoc && 1240 QualifierLoc.getNestedNameSpecifier()-> 1241 containsUnexpandedParameterPack()) || 1242 MemberNameInfo.containsUnexpandedParameterPack())), 1243 Base(Base), BaseType(BaseType), IsArrow(IsArrow), 1244 HasTemplateKWAndArgsInfo(false), 1245 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc), 1246 FirstQualifierFoundInScope(FirstQualifierFoundInScope), 1247 MemberNameInfo(MemberNameInfo) { } 1248 1249 CXXDependentScopeMemberExpr * 1250 CXXDependentScopeMemberExpr::Create(const ASTContext &C, 1251 Expr *Base, QualType BaseType, bool IsArrow, 1252 SourceLocation OperatorLoc, 1253 NestedNameSpecifierLoc QualifierLoc, 1254 SourceLocation TemplateKWLoc, 1255 NamedDecl *FirstQualifierFoundInScope, 1256 DeclarationNameInfo MemberNameInfo, 1257 const TemplateArgumentListInfo *TemplateArgs) { 1258 if (!TemplateArgs && !TemplateKWLoc.isValid()) 1259 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType, 1260 IsArrow, OperatorLoc, 1261 QualifierLoc, 1262 FirstQualifierFoundInScope, 1263 MemberNameInfo); 1264 1265 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0; 1266 std::size_t size = sizeof(CXXDependentScopeMemberExpr) 1267 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs); 1268 1269 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>()); 1270 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType, 1271 IsArrow, OperatorLoc, 1272 QualifierLoc, 1273 TemplateKWLoc, 1274 FirstQualifierFoundInScope, 1275 MemberNameInfo, TemplateArgs); 1276 } 1277 1278 CXXDependentScopeMemberExpr * 1279 CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C, 1280 bool HasTemplateKWAndArgsInfo, 1281 unsigned NumTemplateArgs) { 1282 if (!HasTemplateKWAndArgsInfo) 1283 return new (C) CXXDependentScopeMemberExpr(C, nullptr, QualType(), 1284 0, SourceLocation(), 1285 NestedNameSpecifierLoc(), 1286 nullptr, DeclarationNameInfo()); 1287 1288 std::size_t size = sizeof(CXXDependentScopeMemberExpr) + 1289 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs); 1290 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>()); 1291 CXXDependentScopeMemberExpr *E 1292 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(), 1293 0, SourceLocation(), 1294 NestedNameSpecifierLoc(), 1295 SourceLocation(), nullptr, 1296 DeclarationNameInfo(), nullptr); 1297 E->HasTemplateKWAndArgsInfo = true; 1298 return E; 1299 } 1300 1301 bool CXXDependentScopeMemberExpr::isImplicitAccess() const { 1302 if (!Base) 1303 return true; 1304 1305 return cast<Expr>(Base)->isImplicitCXXThis(); 1306 } 1307 1308 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin, 1309 UnresolvedSetIterator end) { 1310 do { 1311 NamedDecl *decl = *begin; 1312 if (isa<UnresolvedUsingValueDecl>(decl)) 1313 return false; 1314 1315 // Unresolved member expressions should only contain methods and 1316 // method templates. 1317 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction()) 1318 ->isStatic()) 1319 return false; 1320 } while (++begin != end); 1321 1322 return true; 1323 } 1324 1325 UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C, 1326 bool HasUnresolvedUsing, 1327 Expr *Base, QualType BaseType, 1328 bool IsArrow, 1329 SourceLocation OperatorLoc, 1330 NestedNameSpecifierLoc QualifierLoc, 1331 SourceLocation TemplateKWLoc, 1332 const DeclarationNameInfo &MemberNameInfo, 1333 const TemplateArgumentListInfo *TemplateArgs, 1334 UnresolvedSetIterator Begin, 1335 UnresolvedSetIterator End) 1336 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc, 1337 MemberNameInfo, TemplateArgs, Begin, End, 1338 // Dependent 1339 ((Base && Base->isTypeDependent()) || 1340 BaseType->isDependentType()), 1341 ((Base && Base->isInstantiationDependent()) || 1342 BaseType->isInstantiationDependentType()), 1343 // Contains unexpanded parameter pack 1344 ((Base && Base->containsUnexpandedParameterPack()) || 1345 BaseType->containsUnexpandedParameterPack())), 1346 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing), 1347 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) { 1348 1349 // Check whether all of the members are non-static member functions, 1350 // and if so, mark give this bound-member type instead of overload type. 1351 if (hasOnlyNonStaticMemberFunctions(Begin, End)) 1352 setType(C.BoundMemberTy); 1353 } 1354 1355 bool UnresolvedMemberExpr::isImplicitAccess() const { 1356 if (!Base) 1357 return true; 1358 1359 return cast<Expr>(Base)->isImplicitCXXThis(); 1360 } 1361 1362 UnresolvedMemberExpr * 1363 UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing, 1364 Expr *Base, QualType BaseType, bool IsArrow, 1365 SourceLocation OperatorLoc, 1366 NestedNameSpecifierLoc QualifierLoc, 1367 SourceLocation TemplateKWLoc, 1368 const DeclarationNameInfo &MemberNameInfo, 1369 const TemplateArgumentListInfo *TemplateArgs, 1370 UnresolvedSetIterator Begin, 1371 UnresolvedSetIterator End) { 1372 std::size_t size = sizeof(UnresolvedMemberExpr); 1373 if (TemplateArgs) 1374 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size()); 1375 else if (TemplateKWLoc.isValid()) 1376 size += ASTTemplateKWAndArgsInfo::sizeFor(0); 1377 1378 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>()); 1379 return new (Mem) UnresolvedMemberExpr(C, 1380 HasUnresolvedUsing, Base, BaseType, 1381 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc, 1382 MemberNameInfo, TemplateArgs, Begin, End); 1383 } 1384 1385 UnresolvedMemberExpr * 1386 UnresolvedMemberExpr::CreateEmpty(const ASTContext &C, 1387 bool HasTemplateKWAndArgsInfo, 1388 unsigned NumTemplateArgs) { 1389 std::size_t size = sizeof(UnresolvedMemberExpr); 1390 if (HasTemplateKWAndArgsInfo) 1391 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs); 1392 1393 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>()); 1394 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell()); 1395 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo; 1396 return E; 1397 } 1398 1399 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const { 1400 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this. 1401 1402 // If there was a nested name specifier, it names the naming class. 1403 // It can't be dependent: after all, we were actually able to do the 1404 // lookup. 1405 CXXRecordDecl *Record = nullptr; 1406 auto *NNS = getQualifier(); 1407 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) { 1408 const Type *T = getQualifier()->getAsType(); 1409 assert(T && "qualifier in member expression does not name type"); 1410 Record = T->getAsCXXRecordDecl(); 1411 assert(Record && "qualifier in member expression does not name record"); 1412 } 1413 // Otherwise the naming class must have been the base class. 1414 else { 1415 QualType BaseType = getBaseType().getNonReferenceType(); 1416 if (isArrow()) { 1417 const PointerType *PT = BaseType->getAs<PointerType>(); 1418 assert(PT && "base of arrow member access is not pointer"); 1419 BaseType = PT->getPointeeType(); 1420 } 1421 1422 Record = BaseType->getAsCXXRecordDecl(); 1423 assert(Record && "base of member expression does not name record"); 1424 } 1425 1426 return Record; 1427 } 1428 1429 SubstNonTypeTemplateParmPackExpr:: 1430 SubstNonTypeTemplateParmPackExpr(QualType T, 1431 NonTypeTemplateParmDecl *Param, 1432 SourceLocation NameLoc, 1433 const TemplateArgument &ArgPack) 1434 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary, 1435 true, true, true, true), 1436 Param(Param), Arguments(ArgPack.pack_begin()), 1437 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { } 1438 1439 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const { 1440 return TemplateArgument(Arguments, NumArguments); 1441 } 1442 1443 FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack, 1444 SourceLocation NameLoc, 1445 unsigned NumParams, 1446 Decl * const *Params) 1447 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, 1448 true, true, true, true), 1449 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) { 1450 if (Params) 1451 std::uninitialized_copy(Params, Params + NumParams, 1452 reinterpret_cast<Decl**>(this+1)); 1453 } 1454 1455 FunctionParmPackExpr * 1456 FunctionParmPackExpr::Create(const ASTContext &Context, QualType T, 1457 ParmVarDecl *ParamPack, SourceLocation NameLoc, 1458 ArrayRef<Decl *> Params) { 1459 return new (Context.Allocate(sizeof(FunctionParmPackExpr) + 1460 sizeof(ParmVarDecl*) * Params.size())) 1461 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data()); 1462 } 1463 1464 FunctionParmPackExpr * 1465 FunctionParmPackExpr::CreateEmpty(const ASTContext &Context, 1466 unsigned NumParams) { 1467 return new (Context.Allocate(sizeof(FunctionParmPackExpr) + 1468 sizeof(ParmVarDecl*) * NumParams)) 1469 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr); 1470 } 1471 1472 void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy, 1473 unsigned ManglingNumber) { 1474 // We only need extra state if we have to remember more than just the Stmt. 1475 if (!ExtendedBy) 1476 return; 1477 1478 // We may need to allocate extra storage for the mangling number and the 1479 // extended-by ValueDecl. 1480 if (!State.is<ExtraState *>()) { 1481 auto ES = new (ExtendedBy->getASTContext()) ExtraState; 1482 ES->Temporary = State.get<Stmt *>(); 1483 State = ES; 1484 } 1485 1486 auto ES = State.get<ExtraState *>(); 1487 ES->ExtendingDecl = ExtendedBy; 1488 ES->ManglingNumber = ManglingNumber; 1489 } 1490 1491 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind, 1492 ArrayRef<TypeSourceInfo *> Args, 1493 SourceLocation RParenLoc, 1494 bool Value) 1495 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary, 1496 /*TypeDependent=*/false, 1497 /*ValueDependent=*/false, 1498 /*InstantiationDependent=*/false, 1499 /*ContainsUnexpandedParameterPack=*/false), 1500 Loc(Loc), RParenLoc(RParenLoc) 1501 { 1502 TypeTraitExprBits.Kind = Kind; 1503 TypeTraitExprBits.Value = Value; 1504 TypeTraitExprBits.NumArgs = Args.size(); 1505 1506 TypeSourceInfo **ToArgs = getTypeSourceInfos(); 1507 1508 for (unsigned I = 0, N = Args.size(); I != N; ++I) { 1509 if (Args[I]->getType()->isDependentType()) 1510 setValueDependent(true); 1511 if (Args[I]->getType()->isInstantiationDependentType()) 1512 setInstantiationDependent(true); 1513 if (Args[I]->getType()->containsUnexpandedParameterPack()) 1514 setContainsUnexpandedParameterPack(true); 1515 1516 ToArgs[I] = Args[I]; 1517 } 1518 } 1519 1520 TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T, 1521 SourceLocation Loc, 1522 TypeTrait Kind, 1523 ArrayRef<TypeSourceInfo *> Args, 1524 SourceLocation RParenLoc, 1525 bool Value) { 1526 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size(); 1527 void *Mem = C.Allocate(Size); 1528 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value); 1529 } 1530 1531 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C, 1532 unsigned NumArgs) { 1533 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs; 1534 void *Mem = C.Allocate(Size); 1535 return new (Mem) TypeTraitExpr(EmptyShell()); 1536 } 1537 1538 void ArrayTypeTraitExpr::anchor() { } 1539