Pass a unique_ptr<MemoryBuffer> to the constructors in the Binary hierarchy.
[oota-llvm.git] / include / llvm / Object / MachOUniversal.h
1 //===- MachOUniversal.h - Mach-O universal binaries -------------*- 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 Mach-O fat/universal binaries.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_MACHOUNIVERSAL_H
15 #define LLVM_OBJECT_MACHOUNIVERSAL_H
16
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Object/Binary.h"
20 #include "llvm/Object/Archive.h"
21 #include "llvm/Object/MachO.h"
22 #include "llvm/Support/ErrorOr.h"
23 #include "llvm/Support/MachO.h"
24
25 namespace llvm {
26 namespace object {
27
28 class ObjectFile;
29
30 class MachOUniversalBinary : public Binary {
31   virtual void anchor();
32
33   uint32_t NumberOfObjects;
34 public:
35   class ObjectForArch {
36     const MachOUniversalBinary *Parent;
37     /// \brief Index of object in the universal binary.
38     uint32_t Index;
39     /// \brief Descriptor of the object.
40     MachO::fat_arch Header;
41
42   public:
43     ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
44
45     void clear() {
46       Parent = nullptr;
47       Index = 0;
48     }
49
50     bool operator==(const ObjectForArch &Other) const {
51       return (Parent == Other.Parent) && (Index == Other.Index);
52     }
53
54     ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
55     uint32_t getCPUType() const { return Header.cputype; }
56     std::string getArchTypeName() const {
57       return Triple::getArchTypeName(MachOObjectFile::getArch(Header.cputype));
58     }
59
60     ErrorOr<std::unique_ptr<ObjectFile>> getAsObjectFile() const;
61
62     std::error_code getAsArchive(std::unique_ptr<Archive> &Result) const;
63   };
64
65   class object_iterator {
66     ObjectForArch Obj;
67   public:
68     object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
69     const ObjectForArch* operator->() const {
70       return &Obj;
71     }
72
73     bool operator==(const object_iterator &Other) const {
74       return Obj == Other.Obj;
75     }
76     bool operator!=(const object_iterator &Other) const {
77       return !(*this == Other);
78     }
79
80     object_iterator& operator++() {  // Preincrement
81       Obj = Obj.getNext();
82       return *this;
83     }
84   };
85
86   MachOUniversalBinary(std::unique_ptr<MemoryBuffer> Source,
87                        std::error_code &ec);
88   static ErrorOr<MachOUniversalBinary *>
89   create(std::unique_ptr<MemoryBuffer> Source);
90
91   object_iterator begin_objects() const {
92     return ObjectForArch(this, 0);
93   }
94   object_iterator end_objects() const {
95     return ObjectForArch(nullptr, 0);
96   }
97
98   uint32_t getNumberOfObjects() const { return NumberOfObjects; }
99
100   // Cast methods.
101   static inline bool classof(Binary const *V) {
102     return V->isMachOUniversalBinary();
103   }
104
105   ErrorOr<std::unique_ptr<ObjectFile>>
106   getObjectForArch(Triple::ArchType Arch) const;
107 };
108
109 }
110 }
111
112 #endif