blob: 3fc913bf3f138c584af90897a3d4cc81cd3c705d [file] [log] [blame]
Chris Lattner103ff152009-01-02 07:01:27 +00001//===- Parser.cpp - Main dispatch module for the Parser library -----------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Dan Gohmanb116d6c2009-01-08 22:17:05 +000010// This library implements the functionality defined in llvm/Assembly/Parser.h
Dan Gohmanf17a25c2007-07-18 16:29:46 +000011//
Chris Lattner103ff152009-01-02 07:01:27 +000012//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000013
Chris Lattner103ff152009-01-02 07:01:27 +000014#include "llvm/Assembly/Parser.h"
15#include "LLParser.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "llvm/Module.h"
Chris Lattner9df8a4a2009-01-04 20:44:11 +000017#include "llvm/ADT/OwningPtr.h"
Chris Lattner13c68382009-07-02 22:46:18 +000018#include "llvm/Support/SourceMgr.h"
Chris Lattner17e73c22007-11-18 08:46:26 +000019#include "llvm/Support/MemoryBuffer.h"
Chris Lattner103ff152009-01-02 07:01:27 +000020#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000021#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022using namespace llvm;
23
Dan Gohman18721d02009-09-02 17:18:19 +000024Module *llvm::ParseAssembly(MemoryBuffer *F,
25 const std::string &Name,
26 Module *M,
27 SMDiagnostic &Err,
28 LLVMContext &Context) {
29 SourceMgr SM;
30 SM.AddNewSourceBuffer(F, SMLoc());
31
32 // If we are parsing into an existing module, do it.
33 if (M)
34 return LLParser(F, SM, Err, M).Run() ? 0 : M;
35
36 // Otherwise create a new module.
37 OwningPtr<Module> M2(new Module(Name, Context));
38 if (LLParser(F, SM, Err, M2.get()).Run())
39 return 0;
40 return M2.take();
41}
42
Chris Lattner13c68382009-07-02 22:46:18 +000043Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
44 LLVMContext &Context) {
Chris Lattner17e73c22007-11-18 08:46:26 +000045 std::string ErrorStr;
Chris Lattner0ffa08f2009-07-02 23:08:13 +000046 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
Chris Lattner17e73c22007-11-18 08:46:26 +000047 if (F == 0) {
Chris Lattner13c68382009-07-02 22:46:18 +000048 Err = SMDiagnostic("", -1, -1,
49 "Could not open input file '" + Filename + "'", "");
Chris Lattner17e73c22007-11-18 08:46:26 +000050 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 }
Misha Brukman7d987212009-01-02 22:46:48 +000052
Dan Gohman18721d02009-09-02 17:18:19 +000053 return ParseAssembly(F, Filename, 0, Err, Context);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054}
55
Misha Brukman7d987212009-01-02 22:46:48 +000056Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
Chris Lattner13c68382009-07-02 22:46:18 +000057 SMDiagnostic &Err, LLVMContext &Context) {
Chris Lattner0ffa08f2009-07-02 23:08:13 +000058 MemoryBuffer *F =
59 MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
60 "<string>");
Chris Lattner0ffa08f2009-07-02 23:08:13 +000061
Dan Gohman18721d02009-09-02 17:18:19 +000062 return ParseAssembly(F, "<string>", M, Err, Context);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063}