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