JVM IR: build all needed stdlib symbols in JvmSymbols manually

This removes the mandatory dependency of all JVM IR tests on
kotlin-stdlib (ConfigurationKind.ALL in all IR test cases) and speeds up
tests which don't need kotiln-stdlib by about 20%. Another advantage of
this method is that all required dependencies are listed in one file,
are easy to grasp, and changes to the related code generation can be
done independently of the corresponding changes in the actual library,
which may help in bootstrapping the compiler
This commit is contained in:
Alexander Udalov
2019-03-26 18:19:16 +01:00
parent 6b7eba485b
commit b32241c967
17 changed files with 448 additions and 138 deletions
@@ -6,11 +6,16 @@
package org.jetbrains.kotlin.ir.builders.declarations
import org.jetbrains.kotlin.backend.common.descriptors.*
import org.jetbrains.kotlin.backend.common.ir.copyTo
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.*
import org.jetbrains.kotlin.ir.symbols.impl.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
fun IrClassBuilder.buildClass(): IrClass {
val wrappedDescriptor = WrappedClassDescriptor()
@@ -47,6 +52,29 @@ inline fun buildField(b: IrFieldBuilder.() -> Unit) =
buildField()
}
fun IrPropertyBuilder.buildProperty(): IrProperty {
val wrappedDescriptor = WrappedPropertyDescriptor()
return IrPropertyImpl(
startOffset, endOffset, origin,
IrPropertySymbolImpl(wrappedDescriptor),
name, visibility, modality, isVar, isConst, isLateinit, isDelegated, isExternal
).also {
wrappedDescriptor.bind(it)
}
}
inline fun buildProperty(b: IrPropertyBuilder.() -> Unit) =
IrPropertyBuilder().run {
b()
buildProperty()
}
inline fun IrDeclarationContainer.addProperty(b: IrPropertyBuilder.() -> Unit): IrProperty =
buildProperty(b).also { property ->
declarations.add(property)
property.parent = this@addProperty
}
fun IrFunctionBuilder.buildFun(): IrSimpleFunction {
val wrappedDescriptor = WrappedSimpleFunctionDescriptor()
return IrFunctionImpl(
@@ -78,12 +106,43 @@ inline fun buildFun(b: IrFunctionBuilder.() -> Unit): IrSimpleFunction =
buildFun()
}
inline fun IrDeclarationContainer.addFunction(b: IrFunctionBuilder.() -> Unit): IrSimpleFunction =
buildFun(b).also { function ->
declarations.add(function)
function.parent = this@addFunction
}
fun IrDeclarationContainer.addFunction(
name: String,
returnType: IrType,
modality: Modality = Modality.FINAL,
isStatic: Boolean = false
): IrSimpleFunction =
addFunction {
this.name = Name.identifier(name)
this.returnType = returnType
this.modality = modality
}.apply {
if (!isStatic) {
dispatchReceiverParameter = parentAsClass.thisReceiver!!.copyTo(this)
}
}
inline fun buildConstructor(b: IrFunctionBuilder.() -> Unit): IrConstructor =
IrFunctionBuilder().run {
b()
buildConstructor()
}
inline fun IrClass.addConstructor(b: IrFunctionBuilder.() -> Unit = {}): IrConstructor =
buildConstructor {
b()
returnType = defaultType
}.also { constructor ->
declarations.add(constructor)
constructor.parent = this@addConstructor
}
fun IrValueParameterBuilder.build(): IrValueParameter {
val wrappedDescriptor = WrappedValueParameterDescriptor()
return IrValueParameterImpl(
@@ -113,9 +172,40 @@ inline fun IrFunction.addValueParameter(b: IrValueParameterBuilder.() -> Unit):
}
}
fun IrFunction.addValueParameter(name: String, type: IrType, origin: IrDeclarationOrigin): IrValueParameter =
fun IrFunction.addValueParameter(name: String, type: IrType, origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED): IrValueParameter =
addValueParameter {
this.name = Name.identifier(name)
this.type = type
this.origin = origin
}
fun IrTypeParameterBuilder.build(): IrTypeParameter {
val wrappedDescriptor = WrappedTypeParameterDescriptor()
return IrTypeParameterImpl(
startOffset, endOffset, origin,
IrTypeParameterSymbolImpl(wrappedDescriptor),
name, index, isReified, variance
).also {
wrappedDescriptor.bind(it)
it.superTypes.addAll(superTypes)
}
}
inline fun IrTypeParametersContainer.addTypeParameter(b: IrTypeParameterBuilder.() -> Unit): IrTypeParameter =
IrTypeParameterBuilder().run {
b()
if (index == UNDEFINED_PARAMETER_INDEX) {
index = typeParameters.size
}
build().also { typeParameter ->
typeParameters.add(typeParameter)
typeParameter.parent = this@addTypeParameter
}
}
fun IrTypeParametersContainer.addTypeParameter(name: String, upperBound: IrType, variance: Variance = Variance.INVARIANT): IrTypeParameter =
addTypeParameter {
this.name = Name.identifier(name)
this.variance = variance
this.superTypes.add(upperBound)
}