clang  6.0.0svn
Linkage.h
Go to the documentation of this file.
1 //===----- Linkage.h - Linkage calculation-related utilities ----*- 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 provides AST-internal utilities for linkage and visibility
11 // calculation.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_LIB_AST_LINKAGE_H
16 #define LLVM_CLANG_LIB_AST_LINKAGE_H
17 
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 
24 namespace clang {
25 /// Kinds of LV computation. The linkage side of the computation is
26 /// always the same, but different things can change how visibility is
27 /// computed.
29  /// The kind of entity whose visibility is ultimately being computed;
30  /// visibility computations for types and non-types follow different rules.
31  unsigned ExplicitKind : 1;
32  /// Whether explicit visibility attributes should be ignored. When set,
33  /// visibility may only be restricted by the visibility of template arguments.
35  /// Whether all visibility should be ignored. When set, we're only interested
36  /// in computing linkage.
37  unsigned IgnoreAllVisibility : 1;
38 
42 
45  }
46 
47  bool isTypeVisibility() const {
49  }
50  bool isValueVisibility() const {
52  }
53 
54  /// Do an LV computation when we only care about the linkage.
57  Result.IgnoreExplicitVisibility = true;
58  Result.IgnoreAllVisibility = true;
59  return Result;
60  }
61 
62  unsigned toBits() {
63  unsigned Bits = 0;
64  Bits = (Bits << 1) | ExplicitKind;
65  Bits = (Bits << 1) | IgnoreExplicitVisibility;
66  Bits = (Bits << 1) | IgnoreAllVisibility;
67  return Bits;
68  }
69 };
70 
72  // We have a cache for repeated linkage/visibility computations. This saves us
73  // from exponential behavior in heavily templated code, such as:
74  //
75  // template <typename T, typename V> struct {};
76  // using A = int;
77  // using B = Foo<A, A>;
78  // using C = Foo<B, B>;
79  // using D = Foo<C, C>;
80  //
81  // The unsigned represents an LVComputationKind.
82  using QueryType = std::pair<const NamedDecl *, unsigned>;
83  llvm::SmallDenseMap<QueryType, LinkageInfo, 8> CachedLinkageInfo;
84 
85  static QueryType makeCacheKey(const NamedDecl *ND, LVComputationKind Kind) {
86  return std::make_pair(ND, Kind.toBits());
87  }
88 
89  llvm::Optional<LinkageInfo> lookup(const NamedDecl *ND,
90  LVComputationKind Kind) const {
91  auto Iter = CachedLinkageInfo.find(makeCacheKey(ND, Kind));
92  if (Iter == CachedLinkageInfo.end())
93  return None;
94  return Iter->second;
95  }
96 
97  void cache(const NamedDecl *ND, LVComputationKind Kind, LinkageInfo Info) {
98  CachedLinkageInfo[makeCacheKey(ND, Kind)] = Info;
99  }
100 
101  LinkageInfo getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args,
102  LVComputationKind computation);
103 
104  LinkageInfo getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
105  LVComputationKind computation);
106 
107  void mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn,
108  const FunctionTemplateSpecializationInfo *specInfo,
109  LVComputationKind computation);
110 
111  void mergeTemplateLV(LinkageInfo &LV,
113  LVComputationKind computation);
114 
115  void mergeTemplateLV(LinkageInfo &LV,
116  const VarTemplateSpecializationDecl *spec,
117  LVComputationKind computation);
118 
119  LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
120  LVComputationKind computation,
121  bool IgnoreVarTypeLinkage);
122 
123  LinkageInfo getLVForClassMember(const NamedDecl *D,
124  LVComputationKind computation,
125  bool IgnoreVarTypeLinkage);
126 
127  LinkageInfo getLVForClosure(const DeclContext *DC, Decl *ContextDecl,
128  LVComputationKind computation);
129 
130  LinkageInfo getLVForLocalDecl(const NamedDecl *D,
131  LVComputationKind computation);
132 
133  LinkageInfo getLVForType(const Type &T, LVComputationKind computation);
134 
135  LinkageInfo getLVForTemplateParameterList(const TemplateParameterList *Params,
136  LVComputationKind computation);
137 
138 public:
139  LinkageInfo computeLVForDecl(const NamedDecl *D,
140  LVComputationKind computation,
141  bool IgnoreVarTypeLinkage = false);
142 
143  LinkageInfo getLVForDecl(const NamedDecl *D, LVComputationKind computation);
144 
145  LinkageInfo computeTypeLinkageInfo(const Type *T);
147  return computeTypeLinkageInfo(T.getTypePtr());
148  }
149 
150  LinkageInfo getDeclLinkageAndVisibility(const NamedDecl *D);
151 
152  LinkageInfo getTypeLinkageAndVisibility(const Type *T);
154  return getTypeLinkageAndVisibility(T.getTypePtr());
155  }
156 };
157 } // namespace clang
158 
159 #endif
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1698
LVComputationKind(NamedDecl::ExplicitVisibilityKind EK)
Definition: Linkage.h:39
A (possibly-)qualified type.
Definition: Type.h:653
C Language Family Type Representation.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
The base class of the type hierarchy.
Definition: Type.h:1353
ExplicitVisibilityKind
Kinds of explicit visibility.
Definition: Decl.h:390
Represents a variable template specialization, which refers to a variable template with a given set o...
LinkageInfo getTypeLinkageAndVisibility(QualType T)
Definition: Linkage.h:153
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:68
unsigned ExplicitKind
The kind of entity whose visibility is ultimately being computed; visibility computations for types a...
Definition: Linkage.h:31
Represents a class template specialization, which refers to a class template with a given set of temp...
Provides information about a function template specialization, which is a FunctionDecl that has been ...
Definition: DeclTemplate.h:507
Kinds of LV computation.
Definition: Linkage.h:28
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:5720
LinkageInfo computeTypeLinkageInfo(QualType T)
Definition: Linkage.h:146
unsigned IgnoreExplicitVisibility
Whether explicit visibility attributes should be ignored.
Definition: Linkage.h:34
bool isTypeVisibility() const
Definition: Linkage.h:47
const FunctionProtoType * T
unsigned IgnoreAllVisibility
Whether all visibility should be ignored.
Definition: Linkage.h:37
bool isValueVisibility() const
Definition: Linkage.h:50
The result type of a method or function.
#define false
Definition: stdbool.h:33
Kind
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1252
Do an LV computation for, ultimately, a non-type declaration.
Definition: Decl.h:399
Do an LV computation for, ultimately, a type.
Definition: Decl.h:394
NamedDecl::ExplicitVisibilityKind getExplicitVisibilityKind() const
Definition: Linkage.h:43
A template argument list.
Definition: DeclTemplate.h:210
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
NamedDecl - This represents a decl with a name.
Definition: Decl.h:245
static LVComputationKind forLinkageOnly()
Do an LV computation when we only care about the linkage.
Definition: Linkage.h:55