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), Names(Names) {}
38 using MangledSymbols = std::map<const NamedDecl *, MangledSymbol>;
40 bool WriteNamedDecl(
const NamedDecl *ND, MangledSymbols &Symbols,
int RDO) {
48 auto isVisible = [](
const NamedDecl *ND) ->
bool {
52 auto ignoreDecl = [
this, isVisible](
const NamedDecl *ND) ->
bool {
56 if (
const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
57 if (
const auto *
Parent = VD->getParentFunctionOrMethod())
58 if (isa<BlockDecl>(
Parent) || isa<CXXMethodDecl>(
Parent))
61 if ((VD->getStorageClass() == StorageClass::SC_Extern) ||
62 (VD->getStorageClass() == StorageClass::SC_Static &&
63 VD->getParentFunctionOrMethod() ==
nullptr))
67 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
68 if (FD->isInlined() && !isa<CXXMethodDecl>(FD) &&
69 !Instance.getLangOpts().GNUInline)
72 if (
const auto *RC = dyn_cast<CXXRecordDecl>(MD->getParent()))
73 if (isa<ClassTemplateDecl>(RC->getParent()) || !isVisible(RC))
75 if (MD->isDependentContext() || !MD->hasBody())
85 if (
const VarDecl *VD = dyn_cast<VarDecl>(ND))
87 dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod()))
92 auto getMangledNames = [](
const NamedDecl *ND) -> std::vector<std::string> {
96 std::vector<std::string> MangledNames = NameGen.getAllManglings(ND);
97 if (isa<CXXConstructorDecl>(ND) || isa<CXXDestructorDecl>(ND))
99#ifdef EXPENSIVE_CHECKS
100 assert(MangledNames.size() <= 1 &&
"Expected only one name mangling.");
102 return {NameGen.getName(ND)};
107 if (Symbols.find(ND) != Symbols.end())
111 if (isa<FieldDecl>(ND) || isa<ParmVarDecl>(ND))
114 const NamedDecl *ParentDecl = getParentFunctionDecl(ND);
115 if ((ParentDecl && ignoreDecl(ParentDecl)) || ignoreDecl(ND))
119 Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
120 <<
"Generating Interface Stubs is not supported with "
121 "delayed template parsing.";
123 if (
const auto *FD = dyn_cast<FunctionDecl>(ND))
124 if (FD->isDependentContext())
127 const bool IsWeak = (ND->
hasAttr<WeakAttr>() ||
130 Symbols.insert(std::make_pair(
132 MangledSymbol(getMangledNames(ParentDecl).front(),
134 isa<VarDecl>(ND) ? llvm::ELF::STT_OBJECT
135 : llvm::ELF::STT_FUNC,
137 IsWeak ? llvm::ELF::STB_WEAK : llvm::ELF::STB_GLOBAL,
138 getMangledNames(ND))));
144 HandleDecls(
const llvm::iterator_range<DeclContext::decl_iterator> &Decls,
145 MangledSymbols &Symbols,
int RDO) {
146 for (
const auto *D : Decls)
147 HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
151 MangledSymbols &Symbols,
int RDO) {
153 HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
157 MangledSymbols &Symbols,
int RDO) {
159 HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
162 bool HandleNamedDecl(
const NamedDecl *ND, MangledSymbols &Symbols,
int RDO) {
169 case Decl::Kind::Namespace:
170 HandleDecls(cast<NamespaceDecl>(ND)->decls(), Symbols, RDO);
172 case Decl::Kind::CXXRecord:
173 HandleDecls(cast<CXXRecordDecl>(ND)->decls(), Symbols, RDO);
175 case Decl::Kind::ClassTemplateSpecialization:
176 HandleDecls(cast<ClassTemplateSpecializationDecl>(ND)->decls(), Symbols,
179 case Decl::Kind::ClassTemplate:
180 HandleTemplateSpecializations(*cast<ClassTemplateDecl>(ND), Symbols, RDO);
182 case Decl::Kind::FunctionTemplate:
183 HandleTemplateSpecializations(*cast<FunctionTemplateDecl>(ND), Symbols,
186 case Decl::Kind::Record:
187 case Decl::Kind::Typedef:
188 case Decl::Kind::Enum:
189 case Decl::Kind::EnumConstant:
190 case Decl::Kind::TemplateTypeParm:
191 case Decl::Kind::NonTypeTemplateParm:
192 case Decl::Kind::CXXConversion:
193 case Decl::Kind::UnresolvedUsingValue:
194 case Decl::Kind::Using:
195 case Decl::Kind::UsingShadow:
196 case Decl::Kind::TypeAliasTemplate:
197 case Decl::Kind::TypeAlias:
198 case Decl::Kind::VarTemplate:
199 case Decl::Kind::VarTemplateSpecialization:
200 case Decl::Kind::UsingDirective:
201 case Decl::Kind::TemplateTemplateParm:
202 case Decl::Kind::ClassTemplatePartialSpecialization:
203 case Decl::Kind::IndirectField:
204 case Decl::Kind::ConstructorUsingShadow:
205 case Decl::Kind::CXXDeductionGuide:
206 case Decl::Kind::NamespaceAlias:
207 case Decl::Kind::UnresolvedUsingTypename:
209 case Decl::Kind::Var: {
213 const auto *VD = cast<VarDecl>(ND);
215 if (VD->isTemplated() || VD->getType()->isDependentType())
217 if (WriteNamedDecl(ND, Symbols, RDO))
221 case Decl::Kind::ParmVar:
222 case Decl::Kind::CXXMethod:
223 case Decl::Kind::CXXConstructor:
224 case Decl::Kind::CXXDestructor:
225 case Decl::Kind::Function:
226 case Decl::Kind::Field:
227 if (WriteNamedDecl(ND, Symbols, RDO))
233 Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
234 <<
"Expected a function or function template decl.";
239 InterfaceStubFunctionsConsumer(
CompilerInstance &Instance, StringRef InFile,
241 : Instance(Instance), InFile(InFile), Format(Format) {}
246 if (
const auto *FD = dyn_cast<FunctionDecl>(ND))
247 if (FD->isLateTemplateParsed()) {
248 LateParsedDecls.insert(FD);
252 if (
const auto *VD = dyn_cast<ValueDecl>(ND)) {
253 ValueDecls.insert(VD);
257 NamedDecls.insert(ND);
261 std::set<const NamedDecl *> LateParsedDecls;
262 std::set<NamedDecl *> NamedDecls;
263 std::set<const ValueDecl *> ValueDecls;
268 MangledSymbols Symbols;
269 auto OS = Instance.createDefaultOutputFile(
false, InFile,
"ifs");
273 if (Instance.getLangOpts().DelayedTemplateParsing) {
275 for (
const auto *FD :
v.LateParsedDecls) {
279 HandleNamedDecl(FD, Symbols, (FromTU | IsLate));
284 HandleNamedDecl(ND, Symbols, FromTU);
286 HandleNamedDecl(ND, Symbols, FromTU);
288 auto writeIfsV1 = [
this](
const llvm::Triple &T,
289 const MangledSymbols &Symbols,
291 raw_ostream &OS) ->
void {
292 OS <<
"--- !" << Format <<
"\n";
293 OS <<
"IfsVersion: 3.0\n";
294 OS <<
"Target: " << T.str() <<
"\n";
296 for (
const auto &E : Symbols) {
297 const MangledSymbol &Symbol = E.second;
298 for (
auto Name : Symbol.Names) {
299 OS <<
" - { Name: \""
300 << (Symbol.ParentName.empty() || Instance.getLangOpts().CPlusPlus
302 : (Symbol.ParentName +
"."))
303 << Name <<
"\", Type: ";
304 switch (Symbol.Type) {
307 "clang -emit-interface-stubs: Unexpected symbol type.");
308 case llvm::ELF::STT_NOTYPE:
311 case llvm::ELF::STT_OBJECT: {
312 auto VD = cast<ValueDecl>(E.first)->getType();
313 OS <<
"Object, Size: "
314 << context.getTypeSizeInChars(VD).getQuantity();
317 case llvm::ELF::STT_FUNC:
321 if (Symbol.Binding == llvm::ELF::STB_WEAK)
322 OS <<
", Weak: true";
330 assert(Format ==
"ifs-v1" &&
"Unexpected IFS Format.");
331 writeIfsV1(Instance.getTarget().getTriple(), Symbols, context, Format, *OS);
336std::unique_ptr<ASTConsumer>
339 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.
@ DefaultVisibility
Objects with "default" visibility are seen by the dynamic linker and act like normal objects.
Contains a late templated function.