xref: /src/contrib/llvm-project/llvm/lib/TextAPI/RecordVisitor.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
199aabd70SDimitry Andric //===- RecordVisitor.cpp --------------------------------------------------===//
299aabd70SDimitry Andric //
399aabd70SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
499aabd70SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
599aabd70SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
699aabd70SDimitry Andric //
799aabd70SDimitry Andric //===----------------------------------------------------------------------===//
899aabd70SDimitry Andric ///
999aabd70SDimitry Andric /// Implements the TAPI Record Visitor.
1099aabd70SDimitry Andric ///
1199aabd70SDimitry Andric //===----------------------------------------------------------------------===//
1299aabd70SDimitry Andric 
1399aabd70SDimitry Andric #include "llvm/TextAPI/RecordVisitor.h"
1499aabd70SDimitry Andric 
1599aabd70SDimitry Andric using namespace llvm;
1699aabd70SDimitry Andric using namespace llvm::MachO;
1799aabd70SDimitry Andric 
~RecordVisitor()1899aabd70SDimitry Andric RecordVisitor::~RecordVisitor() {}
visitObjCInterface(const ObjCInterfaceRecord &)1999aabd70SDimitry Andric void RecordVisitor::visitObjCInterface(const ObjCInterfaceRecord &) {}
visitObjCCategory(const ObjCCategoryRecord &)2099aabd70SDimitry Andric void RecordVisitor::visitObjCCategory(const ObjCCategoryRecord &) {}
2199aabd70SDimitry Andric 
shouldSkipRecord(const Record & R,const bool RecordUndefs)2299aabd70SDimitry Andric static bool shouldSkipRecord(const Record &R, const bool RecordUndefs) {
2399aabd70SDimitry Andric   if (R.isExported())
2499aabd70SDimitry Andric     return false;
2599aabd70SDimitry Andric 
2699aabd70SDimitry Andric   // Skip non exported symbols unless for flat namespace libraries.
2799aabd70SDimitry Andric   return !(RecordUndefs && R.isUndefined());
2899aabd70SDimitry Andric }
2999aabd70SDimitry Andric 
visitGlobal(const GlobalRecord & GR)3099aabd70SDimitry Andric void SymbolConverter::visitGlobal(const GlobalRecord &GR) {
31ac9a064cSDimitry Andric   auto [SymName, SymKind, InterfaceType] = parseSymbol(GR.getName());
3299aabd70SDimitry Andric   if (shouldSkipRecord(GR, RecordUndefs))
3399aabd70SDimitry Andric     return;
3499aabd70SDimitry Andric   Symbols->addGlobal(SymKind, SymName, GR.getFlags(), Targ);
35ac9a064cSDimitry Andric 
36ac9a064cSDimitry Andric   if (InterfaceType == ObjCIFSymbolKind::None) {
37ac9a064cSDimitry Andric     Symbols->addGlobal(SymKind, SymName, GR.getFlags(), Targ);
38ac9a064cSDimitry Andric     return;
39ac9a064cSDimitry Andric   }
40ac9a064cSDimitry Andric 
41ac9a064cSDimitry Andric   // It is impossible to hold a complete ObjCInterface with a single
42ac9a064cSDimitry Andric   // GlobalRecord, so continue to treat this symbol a generic global.
43ac9a064cSDimitry Andric   Symbols->addGlobal(EncodeKind::GlobalSymbol, GR.getName(), GR.getFlags(),
44ac9a064cSDimitry Andric                      Targ);
4599aabd70SDimitry Andric }
4699aabd70SDimitry Andric 
addIVars(const ArrayRef<ObjCIVarRecord * > IVars,StringRef ContainerName)4799aabd70SDimitry Andric void SymbolConverter::addIVars(const ArrayRef<ObjCIVarRecord *> IVars,
4899aabd70SDimitry Andric                                StringRef ContainerName) {
4999aabd70SDimitry Andric   for (auto *IV : IVars) {
5099aabd70SDimitry Andric     if (shouldSkipRecord(*IV, RecordUndefs))
5199aabd70SDimitry Andric       continue;
5299aabd70SDimitry Andric     std::string Name =
5399aabd70SDimitry Andric         ObjCIVarRecord::createScopedName(ContainerName, IV->getName());
54ac9a064cSDimitry Andric     Symbols->addGlobal(EncodeKind::ObjectiveCInstanceVariable, Name,
5599aabd70SDimitry Andric                        IV->getFlags(), Targ);
5699aabd70SDimitry Andric   }
5799aabd70SDimitry Andric }
5899aabd70SDimitry Andric 
visitObjCInterface(const ObjCInterfaceRecord & ObjCR)5999aabd70SDimitry Andric void SymbolConverter::visitObjCInterface(const ObjCInterfaceRecord &ObjCR) {
6099aabd70SDimitry Andric   if (!shouldSkipRecord(ObjCR, RecordUndefs)) {
61ac9a064cSDimitry Andric     if (ObjCR.isCompleteInterface()) {
62ac9a064cSDimitry Andric       Symbols->addGlobal(EncodeKind::ObjectiveCClass, ObjCR.getName(),
6399aabd70SDimitry Andric                          ObjCR.getFlags(), Targ);
6499aabd70SDimitry Andric       if (ObjCR.hasExceptionAttribute())
65ac9a064cSDimitry Andric         Symbols->addGlobal(EncodeKind::ObjectiveCClassEHType, ObjCR.getName(),
6699aabd70SDimitry Andric                            ObjCR.getFlags(), Targ);
67ac9a064cSDimitry Andric     } else {
68ac9a064cSDimitry Andric       // Because there is not a complete interface, visit individual symbols
69ac9a064cSDimitry Andric       // instead.
70ac9a064cSDimitry Andric       if (ObjCR.isExportedSymbol(ObjCIFSymbolKind::EHType))
71ac9a064cSDimitry Andric         Symbols->addGlobal(EncodeKind::GlobalSymbol,
72ac9a064cSDimitry Andric                            (ObjC2EHTypePrefix + ObjCR.getName()).str(),
73ac9a064cSDimitry Andric                            ObjCR.getFlags(), Targ);
74ac9a064cSDimitry Andric       if (ObjCR.isExportedSymbol(ObjCIFSymbolKind::Class))
75ac9a064cSDimitry Andric         Symbols->addGlobal(EncodeKind::GlobalSymbol,
76ac9a064cSDimitry Andric                            (ObjC2ClassNamePrefix + ObjCR.getName()).str(),
77ac9a064cSDimitry Andric                            ObjCR.getFlags(), Targ);
78ac9a064cSDimitry Andric       if (ObjCR.isExportedSymbol(ObjCIFSymbolKind::MetaClass))
79ac9a064cSDimitry Andric         Symbols->addGlobal(EncodeKind::GlobalSymbol,
80ac9a064cSDimitry Andric                            (ObjC2MetaClassNamePrefix + ObjCR.getName()).str(),
81ac9a064cSDimitry Andric                            ObjCR.getFlags(), Targ);
82ac9a064cSDimitry Andric     }
8399aabd70SDimitry Andric   }
8499aabd70SDimitry Andric 
8599aabd70SDimitry Andric   addIVars(ObjCR.getObjCIVars(), ObjCR.getName());
8699aabd70SDimitry Andric   for (const auto *Cat : ObjCR.getObjCCategories())
8799aabd70SDimitry Andric     addIVars(Cat->getObjCIVars(), ObjCR.getName());
8899aabd70SDimitry Andric }
8999aabd70SDimitry Andric 
visitObjCCategory(const ObjCCategoryRecord & Cat)9099aabd70SDimitry Andric void SymbolConverter::visitObjCCategory(const ObjCCategoryRecord &Cat) {
91ac9a064cSDimitry Andric   addIVars(Cat.getObjCIVars(), Cat.getSuperClassName());
9299aabd70SDimitry Andric }
93