Search in sources :

Example 81 with KotlinType

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

the class TopLevelFIF method getReferenceToOnlyTypeParameter.

private static JsExpression getReferenceToOnlyTypeParameter(@NotNull CallInfo callInfo, @NotNull TranslationContext context) {
    ResolvedCall<? extends CallableDescriptor> resolvedCall = callInfo.getResolvedCall();
    Map<TypeParameterDescriptor, KotlinType> typeArguments = resolvedCall.getTypeArguments();
    assert typeArguments.size() == 1;
    KotlinType type = typeArguments.values().iterator().next();
    return UtilsKt.getReferenceToJsClass(type, context);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 82 with KotlinType

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

the class BinaryOperationTranslator method translateEquals.

private JsExpression translateEquals() {
    JsExpression left = Translation.translateAsExpression(leftKtExpression, context());
    JsExpression right = Translation.translateAsExpression(rightKtExpression, context());
    if (left == JsLiteral.NULL || right == JsLiteral.NULL) {
        JsBinaryOperator operator = operationToken == KtTokens.EXCLEQ ? JsBinaryOperator.NEQ : JsBinaryOperator.EQ;
        return new JsBinaryOperation(operator, left, right);
    }
    KotlinType leftType = context().bindingContext().getType(leftKtExpression);
    KotlinType rightType = context().bindingContext().getType(rightKtExpression);
    if (leftType != null && TypeUtils.isNullableType(leftType) || rightType != null && TypeUtils.isNullableType(rightType)) {
        return mayBeWrapWithNegation(TopLevelFIF.KOTLIN_EQUALS.apply(left, Collections.singletonList(right), context()));
    }
    return translateAsOverloadedBinaryOperation();
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 83 with KotlinType

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

the class StaticContext method buildQualifiedExpression.

@NotNull
private JsExpression buildQualifiedExpression(@NotNull DeclarationDescriptor descriptor) {
    if (descriptor instanceof ClassDescriptor) {
        ClassDescriptor classDescriptor = (ClassDescriptor) descriptor;
        KotlinType type = classDescriptor.getDefaultType();
        if (KotlinBuiltIns.isAny(classDescriptor)) {
            return pureFqn("Object", null);
        } else if (KotlinBuiltIns.isInt(type) || KotlinBuiltIns.isShort(type) || KotlinBuiltIns.isByte(type) || KotlinBuiltIns.isFloat(type) || KotlinBuiltIns.isDouble(type)) {
            return pureFqn("Number", null);
        } else if (KotlinBuiltIns.isLong(type)) {
            return pureFqn("Long", Namer.kotlinObject());
        } else if (KotlinBuiltIns.isChar(type)) {
            return pureFqn("BoxedChar", Namer.kotlinObject());
        } else if (KotlinBuiltIns.isString(type)) {
            return pureFqn("String", null);
        } else if (KotlinBuiltIns.isBoolean(type)) {
            return pureFqn("Boolean", null);
        } else if (KotlinBuiltIns.isArrayOrPrimitiveArray(classDescriptor)) {
            return pureFqn("Array", null);
        } else if (FunctionTypesKt.isBuiltinFunctionalType(type)) {
            return pureFqn("Function", null);
        } else if (TypeUtilsKt.isThrowable(classDescriptor.getDefaultType())) {
            return pureFqn("Error", null);
        }
    }
    SuggestedName suggested = nameSuggestion.suggest(descriptor);
    if (suggested == null) {
        ModuleDescriptor module = DescriptorUtils.getContainingModule(descriptor);
        JsExpression result = getModuleExpressionFor(module);
        return result != null ? result : pureFqn(Namer.getRootPackageName(), null);
    }
    if (config.getModuleKind() != ModuleKind.PLAIN) {
        String moduleName = AnnotationsUtils.getModuleName(suggested.getDescriptor());
        if (moduleName != null) {
            return JsAstUtils.pureFqn(getImportedModule(moduleName, suggested.getDescriptor()).internalName, null);
        }
    }
    JsExpression expression;
    List<JsName> partNames = getActualNameFromSuggested(suggested);
    if (isLibraryObject(suggested.getDescriptor())) {
        expression = Namer.kotlinObject();
    } else // Don't generate qualifier for local declarations
    if (isNativeObject(suggested.getDescriptor()) && !isNativeObject(suggested.getScope()) || suggested.getDescriptor() instanceof CallableDescriptor && suggested.getScope() instanceof FunctionDescriptor) {
        expression = null;
    } else {
        expression = getQualifiedExpression(suggested.getScope());
    }
    if (isNativeObject(suggested.getDescriptor()) && DescriptorUtils.isTopLevelDeclaration(suggested.getDescriptor())) {
        String fileModuleName = AnnotationsUtils.getFileModuleName(getBindingContext(), suggested.getDescriptor());
        if (fileModuleName != null) {
            JsName moduleJsName = getImportedModule(fileModuleName, null).internalName;
            expression = pureFqn(moduleJsName, expression);
        }
        String qualifier = AnnotationsUtils.getFileQualifier(getBindingContext(), suggested.getDescriptor());
        if (qualifier != null) {
            for (String qualifierPart : StringUtil.split(qualifier, ".")) {
                expression = pureFqn(qualifierPart, expression);
            }
        }
    }
    for (JsName partName : partNames) {
        expression = new JsNameRef(partName, expression);
        applySideEffects(expression, suggested.getDescriptor());
    }
    assert expression != null : "Since partNames is not empty, expression must be non-null";
    return expression;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) SuggestedName(org.jetbrains.kotlin.js.naming.SuggestedName) NotNull(org.jetbrains.annotations.NotNull)

Example 84 with KotlinType

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

the class ExpectedResolveDataUtil method standardFunction.

@NotNull
private static FunctionDescriptor standardFunction(ClassDescriptor classDescriptor, String name, Project project, KotlinType... parameterTypes) {
    ModuleDescriptorImpl emptyModule = KotlinTestUtils.createEmptyModule();
    ContainerForTests container = InjectionKt.createContainerForTests(project, emptyModule);
    emptyModule.setDependencies(emptyModule);
    emptyModule.initialize(PackageFragmentProvider.Empty.INSTANCE);
    LexicalScopeImpl lexicalScope = new LexicalScopeImpl(ImportingScope.Empty.INSTANCE, classDescriptor, false, classDescriptor.getThisAsReceiverParameter(), LexicalScopeKind.SYNTHETIC);
    ExpressionTypingContext context = ExpressionTypingContext.newContext(new BindingTraceContext(), lexicalScope, DataFlowInfoFactory.EMPTY, TypeUtils.NO_EXPECTED_TYPE);
    KtExpression callElement = KtPsiFactory(project).createExpression(name);
    TemporaryBindingTrace traceWithFakeArgumentInfo = TemporaryBindingTrace.create(context.trace, "trace to store fake argument for", name);
    List<KtExpression> fakeArguments = new ArrayList<KtExpression>(parameterTypes.length);
    for (KotlinType type : parameterTypes) {
        fakeArguments.add(ExpressionTypingUtils.createFakeExpressionOfType(project, traceWithFakeArgumentInfo, "fakeArgument" + fakeArguments.size(), type));
    }
    OverloadResolutionResults<FunctionDescriptor> functions = container.getFakeCallResolver().resolveFakeCall(context, null, Name.identifier(name), callElement, callElement, FakeCallKind.OTHER, fakeArguments);
    for (ResolvedCall<? extends FunctionDescriptor> resolvedCall : functions.getResultingCalls()) {
        List<ValueParameterDescriptor> unsubstitutedValueParameters = resolvedCall.getResultingDescriptor().getValueParameters();
        for (int i = 0, unsubstitutedValueParametersSize = unsubstitutedValueParameters.size(); i < unsubstitutedValueParametersSize; i++) {
            ValueParameterDescriptor unsubstitutedValueParameter = unsubstitutedValueParameters.get(i);
            if (unsubstitutedValueParameter.getType().equals(parameterTypes[i])) {
                return resolvedCall.getResultingDescriptor();
            }
        }
    }
    throw new IllegalArgumentException("Not found: kotlin::" + classDescriptor.getName() + "." + name + "(" + Arrays.toString(parameterTypes) + ")");
}
Also used : LexicalScopeImpl(org.jetbrains.kotlin.resolve.scopes.LexicalScopeImpl) KotlinType(org.jetbrains.kotlin.types.KotlinType) ContainerForTests(org.jetbrains.kotlin.tests.di.ContainerForTests) ModuleDescriptorImpl(org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl) ExpressionTypingContext(org.jetbrains.kotlin.types.expressions.ExpressionTypingContext) KtExpression(org.jetbrains.kotlin.psi.KtExpression) Assert.assertNotNull(org.junit.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 85 with KotlinType

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

the class TypeReconstructionUtil method reconstructBareType.

@NotNull
public static KotlinType reconstructBareType(@NotNull KtTypeReference right, @NotNull PossiblyBareType possiblyBareTarget, @Nullable KotlinType subjectType, @NotNull BindingTrace trace, @NotNull KotlinBuiltIns builtIns) {
    if (subjectType == null) {
        // Recovery: let's reconstruct as if we were casting from Any, to get some type there
        subjectType = builtIns.getAnyType();
    }
    TypeReconstructionResult reconstructionResult = possiblyBareTarget.reconstruct(subjectType);
    if (!reconstructionResult.isAllArgumentsInferred()) {
        TypeConstructor typeConstructor = possiblyBareTarget.getBareTypeConstructor();
        trace.report(NO_TYPE_ARGUMENTS_ON_RHS.on(right, typeConstructor.getParameters().size(), allStarProjectionsString(typeConstructor)));
    }
    KotlinType targetType = reconstructionResult.getResultingType();
    if (targetType != null) {
        if (possiblyBareTarget.isBare()) {
            trace.record(BindingContext.TYPE, right, targetType);
        }
        return targetType;
    }
    return ErrorUtils.createErrorType("Failed to reconstruct type: " + right.getText());
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) TypeReconstructionResult(org.jetbrains.kotlin.types.TypeReconstructionResult) TypeConstructor(org.jetbrains.kotlin.types.TypeConstructor) NotNull(org.jetbrains.annotations.NotNull)

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