Minor changes in preparation for IR deserialization.

This commit is contained in:
Alexander Gorshenev
2018-08-03 19:26:23 +03:00
committed by alexander-gorshenev
parent 01ab4965b1
commit cea32bd4e3
15 changed files with 141 additions and 53 deletions
@@ -12,7 +12,11 @@ import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.name.FqName
interface CommonBackendContext : BackendContext {
interface LoggingContext {
fun log(message: () -> String)
}
interface CommonBackendContext : BackendContext, LoggingContext {
override val ir: Ir<CommonBackendContext>
//TODO move to builtins
@@ -23,7 +27,5 @@ interface CommonBackendContext : BackendContext {
//TODO move to builtins
fun getInternalFunctions(name: String): List<FunctionDescriptor>
fun log(message: () -> String)
fun report(element: IrElement?, irFile: IrFile?, message: String, isError: Boolean)
}
@@ -21,14 +21,18 @@ import org.jetbrains.kotlin.backend.common.deepCopyWithVariables
import org.jetbrains.kotlin.backend.common.descriptors.WrappedClassConstructorDescriptor
import org.jetbrains.kotlin.backend.common.descriptors.WrappedTypeParameterDescriptor
import org.jetbrains.kotlin.backend.common.descriptors.WrappedValueParameterDescriptor
import org.jetbrains.kotlin.backend.common.descriptors.WrappedVariableDescriptor
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.builders.IrStatementsBuilder
import org.jetbrains.kotlin.ir.builders.Scope
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrConstructorImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrTypeParameterImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionReference
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl
@@ -334,4 +338,24 @@ val IrFunction.isStatic: Boolean
get() = parent is IrClass && dispatchReceiverParameter == null
val IrDeclaration.isTopLevel: Boolean
get() = parent is IrPackageFragment
get() = parent is IrPackageFragment
fun <T : IrElement> IrStatementsBuilder<T>.irTemporaryWithWrappedDescriptor(
value: IrExpression,
nameHint: String? = null): IrVariable {
val temporary = scope.createTemporaryVariableWithWrappedDescriptor(value, nameHint)
+temporary
return temporary
}
fun Scope.createTemporaryVariableWithWrappedDescriptor(
irExpression: IrExpression,
nameHint: String? = null,
isMutable: Boolean = false,
origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_TEMPORARY_VARIABLE): IrVariable {
return createTemporaryVariableWithGivenDescriptor(
irExpression, nameHint, isMutable, origin, WrappedVariableDescriptor()
).apply { (this.descriptor as WrappedVariableDescriptor).bind(this) }
}
@@ -31,7 +31,9 @@ import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrSymbolDeclaration
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrStringConcatenation
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.isNullableAny
import org.jetbrains.kotlin.ir.types.toIrType
import org.jetbrains.kotlin.ir.types.toKotlinType
import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.functions
@@ -55,9 +57,10 @@ private class StringConcatenationTransformer(val lower: StringConcatenationLower
private val buildersStack = mutableListOf<IrBuilderWithScope>()
private val context = lower.context
private val builtIns = context.builtIns
private val irBuiltIns = context.irBuiltIns
private val typesWithSpecialAppendFunction =
PrimitiveType.values().map { builtIns.getPrimitiveKotlinType(it) } + builtIns.stringType
PrimitiveType.values().map { builtIns.getPrimitiveKotlinType(it).toIrType()!! } + irBuiltIns.stringType
private val nameToString = Name.identifier("toString")
private val nameAppend = Name.identifier("append")
@@ -79,7 +82,7 @@ private class StringConcatenationTransformer(val lower: StringConcatenationLower
}
private val appendFunctions: Map<KotlinType, IrSimpleFunction?> =
private val appendFunctions: Map<IrType, IrSimpleFunction?> =
typesWithSpecialAppendFunction.map { type ->
type to stringBuilder.functions.toList().atMostOne {
it.name == nameAppend &&
@@ -88,7 +91,7 @@ private class StringConcatenationTransformer(val lower: StringConcatenationLower
}
}.toMap()
private fun typeToAppendFunction(type: KotlinType): IrSimpleFunction {
private fun typeToAppendFunction(type: IrType): IrSimpleFunction {
return appendFunctions[type] ?: defaultAppendFunction
}
@@ -100,7 +103,7 @@ private class StringConcatenationTransformer(val lower: StringConcatenationLower
return blockBuilder.irBlock(expression) {
val stringBuilderImpl = irTemporary(irCall(constructor))
expression.arguments.forEach { arg ->
val appendFunction = typeToAppendFunction(arg.type.toKotlinType())
val appendFunction = typeToAppendFunction(arg.type)
+irCall(appendFunction).apply {
dispatchReceiver = irGet(stringBuilderImpl)
putValueArgument(0, arg)
@@ -21,27 +21,23 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.DFS
val kotlinPackageFqn = FqName.fromSegments(listOf("kotlin"))
val kotlinReflectionPackageFqn = kotlinPackageFqn.child(Name.identifier("reflect"))
val kotlinReflectionPackageFqn = kotlinPackageFqn.child(Name.identifier("reflection"))
val kotlinCoroutinesPackageFqn = kotlinPackageFqn.child(Name.identifier("coroutines"))
fun IrType.isFunction(): Boolean {
fun IrType.isFunction() = this.isNameInPackage("Function", kotlinPackageFqn)
fun IrType.isKFunction() = this.isNameInPackage("KFunction", kotlinReflectionPackageFqn)
fun IrType.isSuspendFunction() = this.isNameInPackage("SuspendFunction", kotlinCoroutinesPackageFqn)
fun IrType.isNameInPackage(prefix: String, packageFqName: FqName): Boolean {
val classifier = classifierOrNull ?: return false
val name = classifier.descriptor.name.asString()
if (!name.startsWith("Function")) return false
if (!name.startsWith(prefix)) return false
val declaration = classifier.owner as IrDeclaration
val parent = declaration.parent as? IrPackageFragment ?: return false
return parent.fqName == kotlinPackageFqn
}
return parent.fqName == packageFqName
fun IrType.isKFunction(): Boolean {
val classifier = classifierOrNull ?: return false
val name = classifier.descriptor.name.asString()
if (!name.startsWith("KFunction")) return false
val declaration = classifier.owner as IrDeclaration
val parent = declaration.parent as? IrPackageFragment ?: return false
return parent.fqName == kotlinReflectionPackageFqn
}
@@ -88,4 +84,4 @@ fun IrType.isThrowable(): Boolean {
} else return false
}
fun IrType.isThrowableTypeOrSubtype() = DFS.ifAny(listOf(this), IrType::superTypes, IrType::isThrowable)
fun IrType.isThrowableTypeOrSubtype() = DFS.ifAny(listOf(this), IrType::superTypes, IrType::isThrowable)