Parser.cpp revision 206274
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/Assembly/Parser.h
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Assembly/Parser.h"
15#include "LLParser.h"
16#include "llvm/Module.h"
17#include "llvm/ADT/OwningPtr.h"
18#include "llvm/Support/SourceMgr.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/Support/raw_ostream.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  OwningPtr<Module> M2(new Module(F->getBufferIdentifier(), Context));
37  if (LLParser(F, SM, Err, M2.get()).Run())
38    return 0;
39  return M2.take();
40}
41
42Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
43                                LLVMContext &Context) {
44  std::string ErrorStr;
45  MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
46  if (F == 0) {
47    Err = SMDiagnostic(SMLoc(), "", -1, -1,
48                       "Could not open input file '" + Filename + "': " +
49                       ErrorStr, "");
50    return 0;
51  }
52
53  return ParseAssembly(F, 0, Err, Context);
54}
55
56Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
57                                  SMDiagnostic &Err, LLVMContext &Context) {
58  MemoryBuffer *F =
59    MemoryBuffer::getMemBuffer(StringRef(AsmString, strlen(AsmString)),
60                               "<string>");
61
62  return ParseAssembly(F, M, Err, Context);
63}
64