LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
IncludeHelper.h
Go to the documentation of this file.
1//===-- IncludeHelper.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
10#pragma once
11
12#include <mlir/IR/BuiltinOps.h>
13#include <mlir/IR/OwningOpRef.h>
14#include <mlir/Support/LLVM.h>
15#include <mlir/Support/LogicalResult.h>
16
17#include <llvm/ADT/SmallString.h>
18#include <llvm/Support/Path.h>
19#include <llvm/Support/SourceMgr.h>
20
21namespace llzk {
22
24 std::vector<std::string> includeDirectories;
25
26public:
27 static GlobalSourceMgr &get() {
28 static GlobalSourceMgr theInstance;
29 return theInstance;
30 }
31
32 mlir::LogicalResult setup(const std::vector<std::string> &includeDirs) {
33 includeDirectories = includeDirs;
34 return mlir::success();
35 }
36
37 // Adapted from mlir::SourceMgr::OpenIncludeFile() because SourceMgr is
38 // not a mature, usable component of MLIR.
39 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
40 openIncludeFile(const mlir::StringRef filename, std::string &resolvedFile) {
41 auto result = llvm::MemoryBuffer::getFile(filename);
42
43 llvm::SmallString<64> pathBuffer(filename);
44 // If the file didn't exist directly, see if it's in an include path.
45 for (unsigned i = 0, e = includeDirectories.size(); i != e && !result; ++i) {
46 pathBuffer = includeDirectories[i];
47 llvm::sys::path::append(pathBuffer, filename);
48 result = llvm::MemoryBuffer::getFile(pathBuffer);
49 }
50
51 if (result) {
52 resolvedFile = static_cast<std::string>(pathBuffer);
53 }
54
55 return result;
56 }
57};
58
59} // namespace llzk
static GlobalSourceMgr & get()
mlir::LogicalResult setup(const std::vector< std::string > &includeDirs)
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > openIncludeFile(const mlir::StringRef filename, std::string &resolvedFile)