blob: 025e6eb1508fc310742c3bd959b22e24133b25a3 [file] [log] [blame]
Puyan Lotfi68f29da2019-06-20 16:59:48 +00001//===--- InterfaceStubFunctionsConsumer.cpp -------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/AST/Mangle.h"
10#include "clang/AST/RecursiveASTVisitor.h"
11#include "clang/Frontend/CompilerInstance.h"
12#include "clang/Frontend/FrontendActions.h"
13#include "clang/Sema/TemplateInstCallback.h"
14#include "llvm/BinaryFormat/ELF.h"
15
16using namespace clang;
17
Benjamin Kramerdc5f8052019-08-23 19:59:23 +000018namespace {
Puyan Lotfi68f29da2019-06-20 16:59:48 +000019class InterfaceStubFunctionsConsumer : public ASTConsumer {
20 CompilerInstance &Instance;
21 StringRef InFile;
22 StringRef Format;
23 std::set<std::string> ParsedTemplates;
24
25 enum RootDeclOrigin { TopLevel = 0, FromTU = 1, IsLate = 2 };
26 struct MangledSymbol {
27 std::string ParentName;
28 uint8_t Type;
29 uint8_t Binding;
30 std::vector<std::string> Names;
31 MangledSymbol() = delete;
32
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), Names(Names) {}
36 };
37 using MangledSymbols = std::map<const NamedDecl *, MangledSymbol>;
38
39 bool WriteNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) {
40 // Here we filter out anything that's not set to DefaultVisibility.
41 // DefaultVisibility is set on a decl when -fvisibility is not specified on
42 // the command line (or specified as default) and the decl does not have
43 // __attribute__((visibility("hidden"))) set or when the command line
44 // argument is set to hidden but the decl explicitly has
45 // __attribute__((visibility ("default"))) set. We do this so that the user
46 // can have fine grain control of what they want to expose in the stub.
47 auto isVisible = [](const NamedDecl *ND) -> bool {
48 return ND->getVisibility() == DefaultVisibility;
49 };
50
51 auto ignoreDecl = [this, isVisible](const NamedDecl *ND) -> bool {
52 if (!isVisible(ND))
53 return true;
54
55 if (const VarDecl *VD = dyn_cast<VarDecl>(ND))
56 if ((VD->getStorageClass() == StorageClass::SC_Extern) ||
57 (VD->getStorageClass() == StorageClass::SC_Static &&
58 VD->getParentFunctionOrMethod() == nullptr))
59 return true;
60
61 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
62 if (FD->isInlined() && !isa<CXXMethodDecl>(FD) &&
63 !Instance.getLangOpts().GNUInline)
64 return true;
65 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
66 if (const auto *RC = dyn_cast<CXXRecordDecl>(MD->getParent()))
67 if (isa<ClassTemplateDecl>(RC->getParent()) || !isVisible(RC))
68 return true;
69 if (MD->isDependentContext() || !MD->hasBody())
70 return true;
71 }
72 if (FD->getStorageClass() == StorageClass::SC_Static)
73 return true;
74 }
75 return false;
76 };
77
78 auto getParentFunctionDecl = [](const NamedDecl *ND) -> const NamedDecl * {
79 if (const VarDecl *VD = dyn_cast<VarDecl>(ND))
80 if (const auto *FD =
81 dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod()))
82 return FD;
83 return nullptr;
84 };
85
86 auto getMangledNames = [](const NamedDecl *ND) -> std::vector<std::string> {
87 if (!ND)
88 return {""};
89 ASTNameGenerator NameGen(ND->getASTContext());
90 std::vector<std::string> MangledNames = NameGen.getAllManglings(ND);
91 if (isa<CXXConstructorDecl>(ND) || isa<CXXDestructorDecl>(ND))
92 return MangledNames;
93#ifdef EXPENSIVE_CHECKS
94 assert(MangledNames.size() <= 1 && "Expected only one name mangling.");
95#endif
96 return {NameGen.getName(ND)};
97 };
98
99 if (!(RDO & FromTU))
100 return true;
101 if (Symbols.find(ND) != Symbols.end())
102 return true;
103 // - Currently have not figured out how to produce the names for FieldDecls.
104 // - Do not want to produce symbols for function paremeters.
105 if (isa<FieldDecl>(ND) || isa<ParmVarDecl>(ND))
106 return true;
107
108 const NamedDecl *ParentDecl = getParentFunctionDecl(ND);
109 if ((ParentDecl && ignoreDecl(ParentDecl)) || ignoreDecl(ND))
110 return true;
111
112 if (RDO & IsLate) {
113 Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
114 << "Generating Interface Stubs is not supported with "
115 "delayed template parsing.";
116 } else {
117 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
118 if (FD->isDependentContext())
119 return true;
120
121 const bool IsWeak = (ND->hasAttr<WeakAttr>() ||
122 ND->hasAttr<WeakRefAttr>() || ND->isWeakImported());
123
124 Symbols.insert(std::make_pair(
125 ND,
126 MangledSymbol(getMangledNames(ParentDecl).front(),
127 // Type:
128 isa<VarDecl>(ND) ? llvm::ELF::STT_OBJECT
129 : llvm::ELF::STT_FUNC,
130 // Binding:
131 IsWeak ? llvm::ELF::STB_WEAK : llvm::ELF::STB_GLOBAL,
132 getMangledNames(ND))));
133 }
134 return true;
135 }
136
137 void
138 HandleDecls(const llvm::iterator_range<DeclContext::decl_iterator> &Decls,
139 MangledSymbols &Symbols, int RDO) {
140 for (const auto *D : Decls)
141 HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
142 }
143
144 void HandleTemplateSpecializations(const FunctionTemplateDecl &FTD,
145 MangledSymbols &Symbols, int RDO) {
146 for (const auto *D : FTD.specializations())
147 HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
148 }
149
150 void HandleTemplateSpecializations(const ClassTemplateDecl &CTD,
151 MangledSymbols &Symbols, int RDO) {
152 for (const auto *D : CTD.specializations())
153 HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
154 }
155
156 bool HandleNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) {
157 if (!ND)
158 return false;
159
160 switch (ND->getKind()) {
161 default:
162 break;
163 case Decl::Kind::Namespace:
164 HandleDecls(cast<NamespaceDecl>(ND)->decls(), Symbols, RDO);
165 return true;
166 case Decl::Kind::CXXRecord:
167 HandleDecls(cast<CXXRecordDecl>(ND)->decls(), Symbols, RDO);
168 return true;
169 case Decl::Kind::ClassTemplateSpecialization:
170 HandleDecls(cast<ClassTemplateSpecializationDecl>(ND)->decls(), Symbols,
171 RDO);
172 return true;
173 case Decl::Kind::ClassTemplate:
174 HandleTemplateSpecializations(*cast<ClassTemplateDecl>(ND), Symbols, RDO);
175 return true;
176 case Decl::Kind::FunctionTemplate:
177 HandleTemplateSpecializations(*cast<FunctionTemplateDecl>(ND), Symbols,
178 RDO);
179 return true;
Puyan Lotfie3388c42019-10-11 17:24:11 +0000180 case Decl::Kind::Record:
181 case Decl::Kind::Typedef:
182 case Decl::Kind::Enum:
183 case Decl::Kind::EnumConstant:
Puyan Lotfi68f29da2019-06-20 16:59:48 +0000184 case Decl::Kind::TemplateTypeParm:
Puyan Lotfi79e345f2019-11-08 20:21:28 -0500185 case Decl::Kind::NonTypeTemplateParm:
186 case Decl::Kind::CXXConversion:
187 case Decl::Kind::UnresolvedUsingValue:
188 case Decl::Kind::Using:
189 case Decl::Kind::UsingShadow:
190 case Decl::Kind::TypeAliasTemplate:
191 case Decl::Kind::TypeAlias:
192 case Decl::Kind::VarTemplate:
193 case Decl::Kind::VarTemplateSpecialization:
194 case Decl::Kind::UsingDirective:
195 case Decl::Kind::TemplateTemplateParm:
196 case Decl::Kind::ClassTemplatePartialSpecialization:
197 case Decl::Kind::IndirectField:
Puyan Lotfi68f29da2019-06-20 16:59:48 +0000198 return true;
Puyan Lotfi79e345f2019-11-08 20:21:28 -0500199 case Decl::Kind::Var: {
200 // Bail on any VarDecl that either has no named symbol.
201 if (!ND->getIdentifier())
202 return true;
203 const auto *VD = cast<VarDecl>(ND);
204 // Bail on any VarDecl that is a dependent or templated type.
205 if (VD->isTemplated() || VD->getType()->isDependentType())
206 return true;
207 if (WriteNamedDecl(ND, Symbols, RDO))
208 return true;
209 break;
210 }
Puyan Lotfi68f29da2019-06-20 16:59:48 +0000211 case Decl::Kind::ParmVar:
212 case Decl::Kind::CXXMethod:
213 case Decl::Kind::CXXConstructor:
214 case Decl::Kind::CXXDestructor:
215 case Decl::Kind::Function:
216 case Decl::Kind::Field:
217 if (WriteNamedDecl(ND, Symbols, RDO))
218 return true;
219 }
220
221 // While interface stubs are in the development stage, it's probably best to
222 // catch anything that's not a VarDecl or Template/FunctionDecl.
223 Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
224 << "Expected a function or function template decl.";
225 return false;
226 }
227
228public:
229 InterfaceStubFunctionsConsumer(CompilerInstance &Instance, StringRef InFile,
230 StringRef Format)
231 : Instance(Instance), InFile(InFile), Format(Format) {}
232
233 void HandleTranslationUnit(ASTContext &context) override {
234 struct Visitor : public RecursiveASTVisitor<Visitor> {
235 bool VisitNamedDecl(NamedDecl *ND) {
236 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
237 if (FD->isLateTemplateParsed()) {
238 LateParsedDecls.insert(FD);
239 return true;
240 }
241
242 if (const auto *VD = dyn_cast<ValueDecl>(ND)) {
243 ValueDecls.insert(VD);
244 return true;
245 }
246
247 NamedDecls.insert(ND);
248 return true;
249 }
250
251 std::set<const NamedDecl *> LateParsedDecls;
252 std::set<NamedDecl *> NamedDecls;
253 std::set<const ValueDecl *> ValueDecls;
254 } v;
255
256 v.TraverseDecl(context.getTranslationUnitDecl());
257
258 MangledSymbols Symbols;
259 auto OS = Instance.createDefaultOutputFile(/*Binary=*/false, InFile, "ifs");
260 if (!OS)
261 return;
262
263 if (Instance.getLangOpts().DelayedTemplateParsing) {
264 clang::Sema &S = Instance.getSema();
265 for (const auto *FD : v.LateParsedDecls) {
266 clang::LateParsedTemplate &LPT =
267 *S.LateParsedTemplateMap.find(cast<FunctionDecl>(FD))->second;
268 S.LateTemplateParser(S.OpaqueParser, LPT);
269 HandleNamedDecl(FD, Symbols, (FromTU | IsLate));
270 }
271 }
272
273 for (const NamedDecl *ND : v.ValueDecls)
274 HandleNamedDecl(ND, Symbols, FromTU);
275 for (const NamedDecl *ND : v.NamedDecls)
276 HandleNamedDecl(ND, Symbols, FromTU);
277
Puyan Lotfi79e345f2019-11-08 20:21:28 -0500278 auto writeIfsV1 = [this](const llvm::Triple &T,
279 const MangledSymbols &Symbols,
280 const ASTContext &context, StringRef Format,
281 raw_ostream &OS) -> void {
Puyan Lotfid2418452019-08-22 23:29:22 +0000282 OS << "--- !" << Format << "\n";
283 OS << "IfsVersion: 1.0\n";
284 OS << "Triple: " << T.str() << "\n";
Puyan Lotfi79e345f2019-11-08 20:21:28 -0500285 OS << "ObjectFileFormat: "
286 << "ELF"
287 << "\n"; // TODO: For now, just ELF.
Puyan Lotfid2418452019-08-22 23:29:22 +0000288 OS << "Symbols:\n";
289 for (const auto &E : Symbols) {
290 const MangledSymbol &Symbol = E.second;
291 for (auto Name : Symbol.Names) {
Puyan Lotfi76f98692019-10-14 18:03:03 +0000292 OS << " \""
Puyan Lotfid2418452019-08-22 23:29:22 +0000293 << (Symbol.ParentName.empty() || Instance.getLangOpts().CPlusPlus
294 ? ""
295 : (Symbol.ParentName + "."))
Puyan Lotfi76f98692019-10-14 18:03:03 +0000296 << Name << "\" : { Type: ";
Puyan Lotfid2418452019-08-22 23:29:22 +0000297 switch (Symbol.Type) {
298 default:
299 llvm_unreachable(
Puyan Lotfi17bde36a2019-10-12 06:25:07 +0000300 "clang -emit-interface-stubs: Unexpected symbol type.");
Puyan Lotfid2418452019-08-22 23:29:22 +0000301 case llvm::ELF::STT_NOTYPE:
302 OS << "NoType";
303 break;
304 case llvm::ELF::STT_OBJECT: {
305 auto VD = cast<ValueDecl>(E.first)->getType();
306 OS << "Object, Size: "
307 << context.getTypeSizeInChars(VD).getQuantity();
308 break;
309 }
310 case llvm::ELF::STT_FUNC:
311 OS << "Func";
312 break;
313 }
314 if (Symbol.Binding == llvm::ELF::STB_WEAK)
315 OS << ", Weak: true";
316 OS << " }\n";
317 }
318 }
319 OS << "...\n";
320 OS.flush();
321 };
322
Puyan Lotfi926f4f72019-08-22 23:44:34 +0000323 assert(Format == "experimental-ifs-v1" && "Unexpected IFS Format.");
324 writeIfsV1(Instance.getTarget().getTriple(), Symbols, context, Format, *OS);
Puyan Lotfi68f29da2019-06-20 16:59:48 +0000325 }
326};
Benjamin Kramerdc5f8052019-08-23 19:59:23 +0000327} // namespace
Puyan Lotfi68f29da2019-06-20 16:59:48 +0000328
329std::unique_ptr<ASTConsumer>
Puyan Lotfid2418452019-08-22 23:29:22 +0000330GenerateInterfaceIfsExpV1Action::CreateASTConsumer(CompilerInstance &CI,
331 StringRef InFile) {
332 return std::make_unique<InterfaceStubFunctionsConsumer>(
333 CI, InFile, "experimental-ifs-v1");
334}