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').
This commit is contained in:
Alexander Udalov
2020-06-11 22:56:52 +02:00
parent 7013becda5
commit 64f7c3f498
3 changed files with 83 additions and 83 deletions
@@ -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 {
@@ -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<PrimitiveType> 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;
}
}
@@ -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
}
}
}