Home | History | Annotate | Download | only in AST
      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     if (!NextFriend.isOffset())
     75       return cast_or_null<FriendDecl>(NextFriend.get(0));
     76     return getNextFriendSlowCase();
     77   }
     78   FriendDecl *getNextFriendSlowCase();
     79 
     80 public:
     81   static FriendDecl *Create(ASTContext &C, DeclContext *DC,
     82                             SourceLocation L, FriendUnion Friend_,
     83                             SourceLocation FriendL);
     84   static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID);
     85 
     86   /// If this friend declaration names an (untemplated but possibly
     87   /// dependent) type, return the type; otherwise return null.  This
     88   /// is used for elaborated-type-specifiers and, in C++0x, for
     89   /// arbitrary friend type declarations.
     90   TypeSourceInfo *getFriendType() const {
     91     return Friend.dyn_cast<TypeSourceInfo*>();
     92   }
     93 
     94   /// If this friend declaration doesn't name a type, return the inner
     95   /// declaration.
     96   NamedDecl *getFriendDecl() const {
     97     return Friend.dyn_cast<NamedDecl*>();
     98   }
     99 
    100   /// Retrieves the location of the 'friend' keyword.
    101   SourceLocation getFriendLoc() const {
    102     return FriendLoc;
    103   }
    104 
    105   /// Retrieves the source range for the friend declaration.
    106   SourceRange getSourceRange() const LLVM_READONLY {
    107     /* FIXME: consider the case of templates wrt start of range. */
    108     if (NamedDecl *ND = getFriendDecl())
    109       return SourceRange(getFriendLoc(), ND->getLocEnd());
    110     else if (TypeSourceInfo *TInfo = getFriendType())
    111       return SourceRange(getFriendLoc(), TInfo->getTypeLoc().getEndLoc());
    112     else
    113       return SourceRange(getFriendLoc(), getLocation());
    114   }
    115 
    116   /// Determines if this friend kind is unsupported.
    117   bool isUnsupportedFriend() const {
    118     return UnsupportedFriend;
    119   }
    120   void setUnsupportedFriend(bool Unsupported) {
    121     UnsupportedFriend = Unsupported;
    122   }
    123 
    124   // Implement isa/cast/dyncast/etc.
    125   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
    126   static bool classof(const FriendDecl *D) { return true; }
    127   static bool classofKind(Kind K) { return K == Decl::Friend; }
    128 
    129   friend class ASTDeclReader;
    130   friend class ASTDeclWriter;
    131 };
    132 
    133 /// An iterator over the friend declarations of a class.
    134 class CXXRecordDecl::friend_iterator {
    135   FriendDecl *Ptr;
    136 
    137   friend class CXXRecordDecl;
    138   explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
    139 public:
    140   friend_iterator() {}
    141 
    142   typedef FriendDecl *value_type;
    143   typedef FriendDecl *reference;
    144   typedef FriendDecl *pointer;
    145   typedef int difference_type;
    146   typedef std::forward_iterator_tag iterator_category;
    147 
    148   reference operator*() const { return Ptr; }
    149 
    150   friend_iterator &operator++() {
    151     assert(Ptr && "attempt to increment past end of friend list");
    152     Ptr = Ptr->getNextFriend();
    153     return *this;
    154   }
    155 
    156   friend_iterator operator++(int) {
    157     friend_iterator tmp = *this;
    158     ++*this;
    159     return tmp;
    160   }
    161 
    162   bool operator==(const friend_iterator &Other) const {
    163     return Ptr == Other.Ptr;
    164   }
    165 
    166   bool operator!=(const friend_iterator &Other) const {
    167     return Ptr != Other.Ptr;
    168   }
    169 
    170   friend_iterator &operator+=(difference_type N) {
    171     assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
    172     while (N--)
    173       ++*this;
    174     return *this;
    175   }
    176 
    177   friend_iterator operator+(difference_type N) const {
    178     friend_iterator tmp = *this;
    179     tmp += N;
    180     return tmp;
    181   }
    182 };
    183 
    184 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
    185   return friend_iterator(data().FirstFriend);
    186 }
    187 
    188 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
    189   return friend_iterator(0);
    190 }
    191 
    192 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
    193   assert(FD->NextFriend == 0 && "friend already has next friend?");
    194   FD->NextFriend = data().FirstFriend;
    195   data().FirstFriend = FD;
    196 }
    197 
    198 }
    199 
    200 #endif
    201