IR: add NameProvider
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
/*
|
||||||
|
* 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.ir.util
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||||
|
import org.jetbrains.kotlin.name.Name
|
||||||
|
|
||||||
|
interface NameProvider {
|
||||||
|
fun nameForDeclaration(descriptor: DeclarationDescriptor): Name
|
||||||
|
|
||||||
|
object DEFAULT : NameProvider {
|
||||||
|
override fun nameForDeclaration(descriptor: DeclarationDescriptor): Name = descriptor.name
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.ir.symbols.*
|
|||||||
import org.jetbrains.kotlin.ir.symbols.impl.*
|
import org.jetbrains.kotlin.ir.symbols.impl.*
|
||||||
import org.jetbrains.kotlin.ir.types.IrType
|
import org.jetbrains.kotlin.ir.types.IrType
|
||||||
import org.jetbrains.kotlin.ir.types.impl.IrUninitializedType
|
import org.jetbrains.kotlin.ir.types.impl.IrUninitializedType
|
||||||
|
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
|
||||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DescriptorWithContainerSource
|
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DescriptorWithContainerSource
|
||||||
|
|
||||||
interface IrProvider {
|
interface IrProvider {
|
||||||
@@ -86,7 +87,7 @@ interface ReferenceSymbolTable {
|
|||||||
fun leaveScope(owner: DeclarationDescriptor)
|
fun leaveScope(owner: DeclarationDescriptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTable {
|
open class SymbolTable(val signaturer: IdSignatureComposer, val nameProvider: NameProvider = NameProvider.DEFAULT) : ReferenceSymbolTable {
|
||||||
|
|
||||||
@Suppress("LeakingThis")
|
@Suppress("LeakingThis")
|
||||||
val lazyWrapper = IrLazySymbolTable(this)
|
val lazyWrapper = IrLazySymbolTable(this)
|
||||||
@@ -164,7 +165,8 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private open inner class FlatSymbolTable<D : DeclarationDescriptor, B : IrSymbolOwner, S : IrBindableSymbol<D, B>> : SymbolTableBase<D, B, S>() {
|
private open inner class FlatSymbolTable<D : DeclarationDescriptor, B : IrSymbolOwner, S : IrBindableSymbol<D, B>> :
|
||||||
|
SymbolTableBase<D, B, S>() {
|
||||||
val descriptorToSymbol = linkedMapOf<D, S>()
|
val descriptorToSymbol = linkedMapOf<D, S>()
|
||||||
val idSigToSymbol = linkedMapOf<IdSignature, S>()
|
val idSigToSymbol = linkedMapOf<IdSignature, S>()
|
||||||
|
|
||||||
@@ -353,7 +355,7 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
fun declareScript(
|
fun declareScript(
|
||||||
descriptor: ScriptDescriptor,
|
descriptor: ScriptDescriptor,
|
||||||
scriptFactory: (IrScriptSymbol) -> IrScript = { symbol: IrScriptSymbol ->
|
scriptFactory: (IrScriptSymbol) -> IrScript = { symbol: IrScriptSymbol ->
|
||||||
IrScriptImpl(symbol, descriptor.name)
|
IrScriptImpl(symbol, nameProvider.nameForDeclaration(descriptor))
|
||||||
}
|
}
|
||||||
): IrScript {
|
): IrScript {
|
||||||
return scriptSymbolTable.declare(
|
return scriptSymbolTable.declare(
|
||||||
@@ -371,7 +373,18 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassDescriptor,
|
startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassDescriptor,
|
||||||
modality: Modality = descriptor.modality, visibility: Visibility = descriptor.visibility,
|
modality: Modality = descriptor.modality, visibility: Visibility = descriptor.visibility,
|
||||||
classFactory: (IrClassSymbol) -> IrClass = {
|
classFactory: (IrClassSymbol) -> IrClass = {
|
||||||
IrClassImpl(startOffset, endOffset, origin, it, modality, visibility).apply { metadata = MetadataSource.Class(it.descriptor) }
|
IrClassImpl(
|
||||||
|
startOffset, endOffset, origin, it,
|
||||||
|
nameProvider.nameForDeclaration(descriptor), descriptor.kind,
|
||||||
|
visibility, modality,
|
||||||
|
isCompanion = descriptor.isCompanionObject,
|
||||||
|
isInner = descriptor.isInner,
|
||||||
|
isData = descriptor.isData,
|
||||||
|
isExternal = descriptor.isExternal,
|
||||||
|
isInline = descriptor.isInline,
|
||||||
|
isExpect = descriptor.isExpect,
|
||||||
|
isFun = descriptor.isFun
|
||||||
|
).apply { metadata = MetadataSource.Class(it.descriptor) }
|
||||||
}
|
}
|
||||||
): IrClass {
|
): IrClass {
|
||||||
return classSymbolTable.declare(
|
return classSymbolTable.declare(
|
||||||
@@ -414,7 +427,16 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
origin: IrDeclarationOrigin,
|
origin: IrDeclarationOrigin,
|
||||||
descriptor: ClassConstructorDescriptor,
|
descriptor: ClassConstructorDescriptor,
|
||||||
constructorFactory: (IrConstructorSymbol) -> IrConstructor = {
|
constructorFactory: (IrConstructorSymbol) -> IrConstructor = {
|
||||||
IrConstructorImpl(startOffset, endOffset, origin, it, IrUninitializedType).apply {
|
IrConstructorImpl(
|
||||||
|
startOffset, endOffset, origin, it,
|
||||||
|
nameProvider.nameForDeclaration(descriptor),
|
||||||
|
descriptor.visibility,
|
||||||
|
returnType = IrUninitializedType,
|
||||||
|
isInline = descriptor.isInline,
|
||||||
|
isExternal = descriptor.isEffectivelyExternal(),
|
||||||
|
isPrimary = descriptor.isPrimary,
|
||||||
|
isExpect = descriptor.isExpect
|
||||||
|
).apply {
|
||||||
metadata = MetadataSource.Function(it.descriptor)
|
metadata = MetadataSource.Function(it.descriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -457,7 +479,9 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
|
|
||||||
fun declareEnumEntry(
|
fun declareEnumEntry(
|
||||||
startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassDescriptor,
|
startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassDescriptor,
|
||||||
factory: (IrEnumEntrySymbol) -> IrEnumEntry = { IrEnumEntryImpl(startOffset, endOffset, origin, it) }
|
factory: (IrEnumEntrySymbol) -> IrEnumEntry = {
|
||||||
|
IrEnumEntryImpl(startOffset, endOffset, origin, it, nameProvider.nameForDeclaration(descriptor))
|
||||||
|
}
|
||||||
): IrEnumEntry =
|
): IrEnumEntry =
|
||||||
enumEntrySymbolTable.declare(
|
enumEntrySymbolTable.declare(
|
||||||
descriptor,
|
descriptor,
|
||||||
@@ -502,7 +526,15 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
type: IrType,
|
type: IrType,
|
||||||
visibility: Visibility? = null,
|
visibility: Visibility? = null,
|
||||||
fieldFactory: (IrFieldSymbol) -> IrField = {
|
fieldFactory: (IrFieldSymbol) -> IrField = {
|
||||||
IrFieldImpl(startOffset, endOffset, origin, it, type, visibility ?: it.descriptor.visibility).apply {
|
IrFieldImpl(
|
||||||
|
startOffset, endOffset, origin, it,
|
||||||
|
nameProvider.nameForDeclaration(descriptor),
|
||||||
|
type, visibility ?: it.descriptor.visibility,
|
||||||
|
isFinal = !descriptor.isVar,
|
||||||
|
isExternal = descriptor.isExternal,
|
||||||
|
isStatic = descriptor.dispatchReceiverParameter == null,
|
||||||
|
isFakeOverride = origin == IrDeclarationOrigin.FAKE_OVERRIDE
|
||||||
|
).apply {
|
||||||
metadata = MetadataSource.Property(it.descriptor)
|
metadata = MetadataSource.Property(it.descriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -563,7 +595,10 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
descriptor: PropertyDescriptor,
|
descriptor: PropertyDescriptor,
|
||||||
@Suppress("DEPRECATION") isDelegated: Boolean = descriptor.isDelegated,
|
@Suppress("DEPRECATION") isDelegated: Boolean = descriptor.isDelegated,
|
||||||
propertyFactory: (IrPropertySymbol) -> IrProperty = { symbol ->
|
propertyFactory: (IrPropertySymbol) -> IrProperty = { symbol ->
|
||||||
IrPropertyImpl(startOffset, endOffset, origin, symbol, isDelegated = isDelegated).apply {
|
IrPropertyImpl(
|
||||||
|
startOffset, endOffset, origin, symbol, isDelegated = isDelegated,
|
||||||
|
name = nameProvider.nameForDeclaration(descriptor)
|
||||||
|
).apply {
|
||||||
metadata = MetadataSource.Property(symbol.descriptor)
|
metadata = MetadataSource.Property(symbol.descriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -640,7 +675,20 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
origin: IrDeclarationOrigin,
|
origin: IrDeclarationOrigin,
|
||||||
descriptor: FunctionDescriptor,
|
descriptor: FunctionDescriptor,
|
||||||
functionFactory: (IrSimpleFunctionSymbol) -> IrSimpleFunction = {
|
functionFactory: (IrSimpleFunctionSymbol) -> IrSimpleFunction = {
|
||||||
IrFunctionImpl(startOffset, endOffset, origin, it, IrUninitializedType).apply {
|
IrFunctionImpl(
|
||||||
|
startOffset, endOffset, origin, it,
|
||||||
|
nameProvider.nameForDeclaration(descriptor),
|
||||||
|
returnType = IrUninitializedType,
|
||||||
|
visibility = descriptor.visibility,
|
||||||
|
modality = descriptor.modality,
|
||||||
|
isInline = descriptor.isInline,
|
||||||
|
isExternal = descriptor.isExternal,
|
||||||
|
isTailrec = descriptor.isTailrec,
|
||||||
|
isSuspend = descriptor.isSuspend,
|
||||||
|
isOperator = descriptor.isOperator,
|
||||||
|
isExpect = descriptor.isExpect,
|
||||||
|
isFakeOverride = origin == IrDeclarationOrigin.FAKE_OVERRIDE
|
||||||
|
).apply {
|
||||||
metadata = MetadataSource.Function(it.descriptor)
|
metadata = MetadataSource.Function(it.descriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -690,7 +738,13 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
endOffset: Int,
|
endOffset: Int,
|
||||||
origin: IrDeclarationOrigin,
|
origin: IrDeclarationOrigin,
|
||||||
descriptor: TypeParameterDescriptor,
|
descriptor: TypeParameterDescriptor,
|
||||||
typeParameterFactory: (IrTypeParameterSymbol) -> IrTypeParameter = { IrTypeParameterImpl(startOffset, endOffset, origin, it) }
|
typeParameterFactory: (IrTypeParameterSymbol) -> IrTypeParameter = {
|
||||||
|
IrTypeParameterImpl(
|
||||||
|
startOffset, endOffset, origin, it,
|
||||||
|
nameProvider.nameForDeclaration(descriptor),
|
||||||
|
descriptor.index, descriptor.isReified, descriptor.variance
|
||||||
|
)
|
||||||
|
}
|
||||||
): IrTypeParameter =
|
): IrTypeParameter =
|
||||||
globalTypeParameterSymbolTable.declare(
|
globalTypeParameterSymbolTable.declare(
|
||||||
descriptor,
|
descriptor,
|
||||||
@@ -712,7 +766,13 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
endOffset: Int,
|
endOffset: Int,
|
||||||
origin: IrDeclarationOrigin,
|
origin: IrDeclarationOrigin,
|
||||||
descriptor: TypeParameterDescriptor,
|
descriptor: TypeParameterDescriptor,
|
||||||
typeParameterFactory: (IrTypeParameterSymbol) -> IrTypeParameter = { IrTypeParameterImpl(startOffset, endOffset, origin, it) }
|
typeParameterFactory: (IrTypeParameterSymbol) -> IrTypeParameter = {
|
||||||
|
IrTypeParameterImpl(
|
||||||
|
startOffset, endOffset, origin, it,
|
||||||
|
nameProvider.nameForDeclaration(descriptor),
|
||||||
|
descriptor.index, descriptor.isReified, descriptor.variance
|
||||||
|
)
|
||||||
|
}
|
||||||
): IrTypeParameter =
|
): IrTypeParameter =
|
||||||
scopedTypeParameterSymbolTable.declare(
|
scopedTypeParameterSymbolTable.declare(
|
||||||
descriptor,
|
descriptor,
|
||||||
@@ -739,7 +799,15 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
type: IrType,
|
type: IrType,
|
||||||
varargElementType: IrType? = null,
|
varargElementType: IrType? = null,
|
||||||
valueParameterFactory: (IrValueParameterSymbol) -> IrValueParameter = {
|
valueParameterFactory: (IrValueParameterSymbol) -> IrValueParameter = {
|
||||||
IrValueParameterImpl(startOffset, endOffset, origin, it, type, varargElementType)
|
val valueParameterDescriptor = descriptor as? ValueParameterDescriptor
|
||||||
|
IrValueParameterImpl(
|
||||||
|
startOffset, endOffset, origin, it,
|
||||||
|
nameProvider.nameForDeclaration(descriptor),
|
||||||
|
valueParameterDescriptor?.index ?: -1,
|
||||||
|
type, varargElementType,
|
||||||
|
isCrossinline = valueParameterDescriptor?.isCrossinline ?: false,
|
||||||
|
isNoinline = valueParameterDescriptor?.isNoinline ?: false
|
||||||
|
)
|
||||||
}
|
}
|
||||||
): IrValueParameter =
|
): IrValueParameter =
|
||||||
valueParameterSymbolTable.declareLocal(
|
valueParameterSymbolTable.declareLocal(
|
||||||
@@ -775,7 +843,14 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
descriptor: VariableDescriptor,
|
descriptor: VariableDescriptor,
|
||||||
type: IrType,
|
type: IrType,
|
||||||
variableFactory: (IrVariableSymbol) -> IrVariable = {
|
variableFactory: (IrVariableSymbol) -> IrVariable = {
|
||||||
IrVariableImpl(startOffset, endOffset, origin, it, type)
|
IrVariableImpl(
|
||||||
|
startOffset, endOffset, origin, it,
|
||||||
|
nameProvider.nameForDeclaration(descriptor),
|
||||||
|
type,
|
||||||
|
isVar = descriptor.isVar,
|
||||||
|
isConst = descriptor.isConst,
|
||||||
|
isLateinit = descriptor.isLateInit
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
): IrVariable =
|
): IrVariable =
|
||||||
@@ -810,7 +885,11 @@ open class SymbolTable(val signaturer: IdSignatureComposer) : ReferenceSymbolTab
|
|||||||
localDelegatedPropertySymbolTable.declareLocal(
|
localDelegatedPropertySymbolTable.declareLocal(
|
||||||
descriptor,
|
descriptor,
|
||||||
{ IrLocalDelegatedPropertySymbolImpl(descriptor) },
|
{ IrLocalDelegatedPropertySymbolImpl(descriptor) },
|
||||||
{ IrLocalDelegatedPropertyImpl(startOffset, endOffset, origin, it, type) }
|
{
|
||||||
|
IrLocalDelegatedPropertyImpl(
|
||||||
|
startOffset, endOffset, origin, it, nameProvider.nameForDeclaration(descriptor), type, descriptor.isVar
|
||||||
|
)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
fun referenceLocalDelegatedProperty(descriptor: VariableDescriptorWithAccessors) =
|
fun referenceLocalDelegatedProperty(descriptor: VariableDescriptorWithAccessors) =
|
||||||
|
|||||||
Reference in New Issue
Block a user