Skip to content

Commit e308a5c

Browse files
committed
Use variant
1 parent a9d8a29 commit e308a5c

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

src/wasm-ir-builder.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,17 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
238238
Module& wasm;
239239
Function* func;
240240
Builder builder;
241-
// We distinguish three cases:
242-
// - no debug location has been specified for the next instruction;
243-
// then this instruction may inherit a debug location from its
244-
// parent or a previous sibling;
245-
// - we explicitly specified that this instruction has no debug location;
246-
// - we provided a debug location for this instruction.
247-
std::optional<std::optional<Function::DebugLocation>> debugLoc;
241+
242+
// The location lacks debug info as it was marked as not having it.
243+
struct NoDebug : public std::monostate {};
244+
// The location lacks debug info, but was not marked as not having
245+
// it, and it can receive it from the parent or its previous sibling
246+
// (if it has one).
247+
struct CanReceiveDebug : public std::monostate {};
248+
using DebugVariant =
249+
std::variant<NoDebug, CanReceiveDebug, Function::DebugLocation>;
250+
251+
DebugVariant debugLoc;
248252

249253
struct ChildPopper;
250254

src/wasm/wasm-ir-builder.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,29 @@ void IRBuilder::setDebugLocation(
176176
} else {
177177
DBG(std::cerr << "setting debugloc to none\n";);
178178
}
179-
debugLoc = loc;
179+
if (loc) {
180+
debugLoc = *loc;
181+
} else {
182+
debugLoc = NoDebug();
183+
}
180184
}
181185

182186
void IRBuilder::applyDebugLoc(Expression* expr) {
183-
if (debugLoc) {
187+
if (!std::get_if<CanReceiveDebug>(&debugLoc)) {
184188
if (func) {
185-
if (*debugLoc) {
186-
DBG(std::cerr << "applying debugloc " << *debugLoc->fileIndex << ":"
187-
<< *debugLoc->lineNumber << ":" << *debugLoc->columnNumber
189+
if (auto* loc = std::get_if<Function::DebugLocation>(&debugLoc)) {
190+
DBG(std::cerr << "applying debugloc " << loc->fileIndex << ":"
191+
<< loc->lineNumber << ":" << loc->columnNumber
188192
<< " to expression " << ShallowExpression{expr} << "\n");
193+
func->debugLocations[expr] = *loc;
189194
} else {
195+
assert(std::get_if<NoDebug>(&debugLoc));
190196
DBG(std::cerr << "applying debugloc to expression "
191-
<< ShallowExpression{expr} << "\n");
197+
<< ShallowExpression{expr} << "\n");
198+
func->debugLocations[expr] = std::nullopt;
192199
}
193-
func->debugLocations[expr] = *debugLoc;
194200
}
195-
debugLoc.reset();
201+
debugLoc = CanReceiveDebug();
196202
}
197203
}
198204

@@ -687,10 +693,10 @@ Result<> IRBuilder::visitFunctionStart(Function* func) {
687693
if (!scopeStack.empty()) {
688694
return Err{"unexpected start of function"};
689695
}
690-
if (debugLoc && *debugLoc) {
691-
func->prologLocation.insert(**debugLoc);
696+
if (auto* loc = std::get_if<Function::DebugLocation>(&debugLoc)) {
697+
func->prologLocation.insert(*loc);
692698
}
693-
debugLoc.reset();
699+
debugLoc = CanReceiveDebug();
694700
scopeStack.push_back(ScopeCtx::makeFunc(func));
695701
this->func = func;
696702
return Ok{};
@@ -728,12 +734,13 @@ Result<> IRBuilder::visitTryTableStart(TryTable* trytable, Name label) {
728734
}
729735

730736
Result<Expression*> IRBuilder::finishScope(Block* block) {
731-
if (debugLoc && *debugLoc) {
732-
DBG(std::cerr << "discarding debugloc " << *debugLoc->fileIndex << ":"
733-
<< *debugLoc->lineNumber << ":" << *debugLoc->columnNumber
734-
<< "\n");
737+
#if IR_BUILDER_DEBUG
738+
if (auto* loc = std::get_if<Function::DebugLocation>(&debugLoc)) {
739+
std::cerr << "discarding debugloc " << loc->fileIndex << ":"
740+
<< loc->lineNumber << ":" << loc->columnNumber << "\n";
735741
}
736-
debugLoc.reset();
742+
#endif
743+
debugLoc = CanReceiveDebug();
737744

738745
if (scopeStack.empty() || scopeStack.back().isNone()) {
739746
return Err{"unexpected end of scope"};
@@ -918,10 +925,12 @@ Result<> IRBuilder::visitEnd() {
918925
if (scope.isNone()) {
919926
return Err{"unexpected end"};
920927
}
921-
if (auto* func = scope.getFunction(); func && debugLoc && *debugLoc) {
922-
func->epilogLocation.insert(**debugLoc);
928+
if (auto* func = scope.getFunction(); func) {
929+
if (auto* loc = std::get_if<Function::DebugLocation>(&debugLoc)) {
930+
func->epilogLocation.insert(*loc);
931+
}
923932
}
924-
debugLoc.reset();
933+
debugLoc = CanReceiveDebug();
925934
auto expr = finishScope(scope.getBlock());
926935
CHECK_ERR(expr);
927936

0 commit comments

Comments
 (0)