extractableAnalysisUtil: Drop some usages of KotlinBuiltIns.getInstance()

This commit is contained in:
Pavel V. Talanov
2015-09-21 20:23:29 +03:00
parent e42057c71c
commit f1c76f0467
2 changed files with 21 additions and 17 deletions
@@ -47,6 +47,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.resolveTopLevelClass import org.jetbrains.kotlin.resolve.descriptorUtil.resolveTopLevelClass
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typeUtil.builtIns
import org.jetbrains.kotlin.types.typeUtil.isUnit import org.jetbrains.kotlin.types.typeUtil.isUnit
import java.util.Collections import java.util.Collections
@@ -134,10 +135,11 @@ interface OutputValue {
class Jump( class Jump(
val elementsToReplace: List<JetExpression>, val elementsToReplace: List<JetExpression>,
val elementToInsertAfterCall: JetElement?, val elementToInsertAfterCall: JetElement?,
val conditional: Boolean val conditional: Boolean,
private val builtIns: KotlinBuiltIns
): OutputValue { ): OutputValue {
override val originalExpressions: List<JetExpression> get() = elementsToReplace override val originalExpressions: List<JetExpression> get() = elementsToReplace
override val valueType: JetType = with(KotlinBuiltIns.getInstance()) { if (conditional) getBooleanType() else getUnitType() } override val valueType: JetType = with(builtIns) { if (conditional) booleanType else unitType }
} }
class ParameterUpdate( class ParameterUpdate(
@@ -210,7 +212,7 @@ abstract class OutputValueBoxer(val outputValues: List<OutputValue>) {
1 -> return outputValues.first().valueType 1 -> return outputValues.first().valueType
2 -> module.resolveTopLevelClass(FqName("kotlin.Pair"), NoLookupLocation.FROM_IDE)!! 2 -> module.resolveTopLevelClass(FqName("kotlin.Pair"), NoLookupLocation.FROM_IDE)!!
3 -> module.resolveTopLevelClass(FqName("kotlin.Triple"), NoLookupLocation.FROM_IDE)!! 3 -> module.resolveTopLevelClass(FqName("kotlin.Triple"), NoLookupLocation.FROM_IDE)!!
else -> return DEFAULT_RETURN_TYPE else -> return module.builtIns.defaultReturnType
} }
return TypeUtils.substituteParameters(boxingClass, outputValueTypes) return TypeUtils.substituteParameters(boxingClass, outputValueTypes)
} }
@@ -250,9 +252,10 @@ abstract class OutputValueBoxer(val outputValues: List<OutputValue>) {
class AsList(outputValues: List<OutputValue>): OutputValueBoxer(outputValues) { class AsList(outputValues: List<OutputValue>): OutputValueBoxer(outputValues) {
override val returnType: JetType by lazy { override val returnType: JetType by lazy {
if (outputValues.isEmpty()) DEFAULT_RETURN_TYPE assert(outputValues.isNotEmpty())
else TypeUtils.substituteParameters( val builtIns = outputValues.first().valueType.builtIns
KotlinBuiltIns.getInstance().getList(), TypeUtils.substituteParameters(
builtIns.list,
Collections.singletonList(CommonSupertypes.commonSupertype(outputValues.map { it.valueType })) Collections.singletonList(CommonSupertypes.commonSupertype(outputValues.map { it.valueType }))
) )
} }
@@ -89,8 +89,8 @@ import org.jetbrains.kotlin.utils.DFS.Neighbors
import org.jetbrains.kotlin.utils.DFS.VisitedWithSet import org.jetbrains.kotlin.utils.DFS.VisitedWithSet
import java.util.* import java.util.*
internal val DEFAULT_RETURN_TYPE = KotlinBuiltIns.getInstance().getUnitType() internal val KotlinBuiltIns.defaultReturnType: JetType get() = unitType
private val DEFAULT_PARAMETER_TYPE = KotlinBuiltIns.getInstance().getNullableAnyType() internal val KotlinBuiltIns.defaultParameterType: JetType get() = nullableAnyType
private fun DeclarationDescriptor.renderForMessage(): String = private fun DeclarationDescriptor.renderForMessage(): String =
IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.render(this) IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.render(this)
@@ -147,7 +147,8 @@ private fun List<Instruction>.getExitPoints(): List<Instruction> =
private fun List<Instruction>.getResultTypeAndExpressions( private fun List<Instruction>.getResultTypeAndExpressions(
bindingContext: BindingContext, bindingContext: BindingContext,
targetScope: LexicalScope?, targetScope: LexicalScope?,
options: ExtractionOptions options: ExtractionOptions,
module: ModuleDescriptor
): Pair<JetType, List<JetExpression>> { ): Pair<JetType, List<JetExpression>> {
fun instructionToExpression(instruction: Instruction, unwrapReturn: Boolean): JetExpression? { fun instructionToExpression(instruction: Instruction, unwrapReturn: Boolean): JetExpression? {
return when (instruction) { return when (instruction) {
@@ -172,7 +173,7 @@ private fun List<Instruction>.getResultTypeAndExpressions(
} }
val resultTypes = map(::instructionToType).filterNotNull() val resultTypes = map(::instructionToType).filterNotNull()
var commonSupertype = if (resultTypes.isNotEmpty()) CommonSupertypes.commonSupertype(resultTypes) else DEFAULT_RETURN_TYPE var commonSupertype = if (resultTypes.isNotEmpty()) CommonSupertypes.commonSupertype(resultTypes) else module.builtIns.defaultReturnType
val resultType = if (options.allowSpecialClassNames) commonSupertype else commonSupertype.approximateWithResolvableType(targetScope, false) val resultType = if (options.allowSpecialClassNames) commonSupertype else commonSupertype.approximateWithResolvableType(targetScope, false)
val expressions = map { instructionToExpression(it, false) }.filterNotNull() val expressions = map { instructionToExpression(it, false) }.filterNotNull()
@@ -296,8 +297,8 @@ private fun ExtractionData.analyzeControlFlow(
val nonLocallyUsedDeclarations = getLocalDeclarationsWithNonLocalUsages(pseudocode, localInstructions, bindingContext) val nonLocallyUsedDeclarations = getLocalDeclarationsWithNonLocalUsages(pseudocode, localInstructions, bindingContext)
val (declarationsToCopy, declarationsToReport) = nonLocallyUsedDeclarations.partition { it is JetProperty && it.isLocal() } val (declarationsToCopy, declarationsToReport) = nonLocallyUsedDeclarations.partition { it is JetProperty && it.isLocal() }
val (typeOfDefaultFlow, defaultResultExpressions) = defaultExits.getResultTypeAndExpressions(bindingContext, targetScope, options) val (typeOfDefaultFlow, defaultResultExpressions) = defaultExits.getResultTypeAndExpressions(bindingContext, targetScope, options, module)
val (returnValueType, valuedReturnExpressions) = valuedReturnExits.getResultTypeAndExpressions(bindingContext, targetScope, options) val (returnValueType, valuedReturnExpressions) = valuedReturnExits.getResultTypeAndExpressions(bindingContext, targetScope, options, module)
val emptyControlFlow = val emptyControlFlow =
ControlFlow(Collections.emptyList(), { OutputValueBoxer.AsTuple(it, module) }, declarationsToCopy) ControlFlow(Collections.emptyList(), { OutputValueBoxer.AsTuple(it, module) }, declarationsToCopy)
@@ -341,7 +342,7 @@ private fun ExtractionData.analyzeControlFlow(
if (valuedReturnExits.size() != 1) return multipleExitsError if (valuedReturnExits.size() != 1) return multipleExitsError
val element = valuedReturnExits.first().element as JetExpression val element = valuedReturnExits.first().element as JetExpression
return controlFlow.copy(outputValues = Collections.singletonList(Jump(listOf(element), element, true))) to null return controlFlow.copy(outputValues = Collections.singletonList(Jump(listOf(element), element, true, module.builtIns))) to null
} }
if (getCommonNonTrivialSuccessorIfAny(valuedReturnExits) == null) return multipleExitsError if (getCommonNonTrivialSuccessorIfAny(valuedReturnExits) == null) return multipleExitsError
@@ -350,7 +351,7 @@ private fun ExtractionData.analyzeControlFlow(
outDeclarations.mapTo(outputValues) { outDeclarations.mapTo(outputValues) {
val descriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, it] as? CallableDescriptor val descriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, it] as? CallableDescriptor
Initializer(it as JetProperty, descriptor?.getReturnType() ?: DEFAULT_PARAMETER_TYPE) Initializer(it as JetProperty, descriptor?.getReturnType() ?: module.builtIns.defaultParameterType)
} }
outParameters.mapTo(outputValues) { ParameterUpdate(it, modifiedVarDescriptors[it.originalDescriptor]!!) } outParameters.mapTo(outputValues) { ParameterUpdate(it, modifiedVarDescriptors[it.originalDescriptor]!!) }
@@ -382,7 +383,7 @@ private fun ExtractionData.analyzeControlFlow(
val conditional = !singleExit && defaultExits.isNotEmpty() val conditional = !singleExit && defaultExits.isNotEmpty()
val elements = jumpExits.map { it.element as JetExpression } val elements = jumpExits.map { it.element as JetExpression }
val elementToInsertAfterCall = if (singleExit) null else elements.first() val elementToInsertAfterCall = if (singleExit) null else elements.first()
return controlFlow.copy(outputValues = Collections.singletonList(Jump(elements, elementToInsertAfterCall, conditional))) to null return controlFlow.copy(outputValues = Collections.singletonList(Jump(elements, elementToInsertAfterCall, conditional, module.builtIns))) to null
} }
return controlFlow to null return controlFlow to null
@@ -621,7 +622,7 @@ private fun ExtractionData.inferParametersInfo(
KotlinBuiltIns.getInstance().getFunctionType(Annotations.EMPTY, KotlinBuiltIns.getInstance().getFunctionType(Annotations.EMPTY,
originalDescriptor.getExtensionReceiverParameter()?.getType(), originalDescriptor.getExtensionReceiverParameter()?.getType(),
originalDescriptor.getValueParameters().map { it.getType() }, originalDescriptor.getValueParameters().map { it.getType() },
originalDescriptor.getReturnType() ?: DEFAULT_RETURN_TYPE) originalDescriptor.getReturnType() ?: originalDescriptor.builtIns.defaultReturnType)
} }
parameterExpression != null -> parameterExpression != null ->
(if (useSmartCastsIfPossible) bindingContext[BindingContext.SMARTCAST, parameterExpression] else null) (if (useSmartCastsIfPossible) bindingContext[BindingContext.SMARTCAST, parameterExpression] else null)
@@ -642,7 +643,7 @@ private fun ExtractionData.inferParametersInfo(
} }
receiverToExtract.exists() -> receiverToExtract.getType() receiverToExtract.exists() -> receiverToExtract.getType()
else -> null else -> null
} ?: DEFAULT_PARAMETER_TYPE } ?: originalDescriptor.builtIns.defaultParameterType
} }
for (refInfo in getBrokenReferencesInfo(createTemporaryCodeBlock())) { for (refInfo in getBrokenReferencesInfo(createTemporaryCodeBlock())) {