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