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