Coverage Report

Created: 2018-12-11 17:59

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/AST/DeclFriend.h
Line
Count
Source (jump to first uncovered line)
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/Decl.h"
19
#include "clang/AST/DeclBase.h"
20
#include "clang/AST/DeclCXX.h"
21
#include "clang/AST/DeclTemplate.h"
22
#include "clang/AST/ExternalASTSource.h"
23
#include "clang/AST/TypeLoc.h"
24
#include "clang/Basic/LLVM.h"
25
#include "clang/Basic/SourceLocation.h"
26
#include "llvm/ADT/ArrayRef.h"
27
#include "llvm/ADT/None.h"
28
#include "llvm/ADT/PointerUnion.h"
29
#include "llvm/Support/Casting.h"
30
#include "llvm/Support/Compiler.h"
31
#include "llvm/Support/TrailingObjects.h"
32
#include <cassert>
33
#include <iterator>
34
35
namespace clang {
36
37
class ASTContext;
38
39
/// FriendDecl - Represents the declaration of a friend entity,
40
/// which can be a function, a type, or a templated function or type.
41
/// For example:
42
///
43
/// @code
44
/// template <typename T> class A {
45
///   friend int foo(T);
46
///   friend class B;
47
///   friend T; // only in C++0x
48
///   template <typename U> friend class C;
49
///   template <typename U> friend A& operator+=(A&, const U&) { ... }
50
/// };
51
/// @endcode
52
///
53
/// The semantic context of a friend decl is its declaring class.
54
class FriendDecl final
55
    : public Decl,
56
      private llvm::TrailingObjects<FriendDecl, TemplateParameterList *> {
57
  virtual void anchor();
58
59
public:
60
  using FriendUnion = llvm::PointerUnion<NamedDecl *, TypeSourceInfo *>;
61
62
private:
63
  friend class CXXRecordDecl;
64
  friend class CXXRecordDecl::friend_iterator;
65
66
  // The declaration that's a friend of this class.
67
  FriendUnion Friend;
68
69
  // A pointer to the next friend in the sequence.
70
  LazyDeclPtr NextFriend;
71
72
  // Location of the 'friend' specifier.
73
  SourceLocation FriendLoc;
74
75
  /// True if this 'friend' declaration is unsupported.  Eventually we
76
  /// will support every possible friend declaration, but for now we
77
  /// silently ignore some and set this flag to authorize all access.
78
  unsigned UnsupportedFriend : 1;
79
80
  // The number of "outer" template parameter lists in non-templatic
81
  // (currently unsupported) friend type declarations, such as
82
  //     template <class T> friend class A<T>::B;
83
  unsigned NumTPLists : 31;
84
85
  FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
86
             SourceLocation FriendL,
87
             ArrayRef<TemplateParameterList *> FriendTypeTPLists)
88
      : Decl(Decl::Friend, DC, L), Friend(Friend), FriendLoc(FriendL),
89
132k
        UnsupportedFriend(false), NumTPLists(FriendTypeTPLists.size()) {
90
132k
    for (unsigned i = 0; i < NumTPLists; 
++i11
)
91
11
      getTrailingObjects<TemplateParameterList *>()[i] = FriendTypeTPLists[i];
92
132k
  }
93
94
  FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
95
      : Decl(Decl::Friend, Empty), UnsupportedFriend(false),
96
135
        NumTPLists(NumFriendTypeTPLists) {}
97
98
60.7k
  FriendDecl *getNextFriend() {
99
60.7k
    if (!NextFriend.isOffset())
100
60.7k
      return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
101
6
    return getNextFriendSlowCase();
102
6
  }
103
104
  FriendDecl *getNextFriendSlowCase();
105
106
public:
107
  friend class ASTDeclReader;
108
  friend class ASTDeclWriter;
109
  friend class ASTNodeImporter;
110
  friend TrailingObjects;
111
112
  static FriendDecl *Create(ASTContext &C, DeclContext *DC,
113
                            SourceLocation L, FriendUnion Friend_,
114
                            SourceLocation FriendL,
115
                            ArrayRef<TemplateParameterList*> FriendTypeTPLists
116
                            = None);
117
  static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID,
118
                                        unsigned FriendTypeNumTPLists);
119
120
  /// If this friend declaration names an (untemplated but possibly
121
  /// dependent) type, return the type; otherwise return null.  This
122
  /// is used for elaborated-type-specifiers and, in C++0x, for
123
  /// arbitrary friend type declarations.
124
190k
  TypeSourceInfo *getFriendType() const {
125
190k
    return Friend.dyn_cast<TypeSourceInfo*>();
126
190k
  }
127
128
9
  unsigned getFriendTypeNumTemplateParameterLists() const {
129
9
    return NumTPLists;
130
9
  }
131
132
0
  TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const {
133
0
    assert(N < NumTPLists);
134
0
    return getTrailingObjects<TemplateParameterList *>()[N];
135
0
  }
136
137
  /// If this friend declaration doesn't name a type, return the inner
138
  /// declaration.
139
125k
  NamedDecl *getFriendDecl() const {
140
125k
    return Friend.dyn_cast<NamedDecl *>();
141
125k
  }
142
143
  /// Retrieves the location of the 'friend' keyword.
144
67.9k
  SourceLocation getFriendLoc() const {
145
67.9k
    return FriendLoc;
146
67.9k
  }
147
148
  /// Retrieves the source range for the friend declaration.
149
10.5k
  SourceRange getSourceRange() const override LLVM_READONLY {
150
10.5k
    if (NamedDecl *ND = getFriendDecl()) {
151
153
      if (const auto *FD = dyn_cast<FunctionDecl>(ND))
152
108
        return FD->getSourceRange();
153
45
      if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(ND))
154
17
        return FTD->getSourceRange();
155
28
      if (const auto *CTD = dyn_cast<ClassTemplateDecl>(ND))
156
28
        return CTD->getSourceRange();
157
0
      if (const auto *DD = dyn_cast<DeclaratorDecl>(ND)) {
158
0
        if (DD->getOuterLocStart() != DD->getInnerLocStart())
159
0
          return DD->getSourceRange();
160
0
      }
161
0
      return SourceRange(getFriendLoc(), ND->getEndLoc());
162
0
    }
163
10.4k
    else if (TypeSourceInfo *TInfo = getFriendType()) {
164
10.4k
      SourceLocation StartL =
165
10.4k
          (NumTPLists == 0) ? 
getFriendLoc()10.4k
166
10.4k
                            : getTrailingObjects<TemplateParameterList *>()[0]
167
3
                                  ->getTemplateLoc();
168
10.4k
      return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
169
10.4k
    }
170
0
    else
171
0
      return SourceRange(getFriendLoc(), getLocation());
172
10.5k
  }
173
174
  /// Determines if this friend kind is unsupported.
175
189k
  bool isUnsupportedFriend() const {
176
189k
    return UnsupportedFriend;
177
189k
  }
178
57.5k
  void setUnsupportedFriend(bool Unsupported) {
179
57.5k
    UnsupportedFriend = Unsupported;
180
57.5k
  }
181
182
  // Implement isa/cast/dyncast/etc.
183
133k
  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
184
133k
  static bool classofKind(Kind K) { return K == Decl::Friend; }
185
};
186
187
/// An iterator over the friend declarations of a class.
188
class CXXRecordDecl::friend_iterator {
189
  friend class CXXRecordDecl;
190
191
  FriendDecl *Ptr;
192
193
379k
  explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
194
195
public:
196
  friend_iterator() = default;
197
198
  using value_type = FriendDecl *;
199
  using reference = FriendDecl *;
200
  using pointer = FriendDecl *;
201
  using difference_type = int;
202
  using iterator_category = std::forward_iterator_tag;
203
204
121k
  reference operator*() const { return Ptr; }
205
206
60.5k
  friend_iterator &operator++() {
207
60.5k
    assert(Ptr && "attempt to increment past end of friend list");
208
60.5k
    Ptr = Ptr->getNextFriend();
209
60.5k
    return *this;
210
60.5k
  }
211
212
0
  friend_iterator operator++(int) {
213
0
    friend_iterator tmp = *this;
214
0
    ++*this;
215
0
    return tmp;
216
0
  }
217
218
10
  bool operator==(const friend_iterator &Other) const {
219
10
    return Ptr == Other.Ptr;
220
10
  }
221
222
250k
  bool operator!=(const friend_iterator &Other) const {
223
250k
    return Ptr != Other.Ptr;
224
250k
  }
225
226
  friend_iterator &operator+=(difference_type N) {
227
    assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
228
    while (N--)
229
      ++*this;
230
    return *this;
231
  }
232
233
  friend_iterator operator+(difference_type N) const {
234
    friend_iterator tmp = *this;
235
    tmp += N;
236
    return tmp;
237
  }
238
};
239
240
189k
inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
241
189k
  return friend_iterator(getFirstFriend());
242
189k
}
243
244
189k
inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
245
189k
  return friend_iterator(nullptr);
246
189k
}
247
248
166k
inline CXXRecordDecl::friend_range CXXRecordDecl::friends() const {
249
166k
  return friend_range(friend_begin(), friend_end());
250
166k
}
251
252
132k
inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
253
132k
  assert(!FD->NextFriend && "friend already has next friend?");
254
132k
  FD->NextFriend = data().FirstFriend;
255
132k
  data().FirstFriend = FD;
256
132k
}
257
258
} // namespace clang
259
260
#endif // LLVM_CLANG_AST_DECLFRIEND_H