Private constructors are now accessed via synthetic constructor with DEFAULT_CONSTRUCTOR_MARKER as an additional argument #KT-6299 Fixed
A set of tests provided. Some external tests fixed accordingly. Companion object creation changed accordingly. Derived classes now can use base class with the private constructor. Refactoring of AccessorForFunctionDescriptor.
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.kotlin.codegen
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import java.util.*
|
||||
|
||||
open public class AbstractAccessorForFunctionDescriptor(
|
||||
containingDeclaration: DeclarationDescriptor,
|
||||
name: Name
|
||||
) : SimpleFunctionDescriptorImpl(containingDeclaration, null, Annotations.EMPTY,
|
||||
name, CallableMemberDescriptor.Kind.DECLARATION, SourceElement.NO_SOURCE) {
|
||||
|
||||
protected fun copyTypeParameters(descriptor: FunctionDescriptor): List<TypeParameterDescriptor> = descriptor.getTypeParameters().map {
|
||||
val copy = TypeParameterDescriptorImpl.createForFurtherModification(
|
||||
this, it.getAnnotations(), it.isReified(),
|
||||
it.getVariance(), it.getName(),
|
||||
it.getIndex(), SourceElement.NO_SOURCE)
|
||||
for (upperBound in it.getUpperBounds()) {
|
||||
copy.addUpperBound(upperBound)
|
||||
}
|
||||
copy.setInitialized()
|
||||
copy
|
||||
}
|
||||
|
||||
protected fun copyValueParameters(descriptor: FunctionDescriptor): List<ValueParameterDescriptor> =
|
||||
descriptor.getValueParameters().map { it.copy(this, it.getName()) }
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.kotlin.codegen
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
|
||||
import java.util.*
|
||||
|
||||
public class AccessorForConstructorDescriptor(
|
||||
private val calleeDescriptor: ConstructorDescriptor,
|
||||
containingDeclaration: DeclarationDescriptor
|
||||
) : AbstractAccessorForFunctionDescriptor(containingDeclaration, Name.special("<init>"))
|
||||
, ConstructorDescriptor
|
||||
, AccessorForCallableDescriptor<ConstructorDescriptor> {
|
||||
|
||||
override fun getCalleeDescriptor(): ConstructorDescriptor = calleeDescriptor
|
||||
|
||||
override fun getContainingDeclaration(): ClassDescriptor = calleeDescriptor.getContainingDeclaration()
|
||||
|
||||
override fun isPrimary(): Boolean = false
|
||||
|
||||
init {
|
||||
initialize(
|
||||
DescriptorUtils.getReceiverParameterType(getExtensionReceiverParameter()),
|
||||
calleeDescriptor.getDispatchReceiverParameter(),
|
||||
copyTypeParameters(calleeDescriptor),
|
||||
copyValueParameters(calleeDescriptor),
|
||||
calleeDescriptor.getReturnType(),
|
||||
Modality.FINAL,
|
||||
Visibilities.INTERNAL
|
||||
)
|
||||
}
|
||||
}
|
||||
+3
-39
@@ -18,20 +18,13 @@ package org.jetbrains.kotlin.codegen;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.descriptors.*;
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
|
||||
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl;
|
||||
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
||||
import org.jetbrains.kotlin.resolve.annotations.AnnotationsPackage;
|
||||
import org.jetbrains.kotlin.types.JetType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER;
|
||||
|
||||
public class AccessorForFunctionDescriptor extends SimpleFunctionDescriptorImpl implements AccessorForCallableDescriptor<FunctionDescriptor> {
|
||||
public class AccessorForFunctionDescriptor extends AbstractAccessorForFunctionDescriptor implements AccessorForCallableDescriptor<FunctionDescriptor> {
|
||||
|
||||
private final FunctionDescriptor calleeDescriptor;
|
||||
|
||||
@@ -40,9 +33,8 @@ public class AccessorForFunctionDescriptor extends SimpleFunctionDescriptorImpl
|
||||
@NotNull DeclarationDescriptor containingDeclaration,
|
||||
int index
|
||||
) {
|
||||
super(containingDeclaration, null, Annotations.EMPTY,
|
||||
Name.identifier("access$" + (descriptor instanceof ConstructorDescriptor ? "init" : descriptor.getName()) + "$" + index),
|
||||
Kind.DECLARATION, SourceElement.NO_SOURCE);
|
||||
super(containingDeclaration,
|
||||
Name.identifier("access$" + (descriptor instanceof ConstructorDescriptor ? "init" : descriptor.getName()) + "$" + index));
|
||||
this.calleeDescriptor = descriptor;
|
||||
|
||||
initialize(DescriptorUtils.getReceiverParameterType(descriptor.getExtensionReceiverParameter()),
|
||||
@@ -56,34 +48,6 @@ public class AccessorForFunctionDescriptor extends SimpleFunctionDescriptorImpl
|
||||
Visibilities.INTERNAL);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<TypeParameterDescriptor> copyTypeParameters(@NotNull FunctionDescriptor descriptor) {
|
||||
List<TypeParameterDescriptor> typeParameters = descriptor.getTypeParameters();
|
||||
List<TypeParameterDescriptor> result = new ArrayList<TypeParameterDescriptor>(typeParameters.size());
|
||||
for (TypeParameterDescriptor typeParameter : typeParameters) {
|
||||
TypeParameterDescriptorImpl copy = TypeParameterDescriptorImpl.createForFurtherModification(
|
||||
this, typeParameter.getAnnotations(), typeParameter.isReified(),
|
||||
typeParameter.getVariance(), typeParameter.getName(), typeParameter.getIndex(), SourceElement.NO_SOURCE
|
||||
);
|
||||
for (JetType upperBound : typeParameter.getUpperBounds()) {
|
||||
copy.addUpperBound(upperBound);
|
||||
}
|
||||
copy.setInitialized();
|
||||
result.add(copy);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<ValueParameterDescriptor> copyValueParameters(@NotNull FunctionDescriptor descriptor) {
|
||||
List<ValueParameterDescriptor> valueParameters = descriptor.getValueParameters();
|
||||
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>(valueParameters.size());
|
||||
for (ValueParameterDescriptor valueParameter : valueParameters) {
|
||||
result.add(valueParameter.copy(this, valueParameter.getName()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public FunctionDescriptor getCalleeDescriptor() {
|
||||
|
||||
@@ -170,7 +170,7 @@ public class AsmUtil {
|
||||
|
||||
public static boolean isStaticMethod(OwnerKind kind, CallableMemberDescriptor functionDescriptor) {
|
||||
return isStaticKind(kind) ||
|
||||
JetTypeMapper.isAccessor(functionDescriptor) ||
|
||||
JetTypeMapper.isStaticAccessor(functionDescriptor) ||
|
||||
AnnotationsPackage.isPlatformStaticInObjectOrClass(functionDescriptor);
|
||||
}
|
||||
|
||||
|
||||
@@ -70,6 +70,8 @@ import org.jetbrains.kotlin.resolve.constants.evaluate.EvaluatePackage;
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage;
|
||||
import org.jetbrains.kotlin.resolve.inline.InlineUtil;
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm;
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind;
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature;
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature;
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.*;
|
||||
import org.jetbrains.kotlin.types.Approximation;
|
||||
@@ -2410,6 +2412,16 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
callGenerator.putValueIfNeeded(null, Type.INT_TYPE, StackValue.constant(mask, Type.INT_TYPE));
|
||||
}
|
||||
|
||||
// Extra constructor marker argument
|
||||
if (callableMethod instanceof CallableMethod) {
|
||||
List<JvmMethodParameterSignature> callableParameters = ((CallableMethod) callableMethod).getValueParameters();
|
||||
for (JvmMethodParameterSignature parameter: callableParameters) {
|
||||
if (parameter.getKind() == JvmMethodParameterKind.CONSTRUCTOR_MARKER) {
|
||||
callGenerator.putValueIfNeeded(null, parameter.getAsmType(), StackValue.constant(null, parameter.getAsmType()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
callGenerator.genCall(callableMethod, resolvedCall, !masks.isEmpty(), this);
|
||||
}
|
||||
|
||||
@@ -3385,6 +3397,14 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
return generateConstructorCall(resolvedCall, type);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ConstructorDescriptor getConstructorDescriptor(@NotNull ResolvedCall<?> resolvedCall) {
|
||||
FunctionDescriptor accessibleDescriptor = accessibleFunctionDescriptor(resolvedCall);
|
||||
assert accessibleDescriptor instanceof ConstructorDescriptor :
|
||||
"getConstructorDescriptor must be called only for constructors: " + accessibleDescriptor;
|
||||
return (ConstructorDescriptor) accessibleDescriptor;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public StackValue generateConstructorCall(@NotNull final ResolvedCall<?> resolvedCall, @NotNull final Type objectType) {
|
||||
return StackValue.functionCall(objectType, new Function1<InstructionAdapter, Unit>() {
|
||||
@@ -3393,7 +3413,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
v.anew(objectType);
|
||||
v.dup();
|
||||
|
||||
ConstructorDescriptor constructor = (ConstructorDescriptor) resolvedCall.getResultingDescriptor();
|
||||
ConstructorDescriptor constructor = getConstructorDescriptor(resolvedCall);
|
||||
|
||||
ReceiverParameterDescriptor dispatchReceiver = constructor.getDispatchReceiverParameter();
|
||||
if (dispatchReceiver != null) {
|
||||
|
||||
@@ -57,6 +57,7 @@ import org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument;
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
|
||||
import org.jetbrains.kotlin.resolve.calls.model.VarargValueArgument;
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage;
|
||||
import org.jetbrains.kotlin.resolve.jvm.AsmTypes;
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin;
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature;
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind;
|
||||
@@ -831,7 +832,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
||||
|
||||
private void generateSyntheticAccessor(Map.Entry<DeclarationDescriptor, DeclarationDescriptor> entry) {
|
||||
if (entry.getValue() instanceof FunctionDescriptor) {
|
||||
FunctionDescriptor bridge = (FunctionDescriptor) entry.getValue();
|
||||
final FunctionDescriptor bridge = (FunctionDescriptor) entry.getValue();
|
||||
final FunctionDescriptor original = (FunctionDescriptor) entry.getKey();
|
||||
functionCodegen.generateMethod(
|
||||
Synthetic(null, original), bridge,
|
||||
@@ -840,7 +841,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
||||
public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
|
||||
markLineNumberForSyntheticFunction(descriptor, codegen.v);
|
||||
|
||||
generateMethodCallTo(original, codegen.v);
|
||||
generateMethodCallTo(original, bridge, codegen.v);
|
||||
codegen.v.areturn(signature.getReturnType());
|
||||
}
|
||||
}
|
||||
@@ -921,15 +922,21 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
||||
}
|
||||
}
|
||||
|
||||
private void generateMethodCallTo(FunctionDescriptor functionDescriptor, InstructionAdapter iv) {
|
||||
private void generateMethodCallTo(
|
||||
@NotNull FunctionDescriptor functionDescriptor,
|
||||
@Nullable FunctionDescriptor bridgeDescriptor,
|
||||
@NotNull InstructionAdapter iv
|
||||
) {
|
||||
boolean isConstructor = functionDescriptor instanceof ConstructorDescriptor;
|
||||
boolean callFromAccessor = !JetTypeMapper.isAccessor(functionDescriptor);
|
||||
boolean bridgeIsAccessorConstructor = bridgeDescriptor instanceof AccessorForConstructorDescriptor;
|
||||
boolean callFromAccessor = bridgeIsAccessorConstructor
|
||||
|| (bridgeDescriptor != null && JetTypeMapper.isAccessor(bridgeDescriptor));
|
||||
CallableMethod callableMethod = isConstructor ?
|
||||
typeMapper.mapToCallableMethod((ConstructorDescriptor) functionDescriptor) :
|
||||
typeMapper.mapToCallableMethod(functionDescriptor, callFromAccessor, context);
|
||||
|
||||
int reg = 1;
|
||||
if (isConstructor) {
|
||||
if (isConstructor && !bridgeIsAccessorConstructor) {
|
||||
iv.anew(callableMethod.getOwner());
|
||||
iv.dup();
|
||||
reg = 0;
|
||||
@@ -941,8 +948,13 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
||||
}
|
||||
|
||||
for (Type argType : callableMethod.getParameterTypes()) {
|
||||
iv.load(reg, argType);
|
||||
reg += argType.getSize();
|
||||
if (AsmTypes.DEFAULT_CONSTRUCTOR_MARKER.equals(argType)) {
|
||||
iv.aconst(null);
|
||||
}
|
||||
else {
|
||||
iv.load(reg, argType);
|
||||
reg += argType.getSize();
|
||||
}
|
||||
}
|
||||
callableMethod.genInvokeInstruction(iv);
|
||||
}
|
||||
@@ -1036,7 +1048,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
||||
private void generateCompanionObjectInitializer(@NotNull ClassDescriptor companionObject) {
|
||||
ExpressionCodegen codegen = createOrGetClInitCodegen();
|
||||
FunctionDescriptor constructor = context.accessibleFunctionDescriptor(KotlinPackage.single(companionObject.getConstructors()));
|
||||
generateMethodCallTo(constructor, codegen.v);
|
||||
generateMethodCallTo(constructor, null, codegen.v);
|
||||
codegen.v.dup();
|
||||
StackValue instance = StackValue.onStack(typeMapper.mapClass(companionObject));
|
||||
StackValue.singleton(companionObject, typeMapper).store(instance, codegen.v, true);
|
||||
@@ -1475,7 +1487,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
||||
return;
|
||||
}
|
||||
iv.load(0, OBJECT_TYPE);
|
||||
ConstructorDescriptor delegateConstructor = SamCodegenUtil.resolveSamAdapter(delegationConstructorCall.getResultingDescriptor());
|
||||
ConstructorDescriptor delegateConstructor = SamCodegenUtil.resolveSamAdapter(codegen.getConstructorDescriptor(delegationConstructorCall));
|
||||
|
||||
CallableMethod delegateConstructorCallable = typeMapper.mapToCallableMethod(delegateConstructor);
|
||||
CallableMethod callable = typeMapper.mapToCallableMethod(constructorDescriptor);
|
||||
|
||||
@@ -241,9 +241,12 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
|
||||
}
|
||||
|
||||
int accessorIndex = accessors.size();
|
||||
if (descriptor instanceof SimpleFunctionDescriptor || descriptor instanceof ConstructorDescriptor) {
|
||||
if (descriptor instanceof SimpleFunctionDescriptor) {
|
||||
accessor = new AccessorForFunctionDescriptor((FunctionDescriptor) descriptor, contextDescriptor, accessorIndex);
|
||||
}
|
||||
else if (descriptor instanceof ConstructorDescriptor) {
|
||||
accessor = new AccessorForConstructorDescriptor((ConstructorDescriptor) descriptor, contextDescriptor);
|
||||
}
|
||||
else if (descriptor instanceof PropertyDescriptor) {
|
||||
if (isForBackingFieldInOuterClass) {
|
||||
accessor = new AccessorForPropertyBackingFieldInOuterClass((PropertyDescriptor) descriptor, contextDescriptor,
|
||||
|
||||
@@ -584,7 +584,7 @@ public class JetTypeMapper {
|
||||
}
|
||||
else {
|
||||
if (isStaticDeclaration(functionDescriptor) ||
|
||||
isAccessor(functionDescriptor) ||
|
||||
isStaticAccessor(functionDescriptor) ||
|
||||
AnnotationsPackage.isPlatformStaticInObjectOrClass(functionDescriptor)) {
|
||||
invokeOpcode = INVOKESTATIC;
|
||||
}
|
||||
@@ -643,6 +643,11 @@ public class JetTypeMapper {
|
||||
return descriptor instanceof AccessorForCallableDescriptor<?>;
|
||||
}
|
||||
|
||||
public static boolean isStaticAccessor(@NotNull CallableMemberDescriptor descriptor) {
|
||||
if (descriptor instanceof AccessorForConstructorDescriptor) return false;
|
||||
return isAccessor(descriptor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static FunctionDescriptor findAnyDeclaration(@NotNull FunctionDescriptor function) {
|
||||
if (function.getKind() == CallableMemberDescriptor.Kind.DECLARATION) {
|
||||
@@ -748,6 +753,10 @@ public class JetTypeMapper {
|
||||
writeParameter(sw, parameter.getType());
|
||||
}
|
||||
|
||||
if (f instanceof AccessorForConstructorDescriptor) {
|
||||
writeParameter(sw, JvmMethodParameterKind.CONSTRUCTOR_MARKER, DEFAULT_CONSTRUCTOR_MARKER);
|
||||
}
|
||||
|
||||
writeVoidReturn(sw);
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user