[C++11] Replace OwningPtr::take() with OwningPtr::release().
[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, 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 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() || GV.hasLinkerPrivateLinkage() ||
114       GV.hasLinkerPrivateWeakLinkage())
115     Res |= BasicSymbolRef::SF_FormatSpecific;
116   if (!GV.hasLocalLinkage())
117     Res |= BasicSymbolRef::SF_Global;
118   if (GV.hasCommonLinkage())
119     Res |= BasicSymbolRef::SF_Common;
120   if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
121     Res |= BasicSymbolRef::SF_Weak;
122
123   return Res;
124 }
125
126 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
127   const GlobalValue &GV = getGV(Symb);
128   return GV;
129 }
130
131 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
132   Module::const_iterator I = M->begin();
133   DataRefImpl Ret;
134   Ret.p = skipEmpty(I, *M);
135   return basic_symbol_iterator(BasicSymbolRef(Ret, this));
136 }
137
138 basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
139   DataRefImpl Ret;
140   Ret.p = 3;
141   return basic_symbol_iterator(BasicSymbolRef(Ret, this));
142 }
143
144 ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
145     MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
146   error_code EC;
147   OwningPtr<IRObjectFile> Ret(
148       new IRObjectFile(Object, EC, Context, BufferOwned));
149   if (EC)
150     return EC;
151   return Ret.release();
152 }