@@ -41,7 +41,7 @@ open class KotlinFileExtractor(
4141 globalExtensionState : KotlinExtractorGlobalState
4242): KotlinUsesExtractor(logger, tw, dependencyCollector, externalClassExtractor, primitiveTypeMapping, pluginContext, globalExtensionState) {
4343
44- inline fun <T > with (kind : String , element : IrElement , f : () -> T ): T {
44+ private inline fun <T > with (kind : String , element : IrElement , f : () -> T ): T {
4545 val name = when (element) {
4646 is IrFile -> element.name
4747 is IrDeclarationWithName -> element.name.asString()
@@ -86,6 +86,7 @@ open class KotlinFileExtractor(
8686 }
8787 }
8888
89+ @OptIn(ObsoleteDescriptorBasedAPI ::class )
8990 private fun isFake (d : IrDeclarationWithVisibility ): Boolean {
9091 val visibility = d.visibility
9192 if (visibility is DelegatedDescriptorVisibility && visibility.delegate == Visibilities .InvisibleFake ) {
@@ -94,6 +95,9 @@ open class KotlinFileExtractor(
9495 if (d.isFakeOverride) {
9596 return true
9697 }
98+ if ((d as ? IrFunction )?.descriptor?.isHiddenToOvercomeSignatureClash == true ) {
99+ return true
100+ }
97101 return false
98102 }
99103
@@ -367,6 +371,27 @@ open class KotlinFileExtractor(
367371 tw.writeHasLocation(stmtId, locId)
368372 }
369373
374+ fun extractObinitFunction (c : IrClass , parentId : Label <out DbClassorinterface >) {
375+ // add method:
376+ val obinitLabel = getObinitLabel(c)
377+ val obinitId = tw.getLabelFor<DbMethod >(obinitLabel)
378+ val returnType = useType(pluginContext.irBuiltIns.unitType, TypeContext .RETURN )
379+ tw.writeMethods(obinitId, " <obinit>" , " <obinit>()" , returnType.javaResult.id, parentId, obinitId)
380+ tw.writeMethodsKotlinType(obinitId, returnType.kotlinResult.id)
381+
382+ val locId = tw.getLocation(c)
383+ tw.writeHasLocation(obinitId, locId)
384+
385+ addModifiers(obinitId, " private" )
386+
387+ // add body:
388+ val blockId = tw.getFreshIdLabel<DbBlock >()
389+ tw.writeStmts_block(blockId, obinitId, 0 , obinitId)
390+ tw.writeHasLocation(blockId, locId)
391+
392+ extractDeclInitializers(c.declarations, false ) { Pair (blockId, obinitId) }
393+ }
394+
370395 fun extractClassSource (c : IrClass , extractDeclarations : Boolean , extractStaticInitializer : Boolean , extractPrivateMembers : Boolean , extractFunctionBodies : Boolean ): Label <out DbClassorinterface > {
371396 with (" class source" , c) {
372397 DeclarationStackAdjuster (c).use {
@@ -421,6 +446,9 @@ open class KotlinFileExtractor(
421446 addModifiers(instance.id, " public" , " static" , " final" )
422447 tw.writeClass_object(id.cast<DbClass >(), instance.id)
423448 }
449+ if (extractFunctionBodies && needsObinitFunction(c)) {
450+ extractObinitFunction(c, id)
451+ }
424452
425453 extractClassModifiers(c, id)
426454 extractClassSupertypes(c, id, inReceiverContext = true ) // inReceiverContext = true is specified to force extraction of member prototypes of base types
@@ -2101,6 +2129,22 @@ open class KotlinFileExtractor(
21012129 enclosingStmt : Label <out DbStmt >
21022130 ): Label <DbNewexpr > = extractNewExpr(useFunction<DbConstructor >(calledConstructor, constructorTypeArgs), constructedType, locId, parent, idx, callable, enclosingStmt)
21032131
2132+ private fun needsObinitFunction (c : IrClass ) = c.primaryConstructor == null && c.constructors.count() > 1
2133+
2134+ private fun getObinitLabel (c : IrClass ) = getFunctionLabel(
2135+ c,
2136+ null ,
2137+ " <obinit>" ,
2138+ listOf (),
2139+ pluginContext.irBuiltIns.unitType,
2140+ null ,
2141+ functionTypeParameters = listOf (),
2142+ classTypeArgsIncludingOuterClasses = listOf (),
2143+ overridesCollectionsMethod = false ,
2144+ javaSignature = null ,
2145+ addParameterWildcardsByDefault = false
2146+ )
2147+
21042148 private fun extractConstructorCall (
21052149 e : IrFunctionAccessExpression ,
21062150 parent : Label <out DbExprparent >,
@@ -2192,7 +2236,7 @@ open class KotlinFileExtractor(
21922236 }
21932237 }
21942238
2195- fun getStatementOriginOperator (origin : IrStatementOrigin ? ) = when (origin) {
2239+ private fun getStatementOriginOperator (origin : IrStatementOrigin ? ) = when (origin) {
21962240 IrStatementOrigin .PLUSEQ -> " plus"
21972241 IrStatementOrigin .MINUSEQ -> " minus"
21982242 IrStatementOrigin .MULTEQ -> " times"
@@ -2430,13 +2474,29 @@ open class KotlinFileExtractor(
24302474 loopIdMap.remove(e)
24312475 }
24322476 is IrInstanceInitializerCall -> {
2433- val stmtParent = parent.stmt(e, callable)
24342477 val irConstructor = declarationStack.peek() as ? IrConstructor
24352478 if (irConstructor == null ) {
24362479 logger.errorElement(" IrInstanceInitializerCall outside constructor" , e)
24372480 return
24382481 }
2439- extractInstanceInitializerBlock(stmtParent, irConstructor)
2482+ if (needsObinitFunction(irConstructor.parentAsClass)) {
2483+ val exprParent = parent.expr(e, callable)
2484+ val id = tw.getFreshIdLabel<DbMethodaccess >()
2485+ val type = useType(pluginContext.irBuiltIns.unitType)
2486+ val locId = tw.getLocation(e)
2487+ val methodLabel = getObinitLabel(irConstructor.parentAsClass)
2488+ val methodId = tw.getLabelFor<DbMethod >(methodLabel)
2489+ tw.writeExprs_methodaccess(id, type.javaResult.id, exprParent.parent, exprParent.idx)
2490+ tw.writeExprsKotlinType(id, type.kotlinResult.id)
2491+ tw.writeHasLocation(id, locId)
2492+ tw.writeCallableEnclosingExpr(id, callable)
2493+ tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt)
2494+ tw.writeCallableBinding(id, methodId)
2495+ }
2496+ else {
2497+ val stmtParent = parent.stmt(e, callable)
2498+ extractInstanceInitializerBlock(stmtParent, irConstructor)
2499+ }
24402500 }
24412501 is IrConstructorCall -> {
24422502 val exprParent = parent.expr(e, callable)
0 commit comments