Make it easier to pass a custom diagnostic handler to the IR linker.
[oota-llvm.git] / include / llvm / Linker / Linker.h
1 //===- Linker.h - Module Linker Interface -----------------------*- 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 #ifndef LLVM_LINKER_LINKER_H
11 #define LLVM_LINKER_LINKER_H
12
13 #include "llvm/ADT/SmallPtrSet.h"
14
15 #include <functional>
16
17 namespace llvm {
18 class DiagnosticInfo;
19 class Module;
20 class StructType;
21
22 /// This class provides the core functionality of linking in LLVM. It keeps a
23 /// pointer to the merged module so far. It doesn't take ownership of the
24 /// module since it is assumed that the user of this class will want to do
25 /// something with it after the linking.
26 class Linker {
27   public:
28     enum LinkerMode {
29       DestroySource = 0, // Allow source module to be destroyed.
30       PreserveSource = 1 // Preserve the source module.
31     };
32
33     typedef std::function<void(const DiagnosticInfo &)>
34         DiagnosticHandlerFunction;
35
36     Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler);
37     Linker(Module *M);
38     ~Linker();
39
40     Module *getModule() const { return Composite; }
41     void deleteModule();
42
43     /// \brief Link \p Src into the composite. The source is destroyed if
44     /// \p Mode is DestroySource and preserved if it is PreserveSource.
45     /// If \p ErrorMsg is not null, information about any error is written
46     /// to it.
47     /// Returns true on error.
48     bool linkInModule(Module *Src, unsigned Mode);
49     bool linkInModule(Module *Src) {
50       return linkInModule(Src, Linker::DestroySource);
51     }
52
53     static bool
54     LinkModules(Module *Dest, Module *Src, unsigned Mode,
55                 DiagnosticHandlerFunction DiagnosticHandler);
56
57     static bool
58     LinkModules(Module *Dest, Module *Src, unsigned Mode);
59
60
61   private:
62     Module *Composite;
63     SmallPtrSet<StructType*, 32> IdentifiedStructTypes;
64     DiagnosticHandlerFunction DiagnosticHandler;
65 };
66
67 } // End llvm namespace
68
69 #endif