use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.
the class PatternTranslator method translateExpressionPattern.
@NotNull
public JsExpression translateExpressionPattern(@NotNull KotlinType type, @NotNull JsExpression expressionToMatch, @NotNull KtExpression patternExpression) {
JsExpression expressionToMatchAgainst = translateExpressionForExpressionPattern(patternExpression);
KotlinType patternType = BindingUtils.getTypeForExpression(bindingContext(), patternExpression);
EqualityType matchEquality = equalityType(type);
EqualityType patternEquality = equalityType(patternType);
if (matchEquality == EqualityType.PRIMITIVE && patternEquality == EqualityType.PRIMITIVE) {
return equality(expressionToMatch, expressionToMatchAgainst);
} else if (expressionToMatchAgainst == JsLiteral.NULL) {
return TranslationUtils.nullCheck(expressionToMatch, false);
} else {
return TopLevelFIF.KOTLIN_EQUALS.apply(expressionToMatch, Collections.singletonList(expressionToMatchAgainst), context());
}
}
use of org.jetbrains.kotlin.types.KotlinType 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.types.KotlinType in project kotlin by JetBrains.
the class PatternTranslator method doGetIsTypeCheckCallable.
@Nullable
private JsExpression doGetIsTypeCheckCallable(@NotNull KotlinType type) {
ClassifierDescriptor targetDescriptor = type.getConstructor().getDeclarationDescriptor();
if (targetDescriptor != null && AnnotationsUtils.isNativeInterface(targetDescriptor)) {
return type.isMarkedNullable() ? null : namer().isInstanceOf(JsAstUtils.pureFqn("Object", null));
}
JsExpression builtinCheck = getIsTypeCheckCallableForBuiltin(type);
if (builtinCheck != null)
return builtinCheck;
builtinCheck = getIsTypeCheckCallableForPrimitiveBuiltin(type);
if (builtinCheck != null)
return builtinCheck;
TypeParameterDescriptor typeParameterDescriptor = getTypeParameterDescriptorOrNull(type);
if (typeParameterDescriptor != null) {
if (typeParameterDescriptor.isReified()) {
return getIsTypeCheckCallableForReifiedType(typeParameterDescriptor);
}
JsExpression result = null;
for (KotlinType upperBound : typeParameterDescriptor.getUpperBounds()) {
JsExpression next = doGetIsTypeCheckCallable(upperBound);
if (next != null) {
result = result != null ? namer().andPredicate(result, next) : next;
}
}
return result;
}
ClassDescriptor referencedClass = DescriptorUtils.getClassDescriptorForType(type);
JsExpression typeName = ReferenceTranslator.translateAsTypeReference(referencedClass, context());
return namer().isInstanceOf(typeName);
}
use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.
the class IntrinsicAssignmentTranslator method translateRightExpression.
private JsExpression translateRightExpression(TranslationContext context, KtBinaryExpression expression) {
JsExpression result = TranslationUtils.translateRightExpression(context, expression, rightBlock);
KotlinType leftType = context.bindingContext().getType(expression.getLeft());
KotlinType rightType = context.bindingContext().getType(expression.getRight());
if (rightType != null && KotlinBuiltIns.isCharOrNullableChar(rightType)) {
if (leftType != null && KotlinBuiltIns.isStringOrNullableString(leftType)) {
result = JsAstUtils.charToString(result);
} else if (leftType == null || !KotlinBuiltIns.isCharOrNullableChar(leftType)) {
result = JsAstUtils.charToBoxedChar(result);
}
}
return result;
}
use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.
the class Translation method translateConstant.
@Nullable
public static JsExpression translateConstant(@NotNull CompileTimeConstant compileTimeValue, @NotNull KtExpression expression, @NotNull TranslationContext context) {
KotlinType expectedType = context.bindingContext().getType(expression);
ConstantValue<?> constant = compileTimeValue.toConstantValue(expectedType != null ? expectedType : TypeUtils.NO_EXPECTED_TYPE);
if (constant instanceof NullValue) {
return JsLiteral.NULL;
}
Object value = constant.getValue();
if (value instanceof Integer || value instanceof Short || value instanceof Byte) {
return context.program().getNumberLiteral(((Number) value).intValue());
} else if (value instanceof Long) {
return JsAstUtils.newLong((Long) value, context);
} else if (value instanceof Float) {
float floatValue = (Float) value;
double doubleValue;
if (Float.isInfinite(floatValue) || Float.isNaN(floatValue)) {
doubleValue = floatValue;
} else {
doubleValue = Double.parseDouble(Float.toString(floatValue));
}
return context.program().getNumberLiteral(doubleValue);
} else if (value instanceof Number) {
return context.program().getNumberLiteral(((Number) value).doubleValue());
} else if (value instanceof Boolean) {
return JsLiteral.getBoolean((Boolean) value);
}
//TODO: test
if (value instanceof String) {
return context.program().getStringLiteral((String) value);
}
if (value instanceof Character) {
return context.program().getNumberLiteral(((Character) value).charValue());
}
return null;
}
Aggregations