xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===--- InterfaceStubFunctionsConsumer.cpp -------------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg 
97330f729Sjoerg #include "clang/AST/Mangle.h"
107330f729Sjoerg #include "clang/AST/RecursiveASTVisitor.h"
11*e038c9c4Sjoerg #include "clang/Basic/TargetInfo.h"
127330f729Sjoerg #include "clang/Frontend/CompilerInstance.h"
137330f729Sjoerg #include "clang/Frontend/FrontendActions.h"
147330f729Sjoerg #include "clang/Sema/TemplateInstCallback.h"
157330f729Sjoerg #include "llvm/BinaryFormat/ELF.h"
167330f729Sjoerg 
177330f729Sjoerg using namespace clang;
187330f729Sjoerg 
197330f729Sjoerg namespace {
207330f729Sjoerg class InterfaceStubFunctionsConsumer : public ASTConsumer {
217330f729Sjoerg   CompilerInstance &Instance;
227330f729Sjoerg   StringRef InFile;
237330f729Sjoerg   StringRef Format;
247330f729Sjoerg   std::set<std::string> ParsedTemplates;
257330f729Sjoerg 
267330f729Sjoerg   enum RootDeclOrigin { TopLevel = 0, FromTU = 1, IsLate = 2 };
277330f729Sjoerg   struct MangledSymbol {
287330f729Sjoerg     std::string ParentName;
297330f729Sjoerg     uint8_t Type;
307330f729Sjoerg     uint8_t Binding;
317330f729Sjoerg     std::vector<std::string> Names;
327330f729Sjoerg     MangledSymbol() = delete;
337330f729Sjoerg 
MangledSymbol__anon612d5e280111::InterfaceStubFunctionsConsumer::MangledSymbol347330f729Sjoerg     MangledSymbol(const std::string &ParentName, uint8_t Type, uint8_t Binding,
357330f729Sjoerg                   std::vector<std::string> Names)
367330f729Sjoerg         : ParentName(ParentName), Type(Type), Binding(Binding), Names(Names) {}
377330f729Sjoerg   };
387330f729Sjoerg   using MangledSymbols = std::map<const NamedDecl *, MangledSymbol>;
397330f729Sjoerg 
WriteNamedDecl(const NamedDecl * ND,MangledSymbols & Symbols,int RDO)407330f729Sjoerg   bool WriteNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) {
417330f729Sjoerg     // Here we filter out anything that's not set to DefaultVisibility.
427330f729Sjoerg     // DefaultVisibility is set on a decl when -fvisibility is not specified on
437330f729Sjoerg     // the command line (or specified as default) and the decl does not have
447330f729Sjoerg     // __attribute__((visibility("hidden"))) set or when the command line
457330f729Sjoerg     // argument is set to hidden but the decl explicitly has
467330f729Sjoerg     // __attribute__((visibility ("default"))) set. We do this so that the user
477330f729Sjoerg     // can have fine grain control of what they want to expose in the stub.
487330f729Sjoerg     auto isVisible = [](const NamedDecl *ND) -> bool {
497330f729Sjoerg       return ND->getVisibility() == DefaultVisibility;
507330f729Sjoerg     };
517330f729Sjoerg 
527330f729Sjoerg     auto ignoreDecl = [this, isVisible](const NamedDecl *ND) -> bool {
537330f729Sjoerg       if (!isVisible(ND))
547330f729Sjoerg         return true;
557330f729Sjoerg 
56*e038c9c4Sjoerg       if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
57*e038c9c4Sjoerg         if (const auto *Parent = VD->getParentFunctionOrMethod())
58*e038c9c4Sjoerg           if (isa<BlockDecl>(Parent) || isa<CXXMethodDecl>(Parent))
59*e038c9c4Sjoerg             return true;
60*e038c9c4Sjoerg 
617330f729Sjoerg         if ((VD->getStorageClass() == StorageClass::SC_Extern) ||
627330f729Sjoerg             (VD->getStorageClass() == StorageClass::SC_Static &&
637330f729Sjoerg              VD->getParentFunctionOrMethod() == nullptr))
647330f729Sjoerg           return true;
65*e038c9c4Sjoerg       }
667330f729Sjoerg 
677330f729Sjoerg       if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
687330f729Sjoerg         if (FD->isInlined() && !isa<CXXMethodDecl>(FD) &&
697330f729Sjoerg             !Instance.getLangOpts().GNUInline)
707330f729Sjoerg           return true;
717330f729Sjoerg         if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
727330f729Sjoerg           if (const auto *RC = dyn_cast<CXXRecordDecl>(MD->getParent()))
737330f729Sjoerg             if (isa<ClassTemplateDecl>(RC->getParent()) || !isVisible(RC))
747330f729Sjoerg               return true;
757330f729Sjoerg           if (MD->isDependentContext() || !MD->hasBody())
767330f729Sjoerg             return true;
777330f729Sjoerg         }
787330f729Sjoerg         if (FD->getStorageClass() == StorageClass::SC_Static)
797330f729Sjoerg           return true;
807330f729Sjoerg       }
817330f729Sjoerg       return false;
827330f729Sjoerg     };
837330f729Sjoerg 
847330f729Sjoerg     auto getParentFunctionDecl = [](const NamedDecl *ND) -> const NamedDecl * {
857330f729Sjoerg       if (const VarDecl *VD = dyn_cast<VarDecl>(ND))
867330f729Sjoerg         if (const auto *FD =
877330f729Sjoerg                 dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod()))
887330f729Sjoerg           return FD;
897330f729Sjoerg       return nullptr;
907330f729Sjoerg     };
917330f729Sjoerg 
927330f729Sjoerg     auto getMangledNames = [](const NamedDecl *ND) -> std::vector<std::string> {
937330f729Sjoerg       if (!ND)
947330f729Sjoerg         return {""};
957330f729Sjoerg       ASTNameGenerator NameGen(ND->getASTContext());
967330f729Sjoerg       std::vector<std::string> MangledNames = NameGen.getAllManglings(ND);
977330f729Sjoerg       if (isa<CXXConstructorDecl>(ND) || isa<CXXDestructorDecl>(ND))
987330f729Sjoerg         return MangledNames;
997330f729Sjoerg #ifdef EXPENSIVE_CHECKS
1007330f729Sjoerg       assert(MangledNames.size() <= 1 && "Expected only one name mangling.");
1017330f729Sjoerg #endif
1027330f729Sjoerg       return {NameGen.getName(ND)};
1037330f729Sjoerg     };
1047330f729Sjoerg 
1057330f729Sjoerg     if (!(RDO & FromTU))
1067330f729Sjoerg       return true;
1077330f729Sjoerg     if (Symbols.find(ND) != Symbols.end())
1087330f729Sjoerg       return true;
1097330f729Sjoerg     // - Currently have not figured out how to produce the names for FieldDecls.
1107330f729Sjoerg     // - Do not want to produce symbols for function paremeters.
1117330f729Sjoerg     if (isa<FieldDecl>(ND) || isa<ParmVarDecl>(ND))
1127330f729Sjoerg       return true;
1137330f729Sjoerg 
1147330f729Sjoerg     const NamedDecl *ParentDecl = getParentFunctionDecl(ND);
1157330f729Sjoerg     if ((ParentDecl && ignoreDecl(ParentDecl)) || ignoreDecl(ND))
1167330f729Sjoerg       return true;
1177330f729Sjoerg 
1187330f729Sjoerg     if (RDO & IsLate) {
1197330f729Sjoerg       Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
1207330f729Sjoerg           << "Generating Interface Stubs is not supported with "
1217330f729Sjoerg              "delayed template parsing.";
1227330f729Sjoerg     } else {
1237330f729Sjoerg       if (const auto *FD = dyn_cast<FunctionDecl>(ND))
1247330f729Sjoerg         if (FD->isDependentContext())
1257330f729Sjoerg           return true;
1267330f729Sjoerg 
1277330f729Sjoerg       const bool IsWeak = (ND->hasAttr<WeakAttr>() ||
1287330f729Sjoerg                            ND->hasAttr<WeakRefAttr>() || ND->isWeakImported());
1297330f729Sjoerg 
1307330f729Sjoerg       Symbols.insert(std::make_pair(
1317330f729Sjoerg           ND,
1327330f729Sjoerg           MangledSymbol(getMangledNames(ParentDecl).front(),
1337330f729Sjoerg                         // Type:
1347330f729Sjoerg                         isa<VarDecl>(ND) ? llvm::ELF::STT_OBJECT
1357330f729Sjoerg                                          : llvm::ELF::STT_FUNC,
1367330f729Sjoerg                         // Binding:
1377330f729Sjoerg                         IsWeak ? llvm::ELF::STB_WEAK : llvm::ELF::STB_GLOBAL,
1387330f729Sjoerg                         getMangledNames(ND))));
1397330f729Sjoerg     }
1407330f729Sjoerg     return true;
1417330f729Sjoerg   }
1427330f729Sjoerg 
1437330f729Sjoerg   void
HandleDecls(const llvm::iterator_range<DeclContext::decl_iterator> & Decls,MangledSymbols & Symbols,int RDO)1447330f729Sjoerg   HandleDecls(const llvm::iterator_range<DeclContext::decl_iterator> &Decls,
1457330f729Sjoerg               MangledSymbols &Symbols, int RDO) {
1467330f729Sjoerg     for (const auto *D : Decls)
1477330f729Sjoerg       HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
1487330f729Sjoerg   }
1497330f729Sjoerg 
HandleTemplateSpecializations(const FunctionTemplateDecl & FTD,MangledSymbols & Symbols,int RDO)1507330f729Sjoerg   void HandleTemplateSpecializations(const FunctionTemplateDecl &FTD,
1517330f729Sjoerg                                      MangledSymbols &Symbols, int RDO) {
1527330f729Sjoerg     for (const auto *D : FTD.specializations())
1537330f729Sjoerg       HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
1547330f729Sjoerg   }
1557330f729Sjoerg 
HandleTemplateSpecializations(const ClassTemplateDecl & CTD,MangledSymbols & Symbols,int RDO)1567330f729Sjoerg   void HandleTemplateSpecializations(const ClassTemplateDecl &CTD,
1577330f729Sjoerg                                      MangledSymbols &Symbols, int RDO) {
1587330f729Sjoerg     for (const auto *D : CTD.specializations())
1597330f729Sjoerg       HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
1607330f729Sjoerg   }
1617330f729Sjoerg 
HandleNamedDecl(const NamedDecl * ND,MangledSymbols & Symbols,int RDO)1627330f729Sjoerg   bool HandleNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) {
1637330f729Sjoerg     if (!ND)
1647330f729Sjoerg       return false;
1657330f729Sjoerg 
1667330f729Sjoerg     switch (ND->getKind()) {
1677330f729Sjoerg     default:
1687330f729Sjoerg       break;
1697330f729Sjoerg     case Decl::Kind::Namespace:
1707330f729Sjoerg       HandleDecls(cast<NamespaceDecl>(ND)->decls(), Symbols, RDO);
1717330f729Sjoerg       return true;
1727330f729Sjoerg     case Decl::Kind::CXXRecord:
1737330f729Sjoerg       HandleDecls(cast<CXXRecordDecl>(ND)->decls(), Symbols, RDO);
1747330f729Sjoerg       return true;
1757330f729Sjoerg     case Decl::Kind::ClassTemplateSpecialization:
1767330f729Sjoerg       HandleDecls(cast<ClassTemplateSpecializationDecl>(ND)->decls(), Symbols,
1777330f729Sjoerg                   RDO);
1787330f729Sjoerg       return true;
1797330f729Sjoerg     case Decl::Kind::ClassTemplate:
1807330f729Sjoerg       HandleTemplateSpecializations(*cast<ClassTemplateDecl>(ND), Symbols, RDO);
1817330f729Sjoerg       return true;
1827330f729Sjoerg     case Decl::Kind::FunctionTemplate:
1837330f729Sjoerg       HandleTemplateSpecializations(*cast<FunctionTemplateDecl>(ND), Symbols,
1847330f729Sjoerg                                     RDO);
1857330f729Sjoerg       return true;
1867330f729Sjoerg     case Decl::Kind::Record:
1877330f729Sjoerg     case Decl::Kind::Typedef:
1887330f729Sjoerg     case Decl::Kind::Enum:
1897330f729Sjoerg     case Decl::Kind::EnumConstant:
1907330f729Sjoerg     case Decl::Kind::TemplateTypeParm:
191*e038c9c4Sjoerg     case Decl::Kind::NonTypeTemplateParm:
192*e038c9c4Sjoerg     case Decl::Kind::CXXConversion:
193*e038c9c4Sjoerg     case Decl::Kind::UnresolvedUsingValue:
194*e038c9c4Sjoerg     case Decl::Kind::Using:
195*e038c9c4Sjoerg     case Decl::Kind::UsingShadow:
196*e038c9c4Sjoerg     case Decl::Kind::TypeAliasTemplate:
197*e038c9c4Sjoerg     case Decl::Kind::TypeAlias:
198*e038c9c4Sjoerg     case Decl::Kind::VarTemplate:
199*e038c9c4Sjoerg     case Decl::Kind::VarTemplateSpecialization:
200*e038c9c4Sjoerg     case Decl::Kind::UsingDirective:
201*e038c9c4Sjoerg     case Decl::Kind::TemplateTemplateParm:
202*e038c9c4Sjoerg     case Decl::Kind::ClassTemplatePartialSpecialization:
203*e038c9c4Sjoerg     case Decl::Kind::IndirectField:
204*e038c9c4Sjoerg     case Decl::Kind::ConstructorUsingShadow:
205*e038c9c4Sjoerg     case Decl::Kind::CXXDeductionGuide:
206*e038c9c4Sjoerg     case Decl::Kind::NamespaceAlias:
207*e038c9c4Sjoerg     case Decl::Kind::UnresolvedUsingTypename:
2087330f729Sjoerg       return true;
209*e038c9c4Sjoerg     case Decl::Kind::Var: {
210*e038c9c4Sjoerg       // Bail on any VarDecl that either has no named symbol.
211*e038c9c4Sjoerg       if (!ND->getIdentifier())
212*e038c9c4Sjoerg         return true;
213*e038c9c4Sjoerg       const auto *VD = cast<VarDecl>(ND);
214*e038c9c4Sjoerg       // Bail on any VarDecl that is a dependent or templated type.
215*e038c9c4Sjoerg       if (VD->isTemplated() || VD->getType()->isDependentType())
216*e038c9c4Sjoerg         return true;
217*e038c9c4Sjoerg       if (WriteNamedDecl(ND, Symbols, RDO))
218*e038c9c4Sjoerg         return true;
219*e038c9c4Sjoerg       break;
220*e038c9c4Sjoerg     }
2217330f729Sjoerg     case Decl::Kind::ParmVar:
2227330f729Sjoerg     case Decl::Kind::CXXMethod:
2237330f729Sjoerg     case Decl::Kind::CXXConstructor:
2247330f729Sjoerg     case Decl::Kind::CXXDestructor:
2257330f729Sjoerg     case Decl::Kind::Function:
2267330f729Sjoerg     case Decl::Kind::Field:
2277330f729Sjoerg       if (WriteNamedDecl(ND, Symbols, RDO))
2287330f729Sjoerg         return true;
2297330f729Sjoerg     }
2307330f729Sjoerg 
2317330f729Sjoerg     // While interface stubs are in the development stage, it's probably best to
2327330f729Sjoerg     // catch anything that's not a VarDecl or Template/FunctionDecl.
2337330f729Sjoerg     Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
2347330f729Sjoerg         << "Expected a function or function template decl.";
2357330f729Sjoerg     return false;
2367330f729Sjoerg   }
2377330f729Sjoerg 
2387330f729Sjoerg public:
InterfaceStubFunctionsConsumer(CompilerInstance & Instance,StringRef InFile,StringRef Format)2397330f729Sjoerg   InterfaceStubFunctionsConsumer(CompilerInstance &Instance, StringRef InFile,
2407330f729Sjoerg                                  StringRef Format)
2417330f729Sjoerg       : Instance(Instance), InFile(InFile), Format(Format) {}
2427330f729Sjoerg 
HandleTranslationUnit(ASTContext & context)2437330f729Sjoerg   void HandleTranslationUnit(ASTContext &context) override {
2447330f729Sjoerg     struct Visitor : public RecursiveASTVisitor<Visitor> {
2457330f729Sjoerg       bool VisitNamedDecl(NamedDecl *ND) {
2467330f729Sjoerg         if (const auto *FD = dyn_cast<FunctionDecl>(ND))
2477330f729Sjoerg           if (FD->isLateTemplateParsed()) {
2487330f729Sjoerg             LateParsedDecls.insert(FD);
2497330f729Sjoerg             return true;
2507330f729Sjoerg           }
2517330f729Sjoerg 
2527330f729Sjoerg         if (const auto *VD = dyn_cast<ValueDecl>(ND)) {
2537330f729Sjoerg           ValueDecls.insert(VD);
2547330f729Sjoerg           return true;
2557330f729Sjoerg         }
2567330f729Sjoerg 
2577330f729Sjoerg         NamedDecls.insert(ND);
2587330f729Sjoerg         return true;
2597330f729Sjoerg       }
2607330f729Sjoerg 
2617330f729Sjoerg       std::set<const NamedDecl *> LateParsedDecls;
2627330f729Sjoerg       std::set<NamedDecl *> NamedDecls;
2637330f729Sjoerg       std::set<const ValueDecl *> ValueDecls;
2647330f729Sjoerg     } v;
2657330f729Sjoerg 
2667330f729Sjoerg     v.TraverseDecl(context.getTranslationUnitDecl());
2677330f729Sjoerg 
2687330f729Sjoerg     MangledSymbols Symbols;
2697330f729Sjoerg     auto OS = Instance.createDefaultOutputFile(/*Binary=*/false, InFile, "ifs");
2707330f729Sjoerg     if (!OS)
2717330f729Sjoerg       return;
2727330f729Sjoerg 
2737330f729Sjoerg     if (Instance.getLangOpts().DelayedTemplateParsing) {
2747330f729Sjoerg       clang::Sema &S = Instance.getSema();
2757330f729Sjoerg       for (const auto *FD : v.LateParsedDecls) {
2767330f729Sjoerg         clang::LateParsedTemplate &LPT =
2777330f729Sjoerg             *S.LateParsedTemplateMap.find(cast<FunctionDecl>(FD))->second;
2787330f729Sjoerg         S.LateTemplateParser(S.OpaqueParser, LPT);
2797330f729Sjoerg         HandleNamedDecl(FD, Symbols, (FromTU | IsLate));
2807330f729Sjoerg       }
2817330f729Sjoerg     }
2827330f729Sjoerg 
2837330f729Sjoerg     for (const NamedDecl *ND : v.ValueDecls)
2847330f729Sjoerg       HandleNamedDecl(ND, Symbols, FromTU);
2857330f729Sjoerg     for (const NamedDecl *ND : v.NamedDecls)
2867330f729Sjoerg       HandleNamedDecl(ND, Symbols, FromTU);
2877330f729Sjoerg 
288*e038c9c4Sjoerg     auto writeIfsV1 = [this](const llvm::Triple &T,
289*e038c9c4Sjoerg                              const MangledSymbols &Symbols,
2907330f729Sjoerg                              const ASTContext &context, StringRef Format,
2917330f729Sjoerg                              raw_ostream &OS) -> void {
2927330f729Sjoerg       OS << "--- !" << Format << "\n";
293*e038c9c4Sjoerg       OS << "IfsVersion: 2.0\n";
2947330f729Sjoerg       OS << "Triple: " << T.str() << "\n";
295*e038c9c4Sjoerg       OS << "ObjectFileFormat: "
296*e038c9c4Sjoerg          << "ELF"
297*e038c9c4Sjoerg          << "\n"; // TODO: For now, just ELF.
2987330f729Sjoerg       OS << "Symbols:\n";
2997330f729Sjoerg       for (const auto &E : Symbols) {
3007330f729Sjoerg         const MangledSymbol &Symbol = E.second;
3017330f729Sjoerg         for (auto Name : Symbol.Names) {
302*e038c9c4Sjoerg           OS << "  - { Name: \""
3037330f729Sjoerg              << (Symbol.ParentName.empty() || Instance.getLangOpts().CPlusPlus
3047330f729Sjoerg                      ? ""
3057330f729Sjoerg                      : (Symbol.ParentName + "."))
306*e038c9c4Sjoerg              << Name << "\", Type: ";
3077330f729Sjoerg           switch (Symbol.Type) {
3087330f729Sjoerg           default:
3097330f729Sjoerg             llvm_unreachable(
3107330f729Sjoerg                 "clang -emit-interface-stubs: Unexpected symbol type.");
3117330f729Sjoerg           case llvm::ELF::STT_NOTYPE:
3127330f729Sjoerg             OS << "NoType";
3137330f729Sjoerg             break;
3147330f729Sjoerg           case llvm::ELF::STT_OBJECT: {
3157330f729Sjoerg             auto VD = cast<ValueDecl>(E.first)->getType();
3167330f729Sjoerg             OS << "Object, Size: "
3177330f729Sjoerg                << context.getTypeSizeInChars(VD).getQuantity();
3187330f729Sjoerg             break;
3197330f729Sjoerg           }
3207330f729Sjoerg           case llvm::ELF::STT_FUNC:
3217330f729Sjoerg             OS << "Func";
3227330f729Sjoerg             break;
3237330f729Sjoerg           }
3247330f729Sjoerg           if (Symbol.Binding == llvm::ELF::STB_WEAK)
3257330f729Sjoerg             OS << ", Weak: true";
3267330f729Sjoerg           OS << " }\n";
3277330f729Sjoerg         }
3287330f729Sjoerg       }
3297330f729Sjoerg       OS << "...\n";
3307330f729Sjoerg       OS.flush();
3317330f729Sjoerg     };
3327330f729Sjoerg 
333*e038c9c4Sjoerg     assert(Format == "experimental-ifs-v2" && "Unexpected IFS Format.");
3347330f729Sjoerg     writeIfsV1(Instance.getTarget().getTriple(), Symbols, context, Format, *OS);
3357330f729Sjoerg   }
3367330f729Sjoerg };
3377330f729Sjoerg } // namespace
3387330f729Sjoerg 
3397330f729Sjoerg std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)340*e038c9c4Sjoerg GenerateInterfaceStubsAction::CreateASTConsumer(CompilerInstance &CI,
3417330f729Sjoerg                                                 StringRef InFile) {
3427330f729Sjoerg   return std::make_unique<InterfaceStubFunctionsConsumer>(
343*e038c9c4Sjoerg       CI, InFile, "experimental-ifs-v2");
3447330f729Sjoerg }
345