Parser.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- Parser.h - Parser for LLVM IR text assembly files -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // These classes are implemented by the lib/AsmParser library.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ASMPARSER_PARSER_H
  18. #define LLVM_ASMPARSER_PARSER_H
  19. #include "llvm/ADT/STLFunctionalExtras.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include <memory>
  22. #include <optional>
  23. namespace llvm {
  24. class Constant;
  25. class LLVMContext;
  26. class MemoryBufferRef;
  27. class Module;
  28. class ModuleSummaryIndex;
  29. struct SlotMapping;
  30. class SMDiagnostic;
  31. class Type;
  32. typedef llvm::function_ref<std::optional<std::string>(StringRef, StringRef)>
  33. DataLayoutCallbackTy;
  34. /// This function is a main interface to the LLVM Assembly Parser. It parses
  35. /// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
  36. /// Module (intermediate representation) with the corresponding features. Note
  37. /// that this does not verify that the generated Module is valid, so you should
  38. /// run the verifier after parsing the file to check that it is okay.
  39. /// Parse LLVM Assembly from a file
  40. /// \param Filename The name of the file to parse
  41. /// \param Err Error result info.
  42. /// \param Context Context in which to allocate globals info.
  43. /// \param Slots The optional slot mapping that will be initialized during
  44. /// parsing.
  45. std::unique_ptr<Module> parseAssemblyFile(StringRef Filename, SMDiagnostic &Err,
  46. LLVMContext &Context,
  47. SlotMapping *Slots = nullptr);
  48. /// The function is a secondary interface to the LLVM Assembly Parser. It parses
  49. /// an ASCII string that (presumably) contains LLVM Assembly code. It returns a
  50. /// Module (intermediate representation) with the corresponding features. Note
  51. /// that this does not verify that the generated Module is valid, so you should
  52. /// run the verifier after parsing the file to check that it is okay.
  53. /// Parse LLVM Assembly from a string
  54. /// \param AsmString The string containing assembly
  55. /// \param Err Error result info.
  56. /// \param Context Context in which to allocate globals info.
  57. /// \param Slots The optional slot mapping that will be initialized during
  58. /// parsing.
  59. std::unique_ptr<Module> parseAssemblyString(StringRef AsmString,
  60. SMDiagnostic &Err,
  61. LLVMContext &Context,
  62. SlotMapping *Slots = nullptr);
  63. /// Holds the Module and ModuleSummaryIndex returned by the interfaces
  64. /// that parse both.
  65. struct ParsedModuleAndIndex {
  66. std::unique_ptr<Module> Mod;
  67. std::unique_ptr<ModuleSummaryIndex> Index;
  68. };
  69. /// This function is a main interface to the LLVM Assembly Parser. It parses
  70. /// an ASCII file that (presumably) contains LLVM Assembly code, including
  71. /// a module summary. It returns a Module (intermediate representation) and
  72. /// a ModuleSummaryIndex with the corresponding features. Note that this does
  73. /// not verify that the generated Module or Index are valid, so you should
  74. /// run the verifier after parsing the file to check that they are okay.
  75. /// Parse LLVM Assembly from a file
  76. /// \param Filename The name of the file to parse
  77. /// \param Err Error result info.
  78. /// \param Context Context in which to allocate globals info.
  79. /// \param Slots The optional slot mapping that will be initialized during
  80. /// parsing.
  81. /// \param DataLayoutCallback Override datalayout in the llvm assembly.
  82. ParsedModuleAndIndex parseAssemblyFileWithIndex(
  83. StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
  84. SlotMapping *Slots = nullptr,
  85. DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
  86. return std::nullopt;
  87. });
  88. /// Only for use in llvm-as for testing; this does not produce a valid module.
  89. ParsedModuleAndIndex parseAssemblyFileWithIndexNoUpgradeDebugInfo(
  90. StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
  91. SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback);
  92. /// This function is a main interface to the LLVM Assembly Parser. It parses
  93. /// an ASCII file that (presumably) contains LLVM Assembly code for a module
  94. /// summary. It returns a a ModuleSummaryIndex with the corresponding features.
  95. /// Note that this does not verify that the generated Index is valid, so you
  96. /// should run the verifier after parsing the file to check that it is okay.
  97. /// Parse LLVM Assembly Index from a file
  98. /// \param Filename The name of the file to parse
  99. /// \param Err Error result info.
  100. std::unique_ptr<ModuleSummaryIndex>
  101. parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err);
  102. /// The function is a secondary interface to the LLVM Assembly Parser. It parses
  103. /// an ASCII string that (presumably) contains LLVM Assembly code for a module
  104. /// summary. It returns a a ModuleSummaryIndex with the corresponding features.
  105. /// Note that this does not verify that the generated Index is valid, so you
  106. /// should run the verifier after parsing the file to check that it is okay.
  107. /// Parse LLVM Assembly from a string
  108. /// \param AsmString The string containing assembly
  109. /// \param Err Error result info.
  110. std::unique_ptr<ModuleSummaryIndex>
  111. parseSummaryIndexAssemblyString(StringRef AsmString, SMDiagnostic &Err);
  112. /// parseAssemblyFile and parseAssemblyString are wrappers around this function.
  113. /// Parse LLVM Assembly from a MemoryBuffer.
  114. /// \param F The MemoryBuffer containing assembly
  115. /// \param Err Error result info.
  116. /// \param Slots The optional slot mapping that will be initialized during
  117. /// parsing.
  118. /// \param DataLayoutCallback Override datalayout in the llvm assembly.
  119. std::unique_ptr<Module> parseAssembly(
  120. MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
  121. SlotMapping *Slots = nullptr,
  122. DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
  123. return std::nullopt;
  124. });
  125. /// Parse LLVM Assembly including the summary index from a MemoryBuffer.
  126. ///
  127. /// \param F The MemoryBuffer containing assembly with summary
  128. /// \param Err Error result info.
  129. /// \param Slots The optional slot mapping that will be initialized during
  130. /// parsing.
  131. ///
  132. /// parseAssemblyFileWithIndex is a wrapper around this function.
  133. ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F,
  134. SMDiagnostic &Err,
  135. LLVMContext &Context,
  136. SlotMapping *Slots = nullptr);
  137. /// Parse LLVM Assembly for summary index from a MemoryBuffer.
  138. ///
  139. /// \param F The MemoryBuffer containing assembly with summary
  140. /// \param Err Error result info.
  141. ///
  142. /// parseSummaryIndexAssemblyFile is a wrapper around this function.
  143. std::unique_ptr<ModuleSummaryIndex>
  144. parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err);
  145. /// This function is the low-level interface to the LLVM Assembly Parser.
  146. /// This is kept as an independent function instead of being inlined into
  147. /// parseAssembly for the convenience of interactive users that want to add
  148. /// recently parsed bits to an existing module.
  149. ///
  150. /// \param F The MemoryBuffer containing assembly
  151. /// \param M The module to add data to.
  152. /// \param Index The index to add data to.
  153. /// \param Err Error result info.
  154. /// \param Slots The optional slot mapping that will be initialized during
  155. /// parsing.
  156. /// \return true on error.
  157. /// \param DataLayoutCallback Override datalayout in the llvm assembly.
  158. bool parseAssemblyInto(
  159. MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err,
  160. SlotMapping *Slots = nullptr,
  161. DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
  162. return std::nullopt;
  163. });
  164. /// Parse a type and a constant value in the given string.
  165. ///
  166. /// The constant value can be any LLVM constant, including a constant
  167. /// expression.
  168. ///
  169. /// \param Slots The optional slot mapping that will restore the parsing state
  170. /// of the module.
  171. /// \return null on error.
  172. Constant *parseConstantValue(StringRef Asm, SMDiagnostic &Err, const Module &M,
  173. const SlotMapping *Slots = nullptr);
  174. /// Parse a type in the given string.
  175. ///
  176. /// \param Slots The optional slot mapping that will restore the parsing state
  177. /// of the module.
  178. /// \return null on error.
  179. Type *parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
  180. const SlotMapping *Slots = nullptr);
  181. /// Parse a string \p Asm that starts with a type.
  182. /// \p Read[out] gives the number of characters that have been read to parse
  183. /// the type in \p Asm.
  184. ///
  185. /// \param Slots The optional slot mapping that will restore the parsing state
  186. /// of the module.
  187. /// \return null on error.
  188. Type *parseTypeAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err,
  189. const Module &M, const SlotMapping *Slots = nullptr);
  190. } // End llvm namespace
  191. #endif
  192. #ifdef __GNUC__
  193. #pragma GCC diagnostic pop
  194. #endif