]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/DeclFriend.h
Merge ACPICA 20100915.
[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   explicit FriendDecl(EmptyShell Empty)
63     : Decl(Decl::Friend, Empty), NextFriend(0) { }
64
65 public:
66   static FriendDecl *Create(ASTContext &C, DeclContext *DC,
67                             SourceLocation L, FriendUnion Friend_,
68                             SourceLocation FriendL);
69   static FriendDecl *Create(ASTContext &C, EmptyShell Empty);
70
71   /// If this friend declaration names an (untemplated but
72   /// possibly dependent) type, return the type;  otherwise
73   /// return null.  This is used only for C++0x's unelaborated
74   /// friend type declarations.
75   TypeSourceInfo *getFriendType() const {
76     return Friend.dyn_cast<TypeSourceInfo*>();
77   }
78
79   /// If this friend declaration doesn't name an unelaborated
80   /// type, return the inner declaration.
81   NamedDecl *getFriendDecl() const {
82     return Friend.dyn_cast<NamedDecl*>();
83   }
84
85   /// Retrieves the location of the 'friend' keyword.
86   SourceLocation getFriendLoc() const {
87     return FriendLoc;
88   }
89
90   // Implement isa/cast/dyncast/etc.
91   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
92   static bool classof(const FriendDecl *D) { return true; }
93   static bool classofKind(Kind K) { return K == Decl::Friend; }
94
95   friend class PCHDeclReader;
96   friend class PCHDeclWriter;
97 };
98
99 /// An iterator over the friend declarations of a class.
100 class CXXRecordDecl::friend_iterator {
101   FriendDecl *Ptr;
102
103   friend class CXXRecordDecl;
104   explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
105 public:
106   friend_iterator() {}
107
108   typedef FriendDecl *value_type;
109   typedef FriendDecl *reference;
110   typedef FriendDecl *pointer;
111   typedef int difference_type;
112   typedef std::forward_iterator_tag iterator_category;
113
114   reference operator*() const { return Ptr; }
115
116   friend_iterator &operator++() {
117     assert(Ptr && "attempt to increment past end of friend list");
118     Ptr = Ptr->NextFriend;
119     return *this;
120   }
121
122   friend_iterator operator++(int) {
123     friend_iterator tmp = *this;
124     ++*this;
125     return tmp;
126   }
127
128   bool operator==(const friend_iterator &Other) const {
129     return Ptr == Other.Ptr;
130   }
131
132   bool operator!=(const friend_iterator &Other) const {
133     return Ptr != Other.Ptr;
134   }
135
136   friend_iterator &operator+=(difference_type N) {
137     assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
138     while (N--)
139       ++*this;
140     return *this;
141   }
142
143   friend_iterator operator+(difference_type N) const {
144     friend_iterator tmp = *this;
145     tmp += N;
146     return tmp;
147   }
148 };
149
150 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
151   return friend_iterator(data().FirstFriend);
152 }
153
154 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
155   return friend_iterator(0);
156 }
157
158 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
159   assert(FD->NextFriend == 0 && "friend already has next friend?");
160   FD->NextFriend = data().FirstFriend;
161   data().FirstFriend = FD;
162 }
163   
164 }
165
166 #endif