Refactoring. Merge CustomSubstitutionCapability, CustomFlexibleRendering and RawTypeTag into RawTypeCapability.

This commit is contained in:
Stanislav Erokhin
2016-04-22 22:23:03 +03:00
parent 922f133d75
commit 5fe48313e9
8 changed files with 23 additions and 32 deletions
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.resolve.jvm.platform
import org.jetbrains.kotlin.load.java.lazy.types.RawTypeTag
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.resolve.calls.checkers.AdditionalTypeChecker
import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext
@@ -52,7 +51,7 @@ object JavaGenericVarianceViolationTypeChecker : AdditionalTypeChecker {
// Use site variance projection is always the same for flexible types
if (lowerBound.constructor == upperBound.constructor) return
// Anything is acceptable for raw types
if (expectedType.getCapability<RawTypeTag>() != null) return
if (expectedType.getCapability<RawTypeCapability>() != null) return
val correspondingSubType = TypeCheckingProcedure.findCorrespondingSupertype(expressionTypeWithSmartCast, lowerBound) ?: return
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,10 +22,10 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor
import org.jetbrains.kotlin.load.java.lazy.types.RawSubstitution
import org.jetbrains.kotlin.load.java.lazy.types.RawTypeTag
import org.jetbrains.kotlin.resolve.ExternalOverridabilityCondition
import org.jetbrains.kotlin.resolve.ExternalOverridabilityCondition.Result
import org.jetbrains.kotlin.resolve.OverridingUtil
import org.jetbrains.kotlin.types.RawTypeCapability
import org.jetbrains.kotlin.types.getCapability
import org.jetbrains.kotlin.utils.singletonOrEmptyList
@@ -40,7 +40,7 @@ class ErasedOverridabilityCondition : ExternalOverridabilityCondition {
subDescriptor.returnType!! +
subDescriptor.extensionReceiverParameter?.type.singletonOrEmptyList()
if (signatureTypes.any { it.arguments.isNotEmpty() && it.getCapability<RawTypeTag>() == null }) return Result.UNKNOWN
if (signatureTypes.any { it.arguments.isNotEmpty() && it.getCapability<RawTypeCapability>() == null }) return Result.UNKNOWN
var erasedSuper = superDescriptor.substitute(RawSubstitution.buildSubstitutor()) ?: return Result.UNKNOWN
@@ -20,22 +20,18 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.load.java.components.TypeUsage
import org.jetbrains.kotlin.renderer.CustomFlexibleRendering
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.types.*
object RawTypeTag : TypeCapability
object RawTypeCapabilities : TypeCapabilities {
private object RawSubstitutionCapability : CustomSubstitutionCapability {
private object Impl : RawTypeCapability {
override val substitution: TypeSubstitution?
get() = RawSubstitution
override val substitutionToComposeWith: TypeSubstitution?
get() = RawSubstitution
}
private object RawFlexibleRendering : CustomFlexibleRendering {
private fun DescriptorRenderer.renderArguments(jetType: KotlinType) = jetType.arguments.map { renderTypeProjection(it) }
private fun String.replaceArgs(newArgs: String): String {
@@ -76,9 +72,7 @@ object RawTypeCapabilities : TypeCapabilities {
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
@Suppress("UNCHECKED_CAST")
return when(capabilityClass) {
CustomSubstitutionCapability::class.java -> RawSubstitutionCapability as T
CustomFlexibleRendering::class.java -> RawFlexibleRendering as T
RawTypeTag::class.java -> RawTypeTag as T
RawTypeCapability::class.java -> Impl as T
else -> null
}
}
@@ -154,7 +154,7 @@ internal class DescriptorRendererImpl(
private fun renderInflexibleType(type: KotlinType): String {
assert(!type.isFlexible()) { "Flexible types not allowed here: " + renderNormalizedType(type) }
val customResult = type.getCapability<CustomFlexibleRendering>()?.renderInflexible(type, this)
val customResult = type.getCapability<RawTypeCapability>()?.renderInflexible(type, this)
if (customResult != null) return customResult
if (type == CANT_INFER_FUNCTION_PARAM_TYPE || TypeUtils.isDontCarePlaceholder(type)) {
@@ -183,7 +183,7 @@ internal class DescriptorRendererImpl(
val lower = type.flexibility().lowerBound
val upper = type.flexibility().upperBound
val (lowerRendered, upperRendered) = type.getCapability<CustomFlexibleRendering>()?.renderBounds(type.flexibility(), this)
val (lowerRendered, upperRendered) = type.getCapability<RawTypeCapability>()?.renderBounds(type.flexibility(), this)
?: Pair(renderInflexibleType(lower), renderInflexibleType(upper))
if (differsOnlyInNullability(lowerRendered, upperRendered)) {
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@ abstract class AbstractLazyType(storageManager: StorageManager) : AbstractKotlin
override fun getSubstitution() = computeCustomSubstitution() ?: TypeConstructorSubstitution.create(constructor, getArguments())
protected open fun computeCustomSubstitution(): TypeSubstitution? = getCapability<CustomSubstitutionCapability>()?.substitution
protected open fun computeCustomSubstitution(): TypeSubstitution? = getCapability<RawTypeCapability>()?.substitution
private val memberScope = storageManager.createLazyValue { computeMemberScope() }
override fun getMemberScope() = memberScope()
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,13 +14,16 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.renderer
package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.types.Flexibility
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeCapability
import org.jetbrains.kotlin.renderer.DescriptorRenderer
interface RawTypeCapability : TypeCapability {
val substitution: TypeSubstitution?
val substitutionToComposeWith: TypeSubstitution?
interface CustomFlexibleRendering : TypeCapability {
fun renderInflexible(type: KotlinType, renderer: DescriptorRenderer): String?
fun renderBounds(flexibility: Flexibility, renderer: DescriptorRenderer): Pair<String, String>?
}
}
@@ -84,8 +84,3 @@ fun sameTypeConstructors(first: KotlinType, second: KotlinType): Boolean {
return first.getCapability(typeRangeCapability)?.sameTypeConstructor(second) ?: false
|| second.getCapability(typeRangeCapability)?.sameTypeConstructor(first) ?: false
}
interface CustomSubstitutionCapability : TypeCapability {
val substitution: TypeSubstitution?
val substitutionToComposeWith: TypeSubstitution?
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -117,7 +117,7 @@ fun KotlinType.computeNewSubstitution(
val newSubstitution = TypeConstructorSubstitution.create(typeConstructor, newArguments)
// If previous substitution was trivial just replace it with indexed one
val substitutionToComposeWith = getCapability<CustomSubstitutionCapability>()?.substitutionToComposeWith ?: return newSubstitution
val substitutionToComposeWith = getCapability<RawTypeCapability>()?.substitutionToComposeWith ?: return newSubstitution
val composedSubstitution = CompositeTypeSubstitution(newSubstitution, substitutionToComposeWith)
return composedSubstitution