Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
6.2 KiB

  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. #ifndef LLVM_LINKER_H
  10. #define LLVM_LINKER_H
  11. #include <memory>
  12. #include <string>
  13. #include <vector>
  14. namespace llvm {
  15. class Module;
  16. class LLVMContext;
  17. class StringRef;
  18. /// This class provides the core functionality of linking in LLVM. It retains a
  19. /// Module object which is the composite of the modules and libraries linked
  20. /// into it. The composite Module can be retrieved via the getModule() method.
  21. /// In this case the Linker still retains ownership of the Module. If the
  22. /// releaseModule() method is used, the ownership of the Module is transferred
  23. /// to the caller and the Linker object is only suitable for destruction.
  24. /// The Linker can link Modules from memory. By default, the linker
  25. /// will generate error and warning messages to stderr but this capability can
  26. /// be turned off with the QuietWarnings and QuietErrors flags. It can also be
  27. /// instructed to verbosely print out the linking actions it is taking with
  28. /// the Verbose flag.
  29. /// @brief The LLVM Linker.
  30. class Linker {
  31. /// @name Types
  32. /// @{
  33. public:
  34. /// This enumeration is used to control various optional features of the
  35. /// linker.
  36. enum ControlFlags {
  37. Verbose = 1, ///< Print to stderr what steps the linker is taking
  38. QuietWarnings = 2, ///< Don't print warnings to stderr.
  39. QuietErrors = 4 ///< Don't print errors to stderr.
  40. };
  41. enum LinkerMode {
  42. DestroySource = 0, // Allow source module to be destroyed.
  43. PreserveSource = 1 // Preserve the source module.
  44. };
  45. /// @}
  46. /// @name Constructors
  47. /// @{
  48. public:
  49. /// Construct the Linker with an empty module which will be given the
  50. /// name \p progname. \p progname will also be used for error messages.
  51. /// @brief Construct with empty module
  52. Linker(StringRef progname, ///< name of tool running linker
  53. StringRef modulename, ///< name of linker's end-result module
  54. LLVMContext &C, ///< Context for global info
  55. unsigned Flags = 0 ///< ControlFlags (one or more |'d together)
  56. );
  57. /// Construct the Linker with a previously defined module, \p aModule. Use
  58. /// \p progname for the name of the program in error messages.
  59. /// @brief Construct with existing module
  60. Linker(StringRef progname, Module* aModule, unsigned Flags = 0);
  61. /// Destruct the Linker.
  62. /// @brief Destructor
  63. ~Linker();
  64. /// @}
  65. /// @name Accessors
  66. /// @{
  67. public:
  68. /// This method gets the composite module into which linking is being
  69. /// done. The Composite module starts out empty and accumulates modules
  70. /// linked into it via the various LinkIn* methods. This method does not
  71. /// release the Module to the caller. The Linker retains ownership and will
  72. /// destruct the Module when the Linker is destructed.
  73. /// @see releaseModule
  74. /// @brief Get the linked/composite module.
  75. Module* getModule() const { return Composite; }
  76. /// This method releases the composite Module into which linking is being
  77. /// done. Ownership of the composite Module is transferred to the caller who
  78. /// must arrange for its destruct. After this method is called, the Linker
  79. /// terminates the linking session for the returned Module. It will no
  80. /// longer utilize the returned Module but instead resets itself for
  81. /// subsequent linking as if the constructor had been called.
  82. /// @brief Release the linked/composite module.
  83. Module* releaseModule();
  84. /// This method returns an error string suitable for printing to the user.
  85. /// The return value will be empty unless an error occurred in one of the
  86. /// LinkIn* methods. In those cases, the LinkIn* methods will have returned
  87. /// true, indicating an error occurred. At most one error is retained so
  88. /// this function always returns the last error that occurred. Note that if
  89. /// the Quiet control flag is not set, the error string will have already
  90. /// been printed to stderr.
  91. /// @brief Get the text of the last error that occurred.
  92. const std::string &getLastError() const { return Error; }
  93. /// @}
  94. /// @name Mutators
  95. /// @{
  96. public:
  97. /// This method links the \p Src module into the Linker's Composite module
  98. /// by calling LinkModules.
  99. /// @see LinkModules
  100. /// @returns True if an error occurs, false otherwise.
  101. /// @brief Link in a module.
  102. bool LinkInModule(
  103. Module* Src, ///< Module linked into \p Dest
  104. std::string* ErrorMsg = 0 /// Error/diagnostic string
  105. ) {
  106. return LinkModules(Composite, Src, Linker::DestroySource, ErrorMsg);
  107. }
  108. /// This is the heart of the linker. This method will take unconditional
  109. /// control of the \p Src module and link it into the \p Dest module. The
  110. /// \p Src module will be destructed or subsumed by this method. In either
  111. /// case it is not usable by the caller after this method is invoked. Only
  112. /// the \p Dest module will remain. The \p Src module is linked into the
  113. /// Linker's composite module such that types, global variables, functions,
  114. /// and etc. are matched and resolved. If an error occurs, this function
  115. /// returns true and ErrorMsg is set to a descriptive message about the
  116. /// error.
  117. /// @returns True if an error occurs, false otherwise.
  118. /// @brief Generically link two modules together.
  119. static bool LinkModules(Module* Dest, Module* Src, unsigned Mode,
  120. std::string* ErrorMsg);
  121. /// @}
  122. /// @name Implementation
  123. /// @{
  124. private:
  125. bool warning(StringRef message);
  126. bool error(StringRef message);
  127. void verbose(StringRef message);
  128. /// @}
  129. /// @name Data
  130. /// @{
  131. private:
  132. LLVMContext& Context; ///< The context for global information
  133. Module* Composite; ///< The composite module linked together
  134. unsigned Flags; ///< Flags to control optional behavior.
  135. std::string Error; ///< Text of error that occurred.
  136. std::string ProgramName; ///< Name of the program being linked
  137. /// @}
  138. };
  139. } // End llvm namespace
  140. #endif