clang 19.0.0git
Visibility.h
Go to the documentation of this file.
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
22namespace 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.
35 /// Objects with "hidden" visibility are not seen by the dynamic
36 /// linker.
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.
43
44 /// Objects with "default" visibility are seen by the dynamic linker
45 /// and act like normal objects.
47};
48
50 return L < R ? L : R;
51}
52
54 LLVM_PREFERRED_TYPE(Linkage)
55 uint8_t linkage_ : 3;
56 LLVM_PREFERRED_TYPE(Visibility)
57 uint8_t visibility_ : 2;
58 LLVM_PREFERRED_TYPE(bool)
59 uint8_t explicit_ : 1;
60
61 void setVisibility(Visibility V, bool E) { visibility_ = V; explicit_ = E; }
62public:
64 : linkage_(llvm::to_underlying(Linkage::External)),
65 visibility_(DefaultVisibility), explicit_(false) {}
67 : linkage_(llvm::to_underlying(L)), visibility_(V), explicit_(E) {
68 assert(getLinkage() == L && getVisibility() == V &&
69 isVisibilityExplicit() == E && "Enum truncated!");
70 }
71
73 return LinkageInfo();
74 }
77 }
80 }
81 static LinkageInfo none() {
83 }
86 }
87
88 Linkage getLinkage() const { return static_cast<Linkage>(linkage_); }
89 Visibility getVisibility() const { return (Visibility)visibility_; }
90 bool isVisibilityExplicit() const { return explicit_; }
91
92 void setLinkage(Linkage L) { linkage_ = llvm::to_underlying(L); }
93
96 }
98 mergeLinkage(other.getLinkage());
99 }
100
102 Linkage ThisL = getLinkage();
103 if (!isExternallyVisible(L)) {
104 if (ThisL == Linkage::VisibleNone)
105 ThisL = Linkage::None;
106 else if (ThisL == Linkage::External)
108 }
109 setLinkage(ThisL);
110 }
112 mergeExternalVisibility(Other.getLinkage());
113 }
114
115 /// Merge in the visibility 'newVis'.
116 void mergeVisibility(Visibility newVis, bool newExplicit) {
117 Visibility oldVis = getVisibility();
118
119 // Never increase visibility.
120 if (oldVis < newVis)
121 return;
122
123 // If the new visibility is the same as the old and the new
124 // visibility isn't explicit, we have nothing to add.
125 if (oldVis == newVis && !newExplicit)
126 return;
127
128 // Otherwise, we're either decreasing visibility or making our
129 // existing visibility explicit.
130 setVisibility(newVis, newExplicit);
131 }
134 }
135
136 /// Merge both linkage and visibility.
137 void merge(LinkageInfo other) {
138 mergeLinkage(other);
139 mergeVisibility(other);
140 }
141
142 /// Merge linkage and conditionally merge visibility.
143 void mergeMaybeWithVisibility(LinkageInfo other, bool withVis) {
144 mergeLinkage(other);
145 if (withVis) mergeVisibility(other);
146 }
147};
148}
149
150#endif // LLVM_CLANG_BASIC_VISIBILITY_H
#define V(N, I)
Definition: ASTContext.h:3294
Visibility getVisibility() const
Definition: Visibility.h:89
static LinkageInfo external()
Definition: Visibility.h:72
static LinkageInfo none()
Definition: Visibility.h:81
void setLinkage(Linkage L)
Definition: Visibility.h:92
void mergeExternalVisibility(Linkage L)
Definition: Visibility.h:101
void mergeMaybeWithVisibility(LinkageInfo other, bool withVis)
Merge linkage and conditionally merge visibility.
Definition: Visibility.h:143
void mergeLinkage(Linkage L)
Definition: Visibility.h:94
Linkage getLinkage() const
Definition: Visibility.h:88
void mergeExternalVisibility(LinkageInfo Other)
Definition: Visibility.h:111
void mergeVisibility(LinkageInfo other)
Definition: Visibility.h:132
static LinkageInfo internal()
Definition: Visibility.h:75
static LinkageInfo visible_none()
Definition: Visibility.h:84
LinkageInfo(Linkage L, Visibility V, bool E)
Definition: Visibility.h:66
static LinkageInfo uniqueExternal()
Definition: Visibility.h:78
void mergeVisibility(Visibility newVis, bool newExplicit)
Merge in the visibility 'newVis'.
Definition: Visibility.h:116
void mergeLinkage(LinkageInfo other)
Definition: Visibility.h:97
bool isVisibilityExplicit() const
Definition: Visibility.h:90
void merge(LinkageInfo other)
Merge both linkage and visibility.
Definition: Visibility.h:137
Defines the Linkage enumeration and various utility functions.
The JSON file list parser is used to communicate input to InstallAPI.
Linkage minLinkage(Linkage L1, Linkage L2)
Compute the minimum linkage given two linkages.
Definition: Linkage.h:129
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition: Linkage.h:24
@ VisibleNone
No linkage according to the standard, but is visible from other translation units because of types de...
@ None
No linkage, which means that the entity is unique and can only be referred to from within its scope.
@ UniqueExternal
External linkage within a unique namespace.
@ Internal
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
@ External
External linkage, which indicates that the entity can be referred to from other translation units.
Visibility minVisibility(Visibility L, Visibility R)
Definition: Visibility.h:49
bool isExternallyVisible(Linkage L)
Definition: Linkage.h:90
@ Other
Other implicit parameter.
Visibility
Describes the different kinds of visibility that a declaration may have.
Definition: Visibility.h:34
@ HiddenVisibility
Objects with "hidden" visibility are not seen by the dynamic linker.
Definition: Visibility.h:37
@ ProtectedVisibility
Objects with "protected" visibility are seen by the dynamic linker but always dynamically resolve to ...
Definition: Visibility.h:42
@ DefaultVisibility
Objects with "default" visibility are seen by the dynamic linker and act like normal objects.
Definition: Visibility.h:46
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
#define false
Definition: stdbool.h:26