Add {MCInst,MCOperand}::{print,dump}
[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   MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
28   if (F == 0) {
29     Err = SMDiagnostic("", -1, -1,
30                        "Could not open input file '" + Filename + "'", "");
31     return 0;
32   }
33
34   SourceMgr SM;
35   SM.AddNewSourceBuffer(F, SMLoc());
36   
37   OwningPtr<Module> M(new Module(Filename, Context));
38   if (LLParser(F, SM, Err, M.get()).Run())
39     return 0;
40   return M.take();
41 }
42
43 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
44                                   SMDiagnostic &Err, LLVMContext &Context) {
45   MemoryBuffer *F =
46     MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
47                                "<string>");
48   
49   SourceMgr SM;
50   SM.AddNewSourceBuffer(F, SMLoc());
51
52   // If we are parsing into an existing module, do it.
53   if (M)
54     return LLParser(F, SM, Err, M).Run() ? 0 : M;
55
56   // Otherwise create a new module.
57   OwningPtr<Module> M2(new Module("<string>", Context));
58   if (LLParser(F, SM, Err, M2.get()).Run())
59     return 0;
60   return M2.take();
61 }