From 723e739960bb5ebb78ba76880cfd044ff9610cc5 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Thu, 10 Jan 2019 16:03:45 +0300 Subject: [PATCH] Minor: convert ProgressionIteratorBasicValue to Kotlin --- .../boxing/ProgressionIteratorBasicValue.java | 103 ------------------ .../boxing/ProgressionIteratorBasicValue.kt | 72 ++++++++++++ 2 files changed, 72 insertions(+), 103 deletions(-) delete mode 100644 compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/ProgressionIteratorBasicValue.java create mode 100644 compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/ProgressionIteratorBasicValue.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/ProgressionIteratorBasicValue.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/ProgressionIteratorBasicValue.java deleted file mode 100644 index 40bbca8950d..00000000000 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/ProgressionIteratorBasicValue.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.codegen.optimization.boxing; - -import com.google.common.collect.ImmutableMap; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.builtins.PrimitiveType; -import org.jetbrains.kotlin.codegen.intrinsics.IteratorNext; -import org.jetbrains.kotlin.codegen.optimization.common.StrictBasicValue; -import org.jetbrains.kotlin.codegen.range.RangeCodegenUtilKt; -import org.jetbrains.kotlin.name.FqName; -import org.jetbrains.kotlin.name.Name; -import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType; -import org.jetbrains.org.objectweb.asm.Type; - -public class ProgressionIteratorBasicValue extends StrictBasicValue { - private final static ImmutableMap VALUES_TYPENAME_TO_TYPE; - - static { - ImmutableMap.Builder builder = ImmutableMap.builder(); - for (PrimitiveType primitiveType : RangeCodegenUtilKt.getSupportedRangeTypes()) { - builder.put(primitiveType.getTypeName().asString(), Type.getType(JvmPrimitiveType.get(primitiveType).getDesc())); - } - VALUES_TYPENAME_TO_TYPE = builder.build(); - } - - private static final ImmutableMap ITERATOR_VALUE_BY_ELEMENT_PRIMITIVE_TYPE; - - static { - ImmutableMap.Builder builder = ImmutableMap.builder(); - for (PrimitiveType elementType : RangeCodegenUtilKt.getSupportedRangeTypes()) { - builder.put(elementType, new ProgressionIteratorBasicValue(elementType.getTypeName().asString())); - } - ITERATOR_VALUE_BY_ELEMENT_PRIMITIVE_TYPE = builder.build(); - } - - @NotNull - private static Type getValuesType(@NotNull String valuesTypeName) { - Type type = VALUES_TYPENAME_TO_TYPE.get(valuesTypeName); - assert type != null : "Unexpected type " + valuesTypeName; - return type; - } - - private final Type valuesPrimitiveType; - private final String valuesPrimitiveTypeName; - - private ProgressionIteratorBasicValue(@NotNull String valuesPrimitiveTypeName) { - super(IteratorNext.Companion.getPrimitiveIteratorType(Name.identifier(valuesPrimitiveTypeName))); - this.valuesPrimitiveType = getValuesType(valuesPrimitiveTypeName); - this.valuesPrimitiveTypeName = valuesPrimitiveTypeName; - } - - - @Nullable - public static ProgressionIteratorBasicValue byProgressionClassType(@NotNull Type progressionClassType) { - FqName classFqName = new FqName(progressionClassType.getClassName()); - PrimitiveType elementType = RangeCodegenUtilKt.getPrimitiveRangeOrProgressionElementType(classFqName); - return ITERATOR_VALUE_BY_ELEMENT_PRIMITIVE_TYPE.get(elementType); - } - - @NotNull - public Type getValuesPrimitiveType() { - return valuesPrimitiveType; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - if (!super.equals(o)) return false; - - ProgressionIteratorBasicValue value = (ProgressionIteratorBasicValue) o; - - if (!valuesPrimitiveType.equals(value.valuesPrimitiveType)) return false; - - return true; - } - - @NotNull - public String getNextMethodName() { - return "next" + valuesPrimitiveTypeName; - } - - @NotNull - public String getNextMethodDesc() { - return "()" + getValuesPrimitiveType().getDescriptor(); - } -} diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/ProgressionIteratorBasicValue.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/ProgressionIteratorBasicValue.kt new file mode 100644 index 00000000000..dbaad49015b --- /dev/null +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/ProgressionIteratorBasicValue.kt @@ -0,0 +1,72 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.codegen.optimization.boxing + +import org.jetbrains.kotlin.builtins.PrimitiveType +import org.jetbrains.kotlin.codegen.intrinsics.IteratorNext +import org.jetbrains.kotlin.codegen.optimization.common.StrictBasicValue +import org.jetbrains.kotlin.codegen.range.getPrimitiveRangeOrProgressionElementType +import org.jetbrains.kotlin.codegen.range.supportedRangeTypes +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType +import org.jetbrains.org.objectweb.asm.Type + +class ProgressionIteratorBasicValue +private constructor(private val valuesPrimitiveTypeName: String) : + StrictBasicValue(IteratorNext.getPrimitiveIteratorType(Name.identifier(valuesPrimitiveTypeName))) { + + val valuesPrimitiveType: Type = VALUES_TYPENAME_TO_TYPE[valuesPrimitiveTypeName] ?: error("Unexpected type $valuesPrimitiveTypeName") + + val nextMethodName: String + get() = "next$valuesPrimitiveTypeName" + + val nextMethodDesc: String + get() = "()" + valuesPrimitiveType.descriptor + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other == null || javaClass != other.javaClass) return false + if (!super.equals(other)) return false + + val value = other as ProgressionIteratorBasicValue? + + return valuesPrimitiveType == value!!.valuesPrimitiveType + } + + override fun hashCode(): Int = + super.hashCode() * 31 + valuesPrimitiveType.hashCode() + + companion object { + private val VALUES_TYPENAME_TO_TYPE: Map = + supportedRangeTypes.associate { primitiveType -> + primitiveType.typeName.asString() to Type.getType(JvmPrimitiveType.get(primitiveType).desc) + } + + private val ITERATOR_VALUE_BY_ELEMENT_PRIMITIVE_TYPE: Map = + supportedRangeTypes.associate { elementType -> + elementType to ProgressionIteratorBasicValue(elementType.typeName.asString()) + } + + fun byProgressionClassType(progressionClassType: Type): ProgressionIteratorBasicValue? { + val classFqName = FqName(progressionClassType.className) + val elementType = getPrimitiveRangeOrProgressionElementType(classFqName) + return ITERATOR_VALUE_BY_ELEMENT_PRIMITIVE_TYPE[elementType] + } + } +} +