Pseudocode: Record resolved information in read/write instructions. Introduce AccessValueInstruction

This commit is contained in:
Alexey Sedunov
2014-06-06 14:31:31 +04:00
parent 587a9558bc
commit 9b13d05259
11 changed files with 171 additions and 76 deletions
@@ -20,9 +20,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue; import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue;
import org.jetbrains.jet.lang.cfg.pseudocode.Pseudocode; import org.jetbrains.jet.lang.cfg.pseudocode.Pseudocode;
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.eval.AccessTarget;
import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor; import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor; import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant; import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
@@ -126,9 +126,12 @@ public interface JetControlFlowBuilder {
@NotNull @NotNull
PseudoValue readThis(@NotNull JetExpression expression, @Nullable ReceiverParameterDescriptor parameterDescriptor); PseudoValue readThis(@NotNull JetExpression expression, @Nullable ReceiverParameterDescriptor parameterDescriptor);
@NotNull @NotNull
PseudoValue readVariable( PseudoValue readVariable(
@NotNull JetExpression expression, @Nullable VariableDescriptor variableDescriptor, @Nullable PseudoValue receiverValue @NotNull JetExpression expression,
@NotNull ResolvedCall<?> resolvedCall,
@NotNull Map<PseudoValue, ReceiverValue> receiverValues
); );
@Nullable @Nullable
@@ -153,7 +156,12 @@ public interface JetControlFlowBuilder {
void compilationError(@NotNull JetElement element, @NotNull String message); void compilationError(@NotNull JetElement element, @NotNull String message);
void write(@NotNull JetElement assignment, @NotNull JetElement lValue, @NotNull PseudoValue rValue, @Nullable PseudoValue receiverValue); void write(
@NotNull JetElement assignment,
@NotNull JetElement lValue,
@NotNull PseudoValue rValue,
@NotNull AccessTarget target,
@NotNull Map<PseudoValue, ReceiverValue> receiverValues);
// Other // Other
void unsupported(JetElement element); void unsupported(JetElement element);
@@ -20,9 +20,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue; import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue;
import org.jetbrains.jet.lang.cfg.pseudocode.Pseudocode; import org.jetbrains.jet.lang.cfg.pseudocode.Pseudocode;
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.eval.AccessTarget;
import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor; import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor; import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant; import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
@@ -94,9 +94,9 @@ public abstract class JetControlFlowBuilderAdapter implements JetControlFlowBuil
@Override @Override
public PseudoValue readVariable( public PseudoValue readVariable(
@NotNull JetExpression expression, @NotNull JetExpression expression,
@Nullable VariableDescriptor variableDescriptor, @NotNull ResolvedCall<?> resolvedCall,
@Nullable PseudoValue receiverValue) { @NotNull Map<PseudoValue, ReceiverValue> receiverValues) {
return getDelegateBuilder().readVariable(expression, variableDescriptor, receiverValue); return getDelegateBuilder().readVariable(expression, resolvedCall, receiverValues);
} }
@Nullable @Nullable
@@ -258,8 +258,9 @@ public abstract class JetControlFlowBuilderAdapter implements JetControlFlowBuil
@NotNull JetElement assignment, @NotNull JetElement assignment,
@NotNull JetElement lValue, @NotNull JetElement lValue,
@NotNull PseudoValue rValue, @NotNull PseudoValue rValue,
@Nullable PseudoValue receiverValue) { @NotNull AccessTarget target,
getDelegateBuilder().write(assignment, lValue, rValue, receiverValue); @NotNull Map<PseudoValue, ReceiverValue> receiverValues) {
getDelegateBuilder().write(assignment, lValue, rValue, target, receiverValues);
} }
@Override @Override
@@ -32,8 +32,10 @@ import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowInstructionsGenerator
import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue; import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue;
import org.jetbrains.jet.lang.cfg.pseudocode.Pseudocode; import org.jetbrains.jet.lang.cfg.pseudocode.Pseudocode;
import org.jetbrains.jet.lang.cfg.pseudocode.PseudocodeImpl; import org.jetbrains.jet.lang.cfg.pseudocode.PseudocodeImpl;
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.eval.AccessTarget;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils; import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.BindingTrace; import org.jetbrains.jet.lang.resolve.BindingTrace;
@@ -230,6 +232,30 @@ public class JetControlFlowProcessor {
); );
} }
private void generateInitializer(@NotNull JetDeclaration declaration, @NotNull PseudoValue initValue) {
builder.write(
declaration,
declaration,
initValue,
getDeclarationAccessTarget(declaration),
Collections.<PseudoValue, ReceiverValue>emptyMap()
);
}
@NotNull
private AccessTarget getResolvedCallAccessTarget(JetElement element) {
ResolvedCall<?> resolvedCall = trace.get(BindingContext.RESOLVED_CALL, element);
return resolvedCall != null ? new AccessTarget.Call(resolvedCall) : AccessTarget.BlackBox.instance$;
}
@NotNull
private AccessTarget getDeclarationAccessTarget(JetElement element) {
DeclarationDescriptor descriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, element);
return descriptor instanceof VariableDescriptor
? new AccessTarget.Declaration((VariableDescriptor) descriptor)
: AccessTarget.BlackBox.instance$;
}
@Override @Override
public void visitParenthesizedExpressionVoid(@NotNull JetParenthesizedExpression expression, CFPContext context) { public void visitParenthesizedExpressionVoid(@NotNull JetParenthesizedExpression expression, CFPContext context) {
mark(expression); mark(expression);
@@ -407,20 +433,22 @@ public class JetControlFlowProcessor {
} }
PseudoValue rhsValue = rhsDeferredValue.invoke(); PseudoValue rhsValue = rhsDeferredValue.invoke();
PseudoValue receiverValue = null; Map<PseudoValue, ReceiverValue> receiverValues = SmartFMap.emptyMap();
if (left instanceof JetSimpleNameExpression || left instanceof JetProperty) { AccessTarget accessTarget = AccessTarget.BlackBox.instance$;
// Do nothing, just record write below if (left instanceof JetSimpleNameExpression || left instanceof JetQualifiedExpression) {
accessTarget = getResolvedCallAccessTarget(PsiUtilPackage.getQualifiedElementSelector(left));
if (accessTarget instanceof AccessTarget.Call) {
receiverValues = getReceiverValues(((AccessTarget.Call) accessTarget).getResolvedCall(), true);
}
} }
else if (left instanceof JetQualifiedExpression) { else if (left instanceof JetProperty) {
JetExpression receiverExpression = ((JetQualifiedExpression) left).getReceiverExpression(); accessTarget = getDeclarationAccessTarget(left);
generateInstructions(receiverExpression, NOT_IN_CONDITION);
receiverValue = builder.getBoundValue(receiverExpression);
} }
else { else {
builder.unsupported(parentExpression); // TODO builder.unsupported(parentExpression); // TODO
} }
recordWrite(left, rhsValue, receiverValue, parentExpression); recordWrite(left, accessTarget, rhsValue, receiverValues, parentExpression);
} }
private void generateArrayAssignment( private void generateArrayAssignment(
@@ -491,11 +519,21 @@ public class JetControlFlowProcessor {
return argumentValues; return argumentValues;
} }
private void recordWrite(JetExpression left, PseudoValue rightValue, PseudoValue receiverValue, JetExpression parentExpression) { private void recordWrite(
@NotNull JetExpression left,
@NotNull AccessTarget target,
@Nullable PseudoValue rightValue,
@NotNull Map<PseudoValue, ReceiverValue> receiverValues,
@NotNull JetExpression parentExpression) {
VariableDescriptor descriptor = BindingContextUtils.extractVariableDescriptorIfAny(trace.getBindingContext(), left, false); VariableDescriptor descriptor = BindingContextUtils.extractVariableDescriptorIfAny(trace.getBindingContext(), left, false);
if (descriptor != null) { if (descriptor != null) {
builder.write(parentExpression, left, rightValue != null ? rightValue : createSyntheticValue(parentExpression), builder.write(
receiverValue); parentExpression,
left,
rightValue != null ? rightValue : createSyntheticValue(parentExpression),
target,
receiverValues
);
} }
} }
@@ -712,7 +750,7 @@ public class JetControlFlowProcessor {
JetParameter catchParameter = catchClause.getCatchParameter(); JetParameter catchParameter = catchClause.getCatchParameter();
if (catchParameter != null) { if (catchParameter != null) {
builder.declareParameter(catchParameter); builder.declareParameter(catchParameter);
builder.write(catchParameter, catchParameter, createSyntheticValue(catchParameter), null); generateInitializer(catchParameter, createSyntheticValue(catchParameter));
} }
JetExpression catchBody = catchClause.getCatchBody(); JetExpression catchBody = catchClause.getCatchBody();
if (catchBody != null) { if (catchBody != null) {
@@ -834,11 +872,11 @@ public class JetControlFlowProcessor {
); );
if (loopParameter != null) { if (loopParameter != null) {
builder.write(loopParameter, loopParameter, value, null); generateInitializer(loopParameter, value);
} }
else if (multiDeclaration != null) { else if (multiDeclaration != null) {
for (JetMultiDeclarationEntry entry : multiDeclaration.getEntries()) { for (JetMultiDeclarationEntry entry : multiDeclaration.getEntries()) {
builder.write(entry, entry, value, null); generateInitializer(entry, value);
} }
} }
} }
@@ -937,7 +975,7 @@ public class JetControlFlowProcessor {
if (defaultValue != null) { if (defaultValue != null) {
generateInstructions(defaultValue, context); generateInstructions(defaultValue, context);
} }
builder.write(parameter, parameter, createSyntheticValue(parameter), null); generateInitializer(parameter, createSyntheticValue(parameter));
} }
@Override @Override
@@ -1079,7 +1117,7 @@ public class JetControlFlowProcessor {
: builder.magic(entry, null, : builder.magic(entry, null,
ContainerUtil.createMaybeSingletonList(builder.getBoundValue(initializer)), true); ContainerUtil.createMaybeSingletonList(builder.getBoundValue(initializer)), true);
if (generateWriteForEntries) { if (generateWriteForEntries) {
builder.write(entry, entry, writtenValue != null ? writtenValue : createSyntheticValue(entry), null); generateInitializer(entry, writtenValue != null ? writtenValue : createSyntheticValue(entry));
} }
} }
} }
@@ -1341,9 +1379,9 @@ public class JetControlFlowProcessor {
} }
if (resultingDescriptor instanceof VariableDescriptor) { if (resultingDescriptor instanceof VariableDescriptor) {
assert parameterValues.isEmpty() && receivers.size() <= 1 assert parameterValues.isEmpty()
: "Wrong input values for variable-based call (must be at most 1 receiver): " + resolvedCall.getCall().getCallElement().getText(); : "Variable-based call with non-empty argument list: " + resolvedCall.getCall().getCallElement().getText();
builder.readVariable(calleeExpression, (VariableDescriptor) resultingDescriptor, KotlinPackage.firstOrNull(receivers.keySet())); builder.readVariable(calleeExpression, resolvedCall, receivers);
} }
else { else {
builder.call(calleeExpression, resolvedCall, receivers, parameterValues); builder.call(calleeExpression, resolvedCall, receivers, parameterValues);
@@ -27,7 +27,6 @@ import org.jetbrains.jet.lang.cfg.pseudocode.instructions.jumps.*;
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.special.*; import org.jetbrains.jet.lang.cfg.pseudocode.instructions.special.*;
import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor; import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor; import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant; import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
@@ -306,8 +305,9 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
@NotNull JetElement assignment, @NotNull JetElement assignment,
@NotNull JetElement lValue, @NotNull JetElement lValue,
@NotNull PseudoValue rValue, @NotNull PseudoValue rValue,
@Nullable PseudoValue receiverValue) { @NotNull AccessTarget target,
add(new WriteValueInstruction(assignment, lValue, rValue, receiverValue, getCurrentScope())); @NotNull Map<PseudoValue, ReceiverValue> receiverValues) {
add(new WriteValueInstruction(assignment, getCurrentScope(), target, receiverValues, lValue, rValue));
} }
@Override @Override
@@ -402,25 +402,25 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
@NotNull @NotNull
@Override @Override
public PseudoValue loadConstant(@NotNull JetExpression expression, @Nullable CompileTimeConstant<?> constant) { public PseudoValue loadConstant(@NotNull JetExpression expression, @Nullable CompileTimeConstant<?> constant) {
return read(expression, null); return read(expression);
} }
@NotNull @NotNull
@Override @Override
public PseudoValue createAnonymousObject(@NotNull JetObjectLiteralExpression expression) { public PseudoValue createAnonymousObject(@NotNull JetObjectLiteralExpression expression) {
return read(expression, null); return read(expression);
} }
@NotNull @NotNull
@Override @Override
public PseudoValue createFunctionLiteral(@NotNull JetFunctionLiteralExpression expression) { public PseudoValue createFunctionLiteral(@NotNull JetFunctionLiteralExpression expression) {
return read(expression, null); return read(expression);
} }
@NotNull @NotNull
@Override @Override
public PseudoValue loadStringTemplate(@NotNull JetStringTemplateExpression expression, @NotNull List<PseudoValue> inputValues) { public PseudoValue loadStringTemplate(@NotNull JetStringTemplateExpression expression, @NotNull List<PseudoValue> inputValues) {
return inputValues.isEmpty() ? read(expression, null) : magic(expression, expression, inputValues, false); return inputValues.isEmpty() ? read(expression) : magic(expression, expression, inputValues, false);
} }
@NotNull @NotNull
@@ -448,17 +448,17 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
@NotNull @NotNull
@Override @Override
public PseudoValue readThis(@NotNull JetExpression expression, @Nullable ReceiverParameterDescriptor parameterDescriptor) { public PseudoValue readThis(@NotNull JetExpression expression, @Nullable ReceiverParameterDescriptor parameterDescriptor) {
return read(expression, null); return read(expression);
} }
@NotNull @NotNull
@Override @Override
public PseudoValue readVariable( public PseudoValue readVariable(
@NotNull JetExpression expression, @NotNull JetExpression expression,
@Nullable VariableDescriptor variableDescriptor, @NotNull ResolvedCall<?> resolvedCall,
@Nullable PseudoValue receiverValue @NotNull Map<PseudoValue, ReceiverValue> receiverValues
) { ) {
return read(expression, receiverValue); return read(expression, resolvedCall, receiverValues);
} }
@Nullable @Nullable
@@ -498,12 +498,21 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
} }
@NotNull @NotNull
private PseudoValue read(@NotNull JetExpression expression, @Nullable PseudoValue receiverValue) { private PseudoValue read(
@NotNull JetExpression expression,
@Nullable ResolvedCall<?> resolvedCall,
@NotNull Map<PseudoValue, ReceiverValue> receiverValues) {
AccessTarget accessTarget = resolvedCall != null ? new AccessTarget.Call(resolvedCall) : AccessTarget.BlackBox.instance$;
ReadValueInstruction instruction = ReadValueInstruction instruction =
ReadValueInstruction.object$.create(expression, getCurrentScope(), receiverValue, valueFactory); ReadValueInstruction.object$.create(expression, getCurrentScope(), accessTarget, receiverValues, valueFactory);
add(instruction); add(instruction);
return instruction.getOutputValue(); return instruction.getOutputValue();
} }
@NotNull
private PseudoValue read(@NotNull JetExpression expression) {
return read(expression, null, Collections.<PseudoValue, ReceiverValue>emptyMap());
}
} }
public static class TryFinallyBlockInfo extends BlockInfo { public static class TryFinallyBlockInfo extends BlockInfo {
@@ -22,10 +22,14 @@ import org.jetbrains.jet.lang.cfg.pseudocode.instructions.special.*
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.eval.* import org.jetbrains.jet.lang.cfg.pseudocode.instructions.eval.*
public open class InstructionVisitor() { public open class InstructionVisitor() {
public open fun visitReadValue(instruction: ReadValueInstruction) { public open fun visitAccessInstruction(instruction: AccessValueInstruction) {
visitInstructionWithNext(instruction) visitInstructionWithNext(instruction)
} }
public open fun visitReadValue(instruction: ReadValueInstruction) {
visitAccessInstruction(instruction)
}
public open fun visitLocalFunctionDeclarationInstruction(instruction: LocalFunctionDeclarationInstruction) { public open fun visitLocalFunctionDeclarationInstruction(instruction: LocalFunctionDeclarationInstruction) {
visitInstructionWithNext(instruction) visitInstructionWithNext(instruction)
} }
@@ -86,7 +90,7 @@ public open class InstructionVisitor() {
} }
public open fun visitWriteValue(instruction: WriteValueInstruction) { public open fun visitWriteValue(instruction: WriteValueInstruction) {
visitInstructionWithNext(instruction) visitAccessInstruction(instruction)
} }
public open fun visitLoadUnitValue(instruction: LoadUnitValueInstruction) { public open fun visitLoadUnitValue(instruction: LoadUnitValueInstruction) {
@@ -24,10 +24,14 @@ import org.jetbrains.jet.lang.cfg.pseudocode.instructions.special.*
public abstract class InstructionVisitorWithResult<R>() { public abstract class InstructionVisitorWithResult<R>() {
public abstract fun visitInstruction(instruction: Instruction): R public abstract fun visitInstruction(instruction: Instruction): R
public open fun visitReadValue(instruction: ReadValueInstruction): R { public open fun visitAccessInstruction(instruction: AccessValueInstruction): R {
return visitInstructionWithNext(instruction) return visitInstructionWithNext(instruction)
} }
public open fun visitReadValue(instruction: ReadValueInstruction): R {
return visitAccessInstruction(instruction)
}
public open fun visitLocalFunctionDeclarationInstruction(instruction: LocalFunctionDeclarationInstruction): R { public open fun visitLocalFunctionDeclarationInstruction(instruction: LocalFunctionDeclarationInstruction): R {
return visitInstructionWithNext(instruction) return visitInstructionWithNext(instruction)
} }
@@ -85,7 +89,7 @@ public abstract class InstructionVisitorWithResult<R>() {
} }
public open fun visitWriteValue(instruction: WriteValueInstruction): R { public open fun visitWriteValue(instruction: WriteValueInstruction): R {
return visitInstructionWithNext(instruction) return visitAccessInstruction(instruction)
} }
public open fun visitLoadUnitValue(instruction: LoadUnitValueInstruction): R { public open fun visitLoadUnitValue(instruction: LoadUnitValueInstruction): R {
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.cfg.pseudocode.instructions.eval
import org.jetbrains.jet.lang.psi.JetElement
import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.LexicalScope
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionWithNext
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall
import org.jetbrains.jet.lang.descriptors.VariableDescriptor
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.jet.lang.resolve.calls.tasks.ExplicitReceiverKind
import java.util.Collections
public trait AccessTarget {
public data class Declaration(val descriptor: VariableDescriptor): AccessTarget
public data class Call(val resolvedCall: ResolvedCall<*>): AccessTarget
public object BlackBox: AccessTarget
}
public abstract class AccessValueInstruction protected (
element: JetElement,
lexicalScope: LexicalScope,
public val target: AccessTarget,
public override val receiverValues: Map<PseudoValue, ReceiverValue>
) : InstructionWithNext(element, lexicalScope), InstructionWithReceivers
@@ -18,7 +18,8 @@ package org.jetbrains.jet.lang.cfg.pseudocode.instructions.eval
import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.Instruction import org.jetbrains.jet.lang.cfg.pseudocode.instructions.Instruction
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue
public trait InstructionWithReceivers: Instruction { public trait InstructionWithReceivers: Instruction {
public val receiverValues: List<PseudoValue> public val receiverValues: Map<PseudoValue, ReceiverValue>
} }
@@ -23,25 +23,21 @@ import org.jetbrains.jet.lang.cfg.pseudocode.instructions.LexicalScope
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionVisitor import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionVisitor
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionVisitorWithResult import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionVisitorWithResult
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionImpl import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionImpl
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionWithNext
import com.intellij.util.containers.ContainerUtil
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue
public class ReadValueInstruction private ( public class ReadValueInstruction private (
element: JetElement, element: JetElement,
lexicalScope: LexicalScope, lexicalScope: LexicalScope,
public val receiverValue: PseudoValue?, target: AccessTarget,
receiverValues: Map<PseudoValue, ReceiverValue>,
private var _outputValue: PseudoValue? private var _outputValue: PseudoValue?
) : InstructionWithNext(element, lexicalScope), InstructionWithReceivers, InstructionWithValue { ) : AccessValueInstruction(element, lexicalScope, target, receiverValues), InstructionWithValue {
private fun newResultValue(factory: PseudoValueFactory) { private fun newResultValue(factory: PseudoValueFactory) {
_outputValue = factory.newValue(element, this) _outputValue = factory.newValue(element, this)
} }
override val receiverValues: List<PseudoValue>
get() = ContainerUtil.createMaybeSingletonList(receiverValue)
override val inputValues: List<PseudoValue> override val inputValues: List<PseudoValue>
get() = receiverValues get() = receiverValues.keySet().toList()
override val outputValue: PseudoValue override val outputValue: PseudoValue
get() = _outputValue!! get() = _outputValue!!
@@ -55,21 +51,22 @@ public class ReadValueInstruction private (
} }
override fun toString(): String { override fun toString(): String {
val inVal = if (receiverValue != null) "|$receiverValue" else "" val inVal = if (receiverValues.empty) "" else "|${receiverValues.keySet().makeString()}"
return "r(${render(element)}$inVal) -> $outputValue" return "r(${render(element)}$inVal) -> $outputValue"
} }
override fun createCopy(): InstructionImpl = override fun createCopy(): InstructionImpl =
ReadValueInstruction(element, lexicalScope, receiverValue, outputValue) ReadValueInstruction(element, lexicalScope, target, receiverValues, outputValue)
class object { class object {
public fun create ( public fun create (
element: JetElement, element: JetElement,
lexicalScope: LexicalScope, lexicalScope: LexicalScope,
receiverValue: PseudoValue?, target: AccessTarget,
receiverValues: Map<PseudoValue, ReceiverValue>,
factory: PseudoValueFactory factory: PseudoValueFactory
): ReadValueInstruction { ): ReadValueInstruction {
return ReadValueInstruction(element, lexicalScope, receiverValue, null).let { instruction -> return ReadValueInstruction(element, lexicalScope, target, receiverValues, null).let { instruction ->
instruction.newResultValue(factory) instruction.newResultValue(factory)
instruction instruction
} }
@@ -22,24 +22,20 @@ import java.util.Collections
import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.LexicalScope import org.jetbrains.jet.lang.cfg.pseudocode.instructions.LexicalScope
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionVisitor import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionVisitor
import java.util.Arrays
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionVisitorWithResult import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionVisitorWithResult
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionImpl import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionImpl
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionWithNext import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue
import com.intellij.util.containers.ContainerUtil
public class WriteValueInstruction( public class WriteValueInstruction(
assignment: JetElement, assignment: JetElement,
lexicalScope: LexicalScope,
target: AccessTarget,
receiverValues: Map<PseudoValue, ReceiverValue>,
public val lValue: JetElement, public val lValue: JetElement,
public val rValue: PseudoValue, public val rValue: PseudoValue
public val receiverValue: PseudoValue?, ) : AccessValueInstruction(assignment, lexicalScope, target, receiverValues) {
lexicalScope: LexicalScope
) : InstructionWithNext(assignment, lexicalScope), InstructionWithReceivers {
override val receiverValues: List<PseudoValue>
get() = ContainerUtil.createMaybeSingletonList(receiverValue)
override val inputValues: List<PseudoValue> override val inputValues: List<PseudoValue>
get() = receiverValue?.let{ receiverValue -> Arrays.asList(receiverValue, rValue) } ?: Collections.singletonList(rValue) get() = receiverValues.keySet() + rValue
override fun accept(visitor: InstructionVisitor) { override fun accept(visitor: InstructionVisitor) {
visitor.visitWriteValue(this) visitor.visitWriteValue(this)
@@ -55,5 +51,5 @@ public class WriteValueInstruction(
} }
override fun createCopy(): InstructionImpl = override fun createCopy(): InstructionImpl =
WriteValueInstruction(element, lValue, rValue, receiverValue, lexicalScope) WriteValueInstruction(element, lexicalScope, target, receiverValues, lValue, rValue)
} }
@@ -61,12 +61,9 @@ public class CallInstruction private(
element: JetElement, element: JetElement,
lexicalScope: LexicalScope, lexicalScope: LexicalScope,
val resolvedCall: ResolvedCall<*>, val resolvedCall: ResolvedCall<*>,
public val receiverValueMap: Map<PseudoValue, ReceiverValue>, public override val receiverValues: Map<PseudoValue, ReceiverValue>,
public val arguments: Map<PseudoValue, ValueParameterDescriptor> public val arguments: Map<PseudoValue, ValueParameterDescriptor>
) : OperationInstruction(element, lexicalScope, receiverValueMap.keySet() + arguments.keySet()), InstructionWithReceivers { ) : OperationInstruction(element, lexicalScope, receiverValues.keySet() + arguments.keySet()), InstructionWithReceivers {
override val receiverValues: List<PseudoValue>
get() = receiverValueMap.keySet().toList()
override fun accept(visitor: InstructionVisitor) { override fun accept(visitor: InstructionVisitor) {
visitor.visitCallInstruction(this) visitor.visitCallInstruction(this)
} }
@@ -76,7 +73,7 @@ public class CallInstruction private(
} }
override fun createCopy() = override fun createCopy() =
CallInstruction(element, lexicalScope, resolvedCall, receiverValueMap, arguments).setResult(resultValue) CallInstruction(element, lexicalScope, resolvedCall, receiverValues, arguments).setResult(resultValue)
override fun toString() = override fun toString() =
renderInstruction("call", "${render(element)}, ${resolvedCall.getResultingDescriptor()!!.getName()}") renderInstruction("call", "${render(element)}, ${resolvedCall.getResultingDescriptor()!!.getName()}")