1//===- Symbol.cpp ---------------------------------------------------------===//
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// Implements the Symbol.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/TextAPI/Symbol.h"
14#include <string>
15
16namespace llvm {
17namespace MachO {
18
19#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
20LLVM_DUMP_METHOD void Symbol::dump(raw_ostream &OS) const {
21 std::string Result;
22 if (isUndefined())
23 Result += "(undef) ";
24 if (isWeakDefined())
25 Result += "(weak-def) ";
26 if (isWeakReferenced())
27 Result += "(weak-ref) ";
28 if (isThreadLocalValue())
29 Result += "(tlv) ";
30 switch (Kind) {
31 case EncodeKind::GlobalSymbol:
32 Result += Name.str();
33 break;
34 case EncodeKind::ObjectiveCClass:
35 Result += "(ObjC Class) " + Name.str();
36 break;
37 case EncodeKind::ObjectiveCClassEHType:
38 Result += "(ObjC Class EH) " + Name.str();
39 break;
40 case EncodeKind::ObjectiveCInstanceVariable:
41 Result += "(ObjC IVar) " + Name.str();
42 break;
43 }
44 OS << Result;
45}
46#endif
47
48Symbol::const_filtered_target_range
49Symbol::targets(ArchitectureSet Architectures) const {
50 std::function<bool(const Target &)> FN =
51 [Architectures](const Target &Target) {
52 return Architectures.has(Arch: Target.Arch);
53 };
54 return make_filter_range(Range: Targets, Pred: FN);
55}
56
57bool Symbol::operator==(const Symbol &O) const {
58 // Older Tapi files do not express all these symbol flags. In those
59 // cases, ignore those differences.
60 auto RemoveFlag = [](const Symbol &Sym, SymbolFlags &Flag) {
61 if (Sym.isData())
62 Flag &= ~SymbolFlags::Data;
63 if (Sym.isText())
64 Flag &= ~SymbolFlags::Text;
65 };
66 SymbolFlags LHSFlags = Flags;
67 SymbolFlags RHSFlags = O.Flags;
68 // Ignore Text and Data for now.
69 RemoveFlag(*this, LHSFlags);
70 RemoveFlag(O, RHSFlags);
71 return std::tie(args: Name, args: Kind, args: Targets, args&: LHSFlags) ==
72 std::tie(args: O.Name, args: O.Kind, args: O.Targets, args&: RHSFlags);
73}
74
75SimpleSymbol parseSymbol(StringRef SymName) {
76 if (SymName.starts_with(Prefix: ObjC1ClassNamePrefix))
77 return {.Name: SymName.drop_front(N: ObjC1ClassNamePrefix.size()),
78 .Kind: EncodeKind::ObjectiveCClass, .ObjCInterfaceType: ObjCIFSymbolKind::Class};
79 if (SymName.starts_with(Prefix: ObjC2ClassNamePrefix))
80 return {.Name: SymName.drop_front(N: ObjC2ClassNamePrefix.size()),
81 .Kind: EncodeKind::ObjectiveCClass, .ObjCInterfaceType: ObjCIFSymbolKind::Class};
82 if (SymName.starts_with(Prefix: ObjC2MetaClassNamePrefix))
83 return {.Name: SymName.drop_front(N: ObjC2MetaClassNamePrefix.size()),
84 .Kind: EncodeKind::ObjectiveCClass, .ObjCInterfaceType: ObjCIFSymbolKind::MetaClass};
85 if (SymName.starts_with(Prefix: ObjC2EHTypePrefix))
86 return {.Name: SymName.drop_front(N: ObjC2EHTypePrefix.size()),
87 .Kind: EncodeKind::ObjectiveCClassEHType, .ObjCInterfaceType: ObjCIFSymbolKind::EHType};
88 if (SymName.starts_with(Prefix: ObjC2IVarPrefix))
89 return {.Name: SymName.drop_front(N: ObjC2IVarPrefix.size()),
90 .Kind: EncodeKind::ObjectiveCInstanceVariable, .ObjCInterfaceType: ObjCIFSymbolKind::None};
91 return {.Name: SymName, .Kind: EncodeKind::GlobalSymbol, .ObjCInterfaceType: ObjCIFSymbolKind::None};
92}
93
94} // end namespace MachO.
95} // end namespace llvm.
96