1 //===--- Visibility.h - Visibility enumeration and 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 /// \file
11 /// Defines the clang::Visibility enumeration and various utility
12 /// functions.
13 ///
14 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_BASIC_VISIBILITY_H
16 #define LLVM_CLANG_BASIC_VISIBILITY_H
17
18 #include "clang/Basic/Linkage.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
minVisibility(Visibility L,Visibility R)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
setVisibility(Visibility V,bool E)58 void setVisibility(Visibility V, bool E) { visibility_ = V; explicit_ = E; }
59 public:
LinkageInfo()60 LinkageInfo() : linkage_(ExternalLinkage), visibility_(DefaultVisibility),
61 explicit_(false) {}
LinkageInfo(Linkage L,Visibility V,bool E)62 LinkageInfo(Linkage L, Visibility V, bool E)
63 : linkage_(L), visibility_(V), explicit_(E) {
64 assert(getLinkage() == L && getVisibility() == V &&
65 isVisibilityExplicit() == E && "Enum truncated!");
66 }
67
external()68 static LinkageInfo external() {
69 return LinkageInfo();
70 }
internal()71 static LinkageInfo internal() {
72 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
73 }
uniqueExternal()74 static LinkageInfo uniqueExternal() {
75 return LinkageInfo(UniqueExternalLinkage, DefaultVisibility, false);
76 }
none()77 static LinkageInfo none() {
78 return LinkageInfo(NoLinkage, DefaultVisibility, false);
79 }
visible_none()80 static LinkageInfo visible_none() {
81 return LinkageInfo(VisibleNoLinkage, DefaultVisibility, false);
82 }
83
getLinkage()84 Linkage getLinkage() const { return (Linkage)linkage_; }
getVisibility()85 Visibility getVisibility() const { return (Visibility)visibility_; }
isVisibilityExplicit()86 bool isVisibilityExplicit() const { return explicit_; }
87
setLinkage(Linkage L)88 void setLinkage(Linkage L) { linkage_ = L; }
89
mergeLinkage(Linkage L)90 void mergeLinkage(Linkage L) {
91 setLinkage(minLinkage(getLinkage(), L));
92 }
mergeLinkage(LinkageInfo other)93 void mergeLinkage(LinkageInfo other) {
94 mergeLinkage(other.getLinkage());
95 }
96
mergeExternalVisibility(Linkage L)97 void mergeExternalVisibility(Linkage L) {
98 Linkage ThisL = getLinkage();
99 if (!isExternallyVisible(L)) {
100 if (ThisL == VisibleNoLinkage)
101 ThisL = NoLinkage;
102 else if (ThisL == ExternalLinkage)
103 ThisL = UniqueExternalLinkage;
104 }
105 setLinkage(ThisL);
106 }
mergeExternalVisibility(LinkageInfo Other)107 void mergeExternalVisibility(LinkageInfo Other) {
108 mergeExternalVisibility(Other.getLinkage());
109 }
110
111 /// Merge in the visibility 'newVis'.
mergeVisibility(Visibility newVis,bool newExplicit)112 void mergeVisibility(Visibility newVis, bool newExplicit) {
113 Visibility oldVis = getVisibility();
114
115 // Never increase visibility.
116 if (oldVis < newVis)
117 return;
118
119 // If the new visibility is the same as the old and the new
120 // visibility isn't explicit, we have nothing to add.
121 if (oldVis == newVis && !newExplicit)
122 return;
123
124 // Otherwise, we're either decreasing visibility or making our
125 // existing visibility explicit.
126 setVisibility(newVis, newExplicit);
127 }
mergeVisibility(LinkageInfo other)128 void mergeVisibility(LinkageInfo other) {
129 mergeVisibility(other.getVisibility(), other.isVisibilityExplicit());
130 }
131
132 /// Merge both linkage and visibility.
merge(LinkageInfo other)133 void merge(LinkageInfo other) {
134 mergeLinkage(other);
135 mergeVisibility(other);
136 }
137
138 /// Merge linkage and conditionally merge visibility.
mergeMaybeWithVisibility(LinkageInfo other,bool withVis)139 void mergeMaybeWithVisibility(LinkageInfo other, bool withVis) {
140 mergeLinkage(other);
141 if (withVis) mergeVisibility(other);
142 }
143 };
144 }
145
146 #endif // LLVM_CLANG_BASIC_VISIBILITY_H
147