Parser.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //===- Parser.cpp - Main dispatch module for the Parser library -----------===//
  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. //
  10. // This library implements the functionality defined in llvm/AsmParser/Parser.h
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/AsmParser/Parser.h"
  14. #include "LLParser.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. #include "llvm/Support/SourceMgr.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include <cstring>
  21. #include <system_error>
  22. using namespace llvm;
  23. bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err,
  24. SlotMapping *Slots) {
  25. SourceMgr SM;
  26. std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
  27. SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
  28. return LLParser(F.getBuffer(), SM, Err, &M, Slots).Run();
  29. }
  30. std::unique_ptr<Module> llvm::parseAssembly(MemoryBufferRef F,
  31. SMDiagnostic &Err,
  32. LLVMContext &Context,
  33. SlotMapping *Slots) {
  34. std::unique_ptr<Module> M =
  35. make_unique<Module>(F.getBufferIdentifier(), Context);
  36. if (parseAssemblyInto(F, *M, Err, Slots))
  37. return nullptr;
  38. return M;
  39. }
  40. std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
  41. SMDiagnostic &Err,
  42. LLVMContext &Context,
  43. SlotMapping *Slots) {
  44. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  45. MemoryBuffer::getFileOrSTDIN(Filename);
  46. if (std::error_code EC = FileOrErr.getError()) {
  47. Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
  48. "Could not open input file: " + EC.message());
  49. return nullptr;
  50. }
  51. return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots);
  52. }
  53. std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
  54. SMDiagnostic &Err,
  55. LLVMContext &Context,
  56. SlotMapping *Slots) {
  57. MemoryBufferRef F(AsmString, "<string>");
  58. return parseAssembly(F, Err, Context, Slots);
  59. }