]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/DeclFriend.h
Merge ACPICA 20100702.
[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
20 namespace clang {
21
22 /// FriendDecl - Represents the declaration of a friend entity,
23 /// which can be a function, a type, or a templated function or type.
24 //  For example:
25 ///
26 /// @code
27 /// template <typename T> class A {
28 ///   friend int foo(T);
29 ///   friend class B;
30 ///   friend T; // only in C++0x
31 ///   template <typename U> friend class C;
32 ///   template <typename U> friend A& operator+=(A&, const U&) { ... }
33 /// };
34 /// @endcode
35 ///
36 /// The semantic context of a friend decl is its declaring class.
37 class FriendDecl : public Decl {
38 public:
39   typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
40
41 private:
42   // The declaration that's a friend of this class.
43   FriendUnion Friend;
44
45   // A pointer to the next friend in the sequence.
46   FriendDecl *NextFriend;
47
48   // Location of the 'friend' specifier.
49   SourceLocation FriendLoc;
50
51   friend class CXXRecordDecl::friend_iterator;
52   friend class CXXRecordDecl;
53
54   FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
55              SourceLocation FriendL)
56     : Decl(Decl::Friend, DC, L),
57       Friend(Friend),
58       NextFriend(0),
59       FriendLoc(FriendL) {
60   }
61
62 public:
63   static FriendDecl *Create(ASTContext &C, DeclContext *DC,
64                             SourceLocation L, FriendUnion Friend_,
65                             SourceLocation FriendL);
66
67   /// If this friend declaration names an (untemplated but
68   /// possibly dependent) type, return the type;  otherwise
69   /// return null.  This is used only for C++0x's unelaborated
70   /// friend type declarations.
71   TypeSourceInfo *getFriendType() const {
72     return Friend.dyn_cast<TypeSourceInfo*>();
73   }
74
75   /// If this friend declaration doesn't name an unelaborated
76   /// type, return the inner declaration.
77   NamedDecl *getFriendDecl() const {
78     return Friend.dyn_cast<NamedDecl*>();
79   }
80
81   /// Retrieves the location of the 'friend' keyword.
82   SourceLocation getFriendLoc() const {
83     return FriendLoc;
84   }
85
86   // Implement isa/cast/dyncast/etc.
87   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
88   static bool classof(const FriendDecl *D) { return true; }
89   static bool classofKind(Kind K) { return K == Decl::Friend; }
90 };
91
92 /// An iterator over the friend declarations of a class.
93 class CXXRecordDecl::friend_iterator {
94   FriendDecl *Ptr;
95
96   friend class CXXRecordDecl;
97   explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
98 public:
99   friend_iterator() {}
100
101   typedef FriendDecl *value_type;
102   typedef FriendDecl *reference;
103   typedef FriendDecl *pointer;
104   typedef int difference_type;
105   typedef std::forward_iterator_tag iterator_category;
106
107   reference operator*() const { return Ptr; }
108
109   friend_iterator &operator++() {
110     assert(Ptr && "attempt to increment past end of friend list");
111     Ptr = Ptr->NextFriend;
112     return *this;
113   }
114
115   friend_iterator operator++(int) {
116     friend_iterator tmp = *this;
117     ++*this;
118     return tmp;
119   }
120
121   bool operator==(const friend_iterator &Other) const {
122     return Ptr == Other.Ptr;
123   }
124
125   bool operator!=(const friend_iterator &Other) const {
126     return Ptr != Other.Ptr;
127   }
128
129   friend_iterator &operator+=(difference_type N) {
130     assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
131     while (N--)
132       ++*this;
133     return *this;
134   }
135
136   friend_iterator operator+(difference_type N) const {
137     friend_iterator tmp = *this;
138     tmp += N;
139     return tmp;
140   }
141 };
142
143 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
144   return friend_iterator(data().FirstFriend);
145 }
146
147 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
148   return friend_iterator(0);
149 }
150
151 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
152   assert(FD->NextFriend == 0 && "friend already has next friend?");
153   FD->NextFriend = data().FirstFriend;
154   data().FirstFriend = FD;
155 }
156   
157 }
158
159 #endif