Parser.cpp revision 234353
1119610Sache//===- Parser.cpp - Main dispatch module for the Parser library -----------===//
2119610Sache//
3119610Sache//                     The LLVM Compiler Infrastructure
4119610Sache//
5119610Sache// This file is distributed under the University of Illinois Open Source
6119610Sache// License. See LICENSE.TXT for details.
7119610Sache//
8119610Sache//===----------------------------------------------------------------------===//
9119610Sache//
10119610Sache// This library implements the functionality defined in llvm/Assembly/Parser.h
11119610Sache//
12119610Sache//===----------------------------------------------------------------------===//
13119610Sache
14119610Sache#include "llvm/Assembly/Parser.h"
15119610Sache#include "LLParser.h"
16119610Sache#include "llvm/Module.h"
17119610Sache#include "llvm/ADT/OwningPtr.h"
18119610Sache#include "llvm/Support/SourceMgr.h"
19119610Sache#include "llvm/Support/MemoryBuffer.h"
20119610Sache#include "llvm/Support/raw_ostream.h"
21119610Sache#include "llvm/Support/system_error.h"
22119610Sache#include <cstring>
23119610Sacheusing namespace llvm;
24119610Sache
25119610SacheModule *llvm::ParseAssembly(MemoryBuffer *F,
26119610Sache                            Module *M,
27119610Sache                            SMDiagnostic &Err,
28119610Sache                            LLVMContext &Context) {
29119610Sache  SourceMgr SM;
30119610Sache  SM.AddNewSourceBuffer(F, SMLoc());
31119610Sache
32119610Sache  // If we are parsing into an existing module, do it.
33119610Sache  if (M)
34119610Sache    return LLParser(F, SM, Err, M).Run() ? 0 : M;
35119610Sache
36119610Sache  // Otherwise create a new module.
37119610Sache  OwningPtr<Module> M2(new Module(F->getBufferIdentifier(), Context));
38119610Sache  if (LLParser(F, SM, Err, M2.get()).Run())
39119610Sache    return 0;
40119610Sache  return M2.take();
41119610Sache}
42119610Sache
43119610SacheModule *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
44119610Sache                                LLVMContext &Context) {
45119610Sache  OwningPtr<MemoryBuffer> File;
46119610Sache  if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
47119610Sache    Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
48119610Sache                       "Could not open input file: " + ec.message());
49119610Sache    return 0;
50119610Sache  }
51119610Sache
52119610Sache  return ParseAssembly(File.take(), 0, Err, Context);
53119610Sache}
54119610Sache
55119610SacheModule *llvm::ParseAssemblyString(const char *AsmString, Module *M,
56119610Sache                                  SMDiagnostic &Err, LLVMContext &Context) {
57119610Sache  MemoryBuffer *F =
58119610Sache    MemoryBuffer::getMemBuffer(StringRef(AsmString, strlen(AsmString)),
59119610Sache                               "<string>");
60119610Sache
61119610Sache  return ParseAssembly(F, M, Err, Context);
62119610Sache}
63119610Sache