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