Skip to content

Commit

Permalink
KE2: Start working on KtTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
igfoo committed Nov 19, 2024
1 parent c96cb77 commit 380b3f8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
30 changes: 9 additions & 21 deletions java/kotlin-extractor2/src/main/kotlin/entities/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ private fun KotlinUsesExtractor.useClassType(
c: KaClassType
): TypeResults {
// TODO: this cast is unsafe; .symbol is actually a KaClassLikeSymbol
val javaResult = TypeResult(addClassLabel(c.symbol as KaClassSymbol) /* , TODO, TODO */)
val kotlinResult = TypeResult(fakeKotlinType() /* , "TODO", "TODO" */)
val classId = addClassLabel(c.symbol as KaClassSymbol)
val javaResult = TypeResult(classId /* , TODO, TODO */)
val kotlinTypeId =
tw.getLabelFor<DbKt_class_type>("@\"kt_class{$classId}\"") {
tw.writeKt_class_types(it, classId)
}
val kotlinResult = TypeResult(kotlinTypeId /* , "TODO", "TODO" */)
return TypeResults(javaResult, kotlinResult)
}

Expand Down Expand Up @@ -44,32 +49,15 @@ private fun KotlinUsesExtractor.extractJavaErrorType(): TypeResult<DbErrortype>
private fun KotlinUsesExtractor.extractErrorType(): TypeResults {
val javaResult = extractJavaErrorType()
val kotlinTypeId =
tw.getLabelFor<DbKt_nullable_type>("@\"errorKotlinType\"") {
tw.writeKt_nullable_types(it, javaResult.id)
tw.getLabelFor<DbKt_error_type>("@\"errorKotlinType\"") {
tw.writeKt_error_types(it)
}
return TypeResults(
javaResult,
TypeResult(kotlinTypeId /* TODO , "<CodeQL error type>", "<CodeQL error type>" */)
)
}

// TODO
fun KotlinUsesExtractor.fakeKotlinType(): Label<out DbKt_type> {
val fakeKotlinPackageId: Label<DbPackage> =
tw.getLabelFor("@\"FakeKotlinPackage\"", { tw.writePackages(it, "fake.kotlin") })
val fakeKotlinClassId: Label<DbClassorinterface> =
tw.getLabelFor(
"@\"FakeKotlinClass\"",
{ tw.writeClasses_or_interfaces(it, "FakeKotlinClass", fakeKotlinPackageId, it) }
)
val fakeKotlinTypeId: Label<DbKt_nullable_type> =
tw.getLabelFor(
"@\"FakeKotlinType\"",
{ tw.writeKt_nullable_types(it, fakeKotlinClassId) }
)
return fakeKotlinTypeId
}

/*
OLD: KE1
// `args` can be null to describe a raw generic type.
Expand Down
30 changes: 24 additions & 6 deletions java/ql/lib/config/semmlecode.dbscheme
Original file line number Diff line number Diff line change
Expand Up @@ -461,23 +461,41 @@ type_companion_object(
unique int companion_object: @classorinterface ref
);

kt_nullable_types(
unique int id: @kt_nullable_type,
/**
* `id` is the Kotlin type for the non-nullable class type `classid`.
*/
kt_class_types(
unique int id: @kt_class_type,
int classid: @reftype ref
)

kt_notnull_types(
unique int id: @kt_notnull_type,
int classid: @reftype ref
/**
* `id` is the Kotlin type that is the same as `kttypeid`, but is nullable.
*/
kt_nullable_types(
unique int id: @kt_nullable_type,
int kttypeid: @kt_type ref
)

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

@kt_type = @kt_nullable_type | @kt_notnull_type
/**
* A `kt_error_type` is used when the extractor is unable to extract a type
* correctly for some reason.
*/
kt_error_types(
unique int id: @kt_error_type
)

@kt_type = @kt_class_type | @kt_nullable_type | @kt_type_alias | @kt_error_type

/**
* This holds if `id` is an `interface`, rather than a `class`.
Expand Down
25 changes: 14 additions & 11 deletions java/ql/lib/semmle/code/java/KotlinType.qll
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,31 @@ class KotlinType extends Element, @kt_type { }

class KotlinNullableType extends KotlinType, @kt_nullable_type {
override string toString() {
exists(RefType javaType |
kt_nullable_types(this, javaType) and
result = "Kotlin nullable " + javaType.toString()
exists(KotlinType ktType |
kt_nullable_types(this, ktType) and
result = ktType.toString() + "?"
)
}

override string getAPrimaryQlClass() { result = "KotlinNullableType" }
}

class KotlinNotnullType extends KotlinType, @kt_notnull_type {
override string toString() {
exists(RefType javaType |
kt_notnull_types(this, javaType) and
result = "Kotlin not-null " + javaType.toString()
)
}
class KotlinClassType extends KotlinType, @kt_class_type {
override string toString() { result = this.getClass().toString() }

override string getAPrimaryQlClass() { result = "KotlinNotnullType" }

RefType getClass() { kt_class_types(this, result) }
}

class KotlinTypeAlias extends Element, @kt_type_alias {
class KotlinTypeAlias extends KotlinType, @kt_type_alias {
override string getAPrimaryQlClass() { result = "KotlinTypeAlias" }

override string toString() {
result = "{" + this.getKotlinType().toString() + "}" + this.getName()
}

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

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

0 comments on commit 380b3f8

Please sign in to comment.