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