K2 Scripting: add support for script args to FIR part
This commit is contained in:
committed by
Space Team
parent
533a5671bc
commit
a89dfdce03
+25
-6
@@ -21,10 +21,7 @@ import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
||||
import org.jetbrains.kotlin.ir.builders.*
|
||||
import org.jetbrains.kotlin.ir.builders.declarations.*
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrAnonymousInitializerImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.SCRIPT_K2_ORIGIN
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.*
|
||||
import org.jetbrains.kotlin.ir.descriptors.toIrBasedKotlinType
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrClassReferenceImpl
|
||||
@@ -334,7 +331,15 @@ private class ScriptsToClassesLowering(val context: JvmBackendContext, val inner
|
||||
}.also { irConstructor ->
|
||||
irConstructor.valueParameters = buildList {
|
||||
addIfNotNull(irScript.earlierScriptsParameter)
|
||||
addAll(irScript.explicitCallParameters)
|
||||
addAll(irScript.explicitCallParameters.map {
|
||||
IrValueParameterImpl(
|
||||
it.startOffset, it.endOffset,
|
||||
IrDeclarationOrigin.SCRIPT_CALL_PARAMETER, IrValueParameterSymbolImpl(),
|
||||
it.name, index = 0,
|
||||
type = it.type, varargElementType = null,
|
||||
isCrossinline = false, isNoinline = false, isHidden = false, isAssignable = false
|
||||
).also { it.parent = irScript }
|
||||
})
|
||||
addAll(irScript.implicitReceiversParameters)
|
||||
addAll(irScript.providedPropertiesParameters)
|
||||
}
|
||||
@@ -806,7 +811,21 @@ private class ScriptToClassTransformer(
|
||||
}
|
||||
|
||||
override fun visitGetValue(expression: IrGetValue, data: ScriptToClassTransformerContext): IrExpression {
|
||||
if (irScript.needsReceiverProcessing) {
|
||||
val getVar = expression.symbol.owner as? IrVariable
|
||||
if (getVar != null) {
|
||||
if (irScript.explicitCallParameters.contains(getVar)) {
|
||||
val correspondingParam = irScriptClass.constructors.single().valueParameters.find {
|
||||
it.origin == IrDeclarationOrigin.SCRIPT_CALL_PARAMETER && it.name == getVar.name
|
||||
} ?: error("script explicit parameter ${getVar.name.asString()} not found")
|
||||
val newExpression =
|
||||
IrGetValueImpl(
|
||||
expression.startOffset, expression.endOffset,
|
||||
correspondingParam.type, correspondingParam.symbol,
|
||||
expression.origin
|
||||
)
|
||||
return super.visitExpression(newExpression, data)
|
||||
}
|
||||
} else if (irScript.needsReceiverProcessing) {
|
||||
val getValueParameter = expression.symbol.owner as? IrValueParameter
|
||||
if (getValueParameter != null && getValueParameter.name == SpecialNames.THIS) {
|
||||
val newExpression = getDispatchReceiverExpression(
|
||||
|
||||
+14
-3
@@ -15,12 +15,14 @@ import org.jetbrains.kotlin.ir.declarations.DescriptorMetadataSource
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
||||
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrSetFieldImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl
|
||||
import org.jetbrains.kotlin.ir.types.IrSimpleType
|
||||
import org.jetbrains.kotlin.ir.util.indexOrMinusOne
|
||||
import org.jetbrains.kotlin.ir.util.isCrossinline
|
||||
@@ -37,7 +39,6 @@ import org.jetbrains.kotlin.psi2ir.intermediate.createTemporaryVariableInBlock
|
||||
import org.jetbrains.kotlin.psi2ir.intermediate.setExplicitReceiverValue
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
|
||||
import org.jetbrains.kotlin.types.typeUtil.makeNullable
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
|
||||
class ScriptGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGeneratorExtension(declarationGenerator) {
|
||||
@@ -104,7 +105,17 @@ class ScriptGenerator(declarationGenerator: DeclarationGenerator) : DeclarationG
|
||||
irScript.earlierScriptsParameter = descriptor.earlierScriptsConstructorParameter?.let(::createValueParameter)
|
||||
}
|
||||
|
||||
irScript.explicitCallParameters = descriptor.explicitConstructorParameters.map(::createValueParameter)
|
||||
val explicitCallParams = descriptor.explicitConstructorParameters.map(::createValueParameter)
|
||||
|
||||
irScript.explicitCallParameters = descriptor.explicitConstructorParameters.map {
|
||||
IrVariableImpl(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||
IrDeclarationOrigin.SCRIPT_CALL_PARAMETER, IrVariableSymbolImpl(),
|
||||
it.name,
|
||||
it.type.toIrType(),
|
||||
isVar = false, isConst = false, isLateinit = false
|
||||
).also { it.parent = irScript }
|
||||
}
|
||||
|
||||
irScript.implicitReceiversParameters = descriptor.implicitReceivers.map {
|
||||
makeParameter(it.thisAsReceiverParameter, IrDeclarationOrigin.SCRIPT_IMPLICIT_RECEIVER, parametersIndex++)
|
||||
@@ -155,7 +166,7 @@ class ScriptGenerator(declarationGenerator: DeclarationGenerator) : DeclarationG
|
||||
}.also { irConstructor ->
|
||||
irConstructor.valueParameters = buildList {
|
||||
addIfNotNull(irScript.earlierScriptsParameter)
|
||||
addAll(irScript.explicitCallParameters)
|
||||
addAll(explicitCallParams)
|
||||
addAll(irScript.implicitReceiversParameters)
|
||||
addAll(irScript.providedPropertiesParameters)
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ abstract class IrScript : IrDeclarationBase(), IrDeclarationWithName, IrDeclarat
|
||||
|
||||
abstract var baseClass: IrType?
|
||||
|
||||
abstract var explicitCallParameters: List<IrValueParameter>
|
||||
abstract var explicitCallParameters: List<IrVariable>
|
||||
|
||||
abstract var implicitReceiversParameters: List<IrValueParameter>
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ class IrScriptImpl(
|
||||
override var thisReceiver: IrValueParameter? = null
|
||||
override var baseClass: IrType? = null
|
||||
|
||||
override lateinit var explicitCallParameters: List<IrValueParameter>
|
||||
override lateinit var explicitCallParameters: List<IrVariable>
|
||||
override lateinit var implicitReceiversParameters: List<IrValueParameter>
|
||||
override lateinit var providedProperties: List<IrPropertySymbol>
|
||||
override lateinit var providedPropertiesParameters: List<IrValueParameter>
|
||||
|
||||
@@ -400,7 +400,7 @@ object IrTree : AbstractTreeBuilder() {
|
||||
// TODO: consider removing from here and handle appropriately in the lowering
|
||||
+field("thisReceiver", valueParameter, mutable = true, isChild = true, nullable = true) // K1
|
||||
+field("baseClass", irTypeType, mutable = true, nullable = true) // K1
|
||||
+listField("explicitCallParameters", valueParameter, mutability = Var, isChild = true)
|
||||
+listField("explicitCallParameters", variable, mutability = Var, isChild = true)
|
||||
+listField("implicitReceiversParameters", valueParameter, mutability = Var, isChild = true)
|
||||
+listField("providedProperties", propertySymbolType, mutability = Var)
|
||||
+listField("providedPropertiesParameters", valueParameter, mutability = Var, isChild = true)
|
||||
|
||||
Reference in New Issue
Block a user