1 //===- Parser.cpp - Main dispatch module for the Parser library -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This library implements the functionality defined in llvm/AsmParser/Parser.h
12 //===----------------------------------------------------------------------===//
14 #include "llvm/AsmParser/Parser.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "llvm/Support/raw_ostream.h"
22 #include <system_error>
25 bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err,
26 SlotMapping *Slots, bool UpgradeDebugInfo,
27 StringRef DataLayoutString) {
29 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
30 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
32 return LLParser(F.getBuffer(), SM, Err, &M, Slots, UpgradeDebugInfo,
37 std::unique_ptr<Module>
38 llvm::parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
39 SlotMapping *Slots, bool UpgradeDebugInfo,
40 StringRef DataLayoutString) {
41 std::unique_ptr<Module> M =
42 make_unique<Module>(F.getBufferIdentifier(), Context);
44 if (parseAssemblyInto(F, *M, Err, Slots, UpgradeDebugInfo, DataLayoutString))
50 std::unique_ptr<Module>
51 llvm::parseAssemblyFile(StringRef Filename, SMDiagnostic &Err,
52 LLVMContext &Context, SlotMapping *Slots,
53 bool UpgradeDebugInfo, StringRef DataLayoutString) {
54 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
55 MemoryBuffer::getFileOrSTDIN(Filename);
56 if (std::error_code EC = FileOrErr.getError()) {
57 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
58 "Could not open input file: " + EC.message());
62 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots,
63 UpgradeDebugInfo, DataLayoutString);
66 std::unique_ptr<Module>
67 llvm::parseAssemblyString(StringRef AsmString, SMDiagnostic &Err,
68 LLVMContext &Context, SlotMapping *Slots,
69 bool UpgradeDebugInfo, StringRef DataLayoutString) {
70 MemoryBufferRef F(AsmString, "<string>");
71 return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo,
75 Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err,
76 const Module &M, const SlotMapping *Slots) {
78 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
79 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
81 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M))
82 .parseStandaloneConstantValue(C, Slots))
87 Type *llvm::parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
88 const SlotMapping *Slots) {
90 Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
93 if (Read != Asm.size()) {
95 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
96 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
97 Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
98 SourceMgr::DK_Error, "expected end of string");
103 Type *llvm::parseTypeAtBeginning(StringRef Asm, unsigned &Read,
104 SMDiagnostic &Err, const Module &M,
105 const SlotMapping *Slots) {
107 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
108 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
110 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M))
111 .parseTypeAtBeginning(Ty, Read, Slots))