blob: a1da5e11639ed1512fce926fe9918f1d59945fe7 [file] [log] [blame]
Chris Lattnerac161bf2009-01-02 07:01:27 +00001//===- Parser.cpp - Main dispatch module for the Parser library -----------===//
Misha Brukman13f332c2005-04-21 21:10:11 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman13f332c2005-04-21 21:10:11 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chandler Carruth9aca9182014-01-07 12:34:26 +000010// This library implements the functionality defined in llvm/AsmParser/Parser.h
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
Chris Lattnerac161bf2009-01-02 07:01:27 +000012//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000013
Chandler Carruth9aca9182014-01-07 12:34:26 +000014#include "llvm/AsmParser/Parser.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000015#include "LLParser.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Module.h"
Chris Lattner660c6b92007-11-18 08:46:26 +000017#include "llvm/Support/MemoryBuffer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Support/SourceMgr.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000019#include "llvm/Support/raw_ostream.h"
Michael J. Spencer7b6fef82010-12-09 17:36:48 +000020#include "llvm/Support/system_error.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000021#include <cstring>
Chris Lattnerd25cad92004-07-13 08:42:12 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Dan Gohman77ac99d2009-09-02 17:18:19 +000024Module *llvm::ParseAssembly(MemoryBuffer *F,
Dan Gohman77ac99d2009-09-02 17:18:19 +000025 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.
Ahmed Charles56440fd2014-03-06 05:51:42 +000036 std::unique_ptr<Module> M2(new Module(F->getBufferIdentifier(), Context));
Dan Gohman77ac99d2009-09-02 17:18:19 +000037 if (LLParser(F, SM, Err, M2.get()).Run())
38 return 0;
Ahmed Charles96c9d952014-03-05 10:19:29 +000039 return M2.release();
Dan Gohman77ac99d2009-09-02 17:18:19 +000040}
41
Chris Lattnera76611a2009-07-02 22:46:18 +000042Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
43 LLVMContext &Context) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000044 std::unique_ptr<MemoryBuffer> File;
Rafael Espindola8c811722013-06-25 05:28:34 +000045 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
Chris Lattner03b80a42011-10-16 05:43:57 +000046 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
Michael J. Spencer7b6fef82010-12-09 17:36:48 +000047 "Could not open input file: " + ec.message());
Chris Lattner660c6b92007-11-18 08:46:26 +000048 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +000049 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +000050
Ahmed Charles96c9d952014-03-05 10:19:29 +000051 return ParseAssembly(File.release(), 0, Err, Context);
Chris Lattner2f7c9632001-06-06 20:29:01 +000052}
53
Misha Brukman1d9a93d2009-01-02 22:46:48 +000054Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
Chris Lattnera76611a2009-07-02 22:46:18 +000055 SMDiagnostic &Err, LLVMContext &Context) {
Chris Lattner200e0752009-07-02 23:08:13 +000056 MemoryBuffer *F =
Chris Lattner0e45d242010-04-05 22:42:30 +000057 MemoryBuffer::getMemBuffer(StringRef(AsmString, strlen(AsmString)),
Chris Lattner200e0752009-07-02 23:08:13 +000058 "<string>");
Chris Lattner200e0752009-07-02 23:08:13 +000059
Dan Gohmane6a80ce2009-09-08 22:20:35 +000060 return ParseAssembly(F, M, Err, Context);
Chris Lattner416a0d42005-05-20 03:25:47 +000061}