xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Object/TapiUniversal.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- TapiUniversal.h - Text-based Dynamic Library Stub -------*- 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 // This file declares the TapiUniversal interface.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_OBJECT_TAPIUNIVERSAL_H
14 #define LLVM_OBJECT_TAPIUNIVERSAL_H
15 
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Object/Binary.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/MemoryBufferRef.h"
21 #include "llvm/TextAPI/Architecture.h"
22 #include "llvm/TextAPI/InterfaceFile.h"
23 
24 namespace llvm {
25 namespace object {
26 
27 class TapiFile;
28 
29 class LLVM_ABI TapiUniversal : public Binary {
30 public:
31   class ObjectForArch {
32     const TapiUniversal *Parent;
33     int Index;
34 
35   public:
ObjectForArch(const TapiUniversal * Parent,int Index)36     ObjectForArch(const TapiUniversal *Parent, int Index)
37         : Parent(Parent), Index(Index) {}
38 
getNext()39     ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
40 
41     bool operator==(const ObjectForArch &Other) const {
42       return (Parent == Other.Parent) && (Index == Other.Index);
43     }
44 
getCPUType()45     uint32_t getCPUType() const {
46       auto Result =
47           MachO::getCPUTypeFromArchitecture(Parent->Libraries[Index].Arch);
48       return Result.first;
49     }
50 
getCPUSubType()51     uint32_t getCPUSubType() const {
52       auto Result =
53           MachO::getCPUTypeFromArchitecture(Parent->Libraries[Index].Arch);
54       return Result.second;
55     }
56 
getArchFlagName()57     StringRef getArchFlagName() const {
58       return MachO::getArchitectureName(Parent->Libraries[Index].Arch);
59     }
60 
getInstallName()61     std::string getInstallName() const {
62       return std::string(Parent->Libraries[Index].InstallName);
63     }
64 
isTopLevelLib()65     bool isTopLevelLib() const {
66       return Parent->ParsedFile->getInstallName() == getInstallName();
67     }
68 
69     LLVM_ABI Expected<std::unique_ptr<TapiFile>> getAsObjectFile() const;
70   };
71 
72   class object_iterator {
73     ObjectForArch Obj;
74 
75   public:
object_iterator(const ObjectForArch & Obj)76     object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
77     const ObjectForArch *operator->() const { return &Obj; }
78     const ObjectForArch &operator*() const { return Obj; }
79 
80     bool operator==(const object_iterator &Other) const {
81       return Obj == Other.Obj;
82     }
83     bool operator!=(const object_iterator &Other) const {
84       return !(*this == Other);
85     }
86 
87     object_iterator &operator++() { // Preincrement
88       Obj = Obj.getNext();
89       return *this;
90     }
91   };
92 
93   TapiUniversal(MemoryBufferRef Source, Error &Err);
94   static Expected<std::unique_ptr<TapiUniversal>>
95   create(MemoryBufferRef Source);
96   ~TapiUniversal() override;
97 
begin_objects()98   object_iterator begin_objects() const { return ObjectForArch(this, 0); }
end_objects()99   object_iterator end_objects() const {
100     return ObjectForArch(this, Libraries.size());
101   }
102 
objects()103   iterator_range<object_iterator> objects() const {
104     return make_range(begin_objects(), end_objects());
105   }
106 
getInterfaceFile()107   const MachO::InterfaceFile &getInterfaceFile() { return *ParsedFile; }
108 
getNumberOfObjects()109   uint32_t getNumberOfObjects() const { return Libraries.size(); }
110 
classof(const Binary * v)111   static bool classof(const Binary *v) { return v->isTapiUniversal(); }
112 
113 private:
114   /// Attributes of a library that is inlined into a single TBD file.
115   struct Library {
116     const StringRef InstallName;
117     const MachO::Architecture Arch;
118     const std::optional<size_t> DocumentIdx;
119   };
120 
121   std::unique_ptr<MachO::InterfaceFile> ParsedFile;
122   std::vector<Library> Libraries;
123 };
124 
125 } // end namespace object.
126 } // end namespace llvm.
127 
128 #endif // LLVM_OBJECT_TAPIUNIVERSAL_H
129