ba02df90714011653ffbc7f91715349084360b6b
[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/OwningPtr.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Object/Binary.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 = 0;
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     error_code getAsObjectFile(OwningPtr<ObjectFile> &Result) const;
57   };
58
59   class object_iterator {
60     ObjectForArch Obj;
61   public:
62     object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
63     const ObjectForArch* operator->() const {
64       return &Obj;
65     }
66
67     bool operator==(const object_iterator &Other) const {
68       return Obj == Other.Obj;
69     }
70     bool operator!=(const object_iterator &Other) const {
71       return !(*this == Other);
72     }
73
74     object_iterator& operator++() {  // Preincrement
75       Obj = Obj.getNext();
76       return *this;
77     }
78   };
79
80   MachOUniversalBinary(MemoryBuffer *Source, error_code &ec);
81   static ErrorOr<MachOUniversalBinary*> create(MemoryBuffer *Source);
82
83   object_iterator begin_objects() const {
84     return ObjectForArch(this, 0);
85   }
86   object_iterator end_objects() const {
87     return ObjectForArch(0, 0);
88   }
89
90   uint32_t getNumberOfObjects() const { return NumberOfObjects; }
91
92   // Cast methods.
93   static inline bool classof(Binary const *V) {
94     return V->isMachOUniversalBinary();
95   }
96
97   error_code getObjectForArch(Triple::ArchType Arch,
98                               OwningPtr<ObjectFile> &Result) const;
99 };
100
101 }
102 }
103
104 #endif