Generate IR for synthetic nested classes directly in class generator
This commit is contained in:
+1
@@ -164,6 +164,7 @@ class SyntheticClassOrObjectDescriptor(
|
|||||||
// in theory `containingKtFile` is `@NotNull` but in practice EA-114080
|
// in theory `containingKtFile` is `@NotNull` but in practice EA-114080
|
||||||
_parent.containingKtFile ?: throw IllegalStateException("containingKtFile was null for $_parent of ${_parent.javaClass}")
|
_parent.containingKtFile ?: throw IllegalStateException("containingKtFile was null for $_parent of ${_parent.javaClass}")
|
||||||
|
|
||||||
|
override fun getBody(): KtClassBody? = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,10 +22,7 @@ import org.jetbrains.kotlin.descriptors.*
|
|||||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.psi.KtElement
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.KtExpression
|
|
||||||
import org.jetbrains.kotlin.psi.KtPsiUtil
|
|
||||||
import org.jetbrains.kotlin.psi.KtSecondaryConstructor
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
@@ -85,3 +82,6 @@ val PropertyDescriptor.unwrappedGetMethod: FunctionDescriptor?
|
|||||||
|
|
||||||
val PropertyDescriptor.unwrappedSetMethod: FunctionDescriptor?
|
val PropertyDescriptor.unwrappedSetMethod: FunctionDescriptor?
|
||||||
get() = if (this is SyntheticPropertyDescriptor) this.setMethod else setter
|
get() = if (this is SyntheticPropertyDescriptor) this.setMethod else setter
|
||||||
|
|
||||||
|
val KtPureElement?.pureStartOffsetOrUndefined get() = this?.psiOrParent?.startOffset ?: UNDEFINED_OFFSET
|
||||||
|
val KtPureElement?.pureEndOffsetOrUndefined get() = this?.psiOrParent?.endOffset ?: UNDEFINED_OFFSET
|
||||||
@@ -25,7 +25,10 @@ import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
|||||||
import org.jetbrains.kotlin.ir.util.referenceFunction
|
import org.jetbrains.kotlin.ir.util.referenceFunction
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.pureEndOffset
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.pureStartOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
|
import org.jetbrains.kotlin.psi.synthetics.findClassDescriptor
|
||||||
import org.jetbrains.kotlin.psi2ir.intermediate.VariableLValue
|
import org.jetbrains.kotlin.psi2ir.intermediate.VariableLValue
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||||
@@ -176,15 +179,15 @@ class BodyGenerator(
|
|||||||
fun getLoop(expression: KtExpression): IrLoop? =
|
fun getLoop(expression: KtExpression): IrLoop? =
|
||||||
loopTable[expression]
|
loopTable[expression]
|
||||||
|
|
||||||
fun generatePrimaryConstructorBody(ktClassOrObject: KtClassOrObject): IrBody {
|
fun generatePrimaryConstructorBody(ktClassOrObject: KtPureClassOrObject): IrBody {
|
||||||
val irBlockBody = IrBlockBodyImpl(ktClassOrObject.startOffset, ktClassOrObject.endOffset)
|
val irBlockBody = IrBlockBodyImpl(ktClassOrObject.pureStartOffset, ktClassOrObject.pureEndOffset)
|
||||||
|
|
||||||
generateSuperConstructorCall(irBlockBody, ktClassOrObject)
|
generateSuperConstructorCall(irBlockBody, ktClassOrObject)
|
||||||
|
|
||||||
val classDescriptor = (scopeOwner as ClassConstructorDescriptor).containingDeclaration
|
val classDescriptor = (scopeOwner as ClassConstructorDescriptor).containingDeclaration
|
||||||
irBlockBody.statements.add(
|
irBlockBody.statements.add(
|
||||||
IrInstanceInitializerCallImpl(
|
IrInstanceInitializerCallImpl(
|
||||||
ktClassOrObject.startOffset, ktClassOrObject.endOffset,
|
ktClassOrObject.pureStartOffset, ktClassOrObject.pureEndOffset,
|
||||||
context.symbolTable.referenceClass(classDescriptor),
|
context.symbolTable.referenceClass(classDescriptor),
|
||||||
context.irBuiltIns.unitType
|
context.irBuiltIns.unitType
|
||||||
)
|
)
|
||||||
@@ -214,11 +217,12 @@ class BodyGenerator(
|
|||||||
return irBlockBody
|
return irBlockBody
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateSuperConstructorCall(irBlockBody: IrBlockBodyImpl, ktClassOrObject: KtClassOrObject) {
|
private fun generateSuperConstructorCall(irBlockBody: IrBlockBodyImpl, ktClassOrObject: KtPureClassOrObject) {
|
||||||
val classDescriptor = getOrFail(BindingContext.CLASS, ktClassOrObject)
|
val classDescriptor = ktClassOrObject.findClassDescriptor(context.bindingContext)
|
||||||
|
|
||||||
when (classDescriptor.kind) {
|
when (classDescriptor.kind) {
|
||||||
ClassKind.ENUM_CLASS -> generateEnumSuperConstructorCall(irBlockBody, ktClassOrObject, classDescriptor)
|
// enums can't be synthetic
|
||||||
|
ClassKind.ENUM_CLASS -> generateEnumSuperConstructorCall(irBlockBody, ktClassOrObject as KtClassOrObject, classDescriptor)
|
||||||
|
|
||||||
ClassKind.ENUM_ENTRY -> {
|
ClassKind.ENUM_ENTRY -> {
|
||||||
irBlockBody.statements.add(
|
irBlockBody.statements.add(
|
||||||
@@ -229,7 +233,8 @@ class BodyGenerator(
|
|||||||
else -> {
|
else -> {
|
||||||
val statementGenerator = createStatementGenerator()
|
val statementGenerator = createStatementGenerator()
|
||||||
|
|
||||||
ktClassOrObject.getSuperTypeList()?.let { ktSuperTypeList ->
|
// synthetic inheritance is not supported yet
|
||||||
|
(ktClassOrObject as? KtClassOrObject)?.getSuperTypeList()?.let { ktSuperTypeList ->
|
||||||
for (ktSuperTypeListEntry in ktSuperTypeList.entries) {
|
for (ktSuperTypeListEntry in ktSuperTypeList.entries) {
|
||||||
if (ktSuperTypeListEntry is KtSuperTypeCallEntry) {
|
if (ktSuperTypeListEntry is KtSuperTypeCallEntry) {
|
||||||
val superConstructorCall = statementGenerator.pregenerateCall(getResolvedCall(ktSuperTypeListEntry)!!)
|
val superConstructorCall = statementGenerator.pregenerateCall(getResolvedCall(ktSuperTypeListEntry)!!)
|
||||||
@@ -253,11 +258,11 @@ class BodyGenerator(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateAnySuperConstructorCall(irBlockBody: IrBlockBodyImpl, ktElement: KtElement) {
|
private fun generateAnySuperConstructorCall(irBlockBody: IrBlockBodyImpl, ktElement: KtPureElement) {
|
||||||
val anyConstructor = context.builtIns.any.constructors.single()
|
val anyConstructor = context.builtIns.any.constructors.single()
|
||||||
irBlockBody.statements.add(
|
irBlockBody.statements.add(
|
||||||
IrDelegatingConstructorCallImpl(
|
IrDelegatingConstructorCallImpl(
|
||||||
ktElement.startOffset, ktElement.endOffset,
|
ktElement.pureStartOffset, ktElement.pureEndOffset,
|
||||||
context.irBuiltIns.unitType,
|
context.irBuiltIns.unitType,
|
||||||
context.symbolTable.referenceConstructor(anyConstructor),
|
context.symbolTable.referenceConstructor(anyConstructor),
|
||||||
anyConstructor
|
anyConstructor
|
||||||
|
|||||||
+28
-10
@@ -31,11 +31,17 @@ import org.jetbrains.kotlin.ir.util.referenceFunction
|
|||||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||||
import org.jetbrains.kotlin.psi.KtDelegatedSuperTypeEntry
|
import org.jetbrains.kotlin.psi.KtDelegatedSuperTypeEntry
|
||||||
import org.jetbrains.kotlin.psi.KtEnumEntry
|
import org.jetbrains.kotlin.psi.KtEnumEntry
|
||||||
|
import org.jetbrains.kotlin.psi.KtPureClassOrObject
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.pureEndOffset
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.pureStartOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
|
import org.jetbrains.kotlin.psi.synthetics.SyntheticClassOrObjectDescriptor
|
||||||
|
import org.jetbrains.kotlin.psi.synthetics.findClassDescriptor
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||||
|
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.TypeProjectionImpl
|
import org.jetbrains.kotlin.types.TypeProjectionImpl
|
||||||
import org.jetbrains.kotlin.types.TypeSubstitutor
|
import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||||
@@ -47,10 +53,10 @@ class ClassGenerator(
|
|||||||
declarationGenerator: DeclarationGenerator
|
declarationGenerator: DeclarationGenerator
|
||||||
) : DeclarationGeneratorExtension(declarationGenerator) {
|
) : DeclarationGeneratorExtension(declarationGenerator) {
|
||||||
|
|
||||||
fun generateClass(ktClassOrObject: KtClassOrObject): IrClass {
|
fun generateClass(ktClassOrObject: KtPureClassOrObject): IrClass {
|
||||||
val classDescriptor = getOrFail(BindingContext.CLASS, ktClassOrObject)
|
val classDescriptor = ktClassOrObject.findClassDescriptor(this.context.bindingContext)
|
||||||
val startOffset = ktClassOrObject.startOffset
|
val startOffset = ktClassOrObject.pureStartOffset
|
||||||
val endOffset = ktClassOrObject.endOffset
|
val endOffset = ktClassOrObject.pureEndOffset
|
||||||
|
|
||||||
return context.symbolTable.declareClass(
|
return context.symbolTable.declareClass(
|
||||||
startOffset, endOffset, IrDeclarationOrigin.DEFINED, classDescriptor
|
startOffset, endOffset, IrDeclarationOrigin.DEFINED, classDescriptor
|
||||||
@@ -73,13 +79,14 @@ class ClassGenerator(
|
|||||||
generateDeclarationsForPrimaryConstructorParameters(irClass, irPrimaryConstructor, ktClassOrObject)
|
generateDeclarationsForPrimaryConstructorParameters(irClass, irPrimaryConstructor, ktClassOrObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
generateMembersDeclaredInSupertypeList(irClass, ktClassOrObject)
|
if (ktClassOrObject is KtClassOrObject) //todo: supertype list for synthetic declarations
|
||||||
|
generateMembersDeclaredInSupertypeList(irClass, ktClassOrObject)
|
||||||
|
|
||||||
generateMembersDeclaredInClassBody(irClass, ktClassOrObject)
|
generateMembersDeclaredInClassBody(irClass, ktClassOrObject)
|
||||||
|
|
||||||
generateFakeOverrideMemberDeclarations(irClass, ktClassOrObject)
|
generateFakeOverrideMemberDeclarations(irClass, ktClassOrObject)
|
||||||
|
|
||||||
if (irClass.isData) {
|
if (irClass.isData && ktClassOrObject is KtClassOrObject) {
|
||||||
generateAdditionalMembersForDataClass(irClass, ktClassOrObject)
|
generateAdditionalMembersForDataClass(irClass, ktClassOrObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +96,7 @@ class ClassGenerator(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateFakeOverrideMemberDeclarations(irClass: IrClass, ktClassOrObject: KtClassOrObject) {
|
private fun generateFakeOverrideMemberDeclarations(irClass: IrClass, ktClassOrObject: KtPureClassOrObject) {
|
||||||
irClass.descriptor.unsubstitutedMemberScope.getContributedDescriptors()
|
irClass.descriptor.unsubstitutedMemberScope.getContributedDescriptors()
|
||||||
.mapNotNull {
|
.mapNotNull {
|
||||||
it.safeAs<CallableMemberDescriptor>().takeIf {
|
it.safeAs<CallableMemberDescriptor>().takeIf {
|
||||||
@@ -317,7 +324,7 @@ class ClassGenerator(
|
|||||||
EnumClassMembersGenerator(declarationGenerator).generateSpecialMembers(irClass)
|
EnumClassMembersGenerator(declarationGenerator).generateSpecialMembers(irClass)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generatePrimaryConstructor(irClass: IrClass, ktClassOrObject: KtClassOrObject): IrConstructor? {
|
private fun generatePrimaryConstructor(irClass: IrClass, ktClassOrObject: KtPureClassOrObject): IrConstructor? {
|
||||||
val classDescriptor = irClass.descriptor
|
val classDescriptor = irClass.descriptor
|
||||||
val primaryConstructorDescriptor = classDescriptor.unsubstitutedPrimaryConstructor ?: return null
|
val primaryConstructorDescriptor = classDescriptor.unsubstitutedPrimaryConstructor ?: return null
|
||||||
|
|
||||||
@@ -331,7 +338,7 @@ class ClassGenerator(
|
|||||||
private fun generateDeclarationsForPrimaryConstructorParameters(
|
private fun generateDeclarationsForPrimaryConstructorParameters(
|
||||||
irClass: IrClass,
|
irClass: IrClass,
|
||||||
irPrimaryConstructor: IrConstructor,
|
irPrimaryConstructor: IrConstructor,
|
||||||
ktClassOrObject: KtClassOrObject
|
ktClassOrObject: KtPureClassOrObject
|
||||||
) {
|
) {
|
||||||
ktClassOrObject.primaryConstructor?.let { ktPrimaryConstructor ->
|
ktClassOrObject.primaryConstructor?.let { ktPrimaryConstructor ->
|
||||||
irPrimaryConstructor.valueParameters.forEach {
|
irPrimaryConstructor.valueParameters.forEach {
|
||||||
@@ -349,12 +356,23 @@ class ClassGenerator(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateMembersDeclaredInClassBody(irClass: IrClass, ktClassOrObject: KtClassOrObject) {
|
private fun generateMembersDeclaredInClassBody(irClass: IrClass, ktClassOrObject: KtPureClassOrObject) {
|
||||||
|
// generate real body declarations
|
||||||
ktClassOrObject.getBody()?.let { ktClassBody ->
|
ktClassOrObject.getBody()?.let { ktClassBody ->
|
||||||
ktClassBody.declarations.mapTo(irClass.declarations) { ktDeclaration ->
|
ktClassBody.declarations.mapTo(irClass.declarations) { ktDeclaration ->
|
||||||
declarationGenerator.generateClassMemberDeclaration(ktDeclaration, irClass.descriptor)
|
declarationGenerator.generateClassMemberDeclaration(ktDeclaration, irClass.descriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generate synthetic nested classes (including companion)
|
||||||
|
irClass.descriptor
|
||||||
|
.unsubstitutedMemberScope
|
||||||
|
.getContributedDescriptors(DescriptorKindFilter.CLASSIFIERS, MemberScope.ALL_NAME_FILTER)
|
||||||
|
.asSequence()
|
||||||
|
.filterIsInstance<SyntheticClassOrObjectDescriptor>()
|
||||||
|
.mapTo(irClass.declarations) { declarationGenerator.generateSyntheticClassOrObject(it.syntheticDeclaration) }
|
||||||
|
|
||||||
|
// synthetic functions and properties to classes must be contributed by corresponding lowering
|
||||||
}
|
}
|
||||||
|
|
||||||
fun generateEnumEntry(ktEnumEntry: KtEnumEntry): IrEnumEntry {
|
fun generateEnumEntry(ktEnumEntry: KtEnumEntry): IrEnumEntry {
|
||||||
|
|||||||
+12
-6
@@ -28,6 +28,8 @@ import org.jetbrains.kotlin.psi.*
|
|||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
import org.jetbrains.kotlin.psi2ir.endOffsetOrUndefined
|
import org.jetbrains.kotlin.psi2ir.endOffsetOrUndefined
|
||||||
|
import org.jetbrains.kotlin.psi2ir.pureEndOffsetOrUndefined
|
||||||
|
import org.jetbrains.kotlin.psi2ir.pureStartOffsetOrUndefined
|
||||||
import org.jetbrains.kotlin.psi2ir.startOffsetOrUndefined
|
import org.jetbrains.kotlin.psi2ir.startOffsetOrUndefined
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||||
@@ -56,6 +58,10 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun generateSyntheticClassOrObject(syntheticDeclaration: KtPureClassOrObject): IrClass {
|
||||||
|
return generateClassOrObjectDeclaration(syntheticDeclaration)
|
||||||
|
}
|
||||||
|
|
||||||
fun generateClassMemberDeclaration(ktDeclaration: KtDeclaration, classDescriptor: ClassDescriptor): IrDeclaration =
|
fun generateClassMemberDeclaration(ktDeclaration: KtDeclaration, classDescriptor: ClassDescriptor): IrDeclaration =
|
||||||
when (ktDeclaration) {
|
when (ktDeclaration) {
|
||||||
is KtAnonymousInitializer ->
|
is KtAnonymousInitializer ->
|
||||||
@@ -71,7 +77,7 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
|
|||||||
private fun generateEnumEntryDeclaration(ktEnumEntry: KtEnumEntry): IrEnumEntry =
|
private fun generateEnumEntryDeclaration(ktEnumEntry: KtEnumEntry): IrEnumEntry =
|
||||||
ClassGenerator(this).generateEnumEntry(ktEnumEntry)
|
ClassGenerator(this).generateEnumEntry(ktEnumEntry)
|
||||||
|
|
||||||
fun generateClassOrObjectDeclaration(ktClassOrObject: KtClassOrObject): IrClass =
|
fun generateClassOrObjectDeclaration(ktClassOrObject: KtPureClassOrObject): IrClass =
|
||||||
ClassGenerator(this).generateClass(ktClassOrObject)
|
ClassGenerator(this).generateClass(ktClassOrObject)
|
||||||
|
|
||||||
private fun generateTypeAliasDeclaration(ktDeclaration: KtTypeAlias): IrDeclaration =
|
private fun generateTypeAliasDeclaration(ktDeclaration: KtTypeAlias): IrDeclaration =
|
||||||
@@ -135,7 +141,7 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
|
|||||||
fun generateInitializerBody(scopeOwnerSymbol: IrSymbol, ktBody: KtExpression): IrExpressionBody =
|
fun generateInitializerBody(scopeOwnerSymbol: IrSymbol, ktBody: KtExpression): IrExpressionBody =
|
||||||
createBodyGenerator(scopeOwnerSymbol).generateExpressionBody(ktBody)
|
createBodyGenerator(scopeOwnerSymbol).generateExpressionBody(ktBody)
|
||||||
|
|
||||||
fun generateFakeOverrideDeclaration(memberDescriptor: CallableMemberDescriptor, ktElement: KtElement): IrDeclaration {
|
fun generateFakeOverrideDeclaration(memberDescriptor: CallableMemberDescriptor, ktElement: KtPureElement): IrDeclaration {
|
||||||
assert(memberDescriptor.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) {
|
assert(memberDescriptor.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) {
|
||||||
"Fake override expected: $memberDescriptor"
|
"Fake override expected: $memberDescriptor"
|
||||||
}
|
}
|
||||||
@@ -149,9 +155,9 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateFakeOverrideProperty(propertyDescriptor: PropertyDescriptor, ktElement: KtElement): IrProperty {
|
private fun generateFakeOverrideProperty(propertyDescriptor: PropertyDescriptor, ktElement: KtPureElement): IrProperty {
|
||||||
val startOffset = ktElement.startOffset
|
val startOffset = ktElement.pureStartOffsetOrUndefined
|
||||||
val endOffset = ktElement.endOffset
|
val endOffset = ktElement.pureEndOffsetOrUndefined
|
||||||
|
|
||||||
val backingField =
|
val backingField =
|
||||||
if (propertyDescriptor.getter == null)
|
if (propertyDescriptor.getter == null)
|
||||||
@@ -173,7 +179,7 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateFakeOverrideFunction(functionDescriptor: FunctionDescriptor, ktElement: KtElement): IrSimpleFunction =
|
private fun generateFakeOverrideFunction(functionDescriptor: FunctionDescriptor, ktElement: KtPureElement): IrSimpleFunction =
|
||||||
FunctionGenerator(this).generateFakeOverrideFunction(functionDescriptor, ktElement)
|
FunctionGenerator(this).generateFakeOverrideFunction(functionDescriptor, ktElement)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
-16
@@ -25,10 +25,10 @@ import org.jetbrains.kotlin.ir.expressions.impl.*
|
|||||||
import org.jetbrains.kotlin.ir.util.declareSimpleFunctionWithOverrides
|
import org.jetbrains.kotlin.ir.util.declareSimpleFunctionWithOverrides
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.pureEndOffset
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.pureStartOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
import org.jetbrains.kotlin.psi2ir.endOffsetOrUndefined
|
import org.jetbrains.kotlin.psi2ir.*
|
||||||
import org.jetbrains.kotlin.psi2ir.isConstructorDelegatingToSuper
|
|
||||||
import org.jetbrains.kotlin.psi2ir.startOffsetOrUndefined
|
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||||
@@ -58,7 +58,7 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
|
|||||||
generateLambdaBody(ktFunction)
|
generateLambdaBody(ktFunction)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun generateFakeOverrideFunction(functionDescriptor: FunctionDescriptor, ktElement: KtElement): IrSimpleFunction =
|
fun generateFakeOverrideFunction(functionDescriptor: FunctionDescriptor, ktElement: KtPureElement): IrSimpleFunction =
|
||||||
declareSimpleFunctionInner(functionDescriptor, ktElement, IrDeclarationOrigin.FAKE_OVERRIDE).buildWithScope { irFunction ->
|
declareSimpleFunctionInner(functionDescriptor, ktElement, IrDeclarationOrigin.FAKE_OVERRIDE).buildWithScope { irFunction ->
|
||||||
generateFunctionParameterDeclarationsAndReturnType(irFunction, ktElement, null)
|
generateFunctionParameterDeclarationsAndReturnType(irFunction, ktElement, null)
|
||||||
}
|
}
|
||||||
@@ -77,17 +77,17 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
|
|||||||
|
|
||||||
private fun declareSimpleFunctionInner(
|
private fun declareSimpleFunctionInner(
|
||||||
descriptor: FunctionDescriptor,
|
descriptor: FunctionDescriptor,
|
||||||
ktElement: KtElement,
|
ktElement: KtPureElement,
|
||||||
origin: IrDeclarationOrigin
|
origin: IrDeclarationOrigin
|
||||||
): IrSimpleFunction =
|
): IrSimpleFunction =
|
||||||
context.symbolTable.declareSimpleFunctionWithOverrides(
|
context.symbolTable.declareSimpleFunctionWithOverrides(
|
||||||
ktElement.startOffset, ktElement.endOffset, origin,
|
ktElement.pureStartOffset, ktElement.pureEndOffset, origin,
|
||||||
descriptor
|
descriptor
|
||||||
)
|
)
|
||||||
|
|
||||||
fun generateFunctionParameterDeclarationsAndReturnType(
|
fun generateFunctionParameterDeclarationsAndReturnType(
|
||||||
irFunction: IrFunction,
|
irFunction: IrFunction,
|
||||||
ktParameterOwner: KtElement?,
|
ktParameterOwner: KtPureElement?,
|
||||||
ktReceiverParameterElement: KtElement?
|
ktReceiverParameterElement: KtElement?
|
||||||
) {
|
) {
|
||||||
declarationGenerator.generateScopedTypeParameterDeclarations(irFunction, irFunction.descriptor.propertyIfAccessor.typeParameters)
|
declarationGenerator.generateScopedTypeParameterDeclarations(irFunction, irFunction.descriptor.propertyIfAccessor.typeParameters)
|
||||||
@@ -208,7 +208,7 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
|
|||||||
|
|
||||||
fun generatePrimaryConstructor(
|
fun generatePrimaryConstructor(
|
||||||
primaryConstructorDescriptor: ClassConstructorDescriptor,
|
primaryConstructorDescriptor: ClassConstructorDescriptor,
|
||||||
ktClassOrObject: KtClassOrObject
|
ktClassOrObject: KtPureClassOrObject
|
||||||
): IrConstructor =
|
): IrConstructor =
|
||||||
declareConstructor(ktClassOrObject, ktClassOrObject.primaryConstructor ?: ktClassOrObject, primaryConstructorDescriptor) {
|
declareConstructor(ktClassOrObject, ktClassOrObject.primaryConstructor ?: ktClassOrObject, primaryConstructorDescriptor) {
|
||||||
if (
|
if (
|
||||||
@@ -233,13 +233,13 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
|
|||||||
|
|
||||||
|
|
||||||
private inline fun declareConstructor(
|
private inline fun declareConstructor(
|
||||||
ktConstructorElement: KtElement,
|
ktConstructorElement: KtPureElement,
|
||||||
ktParametersElement: KtElement,
|
ktParametersElement: KtPureElement,
|
||||||
constructorDescriptor: ClassConstructorDescriptor,
|
constructorDescriptor: ClassConstructorDescriptor,
|
||||||
generateBody: BodyGenerator.() -> IrBody?
|
generateBody: BodyGenerator.() -> IrBody?
|
||||||
): IrConstructor =
|
): IrConstructor =
|
||||||
context.symbolTable.declareConstructor(
|
context.symbolTable.declareConstructor(
|
||||||
ktConstructorElement.startOffset, ktConstructorElement.endOffset, IrDeclarationOrigin.DEFINED,
|
ktConstructorElement.pureStartOffset, ktConstructorElement.pureEndOffset, IrDeclarationOrigin.DEFINED,
|
||||||
constructorDescriptor
|
constructorDescriptor
|
||||||
).buildWithScope { irConstructor ->
|
).buildWithScope { irConstructor ->
|
||||||
generateValueParameterDeclarations(irConstructor, ktParametersElement, null)
|
generateValueParameterDeclarations(irConstructor, ktParametersElement, null)
|
||||||
@@ -254,8 +254,8 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
|
|||||||
|
|
||||||
private fun generateValueParameterDeclarations(
|
private fun generateValueParameterDeclarations(
|
||||||
irFunction: IrFunction,
|
irFunction: IrFunction,
|
||||||
ktParameterOwner: KtElement?,
|
ktParameterOwner: KtPureElement?,
|
||||||
ktReceiverParameterElement: KtElement?,
|
ktReceiverParameterElement: KtPureElement?,
|
||||||
withDefaultValues: Boolean = true
|
withDefaultValues: Boolean = true
|
||||||
) {
|
) {
|
||||||
val functionDescriptor = irFunction.descriptor
|
val functionDescriptor = irFunction.descriptor
|
||||||
@@ -291,13 +291,13 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
|
|||||||
|
|
||||||
private fun generateReceiverParameterDeclaration(
|
private fun generateReceiverParameterDeclaration(
|
||||||
receiverParameterDescriptor: ReceiverParameterDescriptor,
|
receiverParameterDescriptor: ReceiverParameterDescriptor,
|
||||||
ktElement: KtElement?
|
ktElement: KtPureElement?
|
||||||
): IrValueParameter =
|
): IrValueParameter =
|
||||||
declareParameter(receiverParameterDescriptor, ktElement)
|
declareParameter(receiverParameterDescriptor, ktElement)
|
||||||
|
|
||||||
private fun declareParameter(descriptor: ParameterDescriptor, ktElement: KtElement?) =
|
private fun declareParameter(descriptor: ParameterDescriptor, ktElement: KtPureElement?) =
|
||||||
context.symbolTable.declareValueParameter(
|
context.symbolTable.declareValueParameter(
|
||||||
ktElement.startOffsetOrUndefined, ktElement.endOffsetOrUndefined,
|
ktElement.pureStartOffsetOrUndefined, ktElement.pureEndOffsetOrUndefined,
|
||||||
IrDeclarationOrigin.DEFINED,
|
IrDeclarationOrigin.DEFINED,
|
||||||
descriptor, descriptor.type.toIrType(),
|
descriptor, descriptor.type.toIrType(),
|
||||||
(descriptor as? ValueParameterDescriptor)?.varargElementType?.toIrType()
|
(descriptor as? ValueParameterDescriptor)?.varargElementType?.toIrType()
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ abstract class KtClassOrObject :
|
|||||||
|
|
||||||
fun getAnonymousInitializers(): List<KtAnonymousInitializer> = getBody()?.anonymousInitializers.orEmpty()
|
fun getAnonymousInitializers(): List<KtAnonymousInitializer> = getBody()?.anonymousInitializers.orEmpty()
|
||||||
|
|
||||||
fun getBody(): KtClassBody? = getStubOrPsiChild(KtStubElementTypes.CLASS_BODY)
|
override fun getBody(): KtClassBody? = getStubOrPsiChild(KtStubElementTypes.CLASS_BODY)
|
||||||
|
|
||||||
inline fun <reified T : KtDeclaration> addDeclaration(declaration: T): T {
|
inline fun <reified T : KtDeclaration> addDeclaration(declaration: T): T {
|
||||||
val body = getOrCreateBody()
|
val body = getOrCreateBody()
|
||||||
|
|||||||
@@ -57,5 +57,8 @@ public interface KtPureClassOrObject extends KtPureElement, KtDeclarationContain
|
|||||||
@NotNull
|
@NotNull
|
||||||
@ReadOnly
|
@ReadOnly
|
||||||
List<KtSecondaryConstructor> getSecondaryConstructors();
|
List<KtSecondaryConstructor> getSecondaryConstructors();
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
KtClassBody getBody();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,10 +26,7 @@ import com.intellij.psi.search.SearchScope
|
|||||||
import com.intellij.psi.util.PsiTreeUtil
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
import org.jetbrains.kotlin.diagnostics.PsiDiagnosticUtils
|
import org.jetbrains.kotlin.diagnostics.PsiDiagnosticUtils
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.KtFileAnnotationList
|
|
||||||
import org.jetbrains.kotlin.psi.KtModifierList
|
|
||||||
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
// NOTE: in this file we collect only LANGUAGE INDEPENDENT methods working with PSI and not modifying it
|
// NOTE: in this file we collect only LANGUAGE INDEPENDENT methods working with PSI and not modifying it
|
||||||
@@ -294,6 +291,13 @@ val PsiElement.startOffset: Int
|
|||||||
val PsiElement.endOffset: Int
|
val PsiElement.endOffset: Int
|
||||||
get() = textRange.endOffset
|
get() = textRange.endOffset
|
||||||
|
|
||||||
|
val KtPureElement.pureStartOffset: Int
|
||||||
|
get() = psiOrParent.textRange.startOffset
|
||||||
|
|
||||||
|
val KtPureElement.pureEndOffset: Int
|
||||||
|
get() = psiOrParent.textRange.endOffset
|
||||||
|
|
||||||
|
|
||||||
fun PsiElement.getStartOffsetIn(ancestor: PsiElement): Int {
|
fun PsiElement.getStartOffsetIn(ancestor: PsiElement): Int {
|
||||||
var offset = 0
|
var offset = 0
|
||||||
var parent = this
|
var parent = this
|
||||||
|
|||||||
Reference in New Issue
Block a user