blob: 08fbed3615790f531fe02360b30768684cdab446 [file] [log] [blame]
Eugene Zelenko8998f102017-11-10 00:59:22 +00001//===- DeclFriend.cpp - C++ Friend Declaration AST Node Implementation ----===//
John McCallbbbbe4e2010-03-11 07:50:04 +00002//
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 Zelenko8998f102017-11-10 00:59:22 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclBase.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/ASTContext.h"
John McCallbbbbe4e2010-03-11 07:50:04 +000020#include "clang/AST/DeclTemplate.h"
Eugene Zelenko8998f102017-11-10 00:59:22 +000021#include "clang/Basic/LLVM.h"
22#include "llvm/Support/Casting.h"
23#include <cassert>
24#include <cstddef>
25
John McCallbbbbe4e2010-03-11 07:50:04 +000026using namespace clang;
27
Eugene Zelenko8998f102017-11-10 00:59:22 +000028void FriendDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000029
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000030FriendDecl *FriendDecl::getNextFriendSlowCase() {
31 return cast_or_null<FriendDecl>(
32 NextFriend.get(getASTContext().getExternalSource()));
33}
34
John McCallbbbbe4e2010-03-11 07:50:04 +000035FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
36 SourceLocation L,
37 FriendUnion Friend,
Enea Zaffanellaeb22c872013-01-31 09:54:08 +000038 SourceLocation FriendL,
Eugene Zelenko8998f102017-11-10 00:59:22 +000039 ArrayRef<TemplateParameterList *> FriendTypeTPLists) {
John McCallbbbbe4e2010-03-11 07:50:04 +000040#ifndef NDEBUG
Eugene Zelenko8998f102017-11-10 00:59:22 +000041 if (Friend.is<NamedDecl *>()) {
Eugene Zelenko7855e772018-04-03 00:11:50 +000042 const auto *D = Friend.get<NamedDecl*>();
John McCallbbbbe4e2010-03-11 07:50:04 +000043 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 Zaffanellaeb22c872013-01-31 09:54:08 +000052 // These template parameters are for friend types only.
Eugene Zelenko8998f102017-11-10 00:59:22 +000053 assert(FriendTypeTPLists.empty());
John McCallbbbbe4e2010-03-11 07:50:04 +000054 }
55#endif
56
James Y Knight967eb202015-12-29 22:13:13 +000057 std::size_t Extra =
58 FriendDecl::additionalSizeToAlloc<TemplateParameterList *>(
59 FriendTypeTPLists.size());
Eugene Zelenko7855e772018-04-03 00:11:50 +000060 auto *FD = new (C, DC, Extra) FriendDecl(DC, L, Friend, FriendL,
61 FriendTypeTPLists);
John McCall16927f62010-03-12 01:19:31 +000062 cast<CXXRecordDecl>(DC)->pushFriendDecl(FD);
63 return FD;
John McCallbbbbe4e2010-03-11 07:50:04 +000064}
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000065
Enea Zaffanellaeb22c872013-01-31 09:54:08 +000066FriendDecl *FriendDecl::CreateDeserialized(ASTContext &C, unsigned ID,
67 unsigned FriendTypeNumTPLists) {
James Y Knight967eb202015-12-29 22:13:13 +000068 std::size_t Extra =
69 additionalSizeToAlloc<TemplateParameterList *>(FriendTypeNumTPLists);
Richard Smithf7981722013-11-22 09:01:48 +000070 return new (C, ID, Extra) FriendDecl(EmptyShell(), FriendTypeNumTPLists);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000071}
Enea Zaffanellaeb22c872013-01-31 09:54:08 +000072
Richard Smith68ad0e72013-06-26 02:41:25 +000073FriendDecl *CXXRecordDecl::getFirstFriend() const {
74 ExternalASTSource *Source = getParentASTContext().getExternalSource();
75 Decl *First = data().FirstFriend.get(Source);
Craig Topper36250ad2014-05-12 05:36:57 +000076 return First ? cast<FriendDecl>(First) : nullptr;
Richard Smith68ad0e72013-06-26 02:41:25 +000077}