1 //===- DeclFriend.cpp - C++ Friend Declaration AST Node Implementation ----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the AST classes related to C++ friend
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/DeclFriend.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclBase.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/Basic/LLVM.h"
21 #include "llvm/Support/Casting.h"
25 using namespace clang
;
27 void FriendDecl::anchor() {}
29 FriendDecl
*FriendDecl::getNextFriendSlowCase() {
30 return cast_or_null
<FriendDecl
>(
31 NextFriend
.get(getASTContext().getExternalSource()));
34 FriendDecl
*FriendDecl::Create(ASTContext
&C
, DeclContext
*DC
,
37 SourceLocation FriendL
,
38 ArrayRef
<TemplateParameterList
*> FriendTypeTPLists
) {
40 if (Friend
.is
<NamedDecl
*>()) {
41 const auto *D
= Friend
.get
<NamedDecl
*>();
42 assert(isa
<FunctionDecl
>(D
) ||
43 isa
<CXXRecordDecl
>(D
) ||
44 isa
<FunctionTemplateDecl
>(D
) ||
45 isa
<ClassTemplateDecl
>(D
));
47 // As a temporary hack, we permit template instantiation to point
48 // to the original declaration when instantiating members.
49 assert(D
->getFriendObjectKind() ||
50 (cast
<CXXRecordDecl
>(DC
)->getTemplateSpecializationKind()));
51 // These template parameters are for friend types only.
52 assert(FriendTypeTPLists
.empty());
57 FriendDecl::additionalSizeToAlloc
<TemplateParameterList
*>(
58 FriendTypeTPLists
.size());
59 auto *FD
= new (C
, DC
, Extra
) FriendDecl(DC
, L
, Friend
, FriendL
,
61 cast
<CXXRecordDecl
>(DC
)->pushFriendDecl(FD
);
65 FriendDecl
*FriendDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
,
66 unsigned FriendTypeNumTPLists
) {
68 additionalSizeToAlloc
<TemplateParameterList
*>(FriendTypeNumTPLists
);
69 return new (C
, ID
, Extra
) FriendDecl(EmptyShell(), FriendTypeNumTPLists
);
72 FriendDecl
*CXXRecordDecl::getFirstFriend() const {
73 ExternalASTSource
*Source
= getParentASTContext().getExternalSource();
74 Decl
*First
= data().FirstFriend
.get(Source
);
75 return First
? cast
<FriendDecl
>(First
) : nullptr;