blob: 7759c70d73da9ab0c6cf265539cc267a77914bcf [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 Lattner17e73c22007-11-18 08:46:26 +000018#include "llvm/Support/MemoryBuffer.h"
Chris Lattner103ff152009-01-02 07:01:27 +000019#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000020#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021using namespace llvm;
22
Owen Anderson25209b42009-07-01 16:58:40 +000023Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError &Err,
24 LLVMContext* Context) {
Chris Lattner103ff152009-01-02 07:01:27 +000025 Err.setFilename(Filename);
Misha Brukman7d987212009-01-02 22:46:48 +000026
Chris Lattner17e73c22007-11-18 08:46:26 +000027 std::string ErrorStr;
Chris Lattner9df8a4a2009-01-04 20:44:11 +000028 OwningPtr<MemoryBuffer>
29 F(MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr));
Chris Lattner17e73c22007-11-18 08:46:26 +000030 if (F == 0) {
Chris Lattner103ff152009-01-02 07:01:27 +000031 Err.setError("Could not open input file '" + Filename + "'");
Chris Lattner17e73c22007-11-18 08:46:26 +000032 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 }
Misha Brukman7d987212009-01-02 22:46:48 +000034
Owen Anderson25209b42009-07-01 16:58:40 +000035 OwningPtr<Module> M(new Module(Filename, Context));
Chris Lattner9df8a4a2009-01-04 20:44:11 +000036 if (LLParser(F.get(), Err, M.get()).Run())
37 return 0;
38 return M.take();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039}
40
Misha Brukman7d987212009-01-02 22:46:48 +000041Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
Owen Anderson25209b42009-07-01 16:58:40 +000042 ParseError &Err, LLVMContext* Context) {
Chris Lattner103ff152009-01-02 07:01:27 +000043 Err.setFilename("<string>");
44
Chris Lattner9df8a4a2009-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 Anderson25209b42009-07-01 16:58:40 +000054 OwningPtr<Module> M2(new Module("<string>", Context));
Chris Lattner9df8a4a2009-01-04 20:44:11 +000055 if (LLParser(F.get(), Err, M2.get()).Run())
56 return 0;
57 return M2.take();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058}
59
60
61//===------------------------------------------------------------------------===
62// ParseError Class
63//===------------------------------------------------------------------------===
64
Chris Lattner103ff152009-01-02 07:01:27 +000065void ParseError::PrintError(const char *ProgName, raw_ostream &S) {
66 errs() << ProgName << ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 if (Filename == "-")
Chris Lattner103ff152009-01-02 07:01:27 +000068 errs() << "<stdin>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 else
Chris Lattner103ff152009-01-02 07:01:27 +000070 errs() << Filename;
Misha Brukman7d987212009-01-02 22:46:48 +000071
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 if (LineNo != -1) {
Chris Lattner103ff152009-01-02 07:01:27 +000073 errs() << ':' << LineNo;
74 if (ColumnNo != -1)
75 errs() << ':' << (ColumnNo+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 }
Misha Brukman7d987212009-01-02 22:46:48 +000077
Chris Lattner103ff152009-01-02 07:01:27 +000078 errs() << ": " << Message << '\n';
Misha Brukman7d987212009-01-02 22:46:48 +000079
Chris Lattner103ff152009-01-02 07:01:27 +000080 if (LineNo != -1 && ColumnNo != -1) {
81 errs() << LineContents << '\n';
Misha Brukman7d987212009-01-02 22:46:48 +000082
Chris Lattner103ff152009-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 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088}