Pseudocode: Generate synthetic VALUE_CONSUMER for both property and class delegates
This commit is contained in:
@@ -22,6 +22,7 @@ import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.SmartFMap;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import jet.runtime.typeinfo.JetValueParameter;
|
||||
import kotlin.Function0;
|
||||
import kotlin.Function1;
|
||||
import kotlin.KotlinPackage;
|
||||
@@ -548,7 +549,7 @@ public class JetControlFlowProcessor {
|
||||
@NotNull Map<PseudoValue, ReceiverValue> receiverValues,
|
||||
@NotNull JetExpression parentExpression
|
||||
) {
|
||||
if (target == AccessTarget.BlackBox.instance$) {
|
||||
if (target == AccessTarget.BlackBox.INSTANCE$) {
|
||||
List<PseudoValue> values = ContainerUtil.createMaybeSingletonList(rightValue);
|
||||
builder.magic(parentExpression, parentExpression, values, defaultTypeMap(values), MagicKind.UNSUPPORTED_ELEMENT);
|
||||
}
|
||||
@@ -893,14 +894,8 @@ public class JetControlFlowProcessor {
|
||||
JetMultiDeclaration multiDeclaration = expression.getMultiParameter();
|
||||
JetExpression loopRange = expression.getLoopRange();
|
||||
|
||||
TypePredicate loopRangeTypePredicate = AllTypes.INSTANCE$;
|
||||
ResolvedCall<?> iteratorResolvedCall = trace.get(BindingContext.LOOP_RANGE_ITERATOR_RESOLVED_CALL, loopRange);
|
||||
if (iteratorResolvedCall != null) {
|
||||
ReceiverValue iteratorReceiver = getExplicitReceiverValue(iteratorResolvedCall);
|
||||
if (iteratorReceiver.exists()) {
|
||||
loopRangeTypePredicate = PseudocodePackage.getReceiverTypePredicate(iteratorResolvedCall, iteratorReceiver);
|
||||
}
|
||||
}
|
||||
TypePredicate loopRangeTypePredicate =
|
||||
getTypePredicateByReceiverValue(trace.get(BindingContext.LOOP_RANGE_ITERATOR_RESOLVED_CALL, loopRange));
|
||||
|
||||
PseudoValue loopRangeValue = builder.getBoundValue(loopRange);
|
||||
PseudoValue value = builder.magic(
|
||||
@@ -1154,7 +1149,9 @@ public class JetControlFlowProcessor {
|
||||
JetExpression delegate = property.getDelegateExpression();
|
||||
if (delegate != null) {
|
||||
generateInstructions(delegate);
|
||||
generateDelegateConsumer(property, delegate);
|
||||
}
|
||||
|
||||
if (JetPsiUtil.isLocal(property)) {
|
||||
for (JetPropertyAccessor accessor : property.getAccessors()) {
|
||||
generateInstructions(accessor);
|
||||
@@ -1162,6 +1159,36 @@ public class JetControlFlowProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
private void generateDelegateConsumer(@NotNull JetProperty property, @NotNull JetExpression delegate) {
|
||||
DeclarationDescriptor descriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, property);
|
||||
if (!(descriptor instanceof PropertyDescriptor)) return;
|
||||
|
||||
List<PseudoValue> values = Collections.singletonList(builder.getBoundValue(delegate));
|
||||
List<TypePredicate> typePredicates = KotlinPackage.map(
|
||||
((PropertyDescriptor) descriptor).getAccessors(),
|
||||
new Function1<PropertyAccessorDescriptor, TypePredicate>() {
|
||||
@Override
|
||||
public TypePredicate invoke(PropertyAccessorDescriptor descriptor) {
|
||||
return getTypePredicateByReceiverValue(trace.get(BindingContext.DELEGATED_PROPERTY_RESOLVED_CALL, descriptor));
|
||||
}
|
||||
}
|
||||
);
|
||||
Map<PseudoValue, TypePredicate> valuesToTypePredicates =
|
||||
PseudocodePackage.expectedTypeFor(PseudocodePackage.and(typePredicates), values);
|
||||
builder.magic(property, null, values, valuesToTypePredicates, MagicKind.VALUE_CONSUMER);
|
||||
}
|
||||
|
||||
private TypePredicate getTypePredicateByReceiverValue(@Nullable ResolvedCall<?> resolvedCall) {
|
||||
if (resolvedCall == null) return AllTypes.INSTANCE$;
|
||||
|
||||
ReceiverValue receiverValue = getExplicitReceiverValue(resolvedCall);
|
||||
if (receiverValue.exists()) {
|
||||
return PseudocodePackage.getReceiverTypePredicate(resolvedCall, receiverValue);
|
||||
}
|
||||
|
||||
return AllTypes.INSTANCE$;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitMultiDeclaration(@NotNull JetMultiDeclaration declaration) {
|
||||
visitMultiDeclaration(declaration, true);
|
||||
@@ -1399,12 +1426,9 @@ public class JetControlFlowProcessor {
|
||||
generateInstructions(specifier.getDelegateExpression());
|
||||
|
||||
List<PseudoValue> arguments = ContainerUtil.createMaybeSingletonList(builder.getBoundValue(specifier.getDelegateExpression()));
|
||||
TypePredicate expectedTypePredicate =
|
||||
PseudocodePackage.getSubtypesPredicate(trace.get(BindingContext.TYPE, specifier.getTypeReference()));
|
||||
if (expectedTypePredicate == null) {
|
||||
expectedTypePredicate = AllTypes.INSTANCE$;
|
||||
}
|
||||
builder.magic(specifier, specifier, arguments, PseudocodePackage.expectedTypeFor(expectedTypePredicate, arguments), MagicKind.VALUE_CONSUMER);
|
||||
JetType jetType = trace.get(BindingContext.TYPE, specifier.getTypeReference());
|
||||
TypePredicate expectedTypePredicate = jetType != null ? PseudocodePackage.getSubtypesPredicate(jetType) : AllTypes.INSTANCE$;
|
||||
builder.magic(specifier, null, arguments, PseudocodePackage.expectedTypeFor(expectedTypePredicate, arguments), MagicKind.VALUE_CONSUMER);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -72,7 +72,7 @@ fun or(predicates: Collection<TypePredicate>): TypePredicate? =
|
||||
else -> ForSomeType(predicates.toList())
|
||||
}
|
||||
|
||||
fun JetType.getSubtypesPredicate(): TypePredicate? {
|
||||
fun JetType.getSubtypesPredicate(): TypePredicate {
|
||||
return when {
|
||||
KotlinBuiltIns.getInstance().isAnyOrNullableAny(this) && isNullable() -> AllTypes
|
||||
TypeUtils.canHaveSubtypes(JetTypeChecker.DEFAULT, this) -> AllSubtypes(this)
|
||||
|
||||
+10
-10
@@ -11,14 +11,14 @@ class A(a: Int, b: Int): T
|
||||
== B ==
|
||||
class B(a: Int, b: Int): T by A(a + b, a - b)
|
||||
---------------------
|
||||
<v0>: Int NEW: magic[FAKE_INITIALIZER](a: Int) -> <v0>
|
||||
<v1>: Int NEW: magic[FAKE_INITIALIZER](b: Int) -> <v1>
|
||||
a <v2>: Int NEW: r(a) -> <v2>
|
||||
b <v3>: Int NEW: r(b) -> <v3>
|
||||
a + b <v4>: Int NEW: call(a + b, plus|<v2>, <v3>) -> <v4>
|
||||
a <v5>: Int NEW: r(a) -> <v5>
|
||||
b <v6>: Int NEW: r(b) -> <v6>
|
||||
a - b <v7>: Int NEW: call(a - b, minus|<v5>, <v6>) -> <v7>
|
||||
A(a + b, a - b) <v8>: {<: T} NEW: call(A(a + b, a - b), <init>|<v4>, <v7>) -> <v8>
|
||||
T by A(a + b, a - b) <v9>: * NEW: magic[VALUE_CONSUMER](T by A(a + b, a - b)|<v8>) -> <v9>
|
||||
<v0>: Int NEW: magic[FAKE_INITIALIZER](a: Int) -> <v0>
|
||||
<v1>: Int NEW: magic[FAKE_INITIALIZER](b: Int) -> <v1>
|
||||
<v9>: * NEW: magic[VALUE_CONSUMER](T by A(a + b, a - b)|<v8>) -> <v9>
|
||||
a <v2>: Int NEW: r(a) -> <v2>
|
||||
b <v3>: Int NEW: r(b) -> <v3>
|
||||
a + b <v4>: Int NEW: call(a + b, plus|<v2>, <v3>) -> <v4>
|
||||
a <v5>: Int NEW: r(a) -> <v5>
|
||||
b <v6>: Int NEW: r(b) -> <v6>
|
||||
a - b <v7>: Int NEW: call(a - b, minus|<v5>, <v6>) -> <v7>
|
||||
A(a + b, a - b) <v8>: {<: T} NEW: call(A(a + b, a - b), <init>|<v4>, <v7>) -> <v8>
|
||||
=====================
|
||||
|
||||
@@ -26,9 +26,9 @@ fun foo(b: B) : Int {
|
||||
}
|
||||
---------------------
|
||||
<v0>: B NEW: magic[FAKE_INITIALIZER](b: B) -> <v0>
|
||||
b <v1>: {<: A} NEW: r(b) -> <v1>
|
||||
A by b <v2>: * NEW: magic[VALUE_CONSUMER](A by b|<v1>) -> <v2>
|
||||
object : A by b {} <v3>: <no name provided> NEW: r(object : A by b {}) -> <v3>
|
||||
<v2>: * NEW: magic[VALUE_CONSUMER](A by b|<v1>) -> <v2>
|
||||
b <v1>: {<: A} NEW: r(b) -> <v1>
|
||||
object : A by b {} <v3>: <no name provided> NEW: r(object : A by b {}) -> <v3>
|
||||
o <v4>: {<: A} NEW: r(o) -> <v4>
|
||||
foo() <v5>: Int NEW: call(foo(), foo|<v4>) -> <v5>
|
||||
o.foo() <v5>: Int COPY
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
== Delegate ==
|
||||
class Delegate {
|
||||
fun get(_this: Any, p: PropertyMetadata): Int = 0
|
||||
fun get(_this: Nothing?, p: PropertyMetadata): Int = 0
|
||||
}
|
||||
---------------------
|
||||
L0:
|
||||
@@ -13,12 +13,12 @@ sink:
|
||||
<SINK> PREV:[<ERROR>, <END>]
|
||||
=====================
|
||||
== get ==
|
||||
fun get(_this: Any, p: PropertyMetadata): Int = 0
|
||||
fun get(_this: Nothing?, p: PropertyMetadata): Int = 0
|
||||
---------------------
|
||||
L0:
|
||||
1 <START>
|
||||
v(_this: Any)
|
||||
magic[FAKE_INITIALIZER](_this: Any) -> <v0>
|
||||
v(_this: Nothing?)
|
||||
magic[FAKE_INITIALIZER](_this: Nothing?) -> <v0>
|
||||
w(_this|<v0>)
|
||||
v(p: PropertyMetadata)
|
||||
magic[FAKE_INITIALIZER](p: PropertyMetadata) -> <v1>
|
||||
@@ -55,10 +55,11 @@ L0:
|
||||
1 <START>
|
||||
v(val b by a)
|
||||
r(a) -> <v0>
|
||||
magic[VALUE_CONSUMER](val b by a|<v0>) -> <v1>
|
||||
L1:
|
||||
<END> NEXT:[<SINK>]
|
||||
<END> NEXT:[<SINK>]
|
||||
error:
|
||||
<ERROR> PREV:[]
|
||||
<ERROR> PREV:[]
|
||||
sink:
|
||||
<SINK> PREV:[<ERROR>, <END>]
|
||||
<SINK> PREV:[<ERROR>, <END>]
|
||||
=====================
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class Delegate {
|
||||
fun get(_this: Any, p: PropertyMetadata): Int = 0
|
||||
fun get(_this: Nothing?, p: PropertyMetadata): Int = 0
|
||||
}
|
||||
|
||||
val a = Delegate()
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
== Delegate ==
|
||||
class Delegate {
|
||||
fun get(_this: Any, p: PropertyMetadata): Int = 0
|
||||
fun get(_this: Nothing?, p: PropertyMetadata): Int = 0
|
||||
}
|
||||
---------------------
|
||||
=====================
|
||||
== get ==
|
||||
fun get(_this: Any, p: PropertyMetadata): Int = 0
|
||||
fun get(_this: Nothing?, p: PropertyMetadata): Int = 0
|
||||
---------------------
|
||||
<v0>: {<: Any} NEW: magic[FAKE_INITIALIZER](_this: Any) -> <v0>
|
||||
<v0>: {<: Nothing?} NEW: magic[FAKE_INITIALIZER](_this: Nothing?) -> <v0>
|
||||
<v1>: {<: PropertyMetadata} NEW: magic[FAKE_INITIALIZER](p: PropertyMetadata) -> <v1>
|
||||
0 <v2>: Int NEW: r(0) -> <v2>
|
||||
=====================
|
||||
@@ -19,5 +19,6 @@ Delegate() <v0>: Delegate NEW: call(Delegate(), <init>) -> <v0>
|
||||
== b ==
|
||||
val b by a
|
||||
---------------------
|
||||
a <v0>: * NEW: r(a) -> <v0>
|
||||
<v1>: * NEW: magic[VALUE_CONSUMER](val b by a|<v0>) -> <v1>
|
||||
a <v0>: Delegate NEW: r(a) -> <v0>
|
||||
=====================
|
||||
|
||||
Reference in New Issue
Block a user