Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add translation layer from pattern matching exceptions #4069

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package org.kframework.utils.errorsystem;

import java.util.Objects;
import java.util.Optional;
import org.kframework.attributes.HasLocation;
import org.kframework.attributes.Location;
import org.kframework.attributes.Source;
Expand Down Expand Up @@ -96,6 +97,17 @@ public static KEMException internalError(String message, Throwable e, HasLocatio
node.source().orElse(null));
}

public static KEMException internalError(
String message, Throwable e, Optional<Location> location, Optional<Source> source) {
return create(
ExceptionType.ERROR,
KExceptionGroup.INTERNAL,
message,
e,
location.orElse(null),
source.orElse(null));
}

public static KEMException compilerError(String message) {
return create(ExceptionType.ERROR, KExceptionGroup.COMPILER, message, null, null, null);
}
Expand Down Expand Up @@ -124,6 +136,17 @@ public static KEMException compilerError(String message, Throwable e, HasLocatio
node.source().orElse(null));
}

public static KEMException compilerError(
String message, Throwable e, Optional<Location> location, Optional<Source> source) {
return create(
ExceptionType.ERROR,
KExceptionGroup.COMPILER,
message,
e,
location.orElse(null),
source.orElse(null));
}

public static KEMException innerParserError(String message) {
return create(ExceptionType.ERROR, KExceptionGroup.INNER_PARSER, message, null, null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import org.kframework.attributes.Att;
import org.kframework.backend.kore.KoreBackend;
import org.kframework.backend.llvm.matching.Matching;
import org.kframework.backend.llvm.matching.MatchingException;
import org.kframework.compile.Backend;
import org.kframework.kompile.KompileOptions;
import org.kframework.main.GlobalOptions;
import org.kframework.main.Tool;
import org.kframework.utils.Stopwatch;
import org.kframework.utils.errorsystem.KEMException;
import org.kframework.utils.errorsystem.KException;
import org.kframework.utils.errorsystem.KException.ExceptionType;
import org.kframework.utils.errorsystem.KExceptionManager;
import org.kframework.utils.file.FileUtil;
Expand Down Expand Up @@ -66,8 +68,9 @@ public void accept(Backend.Holder h) {
globalOptions.includesExceptionType(ExceptionType.USELESS_RULE),
options.enableSearch,
ex -> {
kem.addKException(ex);
if (globalOptions.includesExceptionType(ex.getType())) {
var translated = translateError(ex);
kem.addKException(translated);
if (globalOptions.includesExceptionType(translated.getType())) {
warnings.increment();
}
return null;
Expand Down Expand Up @@ -176,6 +179,36 @@ private void llvmKompile(String type, String executable) {
sw.printIntermediate(" \u2514" + executable + ": " + type);
}

private KException translateError(MatchingException ex) {
switch (ex.getType()) {
case USELESS_RULE -> {
return new KException(
ExceptionType.USELESS_RULE,
KException.KExceptionGroup.COMPILER,
ex.getMessage(),
ex.getSource().get(),
ex.getLocation().get());
}

case NON_EXHAUSTIVE_MATCH -> {
return new KException(
ExceptionType.NON_EXHAUSTIVE_MATCH,
KException.KExceptionGroup.COMPILER,
ex.getMessage(),
ex.getSource().get(),
ex.getLocation().get());
}

case INTERNAL_ERROR -> throw KEMException.internalError(
ex.getMessage(), ex, ex.getLocation(), ex.getSource());

case COMPILER_ERROR -> throw KEMException.compilerError(
ex.getMessage(), ex, ex.getLocation(), ex.getSource());
}

throw KEMException.criticalError("Unhandled pattern matching exception", ex);
}

private String getThreshold() {
if (!options.iterated && !kompileOptions.optimize3) {
return "0";
Expand Down
Loading