Skip to content

Commit

Permalink
Fix build of native image
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Apr 3, 2023
1 parent 8bd3e00 commit 1360be6
Showing 1 changed file with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Object sortPrimitives(State state, Object self, long ascending, Object comparato
interop.readArrayElement(self, i)
);
} else {
CompilerDirectives.transferToInterpreter();
throw new PanicException(
ctx.getBuiltins().error().makeUnsupportedArgumentsError(
new Object[]{self},
Expand All @@ -128,16 +129,28 @@ Object sortPrimitives(State state, Object self, long ascending, Object comparato
} catch (UnsupportedMessageException | InvalidArrayIndexException e) {
throw new IllegalStateException("Should not reach here", e);
}
var javaComparator = new DefaultComparator(lessThanNode, equalsNode, typeOfNode,
toTextNode, ascending > 0);
var javaComparator = createDefaultComparator(lessThanNode, equalsNode, typeOfNode, toTextNode,
ascending);
try {
return sortPrimitiveVector(elems, javaComparator, warningEncounteredProfile);
return sortPrimitiveVector(elems, javaComparator);
} catch (CompareException e) {
throw DataflowError.withoutTrace(
incomparableValuesError(e.leftOperand, e.rightOperand), this);
}
}

@TruffleBoundary
private DefaultSortComparator createDefaultComparator(
LessThanNode lessThanNode,
EqualsNode equalsNode,
TypeOfNode typeOfNode,
AnyToTextNode toTextNode,
long ascending
) {
return new DefaultSortComparator(lessThanNode, equalsNode, typeOfNode, toTextNode,
ascending > 0);
}

@TruffleBoundary
@Specialization(guards = {
"interop.hasArrayElements(self)",
Expand All @@ -157,7 +170,7 @@ Object sortGeneric(State state, Object self, long ascending, Object comparatorsA
List<Function> compareFuncs = readInteropArray(interop, warningsLib, compareFuncsArray);
List<Group> groups = splitByComparators(elems, comparators, compareFuncs);

// Prepare input for DefaultComparator and GenericComparator and sort the elements within groups
// Prepare input for DefaultSortComparator and GenericSortComparator and sort the elements within groups
var ctx = EnsoContext.get(this);
Atom less = ctx.getBuiltins().ordering().newLess();
Atom equal = ctx.getBuiltins().ordering().newEqual();
Expand All @@ -166,9 +179,9 @@ Object sortGeneric(State state, Object self, long ascending, Object comparatorsA
List<Object> resultVec = new ArrayList<>();
try {
for (var group : groups) {
Comparator javaComparator;
SortComparator javaComparator;
if (isNothing(byFunc) && isNothing(onFunc) && isPrimitiveGroup(group)) {
javaComparator = new DefaultComparator(
javaComparator = new DefaultSortComparator(
lessThanNode,
equalsNode,
typeOfNode,
Expand All @@ -177,7 +190,7 @@ Object sortGeneric(State state, Object self, long ascending, Object comparatorsA
);
} else {
Object compareFunc = isNothing(byFunc) ? group.compareFunc : byFunc;
javaComparator = new GenericComparator(
javaComparator = new GenericSortComparator(
ascending > 0,
compareFunc,
onFunc,
Expand Down Expand Up @@ -206,15 +219,14 @@ Object sortGeneric(State state, Object self, long ascending, Object comparatorsA
}
}

@TruffleBoundary(allowInlining = true)
private Object sortPrimitiveVector(Object[] elems,
DefaultComparator javaComparator, BranchProfile warningEncounteredProfile)
DefaultSortComparator javaComparator)
throws CompareException {
Arrays.sort(elems, javaComparator);
var sortedVector = Vector.fromArray(new Array(elems));

if (javaComparator.hasWarnings()) {
warningEncounteredProfile.enter();
CompilerDirectives.transferToInterpreter();
return attachWarnings(sortedVector, javaComparator.getEncounteredWarnings());
} else {
return sortedVector;
Expand Down Expand Up @@ -409,9 +421,9 @@ private boolean isNothing(Object object, EnsoContext ctx) {
* Group of elements grouped by comparator.
*
* @param elems Elements of the group.
* @param comparator Comparator for the elems, i.e., it should hold that
* @param comparator SortComparator for the elems, i.e., it should hold that
* {@code elems.each it-> (Comparable.from it) == comparator}.
* @param compareFunc `Comparator.compare` function extracted from the comparator.
* @param compareFunc `SortComparator.compare` function extracted from the comparator.
*/
private record Group(
List<Object> elems,
Expand All @@ -426,12 +438,12 @@ private record Group(
* incomparable values. The warnings are gathered as pure Strings in a hash set, so that they are
* not duplicated.
*/
private static abstract class Comparator implements java.util.Comparator<Object> {
private static abstract class SortComparator implements java.util.Comparator<Object> {

private final Set<String> warnings = new HashSet<>();
private final AnyToTextNode toTextNode;

protected Comparator(AnyToTextNode toTextNode) {
protected SortComparator(AnyToTextNode toTextNode) {
this.toTextNode = toTextNode;
}

Expand All @@ -447,29 +459,30 @@ public Set<String> getEncounteredWarnings() {
return warnings;
}

@TruffleBoundary
public boolean hasWarnings() {
return !warnings.isEmpty();
}
}

/**
* Comparator for comparing all values that have Default_Comparator. These are either primitive
* types, or the types that do not provide their own comparator.
* SortComparator for comparing all values that have Default_Comparator. These are either
* primitive types, or the types that do not provide their own comparator.
* <p>
* Note that it is essential for this class that the {@code by} method parameter to
* {@code Vector.sort} is set to the default value, which is {@code Ordering.compare}, because
* then, we know that the partial ordering for primitive types was not redefined by the user (we
* handle partial ordering for primitive types specifically, partial ordering for other types is
* not implemented yet - that requires topological sorting).
*/
private final class DefaultComparator extends Comparator {
final class DefaultSortComparator extends SortComparator {

private final LessThanNode lessThanNode;
private final EqualsNode equalsNode;
private final TypeOfNode typeOfNode;
private final boolean ascending;

private DefaultComparator(LessThanNode lessThanNode, EqualsNode equalsNode,
private DefaultSortComparator(LessThanNode lessThanNode, EqualsNode equalsNode,
TypeOfNode typeOfNode,
AnyToTextNode toTextNode, boolean ascending) {
super(toTextNode);
Expand Down Expand Up @@ -571,9 +584,9 @@ private int getPrimitiveValueCost(Object object) {
/**
* Comparator for any values. This comparator compares the values by calling back to Enso (by
* {@link #compareFunc}), rather than using compare nodes (i.e. {@link LessThanNode}). directly,
* as opposed to {@link DefaultComparator}.
* as opposed to {@link DefaultSortComparator}.
*/
private final class GenericComparator extends Comparator {
private final class GenericSortComparator extends SortComparator {

private final boolean ascending;
/**
Expand All @@ -591,7 +604,7 @@ private final class GenericComparator extends Comparator {
private final Atom greater;


private GenericComparator(
private GenericSortComparator(
boolean ascending,
Object compareFunc,
Object onFunc,
Expand Down

0 comments on commit 1360be6

Please sign in to comment.