diff --git a/compiler/fir/raw-fir/fir-common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt b/compiler/fir/raw-fir/fir-common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt index d45265091b5..7b0b0df1e92 100644 --- a/compiler/fir/raw-fir/fir-common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt +++ b/compiler/fir/raw-fir/fir-common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt @@ -36,7 +36,7 @@ import org.jetbrains.kotlin.util.OperatorNameConventions //T can be either PsiElement, or LighterASTNode abstract class BaseFirBuilder(val baseSession: FirSession, val context: Context = Context()) { - abstract fun T.toFirSourceElement(kind: FirSourceElementKind = FirRealSourceElementKind): FirSourceElement + abstract fun T.toFirSourceElement(kind: FirFakeSourceElementKind? = null): FirSourceElement protected val implicitUnitType = baseSession.builtinTypes.unitType protected val implicitAnyType = baseSession.builtinTypes.anyType @@ -697,28 +697,32 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte private val session: FirSession, private val source: T, private val classBuilder: FirRegularClassBuilder, - private val primaryConstructor: FirConstructor, private val zippedParameters: List>, private val packageFqName: FqName, private val classFqName: FqName, + private val createClassTypeRefWithSourceKind: (FirFakeSourceElementKind) -> FirTypeRef, + private val createParameterTypeRefWithSourceKind: (FirProperty, FirFakeSourceElementKind) -> FirTypeRef, ) { - private val classTypeRef = primaryConstructor.returnTypeRef - fun generate() { generateComponentFunctions() generateCopyFunction() // Refer to (IR utils or FIR backend) DataClassMembersGenerator for generating equals, hashCode, and toString } - private fun generateComponentAccess(parameterSource: FirSourceElement?, firProperty: FirProperty) = + private fun generateComponentAccess( + parameterSource: FirSourceElement?, + firProperty: FirProperty, + classTypeRefWithCorrectSourceKind: FirTypeRef, + firPropertyReturnTypeRefWithCorrectSourceKind: FirTypeRef + ) = buildQualifiedAccessExpression { source = parameterSource - typeRef = firProperty.returnTypeRef + typeRef = firPropertyReturnTypeRefWithCorrectSourceKind dispatchReceiver = buildThisReceiverExpression { calleeReference = buildImplicitThisReference { boundSymbol = classBuilder.symbol } - typeRef = classTypeRef + typeRef = classTypeRefWithCorrectSourceKind } calleeReference = buildResolvedNamedReference { source = parameterSource @@ -755,7 +759,8 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte private fun generateCopyFunction() { classBuilder.addDeclaration( buildSimpleFunction { - source = this@DataClassMembersGenerator.source.toFirSourceElement() + val classTypeRef = createClassTypeRefWithSourceKind(FirFakeSourceElementKind.DataClassGeneratedMembers) + source = this@DataClassMembersGenerator.source.toFirSourceElement(FirFakeSourceElementKind.DataClassGeneratedMembers) session = this@DataClassMembersGenerator.session origin = FirDeclarationOrigin.Source returnTypeRef = classTypeRef @@ -764,7 +769,9 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte symbol = FirNamedFunctionSymbol(CallableId(packageFqName, classFqName, copyName)) for ((ktParameter, firProperty) in zippedParameters) { val propertyName = firProperty.name - val parameterSource = ktParameter?.toFirSourceElement() + val parameterSource = ktParameter?.toFirSourceElement(FirFakeSourceElementKind.DataClassGeneratedMembers) + val propertyReturnTypeRef = + createParameterTypeRefWithSourceKind(firProperty, FirFakeSourceElementKind.DataClassGeneratedMembers) valueParameters += buildValueParameter { source = parameterSource session = this@DataClassMembersGenerator.session @@ -772,7 +779,7 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte returnTypeRef = propertyReturnTypeRef name = propertyName symbol = FirVariableSymbol(propertyName) - defaultValue = generateComponentAccess(parameterSource, firProperty) + defaultValue = generateComponentAccess(parameterSource, firProperty, propertyReturnTypeRef, classTypeRef) isCrossinline = false isNoinline = false isVararg = false @@ -790,4 +797,14 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte resolvedSymbol = this@toQualifiedAccess.symbol } } + + protected inline fun withDefaultSourceElementKind(newDefault: FirSourceElementKind, action: () -> R): R { + val currentForced = context.forcedElementSourceKind + context.forcedElementSourceKind = newDefault + try { + return action() + } finally { + context.forcedElementSourceKind = currentForced + } + } } diff --git a/compiler/fir/raw-fir/fir-common/src/org/jetbrains/kotlin/fir/builder/Context.kt b/compiler/fir/raw-fir/fir-common/src/org/jetbrains/kotlin/fir/builder/Context.kt index 34a3ccdbc7c..12e146bc4ea 100644 --- a/compiler/fir/raw-fir/fir-common/src/org/jetbrains/kotlin/fir/builder/Context.kt +++ b/compiler/fir/raw-fir/fir-common/src/org/jetbrains/kotlin/fir/builder/Context.kt @@ -6,10 +6,7 @@ package org.jetbrains.kotlin.fir.builder import kotlinx.collections.immutable.persistentListOf -import org.jetbrains.kotlin.fir.FirFakeSourceElementKind -import org.jetbrains.kotlin.fir.FirFunctionTarget -import org.jetbrains.kotlin.fir.FirLabel -import org.jetbrains.kotlin.fir.FirLoopTarget +import org.jetbrains.kotlin.fir.* import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.name.ClassId @@ -28,4 +25,6 @@ class Context { val firLoopTargets = mutableListOf() var capturedTypeParameters = persistentListOf() val arraySetArgument = mutableMapOf() + + var forcedElementSourceKind: FirSourceElementKind? = null } diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/BaseConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/BaseConverter.kt index f902e993034..fbeb1ad7224 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/BaseConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/BaseConverter.kt @@ -10,13 +10,10 @@ import com.intellij.openapi.util.Ref import com.intellij.psi.tree.IElementType import com.intellij.util.diff.FlyweightCapableTreeStructure import org.jetbrains.kotlin.KtNodeTypes -import org.jetbrains.kotlin.fir.FirLightSourceElement -import org.jetbrains.kotlin.fir.FirSession -import org.jetbrains.kotlin.fir.FirSourceElementKind +import org.jetbrains.kotlin.fir.* import org.jetbrains.kotlin.fir.builder.BaseFirBuilder import org.jetbrains.kotlin.fir.builder.Context import org.jetbrains.kotlin.fir.builder.escapedStringToCharacter -import org.jetbrains.kotlin.fir.toFirLightSourceElement import org.jetbrains.kotlin.fir.types.builder.buildImplicitTypeRef import org.jetbrains.kotlin.lexer.KtToken import org.jetbrains.kotlin.lexer.KtTokens.* @@ -31,7 +28,7 @@ open class BaseConverter( ) : BaseFirBuilder(baseSession, context) { protected val implicitType = buildImplicitTypeRef() - override fun LighterASTNode.toFirSourceElement(kind: FirSourceElementKind): FirLightSourceElement { + override fun LighterASTNode.toFirSourceElement(kind: FirFakeSourceElementKind?): FirLightSourceElement { val startOffset = offset + tree.getStartOffset(this) val endOffset = offset + tree.getEndOffset(this) return toFirLightSourceElement(startOffset, endOffset, tree) diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt index cd06871b4cc..738bf781345 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -474,10 +474,11 @@ class DeclarationsConverter( baseSession, classNode, this, - firPrimaryConstructor, zippedParameters, context.packageFqName, - context.className + context.className, + createClassTypeRefWithSourceKind = { firPrimaryConstructor.returnTypeRef }, + createParameterTypeRefWithSourceKind = { property, _ -> property.returnTypeRef }, ).generate() } diff --git a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt index dede28a0251..db99b0dc0cb 100644 --- a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt +++ b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt @@ -62,8 +62,9 @@ class RawFirBuilder( return reference.accept(Visitor(), Unit) as FirTypeRef } - override fun PsiElement.toFirSourceElement(kind: FirSourceElementKind): FirPsiSourceElement<*> { - return this.toFirPsiSourceElement(kind) + override fun PsiElement.toFirSourceElement(kind: FirFakeSourceElementKind?): FirPsiSourceElement<*> { + val actualKind = kind ?: this@RawFirBuilder.context.forcedElementSourceKind ?: FirRealSourceElementKind + return this.toFirPsiSourceElement(actualKind) } override val PsiElement.elementType: IElementType @@ -749,10 +750,17 @@ class RawFirBuilder( baseSession, classOrObject, this, - firPrimaryConstructor, zippedParameters, context.packageFqName, - context.className + context.className, + createClassTypeRefWithSourceKind = { firPrimaryConstructor.returnTypeRef.copyWithNewSourceKind(it) }, + createParameterTypeRefWithSourceKind = { property, newKind -> + // just making a shallow copy isn't enough type ref may be a function type ref + // and contain value parameters inside + withDefaultSourceElementKind(newKind) { + (property.returnTypeRef.psi as KtTypeReference).toFirOrImplicitType() + } + }, ).generate() } diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirComposedSuperTypeRefBuilder.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirComposedSuperTypeRefBuilder.kt index f1490862f28..281df8c7dab 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirComposedSuperTypeRefBuilder.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirComposedSuperTypeRefBuilder.kt @@ -43,3 +43,15 @@ inline fun buildComposedSuperTypeRef(init: FirComposedSuperTypeRefBuilder.() -> } return FirComposedSuperTypeRefBuilder().apply(init).build() } + +@OptIn(ExperimentalContracts::class) +inline fun buildComposedSuperTypeRefCopy(original: FirComposedSuperTypeRef, init: FirComposedSuperTypeRefBuilder.() -> Unit = {}): FirComposedSuperTypeRef { + contract { + callsInPlace(init, kotlin.contracts.InvocationKind.EXACTLY_ONCE) + } + val copyBuilder = FirComposedSuperTypeRefBuilder() + copyBuilder.source = original.source + copyBuilder.annotations.addAll(original.annotations) + copyBuilder.superTypeRefs.addAll(original.superTypeRefs) + return copyBuilder.apply(init).build() +} diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirDelegatedTypeRefBuilder.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirDelegatedTypeRefBuilder.kt index 6b8d5861da7..a981fc49715 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirDelegatedTypeRefBuilder.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirDelegatedTypeRefBuilder.kt @@ -41,3 +41,14 @@ inline fun buildDelegatedTypeRef(init: FirDelegatedTypeRefBuilder.() -> Unit): F } return FirDelegatedTypeRefBuilder().apply(init).build() } + +@OptIn(ExperimentalContracts::class) +inline fun buildDelegatedTypeRefCopy(original: FirDelegatedTypeRef, init: FirDelegatedTypeRefBuilder.() -> Unit): FirDelegatedTypeRef { + contract { + callsInPlace(init, kotlin.contracts.InvocationKind.EXACTLY_ONCE) + } + val copyBuilder = FirDelegatedTypeRefBuilder() + copyBuilder.delegate = original.delegate + copyBuilder.typeRef = original.typeRef + return copyBuilder.apply(init).build() +} diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirErrorTypeRefBuilder.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirErrorTypeRefBuilder.kt index c8bbd868b4d..a313065cb5f 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirErrorTypeRefBuilder.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirErrorTypeRefBuilder.kt @@ -47,3 +47,14 @@ inline fun buildErrorTypeRef(init: FirErrorTypeRefBuilder.() -> Unit): FirErrorT } return FirErrorTypeRefBuilder().apply(init).build() } + +@OptIn(ExperimentalContracts::class) +inline fun buildErrorTypeRefCopy(original: FirErrorTypeRef, init: FirErrorTypeRefBuilder.() -> Unit): FirErrorTypeRef { + contract { + callsInPlace(init, kotlin.contracts.InvocationKind.EXACTLY_ONCE) + } + val copyBuilder = FirErrorTypeRefBuilder() + copyBuilder.source = original.source + copyBuilder.diagnostic = original.diagnostic + return copyBuilder.apply(init).build() +} diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirFunctionTypeRefBuilder.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirFunctionTypeRefBuilder.kt index 720eaff7685..a930a5f4f76 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirFunctionTypeRefBuilder.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirFunctionTypeRefBuilder.kt @@ -53,3 +53,19 @@ inline fun buildFunctionTypeRef(init: FirFunctionTypeRefBuilder.() -> Unit): Fir } return FirFunctionTypeRefBuilder().apply(init).build() } + +@OptIn(ExperimentalContracts::class) +inline fun buildFunctionTypeRefCopy(original: FirFunctionTypeRef, init: FirFunctionTypeRefBuilder.() -> Unit): FirFunctionTypeRef { + contract { + callsInPlace(init, kotlin.contracts.InvocationKind.EXACTLY_ONCE) + } + val copyBuilder = FirFunctionTypeRefBuilder() + copyBuilder.source = original.source + copyBuilder.annotations.addAll(original.annotations) + copyBuilder.isMarkedNullable = original.isMarkedNullable + copyBuilder.receiverTypeRef = original.receiverTypeRef + copyBuilder.valueParameters.addAll(original.valueParameters) + copyBuilder.returnTypeRef = original.returnTypeRef + copyBuilder.isSuspend = original.isSuspend + return copyBuilder.apply(init).build() +} diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirImplicitTypeRefBuilder.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirImplicitTypeRefBuilder.kt index 9356544baf4..4d7864e6ea1 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirImplicitTypeRefBuilder.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirImplicitTypeRefBuilder.kt @@ -37,3 +37,13 @@ inline fun buildImplicitTypeRef(init: FirImplicitTypeRefBuilder.() -> Unit = {}) } return FirImplicitTypeRefBuilder().apply(init).build() } + +@OptIn(ExperimentalContracts::class) +inline fun buildImplicitTypeRefCopy(original: FirImplicitTypeRef, init: FirImplicitTypeRefBuilder.() -> Unit = {}): FirImplicitTypeRef { + contract { + callsInPlace(init, kotlin.contracts.InvocationKind.EXACTLY_ONCE) + } + val copyBuilder = FirImplicitTypeRefBuilder() + copyBuilder.source = original.source + return copyBuilder.apply(init).build() +} diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirResolvedFunctionTypeRefBuilder.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirResolvedFunctionTypeRefBuilder.kt index cf873986592..6c4247de18b 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirResolvedFunctionTypeRefBuilder.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirResolvedFunctionTypeRefBuilder.kt @@ -55,3 +55,20 @@ inline fun buildResolvedFunctionTypeRef(init: FirResolvedFunctionTypeRefBuilder. } return FirResolvedFunctionTypeRefBuilder().apply(init).build() } + +@OptIn(ExperimentalContracts::class) +inline fun buildResolvedFunctionTypeRefCopy(original: FirResolvedFunctionTypeRef, init: FirResolvedFunctionTypeRefBuilder.() -> Unit): FirResolvedFunctionTypeRef { + contract { + callsInPlace(init, kotlin.contracts.InvocationKind.EXACTLY_ONCE) + } + val copyBuilder = FirResolvedFunctionTypeRefBuilder() + copyBuilder.source = original.source + copyBuilder.annotations.addAll(original.annotations) + copyBuilder.type = original.type + copyBuilder.isSuspend = original.isSuspend + copyBuilder.isMarkedNullable = original.isMarkedNullable + copyBuilder.receiverTypeRef = original.receiverTypeRef + copyBuilder.valueParameters.addAll(original.valueParameters) + copyBuilder.returnTypeRef = original.returnTypeRef + return copyBuilder.apply(init).build() +} diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirResolvedTypeRefBuilder.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirResolvedTypeRefBuilder.kt index d092d1e2c43..b700d1e2ddf 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirResolvedTypeRefBuilder.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/builder/FirResolvedTypeRefBuilder.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.fir.types.builder import kotlin.contracts.* +import org.jetbrains.kotlin.fir.FirImplementationDetail import org.jetbrains.kotlin.fir.FirSourceElement import org.jetbrains.kotlin.fir.builder.FirAnnotationContainerBuilder import org.jetbrains.kotlin.fir.builder.FirBuilderDsl @@ -29,6 +30,7 @@ class FirResolvedTypeRefBuilder : FirAnnotationContainerBuilder { var delegatedTypeRef: FirTypeRef? = null var isSuspend: Boolean = false + @OptIn(FirImplementationDetail::class) override fun build(): FirResolvedTypeRef { return FirResolvedTypeRefImpl( source, @@ -48,3 +50,17 @@ inline fun buildResolvedTypeRef(init: FirResolvedTypeRefBuilder.() -> Unit): Fir } return FirResolvedTypeRefBuilder().apply(init).build() } + +@OptIn(ExperimentalContracts::class) +inline fun buildResolvedTypeRefCopy(original: FirResolvedTypeRef, init: FirResolvedTypeRefBuilder.() -> Unit): FirResolvedTypeRef { + contract { + callsInPlace(init, kotlin.contracts.InvocationKind.EXACTLY_ONCE) + } + val copyBuilder = FirResolvedTypeRefBuilder() + copyBuilder.source = original.source + copyBuilder.annotations.addAll(original.annotations) + copyBuilder.type = original.type + copyBuilder.delegatedTypeRef = original.delegatedTypeRef + copyBuilder.isSuspend = original.isSuspend + return copyBuilder.apply(init).build() +} diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/impl/FirResolvedTypeRefImpl.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/impl/FirResolvedTypeRefImpl.kt index 3eb501a63e6..8bdc2a33429 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/impl/FirResolvedTypeRefImpl.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/types/impl/FirResolvedTypeRefImpl.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.fir.types.impl +import org.jetbrains.kotlin.fir.FirImplementationDetail import org.jetbrains.kotlin.fir.FirSourceElement import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall import org.jetbrains.kotlin.fir.types.ConeKotlinType @@ -17,7 +18,7 @@ import org.jetbrains.kotlin.fir.visitors.* * DO NOT MODIFY IT MANUALLY */ -internal class FirResolvedTypeRefImpl( +class FirResolvedTypeRefImpl @FirImplementationDetail constructor( override val source: FirSourceElement?, override val annotations: MutableList, override val type: ConeKotlinType, diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt index 08b9f2b7f6e..8e73f024bc7 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -8,6 +8,10 @@ package org.jetbrains.kotlin.fir import org.jetbrains.kotlin.analyzer.ModuleInfo import org.jetbrains.kotlin.fir.expressions.FirBlock import org.jetbrains.kotlin.fir.expressions.FirExpression +import org.jetbrains.kotlin.fir.types.* +import org.jetbrains.kotlin.fir.types.builder.* +import org.jetbrains.kotlin.fir.types.impl.* +import org.jetbrains.kotlin.fir.types.impl.FirFunctionTypeRefImpl fun ModuleInfo.dependenciesWithoutSelf(): Sequence = dependencies().asSequence().filter { it != this } @@ -16,3 +20,41 @@ fun FirBlock.returnExpressions(): List = listOfNotNull(statements private val PUBLIC_METHOD_NAMES_IN_OBJECT = setOf("equals", "hashCode", "getClass", "wait", "notify", "notifyAll", "toString") +// do we need a deep copy here ? +fun R.copyWithNewSourceKind(newKind: FirFakeSourceElementKind): R { + if (source == null) return this + if (source?.kind == newKind) return this + if (this is FirDelegatedTypeRef) return this + val newSource = source?.withKind(newKind) + + @Suppress("UNCHECKED_CAST") + return when (val typeRef = this) { + is FirResolvedTypeRefImpl -> buildResolvedTypeRefCopy(typeRef) { + source = newSource + } + is FirErrorTypeRef -> buildErrorTypeRefCopy(typeRef) { + source = newSource + } + is FirUserTypeRefImpl -> buildUserTypeRef { + source = newSource + isMarkedNullable = typeRef.isMarkedNullable + qualifier += typeRef.qualifier + annotations += typeRef.annotations + } + is FirResolvedFunctionTypeRef -> buildResolvedFunctionTypeRefCopy(typeRef) { + source = newSource + } + is FirImplicitTypeRef -> buildImplicitTypeRefCopy(typeRef) { + source = newSource + } + is FirComposedSuperTypeRef -> buildComposedSuperTypeRefCopy(typeRef) { + source = newSource + } + is FirFunctionTypeRefImpl -> buildFunctionTypeRefCopy(typeRef) { + source = newSource + } + is FirImplicitBuiltinTypeRef -> typeRef.withFakeSource(newKind) + else -> TODO("Not implemented for ${typeRef::class}") + } as R +} + diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirImplicitBuiltinTypeRef.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirImplicitBuiltinTypeRef.kt index fe8bcc4e8fd..8b7fc34b16c 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirImplicitBuiltinTypeRef.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirImplicitBuiltinTypeRef.kt @@ -1,11 +1,12 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.fir.types.impl import org.jetbrains.kotlin.fir.FirElement +import org.jetbrains.kotlin.fir.FirFakeSourceElementKind import org.jetbrains.kotlin.fir.FirSourceElement import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall import org.jetbrains.kotlin.fir.symbols.StandardClassIds @@ -16,6 +17,7 @@ import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.visitors.FirTransformer import org.jetbrains.kotlin.fir.visitors.FirVisitor +import org.jetbrains.kotlin.fir.withKind import org.jetbrains.kotlin.name.ClassId sealed class FirImplicitBuiltinTypeRef( @@ -132,3 +134,55 @@ class FirImplicitKMutableProperty2TypeRef( source, StandardClassIds.KMutableProperty2, arrayOf(dispatchReceiverTypeArgument, extensionReceiverTypeArgument, propertyTypeArgument) ) + +fun FirImplicitBuiltinTypeRef.withFakeSource(kind: FirFakeSourceElementKind): FirImplicitBuiltinTypeRef { + val source = source ?: return this + if (source.kind == kind) return this + val newSource = source.withKind(kind) + return when (this) { + is FirImplicitUnitTypeRef -> FirImplicitUnitTypeRef(newSource) + is FirImplicitAnyTypeRef -> FirImplicitAnyTypeRef(newSource) + is FirImplicitNullableAnyTypeRef -> FirImplicitNullableAnyTypeRef(newSource) + is FirImplicitEnumTypeRef -> FirImplicitEnumTypeRef(newSource) + is FirImplicitAnnotationTypeRef -> FirImplicitAnnotationTypeRef(newSource) + is FirImplicitBooleanTypeRef -> FirImplicitBooleanTypeRef(newSource) + is FirImplicitIntTypeRef -> FirImplicitIntTypeRef(newSource) + is FirImplicitNothingTypeRef -> FirImplicitNothingTypeRef(newSource) + is FirImplicitNullableNothingTypeRef -> FirImplicitNullableNothingTypeRef(newSource) + is FirImplicitStringTypeRef -> FirImplicitStringTypeRef(newSource) + is FirImplicitKPropertyTypeRef -> FirImplicitKPropertyTypeRef( + newSource, + typeArgument = type.typeArguments[0] + ) + is FirImplicitKProperty0TypeRef -> FirImplicitKProperty0TypeRef( + newSource, + propertyTypeArgument = type.typeArguments[0] + ) + is FirImplicitKMutableProperty0TypeRef -> FirImplicitKMutableProperty0TypeRef( + newSource, + propertyTypeArgument = type.typeArguments[0] + ) + is FirImplicitKProperty1TypeRef -> FirImplicitKProperty1TypeRef( + newSource, + receiverTypeArgument = type.typeArguments[0], + propertyTypeArgument = type.typeArguments[1] + ) + is FirImplicitKMutableProperty1TypeRef -> FirImplicitKMutableProperty1TypeRef( + newSource, + receiverTypeArgument = type.typeArguments[0], + propertyTypeArgument = type.typeArguments[1] + ) + is FirImplicitKProperty2TypeRef -> FirImplicitKProperty2TypeRef( + newSource, + dispatchReceiverTypeArgument = type.typeArguments[0], + extensionReceiverTypeArgument = type.typeArguments[1], + propertyTypeArgument = type.typeArguments[2] + ) + is FirImplicitKMutableProperty2TypeRef -> FirImplicitKMutableProperty2TypeRef( + newSource, + dispatchReceiverTypeArgument = type.typeArguments[0], + extensionReceiverTypeArgument = type.typeArguments[1], + propertyTypeArgument = type.typeArguments[2] + ) + } +} \ No newline at end of file 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 ada2e367243..c68e565e808 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 @@ -222,6 +222,31 @@ object BuilderConfigurator : AbstractBuilderConfigurator(FirTree builder(resolvedTypeRef) { defaultFalse("isSuspend") defaultNull("delegatedTypeRef") + withCopy() + } + + builder(errorTypeRef) { + withCopy() + } + + builder(delegatedTypeRef) { + withCopy() + } + + builder(functionTypeRef) { + withCopy() + } + + builder(resolvedFunctionTypeRef) { + withCopy() + } + + builder(implicitTypeRef) { + withCopy() + } + + builder(composedSuperTypeRef) { + withCopy() } builder(breakExpression) { 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 12945acd2fe..b3b55cb9501 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 @@ -1,5 +1,5 @@ /* - * Copyright 2000-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -380,7 +380,9 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() kind = Object } - impl(resolvedTypeRef) + impl(resolvedTypeRef) { + publicImplementation() + } impl(errorExpression) { defaultEmptyList("annotations")