@@ -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
182186void 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
730736Result<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