Implemented KType, KCallable.returnType

This commit is contained in:
Igor Chevdar
2018-08-14 16:58:33 +03:00
parent 146eac0010
commit 29c03b258a
9 changed files with 168 additions and 35 deletions
@@ -353,6 +353,8 @@ internal class KonanSymbols(context: Context, val symbolTable: SymbolTable, val
val getObjectTypeInfo = internalFunction("getObjectTypeInfo")
val kClassImpl = internalClass("KClassImpl")
val kClassImplConstructor by lazy { kClassImpl.constructors.single() }
val kTypeImpl = internalClass("KTypeImpl")
val kTypeImplForGenerics = internalClass("KTypeImplForGenerics")
val listOfInternal = internalFunction("listOfInternal")
@@ -43,9 +43,7 @@ import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.types.*
@@ -169,18 +167,19 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass
private val kFunctionImplSymbol = context.ir.symbols.kFunctionImpl
private val kFunctionImplConstructorSymbol = kFunctionImplSymbol.constructors.single()
fun build(): BuiltFunctionReference {
val startOffset = functionReference.startOffset
val endOffset = functionReference.endOffset
val returnType = irFunction.returnType
val superTypes = mutableListOf(kFunctionImplSymbol.typeWith(returnType))
val superTypes = mutableListOf(kFunctionImplSymbol.typeWith(irFunction.returnType))
val numberOfParameters = unboundFunctionParameters.size
val functionIrClass = context.ir.symbols.kFunctions[numberOfParameters].owner
val functionParameterTypes = unboundFunctionParameters.map { it.type }
val functionClassTypeParameters = functionParameterTypes + returnType
val functionClassTypeParameters = functionParameterTypes + irFunction.returnType
superTypes += functionIrClass.symbol.typeWith(functionClassTypeParameters)
var suspendFunctionIrClass: IrClass? = null
@@ -256,8 +255,6 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass
private fun createConstructorBuilder()
= object : SymbolWithIrBuilder<IrConstructorSymbol, IrConstructor>() {
private val kFunctionImplConstructorSymbol = kFunctionImplSymbol.constructors.single()
override fun buildSymbol() = IrConstructorSymbolImpl(
ClassConstructorDescriptorImpl.create(
/* containingDeclaration = */ functionReferenceClassDescriptor,
@@ -278,7 +275,6 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass
override fun buildIr(): IrConstructor {
val symbols = this@CallableReferenceLowering.context.ir.symbols
val irBuiltIns = context.irBuiltIns
argumentToPropertiesMap = boundFunctionParameters.associate {
@@ -318,6 +314,7 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass
val needReceiver = boundFunctionParameters.singleOrNull()?.descriptor is ReceiverParameterDescriptor
val receiver = if (needReceiver) irGet(valueParameters.single()) else irNull()
putValueArgument(3, receiver)
putValueArgument(4, irKType(this@CallableReferenceLowering.context, irFunction.returnType))
}
+IrInstanceInitializerCallImpl(startOffset, endOffset, functionReferenceClass.symbol, irBuiltIns.unitType)
// Save all arguments to fields.
@@ -36,7 +36,10 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrLocalDelegatedPropertyReference
import org.jetbrains.kotlin.ir.expressions.IrPropertyReference
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.toKotlinType
import org.jetbrains.kotlin.ir.types.typeWith
@@ -271,10 +274,11 @@ internal class PropertyDelegationLowering(val context: KonanBackendContext) : Fi
isMutable = setterCallableReference != null)
val initializer = irCall(symbol.owner, constructorTypeArguments).apply {
putValueArgument(0, irString(propertyDescriptor.name.asString()))
putValueArgument(1, irKType(this@PropertyDelegationLowering.context, returnType))
if (getterCallableReference != null)
putValueArgument(1, getterCallableReference)
putValueArgument(2, getterCallableReference)
if (setterCallableReference != null)
putValueArgument(2, setterCallableReference)
putValueArgument(3, setterCallableReference)
}
+initializer
}
@@ -291,6 +295,7 @@ internal class PropertyDelegationLowering(val context: KonanBackendContext) : Fi
isMutable = false)
val initializer = irCall(symbol.owner, constructorTypeArguments).apply {
putValueArgument(0, irString(propertyDescriptor.name.asString()))
putValueArgument(1, irKType(this@PropertyDelegationLowering.context, propertyType))
}
return initializer
}
@@ -323,3 +328,26 @@ internal class PropertyDelegationLowering(val context: KonanBackendContext) : Fi
}
}
internal fun IrBuilderWithScope.irKType(context: KonanBackendContext, type: IrType): IrExpression {
val kTypeImplSymbol = context.ir.symbols.kTypeImpl
val kTypeImplForGenericsSymbol = context.ir.symbols.kTypeImplForGenerics
val kClassImplConstructorSymbol = context.ir.symbols.kClassImplConstructor
val kTypeImplConstructorSymbol = kTypeImplSymbol.constructors.single()
val kTypeImplForGenericsConstructorSymbol = kTypeImplForGenericsSymbol.constructors.single()
val getClassTypeInfoSymbol = context.ir.symbols.getClassTypeInfo
return if (type.classifierOrNull !is IrClassSymbol) {
// IrTypeParameterSymbol
irCall(kTypeImplForGenericsConstructorSymbol)
} else {
val returnKClass = irCall(kClassImplConstructorSymbol).apply {
putValueArgument(0, irCall(getClassTypeInfoSymbol, listOf(type)))
}
irCall(kTypeImplConstructorSymbol).apply {
putValueArgument(0, returnKClass)
putValueArgument(1, irBoolean(type.isMarkedNullable()))
}
}
}