1 | //===- llvm/TextAPI/Symbol.h - TAPI Symbol ----------------------*- 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 | #ifndef LLVM_TEXTAPI_MACHO_SYMBOL_H |
10 | #define LLVM_TEXTAPI_MACHO_SYMBOL_H |
11 | |
12 | #include "llvm/ADT/BitmaskEnum.h" |
13 | #include "llvm/ADT/StringRef.h" |
14 | #include "llvm/Support/Error.h" |
15 | #include "llvm/Support/raw_ostream.h" |
16 | #include "llvm/TextAPI/ArchitectureSet.h" |
17 | #include "llvm/TextAPI/Target.h" |
18 | |
19 | namespace llvm { |
20 | namespace MachO { |
21 | |
22 | // clang-format off |
23 | |
24 | /// Symbol flags. |
25 | enum class SymbolFlags : uint8_t { |
26 | /// No flags |
27 | None = 0, |
28 | |
29 | /// Thread-local value symbol |
30 | ThreadLocalValue = 1U << 0, |
31 | |
32 | /// Weak defined symbol |
33 | WeakDefined = 1U << 1, |
34 | |
35 | /// Weak referenced symbol |
36 | WeakReferenced = 1U << 2, |
37 | |
38 | /// Undefined |
39 | Undefined = 1U << 3, |
40 | |
41 | /// Rexported |
42 | Rexported = 1U << 4, |
43 | |
44 | LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/Rexported), |
45 | }; |
46 | |
47 | // clang-format on |
48 | |
49 | enum class SymbolKind : uint8_t { |
50 | GlobalSymbol, |
51 | ObjectiveCClass, |
52 | ObjectiveCClassEHType, |
53 | ObjectiveCInstanceVariable, |
54 | }; |
55 | |
56 | using TargetList = SmallVector<Target, 5>; |
57 | class Symbol { |
58 | public: |
59 | Symbol(SymbolKind Kind, StringRef Name, TargetList Targets, SymbolFlags Flags) |
60 | : Name(Name), Targets(std::move(Targets)), Kind(Kind), Flags(Flags) {} |
61 | |
62 | void addTarget(Target target) { Targets.emplace_back(target); } |
63 | SymbolKind getKind() const { return Kind; } |
64 | StringRef getName() const { return Name; } |
65 | ArchitectureSet getArchitectures() const { |
66 | return mapToArchitectureSet(Targets); |
67 | } |
68 | SymbolFlags getFlags() const { return Flags; } |
69 | |
70 | bool isWeakDefined() const { |
71 | return (Flags & SymbolFlags::WeakDefined) == SymbolFlags::WeakDefined; |
72 | } |
73 | |
74 | bool isWeakReferenced() const { |
75 | return (Flags & SymbolFlags::WeakReferenced) == SymbolFlags::WeakReferenced; |
76 | } |
77 | |
78 | bool isThreadLocalValue() const { |
79 | return (Flags & SymbolFlags::ThreadLocalValue) == |
80 | SymbolFlags::ThreadLocalValue; |
81 | } |
82 | |
83 | bool isUndefined() const { |
84 | return (Flags & SymbolFlags::Undefined) == SymbolFlags::Undefined; |
85 | } |
86 | |
87 | bool isReexported() const { |
88 | return (Flags & SymbolFlags::Rexported) == SymbolFlags::Rexported; |
89 | } |
90 | |
91 | using const_target_iterator = TargetList::const_iterator; |
92 | using const_target_range = llvm::iterator_range<const_target_iterator>; |
93 | const_target_range targets() const { return {Targets}; } |
94 | |
95 | using const_filtered_target_iterator = |
96 | llvm::filter_iterator<const_target_iterator, |
97 | std::function<bool(const Target &)>>; |
98 | using const_filtered_target_range = |
99 | llvm::iterator_range<const_filtered_target_iterator>; |
100 | const_filtered_target_range targets(ArchitectureSet architectures) const; |
101 | |
102 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
103 | void dump(raw_ostream &OS) const; |
104 | void dump() const { dump(llvm::errs()); } |
105 | #endif |
106 | |
107 | bool operator==(const Symbol &O) const { |
108 | return (Kind == O.Kind) && (Name == O.Name) && (Targets == O.Targets) && |
109 | (Flags == O.Flags); |
110 | } |
111 | |
112 | bool operator!=(const Symbol &O) const { return !(*this == O); } |
113 | |
114 | private: |
115 | StringRef Name; |
116 | TargetList Targets; |
117 | SymbolKind Kind; |
118 | SymbolFlags Flags; |
119 | }; |
120 | |
121 | } // end namespace MachO. |
122 | } // end namespace llvm. |
123 | |
124 | #endif // LLVM_TEXTAPI_MACHO_SYMBOL_H |
125 | |