Be a bit more consistent about using ErrorOr when constructing Binary objects.
[oota-llvm.git] / include / llvm / Object / Archive.h
1 //===- Archive.h - ar archive file format -----------------------*- 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 ar archive file format class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_ARCHIVE_H
15 #define LLVM_OBJECT_ARCHIVE_H
16
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Object/Binary.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/ErrorOr.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/MemoryBuffer.h"
23
24 namespace llvm {
25 namespace object {
26 struct ArchiveMemberHeader {
27   char Name[16];
28   char LastModified[12];
29   char UID[6];
30   char GID[6];
31   char AccessMode[8];
32   char Size[10]; ///< Size of data, not including header or padding.
33   char Terminator[2];
34
35   /// Get the name without looking up long names.
36   llvm::StringRef getName() const;
37
38   /// Members are not larger than 4GB.
39   uint32_t getSize() const;
40
41   sys::fs::perms getAccessMode() const;
42   sys::TimeValue getLastModified() const;
43   unsigned getUID() const;
44   unsigned getGID() const;
45 };
46
47 class Archive : public Binary {
48   virtual void anchor();
49 public:
50   class Child {
51     const Archive *Parent;
52     /// \brief Includes header but not padding byte.
53     StringRef Data;
54     /// \brief Offset from Data to the start of the file.
55     uint16_t StartOfFile;
56
57     const ArchiveMemberHeader *getHeader() const {
58       return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
59     }
60
61   public:
62     Child(const Archive *Parent, const char *Start);
63
64     bool operator ==(const Child &other) const {
65       assert(Parent == other.Parent);
66       return Data.begin() == other.Data.begin();
67     }
68
69     bool operator <(const Child &other) const {
70       return Data.begin() < other.Data.begin();
71     }
72
73     Child getNext() const;
74
75     error_code getName(StringRef &Result) const;
76     StringRef getRawName() const { return getHeader()->getName(); }
77     sys::TimeValue getLastModified() const {
78       return getHeader()->getLastModified();
79     }
80     unsigned getUID() const { return getHeader()->getUID(); }
81     unsigned getGID() const { return getHeader()->getGID(); }
82     sys::fs::perms getAccessMode() const {
83       return getHeader()->getAccessMode();
84     }
85     /// \return the size of the archive member without the header or padding.
86     uint64_t getSize() const { return Data.size() - StartOfFile; }
87
88     StringRef getBuffer() const {
89       return StringRef(Data.data() + StartOfFile, getSize());
90     }
91
92     error_code getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
93                                bool FullPath = false) const;
94
95     error_code getAsBinary(OwningPtr<Binary> &Result) const;
96   };
97
98   class child_iterator {
99     Child child;
100   public:
101     child_iterator() : child(Child(0, 0)) {}
102     child_iterator(const Child &c) : child(c) {}
103     const Child* operator->() const {
104       return &child;
105     }
106
107     bool operator==(const child_iterator &other) const {
108       return child == other.child;
109     }
110
111     bool operator!=(const child_iterator &other) const {
112       return !(*this == other);
113     }
114
115     bool operator <(const child_iterator &other) const {
116       return child < other.child;
117     }
118
119     child_iterator& operator++() {  // Preincrement
120       child = child.getNext();
121       return *this;
122     }
123   };
124
125   class Symbol {
126     const Archive *Parent;
127     uint32_t SymbolIndex;
128     uint32_t StringIndex; // Extra index to the string.
129
130   public:
131     bool operator ==(const Symbol &other) const {
132       return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
133     }
134
135     Symbol(const Archive *p, uint32_t symi, uint32_t stri)
136       : Parent(p)
137       , SymbolIndex(symi)
138       , StringIndex(stri) {}
139     error_code getName(StringRef &Result) const;
140     error_code getMember(child_iterator &Result) const;
141     Symbol getNext() const;
142   };
143
144   class symbol_iterator {
145     Symbol symbol;
146   public:
147     symbol_iterator(const Symbol &s) : symbol(s) {}
148     const Symbol *operator->() const {
149       return &symbol;
150     }
151
152     bool operator==(const symbol_iterator &other) const {
153       return symbol == other.symbol;
154     }
155
156     bool operator!=(const symbol_iterator &other) const {
157       return !(*this == other);
158     }
159
160     symbol_iterator& operator++() {  // Preincrement
161       symbol = symbol.getNext();
162       return *this;
163     }
164   };
165
166   Archive(MemoryBuffer *source, error_code &ec);
167   static ErrorOr<Archive *> create(MemoryBuffer *Source);
168
169   enum Kind {
170     K_GNU,
171     K_BSD,
172     K_COFF
173   };
174
175   Kind kind() const { 
176     return Format;
177   }
178
179   child_iterator child_begin(bool SkipInternal = true) const;
180   child_iterator child_end() const;
181
182   symbol_iterator symbol_begin() const;
183   symbol_iterator symbol_end() const;
184
185   // Cast methods.
186   static inline bool classof(Binary const *v) {
187     return v->isArchive();
188   }
189
190   // check if a symbol is in the archive
191   child_iterator findSym(StringRef name) const;
192
193   bool hasSymbolTable() const;
194
195 private:
196   child_iterator SymbolTable;
197   child_iterator StringTable;
198   child_iterator FirstRegular;
199   Kind Format;
200 };
201
202 }
203 }
204
205 #endif