diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt index abbe3550c2b..2bc4f618191 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.classifierOrFail +import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.utils.addToStdlib.assertedCast @@ -46,8 +47,12 @@ inline fun IrBuilderWithScope.irLetS( } -fun IrStatementsBuilder.irTemporary(value: IrExpression, nameHint: String? = null): IrVariable { - val temporary = scope.createTemporaryVariable(value, nameHint) +fun IrStatementsBuilder.irTemporary( + value: IrExpression, + nameHint: String? = null, + typeHint: KotlinType? = null +): IrVariable { + val temporary = scope.createTemporaryVariable(value, nameHint, type = typeHint) +temporary return temporary } @@ -58,8 +63,14 @@ fun IrStatementsBuilder.defineTemporary(value: IrExpression, return temporary.descriptor } -fun IrStatementsBuilder.irTemporaryVar(value: IrExpression, nameHint: String? = null): IrVariable { - val temporary = scope.createTemporaryVariable(value, nameHint, isMutable = true) +fun IrStatementsBuilder.irTemporaryVar( + value: IrExpression, + nameHint: String? = null, + typeHint: KotlinType? = null, + parent: IrDeclarationParent? = null +): IrVariable { + val temporary = scope.createTemporaryVariable(value, nameHint, isMutable = true, type = typeHint) + parent?.let { temporary.parent = it } +temporary return temporary } @@ -125,7 +136,7 @@ fun IrBuilderWithScope.irIfThenMaybeElse(type: IrType, condition: IrExpression, fun IrBuilderWithScope.irIfNull(type: IrType, subject: IrExpression, thenPart: IrExpression, elsePart: IrExpression) = irIfThenElse(type, irEqualsNull(subject), thenPart, elsePart) -fun IrBuilderWithScope.irThrowNpe(origin: IrStatementOrigin) = +fun IrBuilderWithScope.irThrowNpe(origin: IrStatementOrigin? = null) = IrNullaryPrimitiveImpl(startOffset, endOffset, context.irBuiltIns.nothingType, origin, context.irBuiltIns.throwNpeSymbol) fun IrBuilderWithScope.irIfThenReturnTrue(condition: IrExpression) = @@ -165,7 +176,7 @@ fun IrBuilderWithScope.irEqualsNull(argument: IrExpression) = fun IrBuilderWithScope.irEquals(arg1: IrExpression, arg2: IrExpression) = primitiveOp2( - startOffset, endOffset, context.irBuiltIns.eqeqSymbol, IrStatementOrigin.EXCLEQ, + startOffset, endOffset, context.irBuiltIns.eqeqSymbol, IrStatementOrigin.EQEQ, arg1, arg2 ) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/Scope.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/Scope.kt index c5bd79e0319..45802b1275d 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/Scope.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/Scope.kt @@ -60,9 +60,10 @@ class Scope(val scopeOwnerSymbol: IrSymbol) { irExpression: IrExpression, nameHint: String? = null, isMutable: Boolean = false, + type: KotlinType? = null, origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_TEMPORARY_VARIABLE ): IrVariable { - val originalKotlinType = irExpression.type.originalKotlinType ?: irExpression.type.toKotlinType() + val originalKotlinType = type ?: (irExpression.type.originalKotlinType ?: irExpression.type.toKotlinType()) return IrVariableImpl( irExpression.startOffset, irExpression.endOffset, origin, createDescriptorForTemporaryVariable(