Skip to content

Commit 10b1ad7

Browse files
authored
[ctor-eval] Followup refactoring to use std::optional for EvalCtorOutcome (#4448)
1 parent b48c24f commit 10b1ad7

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/tools/wasm-ctor-eval.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -489,19 +489,15 @@ struct CtorEvalExternalInterface : EvallingModuleInstance::ExternalInterface {
489489
}
490490
};
491491

492-
struct EvalCtorOutcome {
493-
// Whether we completely evalled the function (that is, we did not fail, and
494-
// we did not only partially eval it).
495-
bool evalledCompletely;
496-
497-
// If the function was evalled completely, and it returns something, that
498-
// value is given here.
499-
Literals results;
500-
501-
static EvalCtorOutcome incomplete() { return {false, Literals()}; }
502-
503-
static EvalCtorOutcome complete(Literals results) { return {true, results}; }
504-
};
492+
// The outcome of evalling a ctor is one of three states:
493+
//
494+
// 1. We failed to eval it completely (but perhaps we succeeded partially). In
495+
// that case the std::optional here contains nothing.
496+
// 2. We evalled it completely, and it is a function with no return value, so
497+
// it contains an empty Literals.
498+
// 3. We evalled it completely, and it is a function with a return value, so
499+
// it contains Literals with those results.
500+
using EvalCtorOutcome = std::optional<Literals>;
505501

506502
// Eval a single ctor function. Returns whether we succeeded to completely
507503
// evaluate the ctor (which means that the caller can proceed to try to eval
@@ -518,7 +514,7 @@ EvalCtorOutcome evalCtor(EvallingModuleInstance& instance,
518514
// TODO: Maybe use ignoreExternalInput?
519515
if (func->getNumParams() > 0) {
520516
std::cout << " ...stopping due to params\n";
521-
return EvalCtorOutcome::incomplete();
517+
return EvalCtorOutcome();
522518
}
523519

524520
// We want to handle the form of the global constructor function in LLVM. That
@@ -642,9 +638,9 @@ EvalCtorOutcome evalCtor(EvallingModuleInstance& instance,
642638
// Return true if we evalled the entire block. Otherwise, even if we evalled
643639
// some of it, the caller must stop trying to eval further things.
644640
if (successes == block->list.size()) {
645-
return EvalCtorOutcome::complete(results);
641+
return EvalCtorOutcome(results);
646642
} else {
647-
return EvalCtorOutcome::incomplete();
643+
return EvalCtorOutcome();
648644
}
649645
}
650646

@@ -656,12 +652,12 @@ EvalCtorOutcome evalCtor(EvallingModuleInstance& instance,
656652
results = instance.callFunction(funcName, LiteralList());
657653
} catch (FailToEvalException& fail) {
658654
std::cout << " ...stopping since could not eval: " << fail.why << "\n";
659-
return EvalCtorOutcome::incomplete();
655+
return EvalCtorOutcome();
660656
}
661657

662658
// Success! Apply the results.
663659
interface.applyToModule();
664-
return EvalCtorOutcome::complete(results);
660+
return EvalCtorOutcome(results);
665661
}
666662

667663
// Eval all ctors in a module.
@@ -697,7 +693,7 @@ void evalCtors(Module& wasm,
697693
}
698694
auto funcName = ex->value;
699695
auto outcome = evalCtor(instance, interface, funcName, ctor);
700-
if (!outcome.evalledCompletely) {
696+
if (!outcome) {
701697
std::cout << " ...stopping\n";
702698
return;
703699
}
@@ -718,8 +714,7 @@ void evalCtors(Module& wasm,
718714
if (func->getResults() == Type::none) {
719715
copyFunc->body = Builder(wasm).makeNop();
720716
} else {
721-
copyFunc->body =
722-
Builder(wasm).makeConstantExpression(outcome.results);
717+
copyFunc->body = Builder(wasm).makeConstantExpression(*outcome);
723718
}
724719
wasm.getExport(exp->name)->value = copyName;
725720
}

0 commit comments

Comments
 (0)