Don't own the buffer in object::Binary.
[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       Triple T = MachOObjectFile::getArch(Header.cputype, Header.cpusubtype);
58       return T.getArchName();
59     }
60
61     ErrorOr<std::unique_ptr<ObjectFile>> getAsObjectFile() const;
62
63     std::error_code getAsArchive(std::unique_ptr<Archive> &Result) const;
64   };
65
66   class object_iterator {
67     ObjectForArch Obj;
68   public:
69     object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
70     const ObjectForArch* operator->() const {
71       return &Obj;
72     }
73
74     bool operator==(const object_iterator &Other) const {
75       return Obj == Other.Obj;
76     }
77     bool operator!=(const object_iterator &Other) const {
78       return !(*this == Other);
79     }
80
81     object_iterator& operator++() {  // Preincrement
82       Obj = Obj.getNext();
83       return *this;
84     }
85   };
86
87   MachOUniversalBinary(MemoryBufferRef Souce, std::error_code &EC);
88   static ErrorOr<MachOUniversalBinary *> create(MemoryBufferRef Source);
89
90   object_iterator begin_objects() const {
91     return ObjectForArch(this, 0);
92   }
93   object_iterator end_objects() const {
94     return ObjectForArch(nullptr, 0);
95   }
96
97   uint32_t getNumberOfObjects() const { return NumberOfObjects; }
98
99   // Cast methods.
100   static inline bool classof(Binary const *V) {
101     return V->isMachOUniversalBinary();
102   }
103
104   ErrorOr<std::unique_ptr<ObjectFile>>
105   getObjectForArch(Triple::ArchType Arch) const;
106 };
107
108 }
109 }
110
111 #endif