1//===- Parser.cpp - MLIR Unified Parser Interface -------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the parser for the MLIR textual form.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Parser/Parser.h"
14#include "mlir/AsmParser/AsmParser.h"
15#include "mlir/Bytecode/BytecodeReader.h"
16#include "llvm/Support/SourceMgr.h"
17
18using namespace mlir;
19
20LogicalResult mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr,
21 Block *block, const ParserConfig &config,
22 LocationAttr *sourceFileLoc) {
23 const auto *sourceBuf = sourceMgr.getMemoryBuffer(i: sourceMgr.getMainFileID());
24 if (sourceFileLoc) {
25 *sourceFileLoc = FileLineColLoc::get(config.getContext(),
26 sourceBuf->getBufferIdentifier(),
27 /*line=*/0, /*column=*/0);
28 }
29 if (isBytecode(buffer: *sourceBuf))
30 return readBytecodeFile(buffer: *sourceBuf, block, config);
31 return parseAsmSourceFile(sourceMgr, block, config);
32}
33LogicalResult
34mlir::parseSourceFile(const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
35 Block *block, const ParserConfig &config,
36 LocationAttr *sourceFileLoc) {
37 const auto *sourceBuf =
38 sourceMgr->getMemoryBuffer(i: sourceMgr->getMainFileID());
39 if (sourceFileLoc) {
40 *sourceFileLoc = FileLineColLoc::get(config.getContext(),
41 sourceBuf->getBufferIdentifier(),
42 /*line=*/0, /*column=*/0);
43 }
44 if (isBytecode(buffer: *sourceBuf))
45 return readBytecodeFile(sourceMgr, block, config);
46 return parseAsmSourceFile(sourceMgr: *sourceMgr, block, config);
47}
48
49LogicalResult mlir::parseSourceFile(llvm::StringRef filename, Block *block,
50 const ParserConfig &config,
51 LocationAttr *sourceFileLoc) {
52 auto sourceMgr = std::make_shared<llvm::SourceMgr>();
53 return parseSourceFile(filename, sourceMgr, block, config, sourceFileLoc);
54}
55
56static LogicalResult loadSourceFileBuffer(llvm::StringRef filename,
57 llvm::SourceMgr &sourceMgr,
58 MLIRContext *ctx) {
59 if (sourceMgr.getNumBuffers() != 0) {
60 // TODO: Extend to support multiple buffers.
61 return emitError(mlir::UnknownLoc::get(ctx),
62 "only main buffer parsed at the moment");
63 }
64 auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(Filename: filename);
65 if (fileOrErr.getError())
66 return emitError(mlir::UnknownLoc::get(ctx),
67 "could not open input file " + filename);
68
69 // Load the MLIR source file.
70 sourceMgr.AddNewSourceBuffer(F: std::move(*fileOrErr), IncludeLoc: SMLoc());
71 return success();
72}
73
74LogicalResult mlir::parseSourceFile(llvm::StringRef filename,
75 llvm::SourceMgr &sourceMgr, Block *block,
76 const ParserConfig &config,
77 LocationAttr *sourceFileLoc) {
78 if (failed(result: loadSourceFileBuffer(filename, sourceMgr, ctx: config.getContext())))
79 return failure();
80 return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
81}
82LogicalResult mlir::parseSourceFile(
83 llvm::StringRef filename, const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
84 Block *block, const ParserConfig &config, LocationAttr *sourceFileLoc) {
85 if (failed(result: loadSourceFileBuffer(filename, sourceMgr&: *sourceMgr, ctx: config.getContext())))
86 return failure();
87 return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
88}
89
90LogicalResult mlir::parseSourceString(llvm::StringRef sourceStr, Block *block,
91 const ParserConfig &config,
92 StringRef sourceName,
93 LocationAttr *sourceFileLoc) {
94 auto memBuffer =
95 llvm::MemoryBuffer::getMemBuffer(InputData: sourceStr, BufferName: sourceName,
96 /*RequiresNullTerminator=*/false);
97 if (!memBuffer)
98 return failure();
99
100 llvm::SourceMgr sourceMgr;
101 sourceMgr.AddNewSourceBuffer(F: std::move(memBuffer), IncludeLoc: SMLoc());
102 return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
103}
104

source code of mlir/lib/Parser/Parser.cpp