Search in sources :

Example 1 with ValueParameterDescriptorImpl

use of org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl in project kotlin by JetBrains.

the class SignaturesPropagationData method modifyValueParametersAccordingToSuperMethods.

private ValueParameters modifyValueParametersAccordingToSuperMethods(@NotNull List<ValueParameterDescriptor> parameters) {
    KotlinType resultReceiverType = null;
    List<ValueParameterDescriptor> resultParameters = new ArrayList<ValueParameterDescriptor>(parameters.size());
    boolean shouldBeExtension = checkIfShouldBeExtension();
    for (final ValueParameterDescriptor originalParam : parameters) {
        final int originalIndex = originalParam.getIndex();
        List<TypeAndName> typesFromSuperMethods = ContainerUtil.map(superFunctions, new Function<FunctionDescriptor, TypeAndName>() {

            @Override
            public TypeAndName fun(FunctionDescriptor superFunction) {
                ReceiverParameterDescriptor receiver = superFunction.getExtensionReceiverParameter();
                int index = receiver != null ? originalIndex - 1 : originalIndex;
                if (index == -1) {
                    assert receiver != null : "can't happen: index is -1, while function is not extension";
                    return new TypeAndName(receiver.getType(), originalParam.getName());
                }
                ValueParameterDescriptor parameter = superFunction.getValueParameters().get(index);
                return new TypeAndName(parameter.getType(), parameter.getName());
            }
        });
        VarargCheckResult varargCheckResult = checkVarargInSuperFunctions(originalParam);
        KotlinType altType = varargCheckResult.parameterType;
        if (shouldBeExtension && originalIndex == 0) {
            resultReceiverType = altType;
        } else {
            Name stableName = null;
            for (int i = 0; i < superFunctions.size(); i++) {
                if (superFunctions.get(i).hasStableParameterNames()) {
                    // When there's more than one stable name in super functions, we pick the first one. This behaviour is similar to
                    // the compiler front-end, except that it reports a warning in such cases
                    // TODO: report a warning somewhere if there's more than one stable name in super functions
                    stableName = typesFromSuperMethods.get(i).name;
                    break;
                }
            }
            resultParameters.add(new ValueParameterDescriptorImpl(originalParam.getContainingDeclaration(), null, shouldBeExtension ? originalIndex - 1 : originalIndex, originalParam.getAnnotations(), stableName != null ? stableName : originalParam.getName(), altType, originalParam.declaresDefaultValue(), originalParam.isCrossinline(), originalParam.isNoinline(), varargCheckResult.isVararg ? DescriptorUtilsKt.getBuiltIns(originalParam).getArrayElementType(altType) : null, SourceElement.NO_SOURCE));
        }
    }
    boolean hasStableParameterNames = CollectionsKt.any(superFunctions, new Function1<FunctionDescriptor, Boolean>() {

        @Override
        public Boolean invoke(FunctionDescriptor descriptor) {
            return descriptor.hasStableParameterNames();
        }
    });
    return new ValueParameters(resultReceiverType, resultParameters, hasStableParameterNames);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) DescriptorUtils.getFqName(org.jetbrains.kotlin.resolve.DescriptorUtils.getFqName) Name(org.jetbrains.kotlin.name.Name) ValueParameterDescriptorImpl(org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl)

Example 2 with ValueParameterDescriptorImpl

use of org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl in project kotlin by JetBrains.

the class ControlStructureTypingUtils method createFunctionDescriptorForSpecialConstruction.

private SimpleFunctionDescriptorImpl createFunctionDescriptorForSpecialConstruction(@NotNull ResolveConstruct construct, @NotNull List<String> argumentNames, @NotNull List<Boolean> isArgumentNullable) {
    assert argumentNames.size() == isArgumentNullable.size();
    SimpleFunctionDescriptorImpl function = SimpleFunctionDescriptorImpl.create(moduleDescriptor, Annotations.Companion.getEMPTY(), construct.getSpecialFunctionName(), CallableMemberDescriptor.Kind.DECLARATION, SourceElement.NO_SOURCE);
    TypeParameterDescriptor typeParameter = TypeParameterDescriptorImpl.createWithDefaultBound(function, Annotations.Companion.getEMPTY(), false, Variance.INVARIANT, construct.getSpecialTypeParameterName(), 0);
    KotlinType type = typeParameter.getDefaultType();
    KotlinType nullableType = TypeUtils.makeNullable(type);
    List<ValueParameterDescriptor> valueParameters = new ArrayList<ValueParameterDescriptor>(argumentNames.size());
    for (int i = 0; i < argumentNames.size(); i++) {
        KotlinType argumentType = isArgumentNullable.get(i) ? nullableType : type;
        ValueParameterDescriptorImpl valueParameter = new ValueParameterDescriptorImpl(function, null, i, Annotations.Companion.getEMPTY(), Name.identifier(argumentNames.get(i)), argumentType, /* declaresDefaultValue = */
        false, /* isCrossinline = */
        false, /* isNoinline = */
        false, null, SourceElement.NO_SOURCE);
        valueParameters.add(valueParameter);
    }
    KotlinType returnType = construct != ResolveConstruct.ELVIS ? type : TypeUtilsKt.replaceAnnotations(type, AnnotationsForResolveKt.getExactInAnnotations());
    function.initialize(null, null, Lists.newArrayList(typeParameter), valueParameters, returnType, Modality.FINAL, Visibilities.PUBLIC);
    return function;
}
Also used : SimpleFunctionDescriptorImpl(org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl) ValueParameterDescriptorImpl(org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl)

Aggregations

ValueParameterDescriptorImpl (org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl)2 SimpleFunctionDescriptorImpl (org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl)1 Name (org.jetbrains.kotlin.name.Name)1 DescriptorUtils.getFqName (org.jetbrains.kotlin.resolve.DescriptorUtils.getFqName)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1