blob: 1205dff24e8a5b2768388f97f6a6173cf32ed878 [file] [log] [blame]
Chris Lattnerac161bf2009-01-02 07:01:27 +00001//===- Parser.cpp - Main dispatch module for the Parser library -----------===//
Misha Brukman13f332c2005-04-21 21:10:11 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman13f332c2005-04-21 21:10:11 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chandler Carruth9aca9182014-01-07 12:34:26 +000010// This library implements the functionality defined in llvm/AsmParser/Parser.h
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
Chris Lattnerac161bf2009-01-02 07:01:27 +000012//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000013
Chandler Carruth9aca9182014-01-07 12:34:26 +000014#include "llvm/AsmParser/Parser.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000015#include "LLParser.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Module.h"
Teresa Johnson63ee0e72018-06-26 13:56:49 +000018#include "llvm/IR/ModuleSummaryIndex.h"
Chris Lattner660c6b92007-11-18 08:46:26 +000019#include "llvm/Support/MemoryBuffer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/SourceMgr.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000021#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000022#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000023#include <system_error>
Chris Lattnerd25cad92004-07-13 08:42:12 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Teresa Johnson63ee0e72018-06-26 13:56:49 +000026bool llvm::parseAssemblyInto(MemoryBufferRef F, Module *M,
27 ModuleSummaryIndex *Index, SMDiagnostic &Err,
Yaxun Liuc00d81e2018-01-30 22:32:39 +000028 SlotMapping *Slots, bool UpgradeDebugInfo,
29 StringRef DataLayoutString) {
Rafael Espindola3f3d7ac2014-08-19 22:05:47 +000030 SourceMgr SM;
Alex Lorenzc6277792015-05-20 20:41:27 +000031 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
Rafael Espindolad96d5532014-08-26 21:49:01 +000032 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
Rafael Espindola3f3d7ac2014-08-19 22:05:47 +000033
Teresa Johnson63ee0e72018-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 Liuc00d81e2018-01-30 22:32:39 +000037 DataLayoutString)
38 .Run();
Rafael Espindola3f3d7ac2014-08-19 22:05:47 +000039}
40
Adrian Prantla8b2ddb2017-10-02 18:31:29 +000041std::unique_ptr<Module>
42llvm::parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
Yaxun Liuc00d81e2018-01-30 22:32:39 +000043 SlotMapping *Slots, bool UpgradeDebugInfo,
44 StringRef DataLayoutString) {
Rafael Espindola11c07d72014-08-19 16:58:54 +000045 std::unique_ptr<Module> M =
Rafael Espindolad96d5532014-08-26 21:49:01 +000046 make_unique<Module>(F.getBufferIdentifier(), Context);
Rafael Espindola3f3d7ac2014-08-19 22:05:47 +000047
Teresa Johnson63ee0e72018-06-26 13:56:49 +000048 if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, UpgradeDebugInfo,
49 DataLayoutString))
Craig Topper2617dcc2014-04-15 06:32:26 +000050 return nullptr;
Rafael Espindola3f3d7ac2014-08-19 22:05:47 +000051
Richard Trieu73d06522015-01-17 00:46:44 +000052 return M;
Dan Gohman77ac99d2009-09-02 17:18:19 +000053}
54
Yaxun Liuc00d81e2018-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 Espindolaadf21f22014-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 Lattner03b80a42011-10-16 05:43:57 +000062 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
Rafael Espindolaadf21f22014-07-06 17:43:13 +000063 "Could not open input file: " + EC.message());
Craig Topper2617dcc2014-04-15 06:32:26 +000064 return nullptr;
Chris Lattner2f7c9632001-06-06 20:29:01 +000065 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +000066
Adrian Prantla8b2ddb2017-10-02 18:31:29 +000067 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots,
Yaxun Liuc00d81e2018-01-30 22:32:39 +000068 UpgradeDebugInfo, DataLayoutString);
Chris Lattner2f7c9632001-06-06 20:29:01 +000069}
70
Teresa Johnson63ee0e72018-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 Liuc00d81e2018-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 Espindolad96d5532014-08-26 21:49:01 +0000106 MemoryBufferRef F(AsmString, "<string>");
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000107 return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo,
108 DataLayoutString);
Chris Lattner416a0d42005-05-20 03:25:47 +0000109}
Alex Lorenzd2255952015-07-17 22:07:03 +0000110
Teresa Johnson63ee0e72018-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 Lorenzd2255952015-07-17 22:07:03 +0000148Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err,
Alex Lorenz1de2acd2015-08-21 21:32:39 +0000149 const Module &M, const SlotMapping *Slots) {
Alex Lorenzd2255952015-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 Johnson63ee0e72018-06-26 13:56:49 +0000154 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
Alex Lorenz1de2acd2015-08-21 21:32:39 +0000155 .parseStandaloneConstantValue(C, Slots))
Alex Lorenzd2255952015-07-17 22:07:03 +0000156 return nullptr;
157 return C;
158}
Quentin Colombet81e72b42016-03-07 22:09:05 +0000159
160Type *llvm::parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
161 const SlotMapping *Slots) {
Quentin Colombetdafed5d2016-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 Colombet81e72b42016-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 Johnson63ee0e72018-06-26 13:56:49 +0000183 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
Quentin Colombetdafed5d2016-03-08 00:37:07 +0000184 .parseTypeAtBeginning(Ty, Read, Slots))
Quentin Colombet81e72b42016-03-07 22:09:05 +0000185 return nullptr;
186 return Ty;
187}