blob: 5e6cc65e021ac07b854a6e161bfa570abf5c9dfe [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:
185 return true;
186 case Decl::Kind::Var:
187 case Decl::Kind::ParmVar:
188 case Decl::Kind::CXXMethod:
189 case Decl::Kind::CXXConstructor:
190 case Decl::Kind::CXXDestructor:
191 case Decl::Kind::Function:
192 case Decl::Kind::Field:
193 if (WriteNamedDecl(ND, Symbols, RDO))
194 return true;
195 }
196
197 // While interface stubs are in the development stage, it's probably best to
198 // catch anything that's not a VarDecl or Template/FunctionDecl.
199 Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
200 << "Expected a function or function template decl.";
201 return false;
202 }
203
204public:
205 InterfaceStubFunctionsConsumer(CompilerInstance &Instance, StringRef InFile,
206 StringRef Format)
207 : Instance(Instance), InFile(InFile), Format(Format) {}
208
209 void HandleTranslationUnit(ASTContext &context) override {
210 struct Visitor : public RecursiveASTVisitor<Visitor> {
211 bool VisitNamedDecl(NamedDecl *ND) {
212 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
213 if (FD->isLateTemplateParsed()) {
214 LateParsedDecls.insert(FD);
215 return true;
216 }
217
218 if (const auto *VD = dyn_cast<ValueDecl>(ND)) {
219 ValueDecls.insert(VD);
220 return true;
221 }
222
223 NamedDecls.insert(ND);
224 return true;
225 }
226
227 std::set<const NamedDecl *> LateParsedDecls;
228 std::set<NamedDecl *> NamedDecls;
229 std::set<const ValueDecl *> ValueDecls;
230 } v;
231
232 v.TraverseDecl(context.getTranslationUnitDecl());
233
234 MangledSymbols Symbols;
235 auto OS = Instance.createDefaultOutputFile(/*Binary=*/false, InFile, "ifs");
236 if (!OS)
237 return;
238
239 if (Instance.getLangOpts().DelayedTemplateParsing) {
240 clang::Sema &S = Instance.getSema();
241 for (const auto *FD : v.LateParsedDecls) {
242 clang::LateParsedTemplate &LPT =
243 *S.LateParsedTemplateMap.find(cast<FunctionDecl>(FD))->second;
244 S.LateTemplateParser(S.OpaqueParser, LPT);
245 HandleNamedDecl(FD, Symbols, (FromTU | IsLate));
246 }
247 }
248
249 for (const NamedDecl *ND : v.ValueDecls)
250 HandleNamedDecl(ND, Symbols, FromTU);
251 for (const NamedDecl *ND : v.NamedDecls)
252 HandleNamedDecl(ND, Symbols, FromTU);
253
Puyan Lotfid2418452019-08-22 23:29:22 +0000254 auto writeIfsV1 =
255 [this](const llvm::Triple &T, const MangledSymbols &Symbols,
256 const ASTContext &context, StringRef Format,
257 raw_ostream &OS) -> void {
258 OS << "--- !" << Format << "\n";
259 OS << "IfsVersion: 1.0\n";
260 OS << "Triple: " << T.str() << "\n";
261 OS << "ObjectFileFormat: " << "ELF" << "\n"; // TODO: For now, just ELF.
262 OS << "Symbols:\n";
263 for (const auto &E : Symbols) {
264 const MangledSymbol &Symbol = E.second;
265 for (auto Name : Symbol.Names) {
266 OS << " "
267 << (Symbol.ParentName.empty() || Instance.getLangOpts().CPlusPlus
268 ? ""
269 : (Symbol.ParentName + "."))
270 << Name << ": { Type: ";
271 switch (Symbol.Type) {
272 default:
273 llvm_unreachable(
274 "clang -emit-iterface-stubs: Unexpected symbol type.");
275 case llvm::ELF::STT_NOTYPE:
276 OS << "NoType";
277 break;
278 case llvm::ELF::STT_OBJECT: {
279 auto VD = cast<ValueDecl>(E.first)->getType();
280 OS << "Object, Size: "
281 << context.getTypeSizeInChars(VD).getQuantity();
282 break;
283 }
284 case llvm::ELF::STT_FUNC:
285 OS << "Func";
286 break;
287 }
288 if (Symbol.Binding == llvm::ELF::STB_WEAK)
289 OS << ", Weak: true";
290 OS << " }\n";
291 }
292 }
293 OS << "...\n";
294 OS.flush();
295 };
296
Puyan Lotfi926f4f72019-08-22 23:44:34 +0000297 assert(Format == "experimental-ifs-v1" && "Unexpected IFS Format.");
298 writeIfsV1(Instance.getTarget().getTriple(), Symbols, context, Format, *OS);
Puyan Lotfi68f29da2019-06-20 16:59:48 +0000299 }
300};
Benjamin Kramerdc5f8052019-08-23 19:59:23 +0000301} // namespace
Puyan Lotfi68f29da2019-06-20 16:59:48 +0000302
303std::unique_ptr<ASTConsumer>
Puyan Lotfid2418452019-08-22 23:29:22 +0000304GenerateInterfaceIfsExpV1Action::CreateASTConsumer(CompilerInstance &CI,
305 StringRef InFile) {
306 return std::make_unique<InterfaceStubFunctionsConsumer>(
307 CI, InFile, "experimental-ifs-v1");
308}