• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- 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 
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/DeclFriend.h"
17 #include "clang/AST/DeclTemplate.h"
18 using namespace clang;
19 
anchor()20 void FriendDecl::anchor() { }
21 
getNextFriendSlowCase()22 FriendDecl *FriendDecl::getNextFriendSlowCase() {
23   return cast_or_null<FriendDecl>(
24                            NextFriend.get(getASTContext().getExternalSource()));
25 }
26 
Create(ASTContext & C,DeclContext * DC,SourceLocation L,FriendUnion Friend,SourceLocation FriendL,ArrayRef<TemplateParameterList * > FriendTypeTPLists)27 FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
28                                SourceLocation L,
29                                FriendUnion Friend,
30                                SourceLocation FriendL,
31                         ArrayRef<TemplateParameterList*> FriendTypeTPLists) {
32 #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()));
44     // These template parameters are for friend types only.
45     assert(FriendTypeTPLists.size() == 0);
46   }
47 #endif
48 
49   std::size_t Extra =
50       FriendDecl::additionalSizeToAlloc<TemplateParameterList *>(
51           FriendTypeTPLists.size());
52   FriendDecl *FD = new (C, DC, Extra) FriendDecl(DC, L, Friend, FriendL,
53                                                  FriendTypeTPLists);
54   cast<CXXRecordDecl>(DC)->pushFriendDecl(FD);
55   return FD;
56 }
57 
CreateDeserialized(ASTContext & C,unsigned ID,unsigned FriendTypeNumTPLists)58 FriendDecl *FriendDecl::CreateDeserialized(ASTContext &C, unsigned ID,
59                                            unsigned FriendTypeNumTPLists) {
60   std::size_t Extra =
61       additionalSizeToAlloc<TemplateParameterList *>(FriendTypeNumTPLists);
62   return new (C, ID, Extra) FriendDecl(EmptyShell(), FriendTypeNumTPLists);
63 }
64 
getFirstFriend() const65 FriendDecl *CXXRecordDecl::getFirstFriend() const {
66   ExternalASTSource *Source = getParentASTContext().getExternalSource();
67   Decl *First = data().FirstFriend.get(Source);
68   return First ? cast<FriendDecl>(First) : nullptr;
69 }
70