clang 18.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 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; }
59public:
61 : linkage_(llvm::to_underlying(Linkage::External)),
62 visibility_(DefaultVisibility), explicit_(false) {}
64 : linkage_(llvm::to_underlying(L)), visibility_(V), explicit_(E) {
65 assert(getLinkage() == L && getVisibility() == V &&
66 isVisibilityExplicit() == E && "Enum truncated!");
67 }
68
70 return LinkageInfo();
71 }
74 }
77 }
78 static LinkageInfo none() {
80 }
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
93 }
95 mergeLinkage(other.getLinkage());
96 }
97
99 Linkage ThisL = getLinkage();
100 if (!isExternallyVisible(L)) {
101 if (ThisL == Linkage::VisibleNone)
102 ThisL = Linkage::None;
103 else if (ThisL == Linkage::External)
105 }
106 setLinkage(ThisL);
107 }
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 }
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
#define V(N, I)
Definition: ASTContext.h:3241
Visibility getVisibility() const
Definition: Visibility.h:86
static LinkageInfo external()
Definition: Visibility.h:69
static LinkageInfo none()
Definition: Visibility.h:78
void setLinkage(Linkage L)
Definition: Visibility.h:89
void mergeExternalVisibility(Linkage L)
Definition: Visibility.h:98
void mergeMaybeWithVisibility(LinkageInfo other, bool withVis)
Merge linkage and conditionally merge visibility.
Definition: Visibility.h:140
void mergeLinkage(Linkage L)
Definition: Visibility.h:91
Linkage getLinkage() const
Definition: Visibility.h:85
void mergeExternalVisibility(LinkageInfo Other)
Definition: Visibility.h:108
void mergeVisibility(LinkageInfo other)
Definition: Visibility.h:129
static LinkageInfo internal()
Definition: Visibility.h:72
static LinkageInfo visible_none()
Definition: Visibility.h:81
LinkageInfo(Linkage L, Visibility V, bool E)
Definition: Visibility.h:63
static LinkageInfo uniqueExternal()
Definition: Visibility.h:75
void mergeVisibility(Visibility newVis, bool newExplicit)
Merge in the visibility 'newVis'.
Definition: Visibility.h:113
void mergeLinkage(LinkageInfo other)
Definition: Visibility.h:94
bool isVisibilityExplicit() const
Definition: Visibility.h:87
void merge(LinkageInfo other)
Merge both linkage and visibility.
Definition: Visibility.h:134
Defines the Linkage enumeration and various utility functions.
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
YAML serialization mapping.
Definition: Dominators.h:30
#define false
Definition: stdbool.h:22