rename "SkipToWord" to "SkipToFourByteBoundary" since a word is not always 4 bytes.
[oota-llvm.git] / include / llvm / Linker.h
1 //===- llvm/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 // This file defines the interface to the module/file/archive linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LINKER_H
15 #define LLVM_LINKER_H
16
17 #include <memory>
18 #include <string>
19 #include <vector>
20
21 namespace llvm {
22   namespace sys { class Path; }
23
24 class Module;
25 class LLVMContext;
26 class StringRef;
27
28 /// This class provides the core functionality of linking in LLVM. It retains a
29 /// Module object which is the composite of the modules and libraries linked
30 /// into it. The composite Module can be retrieved via the getModule() method.
31 /// In this case the Linker still retains ownership of the Module. If the
32 /// releaseModule() method is used, the ownership of the Module is transferred
33 /// to the caller and the Linker object is only suitable for destruction.
34 /// The Linker can link Modules from memory, bitcode files, or bitcode
35 /// archives.  It retains a set of search paths in which to find any libraries
36 /// presented to it. By default, the linker will generate error and warning
37 /// messages to stderr but this capability can be turned off with the
38 /// QuietWarnings and QuietErrors flags. It can also be instructed to verbosely
39 /// print out the linking actions it is taking with the Verbose flag.
40 /// @brief The LLVM Linker.
41 class Linker {
42
43   /// @name Types
44   /// @{
45   public:
46     /// This enumeration is used to control various optional features of the
47     /// linker.
48     enum ControlFlags {
49       Verbose       = 1, ///< Print to stderr what steps the linker is taking
50       QuietWarnings = 2, ///< Don't print warnings to stderr.
51       QuietErrors   = 4  ///< Don't print errors to stderr.
52     };
53   
54     enum LinkerMode {
55       DestroySource = 0, // Allow source module to be destroyed.
56       PreserveSource = 1 // Preserve the source module.
57     };
58   
59   /// @}
60   /// @name Constructors
61   /// @{
62   public:
63     /// Construct the Linker with an empty module which will be given the
64     /// name \p progname. \p progname will also be used for error messages.
65     /// @brief Construct with empty module
66     Linker(StringRef progname, ///< name of tool running linker
67            StringRef modulename, ///< name of linker's end-result module
68            LLVMContext &C, ///< Context for global info
69            unsigned Flags = 0  ///< ControlFlags (one or more |'d together)
70     );
71
72     /// Construct the Linker with a previously defined module, \p aModule. Use
73     /// \p progname for the name of the program in error messages.
74     /// @brief Construct with existing module
75     Linker(StringRef progname, Module* aModule, unsigned Flags = 0);
76
77     /// Destruct the Linker.
78     /// @brief Destructor
79     ~Linker();
80
81   /// @}
82   /// @name Accessors
83   /// @{
84   public:
85     /// This method gets the composite module into which linking is being
86     /// done. The Composite module starts out empty and accumulates modules
87     /// linked into it via the various LinkIn* methods. This method does not
88     /// release the Module to the caller. The Linker retains ownership and will
89     /// destruct the Module when the Linker is destructed.
90     /// @see releaseModule
91     /// @brief Get the linked/composite module.
92     Module* getModule() const { return Composite; }
93
94     /// This method releases the composite Module into which linking is being
95     /// done. Ownership of the composite Module is transferred to the caller who
96     /// must arrange for its destruct. After this method is called, the Linker
97     /// terminates the linking session for the returned Module. It will no
98     /// longer utilize the returned Module but instead resets itself for
99     /// subsequent linking as if the constructor had been called. The Linker's
100     /// LibPaths and flags to be reset, and memory will be released.
101     /// @brief Release the linked/composite module.
102     Module* releaseModule();
103
104     /// This method gets the list of libraries that form the path that the
105     /// Linker will search when it is presented with a library name.
106     /// @brief Get the Linkers library path
107     const std::vector<sys::Path>& getLibPaths() const { return LibPaths; }
108
109     /// This method returns an error string suitable for printing to the user.
110     /// The return value will be empty unless an error occurred in one of the
111     /// LinkIn* methods. In those cases, the LinkIn* methods will have returned
112     /// true, indicating an error occurred. At most one error is retained so
113     /// this function always returns the last error that occurred. Note that if
114     /// the Quiet control flag is not set, the error string will have already
115     /// been printed to stderr.
116     /// @brief Get the text of the last error that occurred.
117     const std::string &getLastError() const { return Error; }
118
119   /// @}
120   /// @name Mutators
121   /// @{
122   public:
123     /// Add a path to the list of paths that the Linker will search. The Linker
124     /// accumulates the set of libraries added
125     /// library paths for the target platform. The standard libraries will
126     /// always be searched last. The added libraries will be searched in the
127     /// order added.
128     /// @brief Add a path.
129     void addPath(const sys::Path& path);
130
131     /// Add a set of paths to the list of paths that the linker will search. The
132     /// Linker accumulates the set of libraries added. The \p paths will be
133     /// added to the end of the Linker's list. Order will be retained.
134     /// @brief Add a set of paths.
135     void addPaths(const std::vector<std::string>& paths);
136
137     /// This method augments the Linker's list of library paths with the system
138     /// paths of the host operating system, include LLVM_LIB_SEARCH_PATH.
139     /// @brief Add the system paths.
140     void addSystemPaths();
141
142     /// Control optional linker behavior by setting a group of flags. The flags
143     /// are defined in the ControlFlags enumeration.
144     /// @see ControlFlags
145     /// @brief Set control flags.
146     void setFlags(unsigned flags) { Flags = flags; }
147
148     /// This method links the \p Src module into the Linker's Composite module
149     /// by calling LinkModules.  All the other LinkIn* methods eventually
150     /// result in calling this method to link a Module into the Linker's
151     /// composite.
152     /// @see LinkModules
153     /// @returns True if an error occurs, false otherwise.
154     /// @brief Link in a module.
155     bool LinkInModule(
156       Module* Src,              ///< Module linked into \p Dest
157       std::string* ErrorMsg = 0 /// Error/diagnostic string
158     ) { 
159       return LinkModules(Composite, Src, Linker::DestroySource, ErrorMsg ); 
160     }
161
162     /// This is the heart of the linker. This method will take unconditional
163     /// control of the \p Src module and link it into the \p Dest module. The
164     /// \p Src module will be destructed or subsumed by this method. In either
165     /// case it is not usable by the caller after this method is invoked. Only
166     /// the \p Dest module will remain. The \p Src module is linked into the
167     /// Linker's composite module such that types, global variables, functions,
168     /// and etc. are matched and resolved.  If an error occurs, this function
169     /// returns true and ErrorMsg is set to a descriptive message about the
170     /// error.
171     /// @returns True if an error occurs, false otherwise.
172     /// @brief Generically link two modules together.
173     static bool LinkModules(Module* Dest, Module* Src, unsigned Mode,
174                             std::string* ErrorMsg);
175
176   /// @}
177   /// @name Implementation
178   /// @{
179   private:
180     bool warning(StringRef message);
181     bool error(StringRef message);
182     void verbose(StringRef message);
183
184   /// @}
185   /// @name Data
186   /// @{
187   private:
188     LLVMContext& Context; ///< The context for global information
189     Module* Composite; ///< The composite module linked together
190     std::vector<sys::Path> LibPaths; ///< The library search paths
191     unsigned Flags;    ///< Flags to control optional behavior.
192     std::string Error; ///< Text of error that occurred.
193     std::string ProgramName; ///< Name of the program being linked
194   /// @}
195
196 };
197
198 } // End llvm namespace
199
200 #endif