Coverage Report

Created: 2020-09-22 08:39

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/AST/DeclContextInternals.h
Line
Count
Source (jump to first uncovered line)
1
//===- DeclContextInternals.h - DeclContext Representation ------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file defines the data structures used in the implementation
10
//  of DeclContext.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
15
#define LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
16
17
#include "clang/AST/Decl.h"
18
#include "clang/AST/DeclBase.h"
19
#include "clang/AST/DeclCXX.h"
20
#include "clang/AST/DeclarationName.h"
21
#include "llvm/ADT/DenseMap.h"
22
#include "llvm/ADT/PointerIntPair.h"
23
#include "llvm/ADT/PointerUnion.h"
24
#include "llvm/ADT/SmallVector.h"
25
#include <algorithm>
26
#include <cassert>
27
28
namespace clang {
29
30
class DependentDiagnostic;
31
32
/// An array of decls optimized for the common case of only containing
33
/// one entry.
34
struct StoredDeclsList {
35
  /// When in vector form, this is what the Data pointer points to.
36
  using DeclsTy = SmallVector<NamedDecl *, 4>;
37
38
  /// A collection of declarations, with a flag to indicate if we have
39
  /// further external declarations.
40
  using DeclsAndHasExternalTy = llvm::PointerIntPair<DeclsTy *, 1, bool>;
41
42
  /// The stored data, which will be either a pointer to a NamedDecl,
43
  /// or a pointer to a vector with a flag to indicate if there are further
44
  /// external declarations.
45
  llvm::PointerUnion<NamedDecl *, DeclsAndHasExternalTy> Data;
46
47
public:
48
18.7M
  StoredDeclsList() = default;
49
50
17.3M
  StoredDeclsList(StoredDeclsList &&RHS) : Data(RHS.Data) {
51
17.3M
    RHS.Data = (NamedDecl *)nullptr;
52
17.3M
  }
53
54
26.6M
  ~StoredDeclsList() {
55
    // If this is a vector-form, free the vector.
56
26.6M
    if (DeclsTy *Vector = getAsVector())
57
666k
      delete Vector;
58
26.6M
  }
59
60
21
  StoredDeclsList &operator=(StoredDeclsList &&RHS) {
61
21
    if (DeclsTy *Vector = getAsVector())
62
0
      delete Vector;
63
21
    Data = RHS.Data;
64
21
    RHS.Data = (NamedDecl *)nullptr;
65
21
    return *this;
66
21
  }
67
68
102M
  bool isNull() const { return Data.isNull(); }
69
70
48.7M
  NamedDecl *getAsDecl() const {
71
48.7M
    return Data.dyn_cast<NamedDecl *>();
72
48.7M
  }
73
74
62.9M
  DeclsAndHasExternalTy getAsVectorAndHasExternal() const {
75
62.9M
    return Data.dyn_cast<DeclsAndHasExternalTy>();
76
62.9M
  }
77
78
56.2M
  DeclsTy *getAsVector() const {
79
56.2M
    return getAsVectorAndHasExternal().getPointer();
80
56.2M
  }
81
82
6.69M
  bool hasExternalDecls() const {
83
6.69M
    return getAsVectorAndHasExternal().getInt();
84
6.69M
  }
85
86
1.18M
  void setHasExternalDecls() {
87
1.18M
    if (DeclsTy *Vec = getAsVector())
88
611k
      Data = DeclsAndHasExternalTy(Vec, true);
89
572k
    else {
90
572k
      DeclsTy *VT = new DeclsTy();
91
572k
      if (NamedDecl *OldD = getAsDecl())
92
118k
        VT->push_back(OldD);
93
572k
      Data = DeclsAndHasExternalTy(VT, true);
94
572k
    }
95
1.18M
  }
96
97
12.5M
  void setOnlyValue(NamedDecl *ND) {
98
12.5M
    assert(!getAsVector() && "Not inline");
99
12.5M
    Data = ND;
100
    // Make sure that Data is a plain NamedDecl* so we can use its address
101
    // at getLookupResult.
102
12.5M
    assert(*(NamedDecl **)&Data == ND &&
103
12.5M
           "PointerUnion mangles the NamedDecl pointer!");
104
12.5M
  }
105
106
7.61k
  void remove(NamedDecl *D) {
107
7.61k
    assert(!isNull() && "removing from empty list");
108
7.61k
    if (NamedDecl *Singleton = getAsDecl()) {
109
1.11k
      assert(Singleton == D && "list is different singleton");
110
1.11k
      (void)Singleton;
111
1.11k
      Data = (NamedDecl *)nullptr;
112
1.11k
      return;
113
1.11k
    }
114
115
6.49k
    DeclsTy &Vec = *getAsVector();
116
6.49k
    DeclsTy::iterator I = llvm::find(Vec, D);
117
6.49k
    assert(I != Vec.end() && "list does not contain decl");
118
6.49k
    Vec.erase(I);
119
120
6.49k
    assert(llvm::find(Vec, D) == Vec.end() && "list still contains decl");
121
6.49k
  }
122
123
  /// Remove any declarations which were imported from an external
124
  /// AST source.
125
1.34M
  void removeExternalDecls() {
126
1.34M
    if (isNull()) {
127
      // Nothing to do.
128
228k
    } else if (NamedDecl *Singleton = getAsDecl()) {
129
5.90k
      if (Singleton->isFromASTFile())
130
21
        *this = StoredDeclsList();
131
222k
    } else {
132
222k
      DeclsTy &Vec = *getAsVector();
133
222k
      Vec.erase(std::remove_if(Vec.begin(), Vec.end(),
134
317k
                               [](Decl *D) { return D->isFromASTFile(); }),
135
222k
                Vec.end());
136
      // Don't have any external decls any more.
137
222k
      Data = DeclsAndHasExternalTy(&Vec, false);
138
222k
    }
139
1.34M
  }
140
141
  /// getLookupResult - Return an array of all the decls that this list
142
  /// represents.
143
46.5M
  DeclContext::lookup_result getLookupResult() {
144
46.5M
    if (isNull())
145
3.51M
      return DeclContext::lookup_result();
146
147
    // If we have a single NamedDecl, return it.
148
43.0M
    if (NamedDecl *ND = getAsDecl()) {
149
37.0M
      assert(!isNull() && "Empty list isn't allowed");
150
151
      // Data is a raw pointer to a NamedDecl*, return it.
152
37.0M
      return DeclContext::lookup_result(ND);
153
37.0M
    }
154
155
5.97M
    assert(getAsVector() && "Must have a vector at this point");
156
5.97M
    DeclsTy &Vector = *getAsVector();
157
158
    // Otherwise, we have a range result.
159
5.97M
    return DeclContext::lookup_result(Vector);
160
5.97M
  }
161
162
  /// HandleRedeclaration - If this is a redeclaration of an existing decl,
163
  /// replace the old one with D and return true.  Otherwise return false.
164
2.33M
  bool HandleRedeclaration(NamedDecl *D, bool IsKnownNewer) {
165
    // Most decls only have one entry in their list, special case it.
166
2.33M
    if (NamedDecl *OldD = getAsDecl()) {
167
1.12M
      if (!D->declarationReplaces(OldD, IsKnownNewer))
168
872k
        return false;
169
257k
      setOnlyValue(D);
170
257k
      return true;
171
257k
    }
172
173
    // Determine if this declaration is actually a redeclaration.
174
1.20M
    DeclsTy &Vec = *getAsVector();
175
1.20M
    for (DeclsTy::iterator OD = Vec.begin(), ODEnd = Vec.end();
176
8.68M
         OD != ODEnd; 
++OD7.48M
) {
177
7.63M
      NamedDecl *OldD = *OD;
178
7.63M
      if (D->declarationReplaces(OldD, IsKnownNewer)) {
179
151k
        *OD = D;
180
151k
        return true;
181
151k
      }
182
7.63M
    }
183
184
1.04M
    return false;
185
1.20M
  }
186
187
  /// AddSubsequentDecl - This is called on the second and later decl when it is
188
  /// not a redeclaration to merge it into the appropriate place in our list.
189
2.55M
  void AddSubsequentDecl(NamedDecl *D) {
190
2.55M
    assert(!isNull() && "don't AddSubsequentDecl when we have no decls");
191
192
    // If this is the second decl added to the list, convert this to vector
193
    // form.
194
2.55M
    if (NamedDecl *OldD = getAsDecl()) {
195
875k
      DeclsTy *VT = new DeclsTy();
196
875k
      VT->push_back(OldD);
197
875k
      Data = DeclsAndHasExternalTy(VT, false);
198
875k
    }
199
200
2.55M
    DeclsTy &Vec = *getAsVector();
201
202
    // Using directives end up in a special entry which contains only
203
    // other using directives, so all this logic is wasted for them.
204
    // But avoiding the logic wastes time in the far-more-common case
205
    // that we're *not* adding a new using directive.
206
207
    // Tag declarations always go at the end of the list so that an
208
    // iterator which points at the first tag will start a span of
209
    // decls that only contains tags.
210
2.55M
    if (D->hasTagIdentifierNamespace())
211
77.8k
      Vec.push_back(D);
212
213
    // Resolved using declarations go at the front of the list so that
214
    // they won't show up in other lookup results.  Unresolved using
215
    // declarations (which are always in IDNS_Using | IDNS_Ordinary)
216
    // follow that so that the using declarations will be contiguous.
217
2.47M
    else if (D->getIdentifierNamespace() & Decl::IDNS_Using) {
218
2.23k
      DeclsTy::iterator I = Vec.begin();
219
2.23k
      if (D->getIdentifierNamespace() != Decl::IDNS_Using) {
220
465
        while (I != Vec.end() &&
221
50
               (*I)->getIdentifierNamespace() == Decl::IDNS_Using)
222
1
          ++I;
223
464
      }
224
2.23k
      Vec.insert(I, D);
225
226
    // All other declarations go at the end of the list, but before any
227
    // tag declarations.  But we can be clever about tag declarations
228
    // because there can only ever be one in a scope.
229
2.47M
    } else if (!Vec.empty() && 
Vec.back()->hasTagIdentifierNamespace()1.98M
) {
230
34.1k
      NamedDecl *TagD = Vec.back();
231
34.1k
      Vec.back() = D;
232
34.1k
      Vec.push_back(TagD);
233
34.1k
    } else
234
2.44M
      Vec.push_back(D);
235
2.55M
  }
236
};
237
238
class StoredDeclsMap
239
    : public llvm::SmallDenseMap<DeclarationName, StoredDeclsList, 4> {
240
public:
241
  static void DestroyAll(StoredDeclsMap *Map, bool Dependent);
242
243
private:
244
  friend class ASTContext; // walks the chain deleting these
245
  friend class DeclContext;
246
247
  llvm::PointerIntPair<StoredDeclsMap*, 1> Previous;
248
};
249
250
class DependentStoredDeclsMap : public StoredDeclsMap {
251
public:
252
367k
  DependentStoredDeclsMap() = default;
253
254
private:
255
  friend class DeclContext; // iterates over diagnostics
256
  friend class DependentDiagnostic;
257
258
  DependentDiagnostic *FirstDiagnostic = nullptr;
259
};
260
261
} // namespace clang
262
263
#endif // LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H