blob: 331a23323b517214156d78b62505d3ec1ea3a334 [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//
Dan Gohman6031a432009-01-08 22:17:05 +000010// This library implements the functionality defined in llvm/Assembly/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
Chris Lattnerac161bf2009-01-02 07:01:27 +000014#include "llvm/Assembly/Parser.h"
15#include "LLParser.h"
Misha Brukmanad44de12003-10-23 18:00:34 +000016#include "llvm/Module.h"
Chris Lattnerad6f3352009-01-04 20:44:11 +000017#include "llvm/ADT/OwningPtr.h"
Chris Lattnera76611a2009-07-02 22:46:18 +000018#include "llvm/Support/SourceMgr.h"
Chris Lattner660c6b92007-11-18 08:46:26 +000019#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000020#include "llvm/Support/raw_ostream.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.
Dan Gohmane6a80ce2009-09-08 22:20:35 +000036 OwningPtr<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;
39 return M2.take();
40}
41
Chris Lattnera76611a2009-07-02 22:46:18 +000042Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
43 LLVMContext &Context) {
Chris Lattner660c6b92007-11-18 08:46:26 +000044 std::string ErrorStr;
Chris Lattner200e0752009-07-02 23:08:13 +000045 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
Chris Lattner660c6b92007-11-18 08:46:26 +000046 if (F == 0) {
Chris Lattnera76611a2009-07-02 22:46:18 +000047 Err = SMDiagnostic("", -1, -1,
48 "Could not open input file '" + Filename + "'", "");
Chris Lattner660c6b92007-11-18 08:46:26 +000049 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +000050 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +000051
Dan Gohmane6a80ce2009-09-08 22:20:35 +000052 return ParseAssembly(F, 0, Err, Context);
Chris Lattner2f7c9632001-06-06 20:29:01 +000053}
54
Misha Brukman1d9a93d2009-01-02 22:46:48 +000055Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
Chris Lattnera76611a2009-07-02 22:46:18 +000056 SMDiagnostic &Err, LLVMContext &Context) {
Chris Lattner200e0752009-07-02 23:08:13 +000057 MemoryBuffer *F =
58 MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
59 "<string>");
Chris Lattner200e0752009-07-02 23:08:13 +000060
Dan Gohmane6a80ce2009-09-08 22:20:35 +000061 return ParseAssembly(F, M, Err, Context);
Chris Lattner416a0d42005-05-20 03:25:47 +000062}