FIR IDE: add API to get super types of a KtType

This commit is contained in:
Tianyu Geng
2021-10-06 15:49:27 -07:00
committed by Ilya Kirillov
parent 09d16ce849
commit 8b44a4685f
34 changed files with 568 additions and 1 deletions
@@ -32,6 +32,11 @@ public abstract class KtTypeProvider : KtAnalysisSessionComponent() {
public abstract fun haveCommonSubtype(a: KtType, b: KtType): Boolean
public abstract fun getImplicitReceiverTypesAtPosition(position: KtElement): List<KtType>
public abstract fun getDirectSuperTypes(type: KtType, shouldApproximate: Boolean): List<KtType>
public abstract fun getAllSuperTypes(type: KtType, shouldApproximate: Boolean): List<KtType>
}
public interface KtTypeProviderMixIn : KtAnalysisSessionMixIn {
@@ -91,6 +96,28 @@ public interface KtTypeProviderMixIn : KtAnalysisSessionMixIn {
*/
public fun getImplicitReceiverTypesAtPosition(position: KtElement): List<KtType> =
analysisSession.typeProvider.getImplicitReceiverTypesAtPosition(position)
/**
* Gets the direct super types of the given type. For example, given `MutableList<String>`, this returns `List<String>` and
* `MutableCollection<String>`.
*
* Note that for flexible types, both direct super types of the upper and lower bounds are returned. If that's not desirable, please
* first call [KtFlexibleType.upperBound] or [KtFlexibleType.lowerBound] and then call this method.
*
* @param shouldApproximate whether to approximate non-denotable types. For example, super type of `List<out String>` is
* `Collection<CAPTURED out String>`. With approximation set to true, `Collection<out String>` is returned instead.
*/
public fun KtType.getDirectSuperTypes(shouldApproximate: Boolean = false): List<KtType> =
analysisSession.typeProvider.getDirectSuperTypes(this, shouldApproximate)
/**
* Gets all the super types of the given type. The returned result is ordered by a BFS traversal of the class hierarchy, without any
* duplicates.
*
* @param shouldApproximate see [getDirectSuperTypes]
*/
public fun KtType.getAllSuperTypes(shouldApproximate: Boolean = false): List<KtType> =
analysisSession.typeProvider.getAllSuperTypes(this, shouldApproximate)
}
@Suppress("PropertyName")
@@ -0,0 +1,4 @@
interface A
interface B
val o = object: A, B {}
val o2 = <expr>o</expr>
@@ -0,0 +1,17 @@
[direct super types]
A
B
[approximated direct super types]
A
B
[all super types]
A
B
kotlin.Any
[approximated all super types]
A
B
kotlin.Any
@@ -0,0 +1 @@
val a = <expr>Any()</expr>
@@ -0,0 +1,7 @@
[direct super types]
[approximated direct super types]
[all super types]
[approximated all super types]
@@ -0,0 +1,3 @@
val s = <expr>get()</expr>
fun get() : MutableList<in String>
@@ -0,0 +1,23 @@
[direct super types]
kotlin.collections.List<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.MutableCollection<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
[approximated direct super types]
kotlin.collections.List<kotlin.Any?>
kotlin.collections.MutableCollection<in kotlin.String>
[all super types]
kotlin.collections.List<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.MutableCollection<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.Collection<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.MutableIterable<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.Iterable<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.Any
[approximated all super types]
kotlin.collections.List<kotlin.Any?>
kotlin.collections.MutableCollection<in kotlin.String>
kotlin.collections.Collection<kotlin.Any?>
kotlin.collections.MutableIterable<kotlin.Any?>
kotlin.collections.Iterable<kotlin.Any?>
kotlin.Any
@@ -0,0 +1,3 @@
val s = <expr>get()</expr>
fun get() : MutableList<out String>
@@ -0,0 +1,23 @@
[direct super types]
kotlin.collections.List<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.MutableCollection<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
[approximated direct super types]
kotlin.collections.List<kotlin.String>
kotlin.collections.MutableCollection<out kotlin.String>
[all super types]
kotlin.collections.List<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.MutableCollection<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.Collection<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.MutableIterable<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.Iterable<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.Any
[approximated all super types]
kotlin.collections.List<kotlin.String>
kotlin.collections.MutableCollection<out kotlin.String>
kotlin.collections.Collection<kotlin.String>
kotlin.collections.MutableIterable<kotlin.String>
kotlin.collections.Iterable<kotlin.String>
kotlin.Any
@@ -0,0 +1,2 @@
val i: UnresolvedType? = null
val j = <expr>i</expr>
@@ -0,0 +1,7 @@
[direct super types]
[approximated direct super types]
[all super types]
[approximated all super types]
@@ -0,0 +1,12 @@
// FILE: Java.java
import java.util.List
class Java {
public static List<String> getNames() {
throw Exception()
}
}
// FILE: test.kt
val i = <expr>Java.getNames()</expr>
@@ -0,0 +1,32 @@
[direct super types]
kotlin.collections.List<kotlin.String!>
kotlin.collections.MutableCollection<kotlin.String!>
kotlin.collections.Collection<kotlin.String!>?
[approximated direct super types]
kotlin.collections.List<kotlin.String!>
kotlin.collections.MutableCollection<kotlin.String!>
kotlin.collections.Collection<kotlin.String!>?
[all super types]
kotlin.collections.List<kotlin.String!>
kotlin.collections.MutableCollection<kotlin.String!>
kotlin.collections.Collection<kotlin.String!>?
kotlin.collections.Collection<kotlin.String!>
kotlin.collections.MutableIterable<kotlin.String!>
kotlin.collections.Iterable<kotlin.String!>?
kotlin.collections.Iterable<kotlin.String!>
kotlin.Any?
kotlin.Any
[approximated all super types]
kotlin.collections.List<kotlin.String!>
kotlin.collections.MutableCollection<kotlin.String!>
kotlin.collections.Collection<kotlin.String!>?
kotlin.collections.Collection<kotlin.String!>
kotlin.collections.MutableIterable<kotlin.String!>
kotlin.collections.Iterable<kotlin.String!>?
kotlin.collections.Iterable<kotlin.String!>
kotlin.Any?
kotlin.Any
@@ -0,0 +1 @@
val i = <expr>1</expr>
@@ -0,0 +1,21 @@
[direct super types]
kotlin.Number
kotlin.Comparable<kotlin.Int>
java.io.Serializable
[approximated direct super types]
kotlin.Number
kotlin.Comparable<kotlin.Int>
java.io.Serializable
[all super types]
kotlin.Number
kotlin.Comparable<kotlin.Int>
java.io.Serializable
kotlin.Any
[approximated all super types]
kotlin.Number
kotlin.Comparable<kotlin.Int>
java.io.Serializable
kotlin.Any
@@ -0,0 +1,3 @@
val s = <expr>get()</expr>
fun get() : MutableList<String>
@@ -0,0 +1,23 @@
[direct super types]
kotlin.collections.List<kotlin.String>
kotlin.collections.MutableCollection<kotlin.String>
[approximated direct super types]
kotlin.collections.List<kotlin.String>
kotlin.collections.MutableCollection<kotlin.String>
[all super types]
kotlin.collections.List<kotlin.String>
kotlin.collections.MutableCollection<kotlin.String>
kotlin.collections.Collection<kotlin.String>
kotlin.collections.MutableIterable<kotlin.String>
kotlin.collections.Iterable<kotlin.String>
kotlin.Any
[approximated all super types]
kotlin.collections.List<kotlin.String>
kotlin.collections.MutableCollection<kotlin.String>
kotlin.collections.Collection<kotlin.String>
kotlin.collections.MutableIterable<kotlin.String>
kotlin.collections.Iterable<kotlin.String>
kotlin.Any
@@ -0,0 +1,3 @@
class Foo<T : Foo<T>>(t: T) {
val t2 = <expr>t</expr>
}
@@ -0,0 +1,13 @@
[direct super types]
Foo<T>
[approximated direct super types]
Foo<T>
[all super types]
Foo<T>
kotlin.Any
[approximated all super types]
Foo<T>
kotlin.Any
@@ -0,0 +1,3 @@
class A
val a = <expr>A()</expr>
@@ -0,0 +1,11 @@
[direct super types]
kotlin.Any
[approximated direct super types]
kotlin.Any
[all super types]
kotlin.Any
[approximated all super types]
kotlin.Any
@@ -0,0 +1,5 @@
interface I
open class A
class B: A(), I
val b = <expr>B()</expr>
@@ -0,0 +1,17 @@
[direct super types]
A
I
[approximated direct super types]
A
I
[all super types]
A
I
kotlin.Any
[approximated all super types]
A
I
kotlin.Any
@@ -0,0 +1,3 @@
val s = <expr>get()</expr>
fun get() : MutableList<*>
@@ -0,0 +1,23 @@
[direct super types]
kotlin.collections.List<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.MutableCollection<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
[approximated direct super types]
kotlin.collections.List<kotlin.Any?>
kotlin.collections.MutableCollection<out kotlin.Any?>
[all super types]
kotlin.collections.List<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.MutableCollection<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.Collection<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.MutableIterable<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.collections.Iterable<ERROR_TYPE <Unexpected cone type org.jetbrains.kotlin.fir.types.ConeCapturedType>>
kotlin.Any
[approximated all super types]
kotlin.collections.List<kotlin.Any?>
kotlin.collections.MutableCollection<out kotlin.Any?>
kotlin.collections.Collection<kotlin.Any?>
kotlin.collections.MutableIterable<kotlin.Any?>
kotlin.collections.Iterable<kotlin.Any?>
kotlin.Any
@@ -0,0 +1 @@
val s = <expr>""</expr>
@@ -0,0 +1,21 @@
[direct super types]
kotlin.Comparable<kotlin.String>
kotlin.CharSequence
java.io.Serializable
[approximated direct super types]
kotlin.Comparable<kotlin.String>
kotlin.CharSequence
java.io.Serializable
[all super types]
kotlin.Comparable<kotlin.String>
kotlin.CharSequence
java.io.Serializable
kotlin.Any
[approximated all super types]
kotlin.Comparable<kotlin.String>
kotlin.CharSequence
java.io.Serializable
kotlin.Any
@@ -0,0 +1,3 @@
class Foo<T : List<String>>(t: T) {
val t2 = <expr>t</expr>
}
@@ -0,0 +1,17 @@
[direct super types]
kotlin.collections.List<kotlin.String>
[approximated direct super types]
kotlin.collections.List<kotlin.String>
[all super types]
kotlin.collections.List<kotlin.String>
kotlin.collections.Collection<kotlin.String>
kotlin.collections.Iterable<kotlin.String>
kotlin.Any
[approximated all super types]
kotlin.collections.List<kotlin.String>
kotlin.collections.Collection<kotlin.String>
kotlin.collections.Iterable<kotlin.String>
kotlin.Any