Parser.cpp 8.8 KB

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