From 1bf3f4bda796fb1232b8f51c74d57a3bbca57065 Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Tue, 31 Jul 2018 09:09:49 +0200 Subject: [PATCH] Extract ReferenceSymbolTable, support lazy type parameters --- .../jetbrains/kotlin/backend/common/ir/Ir.kt | 4 +- .../psi2ir/generators/AnnotationGenerator.kt | 2 +- .../declarations/IrTypeParametersContainer.kt | 2 +- .../ir/declarations/lazy/IrLazySymbolTable.kt | 41 +++--- .../lazy/LazyScopedTypeParametersResolver.kt | 24 +--- .../kotlin/ir/util/ConstantValueGenerator.kt | 2 +- .../ir/util/DeclarationStubGenerator.kt | 51 +++++-- .../jetbrains/kotlin/ir/util/SymbolTable.kt | 132 +++++++++++------- .../kotlin/ir/util/TypeTranslator.kt | 15 +- 9 files changed, 168 insertions(+), 105 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt index c5b96b7c030..e1d305935cf 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt @@ -10,7 +10,7 @@ import org.jetbrains.kotlin.ir.declarations.IrModuleFragment import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol -import org.jetbrains.kotlin.ir.util.SymbolTable +import org.jetbrains.kotlin.ir.util.ReferenceSymbolTable import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.types.KotlinType @@ -26,7 +26,7 @@ abstract class Ir(val context: T, val irModule: Ir open fun shouldGenerateHandlerParameterForDefaultBodyFun() = false } -abstract class Symbols(val context: T, private val symbolTable: SymbolTable) { +abstract class Symbols(val context: T, private val symbolTable: ReferenceSymbolTable) { protected val builtIns get() = context.builtIns diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/AnnotationGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/AnnotationGenerator.kt index 2a514ff3759..79b8d62fe7e 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/AnnotationGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/AnnotationGenerator.kt @@ -31,7 +31,7 @@ class AnnotationGenerator( generateAnnotationsForDeclaration(declaration) visitElement(declaration) if (declaration is IrTypeParametersContainer) { - typeTranslator.leaveScope() + typeTranslator.leaveScope(declaration) } } diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrTypeParametersContainer.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrTypeParametersContainer.kt index e92214cf261..b728f9b3f17 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrTypeParametersContainer.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrTypeParametersContainer.kt @@ -16,6 +16,6 @@ package org.jetbrains.kotlin.ir.declarations -interface IrTypeParametersContainer { +interface IrTypeParametersContainer : IrDeclaration { val typeParameters: MutableList } \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/IrLazySymbolTable.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/IrLazySymbolTable.kt index f39179dd92c..be7f08fd75b 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/IrLazySymbolTable.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/IrLazySymbolTable.kt @@ -5,57 +5,66 @@ package org.jetbrains.kotlin.ir.declarations.lazy -import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor -import org.jetbrains.kotlin.descriptors.ClassDescriptor -import org.jetbrains.kotlin.descriptors.FunctionDescriptor -import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor +import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator +import org.jetbrains.kotlin.ir.util.ReferenceSymbolTable import org.jetbrains.kotlin.ir.util.SymbolTable -class IrLazySymbolTable(private val stubGenerator: DeclarationStubGenerator, val originalTable: SymbolTable) : SymbolTable() { +class IrLazySymbolTable(private val originalTable: SymbolTable) : ReferenceSymbolTable by originalTable { + override val declarationSystemTable: SymbolTable = originalTable + + override val referenceSymbolTable: ReferenceSymbolTable = this /*Don't force builtins class linking before unbound symbols linking: otherwise stdlib compilation will failed*/ - internal var unboundSymbolGeneration = false + var stubGenerator: DeclarationStubGenerator? = null override fun referenceClass(descriptor: ClassDescriptor): IrClassSymbol { return originalTable.referenceClass(descriptor).also { - if (unboundSymbolGeneration && !it.isBound) { - stubGenerator.generateClassStub(descriptor).symbol + if (!it.isBound) { + stubGenerator?.generateClassStub(descriptor) } } } override fun referenceConstructor(descriptor: ClassConstructorDescriptor): IrConstructorSymbol { return originalTable.referenceConstructor(descriptor).also { - if (!it.isBound && unboundSymbolGeneration) { - stubGenerator.generateConstructorStub(descriptor).symbol + if (!it.isBound) { + stubGenerator?.generateConstructorStub(descriptor) } } } override fun referenceEnumEntry(descriptor: ClassDescriptor): IrEnumEntrySymbol { return originalTable.referenceEnumEntry(descriptor).also { - if (!it.isBound && unboundSymbolGeneration) { - stubGenerator.generateEnumEntryStub(descriptor).symbol + if (!it.isBound) { + stubGenerator?.generateEnumEntryStub(descriptor) } } } override fun referenceSimpleFunction(descriptor: FunctionDescriptor): IrSimpleFunctionSymbol { return originalTable.referenceSimpleFunction(descriptor).also { - if (!it.isBound && unboundSymbolGeneration) { - stubGenerator.generateFunctionStub(descriptor).symbol + if (!it.isBound) { + stubGenerator?.generateFunctionStub(descriptor) } } } override fun referenceTypeParameter(classifier: TypeParameterDescriptor): IrTypeParameterSymbol { return originalTable.referenceTypeParameter(classifier).also { - if (!it.isBound && unboundSymbolGeneration) { - stubGenerator.generateOrGetTypeParameterStub(classifier).symbol + if (!it.isBound) { + stubGenerator?.generateOrGetTypeParameterStub(classifier) } } } + + override fun referenceFunction(callable: CallableDescriptor): IrFunctionSymbol { + return super.referenceFunction(callable) + } + + override fun referenceClassifier(classifier: ClassifierDescriptor): IrClassifierSymbol { + return super.referenceClassifier(classifier) + } } \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/LazyScopedTypeParametersResolver.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/LazyScopedTypeParametersResolver.kt index a7f9258e7a4..d3f4d855942 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/LazyScopedTypeParametersResolver.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/LazyScopedTypeParametersResolver.kt @@ -13,7 +13,7 @@ import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.util.* import java.util.* -class LazyScopedTypeParametersResolver() : TypeParametersResolver { +class LazyScopedTypeParametersResolver(private val symbolTable: ReferenceSymbolTable) : TypeParametersResolver { private val typeParameterScopes = ArrayDeque() @@ -27,25 +27,13 @@ class LazyScopedTypeParametersResolver() : TypeParametersResolver { typeParameterScopes.removeFirst() } - //TODO optimize override fun resolveScopedTypeParameter(typeParameterDescriptor: TypeParameterDescriptor): IrTypeParameterSymbol? { - var parent = typeParameterScopes.first() - while (parent != null) { - val declaration = parent - val symbol = declaration.typeParameters.firstOrNull { + //Just support accessor scoped symbols resolve in external declaration + //there should be enough to process only parent typeparameters + return typeParameterScopes.firstOrNull()?.let { parent -> + parent.typeParameters.firstOrNull { it.descriptor == typeParameterDescriptor }?.symbol - if (symbol != null) return symbol - parent = calcParent(parent) - } - return null + } ?: null } - - private fun calcParent(container: IrTypeParametersContainer): IrTypeParametersContainer? { - return when(container) { - is IrClass -> if (container.isObject || !container.isInner) null else container.parent - else -> (container as? IrDeclaration)?.parent - } as? IrTypeParametersContainer - } - } \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/ConstantValueGenerator.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/ConstantValueGenerator.kt index 44acbc1cebd..db9d98623e4 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/ConstantValueGenerator.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/ConstantValueGenerator.kt @@ -24,7 +24,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.safeAs class ConstantValueGenerator( private val moduleDescriptor: ModuleDescriptor, - private val symbolTable: SymbolTable + private val symbolTable: ReferenceSymbolTable ) { lateinit var typeTranslator: TypeTranslator diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt index 8b286bc3da3..417212f2566 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt @@ -27,7 +27,6 @@ import org.jetbrains.kotlin.ir.declarations.lazy.* import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.expressions.impl.IrErrorExpressionImpl import org.jetbrains.kotlin.ir.expressions.impl.IrExpressionBodyImpl -import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.types.KotlinType @@ -37,14 +36,17 @@ class DeclarationStubGenerator( val origin: IrDeclarationOrigin, val languageVersionSettings: LanguageVersionSettings ) { - private val lazyTable = IrLazySymbolTable(this, symbolTable) + + private val lazyTable = symbolTable.lazyWrapper internal var unboundSymbolGeneration: Boolean - get() = lazyTable.unboundSymbolGeneration - set(value) { lazyTable.unboundSymbolGeneration = value } + get() = lazyTable.stubGenerator != null + set(value) { + lazyTable.stubGenerator = if (value) this else null + } - private val typeTranslator = TypeTranslator(lazyTable, languageVersionSettings, LazyScopedTypeParametersResolver()) + private val typeTranslator = TypeTranslator(lazyTable, languageVersionSettings, LazyScopedTypeParametersResolver(lazyTable), true) private val constantValueGenerator = ConstantValueGenerator(moduleDescriptor, lazyTable) init { @@ -101,13 +103,13 @@ class DeclarationStubGenerator( return referenced.owner } + val origin = + if (descriptor.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) + IrDeclarationOrigin.FAKE_OVERRIDE + else origin return symbolTable.declareSimpleFunction( UNDEFINED_OFFSET, UNDEFINED_OFFSET, - if (descriptor.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) { - IrDeclarationOrigin.FAKE_OVERRIDE - } else { - origin - }, + origin, descriptor.original ) { IrLazyFunction(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator) } } @@ -162,10 +164,29 @@ class DeclarationStubGenerator( } } - internal fun generateTypeParameterStub(descriptor: TypeParameterDescriptor): IrTypeParameter { - return IrLazyTypeParameter( - UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, - IrTypeParameterSymbolImpl(descriptor), this, typeTranslator - ) + internal fun generateOrGetTypeParameterStub(descriptor: TypeParameterDescriptor): IrTypeParameter { + val referenced = symbolTable.referenceTypeParameter(descriptor) + if (referenced.isBound) { + return referenced.owner + } + return symbolTable.declareGlobalTypeParameter(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor) { + IrLazyTypeParameter( + UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, + it, this, typeTranslator + ) + } + } + + internal fun generateOrGetScopedTypeParameterStub(descriptor: TypeParameterDescriptor): IrTypeParameter { + val referenced = symbolTable.referenceTypeParameter(descriptor) + if (referenced.isBound) { + return referenced.owner + } + return symbolTable.declareScopedTypeParameter(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor) { + IrLazyTypeParameter( + UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, + it, this, typeTranslator + ) + } } } \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/SymbolTable.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/SymbolTable.kt index 451d859de50..d2d756ed6e9 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/SymbolTable.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/SymbolTable.kt @@ -20,13 +20,65 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.ir.SourceManager import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.impl.* +import org.jetbrains.kotlin.ir.declarations.lazy.IrLazySymbolTable import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrExpressionBody import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.symbols.impl.* import org.jetbrains.kotlin.ir.types.IrType -open class SymbolTable { +interface ReferenceSymbolTable { + fun referenceClass(descriptor: ClassDescriptor): IrClassSymbol + + fun referenceConstructor(descriptor: ClassConstructorDescriptor): IrConstructorSymbol + + fun referenceEnumEntry(descriptor: ClassDescriptor): IrEnumEntrySymbol + fun referenceField(descriptor: PropertyDescriptor): IrFieldSymbol + + fun referenceSimpleFunction(descriptor: FunctionDescriptor): IrSimpleFunctionSymbol + fun referenceDeclaredFunction(descriptor: FunctionDescriptor): IrSimpleFunctionSymbol + fun referenceValueParameter(descriptor: ParameterDescriptor): IrValueParameterSymbol + + fun referenceTypeParameter(classifier: TypeParameterDescriptor): IrTypeParameterSymbol + fun referenceVariable(descriptor: VariableDescriptor): IrVariableSymbol + + fun referenceFunction(callable: CallableDescriptor): IrFunctionSymbol = + when (callable) { + is ClassConstructorDescriptor -> + referenceConstructor(callable) + is FunctionDescriptor -> + referenceSimpleFunction(callable) + else -> + throw IllegalArgumentException("Unexpected callable descriptor: $callable") + } + + fun referenceClassifier(classifier: ClassifierDescriptor): IrClassifierSymbol = + when (classifier) { + is TypeParameterDescriptor -> + referenceTypeParameter(classifier) + is ClassDescriptor -> + referenceClass(classifier) + else -> + throw IllegalArgumentException("Unexpected classifier descriptor: $classifier") + } + + + val declarationSystemTable: SymbolTable + val referenceSymbolTable: ReferenceSymbolTable + + fun enterScope(owner: DeclarationDescriptor) + + fun leaveScope(owner: DeclarationDescriptor) +} + +open class SymbolTable : ReferenceSymbolTable { + + override val declarationSystemTable: SymbolTable = this + + override val referenceSymbolTable: ReferenceSymbolTable = IrLazySymbolTable(this) + + val lazyWrapper = referenceSymbolTable as IrLazySymbolTable + private abstract class SymbolTableBase> { val unboundSymbols = linkedSetOf() @@ -102,7 +154,7 @@ open class SymbolTable { override fun get(d: D): S? { val scope = currentScope - ?: throw AssertionError("No active scope") + ?: throw AssertionError("No active scope") return scope[d] } @@ -177,14 +229,15 @@ open class SymbolTable { fun declareClass( startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassDescriptor, classFactory: (IrClassSymbol) -> IrClass = { IrClassImpl(startOffset, endOffset, origin, it) } - ): IrClass = - classSymbolTable.declare( + ): IrClass { + return classSymbolTable.declare( descriptor, { IrClassSymbolImpl(descriptor) }, classFactory ) + } - open fun referenceClass(descriptor: ClassDescriptor) = + override fun referenceClass(descriptor: ClassDescriptor) = classSymbolTable.referenced(descriptor) { IrClassSymbolImpl(descriptor) } val unboundClasses: Set get() = classSymbolTable.unboundSymbols @@ -202,20 +255,22 @@ open class SymbolTable { constructorFactory ) - open fun referenceConstructor(descriptor: ClassConstructorDescriptor) = + override fun referenceConstructor(descriptor: ClassConstructorDescriptor) = constructorSymbolTable.referenced(descriptor) { IrConstructorSymbolImpl(descriptor) } val unboundConstructors: Set get() = constructorSymbolTable.unboundSymbols - fun declareEnumEntry(startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassDescriptor, - factory: (IrEnumEntrySymbol) -> IrEnumEntry = { IrEnumEntryImpl(startOffset, endOffset, origin, it) }): IrEnumEntry = + fun declareEnumEntry( + startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassDescriptor, + factory: (IrEnumEntrySymbol) -> IrEnumEntry = { IrEnumEntryImpl(startOffset, endOffset, origin, it) } + ): IrEnumEntry = enumEntrySymbolTable.declare( descriptor, { IrEnumEntrySymbolImpl(descriptor) }, factory ) - open fun referenceEnumEntry(descriptor: ClassDescriptor) = + override fun referenceEnumEntry(descriptor: ClassDescriptor) = enumEntrySymbolTable.referenced(descriptor) { IrEnumEntrySymbolImpl(descriptor) } val unboundEnumEntries: Set get() = enumEntrySymbolTable.unboundSymbols @@ -246,7 +301,7 @@ open class SymbolTable { initializer = irInitializer } - fun referenceField(descriptor: PropertyDescriptor) = + override fun referenceField(descriptor: PropertyDescriptor) = fieldSymbolTable.referenced(descriptor) { IrFieldSymbolImpl(descriptor) } val unboundFields: Set get() = fieldSymbolTable.unboundSymbols @@ -257,17 +312,18 @@ open class SymbolTable { origin: IrDeclarationOrigin, descriptor: FunctionDescriptor, functionFactory: (IrSimpleFunctionSymbol) -> IrSimpleFunction = { IrFunctionImpl(startOffset, endOffset, origin, it) } - ): IrSimpleFunction = - simpleFunctionSymbolTable.declare( + ): IrSimpleFunction { + return simpleFunctionSymbolTable.declare( descriptor, { IrSimpleFunctionSymbolImpl(descriptor) }, functionFactory ) + } - open fun referenceSimpleFunction(descriptor: FunctionDescriptor) = + override fun referenceSimpleFunction(descriptor: FunctionDescriptor) = simpleFunctionSymbolTable.referenced(descriptor) { IrSimpleFunctionSymbolImpl(descriptor) } - fun referenceDeclaredFunction(descriptor: FunctionDescriptor) = + override fun referenceDeclaredFunction(descriptor: FunctionDescriptor) = simpleFunctionSymbolTable.referenced(descriptor) { throw AssertionError("Function is not declared: $descriptor") } val unboundSimpleFunctions: Set get() = simpleFunctionSymbolTable.unboundSymbols @@ -276,26 +332,29 @@ open class SymbolTable { startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, - descriptor: TypeParameterDescriptor + descriptor: TypeParameterDescriptor, + typeParameterFactory: (IrTypeParameterSymbol) -> IrTypeParameter = { IrTypeParameterImpl(startOffset, endOffset, origin, it) } ): IrTypeParameter = globalTypeParameterSymbolTable.declare( descriptor, { IrTypeParameterSymbolImpl(descriptor) }, - { IrTypeParameterImpl(startOffset, endOffset, origin, it) } + typeParameterFactory ) fun declareScopedTypeParameter( startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, - descriptor: TypeParameterDescriptor + descriptor: TypeParameterDescriptor, + typeParameterFactory: (IrTypeParameterSymbol) -> IrTypeParameter = { IrTypeParameterImpl(startOffset, endOffset, origin, it) } ): IrTypeParameter = scopedTypeParameterSymbolTable.declare( descriptor, { IrTypeParameterSymbolImpl(descriptor) }, - { IrTypeParameterImpl(startOffset, endOffset, origin, it) } + typeParameterFactory ) + val unboundTypeParameters: Set get() = globalTypeParameterSymbolTable.unboundSymbols fun declareValueParameter( @@ -316,16 +375,15 @@ open class SymbolTable { valueParameterSymbolTable.introduceLocal(irValueParameter.descriptor, irValueParameter.symbol) } - fun referenceValueParameter(descriptor: ParameterDescriptor) = + override fun referenceValueParameter(descriptor: ParameterDescriptor) = valueParameterSymbolTable.referenced(descriptor) { throw AssertionError("Undefined parameter referenced: $descriptor\n${valueParameterSymbolTable.dump()}") } - open fun referenceTypeParameter(classifier: TypeParameterDescriptor): IrTypeParameterSymbol = - scopedTypeParameterSymbolTable.get(classifier) - ?: globalTypeParameterSymbolTable.referenced(classifier) { - throw AssertionError("Undefined type parameter referenced: $classifier") - } + override fun referenceTypeParameter(classifier: TypeParameterDescriptor): IrTypeParameterSymbol = + scopedTypeParameterSymbolTable.get(classifier) ?: globalTypeParameterSymbolTable.referenced(classifier) { + IrTypeParameterSymbolImpl(classifier) + } val unboundValueParameters: Set get() = valueParameterSymbolTable.unboundSymbols @@ -354,29 +412,19 @@ open class SymbolTable { initializer = irInitializerExpression } - fun referenceVariable(descriptor: VariableDescriptor) = + override fun referenceVariable(descriptor: VariableDescriptor) = variableSymbolTable.referenced(descriptor) { throw AssertionError("Undefined variable referenced: $descriptor") } val unboundVariables: Set get() = variableSymbolTable.unboundSymbols - fun enterScope(owner: DeclarationDescriptor) { + override fun enterScope(owner: DeclarationDescriptor) { scopedSymbolTables.forEach { it.enterScope(owner) } } - fun leaveScope(owner: DeclarationDescriptor) { + override fun leaveScope(owner: DeclarationDescriptor) { scopedSymbolTables.forEach { it.leaveScope(owner) } } - fun referenceFunction(callable: CallableDescriptor): IrFunctionSymbol = - when (callable) { - is ClassConstructorDescriptor -> - referenceConstructor(callable) - is FunctionDescriptor -> - referenceSimpleFunction(callable) - else -> - throw IllegalArgumentException("Unexpected callable descriptor: $callable") - } - fun referenceValue(value: ValueDescriptor): IrValueSymbol = when (value) { is ParameterDescriptor -> @@ -386,16 +434,6 @@ open class SymbolTable { else -> throw IllegalArgumentException("Unexpected value descriptor: $value") } - - fun referenceClassifier(classifier: ClassifierDescriptor): IrClassifierSymbol = - when (classifier) { - is TypeParameterDescriptor -> - referenceTypeParameter(classifier) - is ClassDescriptor -> - referenceClass(classifier) - else -> - throw IllegalArgumentException("Unexpected classifier descriptor: $classifier") - } } inline fun SymbolTable.withScope(owner: DeclarationDescriptor, block: SymbolTable.() -> T): T { diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt index d7360c1c662..126cde25ffa 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt @@ -22,9 +22,10 @@ import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.typesApproximation.approximateCapturedTypes class TypeTranslator( - private val symbolTable: SymbolTable, + private val symbolTable: ReferenceSymbolTable, val languageVersionSettings: LanguageVersionSettings, - private val typeParametersResolver: TypeParametersResolver = ScopedTypeParametersResolver() + private val typeParametersResolver: TypeParametersResolver = ScopedTypeParametersResolver(), + private val enterTableScope: Boolean = false ) { private val typeApproximatorForNI = TypeApproximator() @@ -32,16 +33,22 @@ class TypeTranslator( fun enterScope(irElement: IrTypeParametersContainer) { typeParametersResolver.enterTypeParameterScope(irElement) + if(enterTableScope) { + symbolTable.enterScope(irElement.descriptor) + } } - fun leaveScope() { + fun leaveScope(irElement: IrTypeParametersContainer) { typeParametersResolver.leaveTypeParameterScope() + if(enterTableScope) { + symbolTable.leaveScope(irElement.descriptor) + } } inline fun buildWithScope(container: IrTypeParametersContainer, builder: () -> T): T { enterScope(container) val result = builder() - leaveScope() + leaveScope(container) return result }