Add type operand symbol for type operator expression

This commit is contained in:
Dmitry Petrov
2018-02-22 12:39:04 +03:00
parent e89047d2cc
commit b206bf199f
65 changed files with 442 additions and 61 deletions
@@ -55,7 +55,7 @@ class Psi2IrTranslator(val configuration: Psi2IrConfiguration = Psi2IrConfigurat
}
private fun postprocess(context: GeneratorContext, irElement: IrElement) {
insertImplicitCasts(context.builtIns, irElement)
insertImplicitCasts(context.builtIns, irElement, context.symbolTable)
postprocessingSteps.forEach { it.postprocess(context, irElement) }
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.psi2ir.generators
import org.jetbrains.kotlin.backend.common.descriptors.substitute
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.declarations.*
@@ -33,6 +34,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import java.lang.AssertionError
@@ -94,18 +96,17 @@ class ClassGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGe
}
private fun generateMembersDeclaredInSupertypeList(irClass: IrClass, ktClassOrObject: KtClassOrObject) {
ktClassOrObject.getSuperTypeList()?.let { ktSuperTypeList ->
val delegatedMembers = irClass.descriptor.unsubstitutedMemberScope
.getContributedDescriptors(DescriptorKindFilter.CALLABLES)
.filterIsInstance<CallableMemberDescriptor>()
.filter { it.kind == CallableMemberDescriptor.Kind.DELEGATION }
.sortedWith(StableDescriptorsComparator)
if (delegatedMembers.isEmpty()) return
val ktSuperTypeList = ktClassOrObject.getSuperTypeList() ?: return
val delegatedMembers = irClass.descriptor.unsubstitutedMemberScope
.getContributedDescriptors(DescriptorKindFilter.CALLABLES)
.filterIsInstance<CallableMemberDescriptor>()
.filter { it.kind == CallableMemberDescriptor.Kind.DELEGATION }
.sortedWith(StableDescriptorsComparator)
if (delegatedMembers.isEmpty()) return
for (ktEntry in ktSuperTypeList.entries) {
if (ktEntry is KtDelegatedSuperTypeEntry) {
generateDelegatedImplementationMembers(irClass, ktEntry, delegatedMembers)
}
for (ktEntry in ktSuperTypeList.entries) {
if (ktEntry is KtDelegatedSuperTypeEntry) {
generateDelegatedImplementationMembers(irClass, ktEntry, delegatedMembers)
}
}
}
@@ -139,8 +140,10 @@ class ClassGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGe
}
private fun generateDelegatedMember(
irClass: IrClass, irDelegate: IrField,
delegatedMember: CallableMemberDescriptor, overriddenMember: CallableMemberDescriptor
irClass: IrClass,
irDelegate: IrField,
delegatedMember: CallableMemberDescriptor,
overriddenMember: CallableMemberDescriptor
) {
when (delegatedMember) {
is FunctionDescriptor ->
@@ -198,15 +201,22 @@ class ClassGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGe
}
private fun generateDelegateFunctionBody(
irDelegate: IrField, delegated: FunctionDescriptor, overridden: FunctionDescriptor,
irDelegate: IrField,
delegated: FunctionDescriptor,
overridden: FunctionDescriptor,
irDelegatedFunction: IrSimpleFunction
): IrBlockBodyImpl {
val startOffset = irDelegate.startOffset
val endOffset = irDelegate.endOffset
val irBlockBody = IrBlockBodyImpl(startOffset, endOffset)
val returnType = overridden.returnType!!
val irCall =
IrCallImpl(startOffset, endOffset, returnType, context.symbolTable.referenceFunction(overridden.original), overridden, null)
val substitutedOverridden = substituteOverriddenDescriptorForDelegate(delegated, overridden)
val returnType = substitutedOverridden.returnType!!
val irCall = IrCallImpl(
startOffset, endOffset, returnType,
context.symbolTable.referenceFunction(overridden.original),
substitutedOverridden,
null
)
irCall.dispatchReceiver =
IrGetFieldImpl(
startOffset, endOffset, irDelegate.symbol,
@@ -230,6 +240,20 @@ class ClassGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGe
return irBlockBody
}
private fun substituteOverriddenDescriptorForDelegate(
delegated: FunctionDescriptor,
overridden: FunctionDescriptor
): FunctionDescriptor {
// TODO PropertyAccessorDescriptor doesn't support 'substitute' right now :(
if (overridden is PropertyAccessorDescriptor) return overridden
val typeArguments = HashMap<TypeParameterDescriptor, KotlinType>()
for ((i, overriddenTypeParameter) in overridden.typeParameters.withIndex()) {
typeArguments[overriddenTypeParameter] = delegated.typeParameters[i].defaultType
}
return overridden.substitute(typeArguments)
}
private fun generateAdditionalMembersForDataClass(irClass: IrClass, ktClassOrObject: KtClassOrObject) {
DataClassMembersGenerator(declarationGenerator).generate(ktClassOrObject, irClass)
}
@@ -135,8 +135,8 @@ class DataClassMembersGenerator(declarationGenerator: DeclarationGenerator) : De
override fun generateEqualsMethod(function: FunctionDescriptor, properties: List<PropertyDescriptor>) {
buildMember(function, declaration) {
+irIfThenReturnTrue(irEqeqeq(irThis(), irOther()))
+irIfThenReturnFalse(irNotIs(irOther(), classDescriptor.defaultType))
val otherWithCast = irTemporary(irAs(irOther(), classDescriptor.defaultType), "other_with_cast")
+irIfThenReturnFalse(irNotIs(irOther(), classDescriptor.defaultType, irClass.symbol))
val otherWithCast = irTemporary(irAs(irOther(), classDescriptor.defaultType, irClass.symbol), "other_with_cast")
for (property in properties) {
val arg1 = irGet(irThis(), getPropertyGetterSymbol(property))
val arg2 = irGet(irGet(otherWithCast.symbol), getPropertyGetterSymbol(property))
@@ -92,7 +92,8 @@ class OperatorExpressionGenerator(statementGenerator: StatementGenerator) : Stat
return IrTypeOperatorCallImpl(
expression.startOffset, expression.endOffset, resultType, irOperator, rhsType,
expression.left.genExpr()
expression.left.genExpr(),
context.symbolTable.referenceClassifier(rhsType.constructor.declarationDescriptor!!)
)
}
@@ -103,7 +104,8 @@ class OperatorExpressionGenerator(statementGenerator: StatementGenerator) : Stat
return IrTypeOperatorCallImpl(
expression.startOffset, expression.endOffset, context.builtIns.booleanType, irOperator,
againstType, expression.leftHandSide.genExpr()
againstType, expression.leftHandSide.genExpr(),
context.symbolTable.referenceClassifier(againstType.constructor.declarationDescriptor!!)
)
}
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.psi2ir.containsNull
import org.jetbrains.kotlin.types.KotlinType
@@ -33,11 +34,11 @@ import org.jetbrains.kotlin.types.isNullabilityFlexible
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
import org.jetbrains.kotlin.types.upperIfFlexible
fun insertImplicitCasts(builtIns: KotlinBuiltIns, element: IrElement) {
element.transformChildren(InsertImplicitCasts(builtIns), null)
fun insertImplicitCasts(builtIns: KotlinBuiltIns, element: IrElement, symbolTable: SymbolTable) {
element.transformChildren(InsertImplicitCasts(builtIns, symbolTable), null)
}
class InsertImplicitCasts(val builtIns: KotlinBuiltIns) : IrElementTransformerVoid() {
class InsertImplicitCasts(private val builtIns: KotlinBuiltIns, private val symbolTable: SymbolTable) : IrElementTransformerVoid() {
override fun visitCallableReference(expression: IrCallableReference): IrExpression =
expression.transformPostfix {
transformReceiverArguments()
@@ -175,31 +176,39 @@ class InsertImplicitCasts(val builtIns: KotlinBuiltIns) : IrElementTransformerVo
valueType.isNullabilityFlexible() && valueType.containsNull() && !expectedType.containsNull() -> {
val nonNullValueType = valueType.upperIfFlexible().makeNotNullable()
IrTypeOperatorCallImpl(
startOffset, endOffset, nonNullValueType,
IrTypeOperator.IMPLICIT_NOTNULL, nonNullValueType, this
).cast(expectedType)
implicitCast(nonNullValueType, IrTypeOperator.IMPLICIT_NOTNULL).cast(expectedType)
}
KotlinTypeChecker.DEFAULT.isSubtypeOf(valueType.makeNotNullable(), expectedType) ->
this
KotlinBuiltIns.isInt(valueType) && notNullableExpectedType.isBuiltInIntegerType() ->
IrTypeOperatorCallImpl(
startOffset, endOffset, notNullableExpectedType,
IrTypeOperator.IMPLICIT_INTEGER_COERCION, notNullableExpectedType, this
)
implicitCast(notNullableExpectedType, IrTypeOperator.IMPLICIT_INTEGER_COERCION)
KotlinTypeChecker.DEFAULT.isSubtypeOf(valueType, expectedType) ->
this
else -> {
val targetType = if (!valueType.containsNull()) notNullableExpectedType else expectedType
IrTypeOperatorCallImpl(
startOffset, endOffset, targetType,
IrTypeOperator.IMPLICIT_CAST, targetType, this
)
implicitCast(targetType, IrTypeOperator.IMPLICIT_CAST)
}
}
}
private fun IrExpression.implicitCast(
targetType: KotlinType,
typeOperator: IrTypeOperator
): IrExpression {
val typeDescriptor = targetType.constructor.declarationDescriptor
?: throw AssertionError("No declaration for target type: $targetType")
return IrTypeOperatorCallImpl(
startOffset, endOffset,
targetType, typeOperator, targetType, this,
symbolTable.referenceClassifier(typeDescriptor)
)
}
private fun IrExpression.coerceToUnit(): IrExpression {
val valueType = this.type
@@ -208,7 +217,8 @@ class InsertImplicitCasts(val builtIns: KotlinBuiltIns) : IrElementTransformerVo
else
IrTypeOperatorCallImpl(
startOffset, endOffset, builtIns.unitType,
IrTypeOperator.IMPLICIT_COERCION_TO_UNIT, builtIns.unitType, this
IrTypeOperator.IMPLICIT_COERCION_TO_UNIT, builtIns.unitType, this,
symbolTable.referenceClass(builtIns.unit)
)
}
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol
@@ -168,18 +169,42 @@ fun IrBuilderWithScope.irCallOp(
putValueArgument(0, argument)
}
@Deprecated("Creates unbound symbol")
fun IrBuilderWithScope.irIs(argument: IrExpression, type: KotlinType) =
IrTypeOperatorCallImpl(startOffset, endOffset, context.builtIns.booleanType, IrTypeOperator.INSTANCEOF, type, argument)
fun IrBuilderWithScope.irIs(argument: IrExpression, type: KotlinType, typeClassifier: IrClassifierSymbol) =
IrTypeOperatorCallImpl(startOffset, endOffset, context.builtIns.booleanType, IrTypeOperator.INSTANCEOF, type, argument, typeClassifier)
@Deprecated("Creates unbound symbol")
fun IrBuilderWithScope.irNotIs(argument: IrExpression, type: KotlinType) =
IrTypeOperatorCallImpl(startOffset, endOffset, context.builtIns.booleanType, IrTypeOperator.NOT_INSTANCEOF, type, argument)
fun IrBuilderWithScope.irNotIs(argument: IrExpression, type: KotlinType, typeClassifier: IrClassifierSymbol) =
IrTypeOperatorCallImpl(
startOffset, endOffset,
context.builtIns.booleanType,
IrTypeOperator.NOT_INSTANCEOF,
type, argument, typeClassifier
)
@Deprecated("Creates unbound symbol")
fun IrBuilderWithScope.irAs(argument: IrExpression, type: KotlinType) =
IrTypeOperatorCallImpl(startOffset, endOffset, type, IrTypeOperator.CAST, type, argument)
fun IrBuilderWithScope.irAs(argument: IrExpression, type: KotlinType, typeClassifier: IrClassifierSymbol) =
IrTypeOperatorCallImpl(startOffset, endOffset, type, IrTypeOperator.CAST, type, argument, typeClassifier)
@Deprecated("Creates unbound symbol")
fun IrBuilderWithScope.irImplicitCast(argument: IrExpression, type: KotlinType) =
IrTypeOperatorCallImpl(startOffset, endOffset, type, IrTypeOperator.IMPLICIT_CAST, type, argument)
fun IrBuilderWithScope.irImplicitCast(argument: IrExpression, type: KotlinType, typeClassifier: IrClassifierSymbol) =
IrTypeOperatorCallImpl(startOffset, endOffset, type, IrTypeOperator.IMPLICIT_CAST, type, argument, typeClassifier)
fun IrBuilderWithScope.irInt(value: Int) =
IrConstImpl.int(startOffset, endOffset, context.builtIns.intType, value)
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.ir.expressions
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.types.KotlinType
enum class IrTypeOperator {
@@ -33,5 +34,6 @@ interface IrTypeOperatorCall : IrExpression {
val operator: IrTypeOperator
var argument: IrExpression
val typeOperand: KotlinType
val typeOperandClassifier: IrClassifierSymbol
}
@@ -44,7 +44,7 @@ class IrClassReferenceImpl(
type: KotlinType,
descriptor: ClassifierDescriptor,
classType: KotlinType
) : this(startOffset, endOffset, type, createClassifierSymbolForClassReference(descriptor), classType)
) : this(startOffset, endOffset, type, createClassifierSymbol(descriptor), classType)
override val descriptor: ClassifierDescriptor get() = symbol.descriptor
@@ -52,7 +52,7 @@ class IrClassReferenceImpl(
visitor.visitClassReference(this, data)
}
internal fun createClassifierSymbolForClassReference(descriptor: ClassifierDescriptor): IrClassifierSymbol =
internal fun createClassifierSymbol(descriptor: ClassifierDescriptor): IrClassifierSymbol =
when (descriptor) {
is ClassDescriptor -> IrClassSymbolImpl(descriptor)
is TypeParameterDescriptor -> IrTypeParameterSymbolImpl(descriptor)
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.ir.expressions.impl
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
import org.jetbrains.kotlin.ir.expressions.IrTypeOperatorCall
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.types.KotlinType
@@ -30,6 +31,7 @@ class IrTypeOperatorCallImpl(
override val operator: IrTypeOperator,
override val typeOperand: KotlinType
) : IrExpressionBase(startOffset, endOffset, type), IrTypeOperatorCall {
@Deprecated("Creates unbound symbol")
constructor(
startOffset: Int,
endOffset: Int,
@@ -39,9 +41,28 @@ class IrTypeOperatorCallImpl(
argument: IrExpression
) : this(startOffset, endOffset, type, operator, typeOperand) {
this.argument = argument
val typeOperandDescriptor = typeOperand.constructor.declarationDescriptor
if (typeOperandDescriptor != null) {
this.typeOperandClassifier = createClassifierSymbol(typeOperandDescriptor)
}
}
constructor(
startOffset: Int,
endOffset: Int,
type: KotlinType,
operator: IrTypeOperator,
typeOperand: KotlinType,
argument: IrExpression,
typeOperandClassifier: IrClassifierSymbol
) : this(startOffset, endOffset, type, operator, typeOperand) {
this.argument = argument
this.typeOperandClassifier = typeOperandClassifier
}
override lateinit var argument: IrExpression
override lateinit var typeOperandClassifier: IrClassifierSymbol
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitTypeOperator(this, data)
@@ -506,7 +506,15 @@ open class DeepCopyIrTree : IrElementTransformerVoid() {
expression.type,
expression.operator,
expression.typeOperand,
expression.argument.transform()
expression.argument.transform(),
run {
val oldTypeDescriptor = expression.typeOperandClassifier.descriptor
val newTypeDescriptor = mapClassifierReference(oldTypeDescriptor)
if (newTypeDescriptor == oldTypeDescriptor)
expression.typeOperandClassifier
else
createUnboundClassifierSymbol(newTypeDescriptor)
}
)
override fun visitWhen(expression: IrWhen): IrWhen =
@@ -437,7 +437,8 @@ open class DeepCopyIrTreeWithSymbols(private val symbolRemapper: SymbolRemapper)
expression.type,
expression.operator,
expression.typeOperand,
expression.argument.transform()
expression.argument.transform(),
symbolRemapper.getReferencedClassifier(expression.typeOperandClassifier)
)
override fun visitWhen(expression: IrWhen): IrWhen =
@@ -16,12 +16,10 @@
package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.SourceManager
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.symbols.IrBindableSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
@@ -114,11 +112,16 @@ class DumpIrTreeVisitor(out: Appendable) : IrElementVisitor<Unit, String> {
}
}
private fun IrSymbol.renderDeclarationElementOrDescriptor() {
private fun IrSymbol.renderDeclarationElementOrDescriptor(label: String? = null) {
if (isBound)
owner.render()
else
printer.println("UNBOUND: ", DescriptorRenderer.COMPACT.render(descriptor))
owner.render(label)
else {
if (label != null) {
printer.println("$label: ", "UNBOUND: ", DescriptorRenderer.COMPACT.render(descriptor))
} else {
printer.println("UNBOUND: ", DescriptorRenderer.COMPACT.render(descriptor))
}
}
}
override fun visitConstructor(declaration: IrConstructor, data: String) {
@@ -227,13 +230,25 @@ class DumpIrTreeVisitor(out: Appendable) : IrElementVisitor<Unit, String> {
}
}
override fun visitTypeOperator(expression: IrTypeOperatorCall, data: String) {
expression.dumpLabeledElementWith(data) {
expression.typeOperandClassifier.renderDeclarationElementOrDescriptor("typeOperand")
expression.acceptChildren(this, "")
}
}
private inline fun IrElement.dumpLabeledElementWith(label: String, body: () -> Unit) {
printer.println(accept(elementRenderer, null).withLabel(label))
indented(body)
}
private fun IrElement.render() {
printer.println(accept(elementRenderer, null))
private fun IrElement.render(label: String? = null) {
if (label != null) {
printer.println("$label: ", accept(elementRenderer, null))
} else {
printer.println(accept(elementRenderer, null))
}
}
private fun IrElement.dumpLabeledSubTree(label: String) {
@@ -149,10 +149,10 @@ class SymbolTable {
private val fieldSymbolTable = FlatSymbolTable<PropertyDescriptor, IrField, IrFieldSymbol>()
private val simpleFunctionSymbolTable = FlatSymbolTable<FunctionDescriptor, IrSimpleFunction, IrSimpleFunctionSymbol>()
private val typeParameterSymbolTable = ScopedSymbolTable<TypeParameterDescriptor, IrTypeParameter, IrTypeParameterSymbol>()
private val typeParameterSymbolTable = FlatSymbolTable<TypeParameterDescriptor, IrTypeParameter, IrTypeParameterSymbol>()
private val valueParameterSymbolTable = ScopedSymbolTable<ParameterDescriptor, IrValueParameter, IrValueParameterSymbol>()
private val variableSymbolTable = ScopedSymbolTable<VariableDescriptor, IrVariable, IrVariableSymbol>()
private val scopedSymbolTables = listOf(typeParameterSymbolTable, valueParameterSymbolTable, variableSymbolTable)
private val scopedSymbolTables = listOf(valueParameterSymbolTable, variableSymbolTable)
fun declareFile(fileEntry: SourceManager.FileEntry, packageFragmentDescriptor: PackageFragmentDescriptor): IrFile =
IrFileImpl(fileEntry, IrFileSymbolImpl(packageFragmentDescriptor))
@@ -256,7 +256,7 @@ class SymbolTable {
origin: IrDeclarationOrigin,
descriptor: TypeParameterDescriptor
): IrTypeParameter =
typeParameterSymbolTable.declareLocal(
typeParameterSymbolTable.declare(
descriptor,
{ IrTypeParameterSymbolImpl(descriptor) },
{ IrTypeParameterImpl(startOffset, endOffset, origin, it) }
@@ -342,7 +342,9 @@ class SymbolTable {
fun referenceClassifier(classifier: ClassifierDescriptor): IrClassifierSymbol =
when (classifier) {
is TypeParameterDescriptor ->
typeParameterSymbolTable.referenced(classifier) { throw AssertionError("Undefined type parameter referenced: $classifier") }
typeParameterSymbolTable.referenced(classifier) {
throw AssertionError("Undefined type parameter referenced: $classifier")
}
is ClassDescriptor ->
classSymbolTable.referenced(classifier) { IrClassSymbolImpl(classifier) }
else ->