Parser.h 8.5 KB

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