Parser.cpp revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
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
14#include "llvm/AsmParser/Parser.h"
15#include "LLParser.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 "llvm/Support/system_error.h"
21#include <cstring>
22using namespace llvm;
23
24Module *llvm::ParseAssembly(MemoryBuffer *F,
25                            Module *M,
26                            SMDiagnostic &Err,
27                            LLVMContext &Context) {
28  SourceMgr SM;
29  SM.AddNewSourceBuffer(F, SMLoc());
30
31  // If we are parsing into an existing module, do it.
32  if (M)
33    return LLParser(F, SM, Err, M).Run() ? 0 : M;
34
35  // Otherwise create a new module.
36  std::unique_ptr<Module> M2(new Module(F->getBufferIdentifier(), Context));
37  if (LLParser(F, SM, Err, M2.get()).Run())
38    return 0;
39  return M2.release();
40}
41
42Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
43                                LLVMContext &Context) {
44  std::unique_ptr<MemoryBuffer> File;
45  if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
46    Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
47                       "Could not open input file: " + ec.message());
48    return 0;
49  }
50
51  return ParseAssembly(File.release(), 0, Err, Context);
52}
53
54Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
55                                  SMDiagnostic &Err, LLVMContext &Context) {
56  MemoryBuffer *F =
57    MemoryBuffer::getMemBuffer(StringRef(AsmString, strlen(AsmString)),
58                               "<string>");
59
60  return ParseAssembly(F, M, Err, Context);
61}
62