blob: a996cab093afb4e3e28614da468a53b60ba3abd4 [file] [log] [blame]
John McCallbbbbe4e2010-03-11 07:50:04 +00001//===--- DeclFriend.cpp - C++ Friend Declaration AST Node Implementation --===//
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
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000015#include "clang/AST/ASTContext.h"
John McCallbbbbe4e2010-03-11 07:50:04 +000016#include "clang/AST/DeclFriend.h"
17#include "clang/AST/DeclTemplate.h"
18using namespace clang;
19
David Blaikie68e081d2011-12-20 02:48:34 +000020void FriendDecl::anchor() { }
21
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000022FriendDecl *FriendDecl::getNextFriendSlowCase() {
23 return cast_or_null<FriendDecl>(
24 NextFriend.get(getASTContext().getExternalSource()));
25}
26
John McCallbbbbe4e2010-03-11 07:50:04 +000027FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
28 SourceLocation L,
29 FriendUnion Friend,
Enea Zaffanellaeb22c872013-01-31 09:54:08 +000030 SourceLocation FriendL,
31 ArrayRef<TemplateParameterList*> FriendTypeTPLists) {
John McCallbbbbe4e2010-03-11 07:50:04 +000032#ifndef NDEBUG
33 if (Friend.is<NamedDecl*>()) {
34 NamedDecl *D = Friend.get<NamedDecl*>();
35 assert(isa<FunctionDecl>(D) ||
36 isa<CXXRecordDecl>(D) ||
37 isa<FunctionTemplateDecl>(D) ||
38 isa<ClassTemplateDecl>(D));
39
40 // As a temporary hack, we permit template instantiation to point
41 // to the original declaration when instantiating members.
42 assert(D->getFriendObjectKind() ||
43 (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind()));
Enea Zaffanellaeb22c872013-01-31 09:54:08 +000044 // These template parameters are for friend types only.
45 assert(FriendTypeTPLists.size() == 0);
John McCallbbbbe4e2010-03-11 07:50:04 +000046 }
47#endif
48
Richard Smithf7981722013-11-22 09:01:48 +000049 std::size_t Extra = FriendTypeTPLists.size() * sizeof(TemplateParameterList*);
50 FriendDecl *FD = new (C, DC, Extra) FriendDecl(DC, L, Friend, FriendL,
51 FriendTypeTPLists);
John McCall16927f62010-03-12 01:19:31 +000052 cast<CXXRecordDecl>(DC)->pushFriendDecl(FD);
53 return FD;
John McCallbbbbe4e2010-03-11 07:50:04 +000054}
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000055
Enea Zaffanellaeb22c872013-01-31 09:54:08 +000056FriendDecl *FriendDecl::CreateDeserialized(ASTContext &C, unsigned ID,
57 unsigned FriendTypeNumTPLists) {
Richard Smithf7981722013-11-22 09:01:48 +000058 std::size_t Extra = FriendTypeNumTPLists * sizeof(TemplateParameterList*);
59 return new (C, ID, Extra) FriendDecl(EmptyShell(), FriendTypeNumTPLists);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000060}
Enea Zaffanellaeb22c872013-01-31 09:54:08 +000061
Richard Smith68ad0e72013-06-26 02:41:25 +000062FriendDecl *CXXRecordDecl::getFirstFriend() const {
63 ExternalASTSource *Source = getParentASTContext().getExternalSource();
64 Decl *First = data().FirstFriend.get(Source);
Craig Topper36250ad2014-05-12 05:36:57 +000065 return First ? cast<FriendDecl>(First) : nullptr;
Richard Smith68ad0e72013-06-26 02:41:25 +000066}