From c37bda1d06a4c81c443f89572bdc6d2b443ed40c Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Mon, 14 Aug 2023 15:50:34 -0700 Subject: [NFC] Factor `Result` and `MaybeResult` into a utility header (#5878) Allow them to be used for more than just the new text parser. --- src/support/result.h | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ src/wasm/wat-parser.cpp | 5 --- src/wat-parser.h | 50 +--------------------------- 3 files changed, 88 insertions(+), 54 deletions(-) create mode 100644 src/support/result.h (limited to 'src') diff --git a/src/support/result.h b/src/support/result.h new file mode 100644 index 000000000..acbf92966 --- /dev/null +++ b/src/support/result.h @@ -0,0 +1,87 @@ +/* + * Copyright 2023 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef wasm_support_result_h +#define wasm_support_result_h + +#include +#include + +namespace wasm { + +// Represents a non-erroneous result with no associated data. +struct Ok {}; + +// Represents the non-erroneous absence of a result. +struct None {}; + +// Represents an erroneous result with associated error message. +struct Err { + std::string msg; +}; + +// Check a Result or MaybeResult for error and return the error if it exists. +#define CHECK_ERR(val) \ + if (auto _val = (val); auto err = _val.getErr()) { \ + return Err{*err}; \ + } + +// Represent a result of type T or an error message. +template struct Result { + std::variant val; + + Result(Result& other) = default; + Result(Result&& other) = default; + Result(const Err& e) : val(std::in_place_type, e) {} + Result(Err&& e) : val(std::in_place_type, std::move(e)) {} + template + Result(U&& u) : val(std::in_place_type, std::forward(u)) {} + + Err* getErr() { return std::get_if(&val); } + T& operator*() { return *std::get_if(&val); } + T* operator->() { return std::get_if(&val); } +}; + +// Represent an optional result of type T or an error message. +template struct MaybeResult { + std::variant val; + + MaybeResult() : val(None{}) {} + MaybeResult(MaybeResult& other) = default; + MaybeResult(MaybeResult&& other) = default; + MaybeResult(const Err& e) : val(std::in_place_type, e) {} + MaybeResult(Err&& e) : val(std::in_place_type, std::move(e)) {} + template + MaybeResult(U&& u) : val(std::in_place_type, std::forward(u)) {} + template + MaybeResult(Result&& u) + : val(u.getErr() ? std::variant{*u.getErr()} + : std::variant{*u}) {} + + // Whether we have an error or a value. Useful for assignment in loops and if + // conditions where errors should not get lost. + operator bool() const { return !std::holds_alternative(val); } + + Err* getErr() { return std::get_if(&val); } + T& operator*() { return *std::get_if(&val); } + T* operator->() { return std::get_if(&val); } + + T* getPtr() { return std::get_if(&val); } +}; + +} // namespace wasm + +#endif // wasm_support_result_h diff --git a/src/wasm/wat-parser.cpp b/src/wasm/wat-parser.cpp index d5bda85fe..970901409 100644 --- a/src/wasm/wat-parser.cpp +++ b/src/wasm/wat-parser.cpp @@ -56,11 +56,6 @@ // may be passed the appropriate context type and return the correct result type // for each phase. -#define CHECK_ERR(val) \ - if (auto _val = (val); auto err = _val.getErr()) { \ - return Err{*err}; \ - } - using namespace std::string_view_literals; namespace wasm::WATParser { diff --git a/src/wat-parser.h b/src/wat-parser.h index ebeb566c4..bb31ed0b1 100644 --- a/src/wat-parser.h +++ b/src/wat-parser.h @@ -19,59 +19,11 @@ #include +#include "support/result.h" #include "wasm.h" namespace wasm::WATParser { -struct Ok {}; - -struct None {}; - -struct Err { - std::string msg; -}; - -template struct Result { - std::variant val; - - Result(Result& other) = default; - Result(Result&& other) = default; - Result(const Err& e) : val(std::in_place_type, e) {} - Result(Err&& e) : val(std::in_place_type, std::move(e)) {} - template - Result(U&& u) : val(std::in_place_type, std::forward(u)) {} - - Err* getErr() { return std::get_if(&val); } - T& operator*() { return *std::get_if(&val); } - T* operator->() { return std::get_if(&val); } -}; - -template struct MaybeResult { - std::variant val; - - MaybeResult() : val(None{}) {} - MaybeResult(MaybeResult& other) = default; - MaybeResult(MaybeResult&& other) = default; - MaybeResult(const Err& e) : val(std::in_place_type, e) {} - MaybeResult(Err&& e) : val(std::in_place_type, std::move(e)) {} - template - MaybeResult(U&& u) : val(std::in_place_type, std::forward(u)) {} - template - MaybeResult(Result&& u) - : val(u.getErr() ? std::variant{*u.getErr()} - : std::variant{*u}) {} - - // Whether we have an error or a value. Useful for assignment in loops and if - // conditions where errors should not get lost. - operator bool() const { return !std::holds_alternative(val); } - - Err* getErr() { return std::get_if(&val); } - T& operator*() { return *std::get_if(&val); } - T* operator->() { return std::get_if(&val); } - - T* getPtr() { return std::get_if(&val); } -}; - // Parse a single WAT module. Result<> parseModule(Module& wasm, std::string_view in); -- cgit v1.2.3