1 //===--- Visibility.h - Visibility enumeration and utilities ----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Defines the clang::Visibility enumeration and various utility
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_BASIC_VISIBILITY_H
15 #define LLVM_CLANG_BASIC_VISIBILITY_H
17 #include "clang/Basic/Linkage.h"
23 /// Describes the different kinds of visibility that a declaration
26 /// Visibility determines how a declaration interacts with the dynamic
27 /// linker. It may also affect whether the symbol can be found by runtime
28 /// symbol lookup APIs.
30 /// Visibility is not described in any language standard and
31 /// (nonetheless) sometimes has odd behavior. Not all platforms
32 /// support all visibility kinds.
34 /// Objects with "hidden" visibility are not seen by the dynamic
38 /// Objects with "protected" visibility are seen by the dynamic
39 /// linker but always dynamically resolve to an object within this
43 /// Objects with "default" visibility are seen by the dynamic linker
44 /// and act like normal objects.
48 inline Visibility minVisibility(Visibility L, Visibility R) {
54 uint8_t visibility_ : 2;
55 uint8_t explicit_ : 1;
57 void setVisibility(Visibility V, bool E) { visibility_ = V; explicit_ = E; }
59 LinkageInfo() : linkage_(ExternalLinkage), visibility_(DefaultVisibility),
61 LinkageInfo(Linkage L, Visibility V, bool E)
62 : linkage_(L), visibility_(V), explicit_(E) {
63 assert(getLinkage() == L && getVisibility() == V &&
64 isVisibilityExplicit() == E && "Enum truncated!");
67 static LinkageInfo external() {
70 static LinkageInfo internal() {
71 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
73 static LinkageInfo uniqueExternal() {
74 return LinkageInfo(UniqueExternalLinkage, DefaultVisibility, false);
76 static LinkageInfo none() {
77 return LinkageInfo(NoLinkage, DefaultVisibility, false);
79 static LinkageInfo visible_none() {
80 return LinkageInfo(VisibleNoLinkage, DefaultVisibility, false);
83 Linkage getLinkage() const { return (Linkage)linkage_; }
84 Visibility getVisibility() const { return (Visibility)visibility_; }
85 bool isVisibilityExplicit() const { return explicit_; }
87 void setLinkage(Linkage L) { linkage_ = L; }
89 void mergeLinkage(Linkage L) {
90 setLinkage(minLinkage(getLinkage(), L));
92 void mergeLinkage(LinkageInfo other) {
93 mergeLinkage(other.getLinkage());
96 void mergeExternalVisibility(Linkage L) {
97 Linkage ThisL = getLinkage();
98 if (!isExternallyVisible(L)) {
99 if (ThisL == VisibleNoLinkage)
101 else if (ThisL == ExternalLinkage)
102 ThisL = UniqueExternalLinkage;
106 void mergeExternalVisibility(LinkageInfo Other) {
107 mergeExternalVisibility(Other.getLinkage());
110 /// Merge in the visibility 'newVis'.
111 void mergeVisibility(Visibility newVis, bool newExplicit) {
112 Visibility oldVis = getVisibility();
114 // Never increase visibility.
118 // If the new visibility is the same as the old and the new
119 // visibility isn't explicit, we have nothing to add.
120 if (oldVis == newVis && !newExplicit)
123 // Otherwise, we're either decreasing visibility or making our
124 // existing visibility explicit.
125 setVisibility(newVis, newExplicit);
127 void mergeVisibility(LinkageInfo other) {
128 mergeVisibility(other.getVisibility(), other.isVisibilityExplicit());
131 /// Merge both linkage and visibility.
132 void merge(LinkageInfo other) {
134 mergeVisibility(other);
137 /// Merge linkage and conditionally merge visibility.
138 void mergeMaybeWithVisibility(LinkageInfo other, bool withVis) {
140 if (withVis) mergeVisibility(other);
145 #endif // LLVM_CLANG_BASIC_VISIBILITY_H