17aae397eaa50d6112714423dca4b0904dddbd7c
[oota-llvm.git] / lib / AsmParser / Parser.cpp
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>
22 using namespace llvm;
23
24 Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
25                                 LLVMContext &Context) {
26   std::string ErrorStr;
27   OwningPtr<MemoryBuffer>
28     F(MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr));
29   if (F == 0) {
30     Err = SMDiagnostic("", -1, -1,
31                        "Could not open input file '" + Filename + "'", "");
32     return 0;
33   }
34
35   OwningPtr<Module> M(new Module(Filename, Context));
36   if (LLParser(F.get(), Err, M.get()).Run())
37     return 0;
38   return M.take();
39 }
40
41 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
42                                   SMDiagnostic &Err, LLVMContext &Context) {
43   OwningPtr<MemoryBuffer>
44     F(MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
45                                  "<string>"));
46   
47   // If we are parsing into an existing module, do it.
48   if (M)
49     return LLParser(F.get(), Err, M).Run() ? 0 : M;
50
51   // Otherwise create a new module.
52   OwningPtr<Module> M2(new Module("<string>", Context));
53   if (LLParser(F.get(), Err, M2.get()).Run())
54     return 0;
55   return M2.take();
56 }