diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt index 5a42650c3e8..cdd3f90c633 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt @@ -22,6 +22,7 @@ import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.declarations.IrField import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.declarations.IrVariable +import org.jetbrains.kotlin.ir.declarations.getDefault import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/DeepCopyIrTreeWithDescriptoros.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/DeepCopyIrTreeWithDescriptoros.kt index 24b2189e661..26043857592 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/DeepCopyIrTreeWithDescriptoros.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/DeepCopyIrTreeWithDescriptoros.kt @@ -28,7 +28,6 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl import org.jetbrains.kotlin.ir.descriptors.IrTemporaryVariableDescriptorImpl import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl -import org.jetbrains.kotlin.ir.expressions.impl.IrCallableReferenceImpl import org.jetbrains.kotlin.ir.expressions.impl.IrReturnImpl import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl import org.jetbrains.kotlin.ir.util.DeepCopyIrTree @@ -383,19 +382,18 @@ internal class DeepCopyIrTreeWithDescriptors(val targetDescriptor: DeclarationDe override fun mapLocalPropertyDeclaration (descriptor: VariableDescriptorWithAccessors) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as VariableDescriptorWithAccessors override fun mapEnumEntryDeclaration (descriptor: ClassDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as ClassDescriptor override fun mapVariableDeclaration (descriptor: VariableDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as VariableDescriptor - override fun mapCatchParameterDeclaration (descriptor: VariableDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as VariableDescriptor override fun mapErrorDeclaration (descriptor: DeclarationDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) override fun mapClassReference (descriptor: ClassDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as ClassDescriptor override fun mapValueReference (descriptor: ValueDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as ValueDescriptor override fun mapVariableReference (descriptor: VariableDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as VariableDescriptor override fun mapPropertyReference (descriptor: PropertyDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as PropertyDescriptor - override fun mapCallee (descriptor: CallableDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as CallableDescriptor + override fun mapCallee (descriptor: FunctionDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as FunctionDescriptor override fun mapDelegatedConstructorCallee (descriptor: ClassConstructorDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as ClassConstructorDescriptor override fun mapEnumConstructorCallee (descriptor: ClassConstructorDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as ClassConstructorDescriptor - override fun mapCallableReference (descriptor: CallableDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as CallableDescriptor + override fun mapLocalPropertyReference (descriptor: VariableDescriptorWithAccessors) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as VariableDescriptorWithAccessors override fun mapClassifierReference (descriptor: ClassifierDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as ClassifierDescriptor - override fun mapReturnTarget (descriptor: CallableDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as CallableDescriptor + override fun mapReturnTarget (descriptor: FunctionDescriptor) = descriptorSubstituteMap.getOrDefault(descriptor, descriptor) as FunctionDescriptor //---------------------------------------------------------------------// @@ -413,10 +411,10 @@ internal class DeepCopyIrTreeWithDescriptors(val targetDescriptor: DeclarationDe startOffset = expression.startOffset, endOffset = expression.endOffset, type = newDescriptor.returnType!!, - descriptor = newDescriptor, - typeArguments = substituteTypeArguments(expression.getTypeArgumentsMap()), + calleeDescriptor = newDescriptor, + typeArguments = substituteTypeArguments(expression.transformTypeArguments(newDescriptor)), origin = expression.origin, - superQualifier = mapSuperQualifier(expression.superQualifier) + superQualifierDescriptor = mapSuperQualifier(expression.superQualifier) ).transformValueArguments(expression) } @@ -429,7 +427,7 @@ internal class DeepCopyIrTreeWithDescriptors(val targetDescriptor: DeclarationDe origin = mapDeclarationOrigin(declaration.origin), descriptor = mapFunctionDeclaration(declaration.descriptor), body = declaration.body?.transform(this, null) - ).transformDefaults(declaration) + ).transformParameters(declaration) //---------------------------------------------------------------------// @@ -445,20 +443,6 @@ internal class DeepCopyIrTreeWithDescriptors(val targetDescriptor: DeclarationDe //---------------------------------------------------------------------// - override fun visitCallableReference(expression: IrCallableReference): IrCallableReference { - val newDescriptor = mapCallableReference(expression.descriptor) - return IrCallableReferenceImpl( - startOffset = expression.startOffset, - endOffset = expression.endOffset, - type = expression.type, - descriptor = newDescriptor, - typeArguments = expression.getTypeArgumentsMap(), - origin = mapStatementOrigin(expression.origin) - ).transformValueArguments(expression) - } - - //---------------------------------------------------------------------// - fun getTypeOperatorReturnType(operator: IrTypeOperator, type: KotlinType) : KotlinType { return when (operator) { IrTypeOperator.CAST, @@ -494,7 +478,7 @@ internal class DeepCopyIrTreeWithDescriptors(val targetDescriptor: DeclarationDe startOffset = expression.startOffset, endOffset = expression.endOffset, type = substituteType(expression.type)!!, - returnTarget = mapReturnTarget(expression.returnTarget), + returnTargetDescriptor = mapReturnTarget(expression.returnTarget), value = expression.value.transform(this, null) ) @@ -594,10 +578,10 @@ internal class DeepCopyIrTreeWithDescriptors(val targetDescriptor: DeclarationDe startOffset = oldExpression.startOffset, endOffset = oldExpression.endOffset, type = substituteType(oldExpression.type)!!, - descriptor = newDescriptor, + calleeDescriptor = newDescriptor, typeArguments = substituteTypeArguments(oldExpression.typeArguments), origin = oldExpression.origin, - superQualifier = newSuperQualifier + superQualifierDescriptor = newSuperQualifier ).apply { oldExpression.descriptor.valueParameters.forEach { val valueArgument = oldExpression.getValueArgument(it) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/IrElementTransformerVoidWithContext.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/IrElementTransformerVoidWithContext.kt index d03de2bbfff..c6c40c91db8 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/IrElementTransformerVoidWithContext.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/IrElementTransformerVoidWithContext.kt @@ -31,7 +31,7 @@ abstract internal class IrElementTransformerVoidWithContext : IrElementTransform private val scopeStack = mutableListOf() override final fun visitFile(declaration: IrFile): IrFile { - scopeStack.push(ScopeWithIr(Scope(declaration.packageFragmentDescriptor), declaration)) + scopeStack.push(ScopeWithIr(Scope(declaration.symbol), declaration)) val result = visitFileNew(declaration) scopeStack.pop() return result diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/IrValidator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/IrValidator.kt index aa4ffc6ea2a..993494a79cb 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/IrValidator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/IrValidator.kt @@ -162,12 +162,6 @@ private class Declarations { declarations.add(createKey(declaration)) } - fun addParameterOf(aCatch: IrCatch) { - val kind = IrDeclarationKind.VARIABLE - val key = DeclarationKey(aCatch.parameter, kind) - declarations.add(key) - } - fun isAlreadyDeclared(declaration: IrDeclaration): Boolean { return createKey(declaration) in declarations } @@ -209,7 +203,10 @@ private class IrValidator(val context: BackendContext, performHeavyValidations: private fun recordDeclaration(declaration: IrDeclaration) { if (foundDeclarations.isAlreadyDeclared(declaration)) { - error(declaration, "redeclaration") + if (declaration.descriptor !is ReceiverParameterDescriptor && declaration !is IrTypeParameter) { + // TODO: remove the check. + error(declaration, "redeclaration") + } } else { foundDeclarations.add(declaration) } @@ -224,9 +221,4 @@ private class IrValidator(val context: BackendContext, performHeavyValidations: // Do not treat anonymous initializers as declarations, because they are not unique. super.visitDeclaration(declaration) } - - override fun visitCatch(aCatch: IrCatch) { - foundDeclarations.addParameterOf(aCatch) - super.visitCatch(aCatch) - } } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt index 5cd54e878ea..328c68e9395 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt @@ -25,11 +25,10 @@ import org.jetbrains.kotlin.descriptors.impl.* import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.declarations.* -import org.jetbrains.kotlin.ir.declarations.impl.IrConstructorImpl -import org.jetbrains.kotlin.ir.declarations.impl.IrFieldImpl -import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl +import org.jetbrains.kotlin.ir.declarations.impl.* import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.* +import org.jetbrains.kotlin.ir.util.createParameterDeclarations import org.jetbrains.kotlin.ir.util.transformFlat import org.jetbrains.kotlin.ir.visitors.* import org.jetbrains.kotlin.name.Name @@ -136,8 +135,8 @@ class LocalDeclarationsLowering(val context: BackendContext) : DeclarationContai val transformedDescriptors = mutableMapOf() - val CallableDescriptor.transformed: CallableDescriptor? - get() = transformedDescriptors[this] as CallableDescriptor? + val FunctionDescriptor.transformed: FunctionDescriptor? + get() = transformedDescriptors[this] as FunctionDescriptor? val oldParameterToNew: MutableMap = HashMap() val newParameterToOld: MutableMap = HashMap() @@ -168,6 +167,8 @@ class LocalDeclarationsLowering(val context: BackendContext) : DeclarationContai it.transformedDescriptor, original.body ).apply { + createParameterDeclarations() + original.descriptor.valueParameters.filter { it.declaresDefaultValue() }.forEach { argument -> val body = original.getDefault(argument)!! this.putDefault(oldParameterToNew[argument] as ValueParameterDescriptor, body) @@ -207,6 +208,9 @@ class LocalDeclarationsLowering(val context: BackendContext) : DeclarationContai if (transformedDescriptor != null) { return IrConstructorImpl(declaration.startOffset, declaration.endOffset, declaration.origin, transformedDescriptor, declaration.body!!).apply { + + createParameterDeclarations() + declaration.descriptor.valueParameters.filter { it.declaresDefaultValue() }.forEach { argument -> val body = declaration.getDefault(argument)!! this.putDefault(oldParameterToNew[argument] as ValueParameterDescriptor, body) @@ -287,13 +291,13 @@ class LocalDeclarationsLowering(val context: BackendContext) : DeclarationContai return this } - override fun visitCallableReference(expression: IrCallableReference): IrExpression { + override fun visitFunctionReference(expression: IrFunctionReference): IrExpression { expression.transformChildrenVoid(this) val oldCallee = expression.descriptor.original val newCallee = oldCallee.transformed ?: return expression - val newCallableReference = IrCallableReferenceImpl( + val newCallableReference = IrFunctionReferenceImpl( expression.startOffset, expression.endOffset, expression.type, // TODO functional type for transformed descriptor newCallee, @@ -386,7 +390,7 @@ class LocalDeclarationsLowering(val context: BackendContext) : DeclarationContai rewriteFunctionBody(memberDeclaration, null) } - private fun createNewCall(oldCall: IrCall, newCallee: CallableDescriptor) = + private fun createNewCall(oldCall: IrCall, newCallee: FunctionDescriptor) = if (oldCall is IrCallWithShallowCopy) oldCall.shallowCopy(oldCall.origin, newCallee, oldCall.superQualifier) else diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/LowerUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/LowerUtils.kt index c618dea9ab1..52213ae1558 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/LowerUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/LowerUtils.kt @@ -25,11 +25,13 @@ import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl import org.jetbrains.kotlin.ir.expressions.IrDelegatingConstructorCall import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin import org.jetbrains.kotlin.ir.expressions.IrTypeOperator import org.jetbrains.kotlin.ir.expressions.impl.* +import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid @@ -44,22 +46,45 @@ import org.jetbrains.kotlin.utils.Printer class IrLoweringContext(backendContext: BackendContext) : IrGeneratorContext(backendContext.irBuiltIns) -class DeclarationIrBuilder(backendContext: BackendContext, - declarationDescriptor: DeclarationDescriptor, - startOffset : Int = UNDEFINED_OFFSET, - endOffset : Int = UNDEFINED_OFFSET) : - IrBuilderWithScope( - IrLoweringContext(backendContext), - Scope(declarationDescriptor), - startOffset, - endOffset - ) +class DeclarationIrBuilder : IrBuilderWithScope { + constructor( + backendContext: BackendContext, + symbol: IrSymbol, + startOffset: Int = UNDEFINED_OFFSET, + endOffset: Int = UNDEFINED_OFFSET + ) : super( + IrLoweringContext(backendContext), + Scope(symbol), + startOffset, + endOffset + ) + + @Deprecated("Creates unbound symbol") + constructor( + backendContext: BackendContext, + declarationDescriptor: DeclarationDescriptor, + startOffset: Int = UNDEFINED_OFFSET, + endOffset: Int = UNDEFINED_OFFSET + ) : super( + IrLoweringContext(backendContext), + Scope(declarationDescriptor), + startOffset, + endOffset + ) +} + +@Deprecated("Creates unbound symbol") fun BackendContext.createIrBuilder(declarationDescriptor: DeclarationDescriptor, startOffset : Int = UNDEFINED_OFFSET, endOffset : Int = UNDEFINED_OFFSET) = DeclarationIrBuilder(this, declarationDescriptor, startOffset, endOffset) +fun BackendContext.createIrBuilder(symbol: IrSymbol, + startOffset : Int = UNDEFINED_OFFSET, + endOffset : Int = UNDEFINED_OFFSET) = + DeclarationIrBuilder(this, symbol, startOffset, endOffset) + fun T.at(element: IrElement) = this.at(element.startOffset, element.endOffset) @@ -75,26 +100,36 @@ inline fun IrGeneratorWithScope.irBlockBody(irElement: IrElement, body: IrBlockB this.irBlockBody(irElement.startOffset, irElement.endOffset, body) +@Deprecated("Creates unbound symbol") fun IrBuilderWithScope.irUnit() = IrGetObjectValueImpl(startOffset, endOffset, context.builtIns.unitType, context.builtIns.unit) fun IrBuilderWithScope.irIfThen(condition: IrExpression, thenPart: IrExpression) = IrIfThenElseImpl(startOffset, endOffset, context.builtIns.unitType, condition, thenPart, null) +@Deprecated("Creates unbound symbol") fun IrBuilderWithScope.irGet(descriptor: PropertyDescriptor) = - IrGetterCallImpl(startOffset, endOffset, descriptor.getter!!, null) + IrCallImpl(startOffset, endOffset, descriptor.getter!!) +@Deprecated("Creates unbound symbol") fun IrBuilderWithScope.irSet(receiver: IrExpression, descriptor: PropertyDescriptor, arg: IrExpression) = - IrSetterCallImpl(startOffset, endOffset, descriptor.setter!!, null, receiver, null, arg) + IrCallImpl(startOffset, endOffset, descriptor.setter!!).apply { + dispatchReceiver = receiver + putValueArgument(0, arg) + } fun IrBuilderWithScope.irNot(arg: IrExpression) = - primitiveOp1(startOffset, endOffset, context.irBuiltIns.booleanNot, IrStatementOrigin.EXCL, arg) + primitiveOp1(startOffset, endOffset, context.irBuiltIns.booleanNotSymbol, IrStatementOrigin.EXCL, arg) fun IrBuilderWithScope.irThrow(arg: IrExpression) = IrThrowImpl(startOffset, endOffset, context.builtIns.nothingType, arg) fun IrBuilderWithScope.irCatch(parameter: VariableDescriptor, result: IrExpression) = - IrCatchImpl(startOffset, endOffset, parameter, result) + IrCatchImpl( + startOffset, endOffset, + IrVariableImpl(startOffset, endOffset, IrDeclarationOrigin.CATCH_PARAMETER, parameter), + result + ) fun IrBuilderWithScope.irCast(arg: IrExpression, type: KotlinType, typeOperand: KotlinType) = IrTypeOperatorCallImpl(startOffset, endOffset, type, IrTypeOperator.CAST, typeOperand, arg) @@ -103,12 +138,15 @@ fun IrBuilderWithScope.irImplicitCoercionToUnit(arg: IrExpression) = IrTypeOperatorCallImpl(startOffset, endOffset, context.builtIns.unitType, IrTypeOperator.IMPLICIT_COERCION_TO_UNIT, context.builtIns.unitType, arg) +@Deprecated("Creates unbound symbol") fun IrBuilderWithScope.irGetField(receiver: IrExpression, descriptor: PropertyDescriptor) = IrGetFieldImpl(startOffset, endOffset, descriptor, receiver) +@Deprecated("Creates unbound symbol") fun IrBuilderWithScope.irSetField(receiver: IrExpression, descriptor: PropertyDescriptor, value: IrExpression) = IrSetFieldImpl(startOffset, endOffset, descriptor, receiver, value) +@Deprecated("Creates unbound symbol") open class IrBuildingTransformer(private val context: BackendContext) : IrElementTransformerVoid() { private var currentBuilder: IrBuilderWithScope? = null diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/TailrecLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/TailrecLowering.kt index e6ed6f6d0f4..6a02aacee5b 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/TailrecLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/lower/TailrecLowering.kt @@ -28,7 +28,9 @@ import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.descriptors.VariableDescriptor import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.declarations.IrFunction +import org.jetbrains.kotlin.ir.declarations.getDefault import org.jetbrains.kotlin.ir.expressions.* +import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol import org.jetbrains.kotlin.ir.util.getArguments import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid @@ -60,7 +62,7 @@ private fun lowerTailRecursionCalls(context: BackendContext, irFunction: IrFunct irFunction.body = builder.irBlockBody { // Define variables containing current values of parameters: val parameterToVariable = parameters.associate { - it to defineTemporaryVar(irGet(it), nameHint = it.suggestVariableName()) + it to irTemporaryVar(irGet(it), nameHint = it.suggestVariableName()).symbol } // (these variables are to be updated on any tail call). @@ -93,7 +95,7 @@ private class BodyTransformer( val irFunction: IrFunction, val loop: IrLoop, val parameterToNew: Map, - val parameterToVariable: Map, + val parameterToVariable: Map, val tailRecursionCalls: Set ) : IrElementTransformerVoid() { @@ -139,7 +141,7 @@ private class BodyTransformer( // Copy default value, mapping parameters to variables containing freshly computed arguments: val defaultValue = originalDefaultValue.transform(object : DeepCopyIrTreeWithDeclarations() { override fun mapValueReference(descriptor: ValueDescriptor): ValueDescriptor { - return parameterToVariable[descriptor] ?: super.mapValueReference(descriptor) + return parameterToVariable[descriptor]?.descriptor ?: super.mapValueReference(descriptor) } }, data = null) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/DumpIrTreeWithDescriptorsVisitor.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/DumpIrTreeWithDescriptorsVisitor.kt index 1d73a41c02a..e71202ae06d 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/DumpIrTreeWithDescriptorsVisitor.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/DumpIrTreeWithDescriptorsVisitor.kt @@ -264,7 +264,7 @@ class DumpIrTreeWithDescriptorsVisitor(out: Appendable): IrElementVisitor, boundParams: List, startOffset: Int, endOffset: Int): IrFunctionImpl { @@ -212,6 +214,8 @@ private class CallableReferencesUnbinder(val lower: CallableReferenceLowering, blockBody ) + newFunction.createParameterDeclarations() + return newFunction } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DefaultArgumentStubGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DefaultArgumentStubGenerator.kt index 8686c059d6f..48f618b1734 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DefaultArgumentStubGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DefaultArgumentStubGenerator.kt @@ -35,16 +35,14 @@ import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.builders.* -import org.jetbrains.kotlin.ir.declarations.IrDeclarationContainer -import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin -import org.jetbrains.kotlin.ir.declarations.IrDeclarationOriginImpl -import org.jetbrains.kotlin.ir.declarations.IrFunction +import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.impl.IrConstructorImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl import org.jetbrains.kotlin.ir.descriptors.IrTemporaryVariableDescriptorImpl import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.* +import org.jetbrains.kotlin.ir.util.createParameterDeclarations import org.jetbrains.kotlin.ir.util.transformFlat import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid @@ -141,7 +139,7 @@ class DefaultArgumentStubGenerator internal constructor(val context: Context): D + IrDelegatingConstructorCallImpl( startOffset = irFunction.startOffset, endOffset = irFunction.endOffset, - descriptor = functionDescriptor + constructorDescriptor = functionDescriptor ).apply { params.forEachIndexed { i, variable -> putValueArgument(i, irGet(variable)) @@ -164,12 +162,9 @@ class DefaultArgumentStubGenerator internal constructor(val context: Context): D }) } } - // Replace default argument initializers with empty composites. - functionDescriptor.valueParameters.forEach { - if (it.declaresDefaultValue()) { - irFunction.putDefault(it, IrExpressionBodyImpl(irFunction.startOffset, irFunction.endOffset, - IrCompositeImpl(irFunction.startOffset, irFunction.startOffset, it.type))) - } + // Remove default argument initializers. + irFunction.valueParameters.forEach { + it.defaultValue = null } return if (functionDescriptor is ClassConstructorDescriptor) listOf(irFunction, IrConstructorImpl( @@ -177,14 +172,14 @@ class DefaultArgumentStubGenerator internal constructor(val context: Context): D endOffset = irFunction.endOffset, descriptor = descriptor as ClassConstructorDescriptor, origin = DECLARATION_ORIGIN_FUNCTION_FOR_DEFAULT_PARAMETER, - body = body)) + body = body).apply { createParameterDeclarations() }) else listOf(irFunction, IrFunctionImpl( startOffset = irFunction.startOffset, endOffset = irFunction.endOffset, descriptor = descriptor, origin = DECLARATION_ORIGIN_FUNCTION_FOR_DEFAULT_PARAMETER, - body = body)) + body = body).apply { createParameterDeclarations() }) } return listOf(irFunction) } @@ -246,7 +241,7 @@ class DefaultParameterInjector internal constructor(val context: Context): BodyL return IrDelegatingConstructorCallImpl( startOffset = expression.startOffset, endOffset = expression.endOffset, - descriptor = descriptorForCall as ClassConstructorDescriptor) + constructorDescriptor = descriptorForCall as ClassConstructorDescriptor) .apply { params.forEach { log("call::params@${it.first.index}/${it.first.name.asString()}: ${ir2string(it.second)}") @@ -273,7 +268,7 @@ class DefaultParameterInjector internal constructor(val context: Context): BodyL return IrCallImpl( startOffset = expression.startOffset, endOffset = expression.endOffset, - descriptor = descriptor, + calleeDescriptor = descriptor, typeArguments = expression.descriptor.typeParameters.map{it to (expression.getTypeArgument(it) ?: it.defaultType) }.toMap()) .apply { params.forEach { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt index e9f6448aa14..23cd1d78b15 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt @@ -178,7 +178,7 @@ internal class PropertyDelegationLowering(val context: Context) : FileLoweringPa receiverType = receiverTypes.firstOrNull(), parameterTypes = if (receiverTypes.size < 2) listOf() else listOf(receiverTypes[1]), returnType = returnType) - IrCallableReferenceImpl(startOffset, endOffset, getterKFunctionType, getter, null).apply { + IrFunctionReferenceImpl(startOffset, endOffset, getterKFunctionType, getter, null).apply { dispatchReceiver = expression.dispatchReceiver extensionReceiver = expression.extensionReceiver } @@ -193,7 +193,7 @@ internal class PropertyDelegationLowering(val context: Context) : FileLoweringPa receiverType = receiverTypes.firstOrNull(), parameterTypes = if (receiverTypes.size < 2) listOf(returnType) else listOf(receiverTypes[1], returnType), returnType = context.builtIns.unitType) - IrCallableReferenceImpl(startOffset, endOffset, setterKFunctionType, it, null).apply { + IrFunctionReferenceImpl(startOffset, endOffset, setterKFunctionType, it, null).apply { dispatchReceiver = expression.dispatchReceiver extensionReceiver = expression.extensionReceiver } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/EnumClassLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/EnumClassLowering.kt index 9f126e3cf70..8ba2b79fd97 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/EnumClassLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/EnumClassLowering.kt @@ -32,11 +32,13 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.impl.ClassConstructorDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl import org.jetbrains.kotlin.incremental.components.NoLookupLocation +import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.impl.* import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.* +import org.jetbrains.kotlin.ir.util.createParameterDeclarations import org.jetbrains.kotlin.ir.util.dump import org.jetbrains.kotlin.ir.util.transform import org.jetbrains.kotlin.ir.util.transformFlat @@ -247,6 +249,8 @@ internal class EnumClassLowering(val context: Context) : ClassLoweringPass { ClassKind.CLASS, listOf(descriptor.defaultType), SourceElement.NO_SOURCE, false) val defaultClass = IrClassImpl(startOffset, endOffset, IrDeclarationOrigin.DEFINED, defaultClassDescriptor) + defaultClass.createParameterDeclarations() + val constructors = mutableSetOf() descriptor.constructors.forEach { @@ -286,6 +290,8 @@ internal class EnumClassLowering(val context: Context) : ClassLoweringPass { val implObjectDescriptor = loweredEnum.implObjectDescriptor val implObject = IrClassImpl(startOffset, endOffset, IrDeclarationOrigin.DEFINED, implObjectDescriptor) + implObject.createParameterDeclarations() + val enumEntries = mutableListOf() var i = 0 while (i < irClass.declarations.size) { @@ -329,7 +335,7 @@ internal class EnumClassLowering(val context: Context) : ClassLoweringPass { val startOffset = irClass.startOffset val endOffset = irClass.endOffset val irValuesInitializer = context.createArrayOfExpression(irClass.descriptor.defaultType, - enumEntries.sortedBy { it.descriptor.name }.map { it.initializerExpression }, startOffset, endOffset) + enumEntries.sortedBy { it.descriptor.name }.map { it.initializerExpression!! }, startOffset, endOffset) val irField = IrFieldImpl(startOffset, endOffset, DECLARATION_ORIGIN_ENUM, loweredEnum.valuesProperty, @@ -337,6 +343,8 @@ internal class EnumClassLowering(val context: Context) : ClassLoweringPass { val getter = IrFunctionImpl(startOffset, endOffset, DECLARATION_ORIGIN_ENUM, loweredEnum.valuesGetter) + getter.createParameterDeclarations() + val receiver = IrGetObjectValueImpl(startOffset, endOffset, loweredEnum.implObjectDescriptor.defaultType, loweredEnum.implObjectDescriptor) val value = IrGetFieldImpl(startOffset, endOffset, loweredEnum.valuesProperty, receiver) @@ -374,11 +382,9 @@ internal class EnumClassLowering(val context: Context) : ClassLoweringPass { } private fun lowerEnumConstructors(irClass: IrClass) { - irClass.declarations.transform { declaration -> + irClass.declarations.forEachIndexed { index, declaration -> if (declaration is IrConstructor) - transformEnumConstructor(declaration) - else - declaration + irClass.declarations[index] = transformEnumConstructor(declaration) } } @@ -390,6 +396,9 @@ internal class EnumClassLowering(val context: Context) : ClassLoweringPass { loweredConstructorDescriptor, enumConstructor.body!! // will be transformed later ) + + loweredEnumConstructor.createParameterDeclarations() + enumConstructor.descriptor.valueParameters.filter { it.declaresDefaultValue() }.forEach { val body = enumConstructor.getDefault(it)!! body.transformChildrenVoid(ParameterMapper(constructorDescriptor)) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FinallyBlocksLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FinallyBlocksLowering.kt index 32908bd697f..37460404c9d 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FinallyBlocksLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FinallyBlocksLowering.kt @@ -31,9 +31,9 @@ internal class FinallyBlocksLowering(val context: Context): FunctionLoweringPass fun toIr(context: Context, startOffset: Int, endOffset: Int, value: IrExpression): IrExpression } - private data class Return(val callableDescriptor: CallableDescriptor): HighLevelJump { + private data class Return(val functionDescriptor: FunctionDescriptor): HighLevelJump { override fun toIr(context: Context, startOffset: Int, endOffset: Int, value: IrExpression) - = IrReturnImpl(startOffset, endOffset, callableDescriptor, value) + = IrReturnImpl(startOffset, endOffset, functionDescriptor, value) } private data class Break(val loop: IrLoop): HighLevelJump { @@ -178,7 +178,7 @@ internal class FinallyBlocksLowering(val context: Context): FunctionLoweringPass return IrReturnImpl( startOffset = startOffset, endOffset = endOffset, - returnTarget = it, + returnTargetDescriptor = it, value = value) } } @@ -282,7 +282,7 @@ internal class FinallyBlocksLowering(val context: Context): FunctionLoweringPass private fun IrBuilderWithScope.irVar(descriptor: VariableDescriptor, initializer: IrExpression?) = IrVariableImpl(startOffset, endOffset, DECLARATION_ORIGIN_FINALLY_BLOCK, descriptor, initializer) - fun IrBuilderWithScope.irReturn(target: CallableDescriptor, value: IrExpression) = + fun IrBuilderWithScope.irReturn(target: FunctionDescriptor, value: IrExpression) = IrReturnImpl(startOffset, endOffset, target, value) inline fun IrBuilderWithScope.irReturnableBlock(descriptor: FunctionDescriptor, body: IrBlockBuilder.() -> Unit) = diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt index c2c4664b4b5..cd5301c052a 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt @@ -32,6 +32,7 @@ import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.declarations.IrModuleFragment +import org.jetbrains.kotlin.ir.declarations.getDefault import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InitializersLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InitializersLowering.kt index 8791b57aab2..1ec2f338dbe 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InitializersLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InitializersLowering.kt @@ -30,6 +30,7 @@ import org.jetbrains.kotlin.ir.expressions.IrDelegatingConstructorCall import org.jetbrains.kotlin.ir.expressions.IrInstanceInitializerCall import org.jetbrains.kotlin.ir.expressions.IrStatementOriginImpl import org.jetbrains.kotlin.ir.expressions.impl.* +import org.jetbrains.kotlin.ir.util.createParameterDeclarations import org.jetbrains.kotlin.ir.util.transformFlat import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid @@ -112,6 +113,9 @@ internal class InitializersLowering(val context: Context) : ClassLoweringPass { val endOffset = irClass.endOffset val initializer = IrFunctionImpl(startOffset, endOffset, DECLARATION_ORIGIN_ANONYMOUS_INITIALIZER, initializerMethodDescriptor, IrBlockBodyImpl(startOffset, endOffset, initializers)) + + initializer.createParameterDeclarations() + irClass.declarations.add(initializer) return initializerMethodDescriptor diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt index cc4c949c7f3..ae5e4881272 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt @@ -34,8 +34,8 @@ import org.jetbrains.kotlin.ir.builders.irGet import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl -import org.jetbrains.kotlin.ir.expressions.impl.IrCallableReferenceImpl import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl +import org.jetbrains.kotlin.ir.expressions.impl.IrFunctionReferenceImpl import org.jetbrains.kotlin.ir.expressions.impl.IrGetObjectValueImpl import org.jetbrains.kotlin.ir.util.getArguments import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid @@ -309,7 +309,7 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB } } - IrCallableReferenceImpl( + IrFunctionReferenceImpl( builder.startOffset, builder.endOffset, expression.type, target, @@ -387,8 +387,8 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB reportError("Type $this is not supported in callback signature") } - private fun unwrapStaticFunctionArgument(argument: IrExpression): IrCallableReference? { - if (argument is IrCallableReference) { + private fun unwrapStaticFunctionArgument(argument: IrExpression): IrFunctionReference? { + if (argument is IrFunctionReference) { return argument } @@ -413,7 +413,7 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB // 3. Second statement is IrCallableReference: - return argument.statements.last() as? IrCallableReference + return argument.statements.last() as? IrFunctionReference } private fun IrCall.getSingleTypeArgument(): KotlinType { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/StringConcatenationLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/StringConcatenationLowering.kt index 8c7d3bbc07b..baeb68887cd 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/StringConcatenationLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/StringConcatenationLowering.kt @@ -22,13 +22,14 @@ import org.jetbrains.kotlin.backend.konan.Context import org.jetbrains.kotlin.backend.konan.descriptors.signature2Descriptor import org.jetbrains.kotlin.ir.builders.irLetSequence import org.jetbrains.kotlin.builtins.PrimitiveType -import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.declarations.IrDeclaration import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.declarations.IrSymbolDeclaration import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrStringConcatenation import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid @@ -74,12 +75,12 @@ private class StringConcatenationTransformer(val lower: StringConcatenationLower private val defaultAppendFunction = classStringBuilder.signature2Descriptor(nameAppend, arrayOf(builtIns.nullableAnyType))!! - private val appendFunctions: Map = + private val appendFunctions: Map = typesWithSpecialAppendFunction.map { it to classStringBuilder.signature2Descriptor(nameAppend, arrayOf(it)) }.toMap() - private fun typeToAppendFunction(type : KotlinType) : CallableDescriptor { + private fun typeToAppendFunction(type : KotlinType) : FunctionDescriptor { return appendFunctions[type]?:defaultAppendFunction } @@ -107,9 +108,13 @@ private class StringConcatenationTransformer(val lower: StringConcatenationLower } override fun visitDeclaration(declaration: IrDeclaration): IrStatement { + if (declaration !is IrSymbolDeclaration<*>) { + return super.visitDeclaration(declaration) + } + with(declaration) { buildersStack.add( - context.createIrBuilder(declaration.descriptor, startOffset, endOffset) + context.createIrBuilder(declaration.symbol, startOffset, endOffset) ) transformChildrenVoid(this@StringConcatenationTransformer) buildersStack.removeAt(buildersStack.lastIndex) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/SuspendFunctionsLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/SuspendFunctionsLowering.kt index a71ee1b4a4b..d5f98334d64 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/SuspendFunctionsLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/SuspendFunctionsLowering.kt @@ -40,6 +40,7 @@ import org.jetbrains.kotlin.ir.declarations.impl.* import org.jetbrains.kotlin.ir.descriptors.IrTemporaryVariableDescriptorImpl import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.* +import org.jetbrains.kotlin.ir.util.createParameterDeclarations import org.jetbrains.kotlin.ir.util.getArguments import org.jetbrains.kotlin.ir.util.transformFlat import org.jetbrains.kotlin.ir.visitors.* @@ -131,7 +132,7 @@ internal class SuspendFunctionsLowering(val context: Context): DeclarationContai return IrCallImpl( startOffset = expression.startOffset, endOffset = expression.endOffset, - descriptor = invokeFunctionDescriptor + calleeDescriptor = invokeFunctionDescriptor ).apply { dispatchReceiver = expression.dispatchReceiver putValueArgument(0, expression.extensionReceiver) @@ -164,7 +165,7 @@ internal class SuspendFunctionsLowering(val context: Context): DeclarationContai } SuspendFunctionKind.NEEDS_STATE_MACHINE -> { - val coroutine = buildCoroutine(irFunction, callableReference) // Coroutine implementation. + val coroutine: IrDeclaration = buildCoroutine(irFunction, callableReference) // Coroutine implementation. if (suspendLambdas.contains(irFunction.descriptor)) // Suspend lambdas are called through factory method , listOf(coroutine) // thus we can eliminate original body. else @@ -450,6 +451,9 @@ internal class SuspendFunctionsLowering(val context: Context): DeclarationContai descriptor = coroutineClassDescriptor, members = coroutineMembers ) + + coroutineClass.createParameterDeclarations() + return BuiltCoroutine( coroutineClass = coroutineClass, coroutineConstructorDescriptor = coroutineFactoryConstructorBuilder?.descriptor @@ -496,6 +500,9 @@ internal class SuspendFunctionsLowering(val context: Context): DeclarationContai endOffset = endOffset, origin = DECLARATION_ORIGIN_COROUTINE_IMPL, descriptor = descriptor).apply { + + createParameterDeclarations() + body = irBuilder.irBlockBody { val completionParameter = descriptor.valueParameters.last() +IrDelegatingConstructorCallImpl(startOffset, endOffset, coroutineImplConstructorDescriptor).apply { @@ -561,6 +568,9 @@ internal class SuspendFunctionsLowering(val context: Context): DeclarationContai endOffset = endOffset, origin = DECLARATION_ORIGIN_COROUTINE_IMPL, descriptor = descriptor).apply { + + createParameterDeclarations() + body = irBuilder.irBlockBody { +IrDelegatingConstructorCallImpl(startOffset, endOffset, coroutineImplConstructorDescriptor).apply { putValueArgument(0, irNull()) // Completion. @@ -619,6 +629,9 @@ internal class SuspendFunctionsLowering(val context: Context): DeclarationContai endOffset = endOffset, origin = DECLARATION_ORIGIN_COROUTINE_IMPL, descriptor = descriptor).apply { + + createParameterDeclarations() + body = irBuilder.irBlockBody(startOffset, endOffset) { +irReturn( irCall(coroutineConstructorDescriptor).apply { @@ -684,6 +697,9 @@ internal class SuspendFunctionsLowering(val context: Context): DeclarationContai endOffset = endOffset, origin = DECLARATION_ORIGIN_COROUTINE_IMPL, descriptor = this.descriptor).apply { + + createParameterDeclarations() + body = irBuilder.irBlockBody(startOffset, endOffset) { +irReturn( irCall(doResumeFunctionDescriptor).apply { @@ -731,6 +747,9 @@ internal class SuspendFunctionsLowering(val context: Context): DeclarationContai endOffset = irFunction.endOffset, origin = DECLARATION_ORIGIN_COROUTINE_IMPL, descriptor = descriptor).apply { + + createParameterDeclarations() + body = context.createIrBuilder(descriptor, startOffset, endOffset).irBlockBody { +irReturn(irGetField(irThis(), propertyDescriptor)) } @@ -780,6 +799,9 @@ internal class SuspendFunctionsLowering(val context: Context): DeclarationContai endOffset = irFunction.endOffset, origin = DECLARATION_ORIGIN_COROUTINE_IMPL, descriptor = descriptor).apply { + + createParameterDeclarations() + body = context.createIrBuilder(descriptor, startOffset, endOffset).irBlockBody { +irSetField(irThis(), propertyDescriptor, irGet(valueParameterDescriptor)) } @@ -872,6 +894,8 @@ internal class SuspendFunctionsLowering(val context: Context): DeclarationContai origin = DECLARATION_ORIGIN_COROUTINE_IMPL, descriptor = descriptor).apply { + createParameterDeclarations() + body = irBuilder.irBlockBody(startOffset, endOffset) { // Extract all suspend calls to temporaries in order to make correct jumps to them. @@ -1021,20 +1045,6 @@ internal class SuspendFunctionsLowering(val context: Context): DeclarationContai descriptor = newVariable, initializer = declaration.initializer) } - - override fun visitCatch(aCatch: IrCatch): IrCatch { - aCatch.transformChildrenVoid(this) - - val newVariable = variablesMap[aCatch.parameter] - ?: return aCatch - - return IrCatchImpl( - startOffset = aCatch.startOffset, - endOffset = aCatch.endOffset, - parameter = newVariable, - result = aCatch.result - ) - } }) } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/VarargLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/VarargLowering.kt index 2ae75ad7f0f..6e4da26efef 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/VarargLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/VarargLowering.kt @@ -278,6 +278,6 @@ class VarargInjectionLowering internal constructor(val context: Context): Declar private fun IrBuilderWithScope.irConstInt(value: Int): IrConst = IrConstImpl.int(startOffset, endOffset, context.builtIns.intType, value) private fun IrBuilderWithScope.irBlock(type: KotlinType): IrBlock = IrBlockImpl(startOffset, endOffset, type) -private fun IrBuilderWithScope.irCall(descriptor: CallableDescriptor, typeArguments: Map?): IrCall = IrCallImpl(startOffset, endOffset, descriptor, typeArguments) +private fun IrBuilderWithScope.irCall(descriptor: FunctionDescriptor, typeArguments: Map?): IrCall = IrCallImpl(startOffset, endOffset, descriptor, typeArguments) private val IrBuilderWithScope.kIntZero get() = irConstInt(0) private val IrBuilderWithScope.kIntOne get() = irConstInt(1) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/SerializeIr.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/SerializeIr.kt index 2bf69103f09..33cb55464b8 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/SerializeIr.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/SerializeIr.kt @@ -38,6 +38,8 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.* +import org.jetbrains.kotlin.ir.symbols.impl.createFunctionSymbol +import org.jetbrains.kotlin.ir.util.createParameterDeclarations import org.jetbrains.kotlin.serialization.KonanIr import org.jetbrains.kotlin.serialization.KonanIr.IrConst.ValueCase.* import org.jetbrains.kotlin.serialization.KonanIr.IrOperation.OperationCase.* @@ -496,14 +498,17 @@ internal class IrSerializer(val context: Context, val proto = KonanIr.IrClass.newBuilder() val declarations = clazz.declarations declarations.forEach { - proto.addMember(serializeDeclaration(it)) + val descriptor = it.descriptor + if (descriptor !is CallableMemberDescriptor || descriptor.kind.isReal) { + proto.addMember(serializeDeclaration(it)) + } } return proto.build() } fun serializeIrEnumEntry(enumEntry: IrEnumEntry): KonanIr.IrEnumEntry { val proto = KonanIr.IrEnumEntry.newBuilder() - val initializer = enumEntry.initializerExpression + val initializer = enumEntry.initializerExpression!! proto.setInitializer(serializeExpression(initializer)) val correspondingClass = enumEntry.correspondingClass if (correspondingClass != null) { @@ -626,9 +631,10 @@ internal class IrDeserializer(val context: Context, fun deserializeCatch(proto: KonanIr.IrCatch, start: Int, end: Int): IrCatch { val parameter = deserializeDescriptor(proto.getParameter()) as VariableDescriptor + val catchParameter = IrVariableImpl(start, end, IrDeclarationOrigin.CATCH_PARAMETER, parameter) val result = deserializeExpression(proto.getResult()) - return IrCatchImpl(start, end, parameter, result) + return IrCatchImpl(start, end, catchParameter, result) } fun deserializeStatement(proto: KonanIr.IrStatement): IrElement { @@ -685,7 +691,7 @@ internal class IrDeserializer(val context: Context, } fun deserializeCall(proto: KonanIr.IrCall, start: Int, end: Int, type: KotlinType): IrCall { - val descriptor = deserializeDescriptor(proto.getDescriptor()) as CallableDescriptor + val descriptor = deserializeDescriptor(proto.getDescriptor()) as FunctionDescriptor val superDescriptor = if (proto.hasSuper()) { deserializeDescriptor(proto.getSuper()) as ClassDescriptor @@ -697,7 +703,7 @@ internal class IrDeserializer(val context: Context, // TODO: implement the last three args here. IrCallImpl(start, end, type, descriptor, typeArgs , null, superDescriptor) KonanIr.IrCall.Primitive.NULLARY -> - IrNullaryPrimitiveImpl(start, end, null, descriptor) + IrNullaryPrimitiveImpl(start, end, null, createFunctionSymbol(descriptor)) KonanIr.IrCall.Primitive.UNARY -> IrUnaryPrimitiveImpl(start, end, null, descriptor) KonanIr.IrCall.Primitive.BINARY -> @@ -713,7 +719,10 @@ internal class IrDeserializer(val context: Context, val descriptor = deserializeDescriptor(proto.getDescriptor()) as CallableDescriptor val typeMap = deserializeTypeMap(descriptor, proto.typeMap) - val callable = IrCallableReferenceImpl(start, end, type, descriptor, typeMap, null) + val callable = when (descriptor) { + is FunctionDescriptor -> IrFunctionReferenceImpl(start, end, type, descriptor, typeMap, null) + else -> TODO() + } return callable } @@ -761,7 +770,7 @@ internal class IrDeserializer(val context: Context, fun deserializeReturn(proto: KonanIr.IrReturn, start: Int, end: Int, type: KotlinType): IrReturn { val descriptor = - deserializeDescriptor(proto.getReturnTarget()) as CallableDescriptor + deserializeDescriptor(proto.getReturnTarget()) as FunctionDescriptor val value = deserializeExpression(proto.getValue()) return IrReturnImpl(start, end, type, descriptor, value) } @@ -769,9 +778,7 @@ internal class IrDeserializer(val context: Context, fun deserializeSetVariable(proto: KonanIr.IrSetVariable, start: Int, end: Int, type: KotlinType): IrSetVariable { val descriptor = deserializeDescriptor(proto.getDescriptor()) as VariableDescriptor val value = deserializeExpression(proto.getValue()) - val setVar = IrSetVariableImpl(start, end, descriptor, null) - setVar.value = value - return setVar + return IrSetVariableImpl(start, end, descriptor, value, null) } fun deserializeStringConcat(proto: KonanIr.IrStringConcat, start: Int, end: Int, type: KotlinType): IrStringConcatenation { @@ -972,6 +979,9 @@ internal class IrDeserializer(val context: Context, } val clazz = IrClassImpl(start, end, origin, descriptor, members) + + clazz.createParameterDeclarations() + return clazz } @@ -983,6 +993,8 @@ internal class IrDeserializer(val context: Context, val function = IrFunctionImpl(start, end, origin, descriptor, body as IrBody) + function.createParameterDeclarations() + proto.defaultArgumentList.forEach { val expr = deserializeExpression(it.value) @@ -1024,7 +1036,7 @@ internal class IrDeserializer(val context: Context, val origin = DEFINED // TODO: retore the real origins val declarator = proto.getDeclarator() - val declaration = when { + val declaration: IrDeclaration = when { declarator.hasIrClass() -> deserializeIrClass(declarator.irClass, descriptor as ClassDescriptor, start, end, origin) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/builders/IrBuilders.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/builders/IrBuilders.kt index f4c1966bc1c..f7822094240 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/builders/IrBuilders.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/builders/IrBuilders.kt @@ -16,10 +16,8 @@ package org.jetbrains.kotlin.ir.builders -import org.jetbrains.kotlin.descriptors.CallableDescriptor -import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor -import org.jetbrains.kotlin.descriptors.ValueDescriptor -import org.jetbrains.kotlin.descriptors.VariableDescriptor +import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrLoop import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin @@ -52,11 +50,34 @@ fun IrBuilderWithScope.irContinue(loop: IrLoop) = fun IrBuilderWithScope.irTrue() = IrConstImpl.boolean(startOffset, endOffset, context.builtIns.booleanType, true) +@Deprecated("Creates unbound symbol") fun IrBuilderWithScope.irGet(value: ValueDescriptor) = IrGetValueImpl(startOffset, endOffset, value) +@Deprecated("Creates unbound symbol") +fun IrBuilderWithScope.irGet(receiver: IrExpression?, property: PropertyDescriptor): IrCall = + IrCallImpl(startOffset, endOffset, property.getter!!).apply { + dispatchReceiver = receiver + } + +@Deprecated("Creates unbound symbol") +fun IrBuilderWithScope.irThis() = + scope.classOwner().let { classOwner -> + IrGetValueImpl(startOffset, endOffset, classOwner.thisAsReceiverParameter) + } + +@Deprecated("Creates unbound symbol") +fun IrBuilderWithScope.irSetVar(variable: VariableDescriptor, value: IrExpression) = + IrSetVariableImpl(startOffset, endOffset, variable, value, IrStatementOrigin.EQ) + +@Deprecated("Creates unbound symbol") +fun IrBuilderWithScope.irCall(descriptor: FunctionDescriptor): IrCallImpl { + return IrCallImpl(this.startOffset, this.endOffset, descriptor) +} + +@Deprecated("Creates unbound symbol") fun IrBuilderWithScope.irCall( - callee: CallableDescriptor, + callee: FunctionDescriptor, typeArguments: Map ): IrCallImpl { val substitutionContext = typeArguments.map { (typeParameter, typeArgument) -> diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils.kt index 1130dde6ced..6efe5e7ea32 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils.kt @@ -17,10 +17,19 @@ package org.jetbrains.kotlin.ir.util import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation +import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithSource import org.jetbrains.kotlin.descriptors.ParameterDescriptor import org.jetbrains.kotlin.ir.IrElement +import org.jetbrains.kotlin.ir.declarations.IrClass +import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.declarations.IrFunction +import org.jetbrains.kotlin.ir.declarations.impl.IrTypeParameterImpl +import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl import org.jetbrains.kotlin.ir.expressions.* +import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.startOffset +import org.jetbrains.kotlin.resolve.source.PsiSourceElement /** * Binds the arguments explicitly represented in the IR to the parameters of the accessed function. @@ -93,3 +102,51 @@ fun IrElement.getCompilerMessageLocation(containingFile: IrFile): CompilerMessag lineContent = null // TODO: retrieve the line content. ) } + +fun IrFunction.createParameterDeclarations() { + fun ParameterDescriptor.irValueParameter() = IrValueParameterImpl( + innerStartOffset(this), innerEndOffset(this), + IrDeclarationOrigin.DEFINED, + this + ) + + dispatchReceiverParameter = descriptor.dispatchReceiverParameter?.irValueParameter() + extensionReceiverParameter = descriptor.extensionReceiverParameter?.irValueParameter() + + assert(valueParameters.isEmpty()) + descriptor.valueParameters.mapTo(valueParameters) { it.irValueParameter() } + + assert(typeParameters.isEmpty()) + descriptor.typeParameters.mapTo(typeParameters) { + IrTypeParameterImpl( + innerStartOffset(it), innerEndOffset(it), + IrDeclarationOrigin.DEFINED, + it + ) + } +} + +fun IrClass.createParameterDeclarations() { + descriptor.thisAsReceiverParameter.let { + thisReceiver = IrValueParameterImpl( + innerStartOffset(it), innerEndOffset(it), + IrDeclarationOrigin.INSTANCE_RECEIVER, + it + ) + } + + assert(typeParameters.isEmpty()) + descriptor.declaredTypeParameters.mapTo(typeParameters) { + IrTypeParameterImpl( + innerStartOffset(it), innerEndOffset(it), + IrDeclarationOrigin.DEFINED, + it + ) + } +} + +private fun IrElement.innerStartOffset(descriptor: DeclarationDescriptorWithSource): Int = + (descriptor.source as? PsiSourceElement)?.psi?.startOffset ?: this.startOffset + +private fun IrElement.innerEndOffset(descriptor: DeclarationDescriptorWithSource): Int = + (descriptor.source as? PsiSourceElement)?.psi?.endOffset ?: this.endOffset diff --git a/gradle.properties b/gradle.properties index ac8a795fc4d..14b62e4ba30 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,8 +21,8 @@ remoteRoot=konan_tests #kotlinCompilerModule=org.jetbrains.kotlin:kotlin-compiler:1.1-SNAPSHOT # Download artifacts of https://teamcity.jetbrains.com/viewType.html?buildTypeId=bt345 testDataVersion=1053418:id -#kotlinCompilerRepo=http://dl.bintray.com/jetbrains/kotlin-native-dependencies +kotlinCompilerRepo=http://dl.bintray.com/jetbrains/kotlin-native-dependencies #kotlinCompilerRepo=http://oss.sonatype.org/content/repositories/snapshots -kotlinCompilerRepo=http://dl.bintray.com/kotlin/kotlin-dev -kotlinCompilerModule=org.jetbrains.kotlin:kotlin-compiler:1.1.3-dev-1810 +#kotlinCompilerRepo=http://dl.bintray.com/kotlin/kotlin-dev +kotlinCompilerModule=org.jetbrains.kotlin:kotlin-compiler:1.1-20170505.123347-536 konanVersion=0.2