Skip to content

Commit 910bb51

Browse files
committed
Extract WildcardTypeAccesses
Their absence became more noticeable now that more implicit wildcards are being produced.
1 parent dc7d07f commit 910bb51

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.ir.symbols.*
2020
import org.jetbrains.kotlin.ir.types.*
2121
import org.jetbrains.kotlin.ir.util.*
2222
import org.jetbrains.kotlin.name.FqName
23+
import org.jetbrains.kotlin.types.Variance
2324
import org.jetbrains.kotlin.util.OperatorNameConventions
2425
import java.io.Closeable
2526
import java.util.*
@@ -3748,6 +3749,17 @@ open class KotlinFileExtractor(
37483749
}
37493750
}
37503751

3752+
/**
3753+
* Extracts a single wildcard type access expression with no enclosing callable and statement.
3754+
*/
3755+
private fun extractWildcardTypeAccess(type: TypeResults, location: Label<DbLocation>, parent: Label<out DbExprparent>, idx: Int): Label<out DbExpr> {
3756+
val id = tw.getFreshIdLabel<DbWildcardtypeaccess>()
3757+
tw.writeExprs_wildcardtypeaccess(id, type.javaResult.id, parent, idx)
3758+
tw.writeExprsKotlinType(id, type.kotlinResult.id)
3759+
tw.writeHasLocation(id, location)
3760+
return id
3761+
}
3762+
37513763
/**
37523764
* Extracts a single type access expression with no enclosing callable and statement.
37533765
*/
@@ -3772,15 +3784,36 @@ open class KotlinFileExtractor(
37723784
return id
37733785
}
37743786

3787+
/**
3788+
* Extracts a type argument type access, introducing a wildcard type access if appropriate, or directly calling
3789+
* `extractTypeAccessRecursive` if the argument is invariant.
3790+
* No enclosing callable and statement is extracted, this is useful for type access extraction in field declarations.
3791+
*/
3792+
private fun extractWildcardTypeAccessRecursive(t: IrTypeArgument, location: Label<DbLocation>, parent: Label<out DbExprparent>, idx: Int) {
3793+
val typeLabels by lazy { TypeResults(getTypeArgumentLabel(t), TypeResult(fakeKotlinType(), "TODO", "TODO")) }
3794+
when (t) {
3795+
is IrStarProjection -> extractWildcardTypeAccess(typeLabels, location, parent, idx)
3796+
is IrTypeProjection -> when(t.variance) {
3797+
Variance.INVARIANT -> extractTypeAccessRecursive(t.type, location, parent, idx, TypeContext.GENERIC_ARGUMENT)
3798+
else -> {
3799+
val wildcardLabel = extractWildcardTypeAccess(typeLabels, location, parent, idx)
3800+
// Mimic a Java extractor oddity, that it uses the child index to indicate what kind of wildcard this is
3801+
val boundChildIdx = if (t.variance == Variance.OUT_VARIANCE) 0 else 1
3802+
extractTypeAccessRecursive(t.type, location, wildcardLabel, boundChildIdx, TypeContext.GENERIC_ARGUMENT)
3803+
}
3804+
}
3805+
}
3806+
}
3807+
37753808
/**
37763809
* Extracts a type access expression and its child type access expressions in case of a generic type. Nested generics are also handled.
37773810
* No enclosing callable and statement is extracted, this is useful for type access extraction in field declarations.
37783811
*/
37793812
private fun extractTypeAccessRecursive(t: IrType, location: Label<DbLocation>, parent: Label<out DbExprparent>, idx: Int, typeContext: TypeContext = TypeContext.OTHER): Label<out DbExpr> {
37803813
val typeAccessId = extractTypeAccess(useType(t, typeContext), location, parent, idx)
37813814
if (t is IrSimpleType) {
3782-
t.arguments.filterIsInstance<IrType>().forEachIndexed { argIdx, arg ->
3783-
extractTypeAccessRecursive(arg, location, typeAccessId, argIdx, TypeContext.GENERIC_ARGUMENT)
3815+
t.arguments.forEachIndexed { argIdx, arg ->
3816+
extractWildcardTypeAccessRecursive(arg, location, typeAccessId, argIdx)
37843817
}
37853818
}
37863819
return typeAccessId

0 commit comments

Comments
 (0)