15#include "llvm/BinaryFormat/ELF.h"
20class InterfaceStubFunctionsConsumer :
public ASTConsumer {
21 CompilerInstance &Instance;
24 std::set<std::string> ParsedTemplates;
26 enum RootDeclOrigin { TopLevel = 0, FromTU = 1, IsLate = 2 };
27 struct MangledSymbol {
28 std::string ParentName;
31 std::vector<std::string> Names;
32 MangledSymbol() =
delete;
34 MangledSymbol(
const std::string &ParentName, uint8_t Type, uint8_t Binding,
35 std::vector<std::string> Names)
36 : ParentName(ParentName), Type(Type), Binding(Binding),
37 Names(std::move(Names)) {}
39 using MangledSymbols = std::map<const NamedDecl *, MangledSymbol>;
41 bool WriteNamedDecl(
const NamedDecl *ND, MangledSymbols &Symbols,
int RDO) {
49 auto isVisible = [](
const NamedDecl *ND) ->
bool {
53 auto ignoreDecl = [
this, isVisible](
const NamedDecl *ND) ->
bool {
57 if (
const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
58 if (
const auto *Parent = VD->getParentFunctionOrMethod())
62 if ((VD->getStorageClass() == StorageClass::SC_Extern) ||
63 (VD->getStorageClass() == StorageClass::SC_Static &&
64 VD->getParentFunctionOrMethod() ==
nullptr))
68 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
70 !Instance.getLangOpts().GNUInline)
72 if (
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
73 if (
const auto *RC = dyn_cast<CXXRecordDecl>(MD->getParent()))
76 if (MD->isDependentContext() || !MD->hasBody())
79 if (FD->getStorageClass() == StorageClass::SC_Static)
85 auto getParentFunctionDecl = [](
const NamedDecl *ND) ->
const NamedDecl * {
86 if (
const VarDecl *VD = dyn_cast<VarDecl>(ND))
88 dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod()))
93 auto getMangledNames = [](
const NamedDecl *ND) -> std::vector<std::string> {
97 std::vector<std::string> MangledNames = NameGen.getAllManglings(ND);
100#ifdef EXPENSIVE_CHECKS
101 assert(MangledNames.size() <= 1 &&
"Expected only one name mangling.");
103 return {NameGen.getName(ND)};
108 if (Symbols.find(ND) != Symbols.end())
115 const NamedDecl *ParentDecl = getParentFunctionDecl(ND);
116 if ((ParentDecl && ignoreDecl(ParentDecl)) || ignoreDecl(ND))
120 Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
121 <<
"Generating Interface Stubs is not supported with "
122 "delayed template parsing.";
124 if (
const auto *FD = dyn_cast<FunctionDecl>(ND))
125 if (FD->isDependentContext())
128 const bool IsWeak = (ND->
hasAttr<WeakAttr>() ||
131 Symbols.insert(std::make_pair(
133 MangledSymbol(getMangledNames(ParentDecl).front(),
136 : llvm::ELF::STT_FUNC,
138 IsWeak ? llvm::ELF::STB_WEAK : llvm::ELF::STB_GLOBAL,
139 getMangledNames(ND))));
145 HandleDecls(
const llvm::iterator_range<DeclContext::decl_iterator> &Decls,
146 MangledSymbols &Symbols,
int RDO) {
147 for (
const auto *D : Decls)
148 HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
151 void HandleTemplateSpecializations(
const FunctionTemplateDecl &FTD,
152 MangledSymbols &Symbols,
int RDO) {
154 HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
157 void HandleTemplateSpecializations(
const ClassTemplateDecl &CTD,
158 MangledSymbols &Symbols,
int RDO) {
160 HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
163 bool HandleNamedDecl(
const NamedDecl *ND, MangledSymbols &Symbols,
int RDO) {
170 case Decl::Kind::Namespace:
173 case Decl::Kind::CXXRecord:
176 case Decl::Kind::ClassTemplateSpecialization:
180 case Decl::Kind::ClassTemplate:
183 case Decl::Kind::FunctionTemplate:
187 case Decl::Kind::Record:
188 case Decl::Kind::Typedef:
189 case Decl::Kind::Enum:
190 case Decl::Kind::EnumConstant:
191 case Decl::Kind::TemplateTypeParm:
192 case Decl::Kind::NonTypeTemplateParm:
193 case Decl::Kind::CXXConversion:
194 case Decl::Kind::UnresolvedUsingValue:
195 case Decl::Kind::Using:
196 case Decl::Kind::UsingShadow:
197 case Decl::Kind::TypeAliasTemplate:
198 case Decl::Kind::TypeAlias:
199 case Decl::Kind::VarTemplate:
200 case Decl::Kind::VarTemplateSpecialization:
201 case Decl::Kind::UsingDirective:
202 case Decl::Kind::TemplateTemplateParm:
203 case Decl::Kind::ClassTemplatePartialSpecialization:
204 case Decl::Kind::IndirectField:
205 case Decl::Kind::ConstructorUsingShadow:
206 case Decl::Kind::CXXDeductionGuide:
207 case Decl::Kind::NamespaceAlias:
208 case Decl::Kind::UnresolvedUsingTypename:
210 case Decl::Kind::Var: {
216 if (VD->isTemplated() || VD->getType()->isDependentType())
218 if (WriteNamedDecl(ND, Symbols, RDO))
222 case Decl::Kind::ParmVar:
223 case Decl::Kind::CXXMethod:
224 case Decl::Kind::CXXConstructor:
225 case Decl::Kind::CXXDestructor:
226 case Decl::Kind::Function:
227 case Decl::Kind::Field:
228 if (WriteNamedDecl(ND, Symbols, RDO))
234 Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
235 <<
"Expected a function or function template decl.";
240 InterfaceStubFunctionsConsumer(CompilerInstance &Instance, StringRef InFile,
242 : Instance(Instance), InFile(InFile), Format(Format) {}
244 void HandleTranslationUnit(ASTContext &context)
override {
245 struct Visitor :
public RecursiveASTVisitor<Visitor> {
246 bool VisitNamedDecl(NamedDecl *ND) {
247 if (
const auto *FD = dyn_cast<FunctionDecl>(ND))
248 if (FD->isLateTemplateParsed()) {
249 LateParsedDecls.insert(FD);
253 if (
const auto *VD = dyn_cast<ValueDecl>(ND)) {
254 ValueDecls.insert(VD);
258 NamedDecls.insert(ND);
262 std::set<const NamedDecl *> LateParsedDecls;
263 std::set<NamedDecl *> NamedDecls;
264 std::set<const ValueDecl *> ValueDecls;
269 MangledSymbols Symbols;
270 auto OS = Instance.createDefaultOutputFile(
false, InFile,
"ifs");
274 if (Instance.getLangOpts().DelayedTemplateParsing) {
275 clang::Sema &S = Instance.getSema();
276 for (
const auto *FD : v.LateParsedDecls) {
277 clang::LateParsedTemplate &LPT =
280 HandleNamedDecl(FD, Symbols, (FromTU | IsLate));
284 for (
const NamedDecl *ND : v.ValueDecls)
285 HandleNamedDecl(ND, Symbols, FromTU);
286 for (
const NamedDecl *ND : v.NamedDecls)
287 HandleNamedDecl(ND, Symbols, FromTU);
289 auto writeIfsV1 = [
this](
const llvm::Triple &
T,
290 const MangledSymbols &Symbols,
291 const ASTContext &context, StringRef Format,
292 raw_ostream &
OS) ->
void {
293 OS <<
"--- !" << Format <<
"\n";
294 OS <<
"IfsVersion: 3.0\n";
295 OS <<
"Target: " <<
T.str() <<
"\n";
297 for (
const auto &E : Symbols) {
298 const MangledSymbol &Symbol = E.second;
299 for (
const auto &Name : Symbol.Names) {
300 OS <<
" - { Name: \""
301 << (Symbol.ParentName.empty() || Instance.getLangOpts().CPlusPlus
303 : (Symbol.ParentName +
"."))
304 << Name <<
"\", Type: ";
305 switch (Symbol.Type) {
308 "clang -emit-interface-stubs: Unexpected symbol type.");
309 case llvm::ELF::STT_NOTYPE:
312 case llvm::ELF::STT_OBJECT: {
314 OS <<
"Object, Size: "
315 << context.getTypeSizeInChars(VD).getQuantity();
318 case llvm::ELF::STT_FUNC:
322 if (Symbol.Binding == llvm::ELF::STB_WEAK)
323 OS <<
", Weak: true";
331 assert(Format ==
"ifs-v1" &&
"Unexpected IFS Format.");
332 writeIfsV1(Instance.getTarget().getTriple(), Symbols, context, Format, *OS);
337std::unique_ptr<ASTConsumer>
340 return std::make_unique<InterfaceStubFunctionsConsumer>(CI, InFile,
"ifs-v1");
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
TranslationUnitDecl * getTranslationUnitDecl() const
spec_range specializations() const
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
ASTContext & getASTContext() const LLVM_READONLY
bool isWeakImported() const
Determine whether this is a weak-imported symbol.
spec_range specializations() const
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Visibility getVisibility() const
Determines the visibility of this entity.
LateParsedTemplateMapT LateParsedTemplateMap
LateTemplateParserCB * LateTemplateParser
Defines the clang::TargetInfo interface.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
const FunctionProtoType * T
U cast(CodeGen::Address addr)
@ DefaultVisibility
Objects with "default" visibility are seen by the dynamic linker and act like normal objects.