clang  14.0.0git
Linkage.h
Go to the documentation of this file.
1 //===----- Linkage.h - Linkage calculation-related utilities ----*- 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 provides AST-internal utilities for linkage and visibility
10 // calculation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_AST_LINKAGE_H
15 #define LLVM_CLANG_LIB_AST_LINKAGE_H
16 
17 #include "clang/AST/ASTFwd.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/Type.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/PointerIntPair.h"
24 
25 namespace clang {
26 /// Kinds of LV computation. The linkage side of the computation is
27 /// always the same, but different things can change how visibility is
28 /// computed.
30  /// The kind of entity whose visibility is ultimately being computed;
31  /// visibility computations for types and non-types follow different rules.
32  unsigned ExplicitKind : 1;
33  /// Whether explicit visibility attributes should be ignored. When set,
34  /// visibility may only be restricted by the visibility of template arguments.
36  /// Whether all visibility should be ignored. When set, we're only interested
37  /// in computing linkage.
38  unsigned IgnoreAllVisibility : 1;
39 
41 
45 
48  }
49 
50  bool isTypeVisibility() const {
52  }
53  bool isValueVisibility() const {
55  }
56 
57  /// Do an LV computation when we only care about the linkage.
60  Result.IgnoreExplicitVisibility = true;
61  Result.IgnoreAllVisibility = true;
62  return Result;
63  }
64 
65  unsigned toBits() {
66  unsigned Bits = 0;
67  Bits = (Bits << 1) | ExplicitKind;
68  Bits = (Bits << 1) | IgnoreExplicitVisibility;
69  Bits = (Bits << 1) | IgnoreAllVisibility;
70  return Bits;
71  }
72 };
73 
75  // We have a cache for repeated linkage/visibility computations. This saves us
76  // from exponential behavior in heavily templated code, such as:
77  //
78  // template <typename T, typename V> struct {};
79  // using A = int;
80  // using B = Foo<A, A>;
81  // using C = Foo<B, B>;
82  // using D = Foo<C, C>;
83  //
84  // The integer represents an LVComputationKind.
85  using QueryType =
86  llvm::PointerIntPair<const NamedDecl *,
88  llvm::SmallDenseMap<QueryType, LinkageInfo, 8> CachedLinkageInfo;
89 
90  static QueryType makeCacheKey(const NamedDecl *ND, LVComputationKind Kind) {
91  return QueryType(ND, Kind.toBits());
92  }
93 
94  llvm::Optional<LinkageInfo> lookup(const NamedDecl *ND,
95  LVComputationKind Kind) const {
96  auto Iter = CachedLinkageInfo.find(makeCacheKey(ND, Kind));
97  if (Iter == CachedLinkageInfo.end())
98  return None;
99  return Iter->second;
100  }
101 
102  void cache(const NamedDecl *ND, LVComputationKind Kind, LinkageInfo Info) {
103  CachedLinkageInfo[makeCacheKey(ND, Kind)] = Info;
104  }
105 
106  LinkageInfo getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args,
107  LVComputationKind computation);
108 
109  LinkageInfo getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
110  LVComputationKind computation);
111 
112  void mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn,
113  const FunctionTemplateSpecializationInfo *specInfo,
114  LVComputationKind computation);
115 
116  void mergeTemplateLV(LinkageInfo &LV,
118  LVComputationKind computation);
119 
120  void mergeTemplateLV(LinkageInfo &LV,
121  const VarTemplateSpecializationDecl *spec,
122  LVComputationKind computation);
123 
124  LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
125  LVComputationKind computation,
126  bool IgnoreVarTypeLinkage);
127 
128  LinkageInfo getLVForClassMember(const NamedDecl *D,
129  LVComputationKind computation,
130  bool IgnoreVarTypeLinkage);
131 
132  LinkageInfo getLVForClosure(const DeclContext *DC, Decl *ContextDecl,
133  LVComputationKind computation);
134 
135  LinkageInfo getLVForLocalDecl(const NamedDecl *D,
136  LVComputationKind computation);
137 
138  LinkageInfo getLVForType(const Type &T, LVComputationKind computation);
139 
140  LinkageInfo getLVForTemplateParameterList(const TemplateParameterList *Params,
141  LVComputationKind computation);
142 
143  LinkageInfo getLVForValue(const APValue &V, LVComputationKind computation);
144 
145 public:
147  LVComputationKind computation,
148  bool IgnoreVarTypeLinkage = false);
149 
150  LinkageInfo getLVForDecl(const NamedDecl *D, LVComputationKind computation);
151 
155  }
156 
158 
162  }
163 };
164 } // namespace clang
165 
166 #endif
clang::LVComputationKind::ExplicitKind
unsigned ExplicitKind
The kind of entity whose visibility is ultimately being computed; visibility computations for types a...
Definition: Linkage.h:32
clang::DeclContext
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1347
clang::LVComputationKind::IgnoreExplicitVisibility
unsigned IgnoreExplicitVisibility
Whether explicit visibility attributes should be ignored.
Definition: Linkage.h:35
clang::LinkageComputer::getTypeLinkageAndVisibility
LinkageInfo getTypeLinkageAndVisibility(const Type *T)
Definition: Type.cpp:4024
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:249
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
DeclCXX.h
llvm::Optional
Definition: LLVM.h:40
clang::LVComputationKind::getExplicitVisibilityKind
NamedDecl::ExplicitVisibilityKind getExplicitVisibilityKind() const
Definition: Linkage.h:46
clang::LVComputationKind::isTypeVisibility
bool isTypeVisibility() const
Definition: Linkage.h:50
clang::VarTemplateSpecializationDecl
Represents a variable template specialization, which refers to a variable template with a given set o...
Definition: DeclTemplate.h:2664
ASTFwd.h
clang::LinkageComputer::computeTypeLinkageInfo
LinkageInfo computeTypeLinkageInfo(const Type *T)
Definition: Type.cpp:3937
clang::LVComputationKind
Kinds of LV computation.
Definition: Linkage.h:29
clang::LVComputationKind::NumLVComputationKindBits
@ NumLVComputationKindBits
Definition: Linkage.h:40
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1490
Decl.h
V
#define V(N, I)
Definition: ASTContext.h:3121
clang::NamedDecl::ExplicitVisibilityKind
ExplicitVisibilityKind
Kinds of explicit visibility.
Definition: Decl.h:426
Type.h
clang::LinkageInfo
Definition: Visibility.h:52
clang::TemplateParameterList
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:69
clang::LVComputationKind::toBits
unsigned toBits()
Definition: Linkage.h:65
clang::FunctionTemplateSpecializationInfo
Provides information about a function template specialization, which is a FunctionDecl that has been ...
Definition: DeclTemplate.h:467
clang::LinkageComputer::computeLVForDecl
LinkageInfo computeLVForDecl(const NamedDecl *D, LVComputationKind computation, bool IgnoreVarTypeLinkage=false)
Definition: Decl.cpp:1364
false
#define false
Definition: stdbool.h:17
clang::LVComputationKind::isValueVisibility
bool isValueVisibility() const
Definition: Linkage.h:53
llvm::ArrayRef
Definition: LLVM.h:34
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::NamedDecl::VisibilityForType
@ VisibilityForType
Do an LV computation for, ultimately, a type.
Definition: Decl.h:430
clang::TemplateArgumentList
A template argument list.
Definition: DeclTemplate.h:236
clang::ObjCPropertyAttribute::Kind
Kind
Definition: DeclObjCCommon.h:22
clang::LVComputationKind::LVComputationKind
LVComputationKind(NamedDecl::ExplicitVisibilityKind EK)
Definition: Linkage.h:42
clang::LVComputationKind::forLinkageOnly
static LVComputationKind forLinkageOnly()
Do an LV computation when we only care about the linkage.
Definition: Linkage.h:58
clang
Definition: CalledOnceCheck.h:17
clang::LinkageComputer::computeTypeLinkageInfo
LinkageInfo computeTypeLinkageInfo(QualType T)
Definition: Linkage.h:153
clang::QualType::getTypePtr
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:6424
clang::NamedDecl::VisibilityForValue
@ VisibilityForValue
Do an LV computation for, ultimately, a non-type declaration.
Definition: Decl.h:435
clang::APValue
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
clang::LVComputationKind::IgnoreAllVisibility
unsigned IgnoreAllVisibility
Whether all visibility should be ignored.
Definition: Linkage.h:38
clang::LinkageComputer::getLVForDecl
LinkageInfo getLVForDecl(const NamedDecl *D, LVComputationKind computation)
getLVForDecl - Get the linkage and visibility for the given declaration.
Definition: Decl.cpp:1482
clang::LinkageComputer
Definition: Linkage.h:74
clang::ClassTemplateSpecializationDecl
Represents a class template specialization, which refers to a class template with a given set of temp...
Definition: DeclTemplate.h:1803
clang::LinkageComputer::getDeclLinkageAndVisibility
LinkageInfo getDeclLinkageAndVisibility(const NamedDecl *D)
Definition: Decl.cpp:1528
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1856
clang::LinkageComputer::getTypeLinkageAndVisibility
LinkageInfo getTypeLinkageAndVisibility(QualType T)
Definition: Linkage.h:160