ReifiedTypeParameterMappings renamed to TypeParameterMappings, add isReified property

This commit is contained in:
Michael Bogdanov
2015-12-21 16:56:31 +03:00
parent 833cdb0706
commit dd336a5581
3 changed files with 27 additions and 21 deletions
@@ -2432,7 +2432,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
protected CallGenerator getOrCreateCallGenerator( protected CallGenerator getOrCreateCallGenerator(
@NotNull CallableDescriptor descriptor, @NotNull CallableDescriptor descriptor,
@Nullable KtElement callElement, @Nullable KtElement callElement,
@Nullable ReifiedTypeParameterMappings reifiedTypeParameterMappings @Nullable TypeParameterMappings typeParameterMappings
) { ) {
if (callElement == null) return defaultCallGenerator; if (callElement == null) return defaultCallGenerator;
@@ -2444,7 +2444,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
if (!isInline) return defaultCallGenerator; if (!isInline) return defaultCallGenerator;
SimpleFunctionDescriptor original = DescriptorUtils.unwrapFakeOverride((SimpleFunctionDescriptor) descriptor.getOriginal()); SimpleFunctionDescriptor original = DescriptorUtils.unwrapFakeOverride((SimpleFunctionDescriptor) descriptor.getOriginal());
return new InlineCodegen(this, state, original, callElement, reifiedTypeParameterMappings); return new InlineCodegen(this, state, original, callElement, typeParameterMappings);
} }
@NotNull @NotNull
@@ -2455,10 +2455,9 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
@NotNull @NotNull
private CallGenerator getOrCreateCallGenerator(@NotNull ResolvedCall<?> resolvedCall) { private CallGenerator getOrCreateCallGenerator(@NotNull ResolvedCall<?> resolvedCall) {
Map<TypeParameterDescriptor, KotlinType> typeArguments = resolvedCall.getTypeArguments(); Map<TypeParameterDescriptor, KotlinType> typeArguments = resolvedCall.getTypeArguments();
ReifiedTypeParameterMappings mappings = new ReifiedTypeParameterMappings(); TypeParameterMappings mappings = new TypeParameterMappings();
for (Map.Entry<TypeParameterDescriptor, KotlinType> entry : typeArguments.entrySet()) { for (Map.Entry<TypeParameterDescriptor, KotlinType> entry : typeArguments.entrySet()) {
TypeParameterDescriptor key = entry.getKey(); TypeParameterDescriptor key = entry.getKey();
if (!key.isReified()) continue;
KotlinType type = entry.getValue(); KotlinType type = entry.getValue();
TypeParameterDescriptor parameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(type); TypeParameterDescriptor parameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(type);
@@ -2471,13 +2470,13 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
key.getName().getIdentifier(), key.getName().getIdentifier(),
type, type,
asmType, asmType,
signatureWriter.toString() signatureWriter.toString(),
); key.isReified());
} }
else { else {
mappings.addParameterMappingToNewParameter( mappings.addParameterMappingToNewParameter(
key.getName().getIdentifier(), type, key.getName().getIdentifier(), type,
parameterDescriptor.getName().getIdentifier()); parameterDescriptor.getName().getIdentifier(), key.isReified());
} }
} }
return getOrCreateCallGenerator( return getOrCreateCallGenerator(
@@ -96,7 +96,7 @@ public class InlineCodegen extends CallGenerator {
@NotNull GenerationState state, @NotNull GenerationState state,
@NotNull SimpleFunctionDescriptor functionDescriptor, @NotNull SimpleFunctionDescriptor functionDescriptor,
@NotNull KtElement callElement, @NotNull KtElement callElement,
@Nullable ReifiedTypeParameterMappings typeParameterMappings @Nullable TypeParameterMappings typeParameterMappings
) { ) {
assert InlineUtil.isInline(functionDescriptor) : "InlineCodegen could inline only inline function: " + functionDescriptor; assert InlineUtil.isInline(functionDescriptor) : "InlineCodegen could inline only inline function: " + functionDescriptor;
@@ -33,7 +33,7 @@ import org.jetbrains.org.objectweb.asm.tree.*
private class ParameterNameAndNullability(val name: String, val nullable: Boolean) private class ParameterNameAndNullability(val name: String, val nullable: Boolean)
class ReifiedTypeInliner(private val parametersMapping: ReifiedTypeParameterMappings?) { class ReifiedTypeInliner(private val parametersMapping: TypeParameterMappings?) {
enum class OperationKind { enum class OperationKind {
NEW_ARRAY, AS, SAFE_AS, IS, JAVA_CLASS; NEW_ARRAY, AS, SAFE_AS, IS, JAVA_CLASS;
@@ -68,13 +68,16 @@ class ReifiedTypeInliner(private val parametersMapping: ReifiedTypeParameterMapp
private var maxStackSize = 0 private var maxStackSize = 0
private val hasReifiedParameters = parametersMapping?.hasReifiedParameters() ?: false
/** /**
* @return set of type parameters' identifiers contained in markers that should be reified further * @return set of type parameters' identifiers contained in markers that should be reified further
* e.g. when we're generating inline function containing reified T * e.g. when we're generating inline function containing reified T
* and another function containing reifiable parts is inlined into that function * and another function containing reifiable parts is inlined into that function
*/ */
fun reifyInstructions(node: MethodNode): ReifiedTypeParametersUsages { fun reifyInstructions(node: MethodNode): ReifiedTypeParametersUsages {
if (parametersMapping == null) return ReifiedTypeParametersUsages() if (!hasReifiedParameters) return ReifiedTypeParametersUsages()
val instructions = node.instructions val instructions = node.instructions
maxStackSize = 0 maxStackSize = 0
var result = ReifiedTypeParametersUsages() var result = ReifiedTypeParametersUsages()
@@ -91,8 +94,10 @@ class ReifiedTypeInliner(private val parametersMapping: ReifiedTypeParameterMapp
return result return result
} }
fun reifySignature(oldSignature: String): SignatureReificationResult { fun reifySignature(oldSignature: String): SignatureReificationResult {
if (parametersMapping == null) return SignatureReificationResult(oldSignature, ReifiedTypeParametersUsages()) if (!hasReifiedParameters) return SignatureReificationResult(oldSignature, ReifiedTypeParametersUsages())
val signatureRemapper = object : SignatureWriter() { val signatureRemapper = object : SignatureWriter() {
var typeParamsToReify = ReifiedTypeParametersUsages() var typeParamsToReify = ReifiedTypeParametersUsages()
@@ -117,7 +122,7 @@ class ReifiedTypeInliner(private val parametersMapping: ReifiedTypeParameterMapp
} }
} }
private fun getMappingByName(name: String?) = parametersMapping[name!!] private fun getMappingByName(name: String?) = parametersMapping!![name!!]
} }
SignatureReader(oldSignature).accept(signatureRemapper) SignatureReader(oldSignature).accept(signatureRemapper)
@@ -251,24 +256,26 @@ private val MethodInsnNode.operationKind: ReifiedTypeInliner.OperationKind? get(
ReifiedTypeInliner.OperationKind.values().getOrNull(it) ReifiedTypeInliner.OperationKind.values().getOrNull(it)
} }
class ReifiedTypeParameterMappings() { class TypeParameterMappings() {
private val mappingsByName = hashMapOf<String, ReifiedTypeParameterMapping>() private val mappingsByName = hashMapOf<String, TypeParameterMapping>()
fun addParameterMappingToType(name: String, type: KotlinType, asmType: Type, signature: String) { public fun addParameterMappingToType(name: String, type: KotlinType, asmType: Type, signature: String, isReified: Boolean) {
mappingsByName[name] = ReifiedTypeParameterMapping(name, type, asmType, newName = null, signature = signature) mappingsByName[name] = TypeParameterMapping(name, type, asmType, newName = null, signature = signature, isReified = isReified)
} }
fun addParameterMappingToNewParameter(name: String, type: KotlinType, newName: String) { public fun addParameterMappingToNewParameter(name: String, type: KotlinType, newName: String, isReified: Boolean) {
mappingsByName[name] = ReifiedTypeParameterMapping(name, type = type, asmType = null, newName = newName, signature = null) mappingsByName[name] = TypeParameterMapping(name, type, asmType = null, newName = newName, signature = null, isReified = isReified)
} }
operator fun get(name: String): ReifiedTypeParameterMapping? { operator fun get(name: String): TypeParameterMapping? {
return mappingsByName[name] return mappingsByName[name]
} }
fun hasReifiedParameters() = mappingsByName.values.any { it.isReified }
} }
class ReifiedTypeParameterMapping( class TypeParameterMapping(
val name: String, val type: KotlinType, val asmType: Type?, val newName: String?, val signature: String? val name: String, val type: KotlinType, val asmType: Type?, val newName: String?, val signature: String?, val isReified: Boolean
) )
class ReifiedTypeParametersUsages { class ReifiedTypeParametersUsages {