[Analysis API] implement an API to get a substitution based on an inheritance relationship between classes

^KT-62090 fixed
This commit is contained in:
Ilya Kirillov
2023-09-20 22:34:44 +02:00
committed by Space Team
parent 49cfbb0576
commit 15e4cbc10d
33 changed files with 716 additions and 2 deletions
@@ -68,7 +68,8 @@ public abstract class KtAnalysisSession(final override val token: KtLifetimeToke
KtSymbolProviderByJavaPsiMixIn,
KtResolveExtensionInfoProviderMixIn,
KtCompilerFacilityMixIn,
KtMetadataCalculatorMixIn {
KtMetadataCalculatorMixIn,
KtSubstitutorProviderMixIn {
public abstract val useSiteModule: KtModule
@@ -194,6 +195,9 @@ public abstract class KtAnalysisSession(final override val token: KtLifetimeToke
internal val typesCreator: KtTypeCreator
get() = typesCreatorImpl
protected abstract val typesCreatorImpl: KtTypeCreator
internal val substitutorProvider: KtSubstitutorProvider get() = substitutorProviderImpl
protected abstract val substitutorProviderImpl: KtSubstitutorProvider
}
public fun KtAnalysisSession.getModule(element: PsiElement): KtModule {
@@ -0,0 +1,49 @@
/*
* Copyright 2010-2023 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.analysis.api.components
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol
import org.jetbrains.kotlin.analysis.api.types.KtSubstitutor
public abstract class KtSubstitutorProvider : KtAnalysisSessionComponent() {
public abstract fun createSubstitutor(
subClass: KtClassOrObjectSymbol,
superClass: KtClassOrObjectSymbol,
): KtSubstitutor?
}
public interface KtSubstitutorProviderMixIn : KtAnalysisSessionMixIn {
/**
* Creates a [KtSubstitutor] based on the inheritance relationship between [subClass] and [superClass].
*
* The semantic of resulted [KtSubstitutor] is the substitutor that should be applied to a member of [superClass],
* so it can be called on an instance of [subClass].
*
* Basically, it's a composition of inheritance-based substitutions for all the inheritance chain.
*
* On the following code:
* ```
* class A : B<String>
* class B<T> : C<T, Int>
* class C<X, Y>
* ```
*
* * `createInheritanceTypeSubstitutor(A, B)` returns `KtSubstitutor {T -> String}`
* * `createInheritanceTypeSubstitutor(B, C)` returns `KtSubstitutor {X -> T, Y -> Int}`
* * `createInheritanceTypeSubstitutor(A, C)` returns `KtSubstitutor {X -> T, Y -> Int} andThen KtSubstitutor {T -> String}`
*
* @param subClass the subClass or object symbol.
* @param superClass the super class symbol.
* @return [KtSubstitutor] if [subClass] inherits [superClass] and there are no error types in the inheritance path. Returns `null` otherwise.
*/
public fun createInheritanceTypeSubstitutor(
subClass: KtClassOrObjectSymbol,
superClass: KtClassOrObjectSymbol,
): KtSubstitutor? = withValidityAssertion {
analysisSession.substitutorProvider.createSubstitutor(subClass, superClass)
}
}
@@ -0,0 +1,10 @@
// WITH_STDLIB
class A<caret_base>A : BB<Int, String>
class BB<S, T> : CC<S, T, List<T>>
class C<caret_super>C<X, Y, Z> {
fun foo(): kotlin.Triple<X, Y, Z>
}
@@ -0,0 +1,3 @@
Substitutor: <map substitutor: {X = S, Y = T, Z = kotlin/collections/List<T>}> then <map substitutor: {S = kotlin/Int, T = kotlin/String}>
Substituted callables:
foo(): Triple<Int, String, List<String>>
@@ -0,0 +1,6 @@
class A<caret_base>A : BB<Int, String>
class BB<S, T> : CC<S, T, List<T>>
class C<caret_super>CC<X, Y, Z> : AA
@@ -0,0 +1,8 @@
// WITH_STDLIB
class A<caret_base>A<T, S> : BB<List<T>, Map<S, Int>, S>
class B<caret_super>B<Q, R, T> {
fun foo(): kotlin.Triple<Q, R, T>
}
@@ -0,0 +1,3 @@
Substitutor: <map substitutor: {Q = kotlin/collections/List<T>, R = kotlin/collections/Map<S, kotlin/Int>, T = S}>
Substituted callables:
foo(): Triple<List<T>, Map<S, Int>, S>
@@ -0,0 +1,8 @@
// WITH_STDLIB
class A<caret_base>A : BB<Int>
class B<caret_super>B<S> {
fun foo(): List<S>
}
@@ -0,0 +1,3 @@
Substitutor: <map substitutor: {S = kotlin/Int}>
Substituted callables:
foo(): List<Int>
@@ -0,0 +1,6 @@
class A<caret_base>A<T> : BB<T>
class B<caret_super>B<S> {
fun foo(): S
}
@@ -0,0 +1,3 @@
Substitutor: <map substitutor: {S = T}>
Substituted callables:
foo(): T
@@ -0,0 +1,6 @@
class A<caret_base>A : BB<Int>
class B<caret_super>B<S : String> {
fun foo(): S
}
@@ -0,0 +1,3 @@
Substitutor: <map substitutor: {S = kotlin/Int}>
Substituted callables:
foo(): Int
@@ -0,0 +1,6 @@
class A<caret_base>A : BB<Int, String>
class B<caret_super>B<S> {
fun foo(): S
}
@@ -0,0 +1,7 @@
// WITH_STDLIB
class A<caret_base>A : BB<Int, String>
class B<caret_super>B<S, Q, U> {
fun foo(): Triple<S, Q, U>
}
@@ -0,0 +1,7 @@
class A<caret_base>A : BB<Int>, BB<String>
class B<caret_super>B<S> {
fun foo(): Triple<S>
}
@@ -0,0 +1,3 @@
Substitutor: <map substitutor: {S = kotlin/Int}>
Substituted callables:
foo(): Triple<S>
@@ -0,0 +1,7 @@
// WITH_STDLIB
class A<caret_base>A<caret_super>A : BB<Int, String>
class BB<S, T> : CC<S, T, List<T>> {
fun foo(): Triple<S, T>
}
@@ -0,0 +1 @@
Substitutor: <empty substitutor>