blob: 1205dff24e8a5b2768388f97f6a6173cf32ed878 [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//
Chandler Carruthbc65a8d2014-01-07 12:34:26 +000010// This library implements the functionality defined in llvm/AsmParser/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
Chandler Carruthbc65a8d2014-01-07 12:34:26 +000014#include "llvm/AsmParser/Parser.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000015#include "LLParser.h"
Benjamin Kramerd59c5f92015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/Module.h"
Teresa Johnsonc6dda902018-06-26 13:56:49 +000018#include "llvm/IR/ModuleSummaryIndex.h"
Chris Lattner8e3a8e02007-11-18 08:46:26 +000019#include "llvm/Support/MemoryBuffer.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/Support/SourceMgr.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000021#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000022#include <cstring>
Rafael Espindolad5132f92014-06-12 17:38:55 +000023#include <system_error>
Chris Lattner65cd4b02004-07-13 08:42:12 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Teresa Johnsonc6dda902018-06-26 13:56:49 +000026bool llvm::parseAssemblyInto(MemoryBufferRef F, Module *M,
27 ModuleSummaryIndex *Index, SMDiagnostic &Err,
Yaxun Liu7c2d0492018-01-30 22:32:39 +000028 SlotMapping *Slots, bool UpgradeDebugInfo,
29 StringRef DataLayoutString) {
Rafael Espindolab12ab602014-08-19 22:05:47 +000030 SourceMgr SM;
Alex Lorenz3f1b2bd2015-05-20 20:41:27 +000031 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
Rafael Espindola22929962014-08-26 21:49:01 +000032 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
Rafael Espindolab12ab602014-08-19 22:05:47 +000033
Teresa Johnsonc6dda902018-06-26 13:56:49 +000034 LLVMContext Context;
35 return LLParser(F.getBuffer(), SM, Err, M, Index,
36 M ? M->getContext() : Context, Slots, UpgradeDebugInfo,
Yaxun Liu7c2d0492018-01-30 22:32:39 +000037 DataLayoutString)
38 .Run();
Rafael Espindolab12ab602014-08-19 22:05:47 +000039}
40
Adrian Prantl733fe2f2017-10-02 18:31:29 +000041std::unique_ptr<Module>
42llvm::parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
Yaxun Liu7c2d0492018-01-30 22:32:39 +000043 SlotMapping *Slots, bool UpgradeDebugInfo,
44 StringRef DataLayoutString) {
Rafael Espindola9b29ff92014-08-19 16:58:54 +000045 std::unique_ptr<Module> M =
Rafael Espindola22929962014-08-26 21:49:01 +000046 make_unique<Module>(F.getBufferIdentifier(), Context);
Rafael Espindolab12ab602014-08-19 22:05:47 +000047
Teresa Johnsonc6dda902018-06-26 13:56:49 +000048 if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, UpgradeDebugInfo,
49 DataLayoutString))
Craig Topper0b6cb712014-04-15 06:32:26 +000050 return nullptr;
Rafael Espindolab12ab602014-08-19 22:05:47 +000051
Richard Trieu6cf3f3f2015-01-17 00:46:44 +000052 return M;
Dan Gohman2ec5fe52009-09-02 17:18:19 +000053}
54
Yaxun Liu7c2d0492018-01-30 22:32:39 +000055std::unique_ptr<Module>
56llvm::parseAssemblyFile(StringRef Filename, SMDiagnostic &Err,
57 LLVMContext &Context, SlotMapping *Slots,
58 bool UpgradeDebugInfo, StringRef DataLayoutString) {
Rafael Espindola7cba2a92014-07-06 17:43:13 +000059 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
60 MemoryBuffer::getFileOrSTDIN(Filename);
61 if (std::error_code EC = FileOrErr.getError()) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +000062 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
Rafael Espindola7cba2a92014-07-06 17:43:13 +000063 "Could not open input file: " + EC.message());
Craig Topper0b6cb712014-04-15 06:32:26 +000064 return nullptr;
Chris Lattner00950542001-06-06 20:29:01 +000065 }
Misha Brukman9ea40342009-01-02 22:46:48 +000066
Adrian Prantl733fe2f2017-10-02 18:31:29 +000067 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots,
Yaxun Liu7c2d0492018-01-30 22:32:39 +000068 UpgradeDebugInfo, DataLayoutString);
Chris Lattner00950542001-06-06 20:29:01 +000069}
70
Teresa Johnsonc6dda902018-06-26 13:56:49 +000071ParsedModuleAndIndex llvm::parseAssemblyWithIndex(
72 MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
73 SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
74 std::unique_ptr<Module> M =
75 make_unique<Module>(F.getBufferIdentifier(), Context);
76 std::unique_ptr<ModuleSummaryIndex> Index =
77 make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
78
79 if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
80 DataLayoutString))
81 return {nullptr, nullptr};
82
83 return {std::move(M), std::move(Index)};
84}
85
86ParsedModuleAndIndex llvm::parseAssemblyFileWithIndex(
87 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
88 SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
89 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
90 MemoryBuffer::getFileOrSTDIN(Filename);
91 if (std::error_code EC = FileOrErr.getError()) {
92 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
93 "Could not open input file: " + EC.message());
94 return {nullptr, nullptr};
95 }
96
97 return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
98 Context, Slots, UpgradeDebugInfo,
99 DataLayoutString);
100}
101
Yaxun Liu7c2d0492018-01-30 22:32:39 +0000102std::unique_ptr<Module>
103llvm::parseAssemblyString(StringRef AsmString, SMDiagnostic &Err,
104 LLVMContext &Context, SlotMapping *Slots,
105 bool UpgradeDebugInfo, StringRef DataLayoutString) {
Rafael Espindola22929962014-08-26 21:49:01 +0000106 MemoryBufferRef F(AsmString, "<string>");
Yaxun Liu7c2d0492018-01-30 22:32:39 +0000107 return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo,
108 DataLayoutString);
Chris Lattner6184feb2005-05-20 03:25:47 +0000109}
Alex Lorenz4b50ecb2015-07-17 22:07:03 +0000110
Teresa Johnsonc6dda902018-06-26 13:56:49 +0000111static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F,
112 ModuleSummaryIndex &Index,
113 SMDiagnostic &Err) {
114 SourceMgr SM;
115 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
116 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
117
118 // The parser holds a reference to a context that is unused when parsing the
119 // index, but we need to initialize it.
120 LLVMContext unusedContext;
121 return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext).Run();
122}
123
124std::unique_ptr<ModuleSummaryIndex>
125llvm::parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err) {
126 std::unique_ptr<ModuleSummaryIndex> Index =
127 make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
128
129 if (parseSummaryIndexAssemblyInto(F, *Index, Err))
130 return nullptr;
131
132 return Index;
133}
134
135std::unique_ptr<ModuleSummaryIndex>
136llvm::parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err) {
137 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
138 MemoryBuffer::getFileOrSTDIN(Filename);
139 if (std::error_code EC = FileOrErr.getError()) {
140 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
141 "Could not open input file: " + EC.message());
142 return nullptr;
143 }
144
145 return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
146}
147
Alex Lorenz4b50ecb2015-07-17 22:07:03 +0000148Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err,
Alex Lorenz0e998762015-08-21 21:32:39 +0000149 const Module &M, const SlotMapping *Slots) {
Alex Lorenz4b50ecb2015-07-17 22:07:03 +0000150 SourceMgr SM;
151 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
152 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
153 Constant *C;
Teresa Johnsonc6dda902018-06-26 13:56:49 +0000154 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
Alex Lorenz0e998762015-08-21 21:32:39 +0000155 .parseStandaloneConstantValue(C, Slots))
Alex Lorenz4b50ecb2015-07-17 22:07:03 +0000156 return nullptr;
157 return C;
158}
Quentin Colombet2ba03232016-03-07 22:09:05 +0000159
160Type *llvm::parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
161 const SlotMapping *Slots) {
Quentin Colombetcbd4dbb2016-03-08 00:37:07 +0000162 unsigned Read;
163 Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
164 if (!Ty)
165 return nullptr;
166 if (Read != Asm.size()) {
167 SourceMgr SM;
168 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
169 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
170 Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
171 SourceMgr::DK_Error, "expected end of string");
172 return nullptr;
173 }
174 return Ty;
175}
176Type *llvm::parseTypeAtBeginning(StringRef Asm, unsigned &Read,
177 SMDiagnostic &Err, const Module &M,
178 const SlotMapping *Slots) {
Quentin Colombet2ba03232016-03-07 22:09:05 +0000179 SourceMgr SM;
180 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
181 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
182 Type *Ty;
Teresa Johnsonc6dda902018-06-26 13:56:49 +0000183 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
Quentin Colombetcbd4dbb2016-03-08 00:37:07 +0000184 .parseTypeAtBeginning(Ty, Read, Slots))
Quentin Colombet2ba03232016-03-07 22:09:05 +0000185 return nullptr;
186 return Ty;
187}