[Analysis API] implement an API to get a substitution based on an inheritance relationship between classes
^KT-62090 fixed
This commit is contained in:
committed by
Space Team
parent
49cfbb0576
commit
15e4cbc10d
@@ -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 {
|
||||
|
||||
+49
@@ -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)
|
||||
}
|
||||
}
|
||||
+10
@@ -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>
|
||||
}
|
||||
|
||||
+3
@@ -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>>
|
||||
+6
@@ -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
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
Substitutor: null
|
||||
+8
@@ -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>
|
||||
}
|
||||
|
||||
+3
@@ -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>
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
class A<caret_base>A : BB<Int>
|
||||
|
||||
class B<caret_super>B<S> {
|
||||
fun foo(): List<S>
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Substitutor: <map substitutor: {S = kotlin/Int}>
|
||||
Substituted callables:
|
||||
foo(): List<Int>
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class A<caret_base>A<T> : BB<T>
|
||||
|
||||
class B<caret_super>B<S> {
|
||||
fun foo(): S
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Substitutor: <map substitutor: {S = T}>
|
||||
Substituted callables:
|
||||
foo(): T
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class A<caret_base>A : BB<Int>
|
||||
|
||||
class B<caret_super>B<S : String> {
|
||||
fun foo(): S
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Substitutor: <map substitutor: {S = kotlin/Int}>
|
||||
Substituted callables:
|
||||
foo(): Int
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class A<caret_base>A : BB<Int, String>
|
||||
|
||||
class B<caret_super>B<S> {
|
||||
fun foo(): S
|
||||
}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
Substitutor: null
|
||||
+7
@@ -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>
|
||||
}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
Substitutor: null
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class A<caret_base>A : BB<Int>, BB<String>
|
||||
|
||||
class B<caret_super>B<S> {
|
||||
fun foo(): Triple<S>
|
||||
}
|
||||
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Substitutor: <map substitutor: {S = kotlin/Int}>
|
||||
Substituted callables:
|
||||
foo(): Triple<S>
|
||||
+7
@@ -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>
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
Substitutor: <empty substitutor>
|
||||
Reference in New Issue
Block a user