clang  19.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/PointerIntPair.h"
23 #include <optional>
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  LLVM_PREFERRED_TYPE(bool)
34  /// Whether explicit visibility attributes should be ignored. When set,
35  /// visibility may only be restricted by the visibility of template arguments.
36  LLVM_PREFERRED_TYPE(bool)
38  /// Whether all visibility should be ignored. When set, we're only interested
39  /// in computing linkage.
40  LLVM_PREFERRED_TYPE(bool)
41  unsigned IgnoreAllVisibility : 1;
42 
44 
48 
51  }
52 
53  bool isTypeVisibility() const {
55  }
56  bool isValueVisibility() const {
58  }
59 
60  /// Do an LV computation when we only care about the linkage.
63  Result.IgnoreExplicitVisibility = true;
64  Result.IgnoreAllVisibility = true;
65  return Result;
66  }
67 
68  unsigned toBits() {
69  unsigned Bits = 0;
70  Bits = (Bits << 1) | ExplicitKind;
71  Bits = (Bits << 1) | IgnoreExplicitVisibility;
72  Bits = (Bits << 1) | IgnoreAllVisibility;
73  return Bits;
74  }
75 };
76 
78  // We have a cache for repeated linkage/visibility computations. This saves us
79  // from exponential behavior in heavily templated code, such as:
80  //
81  // template <typename T, typename V> struct {};
82  // using A = int;
83  // using B = Foo<A, A>;
84  // using C = Foo<B, B>;
85  // using D = Foo<C, C>;
86  //
87  // The integer represents an LVComputationKind.
88  using QueryType =
89  llvm::PointerIntPair<const NamedDecl *,
91  llvm::SmallDenseMap<QueryType, LinkageInfo, 8> CachedLinkageInfo;
92 
93  static QueryType makeCacheKey(const NamedDecl *ND, LVComputationKind Kind) {
94  return QueryType(ND, Kind.toBits());
95  }
96 
97  std::optional<LinkageInfo> lookup(const NamedDecl *ND,
98  LVComputationKind Kind) const {
99  auto Iter = CachedLinkageInfo.find(makeCacheKey(ND, Kind));
100  if (Iter == CachedLinkageInfo.end())
101  return std::nullopt;
102  return Iter->second;
103  }
104 
105  void cache(const NamedDecl *ND, LVComputationKind Kind, LinkageInfo Info) {
106  CachedLinkageInfo[makeCacheKey(ND, Kind)] = Info;
107  }
108 
109  LinkageInfo getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args,
110  LVComputationKind computation);
111 
112  LinkageInfo getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
113  LVComputationKind computation);
114 
115  void mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn,
116  const FunctionTemplateSpecializationInfo *specInfo,
117  LVComputationKind computation);
118 
119  void mergeTemplateLV(LinkageInfo &LV,
121  LVComputationKind computation);
122 
123  void mergeTemplateLV(LinkageInfo &LV,
124  const VarTemplateSpecializationDecl *spec,
125  LVComputationKind computation);
126 
127  LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
128  LVComputationKind computation,
129  bool IgnoreVarTypeLinkage);
130 
131  LinkageInfo getLVForClassMember(const NamedDecl *D,
132  LVComputationKind computation,
133  bool IgnoreVarTypeLinkage);
134 
135  LinkageInfo getLVForClosure(const DeclContext *DC, Decl *ContextDecl,
136  LVComputationKind computation);
137 
138  LinkageInfo getLVForLocalDecl(const NamedDecl *D,
139  LVComputationKind computation);
140 
141  LinkageInfo getLVForType(const Type &T, LVComputationKind computation);
142 
143  LinkageInfo getLVForTemplateParameterList(const TemplateParameterList *Params,
144  LVComputationKind computation);
145 
146  LinkageInfo getLVForValue(const APValue &V, LVComputationKind computation);
147 
148 public:
150  LVComputationKind computation,
151  bool IgnoreVarTypeLinkage = false);
152 
153  LinkageInfo getLVForDecl(const NamedDecl *D, LVComputationKind computation);
154 
157  return computeTypeLinkageInfo(T.getTypePtr());
158  }
159 
161 
164  return getTypeLinkageAndVisibility(T.getTypePtr());
165  }
166 };
167 } // namespace clang
168 
169 #endif
#define V(N, I)
Definition: ASTContext.h:3299
Forward declaration of all AST node types.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
unsigned Iter
Definition: HTMLLogger.cpp:154
C Language Family Type Representation.
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Represents a class template specialization, which refers to a class template with a given set of temp...
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1436
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Represents a function declaration or definition.
Definition: Decl.h:1972
Provides information about a function template specialization, which is a FunctionDecl that has been ...
Definition: DeclTemplate.h:467
LinkageInfo computeTypeLinkageInfo(const Type *T)
Definition: Type.cpp:4548
LinkageInfo getTypeLinkageAndVisibility(const Type *T)
Definition: Type.cpp:4636
LinkageInfo computeLVForDecl(const NamedDecl *D, LVComputationKind computation, bool IgnoreVarTypeLinkage=false)
Definition: Decl.cpp:1450
LinkageInfo computeTypeLinkageInfo(QualType T)
Definition: Linkage.h:156
LinkageInfo getLVForDecl(const NamedDecl *D, LVComputationKind computation)
getLVForDecl - Get the linkage and visibility for the given declaration.
Definition: Decl.cpp:1568
LinkageInfo getDeclLinkageAndVisibility(const NamedDecl *D)
Definition: Decl.cpp:1614
LinkageInfo getTypeLinkageAndVisibility(QualType T)
Definition: Linkage.h:163
This represents a decl that may have a name.
Definition: Decl.h:249
ExplicitVisibilityKind
Kinds of explicit visibility.
Definition: Decl.h:428
@ VisibilityForValue
Do an LV computation for, ultimately, a non-type declaration.
Definition: Decl.h:437
@ VisibilityForType
Do an LV computation for, ultimately, a type.
Definition: Decl.h:432
A (possibly-)qualified type.
Definition: Type.h:940
A template argument list.
Definition: DeclTemplate.h:244
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:73
The base class of the type hierarchy.
Definition: Type.h:1813
Represents a variable template specialization, which refers to a variable template with a given set o...
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
#define false
Definition: stdbool.h:26
Kinds of LV computation.
Definition: Linkage.h:29
bool isTypeVisibility() const
Definition: Linkage.h:53
unsigned ExplicitKind
The kind of entity whose visibility is ultimately being computed; visibility computations for types a...
Definition: Linkage.h:33
unsigned IgnoreExplicitVisibility
Whether explicit visibility attributes should be ignored.
Definition: Linkage.h:37
LVComputationKind(NamedDecl::ExplicitVisibilityKind EK)
Definition: Linkage.h:45
unsigned IgnoreAllVisibility
Whether all visibility should be ignored.
Definition: Linkage.h:41
NamedDecl::ExplicitVisibilityKind getExplicitVisibilityKind() const
Definition: Linkage.h:49
static LVComputationKind forLinkageOnly()
Do an LV computation when we only care about the linkage.
Definition: Linkage.h:61
bool isValueVisibility() const
Definition: Linkage.h:56