diff --git a/java/kotlin-extractor2/src/main/kotlin/entities/Types.kt b/java/kotlin-extractor2/src/main/kotlin/entities/Types.kt index ae98127eef979..f54b723b90f4f 100644 --- a/java/kotlin-extractor2/src/main/kotlin/entities/Types.kt +++ b/java/kotlin-extractor2/src/main/kotlin/entities/Types.kt @@ -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("@\"kt_class{$classId}\"") { + tw.writeKt_class_types(it, classId) + } + val kotlinResult = TypeResult(kotlinTypeId /* , "TODO", "TODO" */) return TypeResults(javaResult, kotlinResult) } @@ -44,8 +49,8 @@ private fun KotlinUsesExtractor.extractJavaErrorType(): TypeResult private fun KotlinUsesExtractor.extractErrorType(): TypeResults { val javaResult = extractJavaErrorType() val kotlinTypeId = - tw.getLabelFor("@\"errorKotlinType\"") { - tw.writeKt_nullable_types(it, javaResult.id) + tw.getLabelFor("@\"errorKotlinType\"") { + tw.writeKt_error_types(it) } return TypeResults( javaResult, @@ -53,23 +58,6 @@ private fun KotlinUsesExtractor.extractErrorType(): TypeResults { ) } -// TODO -fun KotlinUsesExtractor.fakeKotlinType(): Label { - val fakeKotlinPackageId: Label = - tw.getLabelFor("@\"FakeKotlinPackage\"", { tw.writePackages(it, "fake.kotlin") }) - val fakeKotlinClassId: Label = - tw.getLabelFor( - "@\"FakeKotlinClass\"", - { tw.writeClasses_or_interfaces(it, "FakeKotlinClass", fakeKotlinPackageId, it) } - ) - val fakeKotlinTypeId: Label = - tw.getLabelFor( - "@\"FakeKotlinType\"", - { tw.writeKt_nullable_types(it, fakeKotlinClassId) } - ) - return fakeKotlinTypeId -} - /* OLD: KE1 // `args` can be null to describe a raw generic type. diff --git a/java/ql/lib/config/semmlecode.dbscheme b/java/ql/lib/config/semmlecode.dbscheme index e0fd7f76ab869..d162a1bc3f652 100644 --- a/java/ql/lib/config/semmlecode.dbscheme +++ b/java/ql/lib/config/semmlecode.dbscheme @@ -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`. diff --git a/java/ql/lib/semmle/code/java/KotlinType.qll b/java/ql/lib/semmle/code/java/KotlinType.qll index 3e5597c5579d8..159484bdc5fb4 100644 --- a/java/ql/lib/semmle/code/java/KotlinType.qll +++ b/java/ql/lib/semmle/code/java/KotlinType.qll @@ -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) } }