]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/DeclFriend.h
Update clang to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / DeclFriend.h
1 //===-- DeclFriend.h - Classes for C++ friend declarations -*- C++ -*------===//
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 defines the section of the AST representing C++ friend
11 // declarations.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_AST_DECLFRIEND_H
16 #define LLVM_CLANG_AST_DECLFRIEND_H
17
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/TypeLoc.h"
21 #include "llvm/Support/Compiler.h"
22
23 namespace clang {
24
25 /// FriendDecl - Represents the declaration of a friend entity,
26 /// which can be a function, a type, or a templated function or type.
27 //  For example:
28 ///
29 /// @code
30 /// template <typename T> class A {
31 ///   friend int foo(T);
32 ///   friend class B;
33 ///   friend T; // only in C++0x
34 ///   template <typename U> friend class C;
35 ///   template <typename U> friend A& operator+=(A&, const U&) { ... }
36 /// };
37 /// @endcode
38 ///
39 /// The semantic context of a friend decl is its declaring class.
40 class FriendDecl final
41     : public Decl,
42       private llvm::TrailingObjects<FriendDecl, TemplateParameterList *> {
43   virtual void anchor();
44 public:
45   typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
46
47 private:
48   // The declaration that's a friend of this class.
49   FriendUnion Friend;
50
51   // A pointer to the next friend in the sequence.
52   LazyDeclPtr NextFriend;
53
54   // Location of the 'friend' specifier.
55   SourceLocation FriendLoc;
56
57   /// True if this 'friend' declaration is unsupported.  Eventually we
58   /// will support every possible friend declaration, but for now we
59   /// silently ignore some and set this flag to authorize all access.
60   unsigned UnsupportedFriend : 1;
61
62   // The number of "outer" template parameter lists in non-templatic
63   // (currently unsupported) friend type declarations, such as
64   //     template <class T> friend class A<T>::B;
65   unsigned NumTPLists : 31;
66
67   friend class CXXRecordDecl::friend_iterator;
68   friend class CXXRecordDecl;
69
70   FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
71              SourceLocation FriendL,
72              ArrayRef<TemplateParameterList*> FriendTypeTPLists)
73     : Decl(Decl::Friend, DC, L),
74       Friend(Friend),
75       NextFriend(),
76       FriendLoc(FriendL),
77       UnsupportedFriend(false),
78       NumTPLists(FriendTypeTPLists.size()) {
79     for (unsigned i = 0; i < NumTPLists; ++i)
80       getTrailingObjects<TemplateParameterList *>()[i] = FriendTypeTPLists[i];
81   }
82
83   FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
84     : Decl(Decl::Friend, Empty), NextFriend(),
85       UnsupportedFriend(false),
86       NumTPLists(NumFriendTypeTPLists) { }
87
88   FriendDecl *getNextFriend() {
89     if (!NextFriend.isOffset())
90       return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
91     return getNextFriendSlowCase();
92   }
93   FriendDecl *getNextFriendSlowCase();
94
95 public:
96   static FriendDecl *Create(ASTContext &C, DeclContext *DC,
97                             SourceLocation L, FriendUnion Friend_,
98                             SourceLocation FriendL,
99                             ArrayRef<TemplateParameterList*> FriendTypeTPLists
100                             = None);
101   static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID,
102                                         unsigned FriendTypeNumTPLists);
103
104   /// If this friend declaration names an (untemplated but possibly
105   /// dependent) type, return the type; otherwise return null.  This
106   /// is used for elaborated-type-specifiers and, in C++0x, for
107   /// arbitrary friend type declarations.
108   TypeSourceInfo *getFriendType() const {
109     return Friend.dyn_cast<TypeSourceInfo*>();
110   }
111   unsigned getFriendTypeNumTemplateParameterLists() const {
112     return NumTPLists;
113   }
114   TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const {
115     assert(N < NumTPLists);
116     return getTrailingObjects<TemplateParameterList *>()[N];
117   }
118
119   /// If this friend declaration doesn't name a type, return the inner
120   /// declaration.
121   NamedDecl *getFriendDecl() const {
122     return Friend.dyn_cast<NamedDecl*>();
123   }
124
125   /// Retrieves the location of the 'friend' keyword.
126   SourceLocation getFriendLoc() const {
127     return FriendLoc;
128   }
129
130   /// Retrieves the source range for the friend declaration.
131   SourceRange getSourceRange() const override LLVM_READONLY {
132     if (NamedDecl *ND = getFriendDecl()) {
133       if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
134         return FD->getSourceRange();
135       if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
136         return FTD->getSourceRange();
137       if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
138         return CTD->getSourceRange();
139       if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(ND)) {
140         if (DD->getOuterLocStart() != DD->getInnerLocStart())
141           return DD->getSourceRange();
142       }
143       return SourceRange(getFriendLoc(), ND->getLocEnd());
144     }
145     else if (TypeSourceInfo *TInfo = getFriendType()) {
146       SourceLocation StartL =
147           (NumTPLists == 0) ? getFriendLoc()
148                             : getTrailingObjects<TemplateParameterList *>()[0]
149                                   ->getTemplateLoc();
150       return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
151     }
152     else
153       return SourceRange(getFriendLoc(), getLocation());
154   }
155
156   /// Determines if this friend kind is unsupported.
157   bool isUnsupportedFriend() const {
158     return UnsupportedFriend;
159   }
160   void setUnsupportedFriend(bool Unsupported) {
161     UnsupportedFriend = Unsupported;
162   }
163
164   // Implement isa/cast/dyncast/etc.
165   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
166   static bool classofKind(Kind K) { return K == Decl::Friend; }
167
168   friend class ASTDeclReader;
169   friend class ASTDeclWriter;
170   friend class ASTNodeImporter;
171   friend TrailingObjects;
172 };
173
174 /// An iterator over the friend declarations of a class.
175 class CXXRecordDecl::friend_iterator {
176   FriendDecl *Ptr;
177
178   friend class CXXRecordDecl;
179   explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
180 public:
181   friend_iterator() {}
182
183   typedef FriendDecl *value_type;
184   typedef FriendDecl *reference;
185   typedef FriendDecl *pointer;
186   typedef int difference_type;
187   typedef std::forward_iterator_tag iterator_category;
188
189   reference operator*() const { return Ptr; }
190
191   friend_iterator &operator++() {
192     assert(Ptr && "attempt to increment past end of friend list");
193     Ptr = Ptr->getNextFriend();
194     return *this;
195   }
196
197   friend_iterator operator++(int) {
198     friend_iterator tmp = *this;
199     ++*this;
200     return tmp;
201   }
202
203   bool operator==(const friend_iterator &Other) const {
204     return Ptr == Other.Ptr;
205   }
206
207   bool operator!=(const friend_iterator &Other) const {
208     return Ptr != Other.Ptr;
209   }
210
211   friend_iterator &operator+=(difference_type N) {
212     assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
213     while (N--)
214       ++*this;
215     return *this;
216   }
217
218   friend_iterator operator+(difference_type N) const {
219     friend_iterator tmp = *this;
220     tmp += N;
221     return tmp;
222   }
223 };
224
225 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
226   return friend_iterator(getFirstFriend());
227 }
228
229 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
230   return friend_iterator(nullptr);
231 }
232
233 inline CXXRecordDecl::friend_range CXXRecordDecl::friends() const {
234   return friend_range(friend_begin(), friend_end());
235 }
236
237 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
238   assert(!FD->NextFriend && "friend already has next friend?");
239   FD->NextFriend = data().FirstFriend;
240   data().FirstFriend = FD;
241 }
242   
243 }
244
245 #endif