mirror of
https://github.com/github/codeql.git
synced 2025-12-24 04:36:35 +01:00
Associate certain companion object fields with the parent class
Specifically `const`, `lateinit` and `@JvmField` properties get a static field which belongs to the containing class not the companion object, such that Java can address them via the containing class name rather than have to navigate a companion object pointer.
This commit is contained in:
@@ -135,7 +135,7 @@ open class KotlinFileExtractor(
|
|||||||
Unit
|
Unit
|
||||||
}
|
}
|
||||||
is IrField -> {
|
is IrField -> {
|
||||||
val parentId = useDeclarationParent(declaration.parent, false)?.cast<DbReftype>()
|
val parentId = useDeclarationParent(getFieldParent(declaration), false)?.cast<DbReftype>()
|
||||||
if (parentId != null) {
|
if (parentId != null) {
|
||||||
extractField(declaration, parentId)
|
extractField(declaration, parentId)
|
||||||
}
|
}
|
||||||
@@ -829,10 +829,13 @@ open class KotlinFileExtractor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (bf != null && extractBackingField) {
|
if (bf != null && extractBackingField) {
|
||||||
val fieldId = extractField(bf, parentId)
|
val fieldParentId = useDeclarationParent(getFieldParent(bf), false)
|
||||||
tw.writeKtPropertyBackingFields(id, fieldId)
|
if (fieldParentId != null) {
|
||||||
if (p.isDelegated) {
|
val fieldId = extractField(bf, fieldParentId.cast())
|
||||||
tw.writeKtPropertyDelegates(id, fieldId)
|
tw.writeKtPropertyBackingFields(id, fieldId)
|
||||||
|
if (p.isDelegated) {
|
||||||
|
tw.writeKtPropertyDelegates(id, fieldId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.semmle.extractor.java.OdasaOutput
|
|||||||
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
|
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
|
||||||
import org.jetbrains.kotlin.backend.common.ir.allOverridden
|
import org.jetbrains.kotlin.backend.common.ir.allOverridden
|
||||||
import org.jetbrains.kotlin.backend.common.lower.parentsWithSelf
|
import org.jetbrains.kotlin.backend.common.lower.parentsWithSelf
|
||||||
|
import org.jetbrains.kotlin.backend.jvm.ir.getJvmNameFromAnnotation
|
||||||
import org.jetbrains.kotlin.backend.jvm.ir.propertyIfAccessor
|
import org.jetbrains.kotlin.backend.jvm.ir.propertyIfAccessor
|
||||||
import org.jetbrains.kotlin.builtins.StandardNames
|
import org.jetbrains.kotlin.builtins.StandardNames
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
@@ -1268,9 +1269,22 @@ open class KotlinUsesExtractor(
|
|||||||
|
|
||||||
fun useValueParameter(vp: IrValueParameter, parent: Label<out DbCallable>?): Label<out DbParam> =
|
fun useValueParameter(vp: IrValueParameter, parent: Label<out DbCallable>?): Label<out DbParam> =
|
||||||
tw.getLabelFor(getValueParameterLabel(vp, parent))
|
tw.getLabelFor(getValueParameterLabel(vp, parent))
|
||||||
|
fun isDirectlyExposedCompanionObjectField(f: IrField) =
|
||||||
|
f.hasAnnotation(FqName("kotlin.jvm.JvmField")) ||
|
||||||
|
f.correspondingPropertySymbol?.owner?.let {
|
||||||
|
it.isConst || it.isLateinit
|
||||||
|
} ?: false
|
||||||
|
|
||||||
|
fun getFieldParent(f: IrField) =
|
||||||
|
f.parentClassOrNull?.let {
|
||||||
|
if (it.isCompanion && isDirectlyExposedCompanionObjectField(f))
|
||||||
|
it.parent
|
||||||
|
else
|
||||||
|
null
|
||||||
|
} ?: f.parent
|
||||||
|
|
||||||
fun getFieldLabel(f: IrField): String {
|
fun getFieldLabel(f: IrField): String {
|
||||||
val parentId = useDeclarationParent(f.parent, false)
|
val parentId = useDeclarationParent(getFieldParent(f), false)
|
||||||
return "@\"field;{$parentId};${f.name.asString()}\""
|
return "@\"field;{$parentId};${f.name.asString()}\""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user