From e6cfd5d06fa568ec9d41ecfe37c15dc90461a9b1 Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Tue, 16 Feb 2021 12:07:18 -0800 Subject: [PATCH] FIR: move data class synthetic members' names --- .../generators/DataClassMembersGenerator.kt | 45 +++++++++---------- .../org/jetbrains/kotlin/fir/DefaultNames.kt | 6 +++ 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt index 78dbdd264c4..1cd31fcf5f2 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt @@ -6,6 +6,10 @@ package org.jetbrains.kotlin.fir.backend.generators import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.fir.COPY_NAME +import org.jetbrains.kotlin.fir.EQUALS_NAME +import org.jetbrains.kotlin.fir.HASHCODE_NAME +import org.jetbrains.kotlin.fir.TOSTRING_NAME import org.jetbrains.kotlin.fir.backend.Fir2IrComponents import org.jetbrains.kotlin.fir.backend.FirMetadataSource import org.jetbrains.kotlin.fir.backend.declareThisReceiverParameter @@ -41,8 +45,6 @@ import org.jetbrains.kotlin.ir.types.classifierOrNull import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.DataClassMembersGenerator import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.typeUtil.representativeUpperBound /** * A generator that generates synthetic members of data class as well as part of inline class. @@ -168,9 +170,9 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) { returnTypeRef.toIrType(components.typeConverter) == components.irBuiltIns.stringType private val FirSimpleFunction.matchesDataClassSyntheticMemberSignatures: Boolean - get() = (this.name == equalsName && matchesEqualsSignature) || - (this.name == hashCodeName && matchesHashCodeSignature) || - (this.name == toStringName && matchesToStringSignature) + get() = (this.name == EQUALS_NAME && matchesEqualsSignature) || + (this.name == HASHCODE_NAME && matchesHashCodeSignature) || + (this.name == TOSTRING_NAME && matchesToStringSignature) fun generate(klass: FirClass<*>): List { val propertyParametersCount = irClass.primaryConstructor?.explicitParameters?.size ?: 0 @@ -192,7 +194,7 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) { val contributedFunctionsInSupertypes = @OptIn(ExperimentalStdlibApi::class) buildMap { - for (name in listOf(equalsName, hashCodeName, toStringName)) { + for (name in listOf(EQUALS_NAME, HASHCODE_NAME, TOSTRING_NAME)) { klass.unsubstitutedScope( components.session, components.scopeSession, @@ -211,12 +213,12 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) { return declaration.modality != Modality.FINAL } - if (!contributedFunctionsInThisType.contains(equalsName) && - isOverridableDeclaration(equalsName) + if (!contributedFunctionsInThisType.contains(EQUALS_NAME) && + isOverridableDeclaration(EQUALS_NAME) ) { - result.add(contributedFunctionsInSupertypes.getValue(equalsName)) + result.add(contributedFunctionsInSupertypes.getValue(EQUALS_NAME)) val equalsFunction = createSyntheticIrFunction( - equalsName, + EQUALS_NAME, components.irBuiltIns.booleanType, otherParameterNeeded = true ) @@ -224,24 +226,24 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) { irClass.declarations.add(equalsFunction) } - if (!contributedFunctionsInThisType.contains(hashCodeName) && - isOverridableDeclaration(hashCodeName) + if (!contributedFunctionsInThisType.contains(HASHCODE_NAME) && + isOverridableDeclaration(HASHCODE_NAME) ) { - result.add(contributedFunctionsInSupertypes.getValue(hashCodeName)) + result.add(contributedFunctionsInSupertypes.getValue(HASHCODE_NAME)) val hashCodeFunction = createSyntheticIrFunction( - hashCodeName, + HASHCODE_NAME, components.irBuiltIns.intType, ) irDataClassMembersGenerator.generateHashCodeMethod(hashCodeFunction, properties) irClass.declarations.add(hashCodeFunction) } - if (!contributedFunctionsInThisType.contains(toStringName) && - isOverridableDeclaration(toStringName) + if (!contributedFunctionsInThisType.contains(TOSTRING_NAME) && + isOverridableDeclaration(TOSTRING_NAME) ) { - result.add(contributedFunctionsInSupertypes.getValue(toStringName)) + result.add(contributedFunctionsInSupertypes.getValue(TOSTRING_NAME)) val toStringFunction = createSyntheticIrFunction( - toStringName, + TOSTRING_NAME, components.irBuiltIns.stringType, ) irDataClassMembersGenerator.generateToStringMethod(toStringFunction, properties) @@ -335,13 +337,8 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) { } companion object { - private val copyName = Name.identifier("copy") - private val equalsName = Name.identifier("equals") - private val hashCodeName = Name.identifier("hashCode") - private val toStringName = Name.identifier("toString") - fun isCopy(irFunction: IrFunction): Boolean = - irFunction.name == copyName + irFunction.name == COPY_NAME fun isComponentN(irFunction: IrFunction): Boolean { if (irFunction.name.isSpecial) { diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/DefaultNames.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/DefaultNames.kt index 8d694fbc145..e345ee83951 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/DefaultNames.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/DefaultNames.kt @@ -10,3 +10,9 @@ import org.jetbrains.kotlin.name.Name val NAME_FOR_BACKING_FIELD = Name.identifier("field") val NAME_FOR_DEFAULT_VALUE_PARAMETER = Name.identifier("value") val CONSTRUCTOR_NAME = Name.special("") + +// Data class synthetic members +val COPY_NAME = Name.identifier("copy") +val EQUALS_NAME = Name.identifier("equals") +val HASHCODE_NAME = Name.identifier("hashCode") +val TOSTRING_NAME = Name.identifier("toString")