[FIR2IR] Use specific symbols for callable declarations
This commit is contained in:
+24
-34
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.fir.backend
|
|||||||
import org.jetbrains.kotlin.KtNodeTypes
|
import org.jetbrains.kotlin.KtNodeTypes
|
||||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns.*
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns.*
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
|
||||||
import org.jetbrains.kotlin.fir.declarations.*
|
import org.jetbrains.kotlin.fir.declarations.*
|
||||||
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyGetter
|
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyGetter
|
||||||
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertySetter
|
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertySetter
|
||||||
@@ -21,6 +20,9 @@ import org.jetbrains.kotlin.fir.expressions.impl.FirExpressionStub
|
|||||||
import org.jetbrains.kotlin.fir.render
|
import org.jetbrains.kotlin.fir.render
|
||||||
import org.jetbrains.kotlin.fir.resolve.*
|
import org.jetbrains.kotlin.fir.resolve.*
|
||||||
import org.jetbrains.kotlin.fir.scopes.impl.FirClassSubstitutionScope
|
import org.jetbrains.kotlin.fir.scopes.impl.FirClassSubstitutionScope
|
||||||
|
import org.jetbrains.kotlin.fir.symbols.Fir2IrConstructorSymbol
|
||||||
|
import org.jetbrains.kotlin.fir.symbols.Fir2IrPropertySymbol
|
||||||
|
import org.jetbrains.kotlin.fir.symbols.Fir2IrSimpleFunctionSymbol
|
||||||
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
|
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
|
||||||
import org.jetbrains.kotlin.fir.symbols.impl.*
|
import org.jetbrains.kotlin.fir.symbols.impl.*
|
||||||
import org.jetbrains.kotlin.fir.types.*
|
import org.jetbrains.kotlin.fir.types.*
|
||||||
@@ -402,7 +404,6 @@ class Fir2IrDeclarationStorage(
|
|||||||
|
|
||||||
private fun <T : IrFunction> T.bindAndDeclareParameters(
|
private fun <T : IrFunction> T.bindAndDeclareParameters(
|
||||||
function: FirFunction<*>?,
|
function: FirFunction<*>?,
|
||||||
descriptor: WrappedCallableDescriptor<T>,
|
|
||||||
irParent: IrDeclarationParent?,
|
irParent: IrDeclarationParent?,
|
||||||
thisReceiverOwner: IrClass? = irParent as? IrClass,
|
thisReceiverOwner: IrClass? = irParent as? IrClass,
|
||||||
isStatic: Boolean,
|
isStatic: Boolean,
|
||||||
@@ -411,7 +412,6 @@ class Fir2IrDeclarationStorage(
|
|||||||
if (irParent != null) {
|
if (irParent != null) {
|
||||||
parent = irParent
|
parent = irParent
|
||||||
}
|
}
|
||||||
descriptor.bind(this)
|
|
||||||
declareParameters(function, thisReceiverOwner, isStatic, parentPropertyReceiverType)
|
declareParameters(function, thisReceiverOwner, isStatic, parentPropertyReceiverType)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
@@ -438,8 +438,6 @@ class Fir2IrDeclarationStorage(
|
|||||||
origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED
|
origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED
|
||||||
): IrSimpleFunction {
|
): IrSimpleFunction {
|
||||||
val simpleFunction = function as? FirSimpleFunction
|
val simpleFunction = function as? FirSimpleFunction
|
||||||
val containerSource = simpleFunction?.containerSource
|
|
||||||
val descriptor = containerSource?.let { WrappedFunctionDescriptorWithContainerSource(it) } ?: WrappedSimpleFunctionDescriptor()
|
|
||||||
val isLambda = function.source?.elementType == KtNodeTypes.FUNCTION_LITERAL
|
val isLambda = function.source?.elementType == KtNodeTypes.FUNCTION_LITERAL
|
||||||
val updatedOrigin = when {
|
val updatedOrigin = when {
|
||||||
isLambda -> IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
|
isLambda -> IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
|
||||||
@@ -454,9 +452,9 @@ class Fir2IrDeclarationStorage(
|
|||||||
val isSuspend =
|
val isSuspend =
|
||||||
if (isLambda) ((function as FirAnonymousFunction).typeRef as? FirResolvedTypeRef)?.isSuspend == true
|
if (isLambda) ((function as FirAnonymousFunction).typeRef as? FirResolvedTypeRef)?.isSuspend == true
|
||||||
else simpleFunction?.isSuspend == true
|
else simpleFunction?.isSuspend == true
|
||||||
|
val signature = signatureComposer.composeSignature(function)
|
||||||
val created = function.convertWithOffsets { startOffset, endOffset ->
|
val created = function.convertWithOffsets { startOffset, endOffset ->
|
||||||
enterScope(descriptor)
|
val result = symbolTable.declareSimpleFunction(signature, { Fir2IrSimpleFunctionSymbol(signature) }) { symbol ->
|
||||||
val result = symbolTable.declareSimpleFunction(descriptor) { symbol ->
|
|
||||||
IrFunctionImpl(
|
IrFunctionImpl(
|
||||||
startOffset, endOffset, updatedOrigin, symbol,
|
startOffset, endOffset, updatedOrigin, symbol,
|
||||||
name, visibility,
|
name, visibility,
|
||||||
@@ -471,10 +469,15 @@ class Fir2IrDeclarationStorage(
|
|||||||
isOperator = simpleFunction?.isOperator == true
|
isOperator = simpleFunction?.isOperator == true
|
||||||
).apply {
|
).apply {
|
||||||
metadata = FirMetadataSource.Function(function, descriptor)
|
metadata = FirMetadataSource.Function(function, descriptor)
|
||||||
|
enterScope(descriptor)
|
||||||
|
bindAndDeclareParameters(
|
||||||
|
function, irParent,
|
||||||
|
thisReceiverOwner, isStatic = simpleFunction?.isStatic == true
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}.bindAndDeclareParameters(function, descriptor, irParent, thisReceiverOwner, isStatic = simpleFunction?.isStatic == true)
|
}
|
||||||
|
|
||||||
leaveScope(created.descriptor)
|
leaveScope(created.descriptor)
|
||||||
if (visibility == Visibilities.LOCAL) {
|
if (visibility == Visibilities.LOCAL) {
|
||||||
@@ -515,11 +518,11 @@ class Fir2IrDeclarationStorage(
|
|||||||
irParent: IrClass,
|
irParent: IrClass,
|
||||||
origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED
|
origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED
|
||||||
): IrConstructor {
|
): IrConstructor {
|
||||||
val descriptor = WrappedClassConstructorDescriptor()
|
|
||||||
val isPrimary = constructor.isPrimary
|
val isPrimary = constructor.isPrimary
|
||||||
classifierStorage.preCacheTypeParameters(constructor)
|
classifierStorage.preCacheTypeParameters(constructor)
|
||||||
|
val signature = signatureComposer.composeSignature(constructor)
|
||||||
val created = constructor.convertWithOffsets { startOffset, endOffset ->
|
val created = constructor.convertWithOffsets { startOffset, endOffset ->
|
||||||
symbolTable.declareConstructor(descriptor) { symbol ->
|
symbolTable.declareConstructor(signature, { Fir2IrConstructorSymbol(signature) }) { symbol ->
|
||||||
IrConstructorImpl(
|
IrConstructorImpl(
|
||||||
startOffset, endOffset, origin, symbol,
|
startOffset, endOffset, origin, symbol,
|
||||||
Name.special("<init>"), constructor.visibility,
|
Name.special("<init>"), constructor.visibility,
|
||||||
@@ -528,7 +531,7 @@ class Fir2IrDeclarationStorage(
|
|||||||
).apply {
|
).apply {
|
||||||
metadata = FirMetadataSource.Function(constructor, descriptor)
|
metadata = FirMetadataSource.Function(constructor, descriptor)
|
||||||
enterScope(descriptor)
|
enterScope(descriptor)
|
||||||
}.bindAndDeclareParameters(constructor, descriptor, irParent, isStatic = false).apply {
|
bindAndDeclareParameters(constructor, irParent, isStatic = false)
|
||||||
leaveScope(descriptor)
|
leaveScope(descriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -549,17 +552,9 @@ class Fir2IrDeclarationStorage(
|
|||||||
startOffset: Int,
|
startOffset: Int,
|
||||||
endOffset: Int
|
endOffset: Int
|
||||||
): IrSimpleFunction {
|
): IrSimpleFunction {
|
||||||
val propertyDescriptor = correspondingProperty.descriptor
|
|
||||||
val descriptor = when {
|
|
||||||
propertyDescriptor is WrappedPropertyDescriptorWithContainerSource ->
|
|
||||||
WrappedFunctionDescriptorWithContainerSource(propertyDescriptor.containerSource)
|
|
||||||
isSetter ->
|
|
||||||
WrappedPropertySetterDescriptor(Annotations.EMPTY, SourceElement.NO_SOURCE)
|
|
||||||
else ->
|
|
||||||
WrappedPropertyGetterDescriptor(Annotations.EMPTY, SourceElement.NO_SOURCE)
|
|
||||||
}
|
|
||||||
val prefix = if (isSetter) "set" else "get"
|
val prefix = if (isSetter) "set" else "get"
|
||||||
return symbolTable.declareSimpleFunction(descriptor) { symbol ->
|
val signature = signatureComposer.composeAccessorSignature(property, isSetter)
|
||||||
|
return symbolTable.declareSimpleFunction(signature, { Fir2IrSimpleFunctionSymbol(signature) }) { symbol ->
|
||||||
val accessorReturnType = if (isSetter) irBuiltIns.unitType else propertyType
|
val accessorReturnType = if (isSetter) irBuiltIns.unitType else propertyType
|
||||||
IrFunctionImpl(
|
IrFunctionImpl(
|
||||||
startOffset, endOffset, origin, symbol,
|
startOffset, endOffset, origin, symbol,
|
||||||
@@ -587,10 +582,10 @@ class Fir2IrDeclarationStorage(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
enterScope(descriptor)
|
enterScope(descriptor)
|
||||||
}.bindAndDeclareParameters(
|
bindAndDeclareParameters(
|
||||||
propertyAccessor, descriptor, irParent, thisReceiverOwner, isStatic = irParent !is IrClass,
|
propertyAccessor, irParent,
|
||||||
parentPropertyReceiverType = property.receiverTypeRef
|
thisReceiverOwner, isStatic = irParent !is IrClass, parentPropertyReceiverType = property.receiverTypeRef
|
||||||
).apply {
|
)
|
||||||
leaveScope(descriptor)
|
leaveScope(descriptor)
|
||||||
if (irParent != null) {
|
if (irParent != null) {
|
||||||
parent = irParent
|
parent = irParent
|
||||||
@@ -648,15 +643,10 @@ class Fir2IrDeclarationStorage(
|
|||||||
thisReceiverOwner: IrClass? = irParent as? IrClass,
|
thisReceiverOwner: IrClass? = irParent as? IrClass,
|
||||||
origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED
|
origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED
|
||||||
): IrProperty {
|
): IrProperty {
|
||||||
val containerSource = property.containerSource
|
|
||||||
val descriptor = containerSource?.let { WrappedPropertyDescriptorWithContainerSource(it) } ?: WrappedPropertyDescriptor()
|
|
||||||
classifierStorage.preCacheTypeParameters(property)
|
classifierStorage.preCacheTypeParameters(property)
|
||||||
|
val signature = signatureComposer.composeSignature(property)
|
||||||
return property.convertWithOffsets { startOffset, endOffset ->
|
return property.convertWithOffsets { startOffset, endOffset ->
|
||||||
enterScope(descriptor)
|
val result = symbolTable.declareProperty(signature, { Fir2IrPropertySymbol(signature) }) { symbol ->
|
||||||
val result = symbolTable.declareProperty(
|
|
||||||
startOffset, endOffset,
|
|
||||||
origin, descriptor, property.delegate != null
|
|
||||||
) { symbol ->
|
|
||||||
IrPropertyImpl(
|
IrPropertyImpl(
|
||||||
startOffset, endOffset, origin, symbol,
|
startOffset, endOffset, origin, symbol,
|
||||||
property.name, property.visibility, property.modality!!,
|
property.name, property.visibility, property.modality!!,
|
||||||
@@ -670,7 +660,7 @@ class Fir2IrDeclarationStorage(
|
|||||||
isFakeOverride = origin == IrDeclarationOrigin.FAKE_OVERRIDE
|
isFakeOverride = origin == IrDeclarationOrigin.FAKE_OVERRIDE
|
||||||
).apply {
|
).apply {
|
||||||
metadata = FirMetadataSource.Variable(property, descriptor)
|
metadata = FirMetadataSource.Variable(property, descriptor)
|
||||||
descriptor.bind(this)
|
enterScope(descriptor)
|
||||||
if (irParent != null) {
|
if (irParent != null) {
|
||||||
parent = irParent
|
parent = irParent
|
||||||
}
|
}
|
||||||
@@ -728,9 +718,9 @@ class Fir2IrDeclarationStorage(
|
|||||||
startOffset, endOffset
|
startOffset, endOffset
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
leaveScope(descriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
leaveScope(descriptor)
|
|
||||||
propertyCache[property] = result
|
propertyCache[property] = result
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,6 +100,19 @@ open class SymbolTable(
|
|||||||
return createOwner(symbol)
|
return createOwner(symbol)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline fun declare(sig: IdSignature, createSymbol: () -> S, createOwner: (S) -> B): B {
|
||||||
|
val existing = get(sig)
|
||||||
|
val symbol = if (existing == null) {
|
||||||
|
createSymbol()
|
||||||
|
} else {
|
||||||
|
unboundSymbols.remove(existing)
|
||||||
|
existing
|
||||||
|
}
|
||||||
|
val result = createOwner(symbol)
|
||||||
|
set(symbol.descriptor, symbol)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
inline fun declareIfNotExists(d: D, createSymbol: () -> S, createOwner: (S) -> B): B {
|
inline fun declareIfNotExists(d: D, createSymbol: () -> S, createOwner: (S) -> B): B {
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
val d0 = d.original as D
|
val d0 = d.original as D
|
||||||
@@ -429,6 +442,18 @@ open class SymbolTable(
|
|||||||
constructorFactory
|
constructorFactory
|
||||||
)
|
)
|
||||||
|
|
||||||
|
fun declareConstructor(
|
||||||
|
sig: IdSignature,
|
||||||
|
symbolFactory: () -> IrConstructorSymbol,
|
||||||
|
constructorFactory: (IrConstructorSymbol) -> IrConstructor
|
||||||
|
): IrConstructor {
|
||||||
|
return constructorSymbolTable.declare(
|
||||||
|
sig,
|
||||||
|
symbolFactory,
|
||||||
|
constructorFactory
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fun declareConstructorIfNotExists(descriptor: ClassConstructorDescriptor, constructorFactory: (IrConstructorSymbol) -> IrConstructor): IrConstructor =
|
fun declareConstructorIfNotExists(descriptor: ClassConstructorDescriptor, constructorFactory: (IrConstructorSymbol) -> IrConstructor): IrConstructor =
|
||||||
constructorSymbolTable.declareIfNotExists(
|
constructorSymbolTable.declareIfNotExists(
|
||||||
descriptor,
|
descriptor,
|
||||||
@@ -608,6 +633,18 @@ open class SymbolTable(
|
|||||||
propertyFactory
|
propertyFactory
|
||||||
)
|
)
|
||||||
|
|
||||||
|
fun declareProperty(
|
||||||
|
sig: IdSignature,
|
||||||
|
symbolFactory: () -> IrPropertySymbol,
|
||||||
|
propertyFactory: (IrPropertySymbol) -> IrProperty
|
||||||
|
): IrProperty {
|
||||||
|
return propertySymbolTable.declare(
|
||||||
|
sig,
|
||||||
|
symbolFactory,
|
||||||
|
propertyFactory
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fun declarePropertyIfNotExists(descriptor: PropertyDescriptor, propertyFactory: (IrPropertySymbol) -> IrProperty): IrProperty =
|
fun declarePropertyIfNotExists(descriptor: PropertyDescriptor, propertyFactory: (IrPropertySymbol) -> IrProperty): IrProperty =
|
||||||
propertySymbolTable.declareIfNotExists(descriptor, { createPropertySymbol(descriptor) }, propertyFactory)
|
propertySymbolTable.declareIfNotExists(descriptor, { createPropertySymbol(descriptor) }, propertyFactory)
|
||||||
|
|
||||||
@@ -688,6 +725,18 @@ open class SymbolTable(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun declareSimpleFunction(
|
||||||
|
sig: IdSignature,
|
||||||
|
symbolFactory: () -> IrSimpleFunctionSymbol,
|
||||||
|
functionFactory: (IrSimpleFunctionSymbol) -> IrSimpleFunction
|
||||||
|
): IrSimpleFunction {
|
||||||
|
return simpleFunctionSymbolTable.declare(
|
||||||
|
sig,
|
||||||
|
symbolFactory,
|
||||||
|
functionFactory
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fun declareSimpleFunctionIfNotExists(
|
fun declareSimpleFunctionIfNotExists(
|
||||||
descriptor: FunctionDescriptor,
|
descriptor: FunctionDescriptor,
|
||||||
functionFactory: (IrSimpleFunctionSymbol) -> IrSimpleFunction
|
functionFactory: (IrSimpleFunctionSymbol) -> IrSimpleFunction
|
||||||
|
|||||||
Reference in New Issue
Block a user