blob: b7f552a6fccb9573c98b69c3d43a0a785f56ff13 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman13f332c2005-04-21 21:10:11 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00008//
Chandler Carruth9aca9182014-01-07 12:34:26 +00009// This library implements the functionality defined in llvm/AsmParser/Parser.h
Chris Lattner2f7c9632001-06-06 20:29:01 +000010//
Chris Lattnerac161bf2009-01-02 07:01:27 +000011//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000012
Chandler Carruth9aca9182014-01-07 12:34:26 +000013#include "llvm/AsmParser/Parser.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000014#include "LLParser.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Module.h"
Teresa Johnson63ee0e72018-06-26 13:56:49 +000017#include "llvm/IR/ModuleSummaryIndex.h"
Chris Lattner660c6b92007-11-18 08:46:26 +000018#include "llvm/Support/MemoryBuffer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/SourceMgr.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000020#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000021#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000022#include <system_error>
Chris Lattnerd25cad92004-07-13 08:42:12 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Teresa Johnson63ee0e72018-06-26 13:56:49 +000025bool llvm::parseAssemblyInto(MemoryBufferRef F, Module *M,
26 ModuleSummaryIndex *Index, SMDiagnostic &Err,
Yaxun Liuc00d81e2018-01-30 22:32:39 +000027 SlotMapping *Slots, bool UpgradeDebugInfo,
28 StringRef DataLayoutString) {
Rafael Espindola3f3d7ac2014-08-19 22:05:47 +000029 SourceMgr SM;
Alex Lorenzc6277792015-05-20 20:41:27 +000030 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
Rafael Espindolad96d5532014-08-26 21:49:01 +000031 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
Rafael Espindola3f3d7ac2014-08-19 22:05:47 +000032
Teresa Johnson63ee0e72018-06-26 13:56:49 +000033 LLVMContext Context;
34 return LLParser(F.getBuffer(), SM, Err, M, Index,
35 M ? M->getContext() : Context, Slots, UpgradeDebugInfo,
Yaxun Liuc00d81e2018-01-30 22:32:39 +000036 DataLayoutString)
37 .Run();
Rafael Espindola3f3d7ac2014-08-19 22:05:47 +000038}
39
Adrian Prantla8b2ddb2017-10-02 18:31:29 +000040std::unique_ptr<Module>
41llvm::parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
Yaxun Liuc00d81e2018-01-30 22:32:39 +000042 SlotMapping *Slots, bool UpgradeDebugInfo,
43 StringRef DataLayoutString) {
Rafael Espindola11c07d72014-08-19 16:58:54 +000044 std::unique_ptr<Module> M =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000045 std::make_unique<Module>(F.getBufferIdentifier(), Context);
Rafael Espindola3f3d7ac2014-08-19 22:05:47 +000046
Teresa Johnson63ee0e72018-06-26 13:56:49 +000047 if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, UpgradeDebugInfo,
48 DataLayoutString))
Craig Topper2617dcc2014-04-15 06:32:26 +000049 return nullptr;
Rafael Espindola3f3d7ac2014-08-19 22:05:47 +000050
Richard Trieu73d06522015-01-17 00:46:44 +000051 return M;
Dan Gohman77ac99d2009-09-02 17:18:19 +000052}
53
Yaxun Liuc00d81e2018-01-30 22:32:39 +000054std::unique_ptr<Module>
55llvm::parseAssemblyFile(StringRef Filename, SMDiagnostic &Err,
56 LLVMContext &Context, SlotMapping *Slots,
57 bool UpgradeDebugInfo, StringRef DataLayoutString) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000058 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
59 MemoryBuffer::getFileOrSTDIN(Filename);
60 if (std::error_code EC = FileOrErr.getError()) {
Chris Lattner03b80a42011-10-16 05:43:57 +000061 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
Rafael Espindolaadf21f22014-07-06 17:43:13 +000062 "Could not open input file: " + EC.message());
Craig Topper2617dcc2014-04-15 06:32:26 +000063 return nullptr;
Chris Lattner2f7c9632001-06-06 20:29:01 +000064 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +000065
Adrian Prantla8b2ddb2017-10-02 18:31:29 +000066 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots,
Yaxun Liuc00d81e2018-01-30 22:32:39 +000067 UpgradeDebugInfo, DataLayoutString);
Chris Lattner2f7c9632001-06-06 20:29:01 +000068}
69
Teresa Johnson63ee0e72018-06-26 13:56:49 +000070ParsedModuleAndIndex llvm::parseAssemblyWithIndex(
71 MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
72 SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
73 std::unique_ptr<Module> M =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000074 std::make_unique<Module>(F.getBufferIdentifier(), Context);
Teresa Johnson63ee0e72018-06-26 13:56:49 +000075 std::unique_ptr<ModuleSummaryIndex> Index =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000076 std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
Teresa Johnson63ee0e72018-06-26 13:56:49 +000077
78 if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
79 DataLayoutString))
80 return {nullptr, nullptr};
81
82 return {std::move(M), std::move(Index)};
83}
84
85ParsedModuleAndIndex llvm::parseAssemblyFileWithIndex(
86 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
87 SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
88 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
89 MemoryBuffer::getFileOrSTDIN(Filename);
90 if (std::error_code EC = FileOrErr.getError()) {
91 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
92 "Could not open input file: " + EC.message());
93 return {nullptr, nullptr};
94 }
95
96 return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
97 Context, Slots, UpgradeDebugInfo,
98 DataLayoutString);
99}
100
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000101std::unique_ptr<Module>
102llvm::parseAssemblyString(StringRef AsmString, SMDiagnostic &Err,
103 LLVMContext &Context, SlotMapping *Slots,
104 bool UpgradeDebugInfo, StringRef DataLayoutString) {
Rafael Espindolad96d5532014-08-26 21:49:01 +0000105 MemoryBufferRef F(AsmString, "<string>");
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000106 return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo,
107 DataLayoutString);
Chris Lattner416a0d42005-05-20 03:25:47 +0000108}
Alex Lorenzd2255952015-07-17 22:07:03 +0000109
Teresa Johnson63ee0e72018-06-26 13:56:49 +0000110static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F,
111 ModuleSummaryIndex &Index,
112 SMDiagnostic &Err) {
113 SourceMgr SM;
114 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
115 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
116
117 // The parser holds a reference to a context that is unused when parsing the
118 // index, but we need to initialize it.
119 LLVMContext unusedContext;
120 return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext).Run();
121}
122
123std::unique_ptr<ModuleSummaryIndex>
124llvm::parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err) {
125 std::unique_ptr<ModuleSummaryIndex> Index =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000126 std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
Teresa Johnson63ee0e72018-06-26 13:56:49 +0000127
128 if (parseSummaryIndexAssemblyInto(F, *Index, Err))
129 return nullptr;
130
131 return Index;
132}
133
134std::unique_ptr<ModuleSummaryIndex>
135llvm::parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err) {
136 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
137 MemoryBuffer::getFileOrSTDIN(Filename);
138 if (std::error_code EC = FileOrErr.getError()) {
139 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
140 "Could not open input file: " + EC.message());
141 return nullptr;
142 }
143
144 return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
145}
146
Alex Lorenzd2255952015-07-17 22:07:03 +0000147Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err,
Alex Lorenz1de2acd2015-08-21 21:32:39 +0000148 const Module &M, const SlotMapping *Slots) {
Alex Lorenzd2255952015-07-17 22:07:03 +0000149 SourceMgr SM;
150 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
151 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
152 Constant *C;
Teresa Johnson63ee0e72018-06-26 13:56:49 +0000153 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
Alex Lorenz1de2acd2015-08-21 21:32:39 +0000154 .parseStandaloneConstantValue(C, Slots))
Alex Lorenzd2255952015-07-17 22:07:03 +0000155 return nullptr;
156 return C;
157}
Quentin Colombet81e72b42016-03-07 22:09:05 +0000158
159Type *llvm::parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
160 const SlotMapping *Slots) {
Quentin Colombetdafed5d2016-03-08 00:37:07 +0000161 unsigned Read;
162 Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
163 if (!Ty)
164 return nullptr;
165 if (Read != Asm.size()) {
166 SourceMgr SM;
167 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
168 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
169 Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
170 SourceMgr::DK_Error, "expected end of string");
171 return nullptr;
172 }
173 return Ty;
174}
175Type *llvm::parseTypeAtBeginning(StringRef Asm, unsigned &Read,
176 SMDiagnostic &Err, const Module &M,
177 const SlotMapping *Slots) {
Quentin Colombet81e72b42016-03-07 22:09:05 +0000178 SourceMgr SM;
179 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
180 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
181 Type *Ty;
Teresa Johnson63ee0e72018-06-26 13:56:49 +0000182 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
Quentin Colombetdafed5d2016-03-08 00:37:07 +0000183 .parseTypeAtBeginning(Ty, Read, Slots))
Quentin Colombet81e72b42016-03-07 22:09:05 +0000184 return nullptr;
185 return Ty;
186}