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