psi2ir: translate types in StatementGenerator
TODO: DI
This commit is contained in:
+11
-2
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.ir.expressions.*
|
|||||||
import org.jetbrains.kotlin.ir.expressions.impl.*
|
import org.jetbrains.kotlin.ir.expressions.impl.*
|
||||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||||
import org.jetbrains.kotlin.ir.util.ConstantValueGenerator
|
import org.jetbrains.kotlin.ir.util.ConstantValueGenerator
|
||||||
|
import org.jetbrains.kotlin.ir.util.TypeTranslator
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
@@ -42,15 +43,23 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
|||||||
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
|
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
|
||||||
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant
|
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant
|
||||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||||
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils
|
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils
|
||||||
|
|
||||||
class StatementGenerator(
|
class StatementGenerator(
|
||||||
val bodyGenerator: BodyGenerator,
|
val bodyGenerator: BodyGenerator,
|
||||||
override val scope: Scope
|
override val scope: Scope
|
||||||
) : KtVisitor<IrStatement, Nothing?>(), GeneratorWithScope {
|
) : KtVisitor<IrStatement, Nothing?>(),
|
||||||
|
GeneratorWithScope {
|
||||||
|
|
||||||
override val context: GeneratorContext get() = bodyGenerator.context
|
override val context: GeneratorContext get() = bodyGenerator.context
|
||||||
|
|
||||||
val scopeOwner: DeclarationDescriptor get() = bodyGenerator.scopeOwner
|
val scopeOwner: DeclarationDescriptor get() = bodyGenerator.scopeOwner
|
||||||
|
|
||||||
|
private val typeTranslator = TypeTranslator(context.moduleDescriptor, context.symbolTable)
|
||||||
|
|
||||||
|
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
|
||||||
|
|
||||||
fun generateStatement(ktElement: KtElement): IrStatement =
|
fun generateStatement(ktElement: KtElement): IrStatement =
|
||||||
ktElement.genStmt()
|
ktElement.genStmt()
|
||||||
|
|
||||||
@@ -145,7 +154,7 @@ class StatementGenerator(
|
|||||||
if (isBlockBody) throw AssertionError("Use IrBlockBody and corresponding body generator to generate blocks as function bodies")
|
if (isBlockBody) throw AssertionError("Use IrBlockBody and corresponding body generator to generate blocks as function bodies")
|
||||||
|
|
||||||
val returnType = getInferredTypeWithImplicitCasts(expression) ?: context.builtIns.unitType
|
val returnType = getInferredTypeWithImplicitCasts(expression) ?: context.builtIns.unitType
|
||||||
val irBlock = IrBlockImpl(expression.startOffset, expression.endOffset, returnType)
|
val irBlock = IrBlockImpl(expression.startOffset, expression.endOffset, returnType.toIrType())
|
||||||
|
|
||||||
expression.statements.forEach {
|
expression.statements.forEach {
|
||||||
irBlock.statements.add(it.genStmt())
|
irBlock.statements.add(it.genStmt())
|
||||||
|
|||||||
Reference in New Issue
Block a user