Kotlin: Add ability to give more type contexts

This commit is contained in:
Ian Lynagh
2021-11-29 19:49:37 +00:00
parent 6c1439c180
commit ec827d2f0a
2 changed files with 10 additions and 6 deletions

View File

@@ -1018,7 +1018,7 @@ open class KotlinFileExtractor(
) {
for (argIdx in 0 until c.typeArgumentsCount) {
val arg = c.getTypeArgument(argIdx)!!
val argType = useType(arg, false)
val argType = useType(arg, TypeContext.GENERIC_ARGUMENT)
val argId = tw.getFreshIdLabel<DbUnannotatedtypeaccess>()
val mul = if (reverse) -1 else 1
tw.writeExprs_unannotatedtypeaccess(argId, argType.javaResult.id, argType.kotlinResult.id, id, argIdx * mul + startIndex)

View File

@@ -48,9 +48,9 @@ open class KotlinUsesExtractor(
data class TypeResult<out LabelType>(val id: Label<out LabelType>, val signature: String?, val shortName: String)
data class TypeResults(val javaResult: TypeResult<DbType>, val kotlinResult: TypeResult<DbKt_type>)
fun useType(t: IrType, canReturnPrimitiveTypes: Boolean = true) =
fun useType(t: IrType, context: TypeContext = TypeContext.OTHER) =
when(t) {
is IrSimpleType -> useSimpleType(t, canReturnPrimitiveTypes)
is IrSimpleType -> useSimpleType(t, context)
else -> {
logger.warn(Severity.ErrorSevere, "Unrecognised IrType: " + t.javaClass)
TypeResults(TypeResult(fakeLabel(), "unknown", "unknown"), TypeResult(fakeLabel(), "unknown", "unknown"))
@@ -260,7 +260,11 @@ open class KotlinUsesExtractor(
return TypeResults(javaResult, arrayClassResult.kotlinResult)
}
fun useSimpleType(s: IrSimpleType, canReturnPrimitiveTypes: Boolean): TypeResults {
enum class TypeContext {
RETURN, GENERIC_ARGUMENT, OTHER
}
fun useSimpleType(s: IrSimpleType, context: TypeContext): TypeResults {
if (s.abbreviation != null) {
// TODO: Extract this information
logger.warn(Severity.ErrorSevere, "Type alias ignored for " + s.render())
@@ -278,7 +282,7 @@ open class KotlinUsesExtractor(
fun primitiveType(kotlinClass: IrClass, primitiveName: String?,
javaPackageName: String, javaClassName: String,
kotlinPackageName: String, kotlinClassName: String): TypeResults {
val javaResult = if (canReturnPrimitiveTypes && !s.hasQuestionMark && primitiveName != null) {
val javaResult = if (context != TypeContext.GENERIC_ARGUMENT && !s.hasQuestionMark && primitiveName != null) {
val label: Label<DbPrimitive> = tw.getLabelFor("@\"type;$primitiveName\"", {
tw.writePrimitives(it, primitiveName)
})
@@ -457,7 +461,7 @@ class X {
TypeResult(extractBoundedWildcard(1, "@\"wildcard;\"", "?", anyTypeLabel), null, "?")
}
is IrTypeProjection -> {
val boundResults = useType(arg.type, false)
val boundResults = useType(arg.type, TypeContext.GENERIC_ARGUMENT)
@Suppress("UNCHECKED_CAST")
val boundLabel = boundResults.javaResult.id as Label<out DbReftype>