Search in sources :

Example 1 with KotlinBuiltIns

use of org.jetbrains.kotlin.builtins.KotlinBuiltIns in project kotlin by JetBrains.

the class ExpectedResolveDataUtil method prepareDefaultNameToDescriptors.

@NotNull
public static Map<String, DeclarationDescriptor> prepareDefaultNameToDescriptors(@NotNull KotlinCoreEnvironment environment) {
    Project project = environment.getProject();
    KotlinBuiltIns builtIns = DefaultBuiltIns.getInstance();
    Map<String, DeclarationDescriptor> nameToDescriptor = new HashMap<String, DeclarationDescriptor>();
    nameToDescriptor.put("kotlin::Int.plus(Int)", standardFunction(builtIns.getInt(), "plus", project, builtIns.getIntType()));
    FunctionDescriptor descriptorForGet = standardFunction(builtIns.getArray(), "get", project, builtIns.getIntType());
    nameToDescriptor.put("kotlin::Array.get(Int)", descriptorForGet.getOriginal());
    nameToDescriptor.put("kotlin::Int.compareTo(Double)", standardFunction(builtIns.getInt(), "compareTo", project, builtIns.getDoubleType()));
    @NotNull FunctionDescriptor descriptorForSet = standardFunction(builtIns.getArray(), "set", project, builtIns.getIntType(), builtIns.getIntType());
    nameToDescriptor.put("kotlin::Array.set(Int, Int)", descriptorForSet.getOriginal());
    return nameToDescriptor;
}
Also used : Project(com.intellij.openapi.project.Project) KotlinBuiltIns(org.jetbrains.kotlin.builtins.KotlinBuiltIns) Assert.assertNotNull(org.junit.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull) Assert.assertNotNull(org.junit.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with KotlinBuiltIns

use of org.jetbrains.kotlin.builtins.KotlinBuiltIns in project kotlin by JetBrains.

the class TypeUtils method getDefaultPrimitiveNumberType.

@Nullable
public static KotlinType getDefaultPrimitiveNumberType(@NotNull Collection<KotlinType> supertypes) {
    if (supertypes.isEmpty()) {
        return null;
    }
    KotlinBuiltIns builtIns = supertypes.iterator().next().getConstructor().getBuiltIns();
    KotlinType doubleType = builtIns.getDoubleType();
    if (supertypes.contains(doubleType)) {
        return doubleType;
    }
    KotlinType intType = builtIns.getIntType();
    if (supertypes.contains(intType)) {
        return intType;
    }
    KotlinType longType = builtIns.getLongType();
    if (supertypes.contains(longType)) {
        return longType;
    }
    return null;
}
Also used : KotlinBuiltIns(org.jetbrains.kotlin.builtins.KotlinBuiltIns) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with KotlinBuiltIns

use of org.jetbrains.kotlin.builtins.KotlinBuiltIns in project kotlin by JetBrains.

the class CommonSupertypes method commonSuperTypeForInflexible.

@NotNull
private static SimpleType commonSuperTypeForInflexible(@NotNull Collection<SimpleType> types, int recursionDepth, int maxDepth) {
    assert !types.isEmpty();
    Collection<SimpleType> typeSet = new HashSet<SimpleType>(types);
    // If any of the types is nullable, the result must be nullable
    // This also removed Nothing and Nothing? because they are subtypes of everything else
    boolean nullable = false;
    for (Iterator<SimpleType> iterator = typeSet.iterator(); iterator.hasNext(); ) {
        KotlinType type = iterator.next();
        assert type != null;
        assert !FlexibleTypesKt.isFlexible(type) : "Flexible type " + type + " passed to commonSuperTypeForInflexible";
        if (KotlinBuiltIns.isNothingOrNullableNothing(type)) {
            iterator.remove();
        }
        if (type.isError()) {
            return ErrorUtils.createErrorType("Supertype of error type " + type);
        }
        nullable |= type.isMarkedNullable();
    }
    // Everything deleted => it's Nothing or Nothing?
    if (typeSet.isEmpty()) {
        // TODO : attributes
        KotlinBuiltIns builtIns = types.iterator().next().getConstructor().getBuiltIns();
        return nullable ? builtIns.getNullableNothingType() : builtIns.getNothingType();
    }
    if (typeSet.size() == 1) {
        return TypeUtils.makeNullableIfNeeded(typeSet.iterator().next(), nullable);
    }
    // constructor of the supertype -> all of its instantiations occurring as supertypes
    Map<TypeConstructor, Set<SimpleType>> commonSupertypes = computeCommonRawSupertypes(typeSet);
    while (commonSupertypes.size() > 1) {
        Set<SimpleType> merge = new HashSet<SimpleType>();
        for (Set<SimpleType> supertypes : commonSupertypes.values()) {
            merge.addAll(supertypes);
        }
        commonSupertypes = computeCommonRawSupertypes(merge);
    }
    assert !commonSupertypes.isEmpty() : commonSupertypes + " <- " + types;
    // constructor of the supertype -> all of its instantiations occurring as supertypes
    Map.Entry<TypeConstructor, Set<SimpleType>> entry = commonSupertypes.entrySet().iterator().next();
    // Reconstructing type arguments if possible
    SimpleType result = computeSupertypeProjections(entry.getKey(), entry.getValue(), recursionDepth, maxDepth);
    return TypeUtils.makeNullableIfNeeded(result, nullable);
}
Also used : KotlinBuiltIns(org.jetbrains.kotlin.builtins.KotlinBuiltIns) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with KotlinBuiltIns

use of org.jetbrains.kotlin.builtins.KotlinBuiltIns in project kotlin by JetBrains.

the class ImplementationBodyCodegen method generateToArray.

private void generateToArray() {
    if (descriptor.getKind() == ClassKind.INTERFACE)
        return;
    final KotlinBuiltIns builtIns = DescriptorUtilsKt.getBuiltIns(descriptor);
    if (!isSubclass(descriptor, builtIns.getCollection()))
        return;
    if (CollectionsKt.any(DescriptorUtilsKt.getAllSuperclassesWithoutAny(descriptor), new Function1<ClassDescriptor, Boolean>() {

        @Override
        public Boolean invoke(ClassDescriptor classDescriptor) {
            return !(classDescriptor instanceof JavaClassDescriptor) && isSubclass(classDescriptor, builtIns.getCollection());
        }
    }))
        return;
    Collection<SimpleFunctionDescriptor> functions = descriptor.getDefaultType().getMemberScope().getContributedFunctions(Name.identifier("toArray"), NoLookupLocation.FROM_BACKEND);
    boolean hasGenericToArray = false;
    boolean hasNonGenericToArray = false;
    for (FunctionDescriptor function : functions) {
        hasGenericToArray |= isGenericToArray(function);
        hasNonGenericToArray |= isNonGenericToArray(function);
    }
    if (!hasNonGenericToArray) {
        MethodVisitor mv = v.newMethod(NO_ORIGIN, ACC_PUBLIC, "toArray", "()[Ljava/lang/Object;", null, null);
        InstructionAdapter iv = new InstructionAdapter(mv);
        mv.visitCode();
        iv.load(0, classAsmType);
        iv.invokestatic("kotlin/jvm/internal/CollectionToArray", "toArray", "(Ljava/util/Collection;)[Ljava/lang/Object;", false);
        iv.areturn(Type.getType("[Ljava/lang/Object;"));
        FunctionCodegen.endVisit(mv, "toArray", myClass);
    }
    if (!hasGenericToArray) {
        MethodVisitor mv = v.newMethod(NO_ORIGIN, ACC_PUBLIC, "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;", "<T:Ljava/lang/Object;>([TT;)[TT;", null);
        InstructionAdapter iv = new InstructionAdapter(mv);
        mv.visitCode();
        iv.load(0, classAsmType);
        iv.load(1, Type.getType("[Ljava/lang/Object;"));
        iv.invokestatic("kotlin/jvm/internal/CollectionToArray", "toArray", "(Ljava/util/Collection;[Ljava/lang/Object;)[Ljava/lang/Object;", false);
        iv.areturn(Type.getType("[Ljava/lang/Object;"));
        FunctionCodegen.endVisit(mv, "toArray", myClass);
    }
}
Also used : JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) KotlinBuiltIns(org.jetbrains.kotlin.builtins.KotlinBuiltIns) JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 5 with KotlinBuiltIns

use of org.jetbrains.kotlin.builtins.KotlinBuiltIns in project kotlin by JetBrains.

the class MemberMatching method typeParametersMatch.

static boolean typeParametersMatch(@NotNull KtTypeParameterListOwner typeParameterListOwner, @NotNull List<TypeParameterDescriptor> typeParameterDescriptors) {
    List<KtTypeParameter> decompiledParameters = typeParameterListOwner.getTypeParameters();
    if (decompiledParameters.size() != typeParameterDescriptors.size()) {
        return false;
    }
    Multimap<Name, String> decompiledParameterToBounds = HashMultimap.create();
    for (KtTypeParameter parameter : decompiledParameters) {
        KtTypeReference extendsBound = parameter.getExtendsBound();
        if (extendsBound != null) {
            decompiledParameterToBounds.put(parameter.getNameAsName(), extendsBound.getText());
        }
    }
    for (KtTypeConstraint typeConstraint : typeParameterListOwner.getTypeConstraints()) {
        KtSimpleNameExpression typeParameterName = typeConstraint.getSubjectTypeParameterName();
        assert typeParameterName != null;
        KtTypeReference bound = typeConstraint.getBoundTypeReference();
        assert bound != null;
        decompiledParameterToBounds.put(typeParameterName.getReferencedNameAsName(), bound.getText());
    }
    for (int i = 0; i < decompiledParameters.size(); i++) {
        KtTypeParameter decompiledParameter = decompiledParameters.get(i);
        TypeParameterDescriptor descriptor = typeParameterDescriptors.get(i);
        Name name = decompiledParameter.getNameAsName();
        assert name != null;
        if (!name.equals(descriptor.getName())) {
            return false;
        }
        Set<String> descriptorUpperBounds = Sets.newHashSet(ContainerUtil.map(descriptor.getUpperBounds(), new Function<KotlinType, String>() {

            @Override
            public String fun(KotlinType type) {
                return DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(type);
            }
        }));
        KotlinBuiltIns builtIns = DescriptorUtilsKt.getBuiltIns(descriptor);
        Set<String> decompiledUpperBounds = decompiledParameterToBounds.get(descriptor.getName()).isEmpty() ? Sets.newHashSet(DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(builtIns.getDefaultBound())) : Sets.newHashSet(decompiledParameterToBounds.get(descriptor.getName()));
        if (!descriptorUpperBounds.equals(decompiledUpperBounds)) {
            return false;
        }
    }
    return true;
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType) Name(org.jetbrains.kotlin.name.Name) Function(com.intellij.util.Function) KotlinBuiltIns(org.jetbrains.kotlin.builtins.KotlinBuiltIns)

Aggregations

KotlinBuiltIns (org.jetbrains.kotlin.builtins.KotlinBuiltIns)5 NotNull (org.jetbrains.annotations.NotNull)2 Project (com.intellij.openapi.project.Project)1 Function (com.intellij.util.Function)1 Nullable (org.jetbrains.annotations.Nullable)1 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)1 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)1 Name (org.jetbrains.kotlin.name.Name)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)1 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1