Skip to content

Commit 4f33682

Browse files
committed
Kotlin: Start handling IrExternalPackageFragment parents
1 parent 77451de commit 4f33682

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.builtins.functions.BuiltInFunctionArity
1111
import org.jetbrains.kotlin.config.JvmAnalysisFlags
1212
import org.jetbrains.kotlin.descriptors.*
1313
import org.jetbrains.kotlin.descriptors.java.JavaVisibilities
14+
import org.jetbrains.kotlin.load.kotlin.FacadeClassSource
1415
import org.jetbrains.kotlin.ir.IrElement
1516
import org.jetbrains.kotlin.ir.IrStatement
1617
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
@@ -2154,7 +2155,7 @@ open class KotlinFileExtractor(
21542155
if (overriddenCallTarget.isLocalFunction()) {
21552156
extractTypeAccess(getLocallyVisibleFunctionLabels(overriddenCallTarget).type, locId, id, -1, enclosingCallable, enclosingStmt)
21562157
} else {
2157-
extractStaticTypeAccessQualifierUnchecked(overriddenCallTarget.parent, id, locId, enclosingCallable, enclosingStmt)
2158+
extractStaticTypeAccessQualifierUnchecked(overriddenCallTarget, id, locId, enclosingCallable, enclosingStmt)
21582159
}
21592160

21602161
extractDefaultsCallArguments(id, overriddenCallTarget, enclosingCallable, enclosingStmt, valueArguments, dispatchReceiver, extensionReceiver)
@@ -2380,8 +2381,29 @@ open class KotlinFileExtractor(
23802381
extractValueArguments(argParent, idxOffset)
23812382
}
23822383

2383-
private fun extractStaticTypeAccessQualifierUnchecked(parent: IrDeclarationParent, parentExpr: Label<out DbExprparent>, locId: Label<DbLocation>, enclosingCallable: Label<out DbCallable>?, enclosingStmt: Label<out DbStmt>?) {
2384-
if (parent is IrClass) {
2384+
private fun extractStaticTypeAccessQualifierUnchecked(target: IrDeclaration, parentExpr: Label<out DbExprparent>, locId: Label<DbLocation>, enclosingCallable: Label<out DbCallable>?, enclosingStmt: Label<out DbStmt>?) {
2385+
val parent = target.parent
2386+
if (parent is IrExternalPackageFragment) {
2387+
// This is in a file class.
2388+
// Get the name in a similar way to the compiler's ExternalPackageParentPatcherLowering
2389+
// visitMemberAccess/generateOrGetFacadeClass.
2390+
if (target is IrMemberWithContainerSource) {
2391+
val containerSource = target.containerSource
2392+
if (containerSource is FacadeClassSource) {
2393+
val facadeClassName = containerSource.facadeClassName
2394+
if (facadeClassName != null) {
2395+
// TODO: This is really a multifile-class rather than a file-class
2396+
extractTypeAccess(useFileClassType(facadeClassName.fqNameForTopLevelClassMaybeWithDollars), locId, parentExpr, -1, enclosingCallable, enclosingStmt)
2397+
} else {
2398+
extractTypeAccess(useFileClassType(containerSource.className.fqNameForTopLevelClassMaybeWithDollars), locId, parentExpr, -1, enclosingCallable, enclosingStmt)
2399+
}
2400+
} else {
2401+
logger.warnElement("Unexpected container source ${containerSource?.javaClass}", target)
2402+
}
2403+
} else {
2404+
logger.warnElement("Element in external package fragment without container source ${target.javaClass}", target)
2405+
}
2406+
} else if (parent is IrClass) {
23852407
extractTypeAccessRecursive(parent.toRawType(), locId, parentExpr, -1, enclosingCallable, enclosingStmt)
23862408
} else if (parent is IrFile) {
23872409
extractTypeAccess(useFileClassType(parent), locId, parentExpr, -1, enclosingCallable, enclosingStmt)
@@ -2392,7 +2414,7 @@ open class KotlinFileExtractor(
23922414

23932415
private fun extractStaticTypeAccessQualifier(target: IrDeclaration, parentExpr: Label<out DbExprparent>, locId: Label<DbLocation>, enclosingCallable: Label<out DbCallable>?, enclosingStmt: Label<out DbStmt>?) {
23942416
if (target.shouldExtractAsStatic) {
2395-
extractStaticTypeAccessQualifierUnchecked(target.parent, parentExpr, locId, enclosingCallable, enclosingStmt)
2417+
extractStaticTypeAccessQualifierUnchecked(target, parentExpr, locId, enclosingCallable, enclosingStmt)
23962418
}
23972419
}
23982420

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,41 @@ open class KotlinUsesExtractor(
7171
TypeResult(fakeKotlinType(), "", "")
7272
)
7373

74+
fun useFileClassType(fqName: FqName) = TypeResults(
75+
TypeResult(extractFileClass(fqName), "", ""),
76+
TypeResult(fakeKotlinType(), "", "")
77+
)
78+
79+
fun useFileClassType(pkg: String, jvmName: String) = TypeResults(
80+
TypeResult(extractFileClass(pkg, jvmName), "", ""),
81+
TypeResult(fakeKotlinType(), "", "")
82+
)
83+
7484
fun extractFileClass(f: IrFile): Label<out DbClassorinterface> {
7585
val pkg = f.fqName.asString()
7686
val jvmName = getFileClassName(f)
87+
val id = extractFileClass(pkg, jvmName)
88+
if (tw.lm.fileClassLocationsExtracted.add(f)) {
89+
val fileId = tw.mkFileId(f.path, false)
90+
val locId = tw.getWholeFileLocation(fileId)
91+
tw.writeHasLocation(id, locId)
92+
}
93+
return id
94+
}
95+
96+
fun extractFileClass(fqName: FqName): Label<out DbClassorinterface> {
97+
val pkg = if (fqName.isRoot()) "" else fqName.parent().asString()
98+
val jvmName = fqName.shortName().asString()
99+
return extractFileClass(pkg, jvmName)
100+
}
101+
102+
fun extractFileClass(pkg: String, jvmName: String): Label<out DbClassorinterface> {
77103
val qualClassName = if (pkg.isEmpty()) jvmName else "$pkg.$jvmName"
78104
val label = "@\"class;$qualClassName\""
79105
val id: Label<DbClassorinterface> = tw.getLabelFor(label) {
80-
val fileId = tw.mkFileId(f.path, false)
81-
val locId = tw.getWholeFileLocation(fileId)
82106
val pkgId = extractPackage(pkg)
83107
tw.writeClasses_or_interfaces(it, jvmName, pkgId, it)
84108
tw.writeFile_class(it)
85-
tw.writeHasLocation(it, locId)
86109

87110
addModifiers(it, "public", "final")
88111
}

java/kotlin-extractor/src/main/kotlin/TrapWriter.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ class TrapLabelManager {
4848
* duplication.
4949
*/
5050
val genericSpecialisationsExtracted = HashSet<String>()
51+
52+
/**
53+
* Sometimes, when we extract a file class we don't have the IrFile
54+
* for it, so we are not able to give it a location. This means that
55+
* the location is written outside of the label creation.
56+
* This allows us to keep track of whether we've written the location
57+
* already in this TRAP file, to avoid duplication.
58+
*/
59+
val fileClassLocationsExtracted = HashSet<IrFile>()
5160
}
5261

5362
/**

0 commit comments

Comments
 (0)