diff --git a/compiler/include/zamalang/Support/Error.h b/compiler/include/zamalang/Support/Error.h new file mode 100644 index 000000000..633b0e716 --- /dev/null +++ b/compiler/include/zamalang/Support/Error.h @@ -0,0 +1,53 @@ +#ifndef ZAMALANG_SUPPORT_STRING_ERROR_H +#define ZAMALANG_SUPPORT_STRING_ERROR_H + +#include + +namespace mlir { +namespace zamalang { + +// Internal error class that allows for composing `llvm::Error`s +// similar to `llvm::createStringError()`, but using stream-like +// composition with `operator<<`. +// +// Example: +// +// llvm::Error foo(int i, size_t s, ...) { +// ... +// if(...) { +// return StreamStringError() +// << "Some error message with an integer: " +// << i << " and a size_t: " << s; +// } +// ... +// } +class StreamStringError { +public: + StreamStringError(const llvm::StringRef &s) : buffer(s.str()), os(buffer){}; + StreamStringError() : buffer(""), os(buffer){}; + + template StreamStringError &operator<<(const T &v) { + this->os << v; + return *this; + } + + operator llvm::Error() { + return llvm::make_error(os.str(), + llvm::inconvertibleErrorCode()); + } + + template operator llvm::Expected() { + return this->operator llvm::Error(); + } + +protected: + std::string buffer; + llvm::raw_string_ostream os; +}; + +StreamStringError &operator<<(StreamStringError &se, llvm::Error &err); + +} // namespace zamalang +} // namespace mlir + +#endif diff --git a/compiler/lib/Support/CMakeLists.txt b/compiler/lib/Support/CMakeLists.txt index 82c3077ca..9694989d3 100644 --- a/compiler/lib/Support/CMakeLists.txt +++ b/compiler/lib/Support/CMakeLists.txt @@ -1,4 +1,5 @@ add_mlir_library(ZamalangSupport + Error.cpp Pipeline.cpp Jit.cpp CompilerEngine.cpp diff --git a/compiler/lib/Support/Error.cpp b/compiler/lib/Support/Error.cpp new file mode 100644 index 000000000..32cfa6399 --- /dev/null +++ b/compiler/lib/Support/Error.cpp @@ -0,0 +1,12 @@ +#include + +namespace mlir { +namespace zamalang { +// Specialized `operator<<` for `llvm::Error` that marks the error +// as checked through `std::move` and `llvm::toString` +StreamStringError &operator<<(StreamStringError &se, llvm::Error &err) { + se << llvm::toString(std::move(err)); + return se; +} +} // namespace zamalang +} // namespace mlir