192b7f70c924cbf4514e9e434cea7def51ab49860John McCall//===--- DeclFriend.cpp - C++ Friend Declaration AST Node Implementation --===//
292b7f70c924cbf4514e9e434cea7def51ab49860John McCall//
392b7f70c924cbf4514e9e434cea7def51ab49860John McCall//                     The LLVM Compiler Infrastructure
492b7f70c924cbf4514e9e434cea7def51ab49860John McCall//
592b7f70c924cbf4514e9e434cea7def51ab49860John McCall// This file is distributed under the University of Illinois Open Source
692b7f70c924cbf4514e9e434cea7def51ab49860John McCall// License. See LICENSE.TXT for details.
792b7f70c924cbf4514e9e434cea7def51ab49860John McCall//
892b7f70c924cbf4514e9e434cea7def51ab49860John McCall//===----------------------------------------------------------------------===//
992b7f70c924cbf4514e9e434cea7def51ab49860John McCall//
1092b7f70c924cbf4514e9e434cea7def51ab49860John McCall// This file implements the AST classes related to C++ friend
1192b7f70c924cbf4514e9e434cea7def51ab49860John McCall// declarations.
1292b7f70c924cbf4514e9e434cea7def51ab49860John McCall//
1392b7f70c924cbf4514e9e434cea7def51ab49860John McCall//===----------------------------------------------------------------------===//
1492b7f70c924cbf4514e9e434cea7def51ab49860John McCall
15471c8b49982d1132f30b0b0da27fef94fd6e4f67Benjamin Kramer#include "clang/AST/ASTContext.h"
1692b7f70c924cbf4514e9e434cea7def51ab49860John McCall#include "clang/AST/DeclFriend.h"
1792b7f70c924cbf4514e9e434cea7def51ab49860John McCall#include "clang/AST/DeclTemplate.h"
1892b7f70c924cbf4514e9e434cea7def51ab49860John McCallusing namespace clang;
1992b7f70c924cbf4514e9e434cea7def51ab49860John McCall
2099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid FriendDecl::anchor() { }
2199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
22471c8b49982d1132f30b0b0da27fef94fd6e4f67Benjamin KramerFriendDecl *FriendDecl::getNextFriendSlowCase() {
23471c8b49982d1132f30b0b0da27fef94fd6e4f67Benjamin Kramer  return cast_or_null<FriendDecl>(
24471c8b49982d1132f30b0b0da27fef94fd6e4f67Benjamin Kramer                           NextFriend.get(getASTContext().getExternalSource()));
25471c8b49982d1132f30b0b0da27fef94fd6e4f67Benjamin Kramer}
26471c8b49982d1132f30b0b0da27fef94fd6e4f67Benjamin Kramer
2792b7f70c924cbf4514e9e434cea7def51ab49860John McCallFriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
2892b7f70c924cbf4514e9e434cea7def51ab49860John McCall                               SourceLocation L,
2992b7f70c924cbf4514e9e434cea7def51ab49860John McCall                               FriendUnion Friend,
308c84028ed9aa0dfd54ab729dee78f29c961d7f37Enea Zaffanella                               SourceLocation FriendL,
318c84028ed9aa0dfd54ab729dee78f29c961d7f37Enea Zaffanella                        ArrayRef<TemplateParameterList*> FriendTypeTPLists) {
3292b7f70c924cbf4514e9e434cea7def51ab49860John McCall#ifndef NDEBUG
3392b7f70c924cbf4514e9e434cea7def51ab49860John McCall  if (Friend.is<NamedDecl*>()) {
3492b7f70c924cbf4514e9e434cea7def51ab49860John McCall    NamedDecl *D = Friend.get<NamedDecl*>();
3592b7f70c924cbf4514e9e434cea7def51ab49860John McCall    assert(isa<FunctionDecl>(D) ||
3692b7f70c924cbf4514e9e434cea7def51ab49860John McCall           isa<CXXRecordDecl>(D) ||
3792b7f70c924cbf4514e9e434cea7def51ab49860John McCall           isa<FunctionTemplateDecl>(D) ||
3892b7f70c924cbf4514e9e434cea7def51ab49860John McCall           isa<ClassTemplateDecl>(D));
3992b7f70c924cbf4514e9e434cea7def51ab49860John McCall
4092b7f70c924cbf4514e9e434cea7def51ab49860John McCall    // As a temporary hack, we permit template instantiation to point
4192b7f70c924cbf4514e9e434cea7def51ab49860John McCall    // to the original declaration when instantiating members.
4292b7f70c924cbf4514e9e434cea7def51ab49860John McCall    assert(D->getFriendObjectKind() ||
4392b7f70c924cbf4514e9e434cea7def51ab49860John McCall           (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind()));
448c84028ed9aa0dfd54ab729dee78f29c961d7f37Enea Zaffanella    // These template parameters are for friend types only.
458c84028ed9aa0dfd54ab729dee78f29c961d7f37Enea Zaffanella    assert(FriendTypeTPLists.size() == 0);
4692b7f70c924cbf4514e9e434cea7def51ab49860John McCall  }
4792b7f70c924cbf4514e9e434cea7def51ab49860John McCall#endif
4892b7f70c924cbf4514e9e434cea7def51ab49860John McCall
49651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::size_t Extra = FriendTypeTPLists.size() * sizeof(TemplateParameterList*);
50651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  FriendDecl *FD = new (C, DC, Extra) FriendDecl(DC, L, Friend, FriendL,
51651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                                 FriendTypeTPLists);
52d60e22e601852ae1345f01514318a0951dc09f89John McCall  cast<CXXRecordDecl>(DC)->pushFriendDecl(FD);
53d60e22e601852ae1345f01514318a0951dc09f89John McCall  return FD;
5492b7f70c924cbf4514e9e434cea7def51ab49860John McCall}
556764334dfa73d67cbbb1b1fc8fe00440aad00f2aArgyrios Kyrtzidis
568c84028ed9aa0dfd54ab729dee78f29c961d7f37Enea ZaffanellaFriendDecl *FriendDecl::CreateDeserialized(ASTContext &C, unsigned ID,
578c84028ed9aa0dfd54ab729dee78f29c961d7f37Enea Zaffanella                                           unsigned FriendTypeNumTPLists) {
58651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::size_t Extra = FriendTypeNumTPLists * sizeof(TemplateParameterList*);
59651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return new (C, ID, Extra) FriendDecl(EmptyShell(), FriendTypeNumTPLists);
606764334dfa73d67cbbb1b1fc8fe00440aad00f2aArgyrios Kyrtzidis}
618c84028ed9aa0dfd54ab729dee78f29c961d7f37Enea Zaffanella
624fc5089e306fe606f2e3e4fa58063ebab35deb62Richard SmithFriendDecl *CXXRecordDecl::getFirstFriend() const {
634fc5089e306fe606f2e3e4fa58063ebab35deb62Richard Smith  ExternalASTSource *Source = getParentASTContext().getExternalSource();
644fc5089e306fe606f2e3e4fa58063ebab35deb62Richard Smith  Decl *First = data().FirstFriend.get(Source);
656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return First ? cast<FriendDecl>(First) : nullptr;
664fc5089e306fe606f2e3e4fa58063ebab35deb62Richard Smith}
67