Convert InliningContext.java to Kotlin

This commit is contained in:
Mikhael Bogdanov
2017-05-12 14:14:56 +02:00
parent 04cf5d49d6
commit fcbd27fdc4
2 changed files with 48 additions and 96 deletions
@@ -14,81 +14,51 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.codegen.inline; package org.jetbrains.kotlin.codegen.inline
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.annotations.Nullable; import java.util.*
import org.jetbrains.kotlin.codegen.state.GenerationState;
import java.util.Collections; open class InliningContext(
import java.util.HashMap; val parent: InliningContext?,
import java.util.Map; private val expressionMap: Map<Int, LambdaInfo>,
val state: GenerationState,
val nameGenerator: NameGenerator,
val typeRemapper: TypeRemapper,
val reifiedTypeInliner: ReifiedTypeInliner,
val isInliningLambda: Boolean,
val classRegeneration: Boolean
) {
val internalNameToAnonymousObjectTransformationInfo: MutableMap<String, AnonymousObjectTransformationInfo> = HashMap()
public class InliningContext { var isContinuation: Boolean = false
@Nullable
private final InliningContext parent;
private final Map<Integer, LambdaInfo> expressionMap; fun subInline(generator: NameGenerator): InliningContext {
public final GenerationState state; return subInline(generator, emptyMap(), isInliningLambda)
public final NameGenerator nameGenerator;
public final TypeRemapper typeRemapper;
public final ReifiedTypeInliner reifiedTypeInliner;
public final boolean isInliningLambda;
public final boolean classRegeneration;
public final Map<String, AnonymousObjectTransformationInfo> internalNameToAnonymousObjectTransformationInfo = new HashMap<>();
private boolean isContinuation;
public InliningContext(
@Nullable InliningContext parent,
@NotNull Map<Integer, LambdaInfo> 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;
} }
@NotNull fun subInlineLambda(lambdaInfo: LambdaInfo): InliningContext {
public InliningContext subInline(@NotNull NameGenerator generator) { val map = HashMap<String, String?>()
return subInline(generator, Collections.emptyMap(), isInliningLambda); map.put(lambdaInfo.lambdaClassType.internalName, null) //mark lambda inlined
return subInline(nameGenerator.subGenerator("lambda"), map, true)
} }
@NotNull fun subInlineWithClassRegeneration(
public InliningContext subInlineLambda(@NotNull LambdaInfo lambdaInfo) { generator: NameGenerator,
Map<String, String> map = new HashMap<>(); newTypeMappings: MutableMap<String, String>,
map.put(lambdaInfo.getLambdaClassType().getInternalName(), null); //mark lambda inlined callSiteInfo: InlineCallSiteInfo
return subInline(nameGenerator.subGenerator("lambda"), map, true); ): InliningContext {
} return RegeneratedClassContext(
@NotNull
public InliningContext subInlineWithClassRegeneration(
@NotNull NameGenerator generator,
@NotNull Map<String, String> newTypeMappings,
@NotNull InlineCallSiteInfo callSiteInfo
) {
return new RegeneratedClassContext(
this, expressionMap, state, generator, TypeRemapper.createFrom(typeRemapper, newTypeMappings), this, expressionMap, state, generator, TypeRemapper.createFrom(typeRemapper, newTypeMappings),
reifiedTypeInliner, isInliningLambda, callSiteInfo reifiedTypeInliner, isInliningLambda, callSiteInfo
); )
} }
@NotNull private fun subInline(
private InliningContext subInline( generator: NameGenerator, additionalTypeMappings: Map<String, String?>, isInliningLambda: Boolean
@NotNull NameGenerator generator, @NotNull Map<String, String> additionalTypeMappings, boolean isInliningLambda ): InliningContext {
) {
//isInliningLambda && !this.isInliningLambda for root inline lambda //isInliningLambda && !this.isInliningLambda for root inline lambda
return new InliningContext( return InliningContext(
this, expressionMap, state, generator, this, expressionMap, state, generator,
TypeRemapper.createFrom( TypeRemapper.createFrom(
typeRemapper, typeRemapper,
@@ -97,44 +67,26 @@ public class InliningContext {
isInliningLambda && !this.isInliningLambda isInliningLambda && !this.isInliningLambda
), ),
reifiedTypeInliner, isInliningLambda, classRegeneration reifiedTypeInliner, isInliningLambda, classRegeneration
); )
} }
public boolean isRoot() { val isRoot: Boolean
return parent == null; get() = parent == null
}
@NotNull val root: RootInliningContext
public RootInliningContext getRoot() { get() = if (isRoot) this as RootInliningContext else parent!!.root
//noinspection ConstantConditions
return isRoot() ? (RootInliningContext) this : parent.getRoot();
}
@Nullable open val callSiteInfo: InlineCallSiteInfo
public InliningContext getParent() { get() {
return parent; assert(parent != null) { "At least root context should return proper value" }
} return parent!!.callSiteInfo
@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);
} }
return null; fun findAnonymousObjectTransformationInfo(internalName: String): AnonymousObjectTransformationInfo? {
} if (root.internalNameToAnonymousObjectTransformationInfo.containsKey(internalName)) {
return root.internalNameToAnonymousObjectTransformationInfo[internalName]
}
public boolean isContinuation() { return null
return isContinuation;
}
public void setContinuation(boolean continuation) {
isContinuation = continuation;
} }
} }
@@ -79,11 +79,11 @@ class TypeRemapper private constructor(
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
fun createFrom(remapper: TypeRemapper, mappings: MutableMap<String, String>, isInlineLambda: Boolean = false): TypeRemapper { fun createFrom(remapper: TypeRemapper, mappings: Map<String, String?>, isInlineLambda: Boolean = false): TypeRemapper {
return TypeRemapper(createNewAndMerge(remapper, mappings), remapper, isInlineLambda) return TypeRemapper(createNewAndMerge(remapper, mappings), remapper, isInlineLambda)
} }
private fun createNewAndMerge(remapper: TypeRemapper, additionalTypeMappings: Map<String, String>): MutableMap<String, String> { private fun createNewAndMerge(remapper: TypeRemapper, additionalTypeMappings: Map<String, String?>): MutableMap<String, String> {
return HashMap(remapper.typeMapping).apply { return HashMap(remapper.typeMapping).apply {
this += additionalTypeMappings this += additionalTypeMappings
} }