Remove some unreachable/duplicated code in StackValue

This commit is contained in:
Alexander Udalov
2015-06-15 12:43:50 +03:00
parent 4dcc373a5a
commit 7c846388bd
2 changed files with 38 additions and 123 deletions
@@ -93,9 +93,7 @@ import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.*;
import static org.jetbrains.kotlin.load.java.JvmAnnotationNames.KotlinSyntheticClass; import static org.jetbrains.kotlin.load.java.JvmAnnotationNames.KotlinSyntheticClass;
import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory; import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
import static org.jetbrains.kotlin.resolve.BindingContext.*; import static org.jetbrains.kotlin.resolve.BindingContext.*;
import static org.jetbrains.kotlin.resolve.BindingContextUtils.getDelegationConstructorCall; import static org.jetbrains.kotlin.resolve.BindingContextUtils.*;
import static org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull;
import static org.jetbrains.kotlin.resolve.BindingContextUtils.isVarCapturedInClosure;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.*; import static org.jetbrains.kotlin.resolve.DescriptorUtils.*;
import static org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilPackage.getResolvedCall; import static org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilPackage.getResolvedCall;
import static org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilPackage.getResolvedCallWithAssert; import static org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilPackage.getResolvedCallWithAssert;
@@ -3182,8 +3180,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
int increment = operationName.equals("inc") ? 1 : -1; int increment = operationName.equals("inc") ? 1 : -1;
Type type = expressionType(expression.getBaseExpression()); Type type = expressionType(expression.getBaseExpression());
StackValue value = gen(expression.getBaseExpression()); StackValue value = gen(expression.getBaseExpression());
Callable callable = resolveToCallable((FunctionDescriptor) op, false, resolvedCall); return StackValue.preIncrement(type, value, increment, resolvedCall, this);
return StackValue.preIncrement(type, value, increment, callable, resolvedCall, this);
} }
@Override @Override
@@ -3458,41 +3455,35 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
boolean isGetter = "get".equals(operationDescriptor.getName().asString()); boolean isGetter = "get".equals(operationDescriptor.getName().asString());
Callable callable = resolveToCallable(operationDescriptor, false, isGetter ? resolvedGetCall : resolvedSetCall); Callable callable = resolveToCallable(operationDescriptor, false, isGetter ? resolvedGetCall : resolvedSetCall);
Callable callableMethod = resolveToCallableMethod(operationDescriptor, false, context); Callable callableMethod = resolveToCallableMethod(operationDescriptor, false, context);
Type[] argumentTypes = callableMethod.getParameterTypes(); Type[] argumentTypes = callableMethod.getParameterTypes();
StackValue collectionElementReceiver = StackValue collectionElementReceiver = createCollectionElementReceiver(
createCollectionElementReceiver(expression, receiver, array, arrayType, operationDescriptor, isGetter, resolvedGetCall, resolvedSetCall, expression, receiver, operationDescriptor, isGetter, resolvedGetCall, resolvedSetCall, callable
callable, );
argumentTypes);
Type elementType = isGetter ? callableMethod.getReturnType() : ArrayUtil.getLastElement(argumentTypes); Type elementType = isGetter ? callableMethod.getReturnType() : ArrayUtil.getLastElement(argumentTypes);
return StackValue.collectionElement(collectionElementReceiver, elementType, resolvedGetCall, resolvedSetCall, this, state); return StackValue.collectionElement(collectionElementReceiver, elementType, resolvedGetCall, resolvedSetCall, this);
} }
} }
@NotNull
private StackValue createCollectionElementReceiver( private StackValue createCollectionElementReceiver(
@NotNull JetArrayAccessExpression expression, @NotNull JetArrayAccessExpression expression,
@NotNull StackValue receiver, @NotNull StackValue receiver,
@NotNull JetExpression array,
@NotNull Type arrayType,
@NotNull FunctionDescriptor operationDescriptor, @NotNull FunctionDescriptor operationDescriptor,
boolean isGetter, boolean isGetter,
ResolvedCall<FunctionDescriptor> resolvedGetCall, ResolvedCall<FunctionDescriptor> resolvedGetCall,
ResolvedCall<FunctionDescriptor> resolvedSetCall, ResolvedCall<FunctionDescriptor> resolvedSetCall,
@NotNull Callable callable, @NotNull Callable callable
@NotNull Type[] argumentTypes
) { ) {
ResolvedCall<FunctionDescriptor> resolvedCall = isGetter ? resolvedGetCall : resolvedSetCall; ResolvedCall<FunctionDescriptor> resolvedCall = isGetter ? resolvedGetCall : resolvedSetCall;
assert resolvedCall != null : "couldn't find resolved call: " + expression.getText(); assert resolvedCall != null : "couldn't find resolved call: " + expression.getText();
ArgumentGenerator argumentGenerator = ArgumentGenerator argumentGenerator = new CallBasedArgumentGenerator(
new CallBasedArgumentGenerator(this, defaultCallGenerator, this, defaultCallGenerator, resolvedCall.getResultingDescriptor().getValueParameters(), callable.getValueParameterTypes()
resolvedCall.getResultingDescriptor().getValueParameters(), );
callable.getValueParameterTypes());
List<ResolvedValueArgument> valueArguments = resolvedCall.getValueArgumentsByIndex(); List<ResolvedValueArgument> valueArguments = resolvedCall.getValueArgumentsByIndex();
assert valueArguments != null : "Failed to arrange value arguments by index: " + operationDescriptor; assert valueArguments != null : "Failed to arrange value arguments by index: " + operationDescriptor;
@@ -3503,9 +3494,9 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
valueArguments.remove(valueArguments.size() - 1); valueArguments.remove(valueArguments.size() - 1);
} }
return new StackValue.CollectionElementReceiver(callable, receiver, resolvedGetCall, resolvedSetCall, isGetter, this, return new StackValue.CollectionElementReceiver(
argumentGenerator, valueArguments, array, arrayType, expression, argumentTypes); callable, receiver, resolvedGetCall, resolvedSetCall, isGetter, this, argumentGenerator, valueArguments
);
} }
@Override @Override
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.codegen.state.JetTypeMapper; import org.jetbrains.kotlin.codegen.state.JetTypeMapper;
import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.load.java.JvmAbi; import org.jetbrains.kotlin.load.java.JvmAbi;
import org.jetbrains.kotlin.psi.JetArrayAccessExpression;
import org.jetbrains.kotlin.psi.JetExpression; import org.jetbrains.kotlin.psi.JetExpression;
import org.jetbrains.kotlin.resolve.annotations.AnnotationsPackage; import org.jetbrains.kotlin.resolve.annotations.AnnotationsPackage;
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
@@ -198,10 +197,9 @@ public abstract class StackValue {
Type type, Type type,
ResolvedCall<FunctionDescriptor> getter, ResolvedCall<FunctionDescriptor> getter,
ResolvedCall<FunctionDescriptor> setter, ResolvedCall<FunctionDescriptor> setter,
ExpressionCodegen codegen, ExpressionCodegen codegen
GenerationState state
) { ) {
return new CollectionElement(collectionElementReceiver, type, getter, setter, codegen, state); return new CollectionElement(collectionElementReceiver, type, getter, setter, codegen);
} }
@NotNull @NotNull
@@ -459,14 +457,13 @@ public abstract class StackValue {
@NotNull Type type, @NotNull Type type,
@NotNull StackValue stackValue, @NotNull StackValue stackValue,
int delta, int delta,
@NotNull Callable method,
ResolvedCall resolvedCall, ResolvedCall resolvedCall,
@NotNull ExpressionCodegen codegen @NotNull ExpressionCodegen codegen
) { ) {
if (stackValue instanceof StackValue.Local && Type.INT_TYPE == stackValue.type) { if (stackValue instanceof StackValue.Local && Type.INT_TYPE == stackValue.type) {
return preIncrementForLocalVar(((StackValue.Local) stackValue).index, delta); return preIncrementForLocalVar(((StackValue.Local) stackValue).index, delta);
} }
return new PrefixIncrement(type, stackValue, delta, method, resolvedCall, codegen); return new PrefixIncrement(type, stackValue, resolvedCall, codegen);
} }
public static StackValue receiver( public static StackValue receiver(
@@ -712,10 +709,6 @@ public abstract class StackValue {
private final Callable callable; private final Callable callable;
private final boolean isGetter; private final boolean isGetter;
private final ExpressionCodegen codegen; private final ExpressionCodegen codegen;
private final JetExpression array;
private final Type arrayType;
private final JetArrayAccessExpression expression;
private final Type[] argumentTypes;
private final ArgumentGenerator argumentGenerator; private final ArgumentGenerator argumentGenerator;
private final List<ResolvedValueArgument> valueArguments; private final List<ResolvedValueArgument> valueArguments;
private final FrameMap frame; private final FrameMap frame;
@@ -731,11 +724,7 @@ public abstract class StackValue {
boolean isGetter, boolean isGetter,
@NotNull ExpressionCodegen codegen, @NotNull ExpressionCodegen codegen,
ArgumentGenerator argumentGenerator, ArgumentGenerator argumentGenerator,
List<ResolvedValueArgument> valueArguments, List<ResolvedValueArgument> valueArguments
JetExpression array,
Type arrayType,
JetArrayAccessExpression expression,
Type[] argumentTypes
) { ) {
super(OBJECT_TYPE); super(OBJECT_TYPE);
this.callable = callable; this.callable = callable;
@@ -747,33 +736,15 @@ public abstract class StackValue {
this.argumentGenerator = argumentGenerator; this.argumentGenerator = argumentGenerator;
this.valueArguments = valueArguments; this.valueArguments = valueArguments;
this.codegen = codegen; this.codegen = codegen;
this.array = array;
this.arrayType = arrayType;
this.expression = expression;
this.argumentTypes = argumentTypes;
this.frame = codegen.myFrameMap; this.frame = codegen.myFrameMap;
} }
@Override @Override
public void putSelector( public void putSelector(@NotNull Type type, @NotNull InstructionAdapter v) {
@NotNull Type type, @NotNull InstructionAdapter v
) {
ResolvedCall<?> call = isGetter ? resolvedGetCall : resolvedSetCall; ResolvedCall<?> call = isGetter ? resolvedGetCall : resolvedSetCall;
if (callable instanceof Callable) { StackValue newReceiver = StackValue.receiver(call, receiver, codegen, callable);
StackValue newReceiver = StackValue.receiver(call, receiver, codegen, (Callable) callable); newReceiver.put(newReceiver.type, v);
newReceiver.put(newReceiver.type, v); argumentGenerator.generate(valueArguments);
argumentGenerator.generate(valueArguments);
}
else {
codegen.gen(array, arrayType); // intrinsic method
int index = call.getExtensionReceiver().exists() ? 1 : 0;
for (JetExpression jetExpression : expression.getIndexExpressions()) {
codegen.gen(jetExpression, argumentTypes[index]);
index++;
}
}
} }
@Override @Override
@@ -782,7 +753,8 @@ public abstract class StackValue {
} }
public void dupReceiver(@NotNull InstructionAdapter v) { public void dupReceiver(@NotNull InstructionAdapter v) {
if (isStandardStack(resolvedGetCall, 1) && isStandardStack(resolvedSetCall, 2)) { if (CollectionElement.isStandardStack(codegen.typeMapper, resolvedGetCall, 1) &&
CollectionElement.isStandardStack(codegen.typeMapper, resolvedSetCall, 2)) {
v.dup2(); // collection and index v.dup2(); // collection and index
return; return;
} }
@@ -869,67 +841,31 @@ public abstract class StackValue {
mark.dropTo(); mark.dropTo();
} }
private boolean isStandardStack(ResolvedCall<?> call, int valueParamsSize) {
if (call == null) {
return true;
}
List<ValueParameterDescriptor> valueParameters = call.getResultingDescriptor().getValueParameters();
if (valueParameters.size() != valueParamsSize) {
return false;
}
for (ValueParameterDescriptor valueParameter : valueParameters) {
if (codegen.typeMapper.mapType(valueParameter.getType()).getSize() != 1) {
return false;
}
}
if (call.getDispatchReceiver().exists()) {
if (call.getExtensionReceiver().exists()) {
return false;
}
}
else {
if (codegen.typeMapper.mapType(call.getResultingDescriptor().getExtensionReceiverParameter().getType())
.getSize() != 1) {
return false;
}
}
return true;
}
} }
public static class CollectionElement extends StackValueWithSimpleReceiver { public static class CollectionElement extends StackValueWithSimpleReceiver {
private final Callable getter; private final Callable getter;
private final Callable setter; private final Callable setter;
private final ExpressionCodegen codegen; private final ExpressionCodegen codegen;
private final GenerationState state;
private final ResolvedCall<FunctionDescriptor> resolvedGetCall; private final ResolvedCall<FunctionDescriptor> resolvedGetCall;
private final ResolvedCall<FunctionDescriptor> resolvedSetCall; private final ResolvedCall<FunctionDescriptor> resolvedSetCall;
private final FunctionDescriptor setterDescriptor; private final FunctionDescriptor setterDescriptor;
private final FunctionDescriptor getterDescriptor; private final FunctionDescriptor getterDescriptor;
public CollectionElement( public CollectionElement(
StackValue collectionElementReceiver, @NotNull StackValue collectionElementReceiver,
Type type, @NotNull Type type,
ResolvedCall<FunctionDescriptor> resolvedGetCall, @Nullable ResolvedCall<FunctionDescriptor> resolvedGetCall,
ResolvedCall<FunctionDescriptor> resolvedSetCall, @Nullable ResolvedCall<FunctionDescriptor> resolvedSetCall,
ExpressionCodegen codegen, @NotNull ExpressionCodegen codegen
GenerationState state
) { ) {
super(type, false, false, collectionElementReceiver, true); super(type, false, false, collectionElementReceiver, true);
this.resolvedGetCall = resolvedGetCall; this.resolvedGetCall = resolvedGetCall;
this.resolvedSetCall = resolvedSetCall; this.resolvedSetCall = resolvedSetCall;
this.state = state;
this.setterDescriptor = resolvedSetCall == null ? null : resolvedSetCall.getResultingDescriptor(); this.setterDescriptor = resolvedSetCall == null ? null : resolvedSetCall.getResultingDescriptor();
this.getterDescriptor = resolvedGetCall == null ? null : resolvedGetCall.getResultingDescriptor(); this.getterDescriptor = resolvedGetCall == null ? null : resolvedGetCall.getResultingDescriptor();
this.setter = this.setter = resolvedSetCall == null ? null : codegen.resolveToCallable(setterDescriptor, false, resolvedSetCall);
resolvedSetCall == null ? null : (Callable) codegen.resolveToCallable(setterDescriptor, false, resolvedSetCall); this.getter = resolvedGetCall == null ? null : codegen.resolveToCallable(getterDescriptor, false, resolvedGetCall);
this.getter =
resolvedGetCall == null ? null : (Callable) codegen.resolveToCallable(getterDescriptor, false, resolvedGetCall);
this.codegen = codegen; this.codegen = codegen;
} }
@@ -945,7 +881,7 @@ public abstract class StackValue {
@Override @Override
public int receiverSize() { public int receiverSize() {
if (isStandardStack(resolvedGetCall, 1) && isStandardStack(resolvedSetCall, 2)) { if (isStandardStack(codegen.typeMapper, resolvedGetCall, 1) && isStandardStack(codegen.typeMapper, resolvedSetCall, 2)) {
return 2; return 2;
} }
else { else {
@@ -953,7 +889,7 @@ public abstract class StackValue {
} }
} }
private boolean isStandardStack(ResolvedCall<?> call, int valueParamsSize) { public static boolean isStandardStack(@NotNull JetTypeMapper typeMapper, @Nullable ResolvedCall<?> call, int valueParamsSize) {
if (call == null) { if (call == null) {
return true; return true;
} }
@@ -964,7 +900,7 @@ public abstract class StackValue {
} }
for (ValueParameterDescriptor valueParameter : valueParameters) { for (ValueParameterDescriptor valueParameter : valueParameters) {
if (codegen.typeMapper.mapType(valueParameter.getType()).getSize() != 1) { if (typeMapper.mapType(valueParameter.getType()).getSize() != 1) {
return false; return false;
} }
} }
@@ -975,8 +911,8 @@ public abstract class StackValue {
} }
} }
else { else {
if (codegen.typeMapper.mapType(call.getResultingDescriptor().getExtensionReceiverParameter().getType()) //noinspection ConstantConditions
.getSize() != 1) { if (typeMapper.mapType(call.getResultingDescriptor().getExtensionReceiverParameter().getType()).getSize() != 1) {
return false; return false;
} }
} }
@@ -1264,8 +1200,6 @@ public abstract class StackValue {
} }
private static class PrefixIncrement extends StackValue { private static class PrefixIncrement extends StackValue {
private final int delta;
private final Callable callable;
private final ResolvedCall resolvedCall; private final ResolvedCall resolvedCall;
private final ExpressionCodegen codegen; private final ExpressionCodegen codegen;
private StackValue value; private StackValue value;
@@ -1273,15 +1207,11 @@ public abstract class StackValue {
public PrefixIncrement( public PrefixIncrement(
@NotNull Type type, @NotNull Type type,
@NotNull StackValue value, @NotNull StackValue value,
int delta,
@NotNull Callable callable,
ResolvedCall resolvedCall, ResolvedCall resolvedCall,
@NotNull ExpressionCodegen codegen @NotNull ExpressionCodegen codegen
) { ) {
super(type); super(type);
this.value = value; this.value = value;
this.delta = delta;
this.callable = callable;
this.resolvedCall = resolvedCall; this.resolvedCall = resolvedCall;
this.codegen = codegen; this.codegen = codegen;
} }
@@ -1291,13 +1221,7 @@ public abstract class StackValue {
value = StackValue.complexReceiver(value, true, false, true); value = StackValue.complexReceiver(value, true, false, true);
value.put(this.type, v); value.put(this.type, v);
if (callable instanceof Callable) { value.store(codegen.invokeFunction(resolvedCall, StackValue.onStack(this.type)), v, true);
value.store(codegen.invokeFunction(resolvedCall, StackValue.onStack(this.type)), v, true);
}
else {
genIncrement(this.type, delta, v);
value.store(StackValue.onStack(this.type), v, true);
}
value.put(this.type, v, true); value.put(this.type, v, true);
coerceTo(type, v); coerceTo(type, v);