Add builder for variable declarations without initializers
This commit is contained in:
committed by
Alexander Udalov
parent
f450a8ed89
commit
f5b9eee83a
@@ -66,6 +66,16 @@ fun <T : IrElement> IrStatementsBuilder<T>.defineTemporary(value: IrExpression,
|
|||||||
return temporary.descriptor
|
return temporary.descriptor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T : IrElement> IrStatementsBuilder<T>.irTemporaryVarDeclaration(
|
||||||
|
type: IrType,
|
||||||
|
nameHint: String? = null,
|
||||||
|
isMutable: Boolean = true
|
||||||
|
): IrVariable {
|
||||||
|
val temporary = scope.createTemporaryVariableDeclaration(type, nameHint, isMutable = isMutable)
|
||||||
|
+temporary
|
||||||
|
return temporary
|
||||||
|
}
|
||||||
|
|
||||||
fun <T : IrElement> IrStatementsBuilder<T>.irTemporaryVar(
|
fun <T : IrElement> IrStatementsBuilder<T>.irTemporaryVar(
|
||||||
value: IrExpression,
|
value: IrExpression,
|
||||||
nameHint: String? = null,
|
nameHint: String? = null,
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.kotlin.ir.builders
|
package org.jetbrains.kotlin.ir.builders
|
||||||
|
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
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.IrDeclaration
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent
|
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"
|
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(
|
fun createTemporaryVariable(
|
||||||
irExpression: IrExpression,
|
irExpression: IrExpression,
|
||||||
nameHint: String? = null,
|
nameHint: String? = null,
|
||||||
@@ -73,16 +96,12 @@ class Scope(val scopeOwnerSymbol: IrSymbol) {
|
|||||||
irType: IrType? = null
|
irType: IrType? = null
|
||||||
): IrVariable {
|
): IrVariable {
|
||||||
val originalKotlinType = type ?: (irExpression.type.originalKotlinType ?: irExpression.type.toKotlinType())
|
val originalKotlinType = type ?: (irExpression.type.originalKotlinType ?: irExpression.type.toKotlinType())
|
||||||
return IrVariableImpl(
|
return createTemporaryVariableDeclaration(
|
||||||
irExpression.startOffset, irExpression.endOffset, origin,
|
|
||||||
createDescriptorForTemporaryVariable(
|
|
||||||
originalKotlinType,
|
|
||||||
nameHint, isMutable
|
|
||||||
),
|
|
||||||
irType ?: irExpression.type,
|
irType ?: irExpression.type,
|
||||||
irExpression
|
nameHint, isMutable, originalKotlinType,
|
||||||
|
origin, irExpression.startOffset, irExpression.endOffset
|
||||||
).apply {
|
).apply {
|
||||||
parent = getLocalDeclarationParent()
|
initializer = irExpression
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user