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 "llvm/Support/SourceMgr.h"
16 
17 using namespace mlir;
18 
19 LogicalResult mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr,
20                                     Block *block, const ParserConfig &config,
21                                     LocationAttr *sourceFileLoc) {
22   const auto *sourceBuf = sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
23   if (sourceFileLoc) {
24     *sourceFileLoc = FileLineColLoc::get(config.getContext(),
25                                          sourceBuf->getBufferIdentifier(),
26                                          /*line=*/0, /*column=*/0);
27   }
28   return parseAsmSourceFile(sourceMgr, block, config);
29 }
30 
31 LogicalResult mlir::parseSourceFile(llvm::StringRef filename, Block *block,
32                                     const ParserConfig &config,
33                                     LocationAttr *sourceFileLoc) {
34   llvm::SourceMgr sourceMgr;
35   return parseSourceFile(filename, sourceMgr, block, config, sourceFileLoc);
36 }
37 
38 LogicalResult mlir::parseSourceFile(llvm::StringRef filename,
39                                     llvm::SourceMgr &sourceMgr, Block *block,
40                                     const ParserConfig &config,
41                                     LocationAttr *sourceFileLoc) {
42   if (sourceMgr.getNumBuffers() != 0) {
43     // TODO: Extend to support multiple buffers.
44     return emitError(mlir::UnknownLoc::get(config.getContext()),
45                      "only main buffer parsed at the moment");
46   }
47   auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(filename);
48   if (std::error_code error = fileOrErr.getError())
49     return emitError(mlir::UnknownLoc::get(config.getContext()),
50                      "could not open input file " + filename);
51 
52   // Load the MLIR source file.
53   sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
54   return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
55 }
56 
57 LogicalResult mlir::parseSourceString(llvm::StringRef sourceStr, Block *block,
58                                       const ParserConfig &config,
59                                       LocationAttr *sourceFileLoc) {
60   auto memBuffer = llvm::MemoryBuffer::getMemBuffer(sourceStr);
61   if (!memBuffer)
62     return failure();
63 
64   llvm::SourceMgr sourceMgr;
65   sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
66   return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
67 }
68