15#include "llvm/BinaryFormat/ELF.h"
20class InterfaceStubFunctionsConsumer :
public ASTConsumer {
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())
59 if (isa<BlockDecl>(
Parent) || isa<CXXMethodDecl>(
Parent))
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)) {
69 if (FD->isInlined() && !isa<CXXMethodDecl>(FD) &&
70 !Instance.getLangOpts().GNUInline)
73 if (
const auto *RC = dyn_cast<CXXRecordDecl>(MD->getParent()))
74 if (isa<ClassTemplateDecl>(RC->getParent()) || !isVisible(RC))
76 if (MD->isDependentContext() || !MD->hasBody())
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);
98 if (isa<CXXConstructorDecl>(ND) || isa<CXXDestructorDecl>(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())
112 if (isa<FieldDecl>(ND) || isa<ParmVarDecl>(ND))
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(),
135 isa<VarDecl>(ND) ? llvm::ELF::STT_OBJECT
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);
152 MangledSymbols &Symbols,
int RDO) {
154 HandleNamedDecl(dyn_cast<NamedDecl>(
D), Symbols, RDO);
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:
171 HandleDecls(cast<NamespaceDecl>(ND)->decls(), Symbols, RDO);
173 case Decl::Kind::CXXRecord:
174 HandleDecls(cast<CXXRecordDecl>(ND)->decls(), Symbols, RDO);
176 case Decl::Kind::ClassTemplateSpecialization:
177 HandleDecls(cast<ClassTemplateSpecializationDecl>(ND)->decls(), Symbols,
180 case Decl::Kind::ClassTemplate:
181 HandleTemplateSpecializations(*cast<ClassTemplateDecl>(ND), Symbols, RDO);
183 case Decl::Kind::FunctionTemplate:
184 HandleTemplateSpecializations(*cast<FunctionTemplateDecl>(ND), Symbols,
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: {
214 const auto *VD = cast<VarDecl>(ND);
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) {}
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) {
276 for (
const auto *FD :
v.LateParsedDecls) {
280 HandleNamedDecl(FD, Symbols, (FromTU | IsLate));
285 HandleNamedDecl(ND, Symbols, FromTU);
287 HandleNamedDecl(ND, Symbols, FromTU);
289 auto writeIfsV1 = [
this](
const llvm::Triple &
T,
290 const MangledSymbols &Symbols,
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: {
313 auto VD = cast<ValueDecl>(
E.first)->getType();
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.
virtual void HandleTranslationUnit(ASTContext &Ctx)
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
TranslationUnitDecl * getTranslationUnitDecl() const
Represents a static or instance method of a struct/union/class.
Declaration of a class template.
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.
Represents a function declaration or definition.
Declaration of a template function.
spec_range specializations() const
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
This represents a decl that may have a name.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Visibility getVisibility() const
Determines the visibility of this entity.
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
Sema - This implements semantic analysis and AST building for C.
LateParsedTemplateMapT LateParsedTemplateMap
LateTemplateParserCB * LateTemplateParser
The base class of the type hierarchy.
Represents a variable declaration or definition.
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
@ DefaultVisibility
Objects with "default" visibility are seen by the dynamic linker and act like normal objects.
Contains a late templated function.