14#include "llvm/BinaryFormat/ELF.h"
19class InterfaceStubFunctionsConsumer :
public ASTConsumer {
20 CompilerInstance &Instance;
23 std::set<std::string> ParsedTemplates;
25 enum RootDeclOrigin { TopLevel = 0, FromTU = 1, IsLate = 2 };
26 struct MangledSymbol {
27 std::string ParentName;
30 std::vector<std::string> Names;
31 MangledSymbol() =
delete;
33 MangledSymbol(
const std::string &ParentName,
uint8_t Type,
uint8_t Binding,
34 std::vector<std::string> Names)
35 : ParentName(ParentName), Type(Type), Binding(Binding),
36 Names(std::move(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())
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)) {
69 !Instance.getLangOpts().GNUInline)
71 if (
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
72 const CXXRecordDecl *RC = MD->getParent();
75 if (MD->isDependentContext() || !MD->hasBody())
78 if (FD->getStorageClass() == StorageClass::SC_Static)
84 auto getParentFunctionDecl = [](
const NamedDecl *ND) ->
const NamedDecl * {
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);
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())
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(),
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);
150 void HandleTemplateSpecializations(
const FunctionTemplateDecl &FTD,
151 MangledSymbols &Symbols,
int RDO) {
153 HandleNamedDecl(D, Symbols, RDO);
156 void HandleTemplateSpecializations(
const ClassTemplateDecl &CTD,
157 MangledSymbols &Symbols,
int RDO) {
159 HandleNamedDecl(D, Symbols, RDO);
162 bool HandleNamedDecl(
const NamedDecl *ND, MangledSymbols &Symbols,
int RDO) {
169 case Decl::Kind::Namespace:
172 case Decl::Kind::CXXRecord:
175 case Decl::Kind::ClassTemplateSpecialization:
179 case Decl::Kind::ClassTemplate:
182 case Decl::Kind::FunctionTemplate:
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: {
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) {}
243 void HandleTranslationUnit(ASTContext &context)
override {
244 struct Visitor :
public RecursiveASTVisitor<Visitor> {
245 bool VisitNamedDecl(NamedDecl *ND) {
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) {
274 clang::Sema &S = Instance.getSema();
275 for (
const auto *FD : v.LateParsedDecls) {
276 clang::LateParsedTemplate &LPT =
279 HandleNamedDecl(FD, Symbols, (FromTU | IsLate));
283 for (
const NamedDecl *ND : v.ValueDecls)
284 HandleNamedDecl(ND, Symbols, FromTU);
285 for (
const NamedDecl *ND : v.NamedDecls)
286 HandleNamedDecl(ND, Symbols, FromTU);
288 auto writeIfsV1 = [
this](
const llvm::Triple &
T,
289 const MangledSymbols &Symbols,
290 const ASTContext &context, StringRef Format,
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 (
const 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: {
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.
TranslationUnitDecl * getTranslationUnitDecl() const
spec_range specializations() const
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
DeclContext * getParent()
getParent - Returns the containing DeclContext.
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.
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t