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;
}
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;
}
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);
}
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);
}
}
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;
}
Aggregations