LLVM API Documentation

Parser.cpp

Go to the documentation of this file.
00001 //===- Parser.cpp - Main dispatch module for the Parser library -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This library implements the functionality defined in llvm/Assembly/Parser.h
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Assembly/Parser.h"
00015 #include "LLParser.h"
00016 #include "llvm/Module.h"
00017 #include "llvm/ADT/OwningPtr.h"
00018 #include "llvm/Support/SourceMgr.h"
00019 #include "llvm/Support/MemoryBuffer.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 #include <cstring>
00022 using namespace llvm;
00023 
00024 Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
00025                                 LLVMContext &Context) {
00026   std::string ErrorStr;
00027   MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
00028   if (F == 0) {
00029     Err = SMDiagnostic("", -1, -1,
00030                        "Could not open input file '" + Filename + "'", "");
00031     return 0;
00032   }
00033 
00034   SourceMgr SM;
00035   SM.AddNewSourceBuffer(F, SMLoc());
00036   
00037   OwningPtr<Module> M(new Module(Filename, Context));
00038   if (LLParser(F, SM, Err, M.get()).Run())
00039     return 0;
00040   return M.take();
00041 }
00042 
00043 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
00044                                   SMDiagnostic &Err, LLVMContext &Context) {
00045   MemoryBuffer *F =
00046     MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
00047                                "<string>");
00048   
00049   SourceMgr SM;
00050   SM.AddNewSourceBuffer(F, SMLoc());
00051 
00052   // If we are parsing into an existing module, do it.
00053   if (M)
00054     return LLParser(F, SM, Err, M).Run() ? 0 : M;
00055 
00056   // Otherwise create a new module.
00057   OwningPtr<Module> M2(new Module("<string>", Context));
00058   if (LLParser(F, SM, Err, M2.get()).Run())
00059     return 0;
00060   return M2.take();
00061 }