Extension properties in class: don't put receiver on stack for GETFIELD/PUTFIELD instruction

#KT-3031 Fixed
This commit is contained in:
Natalia.Ukhorskaya
2012-11-13 18:36:02 +04:00
parent f868d965e1
commit ba2eab526a
17 changed files with 318 additions and 5 deletions
@@ -1409,6 +1409,9 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
}
}
}
if (directToField) {
receiver = StackValue.receiverWithoutReceiverArgument(receiver);
}
JetType receiverType = bindingContext.get(BindingContext.EXPRESSION_TYPE, r);
receiver.put(receiverType != null && !isSuper ? asmType(receiverType) : OBJECT_TYPE, v);
if (receiverType != null) {
@@ -1535,11 +1538,13 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
PropertyDescriptor initialDescriptor = propertyDescriptor;
propertyDescriptor = initialDescriptor.getOriginal();
boolean isInsideClass = isCallInsideSameClassAsDeclared(propertyDescriptor, context);
boolean isExtensionProperty = propertyDescriptor.getReceiverParameter() != null;
Method getter = null;
Method setter = null;
if (!forceField) {
//noinspection ConstantConditions
if (isInsideClass &&
!isExtensionProperty &&
(propertyDescriptor.getGetter() == null ||
!DescriptorUtils.isExternallyAccessible(propertyDescriptor) ||
propertyDescriptor.getGetter().isDefault() && propertyDescriptor.getGetter().getModality() == Modality.FINAL)) {
@@ -1576,7 +1581,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
}
}
//noinspection ConstantConditions
if (!propertyDescriptor.isVar() || isInsideClass &&
if (!propertyDescriptor.isVar() || isInsideClass && !isExtensionProperty &&
(propertyDescriptor.getSetter() == null ||
!DescriptorUtils.isExternallyAccessible(propertyDescriptor) ||
propertyDescriptor.getSetter().isDefault() &&
@@ -336,11 +336,20 @@ public abstract class StackValue {
@Nullable CallableMethod callableMethod
) {
if (resolvedCall.getThisObject().exists() || resolvedCall.getReceiverArgument().exists()) {
return new CallReceiver(resolvedCall, receiver, codegen, callableMethod);
return new CallReceiver(resolvedCall, receiver, codegen, callableMethod, true);
}
return receiver;
}
public static StackValue receiverWithoutReceiverArgument(StackValue receiverWithParameter) {
if (receiverWithParameter instanceof CallReceiver) {
CallReceiver callReceiver = (CallReceiver) receiverWithParameter;
return new CallReceiver(callReceiver.resolvedCall, callReceiver.receiver,
callReceiver.codegen, callReceiver.callableMethod, false);
}
return receiverWithParameter;
}
public static StackValue singleton(ClassDescriptor classDescriptor, JetTypeMapper typeMapper) {
final Type type = typeMapper.mapType(classDescriptor.getDefaultType());
@@ -1228,18 +1237,21 @@ public abstract class StackValue {
final StackValue receiver;
private final ExpressionCodegen codegen;
private final CallableMethod callableMethod;
private final boolean putReceiverArgumentOnStack;
public CallReceiver(
ResolvedCall<? extends CallableDescriptor> resolvedCall,
StackValue receiver,
ExpressionCodegen codegen,
CallableMethod callableMethod
CallableMethod callableMethod,
boolean putReceiverArgumentOnStack
) {
super(calcType(resolvedCall, codegen, callableMethod));
this.resolvedCall = resolvedCall;
this.receiver = receiver;
this.codegen = codegen;
this.callableMethod = callableMethod;
this.putReceiverArgumentOnStack = putReceiverArgumentOnStack;
}
private static Type calcType(
@@ -1301,14 +1313,16 @@ public abstract class StackValue {
codegen.generateFromResolvedCall(thisObject, codegen.typeMapper
.mapType(descriptor.getExpectedThisObject().getType()));
}
genReceiver(v, receiverArgument, type, descriptor.getReceiverParameter(), 1);
if (putReceiverArgumentOnStack) {
genReceiver(v, receiverArgument, type, descriptor.getReceiverParameter(), 1);
}
}
else {
genReceiver(v, thisObject, type, null, 0);
}
}
else {
if (receiverArgument.exists()) {
if (putReceiverArgumentOnStack && receiverArgument.exists()) {
genReceiver(v, receiverArgument, type, descriptor.getReceiverParameter(), 0);
}
}
@@ -0,0 +1,11 @@
class Test {
val Int.foo: String = "OK"
fun test(): String {
return 1.foo
}
}
fun box(): String {
return Test().test()
}
@@ -0,0 +1,16 @@
class Test {
var Double.fooDouble: String = "fail"
var Long.fooLong: String = "fail"
fun test(): String {
val d = 1.0
d.fooDouble = "O"
val l = 1.toLong()
l.fooLong = "K"
return d.fooDouble + l.fooLong
}
}
fun box(): String {
return Test().test()
}
@@ -0,0 +1,13 @@
class Test {
var Int.foo: String = "fail"
fun test(): String {
val i = 1
i.foo = "OK"
return i.foo
}
}
fun box(): String {
return Test().test()
}
@@ -0,0 +1,15 @@
class Test {
val Int.foo: String = "OK"
get() {
val a = $foo
return "OK"
}
fun test(): String {
return 1.foo
}
}
fun box(): String {
return Test().test()
}
@@ -0,0 +1,15 @@
class Test {
private val Int.foo: String = "OK"
get() {
val a = $foo
return "OK"
}
fun test(): String {
return 1.foo
}
}
fun box(): String {
return Test().test()
}
@@ -0,0 +1,16 @@
class Test {
var Int.foo: String = "OK"
private set(str: String) {
$foo = str
}
fun test(): String {
val i = 1
i.foo = "OK"
return i.foo
}
}
fun box(): String {
return Test().test()
}
@@ -0,0 +1,16 @@
class Test {
var Int.foo: String = "fail"
set(str: String) {
$foo = str
}
fun test(): String {
val i = 1
i.foo = "OK"
return i.foo
}
}
fun box(): String {
return Test().test()
}
@@ -0,0 +1,5 @@
val Int.foo: String = "OK"
fun box(): String {
return 1.foo
}
@@ -0,0 +1,10 @@
var Double.fooDouble: String = "fail"
var Long.fooLong: String = "fail"
fun box(): String {
val d = 1.0
d.fooDouble = "O"
val l = 1.toLong()
l.fooLong = "K"
return d.fooDouble + l.fooLong
}
@@ -0,0 +1,17 @@
var Double.fooDouble: String = "fail"
set(str: String) {
$fooDouble = str
}
var Long.fooLong: String = "fail"
set(str: String) {
$fooLong = str
}
fun box(): String {
val d = 1.0
d.fooDouble = "O"
val l = 1.toLong()
l.fooLong = "K"
return d.fooDouble + l.fooLong
}
@@ -0,0 +1,8 @@
val Int.foo: String = "OK"
get() {
return $foo
}
fun box(): String {
return 1.foo
}
@@ -0,0 +1,10 @@
var Int.foo: String = "fail"
set(str: String) {
$foo = str
}
fun box(): String {
val i = 1
i.foo = "OK"
return i.foo
}
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2012 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.
*/
package org.jetbrains.jet.codegen.extension;
import org.jetbrains.jet.ConfigurationKind;
import org.jetbrains.jet.codegen.CodegenTestCase;
import java.io.IOException;
public abstract class AbstractExtensionPropertiesTest extends CodegenTestCase {
private static final String REDUNDANT_PATH_PREFIX = "compiler/testData/codegen";
@Override
public void setUp() throws Exception {
super.setUp();
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_AND_ANNOTATIONS);
}
protected void doTest(String path) throws IOException {
String relativePath = path.substring(REDUNDANT_PATH_PREFIX.length());
blackBoxFile(relativePath);
}
}
@@ -0,0 +1,96 @@
/*
* Copyright 2010-2012 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.
*/
package org.jetbrains.jet.codegen.extension;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.File;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import org.jetbrains.jet.codegen.extension.AbstractExtensionPropertiesTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@TestMetadata("compiler/testData/codegen/extensionProperties")
public class ExtensionPropertiesTestGenerated extends AbstractExtensionPropertiesTest {
public void testAllFilesPresentInExtensionProperties() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/extensionProperties"), "kt", true);
}
@TestMetadata("inClass.kt")
public void testInClass() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/inClass.kt");
}
@TestMetadata("inClassLongTypeInReceiver.kt")
public void testInClassLongTypeInReceiver() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/inClassLongTypeInReceiver.kt");
}
@TestMetadata("inClassWithEmptySetter.kt")
public void testInClassWithEmptySetter() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/inClassWithEmptySetter.kt");
}
@TestMetadata("inClassWithGetter.kt")
public void testInClassWithGetter() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/inClassWithGetter.kt");
}
@TestMetadata("inClassWithPrivateGetter.kt")
public void testInClassWithPrivateGetter() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/inClassWithPrivateGetter.kt");
}
@TestMetadata("inClassWithPrivateSetter.kt")
public void testInClassWithPrivateSetter() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/inClassWithPrivateSetter.kt");
}
@TestMetadata("inClassWithSetter.kt")
public void testInClassWithSetter() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/inClassWithSetter.kt");
}
@TestMetadata("topLevel.kt")
public void testTopLevel() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/topLevel.kt");
}
@TestMetadata("topLevelLongTypeInReceiver.kt")
public void testTopLevelLongTypeInReceiver() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/topLevelLongTypeInReceiver.kt");
}
@TestMetadata("topLevelSetterLongTypeInReceiver.kt")
public void testTopLevelSetterLongTypeInReceiver() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/topLevelSetterLongTypeInReceiver.kt");
}
@TestMetadata("topLevelWithGetter.kt")
public void testTopLevelWithGetter() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/topLevelWithGetter.kt");
}
@TestMetadata("topLevelWithSetter.kt")
public void testTopLevelWithSetter() throws Exception {
doTest("compiler/testData/codegen/extensionProperties/topLevelWithSetter.kt");
}
}
@@ -23,6 +23,7 @@ import org.jetbrains.jet.checkers.AbstractJetPsiCheckerTest;
import org.jetbrains.jet.codegen.AbstractDataClassCodegenTest;
import org.jetbrains.jet.codegen.AbstractIntrinsicsTestCase;
import org.jetbrains.jet.codegen.AbstractMultiDeclTestCase;
import org.jetbrains.jet.codegen.extension.AbstractExtensionPropertiesTest;
import org.jetbrains.jet.codegen.flags.AbstractWriteFlagsTest;
import org.jetbrains.jet.codegen.labels.AbstractLabelGenTest;
import org.jetbrains.jet.jvm.compiler.AbstractLoadCompiledKotlinTest;
@@ -99,6 +100,12 @@ public class GenerateTests {
testModel("compiler/testData/codegen/label")
);
generateTest(
"compiler/tests/",
"ExtensionPropertiesTestGenerated",
AbstractExtensionPropertiesTest.class,
testModel("compiler/testData/codegen/extensionProperties")
);
generateTest(
"compiler/tests/",