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