Search in sources :

Example 1 with KtTypeReference

use of org.jetbrains.kotlin.psi.KtTypeReference in project kotlin by JetBrains.

the class PatternTranslator method translateIsExpression.

@NotNull
public JsExpression translateIsExpression(@NotNull KtIsExpression expression) {
    KtExpression left = expression.getLeftHandSide();
    JsExpression expressionToCheck = Translation.translateAsExpression(left, context());
    KotlinType leftType = context().bindingContext().getType(left);
    if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
        expressionToCheck = JsAstUtils.charToBoxedChar(expressionToCheck);
    }
    KtTypeReference typeReference = expression.getTypeReference();
    assert typeReference != null;
    JsExpression result = translateIsCheck(expressionToCheck, typeReference);
    if (result == null)
        return JsLiteral.getBoolean(!expression.isNegated());
    if (expression.isNegated()) {
        return not(result);
    }
    return result;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with KtTypeReference

use of org.jetbrains.kotlin.psi.KtTypeReference in project kotlin by JetBrains.

the class TypeSubstitutorTest method resolveType.

private KotlinType resolveType(String typeStr) {
    KtTypeReference jetTypeReference = KtPsiFactoryKt.KtPsiFactory(getProject()).createType(typeStr);
    AnalyzingUtils.checkForSyntacticErrors(jetTypeReference);
    BindingTrace trace = new BindingTraceContext();
    KotlinType type = container.getTypeResolver().resolveType(scope, jetTypeReference, trace, true);
    if (!trace.getBindingContext().getDiagnostics().isEmpty()) {
        fail("Errors:\n" + StringUtil.join(trace.getBindingContext().getDiagnostics(), new Function<Diagnostic, String>() {

            @Override
            public String fun(Diagnostic diagnostic) {
                return DefaultErrorMessages.render(diagnostic);
            }
        }, "\n"));
    }
    return type;
}
Also used : KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) Diagnostic(org.jetbrains.kotlin.diagnostics.Diagnostic)

Example 3 with KtTypeReference

use of org.jetbrains.kotlin.psi.KtTypeReference in project kotlin by JetBrains.

the class CompileTimeConstantUtils method checkConstructorParametersType.

public static void checkConstructorParametersType(@NotNull List<KtParameter> parameters, @NotNull BindingTrace trace) {
    for (KtParameter parameter : parameters) {
        VariableDescriptor parameterDescriptor = trace.getBindingContext().get(VALUE_PARAMETER, parameter);
        if (parameterDescriptor == null)
            continue;
        KotlinType parameterType = parameterDescriptor.getType();
        KtTypeReference typeReference = parameter.getTypeReference();
        if (typeReference != null) {
            if (parameterType.isMarkedNullable()) {
                trace.report(NULLABLE_TYPE_OF_ANNOTATION_MEMBER.on(typeReference));
            } else if (!isAcceptableTypeForAnnotationParameter(parameterType)) {
                trace.report(INVALID_TYPE_OF_ANNOTATION_MEMBER.on(typeReference));
            }
        }
    }
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) KtParameter(org.jetbrains.kotlin.psi.KtParameter) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) VariableDescriptor(org.jetbrains.kotlin.descriptors.VariableDescriptor)

Example 4 with KtTypeReference

use of org.jetbrains.kotlin.psi.KtTypeReference in project kotlin by JetBrains.

the class PatternTranslator method translateCastExpression.

@NotNull
public JsExpression translateCastExpression(@NotNull KtBinaryExpressionWithTypeRHS expression) {
    assert isCastExpression(expression) : "Expected cast expression, got " + expression;
    KtExpression left = expression.getLeft();
    JsExpression expressionToCast = Translation.translateAsExpression(left, context());
    KtTypeReference typeReference = expression.getRight();
    assert typeReference != null : "Cast expression must have type reference";
    KotlinType leftType = context().bindingContext().getType(left);
    if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
        expressionToCast = JsAstUtils.charToBoxedChar(expressionToCast);
    }
    TemporaryVariable temporary = context().declareTemporary(expressionToCast);
    JsExpression isCheck = translateIsCheck(temporary.assignmentExpression(), typeReference);
    if (isCheck == null)
        return expressionToCast;
    JsExpression onFail;
    if (isSafeCast(expression)) {
        onFail = JsLiteral.NULL;
    } else {
        JsExpression throwCCEFunRef = Namer.throwClassCastExceptionFunRef();
        onFail = new JsInvocation(throwCCEFunRef);
    }
    JsExpression result = new JsConditional(isCheck, temporary.reference(), onFail);
    KotlinType expressionType = context().bindingContext().getType(expression);
    if (expressionType != null && KotlinBuiltIns.isCharOrNullableChar(expressionType)) {
        result = JsAstUtils.boxedCharToChar(result);
    }
    return result;
}
Also used : TemporaryVariable(org.jetbrains.kotlin.js.translate.context.TemporaryVariable) JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsInvocation(org.jetbrains.kotlin.js.backend.ast.JsInvocation) JsConditional(org.jetbrains.kotlin.js.backend.ast.JsConditional) KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with KtTypeReference

use of org.jetbrains.kotlin.psi.KtTypeReference in project kotlin by JetBrains.

the class TypeUnifierTest method makeType.

private TypeProjection makeType(String typeStr) {
    LexicalScope withX = new LexicalScopeImpl(builtinsImportingScope, module, false, null, LexicalScopeKind.SYNTHETIC, LocalRedeclarationChecker.DO_NOTHING.INSTANCE, new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {

        @Override
        public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
            handler.addClassifierDescriptor(x);
            handler.addClassifierDescriptor(y);
            return Unit.INSTANCE;
        }
    });
    KtTypeProjection projection = KtPsiFactoryKt.KtPsiFactory(getProject()).createTypeArguments("<" + typeStr + ">").getArguments().get(0);
    KtTypeReference typeReference = projection.getTypeReference();
    assert typeReference != null;
    KotlinType type = typeResolver.resolveType(withX, typeReference, KotlinTestUtils.DUMMY_TRACE, true);
    return new TypeProjectionImpl(getProjectionKind(typeStr, projection), type);
}
Also used : KtTypeProjection(org.jetbrains.kotlin.psi.KtTypeProjection) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) Unit(kotlin.Unit)

Aggregations

KtTypeReference (org.jetbrains.kotlin.psi.KtTypeReference)5 KotlinType (org.jetbrains.kotlin.types.KotlinType)3 NotNull (org.jetbrains.annotations.NotNull)2 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)2 KtExpression (org.jetbrains.kotlin.psi.KtExpression)2 Unit (kotlin.Unit)1 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)1 Diagnostic (org.jetbrains.kotlin.diagnostics.Diagnostic)1 JsConditional (org.jetbrains.kotlin.js.backend.ast.JsConditional)1 JsInvocation (org.jetbrains.kotlin.js.backend.ast.JsInvocation)1 TemporaryVariable (org.jetbrains.kotlin.js.translate.context.TemporaryVariable)1 KtParameter (org.jetbrains.kotlin.psi.KtParameter)1 KtTypeProjection (org.jetbrains.kotlin.psi.KtTypeProjection)1