Add KotlinType to methods coerceTo & coerceFrom

This commit is contained in:
Mikhail Zarechenskiy
2018-02-05 04:13:43 +03:00
parent 1ac4e9755a
commit b0ee61620c
6 changed files with 42 additions and 53 deletions
@@ -74,7 +74,7 @@ abstract class SafeCallFusedWithPrimitiveEqualityBase(
v.iconst(0) v.iconst(0)
v.mark(endLabel) v.mark(endLabel)
coerceTo(type, v) coerceTo(type, kotlinType, v)
} }
} }
@@ -31,7 +31,7 @@ open class BranchedValue(
v.visitLabel(branchJumpLabel) v.visitLabel(branchJumpLabel)
v.iconst(0) v.iconst(0)
v.visitLabel(endLabel) v.visitLabel(endLabel)
coerceTo(type, v) coerceTo(type, kotlinType, v)
} }
open fun condJump(jumpLabel: Label, v: InstructionAdapter, jumpIfFalse: Boolean) { open fun condJump(jumpLabel: Label, v: InstructionAdapter, jumpIfFalse: Boolean) {
@@ -67,7 +67,7 @@ open class BranchedValue(
override fun putSelector(type: Type, kotlinType: KotlinType?, v: InstructionAdapter) { override fun putSelector(type: Type, kotlinType: KotlinType?, v: InstructionAdapter) {
v.iconst(1) v.iconst(1)
coerceTo(type, v) coerceTo(type, kotlinType, v)
} }
} }
@@ -89,7 +89,7 @@ open class BranchedValue(
override fun putSelector(type: Type, kotlinType: KotlinType?, v: InstructionAdapter) { override fun putSelector(type: Type, kotlinType: KotlinType?, v: InstructionAdapter) {
v.iconst(0) v.iconst(0)
coerceTo(type, v) coerceTo(type, kotlinType, v)
} }
} }
@@ -1,17 +1,6 @@
/* /*
* Copyright 2010-2017 JetBrains s.r.o. * 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.
* 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.
*/ */
package org.jetbrains.kotlin.codegen; package org.jetbrains.kotlin.codegen;
@@ -728,7 +717,7 @@ public abstract class MemberCodegen<T extends KtPureElement/* TODO: & KtDeclarat
public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) { public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
markLineNumberForElement(element.getPsiOrParent(), codegen.v); markLineNumberForElement(element.getPsiOrParent(), codegen.v);
generateMethodCallTo(original, accessor, codegen.v).coerceTo(signature.getReturnType(), codegen.v); generateMethodCallTo(original, accessor, codegen.v).coerceTo(signature.getReturnType(), null, codegen.v);
codegen.v.areturn(signature.getReturnType()); codegen.v.areturn(signature.getReturnType());
} }
@@ -363,11 +363,11 @@ public abstract class StackValue {
v.invokevirtual(methodOwner.getInternalName(), type.getClassName() + "Value", "()" + type.getDescriptor(), false); v.invokevirtual(methodOwner.getInternalName(), type.getClassName() + "Value", "()" + type.getDescriptor(), false);
} }
protected void coerceTo(@NotNull Type toType, @NotNull InstructionAdapter v) { protected void coerceTo(@NotNull Type toType, @Nullable KotlinType toKotlinType, @NotNull InstructionAdapter v) {
coerce(this.type, toType, v); coerce(this.type, toType, v);
} }
protected void coerceFrom(@NotNull Type topOfStackType, @NotNull InstructionAdapter v) { protected void coerceFrom(@NotNull Type topOfStackType, @Nullable KotlinType topOfStackKotlinType, @NotNull InstructionAdapter v) {
coerce(topOfStackType, this.type, v); coerce(topOfStackType, this.type, v);
} }
@@ -665,7 +665,7 @@ public abstract class StackValue {
@Override @Override
public void putSelector(@NotNull Type type, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) { public void putSelector(@NotNull Type type, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) {
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
} }
@@ -700,13 +700,13 @@ public abstract class StackValue {
if (isLateinit) { if (isLateinit) {
StackValue.genNonNullAssertForLateinit(v, name.asString()); StackValue.genNonNullAssertForLateinit(v, name.asString());
} }
coerceTo(type, v); coerceTo(type, kotlinType, v);
// TODO unbox // TODO unbox
} }
@Override @Override
public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) { public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType topOfStackKotlinType, @NotNull InstructionAdapter v) {
coerceFrom(topOfStackType, v); coerceFrom(topOfStackType, topOfStackKotlinType, v);
v.store(index, this.type); v.store(index, this.type);
if (isLateinit) { if (isLateinit) {
PseudoInsnsKt.storeNotNull(v); PseudoInsnsKt.storeNotNull(v);
@@ -790,7 +790,7 @@ public abstract class StackValue {
@Override @Override
public void putSelector(@NotNull Type type, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) { public void putSelector(@NotNull Type type, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) {
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
@Override @Override
@@ -811,7 +811,7 @@ public abstract class StackValue {
throw new UnsupportedOperationException("don't know how to move type " + type + " to top of stack"); throw new UnsupportedOperationException("don't know how to move type " + type + " to top of stack");
} }
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
else if (depth == 2) { else if (depth == 2) {
int size = this.type.getSize(); int size = this.type.getSize();
@@ -827,7 +827,7 @@ public abstract class StackValue {
throw new UnsupportedOperationException("don't know how to move type " + type + " to top of stack"); throw new UnsupportedOperationException("don't know how to move type " + type + " to top of stack");
} }
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
else { else {
throw new UnsupportedOperationException("unsupported move-to-top depth " + depth); throw new UnsupportedOperationException("unsupported move-to-top depth " + depth);
@@ -867,7 +867,7 @@ public abstract class StackValue {
} }
if (value != null || AsmUtil.isPrimitive(type)) { if (value != null || AsmUtil.isPrimitive(type)) {
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
} }
} }
@@ -881,8 +881,8 @@ public abstract class StackValue {
} }
@Override @Override
public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) { public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType topOfStackKotlinType, @NotNull InstructionAdapter v) {
coerceFrom(topOfStackType, v); coerceFrom(topOfStackType, topOfStackKotlinType, v);
v.astore(this.type); v.astore(this.type);
} }
@@ -896,7 +896,7 @@ public abstract class StackValue {
@NotNull Type type, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v @NotNull Type type, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v
) { ) {
v.aload(this.type); // assumes array and index are on the stack v.aload(this.type); // assumes array and index are on the stack
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
} }
@@ -1086,7 +1086,7 @@ public abstract class StackValue {
} }
CallGenerator callGenerator = getCallGenerator(); CallGenerator callGenerator = getCallGenerator();
callGenerator.genCall(getter, resolvedGetCall, genDefaultMaskIfPresent(callGenerator), codegen); callGenerator.genCall(getter, resolvedGetCall, genDefaultMaskIfPresent(callGenerator), codegen);
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
private boolean genDefaultMaskIfPresent(CallGenerator callGenerator) { private boolean genDefaultMaskIfPresent(CallGenerator callGenerator) {
@@ -1202,12 +1202,12 @@ public abstract class StackValue {
@Override @Override
public void putSelector(@NotNull Type type, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) { public void putSelector(@NotNull Type type, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) {
v.visitFieldInsn(isStaticPut ? GETSTATIC : GETFIELD, owner.getInternalName(), name, this.type.getDescriptor()); v.visitFieldInsn(isStaticPut ? GETSTATIC : GETFIELD, owner.getInternalName(), name, this.type.getDescriptor());
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
@Override @Override
public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) { public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType topOfStackKotlinType, @NotNull InstructionAdapter v) {
coerceFrom(topOfStackType, v); coerceFrom(topOfStackType, topOfStackKotlinType, v);
v.visitFieldInsn(isStaticStore ? PUTSTATIC : PUTFIELD, owner.getInternalName(), name, this.type.getDescriptor()); v.visitFieldInsn(isStaticStore ? PUTSTATIC : PUTFIELD, owner.getInternalName(), name, this.type.getDescriptor());
} }
@@ -1256,7 +1256,7 @@ public abstract class StackValue {
if (!skipLateinitAssertion) { if (!skipLateinitAssertion) {
genNotNullAssertionForLateInitIfNeeded(v); genNotNullAssertionForLateInitIfNeeded(v);
} }
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
else { else {
PropertyGetterDescriptor getterDescriptor = descriptor.getGetter(); PropertyGetterDescriptor getterDescriptor = descriptor.getGetter();
@@ -1348,9 +1348,9 @@ public abstract class StackValue {
} }
@Override @Override
public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) { public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType topOfStackKotlinType, @NotNull InstructionAdapter v) {
if (setter == null) { if (setter == null) {
coerceFrom(topOfStackType, v); coerceFrom(topOfStackType, topOfStackKotlinType, v);
assert fieldName != null : "Property should have either a setter or a field name: " + descriptor; assert fieldName != null : "Property should have either a setter or a field name: " + descriptor;
assert backingFieldOwner != null : "Property should have either a setter or a backingFieldOwner: " + descriptor; assert backingFieldOwner != null : "Property should have either a setter or a backingFieldOwner: " + descriptor;
v.visitFieldInsn(isStaticStore ? PUTSTATIC : PUTFIELD, backingFieldOwner.getInternalName(), fieldName, this.type.getDescriptor()); v.visitFieldInsn(isStaticStore ? PUTSTATIC : PUTFIELD, backingFieldOwner.getInternalName(), fieldName, this.type.getDescriptor());
@@ -1447,13 +1447,13 @@ public abstract class StackValue {
if (isLateinit) { if (isLateinit) {
StackValue.genNonNullAssertForLateinit(v, name.asString()); StackValue.genNonNullAssertForLateinit(v, name.asString());
} }
coerceFrom(refType, v); coerceFrom(refType, null, v);
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
@Override @Override
public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) { public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType topOfStackKotlinType, @NotNull InstructionAdapter v) {
coerceFrom(topOfStackType, v); coerceFrom(topOfStackType, topOfStackKotlinType, v);
Type refType = refType(this.type); Type refType = refType(this.type);
Type sharedType = sharedTypeForType(this.type); Type sharedType = sharedTypeForType(this.type);
v.visitFieldInsn(PUTFIELD, sharedType.getInternalName(), "element", refType.getDescriptor()); v.visitFieldInsn(PUTFIELD, sharedType.getInternalName(), "element", refType.getDescriptor());
@@ -1511,13 +1511,13 @@ public abstract class StackValue {
if (isLateinit) { if (isLateinit) {
StackValue.genNonNullAssertForLateinit(v, variableName.asString()); StackValue.genNonNullAssertForLateinit(v, variableName.asString());
} }
coerceFrom(refType, v); coerceFrom(refType, null, v);
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
@Override @Override
public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) { public void storeSelector(@NotNull Type topOfStackType, @Nullable KotlinType topOfStackKotlinType, @NotNull InstructionAdapter v) {
coerceFrom(topOfStackType, v); coerceFrom(topOfStackType, topOfStackKotlinType, v);
v.visitFieldInsn(PUTFIELD, sharedTypeForType(type).getInternalName(), "element", refType(type).getDescriptor()); v.visitFieldInsn(PUTFIELD, sharedTypeForType(type).getInternalName(), "element", refType(type).getDescriptor());
} }
@@ -1566,7 +1566,7 @@ public abstract class StackValue {
public void putSelector(@NotNull Type type, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) { public void putSelector(@NotNull Type type, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) {
if (!type.equals(Type.VOID_TYPE)) { if (!type.equals(Type.VOID_TYPE)) {
v.load(index, Type.INT_TYPE); v.load(index, Type.INT_TYPE);
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
v.iinc(index, increment); v.iinc(index, increment);
} }
@@ -1587,7 +1587,7 @@ public abstract class StackValue {
v.iinc(index, increment); v.iinc(index, increment);
if (!type.equals(Type.VOID_TYPE)) { if (!type.equals(Type.VOID_TYPE)) {
v.load(index, Type.INT_TYPE); v.load(index, Type.INT_TYPE);
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
} }
} }
@@ -1617,7 +1617,7 @@ public abstract class StackValue {
value.store(codegen.invokeFunction(resolvedCall, StackValue.onStack(this.type)), v, true); value.store(codegen.invokeFunction(resolvedCall, StackValue.onStack(this.type)), v, true);
value.put(this.type, this.kotlinType, v, true); value.put(this.type, this.kotlinType, v, true);
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
} }
@@ -1859,7 +1859,7 @@ public abstract class StackValue {
v.dup(); v.dup();
v.ifnull(ifNull); v.ifnull(ifNull);
} }
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
} }
@@ -1884,7 +1884,7 @@ public abstract class StackValue {
} }
v.mark(end); v.mark(end);
coerceTo(type, v); coerceTo(type, kotlinType, v);
} }
@Override @Override
@@ -64,7 +64,7 @@ open class OperationStackValue(resultType: Type, val lambda: (v: InstructionAdap
override fun putSelector(type: Type, kotlinType: KotlinType?, v: InstructionAdapter) { override fun putSelector(type: Type, kotlinType: KotlinType?, v: InstructionAdapter) {
lambda(v) lambda(v)
coerceTo(type, v) coerceTo(type, kotlinType, v)
} }
} }
@@ -32,7 +32,7 @@ class CallBasedInExpressionGenerator(
object : BranchedValue(argument, null, argument.type, Opcodes.IFEQ) { object : BranchedValue(argument, null, argument.type, Opcodes.IFEQ) {
override fun putSelector(type: Type, kotlinType: KotlinType?, v: InstructionAdapter) { override fun putSelector(type: Type, kotlinType: KotlinType?, v: InstructionAdapter) {
invokeFunction(v) invokeFunction(v)
coerceTo(type, v) coerceTo(type, kotlinType, v)
} }
override fun condJump(jumpLabel: Label, v: InstructionAdapter, jumpIfFalse: Boolean) { override fun condJump(jumpLabel: Label, v: InstructionAdapter, jumpIfFalse: Boolean) {