@@ -1736,7 +1736,7 @@ open class KotlinFileExtractor(
1736
1736
private fun extractsDefaultsCall (
1737
1737
syntacticCallTarget : IrFunction ,
1738
1738
locId : Label <DbLocation >,
1739
- callsite : IrCall ,
1739
+ resultType : IrType ,
1740
1740
enclosingCallable : Label <out DbCallable >,
1741
1741
callsiteParent : Label <out DbExprparent >,
1742
1742
childIdx : Int ,
@@ -1751,7 +1751,7 @@ open class KotlinFileExtractor(
1751
1751
useFunction<DbCallable >(callTarget)
1752
1752
}
1753
1753
val defaultMethodLabel = getDefaultsMethodLabel(callTarget)
1754
- val id = extractMethodAccessWithoutArgs(callsite.type , locId, enclosingCallable, callsiteParent, childIdx, enclosingStmt, defaultMethodLabel)
1754
+ val id = extractMethodAccessWithoutArgs(resultType , locId, enclosingCallable, callsiteParent, childIdx, enclosingStmt, defaultMethodLabel)
1755
1755
1756
1756
if (callTarget.isLocalFunction()) {
1757
1757
extractTypeAccess(getLocallyVisibleFunctionLabels(callTarget).type, locId, id, - 1 , enclosingCallable, enclosingStmt)
@@ -1854,7 +1854,8 @@ open class KotlinFileExtractor(
1854
1854
1855
1855
fun extractRawMethodAccess (
1856
1856
syntacticCallTarget : IrFunction ,
1857
- callsite : IrCall ,
1857
+ locElement : IrElement ,
1858
+ resultType : IrType ,
1858
1859
enclosingCallable : Label <out DbCallable >,
1859
1860
callsiteParent : Label <out DbExprparent >,
1860
1861
childIdx : Int ,
@@ -1866,13 +1867,13 @@ open class KotlinFileExtractor(
1866
1867
extractClassTypeArguments : Boolean = false,
1867
1868
superQualifierSymbol : IrClassSymbol ? = null) {
1868
1869
1869
- val locId = tw.getLocation(callsite )
1870
+ val locId = tw.getLocation(locElement )
1870
1871
1871
1872
if (valueArguments.any { it == null }) {
1872
1873
extractsDefaultsCall(
1873
1874
syntacticCallTarget,
1874
1875
locId,
1875
- callsite ,
1876
+ resultType ,
1876
1877
enclosingCallable,
1877
1878
callsiteParent,
1878
1879
childIdx,
@@ -1885,7 +1886,7 @@ open class KotlinFileExtractor(
1885
1886
extractRawMethodAccess(
1886
1887
syntacticCallTarget,
1887
1888
locId,
1888
- callsite.type ,
1889
+ resultType ,
1889
1890
enclosingCallable,
1890
1891
callsiteParent,
1891
1892
childIdx,
@@ -2216,7 +2217,7 @@ open class KotlinFileExtractor(
2216
2217
return
2217
2218
}
2218
2219
2219
- extractRawMethodAccess(syntacticCallTarget, c, callable, parent, idx, enclosingStmt, (0 until c.valueArgumentsCount).map { c.getValueArgument(it) }, c.dispatchReceiver, c.extensionReceiver, typeArgs, extractClassTypeArguments, c.superQualifierSymbol)
2220
+ extractRawMethodAccess(syntacticCallTarget, c, c.type, callable, parent, idx, enclosingStmt, (0 until c.valueArgumentsCount).map { c.getValueArgument(it) }, c.dispatchReceiver, c.extensionReceiver, typeArgs, extractClassTypeArguments, c.superQualifierSymbol)
2220
2221
}
2221
2222
2222
2223
fun extractSpecialEnumFunction (fnName : String ){
@@ -2320,7 +2321,7 @@ open class KotlinFileExtractor(
2320
2321
}
2321
2322
isFunction(target, " kotlin" , " String" , " plus" , true ) -> {
2322
2323
findJdkIntrinsicOrWarn(" stringPlus" , c)?.let { stringPlusFn ->
2323
- extractRawMethodAccess(stringPlusFn, c, callable, parent, idx, enclosingStmt, listOf (c.extensionReceiver, c.getValueArgument(0 )), null , null )
2324
+ extractRawMethodAccess(stringPlusFn, c, c.type, callable, parent, idx, enclosingStmt, listOf (c.extensionReceiver, c.getValueArgument(0 )), null , null )
2324
2325
}
2325
2326
}
2326
2327
isNumericFunction(target, listOf (" plus" , " minus" , " times" , " div" , " rem" , " and" , " or" , " xor" , " shl" , " shr" , " ushr" )) -> {
@@ -2562,7 +2563,7 @@ open class KotlinFileExtractor(
2562
2563
}
2563
2564
isFunction(target, " kotlin" , " Any" , " toString" , true ) -> {
2564
2565
stringValueOfObjectMethod?.let {
2565
- extractRawMethodAccess(it, c, callable, parent, idx, enclosingStmt, listOf (c.extensionReceiver), null , null )
2566
+ extractRawMethodAccess(it, c, c.type, callable, parent, idx, enclosingStmt, listOf (c.extensionReceiver), null , null )
2566
2567
}
2567
2568
}
2568
2569
isBuiltinCallKotlin(c, " enumValues" ) -> {
@@ -2612,6 +2613,22 @@ open class KotlinFileExtractor(
2612
2613
|| isBuiltinCallKotlin(c, " byteArrayOf" )
2613
2614
|| isBuiltinCallKotlin(c, " booleanArrayOf" ) -> {
2614
2615
2616
+ // TODO: is there any reason not to always use getArrayElementType?
2617
+ val elementType = if (isBuiltinCallKotlin(c, " arrayOf" )) {
2618
+ if (c.typeArgumentsCount == 1 ) {
2619
+ c.getTypeArgument(0 ).also {
2620
+ if (it == null ) {
2621
+ logger.errorElement(" Type argument missing in an arrayOf call" , c)
2622
+ }
2623
+ }
2624
+ } else {
2625
+ logger.errorElement(" Expected to find one type argument in arrayOf call" , c)
2626
+ null
2627
+ }
2628
+ } else {
2629
+ c.type.getArrayElementType(pluginContext.irBuiltIns)
2630
+ }
2631
+
2615
2632
val arg = if (c.valueArgumentsCount == 1 ) c.getValueArgument(0 ) else {
2616
2633
logger.errorElement(" Expected to find only one (vararg) argument in ${c.symbol.owner.name.asString()} call" , c)
2617
2634
null
@@ -2622,59 +2639,7 @@ open class KotlinFileExtractor(
2622
2639
}
2623
2640
}
2624
2641
2625
- // If this is [someType]ArrayOf(*x), x, otherwise null
2626
- val clonedArray = arg?.let {
2627
- if (arg.elements.size == 1 ) {
2628
- val onlyElement = arg.elements[0 ]
2629
- if (onlyElement is IrSpreadElement )
2630
- onlyElement.expression
2631
- else null
2632
- } else null
2633
- }
2634
-
2635
- if (clonedArray != null ) {
2636
- // This is an array clone: extract is as a call to java.lang.Object.clone
2637
- objectCloneMethod?.let {
2638
- extractRawMethodAccess(it, c, callable, parent, idx, enclosingStmt, listOf (), clonedArray, null )
2639
- }
2640
- } else {
2641
- // This is array creation: extract it as a call to new ArrayType[] { ... }
2642
- val id = tw.getFreshIdLabel<DbArraycreationexpr >()
2643
- val type = useType(c.type)
2644
- tw.writeExprs_arraycreationexpr(id, type.javaResult.id, parent, idx)
2645
- tw.writeExprsKotlinType(id, type.kotlinResult.id)
2646
- val locId = tw.getLocation(c)
2647
- tw.writeHasLocation(id, locId)
2648
- tw.writeCallableEnclosingExpr(id, callable)
2649
-
2650
- if (isBuiltinCallKotlin(c, " arrayOf" )) {
2651
- if (c.typeArgumentsCount == 1 ) {
2652
- val typeArgument = c.getTypeArgument(0 )
2653
- if (typeArgument == null ) {
2654
- logger.errorElement(" Type argument missing in an arrayOf call" , c)
2655
- } else {
2656
- extractTypeAccessRecursive(typeArgument, locId, id, - 1 , callable, enclosingStmt, TypeContext .GENERIC_ARGUMENT )
2657
- }
2658
- } else {
2659
- logger.errorElement(" Expected to find one type argument in arrayOf call" , c )
2660
- }
2661
- } else {
2662
- val elementType = c.type.getArrayElementType(pluginContext.irBuiltIns)
2663
- extractTypeAccessRecursive(elementType, locId, id, - 1 , callable, enclosingStmt)
2664
- }
2665
-
2666
- arg?.let {
2667
- val initId = tw.getFreshIdLabel<DbArrayinit >()
2668
- tw.writeExprs_arrayinit(initId, type.javaResult.id, id, - 2 )
2669
- tw.writeExprsKotlinType(initId, type.kotlinResult.id)
2670
- tw.writeHasLocation(initId, locId)
2671
- tw.writeCallableEnclosingExpr(initId, callable)
2672
- tw.writeStatementEnclosingExpr(initId, enclosingStmt)
2673
- it.elements.forEachIndexed { i, arg -> extractVarargElement(arg, callable, initId, i, enclosingStmt) }
2674
-
2675
- extractConstantInteger(it.elements.size, locId, id, 0 , callable, enclosingStmt)
2676
- }
2677
- }
2642
+ extractArrayCreation(arg, c.type, elementType, c, parent, idx, callable, enclosingStmt)
2678
2643
}
2679
2644
isBuiltinCall(c, " <get-java>" , " kotlin.jvm" ) -> {
2680
2645
// Special case for KClass<*>.java, which is used in the Parcelize plugin. In normal cases, this is already rewritten to the property referenced below:
@@ -2694,7 +2659,7 @@ open class KotlinFileExtractor(
2694
2659
val argType = (ext.type as ? IrSimpleType )?.arguments?.firstOrNull()?.typeOrNull
2695
2660
val typeArguments = if (argType == null ) listOf () else listOf (argType)
2696
2661
2697
- extractRawMethodAccess(getter, c, callable, parent, idx, enclosingStmt, listOf (), null , ext, typeArguments)
2662
+ extractRawMethodAccess(getter, c, c.type, callable, parent, idx, enclosingStmt, listOf (), null , ext, typeArguments)
2698
2663
}
2699
2664
}
2700
2665
isFunction(target, " kotlin" , " (some array type)" , { isArrayType(it) }, " iterator" ) -> {
@@ -2725,7 +2690,7 @@ open class KotlinFileExtractor(
2725
2690
else -> pluginContext.irBuiltIns.anyNType
2726
2691
}
2727
2692
}
2728
- extractRawMethodAccess(iteratorFn, c, callable, parent, idx, enclosingStmt, listOf (c.dispatchReceiver), null , null , typeArgs)
2693
+ extractRawMethodAccess(iteratorFn, c, c.type, callable, parent, idx, enclosingStmt, listOf (c.dispatchReceiver), null , null , typeArgs)
2729
2694
}
2730
2695
}
2731
2696
}
@@ -2814,6 +2779,7 @@ open class KotlinFileExtractor(
2814
2779
extractRawMethodAccess(
2815
2780
realCallee,
2816
2781
c,
2782
+ c.type,
2817
2783
callable,
2818
2784
parent,
2819
2785
idx,
@@ -2841,6 +2807,7 @@ open class KotlinFileExtractor(
2841
2807
extractRawMethodAccess(
2842
2808
realCallee,
2843
2809
c,
2810
+ c.type,
2844
2811
callable,
2845
2812
parent,
2846
2813
idx,
@@ -2858,6 +2825,50 @@ open class KotlinFileExtractor(
2858
2825
}
2859
2826
}
2860
2827
2828
+ private fun extractArrayCreation (elementList : IrVararg ? , resultType : IrType , elementType : IrType ? , locElement : IrElement , parent : Label <out DbExprparent >, idx : Int , enclosingCallable : Label <out DbCallable >, enclosingStmt : Label <out DbStmt >) {
2829
+ // If this is [someType]ArrayOf(*x), x, otherwise null
2830
+ val clonedArray = elementList?.let {
2831
+ if (it.elements.size == 1 ) {
2832
+ val onlyElement = it.elements[0 ]
2833
+ if (onlyElement is IrSpreadElement )
2834
+ onlyElement.expression
2835
+ else null
2836
+ } else null
2837
+ }
2838
+
2839
+ if (clonedArray != null ) {
2840
+ // This is an array clone: extract is as a call to java.lang.Object.clone
2841
+ objectCloneMethod?.let {
2842
+ extractRawMethodAccess(it, locElement, resultType, enclosingCallable, parent, idx, enclosingStmt, listOf (), clonedArray, null )
2843
+ }
2844
+ } else {
2845
+ // This is array creation: extract it as a call to new ArrayType[] { ... }
2846
+ val id = tw.getFreshIdLabel<DbArraycreationexpr >()
2847
+ val type = useType(resultType)
2848
+ tw.writeExprs_arraycreationexpr(id, type.javaResult.id, parent, idx)
2849
+ tw.writeExprsKotlinType(id, type.kotlinResult.id)
2850
+ val locId = tw.getLocation(locElement)
2851
+ tw.writeHasLocation(id, locId)
2852
+ tw.writeCallableEnclosingExpr(id, enclosingCallable)
2853
+
2854
+ if (elementType != null ) {
2855
+ extractTypeAccessRecursive(elementType, locId, id, - 1 , enclosingCallable, enclosingStmt, TypeContext .GENERIC_ARGUMENT )
2856
+ }
2857
+
2858
+ if (elementList != null ) {
2859
+ val initId = tw.getFreshIdLabel<DbArrayinit >()
2860
+ tw.writeExprs_arrayinit(initId, type.javaResult.id, id, - 2 )
2861
+ tw.writeExprsKotlinType(initId, type.kotlinResult.id)
2862
+ tw.writeHasLocation(initId, locId)
2863
+ tw.writeCallableEnclosingExpr(initId, enclosingCallable)
2864
+ tw.writeStatementEnclosingExpr(initId, enclosingStmt)
2865
+ elementList.elements.forEachIndexed { i, arg -> extractVarargElement(arg, enclosingCallable, initId, i, enclosingStmt) }
2866
+
2867
+ extractConstantInteger(elementList.elements.size, locId, id, 0 , enclosingCallable, enclosingStmt)
2868
+ }
2869
+ }
2870
+ }
2871
+
2861
2872
private fun extractNewExpr (
2862
2873
methodId : Label <out DbConstructor >,
2863
2874
constructedType : TypeResults ,
@@ -3641,14 +3652,12 @@ open class KotlinFileExtractor(
3641
3652
extractTypeOperatorCall(e, callable, exprParent.parent, exprParent.idx, exprParent.enclosingStmt)
3642
3653
}
3643
3654
is IrVararg -> {
3644
- var spread = e.elements.getOrNull(0 ) as ? IrSpreadElement
3645
- if (spread == null || e.elements.size != 1 ) {
3646
- logger.errorElement(" Unexpected IrVararg" , e)
3647
- return
3648
- }
3649
3655
// There are lowered IR cases when the vararg expression is not within a call, such as
3650
- // val temp0 = [*expr]
3651
- extractExpression(spread.expression, callable, parent)
3656
+ // val temp0 = [*expr].
3657
+ // This AST element can also occur as a collection literal in an annotation class, such as
3658
+ // annotation class Ann(val strings: Array<String> = [])
3659
+ val exprParent = parent.expr(e, callable)
3660
+ extractArrayCreation(e, e.type, e.varargElementType, e, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
3652
3661
}
3653
3662
is IrGetObjectValue -> {
3654
3663
// For `object MyObject { ... }`, the .class has an
0 commit comments