diff options
author | Alon Zakai <azakai@google.com> | 2022-01-12 12:23:54 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-12 12:23:54 -0800 |
commit | 10b1ad720d5f5c991cd7049f8650011d89869f60 (patch) | |
tree | c1a66daa04a81fc1bf977f42e2581862dbd726bf /src | |
parent | b48c24f2f967b23ff65f23d7c3cccda4872b5926 (diff) | |
download | binaryen-10b1ad720d5f5c991cd7049f8650011d89869f60.tar.gz binaryen-10b1ad720d5f5c991cd7049f8650011d89869f60.tar.bz2 binaryen-10b1ad720d5f5c991cd7049f8650011d89869f60.zip |
[ctor-eval] Followup refactoring to use std::optional for EvalCtorOutcome (#4448)
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/wasm-ctor-eval.cpp | 37 |
1 files changed, 16 insertions, 21 deletions
diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index a117474a5..871d4eac0 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -489,19 +489,15 @@ private: } }; -struct EvalCtorOutcome { - // Whether we completely evalled the function (that is, we did not fail, and - // we did not only partially eval it). - bool evalledCompletely; - - // If the function was evalled completely, and it returns something, that - // value is given here. - Literals results; - - static EvalCtorOutcome incomplete() { return {false, Literals()}; } - - static EvalCtorOutcome complete(Literals results) { return {true, results}; } -}; +// The outcome of evalling a ctor is one of three states: +// +// 1. We failed to eval it completely (but perhaps we succeeded partially). In +// that case the std::optional here contains nothing. +// 2. We evalled it completely, and it is a function with no return value, so +// it contains an empty Literals. +// 3. We evalled it completely, and it is a function with a return value, so +// it contains Literals with those results. +using EvalCtorOutcome = std::optional<Literals>; // Eval a single ctor function. Returns whether we succeeded to completely // evaluate the ctor (which means that the caller can proceed to try to eval @@ -518,7 +514,7 @@ EvalCtorOutcome evalCtor(EvallingModuleInstance& instance, // TODO: Maybe use ignoreExternalInput? if (func->getNumParams() > 0) { std::cout << " ...stopping due to params\n"; - return EvalCtorOutcome::incomplete(); + return EvalCtorOutcome(); } // We want to handle the form of the global constructor function in LLVM. That @@ -642,9 +638,9 @@ EvalCtorOutcome evalCtor(EvallingModuleInstance& instance, // Return true if we evalled the entire block. Otherwise, even if we evalled // some of it, the caller must stop trying to eval further things. if (successes == block->list.size()) { - return EvalCtorOutcome::complete(results); + return EvalCtorOutcome(results); } else { - return EvalCtorOutcome::incomplete(); + return EvalCtorOutcome(); } } @@ -656,12 +652,12 @@ EvalCtorOutcome evalCtor(EvallingModuleInstance& instance, results = instance.callFunction(funcName, LiteralList()); } catch (FailToEvalException& fail) { std::cout << " ...stopping since could not eval: " << fail.why << "\n"; - return EvalCtorOutcome::incomplete(); + return EvalCtorOutcome(); } // Success! Apply the results. interface.applyToModule(); - return EvalCtorOutcome::complete(results); + return EvalCtorOutcome(results); } // Eval all ctors in a module. @@ -697,7 +693,7 @@ void evalCtors(Module& wasm, } auto funcName = ex->value; auto outcome = evalCtor(instance, interface, funcName, ctor); - if (!outcome.evalledCompletely) { + if (!outcome) { std::cout << " ...stopping\n"; return; } @@ -718,8 +714,7 @@ void evalCtors(Module& wasm, if (func->getResults() == Type::none) { copyFunc->body = Builder(wasm).makeNop(); } else { - copyFunc->body = - Builder(wasm).makeConstantExpression(outcome.results); + copyFunc->body = Builder(wasm).makeConstantExpression(*outcome); } wasm.getExport(exp->name)->value = copyName; } |