Extend CompositeFIF instead of using FIFBuilder.
This commit is contained in:
+8
-9
@@ -37,7 +37,7 @@ import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternB
|
|||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
public final class ArrayFIF {
|
public final class ArrayFIF extends CompositeFIF {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static final NamePredicate ARRAYS;
|
private static final NamePredicate ARRAYS;
|
||||||
@@ -85,15 +85,14 @@ public final class ArrayFIF {
|
|||||||
public static final BuiltInPropertyIntrinsic ARRAY_LENGTH_INTRINSIC = new BuiltInPropertyIntrinsic("length");
|
public static final BuiltInPropertyIntrinsic ARRAY_LENGTH_INTRINSIC = new BuiltInPropertyIntrinsic("length");
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
|
public static final FunctionIntrinsicFactory INSTANCE = new ArrayFIF();
|
||||||
.add(pattern(ARRAYS, "get"), GET_INTRINSIC)
|
|
||||||
.add(pattern(ARRAYS, "set"), SET_INTRINSIC)
|
|
||||||
.add(pattern(ARRAYS, "<get-size>"), ARRAY_LENGTH_INTRINSIC)
|
|
||||||
.add(pattern(ARRAYS, "<get-indices>"), new CallStandardMethodIntrinsic("Kotlin.arrayIndices", true, 0))
|
|
||||||
.add(pattern(ARRAYS, "iterator"), new CallStandardMethodIntrinsic("Kotlin.arrayIterator", true, 0))
|
|
||||||
.add(pattern(ARRAYS, "<init>"), new CallStandardMethodIntrinsic("Kotlin.arrayFromFun", false, 2))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
private ArrayFIF() {
|
private ArrayFIF() {
|
||||||
|
add(pattern(ARRAYS, "get"), GET_INTRINSIC);
|
||||||
|
add(pattern(ARRAYS, "set"), SET_INTRINSIC);
|
||||||
|
add(pattern(ARRAYS, "<get-size>"), ARRAY_LENGTH_INTRINSIC);
|
||||||
|
add(pattern(ARRAYS, "<get-indices>"), new CallStandardMethodIntrinsic("Kotlin.arrayIndices", true, 0));
|
||||||
|
add(pattern(ARRAYS, "iterator"), new CallStandardMethodIntrinsic("Kotlin.arrayIterator", true, 0));
|
||||||
|
add(pattern(ARRAYS, "<init>"), new CallStandardMethodIntrinsic("Kotlin.arrayFromFun", false, 2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-37
@@ -31,51 +31,38 @@ import java.util.Map;
|
|||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
public final class FIFBuilder {
|
public abstract class CompositeFIF implements FunctionIntrinsicFactory {
|
||||||
|
|
||||||
@NotNull final Map<DescriptorPredicate, FunctionIntrinsic> patternToIntrinsic = Maps.newHashMap();
|
@NotNull final Map<DescriptorPredicate, FunctionIntrinsic> patternToIntrinsic = Maps.newHashMap();
|
||||||
|
|
||||||
private FIFBuilder() {
|
protected CompositeFIF() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static FIFBuilder start() {
|
@Override
|
||||||
return new FIFBuilder();
|
public Predicate<FunctionDescriptor> getPredicate() {
|
||||||
}
|
Collection<DescriptorPredicate> patterns = patternToIntrinsic.keySet();
|
||||||
|
final DescriptorPredicate[] patterns1 = patterns.toArray(new DescriptorPredicate[patterns.size()]);
|
||||||
@NotNull
|
return new DescriptorPredicate() {
|
||||||
public FIFBuilder add(@NotNull DescriptorPredicate pattern, @NotNull FunctionIntrinsic intrinsic) {
|
|
||||||
patternToIntrinsic.put(pattern, intrinsic);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public FunctionIntrinsicFactory build() {
|
|
||||||
return new FunctionIntrinsicFactory() {
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
@Override
|
||||||
public Predicate<FunctionDescriptor> getPredicate() {
|
public boolean apply(@Nullable FunctionDescriptor descriptor) {
|
||||||
Collection<DescriptorPredicate> patterns = patternToIntrinsic.keySet();
|
return Predicates.or(patterns1).apply(descriptor);
|
||||||
final DescriptorPredicate[] patterns1 = patterns.toArray(new DescriptorPredicate[patterns.size()]);
|
|
||||||
return new DescriptorPredicate() {
|
|
||||||
@Override
|
|
||||||
public boolean apply(@Nullable FunctionDescriptor descriptor) {
|
|
||||||
return Predicates.or(patterns1).apply(descriptor);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public FunctionIntrinsic getIntrinsic(@NotNull FunctionDescriptor descriptor) {
|
|
||||||
for (DescriptorPredicate pattern : patternToIntrinsic.keySet()) {
|
|
||||||
if (pattern.apply(descriptor)) {
|
|
||||||
return patternToIntrinsic.get(pattern);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new IllegalStateException("Must have intrinsic for pattern.");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public FunctionIntrinsic getIntrinsic(@NotNull FunctionDescriptor descriptor) {
|
||||||
|
for (DescriptorPredicate pattern : patternToIntrinsic.keySet()) {
|
||||||
|
if (pattern.apply(descriptor)) {
|
||||||
|
return patternToIntrinsic.get(pattern);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalStateException("Must have intrinsic for pattern.");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void add(@NotNull DescriptorPredicate pattern, @NotNull FunctionIntrinsic intrinsic) {
|
||||||
|
patternToIntrinsic.put(pattern, intrinsic);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+5
-6
@@ -36,7 +36,7 @@ import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternB
|
|||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
public final class NumberConversionFIF {
|
public final class NumberConversionFIF extends CompositeFIF {
|
||||||
@NotNull
|
@NotNull
|
||||||
private static final NamePredicate SUPPORTED_CONVERSIONS;
|
private static final NamePredicate SUPPORTED_CONVERSIONS;
|
||||||
|
|
||||||
@@ -74,12 +74,11 @@ public final class NumberConversionFIF {
|
|||||||
@NotNull
|
@NotNull
|
||||||
public static final String FLOATING_POINT_NUMBER_TYPES = "Float|Double|Number";
|
public static final String FLOATING_POINT_NUMBER_TYPES = "Float|Double|Number";
|
||||||
@NotNull
|
@NotNull
|
||||||
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
|
public static final FunctionIntrinsicFactory INSTANCE = new NumberConversionFIF();
|
||||||
.add(pattern(INTEGER_NUMBER_TYPES, SUPPORTED_CONVERSIONS), RETURN_RECEIVER)
|
|
||||||
.add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0))
|
|
||||||
.add(pattern(FLOATING_POINT_NUMBER_TYPES, FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
private NumberConversionFIF() {
|
private NumberConversionFIF() {
|
||||||
|
add(pattern(INTEGER_NUMBER_TYPES, SUPPORTED_CONVERSIONS), RETURN_RECEIVER);
|
||||||
|
add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0));
|
||||||
|
add(pattern(FLOATING_POINT_NUMBER_TYPES, FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-6
@@ -35,7 +35,7 @@ import static org.jetbrains.k2js.translate.utils.JsAstUtils.setQualifier;
|
|||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
public final class StringOperationFIF {
|
public final class StringOperationFIF extends CompositeFIF {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static final DescriptorPredicate GET_PATTERN = pattern("String.get");
|
private static final DescriptorPredicate GET_PATTERN = pattern("String.get");
|
||||||
@@ -57,12 +57,11 @@ public final class StringOperationFIF {
|
|||||||
};
|
};
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
|
public static final FunctionIntrinsicFactory INSTANCE = new StringOperationFIF();
|
||||||
.add(GET_PATTERN, GET_INTRINSIC)
|
|
||||||
.add(pattern("String.<get-length>"), new BuiltInPropertyIntrinsic("length"))
|
|
||||||
.add(pattern("CharSequence.<get-length>"), new BuiltInPropertyIntrinsic("length"))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
private StringOperationFIF() {
|
private StringOperationFIF() {
|
||||||
|
add(GET_PATTERN, GET_INTRINSIC);
|
||||||
|
add(pattern("String.<get-length>"), new BuiltInPropertyIntrinsic("length"));
|
||||||
|
add(pattern("CharSequence.<get-length>"), new BuiltInPropertyIntrinsic("length"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-6
@@ -26,18 +26,17 @@ import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternB
|
|||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
public final class TopLevelFIF {
|
public final class TopLevelFIF extends CompositeFIF {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static final DescriptorPredicate EXT_TO_STRING = pattern("toString");
|
private static final DescriptorPredicate EXT_TO_STRING = pattern("toString");
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
|
public static final FunctionIntrinsicFactory INSTANCE = new TopLevelFIF();
|
||||||
.add(EXT_TO_STRING, new BuiltInFunctionIntrinsic("toString"))
|
|
||||||
//TODO: add intrinsic for calling equals explicitly
|
|
||||||
.add(pattern("arrayOfNulls"), new CallStandardMethodIntrinsic("Kotlin.nullArray", false, 1))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
private TopLevelFIF() {
|
private TopLevelFIF() {
|
||||||
|
add(EXT_TO_STRING, new BuiltInFunctionIntrinsic("toString"));
|
||||||
|
//TODO: add intrinsic for calling equals explicitly
|
||||||
|
add(pattern("arrayOfNulls"), new CallStandardMethodIntrinsic("Kotlin.nullArray", false, 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user