Remove 'using std::errro_code' from lib.
[oota-llvm.git] / lib / Object / IRObjectFile.cpp
1 //===- IRObjectFile.cpp - IR object file implementation ---------*- 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 // Part of the IRObjectFile class implementation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Mangler.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Object/IRObjectFile.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21 using namespace object;
22
23 IRObjectFile::IRObjectFile(MemoryBuffer *Object, std::error_code &EC,
24                            LLVMContext &Context, bool BufferOwned)
25     : SymbolicFile(Binary::ID_IR, Object, BufferOwned) {
26   ErrorOr<Module*> MOrErr = parseBitcodeFile(Object, Context);
27   if ((EC = MOrErr.getError()))
28     return;
29
30   M.reset(MOrErr.get());
31
32   // If we have a DataLayout, setup a mangler.
33   const DataLayout *DL = M->getDataLayout();
34   if (!DL)
35     return;
36
37   Mang.reset(new Mangler(DL));
38 }
39
40 static const GlobalValue &getGV(DataRefImpl &Symb) {
41   return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
42 }
43
44 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
45   if (I == M.alias_end())
46     return 3;
47   const GlobalValue *GV = &*I;
48   return reinterpret_cast<uintptr_t>(GV) | 2;
49 }
50
51 static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
52   if (I == M.global_end())
53     return skipEmpty(M.alias_begin(), M);
54   const GlobalValue *GV = &*I;
55   return reinterpret_cast<uintptr_t>(GV) | 1;
56 }
57
58 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
59   if (I == M.end())
60     return skipEmpty(M.global_begin(), M);
61   const GlobalValue *GV = &*I;
62   return reinterpret_cast<uintptr_t>(GV) | 0;
63 }
64
65 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
66   const GlobalValue *GV = &getGV(Symb);
67   const Module &M = *GV->getParent();
68   uintptr_t Res;
69   switch (Symb.p & 3) {
70   case 0: {
71     Module::const_iterator Iter(static_cast<const Function*>(GV));
72     ++Iter;
73     Res = skipEmpty(Iter, M);
74     break;
75   }
76   case 1: {
77     Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
78     ++Iter;
79     Res = skipEmpty(Iter, M);
80     break;
81   }
82   case 2: {
83     Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
84     ++Iter;
85     Res = skipEmpty(Iter, M);
86     break;
87   }
88   case 3:
89     llvm_unreachable("Invalid symbol reference");
90   }
91
92   Symb.p = Res;
93 }
94
95 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
96                                               DataRefImpl Symb) const {
97   const GlobalValue &GV = getGV(Symb);
98
99   if (Mang)
100     Mang->getNameWithPrefix(OS, &GV, false);
101   else
102     OS << GV.getName();
103
104   return object_error::success;
105 }
106
107 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
108   const GlobalValue &GV = getGV(Symb);
109
110   uint32_t Res = BasicSymbolRef::SF_None;
111   if (GV.isDeclaration() || GV.hasAvailableExternallyLinkage())
112     Res |= BasicSymbolRef::SF_Undefined;
113   if (GV.hasPrivateLinkage())
114     Res |= BasicSymbolRef::SF_FormatSpecific;
115   if (!GV.hasLocalLinkage())
116     Res |= BasicSymbolRef::SF_Global;
117   if (GV.hasCommonLinkage())
118     Res |= BasicSymbolRef::SF_Common;
119   if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
120     Res |= BasicSymbolRef::SF_Weak;
121
122   return Res;
123 }
124
125 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
126   const GlobalValue &GV = getGV(Symb);
127   return GV;
128 }
129
130 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
131   Module::const_iterator I = M->begin();
132   DataRefImpl Ret;
133   Ret.p = skipEmpty(I, *M);
134   return basic_symbol_iterator(BasicSymbolRef(Ret, this));
135 }
136
137 basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
138   DataRefImpl Ret;
139   Ret.p = 3;
140   return basic_symbol_iterator(BasicSymbolRef(Ret, this));
141 }
142
143 ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
144     MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
145   std::error_code EC;
146   std::unique_ptr<IRObjectFile> Ret(
147       new IRObjectFile(Object, EC, Context, BufferOwned));
148   if (EC)
149     return EC;
150   return Ret.release();
151 }