Minor. Drop 'CodegenBased.callableDescriptor' field and relevant generic parameter

It was redundant for most of subclasses
This commit is contained in:
Denis Zharkov
2016-05-19 11:44:28 +03:00
parent 158037dd68
commit f434801bf6
10 changed files with 47 additions and 42 deletions
@@ -1419,7 +1419,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
assert descriptor != null : "Function is not resolved to descriptor: " + declaration.getText(); assert descriptor != null : "Function is not resolved to descriptor: " + declaration.getText();
return genClosure( return genClosure(
declaration, descriptor, new FunctionGenerationStrategy.FunctionDefault(state, descriptor, declaration), samType, null, null declaration, descriptor, new FunctionGenerationStrategy.FunctionDefault(state, declaration), samType, null, null
); );
} }
@@ -120,7 +120,7 @@ public class FunctionCodegen {
if (owner.getContextKind() != OwnerKind.DEFAULT_IMPLS || function.hasBody()) { if (owner.getContextKind() != OwnerKind.DEFAULT_IMPLS || function.hasBody()) {
generateMethod(JvmDeclarationOriginKt.OtherOrigin(function, functionDescriptor), functionDescriptor, generateMethod(JvmDeclarationOriginKt.OtherOrigin(function, functionDescriptor), functionDescriptor,
new FunctionGenerationStrategy.FunctionDefault(state, functionDescriptor, function)); new FunctionGenerationStrategy.FunctionDefault(state, function));
} }
generateDefaultIfNeeded(owner.intoFunction(functionDescriptor), functionDescriptor, owner.getContextKind(), generateDefaultIfNeeded(owner.intoFunction(functionDescriptor), functionDescriptor, owner.getContextKind(),
@@ -33,15 +33,14 @@ public abstract class FunctionGenerationStrategy {
@NotNull MemberCodegen<?> parentCodegen @NotNull MemberCodegen<?> parentCodegen
); );
public static class FunctionDefault extends CodegenBased<CallableDescriptor> { public static class FunctionDefault extends CodegenBased {
private final KtDeclarationWithBody declaration; private final KtDeclarationWithBody declaration;
public FunctionDefault( public FunctionDefault(
@NotNull GenerationState state, @NotNull GenerationState state,
@NotNull CallableDescriptor descriptor,
@NotNull KtDeclarationWithBody declaration @NotNull KtDeclarationWithBody declaration
) { ) {
super(state, descriptor); super(state);
this.declaration = declaration; this.declaration = declaration;
} }
@@ -51,13 +50,11 @@ public abstract class FunctionGenerationStrategy {
} }
} }
public abstract static class CodegenBased<T extends CallableDescriptor> extends FunctionGenerationStrategy { public abstract static class CodegenBased extends FunctionGenerationStrategy {
protected final GenerationState state; protected final GenerationState state;
protected final T callableDescriptor;
public CodegenBased(@NotNull GenerationState state, @NotNull T callableDescriptor) { public CodegenBased(@NotNull GenerationState state) {
this.state = state; this.state = state;
this.callableDescriptor = callableDescriptor;
} }
@Override @Override
@@ -36,9 +36,10 @@ import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter;
import java.util.*; import java.util.*;
public class FunctionReferenceGenerationStrategy extends FunctionGenerationStrategy.CodegenBased<FunctionDescriptor> { public class FunctionReferenceGenerationStrategy extends FunctionGenerationStrategy.CodegenBased {
private final ResolvedCall<?> resolvedCall; private final ResolvedCall<?> resolvedCall;
private final FunctionDescriptor referencedFunction; private final FunctionDescriptor referencedFunction;
private final FunctionDescriptor functionDescriptor;
private final Type receiverType; // non-null for bound references private final Type receiverType; // non-null for bound references
private final StackValue receiverValue; private final StackValue receiverValue;
@@ -49,9 +50,10 @@ public class FunctionReferenceGenerationStrategy extends FunctionGenerationStrat
@Nullable Type receiverType, @Nullable Type receiverType,
@Nullable StackValue receiverValue @Nullable StackValue receiverValue
) { ) {
super(state, functionDescriptor); super(state);
this.resolvedCall = resolvedCall; this.resolvedCall = resolvedCall;
this.referencedFunction = (FunctionDescriptor) resolvedCall.getResultingDescriptor(); this.referencedFunction = (FunctionDescriptor) resolvedCall.getResultingDescriptor();
this.functionDescriptor = functionDescriptor;
this.receiverType = receiverType; this.receiverType = receiverType;
this.receiverValue = receiverValue; this.receiverValue = receiverValue;
assert receiverType != null || receiverValue == null assert receiverType != null || receiverValue == null
@@ -82,7 +84,7 @@ public class FunctionReferenceGenerationStrategy extends FunctionGenerationStrat
{ {
argumentMap = new LinkedHashMap<ValueParameterDescriptor, ResolvedValueArgument>(fakeArguments.size()); argumentMap = new LinkedHashMap<ValueParameterDescriptor, ResolvedValueArgument>(fakeArguments.size());
int index = 0; int index = 0;
List<ValueParameterDescriptor> parameters = callableDescriptor.getValueParameters(); List<ValueParameterDescriptor> parameters = functionDescriptor.getValueParameters();
for (ValueArgument argument : fakeArguments) { for (ValueArgument argument : fakeArguments) {
argumentMap.put(parameters.get(index), new ExpressionValueArgument(argument)); argumentMap.put(parameters.get(index), new ExpressionValueArgument(argument));
index++; index++;
@@ -154,7 +156,7 @@ public class FunctionReferenceGenerationStrategy extends FunctionGenerationStrat
(referencedFunction.getExtensionReceiverParameter() != null ? 1 : 0) - (referencedFunction.getExtensionReceiverParameter() != null ? 1 : 0) -
(receiverType != null ? 1 : 0); (receiverType != null ? 1 : 0);
List<ValueParameterDescriptor> parameters = CollectionsKt.drop(callableDescriptor.getValueParameters(), receivers); List<ValueParameterDescriptor> parameters = CollectionsKt.drop(functionDescriptor.getValueParameters(), receivers);
for (int i = 0; i < parameters.size(); i++) { for (int i = 0; i < parameters.size(); i++) {
ValueParameterDescriptor parameter = parameters.get(i); ValueParameterDescriptor parameter = parameters.get(i);
ValueArgument fakeArgument = fakeArguments.get(i); ValueArgument fakeArgument = fakeArguments.get(i);
@@ -931,7 +931,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
private void generatePrimaryConstructor(final DelegationFieldsInfo delegationFieldsInfo) { private void generatePrimaryConstructor(final DelegationFieldsInfo delegationFieldsInfo) {
if (isInterface(descriptor) || isAnnotationClass(descriptor)) return; if (isInterface(descriptor) || isAnnotationClass(descriptor)) return;
ConstructorDescriptor constructorDescriptor = descriptor.getUnsubstitutedPrimaryConstructor(); final ConstructorDescriptor constructorDescriptor = descriptor.getUnsubstitutedPrimaryConstructor();
if (constructorDescriptor == null) return; if (constructorDescriptor == null) return;
ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor); ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor);
@@ -940,10 +940,10 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
JvmDeclarationOrigin origin = JvmDeclarationOriginKt JvmDeclarationOrigin origin = JvmDeclarationOriginKt
.OtherOrigin(primaryConstructor != null ? primaryConstructor : myClass, constructorDescriptor); .OtherOrigin(primaryConstructor != null ? primaryConstructor : myClass, constructorDescriptor);
functionCodegen.generateMethod(origin, constructorDescriptor, constructorContext, functionCodegen.generateMethod(origin, constructorDescriptor, constructorContext,
new FunctionGenerationStrategy.CodegenBased<ConstructorDescriptor>(state, constructorDescriptor) { new FunctionGenerationStrategy.CodegenBased(state) {
@Override @Override
public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) { public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
generatePrimaryConstructorImpl(callableDescriptor, codegen, delegationFieldsInfo, primaryConstructor); generatePrimaryConstructorImpl(constructorDescriptor, codegen, delegationFieldsInfo, primaryConstructor);
} }
} }
); );
@@ -954,7 +954,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
new DefaultParameterValueSubstitutor(state).generatePrimaryConstructorOverloadsIfNeeded(constructorDescriptor, v, kind, myClass); new DefaultParameterValueSubstitutor(state).generatePrimaryConstructorOverloadsIfNeeded(constructorDescriptor, v, kind, myClass);
} }
private void generateSecondaryConstructor(@NotNull ConstructorDescriptor constructorDescriptor) { private void generateSecondaryConstructor(@NotNull final ConstructorDescriptor constructorDescriptor) {
if (!canHaveDeclaredConstructors(descriptor)) return; if (!canHaveDeclaredConstructors(descriptor)) return;
ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor); ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor);
@@ -964,10 +964,10 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
functionCodegen.generateMethod( functionCodegen.generateMethod(
JvmDeclarationOriginKt.OtherOrigin(constructor, constructorDescriptor), JvmDeclarationOriginKt.OtherOrigin(constructor, constructorDescriptor),
constructorDescriptor, constructorContext, constructorDescriptor, constructorContext,
new FunctionGenerationStrategy.CodegenBased<ConstructorDescriptor>(state, constructorDescriptor) { new FunctionGenerationStrategy.CodegenBased(state) {
@Override @Override
public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) { public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
generateSecondaryConstructorImpl(callableDescriptor, codegen); generateSecondaryConstructorImpl(constructorDescriptor, codegen);
} }
} }
); );
@@ -1335,7 +1335,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
functionCodegen.generateMethod( functionCodegen.generateMethod(
JvmDeclarationOriginKt.DelegationToTraitImpl(descriptorToDeclaration(traitFun), traitFun), JvmDeclarationOriginKt.DelegationToTraitImpl(descriptorToDeclaration(traitFun), traitFun),
inheritedFun, inheritedFun,
new FunctionGenerationStrategy.CodegenBased<FunctionDescriptor>(state, inheritedFun) { new FunctionGenerationStrategy.CodegenBased(state) {
@Override @Override
public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) { public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
DeclarationDescriptor containingDeclaration = traitFun.getContainingDeclaration(); DeclarationDescriptor containingDeclaration = traitFun.getContainingDeclaration();
@@ -114,7 +114,7 @@ class InterfaceImplBodyCodegen(
functionCodegen.generateMethod( functionCodegen.generateMethod(
DelegationToTraitImpl(DescriptorToSourceUtils.descriptorToDeclaration(descriptor), descriptor), DelegationToTraitImpl(DescriptorToSourceUtils.descriptorToDeclaration(descriptor), descriptor),
descriptor, descriptor,
object : FunctionGenerationStrategy.CodegenBased<FunctionDescriptor>(state, descriptor) { object : FunctionGenerationStrategy.CodegenBased(state) {
override fun doGenerateBody(codegen: ExpressionCodegen, signature: JvmMethodSignature) { override fun doGenerateBody(codegen: ExpressionCodegen, signature: JvmMethodSignature) {
val iv = codegen.v val iv = codegen.v
@@ -126,7 +126,7 @@ class InterfaceImplBodyCodegen(
throw AssertionError( throw AssertionError(
"Method from super interface has a different signature.\n" + "Method from super interface has a different signature.\n" +
"This method:\n%s\n%s\n%s\nSuper method:\n%s\n%s\n%s".format( "This method:\n%s\n%s\n%s\nSuper method:\n%s\n%s\n%s".format(
callableDescriptor, signature, myParameters, delegateTo, method, calleeParameters descriptor, signature, myParameters, delegateTo, method, calleeParameters
) )
) )
} }
@@ -43,7 +43,7 @@ class JvmStaticGenerator(
codegen.functionCodegen.generateMethod( codegen.functionCodegen.generateMethod(
Synthetic(originElement, staticFunctionDescriptor), Synthetic(originElement, staticFunctionDescriptor),
staticFunctionDescriptor, staticFunctionDescriptor,
object : FunctionGenerationStrategy.CodegenBased<FunctionDescriptor>(state, descriptor) { object : FunctionGenerationStrategy.CodegenBased(state) {
override fun doGenerateBody(codegen: ExpressionCodegen, signature: JvmMethodSignature) { override fun doGenerateBody(codegen: ExpressionCodegen, signature: JvmMethodSignature) {
val iv = codegen.v val iv = codegen.v
val classDescriptor = descriptor.containingDeclaration as ClassDescriptor val classDescriptor = descriptor.containingDeclaration as ClassDescriptor
@@ -57,7 +57,7 @@ class JvmStaticGenerator(
} }
if (descriptor is PropertyAccessorDescriptor) { if (descriptor is PropertyAccessorDescriptor) {
val propertyValue = codegen.intermediateValueForProperty(descriptor.correspondingProperty, false, null, StackValue.none()) val propertyValue = codegen.intermediateValueForProperty(descriptor.correspondingProperty, false, null, StackValue.none())
if (callableDescriptor is PropertyGetterDescriptor) { if (descriptor is PropertyGetterDescriptor) {
propertyValue.put(signature.returnType, iv) propertyValue.put(signature.returnType, iv)
} }
else { else {
@@ -579,7 +579,7 @@ public abstract class MemberCodegen<T extends KtElement/* TODO: & JetDeclaration
final FunctionDescriptor original = (FunctionDescriptor) accessorForCallableDescriptor.getCalleeDescriptor(); final FunctionDescriptor original = (FunctionDescriptor) accessorForCallableDescriptor.getCalleeDescriptor();
functionCodegen.generateMethod( functionCodegen.generateMethod(
Synthetic(null, original), accessor, Synthetic(null, original), accessor,
new FunctionGenerationStrategy.CodegenBased<FunctionDescriptor>(state, accessor) { new FunctionGenerationStrategy.CodegenBased(state) {
@Override @Override
public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) { public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
markLineNumberForElement(element, codegen.v); markLineNumberForElement(element, codegen.v);
@@ -595,9 +595,11 @@ public abstract class MemberCodegen<T extends KtElement/* TODO: & JetDeclaration
final AccessorForPropertyDescriptor accessor = (AccessorForPropertyDescriptor) accessorForCallableDescriptor; final AccessorForPropertyDescriptor accessor = (AccessorForPropertyDescriptor) accessorForCallableDescriptor;
final PropertyDescriptor original = accessor.getCalleeDescriptor(); final PropertyDescriptor original = accessor.getCalleeDescriptor();
class PropertyAccessorStrategy extends FunctionGenerationStrategy.CodegenBased<PropertyAccessorDescriptor> { class PropertyAccessorStrategy extends FunctionGenerationStrategy.CodegenBased {
private final PropertyAccessorDescriptor callableDescriptor;
public PropertyAccessorStrategy(@NotNull PropertyAccessorDescriptor callableDescriptor) { public PropertyAccessorStrategy(@NotNull PropertyAccessorDescriptor callableDescriptor) {
super(MemberCodegen.this.state, callableDescriptor); super(MemberCodegen.this.state);
this.callableDescriptor = callableDescriptor;
} }
@Override @Override
@@ -430,7 +430,7 @@ public class PropertyCodegen {
} }
} }
else { else {
strategy = new FunctionGenerationStrategy.FunctionDefault(state, accessorDescriptor, accessor); strategy = new FunctionGenerationStrategy.FunctionDefault(state, accessor);
} }
functionCodegen.generateMethod(JvmDeclarationOriginKt.OtherOrigin(accessor != null ? accessor : p, accessorDescriptor), accessorDescriptor, strategy); functionCodegen.generateMethod(JvmDeclarationOriginKt.OtherOrigin(accessor != null ? accessor : p, accessorDescriptor), accessorDescriptor, strategy);
@@ -467,15 +467,17 @@ public class PropertyCodegen {
} }
private static class DefaultPropertyAccessorStrategy extends FunctionGenerationStrategy.CodegenBased<PropertyAccessorDescriptor> { private static class DefaultPropertyAccessorStrategy extends FunctionGenerationStrategy.CodegenBased {
private final PropertyAccessorDescriptor propertyAccessorDescriptor;
public DefaultPropertyAccessorStrategy(@NotNull GenerationState state, @NotNull PropertyAccessorDescriptor descriptor) { public DefaultPropertyAccessorStrategy(@NotNull GenerationState state, @NotNull PropertyAccessorDescriptor descriptor) {
super(state, descriptor); super(state);
propertyAccessorDescriptor = descriptor;
} }
@Override @Override
public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) { public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
InstructionAdapter v = codegen.v; InstructionAdapter v = codegen.v;
PropertyDescriptor propertyDescriptor = callableDescriptor.getCorrespondingProperty(); PropertyDescriptor propertyDescriptor = propertyAccessorDescriptor.getCorrespondingProperty();
StackValue property = codegen.intermediateValueForProperty(propertyDescriptor, true, null, StackValue.LOCAL_0); StackValue property = codegen.intermediateValueForProperty(propertyDescriptor, true, null, StackValue.LOCAL_0);
PsiElement jetProperty = DescriptorToSourceUtils.descriptorToDeclaration(propertyDescriptor); PsiElement jetProperty = DescriptorToSourceUtils.descriptorToDeclaration(propertyDescriptor);
@@ -483,22 +485,22 @@ public class PropertyCodegen {
codegen.markLineNumber((KtElement) jetProperty, false); codegen.markLineNumber((KtElement) jetProperty, false);
} }
if (callableDescriptor instanceof PropertyGetterDescriptor) { if (propertyAccessorDescriptor instanceof PropertyGetterDescriptor) {
Type type = signature.getReturnType(); Type type = signature.getReturnType();
property.put(type, v); property.put(type, v);
v.areturn(type); v.areturn(type);
} }
else if (callableDescriptor instanceof PropertySetterDescriptor) { else if (propertyAccessorDescriptor instanceof PropertySetterDescriptor) {
List<ValueParameterDescriptor> valueParameters = callableDescriptor.getValueParameters(); List<ValueParameterDescriptor> valueParameters = propertyAccessorDescriptor.getValueParameters();
assert valueParameters.size() == 1 : "Property setter should have only one value parameter but has " + callableDescriptor; assert valueParameters.size() == 1 : "Property setter should have only one value parameter but has " + propertyAccessorDescriptor;
int parameterIndex = codegen.lookupLocalIndex(valueParameters.get(0)); int parameterIndex = codegen.lookupLocalIndex(valueParameters.get(0));
assert parameterIndex >= 0 : "Local index for setter parameter should be positive or zero: " + callableDescriptor; assert parameterIndex >= 0 : "Local index for setter parameter should be positive or zero: " + propertyAccessorDescriptor;
Type type = codegen.typeMapper.mapType(propertyDescriptor); Type type = codegen.typeMapper.mapType(propertyDescriptor);
property.store(StackValue.local(parameterIndex, type), codegen.v); property.store(StackValue.local(parameterIndex, type), codegen.v);
v.visitInsn(RETURN); v.visitInsn(RETURN);
} }
else { else {
throw new IllegalStateException("Unknown property accessor: " + callableDescriptor); throw new IllegalStateException("Unknown property accessor: " + propertyAccessorDescriptor);
} }
} }
} }
@@ -545,12 +547,14 @@ public class PropertyCodegen {
return codegen.invokeFunction(resolvedCall, delegatedProperty); return codegen.invokeFunction(resolvedCall, delegatedProperty);
} }
private static class DelegatedPropertyAccessorStrategy extends FunctionGenerationStrategy.CodegenBased<PropertyAccessorDescriptor> { private static class DelegatedPropertyAccessorStrategy extends FunctionGenerationStrategy.CodegenBased {
private final int index; private final int index;
private final PropertyAccessorDescriptor propertyAccessorDescriptor;
public DelegatedPropertyAccessorStrategy(@NotNull GenerationState state, @NotNull PropertyAccessorDescriptor descriptor, int index) { public DelegatedPropertyAccessorStrategy(@NotNull GenerationState state, @NotNull PropertyAccessorDescriptor descriptor, int index) {
super(state, descriptor); super(state);
this.index = index; this.index = index;
propertyAccessorDescriptor = descriptor;
} }
@Override @Override
@@ -559,10 +563,10 @@ public class PropertyCodegen {
BindingContext bindingContext = state.getBindingContext(); BindingContext bindingContext = state.getBindingContext();
ResolvedCall<FunctionDescriptor> resolvedCall = ResolvedCall<FunctionDescriptor> resolvedCall =
bindingContext.get(BindingContext.DELEGATED_PROPERTY_RESOLVED_CALL, callableDescriptor); bindingContext.get(BindingContext.DELEGATED_PROPERTY_RESOLVED_CALL, propertyAccessorDescriptor);
assert resolvedCall != null : "Resolve call should be recorded for delegate call " + signature.toString(); assert resolvedCall != null : "Resolve call should be recorded for delegate call " + signature.toString();
StackValue lastValue = invokeDelegatedPropertyConventionMethod(callableDescriptor.getCorrespondingProperty(), StackValue lastValue = invokeDelegatedPropertyConventionMethod(propertyAccessorDescriptor.getCorrespondingProperty(),
codegen, state.getTypeMapper(), resolvedCall, index, 1); codegen, state.getTypeMapper(), resolvedCall, index, 1);
Type asmType = signature.getReturnType(); Type asmType = signature.getReturnType();
lastValue.put(asmType, v); lastValue.put(asmType, v);
@@ -509,7 +509,7 @@ public class InlineCodegen extends CallGenerator {
); );
} }
else { else {
strategy = new FunctionGenerationStrategy.FunctionDefault(state, descriptor, (KtDeclarationWithBody) expression); strategy = new FunctionGenerationStrategy.FunctionDefault(state, (KtDeclarationWithBody) expression);
} }
FunctionCodegen.generateMethodBody(adapter, descriptor, context, jvmMethodSignature, strategy, parentCodegen); FunctionCodegen.generateMethodBody(adapter, descriptor, context, jvmMethodSignature, strategy, parentCodegen);