clang  14.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 <cassert>
19 #include <cstdint>
20 
21 namespace clang {
22 
23 /// Describes the different kinds of visibility that a declaration
24 /// may have.
25 ///
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.
29 ///
30 /// Visibility is not described in any language standard and
31 /// (nonetheless) sometimes has odd behavior. Not all platforms
32 /// support all visibility kinds.
33 enum Visibility {
34  /// Objects with "hidden" visibility are not seen by the dynamic
35  /// linker.
37 
38  /// Objects with "protected" visibility are seen by the dynamic
39  /// linker but always dynamically resolve to an object within this
40  /// shared object.
42 
43  /// Objects with "default" visibility are seen by the dynamic linker
44  /// and act like normal objects.
46 };
47 
49  return L < R ? L : R;
50 }
51 
52 class LinkageInfo {
53  uint8_t linkage_ : 3;
54  uint8_t visibility_ : 2;
55  uint8_t explicit_ : 1;
56 
57  void setVisibility(Visibility V, bool E) { visibility_ = V; explicit_ = E; }
58 public:
59  LinkageInfo() : linkage_(ExternalLinkage), visibility_(DefaultVisibility),
60  explicit_(false) {}
62  : linkage_(L), visibility_(V), explicit_(E) {
63  assert(getLinkage() == L && getVisibility() == V &&
64  isVisibilityExplicit() == E && "Enum truncated!");
65  }
66 
67  static LinkageInfo external() {
68  return LinkageInfo();
69  }
70  static LinkageInfo internal() {
72  }
75  }
76  static LinkageInfo none() {
77  return LinkageInfo(NoLinkage, DefaultVisibility, false);
78  }
81  }
82 
83  Linkage getLinkage() const { return (Linkage)linkage_; }
84  Visibility getVisibility() const { return (Visibility)visibility_; }
85  bool isVisibilityExplicit() const { return explicit_; }
86 
87  void setLinkage(Linkage L) { linkage_ = L; }
88 
91  }
92  void mergeLinkage(LinkageInfo other) {
93  mergeLinkage(other.getLinkage());
94  }
95 
97  Linkage ThisL = getLinkage();
98  if (!isExternallyVisible(L)) {
99  if (ThisL == VisibleNoLinkage)
100  ThisL = NoLinkage;
101  else if (ThisL == ExternalLinkage)
102  ThisL = UniqueExternalLinkage;
103  }
104  setLinkage(ThisL);
105  }
108  }
109 
110  /// Merge in the visibility 'newVis'.
111  void mergeVisibility(Visibility newVis, bool newExplicit) {
112  Visibility oldVis = getVisibility();
113 
114  // Never increase visibility.
115  if (oldVis < newVis)
116  return;
117 
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)
121  return;
122 
123  // Otherwise, we're either decreasing visibility or making our
124  // existing visibility explicit.
125  setVisibility(newVis, newExplicit);
126  }
129  }
130 
131  /// Merge both linkage and visibility.
132  void merge(LinkageInfo other) {
133  mergeLinkage(other);
134  mergeVisibility(other);
135  }
136 
137  /// Merge linkage and conditionally merge visibility.
138  void mergeMaybeWithVisibility(LinkageInfo other, bool withVis) {
139  mergeLinkage(other);
140  if (withVis) mergeVisibility(other);
141  }
142 };
143 }
144 
145 #endif // LLVM_CLANG_BASIC_VISIBILITY_H
clang::InternalLinkage
@ InternalLinkage
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:31
clang::LinkageInfo::mergeLinkage
void mergeLinkage(LinkageInfo other)
Definition: Visibility.h:92
clang::HiddenVisibility
@ HiddenVisibility
Objects with "hidden" visibility are not seen by the dynamic linker.
Definition: Visibility.h:36
clang::VisibleNoLinkage
@ VisibleNoLinkage
No linkage according to the standard, but is visible from other translation units because of types de...
Definition: Linkage.h:44
clang::minVisibility
Visibility minVisibility(Visibility L, Visibility R)
Definition: Visibility.h:48
clang::LinkageInfo::mergeMaybeWithVisibility
void mergeMaybeWithVisibility(LinkageInfo other, bool withVis)
Merge linkage and conditionally merge visibility.
Definition: Visibility.h:138
clang::LinkageInfo::isVisibilityExplicit
bool isVisibilityExplicit() const
Definition: Visibility.h:85
clang::ProtectedVisibility
@ ProtectedVisibility
Objects with "protected" visibility are seen by the dynamic linker but always dynamically resolve to ...
Definition: Visibility.h:41
clang::LinkageInfo::merge
void merge(LinkageInfo other)
Merge both linkage and visibility.
Definition: Visibility.h:132
V
#define V(N, I)
Definition: ASTContext.h:3121
clang::LinkageInfo::none
static LinkageInfo none()
Definition: Visibility.h:76
clang::NoLinkage
@ NoLinkage
No linkage, which means that the entity is unique and can only be referred to from within its scope.
Definition: Linkage.h:26
clang::minLinkage
Linkage minLinkage(Linkage L1, Linkage L2)
Compute the minimum linkage given two linkages.
Definition: Linkage.h:120
clang::Linkage
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition: Linkage.h:23
clang::LinkageInfo::uniqueExternal
static LinkageInfo uniqueExternal()
Definition: Visibility.h:73
clang::UniqueExternalLinkage
@ UniqueExternalLinkage
External linkage within a unique namespace.
Definition: Linkage.h:40
clang::isExternallyVisible
bool isExternallyVisible(Linkage L)
Definition: Linkage.h:91
Linkage.h
clang::LinkageInfo
Definition: Visibility.h:52
clang::LinkageInfo::mergeVisibility
void mergeVisibility(Visibility newVis, bool newExplicit)
Merge in the visibility 'newVis'.
Definition: Visibility.h:111
clang::LinkageInfo::getVisibility
Visibility getVisibility() const
Definition: Visibility.h:84
clang::Visibility
Visibility
Describes the different kinds of visibility that a declaration may have.
Definition: Visibility.h:33
false
#define false
Definition: stdbool.h:17
clang::LinkageInfo::setLinkage
void setLinkage(Linkage L)
Definition: Visibility.h:87
clang::LinkageInfo::mergeVisibility
void mergeVisibility(LinkageInfo other)
Definition: Visibility.h:127
clang::LinkageInfo::visible_none
static LinkageInfo visible_none()
Definition: Visibility.h:79
clang::LinkageInfo::LinkageInfo
LinkageInfo()
Definition: Visibility.h:59
clang::LinkageInfo::mergeLinkage
void mergeLinkage(Linkage L)
Definition: Visibility.h:89
clang::LinkageInfo::getLinkage
Linkage getLinkage() const
Definition: Visibility.h:83
clang::LinkageInfo::external
static LinkageInfo external()
Definition: Visibility.h:67
clang
Definition: CalledOnceCheck.h:17
clang::ExternalLinkage
@ ExternalLinkage
External linkage, which indicates that the entity can be referred to from other translation units.
Definition: Linkage.h:59
clang::DefaultVisibility
@ DefaultVisibility
Objects with "default" visibility are seen by the dynamic linker and act like normal objects.
Definition: Visibility.h:45
clang::LinkageInfo::LinkageInfo
LinkageInfo(Linkage L, Visibility V, bool E)
Definition: Visibility.h:61
clang::LinkageInfo::mergeExternalVisibility
void mergeExternalVisibility(LinkageInfo Other)
Definition: Visibility.h:106
clang::LinkageInfo::mergeExternalVisibility
void mergeExternalVisibility(Linkage L)
Definition: Visibility.h:96