diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InliningContext.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InliningContext.kt index 3e7ff243fce..8fdd517d7be 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InliningContext.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InliningContext.kt @@ -14,81 +14,51 @@ * limitations under the License. */ -package org.jetbrains.kotlin.codegen.inline; +package org.jetbrains.kotlin.codegen.inline -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.codegen.state.GenerationState; +import org.jetbrains.kotlin.codegen.state.GenerationState +import java.util.* -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; +open class InliningContext( + val parent: InliningContext?, + private val expressionMap: Map, + val state: GenerationState, + val nameGenerator: NameGenerator, + val typeRemapper: TypeRemapper, + val reifiedTypeInliner: ReifiedTypeInliner, + val isInliningLambda: Boolean, + val classRegeneration: Boolean +) { + val internalNameToAnonymousObjectTransformationInfo: MutableMap = HashMap() -public class InliningContext { - @Nullable - private final InliningContext parent; + var isContinuation: Boolean = false - private final Map expressionMap; - public final GenerationState state; - public final NameGenerator nameGenerator; - public final TypeRemapper typeRemapper; - public final ReifiedTypeInliner reifiedTypeInliner; - public final boolean isInliningLambda; - public final boolean classRegeneration; - public final Map internalNameToAnonymousObjectTransformationInfo = new HashMap<>(); - - private boolean isContinuation; - - public InliningContext( - @Nullable InliningContext parent, - @NotNull Map expressionMap, - @NotNull GenerationState state, - @NotNull NameGenerator nameGenerator, - @NotNull TypeRemapper typeRemapper, - @NotNull ReifiedTypeInliner reifiedTypeInliner, - boolean isInliningLambda, - boolean classRegeneration - ) { - this.parent = parent; - this.expressionMap = expressionMap; - this.state = state; - this.nameGenerator = nameGenerator; - this.typeRemapper = typeRemapper; - this.reifiedTypeInliner = reifiedTypeInliner; - this.isInliningLambda = isInliningLambda; - this.classRegeneration = classRegeneration; + fun subInline(generator: NameGenerator): InliningContext { + return subInline(generator, emptyMap(), isInliningLambda) } - @NotNull - public InliningContext subInline(@NotNull NameGenerator generator) { - return subInline(generator, Collections.emptyMap(), isInliningLambda); + fun subInlineLambda(lambdaInfo: LambdaInfo): InliningContext { + val map = HashMap() + map.put(lambdaInfo.lambdaClassType.internalName, null) //mark lambda inlined + return subInline(nameGenerator.subGenerator("lambda"), map, true) } - @NotNull - public InliningContext subInlineLambda(@NotNull LambdaInfo lambdaInfo) { - Map map = new HashMap<>(); - map.put(lambdaInfo.getLambdaClassType().getInternalName(), null); //mark lambda inlined - return subInline(nameGenerator.subGenerator("lambda"), map, true); - } - - @NotNull - public InliningContext subInlineWithClassRegeneration( - @NotNull NameGenerator generator, - @NotNull Map newTypeMappings, - @NotNull InlineCallSiteInfo callSiteInfo - ) { - return new RegeneratedClassContext( + fun subInlineWithClassRegeneration( + generator: NameGenerator, + newTypeMappings: MutableMap, + callSiteInfo: InlineCallSiteInfo + ): InliningContext { + return RegeneratedClassContext( this, expressionMap, state, generator, TypeRemapper.createFrom(typeRemapper, newTypeMappings), reifiedTypeInliner, isInliningLambda, callSiteInfo - ); + ) } - @NotNull - private InliningContext subInline( - @NotNull NameGenerator generator, @NotNull Map additionalTypeMappings, boolean isInliningLambda - ) { + private fun subInline( + generator: NameGenerator, additionalTypeMappings: Map, isInliningLambda: Boolean + ): InliningContext { //isInliningLambda && !this.isInliningLambda for root inline lambda - return new InliningContext( + return InliningContext( this, expressionMap, state, generator, TypeRemapper.createFrom( typeRemapper, @@ -97,44 +67,26 @@ public class InliningContext { isInliningLambda && !this.isInliningLambda ), reifiedTypeInliner, isInliningLambda, classRegeneration - ); + ) } - public boolean isRoot() { - return parent == null; - } + val isRoot: Boolean + get() = parent == null - @NotNull - public RootInliningContext getRoot() { - //noinspection ConstantConditions - return isRoot() ? (RootInliningContext) this : parent.getRoot(); - } + val root: RootInliningContext + get() = if (isRoot) this as RootInliningContext else parent!!.root - @Nullable - public InliningContext getParent() { - return parent; - } - - @NotNull - public InlineCallSiteInfo getCallSiteInfo() { - assert parent != null : "At least root context should return proper value"; - return parent.getCallSiteInfo(); - } - - @Nullable - public AnonymousObjectTransformationInfo findAnonymousObjectTransformationInfo(@NotNull String internalName) { - if (getRoot().internalNameToAnonymousObjectTransformationInfo.containsKey(internalName)) { - return getRoot().internalNameToAnonymousObjectTransformationInfo.get(internalName); + open val callSiteInfo: InlineCallSiteInfo + get() { + assert(parent != null) { "At least root context should return proper value" } + return parent!!.callSiteInfo } - return null; - } + fun findAnonymousObjectTransformationInfo(internalName: String): AnonymousObjectTransformationInfo? { + if (root.internalNameToAnonymousObjectTransformationInfo.containsKey(internalName)) { + return root.internalNameToAnonymousObjectTransformationInfo[internalName] + } - public boolean isContinuation() { - return isContinuation; - } - - public void setContinuation(boolean continuation) { - isContinuation = continuation; + return null } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TypeRemapper.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TypeRemapper.kt index 9fa246c82f4..826e1b3947f 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TypeRemapper.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TypeRemapper.kt @@ -79,11 +79,11 @@ class TypeRemapper private constructor( @JvmStatic @JvmOverloads - fun createFrom(remapper: TypeRemapper, mappings: MutableMap, isInlineLambda: Boolean = false): TypeRemapper { + fun createFrom(remapper: TypeRemapper, mappings: Map, isInlineLambda: Boolean = false): TypeRemapper { return TypeRemapper(createNewAndMerge(remapper, mappings), remapper, isInlineLambda) } - private fun createNewAndMerge(remapper: TypeRemapper, additionalTypeMappings: Map): MutableMap { + private fun createNewAndMerge(remapper: TypeRemapper, additionalTypeMappings: Map): MutableMap { return HashMap(remapper.typeMapping).apply { this += additionalTypeMappings }