Be more specific about return type of MachOUniversalBinary::getObjectForArch
[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 MachOUniversalBinary : public Binary {
29   virtual void anchor();
30
31   uint32_t NumberOfObjects;
32 public:
33   class ObjectForArch {
34     const MachOUniversalBinary *Parent;
35     /// \brief Index of object in the universal binary.
36     uint32_t Index;
37     /// \brief Descriptor of the object.
38     MachO::fat_arch Header;
39
40   public:
41     ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
42
43     void clear() {
44       Parent = nullptr;
45       Index = 0;
46     }
47
48     bool operator==(const ObjectForArch &Other) const {
49       return (Parent == Other.Parent) && (Index == Other.Index);
50     }
51
52     ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
53     uint32_t getCPUType() const { return Header.cputype; }
54     std::string getArchTypeName() const {
55       Triple T = MachOObjectFile::getArch(Header.cputype, Header.cpusubtype);
56       return T.getArchName();
57     }
58
59     ErrorOr<std::unique_ptr<MachOObjectFile>> getAsObjectFile() const;
60
61     std::error_code getAsArchive(std::unique_ptr<Archive> &Result) const;
62   };
63
64   class object_iterator {
65     ObjectForArch Obj;
66   public:
67     object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
68     const ObjectForArch* operator->() const {
69       return &Obj;
70     }
71
72     bool operator==(const object_iterator &Other) const {
73       return Obj == Other.Obj;
74     }
75     bool operator!=(const object_iterator &Other) const {
76       return !(*this == Other);
77     }
78
79     object_iterator& operator++() {  // Preincrement
80       Obj = Obj.getNext();
81       return *this;
82     }
83   };
84
85   MachOUniversalBinary(MemoryBufferRef Souce, std::error_code &EC);
86   static ErrorOr<std::unique_ptr<MachOUniversalBinary>>
87   create(MemoryBufferRef Source);
88
89   object_iterator begin_objects() const {
90     return ObjectForArch(this, 0);
91   }
92   object_iterator end_objects() const {
93     return ObjectForArch(nullptr, 0);
94   }
95
96   uint32_t getNumberOfObjects() const { return NumberOfObjects; }
97
98   // Cast methods.
99   static inline bool classof(Binary const *V) {
100     return V->isMachOUniversalBinary();
101   }
102
103   ErrorOr<std::unique_ptr<MachOObjectFile>>
104   getObjectForArch(Triple::ArchType Arch) const;
105 };
106
107 }
108 }
109
110 #endif