Kotlin: extract type parameters

This commit is contained in:
Tamas Vajk
2021-10-04 16:04:34 +02:00
committed by Ian Lynagh
parent db5afe84b4
commit 0c6e20928c
4 changed files with 41 additions and 0 deletions

View File

@@ -302,6 +302,21 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
return tw.getLabelFor(getTypeParameterLabel(param))
}
private fun extractTypeParameter(tp: IrTypeParameter, optParentid: Optional<Label<out DbTypeorcallable>>) {
val id = useTypeParameter(tp)
if (!optParentid.isPresent) {
logger.warnElement(Severity.ErrorSevere, "Couldn't find expected parent of type parameter.", tp)
return
}
tw.writeTypeVars(id, tp.name.asString(), tp.index, 0, optParentid.get())
val locId = tw.getLocation(tp)
tw.writeHasLocation(id, locId)
// todo: add type bounds
}
private fun getClassLabel(c: IrClass): String {
val pkg = c.packageFqName?.asString() ?: ""
val cls = c.name.asString()
@@ -365,6 +380,9 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
}
}
}
c.typeParameters.map { extractTypeParameter(it, Optional.of(id)) }
c.declarations.map { extractDeclaration(it, Optional.of(id)) }
extractObjectInitializerFunction(c, id)
@@ -552,6 +570,9 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
val extendedType = useType(extReceiver.type)
tw.writeKtExtensionFunctions(id, extendedType)
}
f.typeParameters.map { extractTypeParameter(it, Optional.of(id)) }
currentFunction = null
}

View File

@@ -0,0 +1,10 @@
package foo.bar
fun <S> Int.f(s: S): S {
return s
}
class C1<T>(val t: T) {
fun f1(t: T) {}
fun <U> f2(u: U) {}
}

View File

@@ -0,0 +1,5 @@
genericType
| generics.kt:7:10:7:10 | T | generics.kt:7:1:10:1 | C1 |
genericFunction
| generics.kt:3:6:3:6 | S | generics.kt:3:1:5:1 | f |
| generics.kt:9:10:9:10 | U | generics.kt:9:5:9:23 | f2 |

View File

@@ -0,0 +1,5 @@
import java
query predicate genericType(TypeVariable tv, RefType rt) { tv.getGenericType() = rt }
query predicate genericFunction(TypeVariable tv, GenericCallable c) { tv.getGenericCallable() = c }