1*f4a2713aSLionel Sambuc //===--- DeclFriend.cpp - C++ Friend Declaration AST Node Implementation --===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This file implements the AST classes related to C++ friend 11*f4a2713aSLionel Sambuc // declarations. 12*f4a2713aSLionel Sambuc // 13*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 14*f4a2713aSLionel Sambuc 15*f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h" 16*f4a2713aSLionel Sambuc #include "clang/AST/DeclFriend.h" 17*f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h" 18*f4a2713aSLionel Sambuc using namespace clang; 19*f4a2713aSLionel Sambuc 20*f4a2713aSLionel Sambuc void FriendDecl::anchor() { } 21*f4a2713aSLionel Sambuc 22*f4a2713aSLionel Sambuc FriendDecl *FriendDecl::getNextFriendSlowCase() { 23*f4a2713aSLionel Sambuc return cast_or_null<FriendDecl>( 24*f4a2713aSLionel Sambuc NextFriend.get(getASTContext().getExternalSource())); 25*f4a2713aSLionel Sambuc } 26*f4a2713aSLionel Sambuc 27*f4a2713aSLionel Sambuc FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC, 28*f4a2713aSLionel Sambuc SourceLocation L, 29*f4a2713aSLionel Sambuc FriendUnion Friend, 30*f4a2713aSLionel Sambuc SourceLocation FriendL, 31*f4a2713aSLionel Sambuc ArrayRef<TemplateParameterList*> FriendTypeTPLists) { 32*f4a2713aSLionel Sambuc #ifndef NDEBUG 33*f4a2713aSLionel Sambuc if (Friend.is<NamedDecl*>()) { 34*f4a2713aSLionel Sambuc NamedDecl *D = Friend.get<NamedDecl*>(); 35*f4a2713aSLionel Sambuc assert(isa<FunctionDecl>(D) || 36*f4a2713aSLionel Sambuc isa<CXXRecordDecl>(D) || 37*f4a2713aSLionel Sambuc isa<FunctionTemplateDecl>(D) || 38*f4a2713aSLionel Sambuc isa<ClassTemplateDecl>(D)); 39*f4a2713aSLionel Sambuc 40*f4a2713aSLionel Sambuc // As a temporary hack, we permit template instantiation to point 41*f4a2713aSLionel Sambuc // to the original declaration when instantiating members. 42*f4a2713aSLionel Sambuc assert(D->getFriendObjectKind() || 43*f4a2713aSLionel Sambuc (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind())); 44*f4a2713aSLionel Sambuc // These template parameters are for friend types only. 45*f4a2713aSLionel Sambuc assert(FriendTypeTPLists.size() == 0); 46*f4a2713aSLionel Sambuc } 47*f4a2713aSLionel Sambuc #endif 48*f4a2713aSLionel Sambuc 49*f4a2713aSLionel Sambuc std::size_t Size = sizeof(FriendDecl) 50*f4a2713aSLionel Sambuc + FriendTypeTPLists.size() * sizeof(TemplateParameterList*); 51*f4a2713aSLionel Sambuc void *Mem = C.Allocate(Size); 52*f4a2713aSLionel Sambuc FriendDecl *FD = new (Mem) FriendDecl(DC, L, Friend, FriendL, 53*f4a2713aSLionel Sambuc FriendTypeTPLists); 54*f4a2713aSLionel Sambuc cast<CXXRecordDecl>(DC)->pushFriendDecl(FD); 55*f4a2713aSLionel Sambuc return FD; 56*f4a2713aSLionel Sambuc } 57*f4a2713aSLionel Sambuc 58*f4a2713aSLionel Sambuc FriendDecl *FriendDecl::CreateDeserialized(ASTContext &C, unsigned ID, 59*f4a2713aSLionel Sambuc unsigned FriendTypeNumTPLists) { 60*f4a2713aSLionel Sambuc std::size_t Size = sizeof(FriendDecl) 61*f4a2713aSLionel Sambuc + FriendTypeNumTPLists * sizeof(TemplateParameterList*); 62*f4a2713aSLionel Sambuc void *Mem = AllocateDeserializedDecl(C, ID, Size); 63*f4a2713aSLionel Sambuc return new (Mem) FriendDecl(EmptyShell(), FriendTypeNumTPLists); 64*f4a2713aSLionel Sambuc } 65*f4a2713aSLionel Sambuc 66*f4a2713aSLionel Sambuc FriendDecl *CXXRecordDecl::getFirstFriend() const { 67*f4a2713aSLionel Sambuc ExternalASTSource *Source = getParentASTContext().getExternalSource(); 68*f4a2713aSLionel Sambuc Decl *First = data().FirstFriend.get(Source); 69*f4a2713aSLionel Sambuc return First ? cast<FriendDecl>(First) : 0; 70*f4a2713aSLionel Sambuc } 71