DeclarationStubGenerator uses IrTypes

This commit is contained in:
Dmitry Petrov
2018-04-26 16:03:52 +03:00
parent 1353cf879c
commit 372f280578
10 changed files with 75 additions and 63 deletions
@@ -19,11 +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.psi.KtFile
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
import org.jetbrains.kotlin.psi2ir.generators.ModuleGenerator
import org.jetbrains.kotlin.psi2ir.transformations.generateAnnotationsForDeclarations
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
@@ -63,4 +64,9 @@ class Psi2IrTranslator(val configuration: Psi2IrConfiguration = Psi2IrConfigurat
irElement.patchDeclarationParents()
}
private fun generateAnnotationsForDeclarations(context: GeneratorContext, irElement: IrElement) {
val annotationGenerator = AnnotationGenerator(context.moduleDescriptor, context.symbolTable)
irElement.acceptVoid(annotationGenerator)
}
}
@@ -22,13 +22,13 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.transformations.AnnotationGenerator
import org.jetbrains.kotlin.ir.util.AnnotationGenerator
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.descriptors.findPackageFragmentForFile
class ModuleGenerator(override val context: GeneratorContext) : Generator {
private val annotationGenerator = AnnotationGenerator(context)
private val annotationGenerator = AnnotationGenerator(context.moduleDescriptor, context.symbolTable)
fun generateModuleFragment(ktFiles: Collection<KtFile>): IrModuleFragment =
generateModuleFragmentWithoutDependencies(ktFiles).also { irModule ->
@@ -41,7 +41,9 @@ class ModuleGenerator(override val context: GeneratorContext) : Generator {
}
fun generateUnboundSymbolsAsDependencies(irModule: IrModuleFragment) {
ExternalDependenciesGenerator(context.symbolTable, context.irBuiltIns).generateUnboundSymbolsAsDependencies(irModule)
ExternalDependenciesGenerator(
irModule.descriptor, context.symbolTable, context.irBuiltIns
).generateUnboundSymbolsAsDependencies(irModule)
}
private fun generateFiles(ktFiles: Collection<KtFile>): List<IrFile> {
@@ -28,6 +28,7 @@ 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.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
@@ -42,7 +43,6 @@ import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils
import java.lang.AssertionError
class StatementGenerator(
val bodyGenerator: BodyGenerator,
@@ -206,7 +206,7 @@ class StatementGenerator(
)
fun generateConstantExpression(expression: KtExpression, constant: CompileTimeConstant<*>): IrExpression =
ConstantValueGenerator(context).generateConstantValueAsExpression(
ConstantValueGenerator(context.moduleDescriptor, context.symbolTable, null).generateConstantValueAsExpression(
expression.startOffset,
expression.endOffset,
constant.toConstantValue(getInferredTypeWithImplicitCastsOrFail(expression))
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.util.ScopedTypeParametersResolver
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.psi2ir.containsNull
@@ -3,7 +3,7 @@
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.psi2ir.transformations
package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
@@ -16,29 +16,20 @@ import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi2ir.generators.ConstantValueGenerator
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
fun generateAnnotationsForDeclarations(context: GeneratorContext, irElement: IrElement) {
val annotationGenerator = AnnotationGenerator(context.moduleDescriptor, context.symbolTable)
irElement.acceptVoid(annotationGenerator)
}
class AnnotationGenerator(
moduleDescriptor: ModuleDescriptor,
private val symbolTable: SymbolTable
) : IrElementVisitorVoid {
constructor(context: GeneratorContext) : this(context.moduleDescriptor, context.symbolTable)
private val typeTranslator = TypeTranslator(moduleDescriptor, symbolTable)
private val scopedTypeParameterResolver = ScopedTypeParametersResolver()
private val constantValueGenerator = ConstantValueGenerator(moduleDescriptor, symbolTable, this, scopedTypeParameterResolver)
@@ -86,19 +77,24 @@ class AnnotationGenerator(
}
}
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
fun generateAnnotationConstructorCall(annotationDescriptor: AnnotationDescriptor): IrCall {
val annotationType = annotationDescriptor.type
val annotationClassDescriptor = annotationType.constructor.declarationDescriptor as? ClassDescriptor
?: throw AssertionError("No declaration descriptor for annotation $annotationDescriptor")
if (annotationClassDescriptor !is ClassDescriptor) {
throw AssertionError("Annotation type descriptor is not a class: $annotationClassDescriptor")
}
assert(DescriptorUtils.isAnnotationClass(annotationClassDescriptor)) {
"Annotation class expected: $annotationClassDescriptor"
}
val primaryConstructorDescriptor =
annotationClassDescriptor.unsubstitutedPrimaryConstructor
?: annotationClassDescriptor.constructors.singleOrNull()
?: throw AssertionError("No constructor for annotation class $annotationClassDescriptor")
val primaryConstructorDescriptor = annotationClassDescriptor.unsubstitutedPrimaryConstructor
?: annotationClassDescriptor.constructors.singleOrNull()
?: throw AssertionError("No constructor for annotation class $annotationClassDescriptor")
val primaryConstructorSymbol = symbolTable.referenceConstructor(primaryConstructorDescriptor)
val psi = annotationDescriptor.source.safeAs<PsiSourceElement>()?.psi
@@ -106,7 +102,8 @@ class AnnotationGenerator(
val endOffset = psi?.startOffset ?: UNDEFINED_OFFSET
val irCall = IrCallImpl(
startOffset, endOffset, annotationType,
startOffset, endOffset,
annotationType.toIrType(),
primaryConstructorSymbol, primaryConstructorDescriptor,
typeArgumentsCount = 0
)
@@ -3,7 +3,7 @@
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.psi2ir.generators
package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
@@ -14,9 +14,6 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrClassReferenceImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrGetEnumValueImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrVarargImpl
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.psi2ir.transformations.AnnotationGenerator
import org.jetbrains.kotlin.psi2ir.transformations.ScopedTypeParametersResolver
import org.jetbrains.kotlin.resolve.constants.*
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.builtIns
@@ -29,10 +26,9 @@ class ConstantValueGenerator(
private val scopedTypeParameterResolver: ScopedTypeParametersResolver?
) {
constructor(
context: GeneratorContext,
annotationGenerator: AnnotationGenerator? = null
) : this(context.moduleDescriptor, context.symbolTable, annotationGenerator, null)
private val typeTranslator = TypeTranslator(moduleDescriptor, symbolTable)
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
fun generateConstantValueAsExpression(
startOffset: Int,
@@ -40,7 +36,8 @@ class ConstantValueGenerator(
constantValue: ConstantValue<*>,
varargElementType: KotlinType? = null
): IrExpression {
val constantType = constantValue.getType(moduleDescriptor)
val constantKtType = constantValue.getType(moduleDescriptor)
val constantType = constantKtType.toIrType()
return when (constantValue) {
is StringValue -> IrConstImpl.string(startOffset, endOffset, constantType, constantValue.value)
@@ -59,11 +56,11 @@ class ConstantValueGenerator(
is UShortValue -> IrConstImpl.short(startOffset, endOffset, constantType, constantValue.value)
is ArrayValue -> {
val arrayElementType = varargElementType ?: constantType.getArrayElementType()
val arrayElementType = varargElementType ?: constantKtType.getArrayElementType()
IrVarargImpl(
startOffset, endOffset,
constantType,
arrayElementType,
arrayElementType.toIrType(),
constantValue.value.map {
generateConstantValueAsExpression(startOffset, endOffset, it, null)
}
@@ -72,7 +69,7 @@ class ConstantValueGenerator(
is EnumValue -> {
val enumEntryDescriptor =
constantType.memberScope.getContributedClassifier(constantValue.enumEntryName, NoLookupLocation.FROM_BACKEND)
constantKtType.memberScope.getContributedClassifier(constantValue.enumEntryName, NoLookupLocation.FROM_BACKEND)
?: throw AssertionError("No such enum entry ${constantValue.enumEntryName} in $constantType")
if (enumEntryDescriptor !is ClassDescriptor) {
throw AssertionError("Enum entry $enumEntryDescriptor should be a ClassDescriptor")
@@ -90,9 +87,9 @@ class ConstantValueGenerator(
}
is KClassValue -> {
val classifierType = constantValue.value
val classifierDescriptor = classifierType.constructor.declarationDescriptor
?: throw AssertionError("Unexpected KClassValue: $classifierType")
val classifierKtType = constantValue.value
val classifierDescriptor = classifierKtType.constructor.declarationDescriptor
?: throw AssertionError("Unexpected KClassValue: $classifierKtType")
val typeParameterSymbol = classifierDescriptor.safeAs<TypeParameterDescriptor>()?.let {
scopedTypeParameterResolver?.resolveScopedTypeParameter(it)
@@ -100,9 +97,9 @@ class ConstantValueGenerator(
IrClassReferenceImpl(
startOffset, endOffset,
constantValue.getType(moduleDescriptor),
constantValue.getType(moduleDescriptor).toIrType(),
typeParameterSymbol ?: symbolTable.referenceClassifier(classifierDescriptor),
classifierType
classifierKtType.toIrType()
)
}
@@ -29,12 +29,16 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrExpressionBodyImpl
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.propertyIfAccessor
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
class DeclarationStubGenerator(
moduleDescriptor: ModuleDescriptor,
val symbolTable: SymbolTable,
val origin: IrDeclarationOrigin
) {
private val typeTranslator = TypeTranslator(moduleDescriptor, symbolTable)
fun generateEmptyModuleFragmentStub(descriptor: ModuleDescriptor, irBuiltIns: IrBuiltIns): IrModuleFragment =
IrModuleFragmentImpl(descriptor, irBuiltIns)
@@ -59,7 +63,10 @@ class DeclarationStubGenerator(
}
private fun generatePropertyStub(descriptor: PropertyDescriptor): IrProperty =
IrPropertyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor).also { irProperty ->
IrPropertyImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin,
descriptor, descriptor.type.toIrType()
).also { irProperty ->
val getterDescriptor = descriptor.getter
if (getterDescriptor == null) {
irProperty.backingField =
@@ -97,16 +104,24 @@ class DeclarationStubGenerator(
descriptor.valueParameters.mapTo(function.valueParameters) { generateValueParameterStub(it) }
}
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
private fun ReceiverParameterDescriptor.generateReceiverParameterStub(): IrValueParameter =
IrValueParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, this)
IrValueParameterImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, this,
type.toIrType(), null
)
private fun generateValueParameterStub(descriptor: ValueParameterDescriptor): IrValueParameter =
IrValueParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor).also { irValueParameter ->
IrValueParameterImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin,
descriptor, descriptor.type.toIrType(), descriptor.varargElementType?.toIrType()
).also { irValueParameter ->
if (descriptor.declaresDefaultValue()) {
irValueParameter.defaultValue =
IrExpressionBodyImpl(
IrErrorExpressionImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, descriptor.type,
UNDEFINED_OFFSET, UNDEFINED_OFFSET, descriptor.type.toIrType(),
"Stub expression for default value of ${descriptor.name}"
)
)
@@ -137,7 +152,10 @@ class DeclarationStubGenerator(
}
private fun generateTypeParameterStub(descriptor: TypeParameterDescriptor): IrTypeParameter =
IrTypeParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor)
IrTypeParameterImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin,
descriptor, descriptor.upperBounds.map { it.toIrType() }
)
private fun generateMemberStubs(memberScope: MemberScope, container: IrDeclarationContainer) {
generateChildStubs(memberScope.getContributedDescriptors(), container)
@@ -24,9 +24,12 @@ import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
class ExternalDependenciesGenerator(val symbolTable: SymbolTable, val irBuiltIns: IrBuiltIns) {
private val stubGenerator = DeclarationStubGenerator(symbolTable, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB)
class ExternalDependenciesGenerator(
moduleDescriptor: ModuleDescriptor,
val symbolTable: SymbolTable,
val irBuiltIns: IrBuiltIns
) {
private val stubGenerator = DeclarationStubGenerator(moduleDescriptor,symbolTable, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB)
fun generateUnboundSymbolsAsDependencies(irModule: IrModuleFragment) {
DependencyGenerationTask(irModule).run()
@@ -3,7 +3,7 @@
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.psi2ir.transformations
package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
@@ -3,29 +3,19 @@
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.psi2ir.types
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.IrElement
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.IrTypeProjection
import org.jetbrains.kotlin.ir.types.impl.IrDynamicTypeImpl
import org.jetbrains.kotlin.ir.types.impl.IrErrorTypeImpl
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
import org.jetbrains.kotlin.psi2ir.transformations.AnnotationGenerator
import org.jetbrains.kotlin.psi2ir.transformations.ScopedTypeParametersResolver
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typesApproximation.approximateCapturedTypes
@@ -34,8 +24,6 @@ class TypeTranslator(
private val symbolTable: SymbolTable
) {
constructor(context: GeneratorContext) : this(context.moduleDescriptor, context.symbolTable)
private val annotationGenerator = AnnotationGenerator(moduleDescriptor, symbolTable)
private val typeParametersResolver = ScopedTypeParametersResolver()