From 5766580037af20422ef65c39168ee45ff4d2c560 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Mon, 23 Sep 2024 14:25:22 +0200 Subject: [PATCH] KE2: WIP: Move function extraction to symbols --- .../src/main/kotlin/KotlinFileExtractor.kt | 26 ++++++------- .../src/main/kotlin/KotlinUsesExtractor.kt | 38 +++++++++++-------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt b/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt index 17fae05b671..c525cd25036 100644 --- a/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt +++ b/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt @@ -5,7 +5,7 @@ import org.jetbrains.kotlin.analysis.api.components.KaDiagnosticCheckerFilter import org.jetbrains.kotlin.analysis.api.KaSession import org.jetbrains.kotlin.analysis.api.types.KaType import org.jetbrains.kotlin.KtNodeTypes -import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.analysis.api.symbols.* import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.parsing.parseNumericLiteral @@ -158,7 +158,7 @@ open class KotlinFileExtractor( file.getDeclarations().forEach { extractDeclaration( - it, + it.symbol, /* OLD: KE1 extractPrivateMembers = true, @@ -293,7 +293,7 @@ open class KotlinFileExtractor( */ fun extractDeclaration( - declaration: KtDeclaration, + declaration: KaDeclarationSymbol, /* OLD: KE1 extractPrivateMembers: Boolean, @@ -301,13 +301,13 @@ open class KotlinFileExtractor( extractAnnotations: Boolean */ ) { - with("declaration", declaration) { + with("declaration", declaration.psiSafe() ?: TODO()) { /* OLD: KE1 if (!shouldExtractDecl(declaration, extractPrivateMembers)) return */ when (declaration) { - is KtClass -> { + is KaClassSymbol -> { /* OLD: KE1 if (isExternalDeclaration(declaration)) { @@ -324,7 +324,7 @@ OLD: KE1 */ } - is KtFunction -> { + is KaNamedFunctionSymbol -> { val parentId = useDeclarationParentOf(declaration, false)?.cast() if (parentId != null) { extractFunction( @@ -1832,7 +1832,7 @@ OLD: KE1 */ private fun extractFunction( - f: KtFunction, + f: KaNamedFunctionSymbol, parentId: Label, /* OLD: KE1 @@ -2474,7 +2474,7 @@ OLD: KE1 // TODO: Can this be inlined? private fun forceExtractFunction( - f: KtFunction, + f: KaNamedFunctionSymbol, parentId: Label, /* OLD: KE1 @@ -2487,7 +2487,7 @@ OLD: KE1 overriddenAttributes: OverriddenFunctionAttributes? = null */ ): Label { - with("function", f) { + with("function", f.psiSafe() ?: TODO()) { /* OLD: KE1 DeclarationStackAdjuster(f, overriddenAttributes).use { @@ -2585,9 +2585,9 @@ OLD: KE1 it(adjustedReturnType, TypeContext.RETURN, pluginContext) } ?: adjustedReturnType */ - + val functionSyntax = f.psi as? KtDeclarationWithBody val locId = - tw.getLocation(f) + tw.getLocation(functionSyntax ?: TODO()) /* OLD: KE1 overriddenAttributes?.sourceLoc @@ -2620,7 +2620,7 @@ OLD: KE1 OLD: KE1 locId, */ - f.getNameAsName()!!.asString(), // TODO: Remove !!, // OLD: KE1: shortNames.nameInDB, + f.name.asString(), // TODO: Remove !!, // OLD: KE1: shortNames.nameInDB, f.returnType, // OLD: KE1: substReturnType, paramsSignature, parentId, @@ -2649,7 +2649,7 @@ OLD: KE1 */ tw.writeHasLocation(id, locId) - val body = f.getBodyExpression() + val body = functionSyntax?.bodyExpression ?: functionSyntax?.bodyBlockExpression if (body != null /* TODO && extractBody */) { /* OLD: KE1 diff --git a/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt b/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt index 17f0d5e3aa7..a2d689806d6 100644 --- a/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt +++ b/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt @@ -2,8 +2,10 @@ package com.github.codeql import com.intellij.psi.PsiElement import org.jetbrains.kotlin.analysis.api.KaSession +import org.jetbrains.kotlin.analysis.api.symbols.* import org.jetbrains.kotlin.analysis.api.types.KaClassType import org.jetbrains.kotlin.analysis.api.types.KaType +import org.jetbrains.kotlin.asJava.elements.psiType import org.jetbrains.kotlin.psi.* /* @@ -1012,19 +1014,21 @@ OLD: KE1 } */ - private fun parentOf(d: KtDeclaration): PsiElement { + context(KaSession) + private fun parentOf(d: KaDeclarationSymbol): KaSymbol? { /* OLD: KE1 if (d is IrField) { return getFieldParent(d) } */ - return d.parent + return d.containingSymbol ?: d.containingFile } + context(KaSession) fun useDeclarationParentOf( // The declaration - d: KtDeclaration, + d: KaDeclarationSymbol, // Whether the type of entity whose parent this is can be a // top-level entity in the JVM's eyes. If so, then its parent may // be a file; otherwise, if the parent appears to be a file foo.kt, @@ -1037,7 +1041,7 @@ OLD: KE1 */ ): Label? { - val parent = parentOf(d) + val parent = parentOf(d) ?: TODO() /* OLD: KE1 if (parent is IrExternalPackageFragment) { @@ -1061,9 +1065,9 @@ OLD: KE1 // calling this directly, as this cannot handle // IrExternalPackageFragment */ - fun useDeclarationParent( + private fun useDeclarationParent( // The declaration parent according to Kotlin - dp: PsiElement, + dp: KaSymbol, // Whether the type of entity whose parent this is can be a // top-level entity in the JVM's eyes. If so, then its parent may // be a file; otherwise, if dp is a file foo.kt, then the parent @@ -1076,11 +1080,13 @@ OLD: KE1 */ ): Label? = when (dp) { - is KtFile -> - if (canBeTopLevel) { - usePackage(dp.packageFqName.asString()) - } else { - extractFileClass(dp) + is KaFileSymbol -> + dp.psi().let { + if (canBeTopLevel) { + usePackage(it.packageFqName.asString()) + } else { + extractFileClass(it) + } } /* OLD: KE1 @@ -1432,7 +1438,7 @@ OLD: KE1 @OptIn(ObsoleteDescriptorBasedAPI::class) */ fun getFunctionLabel( - f: KtFunction, + f: KaNamedFunctionSymbol, parentId: Label, /* OLD: KE1 @@ -1446,7 +1452,7 @@ OLD: KE1 f.parent, */ parentId, - f.getNameAsName()!!.asString(), // TODO: Remove the !! // OLD KE1: getFunctionShortName(f).nameInDB, + f.name.asString(), // TODO: Remove the !! // OLD KE1: getFunctionShortName(f).nameInDB, /* OLD: KE1 (maybeParameterList ?: f.valueParameters).map { it.type }, @@ -1845,7 +1851,7 @@ OLD: KE1 */ fun useFunction( - f: KtFunction, + f: KaNamedFunctionSymbol, parentId: Label, /* OLD: KE1 @@ -1865,8 +1871,8 @@ OLD: KE1 } private fun useFunction( - f: KtFunction, - javaFun: KtFunction, + f: KaNamedFunctionSymbol, + javaFun: KaNamedFunctionSymbol, parentId: Label, /* OLD: KE1