xref: /freebsd/contrib/llvm-project/llvm/lib/TextAPI/Symbol.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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)
dump(raw_ostream & OS) const20fe6060f1SDimitry 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) {
31*0fca6ea1SDimitry Andric   case EncodeKind::GlobalSymbol:
32fe6060f1SDimitry Andric     Result += Name.str();
33fe6060f1SDimitry Andric     break;
34*0fca6ea1SDimitry Andric   case EncodeKind::ObjectiveCClass:
35fe6060f1SDimitry Andric     Result += "(ObjC Class) " + Name.str();
36fe6060f1SDimitry Andric     break;
37*0fca6ea1SDimitry Andric   case EncodeKind::ObjectiveCClassEHType:
38fe6060f1SDimitry Andric     Result += "(ObjC Class EH) " + Name.str();
39fe6060f1SDimitry Andric     break;
40*0fca6ea1SDimitry Andric   case EncodeKind::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
targets(ArchitectureSet Architectures) const49fe6060f1SDimitry 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 
operator ==(const Symbol & O) const5706c3fb27SDimitry 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 
parseSymbol(StringRef SymName)75*0fca6ea1SDimitry Andric SimpleSymbol parseSymbol(StringRef SymName) {
765f757f3fSDimitry Andric   if (SymName.starts_with(ObjC1ClassNamePrefix))
775f757f3fSDimitry Andric     return {SymName.drop_front(ObjC1ClassNamePrefix.size()),
78*0fca6ea1SDimitry Andric             EncodeKind::ObjectiveCClass, ObjCIFSymbolKind::Class};
795f757f3fSDimitry Andric   if (SymName.starts_with(ObjC2ClassNamePrefix))
805f757f3fSDimitry Andric     return {SymName.drop_front(ObjC2ClassNamePrefix.size()),
81*0fca6ea1SDimitry Andric             EncodeKind::ObjectiveCClass, ObjCIFSymbolKind::Class};
825f757f3fSDimitry Andric   if (SymName.starts_with(ObjC2MetaClassNamePrefix))
835f757f3fSDimitry Andric     return {SymName.drop_front(ObjC2MetaClassNamePrefix.size()),
84*0fca6ea1SDimitry Andric             EncodeKind::ObjectiveCClass, ObjCIFSymbolKind::MetaClass};
85*0fca6ea1SDimitry Andric   if (SymName.starts_with(ObjC2EHTypePrefix))
865f757f3fSDimitry Andric     return {SymName.drop_front(ObjC2EHTypePrefix.size()),
87*0fca6ea1SDimitry Andric             EncodeKind::ObjectiveCClassEHType, ObjCIFSymbolKind::EHType};
885f757f3fSDimitry Andric   if (SymName.starts_with(ObjC2IVarPrefix))
895f757f3fSDimitry Andric     return {SymName.drop_front(ObjC2IVarPrefix.size()),
90*0fca6ea1SDimitry Andric             EncodeKind::ObjectiveCInstanceVariable, ObjCIFSymbolKind::None};
91*0fca6ea1SDimitry Andric   return {SymName, EncodeKind::GlobalSymbol, ObjCIFSymbolKind::None};
925f757f3fSDimitry Andric }
935f757f3fSDimitry Andric 
94fe6060f1SDimitry Andric } // end namespace MachO.
95fe6060f1SDimitry Andric } // end namespace llvm.
96