George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 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" |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Optional.h" |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 23 | |
| 24 | namespace clang { |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 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. |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 28 | struct LVComputationKind { |
| 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. |
| 34 | unsigned IgnoreExplicitVisibility : 1; |
| 35 | /// Whether all visibility should be ignored. When set, we're only interested |
| 36 | /// in computing linkage. |
| 37 | unsigned IgnoreAllVisibility : 1; |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 38 | |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 39 | explicit LVComputationKind(NamedDecl::ExplicitVisibilityKind EK) |
| 40 | : ExplicitKind(EK), IgnoreExplicitVisibility(false), |
| 41 | IgnoreAllVisibility(false) {} |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 42 | |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 43 | NamedDecl::ExplicitVisibilityKind getExplicitVisibilityKind() const { |
| 44 | return static_cast<NamedDecl::ExplicitVisibilityKind>(ExplicitKind); |
| 45 | } |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 46 | |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 47 | bool isTypeVisibility() const { |
| 48 | return getExplicitVisibilityKind() == NamedDecl::VisibilityForType; |
| 49 | } |
| 50 | bool isValueVisibility() const { |
| 51 | return getExplicitVisibilityKind() == NamedDecl::VisibilityForValue; |
| 52 | } |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 53 | |
| 54 | /// Do an LV computation when we only care about the linkage. |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 55 | static LVComputationKind forLinkageOnly() { |
| 56 | LVComputationKind Result(NamedDecl::VisibilityForValue); |
| 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 | } |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | class LinkageComputer { |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 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>; |
George Burgess IV | b8709ba | 2017-08-09 21:20:41 +0000 | [diff] [blame] | 80 | // |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 81 | // The unsigned represents an LVComputationKind. |
George Burgess IV | b8709ba | 2017-08-09 21:20:41 +0000 | [diff] [blame] | 82 | using QueryType = std::pair<const NamedDecl *, unsigned>; |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 83 | llvm::SmallDenseMap<QueryType, LinkageInfo, 8> CachedLinkageInfo; |
George Burgess IV | b8709ba | 2017-08-09 21:20:41 +0000 | [diff] [blame] | 84 | |
| 85 | static QueryType makeCacheKey(const NamedDecl *ND, LVComputationKind Kind) { |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 86 | return std::make_pair(ND, Kind.toBits()); |
George Burgess IV | b8709ba | 2017-08-09 21:20:41 +0000 | [diff] [blame] | 87 | } |
| 88 | |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 89 | llvm::Optional<LinkageInfo> lookup(const NamedDecl *ND, |
| 90 | LVComputationKind Kind) const { |
George Burgess IV | b8709ba | 2017-08-09 21:20:41 +0000 | [diff] [blame] | 91 | auto Iter = CachedLinkageInfo.find(makeCacheKey(ND, Kind)); |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 92 | if (Iter == CachedLinkageInfo.end()) |
| 93 | return None; |
| 94 | return Iter->second; |
| 95 | } |
| 96 | |
| 97 | void cache(const NamedDecl *ND, LVComputationKind Kind, LinkageInfo Info) { |
George Burgess IV | b8709ba | 2017-08-09 21:20:41 +0000 | [diff] [blame] | 98 | CachedLinkageInfo[makeCacheKey(ND, Kind)] = Info; |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 99 | } |
| 100 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 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, |
| 112 | const ClassTemplateSpecializationDecl *spec, |
| 113 | LVComputationKind computation); |
| 114 | |
| 115 | void mergeTemplateLV(LinkageInfo &LV, |
| 116 | const VarTemplateSpecializationDecl *spec, |
| 117 | LVComputationKind computation); |
| 118 | |
| 119 | LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 120 | LVComputationKind computation, |
| 121 | bool IgnoreVarTypeLinkage); |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 122 | |
| 123 | LinkageInfo getLVForClassMember(const NamedDecl *D, |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 124 | LVComputationKind computation, |
| 125 | bool IgnoreVarTypeLinkage); |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 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, |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 140 | LVComputationKind computation, |
| 141 | bool IgnoreVarTypeLinkage = false); |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 142 | |
| 143 | LinkageInfo getLVForDecl(const NamedDecl *D, LVComputationKind computation); |
| 144 | |
| 145 | LinkageInfo computeTypeLinkageInfo(const Type *T); |
| 146 | LinkageInfo computeTypeLinkageInfo(QualType T) { |
| 147 | return computeTypeLinkageInfo(T.getTypePtr()); |
| 148 | } |
| 149 | |
| 150 | LinkageInfo getDeclLinkageAndVisibility(const NamedDecl *D); |
| 151 | |
| 152 | LinkageInfo getTypeLinkageAndVisibility(const Type *T); |
| 153 | LinkageInfo getTypeLinkageAndVisibility(QualType T) { |
| 154 | return getTypeLinkageAndVisibility(T.getTypePtr()); |
| 155 | } |
| 156 | }; |
| 157 | } // namespace clang |
| 158 | |
| 159 | #endif |