[FIR] Extract java synthetic names calculation to platform specific component

This commit is contained in:
Dmitriy Novozhilov
2020-08-28 13:54:05 +03:00
parent 375140ebcc
commit 28e6050551
5 changed files with 70 additions and 36 deletions
@@ -9,10 +9,12 @@ import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.registerJvmEffectiveVisibilityResolver import org.jetbrains.kotlin.fir.registerJvmEffectiveVisibilityResolver
import org.jetbrains.kotlin.fir.resolve.calls.jvm.registerJvmCallConflictResolverFactory import org.jetbrains.kotlin.fir.resolve.calls.jvm.registerJvmCallConflictResolverFactory
import org.jetbrains.kotlin.fir.resolve.registerJavaClassMapper import org.jetbrains.kotlin.fir.resolve.registerJavaClassMapper
import org.jetbrains.kotlin.fir.resolve.registerJavaSyntheticNamesProvider
fun FirSession.registerJavaSpecificComponents() { fun FirSession.registerJavaSpecificComponents() {
registerJavaVisibilityChecker() registerJavaVisibilityChecker()
registerJvmCallConflictResolverFactory() registerJvmCallConflictResolverFactory()
registerJvmEffectiveVisibilityResolver() registerJvmEffectiveVisibilityResolver()
registerJavaClassMapper() registerJavaClassMapper()
registerJavaSyntheticNamesProvider()
} }
@@ -13,7 +13,7 @@ import org.jetbrains.kotlin.fir.declarations.synthetic.buildSyntheticProperty
import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.java.JavaTypeParameterStack import org.jetbrains.kotlin.fir.java.JavaTypeParameterStack
import org.jetbrains.kotlin.fir.java.declarations.* import org.jetbrains.kotlin.fir.java.declarations.*
import org.jetbrains.kotlin.fir.resolve.calls.FirSyntheticPropertiesScope import org.jetbrains.kotlin.fir.resolve.FirJavaSyntheticNamesProvider
import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.scopes.FirScope
import org.jetbrains.kotlin.fir.scopes.FirTypeScope import org.jetbrains.kotlin.fir.scopes.FirTypeScope
import org.jetbrains.kotlin.fir.scopes.getContainingCallableNamesIfPresent import org.jetbrains.kotlin.fir.scopes.getContainingCallableNamesIfPresent
@@ -157,7 +157,7 @@ class JavaClassUseSiteMemberScope(
if (name.isSpecial) { if (name.isSpecial) {
return processAccessorFunctionsAndPropertiesByName(name, emptyList(), processor) return processAccessorFunctionsAndPropertiesByName(name, emptyList(), processor)
} }
val getterNames = FirSyntheticPropertiesScope.possibleGetterNamesByPropertyName(name) val getterNames = FirJavaSyntheticNamesProvider.possibleGetterNamesByPropertyName(name)
val setterName = Name.identifier(SETTER_PREFIX + name.identifier.capitalize()) val setterName = Name.identifier(SETTER_PREFIX + name.identifier.capitalize())
return processAccessorFunctionsAndPropertiesByName(name, getterNames, processor) return processAccessorFunctionsAndPropertiesByName(name, getterNames, processor)
} }
@@ -0,0 +1,46 @@
/*
* 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.fir.resolve
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.resolve.calls.FirSyntheticNamesProvider
import org.jetbrains.kotlin.load.java.propertyNameByGetMethodName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeAsciiOnly
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeFirstWord
object FirJavaSyntheticNamesProvider : FirSyntheticNamesProvider() {
private const val GETTER_PREFIX = "get"
private const val IS_PREFIX = "is"
override fun possibleGetterNamesByPropertyName(name: Name): List<Name> {
if (name.isSpecial) return emptyList()
val identifier = name.identifier
val capitalizedAsciiName = identifier.capitalizeAsciiOnly()
val capitalizedFirstWordName = identifier.capitalizeFirstWord(asciiOnly = true)
return listOfNotNull(
Name.identifier(GETTER_PREFIX + capitalizedAsciiName),
if (capitalizedFirstWordName == capitalizedAsciiName) null else Name.identifier(GETTER_PREFIX + capitalizedFirstWordName),
name.takeIf { identifier.startsWith(IS_PREFIX) }
).filter {
propertyNameByGetMethodName(it) == name
}
}
override fun setterNameByGetterName(name: Name): Name {
val identifier = name.identifier
val prefix = when {
identifier.startsWith("get") -> "get"
identifier.startsWith("is") -> "is"
else -> throw IllegalArgumentException()
}
return Name.identifier("set" + identifier.removePrefix(prefix))
}
}
fun FirSession.registerJavaSyntheticNamesProvider() {
register(FirSyntheticNamesProvider::class, FirJavaSyntheticNamesProvider)
}
@@ -0,0 +1,17 @@
/*
* 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.fir.resolve.calls
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.FirSessionComponent
import org.jetbrains.kotlin.name.Name
abstract class FirSyntheticNamesProvider : FirSessionComponent {
abstract fun possibleGetterNamesByPropertyName(name: Name): List<Name>
abstract fun setterNameByGetterName(name: Name): Name
}
val FirSession.syntheticNamesProvider: FirSyntheticNamesProvider by FirSession.sessionComponentAccessor()
@@ -19,10 +19,7 @@ import org.jetbrains.kotlin.fir.symbols.StandardClassIds
import org.jetbrains.kotlin.fir.symbols.SyntheticSymbol import org.jetbrains.kotlin.fir.symbols.SyntheticSymbol
import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.load.java.propertyNameByGetMethodName
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeAsciiOnly
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeFirstWord
class SyntheticPropertySymbol( class SyntheticPropertySymbol(
callableId: CallableId, callableId: CallableId,
@@ -37,38 +34,10 @@ class FirSyntheticPropertiesScope(
val session: FirSession, val session: FirSession,
private val baseScope: FirTypeScope private val baseScope: FirTypeScope
) : FirScope() { ) : FirScope() {
private val syntheticNamesProvider = session.syntheticNamesProvider
companion object {
private const val GETTER_PREFIX = "get"
private const val IS_PREFIX = "is"
fun possibleGetterNamesByPropertyName(name: Name): List<Name> {
if (name.isSpecial) return emptyList()
val identifier = name.identifier
val capitalizedAsciiName = identifier.capitalizeAsciiOnly()
val capitalizedFirstWordName = identifier.capitalizeFirstWord(asciiOnly = true)
return listOfNotNull(
Name.identifier(GETTER_PREFIX + capitalizedAsciiName),
if (capitalizedFirstWordName == capitalizedAsciiName) null else Name.identifier(GETTER_PREFIX + capitalizedFirstWordName),
name.takeIf { identifier.startsWith(IS_PREFIX) }
).filter {
propertyNameByGetMethodName(it) == name
}
}
fun setterNameByGetterName(name: Name): Name {
val identifier = name.identifier
val prefix = when {
identifier.startsWith("get") -> "get"
identifier.startsWith("is") -> "is"
else -> throw IllegalArgumentException()
}
return Name.identifier("set" + identifier.removePrefix(prefix))
}
}
override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) { override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) {
val getterNames = possibleGetterNamesByPropertyName(name) val getterNames = syntheticNamesProvider.possibleGetterNamesByPropertyName(name)
for (getterName in getterNames) { for (getterName in getterNames) {
baseScope.processFunctionsByName(getterName) { baseScope.processFunctionsByName(getterName) {
checkGetAndCreateSynthetic(name, getterName, it, processor) checkGetAndCreateSynthetic(name, getterName, it, processor)
@@ -94,7 +63,7 @@ class FirSyntheticPropertiesScope(
var matchingSetter: FirSimpleFunction? = null var matchingSetter: FirSimpleFunction? = null
if (getterReturnType != null) { if (getterReturnType != null) {
val setterName = setterNameByGetterName(getterName) val setterName = syntheticNamesProvider.setterNameByGetterName(getterName)
baseScope.processFunctionsByName(setterName, fun(setterSymbol: FirFunctionSymbol<*>) { baseScope.processFunctionsByName(setterName, fun(setterSymbol: FirFunctionSymbol<*>) {
if (matchingSetter != null) return if (matchingSetter != null) return
val setter = setterSymbol.fir as? FirSimpleFunction ?: return val setter = setterSymbol.fir as? FirSimpleFunction ?: return