Refactoring: Replace factory functions with constructors

This commit is contained in:
Alexey Sedunov
2015-07-10 03:18:01 +03:00
parent 290983687d
commit 34cd2e0ac3
3 changed files with 48 additions and 60 deletions
@@ -446,7 +446,7 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
@NotNull Map<PseudoValue, TypePredicate> expectedTypes, @NotNull Map<PseudoValue, TypePredicate> expectedTypes,
@NotNull MagicKind kind @NotNull MagicKind kind
) { ) {
MagicInstruction instruction = MagicInstruction.Factory.create( MagicInstruction instruction = new MagicInstruction(
instructionElement, valueElement, getCurrentScope(), inputValues, expectedTypes, kind, valueFactory instructionElement, valueElement, getCurrentScope(), inputValues, expectedTypes, kind, valueFactory
); );
add(instruction); add(instruction);
@@ -456,7 +456,7 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
@NotNull @NotNull
@Override @Override
public MergeInstruction merge(@NotNull JetExpression expression, @NotNull List<PseudoValue> inputValues) { public MergeInstruction merge(@NotNull JetExpression expression, @NotNull List<PseudoValue> inputValues) {
MergeInstruction instruction = MergeInstruction.Factory.create(expression, getCurrentScope(), inputValues, valueFactory); MergeInstruction instruction = new MergeInstruction(expression, getCurrentScope(), inputValues, valueFactory);
add(instruction); add(instruction);
return instruction; return instruction;
} }
@@ -480,7 +480,7 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
@NotNull Map<PseudoValue, ValueParameterDescriptor> arguments @NotNull Map<PseudoValue, ValueParameterDescriptor> arguments
) { ) {
JetType returnType = resolvedCall.getResultingDescriptor().getReturnType(); JetType returnType = resolvedCall.getResultingDescriptor().getReturnType();
CallInstruction instruction = CallInstruction.Factory.create( CallInstruction instruction = new CallInstruction(
valueElement, valueElement,
getCurrentScope(), getCurrentScope(),
resolvedCall, resolvedCall,
@@ -537,7 +537,7 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
@NotNull Map<PseudoValue, ReceiverValue> receiverValues @NotNull Map<PseudoValue, ReceiverValue> receiverValues
) { ) {
AccessTarget accessTarget = resolvedCall != null ? new AccessTarget.Call(resolvedCall) : AccessTarget.BlackBox.INSTANCE$; AccessTarget accessTarget = resolvedCall != null ? new AccessTarget.Call(resolvedCall) : AccessTarget.BlackBox.INSTANCE$;
ReadValueInstruction instruction = ReadValueInstruction.Factory.create( ReadValueInstruction instruction = new ReadValueInstruction(
expression, getCurrentScope(), accessTarget, receiverValues, valueFactory expression, getCurrentScope(), accessTarget, receiverValues, valueFactory
); );
add(instruction); add(instruction);
@@ -29,10 +29,10 @@ import org.jetbrains.kotlin.cfg.pseudocode.instructions.InstructionVisitorWithRe
import org.jetbrains.kotlin.cfg.pseudocode.instructions.InstructionImpl import org.jetbrains.kotlin.cfg.pseudocode.instructions.InstructionImpl
import org.jetbrains.kotlin.psi.JetNamedDeclaration import org.jetbrains.kotlin.psi.JetNamedDeclaration
public trait AccessTarget { public sealed class AccessTarget {
public data class Declaration(val descriptor: VariableDescriptor): AccessTarget public data class Declaration(val descriptor: VariableDescriptor): AccessTarget()
public data class Call(val resolvedCall: ResolvedCall<*>): AccessTarget public data class Call(val resolvedCall: ResolvedCall<*>): AccessTarget()
public object BlackBox: AccessTarget public object BlackBox: AccessTarget()
} }
public abstract class AccessValueInstruction protected constructor( public abstract class AccessValueInstruction protected constructor(
@@ -49,8 +49,14 @@ public class ReadValueInstruction private constructor(
receiverValues: Map<PseudoValue, ReceiverValue>, receiverValues: Map<PseudoValue, ReceiverValue>,
private var _outputValue: PseudoValue? private var _outputValue: PseudoValue?
) : AccessValueInstruction(element, lexicalScope, target, receiverValues), InstructionWithValue { ) : AccessValueInstruction(element, lexicalScope, target, receiverValues), InstructionWithValue {
private fun newResultValue(factory: PseudoValueFactory, valueElement: JetElement) { public constructor(
_outputValue = factory.newValue(valueElement, this) element: JetElement,
lexicalScope: LexicalScope,
target: AccessTarget,
receiverValues: Map<PseudoValue, ReceiverValue>,
factory: PseudoValueFactory
): this(element, lexicalScope, target, receiverValues, null) {
_outputValue = factory.newValue(element, this)
} }
override val inputValues: List<PseudoValue> override val inputValues: List<PseudoValue>
@@ -82,21 +88,6 @@ public class ReadValueInstruction private constructor(
override fun createCopy(): InstructionImpl = override fun createCopy(): InstructionImpl =
ReadValueInstruction(element, lexicalScope, target, receiverValues, outputValue) ReadValueInstruction(element, lexicalScope, target, receiverValues, outputValue)
companion object Factory {
public fun create (
element: JetElement,
lexicalScope: LexicalScope,
target: AccessTarget,
receiverValues: Map<PseudoValue, ReceiverValue>,
factory: PseudoValueFactory
): ReadValueInstruction {
return ReadValueInstruction(element, lexicalScope, target, receiverValues, null).let { instruction ->
instruction.newResultValue(factory, element)
instruction
}
}
}
} }
public class WriteValueInstruction( public class WriteValueInstruction(
@@ -60,6 +60,17 @@ public class CallInstruction private constructor(
override val receiverValues: Map<PseudoValue, ReceiverValue>, override val receiverValues: Map<PseudoValue, ReceiverValue>,
public val arguments: Map<PseudoValue, ValueParameterDescriptor> public val arguments: Map<PseudoValue, ValueParameterDescriptor>
) : OperationInstruction(element, lexicalScope, receiverValues.keySet() + arguments.keySet()), InstructionWithReceivers { ) : OperationInstruction(element, lexicalScope, receiverValues.keySet() + arguments.keySet()), InstructionWithReceivers {
public constructor (
element: JetElement,
lexicalScope: LexicalScope,
resolvedCall: ResolvedCall<*>,
receiverValues: Map<PseudoValue, ReceiverValue>,
arguments: Map<PseudoValue, ValueParameterDescriptor>,
factory: PseudoValueFactory?
): this(element, lexicalScope, resolvedCall, receiverValues, arguments) {
setResult(factory)
}
override fun accept(visitor: InstructionVisitor) { override fun accept(visitor: InstructionVisitor) {
visitor.visitCallInstruction(this) visitor.visitCallInstruction(this)
} }
@@ -73,18 +84,6 @@ public class CallInstruction private constructor(
override fun toString() = override fun toString() =
renderInstruction("call", "${render(element)}, ${resolvedCall.getResultingDescriptor()!!.getName()}") renderInstruction("call", "${render(element)}, ${resolvedCall.getResultingDescriptor()!!.getName()}")
companion object Factory {
fun create (
element: JetElement,
lexicalScope: LexicalScope,
resolvedCall: ResolvedCall<*>,
receiverValues: Map<PseudoValue, ReceiverValue>,
arguments: Map<PseudoValue, ValueParameterDescriptor>,
factory: PseudoValueFactory?
): CallInstruction =
CallInstruction(element, lexicalScope, resolvedCall, receiverValues, arguments).setResult(factory, element) as CallInstruction
}
} }
// Introduces black-box operation // Introduces black-box operation
@@ -99,6 +98,18 @@ public class MagicInstruction(
val expectedTypes: Map<PseudoValue, TypePredicate>, val expectedTypes: Map<PseudoValue, TypePredicate>,
val kind: MagicKind val kind: MagicKind
) : OperationInstruction(element, lexicalScope, inputValues) { ) : OperationInstruction(element, lexicalScope, inputValues) {
public constructor (
element: JetElement,
valueElement: JetElement?,
lexicalScope: LexicalScope,
inputValues: List<PseudoValue>,
expectedTypes: Map<PseudoValue, TypePredicate>,
kind: MagicKind,
factory: PseudoValueFactory
): this(element, lexicalScope, inputValues, expectedTypes, kind) {
setResult(factory, valueElement)
}
public val synthetic: Boolean get() = outputValue.element == null public val synthetic: Boolean get() = outputValue.element == null
override val outputValue: PseudoValue override val outputValue: PseudoValue
@@ -112,20 +123,6 @@ public class MagicInstruction(
MagicInstruction(element, lexicalScope, inputValues, expectedTypes, kind).setResult(resultValue) MagicInstruction(element, lexicalScope, inputValues, expectedTypes, kind).setResult(resultValue)
override fun toString() = renderInstruction("magic[$kind]", render(element)) override fun toString() = renderInstruction("magic[$kind]", render(element))
companion object Factory {
fun create(
element: JetElement,
valueElement: JetElement?,
lexicalScope: LexicalScope,
inputValues: List<PseudoValue>,
expectedTypes: Map<PseudoValue, TypePredicate>,
kind: MagicKind,
factory: PseudoValueFactory
): MagicInstruction = MagicInstruction(
element, lexicalScope, inputValues, expectedTypes, kind
).setResult(factory, valueElement) as MagicInstruction
}
} }
public enum class MagicKind(val sideEffectFree: Boolean = false) { public enum class MagicKind(val sideEffectFree: Boolean = false) {
@@ -155,6 +152,15 @@ class MergeInstruction private constructor(
lexicalScope: LexicalScope, lexicalScope: LexicalScope,
inputValues: List<PseudoValue> inputValues: List<PseudoValue>
): OperationInstruction(element, lexicalScope, inputValues) { ): OperationInstruction(element, lexicalScope, inputValues) {
public constructor (
element: JetElement,
lexicalScope: LexicalScope,
inputValues: List<PseudoValue>,
factory: PseudoValueFactory
): this(element, lexicalScope, inputValues) {
setResult(factory)
}
override val outputValue: PseudoValue override val outputValue: PseudoValue
get() = resultValue!! get() = resultValue!!
@@ -165,13 +171,4 @@ class MergeInstruction private constructor(
override fun createCopy() = MergeInstruction(element, lexicalScope, inputValues).setResult(resultValue) override fun createCopy() = MergeInstruction(element, lexicalScope, inputValues).setResult(resultValue)
override fun toString() = renderInstruction("merge", render(element)) override fun toString() = renderInstruction("merge", render(element))
companion object Factory {
fun create(
element: JetElement,
lexicalScope: LexicalScope,
inputValues: List<PseudoValue>,
factory: PseudoValueFactory
): MergeInstruction = MergeInstruction(element, lexicalScope, inputValues).setResult(factory) as MergeInstruction
}
} }