Skip to content

Commit 847a64c

Browse files
committed
Kotlin: extract call target even if it's unbound
1 parent 88baf08 commit 847a64c

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,8 @@ open class KotlinFileExtractor(
17881788

17891789
private fun extractCall(c: IrCall, callable: Label<out DbCallable>, stmtExprParent: StmtExprParent) {
17901790
with("call", c) {
1791-
val target = tryReplaceSyntheticFunction(c.symbol.owner)
1791+
val owner = tryGetPossiblyUnboundSymbolOwner(c.symbol, c) ?: return
1792+
val target = tryReplaceSyntheticFunction(owner)
17921793

17931794
// The vast majority of types of call want an expr context, so make one available lazily:
17941795
val exprParent by lazy {
@@ -2953,15 +2954,7 @@ open class KotlinFileExtractor(
29532954
tw.writeCallableEnclosingExpr(id, callable)
29542955
tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt)
29552956

2956-
val owner = if (e.symbol.isBound) {
2957-
e.symbol.owner
2958-
}
2959-
else {
2960-
logger.warnElement("Unbound enum value, trying to use enum entry stub from descriptor", e)
2961-
2962-
@OptIn(ObsoleteDescriptorBasedAPI::class)
2963-
getIrStubFromDescriptor() { it.generateEnumEntryStub(e.symbol.descriptor) }
2964-
} ?: return
2957+
val owner = tryGetPossiblyUnboundSymbolOwner(e.symbol, e) ?: return
29652958

29662959
val vId = useEnumEntry(owner)
29672960
tw.writeVariableBinding(id, vId)
@@ -3138,15 +3131,7 @@ open class KotlinFileExtractor(
31383131
// automatically-generated `public static final MyObject INSTANCE`
31393132
// field that we are accessing here.
31403133
val exprParent = parent.expr(e, callable)
3141-
val c = if (e.symbol.isBound) {
3142-
e.symbol.owner
3143-
}
3144-
else {
3145-
logger.warnElement("Unbound object value, trying to use class stub from descriptor", e)
3146-
3147-
@OptIn(ObsoleteDescriptorBasedAPI::class)
3148-
getIrStubFromDescriptor() { it.generateClassStub(e.symbol.descriptor) }
3149-
} ?: return
3134+
val c = tryGetPossiblyUnboundSymbolOwner(e.symbol, e) ?: return
31503135

31513136
val instance = if (c.isCompanion) useCompanionObjectClassInstance(c) else useObjectClassInstance(c)
31523137

@@ -3259,6 +3244,24 @@ open class KotlinFileExtractor(
32593244
}
32603245
}
32613246

3247+
private inline fun <D: DeclarationDescriptor, reified B: IrSymbolOwner> tryGetPossiblyUnboundSymbolOwner(symbol: IrBindableSymbol<D, B>, e: IrElement): B? {
3248+
if (symbol.isBound) {
3249+
return symbol.owner
3250+
}
3251+
3252+
logger.warnElement("Unbound symbol, trying to use owner stub from descriptor", e)
3253+
3254+
@OptIn(ObsoleteDescriptorBasedAPI::class)
3255+
val owner = getIrStubFromDescriptor() { it.generateMemberStub(symbol.descriptor) }
3256+
3257+
if (owner is B) {
3258+
return owner
3259+
}
3260+
3261+
logger.errorElement("Couldn't get owner of unbound symbol from its descriptor", e)
3262+
return null
3263+
}
3264+
32623265
private fun extractSuperAccess(irType: IrType, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int, enclosingStmt: Label<out DbStmt>, locId: Label<out DbLocation>) =
32633266
tw.getFreshIdLabel<DbSuperaccess>().also {
32643267
val type = useType(irType)

0 commit comments

Comments
 (0)