From 65fe1101fdc196b4387c7769b45548a7224972d8 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Mon, 23 Jul 2018 11:01:08 +0300 Subject: [PATCH] Object references in object field initializers are 'GET_VAR this@Object' --- .../generators/ArgumentsGenerationUtils.kt | 2 +- .../provideDelegate/memberExtension.txt | 5 +- ...eferenceInClosureInSuperConstructorCall.kt | 3 + ...ferenceInClosureInSuperConstructorCall.txt | 63 +++++++++++++++++++ .../objectReferenceInFieldInitializer.kt | 5 ++ .../objectReferenceInFieldInitializer.txt | 56 +++++++++++++++++ .../kotlin/ir/IrTextTestCaseGenerated.java | 10 +++ 7 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/ir/irText/expressions/objectReferenceInClosureInSuperConstructorCall.kt create mode 100644 compiler/testData/ir/irText/expressions/objectReferenceInClosureInSuperConstructorCall.txt create mode 100644 compiler/testData/ir/irText/expressions/objectReferenceInFieldInitializer.kt create mode 100644 compiler/testData/ir/irText/expressions/objectReferenceInFieldInitializer.txt diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ArgumentsGenerationUtils.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ArgumentsGenerationUtils.kt index 72ca00d7146..52edfd35208 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ArgumentsGenerationUtils.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ArgumentsGenerationUtils.kt @@ -124,7 +124,7 @@ private fun StatementGenerator.shouldGenerateReceiverAsSingletonReference(receiv val scopeOwner = this.scopeOwner return receiverClassDescriptor.kind.isSingleton && scopeOwner != receiverClassDescriptor && // For anonymous initializers - !(scopeOwner is FunctionDescriptor && scopeOwner.containingDeclaration == receiverClassDescriptor) // Methods of object + !(scopeOwner is CallableMemberDescriptor && scopeOwner.containingDeclaration == receiverClassDescriptor) // Members of object } private fun StatementGenerator.generateThisOrSuperReceiver(receiver: ReceiverValue, classDescriptor: ClassDescriptor): IrExpression { diff --git a/compiler/testData/ir/irText/declarations/provideDelegate/memberExtension.txt b/compiler/testData/ir/irText/declarations/provideDelegate/memberExtension.txt index 759325ed9d2..ea2a3e0f73f 100644 --- a/compiler/testData/ir/irText/declarations/provideDelegate/memberExtension.txt +++ b/compiler/testData/ir/irText/declarations/provideDelegate/memberExtension.txt @@ -59,7 +59,7 @@ FILE fqName: fileName:/memberExtension.kt FIELD DELEGATE name:plusK$delegate type:Host.StringDelegate visibility:private flags:final EXPRESSION_BODY CALL 'provideDelegate(Any?, Any) on String: Host.StringDelegate' type=Host.StringDelegate origin=null - $this: GET_OBJECT 'Host' type=Host + $this: GET_VAR 'this@Host: Host' type=Host origin=null $receiver: CONST String type=kotlin.String value=K host: GET_VAR 'this@Host: Host' type=Host origin=null p: PROPERTY_REFERENCE 'plusK: String on String' field=null getter='() on String: String' setter=null type=kotlin.reflect.KProperty2 origin=PROPERTY_REFERENCE_FOR_DELEGATE @@ -78,7 +78,7 @@ FILE fqName: fileName:/memberExtension.kt FIELD PROPERTY_BACKING_FIELD name:ok type:kotlin.String visibility:public flags:final EXPRESSION_BODY CALL '() on String: String' type=kotlin.String origin=GET_PROPERTY - $this: GET_OBJECT 'Host' type=Host + $this: GET_VAR 'this@Host: Host' type=Host origin=null $receiver: CONST String type=kotlin.String value=O FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:Host) returnType:kotlin.String flags: correspondingProperty: PROPERTY name:ok visibility:public modality:FINAL flags:val @@ -100,4 +100,3 @@ FILE fqName: fileName:/memberExtension.kt overridden: FUN IR_EXTERNAL_DECLARATION_STUB name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String flags: $this: VALUE_PARAMETER name: type:kotlin.Any flags: - diff --git a/compiler/testData/ir/irText/expressions/objectReferenceInClosureInSuperConstructorCall.kt b/compiler/testData/ir/irText/expressions/objectReferenceInClosureInSuperConstructorCall.kt new file mode 100644 index 00000000000..cfd4765eb86 --- /dev/null +++ b/compiler/testData/ir/irText/expressions/objectReferenceInClosureInSuperConstructorCall.kt @@ -0,0 +1,3 @@ +abstract class Base(val lambda: () -> Any) + +object Test : Base({ -> Test }) \ No newline at end of file diff --git a/compiler/testData/ir/irText/expressions/objectReferenceInClosureInSuperConstructorCall.txt b/compiler/testData/ir/irText/expressions/objectReferenceInClosureInSuperConstructorCall.txt new file mode 100644 index 00000000000..0a45db87a74 --- /dev/null +++ b/compiler/testData/ir/irText/expressions/objectReferenceInClosureInSuperConstructorCall.txt @@ -0,0 +1,63 @@ +FILE fqName: fileName:/objectReferenceInClosureInSuperConstructorCall.kt + CLASS CLASS name:Base modality:ABSTRACT visibility:public flags: superTypes:[kotlin.Any] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:Base flags: + CONSTRUCTOR visibility:public <> (lambda:() -> kotlin.Any) returnType:Base flags:primary + VALUE_PARAMETER name:lambda index:0 type:() -> kotlin.Any flags: + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'constructor Any()' + INSTANCE_INITIALIZER_CALL classDescriptor='Base' + PROPERTY name:lambda visibility:public modality:FINAL flags:val + FIELD PROPERTY_BACKING_FIELD name:lambda type:() -> kotlin.Any visibility:public flags:final + EXPRESSION_BODY + GET_VAR 'value-parameter lambda: () -> Any' type=() -> kotlin.Any origin=INITIALIZE_PROPERTY_FROM_PARAMETER + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:Base) returnType:() -> kotlin.Any flags: + correspondingProperty: PROPERTY name:lambda visibility:public modality:FINAL flags:val + $this: VALUE_PARAMETER name: type:Base flags: + BLOCK_BODY + RETURN type=kotlin.Nothing from='(): () -> Any' + GET_FIELD 'lambda: () -> Any' type=() -> kotlin.Any origin=null + receiver: GET_VAR 'this@Base: Base' type=Base origin=null + FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean flags: + overridden: + FUN IR_EXTERNAL_DECLARATION_STUB name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: + VALUE_PARAMETER name:other index:0 type:kotlin.Any? flags: + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int flags: + overridden: + FUN IR_EXTERNAL_DECLARATION_STUB name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String flags: + overridden: + FUN IR_EXTERNAL_DECLARATION_STUB name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: + CLASS OBJECT name:Test modality:FINAL visibility:public flags: superTypes:[Base] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:Test flags: + CONSTRUCTOR visibility:private <> () returnType:Test flags:primary + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'constructor Base(() -> Any)' + lambda: BLOCK type=() -> Test origin=LAMBDA + FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> () returnType:Test flags: + BLOCK_BODY + RETURN type=kotlin.Nothing from='(): Test' + GET_OBJECT 'Test' type=Test + FUNCTION_REFERENCE '(): Test' type=() -> Test origin=LAMBDA + INSTANCE_INITIALIZER_CALL classDescriptor='Test' + PROPERTY FAKE_OVERRIDE name:lambda visibility:public modality:FINAL flags:val + FUN FAKE_OVERRIDE name: visibility:public modality:FINAL <> ($this:Base) returnType:() -> kotlin.Any flags: + correspondingProperty: PROPERTY FAKE_OVERRIDE name:lambda visibility:public modality:FINAL flags:val + overridden: + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:Base) returnType:() -> kotlin.Any flags: + $this: VALUE_PARAMETER name: type:Base flags: + FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean flags: + overridden: + FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: + VALUE_PARAMETER name:other index:0 type:kotlin.Any? flags: + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int flags: + overridden: + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String flags: + overridden: + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: diff --git a/compiler/testData/ir/irText/expressions/objectReferenceInFieldInitializer.kt b/compiler/testData/ir/irText/expressions/objectReferenceInFieldInitializer.kt new file mode 100644 index 00000000000..182f9c982f4 --- /dev/null +++ b/compiler/testData/ir/irText/expressions/objectReferenceInFieldInitializer.kt @@ -0,0 +1,5 @@ +object A { + private const val a = "$" + private const val b = "1234$a" + private const val c = 10000 +} diff --git a/compiler/testData/ir/irText/expressions/objectReferenceInFieldInitializer.txt b/compiler/testData/ir/irText/expressions/objectReferenceInFieldInitializer.txt new file mode 100644 index 00000000000..0189228f012 --- /dev/null +++ b/compiler/testData/ir/irText/expressions/objectReferenceInFieldInitializer.txt @@ -0,0 +1,56 @@ +FILE fqName: fileName:/objectReferenceInFieldInitializer.kt + CLASS OBJECT name:A modality:FINAL visibility:public flags: superTypes:[kotlin.Any] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:A flags: + CONSTRUCTOR visibility:private <> () returnType:A flags:primary + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'constructor Any()' + INSTANCE_INITIALIZER_CALL classDescriptor='A' + PROPERTY name:a visibility:private modality:FINAL flags:const,val + FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private flags:final + EXPRESSION_BODY + CONST String type=kotlin.String value=$ + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:private modality:FINAL <> ($this:A) returnType:kotlin.String flags: + correspondingProperty: PROPERTY name:a visibility:private modality:FINAL flags:const,val + $this: VALUE_PARAMETER name: type:A flags: + BLOCK_BODY + RETURN type=kotlin.Nothing from='(): String' + GET_FIELD 'a: String' type=kotlin.String origin=null + receiver: GET_VAR 'this@A: A' type=A origin=null + PROPERTY name:b visibility:private modality:FINAL flags:const,val + FIELD PROPERTY_BACKING_FIELD name:b type:kotlin.String visibility:private flags:final + EXPRESSION_BODY + STRING_CONCATENATION type=kotlin.String + CONST String type=kotlin.String value=1234 + CALL '(): String' type=kotlin.String origin=GET_PROPERTY + $this: GET_VAR 'this@A: A' type=A origin=null + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:private modality:FINAL <> ($this:A) returnType:kotlin.String flags: + correspondingProperty: PROPERTY name:b visibility:private modality:FINAL flags:const,val + $this: VALUE_PARAMETER name: type:A flags: + BLOCK_BODY + RETURN type=kotlin.Nothing from='(): String' + GET_FIELD 'b: String' type=kotlin.String origin=null + receiver: GET_VAR 'this@A: A' type=A origin=null + PROPERTY name:c visibility:private modality:FINAL flags:const,val + FIELD PROPERTY_BACKING_FIELD name:c type:kotlin.Int visibility:private flags:final + EXPRESSION_BODY + CONST Int type=kotlin.Int value=10000 + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:private modality:FINAL <> ($this:A) returnType:kotlin.Int flags: + correspondingProperty: PROPERTY name:c visibility:private modality:FINAL flags:const,val + $this: VALUE_PARAMETER name: type:A flags: + BLOCK_BODY + RETURN type=kotlin.Nothing from='(): Int' + GET_FIELD 'c: Int' type=kotlin.Int origin=null + receiver: GET_VAR 'this@A: A' type=A origin=null + FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean flags: + overridden: + FUN IR_EXTERNAL_DECLARATION_STUB name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: + VALUE_PARAMETER name:other index:0 type:kotlin.Any? flags: + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int flags: + overridden: + FUN IR_EXTERNAL_DECLARATION_STUB name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String flags: + overridden: + FUN IR_EXTERNAL_DECLARATION_STUB name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: diff --git a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java index 44cb50fe150..fecffb6e47b 100644 --- a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java @@ -977,6 +977,16 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase { runTest("compiler/testData/ir/irText/expressions/objectReference.kt"); } + @TestMetadata("objectReferenceInClosureInSuperConstructorCall.kt") + public void testObjectReferenceInClosureInSuperConstructorCall() throws Exception { + runTest("compiler/testData/ir/irText/expressions/objectReferenceInClosureInSuperConstructorCall.kt"); + } + + @TestMetadata("objectReferenceInFieldInitializer.kt") + public void testObjectReferenceInFieldInitializer() throws Exception { + runTest("compiler/testData/ir/irText/expressions/objectReferenceInFieldInitializer.kt"); + } + @TestMetadata("outerClassInstanceReference.kt") public void testOuterClassInstanceReference() throws Exception { runTest("compiler/testData/ir/irText/expressions/outerClassInstanceReference.kt");