diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt index ff5e82e4a6d..90268f4f7e9 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt @@ -5,10 +5,6 @@ package org.jetbrains.kotlin.ir.backend.js -import org.jetbrains.kotlin.backend.common.BodyLoweringPass -import org.jetbrains.kotlin.backend.common.DeclarationTransformer -import org.jetbrains.kotlin.backend.common.FileLoweringPass -import org.jetbrains.kotlin.backend.common.lower import org.jetbrains.kotlin.backend.common.lower.* import org.jetbrains.kotlin.backend.common.lower.coroutines.AddContinuationToLocalSuspendFunctionsLowering import org.jetbrains.kotlin.backend.common.lower.coroutines.AddContinuationToNonLocalSuspendFunctionsLowering @@ -31,246 +27,165 @@ import org.jetbrains.kotlin.ir.declarations.IrModuleFragment import org.jetbrains.kotlin.ir.interpreter.IrInterpreterConfiguration import org.jetbrains.kotlin.platform.js.JsPlatforms -private fun List.toCompilerPhase() = - map { - @Suppress("USELESS_CAST") - it.modulePhase as CompilerPhase - }.reduce { acc, lowering -> acc.then(lowering) } +private fun List>.toCompilerPhase() = + reduce { acc, lowering -> acc.then(lowering) } -private fun makeJsModulePhase( - lowering: (JsIrBackendContext) -> FileLoweringPass, - name: String, - description: String, - prerequisite: Set> = emptySet() -): SameTypeNamedCompilerPhase = makeCustomJsModulePhase( - op = { context, modules -> lowering(context).lower(modules) }, - name = name, - description = description, - prerequisite = prerequisite -) - -private fun makeCustomJsModulePhase( - op: (JsIrBackendContext, IrModuleFragment) -> Unit, - description: String, - name: String, - prerequisite: Set> = emptySet() -): SameTypeNamedCompilerPhase = SameTypeNamedCompilerPhase( - name = name, - description = description, - prerequisite = prerequisite, - lower = object : SameTypeCompilerPhase { - override fun invoke( - phaseConfig: PhaseConfigurationService, - phaserState: PhaserState, - context: JsIrBackendContext, - input: IrModuleFragment - ): IrModuleFragment { - op(context, input) - return input - } - }, - actions = setOf(defaultDumper, validationAction), -) - -sealed class Lowering(val name: String) { - abstract val modulePhase: SameTypeNamedCompilerPhase -} - -class DeclarationLowering( - name: String, - description: String, - prerequisite: Set> = emptySet(), - factory: (JsIrBackendContext) -> DeclarationTransformer -) : Lowering(name) { - override val modulePhase = makeJsModulePhase(factory, name, description, prerequisite) -} - -class BodyLowering( - name: String, - description: String, - prerequisite: Set> = emptySet(), - factory: (JsIrBackendContext) -> BodyLoweringPass -) : Lowering(name) { - override val modulePhase = makeJsModulePhase(factory, name, description, prerequisite) -} - -class ModuleLowering( - name: String, - override val modulePhase: SameTypeNamedCompilerPhase -) : Lowering(name) - -private fun makeDeclarationTransformerPhase( - lowering: (JsIrBackendContext) -> DeclarationTransformer, - name: String, - description: String, - prerequisite: Set = emptySet() -) = DeclarationLowering(name, description, prerequisite.map { it.modulePhase }.toSet(), lowering) - -private fun makeBodyLoweringPhase( - lowering: (JsIrBackendContext) -> BodyLoweringPass, - name: String, - description: String, - prerequisite: Set = emptySet() -) = BodyLowering(name, description, prerequisite.map { it.modulePhase }.toSet(), lowering) - -fun SameTypeNamedCompilerPhase.toModuleLowering() = ModuleLowering(this.name, this) - -private val validateIrBeforeLowering = makeCustomJsModulePhase( +private val validateIrBeforeLowering = makeCustomPhase( { context, module -> validationCallback(context, module) }, name = "ValidateIrBeforeLowering", description = "Validate IR before lowering" -).toModuleLowering() +) -private val validateIrAfterLowering = makeCustomJsModulePhase( +private val validateIrAfterLowering = makeCustomPhase( { context, module -> validationCallback(context, module) }, name = "ValidateIrAfterLowering", description = "Validate IR after lowering" -).toModuleLowering() +) -private val collectClassDefaultConstructorsPhase = makeDeclarationTransformerPhase( +private val collectClassDefaultConstructorsPhase = makeIrModulePhase( ::CollectClassDefaultConstructorsLowering, name = "CollectClassDefaultConstructorsLowering", description = "Collect classes default constructors to add it to metadata on code generating phase" ) -private val preventExportOfSyntheticDeclarationsLowering = makeDeclarationTransformerPhase( +private val preventExportOfSyntheticDeclarationsLowering = makeIrModulePhase( ::ExcludeSyntheticDeclarationsFromExportLowering, name = "ExcludeSyntheticDeclarationsFromExportLowering", description = "Exclude synthetic declarations which we don't want to export such as `Enum.entries` or `DataClass::componentN`", ) -val scriptRemoveReceiverLowering = makeJsModulePhase( +val scriptRemoveReceiverLowering = makeIrModulePhase( ::ScriptRemoveReceiverLowering, name = "ScriptRemoveReceiver", description = "Remove receivers for declarations in script" -).toModuleLowering() +) -val createScriptFunctionsPhase = makeJsModulePhase( +val createScriptFunctionsPhase = makeIrModulePhase( ::CreateScriptFunctionsPhase, name = "CreateScriptFunctionsPhase", description = "Create functions for initialize and evaluate script" -).toModuleLowering() +) -private val collectClassIdentifiersLowering = makeJsModulePhase( +private val collectClassIdentifiersLowering = makeIrModulePhase( ::JsCollectClassIdentifiersLowering, name = "CollectClassIdentifiersLowering", description = "Save classId before all the lowerings", -).toModuleLowering() +) -private val inventNamesForLocalClassesPhase = makeJsModulePhase( +private val inventNamesForLocalClassesPhase = makeIrModulePhase( ::JsInventNamesForLocalClasses, name = "InventNamesForLocalClasses", description = "Invent names for local classes and anonymous objects", -).toModuleLowering() +) -private val annotationInstantiationLowering = makeDeclarationTransformerPhase( +private val annotationInstantiationLowering = makeIrModulePhase( ::JsAnnotationImplementationTransformer, name = "AnnotationImplementation", description = "Create synthetic annotations implementations and use them in annotations constructor calls" ) -private val expectDeclarationsRemovingPhase = makeDeclarationTransformerPhase( +private val expectDeclarationsRemovingPhase = makeIrModulePhase( ::ExpectDeclarationsRemoveLowering, name = "ExpectDeclarationsRemoving", description = "Remove expect declaration from module fragment" ) -private val stringConcatenationLoweringPhase = makeJsModulePhase( +private val stringConcatenationLoweringPhase = makeIrModulePhase( ::JsStringConcatenationLowering, name = "JsStringConcatenationLowering", description = "Call toString() for values of some types when concatenating strings" -).toModuleLowering() +) -private val lateinitNullableFieldsPhase = makeDeclarationTransformerPhase( +private val lateinitNullableFieldsPhase = makeIrModulePhase( ::NullableFieldsForLateinitCreationLowering, name = "LateinitNullableFields", description = "Create nullable fields for lateinit properties" ) -private val lateinitDeclarationLoweringPhase = makeDeclarationTransformerPhase( +private val lateinitDeclarationLoweringPhase = makeIrModulePhase( ::NullableFieldsDeclarationLowering, name = "LateinitDeclarations", description = "Reference nullable fields from properties and getters + insert checks" ) -private val lateinitUsageLoweringPhase = makeBodyLoweringPhase( +private val lateinitUsageLoweringPhase = makeIrModulePhase( ::LateinitUsageLowering, name = "LateinitUsage", description = "Insert checks for lateinit field references" ) -private val kotlinNothingValueExceptionPhase = makeBodyLoweringPhase( +private val kotlinNothingValueExceptionPhase = makeIrModulePhase( ::KotlinNothingValueExceptionLowering, name = "KotlinNothingValueException", description = "Throw proper exception for calls returning value of type 'kotlin.Nothing'" ) -private val stripTypeAliasDeclarationsPhase = makeDeclarationTransformerPhase( +private val stripTypeAliasDeclarationsPhase = makeIrModulePhase( { StripTypeAliasDeclarationsLowering() }, name = "StripTypeAliasDeclarations", description = "Strip typealias declarations" ) -private val jsCodeOutliningPhase = makeBodyLoweringPhase( +private val jsCodeOutliningPhase = makeIrModulePhase( ::JsCodeOutliningLowering, name = "JsCodeOutliningLowering", description = "Outline js() calls where JS code references Kotlin locals" ) -private val arrayConstructorReferencePhase = makeBodyLoweringPhase( +private val arrayConstructorReferencePhase = makeIrModulePhase( ::ArrayConstructorReferenceLowering, name = "ArrayConstructorReference", description = "Transform `::Array` into a lambda" ) -private val arrayConstructorPhase = makeBodyLoweringPhase( +private val arrayConstructorPhase = makeIrModulePhase( ::ArrayConstructorLowering, name = "ArrayConstructor", description = "Transform `Array(size) { index -> value }` into a loop", prerequisite = setOf(arrayConstructorReferencePhase) ) -private val sharedVariablesLoweringPhase = makeBodyLoweringPhase( +private val sharedVariablesLoweringPhase = makeIrModulePhase( ::SharedVariablesLowering, name = "SharedVariablesLowering", description = "Box captured mutable variables", prerequisite = setOf(lateinitDeclarationLoweringPhase, lateinitUsageLoweringPhase) ) -private val localClassesInInlineLambdasPhase = makeBodyLoweringPhase( +private val localClassesInInlineLambdasPhase = makeIrModulePhase( ::LocalClassesInInlineLambdasLowering, name = "LocalClassesInInlineLambdasPhase", description = "Extract local classes from inline lambdas", prerequisite = setOf(inventNamesForLocalClassesPhase) ) -private val localClassesInInlineFunctionsPhase = makeBodyLoweringPhase( +private val localClassesInInlineFunctionsPhase = makeIrModulePhase( ::LocalClassesInInlineFunctionsLowering, name = "LocalClassesInInlineFunctionsPhase", description = "Extract local classes from inline functions", prerequisite = setOf(inventNamesForLocalClassesPhase) ) -private val localClassesExtractionFromInlineFunctionsPhase = makeBodyLoweringPhase( +private val localClassesExtractionFromInlineFunctionsPhase = makeIrModulePhase( { context -> LocalClassesExtractionFromInlineFunctionsLowering(context) }, name = "localClassesExtractionFromInlineFunctionsPhase", description = "Move local classes from inline functions into nearest declaration container", prerequisite = setOf(localClassesInInlineFunctionsPhase) ) -private val syntheticAccessorLoweringPhase = makeBodyLoweringPhase( +private val syntheticAccessorLoweringPhase = makeIrModulePhase( ::SyntheticAccessorLowering, name = "syntheticAccessorLoweringPhase", description = "Wrap top level inline function to access through them from inline functions" ) -private val wrapInlineDeclarationsWithReifiedTypeParametersLowering = makeBodyLoweringPhase( +private val wrapInlineDeclarationsWithReifiedTypeParametersLowering = makeIrModulePhase( ::WrapInlineDeclarationsWithReifiedTypeParametersLowering, name = "WrapInlineDeclarationsWithReifiedTypeParametersLowering", description = "Wrap inline declarations with reified type parameters" ) -private val saveInlineFunctionsBeforeInlining = makeDeclarationTransformerPhase( +private val saveInlineFunctionsBeforeInlining = makeIrModulePhase( ::SaveInlineFunctionsBeforeInlining, name = "SaveInlineFunctionsBeforeInlining", description = "Save inline function before inlining", @@ -281,7 +196,7 @@ private val saveInlineFunctionsBeforeInlining = makeDeclarationTransformerPhase( ) ) -private val functionInliningPhase = makeBodyLoweringPhase( +private val functionInliningPhase = makeIrModulePhase( { FunctionInlining( it, @@ -298,27 +213,27 @@ private val functionInliningPhase = makeBodyLoweringPhase( prerequisite = setOf(saveInlineFunctionsBeforeInlining) ) -private val copyInlineFunctionBodyLoweringPhase = makeDeclarationTransformerPhase( +private val copyInlineFunctionBodyLoweringPhase = makeIrModulePhase( ::CopyInlineFunctionBodyLowering, name = "CopyInlineFunctionBody", description = "Copy inline function body", prerequisite = setOf(functionInliningPhase) ) -private val removeInlineDeclarationsWithReifiedTypeParametersLoweringPhase = makeDeclarationTransformerPhase( +private val removeInlineDeclarationsWithReifiedTypeParametersLoweringPhase = makeIrModulePhase( { RemoveInlineDeclarationsWithReifiedTypeParametersLowering() }, name = "RemoveInlineFunctionsWithReifiedTypeParametersLowering", description = "Remove Inline functions with reified parameters from context", prerequisite = setOf(functionInliningPhase) ) -private val captureStackTraceInThrowablesPhase = makeBodyLoweringPhase( +private val captureStackTraceInThrowablesPhase = makeIrModulePhase( ::CaptureStackTraceInThrowables, name = "CaptureStackTraceInThrowables", description = "Capture stack trace in Throwable constructors" ) -private val throwableSuccessorsLoweringPhase = makeBodyLoweringPhase( +private val throwableSuccessorsLoweringPhase = makeIrModulePhase( { context -> context.run { val extendThrowableSymbol = @@ -332,53 +247,53 @@ private val throwableSuccessorsLoweringPhase = makeBodyLoweringPhase( prerequisite = setOf(captureStackTraceInThrowablesPhase) ) -private val tailrecLoweringPhase = makeBodyLoweringPhase( +private val tailrecLoweringPhase = makeIrModulePhase( ::TailrecLowering, name = "TailrecLowering", description = "Replace `tailrec` callsites with equivalent loop" ) -private val enumClassConstructorLoweringPhase = makeDeclarationTransformerPhase( +private val enumClassConstructorLoweringPhase = makeIrModulePhase( ::EnumClassConstructorLowering, name = "EnumClassConstructorLowering", description = "Transform Enum Class into regular Class" ) -private val enumClassConstructorBodyLoweringPhase = makeBodyLoweringPhase( +private val enumClassConstructorBodyLoweringPhase = makeIrModulePhase( ::EnumClassConstructorBodyTransformer, name = "EnumClassConstructorBodyLowering", description = "Transform Enum Class into regular Class" ) -private val enumEntryInstancesLoweringPhase = makeDeclarationTransformerPhase( +private val enumEntryInstancesLoweringPhase = makeIrModulePhase( ::EnumEntryInstancesLowering, name = "EnumEntryInstancesLowering", description = "Create instance variable for each enum entry initialized with `null`", prerequisite = setOf(enumClassConstructorLoweringPhase) ) -private val enumEntryInstancesBodyLoweringPhase = makeBodyLoweringPhase( +private val enumEntryInstancesBodyLoweringPhase = makeIrModulePhase( ::EnumEntryInstancesBodyLowering, name = "EnumEntryInstancesBodyLowering", description = "Insert enum entry field initialization into correxposnding class constructors", prerequisite = setOf(enumEntryInstancesLoweringPhase) ) -private val enumClassCreateInitializerLoweringPhase = makeDeclarationTransformerPhase( +private val enumClassCreateInitializerLoweringPhase = makeIrModulePhase( ::EnumClassCreateInitializerLowering, name = "EnumClassCreateInitializerLowering", description = "Create initializer for enum entries", prerequisite = setOf(enumClassConstructorLoweringPhase) ) -private val enumEntryCreateGetInstancesFunsLoweringPhase = makeDeclarationTransformerPhase( +private val enumEntryCreateGetInstancesFunsLoweringPhase = makeIrModulePhase( ::EnumEntryCreateGetInstancesFunsLowering, name = "EnumEntryCreateGetInstancesFunsLowering", description = "Create enumEntry_getInstance functions", prerequisite = setOf(enumClassConstructorLoweringPhase) ) -private val enumSyntheticFunsLoweringPhase = makeDeclarationTransformerPhase( +private val enumSyntheticFunsLoweringPhase = makeIrModulePhase( ::EnumSyntheticFunctionsAndPropertiesLowering, name = "EnumSyntheticFunctionsAndPropertiesLowering", description = "Implement `valueOf, `values` and `entries`", @@ -389,148 +304,148 @@ private val enumSyntheticFunsLoweringPhase = makeDeclarationTransformerPhase( ) ) -private val enumUsageLoweringPhase = makeBodyLoweringPhase( +private val enumUsageLoweringPhase = makeIrModulePhase( ::EnumUsageLowering, name = "EnumUsageLowering", description = "Replace enum access with invocation of corresponding function", prerequisite = setOf(enumEntryCreateGetInstancesFunsLoweringPhase) ) -private val externalEnumUsageLoweringPhase = makeBodyLoweringPhase( +private val externalEnumUsageLoweringPhase = makeIrModulePhase( ::ExternalEnumUsagesLowering, name = "ExternalEnumUsagesLowering", description = "Replace external enum entry accesses with field accesses" ) -private val enumEntryRemovalLoweringPhase = makeDeclarationTransformerPhase( +private val enumEntryRemovalLoweringPhase = makeIrModulePhase( ::EnumClassRemoveEntriesLowering, name = "EnumEntryRemovalLowering", description = "Replace enum entry with corresponding class", prerequisite = setOf(enumUsageLoweringPhase) ) -private val callableReferenceLowering = makeBodyLoweringPhase( +private val callableReferenceLowering = makeIrModulePhase( ::CallableReferenceLowering, name = "CallableReferenceLowering", description = "Build a lambda/callable reference class", prerequisite = setOf(functionInliningPhase, wrapInlineDeclarationsWithReifiedTypeParametersLowering) ) -private val returnableBlockLoweringPhase = makeBodyLoweringPhase( +private val returnableBlockLoweringPhase = makeIrModulePhase( ::JsReturnableBlockLowering, name = "JsReturnableBlockLowering", description = "Introduce temporary variable for result and change returnable block's type to Unit", prerequisite = setOf(functionInliningPhase) ) -private val rangeContainsLoweringPhase = makeBodyLoweringPhase( +private val rangeContainsLoweringPhase = makeIrModulePhase( ::RangeContainsLowering, name = "RangeContainsLowering", description = "[Optimization] Optimizes calls to contains() for ClosedRanges" ) -private val forLoopsLoweringPhase = makeBodyLoweringPhase( +private val forLoopsLoweringPhase = makeIrModulePhase( ::ForLoopsLowering, name = "ForLoopsLowering", description = "[Optimization] For loops lowering" ) -private val enumWhenPhase = makeJsModulePhase( +private val enumWhenPhase = makeIrModulePhase( ::EnumWhenLowering, name = "EnumWhenLowering", description = "Replace `when` subjects of enum types with their ordinals" -).toModuleLowering() +) -private val propertyLazyInitLoweringPhase = makeBodyLoweringPhase( +private val propertyLazyInitLoweringPhase = makeIrModulePhase( ::PropertyLazyInitLowering, name = "PropertyLazyInitLowering", description = "Make property init as lazy" ) -private val removeInitializersForLazyProperties = makeDeclarationTransformerPhase( +private val removeInitializersForLazyProperties = makeIrModulePhase( ::RemoveInitializersForLazyProperties, name = "RemoveInitializersForLazyProperties", description = "Remove property initializers if they was initialized lazily" ) -private val propertyAccessorInlinerLoweringPhase = makeBodyLoweringPhase( +private val propertyAccessorInlinerLoweringPhase = makeIrModulePhase( ::JsPropertyAccessorInlineLowering, name = "PropertyAccessorInlineLowering", description = "[Optimization] Inline property accessors" ) -private val copyPropertyAccessorBodiesLoweringPass = makeDeclarationTransformerPhase( +private val copyPropertyAccessorBodiesLoweringPass = makeIrModulePhase( ::CopyAccessorBodyLowerings, name = "CopyAccessorBodyLowering", description = "Copy accessor bodies so that ist can be safely read in PropertyAccessorInlineLowering", prerequisite = setOf(propertyAccessorInlinerLoweringPhase) ) -private val booleanPropertyInExternalLowering = makeBodyLoweringPhase( +private val booleanPropertyInExternalLowering = makeIrModulePhase( ::BooleanPropertyInExternalLowering, name = "BooleanPropertyInExternalLowering", description = "Lowering which wrap boolean in external declarations with Boolean() call and add diagnostic for such cases" ) -private val localDelegatedPropertiesLoweringPhase = makeBodyLoweringPhase( +private val localDelegatedPropertiesLoweringPhase = makeIrModulePhase( { LocalDelegatedPropertiesLowering() }, name = "LocalDelegatedPropertiesLowering", description = "Transform Local Delegated properties" ) -private val localDeclarationsLoweringPhase = makeBodyLoweringPhase( +private val localDeclarationsLoweringPhase = makeIrModulePhase( { context -> LocalDeclarationsLowering(context, suggestUniqueNames = false) }, name = "LocalDeclarationsLowering", description = "Move local declarations into nearest declaration container", prerequisite = setOf(sharedVariablesLoweringPhase, localDelegatedPropertiesLoweringPhase) ) -private val localClassExtractionPhase = makeBodyLoweringPhase( +private val localClassExtractionPhase = makeIrModulePhase( { context -> LocalClassPopupLowering(context) }, name = "LocalClassExtractionPhase", description = "Move local declarations into nearest declaration container", prerequisite = setOf(localDeclarationsLoweringPhase) ) -private val innerClassesLoweringPhase = makeDeclarationTransformerPhase( +private val innerClassesLoweringPhase = makeIrModulePhase( { context -> InnerClassesLowering(context, context.innerClassesSupport) }, name = "InnerClassesLowering", description = "Capture outer this reference to inner class" ) -private val innerClassesMemberBodyLoweringPhase = makeBodyLoweringPhase( +private val innerClassesMemberBodyLoweringPhase = makeIrModulePhase( { context -> InnerClassesMemberBodyLowering(context, context.innerClassesSupport) }, name = "InnerClassesMemberBody", description = "Replace `this` with 'outer this' field references", prerequisite = setOf(innerClassesLoweringPhase) ) -private val innerClassConstructorCallsLoweringPhase = makeBodyLoweringPhase( +private val innerClassConstructorCallsLoweringPhase = makeIrModulePhase( { context -> InnerClassConstructorCallsLowering(context, context.innerClassesSupport) }, name = "InnerClassConstructorCallsLowering", description = "Replace inner class constructor invocation" ) -private val suspendFunctionsLoweringPhase = makeBodyLoweringPhase( +private val suspendFunctionsLoweringPhase = makeIrModulePhase( ::JsSuspendFunctionsLowering, name = "SuspendFunctionsLowering", description = "Transform suspend functions into CoroutineImpl instance and build state machine" ) -private val addContinuationToNonLocalSuspendFunctionsLoweringPhase = makeDeclarationTransformerPhase( +private val addContinuationToNonLocalSuspendFunctionsLoweringPhase = makeIrModulePhase( ::AddContinuationToNonLocalSuspendFunctionsLowering, name = "AddContinuationToNonLocalSuspendFunctionsLowering", description = "Add explicit continuation as last parameter of non-local suspend functions" ) -private val addContinuationToLocalSuspendFunctionsLoweringPhase = makeBodyLoweringPhase( +private val addContinuationToLocalSuspendFunctionsLoweringPhase = makeIrModulePhase( ::AddContinuationToLocalSuspendFunctionsLowering, name = "AddContinuationToLocalSuspendFunctionsLowering", description = "Add explicit continuation as last parameter of local suspend functions" ) -private val addContinuationToFunctionCallsLoweringPhase = makeBodyLoweringPhase( +private val addContinuationToFunctionCallsLoweringPhase = makeIrModulePhase( ::AddContinuationToFunctionCallsLowering, name = "AddContinuationToFunctionCallsLowering", description = "Replace suspend function calls with calls with continuation", @@ -540,25 +455,25 @@ private val addContinuationToFunctionCallsLoweringPhase = makeBodyLoweringPhase( ) ) -private val privateMembersLoweringPhase = makeDeclarationTransformerPhase( +private val privateMembersLoweringPhase = makeIrModulePhase( ::PrivateMembersLowering, name = "PrivateMembersLowering", description = "Extract private members from classes" ) -private val privateMemberUsagesLoweringPhase = makeBodyLoweringPhase( +private val privateMemberUsagesLoweringPhase = makeIrModulePhase( ::PrivateMemberBodiesLowering, name = "PrivateMemberUsagesLowering", description = "Rewrite the private member usages" ) -private val propertyReferenceLoweringPhase = makeBodyLoweringPhase( +private val propertyReferenceLoweringPhase = makeIrModulePhase( ::PropertyReferenceLowering, name = "PropertyReferenceLowering", description = "Transform property references", ) -private val interopCallableReferenceLoweringPhase = makeBodyLoweringPhase( +private val interopCallableReferenceLoweringPhase = makeIrModulePhase( ::InteropCallableReferenceLowering, name = "InteropCallableReferenceLowering", description = "Interop layer for function references and lambdas", @@ -570,66 +485,66 @@ private val interopCallableReferenceLoweringPhase = makeBodyLoweringPhase( ) ) -private val defaultArgumentStubGeneratorPhase = makeDeclarationTransformerPhase( +private val defaultArgumentStubGeneratorPhase = makeIrModulePhase( ::JsDefaultArgumentStubGenerator, name = "DefaultArgumentStubGenerator", description = "Generate synthetic stubs for functions with default parameter values" ) -private val defaultArgumentPatchOverridesPhase = makeDeclarationTransformerPhase( +private val defaultArgumentPatchOverridesPhase = makeIrModulePhase( ::DefaultParameterPatchOverridenSymbolsLowering, name = "DefaultArgumentsPatchOverrides", description = "Patch overrides for fake override dispatch functions", prerequisite = setOf(defaultArgumentStubGeneratorPhase) ) -private val defaultParameterInjectorPhase = makeBodyLoweringPhase( +private val defaultParameterInjectorPhase = makeIrModulePhase( ::JsDefaultParameterInjector, name = "DefaultParameterInjector", description = "Replace callsite with default parameters with corresponding stub function", prerequisite = setOf(interopCallableReferenceLoweringPhase, innerClassesLoweringPhase) ) -private val defaultParameterCleanerPhase = makeDeclarationTransformerPhase( +private val defaultParameterCleanerPhase = makeIrModulePhase( ::DefaultParameterCleaner, name = "DefaultParameterCleaner", description = "Clean default parameters up" ) -private val varargLoweringPhase = makeBodyLoweringPhase( +private val varargLoweringPhase = makeIrModulePhase( ::VarargLowering, name = "VarargLowering", description = "Lower vararg arguments", prerequisite = setOf(interopCallableReferenceLoweringPhase) ) -private val propertiesLoweringPhase = makeDeclarationTransformerPhase( +private val propertiesLoweringPhase = makeIrModulePhase( { PropertiesLowering() }, name = "PropertiesLowering", description = "Move fields and accessors out from its property" ) -private val primaryConstructorLoweringPhase = makeDeclarationTransformerPhase( +private val primaryConstructorLoweringPhase = makeIrModulePhase( ::PrimaryConstructorLowering, name = "PrimaryConstructorLowering", description = "Creates primary constructor if it doesn't exist", prerequisite = setOf(enumClassConstructorLoweringPhase) ) -private val delegateToPrimaryConstructorLoweringPhase = makeBodyLoweringPhase( +private val delegateToPrimaryConstructorLoweringPhase = makeIrModulePhase( ::DelegateToSyntheticPrimaryConstructor, name = "DelegateToSyntheticPrimaryConstructor", description = "Delegates to synthetic primary constructor", prerequisite = setOf(primaryConstructorLoweringPhase) ) -private val annotationConstructorLowering = makeDeclarationTransformerPhase( +private val annotationConstructorLowering = makeIrModulePhase( ::AnnotationConstructorLowering, name = "AnnotationConstructorLowering", description = "Generate annotation constructor body" ) -private val initializersLoweringPhase = makeBodyLoweringPhase( +private val initializersLoweringPhase = makeIrModulePhase( ::InitializersLowering, name = "InitializersLowering", description = "Merge init block and field initializers into [primary] constructor", @@ -638,46 +553,46 @@ private val initializersLoweringPhase = makeBodyLoweringPhase( ) ) -private val initializersCleanupLoweringPhase = makeDeclarationTransformerPhase( +private val initializersCleanupLoweringPhase = makeIrModulePhase( ::InitializersCleanupLowering, name = "InitializersCleanupLowering", description = "Remove non-static anonymous initializers and field init expressions", prerequisite = setOf(initializersLoweringPhase) ) -private val multipleCatchesLoweringPhase = makeBodyLoweringPhase( +private val multipleCatchesLoweringPhase = makeIrModulePhase( ::MultipleCatchesLowering, name = "MultipleCatchesLowering", description = "Replace multiple catches with single one" ) -private val errorExpressionLoweringPhase = makeBodyLoweringPhase( +private val errorExpressionLoweringPhase = makeIrModulePhase( ::JsErrorExpressionLowering, name = "errorExpressionLoweringPhase", description = "Transform error expressions into simple ir code", prerequisite = setOf(multipleCatchesLoweringPhase) ) -private val errorDeclarationLoweringPhase = makeDeclarationTransformerPhase( +private val errorDeclarationLoweringPhase = makeIrModulePhase( ::JsErrorDeclarationLowering, name = "errorDeclarationLoweringPhase", description = "Transform error declarations into simple ir code" ) -private val bridgesConstructionPhase = makeDeclarationTransformerPhase( +private val bridgesConstructionPhase = makeIrModulePhase( ::JsBridgesConstruction, name = "BridgesConstruction", description = "Generate bridges", prerequisite = setOf(suspendFunctionsLoweringPhase) ) -private val singleAbstractMethodPhase = makeBodyLoweringPhase( +private val singleAbstractMethodPhase = makeIrModulePhase( ::JsSingleAbstractMethodLowering, name = "SingleAbstractMethod", description = "Replace SAM conversions with instances of interface-implementing classes" ) -private val typeOperatorLoweringPhase = makeBodyLoweringPhase( +private val typeOperatorLoweringPhase = makeIrModulePhase( ::TypeOperatorLowering, name = "TypeOperatorLowering", description = "Lower IrTypeOperator with corresponding logic", @@ -689,32 +604,32 @@ private val typeOperatorLoweringPhase = makeBodyLoweringPhase( ) ) -private val secondaryConstructorLoweringPhase = makeDeclarationTransformerPhase( +private val secondaryConstructorLoweringPhase = makeIrModulePhase( ::SecondaryConstructorLowering, name = "SecondaryConstructorLoweringPhase", description = "Generate static functions for each secondary constructor", prerequisite = setOf(innerClassesLoweringPhase) ) -private val secondaryFactoryInjectorLoweringPhase = makeBodyLoweringPhase( +private val secondaryFactoryInjectorLoweringPhase = makeIrModulePhase( ::SecondaryFactoryInjectorLowering, name = "SecondaryFactoryInjectorLoweringPhase", description = "Replace usage of secondary constructor with corresponding static function", prerequisite = setOf(innerClassesLoweringPhase) ) -private val constLoweringPhase = makeBodyLoweringPhase( +private val constLoweringPhase = makeIrModulePhase( ::ConstLowering, name = "ConstLowering", description = "Wrap Long and Char constants into constructor invocation" ) -private val inlineClassDeclarationLoweringPhase = makeDeclarationTransformerPhase( +private val inlineClassDeclarationLoweringPhase = makeIrModulePhase( { InlineClassLowering(it).inlineClassDeclarationLowering }, name = "InlineClassDeclarationLowering", description = "Handle inline class declarations" ) -private val inlineClassUsageLoweringPhase = makeBodyLoweringPhase( +private val inlineClassUsageLoweringPhase = makeIrModulePhase( { InlineClassLowering(it).inlineClassUsageLowering }, name = "InlineClassUsageLowering", description = "Handle inline class usages", @@ -725,117 +640,117 @@ private val inlineClassUsageLoweringPhase = makeBodyLoweringPhase( ) ) -private val autoboxingTransformerPhase = makeBodyLoweringPhase( +private val autoboxingTransformerPhase = makeIrModulePhase( ::AutoboxingTransformer, name = "AutoboxingTransformer", description = "Insert box/unbox intrinsics" ) -private val blockDecomposerLoweringPhase = makeBodyLoweringPhase( +private val blockDecomposerLoweringPhase = makeIrModulePhase( ::JsBlockDecomposerLowering, name = "BlockDecomposerLowering", description = "Transform statement-like-expression nodes into pure-statement to make it easily transform into JS", prerequisite = setOf(typeOperatorLoweringPhase, suspendFunctionsLoweringPhase) ) -private val jsClassUsageInReflectionPhase = makeBodyLoweringPhase( +private val jsClassUsageInReflectionPhase = makeIrModulePhase( ::JsClassUsageInReflectionLowering, name = "JsClassUsageInReflectionLowering", description = "[Optimization] Eliminate ClassReference and GetClassExpression usages in a simple case of usage raw js constructor", prerequisite = setOf(functionInliningPhase) ) -private val classReferenceLoweringPhase = makeBodyLoweringPhase( +private val classReferenceLoweringPhase = makeIrModulePhase( ::ClassReferenceLowering, name = "ClassReferenceLowering", description = "Handle class references", prerequisite = setOf(jsClassUsageInReflectionPhase) ) -private val primitiveCompanionLoweringPhase = makeBodyLoweringPhase( +private val primitiveCompanionLoweringPhase = makeIrModulePhase( ::PrimitiveCompanionLowering, name = "PrimitiveCompanionLowering", description = "Replace common companion object access with platform one" ) -private val callsLoweringPhase = makeBodyLoweringPhase( +private val callsLoweringPhase = makeIrModulePhase( ::CallsLowering, name = "CallsLowering", description = "Handle intrinsics" ) -private val staticMembersLoweringPhase = makeDeclarationTransformerPhase( +private val staticMembersLoweringPhase = makeIrModulePhase( ::StaticMembersLowering, name = "StaticMembersLowering", description = "Move static member declarations to top-level" ) -private val objectDeclarationLoweringPhase = makeDeclarationTransformerPhase( +private val objectDeclarationLoweringPhase = makeIrModulePhase( ::ObjectDeclarationLowering, name = "ObjectDeclarationLowering", description = "Create lazy object instance generator functions", prerequisite = setOf(enumClassCreateInitializerLoweringPhase) ) -private val invokeStaticInitializersPhase = makeBodyLoweringPhase( +private val invokeStaticInitializersPhase = makeIrModulePhase( ::InvokeStaticInitializersLowering, name = "IntroduceStaticInitializersLowering", description = "Invoke companion object's initializers from companion object in object constructor", prerequisite = setOf(objectDeclarationLoweringPhase) ) -private val es6AddBoxParameterToConstructorsLowering = makeDeclarationTransformerPhase( +private val es6AddBoxParameterToConstructorsLowering = makeIrModulePhase( ::ES6AddBoxParameterToConstructorsLowering, name = "ES6AddBoxParameterToConstructorsLowering", description = "Add box parameter to a constructor if needed", ) -private val es6ConstructorLowering = makeDeclarationTransformerPhase( +private val es6ConstructorLowering = makeIrModulePhase( ::ES6ConstructorLowering, name = "ES6ConstructorLowering", description = "Lower constructors declarations to support ES classes", prerequisite = setOf(es6AddBoxParameterToConstructorsLowering) ) -private val es6ConstructorUsageLowering = makeBodyLoweringPhase( +private val es6ConstructorUsageLowering = makeIrModulePhase( ::ES6ConstructorCallLowering, name = "ES6ConstructorCallLowering", description = "Lower constructor usages to support ES classes", prerequisite = setOf(es6ConstructorLowering) ) -private val objectUsageLoweringPhase = makeBodyLoweringPhase( +private val objectUsageLoweringPhase = makeIrModulePhase( ::ObjectUsageLowering, name = "ObjectUsageLowering", description = "Transform IrGetObjectValue into instance generator call", prerequisite = setOf(primaryConstructorLoweringPhase) ) -private val escapedIdentifiersLowering = makeBodyLoweringPhase( +private val escapedIdentifiersLowering = makeIrModulePhase( ::EscapedIdentifiersLowering, name = "EscapedIdentifiersLowering", description = "Convert global variables with invalid names access to globalThis member expression" ) -private val implicitlyExportedDeclarationsMarkingLowering = makeDeclarationTransformerPhase( +private val implicitlyExportedDeclarationsMarkingLowering = makeIrModulePhase( ::ImplicitlyExportedDeclarationsMarkingLowering, name = "ImplicitlyExportedDeclarationsMarkingLowering", description = "Add @JsImplicitExport annotation to declarations which are not exported but are used inside other exported declarations as a type" ) -private val cleanupLoweringPhase = makeBodyLoweringPhase( +private val cleanupLoweringPhase = makeIrModulePhase( { CleanupLowering() }, name = "CleanupLowering", description = "Clean up IR before codegen" ) -private val jsSuspendArityStorePhase = makeDeclarationTransformerPhase( +private val jsSuspendArityStorePhase = makeIrModulePhase( ::JsSuspendArityStoreLowering, name = "JsSuspendArityStoreLowering", description = "Store arity for suspend functions to not remove it during DCE" ) -val constEvaluationPhase = makeJsModulePhase( +val constEvaluationPhase = makeIrModulePhase( { context -> val configuration = IrInterpreterConfiguration( printOnlyExceptionMessage = true, @@ -845,15 +760,15 @@ val constEvaluationPhase = makeJsModulePhase( }, name = "ConstEvaluationLowering", description = "Evaluate functions that are marked as `IntrinsicConstEvaluation`", -).toModuleLowering() +) -val mainFunctionCallWrapperLowering = makeJsModulePhase( +val mainFunctionCallWrapperLowering = makeIrModulePhase( ::MainFunctionCallWrapperLowering, name = "MainFunctionCallWrapperLowering", description = "Generate main function call inside the wrapper-function" -).toModuleLowering() +) -val loweringList = listOf( +val loweringList = listOf>( scriptRemoveReceiverLowering, validateIrBeforeLowering, preventExportOfSyntheticDeclarationsLowering, @@ -973,53 +888,53 @@ val jsPhases = SameTypeNamedCompilerPhase( nlevels = 1 ) -private val es6CollectConstructorsWhichNeedBoxParameterLowering = makeDeclarationTransformerPhase( +private val es6CollectConstructorsWhichNeedBoxParameterLowering = makeIrModulePhase( ::ES6CollectConstructorsWhichNeedBoxParameters, name = "ES6CollectConstructorsWhichNeedBoxParameters", description = "[Optimization] Collect all of the constructors which requires box parameter", ) -private val es6BoxParameterOptimization = makeBodyLoweringPhase( +private val es6BoxParameterOptimization = makeIrModulePhase( ::ES6ConstructorBoxParameterOptimizationLowering, name = "ES6ConstructorBoxParameterOptimizationLowering", description = "[Optimization] Remove box parameter from the constructors which don't require box parameter", prerequisite = setOf(es6CollectConstructorsWhichNeedBoxParameterLowering) ) -private val es6CollectPrimaryConstructorsWhichCouldBeOptimizedLowering = makeDeclarationTransformerPhase( +private val es6CollectPrimaryConstructorsWhichCouldBeOptimizedLowering = makeIrModulePhase( ::ES6CollectPrimaryConstructorsWhichCouldBeOptimizedLowering, name = "ES6CollectPrimaryConstructorsWhichCouldBeOptimizedLowering", description = "[Optimization] Collect all of the constructors which could be translated into a regular constructor", ) -private val es6PrimaryConstructorOptimizationLowering = makeDeclarationTransformerPhase( +private val es6PrimaryConstructorOptimizationLowering = makeIrModulePhase( ::ES6PrimaryConstructorOptimizationLowering, name = "ES6PrimaryConstructorOptimizationLowering", description = "[Optimization] Replace synthetically generated static fabric method with a plain old ES6 constructors whenever it's possible", prerequisite = setOf(es6CollectPrimaryConstructorsWhichCouldBeOptimizedLowering) ) -private val es6PrimaryConstructorUsageOptimizationLowering = makeBodyLoweringPhase( +private val es6PrimaryConstructorUsageOptimizationLowering = makeIrModulePhase( ::ES6PrimaryConstructorUsageOptimizationLowering, name = "ES6PrimaryConstructorUsageOptimizationLowering", description = "[Optimization] Replace usage of synthetically generated static fabric method with a plain old ES6 constructors whenever it's possible", prerequisite = setOf(es6BoxParameterOptimization, es6PrimaryConstructorOptimizationLowering) ) -private val purifyObjectInstanceGetters = makeDeclarationTransformerPhase( +private val purifyObjectInstanceGetters = makeIrModulePhase( ::PurifyObjectInstanceGettersLowering, name = "PurifyObjectInstanceGettersLowering", description = "[Optimization] Make object instance getter functions pure whenever it's possible", ) -private val inlineObjectsWithPureInitialization = makeBodyLoweringPhase( +private val inlineObjectsWithPureInitialization = makeIrModulePhase( ::InlineObjectsWithPureInitializationLowering, name = "InlineObjectsWithPureInitializationLowering", description = "[Optimization] Inline object instance fields getters whenever it's possible", prerequisite = setOf(purifyObjectInstanceGetters) ) -val optimizationLoweringList = listOf( +val optimizationLoweringList = listOf>( es6CollectConstructorsWhichNeedBoxParameterLowering, es6CollectPrimaryConstructorsWhichCouldBeOptimizedLowering, es6BoxParameterOptimization, diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt index f20ebc5be61..2b047bd014c 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt @@ -141,7 +141,7 @@ fun compileIr( val phaserState = PhaserState() loweringList.forEachIndexed { _, lowering -> allModules.forEach { module -> - lowering.modulePhase.invoke(phaseConfig, phaserState, context, module) + lowering.invoke(phaseConfig, phaserState, context, module) } } } diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compilerWithIC.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compilerWithIC.kt index ebae7831522..41c5a4a9a20 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compilerWithIC.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compilerWithIC.kt @@ -81,7 +81,7 @@ fun lowerPreservingTags( loweringList.forEachIndexed { i, lowering -> controller.currentStage = i + 1 modules.forEach { module -> - lowering.modulePhase.invoke(phaseConfig, phaserState, context, module) + lowering.invoke(phaseConfig, phaserState, context, module) } } diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/optimizations.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/optimizations.kt index 7ff771d9a65..e6b7af92bc4 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/optimizations.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/optimizations.kt @@ -30,7 +30,7 @@ fun optimizeProgramByIr( val phaserState = PhaserState() optimizationLoweringList.forEachIndexed { _, lowering -> modules.forEach { module -> - lowering.modulePhase.invoke(phaseConfig, phaserState, context, module) + lowering.invoke(phaseConfig, phaserState, context, module) } } }