Propagate KotlinType through call generator, fix boxing on arguments

This commit is contained in:
Mikhail Zarechenskiy
2018-02-09 06:42:13 +03:00
parent 22b9c25dd0
commit 80dd5c106e
17 changed files with 182 additions and 104 deletions
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.codegen;
@@ -28,7 +17,7 @@ import org.jetbrains.org.objectweb.asm.Type;
import java.util.List;
import static org.jetbrains.kotlin.codegen.StackValue.*;
import static org.jetbrains.kotlin.codegen.StackValue.createDefaultValue;
public class CallBasedArgumentGenerator extends ArgumentGenerator {
private final ExpressionCodegen codegen;
@@ -64,7 +53,12 @@ public class CallBasedArgumentGenerator extends ArgumentGenerator {
@Override
protected void generateDefault(int i, @NotNull DefaultValueArgument argument) {
callGenerator.putValueIfNeeded(valueParameterTypes.get(i), createDefaultValue(valueParameterTypes.get(i)), ValueKind.DEFAULT_PARAMETER, i);
callGenerator.putValueIfNeeded(
getJvmKotlinType(valueParameterTypes, valueParameters, i),
createDefaultValue(valueParameterTypes.get(i)),
ValueKind.DEFAULT_PARAMETER,
i
);
}
@Override
@@ -73,7 +67,7 @@ public class CallBasedArgumentGenerator extends ArgumentGenerator {
// Upper bound for type of vararg parameter should always have a form of 'Array<out T>',
// while its lower bound may be Nothing-typed after approximation
StackValue lazyVararg = codegen.genVarargs(argument, FlexibleTypesKt.upperIfFlexible(parameter.getType()));
callGenerator.putValueIfNeeded(valueParameterTypes.get(i), lazyVararg, ValueKind.GENERAL_VARARG, i);
callGenerator.putValueIfNeeded(getJvmKotlinType(valueParameterTypes, valueParameters, i), lazyVararg, ValueKind.GENERAL_VARARG, i);
}
@Override
@@ -84,11 +78,19 @@ public class CallBasedArgumentGenerator extends ArgumentGenerator {
codegen.typeMapper
);
callGenerator.putValueIfNeeded(valueParameterTypes.get(i), argumentValue);
callGenerator.putValueIfNeeded(getJvmKotlinType(valueParameterTypes, valueParameters, i), argumentValue);
}
@Override
protected void reorderArgumentsIfNeeded(@NotNull List<ArgumentAndDeclIndex> actualArgsWithDeclIndex) {
callGenerator.reorderArgumentsIfNeeded(actualArgsWithDeclIndex, valueParameterTypes);
}
@NotNull
private static JvmKotlinType getJvmKotlinType(
@NotNull List<Type> valueParameterTypes,
@NotNull List<ValueParameterDescriptor> valueParameters, int i
) {
return new JvmKotlinType(valueParameterTypes.get(i), valueParameters.get(i).getOriginal().getType());
}
}
@@ -59,8 +59,8 @@ interface CallGenerator {
stackValue.put(stackValue.type, codegen.v)
}
override fun putValueIfNeeded(parameterType: Type, value: StackValue, kind: ValueKind, parameterIndex: Int) {
value.put(value.type, codegen.v)
override fun putValueIfNeeded(parameterType: JvmKotlinType, value: StackValue, kind: ValueKind, parameterIndex: Int) {
value.put(value.type, value.kotlinType, codegen.v)
}
override fun reorderArgumentsIfNeeded(actualArgsWithDeclIndex: List<ArgumentAndDeclIndex>, valueParameterTypes: List<Type>) {
@@ -104,13 +104,13 @@ interface CallGenerator {
parameterIndex: Int)
fun putValueIfNeeded(
parameterType: Type,
parameterType: JvmKotlinType,
value: StackValue) {
putValueIfNeeded(parameterType, value, ValueKind.GENERAL)
}
fun putValueIfNeeded(
parameterType: Type,
parameterType: JvmKotlinType,
value: StackValue,
kind: ValueKind = ValueKind.GENERAL,
parameterIndex: Int = -1)
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.codegen
@@ -55,11 +44,15 @@ class DefaultCallArgs(val size: Int) {
val toInts = toInts()
if (!toInts.isEmpty()) {
for (mask in toInts) {
callGenerator.putValueIfNeeded(Type.INT_TYPE, StackValue.constant(mask, Type.INT_TYPE), ValueKind.DEFAULT_MASK)
callGenerator.putValueIfNeeded(
JvmKotlinType(Type.INT_TYPE), StackValue.constant(mask, Type.INT_TYPE), ValueKind.DEFAULT_MASK
)
}
val parameterType = if (isConstructor) AsmTypes.DEFAULT_CONSTRUCTOR_MARKER else AsmTypes.OBJECT_TYPE
callGenerator.putValueIfNeeded(parameterType, StackValue.constant(null, parameterType), ValueKind.METHOD_HANDLE_IN_DEFAULT)
callGenerator.putValueIfNeeded(
JvmKotlinType(parameterType), StackValue.constant(null, parameterType), ValueKind.METHOD_HANDLE_IN_DEFAULT
)
}
return toInts.isNotEmpty()
}
@@ -2342,7 +2342,9 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
List<JvmMethodParameterSignature> callableParameters = ((CallableMethod) callableMethod).getValueParameters();
for (JvmMethodParameterSignature parameter: callableParameters) {
if (parameter.getKind() == JvmMethodParameterKind.CONSTRUCTOR_MARKER) {
callGenerator.putValueIfNeeded(parameter.getAsmType(), StackValue.constant(null, parameter.getAsmType()));
callGenerator.putValueIfNeeded(
new JvmKotlinType(parameter.getAsmType(), null), StackValue.constant(null, parameter.getAsmType())
);
}
}
}
@@ -2584,10 +2586,13 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
@NotNull
private StackValue generateExtensionReceiver(@NotNull CallableDescriptor descriptor) {
if (myFrameMap.getIndex(descriptor.getExtensionReceiverParameter()) != -1) {
ReceiverParameterDescriptor parameter = descriptor.getExtensionReceiverParameter();
if (myFrameMap.getIndex(parameter) != -1) {
KotlinType type = parameter.getReturnType();
return StackValue.local(
myFrameMap.getIndex(descriptor.getExtensionReceiverParameter()),
typeMapper.mapType(descriptor.getExtensionReceiverParameter())
myFrameMap.getIndex(parameter),
typeMapper.mapType(type),
type
);
}
@@ -1192,7 +1192,7 @@ public class FunctionCodegen {
ValueParameterDescriptor parameterDescriptor = valueParameters.get(index);
Type type = mappedParameters.get(capturedArgumentsCount + index).getAsmType();
int parameterIndex = frameMap.getIndex(parameterDescriptor);
generator.putValueIfNeeded(type, StackValue.local(parameterIndex, type));
generator.putValueIfNeeded(new JvmKotlinType(type, null), StackValue.local(parameterIndex, type));
}
CallableMethod method = state.getTypeMapper().mapToCallableMethod(functionDescriptor, false);
@@ -1264,14 +1264,14 @@ public class FunctionCodegen {
) {
int var = 0;
if (!isStatic) {
callGenerator.putValueIfNeeded(ownerType, StackValue.local(var, ownerType));
callGenerator.putValueIfNeeded(new JvmKotlinType(ownerType, null), StackValue.local(var, ownerType));
var += ownerType.getSize();
}
for (JvmMethodParameterSignature parameterSignature : signature.getValueParameters()) {
if (parameterSignature.getKind() != JvmMethodParameterKind.VALUE) {
Type type = parameterSignature.getAsmType();
callGenerator.putValueIfNeeded(type, StackValue.local(var, type));
callGenerator.putValueIfNeeded(new JvmKotlinType(type, null), StackValue.local(var, type));
var += type.getSize();
}
}
@@ -1294,7 +1294,10 @@ public abstract class StackValue {
coerce(topOfStackType, topOfStackKotlinType, lastParameterType, lastParameterKotlinType, v);
getCallGenerator().putValueIfNeeded(lastParameterType, StackValue.onStack(lastParameterType));
getCallGenerator().putValueIfNeeded(
new JvmKotlinType(lastParameterType, lastParameterKotlinType),
StackValue.onStack(lastParameterType, lastParameterKotlinType)
);
//Convention setter couldn't have default parameters, just getter can have it at last positions
//We should remove default parameters of getter from stack*/
@@ -1481,7 +1484,7 @@ public abstract class StackValue {
putReceiver(v, false);
}
callGenerator.processAndPutHiddenParameters(true);
callGenerator.putValueIfNeeded(rightSide.type, rightSide);
callGenerator.putValueIfNeeded(new JvmKotlinType(rightSide.type, rightSide.kotlinType), rightSide);
callGenerator.putHiddenParamsIntoLocals();
callGenerator.genCall(setter, resolvedCall, false, codegen);
}
@@ -270,7 +270,7 @@ private fun CallableDescriptor.isJvmStaticIn(predicate: (DeclarationDescriptor)
fun Collection<VariableDescriptor>.filterOutDescriptorsWithSpecialNames() = filterNot { it.name.isSpecial }
class JvmKotlinType(val type: Type, val kotlinType: KotlinType?)
class JvmKotlinType(val type: Type, val kotlinType: KotlinType? = null)
fun KotlinType.asmType(typeMapper: KotlinTypeMapper) = typeMapper.mapType(this)
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.codegen.inline
@@ -52,7 +41,6 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.expressions.DoubleColonLHS
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils.isFunctionLiteral
import org.jetbrains.kotlin.types.expressions.LabelResolver
import org.jetbrains.org.objectweb.asm.Opcodes
@@ -326,20 +314,21 @@ abstract class InlineCodegen<out T: BaseExpressionCodegen>(
}
protected fun putArgumentOrCapturedToLocalVal(
type: Type,
stackValue: StackValue,
capturedParamIndex: Int,
parameterIndex: Int,
kind: ValueKind
jvmKotlinType: JvmKotlinType,
stackValue: StackValue,
capturedParamIndex: Int,
parameterIndex: Int,
kind: ValueKind
) {
val isDefaultParameter = kind === ValueKind.DEFAULT_PARAMETER
if (!isDefaultParameter && shouldPutGeneralValue(type, stackValue)) {
stackValue.put(type, codegen.v)
val jvmType = jvmKotlinType.type
if (!isDefaultParameter && shouldPutGeneralValue(jvmType, stackValue)) {
stackValue.put(jvmType, jvmKotlinType.kotlinType, codegen.v)
}
if (!asFunctionInline && Type.VOID_TYPE !== type) {
if (!asFunctionInline && Type.VOID_TYPE !== jvmType) {
//TODO remap only inlinable closure => otherwise we could get a lot of problem
val couldBeRemapped = !shouldPutGeneralValue(type, stackValue) && kind !== ValueKind.DEFAULT_PARAMETER
val couldBeRemapped = !shouldPutGeneralValue(jvmType, stackValue) && kind !== ValueKind.DEFAULT_PARAMETER
val remappedValue = if (couldBeRemapped) stackValue else null
val info: ParameterInfo
@@ -349,7 +338,7 @@ abstract class InlineCodegen<out T: BaseExpressionCodegen>(
info.setRemapValue(remappedValue)
}
else {
info = invocationParamBuilder.addNextValueParameter(type, false, remappedValue, parameterIndex)
info = invocationParamBuilder.addNextValueParameter(jvmType, false, remappedValue, parameterIndex)
}
recordParameterValueInLocalVal(
@@ -413,7 +402,7 @@ abstract class InlineCodegen<out T: BaseExpressionCodegen>(
protected fun rememberCapturedForDefaultLambda(defaultLambda: DefaultLambda) {
for ((paramIndex, captured) in defaultLambda.capturedVars.withIndex()) {
putArgumentOrCapturedToLocalVal(
captured.type,
JvmKotlinType(captured.type),
//HACK: actually parameter would be placed on stack in default function
// also see ValueKind.DEFAULT_LAMBDA_CAPTURED_PARAMETER check
StackValue.onStack(captured.type),
@@ -736,7 +725,12 @@ class PsiInlineCodegen(
}
else {
val value = codegen.gen(argumentExpression)
putValueIfNeeded(parameterType, value, ValueKind.GENERAL, valueParameterDescriptor.index)
putValueIfNeeded(
JvmKotlinType(parameterType, valueParameterDescriptor.original.type),
value,
ValueKind.GENERAL,
valueParameterDescriptor.index
)
}
}
@@ -753,7 +747,7 @@ class PsiInlineCodegen(
}
}
override fun putValueIfNeeded(parameterType: Type, value: StackValue, kind: ValueKind, parameterIndex: Int) {
override fun putValueIfNeeded(parameterType: JvmKotlinType, value: StackValue, kind: ValueKind, parameterIndex: Int) {
if (processDefaultMaskOrMethodHandler(value, kind)) return
assert(maskValues.isEmpty()) { "Additional default call arguments should be last ones, but " + value }
@@ -762,7 +756,9 @@ class PsiInlineCodegen(
}
override fun putCapturedValueOnStack(stackValue: StackValue, valueType: Type, paramIndex: Int) {
putArgumentOrCapturedToLocalVal(stackValue.type, stackValue, paramIndex, paramIndex, ValueKind.CAPTURED)
putArgumentOrCapturedToLocalVal(
JvmKotlinType(stackValue.type, stackValue.kotlinType), stackValue, paramIndex, paramIndex, ValueKind.CAPTURED
)
}
override fun reorderArgumentsIfNeeded(actualArgsWithDeclIndex: List<ArgumentAndDeclIndex>, valueParameterTypes: List<Type>) = Unit
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.codegen.inline
@@ -92,7 +81,7 @@ class InlineCodegenForDefaultBody(
throw UnsupportedOperationException("Shouldn't be called")
}
override fun putValueIfNeeded(parameterType: Type, value: StackValue, kind: ValueKind, parameterIndex: Int) {
override fun putValueIfNeeded(parameterType: JvmKotlinType, value: StackValue, kind: ValueKind, parameterIndex: Int) {
//original method would be inlined directly into default impl body without any inline magic
//so we no need to load variables on stack to further method call
}