Psi2ir: support mode without generation of bodies

This is needed for kapt + JVM IR.

 #KT-49682
This commit is contained in:
Alexander Udalov
2021-12-20 22:19:46 +01:00
parent b3820564b0
commit 25edf64daf
19 changed files with 250 additions and 34 deletions
@@ -53,12 +53,14 @@ interface CodegenFactory {
val bindingContext: BindingContext, val bindingContext: BindingContext,
val languageVersionSettings: LanguageVersionSettings, val languageVersionSettings: LanguageVersionSettings,
val ignoreErrors: Boolean, val ignoreErrors: Boolean,
val skipBodies: Boolean,
) { ) {
companion object { companion object {
fun fromGenerationState(state: GenerationState): IrConversionInput = fun fromGenerationState(state: GenerationState): IrConversionInput =
with(state) { with(state) {
IrConversionInput( IrConversionInput(
project, files, configuration, module, originalFrontendBindingContext, languageVersionSettings, ignoreErrors project, files, configuration, module, originalFrontendBindingContext, languageVersionSettings, ignoreErrors,
skipBodies = !state.classBuilderMode.generateBodies
) )
} }
} }
@@ -269,7 +269,8 @@ object KotlinToJVMBytecodeCompiler {
result.moduleDescriptor, result.moduleDescriptor,
result.bindingContext, result.bindingContext,
configuration.languageVersionSettings, configuration.languageVersionSettings,
false, ignoreErrors = false,
skipBodies = false,
) )
val performanceManager = environment.configuration[CLIConfigurationKeys.PERF_MANAGER] val performanceManager = environment.configuration[CLIConfigurationKeys.PERF_MANAGER]
@@ -88,7 +88,14 @@ open class JvmIrCodegenFactory(
val symbolTable = SymbolTable(signaturer, IrFactoryImpl) val symbolTable = SymbolTable(signaturer, IrFactoryImpl)
mangler to symbolTable mangler to symbolTable
} }
val psi2ir = Psi2IrTranslator(input.languageVersionSettings, Psi2IrConfiguration(input.ignoreErrors)) val psi2ir = Psi2IrTranslator(
input.languageVersionSettings,
Psi2IrConfiguration(
input.ignoreErrors,
allowUnboundSymbols = false,
input.skipBodies,
)
)
val messageLogger = input.configuration[IrMessageLogger.IR_MESSAGE_LOGGER] ?: IrMessageLogger.None val messageLogger = input.configuration[IrMessageLogger.IR_MESSAGE_LOGGER] ?: IrMessageLogger.None
val psi2irContext = psi2ir.createGeneratorContext( val psi2irContext = psi2ir.createGeneratorContext(
input.module, input.module,
@@ -121,7 +121,7 @@ private class MoveOrCopyCompanionObjectFieldsLowering(val context: JvmBackendCon
parent = newParent parent = newParent
correspondingPropertySymbol = property.symbol correspondingPropertySymbol = property.symbol
annotations += oldField.annotations annotations += oldField.annotations
initializer = with(oldField.initializer!!) { initializer = oldField.initializer?.run {
IrExpressionBodyImpl(startOffset, endOffset, (expression as IrConst<*>).shallowCopy()) IrExpressionBodyImpl(startOffset, endOffset, (expression as IrConst<*>).shallowCopy())
} }
@@ -18,5 +18,9 @@ package org.jetbrains.kotlin.psi2ir
class Psi2IrConfiguration( class Psi2IrConfiguration(
val ignoreErrors: Boolean = false, val ignoreErrors: Boolean = false,
val allowUnboundSymbols: Boolean = false val allowUnboundSymbols: Boolean = false,
) val skipBodies: Boolean = false,
) {
val generateBodies: Boolean
get() = !skipBodies
}
@@ -104,7 +104,9 @@ class DelegatedPropertyGenerator(declarationGenerator: DeclarationGenerator) : D
accessorDescriptor accessorDescriptor
).buildWithScope { irAccessor -> ).buildWithScope { irAccessor ->
FunctionGenerator(declarationGenerator).generateFunctionParameterDeclarationsAndReturnType(irAccessor, ktProperty, null, emptyList()) FunctionGenerator(declarationGenerator).generateFunctionParameterDeclarationsAndReturnType(irAccessor, ktProperty, null, emptyList())
irAccessor.body = generateBody(irAccessor) if (context.configuration.generateBodies) {
irAccessor.body = generateBody(irAccessor)
}
} }
@@ -54,7 +54,8 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
origin, origin,
getOrFail(BindingContext.FUNCTION, ktFunction) getOrFail(BindingContext.FUNCTION, ktFunction)
) { ) {
ktFunction.bodyExpression?.let { generateFunctionBody(it) } if (context.configuration.skipBodies) null
else ktFunction.bodyExpression?.let { generateFunctionBody(it) }
} }
fun generateLambdaFunctionDeclaration(ktFunction: KtFunctionLiteral): IrSimpleFunction { fun generateLambdaFunctionDeclaration(ktFunction: KtFunctionLiteral): IrSimpleFunction {
@@ -131,12 +132,14 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
irAccessor, ktAccessor ?: ktProperty, ktProperty.receiverTypeReference, irAccessor, ktAccessor ?: ktProperty, ktProperty.receiverTypeReference,
ktProperty.contextReceivers.mapNotNull { it.typeReference() } ktProperty.contextReceivers.mapNotNull { it.typeReference() }
) )
val ktBodyExpression = ktAccessor?.bodyExpression if (context.configuration.generateBodies) {
irAccessor.body = val ktBodyExpression = ktAccessor?.bodyExpression
if (ktBodyExpression != null) irAccessor.body =
createBodyGenerator(irAccessor.symbol).generateFunctionBody(ktBodyExpression) if (ktBodyExpression != null)
else createBodyGenerator(irAccessor.symbol).generateFunctionBody(ktBodyExpression)
generateDefaultAccessorBody(descriptor, irAccessor) else
generateDefaultAccessorBody(descriptor, irAccessor)
}
} }
fun generateDefaultAccessorForPrimaryConstructorParameter( fun generateDefaultAccessorForPrimaryConstructorParameter(
@@ -76,7 +76,7 @@ class PropertyGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
): IrProperty { ): IrProperty {
val irPropertyType = propertyDescriptor.type.toIrType() val irPropertyType = propertyDescriptor.type.toIrType()
return generateSyntheticPropertyWithInitializer(ktDeclarationContainer, propertyDescriptor, generateSyntheticAccessors) { return generateSyntheticPropertyWithInitializer(ktDeclarationContainer, propertyDescriptor, generateSyntheticAccessors) {
if (irValueParameter == null) null if (irValueParameter == null || context.configuration.skipBodies) null
else { else {
context.irFactory.createExpressionBody( context.irFactory.createExpressionBody(
IrGetValueImpl( IrGetValueImpl(
@@ -205,7 +205,8 @@ class PropertyGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
} }
} }
declarationGenerator.generateInitializerBody(irField.symbol, ktInitializer) if (context.configuration.skipBodies) null
else declarationGenerator.generateInitializerBody(irField.symbol, ktInitializer)
} }
} }
else else
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
class Test { class Test {
private val foo = Example.FOO private val foo = Example.FOO
@@ -0,0 +1,179 @@
import java.lang.System;
@kotlin.Metadata()
public final class Test {
@org.jetbrains.annotations.NotNull()
private final Test.Companion.Example foo;
@org.jetbrains.annotations.NotNull()
public static final Test.Companion Companion = null;
public Test() {
super();
}
@kotlin.Metadata()
public static final class Companion {
private Companion() {
super();
}
@kotlin.Metadata()
public static enum Example {
/*public static final*/ FOO /* = new Example() */;
Example() {
}
}
}
}
////////////////////
import java.lang.System;
@kotlin.Metadata()
public final class Test2 {
@org.jetbrains.annotations.NotNull()
private final Test2.Amigo.Example foo;
@org.jetbrains.annotations.NotNull()
public static final Test2.Amigo Amigo = null;
public Test2() {
super();
}
@kotlin.Metadata()
public static final class Amigo {
private Amigo() {
super();
}
@kotlin.Metadata()
public static enum Example {
/*public static final*/ FOO /* = new Example() */;
Example() {
}
}
}
}
////////////////////
import java.lang.System;
@kotlin.Metadata()
public final class Test3 {
@org.jetbrains.annotations.NotNull()
private final Test3.Amigo.Example foo = null;
public Test3() {
super();
}
@kotlin.Metadata()
public static final class Amigo {
@org.jetbrains.annotations.NotNull()
public static final Test3.Amigo INSTANCE = null;
private Amigo() {
super();
}
@kotlin.Metadata()
public static enum Example {
/*public static final*/ FOO /* = new Example() */;
Example() {
}
}
}
}
////////////////////
import java.lang.System;
@kotlin.Metadata()
public final class Test4 {
private final int foo = 0;
@org.jetbrains.annotations.NotNull()
public static final Test4.Companion Companion = null;
public Test4() {
super();
}
@kotlin.Metadata()
public static final class Companion {
private Companion() {
super();
}
@kotlin.Metadata()
public static final class Foo {
public static final int constProperty = 1;
@org.jetbrains.annotations.NotNull()
public static final Test4.Companion.Foo INSTANCE = null;
private Foo() {
super();
}
}
}
}
////////////////////
import java.lang.System;
@kotlin.Metadata()
public final class Test5 {
@org.jetbrains.annotations.NotNull()
private final Test5.Amigos.Companion.Goo.Example foo;
public Test5() {
super();
}
@kotlin.Metadata()
public static final class Amigos {
@org.jetbrains.annotations.NotNull()
public static final Test5.Amigos.Companion Companion = null;
public Amigos() {
super();
}
@kotlin.Metadata()
public static final class Companion {
private Companion() {
super();
}
@kotlin.Metadata()
public static final class Goo {
public Goo() {
super();
}
@kotlin.Metadata()
public static enum Example {
/*public static final*/ FOO /* = new Example() */;
Example() {
}
}
}
}
}
}
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
class GenericRawSignatures { class GenericRawSignatures {
fun <T> genericFun(): T? = null fun <T> genericFun(): T? = null
fun nonGenericFun(): String? = null fun nonGenericFun(): String? = null
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
fun crashMe(values: List<String>): String { fun crashMe(values: List<String>): String {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
import java.util.Date import java.util.Date
fun Date(double: Double): Date = Date(double.times(1000).toLong()) fun Date(double: Double): Date = Date(double.times(1000).toLong())
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
interface Intf { interface Intf {
fun foo(abc: String) fun foo(abc: String)
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
class CrashMe { class CrashMe {
val resources = 1 val resources = 1
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
class Test { class Test {
class Nested { class Nested {
class NestedNested class NestedNested
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
open class Test { open class Test {
open fun getTestNoSuspend(text: String): String { open fun getTestNoSuspend(text: String): String {
@@ -0,0 +1,35 @@
import java.lang.System;
@kotlin.Metadata()
public class Test {
public Test() {
super();
}
@org.jetbrains.annotations.NotNull()
public java.lang.String getTestNoSuspend(@org.jetbrains.annotations.NotNull()
java.lang.String text) {
return null;
}
@org.jetbrains.annotations.Nullable()
public java.lang.Object getTest(@org.jetbrains.annotations.NotNull()
java.lang.String text, @org.jetbrains.annotations.NotNull()
kotlin.coroutines.Continuation<? super java.lang.String> $completion) {
return null;
}
@org.jetbrains.annotations.NotNull()
public java.lang.String getTestNoSuspendInvalid(@org.jetbrains.annotations.NotNull()
java.lang.String p0_55085957) {
return null;
}
@org.jetbrains.annotations.Nullable()
public java.lang.Object getTestInvalid(@org.jetbrains.annotations.NotNull()
java.lang.String p0_55085957, @org.jetbrains.annotations.NotNull()
kotlin.coroutines.Continuation<? super java.lang.String> $completion) {
return null;
}
}
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
package test package test
/** /**