1 //===- DeclFriend.cpp - C++ Friend Declaration 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 AST classes related to C++ friend 11 // declarations. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/AST/DeclFriend.h" 16 #include "clang/AST/Decl.h" 17 #include "clang/AST/DeclBase.h" 18 #include "clang/AST/DeclCXX.h" 19 #include "clang/AST/ASTContext.h" 20 #include "clang/AST/DeclTemplate.h" 21 #include "clang/Basic/LLVM.h" 22 #include "llvm/Support/Casting.h" 23 #include <cassert> 24 #include <cstddef> 25 26 using namespace clang; 27 28 void FriendDecl::anchor() {} 29 30 FriendDecl *FriendDecl::getNextFriendSlowCase() { 31 return cast_or_null<FriendDecl>( 32 NextFriend.get(getASTContext().getExternalSource())); 33 } 34 35 FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC, 36 SourceLocation L, 37 FriendUnion Friend, 38 SourceLocation FriendL, 39 ArrayRef<TemplateParameterList *> FriendTypeTPLists) { 40 #ifndef NDEBUG 41 if (Friend.is<NamedDecl *>()) { 42 const auto *D = Friend.get<NamedDecl*>(); 43 assert(isa<FunctionDecl>(D) || 44 isa<CXXRecordDecl>(D) || 45 isa<FunctionTemplateDecl>(D) || 46 isa<ClassTemplateDecl>(D)); 47 48 // As a temporary hack, we permit template instantiation to point 49 // to the original declaration when instantiating members. 50 assert(D->getFriendObjectKind() || 51 (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind())); 52 // These template parameters are for friend types only. 53 assert(FriendTypeTPLists.empty()); 54 } 55 #endif 56 57 std::size_t Extra = 58 FriendDecl::additionalSizeToAlloc<TemplateParameterList *>( 59 FriendTypeTPLists.size()); 60 auto *FD = new (C, DC, Extra) FriendDecl(DC, L, Friend, FriendL, 61 FriendTypeTPLists); 62 cast<CXXRecordDecl>(DC)->pushFriendDecl(FD); 63 return FD; 64 } 65 66 FriendDecl *FriendDecl::CreateDeserialized(ASTContext &C, unsigned ID, 67 unsigned FriendTypeNumTPLists) { 68 std::size_t Extra = 69 additionalSizeToAlloc<TemplateParameterList *>(FriendTypeNumTPLists); 70 return new (C, ID, Extra) FriendDecl(EmptyShell(), FriendTypeNumTPLists); 71 } 72 73 FriendDecl *CXXRecordDecl::getFirstFriend() const { 74 ExternalASTSource *Source = getParentASTContext().getExternalSource(); 75 Decl *First = data().FirstFriend.get(Source); 76 return First ? cast<FriendDecl>(First) : nullptr; 77 } 78