Parser.cpp revision 195340
1193323Sed//===- Parser.cpp - Main dispatch module for the Parser library -----------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This library implements the functionality defined in llvm/Assembly/Parser.h
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14193323Sed#include "llvm/Assembly/Parser.h"
15193323Sed#include "LLParser.h"
16193323Sed#include "llvm/Module.h"
17193323Sed#include "llvm/ADT/OwningPtr.h"
18195340Sed#include "llvm/Support/SourceMgr.h"
19193323Sed#include "llvm/Support/MemoryBuffer.h"
20193323Sed#include "llvm/Support/raw_ostream.h"
21193323Sed#include <cstring>
22193323Sedusing namespace llvm;
23193323Sed
24195340SedModule *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
25195340Sed                                LLVMContext &Context) {
26193323Sed  std::string ErrorStr;
27195340Sed  MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
28193323Sed  if (F == 0) {
29195340Sed    Err = SMDiagnostic("", -1, -1,
30195340Sed                       "Could not open input file '" + Filename + "'", "");
31193323Sed    return 0;
32193323Sed  }
33193323Sed
34195340Sed  SourceMgr SM;
35195340Sed  SM.AddNewSourceBuffer(F, SMLoc());
36195340Sed
37195340Sed  OwningPtr<Module> M(new Module(Filename, Context));
38195340Sed  if (LLParser(F, SM, Err, M.get()).Run())
39193323Sed    return 0;
40193323Sed  return M.take();
41193323Sed}
42193323Sed
43193323SedModule *llvm::ParseAssemblyString(const char *AsmString, Module *M,
44195340Sed                                  SMDiagnostic &Err, LLVMContext &Context) {
45195340Sed  MemoryBuffer *F =
46195340Sed    MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
47195340Sed                               "<string>");
48195340Sed
49195340Sed  SourceMgr SM;
50195340Sed  SM.AddNewSourceBuffer(F, SMLoc());
51193323Sed
52193323Sed  // If we are parsing into an existing module, do it.
53193323Sed  if (M)
54195340Sed    return LLParser(F, SM, Err, M).Run() ? 0 : M;
55193323Sed
56193323Sed  // Otherwise create a new module.
57195340Sed  OwningPtr<Module> M2(new Module("<string>", Context));
58195340Sed  if (LLParser(F, SM, Err, M2.get()).Run())
59193323Sed    return 0;
60193323Sed  return M2.take();
61193323Sed}
62