[ADT] Remove a couple of the always inline attributes I added.
[oota-llvm.git] / include / llvm / Object / Binary.h
1 //===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the Binary class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_BINARY_H
15 #define LLVM_OBJECT_BINARY_H
16
17 #include "llvm/Object/Error.h"
18 #include "llvm/Support/ErrorOr.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/MemoryBuffer.h"
21
22 namespace llvm {
23
24 class LLVMContext;
25 class StringRef;
26
27 namespace object {
28
29 class Binary {
30 private:
31   Binary() = delete;
32   Binary(const Binary &other) = delete;
33
34   unsigned int TypeID;
35
36 protected:
37   MemoryBufferRef Data;
38
39   Binary(unsigned int Type, MemoryBufferRef Source);
40
41   enum {
42     ID_Archive,
43     ID_MachOUniversalBinary,
44     ID_COFFImportFile,
45     ID_IR, // LLVM IR
46
47     // Object and children.
48     ID_StartObjects,
49     ID_COFF,
50
51     ID_ELF32L, // ELF 32-bit, little endian
52     ID_ELF32B, // ELF 32-bit, big endian
53     ID_ELF64L, // ELF 64-bit, little endian
54     ID_ELF64B, // ELF 64-bit, big endian
55
56     ID_MachO32L, // MachO 32-bit, little endian
57     ID_MachO32B, // MachO 32-bit, big endian
58     ID_MachO64L, // MachO 64-bit, little endian
59     ID_MachO64B, // MachO 64-bit, big endian
60
61     ID_EndObjects
62   };
63
64   static inline unsigned int getELFType(bool isLE, bool is64Bits) {
65     if (isLE)
66       return is64Bits ? ID_ELF64L : ID_ELF32L;
67     else
68       return is64Bits ? ID_ELF64B : ID_ELF32B;
69   }
70
71   static unsigned int getMachOType(bool isLE, bool is64Bits) {
72     if (isLE)
73       return is64Bits ? ID_MachO64L : ID_MachO32L;
74     else
75       return is64Bits ? ID_MachO64B : ID_MachO32B;
76   }
77
78 public:
79   virtual ~Binary();
80
81   StringRef getData() const;
82   StringRef getFileName() const;
83   MemoryBufferRef getMemoryBufferRef() const;
84
85   // Cast methods.
86   unsigned int getType() const { return TypeID; }
87
88   // Convenience methods
89   bool isObject() const {
90     return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
91   }
92
93   bool isSymbolic() const {
94     return isIR() || isObject();
95   }
96
97   bool isArchive() const {
98     return TypeID == ID_Archive;
99   }
100
101   bool isMachOUniversalBinary() const {
102     return TypeID == ID_MachOUniversalBinary;
103   }
104
105   bool isELF() const {
106     return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
107   }
108
109   bool isMachO() const {
110     return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
111   }
112
113   bool isCOFF() const {
114     return TypeID == ID_COFF;
115   }
116
117   bool isCOFFImportFile() const {
118     return TypeID == ID_COFFImportFile;
119   }
120
121   bool isIR() const {
122     return TypeID == ID_IR;
123   }
124
125   bool isLittleEndian() const {
126     return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
127              TypeID == ID_MachO32B || TypeID == ID_MachO64B);
128   }
129 };
130
131 /// @brief Create a Binary from Source, autodetecting the file type.
132 ///
133 /// @param Source The data to create the Binary from.
134 ErrorOr<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
135                                               LLVMContext *Context = nullptr);
136
137 template <typename T> class OwningBinary {
138   std::unique_ptr<T> Bin;
139   std::unique_ptr<MemoryBuffer> Buf;
140
141 public:
142   OwningBinary();
143   OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
144   OwningBinary(OwningBinary<T>&& Other);
145   OwningBinary<T> &operator=(OwningBinary<T> &&Other);
146
147   std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
148
149   T* getBinary();
150   const T* getBinary() const;
151 };
152
153 template <typename T>
154 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
155                               std::unique_ptr<MemoryBuffer> Buf)
156     : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
157
158 template <typename T> OwningBinary<T>::OwningBinary() {}
159
160 template <typename T>
161 OwningBinary<T>::OwningBinary(OwningBinary &&Other)
162     : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
163
164 template <typename T>
165 OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
166   Bin = std::move(Other.Bin);
167   Buf = std::move(Other.Buf);
168   return *this;
169 }
170
171 template <typename T>
172 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
173 OwningBinary<T>::takeBinary() {
174   return std::make_pair(std::move(Bin), std::move(Buf));
175 }
176
177 template <typename T> T* OwningBinary<T>::getBinary() {
178   return Bin.get();
179 }
180
181 template <typename T> const T* OwningBinary<T>::getBinary() const {
182   return Bin.get();
183 }
184
185 ErrorOr<OwningBinary<Binary>> createBinary(StringRef Path);
186 }
187 }
188
189 #endif