blob: 6dbb60f135cb8a7f202a22cf4e213d383c7064d6 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Parser.cpp - Main dispatch module for the Parser library -------------===
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This library implements the functionality defined in llvm/assembly/parser.h
11//
12//===------------------------------------------------------------------------===
13
14#include "ParserInternals.h"
15#include "llvm/Module.h"
Chris Lattner17e73c22007-11-18 08:46:26 +000016#include "llvm/Support/MemoryBuffer.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017using namespace llvm;
18
19
20ParseError* TheParseError = 0; /// FIXME: Not threading friendly
21
22Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError* Err) {
Chris Lattner17e73c22007-11-18 08:46:26 +000023 std::string ErrorStr;
24 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(&Filename[0], Filename.size(),
25 &ErrorStr);
26 if (F == 0) {
27 if (Err)
28 Err->setError(Filename, "Could not open input file '" + Filename + "'");
29 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030 }
Chris Lattner17e73c22007-11-18 08:46:26 +000031
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 TheParseError = Err;
Chris Lattner17e73c22007-11-18 08:46:26 +000033 Module *Result = RunVMAsmParser(F);
34 delete F;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035 return Result;
36}
37
Chris Lattner17e73c22007-11-18 08:46:26 +000038Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
39 ParseError *Err) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 TheParseError = Err;
Chris Lattner17e73c22007-11-18 08:46:26 +000041 MemoryBuffer *F = MemoryBuffer::getMemBuffer(AsmString,
42 AsmString+strlen(AsmString),
43 "<string>");
44 Module *Result = RunVMAsmParser(F);
45 delete F;
46 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047}
48
49
50//===------------------------------------------------------------------------===
51// ParseError Class
52//===------------------------------------------------------------------------===
53
54
55void ParseError::setError(const std::string &filename,
Chris Lattner17e73c22007-11-18 08:46:26 +000056 const std::string &message,
57 int lineNo, int colNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 Filename = filename;
59 Message = message;
60 LineNo = lineNo;
61 colNo = colNo;
62}
63
64ParseError::ParseError(const ParseError &E)
65 : Filename(E.Filename), Message(E.Message) {
66 LineNo = E.LineNo;
67 ColumnNo = E.ColumnNo;
68}
69
70// Includes info from options
71const std::string ParseError::getMessage() const {
72 std::string Result;
73 char Buffer[10];
74
75 if (Filename == "-")
76 Result += "<stdin>";
77 else
78 Result += Filename;
79
80 if (LineNo != -1) {
81 sprintf(Buffer, "%d", LineNo);
82 Result += std::string(":") + Buffer;
83 if (ColumnNo != -1) {
84 sprintf(Buffer, "%d", ColumnNo);
85 Result += std::string(",") + Buffer;
86 }
87 }
88
89 return Result + ": " + Message;
90}