Move BuiltinSpecialProperties and JvmAbi to :core:compiler.common.jvm

This commit is contained in:
Dmitriy Novozhilov
2020-08-31 16:44:42 +03:00
parent 10fd164b77
commit 64ec3fc42b
26 changed files with 238 additions and 233 deletions
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.load.java
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.FqNameUnsafe
import org.jetbrains.kotlin.name.Name
object BuiltinSpecialProperties {
val PROPERTY_FQ_NAME_TO_JVM_GETTER_NAME_MAP: Map<FqName, Name> = mapOf(
StandardNames.FqNames._enum.childSafe("name") to Name.identifier("name"),
StandardNames.FqNames._enum.childSafe("ordinal") to Name.identifier("ordinal"),
StandardNames.FqNames.collection.child("size") to Name.identifier("size"),
StandardNames.FqNames.map.child("size") to Name.identifier("size"),
StandardNames.FqNames.charSequence.childSafe("length") to Name.identifier("length"),
StandardNames.FqNames.map.child("keys") to Name.identifier("keySet"),
StandardNames.FqNames.map.child("values") to Name.identifier("values"),
StandardNames.FqNames.map.child("entries") to Name.identifier("entrySet")
)
private val GETTER_JVM_NAME_TO_PROPERTIES_SHORT_NAME_MAP: Map<Name, List<Name>> =
PROPERTY_FQ_NAME_TO_JVM_GETTER_NAME_MAP.entries
.map { Pair(it.key.shortName(), it.value) }
.groupBy({ it.second }, { it.first })
val SPECIAL_FQ_NAMES: Set<FqName> = PROPERTY_FQ_NAME_TO_JVM_GETTER_NAME_MAP.keys
val SPECIAL_SHORT_NAMES: Set<Name> = SPECIAL_FQ_NAMES.map(FqName::shortName).toSet()
fun getPropertyNameCandidatesBySpecialGetterName(name1: Name): List<Name> =
GETTER_JVM_NAME_TO_PROPERTIES_SHORT_NAME_MAP[name1] ?: emptyList()
}
private fun FqName.child(name: String): FqName = child(Name.identifier(name))
private fun FqNameUnsafe.childSafe(name: String): FqName = child(Name.identifier(name)).toSafe()
@@ -0,0 +1,90 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.load.java
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeAsciiOnly
object JvmAbi {
const val DEFAULT_IMPLS_CLASS_NAME = "DefaultImpls"
const val ERASED_INLINE_CONSTRUCTOR_NAME = "constructor"
@JvmField
val JVM_FIELD_ANNOTATION_FQ_NAME = FqName("kotlin.jvm.JvmField")
/**
* Warning: use DEFAULT_IMPLS_CLASS_NAME and TypeMappingConfiguration.innerClassNameFactory when possible.
* This is false for KAPT3 mode.
*/
const val DEFAULT_IMPLS_SUFFIX = "$$DEFAULT_IMPLS_CLASS_NAME"
const val DEFAULT_PARAMS_IMPL_SUFFIX = "\$default"
private const val GET_PREFIX = "get"
private const val IS_PREFIX = "is"
private const val SET_PREFIX = "set"
const val DELEGATED_PROPERTY_NAME_SUFFIX = "\$delegate"
const val DELEGATED_PROPERTIES_ARRAY_NAME = "$\$delegatedProperties"
const val DELEGATE_SUPER_FIELD_PREFIX = "$\$delegate_"
private const val ANNOTATIONS_SUFFIX = "\$annotations"
const val ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX = ANNOTATIONS_SUFFIX
private const val ANNOTATED_TYPEALIAS_METHOD_NAME_SUFFIX = ANNOTATIONS_SUFFIX
const val INSTANCE_FIELD = "INSTANCE"
const val HIDDEN_INSTANCE_FIELD = "$$$INSTANCE_FIELD"
val REFLECTION_FACTORY_IMPL = ClassId.topLevel(FqName("kotlin.reflect.jvm.internal.ReflectionFactoryImpl"))
const val LOCAL_VARIABLE_NAME_PREFIX_INLINE_ARGUMENT = "\$i\$a$"
const val LOCAL_VARIABLE_NAME_PREFIX_INLINE_FUNCTION = "\$i\$f$"
const val IMPL_SUFFIX_FOR_INLINE_CLASS_MEMBERS = "-impl"
/**
* @param baseName JVM name of the property getter since Kotlin 1.4, or Kotlin name of the property otherwise.
*/
@JvmStatic
fun getSyntheticMethodNameForAnnotatedProperty(baseName: String): String {
return baseName + ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX
}
@JvmStatic
fun getSyntheticMethodNameForAnnotatedTypeAlias(typeAliasName: Name): String {
return typeAliasName.asString() + ANNOTATED_TYPEALIAS_METHOD_NAME_SUFFIX
}
@JvmStatic
fun isGetterName(name: String): Boolean {
return name.startsWith(GET_PREFIX) || name.startsWith(IS_PREFIX)
}
@JvmStatic
fun isSetterName(name: String): Boolean {
return name.startsWith(SET_PREFIX)
}
@JvmStatic
fun getterName(propertyName: String): String {
return if (startsWithIsPrefix(propertyName)) propertyName else GET_PREFIX + propertyName.capitalizeAsciiOnly()
}
@JvmStatic
fun setterName(propertyName: String): String {
return SET_PREFIX +
if (startsWithIsPrefix(propertyName)) propertyName.substring(IS_PREFIX.length) else propertyName.capitalizeAsciiOnly()
}
@JvmStatic
fun startsWithIsPrefix(name: String): Boolean {
if (!name.startsWith(IS_PREFIX)) return false
if (name.length == IS_PREFIX.length) return false
val c = name[IS_PREFIX.length]
return !('a' <= c && c <= 'z')
}
}
@@ -0,0 +1,56 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.load.java
import org.jetbrains.kotlin.load.java.BuiltinSpecialProperties.getPropertyNameCandidatesBySpecialGetterName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.util.capitalizeDecapitalize.decapitalizeSmartForCompiler
fun propertyNameByGetMethodName(methodName: Name): Name? =
propertyNameFromAccessorMethodName(methodName, "get") ?: propertyNameFromAccessorMethodName(methodName, "is", removePrefix = false)
fun propertyNameBySetMethodName(methodName: Name, withIsPrefix: Boolean): Name? =
propertyNameFromAccessorMethodName(methodName, "set", addPrefix = if (withIsPrefix) "is" else null)
fun propertyNamesBySetMethodName(methodName: Name): List<Name> =
listOfNotNull(propertyNameBySetMethodName(methodName, false), propertyNameBySetMethodName(methodName, true))
private fun propertyNameFromAccessorMethodName(
methodName: Name,
prefix: String,
removePrefix: Boolean = true,
addPrefix: String? = null
): Name? {
if (methodName.isSpecial) return null
val identifier = methodName.identifier
if (!identifier.startsWith(prefix)) return null
if (identifier.length == prefix.length) return null
if (identifier[prefix.length] in 'a'..'z') return null
if (addPrefix != null) {
assert(removePrefix)
return Name.identifier(addPrefix + identifier.removePrefix(prefix))
}
if (!removePrefix) return methodName
val name = identifier.removePrefix(prefix).decapitalizeSmartForCompiler(asciiOnly = true)
if (!Name.isValidIdentifier(name)) return null
return Name.identifier(name)
}
fun getPropertyNamesCandidatesByAccessorName(name: Name): List<Name> {
val nameAsString = name.asString()
if (JvmAbi.isGetterName(nameAsString)) {
return listOfNotNull(propertyNameByGetMethodName(name))
}
if (JvmAbi.isSetterName(nameAsString)) {
return propertyNamesBySetMethodName(name)
}
return getPropertyNameCandidatesBySpecialGetterName(name)
}