1 //===- DeclFriend.cpp - C++ Friend Declaration AST Node Implementation ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AST classes related to C++ friend
13 //===----------------------------------------------------------------------===//
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"
26 using namespace clang;
28 void FriendDecl::anchor() {}
30 FriendDecl *FriendDecl::getNextFriendSlowCase() {
31 return cast_or_null<FriendDecl>(
32 NextFriend.get(getASTContext().getExternalSource()));
35 FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
38 SourceLocation FriendL,
39 ArrayRef<TemplateParameterList *> FriendTypeTPLists) {
41 if (Friend.is<NamedDecl *>()) {
42 NamedDecl *D = Friend.get<NamedDecl*>();
43 assert(isa<FunctionDecl>(D) ||
44 isa<CXXRecordDecl>(D) ||
45 isa<FunctionTemplateDecl>(D) ||
46 isa<ClassTemplateDecl>(D));
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());
58 FriendDecl::additionalSizeToAlloc<TemplateParameterList *>(
59 FriendTypeTPLists.size());
60 FriendDecl *FD = new (C, DC, Extra) FriendDecl(DC, L, Friend, FriendL,
62 cast<CXXRecordDecl>(DC)->pushFriendDecl(FD);
66 FriendDecl *FriendDecl::CreateDeserialized(ASTContext &C, unsigned ID,
67 unsigned FriendTypeNumTPLists) {
69 additionalSizeToAlloc<TemplateParameterList *>(FriendTypeNumTPLists);
70 return new (C, ID, Extra) FriendDecl(EmptyShell(), FriendTypeNumTPLists);
73 FriendDecl *CXXRecordDecl::getFirstFriend() const {
74 ExternalASTSource *Source = getParentASTContext().getExternalSource();
75 Decl *First = data().FirstFriend.get(Source);
76 return First ? cast<FriendDecl>(First) : nullptr;