diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 1f1179d4a42..c448d77a1a3 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -269,7 +269,8 @@ public class ExpressionCodegen extends KtVisitor impleme @NotNull ClassDescriptor required ) { if (!isJvmInterface(provided) && isJvmInterface(required)) { - return StackValue.coercion(inner, asmType(required.getDefaultType())); + SimpleType requiredDefaultType = required.getDefaultType(); + return StackValue.coercion(inner, asmType(requiredDefaultType), requiredDefaultType); } return inner; @@ -359,7 +360,7 @@ public class ExpressionCodegen extends KtVisitor impleme public StackValue genLazy(KtElement expr, Type type) { StackValue value = gen(expr); - return StackValue.coercion(value, type); + return StackValue.coercion(value, type, null); } private StackValue genStatement(KtElement statement) { @@ -463,6 +464,7 @@ public class ExpressionCodegen extends KtVisitor impleme /* package */ StackValue generateIfExpression(@NotNull KtIfExpression expression, boolean isStatement) { Type asmType = isStatement ? Type.VOID_TYPE : expressionType(expression); + KotlinType kotlinType = isStatement ? null : kotlinType(expression); StackValue condition = gen(expression.getCondition()); KtExpression thenExpression = expression.getThen(); @@ -470,7 +472,7 @@ public class ExpressionCodegen extends KtVisitor impleme if (isEmptyExpression(thenExpression)) { if (isEmptyExpression(elseExpression)) { - return StackValue.coercion(condition, asmType); + return StackValue.coercion(condition, asmType, kotlinType); } return generateSingleBranchIf(condition, expression, elseExpression, false, isStatement); } @@ -2861,7 +2863,8 @@ public class ExpressionCodegen extends KtVisitor impleme ReceiverValue receiver = getBoundCallableReferenceReceiver(resolvedCall); if (receiver == null) return null; - return StackValue.coercion(generateReceiverValue(receiver, false), asmType(receiver.getType())); + KotlinType receiverType = receiver.getType(); + return StackValue.coercion(generateReceiverValue(receiver, false), asmType(receiverType), receiverType); } @NotNull @@ -2930,9 +2933,10 @@ public class ExpressionCodegen extends KtVisitor impleme expression = deparenthesized; Type type = expressionType(expression); + KotlinType kotlinType = kotlinType(expression); if (expression instanceof KtSafeQualifiedExpression && !isPrimitive(type)) { - return StackValue.coercion(generateSafeQualifiedExpression((KtSafeQualifiedExpression) expression, ifnull), type); + return StackValue.coercion(generateSafeQualifiedExpression((KtSafeQualifiedExpression) expression, ifnull), type, kotlinType); } else { return genLazy(expression, type); @@ -2958,9 +2962,10 @@ public class ExpressionCodegen extends KtVisitor impleme public StackValue visitSafeQualifiedExpression(@NotNull KtSafeQualifiedExpression expression, StackValue unused) { Label ifnull = new Label(); Type type = boxType(expressionType(expression)); + KotlinType kotlinType = kotlinType(expression); StackValue value = generateSafeQualifiedExpression(expression, ifnull); - StackValue newReceiver = StackValue.coercion(value, type); + StackValue newReceiver = StackValue.coercion(value, type, kotlinType); StackValue result; if (!isPrimitive(expressionType(expression.getReceiverExpression()))) { @@ -3039,7 +3044,7 @@ public class ExpressionCodegen extends KtVisitor impleme } private StackValue genLazyUnlessProvided(@Nullable StackValue pregenerated, @NotNull KtExpression expr, @NotNull Type type) { - return pregenerated != null ? StackValue.coercion(pregenerated, type) : genLazy(expr, type); + return pregenerated != null ? StackValue.coercion(pregenerated, type, null) : genLazy(expr, type); } private StackValue genUnlessProvided(@Nullable StackValue pregenerated, @NotNull KtExpression expr, @NotNull Type type) { @@ -3309,7 +3314,7 @@ public class ExpressionCodegen extends KtVisitor impleme leftValue.dup(v, false); Label leftIsNull = new Label(); v.ifnull(leftIsNull); - StackValue.coercion(leftValue, leftType).put(leftType, null, v); + StackValue.coercion(leftValue, leftType, null).put(leftType, null, v); StackValue nonNullLeftValue = StackValue.onStack(leftType); StackValue rightValue = gen(right); @@ -3326,7 +3331,7 @@ public class ExpressionCodegen extends KtVisitor impleme v.mark(rightIsNotNull); } - StackValue.coercion(rightValue, rightType).put(rightType, null, v); + StackValue.coercion(rightValue, rightType, null).put(rightType, null, v); StackValue nonNullRightValue = StackValue.onStack(rightType); StackValue.cmp(opToken, leftType, nonNullLeftValue, nonNullRightValue).put(Type.BOOLEAN_TYPE, null, v); v.goTo(end); @@ -3356,7 +3361,7 @@ public class ExpressionCodegen extends KtVisitor impleme return; } else { - StackValue.coercion(leftValue, leftType).put(leftType, null, v); + StackValue.coercion(leftValue, leftType, null).put(leftType, null, v); leftValue = StackValue.onStack(leftType); } @@ -3374,7 +3379,7 @@ public class ExpressionCodegen extends KtVisitor impleme v.goTo(end); v.mark(rightIsNotNull); - StackValue.coercion(rightValue, rightType).put(rightType, null, v); + StackValue.coercion(rightValue, rightType, null).put(rightType, null, v); StackValue nonNullRightValue = StackValue.onStack(rightType); StackValue.cmp(opToken, leftType, leftValue, nonNullRightValue).put(Type.BOOLEAN_TYPE, null, v); diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java index 6dfa31f1ca7..19c8b6edeed 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java @@ -566,11 +566,11 @@ public abstract class StackValue { return new FieldForSharedVar(field.type, field.owner, field.name, newSharedVarReceiver, field.isLateinit, field.variableName); } - public static StackValue coercion(@NotNull StackValue value, @NotNull Type castType) { + public static StackValue coercion(@NotNull StackValue value, @NotNull Type castType, @Nullable KotlinType castKotlinType) { if (value.type.equals(castType)) { return value; } - return new CoercionValue(value, castType); + return new CoercionValue(value, castType, castKotlinType); } @NotNull @@ -683,7 +683,7 @@ public abstract class StackValue { private static StackValue platformStaticCallIfPresent(@NotNull StackValue resultReceiver, @NotNull CallableDescriptor descriptor) { if (CodegenUtilKt.isJvmStaticInObjectOrClassOrInterface(descriptor)) { if (resultReceiver.canHaveSideEffects()) { - return coercion(resultReceiver, Type.VOID_TYPE); + return coercion(resultReceiver, Type.VOID_TYPE, null); } else { return none(); diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.kt index f5a034c5de1..056774fa3b2 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.kt @@ -22,13 +22,14 @@ import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter class CoercionValue( val value: StackValue, - private val castType: Type + private val castType: Type, + private val castKotlinType: KotlinType? ) : StackValue(castType, value.canHaveSideEffects()) { override fun putSelector(type: Type, kotlinType: KotlinType?, v: InstructionAdapter) { value.putSelector(value.type, value.kotlinType, v) - StackValue.coerce(value.type, castType, v) - StackValue.coerce(castType, type, v) + StackValue.coerce(value.type, value.kotlinType, castType, castKotlinType, v) + StackValue.coerce(castType, castKotlinType, type, kotlinType, v) } override fun storeSelector(topOfStackType: Type, topOfStackKotlinType: KotlinType?, v: InstructionAdapter) { @@ -110,7 +111,7 @@ fun ValueParameterDescriptor.findJavaDefaultArgumentValue(targetType: Type, type is EnumEntry -> enumEntry(castResult.descriptor, typeMapper) is Constant -> { val unboxedType = unboxPrimitiveTypeOrNull(targetType) ?: targetType - return coercion(constant(castResult.value, unboxedType), targetType) + return coercion(constant(castResult.value, unboxedType), targetType, null) } } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt index 1cea6afb8fe..e87e0d5847d 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt @@ -728,7 +728,10 @@ class PsiInlineCodegen( val receiverValue = getBoundCallableReferenceReceiver(argumentExpression) if (receiverValue != null) { val receiver = codegen.generateReceiverValue(receiverValue, false) - putClosureParametersOnStack(lambdaInfo, StackValue.coercion(receiver, receiver.type.boxReceiverForBoundReference())) + putClosureParametersOnStack( + lambdaInfo, + StackValue.coercion(receiver, receiver.type.boxReceiverForBoundReference(), null) + ) } } else { diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/KCallableNameProperty.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/KCallableNameProperty.kt index deaec63e447..8bbc550f19c 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/KCallableNameProperty.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/KCallableNameProperty.kt @@ -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.intrinsics @@ -40,7 +29,8 @@ class KCallableNameProperty : IntrinsicPropertyGetter() { // Generate the left-hand side of a bound callable reference expression if (callableReferenceReceiver != null && callableReferenceReceiver !is ImplicitClassReceiver) { val stackValue = codegen.generateReceiverValue(callableReferenceReceiver, false) - StackValue.coercion(stackValue, codegen.asmType(callableReferenceReceiver.type)).put(VOID_TYPE, iv) + val kotlinType = callableReferenceReceiver.type + StackValue.coercion(stackValue, codegen.asmType(kotlinType), kotlinType).put(VOID_TYPE, iv) } iv.aconst(referenceResolvedCall.resultingDescriptor.name.asString()) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/KClassJavaProperty.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/KClassJavaProperty.kt index 6be791e5b78..7a95ba161bd 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/KClassJavaProperty.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/KClassJavaProperty.kt @@ -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.intrinsics @@ -31,6 +20,6 @@ class KClassJavaProperty : IntrinsicPropertyGetter() { val receiverExpression = classLiteralExpression.receiverExpression ?: return null val lhs = codegen.bindingContext.get(DOUBLE_COLON_LHS, receiverExpression) ?: return null val value = codegen.generateClassLiteralReference(lhs, receiverExpression, /* wrapIntoKClass = */ false) - return StackValue.coercion(value, returnType) + return StackValue.coercion(value, returnType, null) } }