@@ -1038,13 +1038,7 @@ open class KotlinFileExtractor(
10381038 val methodId = ids.function
10391039 tw.writeCallableBinding(id, methodId)
10401040
1041- val idNewexpr = tw.getFreshIdLabel<DbNewexpr >()
1042- tw.writeExprs_newexpr(idNewexpr, ids.type.javaResult.id, id, - 1 )
1043- tw.writeExprsKotlinType(idNewexpr, ids.type.kotlinResult.id)
1044- tw.writeHasLocation(idNewexpr, locId)
1045- tw.writeCallableEnclosingExpr(idNewexpr, enclosingCallable)
1046- tw.writeStatementEnclosingExpr(idNewexpr, enclosingStmt)
1047- tw.writeCallableBinding(idNewexpr, ids.constructor )
1041+ val idNewexpr = extractNewExpr(ids.constructor , ids.type, locId, id, - 1 , enclosingCallable, enclosingStmt)
10481042
10491043 @Suppress(" UNCHECKED_CAST" )
10501044 tw.writeIsAnonymClass(ids.type.javaResult.id as Label <DbClass >, idNewexpr)
@@ -1150,6 +1144,23 @@ open class KotlinFileExtractor(
11501144 result
11511145 }
11521146
1147+ val kotlinNoWhenBranchMatchedExn by lazy {
1148+ val result = pluginContext.referenceClass(FqName (" kotlin.NoWhenBranchMatchedException" ))?.owner
1149+ result?.let { extractExternalClassLater(it) }
1150+ result
1151+ }
1152+
1153+ val kotlinNoWhenBranchMatchedConstructor by lazy {
1154+ val result = kotlinNoWhenBranchMatchedExn?.declarations?.find {
1155+ it is IrConstructor &&
1156+ it.valueParameters.isEmpty()
1157+ } as IrConstructor ?
1158+ if (result == null ) {
1159+ logger.error(" Couldn't find no-arg constructor for kotlin.NoWhenBranchMatchedException" )
1160+ }
1161+ result
1162+ }
1163+
11531164 fun isFunction (target : IrFunction , pkgName : String , classNameLogged : String , classNamePredicate : (String ) -> Boolean , fName : String , hasQuestionMark : Boolean? = false): Boolean {
11541165 val verbose = false
11551166 fun verboseln (s : String ) { if (verbose) println (s) }
@@ -1220,10 +1231,27 @@ open class KotlinFileExtractor(
12201231 else -> false
12211232 }
12221233
1223- fun extractCall (c : IrCall , callable : Label <out DbCallable >, parent : Label < out DbExprparent >, idx : Int , enclosingStmt : Label < out DbStmt > ) {
1234+ fun extractCall (c : IrCall , callable : Label <out DbCallable >, stmtExprParent : StmtExprParent ) {
12241235 with (" call" , c) {
12251236 val target = c.symbol.owner
12261237
1238+ // The vast majority of types of call want an expr context, so make one available lazily:
1239+ val exprParent by lazy {
1240+ stmtExprParent.expr(c, callable)
1241+ }
1242+
1243+ val parent by lazy {
1244+ exprParent.parent
1245+ }
1246+
1247+ val idx by lazy {
1248+ exprParent.idx
1249+ }
1250+
1251+ val enclosingStmt by lazy {
1252+ exprParent.enclosingStmt
1253+ }
1254+
12271255 fun extractMethodAccess (syntacticCallTarget : IrFunction , extractMethodTypeArguments : Boolean = true, extractClassTypeArguments : Boolean = false) {
12281256 val typeArgs =
12291257 if (extractMethodTypeArguments)
@@ -1451,8 +1479,16 @@ open class KotlinFileExtractor(
14511479 logger.errorElement(" Unhandled builtin" , c)
14521480 }
14531481 isBuiltinCallInternal(c, " noWhenBranchMatchedException" ) -> {
1454- // TODO
1455- logger.errorElement(" Unhandled builtin" , c)
1482+ kotlinNoWhenBranchMatchedConstructor?.let {
1483+ val locId = tw.getLocation(c)
1484+ val thrownType = useSimpleTypeClass(it.parentAsClass, listOf (), false )
1485+ val stmtParent = stmtExprParent.stmt(c, callable)
1486+ val throwId = tw.getFreshIdLabel<DbThrowstmt >()
1487+ tw.writeStmts_throwstmt(throwId, stmtParent.parent, stmtParent.idx, callable)
1488+ tw.writeHasLocation(throwId, locId)
1489+ val newExprId = extractNewExpr(it, null , thrownType, locId, throwId, 0 , callable, throwId)
1490+ extractTypeAccess(thrownType, callable, newExprId, - 3 , c, throwId)
1491+ }
14561492 }
14571493 isBuiltinCallInternal(c, " illegalArgumentException" ) -> {
14581494 // TODO
@@ -1676,38 +1712,61 @@ open class KotlinFileExtractor(
16761712 extractTypeArguments((0 until c.typeArgumentsCount).map { c.getTypeArgument(it)!! }, c, parentExpr, enclosingCallable, enclosingStmt, startIndex, reverse)
16771713 }
16781714
1715+ private fun extractNewExpr (
1716+ methodId : Label <out DbConstructor >,
1717+ constructedType : TypeResults ,
1718+ locId : Label <out DbLocation >,
1719+ parent : Label <out DbExprparent >,
1720+ idx : Int ,
1721+ callable : Label <out DbCallable >,
1722+ enclosingStmt : Label <out DbStmt >
1723+ ): Label <DbNewexpr > {
1724+ val id = tw.getFreshIdLabel<DbNewexpr >()
1725+ tw.writeExprs_newexpr(id, constructedType.javaResult.id, parent, idx)
1726+ tw.writeExprsKotlinType(id, constructedType.kotlinResult.id)
1727+ tw.writeHasLocation(id, locId)
1728+ tw.writeCallableEnclosingExpr(id, callable)
1729+ tw.writeStatementEnclosingExpr(id, enclosingStmt)
1730+ tw.writeCallableBinding(id, methodId)
1731+ return id
1732+ }
1733+
1734+ private fun extractNewExpr (
1735+ calledConstructor : IrFunction ,
1736+ constructorTypeArgs : List <IrTypeArgument >? ,
1737+ constructedType : TypeResults ,
1738+ locId : Label <out DbLocation >,
1739+ parent : Label <out DbExprparent >,
1740+ idx : Int ,
1741+ callable : Label <out DbCallable >,
1742+ enclosingStmt : Label <out DbStmt >
1743+ ): Label <DbNewexpr > = extractNewExpr(useFunction<DbConstructor >(calledConstructor, constructorTypeArgs), constructedType, locId, parent, idx, callable, enclosingStmt)
1744+
16791745 private fun extractConstructorCall (
16801746 e : IrFunctionAccessExpression ,
16811747 parent : Label <out DbExprparent >,
16821748 idx : Int ,
16831749 callable : Label <out DbCallable >,
16841750 enclosingStmt : Label <out DbStmt >
16851751 ) {
1686- val id = tw.getFreshIdLabel<DbNewexpr >()
1687- val type: TypeResults
16881752 val isAnonymous = e.type.isAnonymous
1689- if (isAnonymous) {
1753+ val type : TypeResults = if (isAnonymous) {
16901754 if (e.typeArgumentsCount > 0 ) {
16911755 logger.warn(" Unexpected type arguments for anonymous class constructor call" )
16921756 }
1693-
16941757 val c = (e.type as IrSimpleType ).classifier.owner as IrClass
1758+ useAnonymousClass(c)
1759+ } else {
1760+ useType(e.type)
1761+ }
1762+ val locId = tw.getLocation(e)
1763+ val id = extractNewExpr(e.symbol.owner, (e.type as ? IrSimpleType )?.arguments, type, locId, parent, idx, callable, enclosingStmt)
16951764
1696- type = useAnonymousClass(c)
1697-
1765+ if (isAnonymous) {
16981766 @Suppress(" UNCHECKED_CAST" )
16991767 tw.writeIsAnonymClass(type.javaResult.id as Label <DbClass >, id)
1700- } else {
1701- type = useType(e.type)
17021768 }
1703- val locId = tw.getLocation(e)
1704- val methodId = useFunction<DbConstructor >(e.symbol.owner, (e.type as ? IrSimpleType )?.arguments)
1705- tw.writeExprs_newexpr(id, type.javaResult.id, parent, idx)
1706- tw.writeExprsKotlinType(id, type.kotlinResult.id)
1707- tw.writeHasLocation(id, locId)
1708- tw.writeCallableEnclosingExpr(id, callable)
1709- tw.writeStatementEnclosingExpr(id, enclosingStmt)
1710- tw.writeCallableBinding(id, methodId)
1769+
17111770 for (i in 0 until e.valueArgumentsCount) {
17121771 val arg = e.getValueArgument(i)
17131772 if (arg != null ) {
@@ -2055,8 +2114,7 @@ open class KotlinFileExtractor(
20552114 extractConstructorCall(e, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
20562115 }
20572116 is IrCall -> {
2058- val exprParent = parent.expr(e, callable)
2059- extractCall(e, callable, exprParent.parent, exprParent.idx, exprParent.enclosingStmt)
2117+ extractCall(e, callable, parent)
20602118 }
20612119 is IrStringConcatenation -> {
20622120 val exprParent = parent.expr(e, callable)
@@ -3255,13 +3313,7 @@ open class KotlinFileExtractor(
32553313 tw.writeStatementEnclosingExpr(id, enclosingStmt)
32563314 extractTypeAccess(e.typeOperand, callable, id, 0 , e, enclosingStmt)
32573315
3258- val idNewexpr = tw.getFreshIdLabel<DbNewexpr >()
3259- tw.writeExprs_newexpr(idNewexpr, ids.type.javaResult.id, id, 1 )
3260- tw.writeExprsKotlinType(idNewexpr, ids.type.kotlinResult.id)
3261- tw.writeHasLocation(idNewexpr, locId)
3262- tw.writeCallableEnclosingExpr(idNewexpr, callable)
3263- tw.writeStatementEnclosingExpr(idNewexpr, enclosingStmt)
3264- tw.writeCallableBinding(idNewexpr, ids.constructor )
3316+ val idNewexpr = extractNewExpr(ids.constructor , ids.type, locId, id, 1 , callable, enclosingStmt)
32653317
32663318 @Suppress(" UNCHECKED_CAST" )
32673319 tw.writeIsAnonymClass(ids.type.javaResult.id as Label <DbClass >, idNewexpr)
0 commit comments