DeclFriend.cpp 2.8 KB

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