[C++11] Expand and eliminate the LLVM_ENUM_INT_TYPE() macro
[oota-llvm.git] / include / llvm / Object / SymbolicFile.h
1 //===- SymbolicFile.h - Interface that only provides symbols ----*- 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 the SymbolicFile interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_SYMBOLIC_FILE_H
15 #define LLVM_OBJECT_SYMBOLIC_FILE_H
16
17 #include "llvm/Object/Binary.h"
18
19 namespace llvm {
20 namespace object {
21
22 union DataRefImpl {
23   // This entire union should probably be a
24   // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
25   struct {
26     uint32_t a, b;
27   } d;
28   uintptr_t p;
29   DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
30 };
31
32 inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) {
33   // Check bitwise identical. This is the only legal way to compare a union w/o
34   // knowing which member is in use.
35   return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
36 }
37
38 inline bool operator<(const DataRefImpl &a, const DataRefImpl &b) {
39   // Check bitwise identical. This is the only legal way to compare a union w/o
40   // knowing which member is in use.
41   return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
42 }
43
44 template <class content_type> class content_iterator {
45   content_type Current;
46
47 public:
48   content_iterator(content_type symb) : Current(symb) {}
49
50   const content_type *operator->() const { return &Current; }
51
52   const content_type &operator*() const { return Current; }
53
54   bool operator==(const content_iterator &other) const {
55     return Current == other.Current;
56   }
57
58   bool operator!=(const content_iterator &other) const {
59     return !(*this == other);
60   }
61
62   content_iterator &operator++() { // preincrement
63     Current.moveNext();
64     return *this;
65   }
66 };
67
68 class SymbolicFile;
69
70 /// This is a value type class that represents a single symbol in the list of
71 /// symbols in the object file.
72 class BasicSymbolRef {
73   DataRefImpl SymbolPimpl;
74   const SymbolicFile *OwningObject;
75
76 public:
77   // FIXME: should we add a SF_Text?
78   enum Flags : unsigned {
79     SF_None = 0,
80     SF_Undefined = 1U << 0,      // Symbol is defined in another object file
81     SF_Global = 1U << 1,         // Global symbol
82     SF_Weak = 1U << 2,           // Weak symbol
83     SF_Absolute = 1U << 3,       // Absolute symbol
84     SF_Common = 1U << 4,         // Symbol has common linkage
85     SF_FormatSpecific = 1U << 5  // Specific to the object file format
86                                  // (e.g. section symbols)
87   };
88
89   BasicSymbolRef() : OwningObject(NULL) { }
90   BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
91
92   bool operator==(const BasicSymbolRef &Other) const;
93   bool operator<(const BasicSymbolRef &Other) const;
94
95   void moveNext();
96
97   error_code printName(raw_ostream &OS) const;
98
99   /// Get symbol flags (bitwise OR of SymbolRef::Flags)
100   uint32_t getFlags() const;
101
102   DataRefImpl getRawDataRefImpl() const;
103   const SymbolicFile *getObject() const;
104 };
105
106 typedef content_iterator<BasicSymbolRef> basic_symbol_iterator;
107
108 const uint64_t UnknownAddressOrSize = ~0ULL;
109
110 class SymbolicFile : public Binary {
111 public:
112   virtual ~SymbolicFile();
113   SymbolicFile(unsigned int Type, MemoryBuffer *Source, bool BufferOwned);
114
115   // virtual interface.
116   virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
117
118   virtual error_code printSymbolName(raw_ostream &OS,
119                                      DataRefImpl Symb) const = 0;
120
121   virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
122
123   virtual basic_symbol_iterator symbol_begin_impl() const = 0;
124
125   virtual basic_symbol_iterator symbol_end_impl() const = 0;
126
127   // convenience wrappers.
128   basic_symbol_iterator symbol_begin() const {
129     return symbol_begin_impl();
130   }
131   basic_symbol_iterator symbol_end() const {
132     return symbol_end_impl();
133   }
134
135   // construction aux.
136   static ErrorOr<SymbolicFile *> createIRObjectFile(MemoryBuffer *Object,
137                                                     LLVMContext &Context,
138                                                     bool BufferOwned = true);
139
140   static ErrorOr<SymbolicFile *> createSymbolicFile(MemoryBuffer *Object,
141                                                     bool BufferOwned,
142                                                     sys::fs::file_magic Type,
143                                                     LLVMContext *Context);
144
145   static ErrorOr<SymbolicFile *> createSymbolicFile(MemoryBuffer *Object) {
146     return createSymbolicFile(Object, true, sys::fs::file_magic::unknown, 0);
147   }
148   static ErrorOr<SymbolicFile *> createSymbolicFile(StringRef ObjectPath);
149
150   static inline bool classof(const Binary *v) {
151     return v->isSymbolic();
152   }
153 };
154
155 inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
156                                       const SymbolicFile *Owner)
157     : SymbolPimpl(SymbolP), OwningObject(Owner) {}
158
159 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
160   return SymbolPimpl == Other.SymbolPimpl;
161 }
162
163 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
164   return SymbolPimpl < Other.SymbolPimpl;
165 }
166
167 inline void BasicSymbolRef::moveNext() {
168   return OwningObject->moveSymbolNext(SymbolPimpl);
169 }
170
171 inline error_code BasicSymbolRef::printName(raw_ostream &OS) const {
172   return OwningObject->printSymbolName(OS, SymbolPimpl);
173 }
174
175 inline uint32_t BasicSymbolRef::getFlags() const {
176   return OwningObject->getSymbolFlags(SymbolPimpl);
177 }
178
179 inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
180   return SymbolPimpl;
181 }
182
183 inline const SymbolicFile *BasicSymbolRef::getObject() const {
184   return OwningObject;
185 }
186
187 }
188 }
189
190 #endif