xref: /freebsd/contrib/llvm-project/llvm/include/llvm/TextAPI/RecordsSlice.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- llvm/TextAPI/RecordSlice.h - TAPI RecordSlice ------------*- C++ -*-===//
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 /// \file
10 /// \brief Implements the TAPI Record Collection Type.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TEXTAPI_RECORDSLICE_H
15 #define LLVM_TEXTAPI_RECORDSLICE_H
16 
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/TextAPI/FileTypes.h"
20 #include "llvm/TextAPI/PackedVersion.h"
21 #include "llvm/TextAPI/Record.h"
22 #include "llvm/TextAPI/RecordVisitor.h"
23 
24 namespace llvm {
25 namespace MachO {
26 
27 // Define collection of records for a library that are tied to a darwin target
28 // triple.
29 class RecordsSlice {
30 public:
RecordsSlice(const llvm::Triple & T)31   RecordsSlice(const llvm::Triple &T) : TargetTriple(T), TAPITarget(T) {}
32   /// Get target triple.
getTriple()33   const llvm::Triple &getTriple() const { return TargetTriple; }
34   /// Get TAPI converted target.
getTarget()35   const Target &getTarget() const { return TAPITarget; }
36 
37   /// Add unspecified record to slice.
38   ///
39   /// Assign specific record type based on properties and symbol name.
40   ///
41   /// \param Name The name of symbol.
42   /// \param Flags The flags that describe attributes of the symbol.
43   /// \param GV The kind of global, if this represents a non obj-c global
44   /// symbol.
45   /// \param Linkage The linkage of symbol.
46   /// \return The non-owning pointer to added record in slice.
47   LLVM_ABI Record *
48   addRecord(StringRef Name, SymbolFlags Flags,
49             GlobalRecord::Kind GV = GlobalRecord::Kind::Unknown,
50             RecordLinkage Linkage = RecordLinkage::Unknown);
51 
52   /// Add non-ObjC global record.
53   ///
54   /// \param Name The name of symbol.
55   /// \param Linkage The linkage of symbol.
56   /// \param GV The kind of global.
57   /// \param Flags The flags that describe attributes of the symbol.
58   /// \param Inlined Whether declaration is inlined, only applicable to
59   /// functions.
60   /// \return The non-owning pointer to added record in slice.
61   LLVM_ABI GlobalRecord *addGlobal(StringRef Name, RecordLinkage Linkage,
62                                    GlobalRecord::Kind GV,
63                                    SymbolFlags Flags = SymbolFlags::None,
64                                    bool Inlined = false);
65 
66   /// Add ObjC Class record.
67   ///
68   /// \param Name The name of class, not symbol.
69   /// \param Linkage The linkage of symbol.
70   /// \param SymType The symbols this class represents.
71   /// \return The non-owning pointer to added record in slice.
72   LLVM_ABI ObjCInterfaceRecord *addObjCInterface(StringRef Name,
73                                                  RecordLinkage Linkage,
74                                                  ObjCIFSymbolKind SymType);
75 
76   /// Add ObjC IVar record.
77   ///
78   /// \param Container Owning pointer for instance variable.
79   /// \param Name The name of ivar, not symbol.
80   /// \param Linkage The linkage of symbol.
81   /// \return The non-owning pointer to added record in slice.
82   LLVM_ABI ObjCIVarRecord *addObjCIVar(ObjCContainerRecord *Container,
83                                        StringRef Name, RecordLinkage Linkage);
84 
85   /// Add ObjC Category record.
86   ///
87   /// \param ClassToExtend The name of class that is being extended by the
88   /// category, not symbol.
89   /// \param Category The name of category.
90   /// \return The non-owning pointer to added record in slice.
91   LLVM_ABI ObjCCategoryRecord *addObjCCategory(StringRef ClassToExtend,
92                                                StringRef Category);
93 
94   /// Find ObjC Class.
95   ///
96   /// \param Name name of class, not full symbol name.
97   /// \return The non-owning pointer to record in slice.
98   LLVM_ABI ObjCInterfaceRecord *findObjCInterface(StringRef Name) const;
99 
100   /// Find ObjC Category.
101   ///
102   /// \param ClassToExtend The name of class, not full symbol name.
103   /// \param Category The name of category.
104   /// \return The non-owning pointer to record in slice.
105   LLVM_ABI ObjCCategoryRecord *findObjCCategory(StringRef ClassToExtend,
106                                                 StringRef Category) const;
107 
108   /// Find ObjC Container. This is commonly used for assigning for looking up
109   /// instance variables that are assigned to either a category or class.
110   ///
111   /// \param IsIVar If true, the name is the name of the IVar, otherwise it will
112   /// be looked up as the name of the container.
113   /// \param Name Either the name of ivar or name of container.
114   /// \return The non-owning pointer to record in
115   /// slice.
116   LLVM_ABI ObjCContainerRecord *findContainer(bool IsIVar,
117                                               StringRef Name) const;
118 
119   /// Find ObjC instance variable.
120   ///
121   /// \param IsScopedName This is used to determine how to parse the name.
122   /// \param Name Either the full name of the symbol or just the ivar.
123   /// \return The non-owning pointer to record in slice.
124   LLVM_ABI ObjCIVarRecord *findObjCIVar(bool IsScopedName,
125                                         StringRef Name) const;
126 
127   /// Find non-objc global.
128   ///
129   /// \param Name The name of symbol.
130   /// \param GV The Kind of global to find.
131   /// \return The non-owning pointer to record in slice.
132   LLVM_ABI GlobalRecord *
133   findGlobal(StringRef Name,
134              GlobalRecord::Kind GV = GlobalRecord::Kind::Unknown) const;
135 
136   // Determine if library attributes were assigned.
hasBinaryAttrs()137   bool hasBinaryAttrs() const { return BA.get(); }
138 
139   // Determine if record slice is unassigned.
empty()140   bool empty() const {
141     return !hasBinaryAttrs() && Globals.empty() && Classes.empty() &&
142            Categories.empty();
143   }
144 
145   // Visit all records known to RecordsSlice.
146   LLVM_ABI void visit(RecordVisitor &V) const;
147 
148   struct BinaryAttrs {
149     std::vector<StringRef> AllowableClients;
150     std::vector<StringRef> RexportedLibraries;
151     std::vector<StringRef> RPaths;
152     StringRef ParentUmbrella;
153     StringRef InstallName;
154     StringRef UUID;
155     StringRef Path;
156     FileType File = FileType::Invalid;
157     llvm::MachO::PackedVersion CurrentVersion;
158     llvm::MachO::PackedVersion CompatVersion;
159     uint8_t SwiftABI = 0;
160     bool TwoLevelNamespace = false;
161     bool AppExtensionSafe = false;
162     bool OSLibNotForSharedCache = false;
163   };
164 
165   /// Return reference to BinaryAttrs.
166   LLVM_ABI BinaryAttrs &getBinaryAttrs();
167 
168   /// Store any strings owned by RecordSlice into allocator and return back
169   /// reference to that.
170   LLVM_ABI StringRef copyString(StringRef String);
171 
172 private:
173   const llvm::Triple TargetTriple;
174   // Hold tapi converted triple to avoid unecessary casts.
175   const Target TAPITarget;
176 
177   /// BumpPtrAllocator to store generated/copied strings.
178   llvm::BumpPtrAllocator StringAllocator;
179 
180   /// Promote linkage of requested record. It is no-op if linkage type is lower
181   /// than the current assignment.
182   ///
183   /// \param R The record to update.
184   /// \param L Linkage type to update to.
updateLinkage(Record * R,RecordLinkage L)185   void updateLinkage(Record *R, RecordLinkage L) {
186     R->Linkage = std::max(R->Linkage, L);
187   }
188 
189   /// Update set flags of requested record.
190   ///
191   /// \param R The record to update.
192   /// \param F Flags to update to.
updateFlags(Record * R,SymbolFlags F)193   void updateFlags(Record *R, SymbolFlags F) { R->Flags |= F; }
194 
195   RecordMap<GlobalRecord> Globals;
196   RecordMap<ObjCInterfaceRecord> Classes;
197   RecordMap<ObjCCategoryRecord, std::pair<StringRef, StringRef>> Categories;
198 
199   std::unique_ptr<BinaryAttrs> BA{nullptr};
200 };
201 
202 using Records = llvm::SmallVector<std::shared_ptr<RecordsSlice>, 4>;
203 class InterfaceFile;
204 LLVM_ABI std::unique_ptr<InterfaceFile>
205 convertToInterfaceFile(const Records &Slices);
206 
207 } // namespace MachO
208 } // namespace llvm
209 #endif // LLVM_TEXTAPI_RECORDSLICE_H
210