[Linker] Kill Linker::LoadObject which is dead, and drop the BitReader dependency...
[oota-llvm.git] / lib / Linker / Linker.cpp
1 //===- lib/Linker/Linker.cpp - Basic Linker functionality  ----------------===//
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 contains basic Linker functionality that all usages will need.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Linker.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/system_error.h"
21 using namespace llvm;
22
23 Linker::Linker(StringRef progname, StringRef modname,
24                LLVMContext& C, unsigned flags):
25   Context(C),
26   Composite(new Module(modname, C)),
27   LibPaths(),
28   Flags(flags),
29   Error(),
30   ProgramName(progname) { }
31
32 Linker::Linker(StringRef progname, Module* aModule, unsigned flags) :
33   Context(aModule->getContext()),
34   Composite(aModule),
35   LibPaths(),
36   Flags(flags),
37   Error(),
38   ProgramName(progname) { }
39
40 Linker::~Linker() {
41   delete Composite;
42 }
43
44 bool
45 Linker::error(StringRef message) {
46   Error = message;
47   if (!(Flags&QuietErrors))
48     errs() << ProgramName << ": error: " << message << "\n";
49   return true;
50 }
51
52 bool
53 Linker::warning(StringRef message) {
54   Error = message;
55   if (!(Flags&QuietWarnings))
56     errs() << ProgramName << ": warning: " << message << "\n";
57   return false;
58 }
59
60 void
61 Linker::verbose(StringRef message) {
62   if (Flags&Verbose)
63     errs() << "  " << message << "\n";
64 }
65
66 void
67 Linker::addPath(const sys::Path& path) {
68   LibPaths.push_back(path);
69 }
70
71 void
72 Linker::addPaths(const std::vector<std::string>& paths) {
73   for (unsigned i = 0, e = paths.size(); i != e; ++i)
74     LibPaths.push_back(sys::Path(paths[i]));
75 }
76
77 void
78 Linker::addSystemPaths() {
79   sys::Path::GetBitcodeLibraryPaths(LibPaths);
80   LibPaths.insert(LibPaths.begin(),sys::Path("./"));
81 }
82
83 Module*
84 Linker::releaseModule() {
85   Module* result = Composite;
86   LibPaths.clear();
87   Error.clear();
88   Composite = 0;
89   Flags = 0;
90   return result;
91 }