xref: /freebsd/contrib/llvm-project/llvm/lib/TextAPI/Symbol.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1fe6060f1SDimitry Andric //===- Symbol.cpp ---------------------------------------------------------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric //
9fe6060f1SDimitry Andric // Implements the Symbol.
10fe6060f1SDimitry Andric //
11fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
12fe6060f1SDimitry Andric 
13fe6060f1SDimitry Andric #include "llvm/TextAPI/Symbol.h"
14fe6060f1SDimitry Andric #include <string>
15fe6060f1SDimitry Andric 
16fe6060f1SDimitry Andric namespace llvm {
17fe6060f1SDimitry Andric namespace MachO {
18fe6060f1SDimitry Andric 
19fe6060f1SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
20fe6060f1SDimitry Andric LLVM_DUMP_METHOD void Symbol::dump(raw_ostream &OS) const {
21fe6060f1SDimitry Andric   std::string Result;
22fe6060f1SDimitry Andric   if (isUndefined())
23fe6060f1SDimitry Andric     Result += "(undef) ";
24fe6060f1SDimitry Andric   if (isWeakDefined())
25fe6060f1SDimitry Andric     Result += "(weak-def) ";
26fe6060f1SDimitry Andric   if (isWeakReferenced())
27fe6060f1SDimitry Andric     Result += "(weak-ref) ";
28fe6060f1SDimitry Andric   if (isThreadLocalValue())
29fe6060f1SDimitry Andric     Result += "(tlv) ";
30fe6060f1SDimitry Andric   switch (Kind) {
31fe6060f1SDimitry Andric   case SymbolKind::GlobalSymbol:
32fe6060f1SDimitry Andric     Result += Name.str();
33fe6060f1SDimitry Andric     break;
34fe6060f1SDimitry Andric   case SymbolKind::ObjectiveCClass:
35fe6060f1SDimitry Andric     Result += "(ObjC Class) " + Name.str();
36fe6060f1SDimitry Andric     break;
37fe6060f1SDimitry Andric   case SymbolKind::ObjectiveCClassEHType:
38fe6060f1SDimitry Andric     Result += "(ObjC Class EH) " + Name.str();
39fe6060f1SDimitry Andric     break;
40fe6060f1SDimitry Andric   case SymbolKind::ObjectiveCInstanceVariable:
41fe6060f1SDimitry Andric     Result += "(ObjC IVar) " + Name.str();
42fe6060f1SDimitry Andric     break;
43fe6060f1SDimitry Andric   }
44fe6060f1SDimitry Andric   OS << Result;
45fe6060f1SDimitry Andric }
46fe6060f1SDimitry Andric #endif
47fe6060f1SDimitry Andric 
48fe6060f1SDimitry Andric Symbol::const_filtered_target_range
49fe6060f1SDimitry Andric Symbol::targets(ArchitectureSet Architectures) const {
50fe6060f1SDimitry Andric   std::function<bool(const Target &)> FN =
51fe6060f1SDimitry Andric       [Architectures](const Target &Target) {
52fe6060f1SDimitry Andric         return Architectures.has(Target.Arch);
53fe6060f1SDimitry Andric       };
54fe6060f1SDimitry Andric   return make_filter_range(Targets, FN);
55fe6060f1SDimitry Andric }
56fe6060f1SDimitry Andric 
5706c3fb27SDimitry Andric bool Symbol::operator==(const Symbol &O) const {
5806c3fb27SDimitry Andric   // Older Tapi files do not express all these symbol flags. In those
5906c3fb27SDimitry Andric   // cases, ignore those differences.
6006c3fb27SDimitry Andric   auto RemoveFlag = [](const Symbol &Sym, SymbolFlags &Flag) {
6106c3fb27SDimitry Andric     if (Sym.isData())
6206c3fb27SDimitry Andric       Flag &= ~SymbolFlags::Data;
6306c3fb27SDimitry Andric     if (Sym.isText())
6406c3fb27SDimitry Andric       Flag &= ~SymbolFlags::Text;
6506c3fb27SDimitry Andric   };
6606c3fb27SDimitry Andric   SymbolFlags LHSFlags = Flags;
6706c3fb27SDimitry Andric   SymbolFlags RHSFlags = O.Flags;
6806c3fb27SDimitry Andric   // Ignore Text and Data for now.
6906c3fb27SDimitry Andric   RemoveFlag(*this, LHSFlags);
7006c3fb27SDimitry Andric   RemoveFlag(O, RHSFlags);
7106c3fb27SDimitry Andric   return std::tie(Name, Kind, Targets, LHSFlags) ==
7206c3fb27SDimitry Andric          std::tie(O.Name, O.Kind, O.Targets, RHSFlags);
7306c3fb27SDimitry Andric }
7406c3fb27SDimitry Andric 
75*5f757f3fSDimitry Andric SimpleSymbol parseSymbol(StringRef SymName, const SymbolFlags Flags) {
76*5f757f3fSDimitry Andric   if (SymName.starts_with(ObjC1ClassNamePrefix))
77*5f757f3fSDimitry Andric     return {SymName.drop_front(ObjC1ClassNamePrefix.size()),
78*5f757f3fSDimitry Andric             SymbolKind::ObjectiveCClass};
79*5f757f3fSDimitry Andric   if (SymName.starts_with(ObjC2ClassNamePrefix))
80*5f757f3fSDimitry Andric     return {SymName.drop_front(ObjC2ClassNamePrefix.size()),
81*5f757f3fSDimitry Andric             SymbolKind::ObjectiveCClass};
82*5f757f3fSDimitry Andric   if (SymName.starts_with(ObjC2MetaClassNamePrefix))
83*5f757f3fSDimitry Andric     return {SymName.drop_front(ObjC2MetaClassNamePrefix.size()),
84*5f757f3fSDimitry Andric             SymbolKind::ObjectiveCClass};
85*5f757f3fSDimitry Andric   if (SymName.starts_with(ObjC2EHTypePrefix)) {
86*5f757f3fSDimitry Andric     // When classes without ehtype are used in try/catch blocks
87*5f757f3fSDimitry Andric     // a weak-defined symbol is exported. In those cases, treat these as a
88*5f757f3fSDimitry Andric     // global instead.
89*5f757f3fSDimitry Andric     if ((Flags & SymbolFlags::WeakDefined) == SymbolFlags::WeakDefined)
90*5f757f3fSDimitry Andric       return {SymName, SymbolKind::GlobalSymbol};
91*5f757f3fSDimitry Andric     return {SymName.drop_front(ObjC2EHTypePrefix.size()),
92*5f757f3fSDimitry Andric             SymbolKind::ObjectiveCClassEHType};
93*5f757f3fSDimitry Andric   }
94*5f757f3fSDimitry Andric 
95*5f757f3fSDimitry Andric   if (SymName.starts_with(ObjC2IVarPrefix))
96*5f757f3fSDimitry Andric     return {SymName.drop_front(ObjC2IVarPrefix.size()),
97*5f757f3fSDimitry Andric             SymbolKind::ObjectiveCInstanceVariable};
98*5f757f3fSDimitry Andric   return {SymName, SymbolKind::GlobalSymbol};
99*5f757f3fSDimitry Andric }
100*5f757f3fSDimitry Andric 
101fe6060f1SDimitry Andric } // end namespace MachO.
102fe6060f1SDimitry Andric } // end namespace llvm.
103