[FIR2IR] Get rid of IrSymbol.owner usages in Fir2IrLazyClass

^KT-60924
This commit is contained in:
Dmitriy Novozhilov
2023-09-13 15:05:35 +03:00
committed by Space Team
parent 5dbc0f12d1
commit 4e08cafc78
2 changed files with 44 additions and 26 deletions
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
import org.jetbrains.kotlin.fir.expressions.impl.FirExpressionStub
import org.jetbrains.kotlin.fir.java.hasJvmFieldAnnotation
import org.jetbrains.kotlin.fir.lazy.Fir2IrLazyClass
import org.jetbrains.kotlin.fir.lazy.Fir2IrLazyConstructor
import org.jetbrains.kotlin.fir.lazy.Fir2IrLazyProperty
import org.jetbrains.kotlin.fir.references.toResolvedBaseSymbol
import org.jetbrains.kotlin.fir.resolve.calls.FirSimpleSyntheticPropertySymbol
@@ -92,7 +93,10 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
val updatedOrigin = when {
isLambda -> IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
function.symbol.callableId.isKFunctionInvoke() -> IrDeclarationOrigin.FAKE_OVERRIDE
simpleFunction?.isStatic == true && simpleFunction.name in Fir2IrDeclarationStorage.ENUM_SYNTHETIC_NAMES -> IrDeclarationOrigin.ENUM_CLASS_SPECIAL_MEMBER
!predefinedOrigin.isExternal && // we should preserve origin for external enums
simpleFunction?.isStatic == true &&
simpleFunction.name in Fir2IrDeclarationStorage.ENUM_SYNTHETIC_NAMES
-> IrDeclarationOrigin.ENUM_CLASS_SPECIAL_MEMBER
// Kotlin built-in class and Java originated method (Collection.forEach, etc.)
// It's necessary to understand that such methods do not belong to DefaultImpls but actually generated as default
@@ -175,6 +179,9 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
return created
}
private val IrDeclarationOrigin?.isExternal: Boolean
get() = (this == IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB || this == IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB)
// ------------------------------------ constructors ------------------------------------
private fun declareIrConstructor(signature: IdSignature?, factory: (IrConstructorSymbol) -> IrConstructor): IrConstructor {
@@ -196,6 +203,16 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
runUnless(isLocal || !configuration.linkViaSignatures) {
signatureComposer.composeSignature(constructor)
}
if (irParent is Fir2IrLazyClass && signature != null) {
val lazyConstructor = lazyDeclarationsGenerator.createIrLazyConstructor(constructor, signature, origin, irParent) as Fir2IrLazyConstructor
// Add to cache before generating parameters to prevent an infinite loop when an annotation value parameter is annotated
// with the annotation itself.
@OptIn(LeakedDeclarationCaches::class)
declarationStorage.cacheIrConstructor(constructor, lazyConstructor)
lazyConstructor.prepareTypeParameters()
return lazyConstructor
}
val visibility = if (irParent.isAnonymousObject) Visibilities.Public else constructor.visibility
return constructor.convertWithOffsets { startOffset, endOffset ->
declareIrConstructor(signature) { symbol ->
@@ -247,8 +264,14 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null,
): IrProperty = convertCatching(property) {
val origin =
if (property.isStatic && property.name in Fir2IrDeclarationStorage.ENUM_SYNTHETIC_NAMES) IrDeclarationOrigin.ENUM_CLASS_SPECIAL_MEMBER
else property.computeIrOrigin(predefinedOrigin)
when {
!predefinedOrigin.isExternal &&
property.isStatic &&
property.name in Fir2IrDeclarationStorage.ENUM_SYNTHETIC_NAMES
-> IrDeclarationOrigin.ENUM_CLASS_SPECIAL_MEMBER
else -> property.computeIrOrigin(predefinedOrigin)
}
// See similar comments in createIrFunction above
val signature =
runUnless(
@@ -778,8 +801,9 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
// may produce incorrect results for values that may be encountered outside annotations.
// Does not do anything if valueParameter.defaultValue is already FirExpressionStub.
forcedDefaultValueConversion: Boolean = false,
predefinedOrigin: IrDeclarationOrigin? = null
): IrValueParameter = convertCatching(valueParameter) {
val origin = valueParameter.computeIrOrigin()
val origin = valueParameter.computeIrOrigin(predefinedOrigin)
val type = valueParameter.returnTypeRef.toIrType(typeOrigin)
val irParameter = valueParameter.convertWithOffsets { startOffset, endOffset ->
irFactory.createValueParameter(
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.ir.declarations.lazy.IrMaybeDeserializedClass
import org.jetbrains.kotlin.ir.declarations.lazy.lazyVar
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbolInternals
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
@@ -155,9 +154,9 @@ class Fir2IrLazyClass(
// e.g. to avoid accessing un-enhanced Java declarations with FirJavaTypeRef etc. inside
val scope = fir.unsubstitutedScope()
scope.processDeclaredConstructors {
if (shouldBuildStub(it.fir)) {
@OptIn(IrSymbolInternals::class)
result += declarationStorage.getIrConstructorSymbol(it).owner
val constructor = it.fir
if (shouldBuildStub(constructor)) {
result += declarationStorage.getOrCreateIrConstructor(constructor, this, origin)
}
}
@@ -165,9 +164,7 @@ class Fir2IrLazyClass(
scope.processClassifiersByName(name) {
val declaration = it.fir as? FirRegularClass ?: return@processClassifiersByName
if (declaration.classId.outerClassId == fir.classId && shouldBuildStub(declaration)) {
val nestedSymbol = classifierStorage.getOrCreateIrClass(declaration.symbol).symbol
@OptIn(IrSymbolInternals::class)
result += nestedSymbol.owner
result += classifierStorage.getOrCreateIrClass(declaration.symbol)
}
}
}
@@ -175,8 +172,7 @@ class Fir2IrLazyClass(
if (fir.classKind == ClassKind.ENUM_CLASS) {
for (declaration in fir.declarations) {
if (declaration is FirEnumEntry && shouldBuildStub(declaration)) {
@OptIn(IrSymbolInternals::class)
result += declarationStorage.getIrValueSymbol(declaration.symbol).owner as IrDeclaration
result += classifierStorage.getOrCreateIrEnumEntry(declaration, this, origin)
}
}
}
@@ -186,24 +182,22 @@ class Fir2IrLazyClass(
fun addDeclarationsFromScope(scope: FirContainingNamesAwareScope?) {
if (scope == null) return
for (name in scope.getCallableNames()) {
scope.processFunctionsByName(name) {
if (it.isSubstitutionOrIntersectionOverride) return@processFunctionsByName
if (!shouldBuildStub(it.fir)) return@processFunctionsByName
if (it.isStatic || it.dispatchReceiverClassLookupTagOrNull() == ownerLookupTag) {
if (it.isAbstractMethodOfAny()) {
scope.processFunctionsByName(name) { symbol ->
if (symbol.isSubstitutionOrIntersectionOverride) return@processFunctionsByName
if (!shouldBuildStub(symbol.fir)) return@processFunctionsByName
if (symbol.isStatic || symbol.dispatchReceiverClassLookupTagOrNull() == ownerLookupTag) {
if (symbol.isAbstractMethodOfAny()) {
return@processFunctionsByName
}
@OptIn(IrSymbolInternals::class)
result += declarationStorage.getIrFunctionSymbol(it).owner
result += declarationStorage.getOrCreateIrFunction(symbol.fir, this, origin)
}
}
scope.processPropertiesByName(name) {
if (it.isSubstitutionOrIntersectionOverride) return@processPropertiesByName
if (!shouldBuildStub(it.fir)) return@processPropertiesByName
if (it is FirPropertySymbol && (it.isStatic || it.dispatchReceiverClassLookupTagOrNull() == ownerLookupTag)) {
scope.processPropertiesByName(name) { symbol ->
if (symbol.isSubstitutionOrIntersectionOverride) return@processPropertiesByName
if (!shouldBuildStub(symbol.fir)) return@processPropertiesByName
if (symbol is FirPropertySymbol && (symbol.isStatic || symbol.dispatchReceiverClassLookupTagOrNull() == ownerLookupTag)) {
result.addIfNotNull(
@OptIn(IrSymbolInternals::class)
declarationStorage.getIrPropertySymbol(it).owner as? IrDeclaration
declarationStorage.getOrCreateIrProperty(symbol.fir, this, origin)
)
}
}