b12b17e1f5b5e4b30867cc09277261a2a72f41ac
[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/Object/Binary.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
20
21 namespace llvm {
22 namespace object {
23
24 class Archive : public Binary {
25 public:
26   class Child {
27     const Archive *Parent;
28     StringRef Data;
29
30   public:
31     Child(const Archive *p, StringRef d) : Parent(p), Data(d) {}
32
33     bool operator ==(const Child &other) const {
34       return (Parent == other.Parent) && (Data.begin() == other.Data.begin());
35     }
36
37     Child getNext() const;
38     error_code getName(StringRef &Result) const;
39     int getLastModified() const;
40     int getUID() const;
41     int getGID() const;
42     int getAccessMode() const;
43     ///! Return the size of the archive member without the header or padding.
44     uint64_t getSize() const;
45
46     MemoryBuffer *getBuffer() const;
47     error_code getAsBinary(OwningPtr<Binary> &Result) const;
48   };
49
50   class child_iterator {
51     Child child;
52   public:
53     child_iterator() : child(Child(0, StringRef())) {}
54     child_iterator(const Child &c) : child(c) {}
55     const Child* operator->() const {
56       return &child;
57     }
58
59     bool operator==(const child_iterator &other) const {
60       return child == other.child;
61     }
62
63     bool operator!=(const child_iterator &other) const {
64       return !(*this == other);
65     }
66
67     child_iterator& operator++() {  // Preincrement
68       child = child.getNext();
69       return *this;
70     }
71   };
72
73   class Symbol {
74     const Archive *Parent;
75     uint32_t SymbolIndex;
76     uint32_t StringIndex; // Extra index to the string.
77
78   public:
79     bool operator ==(const Symbol &other) const {
80       return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
81     }
82
83     Symbol(const Archive *p, uint32_t symi, uint32_t stri)
84       : Parent(p)
85       , SymbolIndex(symi)
86       , StringIndex(stri) {}
87     error_code getName(StringRef &Result) const;
88     error_code getMember(child_iterator &Result) const;
89     Symbol getNext() const;
90   };
91
92   class symbol_iterator {
93     Symbol symbol;
94   public:
95     symbol_iterator(const Symbol &s) : symbol(s) {}
96     const Symbol *operator->() const {
97       return &symbol;
98     }
99
100     bool operator==(const symbol_iterator &other) const {
101       return symbol == other.symbol;
102     }
103
104     bool operator!=(const symbol_iterator &other) const {
105       return !(*this == other);
106     }
107
108     symbol_iterator& operator++() {  // Preincrement
109       symbol = symbol.getNext();
110       return *this;
111     }
112   };
113
114   Archive(MemoryBuffer *source, error_code &ec);
115
116   child_iterator begin_children(bool skip_internal = true) const;
117   child_iterator end_children() const;
118
119   symbol_iterator begin_symbols() const;
120   symbol_iterator end_symbols() const;
121
122   // Cast methods.
123   static inline bool classof(Archive const *v) { return true; }
124   static inline bool classof(Binary const *v) {
125     return v->getType() == Binary::isArchive;
126   }
127
128 private:
129   child_iterator SymbolTable;
130   child_iterator StringTable;
131 };
132
133 }
134 }
135
136 #endif