Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KE2: Start working on KtTypes #18031

Open
wants to merge 4 commits into
base: ke2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt
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 Expand Up @@ -7569,11 +7569,11 @@ open class KotlinFileExtractor(
val locId = tw.getLocation(propertyReferenceExpr)

val javaResult = TypeResult(tw.getFreshIdLabel<DbClassorinterface>(), "", "")
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
TODO() // TODO:KotType val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
TODO() // TODO:KotType tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
val ids =
GeneratedClassLabels(
TypeResults(javaResult, kotlinResult),
TODO(), // TODO:KotType TypeResults(javaResult, kotlinResult),
constructor = tw.getFreshIdLabel(),
constructorBlock = tw.getFreshIdLabel()
)
Expand Down Expand Up @@ -7669,6 +7669,8 @@ open class KotlinFileExtractor(
classId,
locId
)
TODO() // TODO:KotType
/*
val fieldId = useField(backingField.owner)

helper.extractFieldReturnOfReflectionTarget(getLabels, backingField)
Expand All @@ -7680,6 +7682,7 @@ open class KotlinFileExtractor(
getterParameterTypes,
getterReturnType
)
*/
}

if (setter != null) {
Expand Down Expand Up @@ -7890,11 +7893,11 @@ open class KotlinFileExtractor(
val locId = tw.getLocation(functionReferenceExpr)

val javaResult = TypeResult(tw.getFreshIdLabel<DbClassorinterface>(), "", "")
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
TODO() // TODO:KotType val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
TODO() // TODO:KotType tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
val ids =
LocallyVisibleFunctionLabels(
TypeResults(javaResult, kotlinResult),
TODO(), // TODO:KotType TypeResults(javaResult, kotlinResult),
constructor = tw.getFreshIdLabel(),
function = tw.getFreshIdLabel(),
constructorBlock = tw.getFreshIdLabel()
Expand Down Expand Up @@ -8782,11 +8785,11 @@ open class KotlinFileExtractor(
}

val javaResult = TypeResult(tw.getFreshIdLabel<DbClassorinterface>(), "", "")
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
TODO() // TODO:KotType val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
TODO() // TODO:KotType tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
val ids =
LocallyVisibleFunctionLabels(
TypeResults(javaResult, kotlinResult),
TODO(), // TODO:KotType TypeResults(javaResult, kotlinResult),
constructor = tw.getFreshIdLabel(),
constructorBlock = tw.getFreshIdLabel(),
function = tw.getFreshIdLabel()
Expand Down
68 changes: 34 additions & 34 deletions java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ open class KotlinUsesExtractor(

private fun extractErrorType(): TypeResults {
val javaResult = extractJavaErrorType()
val kotlinTypeId =
tw.getLabelFor<DbKt_nullable_type>("@\"errorKotlinType\"") {
tw.writeKt_nullable_types(it, javaResult.id)
}
TODO() // TODO:KotType val kotlinTypeId =
TODO() // TODO:KotType tw.getLabelFor<DbKt_nullable_type>("@\"errorKotlinType\"") {
TODO() // TODO:KotType tw.writeKt_nullable_types(it, javaResult.id)
TODO() // TODO:KotType }
return TypeResults(
javaResult,
TypeResult(kotlinTypeId, "<CodeQL error type>", "<CodeQL error type>")
TODO() // TODO:KotType TypeResult(kotlinTypeId, "<CodeQL error type>", "<CodeQL error type>")
)
}

Expand Down Expand Up @@ -635,12 +635,12 @@ open class KotlinUsesExtractor(
"@\"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
TODO() // TODO:KotType val fakeKotlinTypeId: Label<DbKt_nullable_type> =
TODO() // TODO:KotType tw.getLabelFor(
TODO() // TODO:KotType "@\"FakeKotlinType\"",
TODO() // TODO:KotType { tw.writeKt_nullable_types(it, fakeKotlinClassId) }
TODO() // TODO:KotType )
TODO() // TODO:KotType return fakeKotlinTypeId
}

// `args` can be null to describe a raw generic type.
Expand All @@ -659,15 +659,15 @@ open class KotlinUsesExtractor(
else if (hasQuestionMark) {
val kotlinSignature = "$kotlinQualClassName?" // TODO: Is this right?
val kotlinLabel = "@\"kt_type;nullable;$kotlinQualClassName\""
val kotlinId: Label<DbKt_nullable_type> =
tw.getLabelFor(kotlinLabel, { tw.writeKt_nullable_types(it, javaClassId) })
TypeResult(kotlinId, kotlinSignature, "TODO")
TODO() // TODO:KotType val kotlinId: Label<DbKt_nullable_type> =
TODO() // TODO:KotType tw.getLabelFor(kotlinLabel, { tw.writeKt_nullable_types(it, javaClassId) })
TODO() // TODO:KotType TypeResult(kotlinId, kotlinSignature, "TODO")
} else {
val kotlinSignature = kotlinQualClassName // TODO: Is this right?
val kotlinLabel = "@\"kt_type;notnull;$kotlinQualClassName\""
val kotlinId: Label<DbKt_notnull_type> =
tw.getLabelFor(kotlinLabel, { tw.writeKt_notnull_types(it, javaClassId) })
TypeResult(kotlinId, kotlinSignature, "TODO")
TODO() // TODO:KotType val kotlinId: Label<DbKt_notnull_type> =
TODO() // TODO:KotType tw.getLabelFor(kotlinLabel, { tw.writeKt_notnull_types(it, javaClassId) })
TODO() // TODO:KotType TypeResult(kotlinId, kotlinSignature, "TODO")
}
return TypeResults(javaResult, kotlinResult)
}
Expand Down Expand Up @@ -864,19 +864,19 @@ open class KotlinUsesExtractor(
val kotlinSignature =
"$kotlinPackageName.$kotlinClassName?" // TODO: Is this right?
val kotlinLabel = "@\"kt_type;nullable;$kotlinPackageName.$kotlinClassName\""
val kotlinId: Label<DbKt_nullable_type> =
tw.getLabelFor(
kotlinLabel,
{ tw.writeKt_nullable_types(it, kotlinClassId) }
)
TypeResult(kotlinId, kotlinSignature, "TODO")
TODO() // TODO:KotType val kotlinId: Label<DbKt_nullable_type> =
TODO() // TODO:KotType tw.getLabelFor(
TODO() // TODO:KotType kotlinLabel,
TODO() // TODO:KotType { tw.writeKt_nullable_types(it, kotlinClassId) }
TODO() // TODO:KotType )
TODO() // TODO:KotType TypeResult(kotlinId, kotlinSignature, "TODO")
} else {
val kotlinSignature =
"$kotlinPackageName.$kotlinClassName" // TODO: Is this right?
val kotlinLabel = "@\"kt_type;notnull;$kotlinPackageName.$kotlinClassName\""
val kotlinId: Label<DbKt_notnull_type> =
tw.getLabelFor(kotlinLabel, { tw.writeKt_notnull_types(it, kotlinClassId) })
TypeResult(kotlinId, kotlinSignature, "TODO")
TODO() // TODO:KotType val kotlinId: Label<DbKt_notnull_type> =
TODO() // TODO:KotType tw.getLabelFor(kotlinLabel, { tw.writeKt_notnull_types(it, kotlinClassId) })
TODO() // TODO:KotType TypeResult(kotlinId, kotlinSignature, "TODO")
}
return TypeResults(javaResult, kotlinResult)
}
Expand Down Expand Up @@ -919,15 +919,15 @@ open class KotlinUsesExtractor(
else if (s.isNullable()) {
val kotlinSignature = "${javaResult.signature}?" // TODO: Wrong
val kotlinLabel = "@\"kt_type;nullable;type_param\"" // TODO: Wrong
val kotlinId: Label<DbKt_nullable_type> =
tw.getLabelFor(kotlinLabel, { tw.writeKt_nullable_types(it, aClassId) })
TypeResult(kotlinId, kotlinSignature, "TODO")
TODO() // TODO:KotType val kotlinId: Label<DbKt_nullable_type> =
TODO() // TODO:KotType tw.getLabelFor(kotlinLabel, { tw.writeKt_nullable_types(it, aClassId) })
TODO() // TODO:KotType TypeResult(kotlinId, kotlinSignature, "TODO")
} else {
val kotlinSignature = javaResult.signature // TODO: Wrong
val kotlinLabel = "@\"kt_type;notnull;type_param\"" // TODO: Wrong
val kotlinId: Label<DbKt_notnull_type> =
tw.getLabelFor(kotlinLabel, { tw.writeKt_notnull_types(it, aClassId) })
TypeResult(kotlinId, kotlinSignature, "TODO")
TODO() // TODO:KotType val kotlinId: Label<DbKt_notnull_type> =
TODO() // TODO:KotType tw.getLabelFor(kotlinLabel, { tw.writeKt_notnull_types(it, aClassId) })
TODO() // TODO:KotType TypeResult(kotlinId, kotlinSignature, "TODO")
}
return TypeResults(javaResult, kotlinResult)
}
Expand Down Expand Up @@ -1621,8 +1621,8 @@ open class KotlinUsesExtractor(
var res = tw.lm.locallyVisibleFunctionLabelMapping[f]
if (res == null) {
val javaResult = TypeResult(tw.getFreshIdLabel<DbClassorinterface>(), "", "")
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
val kotlinResult = TODO() // TODO:KotType TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
TODO() // TODO:KotType tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
res =
LocallyVisibleFunctionLabels(
TypeResults(javaResult, kotlinResult),
Expand Down
68 changes: 34 additions & 34 deletions java/kotlin-extractor2/src/main/kotlin/entities/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,48 @@ 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 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? Shouldn't we check the nullability of the KaClassType?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the nullability belongs in useType, but it looks like that I got so focused on working out what the dbscheme tables should look like that I didn't add it. I'll add a commit for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I'm not sure it's perfect yet, and we'll have to think about how to represent the definition of a type alias, but I think this gives us something to work with in the mean time.

tw.getLabelFor<DbKt_class_type>("@\"kt_class;{$classId}\"") {
tw.writeKt_class_types(it, classId)
}
val kotlinResult = TypeResult(kotlinTypeId /* , "TODO", "TODO" */)
return TypeResults(javaResult, kotlinResult)
}

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 All @@ -44,32 +61,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
34 changes: 26 additions & 8 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
)

kt_type_alias(
/**
* `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_aliases(
unique int id: @kt_type_alias,
string name: string ref,
int classid: @reftype 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
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
Loading
Loading