Coverage Report

Created: 2022-07-16 07:03

/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.3M
        Diagnose(Redecl == Sema::NotForRedeclaration) {
152
49.3M
    configure();
153
49.3M
  }
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
144M
        Diagnose(Redecl == Sema::NotForRedeclaration) {
165
144M
    configure();
166
144M
  }
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.9k
        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
194M
  ~LookupResult() {
228
194M
    if (Diagnose) 
diagnose()131M
;
229
194M
    if (Paths) 
deletePaths(Paths)107
;
230
194M
  }
231
232
  /// Gets the name info to look up.
233
21.8M
  const DeclarationNameInfo &getLookupNameInfo() const {
234
21.8M
    return NameInfo;
235
21.8M
  }
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
634M
  DeclarationName getLookupName() const {
244
634M
    return NameInfo.getName();
245
634M
  }
246
247
  /// Sets the name to look up.
248
107k
  void setLookupName(DeclarationName Name) {
249
107k
    NameInfo.setName(Name);
250
107k
  }
251
252
  /// Gets the kind of lookup to perform.
253
369M
  Sema::LookupNameKind getLookupKind() const {
254
369M
    return LookupKind;
255
369M
  }
256
257
  /// True if this lookup is just looking for an existing declaration.
258
303M
  bool isForRedeclaration() const {
259
303M
    return Redecl;
260
303M
  }
261
262
  /// True if this lookup is just looking for an existing declaration to link
263
  /// against a declaration with external linkage.
264
216k
  bool isForExternalRedeclaration() const {
265
216k
    return ExternalRedecl;
266
216k
  }
267
268
603
  Sema::RedeclarationKind redeclarationKind() const {
269
603
    return ExternalRedecl ? 
Sema::ForExternalRedeclaration0
:
270
603
           Redecl ? 
Sema::ForVisibleRedeclaration0
: Sema::NotForRedeclaration;
271
603
  }
272
273
  /// Specify whether hidden declarations are visible, e.g.,
274
  /// for recovery reasons.
275
24.4k
  void setAllowHidden(bool AH) {
276
24.4k
    AllowHidden = AH;
277
24.4k
  }
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
217k
  bool isHiddenDeclarationVisible(NamedDecl *ND) const {
282
217k
    return AllowHidden ||
283
217k
           
(216k
isForExternalRedeclaration()216k
&&
ND->isExternallyDeclarable()40.1k
);
284
217k
  }
285
286
  /// Sets whether tag declarations should be hidden by non-tag
287
  /// declarations during resolution.  The default is true.
288
454k
  void setHideTags(bool Hide) {
289
454k
    HideTags = Hide;
290
454k
  }
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
5.13M
  void setTemplateNameLookup(bool TemplateName) {
296
5.13M
    TemplateNameLookup = TemplateName;
297
5.13M
  }
298
299
710k
  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
89.0M
  bool isSingleResult() const {
309
89.0M
    return getResultKind() == Found;
310
89.0M
  }
311
312
  /// Determines if the results are overloaded.
313
3.71M
  bool isOverloadedResult() const {
314
3.71M
    return getResultKind() == FoundOverloaded;
315
3.71M
  }
316
317
4.37M
  bool isUnresolvableResult() const {
318
4.37M
    return getResultKind() == FoundUnresolvedValue;
319
4.37M
  }
320
321
605M
  LookupResultKind getResultKind() const {
322
605M
    assert(checkDebugAssumptions());
323
0
    return ResultKind;
324
605M
  }
325
326
267
  AmbiguityKind getAmbiguityKind() const {
327
267
    assert(isAmbiguous());
328
0
    return Ambiguity;
329
267
  }
330
331
4.81k
  const UnresolvedSetImpl &asUnresolvedSet() const {
332
4.81k
    return Decls;
333
4.81k
  }
334
335
394M
  iterator begin() const { return iterator(Decls.begin()); }
336
198M
  iterator end() const { return iterator(Decls.end()); }
337
338
  /// Return true if no decls were found
339
264M
  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
90
  CXXBasePaths *getBasePaths() const {
344
90
    return Paths;
345
90
  }
346
347
  /// Determine whether the given declaration is visible to the
348
  /// program.
349
  static bool isVisible(Sema &SemaRef, NamedDecl *D);
350
351
  static bool isReachable(Sema &SemaRef, NamedDecl *D);
352
353
  static bool isAcceptable(Sema &SemaRef, NamedDecl *D,
354
652k
                           Sema::AcceptableKind Kind) {
355
652k
    return Kind == Sema::AcceptableKind::Visible ? 
isVisible(SemaRef, D)307k
356
652k
                                                 : 
isReachable(SemaRef, D)344k
;
357
652k
  }
358
359
  /// Determine whether this lookup is permitted to see the declaration.
360
  /// Note that a reachable but not visible declaration inhabiting a namespace
361
  /// is not allowed to be seen during name lookup.
362
  ///
363
  /// For example:
364
  /// ```
365
  /// // m.cppm
366
  /// export module m;
367
  /// struct reachable { int v; }
368
  /// export auto func() { return reachable{43}; }
369
  /// // Use.cpp
370
  /// import m;
371
  /// auto Use() {
372
  ///   // Not valid. We couldn't see reachable here.
373
  ///   // So isAvailableForLookup would return false when we look
374
  ///   up 'reachable' here.
375
  ///   // return reachable(43).v;
376
  ///   // Valid. The field name 'v' is allowed during name lookup.
377
  ///   // So isAvailableForLookup would return true when we look up 'v' here.
378
  ///   return func().v;
379
  /// }
380
  /// ```
381
  static bool isAvailableForLookup(Sema &SemaRef, NamedDecl *ND);
382
383
  /// Retrieve the accepted (re)declaration of the given declaration,
384
  /// if there is one.
385
274M
  NamedDecl *getAcceptableDecl(NamedDecl *D) const {
386
274M
    if (!D->isInIdentifierNamespace(IDNS))
387
3.21M
      return nullptr;
388
389
270M
    if (isAvailableForLookup(getSema(), D) || 
isHiddenDeclarationVisible(D)217k
)
390
270M
      return D;
391
392
176k
    return getAcceptableDeclSlow(D);
393
270M
  }
394
395
private:
396
  static bool isAcceptableSlow(Sema &SemaRef, NamedDecl *D,
397
                               Sema::AcceptableKind Kind);
398
  static bool isReachableSlow(Sema &SemaRef, NamedDecl *D);
399
  NamedDecl *getAcceptableDeclSlow(NamedDecl *D) const;
400
401
public:
402
  /// Returns the identifier namespace mask for this lookup.
403
368M
  unsigned getIdentifierNamespace() const {
404
368M
    return IDNS;
405
368M
  }
406
407
  /// Returns whether these results arose from performing a
408
  /// lookup into a class.
409
131M
  bool isClassLookup() const {
410
131M
    return NamingClass != nullptr;
411
131M
  }
412
413
  /// Returns the 'naming class' for this lookup, i.e. the
414
  /// class which was looked into to find these results.
415
  ///
416
  /// C++0x [class.access.base]p5:
417
  ///   The access to a member is affected by the class in which the
418
  ///   member is named. This naming class is the class in which the
419
  ///   member name was looked up and found. [Note: this class can be
420
  ///   explicit, e.g., when a qualified-id is used, or implicit,
421
  ///   e.g., when a class member access operator (5.2.5) is used
422
  ///   (including cases where an implicit "this->" is added). If both
423
  ///   a class member access operator and a qualified-id are used to
424
  ///   name the member (as in p->T::m), the class naming the member
425
  ///   is the class named by the nested-name-specifier of the
426
  ///   qualified-id (that is, T). -- end note ]
427
  ///
428
  /// This is set by the lookup routines when they find results in a class.
429
11.8M
  CXXRecordDecl *getNamingClass() const {
430
11.8M
    return NamingClass;
431
11.8M
  }
432
433
  /// Sets the 'naming class' for this lookup.
434
9.16M
  void setNamingClass(CXXRecordDecl *Record) {
435
9.16M
    NamingClass = Record;
436
9.16M
  }
437
438
  /// Returns the base object type associated with this lookup;
439
  /// important for [class.protected].  Most lookups do not have an
440
  /// associated base object.
441
1.15M
  QualType getBaseObjectType() const {
442
1.15M
    return BaseObjectType;
443
1.15M
  }
444
445
  /// Sets the base object type for this lookup.
446
1.89M
  void setBaseObjectType(QualType T) {
447
1.89M
    BaseObjectType = T;
448
1.89M
  }
449
450
  /// Add a declaration to these results with its natural access.
451
  /// Does not test the acceptance criteria.
452
275M
  void addDecl(NamedDecl *D) {
453
275M
    addDecl(D, D->getAccess());
454
275M
  }
455
456
  /// Add a declaration to these results with the given access.
457
  /// Does not test the acceptance criteria.
458
276M
  void addDecl(NamedDecl *D, AccessSpecifier AS) {
459
276M
    Decls.addDecl(D, AS);
460
276M
    ResultKind = Found;
461
276M
  }
462
463
  /// Add all the declarations from another set of lookup
464
  /// results.
465
107
  void addAllDecls(const LookupResult &Other) {
466
107
    Decls.append(Other.Decls.begin(), Other.Decls.end());
467
107
    ResultKind = Found;
468
107
  }
469
470
  /// Determine whether no result was found because we could not
471
  /// search into dependent base classes of the current instantiation.
472
4.92M
  bool wasNotFoundInCurrentInstantiation() const {
473
4.92M
    return ResultKind == NotFoundInCurrentInstantiation;
474
4.92M
  }
475
476
  /// Note that while no result was found in the current instantiation,
477
  /// there were dependent base classes that could not be searched.
478
5.61k
  void setNotFoundInCurrentInstantiation() {
479
5.61k
    assert(ResultKind == NotFound && Decls.empty());
480
0
    ResultKind = NotFoundInCurrentInstantiation;
481
5.61k
  }
482
483
  /// Determine whether the lookup result was shadowed by some other
484
  /// declaration that lookup ignored.
485
13.9M
  bool isShadowed() const { return Shadowed; }
486
487
  /// Note that we found and ignored a declaration while performing
488
  /// lookup.
489
58
  void setShadowed() { Shadowed = true; }
490
491
  /// Resolves the result kind of the lookup, possibly hiding
492
  /// decls.
493
  ///
494
  /// This should be called in any environment where lookup might
495
  /// generate multiple lookup results.
496
  void resolveKind();
497
498
  /// Re-resolves the result kind of the lookup after a set of
499
  /// removals has been performed.
500
1.09M
  void resolveKindAfterFilter() {
501
1.09M
    if (Decls.empty()) {
502
1.08M
      if (ResultKind != NotFoundInCurrentInstantiation)
503
1.08M
        ResultKind = NotFound;
504
505
1.08M
      if (Paths) {
506
1
        deletePaths(Paths);
507
1
        Paths = nullptr;
508
1
      }
509
1.08M
    } else {
510
14.3k
      llvm::Optional<AmbiguityKind> SavedAK;
511
14.3k
      bool WasAmbiguous = false;
512
14.3k
      if (ResultKind == Ambiguous) {
513
11
        SavedAK = Ambiguity;
514
11
        WasAmbiguous = true;
515
11
      }
516
14.3k
      ResultKind = Found;
517
14.3k
      resolveKind();
518
519
      // If we didn't make the lookup unambiguous, restore the old
520
      // ambiguity kind.
521
14.3k
      if (ResultKind == Ambiguous) {
522
0
        (void)WasAmbiguous;
523
0
        assert(WasAmbiguous);
524
0
        Ambiguity = *SavedAK;
525
14.3k
      } else if (Paths) {
526
4
        deletePaths(Paths);
527
4
        Paths = nullptr;
528
4
      }
529
14.3k
    }
530
1.09M
  }
531
532
  template <class DeclClass>
533
28.2M
  DeclClass *getAsSingle() const {
534
28.2M
    if (getResultKind() != Found) 
return nullptr8.73M
;
535
19.5M
    return dyn_cast<DeclClass>(getFoundDecl());
536
28.2M
  }
clang::VarDecl* clang::LookupResult::getAsSingle<clang::VarDecl>() const
Line
Count
Source
533
6.05k
  DeclClass *getAsSingle() const {
534
6.05k
    if (getResultKind() != Found) 
return nullptr16
;
535
6.03k
    return dyn_cast<DeclClass>(getFoundDecl());
536
6.05k
  }
clang::ClassTemplateDecl* clang::LookupResult::getAsSingle<clang::ClassTemplateDecl>() const
Line
Count
Source
533
37.0k
  DeclClass *getAsSingle() const {
534
37.0k
    if (getResultKind() != Found) 
return nullptr29.4k
;
535
7.60k
    return dyn_cast<DeclClass>(getFoundDecl());
536
37.0k
  }
clang::NamespaceDecl* clang::LookupResult::getAsSingle<clang::NamespaceDecl>() const
Line
Count
Source
533
229k
  DeclClass *getAsSingle() const {
534
229k
    if (getResultKind() != Found) 
return nullptr43.4k
;
535
185k
    return dyn_cast<DeclClass>(getFoundDecl());
536
229k
  }
clang::UnresolvedUsingIfExistsDecl* clang::LookupResult::getAsSingle<clang::UnresolvedUsingIfExistsDecl>() const
Line
Count
Source
533
32
  DeclClass *getAsSingle() const {
534
32
    if (getResultKind() != Found) 
return nullptr0
;
535
32
    return dyn_cast<DeclClass>(getFoundDecl());
536
32
  }
clang::EnumConstantDecl* clang::LookupResult::getAsSingle<clang::EnumConstantDecl>() const
Line
Count
Source
533
226k
  DeclClass *getAsSingle() const {
534
226k
    if (getResultKind() != Found) 
return nullptr45.1k
;
535
181k
    return dyn_cast<DeclClass>(getFoundDecl());
536
226k
  }
clang::TypeAliasTemplateDecl* clang::LookupResult::getAsSingle<clang::TypeAliasTemplateDecl>() const
Line
Count
Source
533
547
  DeclClass *getAsSingle() const {
534
547
    if (getResultKind() != Found) 
return nullptr0
;
535
547
    return dyn_cast<DeclClass>(getFoundDecl());
536
547
  }
clang::RecordDecl* clang::LookupResult::getAsSingle<clang::RecordDecl>() const
Line
Count
Source
533
635
  DeclClass *getAsSingle() const {
534
635
    if (getResultKind() != Found) 
return nullptr4
;
535
631
    return dyn_cast<DeclClass>(getFoundDecl());
536
635
  }
clang::CXXRecordDecl* clang::LookupResult::getAsSingle<clang::CXXRecordDecl>() const
Line
Count
Source
533
8
  DeclClass *getAsSingle() const {
534
8
    if (getResultKind() != Found) 
return nullptr0
;
535
8
    return dyn_cast<DeclClass>(getFoundDecl());
536
8
  }
clang::TagDecl* clang::LookupResult::getAsSingle<clang::TagDecl>() const
Line
Count
Source
533
1.01k
  DeclClass *getAsSingle() const {
534
1.01k
    if (getResultKind() != Found) 
return nullptr851
;
535
163
    return dyn_cast<DeclClass>(getFoundDecl());
536
1.01k
  }
clang::NamedDecl* clang::LookupResult::getAsSingle<clang::NamedDecl>() const
Line
Count
Source
533
7.50M
  DeclClass *getAsSingle() const {
534
7.50M
    if (getResultKind() != Found) 
return nullptr7.01M
;
535
489k
    return dyn_cast<DeclClass>(getFoundDecl());
536
7.50M
  }
clang::ObjCIvarDecl* clang::LookupResult::getAsSingle<clang::ObjCIvarDecl>() const
Line
Count
Source
533
89
  DeclClass *getAsSingle() const {
534
89
    if (getResultKind() != Found) 
return nullptr0
;
535
89
    return dyn_cast<DeclClass>(getFoundDecl());
536
89
  }
clang::VarTemplateDecl* clang::LookupResult::getAsSingle<clang::VarTemplateDecl>() const
Line
Count
Source
533
562k
  DeclClass *getAsSingle() const {
534
562k
    if (getResultKind() != Found) 
return nullptr532k
;
535
29.5k
    return dyn_cast<DeclClass>(getFoundDecl());
536
562k
  }
clang::TypeDecl* clang::LookupResult::getAsSingle<clang::TypeDecl>() const
Line
Count
Source
533
1.03M
  DeclClass *getAsSingle() const {
534
1.03M
    if (getResultKind() != Found) 
return nullptr759
;
535
1.03M
    return dyn_cast<DeclClass>(getFoundDecl());
536
1.03M
  }
clang::FunctionTemplateDecl* clang::LookupResult::getAsSingle<clang::FunctionTemplateDecl>() const
Line
Count
Source
533
17.5M
  DeclClass *getAsSingle() const {
534
17.5M
    if (getResultKind() != Found) 
return nullptr0
;
535
17.5M
    return dyn_cast<DeclClass>(getFoundDecl());
536
17.5M
  }
clang::FunctionDecl* clang::LookupResult::getAsSingle<clang::FunctionDecl>() const
Line
Count
Source
533
4.48k
  DeclClass *getAsSingle() const {
534
4.48k
    if (getResultKind() != Found) 
return nullptr0
;
535
4.48k
    return dyn_cast<DeclClass>(getFoundDecl());
536
4.48k
  }
clang::FieldDecl* clang::LookupResult::getAsSingle<clang::FieldDecl>() const
Line
Count
Source
533
364
  DeclClass *getAsSingle() const {
534
364
    if (getResultKind() != Found) 
return nullptr3
;
535
361
    return dyn_cast<DeclClass>(getFoundDecl());
536
364
  }
clang::IndirectFieldDecl* clang::LookupResult::getAsSingle<clang::IndirectFieldDecl>() const
Line
Count
Source
533
7
  DeclClass *getAsSingle() const {
534
7
    if (getResultKind() != Found) 
return nullptr3
;
535
4
    return dyn_cast<DeclClass>(getFoundDecl());
536
7
  }
clang::TypedefNameDecl* clang::LookupResult::getAsSingle<clang::TypedefNameDecl>() const
Line
Count
Source
533
155
  DeclClass *getAsSingle() const {
534
155
    if (getResultKind() != Found) 
return nullptr0
;
535
155
    return dyn_cast<DeclClass>(getFoundDecl());
536
155
  }
clang::EnumDecl* clang::LookupResult::getAsSingle<clang::EnumDecl>() const
Line
Count
Source
533
6.88k
  DeclClass *getAsSingle() const {
534
6.88k
    if (getResultKind() != Found) 
return nullptr0
;
535
6.88k
    return dyn_cast<DeclClass>(getFoundDecl());
536
6.88k
  }
clang::TemplateDecl* clang::LookupResult::getAsSingle<clang::TemplateDecl>() const
Line
Count
Source
533
558k
  DeclClass *getAsSingle() const {
534
558k
    if (getResultKind() != Found) 
return nullptr532k
;
535
25.1k
    return dyn_cast<DeclClass>(getFoundDecl());
536
558k
  }
clang::ConceptDecl* clang::LookupResult::getAsSingle<clang::ConceptDecl>() const
Line
Count
Source
533
544k
  DeclClass *getAsSingle() const {
534
544k
    if (getResultKind() != Found) 
return nullptr532k
;
535
11.5k
    return dyn_cast<DeclClass>(getFoundDecl());
536
544k
  }
537
538
  /// Fetch the unique decl found by this lookup.  Asserts
539
  /// that one was found.
540
  ///
541
  /// This is intended for users who have examined the result kind
542
  /// and are certain that there is only one result.
543
150M
  NamedDecl *getFoundDecl() const {
544
150M
    assert(getResultKind() == Found
545
150M
           && "getFoundDecl called on non-unique result");
546
0
    return (*begin())->getUnderlyingDecl();
547
150M
  }
548
549
  /// Fetches a representative decl.  Useful for lazy diagnostics.
550
28.2M
  NamedDecl *getRepresentativeDecl() const {
551
28.2M
    assert(!Decls.empty() && "cannot get representative of empty set");
552
0
    return *begin();
553
28.2M
  }
554
555
  /// Asks if the result is a single tag decl.
556
30.8M
  bool isSingleTagDecl() const {
557
30.8M
    return getResultKind() == Found && 
isa<TagDecl>(getFoundDecl())1.91M
;
558
30.8M
  }
559
560
  /// Make these results show that the name was found in
561
  /// base classes of different types.
562
  ///
563
  /// The given paths object is copied and invalidated.
564
  void setAmbiguousBaseSubobjectTypes(CXXBasePaths &P);
565
566
  /// Make these results show that the name was found in
567
  /// distinct base classes of the same type.
568
  ///
569
  /// The given paths object is copied and invalidated.
570
  void setAmbiguousBaseSubobjects(CXXBasePaths &P);
571
572
  /// Make these results show that the name was found in
573
  /// different contexts and a tag decl was hidden by an ordinary
574
  /// decl in a different context.
575
6
  void setAmbiguousQualifiedTagHiding() {
576
6
    setAmbiguous(AmbiguousTagHiding);
577
6
  }
578
579
  /// Clears out any current state.
580
531k
  LLVM_ATTRIBUTE_REINITIALIZES void clear() {
581
531k
    ResultKind = NotFound;
582
531k
    Decls.clear();
583
531k
    if (Paths) 
deletePaths(Paths)10
;
584
531k
    Paths = nullptr;
585
531k
    NamingClass = nullptr;
586
531k
    Shadowed = false;
587
531k
  }
588
589
  /// Clears out any current state and re-initializes for a
590
  /// different kind of lookup.
591
10.4k
  void clear(Sema::LookupNameKind Kind) {
592
10.4k
    clear();
593
10.4k
    LookupKind = Kind;
594
10.4k
    configure();
595
10.4k
  }
596
597
  /// Change this lookup's redeclaration kind.
598
131k
  void setRedeclarationKind(Sema::RedeclarationKind RK) {
599
131k
    Redecl = (RK != Sema::NotForRedeclaration);
600
131k
    ExternalRedecl = (RK == Sema::ForExternalRedeclaration);
601
131k
    configure();
602
131k
  }
603
604
  void dump();
605
  void print(raw_ostream &);
606
607
  /// Suppress the diagnostics that would normally fire because of this
608
  /// lookup.  This happens during (e.g.) redeclaration lookups.
609
9.90M
  void suppressDiagnostics() {
610
9.90M
    Diagnose = false;
611
9.90M
  }
612
613
  /// Determines whether this lookup is suppressing diagnostics.
614
8
  bool isSuppressingDiagnostics() const {
615
8
    return !Diagnose;
616
8
  }
617
618
  /// Sets a 'context' source range.
619
754k
  void setContextRange(SourceRange SR) {
620
754k
    NameContextRange = SR;
621
754k
  }
622
623
  /// Gets the source range of the context of this name; for C++
624
  /// qualified lookups, this is the source range of the scope
625
  /// specifier.
626
197
  SourceRange getContextRange() const {
627
197
    return NameContextRange;
628
197
  }
629
630
  /// Gets the location of the identifier.  This isn't always defined:
631
  /// sometimes we're doing lookups on synthesized names.
632
107M
  SourceLocation getNameLoc() const {
633
107M
    return NameInfo.getLoc();
634
107M
  }
635
636
  /// Get the Sema object that this lookup result is searching
637
  /// with.
638
676M
  Sema &getSema() const { return *SemaPtr; }
639
640
  /// A class for iterating through a result set and possibly
641
  /// filtering out results.  The results returned are possibly
642
  /// sugared.
643
  class Filter {
644
    friend class LookupResult;
645
646
    LookupResult &Results;
647
    LookupResult::iterator I;
648
    bool Changed = false;
649
    bool CalledDone = false;
650
651
35.6M
    Filter(LookupResult &Results) : Results(Results), I(Results.begin()) {}
652
653
  public:
654
    Filter(Filter &&F)
655
        : Results(F.Results), I(F.I), Changed(F.Changed),
656
0
          CalledDone(F.CalledDone) {
657
0
      F.CalledDone = true;
658
0
    }
659
660
35.6M
    ~Filter() {
661
35.6M
      assert(CalledDone &&
662
35.6M
             "LookupResult::Filter destroyed without done() call");
663
35.6M
    }
664
665
100M
    bool hasNext() const {
666
100M
      return I != Results.end();
667
100M
    }
668
669
65.1M
    NamedDecl *next() {
670
65.1M
      assert(I != Results.end() && "next() called on empty filter");
671
0
      return *I++;
672
65.1M
    }
673
674
    /// Restart the iteration.
675
12
    void restart() {
676
12
      I = Results.begin();
677
12
    }
678
679
    /// Erase the last element returned from this iterator.
680
1.48M
    void erase() {
681
1.48M
      Results.Decls.erase(--I);
682
1.48M
      Changed = true;
683
1.48M
    }
684
685
    /// Replaces the current entry with the given one, preserving the
686
    /// access bits.
687
0
    void replace(NamedDecl *D) {
688
0
      Results.Decls.replace(I-1, D);
689
0
      Changed = true;
690
0
    }
691
692
    /// Replaces the current entry with the given one.
693
0
    void replace(NamedDecl *D, AccessSpecifier AS) {
694
0
      Results.Decls.replace(I-1, D, AS);
695
0
      Changed = true;
696
0
    }
697
698
35.6M
    void done() {
699
35.6M
      assert(!CalledDone && "done() called twice");
700
0
      CalledDone = true;
701
702
35.6M
      if (Changed)
703
1.09M
        Results.resolveKindAfterFilter();
704
35.6M
    }
705
  };
706
707
  /// Create a filter for this result set.
708
35.6M
  Filter makeFilter() {
709
35.6M
    return Filter(*this);
710
35.6M
  }
711
712
405M
  void setFindLocalExtern(bool FindLocalExtern) {
713
405M
    if (FindLocalExtern)
714
224M
      IDNS |= Decl::IDNS_LocalExtern;
715
180M
    else
716
180M
      IDNS &= ~Decl::IDNS_LocalExtern;
717
405M
  }
718
719
private:
720
131M
  void diagnose() {
721
131M
    if (isAmbiguous())
722
197
      getSema().DiagnoseAmbiguousLookup(*this);
723
131M
    else if (isClassLookup() && 
getSema().getLangOpts().AccessControl6.25M
)
724
6.04M
      getSema().CheckLookupAccess(*this);
725
131M
  }
726
727
4.87k
  void setAmbiguous(AmbiguityKind AK) {
728
4.87k
    ResultKind = Ambiguous;
729
4.87k
    Ambiguity = AK;
730
4.87k
  }
731
732
  void addDeclsFromBasePaths(const CXXBasePaths &P);
733
  void configure();
734
735
  bool checkDebugAssumptions() const;
736
737
2.20k
  bool checkUnresolved() const {
738
2.59k
    for (iterator I = begin(), E = end(); I != E; 
++I390
)
739
2.59k
      if (isa<UnresolvedUsingValueDecl>((*I)->getUnderlyingDecl()))
740
2.20k
        return true;
741
0
    return false;
742
2.20k
  }
743
744
  static void deletePaths(CXXBasePaths *);
745
746
  // Results.
747
  LookupResultKind ResultKind = NotFound;
748
  // ill-defined unless ambiguous. Still need to be initialized it will be
749
  // copied/moved.
750
  AmbiguityKind Ambiguity = {};
751
  UnresolvedSet<8> Decls;
752
  CXXBasePaths *Paths = nullptr;
753
  CXXRecordDecl *NamingClass = nullptr;
754
  QualType BaseObjectType;
755
756
  // Parameters.
757
  Sema *SemaPtr;
758
  DeclarationNameInfo NameInfo;
759
  SourceRange NameContextRange;
760
  Sema::LookupNameKind LookupKind;
761
  unsigned IDNS = 0; // set by configure()
762
763
  bool Redecl;
764
  bool ExternalRedecl;
765
766
  /// True if tag declarations should be hidden if non-tags
767
  ///   are present
768
  bool HideTags = true;
769
770
  bool Diagnose = false;
771
772
  /// True if we should allow hidden declarations to be 'visible'.
773
  bool AllowHidden = false;
774
775
  /// True if the found declarations were shadowed by some other
776
  /// declaration that we skipped. This only happens when \c LookupKind
777
  /// is \c LookupRedeclarationWithLinkage.
778
  bool Shadowed = false;
779
780
  /// True if we're looking up a template-name.
781
  bool TemplateNameLookup = false;
782
};
783
784
/// Consumes visible declarations found when searching for
785
/// all visible names within a given scope or context.
786
///
787
/// This abstract class is meant to be subclassed by clients of \c
788
/// Sema::LookupVisibleDecls(), each of which should override the \c
789
/// FoundDecl() function to process declarations as they are found.
790
class VisibleDeclConsumer {
791
public:
792
  /// Destroys the visible declaration consumer.
793
  virtual ~VisibleDeclConsumer();
794
795
  /// Determine whether hidden declarations (from unimported
796
  /// modules) should be given to this consumer. By default, they
797
  /// are not included.
798
  virtual bool includeHiddenDecls() const;
799
800
  /// Invoked each time \p Sema::LookupVisibleDecls() finds a
801
  /// declaration visible from the current scope or context.
802
  ///
803
  /// \param ND the declaration found.
804
  ///
805
  /// \param Hiding a declaration that hides the declaration \p ND,
806
  /// or NULL if no such declaration exists.
807
  ///
808
  /// \param Ctx the original context from which the lookup started.
809
  ///
810
  /// \param InBaseClass whether this declaration was found in base
811
  /// class of the context we searched.
812
  virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
813
                         bool InBaseClass) = 0;
814
815
  /// Callback to inform the client that Sema entered into a new context
816
  /// to find a visible declaration.
817
  //
818
  /// \param Ctx the context which Sema entered.
819
1.16k
  virtual void EnteredContext(DeclContext *Ctx) {}
820
};
821
822
/// A class for storing results from argument-dependent lookup.
823
class ADLResult {
824
private:
825
  /// A map from canonical decls to the 'most recent' decl.
826
  llvm::MapVector<NamedDecl*, NamedDecl*> Decls;
827
828
  struct select_second {
829
2.05M
    NamedDecl *operator()(std::pair<NamedDecl*, NamedDecl*> P) const {
830
2.05M
      return P.second;
831
2.05M
    }
832
  };
833
834
public:
835
  /// Adds a new ADL candidate to this map.
836
  void insert(NamedDecl *D);
837
838
  /// Removes any data associated with a given decl.
839
1.59M
  void erase(NamedDecl *D) {
840
1.59M
    Decls.erase(cast<NamedDecl>(D->getCanonicalDecl()));
841
1.59M
  }
842
843
  using iterator =
844
      llvm::mapped_iterator<decltype(Decls)::iterator, select_second>;
845
846
714k
  iterator begin() { return iterator(Decls.begin(), select_second()); }
847
714k
  iterator end() { return iterator(Decls.end(), select_second()); }
848
};
849
850
} // namespace clang
851
852
#endif // LLVM_CLANG_SEMA_LOOKUP_H