clang  8.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 /// 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 #include <cassert>
20 #include <cstdint>
21 
22 namespace 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.
34 enum Visibility {
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 
53 class LinkageInfo {
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; }
59 public:
60  LinkageInfo() : linkage_(ExternalLinkage), visibility_(DefaultVisibility),
61  explicit_(false) {}
63  : linkage_(L), visibility_(V), explicit_(E) {
64  assert(getLinkage() == L && getVisibility() == V &&
65  isVisibilityExplicit() == E && "Enum truncated!");
66  }
67 
68  static LinkageInfo external() {
69  return LinkageInfo();
70  }
71  static LinkageInfo internal() {
73  }
76  }
77  static LinkageInfo none() {
78  return LinkageInfo(NoLinkage, DefaultVisibility, false);
79  }
82  }
83 
84  Linkage getLinkage() const { return (Linkage)linkage_; }
85  Visibility getVisibility() const { return (Visibility)visibility_; }
86  bool isVisibilityExplicit() const { return explicit_; }
87 
88  void setLinkage(Linkage L) { linkage_ = L; }
89 
92  }
93  void mergeLinkage(LinkageInfo other) {
94  mergeLinkage(other.getLinkage());
95  }
96 
98  Linkage ThisL = getLinkage();
99  if (!isExternallyVisible(L)) {
100  if (ThisL == VisibleNoLinkage)
101  ThisL = NoLinkage;
102  else if (ThisL == ExternalLinkage)
103  ThisL = UniqueExternalLinkage;
104  }
105  setLinkage(ThisL);
106  }
109  }
110 
111  /// Merge in the visibility 'newVis'.
112  void mergeVisibility(Visibility newVis, bool newExplicit) {
113  Visibility oldVis = getVisibility();
114 
115  // Never increase visibility.
116  if (oldVis < newVis)
117  return;
118 
119  // If the new visibility is the same as the old and the new
120  // visibility isn't explicit, we have nothing to add.
121  if (oldVis == newVis && !newExplicit)
122  return;
123 
124  // Otherwise, we're either decreasing visibility or making our
125  // existing visibility explicit.
126  setVisibility(newVis, newExplicit);
127  }
130  }
131 
132  /// Merge both linkage and visibility.
133  void merge(LinkageInfo other) {
134  mergeLinkage(other);
135  mergeVisibility(other);
136  }
137 
138  /// Merge linkage and conditionally merge visibility.
139  void mergeMaybeWithVisibility(LinkageInfo other, bool withVis) {
140  mergeLinkage(other);
141  if (withVis) mergeVisibility(other);
142  }
143 };
144 }
145 
146 #endif // LLVM_CLANG_BASIC_VISIBILITY_H
void setLinkage(Linkage L)
Definition: Visibility.h:88
External linkage, which indicates that the entity can be referred to from other translation units...
Definition: Linkage.h:60
No linkage, which means that the entity is unique and can only be referred to from within its scope...
Definition: Linkage.h:27
Objects with "hidden" visibility are not seen by the dynamic linker.
Definition: Visibility.h:37
void mergeVisibility(Visibility newVis, bool newExplicit)
Merge in the visibility &#39;newVis&#39;.
Definition: Visibility.h:112
Visibility getVisibility() const
Definition: Visibility.h:85
Visibility minVisibility(Visibility L, Visibility R)
Definition: Visibility.h:49
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:24
Objects with "default" visibility are seen by the dynamic linker and act like normal objects...
Definition: Visibility.h:46
Visibility
Describes the different kinds of visibility that a declaration may have.
Definition: Visibility.h:34
LinkageInfo(Linkage L, Visibility V, bool E)
Definition: Visibility.h:62
Defines the Linkage enumeration and various utility functions.
void mergeExternalVisibility(Linkage L)
Definition: Visibility.h:97
Linkage getLinkage() const
Definition: Visibility.h:84
Objects with "protected" visibility are seen by the dynamic linker but always dynamically resolve to ...
Definition: Visibility.h:42
bool isExternallyVisible(Linkage L)
Definition: Linkage.h:86
static LinkageInfo visible_none()
Definition: Visibility.h:80
static LinkageInfo external()
Definition: Visibility.h:68
#define false
Definition: stdbool.h:33
Linkage minLinkage(Linkage L1, Linkage L2)
Compute the minimum linkage given two linkages.
Definition: Linkage.h:115
Dataflow Directional Tag Classes.
External linkage within a unique namespace.
Definition: Linkage.h:41
void mergeExternalVisibility(LinkageInfo Other)
Definition: Visibility.h:107
void mergeVisibility(LinkageInfo other)
Definition: Visibility.h:128
bool isVisibilityExplicit() const
Definition: Visibility.h:86
void mergeLinkage(Linkage L)
Definition: Visibility.h:90
void mergeLinkage(LinkageInfo other)
Definition: Visibility.h:93
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:32
void merge(LinkageInfo other)
Merge both linkage and visibility.
Definition: Visibility.h:133
No linkage according to the standard, but is visible from other translation units because of types de...
Definition: Linkage.h:45
void mergeMaybeWithVisibility(LinkageInfo other, bool withVis)
Merge linkage and conditionally merge visibility.
Definition: Visibility.h:139
static LinkageInfo none()
Definition: Visibility.h:77
static LinkageInfo uniqueExternal()
Definition: Visibility.h:74