IR: use IrFactory in IR builders

This commit is contained in:
Alexander Udalov
2020-07-14 18:08:14 +02:00
parent 898dd20a9e
commit d1dc938a5d
58 changed files with 291 additions and 265 deletions
@@ -15,7 +15,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.builders.Scope
import org.jetbrains.kotlin.ir.builders.declarations.buildConstructor
import org.jetbrains.kotlin.ir.builders.declarations.addConstructor
import org.jetbrains.kotlin.ir.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.builders.declarations.buildReceiverParameter
import org.jetbrains.kotlin.ir.builders.declarations.buildTypeParameter
@@ -51,18 +51,14 @@ fun IrClass.addSimpleDelegatingConstructor(
isPrimary: Boolean = false,
origin: IrDeclarationOrigin? = null
): IrConstructor =
buildConstructor {
addConstructor {
val klass = this@addSimpleDelegatingConstructor
this.startOffset = klass.startOffset
this.endOffset = klass.endOffset
this.origin = origin ?: klass.origin
this.visibility = superConstructor.visibility
this.returnType = klass.defaultType
this.isPrimary = isPrimary
}.also { constructor ->
constructor.parent = this
declarations += constructor
constructor.valueParameters = superConstructor.valueParameters.mapIndexed { index, parameter ->
parameter.copyTo(constructor, index = index)
}
@@ -477,7 +473,7 @@ fun IrClass.addFakeOverridesViaIncorrectHeuristic(implementedMembers: List<IrSim
fun createFakeOverride(overriddenFunctions: List<IrSimpleFunction>) =
overriddenFunctions.first().let { irFunction ->
buildFun {
irFunction.factory.buildFun {
origin = IrDeclarationOrigin.FAKE_OVERRIDE
name = irFunction.name
visibility = Visibilities.PUBLIC
@@ -271,7 +271,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
private val unboundFunctionParameters = boundFunctionParameters?.let { functionParameters - it }
private val coroutineClass: IrClass =
buildClass {
context.irFactory.buildClass {
startOffset = irFunction.startOffset
endOffset = irFunction.endOffset
origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -366,7 +366,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
}
fun buildConstructor(): IrConstructor =
buildConstructor {
context.irFactory.buildConstructor {
startOffset = irFunction.startOffset
endOffset = irFunction.endOffset
origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -406,7 +406,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
}
private fun buildFactoryConstructor(boundParams: List<IrValueParameter>): IrConstructor =
buildConstructor {
context.irFactory.buildConstructor {
startOffset = irFunction.startOffset
endOffset = irFunction.endOffset
origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -444,7 +444,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
unboundArgs: List<IrValueParameter>,
superFunctionSymbol: IrSimpleFunctionSymbol?,
coroutineConstructor: IrConstructor
): IrSimpleFunction = buildFun {
): IrSimpleFunction = context.irFactory.buildFun {
startOffset = irFunction.startOffset
endOffset = irFunction.endOffset
origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -500,7 +500,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
functionInvokeFunctionSymbol: IrSimpleFunctionSymbol,
createFunction: IrFunction,
stateMachineFunction: IrFunction
): IrSimpleFunction = buildFun {
): IrSimpleFunction = context.irFactory.buildFun {
startOffset = irFunction.startOffset
endOffset = irFunction.endOffset
origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -550,7 +550,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
stateMachineFunction: IrSimpleFunction,
coroutineClass: IrClass
): IrSimpleFunction {
val function = buildFun {
val function = context.irFactory.buildFun {
startOffset = irFunction.startOffset
endOffset = irFunction.endOffset
origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -617,7 +617,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
fun IrClass.addField(name: Name, type: IrType, isMutable: Boolean): IrField {
val klass = this
return buildField {
return factory.buildField {
this.startOffset = klass.startOffset
this.endOffset = klass.endOffset
this.origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -527,7 +527,7 @@ private fun IrFunction.generateDefaultsFunctionImpl(
): IrFunction {
val newFunction = when (this) {
is IrConstructor ->
buildConstructor {
factory.buildConstructor {
updateFrom(this@generateDefaultsFunctionImpl)
origin = newOrigin
isExternal = false
@@ -536,7 +536,7 @@ private fun IrFunction.generateDefaultsFunctionImpl(
visibility = newVisibility
}
is IrSimpleFunction ->
buildFun(descriptor) {
factory.buildFun(descriptor) {
updateFrom(this@generateDefaultsFunctionImpl)
name = Name.identifier("${this@generateDefaultsFunctionImpl.name}\$default")
origin = newOrigin
@@ -201,7 +201,7 @@ class LateinitUsageLowering(val backendContext: CommonBackendContext) : BodyLowe
private fun CommonBackendContext.buildOrGetNullableField(originalField: IrField): IrField {
if (originalField.type.isMarkedNullable()) return originalField
return mapping.lateInitFieldToNullableField.getOrPut(originalField) {
buildField {
irFactory.buildField {
updateFrom(originalField)
type = originalField.type.makeNullable()
name = originalField.name
@@ -213,4 +213,4 @@ private fun CommonBackendContext.buildOrGetNullableField(originalField: IrField)
}
}
private val IrProperty.isRealLateinit get() = isLateinit && !isFakeOverride
private val IrProperty.isRealLateinit get() = isLateinit && !isFakeOverride
@@ -595,7 +595,7 @@ class LocalDeclarationsLowering(
// TODO: consider using fields to access the closure of enclosing class.
val (capturedValues, capturedTypeParameters) = localFunctionContext.closure
val newDeclaration = buildFun(oldDeclaration.descriptor) {
val newDeclaration = context.irFactory.buildFun(oldDeclaration.descriptor) {
updateFrom(oldDeclaration)
name = newName
visibility = Visibilities.PRIVATE
@@ -694,7 +694,7 @@ class LocalDeclarationsLowering(
val localClassContext = localClasses[oldDeclaration.parent]!!
val capturedValues = localClassContext.closure.capturedValues
val newDeclaration = buildConstructor(oldDeclaration.descriptor) {
val newDeclaration = context.irFactory.buildConstructor(oldDeclaration.descriptor) {
updateFrom(oldDeclaration)
visibility = visibilityPolicy.forConstructor(oldDeclaration, constructorContext.inInlineFunctionScope)
returnType = oldDeclaration.returnType
@@ -730,7 +730,7 @@ class LocalDeclarationsLowering(
fieldType: IrType,
isCrossinline: Boolean
): IrField =
buildField {
context.irFactory.buildField {
this.startOffset = startOffset
this.endOffset = endOffset
this.origin =
@@ -21,7 +21,10 @@ import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
import org.jetbrains.kotlin.ir.expressions.IrTypeOperatorCall
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.types.IrType
@@ -159,7 +162,7 @@ abstract class SingleAbstractMethodLowering(val context: CommonBackendContext) :
context.ir.symbols.functionN(superMethod.valueParameters.size + extensionReceiversCount).owner
val wrappedFunctionType = wrappedFunctionClass.defaultType
val subclass = buildClass {
val subclass = context.irFactory.buildClass {
name = wrapperName
origin = IrDeclarationOrigin.GENERATED_SAM_IMPLEMENTATION
visibility = wrapperVisibility
@@ -9,7 +9,7 @@ import org.jetbrains.kotlin.backend.common.descriptors.synthesizedName
import org.jetbrains.kotlin.backend.common.ir.copyTo
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.*
import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl
import org.jetbrains.kotlin.ir.descriptors.*
import org.jetbrains.kotlin.ir.symbols.impl.*
import org.jetbrains.kotlin.ir.types.IrType
@@ -20,9 +20,9 @@ import org.jetbrains.kotlin.serialization.deserialization.descriptors.Descriptor
import org.jetbrains.kotlin.types.Variance
@PublishedApi
internal fun IrClassBuilder.buildClass(): IrClass {
internal fun IrFactory.buildClass(builder: IrClassBuilder): IrClass = with(builder) {
val wrappedDescriptor = WrappedClassDescriptor()
return IrClassImpl(
createClass(
startOffset, endOffset, origin,
IrClassSymbolImpl(wrappedDescriptor),
name, kind, visibility, modality,
@@ -32,15 +32,16 @@ internal fun IrClassBuilder.buildClass(): IrClass {
}
}
inline fun buildClass(builder: IrClassBuilder.() -> Unit) =
inline fun IrFactory.buildClass(builder: IrClassBuilder.() -> Unit) =
IrClassBuilder().run {
builder()
buildClass()
buildClass(this)
}
fun IrFieldBuilder.buildField(): IrField {
@PublishedApi
internal fun IrFactory.buildField(builder: IrFieldBuilder): IrField = with(builder) {
val wrappedDescriptor = WrappedFieldDescriptor()
return IrFieldImpl(
createField(
startOffset, endOffset, origin,
IrFieldSymbolImpl(wrappedDescriptor),
name, type, visibility, isFinal, isExternal, isStatic,
@@ -50,14 +51,14 @@ fun IrFieldBuilder.buildField(): IrField {
}
}
inline fun buildField(builder: IrFieldBuilder.() -> Unit) =
inline fun IrFactory.buildField(builder: IrFieldBuilder.() -> Unit) =
IrFieldBuilder().run {
builder()
buildField()
buildField(this)
}
inline fun IrDeclarationContainer.addField(builder: IrFieldBuilder.() -> Unit) =
buildField(builder).also { field ->
inline fun IrClass.addField(builder: IrFieldBuilder.() -> Unit) =
factory.buildField(builder).also { field ->
field.parent = this
declarations.add(field)
}
@@ -73,12 +74,15 @@ fun IrClass.addField(fieldName: String, fieldType: IrType, fieldVisibility: Visi
addField(Name.identifier(fieldName), fieldType, fieldVisibility)
@PublishedApi
internal fun IrPropertyBuilder.buildProperty(originalDescriptor: PropertyDescriptor? = null): IrProperty {
internal fun IrFactory.buildProperty(
builder: IrPropertyBuilder,
originalDescriptor: PropertyDescriptor? = null
): IrProperty = with(builder) {
val wrappedDescriptor = when (originalDescriptor) {
is DescriptorWithContainerSource -> WrappedPropertyDescriptorWithContainerSource(originalDescriptor.containerSource)
else -> WrappedPropertyDescriptor()
}
return IrPropertyImpl(
createProperty(
startOffset, endOffset, origin,
IrPropertySymbolImpl(wrappedDescriptor),
name, visibility, modality,
@@ -88,14 +92,14 @@ internal fun IrPropertyBuilder.buildProperty(originalDescriptor: PropertyDescrip
}
}
inline fun buildProperty(originalDescriptor: PropertyDescriptor? = null, builder: IrPropertyBuilder.() -> Unit) =
inline fun IrFactory.buildProperty(originalDescriptor: PropertyDescriptor? = null, builder: IrPropertyBuilder.() -> Unit) =
IrPropertyBuilder().run {
builder()
buildProperty(originalDescriptor)
buildProperty(this, originalDescriptor)
}
inline fun IrDeclarationContainer.addProperty(originalDescriptor: PropertyDescriptor? = null, builder: IrPropertyBuilder.() -> Unit): IrProperty =
buildProperty(originalDescriptor, builder).also { property ->
inline fun IrClass.addProperty(originalDescriptor: PropertyDescriptor? = null, builder: IrPropertyBuilder.() -> Unit): IrProperty =
factory.buildProperty(originalDescriptor, builder).also { property ->
declarations.add(property)
property.parent = this@addProperty
}
@@ -104,25 +108,18 @@ inline fun IrProperty.addGetter(builder: IrFunctionBuilder.() -> Unit = {}): IrS
IrFunctionBuilder().run {
name = Name.special("<get-${this@addGetter.name}>")
builder()
buildFunction().also { getter ->
factory.buildFunction(this).also { getter ->
this@addGetter.getter = getter
getter.correspondingPropertySymbol = this@addGetter.symbol
getter.parent = this@addGetter.parent
}
}
inline fun IrProperty.addSetter(builder: IrFunctionBuilder.() -> Unit = {}): IrSimpleFunction =
IrFunctionBuilder().run {
name = Name.special("<set-${this@addSetter.name}>")
builder()
buildFunction().also { setter ->
this@addSetter.setter = setter
setter.parent = this@addSetter.parent
}
}
@PublishedApi
internal fun IrFunctionBuilder.buildFunction(originalDescriptor: FunctionDescriptor? = null): IrSimpleFunction {
internal fun IrFactory.buildFunction(
builder: IrFunctionBuilder,
originalDescriptor: FunctionDescriptor? = null
): IrSimpleFunction = with(builder) {
// Inlining relies on descriptors for external declarations. When replacing a potentially external function (e.g. in an IrCall),
// we have to ensure that we keep information from the original descriptor so as not to break inlining.
val wrappedDescriptor = when (originalDescriptor) {
@@ -132,7 +129,7 @@ internal fun IrFunctionBuilder.buildFunction(originalDescriptor: FunctionDescrip
null -> WrappedSimpleFunctionDescriptor()
else -> WrappedSimpleFunctionDescriptor(originalDescriptor)
}
return IrFunctionImpl(
createFunction(
startOffset, endOffset, origin,
IrSimpleFunctionSymbolImpl(wrappedDescriptor),
name, visibility, modality, returnType,
@@ -143,11 +140,13 @@ internal fun IrFunctionBuilder.buildFunction(originalDescriptor: FunctionDescrip
}
@PublishedApi
internal fun IrFunctionBuilder.buildConstructor(originalDescriptor: ConstructorDescriptor?): IrConstructor {
internal fun IrFactory.buildConstructor(
builder: IrFunctionBuilder, originalDescriptor: ConstructorDescriptor?
): IrConstructor = with(builder) {
val wrappedDescriptor =
if (originalDescriptor != null) WrappedClassConstructorDescriptor(originalDescriptor.annotations, originalDescriptor.source)
else WrappedClassConstructorDescriptor()
return IrConstructorImpl(
return createConstructor(
startOffset, endOffset, origin,
IrConstructorSymbolImpl(wrappedDescriptor),
Name.special("<init>"),
@@ -158,19 +157,24 @@ internal fun IrFunctionBuilder.buildConstructor(originalDescriptor: ConstructorD
}
}
inline fun buildFun(originalDescriptor: FunctionDescriptor? = null, builder: IrFunctionBuilder.() -> Unit): IrSimpleFunction =
inline fun IrFactory.buildFun(
originalDescriptor: FunctionDescriptor? = null, builder: IrFunctionBuilder.() -> Unit
): IrSimpleFunction =
IrFunctionBuilder().run {
builder()
buildFunction(originalDescriptor)
buildFunction(this, originalDescriptor)
}
inline fun IrDeclarationContainer.addFunction(builder: IrFunctionBuilder.() -> Unit): IrSimpleFunction =
inline fun IrFactory.addFunction(klass: IrDeclarationContainer, builder: IrFunctionBuilder.() -> Unit): IrSimpleFunction =
buildFun(null, builder).also { function ->
declarations.add(function)
function.parent = this@addFunction
klass.declarations.add(function)
function.parent = klass
}
fun IrDeclarationContainer.addFunction(
inline fun IrClass.addFunction(builder: IrFunctionBuilder.() -> Unit): IrSimpleFunction =
factory.addFunction(this, builder)
fun IrClass.addFunction(
name: String,
returnType: IrType,
modality: Modality = Modality.FINAL,
@@ -194,14 +198,17 @@ fun IrDeclarationContainer.addFunction(
}
}
inline fun buildConstructor(originalDescriptor: ConstructorDescriptor? = null, builder: IrFunctionBuilder.() -> Unit): IrConstructor =
inline fun IrFactory.buildConstructor(
originalDescriptor: ConstructorDescriptor? = null,
builder: IrFunctionBuilder.() -> Unit
): IrConstructor =
IrFunctionBuilder().run {
builder()
buildConstructor(originalDescriptor)
buildConstructor(this, originalDescriptor)
}
inline fun IrClass.addConstructor(builder: IrFunctionBuilder.() -> Unit = {}): IrConstructor =
buildConstructor {
factory.buildConstructor {
builder()
returnType = defaultType
}.also { constructor ->
@@ -211,14 +218,15 @@ inline fun IrClass.addConstructor(builder: IrFunctionBuilder.() -> Unit = {}): I
private val RECEIVER_PARAMETER_NAME = Name.special("<this>")
fun buildReceiverParameter(
parent: IrDeclarationParent,
fun <D> buildReceiverParameter(
parent: D,
origin: IrDeclarationOrigin,
type: IrType,
startOffset: Int = parent.startOffset,
endOffset: Int = parent.endOffset
): IrValueParameter = WrappedReceiverParameterDescriptor().let { wrappedDescriptor ->
IrValueParameterImpl(
): IrValueParameter
where D : IrDeclaration, D : IrDeclarationParent = WrappedReceiverParameterDescriptor().let { wrappedDescriptor ->
parent.factory.createValueParameter(
startOffset, endOffset, origin,
IrValueParameterSymbolImpl(wrappedDescriptor),
RECEIVER_PARAMETER_NAME, -1, type, null, isCrossinline = false, isNoinline = false
@@ -229,22 +237,25 @@ fun buildReceiverParameter(
}
@PublishedApi
internal fun IrValueParameterBuilder.buildValueParameter(parent: IrDeclarationParent): IrValueParameter {
val wrappedDescriptor = WrappedValueParameterDescriptor(wrappedDescriptorAnnotations)
return IrValueParameterImpl(
startOffset, endOffset, origin,
IrValueParameterSymbolImpl(wrappedDescriptor),
name, index, type, varargElementType, isCrossInline, isNoinline
).also {
wrappedDescriptor.bind(it)
it.parent = parent
internal fun IrFactory.buildValueParameter(builder: IrValueParameterBuilder, parent: IrDeclarationParent): IrValueParameter =
with(builder) {
val wrappedDescriptor = WrappedValueParameterDescriptor(wrappedDescriptorAnnotations)
return createValueParameter(
startOffset, endOffset, origin,
IrValueParameterSymbolImpl(wrappedDescriptor),
name, index, type, varargElementType, isCrossInline, isNoinline
).also {
wrappedDescriptor.bind(it)
it.parent = parent
}
}
}
inline fun buildValueParameter(parent: IrDeclarationParent, builder: IrValueParameterBuilder.() -> Unit): IrValueParameter =
inline fun <D> buildValueParameter(declaration: D, builder: IrValueParameterBuilder.() -> Unit): IrValueParameter
where D : IrDeclaration, D : IrDeclarationParent =
IrValueParameterBuilder().run {
builder()
buildValueParameter(parent)
declaration.factory.buildValueParameter(this, declaration)
}
inline fun IrFunction.addValueParameter(builder: IrValueParameterBuilder.() -> Unit): IrValueParameter =
@@ -253,8 +264,8 @@ inline fun IrFunction.addValueParameter(builder: IrValueParameterBuilder.() -> U
if (index == UNDEFINED_PARAMETER_INDEX) {
index = valueParameters.size
}
buildValueParameter(this@addValueParameter).also { valueParameter ->
valueParameters += valueParameter
factory.buildValueParameter(this, this@addValueParameter).also { valueParameter ->
valueParameters = valueParameters + valueParameter
}
}
@@ -270,45 +281,41 @@ inline fun IrSimpleFunction.addDispatchReceiver(builder: IrValueParameterBuilder
builder()
index = -1
name = "this".synthesizedName
buildValueParameter(this@addDispatchReceiver).also { receiver ->
factory.buildValueParameter(this, this@addDispatchReceiver).also { receiver ->
dispatchReceiverParameter = receiver
}
}
inline fun IrSimpleFunction.addExtensionReceiver(builder: IrValueParameterBuilder.() -> Unit): IrValueParameter =
fun IrSimpleFunction.addExtensionReceiver(type: IrType, origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED): IrValueParameter =
IrValueParameterBuilder().run {
builder()
index = -1
name = "receiver".synthesizedName
buildValueParameter(this@addExtensionReceiver).also { receiver ->
this.type = type
this.origin = origin
this.index = -1
this.name = "receiver".synthesizedName
factory.buildValueParameter(this, this@addExtensionReceiver).also { receiver ->
extensionReceiverParameter = receiver
}
}
fun IrSimpleFunction.addExtensionReceiver(type: IrType, origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED ): IrValueParameter =
addExtensionReceiver {
this.type = type
this.origin = origin
}
@PublishedApi
internal fun IrTypeParameterBuilder.buildTypeParameter(parent: IrDeclarationParent): IrTypeParameter {
val wrappedDescriptor = WrappedTypeParameterDescriptor()
return IrTypeParameterImpl(
startOffset, endOffset, origin,
IrTypeParameterSymbolImpl(wrappedDescriptor),
name, index, isReified, variance
).also {
wrappedDescriptor.bind(it)
it.superTypes.addAll(superTypes)
it.parent = parent
internal fun IrFactory.buildTypeParameter(builder: IrTypeParameterBuilder, parent: IrDeclarationParent): IrTypeParameter =
with(builder) {
val wrappedDescriptor = WrappedTypeParameterDescriptor()
createTypeParameter(
startOffset, endOffset, origin,
IrTypeParameterSymbolImpl(wrappedDescriptor),
name, index, isReified, variance
).also {
wrappedDescriptor.bind(it)
it.superTypes.addAll(superTypes)
it.parent = parent
}
}
}
inline fun buildTypeParameter(parent: IrDeclarationParent, builder: IrTypeParameterBuilder.() -> Unit): IrTypeParameter =
inline fun buildTypeParameter(parent: IrTypeParametersContainer, builder: IrTypeParameterBuilder.() -> Unit): IrTypeParameter =
IrTypeParameterBuilder().run {
builder()
buildTypeParameter(parent)
parent.factory.buildTypeParameter(this, parent)
}
inline fun IrTypeParametersContainer.addTypeParameter(builder: IrTypeParameterBuilder.() -> Unit): IrTypeParameter =
@@ -317,8 +324,8 @@ inline fun IrTypeParametersContainer.addTypeParameter(builder: IrTypeParameterBu
if (index == UNDEFINED_PARAMETER_INDEX) {
index = typeParameters.size
}
buildTypeParameter(this@addTypeParameter).also { typeParameter ->
typeParameters += typeParameter
factory.buildTypeParameter(this, this@addTypeParameter).also { typeParameter ->
typeParameters = typeParameters + typeParameter
}
}
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.ir.backend.js.utils.Namer
import org.jetbrains.kotlin.ir.builders.declarations.addFunction
import org.jetbrains.kotlin.ir.builders.declarations.addTypeParameter
import org.jetbrains.kotlin.ir.builders.declarations.addValueParameter
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
@@ -326,9 +327,11 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
private fun getInternalClassWithoutPackage(fqName: String) =
context.symbolTable.referenceClass(context.getClass(FqName(fqName)))
private val irFactory: IrFactory get() = context.irFactory
// TODO: unify how we create intrinsic symbols
private fun defineObjectCreateIntrinsic(): IrSimpleFunction {
return externalPackageFragment.addFunction {
return irFactory.addFunction(externalPackageFragment) {
name = Name.identifier("Object\$create")
origin = JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB
}.apply {
@@ -337,7 +340,7 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
}
private fun defineCreateSharedBox(): IrSimpleFunction {
return externalPackageFragment.addFunction {
return irFactory.addFunction(externalPackageFragment) {
name = Name.identifier("\$sharedBox\$create")
origin = JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB
returnType = context.dynamicType
@@ -356,7 +359,7 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
}
private fun defineReadSharedBox(): IrSimpleFunction {
return externalPackageFragment.addFunction {
return irFactory.addFunction(externalPackageFragment) {
name = Name.identifier("\$sharedBox\$read")
origin = JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB
}.apply {
@@ -375,7 +378,7 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
}
private fun defineWriteSharedBox(): IrSimpleFunction {
return externalPackageFragment.addFunction {
return irFactory.addFunction(externalPackageFragment) {
name = Name.identifier("\$sharedBox\$write")
origin = JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB
returnType = irBuiltIns.unitType
@@ -399,7 +402,7 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
}
private fun defineEs6DefaultTypeIntrinsic(): IrSimpleFunction {
return externalPackageFragment.addFunction {
return irFactory.addFunction(externalPackageFragment) {
name = Name.identifier("DefaultType")
}.apply {
returnType = addTypeParameter("T", irBuiltIns.anyType).defaultType
@@ -407,7 +410,7 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
}
private fun defineJsBindIntrinsic(): IrSimpleFunction {
return externalPackageFragment.addFunction {
return irFactory.addFunction(externalPackageFragment) {
name = Name.identifier("\$jsBind\$")
origin = JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB
returnType = irBuiltIns.anyNType
@@ -417,7 +420,7 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
}
private fun defineJsSliceIntrinsic(): IrSimpleFunction {
return externalPackageFragment.addFunction {
return irFactory.addFunction(externalPackageFragment) {
name = Name.identifier("slice")
origin = JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB
}.apply {
@@ -428,7 +431,7 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
}
private fun defineUnreachableIntrinsic(): IrSimpleFunction {
return externalPackageFragment.addFunction {
return irFactory.addFunction(externalPackageFragment) {
name = Name.identifier(Namer.UNREACHABLE_NAME)
origin = JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB
returnType = irBuiltIns.nothingType
@@ -19,8 +19,8 @@ import org.jetbrains.kotlin.ir.backend.js.ir.JsIrDeclarationBuilder
import org.jetbrains.kotlin.ir.backend.js.lower.JsInnerClassesSupport
import org.jetbrains.kotlin.ir.backend.js.utils.OperatorNames
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.*
@@ -56,7 +56,7 @@ class JsIrBackendContext(
override var inVerbosePhase: Boolean = false
override val irFactory: IrFactory = IrFactoryImpl
override val jsIrDeclarationBuilder: JsIrDeclarationBuilder = JsIrDeclarationBuilder()
override val jsIrDeclarationBuilder: JsIrDeclarationBuilder = JsIrDeclarationBuilder(irFactory)
val devMode = configuration[JSConfigurationKeys.DEVELOPER_MODE] ?: false
@@ -139,7 +139,7 @@ class JsIrBackendContext(
get() = testContainerFuns
override val mapping = JsMapping()
val innerClassesSupport = JsInnerClassesSupport(mapping)
val innerClassesSupport = JsInnerClassesSupport(mapping, irFactory)
companion object {
val KOTLIN_PACKAGE_FQN = FqName.fromSegments(listOf("kotlin"))
@@ -10,17 +10,14 @@ import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.ir.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.builders.declarations.buildValueParameter
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.name.Name
// This is a temporary class for migration to IrFactory. Usages should be refactored to use the factory directly once possible,
// since it doesn't add sufficient value on its own.
class JsIrDeclarationBuilder {
fun buildValueParameter(parent: IrDeclarationParent, name: String, index: Int, type: IrType): IrValueParameter =
class JsIrDeclarationBuilder(private val irFactory: IrFactory) {
fun buildValueParameter(parent: IrFunction, name: String, index: Int, type: IrType): IrValueParameter =
buildValueParameter(parent) {
this.origin = JsIrBuilder.SYNTHESIZED_DECLARATION
this.name = Name.identifier(name)
@@ -64,7 +61,7 @@ class JsIrDeclarationBuilder {
isInfix: Boolean = false,
isFakeOverride: Boolean = false,
origin: IrDeclarationOrigin = JsIrBuilder.SYNTHESIZED_DECLARATION,
): IrSimpleFunction = buildFun {
): IrSimpleFunction = irFactory.buildFun {
this.origin = origin
this.name = name
this.visibility = visibility
@@ -117,7 +117,7 @@ class CallableReferenceLowering(private val context: CommonBackendContext) : Bod
private val isKReference = superFunctionInterface.name.identifier[0] == 'K'
private fun buildReferenceClass(): IrClass {
return buildClass {
return context.irFactory.buildClass {
setSourceRange(reference)
visibility = Visibilities.LOCAL
// A callable reference results in a synthetic class, while a lambda is not synthetic.
@@ -86,7 +86,7 @@ class CreateScriptFunctionsPhase(val context: CommonBackendContext) : FileLoweri
}
private fun createFunction(irScript: IrScript, name: String, returnType: IrType): IrSimpleFunction =
buildFun {
context.irFactory.buildFun {
val (startOffset, endOffset) = getFunctionBodyOffsets(irScript)
this.startOffset = startOffset
this.endOffset = endOffset
@@ -62,7 +62,7 @@ class EnumUsageLowering(val context: JsIrBackendContext) : BodyLoweringPass {
}
private fun createFieldForEntry(entry: IrEnumEntry, irClass: IrClass): IrField =
buildField {
context.irFactory.buildField {
startOffset = entry.startOffset
endOffset = entry.endOffset
origin = entry.origin
@@ -117,7 +117,7 @@ class EnumClassConstructorLowering(val context: JsCommonBackendContext) : Declar
}
private fun transformEnumConstructor(enumConstructor: IrConstructor, enumClass: IrClass): IrConstructor {
return buildConstructor {
return context.irFactory.buildConstructor {
updateFrom(enumConstructor)
returnType = enumConstructor.returnType
}.apply {
@@ -321,7 +321,7 @@ class EnumEntryInstancesLowering(val context: JsIrBackendContext) : DeclarationT
private fun createEnumEntryInstanceVariable(irClass: IrClass, enumEntry: IrEnumEntry): IrField {
val enumName = irClass.name.identifier
val result = buildField {
val result = context.irFactory.buildField {
name = Name.identifier("${enumName}_${enumEntry.name.identifier}_instance")
type = enumEntry.getType(irClass).makeNullable()
isStatic = true
@@ -389,7 +389,7 @@ class EnumClassCreateInitializerLowering(val context: JsIrBackendContext) : Decl
return null
}
private fun createEntryInstancesInitializedVar(irClass: IrClass): IrField = buildField {
private fun createEntryInstancesInitializedVar(irClass: IrClass): IrField = context.irFactory.buildField {
val enumName = irClass.name.identifier
name = Name.identifier("${enumName}_entriesInitialized")
type = context.irBuiltIns.booleanType
@@ -109,7 +109,7 @@ class InteropCallableReferenceLowering(val context: JsIrBackendContext) : BodyLo
val superInvokeFun = invokeFun.overriddenSymbols.single { it.owner.isSuspend == invokeFun.isSuspend }.owner
val lambdaName = Name.identifier("${lambdaClass.name.asString()}\$lambda")
val lambdaDeclaration = buildFun {
val lambdaDeclaration = context.irFactory.buildFun {
startOffset = invokeFun.startOffset
endOffset = invokeFun.endOffset
// Since box/unbox is done on declaration side in case of suspend function use the specified type
@@ -200,7 +200,7 @@ class InteropCallableReferenceLowering(val context: JsIrBackendContext) : BodyLo
val factoryName = Name.identifier("${lambdaClass.name.asString()}\$factory")
val factoryDeclaration = buildFun {
val factoryDeclaration = context.irFactory.buildFun {
startOffset = expression.startOffset
endOffset = expression.endOffset
visibility = lambdaClass.visibility
@@ -6,9 +6,9 @@
package org.jetbrains.kotlin.ir.backend.js.lower
import org.jetbrains.kotlin.backend.common.getOrPut
import org.jetbrains.kotlin.backend.common.lower.InnerClassesSupport
import org.jetbrains.kotlin.backend.common.ir.copyTo
import org.jetbrains.kotlin.backend.common.ir.copyTypeParametersFrom
import org.jetbrains.kotlin.backend.common.lower.InnerClassesSupport
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.backend.js.JsMapping
@@ -19,12 +19,13 @@ import org.jetbrains.kotlin.ir.builders.declarations.buildField
import org.jetbrains.kotlin.ir.builders.declarations.buildValueParameter
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrConstructor
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.dump
import org.jetbrains.kotlin.name.Name
class JsInnerClassesSupport(mapping: JsMapping) : InnerClassesSupport {
class JsInnerClassesSupport(mapping: JsMapping, private val irFactory: IrFactory) : InnerClassesSupport {
private val outerThisFieldSymbols = mapping.outerThisFieldSymbols
private val innerClassConstructors = mapping.innerClassConstructors
private val originalInnerClassPrimaryConstructorByClass = mapping.originalInnerClassPrimaryConstructorByClass
@@ -36,7 +37,7 @@ class JsInnerClassesSupport(mapping: JsMapping) : InnerClassesSupport {
val outerClass = innerClass.parent as? IrClass
?: throw AssertionError("No containing class for inner class ${innerClass.dump()}")
buildField {
irFactory.buildField {
origin = InnerClassesSupport.FIELD_FOR_OUTER_THIS
name = Name.identifier("\$this")
type = outerClass.defaultType
@@ -74,7 +75,7 @@ class JsInnerClassesSupport(mapping: JsMapping) : InnerClassesSupport {
val irClass = oldConstructor.parent as IrClass
val outerThisType = (irClass.parent as IrClass).defaultType
val newConstructor = buildConstructor(oldConstructor.descriptor) {
val newConstructor = irFactory.buildConstructor(oldConstructor.descriptor) {
updateFrom(oldConstructor)
returnType = oldConstructor.returnType
}.also {
@@ -12,14 +12,16 @@ import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.lower.irBlockBody
import org.jetbrains.kotlin.backend.common.lower.irIfThen
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.backend.js.JsCommonBackendContext
import org.jetbrains.kotlin.ir.backend.js.JsLoweredDeclarationOrigin
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.buildField
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrConstructor
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl
import org.jetbrains.kotlin.ir.types.makeNullable
@@ -41,7 +43,7 @@ class ObjectDeclarationLowering(
val getInstanceFun = context.getOrCreateGetInstanceFunction(declaration)
val instanceField = buildField {
val instanceField = context.irFactory.buildField {
name = Name.identifier(declaration.name.asString() + "_instance")
type = declaration.defaultType.makeNullable()
isStatic = true
@@ -46,7 +46,7 @@ class PrivateMembersLowering(val context: JsIrBackendContext) : DeclarationTrans
if (function.visibility != Visibilities.PRIVATE || function.dispatchReceiverParameter == null) return null
val staticFunction = buildFun {
val staticFunction = context.irFactory.buildFun {
updateFrom(function)
name = function.name
returnType = function.returnType
@@ -58,7 +58,7 @@ class PropertyReferenceLowering(private val context: JsIrBackendContext) : BodyL
private fun buildFactoryFunction(reference: IrPropertyReference): IrSimpleFunction {
val property = reference.symbol.owner
val factoryDeclaration = buildFun {
val factoryDeclaration = context.irFactory.buildFun {
startOffset = reference.startOffset
endOffset = reference.endOffset
returnType = reference.type
@@ -126,7 +126,7 @@ class PropertyReferenceLowering(private val context: JsIrBackendContext) : BodyL
val supperAccessor =
classifier.owner.declarations.filterIsInstance<IrSimpleFunction>().single { it.name.asString() == superName }
val function = buildFun {
val function = context.irFactory.buildFun {
startOffset = reference.startOffset
endOffset = reference.endOffset
returnType = supperAccessor.returnType
@@ -239,7 +239,7 @@ class PropertyReferenceLowering(private val context: JsIrBackendContext) : BodyL
private fun buildLocalDelegateLambda(expression: IrLocalDelegatedPropertyReference): IrExpression {
val delegatedVar = expression.delegate.owner
val function = buildFun {
val function = context.irFactory.buildFun {
startOffset = expression.startOffset
endOffset = expression.endOffset
returnType = context.irBuiltIns.nothingType
@@ -259,4 +259,4 @@ class PropertyReferenceLowering(private val context: JsIrBackendContext) : BodyL
}
}
}
}
}
@@ -220,7 +220,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
}
private fun buildNewCoroutineClass(function: IrSimpleFunction): IrClass =
buildClass {
context.irFactory.buildClass {
startOffset = function.startOffset
endOffset = function.endOffset
origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -242,7 +242,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
}
}
return buildConstructor {
return context.irFactory.buildConstructor {
startOffset = function.startOffset
endOffset = function.endOffset
origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -285,7 +285,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
}
private fun buildInvokeSuspendMethod(stateMachineFunction: IrSimpleFunction): IrSimpleFunction {
val smFunction = buildFun {
val smFunction = context.irFactory.buildFun {
startOffset = function.startOffset
endOffset = function.endOffset
origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -328,7 +328,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
// i.sn = pn
// return i
private fun buildCreateMethod(superCreateFunction: IrSimpleFunction?, constructor: IrConstructor): IrSimpleFunction =
buildFun {
context.irFactory.buildFun {
startOffset = function.startOffset
endOffset = function.endOffset
origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -515,7 +515,7 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
fun IrClass.addField(name: Name, type: IrType, isMutable: Boolean): IrField {
val klass = this
return buildField {
return factory.buildField {
this.startOffset = klass.startOffset
this.endOffset = klass.endOffset
this.origin = DECLARATION_ORIGIN_COROUTINE_IMPL
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.ir.util.constructedClass
import org.jetbrains.kotlin.ir.util.isEffectivelyExternal
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.js.backend.ast.*
import org.jetbrains.kotlin.js.backend.ast.JsName
import org.jetbrains.kotlin.name.Name
interface CrossModuleReferenceInfo {
@@ -127,7 +127,7 @@ fun breakCrossModuleFieldAccess(
fun IrField.getter(): IrSimpleFunction {
return fieldToGetter.getOrPut(this) {
val fieldName = name
val getter = buildFun {
val getter = context.irFactory.buildFun {
name = Name.identifier("get-$fieldName")
returnType = type
}
@@ -151,7 +151,7 @@ fun breakCrossModuleFieldAccess(
fun IrField.setter(): IrSimpleFunction {
return fieldToSetter.getOrPut(this) {
val fieldName = name
val setter = buildFun {
val setter = context.irFactory.buildFun {
name = Name.identifier("set-$fieldName")
returnType = context.irBuiltIns.unitType
}
@@ -70,7 +70,7 @@ class JvmBackendContext(
val typeMapper = IrTypeMapper(this)
val methodSignatureMapper = MethodSignatureMapper(this)
internal val innerClassesSupport = JvmInnerClassesSupport()
internal val innerClassesSupport = JvmInnerClassesSupport(irFactory)
internal val cachedDeclarations = JvmCachedDeclarations(this, methodSignatureMapper, state.languageVersionSettings)
override val mapping: Mapping = DefaultMapping()
@@ -79,7 +79,7 @@ class JvmBackendContext(
override val ir = JvmIr(irModuleFragment, this.symbolTable)
override val sharedVariablesManager = JvmSharedVariablesManager(state.module, ir.symbols, irBuiltIns)
override val sharedVariablesManager = JvmSharedVariablesManager(state.module, ir.symbols, irBuiltIns, irFactory)
val irIntrinsics by lazy { IrIntrinsicMethods(irBuiltIns, ir.symbols) }
@@ -119,7 +119,7 @@ class JvmBackendContext(
val staticDefaultStubs = mutableMapOf<IrFunctionSymbol, IrFunction>()
val inlineClassReplacements = MemoizedInlineClassReplacements(state.functionsWithInlineClassReturnTypesMangled)
val inlineClassReplacements = MemoizedInlineClassReplacements(state.functionsWithInlineClassReturnTypesMangled, irFactory)
internal fun referenceClass(descriptor: ClassDescriptor): IrClassSymbol =
symbolTable.lazyWrapper.referenceClass(descriptor)
@@ -46,7 +46,7 @@ class JvmCachedDeclarations(
fun getFieldForEnumEntry(enumEntry: IrEnumEntry): IrField =
singletonFieldDeclarations.getOrPut(enumEntry) {
buildField {
context.irFactory.buildField {
setSourceRange(enumEntry)
name = enumEntry.name
type = enumEntry.parentAsClass.defaultType
@@ -66,7 +66,7 @@ class JvmCachedDeclarations(
languageVersionSettings.supportsFeature(LanguageFeature.ProperVisibilityForCompanionObjectInstanceField)
&& singleton.isCompanion
&& !singleton.parentAsClass.isInterface
buildField {
context.irFactory.buildField {
name = if (isNotMappedCompanion) singleton.name else Name.identifier(JvmAbi.INSTANCE_FIELD)
type = singleton.defaultType
origin = IrDeclarationOrigin.FIELD_FOR_OBJECT_INSTANCE
@@ -86,7 +86,7 @@ class JvmCachedDeclarations(
fun getPrivateFieldForObjectInstance(singleton: IrClass): IrField =
if (singleton.isCompanion && singleton.parentAsClass.isJvmInterface)
interfaceCompanionFieldDeclarations.getOrPut(singleton) {
buildField {
context.irFactory.buildField {
name = Name.identifier("\$\$INSTANCE")
type = singleton.defaultType
origin = JvmLoweredDeclarationOrigin.INTERFACE_COMPANION_PRIVATE_INSTANCE
@@ -108,7 +108,7 @@ class JvmCachedDeclarations(
val oldParent = irProperty.parent as? IrClass ?: return null
if (!oldParent.isObject) return null
return staticBackingFields.getOrPut(irProperty) {
buildField {
context.irFactory.buildField {
updateFrom(oldField)
name = oldField.name
isStatic = true
@@ -182,7 +182,7 @@ class JvmCachedDeclarations(
fun getDefaultImplsClass(interfaceClass: IrClass): IrClass =
defaultImplsClasses.getOrPut(interfaceClass) {
buildClass {
context.irFactory.buildClass {
startOffset = interfaceClass.startOffset
endOffset = interfaceClass.endOffset
origin = JvmLoweredDeclarationOrigin.DEFAULT_IMPLS
@@ -197,7 +197,7 @@ class JvmCachedDeclarations(
defaultImplsRedirections.getOrPut(fakeOverride) {
assert(fakeOverride.isFakeOverride)
val irClass = fakeOverride.parentAsClass
buildFun(fakeOverride.descriptor) {
context.irFactory.buildFun(fakeOverride.descriptor) {
origin = JvmLoweredDeclarationOrigin.DEFAULT_IMPLS_BRIDGE
name = fakeOverride.name
visibility = fakeOverride.visibility
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations
import org.jetbrains.kotlin.ir.builders.declarations.buildClass
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
@@ -56,11 +57,11 @@ class JvmGeneratorExtensions(private val generateFacades: Boolean = true) : Gene
else
IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB
override fun generateFacadeClass(source: DeserializedContainerSource): IrClass? {
override fun generateFacadeClass(irFactory: IrFactory, source: DeserializedContainerSource): IrClass? {
if (!generateFacades) return null
val jvmPackagePartSource = source as? JvmPackagePartSource ?: return null
val facadeName = jvmPackagePartSource.facadeClassName ?: jvmPackagePartSource.className
return buildClass {
return irFactory.buildClass {
origin = IrDeclarationOrigin.FILE_CLASS
name = facadeName.fqNameForTopLevelClassMaybeWithDollars.shortName()
}.also {
@@ -19,14 +19,12 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.impl.EmptyPackageFragmentDescriptor
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrPackageFragment
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.name.FqName
@@ -38,9 +36,11 @@ import org.jetbrains.kotlin.types.Variance
class JvmSymbols(
context: JvmBackendContext,
private val symbolTable: SymbolTable
symbolTable: SymbolTable
) : Symbols<JvmBackendContext>(context, context.irBuiltIns, symbolTable) {
private val storageManager = LockBasedStorageManager(this::class.java.simpleName)
private val irFactory = context.irFactory
private val kotlinPackage: IrPackageFragment = createPackage(FqName("kotlin"))
private val kotlinCoroutinesPackage: IrPackageFragment = createPackage(FqName("kotlin.coroutines"))
private val kotlinCoroutinesJvmInternalPackage: IrPackageFragment = createPackage(FqName("kotlin.coroutines.jvm.internal"))
@@ -96,7 +96,7 @@ class JvmSymbols(
classIsInline: Boolean = false,
block: (IrClass) -> Unit = {}
): IrClassSymbol =
buildClass {
irFactory.buildClass {
name = fqName.shortName()
kind = classKind
modality = classModality
@@ -139,7 +139,7 @@ class JvmSymbols(
}
klass.addFunction("throwNpe", irBuiltIns.unitType, isStatic = true)
klass.declarations.add(buildClass {
klass.declarations.add(irFactory.buildClass {
name = Name.identifier("Kotlin")
}.apply {
parent = klass
@@ -438,7 +438,7 @@ class JvmSymbols(
klass.addProperty {
name = receiverFieldName
}.apply {
backingField = buildField {
backingField = irFactory.buildField {
name = receiverFieldName
type = irBuiltIns.anyNType
visibility = Visibilities.PROTECTED
@@ -515,7 +515,7 @@ class JvmSymbols(
}
override val unsafeCoerceIntrinsic: IrSimpleFunctionSymbol =
buildFun {
irFactory.buildFun {
name = Name.special("<unsafe-coerce>")
origin = IrDeclarationOrigin.IR_BUILTINS_STUB
}.apply {
@@ -527,7 +527,7 @@ class JvmSymbols(
}.symbol
val reassignParameterIntrinsic: IrSimpleFunctionSymbol =
buildFun {
irFactory.buildFun {
name = Name.special("<set-parameter>")
origin = IrDeclarationOrigin.IR_BUILTINS_STUB
}.apply {
@@ -561,7 +561,7 @@ class JvmSymbols(
collectionToArrayClass.functions.single { it.owner.name.asString() == "toArray" && it.owner.valueParameters.size == 2 }
val kClassJava: IrPropertySymbol =
buildProperty {
irFactory.buildProperty {
name = Name.identifier("java")
}.apply {
parent = kotlinJvmPackage
@@ -672,7 +672,7 @@ class JvmSymbols(
val systemArraycopy: IrSimpleFunctionSymbol = systemClass.functionByName("arraycopy")
val signatureStringIntrinsic: IrSimpleFunctionSymbol =
buildFun {
irFactory.buildFun {
name = Name.special("<signature-string>")
origin = IrDeclarationOrigin.IR_BUILTINS_STUB
}.apply {
@@ -737,7 +737,7 @@ class JvmSymbols(
kotlinCoroutinesJvmInternalRunSuspendKt.functionByName("runSuspend")
override val ThrowKotlinNothingValueException: IrSimpleFunctionSymbol =
buildFun {
irFactory.buildFun {
name = Name.identifier("ThrowKotlinNothingValueException")
origin = IrDeclarationOrigin.IR_BUILTINS_STUB
returnType = irBuiltIns.nothingType
@@ -180,7 +180,7 @@ abstract class ClassCodegen protected constructor(
field.initializer!!.expression, context.irBuiltIns.unitType
)
if (classInitializer == null) {
classInitializer = buildFun {
classInitializer = context.irFactory.buildFun {
name = Name.special("<clinit>")
returnType = context.irBuiltIns.unitType
}.apply {
@@ -17,7 +17,10 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.descriptors.WrappedVariableDescriptor
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.expressions.IrSetVariable
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl
@@ -28,13 +31,14 @@ import org.jetbrains.kotlin.name.Name
class JvmSharedVariablesManager(
module: ModuleDescriptor,
val symbols: JvmSymbols,
val irBuiltIns: IrBuiltIns
val irBuiltIns: IrBuiltIns,
irFactory: IrFactory,
) : SharedVariablesManager {
private val jvmInternalPackage = IrExternalPackageFragmentImpl.createEmptyExternalPackageFragment(
module, FqName("kotlin.jvm.internal")
)
private val refNamespaceClass = jvmInternalPackage.addClass {
private val refNamespaceClass = irFactory.addClass(jvmInternalPackage) {
name = Name.identifier("Ref")
}
@@ -51,7 +55,7 @@ class JvmSharedVariablesManager(
}
private val primitiveRefProviders = irBuiltIns.primitiveIrTypes.associate { primitiveType ->
val refClass = refNamespaceClass.addClass {
val refClass = irFactory.addClass(refNamespaceClass) {
origin = IrDeclarationOrigin.IR_BUILTINS_STUB
name = Name.identifier(primitiveType.classOrNull!!.owner.name.asString() + "Ref")
}.apply {
@@ -61,7 +65,7 @@ class JvmSharedVariablesManager(
}
private val objectRefProvider = run {
val refClass = refNamespaceClass.addClass {
val refClass = irFactory.addClass(refNamespaceClass) {
origin = IrDeclarationOrigin.IR_BUILTINS_STUB
name = Name.identifier("ObjectRef")
}.apply {
@@ -148,7 +152,10 @@ class JvmSharedVariablesManager(
}
}
private inline fun IrDeclarationContainer.addClass(builder: IrClassBuilder.() -> Unit) = buildClass(builder).also {
it.parent = this
declarations += it
private inline fun IrFactory.addClass(
container: IrDeclarationContainer,
builder: IrClassBuilder.() -> Unit
): IrClass = buildClass(builder).also {
it.parent = container
container.declarations += it
}
@@ -320,7 +320,7 @@ fun IrSimpleFunction.copyCorrespondingPropertyFrom(source: IrSimpleFunction) {
val property = source.correspondingPropertySymbol?.owner ?: return
val target = this
correspondingPropertySymbol = buildProperty(property.symbol.descriptor) {
correspondingPropertySymbol = factory.buildProperty(property.symbol.descriptor) {
name = property.name
updateFrom(property)
}.apply {
@@ -266,13 +266,13 @@ private class AddContinuationLowering(private val context: JvmBackendContext) :
body = IrExpressionBodyImpl(startOffset, endOffset, IrErrorExpressionImpl(startOffset, endOffset, returnType, message))
}
private fun IrDeclarationContainer.addFunctionOverride(function: IrSimpleFunction): IrSimpleFunction =
private fun IrClass.addFunctionOverride(function: IrSimpleFunction): IrSimpleFunction =
addFunction(function.name.asString(), function.returnType).apply {
overriddenSymbols += function.symbol
valueParameters += function.valueParameters.map { it.copyTo(this) }
}
private fun IrDeclarationContainer.addFunctionOverride(
private fun IrClass.addFunctionOverride(
function: IrSimpleFunction,
makeBody: IrBlockBodyBuilder.(IrFunction) -> Unit
): IrSimpleFunction =
@@ -356,7 +356,7 @@ private class AddContinuationLowering(private val context: JvmBackendContext) :
parent: IrDeclarationParent,
newOrigin: IrDeclarationOrigin,
newVisibility: Visibility
): IrClass = buildClass {
): IrClass = context.irFactory.buildClass {
name = Name.special("<Continuation>")
origin = newOrigin
visibility = newVisibility
@@ -590,7 +590,7 @@ private class AddContinuationLowering(private val context: JvmBackendContext) :
if (function.body == null || !function.hasContinuation()) return result
if (flag.capturesCrossinline || function.isInline) {
result += buildFun(view.descriptor) {
result += context.irFactory.buildFun(view.descriptor) {
name = Name.identifier(view.name.asString() + FOR_INLINE_SUFFIX)
returnType = view.returnType
modality = view.modality
@@ -658,7 +658,7 @@ internal fun IrFunction.suspendFunctionOriginal(): IrFunction =
private fun IrFunction.createSuspendFunctionStub(context: JvmBackendContext): IrFunction {
require(this.isSuspend && this is IrSimpleFunction)
return buildFun(descriptor) {
return factory.buildFun(descriptor) {
updateFrom(this@createSuspendFunctionStub)
name = this@createSuspendFunctionStub.name
origin = this@createSuspendFunctionStub.origin
@@ -31,7 +31,6 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrGetEnumValueImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrVarargImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrEnumEntrySymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.types.typeWith
import org.jetbrains.kotlin.ir.util.defaultType
@@ -61,7 +60,7 @@ private class AdditionalClassAnnotationLowering(private val context: JvmBackendC
private fun buildAnnotationClass(
className: String,
classKind: ClassKind = ClassKind.ANNOTATION_CLASS
): IrClass = buildClass {
): IrClass = context.irFactory.buildClass {
name = Name.identifier(className)
kind = classKind
}.apply {
@@ -74,7 +73,7 @@ private class AdditionalClassAnnotationLowering(private val context: JvmBackendC
}
}
private fun buildAnnotationConstructor(annotationClass: IrClass): IrConstructor = buildConstructor {
private fun buildAnnotationConstructor(annotationClass: IrClass): IrConstructor = context.irFactory.buildConstructor {
isPrimary = true
}.apply {
parent = annotationClass
@@ -138,7 +138,7 @@ private fun IrBuilderWithScope.getJavaClass(backendContext: JvmBackendContext, i
}
fun IrClass.buildAssertionsDisabledField(backendContext: JvmBackendContext, topLevelClass: IrClass) =
buildField {
factory.buildField {
name = Name.identifier(ASSERTIONS_DISABLED_FIELD_NAME)
origin = JvmLoweredDeclarationOrigin.GENERATED_ASSERTION_ENABLED_FIELD
visibility = JavaVisibilities.PACKAGE_VISIBILITY
@@ -326,7 +326,9 @@ private class BridgeLowering(val context: JvmBackendContext) : FileLoweringPass,
private val IrSimpleFunction.specialBridgeOrNull: SpecialBridge?
get() = specialBridgeCache.getOrPutNullable(symbol) {
specialBridgeMethods.getSpecialMethodInfo(this)?.let { specialMethodInfo ->
SpecialBridge(this, jvmMethod, methodInfo = specialMethodInfo, needsGenericSignature = specialMethodInfo.needsGenericSignature)
SpecialBridge(
this, jvmMethod, methodInfo = specialMethodInfo, needsGenericSignature = specialMethodInfo.needsGenericSignature
)
} ?: specialBridgeMethods.getBuiltInWithDifferentJvmName(this)?.let { specialBuiltInInfo ->
SpecialBridge(this, jvmMethod, needsGenericSignature = specialBuiltInInfo.needsGenericSignature)
} ?: overriddenSymbols.asSequence().mapNotNull { it.owner.specialBridgeOrNull }.firstOrNull()?.let { specialBridge ->
@@ -337,7 +339,7 @@ private class BridgeLowering(val context: JvmBackendContext) : FileLoweringPass,
if (index < erasedParameterCount) context.irBuiltIns.anyNType else param.type
}
val substitutedOverride = buildFun {
val substitutedOverride = context.irFactory.buildFun {
updateFrom(specialBridge.overridden)
name = Name.identifier(specialBridge.signature.name)
returnType = this@specialBridgeOrNull.returnType
@@ -487,7 +489,7 @@ private class BridgeLowering(val context: JvmBackendContext) : FileLoweringPass,
}
}
valueParameters = newValueParameters
// After the checks, insert the orignal method body.
// After the checks, insert the original method body.
if (body is IrExpressionBody) {
+irReturn((body as IrExpressionBody).expression)
} else {
@@ -67,7 +67,7 @@ internal class CollectionStubMethodLowering(val context: JvmBackendContext) : Cl
private fun createStubMethod(
function: IrSimpleFunction, irClass: IrClass, substitutionMap: Map<IrTypeParameterSymbol, IrType>
): IrSimpleFunction {
return buildFun {
return context.irFactory.buildFun {
name = function.name
returnType = function.returnType.substitute(substitutionMap)
visibility = function.visibility
@@ -106,7 +106,7 @@ private class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringP
if (declaration.isEnumEntry) super.visitClass(declaration) else declaration
override fun visitConstructor(declaration: IrConstructor): IrStatement =
buildConstructor {
context.irFactory.buildConstructor {
updateFrom(declaration)
returnType = declaration.returnType
}.apply {
@@ -165,7 +165,7 @@ internal class FunctionReferenceLowering(private val context: JvmBackendContext)
else -> context.ir.symbols.functionReference
}.defaultType
private val functionReferenceClass = buildClass {
private val functionReferenceClass = context.irFactory.buildClass {
setSourceRange(irFunctionReference)
visibility = Visibilities.LOCAL
// A callable reference results in a synthetic class, while a lambda is not synthetic.
@@ -109,7 +109,7 @@ private fun generateMultifileFacades(
"Multifile facade $jvmClassName:\n ${partClasses.joinToString("\n ") { it.fqNameWhenAvailable!!.asString() }}\n"
}
val facadeClass = buildClass {
val facadeClass = context.irFactory.buildClass {
name = jvmClassName.fqNameForTopLevelClassMaybeWithDollars.shortName()
}.apply {
parent = file
@@ -206,7 +206,7 @@ private fun IrSimpleFunction.createMultifileDelegateIfNeeded(
origin == JvmLoweredDeclarationOrigin.SYNTHETIC_ACCESSOR
) return null
val function = buildFun {
val function = context.irFactory.buildFun {
updateFrom(target)
isFakeOverride = shouldGeneratePartHierarchy
name = if (shouldGeneratePartHierarchy) {
@@ -56,7 +56,7 @@ internal class InlineCallableReferenceToLambdaPhase(val context: JvmBackendConte
override fun visitFunctionReference(expression: IrFunctionReference): IrExpression {
expression.transformChildrenVoid(this)
if (expression !in inlinableReferences || expression.origin.isLambda) return expression
return expandInlineFunctionReferenceToLambda(expression, expression.symbol.owner)
return context.expandInlineFunctionReferenceToLambda(expression, expression.symbol.owner)
}
override fun visitPropertyReference(expression: IrPropertyReference): IrExpression {
@@ -65,17 +65,19 @@ internal class InlineCallableReferenceToLambdaPhase(val context: JvmBackendConte
return if (expression.field?.owner == null) {
// Use getter if field is absent ...
expandInlineFunctionReferenceToLambda(expression, expression.getter!!.owner)
context.expandInlineFunctionReferenceToLambda(expression, expression.getter!!.owner)
} else {
// ... else use field itself
expandInlineFieldReferenceToLambda(expression, expression.field!!.owner)
context.expandInlineFieldReferenceToLambda(expression, expression.field!!.owner)
}
}
private fun expandInlineFieldReferenceToLambda(expression: IrPropertyReference, field: IrField): IrExpression {
val irBuilder = context.createJvmIrBuilder(currentScope!!.scope.scopeOwnerSymbol, expression.startOffset, expression.endOffset)
private fun JvmBackendContext.expandInlineFieldReferenceToLambda(
expression: IrPropertyReference, field: IrField
): IrExpression {
val irBuilder = createJvmIrBuilder(currentScope!!.scope.scopeOwnerSymbol, expression.startOffset, expression.endOffset)
return irBuilder.irBlock(expression, IrStatementOrigin.LAMBDA) {
val function = buildFun {
val function = irFactory.buildFun {
setSourceRange(expression)
origin = IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
name = Name.identifier("stub_for_inline")
@@ -93,7 +95,7 @@ internal class InlineCallableReferenceToLambdaPhase(val context: JvmBackendConte
else -> irGet(addValueParameter("receiver", field.parentAsClass.defaultType))
}
body = this@InlineCallableReferenceToLambdaPhase.context.createIrBuilder(symbol).run {
body = createIrBuilder(symbol).run {
irExprBody(irGetField(receiver, field))
}
}
@@ -113,8 +115,11 @@ internal class InlineCallableReferenceToLambdaPhase(val context: JvmBackendConte
}
}
private fun expandInlineFunctionReferenceToLambda(expression: IrCallableReference<*>, referencedFunction: IrFunction): IrExpression {
val irBuilder = context.createJvmIrBuilder(currentScope!!.scope.scopeOwnerSymbol, expression.startOffset, expression.endOffset)
private fun JvmBackendContext.expandInlineFunctionReferenceToLambda(
expression: IrCallableReference<*>, referencedFunction: IrFunction
): IrExpression {
val irBuilder =
createJvmIrBuilder(currentScope!!.scope.scopeOwnerSymbol, expression.startOffset, expression.endOffset)
return irBuilder.irBlock(expression, IrStatementOrigin.LAMBDA) {
// We find the number of parameters for constructed lambda from the type of the function reference,
@@ -132,7 +137,7 @@ internal class InlineCallableReferenceToLambdaPhase(val context: JvmBackendConte
)
}
val function = buildFun {
val function = irFactory.buildFun {
setSourceRange(expression)
origin = IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
name = Name.identifier("stub_for_inlining")
@@ -163,7 +163,7 @@ private class JvmInlineClassLowering(private val context: JvmBackendContext) : F
// but unfortunately this is a special case in the old backend. The bridge method is not marked as such and does not follow the normal
// visibility rules for bridge methods.
private fun createBridgeDeclaration(source: IrSimpleFunction, mangledName: Name) =
buildFun {
context.irFactory.buildFun {
updateFrom(source)
name = mangledName
returnType = source.returnType
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.ir.builders.declarations.buildField
import org.jetbrains.kotlin.ir.builders.declarations.buildValueParameter
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrConstructor
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols
import org.jetbrains.kotlin.ir.util.defaultType
@@ -23,7 +24,7 @@ import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.load.java.JavaVisibilities
import org.jetbrains.kotlin.name.Name
class JvmInnerClassesSupport : InnerClassesSupport {
class JvmInnerClassesSupport(private val irFactory: IrFactory) : InnerClassesSupport {
private val outerThisDeclarations = HashMap<IrClass, IrField>()
private val innerClassConstructors = HashMap<IrConstructor, IrConstructor>()
private val originalInnerClassPrimaryConstructorByClass = HashMap<IrClass, IrConstructor>()
@@ -31,7 +32,7 @@ class JvmInnerClassesSupport : InnerClassesSupport {
override fun getOuterThisField(innerClass: IrClass): IrField =
outerThisDeclarations.getOrPut(innerClass) {
assert(innerClass.isInner) { "Class is not inner: ${innerClass.dump()}" }
buildField {
irFactory.buildField {
name = Name.identifier("this$0")
type = innerClass.parentAsClass.defaultType
origin = InnerClassesSupport.FIELD_FOR_OUTER_THIS
@@ -62,7 +63,7 @@ class JvmInnerClassesSupport : InnerClassesSupport {
}
private fun createInnerClassConstructorWithOuterThisParameter(oldConstructor: IrConstructor): IrConstructor =
buildConstructor(oldConstructor.descriptor) {
irFactory.buildConstructor(oldConstructor.descriptor) {
updateFrom(oldConstructor)
returnType = oldConstructor.returnType
}.apply {
@@ -53,7 +53,7 @@ private class JvmOverloadsAnnotationLowering(val context: JvmBackendContext) : C
}
private fun generateWrapper(target: IrFunction, numDefaultParametersToExpect: Int): IrFunction {
val wrapperIrFunction = generateWrapperHeader(target, numDefaultParametersToExpect)
val wrapperIrFunction = context.irFactory.generateWrapperHeader(target, numDefaultParametersToExpect)
val call = if (target is IrConstructor)
IrDelegatingConstructorCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, context.irBuiltIns.unitType, target.symbol)
@@ -114,7 +114,7 @@ private class JvmOverloadsAnnotationLowering(val context: JvmBackendContext) : C
return wrapperIrFunction
}
private fun generateWrapperHeader(oldFunction: IrFunction, numDefaultParametersToExpect: Int): IrFunction {
private fun IrFactory.generateWrapperHeader(oldFunction: IrFunction, numDefaultParametersToExpect: Int): IrFunction {
val res = when (oldFunction) {
is IrConstructor -> {
buildConstructor(oldFunction.descriptor) {
@@ -175,4 +175,4 @@ private class JvmOverloadsAnnotationLowering(val context: JvmBackendContext) : C
}
return result
}
}
}
@@ -121,7 +121,7 @@ class JvmPropertiesLowering(private val backendContext: JvmBackendContext) : IrE
accessor != null && !property.needsAccessor(accessor)
private fun createSyntheticMethodForAnnotations(declaration: IrProperty): IrSimpleFunction =
buildFun {
backendContext.irFactory.buildFun {
origin = JvmLoweredDeclarationOrigin.SYNTHETIC_METHOD_FOR_PROPERTY_ANNOTATIONS
name = Name.identifier(computeSyntheticMethodName(declaration))
visibility = declaration.visibility
@@ -192,7 +192,7 @@ private class MakeCallsStatic(
}
private fun IrSimpleFunction.copyRemovingDispatchReceiver(): IrSimpleFunction =
buildFun(descriptor) {
factory.buildFun(descriptor) {
updateFrom(this@copyRemovingDispatchReceiver)
name = this@copyRemovingDispatchReceiver.name
returnType = this@copyRemovingDispatchReceiver.returnType
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.backend.jvm.lower
import org.jetbrains.kotlin.backend.common.ClassLoweringPass
import org.jetbrains.kotlin.backend.common.ir.allParameters
import org.jetbrains.kotlin.backend.common.ir.copyTo
import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor
import org.jetbrains.kotlin.backend.common.lower.LocalDeclarationsLowering
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
@@ -25,13 +24,12 @@ import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.declarations.copyAttributes
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrFunctionReferenceImpl
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.functions
import org.jetbrains.kotlin.ir.util.isFileClass
import org.jetbrains.kotlin.load.java.JavaVisibilities
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
@@ -139,7 +137,7 @@ private class MainMethodGenerationLowering(private val context: JvmBackendContex
private fun IrBuilderWithScope.irRunSuspend(target: IrSimpleFunction, args: IrValueParameter?): IrExpression {
val backendContext = this@MainMethodGenerationLowering.context
return irBlock {
val wrapperConstructor = buildClass {
val wrapperConstructor = backendContext.irFactory.buildClass {
name = Name.special("<main-wrapper>")
visibility = JavaVisibilities.PACKAGE_VISIBILITY
modality = Modality.FINAL
@@ -72,7 +72,7 @@ private class MappedEnumWhenLowering(context: CommonBackendContext) : EnumWhenLo
private inner class EnumMappingState {
val mappings = mutableMapOf<IrClass /* enum */, Pair<MutableMap<IrEnumEntry, Int>, IrField>>()
val mappingsClass by lazy {
buildClass {
context.irFactory.buildClass {
name = Name.identifier("WhenMappings")
origin = JvmLoweredDeclarationOrigin.ENUM_MAPPINGS_FOR_WHEN
}.apply {
@@ -113,7 +113,7 @@ private class MoveOrCopyCompanionObjectFieldsLowering(val context: JvmBackendCon
isConst = true
}.also { property ->
val oldField = oldProperty.backingField!!
property.backingField = buildField {
property.backingField = context.irFactory.buildField {
updateFrom(oldField)
name = oldField.name
isStatic = true
@@ -63,7 +63,7 @@ class PolymorphicSignatureLowering(val context: JvmBackendContext) : IrElementTr
else -> throw AssertionError("unknown IrVarargElement: $it")
}
} ?: listOf()
val fakeFunction = buildFun {
val fakeFunction = context.irFactory.buildFun {
updateFrom(function)
name = function.name
origin = JvmLoweredDeclarationOrigin.POLYMORPHIC_SIGNATURE_INSTANTIATION
@@ -193,7 +193,7 @@ internal class PropertyReferenceLowering(val context: JvmBackendContext) : Class
override fun lower(irClass: IrClass) {
val kProperties = mutableMapOf<IrSymbol, PropertyInstance>()
val kPropertiesField = buildField {
val kPropertiesField = context.irFactory.buildField {
name = Name.identifier(JvmAbi.DELEGATED_PROPERTIES_ARRAY_NAME)
type = kPropertiesFieldType
origin = JvmLoweredDeclarationOrigin.GENERATED_PROPERTY_REFERENCE
@@ -279,7 +279,7 @@ internal class PropertyReferenceLowering(val context: JvmBackendContext) : Class
private fun createKPropertySubclass(expression: IrCallableReference<*>): IrClass {
val kind = propertyReferenceKindFor(expression)
val superClass = if (useOptimizedSuperClass) kind.implSymbol.owner else kind.interfaceSymbol.owner
val referenceClass = buildClass {
val referenceClass = context.irFactory.buildClass {
setSourceRange(expression)
name = SpecialNames.NO_NAME_PROVIDED
origin = JvmLoweredDeclarationOrigin.GENERATED_PROPERTY_REFERENCE
@@ -103,7 +103,7 @@ private class FieldRenamer(private val newNames: Map<IrField, Name>) : IrElement
override fun visitField(declaration: IrField): IrStatement {
val newName = newNames[declaration] ?: return super.visitField(declaration)
return buildField {
return declaration.factory.buildField {
updateFrom(declaration)
name = newName
}.also {
@@ -60,7 +60,7 @@ class StaticLambdaLowering(val backendContext: JvmBackendContext) : FileLowering
private fun getFieldForStaticLambdaInstance(lambdaClass: IrClass): IrField =
staticLambdaFields.getOrPut(lambdaClass) {
buildField {
backendContext.irFactory.buildField {
name = Name.identifier(JvmAbi.INSTANCE_FIELD)
type = lambdaClass.defaultType
origin = JvmLoweredDeclarationOrigin.FIELD_FOR_STATIC_LAMBDA_INSTANCE
@@ -256,7 +256,7 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
): IrConstructor {
val source = this
return buildConstructor {
return factory.buildConstructor {
origin = originForConstructorAccessor
name = source.name
visibility = Visibilities.PUBLIC
@@ -292,7 +292,7 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
private fun IrSimpleFunction.makeSimpleFunctionAccessor(expression: IrCall, parent: IrDeclarationParent): IrSimpleFunction {
val source = this
return buildFun {
return factory.buildFun {
origin = JvmLoweredDeclarationOrigin.SYNTHETIC_ACCESSOR
name = source.accessorName(expression.superQualifierSymbol)
visibility = Visibilities.PUBLIC
@@ -324,7 +324,7 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
}
private fun makeGetterAccessorSymbol(fieldSymbol: IrFieldSymbol, parent: IrClass): IrSimpleFunctionSymbol =
buildFun {
context.irFactory.buildFun {
origin = JvmLoweredDeclarationOrigin.SYNTHETIC_ACCESSOR
name = fieldSymbol.owner.accessorNameForGetter()
visibility = Visibilities.PUBLIC
@@ -360,7 +360,7 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
}
private fun makeSetterAccessorSymbol(fieldSymbol: IrFieldSymbol, parent: IrClass): IrSimpleFunctionSymbol =
buildFun {
context.irFactory.buildFun {
origin = JvmLoweredDeclarationOrigin.SYNTHETIC_ACCESSOR
name = fieldSymbol.owner.accessorNameForSetter()
visibility = Visibilities.PUBLIC
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.declarations.addFunction
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclarationContainer
import org.jetbrains.kotlin.ir.declarations.IrTypeAlias
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl
import org.jetbrains.kotlin.load.java.JvmAbi
@@ -35,7 +34,7 @@ class TypeAliasAnnotationMethodsLowering(val context: CommonBackendContext) :
private val IrTypeAlias.syntheticAnnotationMethodName
get() = Name.identifier(JvmAbi.getSyntheticMethodNameForAnnotatedTypeAlias(name))
private fun IrDeclarationContainer.visitTypeAliases() {
private fun IrClass.visitTypeAliases() {
val annotatedAliases = declarations
.filterIsInstance<IrTypeAlias>()
.filter { !it.descriptor.annotations.isEmpty() }
@@ -32,7 +32,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.safeAs
/**
* Keeps track of replacement functions and inline class box/unbox functions.
*/
class MemoizedInlineClassReplacements(private val mangleReturnTypes: Boolean) {
class MemoizedInlineClassReplacements(private val mangleReturnTypes: Boolean, private val irFactory: IrFactory) {
private val storageManager = LockBasedStorageManager("inline-class-replacements")
private val propertyMap = mutableMapOf<IrPropertySymbol, IrProperty>()
@@ -75,7 +75,7 @@ class MemoizedInlineClassReplacements(private val mangleReturnTypes: Boolean) {
val getBoxFunction: (IrClass) -> IrSimpleFunction =
storageManager.createMemoizedFunction { irClass ->
require(irClass.isInline)
buildFun {
irFactory.buildFun {
name = Name.identifier(KotlinTypeMapper.BOX_JVM_METHOD_NAME)
origin = JvmLoweredDeclarationOrigin.SYNTHETIC_INLINE_CLASS_MEMBER
returnType = irClass.defaultType
@@ -96,7 +96,7 @@ class MemoizedInlineClassReplacements(private val mangleReturnTypes: Boolean) {
val getUnboxFunction: (IrClass) -> IrSimpleFunction =
storageManager.createMemoizedFunction { irClass ->
require(irClass.isInline)
buildFun {
irFactory.buildFun {
name = Name.identifier(KotlinTypeMapper.UNBOX_JVM_METHOD_NAME)
origin = JvmLoweredDeclarationOrigin.SYNTHETIC_INLINE_CLASS_MEMBER
returnType = InlineClassAbi.getUnderlyingType(irClass)
@@ -110,7 +110,7 @@ class MemoizedInlineClassReplacements(private val mangleReturnTypes: Boolean) {
fun getSpecializedEqualsMethod(irClass: IrClass, irBuiltIns: IrBuiltIns): IrSimpleFunction {
require(irClass.isInline)
return specializedEqualsCache.computeIfAbsent(irClass) {
buildFun {
irFactory.buildFun {
name = InlineClassDescriptorResolver.SPECIALIZED_EQUALS_NAME
// TODO: Revisit this once we allow user defined equals methods in inline classes.
origin = JvmLoweredDeclarationOrigin.INLINE_CLASS_GENERATED_IMPL_METHOD
@@ -184,7 +184,7 @@ class MemoizedInlineClassReplacements(private val mangleReturnTypes: Boolean) {
replacementOrigin: IrDeclarationOrigin,
noFakeOverride: Boolean = false,
body: IrFunction.() -> Unit
): IrSimpleFunction = buildFun(function.descriptor) {
): IrSimpleFunction = irFactory.buildFun(function.descriptor) {
updateFrom(function)
if (function is IrConstructor) {
// The [updateFrom] call will set the modality to FINAL for constructors, while the JVM backend would use OPEN here.
@@ -216,7 +216,7 @@ class MemoizedInlineClassReplacements(private val mangleReturnTypes: Boolean) {
val propertySymbol = function.correspondingPropertySymbol
if (propertySymbol != null) {
val property = propertyMap.getOrPut(propertySymbol) {
buildProperty(propertySymbol.descriptor) {
irFactory.buildProperty(propertySymbol.descriptor) {
name = propertySymbol.owner.name
updateFrom(propertySymbol.owner)
}.apply {
@@ -18,8 +18,8 @@ import org.jetbrains.kotlin.ir.backend.js.JsSharedVariablesManager
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrDeclarationBuilder
import org.jetbrains.kotlin.ir.backend.js.lower.JsInnerClassesSupport
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrExternalPackageFragmentSymbol
@@ -44,7 +44,7 @@ class WasmBackendContext(
override val lateinitNullableFields = mutableMapOf<IrField, IrField>()
override val extractedLocalClasses: MutableSet<IrClass> = hashSetOf()
override val irFactory: IrFactory = IrFactoryImpl
override val jsIrDeclarationBuilder: JsIrDeclarationBuilder = JsIrDeclarationBuilder()
override val jsIrDeclarationBuilder: JsIrDeclarationBuilder = JsIrDeclarationBuilder(irFactory)
// Place to store declarations excluded from code generation
val excludedDeclarations: IrPackageFragment by lazy {
@@ -56,7 +56,7 @@ class WasmBackendContext(
override val mapping = JsMapping()
val innerClassesSupport = JsInnerClassesSupport(mapping)
val innerClassesSupport = JsInnerClassesSupport(mapping, irFactory)
val objectToGetInstanceFunction = mutableMapOf<IrClassSymbol, IrSimpleFunction>()
override val internalPackageFqn = FqName("kotlin.wasm")
@@ -14,12 +14,10 @@ import org.jetbrains.kotlin.ir.builders.declarations.addFunction
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.SimpleType
class WasmSymbols(
context: WasmBackendContext,
@@ -49,7 +47,7 @@ class WasmSymbols(
override val getContinuation
get() = TODO()
override val coroutineContextGetter by lazy {
context.excludedDeclarations.addFunction {
context.irFactory.addFunction(context.excludedDeclarations) {
name = Name.identifier("coroutineContextGetter\$Stub")
}.symbol
}
@@ -106,7 +106,7 @@ class DeclarationStubGenerator(
val packageFragment = directMember.containingDeclaration as? PackageFragmentDescriptor ?: return null
val containerSource = directMember.safeAs<DescriptorWithContainerSource>()?.containerSource ?: return null
return facadeClassMap.getOrPut(containerSource) {
extensions.generateFacadeClass(containerSource)?.also { facade ->
extensions.generateFacadeClass(symbolTable.irFactory, containerSource)?.also { facade ->
val packageStub = generateOrGetEmptyExternalPackageFragmentStub(packageFragment)
facade.parent = packageStub
packageStub.declarations.add(facade)
@@ -9,13 +9,14 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
import org.jetbrains.kotlin.types.KotlinType
open class StubGeneratorExtensions {
open fun computeExternalDeclarationOrigin(descriptor: DeclarationDescriptor): IrDeclarationOrigin? = null
open fun generateFacadeClass(source: DeserializedContainerSource): IrClass? = null
open fun generateFacadeClass(irFactory: IrFactory, source: DeserializedContainerSource): IrClass? = null
open fun isPropertyWithPlatformField(descriptor: PropertyDescriptor): Boolean = false
@@ -5,20 +5,18 @@
// This file was autogenerated based on android.jar, do not edit it directly.
package org.jetbrains.kotlin.android.parcel.ir
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.ir.Symbols
import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.impl.EmptyPackageFragmentDescriptor
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.declarations.IrPackageFragment
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.types.makeNullable
import org.jetbrains.kotlin.ir.types.starProjectedType
@@ -35,6 +33,8 @@ class AndroidSymbols(
private val charSequence: IrClassSymbol,
private val moduleFragment: IrModuleFragment
) {
private val irFactory: IrFactory = IrFactoryImpl
private val androidOs: IrPackageFragment = createPackage("android.os")
private val androidText: IrPackageFragment = createPackage("android.text")
@@ -109,7 +109,7 @@ class AndroidSymbols(
private val javaUtilTreeSet: IrClassSymbol =
createClass(javaUtil, "TreeSet", ClassKind.CLASS, Modality.OPEN)
val androidOsParcelableCreator: IrClassSymbol = buildClass {
val androidOsParcelableCreator: IrClassSymbol = irFactory.buildClass {
name = Name.identifier("Creator")
kind = ClassKind.INTERFACE
modality = Modality.ABSTRACT
@@ -130,7 +130,7 @@ class AndroidSymbols(
}
}.symbol
val kotlinKClassJava: IrPropertySymbol = buildProperty {
val kotlinKClassJava: IrPropertySymbol = irFactory.buildProperty {
name = Name.identifier("java")
}.apply {
parent = kotlinJvm
@@ -454,7 +454,7 @@ class AndroidSymbols(
shortName: String,
classKind: ClassKind,
classModality: Modality
): IrClassSymbol = buildClass {
): IrClassSymbol = irFactory.buildClass {
name = Name.identifier(shortName)
kind = classKind
modality = classModality
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionReference
@@ -52,6 +53,8 @@ class ParcelableIrTransformer(private val context: IrPluginContext, private val
private val symbolMap = mutableMapOf<IrFunctionSymbol, IrFunctionSymbol>()
private val irFactory: IrFactory = IrFactoryImpl
fun transform(moduleFragment: IrModuleFragment) {
moduleFragment.accept(this, null)
deferredOperations.forEach { it() }
@@ -199,7 +202,7 @@ class ParcelableIrTransformer(private val context: IrPluginContext, private val
isFinal = true
}.apply {
val irField = this
val creatorClass = buildClass {
val creatorClass = irFactory.buildClass {
name = Name.identifier("Creator")
visibility = Visibilities.LOCAL
}.apply {
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
@@ -57,6 +58,8 @@ private class AndroidIrTransformer(val extension: AndroidIrExtension, val plugin
private val cachedMethods = mutableMapOf<FqName, IrSimpleFunction>()
private val cachedFields = mutableMapOf<FqName, IrField>()
private val irFactory: IrFactory = IrFactoryImpl
private fun createPackage(fqName: FqName) =
cachedPackages.getOrPut(fqName) {
IrExternalPackageFragmentImpl.createEmptyExternalPackageFragment(pluginContext.moduleDescriptor, fqName)
@@ -64,7 +67,7 @@ private class AndroidIrTransformer(val extension: AndroidIrExtension, val plugin
private fun createClass(fqName: FqName, isInterface: Boolean = false) =
cachedClasses.getOrPut(fqName) {
buildClass {
irFactory.buildClass {
name = fqName.shortName()
kind = if (isInterface) ClassKind.INTERFACE else ClassKind.CLASS
origin = IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB