Search in sources :

Example 51 with KotlinType

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

the class TranslationUtils method hasCorrespondingFunctionIntrinsic.

public static boolean hasCorrespondingFunctionIntrinsic(@NotNull TranslationContext context, @NotNull KtOperationExpression expression) {
    CallableDescriptor operationDescriptor = getCallableDescriptorForOperationExpression(context.bindingContext(), expression);
    if (operationDescriptor == null || !(operationDescriptor instanceof FunctionDescriptor))
        return true;
    KotlinType returnType = operationDescriptor.getReturnType();
    if (returnType != null && (KotlinBuiltIns.isChar(returnType) || KotlinBuiltIns.isLong(returnType) || KotlinBuiltIns.isInt(returnType))) {
        return false;
    }
    if (context.intrinsics().getFunctionIntrinsic((FunctionDescriptor) operationDescriptor).exists())
        return true;
    return false;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 52 with KotlinType

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

the class QuickFixUtil method getDeclarationReturnType.

@Nullable
public static KotlinType getDeclarationReturnType(KtNamedDeclaration declaration) {
    PsiFile file = declaration.getContainingFile();
    if (!(file instanceof KtFile))
        return null;
    DeclarationDescriptor descriptor = ResolutionUtils.resolveToDescriptor(declaration, BodyResolveMode.FULL);
    if (!(descriptor instanceof CallableDescriptor))
        return null;
    KotlinType type = ((CallableDescriptor) descriptor).getReturnType();
    if (type instanceof DeferredType) {
        type = ((DeferredType) type).getDelegate();
    }
    return type;
}
Also used : DeferredType(org.jetbrains.kotlin.types.DeferredType) DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType) PsiFile(com.intellij.psi.PsiFile) CallableDescriptor(org.jetbrains.kotlin.descriptors.CallableDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 53 with KotlinType

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

the class DescriptorUtils method getSuperclassDescriptors.

@NotNull
public static List<ClassDescriptor> getSuperclassDescriptors(@NotNull ClassDescriptor classDescriptor) {
    Collection<KotlinType> superclassTypes = classDescriptor.getTypeConstructor().getSupertypes();
    List<ClassDescriptor> superClassDescriptors = new ArrayList<ClassDescriptor>();
    for (KotlinType type : superclassTypes) {
        ClassDescriptor result = getClassDescriptorForType(type);
        if (!isAny(result)) {
            superClassDescriptors.add(result);
        }
    }
    return superClassDescriptors;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Example 54 with KotlinType

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

the class SamWrapperCodegen method genWrapper.

@NotNull
public Type genWrapper(@NotNull KtFile file) {
    // Name for generated class, in form of whatever$1
    FqName fqName = getWrapperName(file);
    Type asmType = asmTypeByFqNameWithoutInnerClasses(fqName);
    // e.g. (T, T) -> Int
    KotlinType functionType = samType.getKotlinFunctionType();
    ClassDescriptor classDescriptor = new ClassDescriptorImpl(samType.getJavaClassDescriptor().getContainingDeclaration(), fqName.shortName(), Modality.FINAL, ClassKind.CLASS, Collections.singleton(samType.getType()), SourceElement.NO_SOURCE, /* isExternal = */
    false);
    // e.g. compare(T, T)
    SimpleFunctionDescriptor erasedInterfaceFunction = samType.getAbstractMethod().getOriginal().copy(classDescriptor, Modality.FINAL, Visibilities.PUBLIC, CallableMemberDescriptor.Kind.SYNTHESIZED, /*copyOverrides=*/
    false);
    ClassBuilder cv = state.getFactory().newVisitor(JvmDeclarationOriginKt.OtherOrigin(erasedInterfaceFunction), asmType, file);
    cv.defineClass(file, state.getClassFileVersion(), ACC_FINAL | ACC_SUPER | visibility, asmType.getInternalName(), null, OBJECT_TYPE.getInternalName(), new String[] { typeMapper.mapType(samType.getType()).getInternalName() });
    cv.visitSource(file.getName(), null);
    WriteAnnotationUtilKt.writeSyntheticClassMetadata(cv, state);
    // e.g. ASM type for Function2
    Type functionAsmType = typeMapper.mapType(functionType);
    cv.newField(JvmDeclarationOriginKt.OtherOrigin(erasedInterfaceFunction), ACC_SYNTHETIC | ACC_PRIVATE | ACC_FINAL, FUNCTION_FIELD_NAME, functionAsmType.getDescriptor(), null, null);
    generateConstructor(asmType, functionAsmType, cv);
    generateMethod(asmType, functionAsmType, cv, erasedInterfaceFunction, functionType);
    cv.done();
    return asmType;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) Type(org.jetbrains.org.objectweb.asm.Type) JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) FqName(org.jetbrains.kotlin.name.FqName) KotlinType(org.jetbrains.kotlin.types.KotlinType) ClassDescriptorImpl(org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 55 with KotlinType

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

the class CodegenAnnotatingVisitor method visitCallableReferenceExpression.

@Override
public void visitCallableReferenceExpression(@NotNull KtCallableReferenceExpression expression) {
    ResolvedCall<?> referencedFunction = CallUtilKt.getResolvedCall(expression.getCallableReference(), bindingContext);
    if (referencedFunction == null)
        return;
    CallableDescriptor target = referencedFunction.getResultingDescriptor();
    CallableDescriptor callableDescriptor;
    Collection<KotlinType> supertypes;
    KtExpression receiverExpression = expression.getReceiverExpression();
    KotlinType receiverType = receiverExpression != null ? bindingContext.getType(receiverExpression) : null;
    if (target instanceof FunctionDescriptor) {
        callableDescriptor = bindingContext.get(FUNCTION, expression);
        if (callableDescriptor == null)
            return;
        supertypes = runtimeTypes.getSupertypesForFunctionReference((FunctionDescriptor) target, receiverType != null);
    } else if (target instanceof PropertyDescriptor) {
        callableDescriptor = bindingContext.get(VARIABLE, expression);
        if (callableDescriptor == null)
            return;
        //noinspection ConstantConditions
        supertypes = Collections.singleton(runtimeTypes.getSupertypeForPropertyReference((PropertyDescriptor) target, ReflectionTypes.Companion.isNumberedKMutablePropertyType(callableDescriptor.getReturnType()), receiverType != null));
    } else {
        return;
    }
    String name = inventAnonymousClassName();
    ClassDescriptor classDescriptor = recordClassForCallable(expression, callableDescriptor, supertypes, name);
    MutableClosure closure = recordClosure(classDescriptor, name);
    if (receiverType != null) {
        closure.setCaptureReceiverType(receiverType);
    }
    super.visitCallableReferenceExpression(expression);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType)

Aggregations

KotlinType (org.jetbrains.kotlin.types.KotlinType)110 NotNull (org.jetbrains.annotations.NotNull)34 IElementType (com.intellij.psi.tree.IElementType)16 Type (org.jetbrains.org.objectweb.asm.Type)16 Nullable (org.jetbrains.annotations.Nullable)10 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)7 PsiElement (com.intellij.psi.PsiElement)6 Name (org.jetbrains.kotlin.name.Name)6 ArrayList (java.util.ArrayList)4 KtExpression (org.jetbrains.kotlin.psi.KtExpression)4 Map (java.util.Map)3 BothSignatureWriter (org.jetbrains.kotlin.codegen.signature.BothSignatureWriter)3 JvmSignatureWriter (org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter)3 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)3 LocalVariableDescriptor (org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor)3 DataFlowInfo (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)3 ExpressionReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver)3 PrimitiveType (org.jetbrains.kotlin.builtins.PrimitiveType)2 CallableDescriptor (org.jetbrains.kotlin.descriptors.CallableDescriptor)2 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)2