blob: 7759c70d73da9ab0c6cf265539cc267a77914bcf [file] [log] [blame]
Chris Lattnerdf986172009-01-02 07:01:27 +00001//===- Parser.cpp - Main dispatch module for the Parser library -----------===//
Misha Brukman019b6392005-04-21 21:10:11 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukman019b6392005-04-21 21:10:11 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Dan Gohman5d136652009-01-08 22:17:05 +000010// This library implements the functionality defined in llvm/Assembly/Parser.h
Chris Lattner00950542001-06-06 20:29:01 +000011//
Chris Lattnerdf986172009-01-02 07:01:27 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattnerdf986172009-01-02 07:01:27 +000014#include "llvm/Assembly/Parser.h"
15#include "LLParser.h"
Misha Brukman19138482003-10-23 18:00:34 +000016#include "llvm/Module.h"
Chris Lattnerad7d1e22009-01-04 20:44:11 +000017#include "llvm/ADT/OwningPtr.h"
Chris Lattner8e3a8e02007-11-18 08:46:26 +000018#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000019#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000020#include <cstring>
Chris Lattner65cd4b02004-07-13 08:42:12 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Owen Anderson8b477ed2009-07-01 16:58:40 +000023Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError &Err,
24 LLVMContext* Context) {
Chris Lattnerdf986172009-01-02 07:01:27 +000025 Err.setFilename(Filename);
Misha Brukman9ea40342009-01-02 22:46:48 +000026
Chris Lattner8e3a8e02007-11-18 08:46:26 +000027 std::string ErrorStr;
Chris Lattnerad7d1e22009-01-04 20:44:11 +000028 OwningPtr<MemoryBuffer>
29 F(MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr));
Chris Lattner8e3a8e02007-11-18 08:46:26 +000030 if (F == 0) {
Chris Lattnerdf986172009-01-02 07:01:27 +000031 Err.setError("Could not open input file '" + Filename + "'");
Chris Lattner8e3a8e02007-11-18 08:46:26 +000032 return 0;
Chris Lattner00950542001-06-06 20:29:01 +000033 }
Misha Brukman9ea40342009-01-02 22:46:48 +000034
Owen Anderson8b477ed2009-07-01 16:58:40 +000035 OwningPtr<Module> M(new Module(Filename, Context));
Chris Lattnerad7d1e22009-01-04 20:44:11 +000036 if (LLParser(F.get(), Err, M.get()).Run())
37 return 0;
38 return M.take();
Chris Lattner00950542001-06-06 20:29:01 +000039}
40
Misha Brukman9ea40342009-01-02 22:46:48 +000041Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
Owen Anderson8b477ed2009-07-01 16:58:40 +000042 ParseError &Err, LLVMContext* Context) {
Chris Lattnerdf986172009-01-02 07:01:27 +000043 Err.setFilename("<string>");
44
Chris Lattnerad7d1e22009-01-04 20:44:11 +000045 OwningPtr<MemoryBuffer>
46 F(MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
47 "<string>"));
48
49 // If we are parsing into an existing module, do it.
50 if (M)
51 return LLParser(F.get(), Err, M).Run() ? 0 : M;
52
53 // Otherwise create a new module.
Owen Anderson8b477ed2009-07-01 16:58:40 +000054 OwningPtr<Module> M2(new Module("<string>", Context));
Chris Lattnerad7d1e22009-01-04 20:44:11 +000055 if (LLParser(F.get(), Err, M2.get()).Run())
56 return 0;
57 return M2.take();
Chris Lattner6184feb2005-05-20 03:25:47 +000058}
59
Chris Lattner00950542001-06-06 20:29:01 +000060
61//===------------------------------------------------------------------------===
Reid Spencer61c83e02006-08-18 08:43:06 +000062// ParseError Class
Chris Lattner00950542001-06-06 20:29:01 +000063//===------------------------------------------------------------------------===
64
Chris Lattnerdf986172009-01-02 07:01:27 +000065void ParseError::PrintError(const char *ProgName, raw_ostream &S) {
66 errs() << ProgName << ": ";
Misha Brukman019b6392005-04-21 21:10:11 +000067 if (Filename == "-")
Chris Lattnerdf986172009-01-02 07:01:27 +000068 errs() << "<stdin>";
Chris Lattner00950542001-06-06 20:29:01 +000069 else
Chris Lattnerdf986172009-01-02 07:01:27 +000070 errs() << Filename;
Misha Brukman9ea40342009-01-02 22:46:48 +000071
Chris Lattner00950542001-06-06 20:29:01 +000072 if (LineNo != -1) {
Chris Lattnerdf986172009-01-02 07:01:27 +000073 errs() << ':' << LineNo;
74 if (ColumnNo != -1)
75 errs() << ':' << (ColumnNo+1);
Chris Lattner00950542001-06-06 20:29:01 +000076 }
Misha Brukman9ea40342009-01-02 22:46:48 +000077
Chris Lattnerdf986172009-01-02 07:01:27 +000078 errs() << ": " << Message << '\n';
Misha Brukman9ea40342009-01-02 22:46:48 +000079
Chris Lattnerdf986172009-01-02 07:01:27 +000080 if (LineNo != -1 && ColumnNo != -1) {
81 errs() << LineContents << '\n';
Misha Brukman9ea40342009-01-02 22:46:48 +000082
Chris Lattnerdf986172009-01-02 07:01:27 +000083 // Print out spaces/tabs before the caret.
84 for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
85 errs() << (LineContents[i] == '\t' ? '\t' : ' ');
86 errs() << "^\n";
87 }
Chris Lattner00950542001-06-06 20:29:01 +000088}