From f5b9eee83aca38a7e1917e21ddf0add042edffeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Fri, 10 May 2019 11:14:22 +0200 Subject: [PATCH] Add builder for variable declarations without initializers --- .../kotlin/ir/builders/ExpressionHelpers.kt | 10 ++++++ .../org/jetbrains/kotlin/ir/builders/Scope.kt | 35 ++++++++++++++----- 2 files changed, 37 insertions(+), 8 deletions(-) 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 aa69094d3e0..a88265b787a 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 @@ -66,6 +66,16 @@ fun IrStatementsBuilder.defineTemporary(value: IrExpression, return temporary.descriptor } +fun IrStatementsBuilder.irTemporaryVarDeclaration( + type: IrType, + nameHint: String? = null, + isMutable: Boolean = true +): IrVariable { + val temporary = scope.createTemporaryVariableDeclaration(type, nameHint, isMutable = isMutable) + +temporary + return temporary +} + fun IrStatementsBuilder.irTemporaryVar( value: IrExpression, nameHint: String? = null, 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 617c65c03c8..84b4cad1b96 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 @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.ir.builders import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.declarations.IrDeclaration import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent @@ -64,6 +65,28 @@ class Scope(val scopeOwnerSymbol: IrSymbol) { return if (nameHint != null) "tmp${index}_$nameHint" else "tmp$index" } + fun createTemporaryVariableDeclaration( + irType: IrType, + nameHint: String? = null, + isMutable: Boolean = false, + type: KotlinType? = null, + origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_TEMPORARY_VARIABLE, + startOffset: Int = UNDEFINED_OFFSET, + endOffset: Int = UNDEFINED_OFFSET + ): IrVariable { + val originalKotlinType = type ?: irType.toKotlinType() + return IrVariableImpl( + startOffset, endOffset, origin, + createDescriptorForTemporaryVariable( + originalKotlinType, + nameHint, isMutable + ), + irType + ).apply { + parent = getLocalDeclarationParent() + } + } + fun createTemporaryVariable( irExpression: IrExpression, nameHint: String? = null, @@ -73,16 +96,12 @@ class Scope(val scopeOwnerSymbol: IrSymbol) { irType: IrType? = null ): IrVariable { val originalKotlinType = type ?: (irExpression.type.originalKotlinType ?: irExpression.type.toKotlinType()) - return IrVariableImpl( - irExpression.startOffset, irExpression.endOffset, origin, - createDescriptorForTemporaryVariable( - originalKotlinType, - nameHint, isMutable - ), + return createTemporaryVariableDeclaration( irType ?: irExpression.type, - irExpression + nameHint, isMutable, originalKotlinType, + origin, irExpression.startOffset, irExpression.endOffset ).apply { - parent = getLocalDeclarationParent() + initializer = irExpression } }