Eugene Zelenko | 8998f10 | 2017-11-10 00:59:22 +0000 | [diff] [blame] | 1 | //===- DeclFriend.cpp - C++ Friend Declaration AST Node Implementation ----===// |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the AST classes related to C++ friend |
| 11 | // declarations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/AST/DeclFriend.h" |
Eugene Zelenko | 8998f10 | 2017-11-10 00:59:22 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclBase.h" |
| 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/ASTContext.h" |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Eugene Zelenko | 8998f10 | 2017-11-10 00:59:22 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LLVM.h" |
| 22 | #include "llvm/Support/Casting.h" |
| 23 | #include <cassert> |
| 24 | #include <cstddef> |
| 25 | |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
Eugene Zelenko | 8998f10 | 2017-11-10 00:59:22 +0000 | [diff] [blame] | 28 | void FriendDecl::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 29 | |
Benjamin Kramer | 4ab984e | 2012-07-04 20:19:54 +0000 | [diff] [blame] | 30 | FriendDecl *FriendDecl::getNextFriendSlowCase() { |
| 31 | return cast_or_null<FriendDecl>( |
| 32 | NextFriend.get(getASTContext().getExternalSource())); |
| 33 | } |
| 34 | |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 35 | FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC, |
| 36 | SourceLocation L, |
| 37 | FriendUnion Friend, |
Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 38 | SourceLocation FriendL, |
Eugene Zelenko | 8998f10 | 2017-11-10 00:59:22 +0000 | [diff] [blame] | 39 | ArrayRef<TemplateParameterList *> FriendTypeTPLists) { |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 40 | #ifndef NDEBUG |
Eugene Zelenko | 8998f10 | 2017-11-10 00:59:22 +0000 | [diff] [blame] | 41 | if (Friend.is<NamedDecl *>()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 42 | const auto *D = Friend.get<NamedDecl*>(); |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 43 | assert(isa<FunctionDecl>(D) || |
| 44 | isa<CXXRecordDecl>(D) || |
| 45 | isa<FunctionTemplateDecl>(D) || |
| 46 | isa<ClassTemplateDecl>(D)); |
| 47 | |
| 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())); |
Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 52 | // These template parameters are for friend types only. |
Eugene Zelenko | 8998f10 | 2017-11-10 00:59:22 +0000 | [diff] [blame] | 53 | assert(FriendTypeTPLists.empty()); |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 54 | } |
| 55 | #endif |
| 56 | |
James Y Knight | 967eb20 | 2015-12-29 22:13:13 +0000 | [diff] [blame] | 57 | std::size_t Extra = |
| 58 | FriendDecl::additionalSizeToAlloc<TemplateParameterList *>( |
| 59 | FriendTypeTPLists.size()); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 60 | auto *FD = new (C, DC, Extra) FriendDecl(DC, L, Friend, FriendL, |
| 61 | FriendTypeTPLists); |
John McCall | 16927f6 | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 62 | cast<CXXRecordDecl>(DC)->pushFriendDecl(FD); |
| 63 | return FD; |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 64 | } |
Argyrios Kyrtzidis | 74d28bd | 2010-06-29 22:47:00 +0000 | [diff] [blame] | 65 | |
Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 66 | FriendDecl *FriendDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 67 | unsigned FriendTypeNumTPLists) { |
James Y Knight | 967eb20 | 2015-12-29 22:13:13 +0000 | [diff] [blame] | 68 | std::size_t Extra = |
| 69 | additionalSizeToAlloc<TemplateParameterList *>(FriendTypeNumTPLists); |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 70 | return new (C, ID, Extra) FriendDecl(EmptyShell(), FriendTypeNumTPLists); |
Argyrios Kyrtzidis | 74d28bd | 2010-06-29 22:47:00 +0000 | [diff] [blame] | 71 | } |
Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 72 | |
Richard Smith | 68ad0e7 | 2013-06-26 02:41:25 +0000 | [diff] [blame] | 73 | FriendDecl *CXXRecordDecl::getFirstFriend() const { |
| 74 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 75 | Decl *First = data().FirstFriend.get(Source); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 76 | return First ? cast<FriendDecl>(First) : nullptr; |
Richard Smith | 68ad0e7 | 2013-06-26 02:41:25 +0000 | [diff] [blame] | 77 | } |