Fix circular dependency: TypeTranslator <-> ConstantValueGenerator

TODO proper DI?
This commit is contained in:
Dmitry Petrov
2018-06-26 14:49:08 +03:00
parent ede3a34baa
commit d5286874bd
17 changed files with 61 additions and 56 deletions
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.builtins.getFunctionalClassKind
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.IrGeneratorContext
import org.jetbrains.kotlin.ir.builders.IrGeneratorContextBase
import org.jetbrains.kotlin.ir.builders.Scope
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.expressions.IrCall
@@ -104,5 +104,5 @@ private class KCallableNamePropertyTransformer(val lower: KCallableNamePropertyL
endOffset
)
class IrLoweringContext(backendContext: BackendContext) : IrGeneratorContext(backendContext.irBuiltIns)
class IrLoweringContext(backendContext: BackendContext) : IrGeneratorContextBase(backendContext.irBuiltIns)
}
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
@@ -40,10 +39,9 @@ import org.jetbrains.kotlin.resolve.OverridingUtil
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.utils.Printer
class IrLoweringContext(backendContext: BackendContext) : IrGeneratorContext(backendContext.irBuiltIns)
class IrLoweringContext(backendContext: BackendContext) : IrGeneratorContextBase(backendContext.irBuiltIns)
class DeclarationIrBuilder(
backendContext: BackendContext,
@@ -25,7 +25,7 @@ class JsIntrinsics(
context: JsIrBackendContext
) {
private val stubBuilder = DeclarationStubGenerator(context.symbolTable, JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB)
private val stubBuilder = DeclarationStubGenerator(context.module, context.symbolTable, JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB)
// Equality operations:
@@ -19,12 +19,12 @@ package org.jetbrains.kotlin.psi2ir
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.util.AnnotationGenerator
import org.jetbrains.kotlin.ir.util.patchDeclarationParents
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.generators.AnnotationGenerator
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
import org.jetbrains.kotlin.psi2ir.generators.ModuleGenerator
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.psi2ir.transformations.insertImplicitCasts
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.utils.SmartList
@@ -66,7 +66,7 @@ class Psi2IrTranslator(val configuration: Psi2IrConfiguration = Psi2IrConfigurat
}
private fun generateAnnotationsForDeclarations(context: GeneratorContext, irElement: IrElement) {
val annotationGenerator = AnnotationGenerator(context.moduleDescriptor, context.symbolTable)
val annotationGenerator = AnnotationGenerator(context)
irElement.acceptVoid(annotationGenerator)
}
}
@@ -3,9 +3,8 @@
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.ir.util
package org.jetbrains.kotlin.psi2ir.generators
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PropertySetterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget
@@ -13,16 +12,13 @@ import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.types.KotlinType
class AnnotationGenerator(
moduleDescriptor: ModuleDescriptor,
symbolTable: SymbolTable
context: GeneratorContext
) : IrElementVisitorVoid {
private val typeTranslator = TypeTranslator(moduleDescriptor, symbolTable)
private val scopedTypeParameterResolver = ScopedTypeParametersResolver()
private val constantValueGenerator = ConstantValueGenerator(moduleDescriptor, symbolTable, scopedTypeParameterResolver)
private val typeTranslator = context.typeTranslator
private val constantValueGenerator = context.constantValueGenerator
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
@@ -30,12 +26,12 @@ class AnnotationGenerator(
override fun visitDeclaration(declaration: IrDeclaration) {
if (declaration is IrTypeParametersContainer) {
scopedTypeParameterResolver.enterTypeParameterScope(declaration)
typeTranslator.enterScope(declaration)
}
generateAnnotationsForDeclaration(declaration)
visitElement(declaration)
if (declaration is IrTypeParametersContainer) {
scopedTypeParameterResolver.leaveTypeParameterScope()
typeTranslator.leaveScope()
}
}
@@ -68,8 +64,6 @@ class AnnotationGenerator(
}
}
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
private fun isAnnotationTargetMatchingDeclaration(target: AnnotationUseSiteTarget?, element: IrElement): Boolean =
when (element) {
is IrProperty ->
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.ir.builders.Scope
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
@@ -40,7 +39,7 @@ class BodyGenerator(
val scopeOwner: DeclarationDescriptor get() = scopeOwnerSymbol.descriptor
private val typeTranslator = TypeTranslator(context.moduleDescriptor, context.symbolTable)
private val typeTranslator = context.typeTranslator
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
override val scope = Scope(scopeOwnerSymbol)
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrPropertyImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrTypeAliasImpl
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.ir.util.withScope
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
@@ -36,7 +35,7 @@ import org.jetbrains.kotlin.types.KotlinType
class DeclarationGenerator(override val context: GeneratorContext) : Generator {
private val typeTranslator = TypeTranslator(context.moduleDescriptor, context.symbolTable)
private val typeTranslator = context.typeTranslator
fun KotlinType.toIrType() = typeTranslator.translateType(this)
@@ -21,7 +21,9 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.NotFoundClasses
import org.jetbrains.kotlin.ir.builders.IrGeneratorContext
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.util.ConstantValueGenerator
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
import org.jetbrains.kotlin.psi2ir.PsiSourceManager
import org.jetbrains.kotlin.resolve.BindingContext
@@ -32,7 +34,18 @@ class GeneratorContext(
val moduleDescriptor: ModuleDescriptor,
val bindingContext: BindingContext,
val symbolTable: SymbolTable = SymbolTable()
) : IrGeneratorContext(IrBuiltIns(moduleDescriptor.builtIns, symbolTable)) {
) : IrGeneratorContext() {
val constantValueGenerator: ConstantValueGenerator = ConstantValueGenerator(moduleDescriptor, symbolTable)
val typeTranslator: TypeTranslator = TypeTranslator(symbolTable)
init {
typeTranslator.constantValueGenerator = constantValueGenerator
constantValueGenerator.typeTranslator = typeTranslator
}
override val irBuiltIns: IrBuiltIns = IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable)
val sourceManager = PsiSourceManager()
// TODO: inject a correct StorageManager instance, or store NotFoundClasses inside ModuleDescriptor
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl
import org.jetbrains.kotlin.ir.util.ConstantValueGenerator
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.BindingContext
@@ -28,7 +27,7 @@ import org.jetbrains.kotlin.resolve.lazy.descriptors.findPackageFragmentForFile
class ModuleGenerator(override val context: GeneratorContext) : Generator {
private val constantValueGenerator = ConstantValueGenerator(context.moduleDescriptor, context.symbolTable, null)
private val constantValueGenerator = context.constantValueGenerator
fun generateModuleFragment(ktFiles: Collection<KtFile>): IrModuleFragment =
generateModuleFragmentWithoutDependencies(ktFiles).also { irModule ->
@@ -28,8 +28,6 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrTypeAliasImpl
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.ConstantValueGenerator
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
@@ -56,7 +54,7 @@ class StatementGenerator(
val scopeOwner: DeclarationDescriptor get() = bodyGenerator.scopeOwner
private val typeTranslator = TypeTranslator(context.moduleDescriptor, context.symbolTable)
private val typeTranslator = context.typeTranslator
fun KotlinType.toIrType() = typeTranslator.translateType(this)
@@ -222,7 +220,7 @@ class StatementGenerator(
)
fun generateConstantExpression(expression: KtExpression, constant: CompileTimeConstant<*>): IrExpression =
ConstantValueGenerator(context.moduleDescriptor, context.symbolTable, null).generateConstantValueAsExpression(
context.constantValueGenerator.generateConstantValueAsExpression(
expression.startOffset,
expression.endOffset,
constant.toConstantValue(getInferredTypeWithImplicitCastsOrFail(expression))
@@ -19,13 +19,15 @@ package org.jetbrains.kotlin.psi2ir.transformations
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.psi2ir.containsNull
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
@@ -45,9 +47,8 @@ class InsertImplicitCasts(context: GeneratorContext) : IrElementTransformerVoid(
private val builtIns = context.builtIns
private val irBuiltIns = context.irBuiltIns
private val symbolTable = context.symbolTable
private val typeTranslator = TypeTranslator(context.moduleDescriptor, symbolTable)
private val typeTranslator = context.typeTranslator
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
override fun visitCallableReference(expression: IrCallableReference): IrExpression =
@@ -27,6 +27,10 @@ interface IrGeneratorWithScope : IrGenerator {
val scope: Scope
}
open class IrGeneratorContext(val irBuiltIns: IrBuiltIns) {
abstract class IrGeneratorContext {
abstract val irBuiltIns: IrBuiltIns
val builtIns: KotlinBuiltIns get() = irBuiltIns.builtIns
}
}
open class IrGeneratorContextBase(override val irBuiltIns: IrBuiltIns) : IrGeneratorContext()
@@ -38,9 +38,11 @@ import org.jetbrains.kotlin.types.SimpleType
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.typeUtil.makeNullable
class IrBuiltIns(val builtIns: KotlinBuiltIns, outerSymbolTable: SymbolTable?) {
constructor(builtIns: KotlinBuiltIns) : this(builtIns, null)
class IrBuiltIns(
val builtIns: KotlinBuiltIns,
private val typeTranslator: TypeTranslator,
outerSymbolTable: SymbolTable? = null
) {
private val builtInsModule = builtIns.builtInsModule
@@ -49,7 +51,6 @@ class IrBuiltIns(val builtIns: KotlinBuiltIns, outerSymbolTable: SymbolTable?) {
private val symbolTable = outerSymbolTable ?: SymbolTable()
private val stubBuilder = DeclarationStubGenerator(builtInsModule, symbolTable, IrDeclarationOrigin.IR_BUILTINS_STUB)
private val typeTranslator = TypeTranslator(builtInsModule, symbolTable)
private fun ClassDescriptor.toIrSymbol() = symbolTable.referenceClass(this)
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
@@ -7,13 +7,13 @@ package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.constants.*
@@ -24,11 +24,10 @@ import org.jetbrains.kotlin.utils.addToStdlib.safeAs
class ConstantValueGenerator(
private val moduleDescriptor: ModuleDescriptor,
private val symbolTable: SymbolTable,
private val scopedTypeParameterResolver: ScopedTypeParametersResolver?
private val symbolTable: SymbolTable
) {
private val typeTranslator = TypeTranslator(moduleDescriptor, symbolTable)
lateinit var typeTranslator: TypeTranslator
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
@@ -90,14 +89,10 @@ class ConstantValueGenerator(
val classifierDescriptor = classifierKtType.constructor.declarationDescriptor
?: throw AssertionError("Unexpected KClassValue: $classifierKtType")
val typeParameterSymbol = classifierDescriptor.safeAs<TypeParameterDescriptor>()?.let {
scopedTypeParameterResolver?.resolveScopedTypeParameter(it)
}
IrClassReferenceImpl(
startOffset, endOffset,
constantValue.getType(moduleDescriptor).toIrType(),
typeParameterSymbol ?: symbolTable.referenceClassifier(classifierDescriptor),
classifierDescriptor.defaultType.toIrType().classifierOrFail,
classifierKtType.toIrType()
)
}
@@ -36,7 +36,13 @@ class DeclarationStubGenerator(
val symbolTable: SymbolTable,
val origin: IrDeclarationOrigin
) {
private val typeTranslator = TypeTranslator(moduleDescriptor, symbolTable)
private val typeTranslator = TypeTranslator(symbolTable)
private val constantValueGenerator = ConstantValueGenerator(moduleDescriptor, symbolTable)
init {
typeTranslator.constantValueGenerator = constantValueGenerator
constantValueGenerator.typeTranslator = typeTranslator
}
fun generateEmptyModuleFragmentStub(descriptor: ModuleDescriptor, irBuiltIns: IrBuiltIns): IrModuleFragment =
IrModuleFragmentImpl(descriptor, irBuiltIns)
@@ -29,7 +29,7 @@ class ExternalDependenciesGenerator(
val symbolTable: SymbolTable,
val irBuiltIns: IrBuiltIns
) {
private val stubGenerator = DeclarationStubGenerator(moduleDescriptor,symbolTable, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB)
private val stubGenerator = DeclarationStubGenerator(moduleDescriptor, symbolTable, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB)
fun generateUnboundSymbolsAsDependencies(irModule: IrModuleFragment) {
DependencyGenerationTask(irModule).run()
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
@@ -21,12 +20,11 @@ import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typesApproximation.approximateCapturedTypes
class TypeTranslator(
moduleDescriptor: ModuleDescriptor,
private val symbolTable: SymbolTable
) {
private val typeParametersResolver = ScopedTypeParametersResolver()
private val constantValueGenerator = ConstantValueGenerator(moduleDescriptor, symbolTable, typeParametersResolver)
lateinit var constantValueGenerator: ConstantValueGenerator
fun enterScope(irElement: IrTypeParametersContainer) {
typeParametersResolver.enterTypeParameterScope(irElement)