xref: /freebsd/contrib/llvm-project/clang/lib/AST/DeclFriend.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- DeclFriend.cpp - C++ Friend Declaration AST Node Implementation ----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the AST classes related to C++ friend
100b57cec5SDimitry Andric // declarations.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "clang/AST/DeclFriend.h"
150b57cec5SDimitry Andric #include "clang/AST/Decl.h"
160b57cec5SDimitry Andric #include "clang/AST/DeclBase.h"
170b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h"
180b57cec5SDimitry Andric #include "clang/AST/ASTContext.h"
190b57cec5SDimitry Andric #include "clang/AST/DeclTemplate.h"
200b57cec5SDimitry Andric #include "clang/Basic/LLVM.h"
210b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
220b57cec5SDimitry Andric #include <cassert>
230b57cec5SDimitry Andric #include <cstddef>
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric using namespace clang;
260b57cec5SDimitry Andric 
anchor()270b57cec5SDimitry Andric void FriendDecl::anchor() {}
280b57cec5SDimitry Andric 
getNextFriendSlowCase()290b57cec5SDimitry Andric FriendDecl *FriendDecl::getNextFriendSlowCase() {
300b57cec5SDimitry Andric   return cast_or_null<FriendDecl>(
310b57cec5SDimitry Andric                            NextFriend.get(getASTContext().getExternalSource()));
320b57cec5SDimitry Andric }
330b57cec5SDimitry Andric 
Create(ASTContext & C,DeclContext * DC,SourceLocation L,FriendUnion Friend,SourceLocation FriendL,ArrayRef<TemplateParameterList * > FriendTypeTPLists)340b57cec5SDimitry Andric FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
350b57cec5SDimitry Andric                                SourceLocation L,
360b57cec5SDimitry Andric                                FriendUnion Friend,
370b57cec5SDimitry Andric                                SourceLocation FriendL,
380b57cec5SDimitry Andric                           ArrayRef<TemplateParameterList *> FriendTypeTPLists) {
390b57cec5SDimitry Andric #ifndef NDEBUG
400b57cec5SDimitry Andric   if (Friend.is<NamedDecl *>()) {
410b57cec5SDimitry Andric     const auto *D = Friend.get<NamedDecl*>();
420b57cec5SDimitry Andric     assert(isa<FunctionDecl>(D) ||
430b57cec5SDimitry Andric            isa<CXXRecordDecl>(D) ||
440b57cec5SDimitry Andric            isa<FunctionTemplateDecl>(D) ||
450b57cec5SDimitry Andric            isa<ClassTemplateDecl>(D));
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric     // As a temporary hack, we permit template instantiation to point
480b57cec5SDimitry Andric     // to the original declaration when instantiating members.
490b57cec5SDimitry Andric     assert(D->getFriendObjectKind() ||
500b57cec5SDimitry Andric            (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind()));
510b57cec5SDimitry Andric     // These template parameters are for friend types only.
520b57cec5SDimitry Andric     assert(FriendTypeTPLists.empty());
530b57cec5SDimitry Andric   }
540b57cec5SDimitry Andric #endif
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric   std::size_t Extra =
570b57cec5SDimitry Andric       FriendDecl::additionalSizeToAlloc<TemplateParameterList *>(
580b57cec5SDimitry Andric           FriendTypeTPLists.size());
590b57cec5SDimitry Andric   auto *FD = new (C, DC, Extra) FriendDecl(DC, L, Friend, FriendL,
600b57cec5SDimitry Andric                                            FriendTypeTPLists);
610b57cec5SDimitry Andric   cast<CXXRecordDecl>(DC)->pushFriendDecl(FD);
620b57cec5SDimitry Andric   return FD;
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric 
CreateDeserialized(ASTContext & C,GlobalDeclID ID,unsigned FriendTypeNumTPLists)65*0fca6ea1SDimitry Andric FriendDecl *FriendDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID,
660b57cec5SDimitry Andric                                            unsigned FriendTypeNumTPLists) {
670b57cec5SDimitry Andric   std::size_t Extra =
680b57cec5SDimitry Andric       additionalSizeToAlloc<TemplateParameterList *>(FriendTypeNumTPLists);
690b57cec5SDimitry Andric   return new (C, ID, Extra) FriendDecl(EmptyShell(), FriendTypeNumTPLists);
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric 
getFirstFriend() const720b57cec5SDimitry Andric FriendDecl *CXXRecordDecl::getFirstFriend() const {
730b57cec5SDimitry Andric   ExternalASTSource *Source = getParentASTContext().getExternalSource();
740b57cec5SDimitry Andric   Decl *First = data().FirstFriend.get(Source);
750b57cec5SDimitry Andric   return First ? cast<FriendDecl>(First) : nullptr;
760b57cec5SDimitry Andric }
77