From d66d582f25169f6b9f2590acf7e3ede626dcdf3f Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Thu, 22 Sep 2016 13:48:43 +0300 Subject: [PATCH] Update to origin/master. IrReturn should return 'Unit' for "return without value". --- .../backend/jvm/lower/FileClassDescriptor.kt | 4 ++-- .../psi2ir/generators/AssignmentGenerator.kt | 2 +- .../kotlin/psi2ir/generators/BodyGenerator.kt | 12 +++++++----- .../kotlin/psi2ir/generators/CallGenerator.kt | 2 +- .../psi2ir/generators/StatementGenerator.kt | 3 ++- .../kotlin/ir/expressions/IrReturn.kt | 2 +- .../ir/expressions/impl/IrReturnImpl.kt | 19 ++++--------------- .../ir/irText/expressions/smartCasts.txt | 1 + .../smartCastsWithDestructuring.txt | 1 + .../expressions/tryCatchWithImplicitCast.txt | 1 + .../testData/ir/irText/lambdas/justLambda.txt | 1 + .../ir/irText/lambdas/nonLocalReturn.txt | 5 +++++ 12 files changed, 27 insertions(+), 26 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FileClassDescriptor.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FileClassDescriptor.kt index a4607f74244..d3f1197abd6 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FileClassDescriptor.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FileClassDescriptor.kt @@ -31,7 +31,7 @@ class FileClassDescriptorImpl( private val sourceElement: SourceElement ) : FileClassDescriptor { override fun getCompanionObjectDescriptor(): ClassDescriptor? = null - override fun getConstructors(): Collection = emptyList() + override fun getConstructors(): Collection = emptyList() override fun getContainingDeclaration(): DeclarationDescriptor = containingDeclarationImpl override fun getDeclaredTypeParameters(): List = emptyList() override fun getDefaultType(): SimpleType = ErrorUtils.createErrorType("File class type for $nameImpl") @@ -45,7 +45,7 @@ class FileClassDescriptorImpl( override fun getThisAsReceiverParameter(): ReceiverParameterDescriptor = error("File class has no instances") override fun getUnsubstitutedInnerClassesScope(): MemberScope = error("File class has no inner classes scope") override fun getUnsubstitutedMemberScope(): MemberScope = error("File class has no member scope") - override fun getUnsubstitutedPrimaryConstructor(): ConstructorDescriptor? = null + override fun getUnsubstitutedPrimaryConstructor(): ClassConstructorDescriptor? = null override fun getVisibility(): Visibility = Visibilities.PUBLIC override fun isCompanionObject(): Boolean = false override fun isData(): Boolean = false diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/AssignmentGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/AssignmentGenerator.kt index 1df54ece6cf..74785f9f49a 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/AssignmentGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/AssignmentGenerator.kt @@ -163,7 +163,7 @@ class AssignmentGenerator(statementGenerator: StatementGenerator) : StatementGen private fun getThisClass(): ClassDescriptor { val scopeOwner = statementGenerator.scopeOwner return when (scopeOwner) { - is ConstructorDescriptor -> scopeOwner.containingDeclaration + is ClassConstructorDescriptor -> scopeOwner.containingDeclaration is ClassDescriptor -> scopeOwner else -> scopeOwner.containingDeclaration as ClassDescriptor } diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BodyGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BodyGenerator.kt index 4ac352ffcfe..39ace11f8dd 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BodyGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BodyGenerator.kt @@ -80,7 +80,9 @@ class BodyGenerator(val scopeOwner: DeclarationDescriptor, override val context: statementGenerator.generateReturnExpression(ktReturnedValue, irBlockBody) } else { - irBlockBody.statements.add(generateReturnExpression(ktBody.startOffset, ktBody.endOffset, null)) + irBlockBody.statements.add(generateReturnExpression( + ktBody.startOffset, ktBody.endOffset, + IrGetObjectValueImpl(ktBody.startOffset, ktBody.endOffset, context.builtIns.unitType, context.builtIns.unit))) } } @@ -110,7 +112,7 @@ class BodyGenerator(val scopeOwner: DeclarationDescriptor, override val context: generateReturnExpression(startOffset, endOffset, this) } - private fun generateReturnExpression(startOffset: Int, endOffset: Int, returnValue: IrExpression?): IrReturnImpl { + private fun generateReturnExpression(startOffset: Int, endOffset: Int, returnValue: IrExpression): IrReturnImpl { val returnTarget = (scopeOwner as? CallableDescriptor) ?: throw AssertionError("'return' in a non-callable: $scopeOwner") return IrReturnImpl(startOffset, endOffset, context.builtIns.nothingType, returnTarget, returnValue) @@ -130,7 +132,7 @@ class BodyGenerator(val scopeOwner: DeclarationDescriptor, override val context: } private fun generateDelegatingConstructorCall(irBlockBody: IrBlockBodyImpl, ktConstructor: KtSecondaryConstructor) { - val constructorDescriptor = scopeOwner as ConstructorDescriptor + val constructorDescriptor = scopeOwner as ClassConstructorDescriptor val statementGenerator = createStatementGenerator() val ktDelegatingConstructorCall = ktConstructor.getDelegationCall() @@ -167,7 +169,7 @@ class BodyGenerator(val scopeOwner: DeclarationDescriptor, override val context: generateSuperConstructorCall(irBlockBody, ktClassOrObject) - val classDescriptor = (scopeOwner as ConstructorDescriptor).containingDeclaration + val classDescriptor = (scopeOwner as ClassConstructorDescriptor).containingDeclaration irBlockBody.statements.add(IrInstanceInitializerCallImpl(ktClassOrObject.startOffset, ktClassOrObject.endOffset, classDescriptor)) return irBlockBody @@ -178,7 +180,7 @@ class BodyGenerator(val scopeOwner: DeclarationDescriptor, override val context: generateDelegatingConstructorCall(irBlockBody, ktConstructor) - val classDescriptor = getOrFail(BindingContext.CONSTRUCTOR, ktConstructor).containingDeclaration + val classDescriptor = getOrFail(BindingContext.CONSTRUCTOR, ktConstructor).containingDeclaration as ClassDescriptor irBlockBody.statements.add(IrInstanceInitializerCallImpl(ktConstructor.startOffset, ktConstructor.endOffset, classDescriptor)) ktConstructor.bodyExpression?.let { ktBody -> diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/CallGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/CallGenerator.kt index fc93cac07a3..7702a26f418 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/CallGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/CallGenerator.kt @@ -71,7 +71,7 @@ class CallGenerator(statementGenerator: StatementGenerator): StatementGeneratorE fun generateEnumConstructorSuperCall(startOffset: Int, endOffset: Int, call: CallBuilder, enumEntryDescriptor: ClassDescriptor?) : IrExpression { val constructorDescriptor = call.descriptor - if (constructorDescriptor !is ConstructorDescriptor) throw AssertionError("Constructor expected: $constructorDescriptor") + if (constructorDescriptor !is ClassConstructorDescriptor) throw AssertionError("Constructor expected: $constructorDescriptor") val classDescriptor = constructorDescriptor.containingDeclaration if (classDescriptor.kind != ClassKind.ENUM_CLASS) throw AssertionError("Enum class constructor expected: $classDescriptor") diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt index e3eab4804d4..592378b6982 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt @@ -136,7 +136,8 @@ class StatementGenerator( override fun visitReturnExpression(expression: KtReturnExpression, data: Nothing?): IrStatement { val returnTarget = getReturnExpressionTarget(expression) - val irReturnedExpression = expression.returnedExpression?.genExpr() + val irReturnedExpression = expression.returnedExpression?.genExpr() ?: + IrGetObjectValueImpl(expression.startOffset, expression.endOffset, context.builtIns.unitType, context.builtIns.unit) return IrReturnImpl(expression.startOffset, expression.endOffset, context.builtIns.nothingType, returnTarget, irReturnedExpression) } diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrReturn.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrReturn.kt index ab551dc893a..8ff4f835d8f 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrReturn.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrReturn.kt @@ -20,7 +20,7 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor interface IrReturn : IrExpression { - var value: IrExpression? + var value: IrExpression val returnTarget: CallableDescriptor } diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrReturnImpl.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrReturnImpl.kt index dd51dbec2ed..528853898db 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrReturnImpl.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrReturnImpl.kt @@ -27,28 +27,17 @@ class IrReturnImpl( startOffset: Int, endOffset: Int, type: KotlinType, - override val returnTarget: CallableDescriptor + override val returnTarget: CallableDescriptor, + override var value: IrExpression ) : IrExpressionBase(startOffset, endOffset, type), IrReturn { - constructor( - startOffset: Int, - endOffset: Int, - type: KotlinType, - returnTarget: CallableDescriptor, - value: IrExpression? - ) : this(startOffset, endOffset, type, returnTarget) { - this.value = value - } - - override var value: IrExpression? = null - override fun accept(visitor: IrElementVisitor, data: D): R = visitor.visitReturn(this, data) override fun acceptChildren(visitor: IrElementVisitor, data: D) { - value?.accept(visitor, data) + value.accept(visitor, data) } override fun transformChildren(transformer: IrElementTransformer, data: D) { - value = value?.transform(transformer, data) + value = value.transform(transformer, data) } } \ No newline at end of file diff --git a/compiler/testData/ir/irText/expressions/smartCasts.txt b/compiler/testData/ir/irText/expressions/smartCasts.txt index 67bc7514f51..7f1f46165b5 100644 --- a/compiler/testData/ir/irText/expressions/smartCasts.txt +++ b/compiler/testData/ir/irText/expressions/smartCasts.txt @@ -18,6 +18,7 @@ FILE /smartCasts.kt if: TYPE_OP origin=NOT_INSTANCEOF typeOperand=kotlin.String GET_VAR 'value-parameter x: Any' type=kotlin.Any origin=null then: RETURN type=kotlin.Nothing from='test1(Any): Unit' + GET_OBJECT 'Unit' type=kotlin.Unit CALL 'println(Int): Unit' type=kotlin.Unit origin=null message: CALL '(): Int' type=kotlin.Int origin=GET_PROPERTY $this: TYPE_OP origin=IMPLICIT_CAST typeOperand=kotlin.String diff --git a/compiler/testData/ir/irText/expressions/smartCastsWithDestructuring.txt b/compiler/testData/ir/irText/expressions/smartCastsWithDestructuring.txt index 7719bde096c..a55bd06595a 100644 --- a/compiler/testData/ir/irText/expressions/smartCastsWithDestructuring.txt +++ b/compiler/testData/ir/irText/expressions/smartCastsWithDestructuring.txt @@ -16,6 +16,7 @@ FILE /smartCastsWithDestructuring.kt if: TYPE_OP origin=NOT_INSTANCEOF typeOperand=I2 GET_VAR 'value-parameter x: I1' type=I1 origin=null then: RETURN type=kotlin.Nothing from='test(I1): Unit' + GET_OBJECT 'Unit' type=kotlin.Unit COMPOSITE type=kotlin.Unit origin=DESTRUCTURING_DECLARATION VAR IR_TEMPORARY_VARIABLE val tmp0_container: I1 GET_VAR 'value-parameter x: I1' type=I1 origin=null diff --git a/compiler/testData/ir/irText/expressions/tryCatchWithImplicitCast.txt b/compiler/testData/ir/irText/expressions/tryCatchWithImplicitCast.txt index fd4eaffaf15..548b788ee57 100644 --- a/compiler/testData/ir/irText/expressions/tryCatchWithImplicitCast.txt +++ b/compiler/testData/ir/irText/expressions/tryCatchWithImplicitCast.txt @@ -6,6 +6,7 @@ FILE /tryCatchWithImplicitCast.kt if: TYPE_OP origin=NOT_INSTANCEOF typeOperand=kotlin.String GET_VAR 'value-parameter a: Any' type=kotlin.Any origin=null then: RETURN type=kotlin.Nothing from='testImplicitCast(Any): Unit' + GET_OBJECT 'Unit' type=kotlin.Unit VAR val t: kotlin.String TRY type=kotlin.String try: BLOCK type=kotlin.String origin=null diff --git a/compiler/testData/ir/irText/lambdas/justLambda.txt b/compiler/testData/ir/irText/lambdas/justLambda.txt index 12ce9877b9f..69fd6d6b588 100644 --- a/compiler/testData/ir/irText/lambdas/justLambda.txt +++ b/compiler/testData/ir/irText/lambdas/justLambda.txt @@ -19,6 +19,7 @@ FILE /justLambda.kt FUN LOCAL_FUNCTION_FOR_LAMBDA local final fun (): kotlin.Unit BLOCK_BODY RETURN type=kotlin.Nothing from='(): Unit' + GET_OBJECT 'Unit' type=kotlin.Unit CALLABLE_REFERENCE '(): Unit' type=() -> kotlin.Unit origin=LAMBDA FUN DEFAULT_PROPERTY_ACCESSOR public fun (): () -> kotlin.Unit BLOCK_BODY diff --git a/compiler/testData/ir/irText/lambdas/nonLocalReturn.txt b/compiler/testData/ir/irText/lambdas/nonLocalReturn.txt index d158993a8ce..c7c01bf4162 100644 --- a/compiler/testData/ir/irText/lambdas/nonLocalReturn.txt +++ b/compiler/testData/ir/irText/lambdas/nonLocalReturn.txt @@ -7,6 +7,7 @@ FILE /nonLocalReturn.kt FUN LOCAL_FUNCTION_FOR_LAMBDA local final fun (): kotlin.Nothing BLOCK_BODY RETURN type=kotlin.Nothing from='test0(): Unit' + GET_OBJECT 'Unit' type=kotlin.Unit CALLABLE_REFERENCE '(): Nothing' type=() -> kotlin.Nothing origin=LAMBDA FUN public fun test1(): kotlin.Unit BLOCK_BODY @@ -16,6 +17,7 @@ FILE /nonLocalReturn.kt FUN LOCAL_FUNCTION_FOR_LAMBDA local final fun (): kotlin.Unit BLOCK_BODY RETURN type=kotlin.Nothing from='(): Unit' + GET_OBJECT 'Unit' type=kotlin.Unit CALLABLE_REFERENCE '(): Unit' type=() -> kotlin.Unit origin=LAMBDA FUN public fun test2(): kotlin.Unit BLOCK_BODY @@ -25,6 +27,7 @@ FILE /nonLocalReturn.kt FUN LOCAL_FUNCTION_FOR_LAMBDA local final fun (): kotlin.Unit BLOCK_BODY RETURN type=kotlin.Nothing from='(): Unit' + GET_OBJECT 'Unit' type=kotlin.Unit CALLABLE_REFERENCE '(): Unit' type=() -> kotlin.Unit origin=LAMBDA FUN public fun testLrmFoo1(ints: kotlin.collections.List): kotlin.Unit BLOCK_BODY @@ -40,6 +43,7 @@ FILE /nonLocalReturn.kt arg0: GET_VAR 'value-parameter it: Int' type=kotlin.Int origin=null arg1: CONST Int type=kotlin.Int value='0' then: RETURN type=kotlin.Nothing from='(Int): Unit' + GET_OBJECT 'Unit' type=kotlin.Unit RETURN type=kotlin.Nothing from='(Int): Unit' CALL 'print(Int): Unit' type=kotlin.Unit origin=null message: GET_VAR 'value-parameter it: Int' type=kotlin.Int origin=null @@ -58,6 +62,7 @@ FILE /nonLocalReturn.kt arg0: GET_VAR 'value-parameter it: Int' type=kotlin.Int origin=null arg1: CONST Int type=kotlin.Int value='0' then: RETURN type=kotlin.Nothing from='(Int): Unit' + GET_OBJECT 'Unit' type=kotlin.Unit RETURN type=kotlin.Nothing from='(Int): Unit' CALL 'print(Int): Unit' type=kotlin.Unit origin=null message: GET_VAR 'value-parameter it: Int' type=kotlin.Int origin=null