Skip to content

Commit

Permalink
KE2: Populate Kotlin type nullability and alias information
Browse files Browse the repository at this point in the history
  • Loading branch information
igfoo committed Nov 21, 2024
1 parent 7baeea6 commit 82c4131
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2776,7 +2776,7 @@ open class KotlinFileExtractor(
val locId = tw.getLocation(ta)
// TODO: We don't really want to generate any Java types here; we only want the KT type:
val type = useType(ta.expandedType)
tw.writeKt_type_alias(id, ta.name.asString(), type.kotlinResult.id)
TODO() // TODO: KotType tw.writeKt_type_alias(id, ta.name.asString(), type.kotlinResult.id)
tw.writeHasLocation(id, locId)

// TODO: extract annotations
Expand Down
38 changes: 25 additions & 13 deletions java/kotlin-extractor2/src/main/kotlin/entities/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,37 @@ private fun KotlinUsesExtractor.useClassType(
}

fun KotlinUsesExtractor.useType(t: KaType?, context: TypeContext = TypeContext.OTHER): TypeResults {
when (t) {
val tr = when (t) {
null -> {
logger.error("Unexpected null type")
return extractErrorType()
}
is KaClassType -> return useClassType(t)
is KaFlexibleType -> return useType(t.lowerBound) // TODO: take a more reasoned choice here
is KaClassType -> useClassType(t)
is KaFlexibleType -> useType(t.lowerBound) // TODO: take a more reasoned choice here
else -> TODO()
}
/*
OLD: KE1
when (t) {
is IrSimpleType -> return useSimpleType(t, context)
else -> {
logger.error("Unrecognised IrType: " + t.javaClass)
return extractErrorType()
}
}
*/
val javaResult = tr.javaResult
val kotlinResultBase = tr.kotlinResult
val abbreviation = t.abbreviatedType
val kotlinResultAlias = if (abbreviation == null) kotlinResultBase else {
// TODO: this cast is unsafe; .symbol is actually a KaClassLikeSymbol
val classId = addClassLabel(abbreviation.symbol as KaClassSymbol)
val kotlinBaseTypeId = kotlinResultBase.id
val kotlinAliasTypeId =
tw.getLabelFor<DbKt_type_alias>("@\"kt_type_alias;{$classId};{$kotlinBaseTypeId}\"") {
tw.writeKt_type_aliases(it, classId, kotlinBaseTypeId)
}
TypeResult(kotlinAliasTypeId /* , "TODO", "TODO" */)
}
val kotlinResultNullability = if (t.nullability.isNullable) {
val kotlinAliasTypeId = kotlinResultAlias.id
val kotlinNullableTypeId =
tw.getLabelFor<DbKt_nullable_type>("@\"kt_nullable_type;{$kotlinAliasTypeId}\"") {
tw.writeKt_nullable_types(it, kotlinAliasTypeId)
}
TypeResult(kotlinNullableTypeId /* , "TODO", "TODO" */)
} else kotlinResultAlias
return TypeResults(javaResult, kotlinResultNullability)
}

private fun KotlinUsesExtractor.extractJavaErrorType(): TypeResult<DbErrortype> {
Expand Down
8 changes: 4 additions & 4 deletions java/ql/lib/config/semmlecode.dbscheme
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,12 @@ kt_nullable_types(
)

/**
* `id` is the Kotlin type that is the alias called `name` of `kttypeid`.
* That is, it has been defined by `typealias name = kttypeid`.
* `id` is the Kotlin type that is the alias called `classid` of `kttypeid`.
* That is, it has been defined by `typealias classid = kttypeid`.
*/
kt_type_alias(
kt_type_aliases(
unique int id: @kt_type_alias,
string name: string ref,
int classid: @reftype ref,
int kttypeid: @kt_type ref
)

Expand Down
6 changes: 4 additions & 2 deletions java/ql/lib/semmle/code/Location.qll
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ predicate hasName(Element e, string name) {
or
modifiers(e, name)
or
kt_type_alias(e, name, _)
or
// TODO: An alias declaration might have a name, but the type that
// uses it doesn't
// or
// kt_type_alias(e, name, _)
ktProperties(e, name)
or
e instanceof ErrorType and name = "<CodeQL error type>"
Expand Down
6 changes: 4 additions & 2 deletions java/ql/lib/semmle/code/java/KotlinType.qll
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class KotlinTypeAlias extends KotlinType, @kt_type_alias {
result = "{" + this.getKotlinType().toString() + "}" + this.getName()
}

override string getName() { kt_type_alias(this, result, _) }
override string getName() { result = this.getAliasClass().getName() }

KotlinType getKotlinType() { kt_type_alias(this, _, result) }
Class getAliasClass() { kt_type_aliases(this, result, _) }

KotlinType getKotlinType() { kt_type_aliases(this, _, result) }
}

0 comments on commit 82c4131

Please sign in to comment.