From 64f7c3f49868d943dcd7dd4dfc2862b46e673ef0 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 11 Jun 2020 22:56:52 +0200 Subject: [PATCH] IR: do not use descriptors in IrTypeSystemContext.getPrimitiveType Also optimize getPrimitiveType and getPrimitiveArrayType. Convert PrimitiveType to Kotlin to leverage the switch over string optimization, which is not possible in Java 6 (which is used to compile 'descriptors'). --- .../kotlin/ir/types/IrTypeSystemContext.kt | 17 ++-- .../kotlin/builtins/PrimitiveType.java | 78 ------------------- .../kotlin/builtins/PrimitiveType.kt | 71 +++++++++++++++++ 3 files changed, 83 insertions(+), 83 deletions(-) delete mode 100644 core/descriptors/src/org/jetbrains/kotlin/builtins/PrimitiveType.java create mode 100644 core/descriptors/src/org/jetbrains/kotlin/builtins/PrimitiveType.kt diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt index 174fff3a5d4..db40bab9be3 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt @@ -7,7 +7,6 @@ package org.jetbrains.kotlin.ir.types import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.PrimitiveType -import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Visibilities @@ -345,13 +344,21 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon } override fun TypeConstructorMarker.getPrimitiveType(): PrimitiveType? { - // TODO: get rid of descriptor - return KotlinBuiltIns.getPrimitiveType((this as IrClassifierSymbol).descriptor as ClassDescriptor) + if (this !is IrClassSymbol || !isPublicApi) return null + + val signature = signature.asPublic() + if (signature == null || signature.packageFqName != "kotlin") return null + + return PrimitiveType.getByShortName(signature.declarationFqName) } override fun TypeConstructorMarker.getPrimitiveArrayType(): PrimitiveType? { - // TODO: get rid of descriptor - return KotlinBuiltIns.getPrimitiveArrayType((this as IrClassifierSymbol).descriptor as ClassDescriptor) + if (this !is IrClassSymbol || !isPublicApi) return null + + val signature = signature.asPublic() + if (signature == null || signature.packageFqName != "kotlin") return null + + return PrimitiveType.getByShortArrayName(signature.declarationFqName) } override fun TypeConstructorMarker.isUnderKotlinPackage(): Boolean { diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/PrimitiveType.java b/core/descriptors/src/org/jetbrains/kotlin/builtins/PrimitiveType.java deleted file mode 100644 index a0fe89fd23e..00000000000 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/PrimitiveType.java +++ /dev/null @@ -1,78 +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.builtins; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.name.FqName; -import org.jetbrains.kotlin.name.Name; - -import java.util.Collections; -import java.util.EnumSet; -import java.util.Set; - -public enum PrimitiveType { - BOOLEAN("Boolean"), - CHAR("Char"), - BYTE("Byte"), - SHORT("Short"), - INT("Int"), - FLOAT("Float"), - LONG("Long"), - DOUBLE("Double"), - ; - - public static final Set NUMBER_TYPES = - Collections.unmodifiableSet(EnumSet.of(CHAR, BYTE, SHORT, INT, FLOAT, LONG, DOUBLE)); - - private final Name typeName; - private final Name arrayTypeName; - private FqName typeFqName = null; - private FqName arrayTypeFqName = null; - - private PrimitiveType(String typeName) { - this.typeName = Name.identifier(typeName); - this.arrayTypeName = Name.identifier(typeName + "Array"); - } - - @NotNull - public Name getTypeName() { - return typeName; - } - - @NotNull - public FqName getTypeFqName() { - if (typeFqName != null) - return typeFqName; - - typeFqName = KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME.child(typeName); - return typeFqName; - } - - @NotNull - public Name getArrayTypeName() { - return arrayTypeName; - } - - @NotNull - public FqName getArrayTypeFqName() { - if (arrayTypeFqName != null) - return arrayTypeFqName; - - arrayTypeFqName = KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME.child(arrayTypeName); - return arrayTypeFqName; - } -} diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/PrimitiveType.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/PrimitiveType.kt new file mode 100644 index 00000000000..956b31cd4c6 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/PrimitiveType.kt @@ -0,0 +1,71 @@ +/* + * 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.builtins + +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name + +enum class PrimitiveType(typeName: String) { + BOOLEAN("Boolean"), + CHAR("Char"), + BYTE("Byte"), + SHORT("Short"), + INT("Int"), + FLOAT("Float"), + LONG("Long"), + DOUBLE("Double"), + ; + + val typeName: Name = Name.identifier(typeName) + + val arrayTypeName: Name = Name.identifier("${typeName}Array") + + val typeFqName: FqName = KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME.child(this.typeName) + + val arrayTypeFqName: FqName = KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME.child(arrayTypeName) + + companion object { + @JvmField + val NUMBER_TYPES = setOf(CHAR, BYTE, SHORT, INT, FLOAT, LONG, DOUBLE) + + @JvmStatic + fun getByShortName(name: String): PrimitiveType? = when (name) { + "Boolean" -> BOOLEAN + "Char" -> CHAR + "Byte" -> BYTE + "Short" -> SHORT + "Int" -> INT + "Float" -> FLOAT + "Long" -> LONG + "Double" -> DOUBLE + else -> null + } + + @JvmStatic + fun getByShortArrayName(name: String): PrimitiveType? = when (name) { + "BooleanArray" -> BOOLEAN + "CharArray" -> CHAR + "ByteArray" -> BYTE + "ShortArray" -> SHORT + "IntArray" -> INT + "FloatArray" -> FLOAT + "LongArray" -> LONG + "DoubleArray" -> DOUBLE + else -> null + } + } +}