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