1 //===--- Visibility.h - Visibility enumeration and 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 /// \file
10 /// Defines the clang::Visibility enumeration and various utility
11 /// functions.
12 ///
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_BASIC_VISIBILITY_H
15 #define LLVM_CLANG_BASIC_VISIBILITY_H
16 
17 #include "clang/Basic/Linkage.h"
18 #include "llvm/ADT/STLForwardCompat.h"
19 #include <cassert>
20 #include <cstdint>
21 
22 namespace clang {
23 
24 /// Describes the different kinds of visibility that a declaration
25 /// may have.
26 ///
27 /// Visibility determines how a declaration interacts with the dynamic
28 /// linker.  It may also affect whether the symbol can be found by runtime
29 /// symbol lookup APIs.
30 ///
31 /// Visibility is not described in any language standard and
32 /// (nonetheless) sometimes has odd behavior.  Not all platforms
33 /// support all visibility kinds.
34 enum Visibility {
35   /// Objects with "hidden" visibility are not seen by the dynamic
36   /// linker.
37   HiddenVisibility,
38 
39   /// Objects with "protected" visibility are seen by the dynamic
40   /// linker but always dynamically resolve to an object within this
41   /// shared object.
42   ProtectedVisibility,
43 
44   /// Objects with "default" visibility are seen by the dynamic linker
45   /// and act like normal objects.
46   DefaultVisibility
47 };
48 
49 inline Visibility minVisibility(Visibility L, Visibility R) {
50   return L < R ? L : R;
51 }
52 
53 class LinkageInfo {
54   uint8_t linkage_    : 3;
55   uint8_t visibility_ : 2;
56   uint8_t explicit_   : 1;
57 
58   void setVisibility(Visibility V, bool E) { visibility_ = V; explicit_ = E; }
59 public:
60   LinkageInfo()
61       : linkage_(llvm::to_underlying(Linkage::External)),
62         visibility_(DefaultVisibility), explicit_(false) {}
63   LinkageInfo(Linkage L, Visibility V, bool E)
64       : linkage_(llvm::to_underlying(L)), visibility_(V), explicit_(E) {
65     assert(getLinkage() == L && getVisibility() == V &&
66            isVisibilityExplicit() == E && "Enum truncated!");
67   }
68 
69   static LinkageInfo external() {
70     return LinkageInfo();
71   }
72   static LinkageInfo internal() {
73     return LinkageInfo(Linkage::Internal, DefaultVisibility, false);
74   }
75   static LinkageInfo uniqueExternal() {
76     return LinkageInfo(Linkage::UniqueExternal, DefaultVisibility, false);
77   }
78   static LinkageInfo none() {
79     return LinkageInfo(Linkage::None, DefaultVisibility, false);
80   }
81   static LinkageInfo visible_none() {
82     return LinkageInfo(Linkage::VisibleNone, DefaultVisibility, false);
83   }
84 
85   Linkage getLinkage() const { return static_cast<Linkage>(linkage_); }
86   Visibility getVisibility() const { return (Visibility)visibility_; }
87   bool isVisibilityExplicit() const { return explicit_; }
88 
89   void setLinkage(Linkage L) { linkage_ = llvm::to_underlying(L); }
90 
91   void mergeLinkage(Linkage L) {
92     setLinkage(minLinkage(getLinkage(), L));
93   }
94   void mergeLinkage(LinkageInfo other) {
95     mergeLinkage(other.getLinkage());
96   }
97 
98   void mergeExternalVisibility(Linkage L) {
99     Linkage ThisL = getLinkage();
100     if (!isExternallyVisible(L)) {
101       if (ThisL == Linkage::VisibleNone)
102         ThisL = Linkage::None;
103       else if (ThisL == Linkage::External)
104         ThisL = Linkage::UniqueExternal;
105     }
106     setLinkage(ThisL);
107   }
108   void mergeExternalVisibility(LinkageInfo Other) {
109     mergeExternalVisibility(Other.getLinkage());
110   }
111 
112   /// Merge in the visibility 'newVis'.
113   void mergeVisibility(Visibility newVis, bool newExplicit) {
114     Visibility oldVis = getVisibility();
115 
116     // Never increase visibility.
117     if (oldVis < newVis)
118       return;
119 
120     // If the new visibility is the same as the old and the new
121     // visibility isn't explicit, we have nothing to add.
122     if (oldVis == newVis && !newExplicit)
123       return;
124 
125     // Otherwise, we're either decreasing visibility or making our
126     // existing visibility explicit.
127     setVisibility(newVis, newExplicit);
128   }
129   void mergeVisibility(LinkageInfo other) {
130     mergeVisibility(other.getVisibility(), other.isVisibilityExplicit());
131   }
132 
133   /// Merge both linkage and visibility.
134   void merge(LinkageInfo other) {
135     mergeLinkage(other);
136     mergeVisibility(other);
137   }
138 
139   /// Merge linkage and conditionally merge visibility.
140   void mergeMaybeWithVisibility(LinkageInfo other, bool withVis) {
141     mergeLinkage(other);
142     if (withVis) mergeVisibility(other);
143   }
144 };
145 }
146 
147 #endif // LLVM_CLANG_BASIC_VISIBILITY_H
148