From bec39a9258572affa30bf061348da2844db42916 Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Fri, 4 Aug 2023 10:37:06 +0200 Subject: [PATCH] [FIR] Change FIR structure by replacing FirExpression.typeRef with cone type #KT-59855 --- .../fir/tree/generator/BuilderConfigurator.kt | 39 ++--------- .../generator/ImplementationConfigurator.kt | 69 ++++++++++--------- .../fir/tree/generator/NodeConfigurator.kt | 8 +-- .../kotlin/fir/tree/generator/Types.kt | 4 ++ ...stractFirTreeImplementationConfigurator.kt | 9 +-- .../kotlin/fir/tree/generator/importables.kt | 2 + 6 files changed, 56 insertions(+), 75 deletions(-) diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/BuilderConfigurator.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/BuilderConfigurator.kt index e13ff2dd31f..cdcbb7ea60f 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/BuilderConfigurator.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/BuilderConfigurator.kt @@ -293,8 +293,9 @@ object BuilderConfigurator : AbstractBuilderConfigurator(FirTree default("inlineStatus", "InlineStatus.Unknown") default("contractDescription", "FirEmptyContractDescription") default("status", "FirResolvedDeclarationStatusImpl.DEFAULT_STATUS_FOR_STATUSLESS_DECLARATIONS") + default("typeRef", "FirImplicitTypeRefImplWithoutSource") withCopy() - useTypes(emptyContractDescriptionType, resolvedDeclarationStatusImport) + useTypes(emptyContractDescriptionType, resolvedDeclarationStatusImport, firImplicitTypeWithoutSourceType) } builder(propertyAccessor) { @@ -313,7 +314,6 @@ object BuilderConfigurator : AbstractBuilderConfigurator(FirTree builder(resolvedTypeRef) { defaultNull("delegatedTypeRef") - default("isFromStubType", "false") withCopy() } @@ -391,12 +391,12 @@ object BuilderConfigurator : AbstractBuilderConfigurator(FirTree builder(resolvedQualifier) { parents += abstractResolvedQualifierBuilder - defaultFalse("isNullableLHSForCallableReference", "isFullyQualified") + defaultFalse("isNullableLHSForCallableReference", "isFullyQualified", "canBeValue") } builder(errorResolvedQualifier) { parents += abstractResolvedQualifierBuilder - defaultFalse("isNullableLHSForCallableReference", "isFullyQualified") + defaultFalse("isNullableLHSForCallableReference", "isFullyQualified", "canBeValue") } // builder(safeCallExpression) { @@ -411,37 +411,6 @@ object BuilderConfigurator : AbstractBuilderConfigurator(FirTree // useTypes(whenExpressionType) // } - val elementsWithDefaultTypeRef = listOf( - thisReceiverExpression, - callableReferenceAccess, - propertyAccessExpression, - functionCall, - anonymousFunction, - whenExpression, - tryExpression, - checkNotNullCall, - resolvedQualifier, - resolvedReifiedParameterReference, - expression to "FirExpressionStub", - varargArgumentsExpression, - checkedSafeCallSubject, - safeCallExpression, - arrayLiteral, - classReferenceExpression, - getClassCall - ) - elementsWithDefaultTypeRef.forEach { - val (element, name) = when (it) { - is Pair<*, *> -> it.first as Element to it.second as String - is Element -> it to null - else -> throw IllegalArgumentException() - } - builder(element, name) { - default("typeRef", "FirImplicitTypeRefImplWithoutSource") - useTypes(firImplicitTypeWithoutSourceType) - } - } - noBuilder(constExpression) // ----------------------------------------------------------------------- diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/ImplementationConfigurator.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/ImplementationConfigurator.kt index 00b0c246d2b..177895f392b 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/ImplementationConfigurator.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/ImplementationConfigurator.kt @@ -85,10 +85,11 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() fun ImplementationContext.commonAnnotationConfig() { defaultEmptyList("annotations") - default("typeRef") { - value = "annotationTypeRef" + default("coneTypeOrNull") { + value = "annotationTypeRef.coneTypeOrNull" withGetter = true } + useTypes(coneTypeOrNullImport) } impl(annotation) { @@ -204,7 +205,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() } impl(expression, "FirElseIfTrueCondition") { - defaultTypeRefWithSource("FirImplicitBooleanTypeRef") + defaultBuiltInType("Boolean") useTypes(implicitBooleanTypeRefType) publicImplementation() } @@ -216,6 +217,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() defaultEmptyList("statements") defaultEmptyList("annotations") publicImplementation() + defaultNull("coneTypeOrNull") } impl(lazyBlock) { @@ -232,7 +234,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() value = error withGetter = true } - default("typeRef") { + default("coneTypeOrNull") { value = error withGetter = true } @@ -250,7 +252,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() impl(lazyExpression) { val error = """error("FirLazyExpression should be calculated before accessing")""" - default("typeRef") { + default("coneTypeOrNull") { value = error withGetter = true } @@ -305,7 +307,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() "getter", "setter", withGetter = true ) - default("returnTypeRef", "FirErrorTypeRefImpl(null, null, diagnostic, false)") + default("returnTypeRef", "FirErrorTypeRefImpl(null, null, diagnostic)") useTypes(errorTypeRefImplType) } @@ -326,7 +328,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() } impl(namedArgumentExpression) { - default("typeRef") { + default("coneTypeOrNull") { delegate = "expression" } } @@ -336,7 +338,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() value = "false" withGetter = true } - default("typeRef") { + default("coneTypeOrNull") { delegate = "expression" } } @@ -346,25 +348,27 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() value = "true" withGetter = true } - default("typeRef") { + default("coneTypeOrNull") { delegate = "expression" } } impl(comparisonExpression) { - default("typeRef", "FirImplicitBooleanTypeRef(null)") - useTypes(implicitBooleanTypeRefType) + default("coneTypeOrNull", "StandardClassIds.Boolean.constructClassLikeType()") + useTypes(standardClassIdsType, constructClassLikeTypeImport) } - impl(typeOperatorCall) + impl(typeOperatorCall) { + defaultFalse("argFromStubType") + } impl(assignmentOperatorStatement) impl(incrementDecrementExpression) impl(equalityOperatorCall) { - default("typeRef", "FirImplicitBooleanTypeRef(null)") - useTypes(implicitBooleanTypeRefType) + default("coneTypeOrNull", "StandardClassIds.Boolean.constructClassLikeType()") + useTypes(standardClassIdsType, constructClassLikeTypeImport) } impl(resolvedQualifier) { @@ -375,17 +379,17 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() impl(resolvedReifiedParameterReference) impl(returnExpression) { - defaultTypeRefWithSource("FirImplicitNothingTypeRef") + defaultBuiltInType("Nothing") useTypes(implicitNothingTypeRefType) } impl(stringConcatenationCall) { - defaultTypeRefWithSource("FirImplicitStringTypeRef") + defaultBuiltInType("String") useTypes(implicitStringTypeRefType) } impl(throwExpression) { - defaultTypeRefWithSource("FirImplicitNothingTypeRef") + defaultBuiltInType("Nothing") useTypes(implicitNothingTypeRefType) } @@ -394,7 +398,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() } impl(expression, "FirUnitExpression") { - defaultTypeRefWithSource("FirImplicitUnitTypeRef") + defaultBuiltInType("Unit") useTypes(implicitUnitTypeRefType) publicImplementation() } @@ -422,11 +426,12 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() } impl(whenSubjectExpression) { - default("typeRef") { - value = "whenRef.value.subject?.typeRef ?: org.jetbrains.kotlin.fir.types.impl.FirImplicitUnitTypeRef(source)" + default("coneTypeOrNull") { + value = "whenRef.value.subject?.coneTypeOrNull ?: StandardClassIds.Unit.constructClassLikeType()" withGetter = true } - useTypes(whenExpression) + useTypes(whenExpression, standardClassIdsType, constructClassLikeTypeImport) + useTypes(standardClassIdsType, constructClassLikeTypeImport) } impl(desugaredAssignmentValueReferenceExpression) { @@ -434,16 +439,16 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() } impl(wrappedDelegateExpression) { - default("typeRef") { + default("coneTypeOrNull") { delegate = "expression" } } impl(enumEntryDeserializedAccessExpression) { noSource() - default("typeRef") { - value = "buildResolvedTypeRef { type = enumClassId.toLookupTag().constructClassType(emptyArray(), false) }" - useTypes(buildResolvedTypeRefImport, toLookupTagImport, constructClassTypeImport) + default("coneTypeOrNull") { + value = "enumClassId.toLookupTag().constructClassType(emptyArray(), false)" + useTypes(toLookupTagImport, constructClassTypeImport) } } @@ -520,18 +525,18 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() } impl(errorExpression) { - default("typeRef", "FirErrorTypeRefImpl(source, null, ConeStubDiagnostic(diagnostic), false)") - useTypes(errorTypeRefImplType, coneStubDiagnosticType) + default("coneTypeOrNull", "ConeErrorType(ConeStubDiagnostic(diagnostic))") + useTypes(coneErrorTypeType, coneStubDiagnosticType) } impl(qualifiedErrorAccessExpression) { - default("typeRef", "FirErrorTypeRefImpl(source, null, ConeStubDiagnostic(diagnostic), false)") - useTypes(errorTypeRefImplType, coneStubDiagnosticType) + default("coneTypeOrNull", "ConeErrorType(ConeStubDiagnostic(diagnostic))") + useTypes(coneErrorTypeType, coneStubDiagnosticType) } impl(errorFunction) { defaultNull("receiverParameter", "body", withGetter = true) - default("returnTypeRef", "FirErrorTypeRefImpl(null, null, diagnostic, false)") + default("returnTypeRef", "FirErrorTypeRefImpl(null, null, diagnostic)") useTypes(errorTypeRefImplType) } @@ -553,12 +558,12 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() impl(fromMissingDependenciesNamedReference) impl(breakExpression) { - defaultTypeRefWithSource("FirImplicitNothingTypeRef") + defaultBuiltInType("Nothing") useTypes(implicitNothingTypeRefType) } impl(continueExpression) { - defaultTypeRefWithSource("FirImplicitNothingTypeRef") + defaultBuiltInType("Nothing") useTypes(implicitNothingTypeRefType) } diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt index 85abdba1ef5..704cae32904 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt @@ -132,7 +132,7 @@ object NodeConfigurator : AbstractFieldConfigurator(FirTreeBuild } expression.configure { - +typeRefField + +field("coneTypeOrNull", coneKotlinTypeType, nullable = true, withReplace = true) +annotations } @@ -146,7 +146,6 @@ object NodeConfigurator : AbstractFieldConfigurator(FirTreeBuild block.configure { +fieldList(statement).withTransform() - +typeRefField needTransformOtherChildren() } @@ -250,6 +249,7 @@ object NodeConfigurator : AbstractFieldConfigurator(FirTreeBuild typeOperatorCall.configure { +field("operation", operationType) +field("conversionTypeRef", typeRef).withTransform() + +booleanField("argFromStubType", withReplace = true) needTransformOtherChildren() } @@ -568,7 +568,6 @@ object NodeConfigurator : AbstractFieldConfigurator(FirTreeBuild } smartCastExpression.configure { - +typeRefField +field("originalExpression", expression, withReplace = true).withTransform() +field("typesFromSmartCast", "Collection", null, customType = coneKotlinTypeType) +field("smartcastType", typeRef) @@ -618,6 +617,7 @@ object NodeConfigurator : AbstractFieldConfigurator(FirTreeBuild +field("symbol", classLikeSymbolType, nullable = true) +booleanField("isNullableLHSForCallableReference", withReplace = true) +booleanField("resolvedToCompanionObject", withReplace = true) + +booleanField("canBeValue", withReplace = true) +booleanField("isFullyQualified") +fieldList("nonFatalDiagnostics", coneDiagnosticType, useMutableOrEmpty = true) +typeArguments.withTransform() @@ -695,6 +695,7 @@ object NodeConfigurator : AbstractFieldConfigurator(FirTreeBuild +field("boundSymbol", firBasedSymbolType, "*", nullable = true, withReplace = true) +intField("contextReceiverNumber", withReplace = true) +booleanField("isImplicit") + +field("diagnostic", coneDiagnosticType, nullable = true, withReplace = true) } typeRef.configure { @@ -704,7 +705,6 @@ object NodeConfigurator : AbstractFieldConfigurator(FirTreeBuild resolvedTypeRef.configure { +field("type", coneKotlinTypeType) +field("delegatedTypeRef", typeRef, nullable = true) - +booleanField("isFromStubType") } typeRefWithNullability.configure { diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/Types.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/Types.kt index bebf477106e..c6152cdca0d 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/Types.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/Types.kt @@ -16,11 +16,13 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget import org.jetbrains.kotlin.fir.tree.generator.context.generatedType import org.jetbrains.kotlin.fir.tree.generator.context.type import org.jetbrains.kotlin.fir.types.ConeClassLikeType +import org.jetbrains.kotlin.fir.types.ConeErrorType import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.ConeSimpleKotlinType import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.types.SmartcastStability import org.jetbrains.kotlin.types.Variance @@ -44,8 +46,10 @@ val classIdType = type(ClassId::class) val annotationUseSiteTargetType = type(AnnotationUseSiteTarget::class) val operationKindType = type("contracts.description", "LogicOperationKind") val coneKotlinTypeType = type(ConeKotlinType::class) +val coneErrorTypeType = type(ConeErrorType::class) val coneSimpleKotlinTypeType = type(ConeSimpleKotlinType::class) val coneClassLikeTypeType = type(ConeClassLikeType::class) +val standardClassIdsType = type(StandardClassIds::class) val whenRefType = generatedType("", "FirExpressionRef") val referenceToSimpleExpressionType = generatedType("", "FirExpressionRef") diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/context/AbstractFirTreeImplementationConfigurator.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/context/AbstractFirTreeImplementationConfigurator.kt index 490523f782f..9ff94b70844 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/context/AbstractFirTreeImplementationConfigurator.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/context/AbstractFirTreeImplementationConfigurator.kt @@ -5,9 +5,11 @@ package org.jetbrains.kotlin.fir.tree.generator.context +import org.jetbrains.kotlin.fir.tree.generator.constructClassLikeTypeImport import org.jetbrains.kotlin.fir.tree.generator.model.* import org.jetbrains.kotlin.fir.tree.generator.noReceiverExpressionType import org.jetbrains.kotlin.fir.tree.generator.printer.call +import org.jetbrains.kotlin.fir.tree.generator.standardClassIdsType abstract class AbstractFirTreeImplementationConfigurator { private val elementsWithImpl = mutableSetOf() @@ -119,10 +121,9 @@ abstract class AbstractFirTreeImplementationConfigurator { } } - fun defaultTypeRefWithSource(typeRefClass: String) { - default("typeRef", "$typeRefClass(source?.fakeElement(KtFakeSourceElementKind.ImplicitTypeRef))") - implementation.arbitraryImportables += ArbitraryImportable("org.jetbrains.kotlin", "KtFakeSourceElementKind") - implementation.arbitraryImportables += ArbitraryImportable("org.jetbrains.kotlin", "fakeElement") + fun defaultBuiltInType(type: String) { + default("coneTypeOrNull", "StandardClassIds.$type.constructClassLikeType()") + useTypes(standardClassIdsType, constructClassLikeTypeImport) } fun defaultTrue(field: String, withGetter: Boolean = false) { diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/importables.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/importables.kt index 273b397dba0..5e4b1a5467f 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/importables.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/importables.kt @@ -14,7 +14,9 @@ val resolvedDeclarationStatusImport = ArbitraryImportable("org.jetbrains.kotlin. val buildResolvedTypeRefImport = ArbitraryImportable("org.jetbrains.kotlin.fir.types.builder", "buildResolvedTypeRef") val constructClassTypeImport = ArbitraryImportable("org.jetbrains.kotlin.fir.types", "constructClassType") +val constructClassLikeTypeImport = ArbitraryImportable("org.jetbrains.kotlin.fir.types", "constructClassLikeType") val toLookupTagImport = ArbitraryImportable("org.jetbrains.kotlin.fir.types", "toLookupTag") +val coneTypeOrNullImport = ArbitraryImportable("org.jetbrains.kotlin.fir.types", "coneTypeOrNull") val fakeSourceElementKindImport = ArbitraryImportable("org.jetbrains.kotlin", "KtFakeSourceElementKind") val fakeElementImport = ArbitraryImportable("org.jetbrains.kotlin", "fakeElement")