LLVM 23.0.0git
RecordsSlice.cpp
Go to the documentation of this file.
1//===- RecordsSlice.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// Implements the Records Slice APIs.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/SetVector.h"
16#include "llvm/TextAPI/Record.h"
17#include "llvm/TextAPI/Symbol.h"
18#include <utility>
19
20using namespace llvm;
21using namespace llvm::MachO;
22
25 // Find a specific Record type to capture.
26 auto [APIName, SymKind, InterfaceType] = parseSymbol(Name);
27 Name = APIName;
28 switch (SymKind) {
30 return addGlobal(Name, Linkage, GV, Flags);
32 return addObjCInterface(Name, Linkage, InterfaceType);
34 ObjCInterfaceRecord *Rec = addObjCInterface(Name, Linkage, InterfaceType);
35 // When classes without ehtype are used in try/catch blocks
36 // a weak-defined symbol is exported.
38 updateFlags(Rec, SymbolFlags::WeakDefined);
39 return Rec;
40 }
42 auto [Super, IVar] = Name.split('.');
43 // Attempt to find super class.
44 ObjCContainerRecord *Container = findContainer(/*isIVar=*/false, Super);
45 // If not found, create extension since there is no mapped class symbol.
46 if (Container == nullptr)
47 Container = addObjCCategory(Super, {});
48 return addObjCIVar(Container, IVar, Linkage);
49 }
50 }
51
52 llvm_unreachable("unexpected symbol kind when adding to Record Slice");
53}
54
56 StringRef Name) const {
57 StringRef Super = IsIVar ? Name.split('.').first : Name;
58 ObjCContainerRecord *Container = findObjCInterface(Super);
59 // Ivars can only exist with extensions, if they did not come from
60 // class.
61 if (Container == nullptr)
62 Container = findObjCCategory(Super, "");
63 return Container;
64}
65
66template <typename R, typename C = RecordMap<R>, typename K = StringRef>
67R *findRecord(K Key, const C &Container) {
68 const auto *Record = Container.find(Key);
69 if (Record == Container.end())
70 return nullptr;
71 return Record->second.get();
72}
73
75 GlobalRecord::Kind GV) const {
76 auto *Record = findRecord<GlobalRecord>(Name, Globals);
77 if (!Record)
78 return nullptr;
79
80 switch (GV) {
82 if (!Record->isVariable())
83 return nullptr;
84 break;
85 }
87 if (!Record->isFunction())
88 return nullptr;
89 break;
90 }
92 return Record;
93 }
94
95 return Record;
96}
97
100 assert(CurrType <= ObjCIFSymbolKind::EHType &&
101 "expected single ObjCIFSymbolKind enum value");
102 if (CurrType == ObjCIFSymbolKind::Class)
103 return Linkages.Class;
104
105 if (CurrType == ObjCIFSymbolKind::MetaClass)
106 return Linkages.MetaClass;
107
108 if (CurrType == ObjCIFSymbolKind::EHType)
109 return Linkages.EHType;
110
111 llvm_unreachable("unexpected ObjCIFSymbolKind");
112}
113
115 RecordLinkage Link) {
117 Linkages.Class = std::max(Link, Linkages.Class);
119 Linkages.MetaClass = std::max(Link, Linkages.MetaClass);
121 Linkages.EHType = std::max(Link, Linkages.EHType);
122
123 // Obj-C Classes represent multiple symbols that could have competing
124 // linkages, in this case assign the largest one, when querying the linkage of
125 // the record itself. This allows visitors pick whether they want to account
126 // for complete symbol information.
127 Linkage = std::max({Linkages.Class, Linkages.MetaClass, Linkages.EHType});
128}
129
133
135 StringRef Category) const {
136 return findRecord<ObjCCategoryRecord>(std::make_pair(ClassToExtend, Category),
137 Categories);
138}
139
143
145 StringRef Name) const {
146 // If scoped name, the name of the container is known.
147 if (IsScopedName) {
148 // IVar does not exist if there is not a container assigned to it.
149 auto *Container = findContainer(/*IsIVar=*/true, Name);
150 if (!Container)
151 return nullptr;
152
153 StringRef IVar = Name.substr(Name.find_first_of('.') + 1);
154 return Container->findObjCIVar(IVar);
155 }
156
157 // Otherwise traverse through containers and attempt to find IVar.
158 auto getIVar = [Name](auto &Records) -> ObjCIVarRecord * {
159 for (const auto &[_, Container] : Records) {
160 if (auto *IVarR = Container->findObjCIVar(Name))
161 return IVarR;
162 }
163 return nullptr;
164 };
165
166 if (auto *IVarRecord = getIVar(Classes))
167 return IVarRecord;
168
169 return getIVar(Categories);
170}
171
174 bool Inlined) {
176 Flags |= SymbolFlags::Text;
177 else if (GV == GlobalRecord::Kind::Variable)
178 Flags |= SymbolFlags::Data;
179
180 Name = copyString(Name);
181 auto Result = Globals.try_emplace(Name);
182 if (Result.second)
183 Result.first->second =
184 std::make_unique<GlobalRecord>(Name, Linkage, Flags, GV, Inlined);
185 else {
186 updateLinkage(Result.first->second.get(), Linkage);
187 updateFlags(Result.first->second.get(), Flags);
188 }
189 return Result.first->second.get();
190}
191
193 RecordLinkage Linkage,
194 ObjCIFSymbolKind SymType) {
195 Name = copyString(Name);
196 auto Result = Classes.try_emplace(Name);
197 if (Result.second)
198 Result.first->second =
199 std::make_unique<ObjCInterfaceRecord>(Name, Linkage, SymType);
200 else
201 Result.first->second->updateLinkageForSymbols(SymType, Linkage);
202 return Result.first->second.get();
203}
204
205SymbolFlags Record::mergeFlags(SymbolFlags Flags, RecordLinkage Linkage) {
206 // Add Linkage properties into Flags.
207 switch (Linkage) {
209 Flags |= SymbolFlags::Rexported;
210 return Flags;
212 Flags |= SymbolFlags::Undefined;
213 return Flags;
214 default:
215 return Flags;
216 }
217}
218
220 auto Result = Categories.insert({Name, Record});
221 return Result.second;
222}
223
225 StringRef Category) {
226 Category = copyString(Category);
227 ClassToExtend = copyString(ClassToExtend);
228
229 // Add owning record first into record slice.
230 auto Result = Categories.try_emplace(std::make_pair(ClassToExtend, Category));
231 if (Result.second)
232 Result.first->second =
233 std::make_unique<ObjCCategoryRecord>(ClassToExtend, Category);
234
235 // Then add reference to it in in the class.
236 if (auto *ObjCClass = findObjCInterface(ClassToExtend))
237 ObjCClass->addObjCCategory(Result.first->second.get());
238
239 return Result.first->second.get();
240}
241
242std::vector<ObjCIVarRecord *> ObjCContainerRecord::getObjCIVars() const {
243 std::vector<ObjCIVarRecord *> Records;
244 Records.reserve(IVars.size());
245 for (const auto &Record : IVars)
246 Records.push_back(Record.second.get());
247 return Records;
248}
249
250std::vector<ObjCCategoryRecord *>
252 std::vector<ObjCCategoryRecord *> Records;
253 Records.reserve(Categories.size());
254 for (const auto &Record : Categories)
255 Records.push_back(Record.second);
256 return Records;
257}
258
261 auto Result = IVars.try_emplace(IVar);
262 if (Result.second)
263 Result.first->second = std::make_unique<ObjCIVarRecord>(IVar, Linkage);
264 return Result.first->second.get();
265}
266
268 StringRef Name,
269 RecordLinkage Linkage) {
270 Name = copyString(Name);
271 ObjCIVarRecord *Record = Container->addObjCIVar(Name, Linkage);
272 updateLinkage(Record, Linkage);
273 return Record;
274}
275
277 if (String.empty())
278 return {};
279
280 if (StringAllocator.identifyObject(String.data()))
281 return String;
282
283 void *Ptr = StringAllocator.Allocate(String.size(), 1);
284 memcpy(Ptr, String.data(), String.size());
285 return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
286}
287
289 if (!hasBinaryAttrs())
290 BA = std::make_unique<BinaryAttrs>();
291 return *BA;
292}
293
295 for (auto &G : Globals)
296 V.visitGlobal(*G.second);
297 for (auto &C : Classes)
298 V.visitObjCInterface(*C.second);
299 for (auto &Cat : Categories)
300 V.visitObjCCategory(*Cat.second);
301}
302
303static std::unique_ptr<InterfaceFile>
304createInterfaceFile(const Records &Slices, StringRef InstallName) {
305 // Pickup symbols first.
306 auto Symbols = std::make_unique<SymbolSet>();
307 for (auto &S : Slices) {
308 if (S->empty())
309 continue;
310 auto &BA = S->getBinaryAttrs();
311 if (BA.InstallName != InstallName)
312 continue;
313
314 SymbolConverter Converter(Symbols.get(), S->getTarget(),
315 !BA.TwoLevelNamespace);
316 S->visit(Converter);
317 }
318
319 auto File = std::make_unique<InterfaceFile>(std::move(Symbols));
320 File->setInstallName(InstallName);
321 // Assign other attributes.
322 for (auto &S : Slices) {
323 if (S->empty())
324 continue;
325 auto &BA = S->getBinaryAttrs();
326 if (BA.InstallName != InstallName)
327 continue;
328 const Target &Targ = S->getTarget();
329 File->addTarget(Targ);
330 File->setFromBinaryAttrs(BA, Targ);
331 }
332
333 return File;
334}
335
336std::unique_ptr<InterfaceFile>
338 std::unique_ptr<InterfaceFile> File;
339 if (Slices.empty())
340 return File;
341
342 SetVector<StringRef> InstallNames;
343 for (auto &S : Slices) {
344 auto Name = S->getBinaryAttrs().InstallName;
345 if (Name.empty())
346 continue;
347 InstallNames.insert(Name);
348 }
349
350 File = createInterfaceFile(Slices, *InstallNames.begin());
351 for (StringRef IN : llvm::drop_begin(InstallNames))
352 File->addDocument(createInterfaceFile(Slices, IN));
353
354 return File;
355}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Early If Converter
#define _
#define G(x, y, z)
Definition MD5.cpp:55
static std::unique_ptr< InterfaceFile > createInterfaceFile(const Records &Slices, StringRef InstallName)
R * findRecord(K Key, const C &Container)
Implements the TAPI Record Collection Type.
This file implements a set that has insertion order iteration characteristics.
Implements the TAPI Record Types.
LLVM_ABI ObjCIVarRecord * addObjCIVar(StringRef IVar, RecordLinkage Linkage)
LLVM_ABI ObjCIVarRecord * findObjCIVar(StringRef IVar) const
LLVM_ABI std::vector< ObjCIVarRecord * > getObjCIVars() const
LLVM_ABI RecordLinkage getLinkageForSymbol(ObjCIFSymbolKind CurrType) const
LLVM_ABI std::vector< ObjCCategoryRecord * > getObjCCategories() const
LLVM_ABI bool addObjCCategory(ObjCCategoryRecord *Record)
LLVM_ABI void updateLinkageForSymbols(ObjCIFSymbolKind SymType, RecordLinkage Link)
Base class for any usage of traversing over collected Records.
Define Record.
Definition Record.h:68
RecordLinkage Linkage
Definition Record.h:112
StringRef Name
Definition Record.h:111
LLVM_ABI StringRef copyString(StringRef String)
Store any strings owned by RecordSlice into allocator and return back reference to that.
LLVM_ABI ObjCIVarRecord * findObjCIVar(bool IsScopedName, StringRef Name) const
Find ObjC instance variable.
LLVM_ABI ObjCCategoryRecord * addObjCCategory(StringRef ClassToExtend, StringRef Category)
Add ObjC Category record.
LLVM_ABI ObjCInterfaceRecord * addObjCInterface(StringRef Name, RecordLinkage Linkage, ObjCIFSymbolKind SymType)
Add ObjC Class record.
LLVM_ABI ObjCContainerRecord * findContainer(bool IsIVar, StringRef Name) const
Find ObjC Container.
LLVM_ABI ObjCIVarRecord * addObjCIVar(ObjCContainerRecord *Container, StringRef Name, RecordLinkage Linkage)
Add ObjC IVar record.
LLVM_ABI GlobalRecord * addGlobal(StringRef Name, RecordLinkage Linkage, GlobalRecord::Kind GV, SymbolFlags Flags=SymbolFlags::None, bool Inlined=false)
Add non-ObjC global record.
LLVM_ABI GlobalRecord * findGlobal(StringRef Name, GlobalRecord::Kind GV=GlobalRecord::Kind::Unknown) const
Find non-objc global.
LLVM_ABI ObjCCategoryRecord * findObjCCategory(StringRef ClassToExtend, StringRef Category) const
Find ObjC Category.
LLVM_ABI BinaryAttrs & getBinaryAttrs()
Return reference to BinaryAttrs.
LLVM_ABI ObjCInterfaceRecord * findObjCInterface(StringRef Name) const
Find ObjC Class.
LLVM_ABI Record * addRecord(StringRef Name, SymbolFlags Flags, GlobalRecord::Kind GV=GlobalRecord::Kind::Unknown, RecordLinkage Linkage=RecordLinkage::Unknown)
Add unspecified record to slice.
LLVM_ABI void visit(RecordVisitor &V) const
Specialized RecordVisitor for collecting exported symbols and undefined symbols if RecordSlice being ...
A vector that has set insertion semantics.
Definition SetVector.h:57
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition SetVector.h:106
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Target - Wrapper for Target specific information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI std::unique_ptr< InterfaceFile > convertToInterfaceFile(const Records &Slices)
ObjCIFSymbolKind
ObjC Interface symbol mappings.
Definition Symbol.h:70
@ EHType
Is OBJC_EHTYPE* symbol.
Definition Symbol.h:77
@ Class
Is OBJC_CLASS* symbol.
Definition Symbol.h:73
@ MetaClass
Is OBJC_METACLASS* symbol.
Definition Symbol.h:75
LLVM_ABI SimpleSymbol parseSymbol(StringRef SymName)
Get symbol classification by parsing the name of a symbol.
Definition Symbol.cpp:75
llvm::SmallVector< std::shared_ptr< RecordsSlice >, 4 > Records
SymbolFlags
Symbol flags.
Definition Symbol.h:25
@ Rexported
Rexported.
Definition Symbol.h:42
@ WeakDefined
Weak defined symbol.
Definition Symbol.h:33
@ Text
Text Segment.
Definition Symbol.h:48
@ Undefined
Undefined.
Definition Symbol.h:39
@ Data
Data Segment.
Definition Symbol.h:45
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key