blob: ce0d268a16ecbb33033059b0d38c9867cc85cb1e [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//
10// This library implements the functionality defined in llvm/assembly/parser.h
11//
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 Lattner8e3a8e02007-11-18 08:46:26 +000017#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000018#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000019#include <cstring>
Chris Lattner65cd4b02004-07-13 08:42:12 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattnerdf986172009-01-02 07:01:27 +000022Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError &Err) {
23 Err.setFilename(Filename);
Misha Brukman9ea40342009-01-02 22:46:48 +000024
Chris Lattner8e3a8e02007-11-18 08:46:26 +000025 std::string ErrorStr;
Chris Lattner038112a2008-04-01 18:04:03 +000026 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
Chris Lattner8e3a8e02007-11-18 08:46:26 +000027 if (F == 0) {
Chris Lattnerdf986172009-01-02 07:01:27 +000028 Err.setError("Could not open input file '" + Filename + "'");
Chris Lattner8e3a8e02007-11-18 08:46:26 +000029 return 0;
Chris Lattner00950542001-06-06 20:29:01 +000030 }
Misha Brukman9ea40342009-01-02 22:46:48 +000031
Chris Lattnerdf986172009-01-02 07:01:27 +000032 Module *Result = LLParser(F, Err).Run();
Chris Lattner8e3a8e02007-11-18 08:46:26 +000033 delete F;
Chris Lattner00950542001-06-06 20:29:01 +000034 return Result;
35}
36
Chris Lattnerdf986172009-01-02 07:01:27 +000037// FIXME: M is ignored??
Misha Brukman9ea40342009-01-02 22:46:48 +000038Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
Chris Lattnerdf986172009-01-02 07:01:27 +000039 ParseError &Err) {
40 Err.setFilename("<string>");
41
Misha Brukman9ea40342009-01-02 22:46:48 +000042 MemoryBuffer *F = MemoryBuffer::getMemBuffer(AsmString,
Chris Lattner8e3a8e02007-11-18 08:46:26 +000043 AsmString+strlen(AsmString),
44 "<string>");
Chris Lattnerdf986172009-01-02 07:01:27 +000045 Module *Result = LLParser(F, Err).Run();
Chris Lattner8e3a8e02007-11-18 08:46:26 +000046 delete F;
47 return Result;
Chris Lattner6184feb2005-05-20 03:25:47 +000048}
49
Chris Lattner00950542001-06-06 20:29:01 +000050
51//===------------------------------------------------------------------------===
Reid Spencer61c83e02006-08-18 08:43:06 +000052// ParseError Class
Chris Lattner00950542001-06-06 20:29:01 +000053//===------------------------------------------------------------------------===
54
Chris Lattnerdf986172009-01-02 07:01:27 +000055void ParseError::PrintError(const char *ProgName, raw_ostream &S) {
56 errs() << ProgName << ": ";
Misha Brukman019b6392005-04-21 21:10:11 +000057 if (Filename == "-")
Chris Lattnerdf986172009-01-02 07:01:27 +000058 errs() << "<stdin>";
Chris Lattner00950542001-06-06 20:29:01 +000059 else
Chris Lattnerdf986172009-01-02 07:01:27 +000060 errs() << Filename;
Misha Brukman9ea40342009-01-02 22:46:48 +000061
Chris Lattner00950542001-06-06 20:29:01 +000062 if (LineNo != -1) {
Chris Lattnerdf986172009-01-02 07:01:27 +000063 errs() << ':' << LineNo;
64 if (ColumnNo != -1)
65 errs() << ':' << (ColumnNo+1);
Chris Lattner00950542001-06-06 20:29:01 +000066 }
Misha Brukman9ea40342009-01-02 22:46:48 +000067
Chris Lattnerdf986172009-01-02 07:01:27 +000068 errs() << ": " << Message << '\n';
Misha Brukman9ea40342009-01-02 22:46:48 +000069
Chris Lattnerdf986172009-01-02 07:01:27 +000070 if (LineNo != -1 && ColumnNo != -1) {
71 errs() << LineContents << '\n';
Misha Brukman9ea40342009-01-02 22:46:48 +000072
Chris Lattnerdf986172009-01-02 07:01:27 +000073 // Print out spaces/tabs before the caret.
74 for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
75 errs() << (LineContents[i] == '\t' ? '\t' : ' ');
76 errs() << "^\n";
77 }
Chris Lattner00950542001-06-06 20:29:01 +000078}