Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Sema/Lookup.h
Line
Count
Source (jump to first uncovered line)
1
//===- Lookup.h - Classes for name lookup -----------------------*- 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 LookupResult class, which is integral to
10
// Sema's name-lookup subsystem.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_SEMA_LOOKUP_H
15
#define LLVM_CLANG_SEMA_LOOKUP_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 "clang/AST/Type.h"
22
#include "clang/AST/UnresolvedSet.h"
23
#include "clang/Basic/LLVM.h"
24
#include "clang/Basic/LangOptions.h"
25
#include "clang/Basic/SourceLocation.h"
26
#include "clang/Basic/Specifiers.h"
27
#include "clang/Sema/Sema.h"
28
#include "llvm/ADT/MapVector.h"
29
#include "llvm/ADT/Optional.h"
30
#include "llvm/ADT/STLExtras.h"
31
#include "llvm/Support/Casting.h"
32
#include <cassert>
33
#include <utility>
34
35
namespace clang {
36
37
class CXXBasePaths;
38
39
/// Represents the results of name lookup.
40
///
41
/// An instance of the LookupResult class captures the results of a
42
/// single name lookup, which can return no result (nothing found),
43
/// a single declaration, a set of overloaded functions, or an
44
/// ambiguity. Use the getKind() method to determine which of these
45
/// results occurred for a given lookup.
46
class LookupResult {
47
public:
48
  enum LookupResultKind {
49
    /// No entity found met the criteria.
50
    NotFound = 0,
51
52
    /// No entity found met the criteria within the current
53
    /// instantiation,, but there were dependent base classes of the
54
    /// current instantiation that could not be searched.
55
    NotFoundInCurrentInstantiation,
56
57
    /// Name lookup found a single declaration that met the
58
    /// criteria.  getFoundDecl() will return this declaration.
59
    Found,
60
61
    /// Name lookup found a set of overloaded functions that
62
    /// met the criteria.
63
    FoundOverloaded,
64
65
    /// Name lookup found an unresolvable value declaration
66
    /// and cannot yet complete.  This only happens in C++ dependent
67
    /// contexts with dependent using declarations.
68
    FoundUnresolvedValue,
69
70
    /// Name lookup results in an ambiguity; use
71
    /// getAmbiguityKind to figure out what kind of ambiguity
72
    /// we have.
73
    Ambiguous
74
  };
75
76
  enum AmbiguityKind {
77
    /// Name lookup results in an ambiguity because multiple
78
    /// entities that meet the lookup criteria were found in
79
    /// subobjects of different types. For example:
80
    /// @code
81
    /// struct A { void f(int); }
82
    /// struct B { void f(double); }
83
    /// struct C : A, B { };
84
    /// void test(C c) {
85
    ///   c.f(0); // error: A::f and B::f come from subobjects of different
86
    ///           // types. overload resolution is not performed.
87
    /// }
88
    /// @endcode
89
    AmbiguousBaseSubobjectTypes,
90
91
    /// Name lookup results in an ambiguity because multiple
92
    /// nonstatic entities that meet the lookup criteria were found
93
    /// in different subobjects of the same type. For example:
94
    /// @code
95
    /// struct A { int x; };
96
    /// struct B : A { };
97
    /// struct C : A { };
98
    /// struct D : B, C { };
99
    /// int test(D d) {
100
    ///   return d.x; // error: 'x' is found in two A subobjects (of B and C)
101
    /// }
102
    /// @endcode
103
    AmbiguousBaseSubobjects,
104
105
    /// Name lookup results in an ambiguity because multiple definitions
106
    /// of entity that meet the lookup criteria were found in different
107
    /// declaration contexts.
108
    /// @code
109
    /// namespace A {
110
    ///   int i;
111
    ///   namespace B { int i; }
112
    ///   int test() {
113
    ///     using namespace B;
114
    ///     return i; // error 'i' is found in namespace A and A::B
115
    ///    }
116
    /// }
117
    /// @endcode
118
    AmbiguousReference,
119
120
    /// Name lookup results in an ambiguity because an entity with a
121
    /// tag name was hidden by an entity with an ordinary name from
122
    /// a different context.
123
    /// @code
124
    /// namespace A { struct Foo {}; }
125
    /// namespace B { void Foo(); }
126
    /// namespace C {
127
    ///   using namespace A;
128
    ///   using namespace B;
129
    /// }
130
    /// void test() {
131
    ///   C::Foo(); // error: tag 'A::Foo' is hidden by an object in a
132
    ///             // different namespace
133
    /// }
134
    /// @endcode
135
    AmbiguousTagHiding
136
  };
137
138
  /// A little identifier for flagging temporary lookup results.
139
  enum TemporaryToken {
140
    Temporary
141
  };
142
143
  using iterator = UnresolvedSetImpl::iterator;
144
145
  LookupResult(Sema &SemaRef, const DeclarationNameInfo &NameInfo,
146
               Sema::LookupNameKind LookupKind,
147
               Sema::RedeclarationKind Redecl = Sema::NotForRedeclaration)
148
      : SemaPtr(&SemaRef), NameInfo(NameInfo), LookupKind(LookupKind),
149
        Redecl(Redecl != Sema::NotForRedeclaration),
150
        ExternalRedecl(Redecl == Sema::ForExternalRedeclaration),
151
49.5M
        Diagnose(Redecl == Sema::NotForRedeclaration) {
152
49.5M
    configure();
153
49.5M
  }
154
155
  // TODO: consider whether this constructor should be restricted to take
156
  // as input a const IdentifierInfo* (instead of Name),
157
  // forcing other cases towards the constructor taking a DNInfo.
158
  LookupResult(Sema &SemaRef, DeclarationName Name,
159
               SourceLocation NameLoc, Sema::LookupNameKind LookupKind,
160
               Sema::RedeclarationKind Redecl = Sema::NotForRedeclaration)
161
      : SemaPtr(&SemaRef), NameInfo(Name, NameLoc), LookupKind(LookupKind),
162
        Redecl(Redecl != Sema::NotForRedeclaration),
163
        ExternalRedecl(Redecl == Sema::ForExternalRedeclaration),
164
149M
        Diagnose(Redecl == Sema::NotForRedeclaration) {
165
149M
    configure();
166
149M
  }
167
168
  /// Creates a temporary lookup result, initializing its core data
169
  /// using the information from another result.  Diagnostics are always
170
  /// disabled.
171
  LookupResult(TemporaryToken _, const LookupResult &Other)
172
      : SemaPtr(Other.SemaPtr), NameInfo(Other.NameInfo),
173
        LookupKind(Other.LookupKind), IDNS(Other.IDNS), Redecl(Other.Redecl),
174
        ExternalRedecl(Other.ExternalRedecl), HideTags(Other.HideTags),
175
        AllowHidden(Other.AllowHidden),
176
34.4k
        TemplateNameLookup(Other.TemplateNameLookup) {}
177
178
  // FIXME: Remove these deleted methods once the default build includes
179
  // -Wdeprecated.
180
  LookupResult(const LookupResult &) = delete;
181
  LookupResult &operator=(const LookupResult &) = delete;
182
183
  LookupResult(LookupResult &&Other)
184
      : ResultKind(std::move(Other.ResultKind)),
185
        Ambiguity(std::move(Other.Ambiguity)), Decls(std::move(Other.Decls)),
186
        Paths(std::move(Other.Paths)),
187
        NamingClass(std::move(Other.NamingClass)),
188
        BaseObjectType(std::move(Other.BaseObjectType)),
189
        SemaPtr(std::move(Other.SemaPtr)), NameInfo(std::move(Other.NameInfo)),
190
        NameContextRange(std::move(Other.NameContextRange)),
191
        LookupKind(std::move(Other.LookupKind)), IDNS(std::move(Other.IDNS)),
192
        Redecl(std::move(Other.Redecl)),
193
        ExternalRedecl(std::move(Other.ExternalRedecl)),
194
        HideTags(std::move(Other.HideTags)),
195
        Diagnose(std::move(Other.Diagnose)),
196
        AllowHidden(std::move(Other.AllowHidden)),
197
        Shadowed(std::move(Other.Shadowed)),
198
26
        TemplateNameLookup(std::move(Other.TemplateNameLookup)) {
199
26
    Other.Paths = nullptr;
200
26
    Other.Diagnose = false;
201
26
  }
202
203
0
  LookupResult &operator=(LookupResult &&Other) {
204
0
    ResultKind = std::move(Other.ResultKind);
205
0
    Ambiguity = std::move(Other.Ambiguity);
206
0
    Decls = std::move(Other.Decls);
207
0
    Paths = std::move(Other.Paths);
208
0
    NamingClass = std::move(Other.NamingClass);
209
0
    BaseObjectType = std::move(Other.BaseObjectType);
210
0
    SemaPtr = std::move(Other.SemaPtr);
211
0
    NameInfo = std::move(Other.NameInfo);
212
0
    NameContextRange = std::move(Other.NameContextRange);
213
0
    LookupKind = std::move(Other.LookupKind);
214
0
    IDNS = std::move(Other.IDNS);
215
0
    Redecl = std::move(Other.Redecl);
216
0
    ExternalRedecl = std::move(Other.ExternalRedecl);
217
0
    HideTags = std::move(Other.HideTags);
218
0
    Diagnose = std::move(Other.Diagnose);
219
0
    AllowHidden = std::move(Other.AllowHidden);
220
0
    Shadowed = std::move(Other.Shadowed);
221
0
    TemplateNameLookup = std::move(Other.TemplateNameLookup);
222
0
    Other.Paths = nullptr;
223
0
    Other.Diagnose = false;
224
0
    return *this;
225
0
  }
226
227
199M
  ~LookupResult() {
228
199M
    if (Diagnose) 
diagnose()137M
;
229
199M
    if (Paths) 
deletePaths(Paths)106
;
230
199M
  }
231
232
  /// Gets the name info to look up.
233
18.6M
  const DeclarationNameInfo &getLookupNameInfo() const {
234
18.6M
    return NameInfo;
235
18.6M
  }
236
237
  /// Sets the name info to look up.
238
0
  void setLookupNameInfo(const DeclarationNameInfo &NameInfo) {
239
0
    this->NameInfo = NameInfo;
240
0
  }
241
242
  /// Gets the name to look up.
243
592M
  DeclarationName getLookupName() const {
244
592M
    return NameInfo.getName();
245
592M
  }
246
247
  /// Sets the name to look up.
248
99.7k
  void setLookupName(DeclarationName Name) {
249
99.7k
    NameInfo.setName(Name);
250
99.7k
  }
251
252
  /// Gets the kind of lookup to perform.
253
359M
  Sema::LookupNameKind getLookupKind() const {
254
359M
    return LookupKind;
255
359M
  }
256
257
  /// True if this lookup is just looking for an existing declaration.
258
296M
  bool isForRedeclaration() const {
259
296M
    return Redecl;
260
296M
  }
261
262
  /// True if this lookup is just looking for an existing declaration to link
263
  /// against a declaration with external linkage.
264
27.1k
  bool isForExternalRedeclaration() const {
265
27.1k
    return ExternalRedecl;
266
27.1k
  }
267
268
527
  Sema::RedeclarationKind redeclarationKind() const {
269
527
    return ExternalRedecl ? 
Sema::ForExternalRedeclaration0
:
270
527
           Redecl ? 
Sema::ForVisibleRedeclaration0
: Sema::NotForRedeclaration;
271
527
  }
272
273
  /// Specify whether hidden declarations are visible, e.g.,
274
  /// for recovery reasons.
275
20.6k
  void setAllowHidden(bool AH) {
276
20.6k
    AllowHidden = AH;
277
20.6k
  }
278
279
  /// Determine whether this lookup is permitted to see hidden
280
  /// declarations, such as those in modules that have not yet been imported.
281
28.1k
  bool isHiddenDeclarationVisible(NamedDecl *ND) const {
282
28.1k
    return AllowHidden ||
283
28.1k
           
(27.1k
isForExternalRedeclaration()27.1k
&&
ND->isExternallyDeclarable()16.8k
);
284
28.1k
  }
285
286
  /// Sets whether tag declarations should be hidden by non-tag
287
  /// declarations during resolution.  The default is true.
288
425k
  void setHideTags(bool Hide) {
289
425k
    HideTags = Hide;
290
425k
  }
291
292
  /// Sets whether this is a template-name lookup. For template-name lookups,
293
  /// injected-class-names are treated as naming a template rather than a
294
  /// template specialization.
295
4.11M
  void setTemplateNameLookup(bool TemplateName) {
296
4.11M
    TemplateNameLookup = TemplateName;
297
4.11M
  }
298
299
554k
  bool isTemplateNameLookup() const { return TemplateNameLookup; }
300
301
169M
  bool isAmbiguous() const {
302
169M
    return getResultKind() == Ambiguous;
303
169M
  }
304
305
  /// Determines if this names a single result which is not an
306
  /// unresolved value using decl.  If so, it is safe to call
307
  /// getFoundDecl().
308
85.3M
  bool isSingleResult() const {
309
85.3M
    return getResultKind() == Found;
310
85.3M
  }
311
312
  /// Determines if the results are overloaded.
313
2.75M
  bool isOverloadedResult() const {
314
2.75M
    return getResultKind() == FoundOverloaded;
315
2.75M
  }
316
317
3.26M
  bool isUnresolvableResult() const {
318
3.26M
    return getResultKind() == FoundUnresolvedValue;
319
3.26M
  }
320
321
619M
  LookupResultKind getResultKind() const {
322
619M
    assert(checkDebugAssumptions());
323
0
    return ResultKind;
324
619M
  }
325
326
261
  AmbiguityKind getAmbiguityKind() const {
327
261
    assert(isAmbiguous());
328
0
    return Ambiguity;
329
261
  }
330
331
4.19k
  const UnresolvedSetImpl &asUnresolvedSet() const {
332
4.19k
    return Decls;
333
4.19k
  }
334
335
394M
  iterator begin() const { return iterator(Decls.begin()); }
336
208M
  iterator end() const { return iterator(Decls.end()); }
337
338
  /// Return true if no decls were found
339
260M
  bool empty() const { return Decls.empty(); }
340
341
  /// Return the base paths structure that's associated with
342
  /// these results, or null if none is.
343
89
  CXXBasePaths *getBasePaths() const {
344
89
    return Paths;
345
89
  }
346
347
  /// Determine whether the given declaration is visible to the
348
  /// program.
349
277M
  static bool isVisible(Sema &SemaRef, NamedDecl *D) {
350
    // If this declaration is not hidden, it's visible.
351
277M
    if (D->isUnconditionallyVisible())
352
277M
      return true;
353
354
    // During template instantiation, we can refer to hidden declarations, if
355
    // they were visible in any module along the path of instantiation.
356
98.3k
    return isVisibleSlow(SemaRef, D);
357
277M
  }
358
359
  /// Retrieve the accepted (re)declaration of the given declaration,
360
  /// if there is one.
361
279M
  NamedDecl *getAcceptableDecl(NamedDecl *D) const {
362
279M
    if (!D->isInIdentifierNamespace(IDNS))
363
2.53M
      return nullptr;
364
365
277M
    if (isVisible(getSema(), D) || 
isHiddenDeclarationVisible(D)28.1k
)
366
277M
      return D;
367
368
10.4k
    return getAcceptableDeclSlow(D);
369
277M
  }
370
371
private:
372
  static bool isVisibleSlow(Sema &SemaRef, NamedDecl *D);
373
  NamedDecl *getAcceptableDeclSlow(NamedDecl *D) const;
374
375
public:
376
  /// Returns the identifier namespace mask for this lookup.
377
382M
  unsigned getIdentifierNamespace() const {
378
382M
    return IDNS;
379
382M
  }
380
381
  /// Returns whether these results arose from performing a
382
  /// lookup into a class.
383
137M
  bool isClassLookup() const {
384
137M
    return NamingClass != nullptr;
385
137M
  }
386
387
  /// Returns the 'naming class' for this lookup, i.e. the
388
  /// class which was looked into to find these results.
389
  ///
390
  /// C++0x [class.access.base]p5:
391
  ///   The access to a member is affected by the class in which the
392
  ///   member is named. This naming class is the class in which the
393
  ///   member name was looked up and found. [Note: this class can be
394
  ///   explicit, e.g., when a qualified-id is used, or implicit,
395
  ///   e.g., when a class member access operator (5.2.5) is used
396
  ///   (including cases where an implicit "this->" is added). If both
397
  ///   a class member access operator and a qualified-id are used to
398
  ///   name the member (as in p->T::m), the class naming the member
399
  ///   is the class named by the nested-name-specifier of the
400
  ///   qualified-id (that is, T). -- end note ]
401
  ///
402
  /// This is set by the lookup routines when they find results in a class.
403
8.53M
  CXXRecordDecl *getNamingClass() const {
404
8.53M
    return NamingClass;
405
8.53M
  }
406
407
  /// Sets the 'naming class' for this lookup.
408
6.82M
  void setNamingClass(CXXRecordDecl *Record) {
409
6.82M
    NamingClass = Record;
410
6.82M
  }
411
412
  /// Returns the base object type associated with this lookup;
413
  /// important for [class.protected].  Most lookups do not have an
414
  /// associated base object.
415
846k
  QualType getBaseObjectType() const {
416
846k
    return BaseObjectType;
417
846k
  }
418
419
  /// Sets the base object type for this lookup.
420
1.42M
  void setBaseObjectType(QualType T) {
421
1.42M
    BaseObjectType = T;
422
1.42M
  }
423
424
  /// Add a declaration to these results with its natural access.
425
  /// Does not test the acceptance criteria.
426
281M
  void addDecl(NamedDecl *D) {
427
281M
    addDecl(D, D->getAccess());
428
281M
  }
429
430
  /// Add a declaration to these results with the given access.
431
  /// Does not test the acceptance criteria.
432
282M
  void addDecl(NamedDecl *D, AccessSpecifier AS) {
433
282M
    Decls.addDecl(D, AS);
434
282M
    ResultKind = Found;
435
282M
  }
436
437
  /// Add all the declarations from another set of lookup
438
  /// results.
439
107
  void addAllDecls(const LookupResult &Other) {
440
107
    Decls.append(Other.Decls.begin(), Other.Decls.end());
441
107
    ResultKind = Found;
442
107
  }
443
444
  /// Determine whether no result was found because we could not
445
  /// search into dependent base classes of the current instantiation.
446
3.92M
  bool wasNotFoundInCurrentInstantiation() const {
447
3.92M
    return ResultKind == NotFoundInCurrentInstantiation;
448
3.92M
  }
449
450
  /// Note that while no result was found in the current instantiation,
451
  /// there were dependent base classes that could not be searched.
452
4.36k
  void setNotFoundInCurrentInstantiation() {
453
4.36k
    assert(ResultKind == NotFound && Decls.empty());
454
0
    ResultKind = NotFoundInCurrentInstantiation;
455
4.36k
  }
456
457
  /// Determine whether the lookup result was shadowed by some other
458
  /// declaration that lookup ignored.
459
17.6M
  bool isShadowed() const { return Shadowed; }
460
461
  /// Note that we found and ignored a declaration while performing
462
  /// lookup.
463
58
  void setShadowed() { Shadowed = true; }
464
465
  /// Resolves the result kind of the lookup, possibly hiding
466
  /// decls.
467
  ///
468
  /// This should be called in any environment where lookup might
469
  /// generate multiple lookup results.
470
  void resolveKind();
471
472
  /// Re-resolves the result kind of the lookup after a set of
473
  /// removals has been performed.
474
895k
  void resolveKindAfterFilter() {
475
895k
    if (Decls.empty()) {
476
881k
      if (ResultKind != NotFoundInCurrentInstantiation)
477
881k
        ResultKind = NotFound;
478
479
881k
      if (Paths) {
480
1
        deletePaths(Paths);
481
1
        Paths = nullptr;
482
1
      }
483
881k
    } else {
484
13.5k
      llvm::Optional<AmbiguityKind> SavedAK;
485
13.5k
      bool WasAmbiguous = false;
486
13.5k
      if (ResultKind == Ambiguous) {
487
11
        SavedAK = Ambiguity;
488
11
        WasAmbiguous = true;
489
11
      }
490
13.5k
      ResultKind = Found;
491
13.5k
      resolveKind();
492
493
      // If we didn't make the lookup unambiguous, restore the old
494
      // ambiguity kind.
495
13.5k
      if (ResultKind == Ambiguous) {
496
0
        (void)WasAmbiguous;
497
0
        assert(WasAmbiguous);
498
0
        Ambiguity = SavedAK.getValue();
499
13.5k
      } else if (Paths) {
500
4
        deletePaths(Paths);
501
4
        Paths = nullptr;
502
4
      }
503
13.5k
    }
504
895k
  }
505
506
  template <class DeclClass>
507
24.7M
  DeclClass *getAsSingle() const {
508
24.7M
    if (getResultKind() != Found) 
return nullptr7.57M
;
509
17.2M
    return dyn_cast<DeclClass>(getFoundDecl());
510
24.7M
  }
clang::VarDecl* clang::LookupResult::getAsSingle<clang::VarDecl>() const
Line
Count
Source
507
5.74k
  DeclClass *getAsSingle() const {
508
5.74k
    if (getResultKind() != Found) 
return nullptr16
;
509
5.72k
    return dyn_cast<DeclClass>(getFoundDecl());
510
5.74k
  }
clang::ObjCIvarDecl* clang::LookupResult::getAsSingle<clang::ObjCIvarDecl>() const
Line
Count
Source
507
77
  DeclClass *getAsSingle() const {
508
77
    if (getResultKind() != Found) 
return nullptr0
;
509
77
    return dyn_cast<DeclClass>(getFoundDecl());
510
77
  }
clang::TypeDecl* clang::LookupResult::getAsSingle<clang::TypeDecl>() const
Line
Count
Source
507
845k
  DeclClass *getAsSingle() const {
508
845k
    if (getResultKind() != Found) 
return nullptr716
;
509
844k
    return dyn_cast<DeclClass>(getFoundDecl());
510
845k
  }
clang::ClassTemplateDecl* clang::LookupResult::getAsSingle<clang::ClassTemplateDecl>() const
Line
Count
Source
507
15.1k
  DeclClass *getAsSingle() const {
508
15.1k
    if (getResultKind() != Found) 
return nullptr7.68k
;
509
7.44k
    return dyn_cast<DeclClass>(getFoundDecl());
510
15.1k
  }
clang::NamespaceDecl* clang::LookupResult::getAsSingle<clang::NamespaceDecl>() const
Line
Count
Source
507
215k
  DeclClass *getAsSingle() const {
508
215k
    if (getResultKind() != Found) 
return nullptr39.6k
;
509
175k
    return dyn_cast<DeclClass>(getFoundDecl());
510
215k
  }
clang::UnresolvedUsingIfExistsDecl* clang::LookupResult::getAsSingle<clang::UnresolvedUsingIfExistsDecl>() const
Line
Count
Source
507
32
  DeclClass *getAsSingle() const {
508
32
    if (getResultKind() != Found) 
return nullptr0
;
509
32
    return dyn_cast<DeclClass>(getFoundDecl());
510
32
  }
clang::EnumConstantDecl* clang::LookupResult::getAsSingle<clang::EnumConstantDecl>() const
Line
Count
Source
507
213k
  DeclClass *getAsSingle() const {
508
213k
    if (getResultKind() != Found) 
return nullptr41.2k
;
509
171k
    return dyn_cast<DeclClass>(getFoundDecl());
510
213k
  }
clang::TypeAliasTemplateDecl* clang::LookupResult::getAsSingle<clang::TypeAliasTemplateDecl>() const
Line
Count
Source
507
520
  DeclClass *getAsSingle() const {
508
520
    if (getResultKind() != Found) 
return nullptr0
;
509
520
    return dyn_cast<DeclClass>(getFoundDecl());
510
520
  }
clang::VarTemplateDecl* clang::LookupResult::getAsSingle<clang::VarTemplateDecl>() const
Line
Count
Source
507
397k
  DeclClass *getAsSingle() const {
508
397k
    if (getResultKind() != Found) 
return nullptr386k
;
509
10.1k
    return dyn_cast<DeclClass>(getFoundDecl());
510
397k
  }
clang::FunctionTemplateDecl* clang::LookupResult::getAsSingle<clang::FunctionTemplateDecl>() const
Line
Count
Source
507
15.4M
  DeclClass *getAsSingle() const {
508
15.4M
    if (getResultKind() != Found) 
return nullptr0
;
509
15.4M
    return dyn_cast<DeclClass>(getFoundDecl());
510
15.4M
  }
clang::FieldDecl* clang::LookupResult::getAsSingle<clang::FieldDecl>() const
Line
Count
Source
507
346
  DeclClass *getAsSingle() const {
508
346
    if (getResultKind() != Found) 
return nullptr3
;
509
343
    return dyn_cast<DeclClass>(getFoundDecl());
510
346
  }
clang::IndirectFieldDecl* clang::LookupResult::getAsSingle<clang::IndirectFieldDecl>() const
Line
Count
Source
507
7
  DeclClass *getAsSingle() const {
508
7
    if (getResultKind() != Found) 
return nullptr3
;
509
4
    return dyn_cast<DeclClass>(getFoundDecl());
510
7
  }
clang::RecordDecl* clang::LookupResult::getAsSingle<clang::RecordDecl>() const
Line
Count
Source
507
593
  DeclClass *getAsSingle() const {
508
593
    if (getResultKind() != Found) 
return nullptr4
;
509
589
    return dyn_cast<DeclClass>(getFoundDecl());
510
593
  }
clang::TypedefNameDecl* clang::LookupResult::getAsSingle<clang::TypedefNameDecl>() const
Line
Count
Source
507
108
  DeclClass *getAsSingle() const {
508
108
    if (getResultKind() != Found) 
return nullptr0
;
509
108
    return dyn_cast<DeclClass>(getFoundDecl());
510
108
  }
clang::EnumDecl* clang::LookupResult::getAsSingle<clang::EnumDecl>() const
Line
Count
Source
507
7.11k
  DeclClass *getAsSingle() const {
508
7.11k
    if (getResultKind() != Found) 
return nullptr0
;
509
7.11k
    return dyn_cast<DeclClass>(getFoundDecl());
510
7.11k
  }
clang::TagDecl* clang::LookupResult::getAsSingle<clang::TagDecl>() const
Line
Count
Source
507
940
  DeclClass *getAsSingle() const {
508
940
    if (getResultKind() != Found) 
return nullptr781
;
509
159
    return dyn_cast<DeclClass>(getFoundDecl());
510
940
  }
clang::NamedDecl* clang::LookupResult::getAsSingle<clang::NamedDecl>() const
Line
Count
Source
507
6.79M
  DeclClass *getAsSingle() const {
508
6.79M
    if (getResultKind() != Found) 
return nullptr6.31M
;
509
480k
    return dyn_cast<DeclClass>(getFoundDecl());
510
6.79M
  }
clang::FunctionDecl* clang::LookupResult::getAsSingle<clang::FunctionDecl>() const
Line
Count
Source
507
4.40k
  DeclClass *getAsSingle() const {
508
4.40k
    if (getResultKind() != Found) 
return nullptr0
;
509
4.40k
    return dyn_cast<DeclClass>(getFoundDecl());
510
4.40k
  }
clang::TemplateDecl* clang::LookupResult::getAsSingle<clang::TemplateDecl>() const
Line
Count
Source
507
394k
  DeclClass *getAsSingle() const {
508
394k
    if (getResultKind() != Found) 
return nullptr386k
;
509
7.83k
    return dyn_cast<DeclClass>(getFoundDecl());
510
394k
  }
clang::ConceptDecl* clang::LookupResult::getAsSingle<clang::ConceptDecl>() const
Line
Count
Source
507
388k
  DeclClass *getAsSingle() const {
508
388k
    if (getResultKind() != Found) 
return nullptr386k
;
509
1.55k
    return dyn_cast<DeclClass>(getFoundDecl());
510
388k
  }
511
512
  /// Fetch the unique decl found by this lookup.  Asserts
513
  /// that one was found.
514
  ///
515
  /// This is intended for users who have examined the result kind
516
  /// and are certain that there is only one result.
517
155M
  NamedDecl *getFoundDecl() const {
518
155M
    assert(getResultKind() == Found
519
155M
           && "getFoundDecl called on non-unique result");
520
0
    return (*begin())->getUnderlyingDecl();
521
155M
  }
522
523
  /// Fetches a representative decl.  Useful for lazy diagnostics.
524
24.5M
  NamedDecl *getRepresentativeDecl() const {
525
24.5M
    assert(!Decls.empty() && "cannot get representative of empty set");
526
0
    return *begin();
527
24.5M
  }
528
529
  /// Asks if the result is a single tag decl.
530
33.4M
  bool isSingleTagDecl() const {
531
33.4M
    return getResultKind() == Found && 
isa<TagDecl>(getFoundDecl())1.96M
;
532
33.4M
  }
533
534
  /// Make these results show that the name was found in
535
  /// base classes of different types.
536
  ///
537
  /// The given paths object is copied and invalidated.
538
  void setAmbiguousBaseSubobjectTypes(CXXBasePaths &P);
539
540
  /// Make these results show that the name was found in
541
  /// distinct base classes of the same type.
542
  ///
543
  /// The given paths object is copied and invalidated.
544
  void setAmbiguousBaseSubobjects(CXXBasePaths &P);
545
546
  /// Make these results show that the name was found in
547
  /// different contexts and a tag decl was hidden by an ordinary
548
  /// decl in a different context.
549
6
  void setAmbiguousQualifiedTagHiding() {
550
6
    setAmbiguous(AmbiguousTagHiding);
551
6
  }
552
553
  /// Clears out any current state.
554
381k
  LLVM_ATTRIBUTE_REINITIALIZES void clear() {
555
381k
    ResultKind = NotFound;
556
381k
    Decls.clear();
557
381k
    if (Paths) 
deletePaths(Paths)10
;
558
381k
    Paths = nullptr;
559
381k
    NamingClass = nullptr;
560
381k
    Shadowed = false;
561
381k
  }
562
563
  /// Clears out any current state and re-initializes for a
564
  /// different kind of lookup.
565
9.85k
  void clear(Sema::LookupNameKind Kind) {
566
9.85k
    clear();
567
9.85k
    LookupKind = Kind;
568
9.85k
    configure();
569
9.85k
  }
570
571
  /// Change this lookup's redeclaration kind.
572
131k
  void setRedeclarationKind(Sema::RedeclarationKind RK) {
573
131k
    Redecl = (RK != Sema::NotForRedeclaration);
574
131k
    ExternalRedecl = (RK == Sema::ForExternalRedeclaration);
575
131k
    configure();
576
131k
  }
577
578
  void dump();
579
  void print(raw_ostream &);
580
581
  /// Suppress the diagnostics that would normally fire because of this
582
  /// lookup.  This happens during (e.g.) redeclaration lookups.
583
8.20M
  void suppressDiagnostics() {
584
8.20M
    Diagnose = false;
585
8.20M
  }
586
587
  /// Determines whether this lookup is suppressing diagnostics.
588
8
  bool isSuppressingDiagnostics() const {
589
8
    return !Diagnose;
590
8
  }
591
592
  /// Sets a 'context' source range.
593
916k
  void setContextRange(SourceRange SR) {
594
916k
    NameContextRange = SR;
595
916k
  }
596
597
  /// Gets the source range of the context of this name; for C++
598
  /// qualified lookups, this is the source range of the scope
599
  /// specifier.
600
191
  SourceRange getContextRange() const {
601
191
    return NameContextRange;
602
191
  }
603
604
  /// Gets the location of the identifier.  This isn't always defined:
605
  /// sometimes we're doing lookups on synthesized names.
606
93.7M
  SourceLocation getNameLoc() const {
607
93.7M
    return NameInfo.getLoc();
608
93.7M
  }
609
610
  /// Get the Sema object that this lookup result is searching
611
  /// with.
612
677M
  Sema &getSema() const { return *SemaPtr; }
613
614
  /// A class for iterating through a result set and possibly
615
  /// filtering out results.  The results returned are possibly
616
  /// sugared.
617
  class Filter {
618
    friend class LookupResult;
619
620
    LookupResult &Results;
621
    LookupResult::iterator I;
622
    bool Changed = false;
623
    bool CalledDone = false;
624
625
37.2M
    Filter(LookupResult &Results) : Results(Results), I(Results.begin()) {}
626
627
  public:
628
    Filter(Filter &&F)
629
        : Results(F.Results), I(F.I), Changed(F.Changed),
630
0
          CalledDone(F.CalledDone) {
631
0
      F.CalledDone = true;
632
0
    }
633
634
37.2M
    ~Filter() {
635
37.2M
      assert(CalledDone &&
636
37.2M
             "LookupResult::Filter destroyed without done() call");
637
37.2M
    }
638
639
108M
    bool hasNext() const {
640
108M
      return I != Results.end();
641
108M
    }
642
643
71.7M
    NamedDecl *next() {
644
71.7M
      assert(I != Results.end() && "next() called on empty filter");
645
0
      return *I++;
646
71.7M
    }
647
648
    /// Restart the iteration.
649
12
    void restart() {
650
12
      I = Results.begin();
651
12
    }
652
653
    /// Erase the last element returned from this iterator.
654
1.12M
    void erase() {
655
1.12M
      Results.Decls.erase(--I);
656
1.12M
      Changed = true;
657
1.12M
    }
658
659
    /// Replaces the current entry with the given one, preserving the
660
    /// access bits.
661
0
    void replace(NamedDecl *D) {
662
0
      Results.Decls.replace(I-1, D);
663
0
      Changed = true;
664
0
    }
665
666
    /// Replaces the current entry with the given one.
667
0
    void replace(NamedDecl *D, AccessSpecifier AS) {
668
0
      Results.Decls.replace(I-1, D, AS);
669
0
      Changed = true;
670
0
    }
671
672
37.2M
    void done() {
673
37.2M
      assert(!CalledDone && "done() called twice");
674
0
      CalledDone = true;
675
676
37.2M
      if (Changed)
677
895k
        Results.resolveKindAfterFilter();
678
37.2M
    }
679
  };
680
681
  /// Create a filter for this result set.
682
37.2M
  Filter makeFilter() {
683
37.2M
    return Filter(*this);
684
37.2M
  }
685
686
417M
  void setFindLocalExtern(bool FindLocalExtern) {
687
417M
    if (FindLocalExtern)
688
234M
      IDNS |= Decl::IDNS_LocalExtern;
689
183M
    else
690
183M
      IDNS &= ~Decl::IDNS_LocalExtern;
691
417M
  }
692
693
private:
694
137M
  void diagnose() {
695
137M
    if (isAmbiguous())
696
191
      getSema().DiagnoseAmbiguousLookup(*this);
697
137M
    else if (isClassLookup() && 
getSema().getLangOpts().AccessControl4.68M
)
698
4.47M
      getSema().CheckLookupAccess(*this);
699
137M
  }
700
701
4.65k
  void setAmbiguous(AmbiguityKind AK) {
702
4.65k
    ResultKind = Ambiguous;
703
4.65k
    Ambiguity = AK;
704
4.65k
  }
705
706
  void addDeclsFromBasePaths(const CXXBasePaths &P);
707
  void configure();
708
709
  bool checkDebugAssumptions() const;
710
711
1.47k
  bool checkUnresolved() const {
712
1.68k
    for (iterator I = begin(), E = end(); I != E; 
++I210
)
713
1.68k
      if (isa<UnresolvedUsingValueDecl>((*I)->getUnderlyingDecl()))
714
1.47k
        return true;
715
0
    return false;
716
1.47k
  }
717
718
  static void deletePaths(CXXBasePaths *);
719
720
  // Results.
721
  LookupResultKind ResultKind = NotFound;
722
  // ill-defined unless ambiguous. Still need to be initialized it will be
723
  // copied/moved.
724
  AmbiguityKind Ambiguity = {};
725
  UnresolvedSet<8> Decls;
726
  CXXBasePaths *Paths = nullptr;
727
  CXXRecordDecl *NamingClass = nullptr;
728
  QualType BaseObjectType;
729
730
  // Parameters.
731
  Sema *SemaPtr;
732
  DeclarationNameInfo NameInfo;
733
  SourceRange NameContextRange;
734
  Sema::LookupNameKind LookupKind;
735
  unsigned IDNS = 0; // set by configure()
736
737
  bool Redecl;
738
  bool ExternalRedecl;
739
740
  /// True if tag declarations should be hidden if non-tags
741
  ///   are present
742
  bool HideTags = true;
743
744
  bool Diagnose = false;
745
746
  /// True if we should allow hidden declarations to be 'visible'.
747
  bool AllowHidden = false;
748
749
  /// True if the found declarations were shadowed by some other
750
  /// declaration that we skipped. This only happens when \c LookupKind
751
  /// is \c LookupRedeclarationWithLinkage.
752
  bool Shadowed = false;
753
754
  /// True if we're looking up a template-name.
755
  bool TemplateNameLookup = false;
756
};
757
758
/// Consumes visible declarations found when searching for
759
/// all visible names within a given scope or context.
760
///
761
/// This abstract class is meant to be subclassed by clients of \c
762
/// Sema::LookupVisibleDecls(), each of which should override the \c
763
/// FoundDecl() function to process declarations as they are found.
764
class VisibleDeclConsumer {
765
public:
766
  /// Destroys the visible declaration consumer.
767
  virtual ~VisibleDeclConsumer();
768
769
  /// Determine whether hidden declarations (from unimported
770
  /// modules) should be given to this consumer. By default, they
771
  /// are not included.
772
  virtual bool includeHiddenDecls() const;
773
774
  /// Invoked each time \p Sema::LookupVisibleDecls() finds a
775
  /// declaration visible from the current scope or context.
776
  ///
777
  /// \param ND the declaration found.
778
  ///
779
  /// \param Hiding a declaration that hides the declaration \p ND,
780
  /// or NULL if no such declaration exists.
781
  ///
782
  /// \param Ctx the original context from which the lookup started.
783
  ///
784
  /// \param InBaseClass whether this declaration was found in base
785
  /// class of the context we searched.
786
  virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
787
                         bool InBaseClass) = 0;
788
789
  /// Callback to inform the client that Sema entered into a new context
790
  /// to find a visible declaration.
791
  //
792
  /// \param Ctx the context which Sema entered.
793
1.15k
  virtual void EnteredContext(DeclContext *Ctx) {}
794
};
795
796
/// A class for storing results from argument-dependent lookup.
797
class ADLResult {
798
private:
799
  /// A map from canonical decls to the 'most recent' decl.
800
  llvm::MapVector<NamedDecl*, NamedDecl*> Decls;
801
802
  struct select_second {
803
1.66M
    NamedDecl *operator()(std::pair<NamedDecl*, NamedDecl*> P) const {
804
1.66M
      return P.second;
805
1.66M
    }
806
  };
807
808
public:
809
  /// Adds a new ADL candidate to this map.
810
  void insert(NamedDecl *D);
811
812
  /// Removes any data associated with a given decl.
813
1.34M
  void erase(NamedDecl *D) {
814
1.34M
    Decls.erase(cast<NamedDecl>(D->getCanonicalDecl()));
815
1.34M
  }
816
817
  using iterator =
818
      llvm::mapped_iterator<decltype(Decls)::iterator, select_second>;
819
820
637k
  iterator begin() { return iterator(Decls.begin(), select_second()); }
821
637k
  iterator end() { return iterator(Decls.end(), select_second()); }
822
};
823
824
} // namespace clang
825
826
#endif // LLVM_CLANG_SEMA_LOOKUP_H