Skip to content

Commit

Permalink
[IR] Merge LateinitLowering with JvmLateinitLowering
Browse files Browse the repository at this point in the history
Those lowerings were essentially duplicates and were doing the same thing.
Here we choose the JVM version, becouse it's simpler, but adopt it a bit
to support lateinit properties in other files.

^KT-71141
  • Loading branch information
mcpiroman authored and qodana-bot committed Nov 19, 2024
1 parent 2a2bc69 commit 17aa734
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 392 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ var IrSimpleFunction.suspendFunction: IrSimpleFunction? by irAttribute(followAtt
var IrFunction.defaultArgumentsOriginalFunction: IrFunction? by irAttribute(followAttributeOwner = false)

open class Mapping {
val lateInitFieldToNullableField: DeclarationMapping<IrField, IrField> by AttributeBasedMappingDelegate()
val capturedConstructors: MapBasedMapping<IrConstructor, IrConstructor> = MapBasedMapping()

abstract class DeclarationMapping<K : IrDeclaration, V> {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,9 @@ private val stringConcatenationLoweringPhase = makeIrModulePhase(
name = "JsStringConcatenationLowering",
)

private val lateinitNullableFieldsPhase = makeIrModulePhase(
::NullableFieldsForLateinitCreationLowering,
name = "LateinitNullableFields",
)

private val lateinitDeclarationLoweringPhase = makeIrModulePhase(
::NullableFieldsDeclarationLowering,
name = "LateinitDeclarations",
)

private val lateinitUsageLoweringPhase = makeIrModulePhase(
::LateinitUsageLowering,
name = "LateinitUsage",
private val lateinitPhase = makeIrModulePhase(
::LateinitLowering,
name = "LateinitLowering",
)

private val kotlinNothingValueExceptionPhase = makeIrModulePhase(
Expand Down Expand Up @@ -192,7 +182,7 @@ private val sharedVariablesLoweringPhase = makeIrModulePhase(
SharedVariablesLowering(JsSharedVariablesManager(context.irBuiltIns, context.dynamicType, context.intrinsics))
},
name = "SharedVariablesLowering",
prerequisite = setOf(lateinitDeclarationLoweringPhase, lateinitUsageLoweringPhase)
prerequisite = setOf(lateinitPhase)
)

private val outerThisSpecialAccessorInInlineFunctionsPhase = makeIrModulePhase(
Expand Down Expand Up @@ -753,9 +743,7 @@ fun getJsLowerings(
// BEGIN: Common Native/JS prefix.
validateIrBeforeLowering,
jsCodeOutliningPhase,
lateinitNullableFieldsPhase,
lateinitDeclarationLoweringPhase,
lateinitUsageLoweringPhase,
lateinitPhase,
sharedVariablesLoweringPhase,
outerThisSpecialAccessorInInlineFunctionsPhase,
localClassesInInlineLambdasPhase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private val jvmFilePhases = createFilePhases<JvmBackendContext>(
::PolymorphicSignatureLowering,
::VarargLowering,

::JvmLateinitLowering,
::LateinitLowering,
::JvmInventNamesForLocalClasses,

::JvmInlineCallableReferenceToLambdaPhase,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,9 @@ private val stringConcatenationLowering = makeIrModulePhase(
name = "StringConcatenation",
)

private val lateinitNullableFieldsPhase = makeIrModulePhase(
::NullableFieldsForLateinitCreationLowering,
name = "LateinitNullableFields",
)

private val lateinitDeclarationLoweringPhase = makeIrModulePhase(
::NullableFieldsDeclarationLowering,
name = "LateinitDeclarations",
)

private val lateinitUsageLoweringPhase = makeIrModulePhase(
::LateinitUsageLowering,
name = "LateinitUsage",
private val lateinitPhase = makeIrModulePhase(
::LateinitLowering,
name = "LateinitLowering",
)

private val rangeContainsLoweringPhase = makeIrModulePhase(
Expand All @@ -96,10 +86,7 @@ private val arrayConstructorPhase = makeIrModulePhase(
private val sharedVariablesLoweringPhase = makeIrModulePhase(
{ context: WasmBackendContext -> SharedVariablesLowering(WasmSharedVariablesManager(context.wasmSymbols)) },
name = "SharedVariablesLowering",
prerequisite = setOf(
lateinitDeclarationLoweringPhase,
lateinitUsageLoweringPhase
)
prerequisite = setOf(lateinitPhase)
)

private val localClassesInInlineLambdasPhase = makeIrModulePhase(
Expand Down Expand Up @@ -547,9 +534,7 @@ fun getWasmLowerings(
excludeDeclarationsFromCodegenPhase,
expectDeclarationsRemovingPhase,

lateinitNullableFieldsPhase,
lateinitDeclarationLoweringPhase,
lateinitUsageLoweringPhase,
lateinitPhase,
rangeContainsLoweringPhase,

sharedVariablesLoweringPhase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,7 @@ private val arrayConstructorPhase = createFileLoweringPhase(
)

private val lateinitPhase = createFileLoweringPhase(
{ context, irFile ->
NullableFieldsForLateinitCreationLowering(context).lower(irFile)
NullableFieldsDeclarationLowering(context).lower(irFile)
LateinitUsageLowering(context).lower(irFile)
},
::LateinitLowering,
name = "Lateinit",
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,7 @@ internal class ClassLayoutBuilder(val irClass: IrClass, val context: Context) {
* All fields of the class instance.
* The order respects the class hierarchy, i.e. a class [fields] contains superclass [fields] as a prefix.
*/
fun getFields(llvm: CodegenLlvmHelpers): List<FieldInfo> = getFieldsInternal(llvm).map { fieldInfo ->
val mappedField = fieldInfo.irField?.let { context.mapping.lateInitFieldToNullableField[it] ?: it }
if (mappedField == fieldInfo.irField)
fieldInfo
else
mappedField!!.toFieldInfo(llvm)
}
fun getFields(llvm: CodegenLlvmHelpers): List<FieldInfo> = getFieldsInternal(llvm)

private var fields: List<FieldInfo>? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ internal val INTERNAL_ABI_ORIGIN = IrDeclarationOriginImpl("INTERNAL_ABI")
* and we have to explicitly add an external declaration.
*/
internal class CachesAbiSupport(mapping: Mapping, private val irFactory: IrFactory) {
private val lateInitFieldToNullableField = mapping.lateInitFieldToNullableField


fun getOuterThisAccessor(irClass: IrClass): IrSimpleFunction {
require(irClass.isInner) { "Expected an inner class but was: ${irClass.render()}" }
return irClass::outerThisAccessor.getOrSetIfNull {
Expand Down Expand Up @@ -83,12 +80,11 @@ internal class CachesAbiSupport(mapping: Mapping, private val irFactory: IrFacto
require(irProperty.isLateinit) { "Expected a lateinit property but was: ${irProperty.render()}" }
return irProperty::lateinitPropertyAccessor.getOrSetIfNull {
val backingField = irProperty.backingField ?: error("Lateinit property ${irProperty.render()} should have a backing field")
val actualField = lateInitFieldToNullableField[backingField] ?: backingField
val owner = irProperty.parent
irFactory.buildFun {
name = getMangledNameFor("${irProperty.name}_field", owner)
origin = INTERNAL_ABI_ORIGIN
returnType = actualField.type
returnType = backingField.type
}.apply {
parent = irProperty.getPackageFragment()
attributeOwnerId = irProperty // To be able to get the file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ internal class NativeInlineFunctionResolver(

NativeAssertionWrapperLowering(context).lower(function)

NullableFieldsForLateinitCreationLowering(context).lowerWithLocalDeclarations(function)
NullableFieldsDeclarationLowering(context).lowerWithLocalDeclarations(function)
LateinitUsageLowering(context).lower(body, function)
LateinitLowering(context).lower(body)

SharedVariablesLowering(KonanSharedVariablesManager(context.irBuiltIns, context.ir.symbols)).lower(body, function)

Expand Down

0 comments on commit 17aa734

Please sign in to comment.