xref: /llvm-project/clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp (revision e84fae837c0b154153bd9b9a3255ec5a67b1ea61)
168f29dacSPuyan Lotfi //===--- InterfaceStubFunctionsConsumer.cpp -------------------------------===//
268f29dacSPuyan Lotfi //
368f29dacSPuyan Lotfi // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468f29dacSPuyan Lotfi // See https://llvm.org/LICENSE.txt for license information.
568f29dacSPuyan Lotfi // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668f29dacSPuyan Lotfi //
768f29dacSPuyan Lotfi //===----------------------------------------------------------------------===//
868f29dacSPuyan Lotfi 
968f29dacSPuyan Lotfi #include "clang/AST/Mangle.h"
1068f29dacSPuyan Lotfi #include "clang/AST/RecursiveASTVisitor.h"
11d7c5037eSReid Kleckner #include "clang/Basic/TargetInfo.h"
1268f29dacSPuyan Lotfi #include "clang/Frontend/CompilerInstance.h"
1368f29dacSPuyan Lotfi #include "clang/Frontend/FrontendActions.h"
1468f29dacSPuyan Lotfi #include "clang/Sema/TemplateInstCallback.h"
1568f29dacSPuyan Lotfi #include "llvm/BinaryFormat/ELF.h"
1668f29dacSPuyan Lotfi 
1768f29dacSPuyan Lotfi using namespace clang;
1868f29dacSPuyan Lotfi 
19dc5f805dSBenjamin Kramer namespace {
2068f29dacSPuyan Lotfi class InterfaceStubFunctionsConsumer : public ASTConsumer {
2168f29dacSPuyan Lotfi   CompilerInstance &Instance;
2268f29dacSPuyan Lotfi   StringRef InFile;
2368f29dacSPuyan Lotfi   StringRef Format;
2468f29dacSPuyan Lotfi   std::set<std::string> ParsedTemplates;
2568f29dacSPuyan Lotfi 
2668f29dacSPuyan Lotfi   enum RootDeclOrigin { TopLevel = 0, FromTU = 1, IsLate = 2 };
2768f29dacSPuyan Lotfi   struct MangledSymbol {
2868f29dacSPuyan Lotfi     std::string ParentName;
2968f29dacSPuyan Lotfi     uint8_t Type;
3068f29dacSPuyan Lotfi     uint8_t Binding;
3168f29dacSPuyan Lotfi     std::vector<std::string> Names;
3268f29dacSPuyan Lotfi     MangledSymbol() = delete;
3368f29dacSPuyan Lotfi 
MangledSymbol__anon3b6cdc100111::InterfaceStubFunctionsConsumer::MangledSymbol3468f29dacSPuyan Lotfi     MangledSymbol(const std::string &ParentName, uint8_t Type, uint8_t Binding,
3568f29dacSPuyan Lotfi                   std::vector<std::string> Names)
36*e84fae83SAndrew Sukach         : ParentName(ParentName), Type(Type), Binding(Binding),
37*e84fae83SAndrew Sukach           Names(std::move(Names)) {}
3868f29dacSPuyan Lotfi   };
3968f29dacSPuyan Lotfi   using MangledSymbols = std::map<const NamedDecl *, MangledSymbol>;
4068f29dacSPuyan Lotfi 
WriteNamedDecl(const NamedDecl * ND,MangledSymbols & Symbols,int RDO)4168f29dacSPuyan Lotfi   bool WriteNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) {
4268f29dacSPuyan Lotfi     // Here we filter out anything that's not set to DefaultVisibility.
4368f29dacSPuyan Lotfi     // DefaultVisibility is set on a decl when -fvisibility is not specified on
4468f29dacSPuyan Lotfi     // the command line (or specified as default) and the decl does not have
4568f29dacSPuyan Lotfi     // __attribute__((visibility("hidden"))) set or when the command line
4668f29dacSPuyan Lotfi     // argument is set to hidden but the decl explicitly has
4768f29dacSPuyan Lotfi     // __attribute__((visibility ("default"))) set. We do this so that the user
4868f29dacSPuyan Lotfi     // can have fine grain control of what they want to expose in the stub.
4968f29dacSPuyan Lotfi     auto isVisible = [](const NamedDecl *ND) -> bool {
5068f29dacSPuyan Lotfi       return ND->getVisibility() == DefaultVisibility;
5168f29dacSPuyan Lotfi     };
5268f29dacSPuyan Lotfi 
5368f29dacSPuyan Lotfi     auto ignoreDecl = [this, isVisible](const NamedDecl *ND) -> bool {
5468f29dacSPuyan Lotfi       if (!isVisible(ND))
5568f29dacSPuyan Lotfi         return true;
5668f29dacSPuyan Lotfi 
57b7526cc2SPuyan Lotfi       if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
58bd8c8827SPuyan Lotfi         if (const auto *Parent = VD->getParentFunctionOrMethod())
59bd8c8827SPuyan Lotfi           if (isa<BlockDecl>(Parent) || isa<CXXMethodDecl>(Parent))
60bd8c8827SPuyan Lotfi             return true;
61bd8c8827SPuyan Lotfi 
622fd11e0bSThorsten Schütt         if ((VD->getStorageClass() == StorageClass::SC_Extern) ||
632fd11e0bSThorsten Schütt             (VD->getStorageClass() == StorageClass::SC_Static &&
6468f29dacSPuyan Lotfi              VD->getParentFunctionOrMethod() == nullptr))
6568f29dacSPuyan Lotfi           return true;
66b7526cc2SPuyan Lotfi       }
6768f29dacSPuyan Lotfi 
6868f29dacSPuyan Lotfi       if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
6968f29dacSPuyan Lotfi         if (FD->isInlined() && !isa<CXXMethodDecl>(FD) &&
7068f29dacSPuyan Lotfi             !Instance.getLangOpts().GNUInline)
7168f29dacSPuyan Lotfi           return true;
7268f29dacSPuyan Lotfi         if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
7368f29dacSPuyan Lotfi           if (const auto *RC = dyn_cast<CXXRecordDecl>(MD->getParent()))
7468f29dacSPuyan Lotfi             if (isa<ClassTemplateDecl>(RC->getParent()) || !isVisible(RC))
7568f29dacSPuyan Lotfi               return true;
7668f29dacSPuyan Lotfi           if (MD->isDependentContext() || !MD->hasBody())
7768f29dacSPuyan Lotfi             return true;
7868f29dacSPuyan Lotfi         }
792fd11e0bSThorsten Schütt         if (FD->getStorageClass() == StorageClass::SC_Static)
8068f29dacSPuyan Lotfi           return true;
8168f29dacSPuyan Lotfi       }
8268f29dacSPuyan Lotfi       return false;
8368f29dacSPuyan Lotfi     };
8468f29dacSPuyan Lotfi 
8568f29dacSPuyan Lotfi     auto getParentFunctionDecl = [](const NamedDecl *ND) -> const NamedDecl * {
8668f29dacSPuyan Lotfi       if (const VarDecl *VD = dyn_cast<VarDecl>(ND))
8768f29dacSPuyan Lotfi         if (const auto *FD =
8868f29dacSPuyan Lotfi                 dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod()))
8968f29dacSPuyan Lotfi           return FD;
9068f29dacSPuyan Lotfi       return nullptr;
9168f29dacSPuyan Lotfi     };
9268f29dacSPuyan Lotfi 
9368f29dacSPuyan Lotfi     auto getMangledNames = [](const NamedDecl *ND) -> std::vector<std::string> {
9468f29dacSPuyan Lotfi       if (!ND)
9568f29dacSPuyan Lotfi         return {""};
9668f29dacSPuyan Lotfi       ASTNameGenerator NameGen(ND->getASTContext());
9768f29dacSPuyan Lotfi       std::vector<std::string> MangledNames = NameGen.getAllManglings(ND);
9868f29dacSPuyan Lotfi       if (isa<CXXConstructorDecl>(ND) || isa<CXXDestructorDecl>(ND))
9968f29dacSPuyan Lotfi         return MangledNames;
10068f29dacSPuyan Lotfi #ifdef EXPENSIVE_CHECKS
10168f29dacSPuyan Lotfi       assert(MangledNames.size() <= 1 && "Expected only one name mangling.");
10268f29dacSPuyan Lotfi #endif
10368f29dacSPuyan Lotfi       return {NameGen.getName(ND)};
10468f29dacSPuyan Lotfi     };
10568f29dacSPuyan Lotfi 
10668f29dacSPuyan Lotfi     if (!(RDO & FromTU))
10768f29dacSPuyan Lotfi       return true;
10868f29dacSPuyan Lotfi     if (Symbols.find(ND) != Symbols.end())
10968f29dacSPuyan Lotfi       return true;
11068f29dacSPuyan Lotfi     // - Currently have not figured out how to produce the names for FieldDecls.
11168f29dacSPuyan Lotfi     // - Do not want to produce symbols for function paremeters.
11268f29dacSPuyan Lotfi     if (isa<FieldDecl>(ND) || isa<ParmVarDecl>(ND))
11368f29dacSPuyan Lotfi       return true;
11468f29dacSPuyan Lotfi 
11568f29dacSPuyan Lotfi     const NamedDecl *ParentDecl = getParentFunctionDecl(ND);
11668f29dacSPuyan Lotfi     if ((ParentDecl && ignoreDecl(ParentDecl)) || ignoreDecl(ND))
11768f29dacSPuyan Lotfi       return true;
11868f29dacSPuyan Lotfi 
11968f29dacSPuyan Lotfi     if (RDO & IsLate) {
12068f29dacSPuyan Lotfi       Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
12168f29dacSPuyan Lotfi           << "Generating Interface Stubs is not supported with "
12268f29dacSPuyan Lotfi              "delayed template parsing.";
12368f29dacSPuyan Lotfi     } else {
12468f29dacSPuyan Lotfi       if (const auto *FD = dyn_cast<FunctionDecl>(ND))
12568f29dacSPuyan Lotfi         if (FD->isDependentContext())
12668f29dacSPuyan Lotfi           return true;
12768f29dacSPuyan Lotfi 
12868f29dacSPuyan Lotfi       const bool IsWeak = (ND->hasAttr<WeakAttr>() ||
12968f29dacSPuyan Lotfi                            ND->hasAttr<WeakRefAttr>() || ND->isWeakImported());
13068f29dacSPuyan Lotfi 
13168f29dacSPuyan Lotfi       Symbols.insert(std::make_pair(
13268f29dacSPuyan Lotfi           ND,
13368f29dacSPuyan Lotfi           MangledSymbol(getMangledNames(ParentDecl).front(),
13468f29dacSPuyan Lotfi                         // Type:
13568f29dacSPuyan Lotfi                         isa<VarDecl>(ND) ? llvm::ELF::STT_OBJECT
13668f29dacSPuyan Lotfi                                          : llvm::ELF::STT_FUNC,
13768f29dacSPuyan Lotfi                         // Binding:
13868f29dacSPuyan Lotfi                         IsWeak ? llvm::ELF::STB_WEAK : llvm::ELF::STB_GLOBAL,
13968f29dacSPuyan Lotfi                         getMangledNames(ND))));
14068f29dacSPuyan Lotfi     }
14168f29dacSPuyan Lotfi     return true;
14268f29dacSPuyan Lotfi   }
14368f29dacSPuyan Lotfi 
14468f29dacSPuyan Lotfi   void
HandleDecls(const llvm::iterator_range<DeclContext::decl_iterator> & Decls,MangledSymbols & Symbols,int RDO)14568f29dacSPuyan Lotfi   HandleDecls(const llvm::iterator_range<DeclContext::decl_iterator> &Decls,
14668f29dacSPuyan Lotfi               MangledSymbols &Symbols, int RDO) {
14768f29dacSPuyan Lotfi     for (const auto *D : Decls)
14868f29dacSPuyan Lotfi       HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
14968f29dacSPuyan Lotfi   }
15068f29dacSPuyan Lotfi 
HandleTemplateSpecializations(const FunctionTemplateDecl & FTD,MangledSymbols & Symbols,int RDO)15168f29dacSPuyan Lotfi   void HandleTemplateSpecializations(const FunctionTemplateDecl &FTD,
15268f29dacSPuyan Lotfi                                      MangledSymbols &Symbols, int RDO) {
15368f29dacSPuyan Lotfi     for (const auto *D : FTD.specializations())
15468f29dacSPuyan Lotfi       HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
15568f29dacSPuyan Lotfi   }
15668f29dacSPuyan Lotfi 
HandleTemplateSpecializations(const ClassTemplateDecl & CTD,MangledSymbols & Symbols,int RDO)15768f29dacSPuyan Lotfi   void HandleTemplateSpecializations(const ClassTemplateDecl &CTD,
15868f29dacSPuyan Lotfi                                      MangledSymbols &Symbols, int RDO) {
15968f29dacSPuyan Lotfi     for (const auto *D : CTD.specializations())
16068f29dacSPuyan Lotfi       HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
16168f29dacSPuyan Lotfi   }
16268f29dacSPuyan Lotfi 
HandleNamedDecl(const NamedDecl * ND,MangledSymbols & Symbols,int RDO)16368f29dacSPuyan Lotfi   bool HandleNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) {
16468f29dacSPuyan Lotfi     if (!ND)
16568f29dacSPuyan Lotfi       return false;
16668f29dacSPuyan Lotfi 
16768f29dacSPuyan Lotfi     switch (ND->getKind()) {
16868f29dacSPuyan Lotfi     default:
16968f29dacSPuyan Lotfi       break;
17068f29dacSPuyan Lotfi     case Decl::Kind::Namespace:
17168f29dacSPuyan Lotfi       HandleDecls(cast<NamespaceDecl>(ND)->decls(), Symbols, RDO);
17268f29dacSPuyan Lotfi       return true;
17368f29dacSPuyan Lotfi     case Decl::Kind::CXXRecord:
17468f29dacSPuyan Lotfi       HandleDecls(cast<CXXRecordDecl>(ND)->decls(), Symbols, RDO);
17568f29dacSPuyan Lotfi       return true;
17668f29dacSPuyan Lotfi     case Decl::Kind::ClassTemplateSpecialization:
17768f29dacSPuyan Lotfi       HandleDecls(cast<ClassTemplateSpecializationDecl>(ND)->decls(), Symbols,
17868f29dacSPuyan Lotfi                   RDO);
17968f29dacSPuyan Lotfi       return true;
18068f29dacSPuyan Lotfi     case Decl::Kind::ClassTemplate:
18168f29dacSPuyan Lotfi       HandleTemplateSpecializations(*cast<ClassTemplateDecl>(ND), Symbols, RDO);
18268f29dacSPuyan Lotfi       return true;
18368f29dacSPuyan Lotfi     case Decl::Kind::FunctionTemplate:
18468f29dacSPuyan Lotfi       HandleTemplateSpecializations(*cast<FunctionTemplateDecl>(ND), Symbols,
18568f29dacSPuyan Lotfi                                     RDO);
18668f29dacSPuyan Lotfi       return true;
187e3388c42SPuyan Lotfi     case Decl::Kind::Record:
188e3388c42SPuyan Lotfi     case Decl::Kind::Typedef:
189e3388c42SPuyan Lotfi     case Decl::Kind::Enum:
190e3388c42SPuyan Lotfi     case Decl::Kind::EnumConstant:
19168f29dacSPuyan Lotfi     case Decl::Kind::TemplateTypeParm:
19279e345fbSPuyan Lotfi     case Decl::Kind::NonTypeTemplateParm:
19379e345fbSPuyan Lotfi     case Decl::Kind::CXXConversion:
19479e345fbSPuyan Lotfi     case Decl::Kind::UnresolvedUsingValue:
19579e345fbSPuyan Lotfi     case Decl::Kind::Using:
19679e345fbSPuyan Lotfi     case Decl::Kind::UsingShadow:
19779e345fbSPuyan Lotfi     case Decl::Kind::TypeAliasTemplate:
19879e345fbSPuyan Lotfi     case Decl::Kind::TypeAlias:
19979e345fbSPuyan Lotfi     case Decl::Kind::VarTemplate:
20079e345fbSPuyan Lotfi     case Decl::Kind::VarTemplateSpecialization:
20179e345fbSPuyan Lotfi     case Decl::Kind::UsingDirective:
20279e345fbSPuyan Lotfi     case Decl::Kind::TemplateTemplateParm:
20379e345fbSPuyan Lotfi     case Decl::Kind::ClassTemplatePartialSpecialization:
20479e345fbSPuyan Lotfi     case Decl::Kind::IndirectField:
20507e44510SPuyan Lotfi     case Decl::Kind::ConstructorUsingShadow:
20607e44510SPuyan Lotfi     case Decl::Kind::CXXDeductionGuide:
20707e44510SPuyan Lotfi     case Decl::Kind::NamespaceAlias:
20807e44510SPuyan Lotfi     case Decl::Kind::UnresolvedUsingTypename:
20968f29dacSPuyan Lotfi       return true;
21079e345fbSPuyan Lotfi     case Decl::Kind::Var: {
21179e345fbSPuyan Lotfi       // Bail on any VarDecl that either has no named symbol.
21279e345fbSPuyan Lotfi       if (!ND->getIdentifier())
21379e345fbSPuyan Lotfi         return true;
21479e345fbSPuyan Lotfi       const auto *VD = cast<VarDecl>(ND);
21579e345fbSPuyan Lotfi       // Bail on any VarDecl that is a dependent or templated type.
21679e345fbSPuyan Lotfi       if (VD->isTemplated() || VD->getType()->isDependentType())
21779e345fbSPuyan Lotfi         return true;
21879e345fbSPuyan Lotfi       if (WriteNamedDecl(ND, Symbols, RDO))
21979e345fbSPuyan Lotfi         return true;
22079e345fbSPuyan Lotfi       break;
22179e345fbSPuyan Lotfi     }
22268f29dacSPuyan Lotfi     case Decl::Kind::ParmVar:
22368f29dacSPuyan Lotfi     case Decl::Kind::CXXMethod:
22468f29dacSPuyan Lotfi     case Decl::Kind::CXXConstructor:
22568f29dacSPuyan Lotfi     case Decl::Kind::CXXDestructor:
22668f29dacSPuyan Lotfi     case Decl::Kind::Function:
22768f29dacSPuyan Lotfi     case Decl::Kind::Field:
22868f29dacSPuyan Lotfi       if (WriteNamedDecl(ND, Symbols, RDO))
22968f29dacSPuyan Lotfi         return true;
23068f29dacSPuyan Lotfi     }
23168f29dacSPuyan Lotfi 
23268f29dacSPuyan Lotfi     // While interface stubs are in the development stage, it's probably best to
23368f29dacSPuyan Lotfi     // catch anything that's not a VarDecl or Template/FunctionDecl.
23468f29dacSPuyan Lotfi     Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
23568f29dacSPuyan Lotfi         << "Expected a function or function template decl.";
23668f29dacSPuyan Lotfi     return false;
23768f29dacSPuyan Lotfi   }
23868f29dacSPuyan Lotfi 
23968f29dacSPuyan Lotfi public:
InterfaceStubFunctionsConsumer(CompilerInstance & Instance,StringRef InFile,StringRef Format)24068f29dacSPuyan Lotfi   InterfaceStubFunctionsConsumer(CompilerInstance &Instance, StringRef InFile,
24168f29dacSPuyan Lotfi                                  StringRef Format)
24268f29dacSPuyan Lotfi       : Instance(Instance), InFile(InFile), Format(Format) {}
24368f29dacSPuyan Lotfi 
HandleTranslationUnit(ASTContext & context)24468f29dacSPuyan Lotfi   void HandleTranslationUnit(ASTContext &context) override {
24568f29dacSPuyan Lotfi     struct Visitor : public RecursiveASTVisitor<Visitor> {
24668f29dacSPuyan Lotfi       bool VisitNamedDecl(NamedDecl *ND) {
24768f29dacSPuyan Lotfi         if (const auto *FD = dyn_cast<FunctionDecl>(ND))
24868f29dacSPuyan Lotfi           if (FD->isLateTemplateParsed()) {
24968f29dacSPuyan Lotfi             LateParsedDecls.insert(FD);
25068f29dacSPuyan Lotfi             return true;
25168f29dacSPuyan Lotfi           }
25268f29dacSPuyan Lotfi 
25368f29dacSPuyan Lotfi         if (const auto *VD = dyn_cast<ValueDecl>(ND)) {
25468f29dacSPuyan Lotfi           ValueDecls.insert(VD);
25568f29dacSPuyan Lotfi           return true;
25668f29dacSPuyan Lotfi         }
25768f29dacSPuyan Lotfi 
25868f29dacSPuyan Lotfi         NamedDecls.insert(ND);
25968f29dacSPuyan Lotfi         return true;
26068f29dacSPuyan Lotfi       }
26168f29dacSPuyan Lotfi 
26268f29dacSPuyan Lotfi       std::set<const NamedDecl *> LateParsedDecls;
26368f29dacSPuyan Lotfi       std::set<NamedDecl *> NamedDecls;
26468f29dacSPuyan Lotfi       std::set<const ValueDecl *> ValueDecls;
26568f29dacSPuyan Lotfi     } v;
26668f29dacSPuyan Lotfi 
26768f29dacSPuyan Lotfi     v.TraverseDecl(context.getTranslationUnitDecl());
26868f29dacSPuyan Lotfi 
26968f29dacSPuyan Lotfi     MangledSymbols Symbols;
27068f29dacSPuyan Lotfi     auto OS = Instance.createDefaultOutputFile(/*Binary=*/false, InFile, "ifs");
27168f29dacSPuyan Lotfi     if (!OS)
27268f29dacSPuyan Lotfi       return;
27368f29dacSPuyan Lotfi 
27468f29dacSPuyan Lotfi     if (Instance.getLangOpts().DelayedTemplateParsing) {
27568f29dacSPuyan Lotfi       clang::Sema &S = Instance.getSema();
27668f29dacSPuyan Lotfi       for (const auto *FD : v.LateParsedDecls) {
27768f29dacSPuyan Lotfi         clang::LateParsedTemplate &LPT =
27868f29dacSPuyan Lotfi             *S.LateParsedTemplateMap.find(cast<FunctionDecl>(FD))->second;
27968f29dacSPuyan Lotfi         S.LateTemplateParser(S.OpaqueParser, LPT);
28068f29dacSPuyan Lotfi         HandleNamedDecl(FD, Symbols, (FromTU | IsLate));
28168f29dacSPuyan Lotfi       }
28268f29dacSPuyan Lotfi     }
28368f29dacSPuyan Lotfi 
28468f29dacSPuyan Lotfi     for (const NamedDecl *ND : v.ValueDecls)
28568f29dacSPuyan Lotfi       HandleNamedDecl(ND, Symbols, FromTU);
28668f29dacSPuyan Lotfi     for (const NamedDecl *ND : v.NamedDecls)
28768f29dacSPuyan Lotfi       HandleNamedDecl(ND, Symbols, FromTU);
28868f29dacSPuyan Lotfi 
28979e345fbSPuyan Lotfi     auto writeIfsV1 = [this](const llvm::Triple &T,
29079e345fbSPuyan Lotfi                              const MangledSymbols &Symbols,
291d2418459SPuyan Lotfi                              const ASTContext &context, StringRef Format,
292d2418459SPuyan Lotfi                              raw_ostream &OS) -> void {
293d2418459SPuyan Lotfi       OS << "--- !" << Format << "\n";
29461fa9afeSHaowei Wu       OS << "IfsVersion: 3.0\n";
29561fa9afeSHaowei Wu       OS << "Target: " << T.str() << "\n";
296d2418459SPuyan Lotfi       OS << "Symbols:\n";
297d2418459SPuyan Lotfi       for (const auto &E : Symbols) {
298d2418459SPuyan Lotfi         const MangledSymbol &Symbol = E.second;
299d751e407SDanny Mösch         for (const auto &Name : Symbol.Names) {
300e3033c0cSPuyan Lotfi           OS << "  - { Name: \""
301d2418459SPuyan Lotfi              << (Symbol.ParentName.empty() || Instance.getLangOpts().CPlusPlus
302d2418459SPuyan Lotfi                      ? ""
303d2418459SPuyan Lotfi                      : (Symbol.ParentName + "."))
304e3033c0cSPuyan Lotfi              << Name << "\", Type: ";
305d2418459SPuyan Lotfi           switch (Symbol.Type) {
306d2418459SPuyan Lotfi           default:
307d2418459SPuyan Lotfi             llvm_unreachable(
30817bde36aSPuyan Lotfi                 "clang -emit-interface-stubs: Unexpected symbol type.");
309d2418459SPuyan Lotfi           case llvm::ELF::STT_NOTYPE:
310d2418459SPuyan Lotfi             OS << "NoType";
311d2418459SPuyan Lotfi             break;
312d2418459SPuyan Lotfi           case llvm::ELF::STT_OBJECT: {
313d2418459SPuyan Lotfi             auto VD = cast<ValueDecl>(E.first)->getType();
314d2418459SPuyan Lotfi             OS << "Object, Size: "
315d2418459SPuyan Lotfi                << context.getTypeSizeInChars(VD).getQuantity();
316d2418459SPuyan Lotfi             break;
317d2418459SPuyan Lotfi           }
318d2418459SPuyan Lotfi           case llvm::ELF::STT_FUNC:
319d2418459SPuyan Lotfi             OS << "Func";
320d2418459SPuyan Lotfi             break;
321d2418459SPuyan Lotfi           }
322d2418459SPuyan Lotfi           if (Symbol.Binding == llvm::ELF::STB_WEAK)
323d2418459SPuyan Lotfi             OS << ", Weak: true";
324d2418459SPuyan Lotfi           OS << " }\n";
325d2418459SPuyan Lotfi         }
326d2418459SPuyan Lotfi       }
327d2418459SPuyan Lotfi       OS << "...\n";
328d2418459SPuyan Lotfi       OS.flush();
329d2418459SPuyan Lotfi     };
330d2418459SPuyan Lotfi 
33161fa9afeSHaowei Wu     assert(Format == "ifs-v1" && "Unexpected IFS Format.");
332926f4f76SPuyan Lotfi     writeIfsV1(Instance.getTarget().getTriple(), Symbols, context, Format, *OS);
33368f29dacSPuyan Lotfi   }
33468f29dacSPuyan Lotfi };
335dc5f805dSBenjamin Kramer } // namespace
33668f29dacSPuyan Lotfi 
33768f29dacSPuyan Lotfi std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)338e3033c0cSPuyan Lotfi GenerateInterfaceStubsAction::CreateASTConsumer(CompilerInstance &CI,
339d2418459SPuyan Lotfi                                                 StringRef InFile) {
34061fa9afeSHaowei Wu   return std::make_unique<InterfaceStubFunctionsConsumer>(CI, InFile, "ifs-v1");
341d2418459SPuyan Lotfi }
342