Search in sources :

Example 1 with TypeProjection

use of org.jetbrains.kotlin.types.TypeProjection in project kotlin by JetBrains.

the class ResolvedCallImpl method setResultingSubstitutor.

@Override
public void setResultingSubstitutor(@NotNull TypeSubstitutor substitutor) {
    resultingDescriptor = (D) candidateDescriptor.substitute(substitutor);
    assert resultingDescriptor != null : candidateDescriptor;
    for (TypeParameterDescriptor typeParameter : candidateDescriptor.getTypeParameters()) {
        TypeProjection typeArgumentProjection = substitutor.getSubstitution().get(typeParameter.getDefaultType());
        if (typeArgumentProjection != null) {
            typeArguments.put(typeParameter, typeArgumentProjection.getType());
        }
    }
    if (candidateDescriptor.getValueParameters().isEmpty())
        return;
    List<ValueParameterDescriptor> substitutedParameters = resultingDescriptor.getValueParameters();
    Collection<Map.Entry<ValueParameterDescriptor, ResolvedValueArgument>> valueArgumentsBeforeSubstitution = new SmartList<Map.Entry<ValueParameterDescriptor, ResolvedValueArgument>>(valueArguments.entrySet());
    valueArguments.clear();
    for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> entry : valueArgumentsBeforeSubstitution) {
        ValueParameterDescriptor substitutedVersion = substitutedParameters.get(entry.getKey().getIndex());
        assert substitutedVersion != null : entry.getKey();
        valueArguments.put(substitutedVersion, entry.getValue());
    }
    Collection<Map.Entry<ValueArgument, ArgumentMatchImpl>> unsubstitutedArgumentMappings = new SmartList<Map.Entry<ValueArgument, ArgumentMatchImpl>>(argumentToParameterMap.entrySet());
    argumentToParameterMap.clear();
    for (Map.Entry<ValueArgument, ArgumentMatchImpl> entry : unsubstitutedArgumentMappings) {
        ArgumentMatchImpl argumentMatch = entry.getValue();
        ValueParameterDescriptor valueParameterDescriptor = argumentMatch.getValueParameter();
        ValueParameterDescriptor substitutedVersion = substitutedParameters.get(valueParameterDescriptor.getIndex());
        assert substitutedVersion != null : valueParameterDescriptor;
        argumentToParameterMap.put(entry.getKey(), argumentMatch.replaceValueParameter(substitutedVersion));
    }
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) TypeProjection(org.jetbrains.kotlin.types.TypeProjection) ValueArgument(org.jetbrains.kotlin.psi.ValueArgument) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor) SmartList(com.intellij.util.SmartList)

Example 2 with TypeProjection

use of org.jetbrains.kotlin.types.TypeProjection in project kotlin by JetBrains.

the class ForceResolveUtil method forceResolveAllContents.

@Nullable
public static KotlinType forceResolveAllContents(@Nullable KotlinType type) {
    if (type == null)
        return null;
    forceResolveAllContents(type.getAnnotations());
    if (FlexibleTypesKt.isFlexible(type)) {
        forceResolveAllContents(FlexibleTypesKt.asFlexibleType(type).getLowerBound());
        forceResolveAllContents(FlexibleTypesKt.asFlexibleType(type).getUpperBound());
    } else {
        forceResolveAllContents(type.getConstructor());
        for (TypeProjection projection : type.getArguments()) {
            if (!projection.isStarProjection()) {
                forceResolveAllContents(projection.getType());
            }
        }
    }
    return type;
}
Also used : TypeProjection(org.jetbrains.kotlin.types.TypeProjection) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with TypeProjection

use of org.jetbrains.kotlin.types.TypeProjection in project kotlin by JetBrains.

the class CompileTimeConstantUtils method isAcceptableTypeForAnnotationParameter.

private static boolean isAcceptableTypeForAnnotationParameter(@NotNull KotlinType parameterType) {
    ClassDescriptor typeDescriptor = TypeUtils.getClassDescriptor(parameterType);
    if (typeDescriptor == null) {
        return false;
    }
    if (isEnumClass(typeDescriptor) || isAnnotationClass(typeDescriptor) || KotlinBuiltIns.isKClass(typeDescriptor) || KotlinBuiltIns.isPrimitiveArray(parameterType) || KotlinBuiltIns.isPrimitiveType(parameterType) || KotlinBuiltIns.isString(parameterType)) {
        return true;
    }
    if (KotlinBuiltIns.isArray(parameterType)) {
        List<TypeProjection> arguments = parameterType.getArguments();
        if (arguments.size() == 1) {
            KotlinType arrayType = arguments.get(0).getType();
            if (arrayType.isMarkedNullable()) {
                return false;
            }
            ClassDescriptor arrayTypeDescriptor = TypeUtils.getClassDescriptor(arrayType);
            if (arrayTypeDescriptor != null) {
                return isEnumClass(arrayTypeDescriptor) || isAnnotationClass(arrayTypeDescriptor) || KotlinBuiltIns.isKClass(arrayTypeDescriptor) || KotlinBuiltIns.isString(arrayType);
            }
        }
    }
    return false;
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) TypeProjection(org.jetbrains.kotlin.types.TypeProjection) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 4 with TypeProjection

use of org.jetbrains.kotlin.types.TypeProjection in project kotlin by JetBrains.

the class AbstractAnnotationDescriptorResolveTest method getPropertyDescriptor.

@Nullable
protected static PropertyDescriptor getPropertyDescriptor(@NotNull PackageFragmentDescriptor packageView, @NotNull String name, boolean failOnMissing) {
    Name propertyName = Name.identifier(name);
    MemberScope memberScope = packageView.getMemberScope();
    Collection<PropertyDescriptor> properties = memberScope.getContributedVariables(propertyName, NoLookupLocation.FROM_TEST);
    if (properties.isEmpty()) {
        for (DeclarationDescriptor descriptor : DescriptorUtils.getAllDescriptors(memberScope)) {
            if (descriptor instanceof ClassDescriptor) {
                Collection<PropertyDescriptor> classProperties = ((ClassDescriptor) descriptor).getMemberScope(Collections.<TypeProjection>emptyList()).getContributedVariables(propertyName, NoLookupLocation.FROM_TEST);
                if (!classProperties.isEmpty()) {
                    properties = classProperties;
                    break;
                }
            }
        }
    }
    if (failOnMissing) {
        assert properties.size() == 1 : "Failed to find property " + propertyName + " in class " + packageView.getName();
    } else if (properties.size() != 1) {
        return null;
    }
    return properties.iterator().next();
}
Also used : TypeProjection(org.jetbrains.kotlin.types.TypeProjection) MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) FqName(org.jetbrains.kotlin.name.FqName) Name(org.jetbrains.kotlin.name.Name) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with TypeProjection

use of org.jetbrains.kotlin.types.TypeProjection in project kotlin by JetBrains.

the class ExpressionCodegen method getTypeArgumentsForResolvedCall.

@NotNull
private static Map<TypeParameterDescriptor, KotlinType> getTypeArgumentsForResolvedCall(@NotNull ResolvedCall<?> resolvedCall, @NotNull CallableDescriptor descriptor) {
    if (!(descriptor instanceof TypeAliasConstructorDescriptor)) {
        return resolvedCall.getTypeArguments();
    }
    TypeAliasConstructorDescriptor typeAliasConstructorDescriptor = (TypeAliasConstructorDescriptor) descriptor;
    ClassConstructorDescriptor underlyingConstructorDescriptor = typeAliasConstructorDescriptor.getUnderlyingConstructorDescriptor();
    KotlinType resultingType = typeAliasConstructorDescriptor.getReturnType();
    List<TypeProjection> typeArgumentsForReturnType = resultingType.getArguments();
    List<TypeParameterDescriptor> typeParameters = underlyingConstructorDescriptor.getTypeParameters();
    assert typeParameters.size() == typeArgumentsForReturnType.size() : "Type parameters of the underlying constructor " + underlyingConstructorDescriptor + "should correspond to type arguments for the resulting type " + resultingType;
    Map<TypeParameterDescriptor, KotlinType> typeArgumentsMap = Maps.newHashMapWithExpectedSize(typeParameters.size());
    for (TypeParameterDescriptor typeParameter : typeParameters) {
        KotlinType typeArgument = typeArgumentsForReturnType.get(typeParameter.getIndex()).getType();
        typeArgumentsMap.put(typeParameter, typeArgument);
    }
    return typeArgumentsMap;
}
Also used : TypeProjection(org.jetbrains.kotlin.types.TypeProjection) KotlinType(org.jetbrains.kotlin.types.KotlinType) TypeAliasConstructorDescriptor(org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

TypeProjection (org.jetbrains.kotlin.types.TypeProjection)5 Nullable (org.jetbrains.annotations.Nullable)2 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 SmartList (com.intellij.util.SmartList)1 NotNull (org.jetbrains.annotations.NotNull)1 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)1 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)1 ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)1 TypeAliasConstructorDescriptor (org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor)1 FqName (org.jetbrains.kotlin.name.FqName)1 Name (org.jetbrains.kotlin.name.Name)1 ValueArgument (org.jetbrains.kotlin.psi.ValueArgument)1 MemberScope (org.jetbrains.kotlin.resolve.scopes.MemberScope)1