Parser.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //===- Parser.cpp - Main dispatch module for the Parser library -----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This library implements the functionality defined in llvm/AsmParser/Parser.h
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/AsmParser/Parser.h"
  13. #include "llvm/AsmParser/LLParser.h"
  14. #include "llvm/IR/Module.h"
  15. #include "llvm/IR/ModuleSummaryIndex.h"
  16. #include "llvm/Support/MemoryBuffer.h"
  17. #include "llvm/Support/SourceMgr.h"
  18. #include <system_error>
  19. using namespace llvm;
  20. static bool parseAssemblyInto(MemoryBufferRef F, Module *M,
  21. ModuleSummaryIndex *Index, SMDiagnostic &Err,
  22. SlotMapping *Slots, bool UpgradeDebugInfo,
  23. DataLayoutCallbackTy DataLayoutCallback) {
  24. SourceMgr SM;
  25. std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
  26. SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
  27. LLVMContext Context;
  28. return LLParser(F.getBuffer(), SM, Err, M, Index,
  29. M ? M->getContext() : Context, Slots)
  30. .Run(UpgradeDebugInfo, DataLayoutCallback);
  31. }
  32. bool llvm::parseAssemblyInto(MemoryBufferRef F, Module *M,
  33. ModuleSummaryIndex *Index, SMDiagnostic &Err,
  34. SlotMapping *Slots,
  35. DataLayoutCallbackTy DataLayoutCallback) {
  36. return ::parseAssemblyInto(F, M, Index, Err, Slots,
  37. /*UpgradeDebugInfo*/ true, DataLayoutCallback);
  38. }
  39. std::unique_ptr<Module>
  40. llvm::parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
  41. SlotMapping *Slots,
  42. DataLayoutCallbackTy DataLayoutCallback) {
  43. std::unique_ptr<Module> M =
  44. std::make_unique<Module>(F.getBufferIdentifier(), Context);
  45. if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, DataLayoutCallback))
  46. return nullptr;
  47. return M;
  48. }
  49. std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
  50. SMDiagnostic &Err,
  51. LLVMContext &Context,
  52. SlotMapping *Slots) {
  53. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  54. MemoryBuffer::getFileOrSTDIN(Filename);
  55. if (std::error_code EC = FileOrErr.getError()) {
  56. Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
  57. "Could not open input file: " + EC.message());
  58. return nullptr;
  59. }
  60. return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots);
  61. }
  62. static ParsedModuleAndIndex
  63. parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err,
  64. LLVMContext &Context, SlotMapping *Slots,
  65. bool UpgradeDebugInfo,
  66. DataLayoutCallbackTy DataLayoutCallback) {
  67. std::unique_ptr<Module> M =
  68. std::make_unique<Module>(F.getBufferIdentifier(), Context);
  69. std::unique_ptr<ModuleSummaryIndex> Index =
  70. std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
  71. if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
  72. DataLayoutCallback))
  73. return {nullptr, nullptr};
  74. return {std::move(M), std::move(Index)};
  75. }
  76. ParsedModuleAndIndex llvm::parseAssemblyWithIndex(MemoryBufferRef F,
  77. SMDiagnostic &Err,
  78. LLVMContext &Context,
  79. SlotMapping *Slots) {
  80. return ::parseAssemblyWithIndex(
  81. F, Err, Context, Slots,
  82. /*UpgradeDebugInfo*/ true,
  83. [](StringRef, StringRef) { return std::nullopt; });
  84. }
  85. static ParsedModuleAndIndex
  86. parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Err,
  87. LLVMContext &Context, SlotMapping *Slots,
  88. bool UpgradeDebugInfo,
  89. DataLayoutCallbackTy DataLayoutCallback) {
  90. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  91. MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
  92. if (std::error_code EC = FileOrErr.getError()) {
  93. Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
  94. "Could not open input file: " + EC.message());
  95. return {nullptr, nullptr};
  96. }
  97. return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
  98. Context, Slots, UpgradeDebugInfo,
  99. DataLayoutCallback);
  100. }
  101. ParsedModuleAndIndex
  102. llvm::parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Err,
  103. LLVMContext &Context, SlotMapping *Slots,
  104. DataLayoutCallbackTy DataLayoutCallback) {
  105. return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
  106. /*UpgradeDebugInfo*/ true,
  107. DataLayoutCallback);
  108. }
  109. ParsedModuleAndIndex llvm::parseAssemblyFileWithIndexNoUpgradeDebugInfo(
  110. StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
  111. SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback) {
  112. return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
  113. /*UpgradeDebugInfo*/ false,
  114. DataLayoutCallback);
  115. }
  116. std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
  117. SMDiagnostic &Err,
  118. LLVMContext &Context,
  119. SlotMapping *Slots) {
  120. MemoryBufferRef F(AsmString, "<string>");
  121. return parseAssembly(F, Err, Context, Slots);
  122. }
  123. static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F,
  124. ModuleSummaryIndex &Index,
  125. SMDiagnostic &Err) {
  126. SourceMgr SM;
  127. std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
  128. SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
  129. // The parser holds a reference to a context that is unused when parsing the
  130. // index, but we need to initialize it.
  131. LLVMContext unusedContext;
  132. return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext)
  133. .Run(true, [](StringRef, StringRef) { return std::nullopt; });
  134. }
  135. std::unique_ptr<ModuleSummaryIndex>
  136. llvm::parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err) {
  137. std::unique_ptr<ModuleSummaryIndex> Index =
  138. std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
  139. if (parseSummaryIndexAssemblyInto(F, *Index, Err))
  140. return nullptr;
  141. return Index;
  142. }
  143. std::unique_ptr<ModuleSummaryIndex>
  144. llvm::parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err) {
  145. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  146. MemoryBuffer::getFileOrSTDIN(Filename);
  147. if (std::error_code EC = FileOrErr.getError()) {
  148. Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
  149. "Could not open input file: " + EC.message());
  150. return nullptr;
  151. }
  152. return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
  153. }
  154. std::unique_ptr<ModuleSummaryIndex>
  155. llvm::parseSummaryIndexAssemblyString(StringRef AsmString, SMDiagnostic &Err) {
  156. MemoryBufferRef F(AsmString, "<string>");
  157. return parseSummaryIndexAssembly(F, Err);
  158. }
  159. Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err,
  160. const Module &M, const SlotMapping *Slots) {
  161. SourceMgr SM;
  162. std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
  163. SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
  164. Constant *C;
  165. if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
  166. .parseStandaloneConstantValue(C, Slots))
  167. return nullptr;
  168. return C;
  169. }
  170. Type *llvm::parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
  171. const SlotMapping *Slots) {
  172. unsigned Read;
  173. Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
  174. if (!Ty)
  175. return nullptr;
  176. if (Read != Asm.size()) {
  177. SourceMgr SM;
  178. std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
  179. SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
  180. Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
  181. SourceMgr::DK_Error, "expected end of string");
  182. return nullptr;
  183. }
  184. return Ty;
  185. }
  186. Type *llvm::parseTypeAtBeginning(StringRef Asm, unsigned &Read,
  187. SMDiagnostic &Err, const Module &M,
  188. const SlotMapping *Slots) {
  189. SourceMgr SM;
  190. std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
  191. SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
  192. Type *Ty;
  193. if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
  194. .parseTypeAtBeginning(Ty, Read, Slots))
  195. return nullptr;
  196. return Ty;
  197. }