Coverage Report

Created: 2018-11-16 02:38

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