@@ -1753,7 +1753,7 @@ open class KotlinFileExtractor(
1753
1753
private fun extractsDefaultsCall (
1754
1754
syntacticCallTarget : IrFunction ,
1755
1755
locId : Label <DbLocation >,
1756
- callsite : IrCall ,
1756
+ resultType : IrType ,
1757
1757
enclosingCallable : Label <out DbCallable >,
1758
1758
callsiteParent : Label <out DbExprparent >,
1759
1759
childIdx : Int ,
@@ -1768,7 +1768,7 @@ open class KotlinFileExtractor(
1768
1768
useFunction<DbCallable >(callTarget)
1769
1769
}
1770
1770
val defaultMethodLabel = getDefaultsMethodLabel(callTarget)
1771
- val id = extractMethodAccessWithoutArgs(callsite.type , locId, enclosingCallable, callsiteParent, childIdx, enclosingStmt, defaultMethodLabel)
1771
+ val id = extractMethodAccessWithoutArgs(resultType , locId, enclosingCallable, callsiteParent, childIdx, enclosingStmt, defaultMethodLabel)
1772
1772
1773
1773
if (callTarget.isLocalFunction()) {
1774
1774
extractTypeAccess(getLocallyVisibleFunctionLabels(callTarget).type, locId, id, - 1 , enclosingCallable, enclosingStmt)
@@ -1871,7 +1871,8 @@ open class KotlinFileExtractor(
1871
1871
1872
1872
fun extractRawMethodAccess (
1873
1873
syntacticCallTarget : IrFunction ,
1874
- callsite : IrCall ,
1874
+ locElement : IrElement ,
1875
+ resultType : IrType ,
1875
1876
enclosingCallable : Label <out DbCallable >,
1876
1877
callsiteParent : Label <out DbExprparent >,
1877
1878
childIdx : Int ,
@@ -1883,13 +1884,13 @@ open class KotlinFileExtractor(
1883
1884
extractClassTypeArguments : Boolean = false,
1884
1885
superQualifierSymbol : IrClassSymbol ? = null) {
1885
1886
1886
- val locId = tw.getLocation(callsite )
1887
+ val locId = tw.getLocation(locElement )
1887
1888
1888
1889
if (valueArguments.any { it == null }) {
1889
1890
extractsDefaultsCall(
1890
1891
syntacticCallTarget,
1891
1892
locId,
1892
- callsite ,
1893
+ resultType ,
1893
1894
enclosingCallable,
1894
1895
callsiteParent,
1895
1896
childIdx,
@@ -1902,7 +1903,7 @@ open class KotlinFileExtractor(
1902
1903
extractRawMethodAccess(
1903
1904
syntacticCallTarget,
1904
1905
locId,
1905
- callsite.type ,
1906
+ resultType ,
1906
1907
enclosingCallable,
1907
1908
callsiteParent,
1908
1909
childIdx,
@@ -2233,7 +2234,7 @@ open class KotlinFileExtractor(
2233
2234
return
2234
2235
}
2235
2236
2236
- extractRawMethodAccess(syntacticCallTarget, c, callable, parent, idx, enclosingStmt, (0 until c.valueArgumentsCount).map { c.getValueArgument(it) }, c.dispatchReceiver, c.extensionReceiver, typeArgs, extractClassTypeArguments, c.superQualifierSymbol)
2237
+ 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)
2237
2238
}
2238
2239
2239
2240
fun extractSpecialEnumFunction (fnName : String ){
@@ -2337,7 +2338,7 @@ open class KotlinFileExtractor(
2337
2338
}
2338
2339
isFunction(target, " kotlin" , " String" , " plus" , true ) -> {
2339
2340
findJdkIntrinsicOrWarn(" stringPlus" , c)?.let { stringPlusFn ->
2340
- extractRawMethodAccess(stringPlusFn, c, callable, parent, idx, enclosingStmt, listOf (c.extensionReceiver, c.getValueArgument(0 )), null , null )
2341
+ extractRawMethodAccess(stringPlusFn, c, c.type, callable, parent, idx, enclosingStmt, listOf (c.extensionReceiver, c.getValueArgument(0 )), null , null )
2341
2342
}
2342
2343
}
2343
2344
isNumericFunction(target, listOf (" plus" , " minus" , " times" , " div" , " rem" , " and" , " or" , " xor" , " shl" , " shr" , " ushr" )) -> {
@@ -2582,7 +2583,7 @@ open class KotlinFileExtractor(
2582
2583
}
2583
2584
isFunction(target, " kotlin" , " Any" , " toString" , true ) -> {
2584
2585
stringValueOfObjectMethod?.let {
2585
- extractRawMethodAccess(it, c, callable, parent, idx, enclosingStmt, listOf (c.extensionReceiver), null , null )
2586
+ extractRawMethodAccess(it, c, c.type, callable, parent, idx, enclosingStmt, listOf (c.extensionReceiver), null , null )
2586
2587
}
2587
2588
}
2588
2589
isBuiltinCallKotlin(c, " enumValues" ) -> {
@@ -2632,69 +2633,35 @@ open class KotlinFileExtractor(
2632
2633
|| isBuiltinCallKotlin(c, " byteArrayOf" )
2633
2634
|| isBuiltinCallKotlin(c, " booleanArrayOf" ) -> {
2634
2635
2635
- val arg = if (c.valueArgumentsCount == 1 ) c.getValueArgument(0 ) else {
2636
- logger.errorElement(" Expected to find only one (vararg) argument in ${c.symbol.owner.name.asString()} call" , c)
2637
- null
2638
- }?.let {
2639
- if (it is IrVararg ) it else {
2640
- logger.errorElement(" Expected to find vararg argument in ${c.symbol.owner.name.asString()} call" , c)
2641
- null
2642
- }
2643
- }
2644
2636
2645
- // If this is [someType]ArrayOf(*x), x, otherwise null
2646
- val clonedArray = arg?.let {
2647
- if (arg.elements.size == 1 ) {
2648
- val onlyElement = arg.elements[0 ]
2649
- if (onlyElement is IrSpreadElement )
2650
- onlyElement.expression
2651
- else null
2652
- } else null
2653
- }
2654
-
2655
- if (clonedArray != null ) {
2656
- // This is an array clone: extract is as a call to java.lang.Object.clone
2657
- objectCloneMethod?.let {
2658
- extractRawMethodAccess(it, c, callable, parent, idx, enclosingStmt, listOf (), clonedArray, null )
2659
- }
2637
+ val isPrimitiveArrayCreation = ! isBuiltinCallKotlin(c, " arrayOf" )
2638
+ val elementType = if (isPrimitiveArrayCreation) {
2639
+ c.type.getArrayElementType(pluginContext.irBuiltIns)
2660
2640
} else {
2661
- // This is array creation: extract it as a call to new ArrayType[] { ... }
2662
- val id = tw.getFreshIdLabel<DbArraycreationexpr >()
2663
- val type = useType(c.type)
2664
- tw.writeExprs_arraycreationexpr(id, type.javaResult.id, parent, idx)
2665
- tw.writeExprsKotlinType(id, type.kotlinResult.id)
2666
- val locId = tw.getLocation(c)
2667
- tw.writeHasLocation(id, locId)
2668
- tw.writeCallableEnclosingExpr(id, callable)
2669
-
2670
- if (isBuiltinCallKotlin(c, " arrayOf" )) {
2671
- if (c.typeArgumentsCount == 1 ) {
2672
- val typeArgument = c.getTypeArgument(0 )
2673
- if (typeArgument == null ) {
2641
+ // TODO: is there any reason not to always use getArrayElementType?
2642
+ if (c.typeArgumentsCount == 1 ) {
2643
+ c.getTypeArgument(0 ).also {
2644
+ if (it == null ) {
2674
2645
logger.errorElement(" Type argument missing in an arrayOf call" , c)
2675
- } else {
2676
- extractTypeAccessRecursive(typeArgument, locId, id, - 1 , callable, enclosingStmt, TypeContext .GENERIC_ARGUMENT )
2677
2646
}
2678
- } else {
2679
- logger.errorElement(" Expected to find one type argument in arrayOf call" , c )
2680
2647
}
2681
2648
} else {
2682
- val elementType = c. type.getArrayElementType(pluginContext.irBuiltIns )
2683
- extractTypeAccessRecursive(elementType, locId, id, - 1 , callable, enclosingStmt)
2649
+ logger.errorElement( " Expected to find one type argument in arrayOf call " , c )
2650
+ null
2684
2651
}
2652
+ }
2685
2653
2686
- arg?.let {
2687
- val initId = tw.getFreshIdLabel<DbArrayinit >()
2688
- tw.writeExprs_arrayinit(initId, type.javaResult.id, id, - 2 )
2689
- tw.writeExprsKotlinType(initId, type.kotlinResult.id)
2690
- tw.writeHasLocation(initId, locId)
2691
- tw.writeCallableEnclosingExpr(initId, callable)
2692
- tw.writeStatementEnclosingExpr(initId, enclosingStmt)
2693
- it.elements.forEachIndexed { i, arg -> extractVarargElement(arg, callable, initId, i, enclosingStmt) }
2694
-
2695
- extractConstantInteger(it.elements.size, locId, id, 0 , callable, enclosingStmt)
2654
+ val arg = if (c.valueArgumentsCount == 1 ) c.getValueArgument(0 ) else {
2655
+ logger.errorElement(" Expected to find only one (vararg) argument in ${c.symbol.owner.name.asString()} call" , c)
2656
+ null
2657
+ }?.let {
2658
+ if (it is IrVararg ) it else {
2659
+ logger.errorElement(" Expected to find vararg argument in ${c.symbol.owner.name.asString()} call" , c)
2660
+ null
2696
2661
}
2697
2662
}
2663
+
2664
+ extractArrayCreation(arg, c.type, elementType, isPrimitiveArrayCreation, c, parent, idx, callable, enclosingStmt)
2698
2665
}
2699
2666
isBuiltinCall(c, " <get-java>" , " kotlin.jvm" ) -> {
2700
2667
// Special case for KClass<*>.java, which is used in the Parcelize plugin. In normal cases, this is already rewritten to the property referenced below:
@@ -2714,7 +2681,7 @@ open class KotlinFileExtractor(
2714
2681
val argType = (ext.type as ? IrSimpleType )?.arguments?.firstOrNull()?.typeOrNull
2715
2682
val typeArguments = if (argType == null ) listOf () else listOf (argType)
2716
2683
2717
- extractRawMethodAccess(getter, c, callable, parent, idx, enclosingStmt, listOf (), null , ext, typeArguments)
2684
+ extractRawMethodAccess(getter, c, c.type, callable, parent, idx, enclosingStmt, listOf (), null , ext, typeArguments)
2718
2685
}
2719
2686
}
2720
2687
isFunction(target, " kotlin" , " (some array type)" , { isArrayType(it) }, " iterator" ) -> {
@@ -2745,7 +2712,7 @@ open class KotlinFileExtractor(
2745
2712
else -> pluginContext.irBuiltIns.anyNType
2746
2713
}
2747
2714
}
2748
- extractRawMethodAccess(iteratorFn, c, callable, parent, idx, enclosingStmt, listOf (c.dispatchReceiver), null , null , typeArgs)
2715
+ extractRawMethodAccess(iteratorFn, c, c.type, callable, parent, idx, enclosingStmt, listOf (c.dispatchReceiver), null , null , typeArgs)
2749
2716
}
2750
2717
}
2751
2718
}
@@ -2834,6 +2801,7 @@ open class KotlinFileExtractor(
2834
2801
extractRawMethodAccess(
2835
2802
realCallee,
2836
2803
c,
2804
+ c.type,
2837
2805
callable,
2838
2806
parent,
2839
2807
idx,
@@ -2861,6 +2829,7 @@ open class KotlinFileExtractor(
2861
2829
extractRawMethodAccess(
2862
2830
realCallee,
2863
2831
c,
2832
+ c.type,
2864
2833
callable,
2865
2834
parent,
2866
2835
idx,
@@ -2878,6 +2847,51 @@ open class KotlinFileExtractor(
2878
2847
}
2879
2848
}
2880
2849
2850
+ private fun extractArrayCreation (elementList : IrVararg ? , resultType : IrType , elementType : IrType ? , allowPrimitiveElementType : Boolean , locElement : IrElement , parent : Label <out DbExprparent >, idx : Int , enclosingCallable : Label <out DbCallable >, enclosingStmt : Label <out DbStmt >) {
2851
+ // If this is [someType]ArrayOf(*x), x, otherwise null
2852
+ val clonedArray = elementList?.let {
2853
+ if (it.elements.size == 1 ) {
2854
+ val onlyElement = it.elements[0 ]
2855
+ if (onlyElement is IrSpreadElement )
2856
+ onlyElement.expression
2857
+ else null
2858
+ } else null
2859
+ }
2860
+
2861
+ if (clonedArray != null ) {
2862
+ // This is an array clone: extract is as a call to java.lang.Object.clone
2863
+ objectCloneMethod?.let {
2864
+ extractRawMethodAccess(it, locElement, resultType, enclosingCallable, parent, idx, enclosingStmt, listOf (), clonedArray, null )
2865
+ }
2866
+ } else {
2867
+ // This is array creation: extract it as a call to new ArrayType[] { ... }
2868
+ val id = tw.getFreshIdLabel<DbArraycreationexpr >()
2869
+ val type = useType(resultType)
2870
+ tw.writeExprs_arraycreationexpr(id, type.javaResult.id, parent, idx)
2871
+ tw.writeExprsKotlinType(id, type.kotlinResult.id)
2872
+ val locId = tw.getLocation(locElement)
2873
+ tw.writeHasLocation(id, locId)
2874
+ tw.writeCallableEnclosingExpr(id, enclosingCallable)
2875
+
2876
+ if (elementType != null ) {
2877
+ val typeContext = if (allowPrimitiveElementType) TypeContext .OTHER else TypeContext .GENERIC_ARGUMENT
2878
+ extractTypeAccessRecursive(elementType, locId, id, - 1 , enclosingCallable, enclosingStmt, typeContext)
2879
+ }
2880
+
2881
+ if (elementList != null ) {
2882
+ val initId = tw.getFreshIdLabel<DbArrayinit >()
2883
+ tw.writeExprs_arrayinit(initId, type.javaResult.id, id, - 2 )
2884
+ tw.writeExprsKotlinType(initId, type.kotlinResult.id)
2885
+ tw.writeHasLocation(initId, locId)
2886
+ tw.writeCallableEnclosingExpr(initId, enclosingCallable)
2887
+ tw.writeStatementEnclosingExpr(initId, enclosingStmt)
2888
+ elementList.elements.forEachIndexed { i, arg -> extractVarargElement(arg, enclosingCallable, initId, i, enclosingStmt) }
2889
+
2890
+ extractConstantInteger(elementList.elements.size, locId, id, 0 , enclosingCallable, enclosingStmt)
2891
+ }
2892
+ }
2893
+ }
2894
+
2881
2895
private fun extractNewExpr (
2882
2896
methodId : Label <out DbConstructor >,
2883
2897
constructedType : TypeResults ,
@@ -3661,14 +3675,12 @@ open class KotlinFileExtractor(
3661
3675
extractTypeOperatorCall(e, callable, exprParent.parent, exprParent.idx, exprParent.enclosingStmt)
3662
3676
}
3663
3677
is IrVararg -> {
3664
- var spread = e.elements.getOrNull(0 ) as ? IrSpreadElement
3665
- if (spread == null || e.elements.size != 1 ) {
3666
- logger.errorElement(" Unexpected IrVararg" , e)
3667
- return
3668
- }
3669
3678
// There are lowered IR cases when the vararg expression is not within a call, such as
3670
- // val temp0 = [*expr]
3671
- extractExpression(spread.expression, callable, parent)
3679
+ // val temp0 = [*expr].
3680
+ // This AST element can also occur as a collection literal in an annotation class, such as
3681
+ // annotation class Ann(val strings: Array<String> = [])
3682
+ val exprParent = parent.expr(e, callable)
3683
+ extractArrayCreation(e, e.type, e.varargElementType, true , e, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
3672
3684
}
3673
3685
is IrGetObjectValue -> {
3674
3686
// For `object MyObject { ... }`, the .class has an
0 commit comments