use of org.jetbrains.kotlin.types.KotlinType 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.types.KotlinType in project kotlin by JetBrains.
the class PatternTranslator method translateIsCheck.
@Nullable
public JsExpression translateIsCheck(@NotNull JsExpression subject, @NotNull KtTypeReference targetTypeReference) {
KotlinType targetType = getTypeByReference(bindingContext(), targetTypeReference);
JsExpression checkFunReference = doGetIsTypeCheckCallable(targetType);
if (checkFunReference == null) {
return null;
}
boolean isReifiedType = isReifiedTypeParameter(targetType);
if (!isReifiedType && isNullableType(targetType) || isReifiedType && findChildByType(targetTypeReference, KtNodeTypes.NULLABLE_TYPE) != null) {
checkFunReference = namer().orNull(checkFunReference);
}
return new JsInvocation(checkFunReference, subject);
}
use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.
the class TranslationUtils method translateInitializerForProperty.
@Nullable
public static JsExpression translateInitializerForProperty(@NotNull KtProperty declaration, @NotNull TranslationContext context) {
JsExpression jsInitExpression = null;
KtExpression initializer = declaration.getInitializer();
if (initializer != null) {
jsInitExpression = Translation.translateAsExpression(initializer, context);
KotlinType propertyType = BindingContextUtils.getNotNull(context.bindingContext(), BindingContext.VARIABLE, declaration).getType();
KotlinType initType = context.bindingContext().getType(initializer);
if (initType != null && KotlinBuiltIns.isCharOrNullableChar(initType) && !KotlinBuiltIns.isCharOrNullableChar(propertyType)) {
jsInitExpression = JsAstUtils.charToBoxedChar(jsInitExpression);
}
}
return jsInitExpression;
}
use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.
the class AsmUtil method genParamAssertion.
private static void genParamAssertion(@NotNull InstructionAdapter v, @NotNull KotlinTypeMapper typeMapper, @NotNull FrameMap frameMap, @NotNull CallableDescriptor parameter, @NotNull String name) {
KotlinType type = parameter.getReturnType();
if (type == null || isNullableType(type))
return;
int index = frameMap.getIndex(parameter);
Type asmType = typeMapper.mapType(type);
if (asmType.getSort() == Type.OBJECT || asmType.getSort() == Type.ARRAY) {
v.load(index, asmType);
v.visitLdcInsn(name);
v.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "checkParameterIsNotNull", "(Ljava/lang/Object;Ljava/lang/String;)V", false);
}
}
use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.
the class ClosureCodegen method calculateConstructorParameters.
@NotNull
public static List<FieldInfo> calculateConstructorParameters(@NotNull KotlinTypeMapper typeMapper, @NotNull CalculatedClosure closure, @NotNull Type ownerType) {
List<FieldInfo> args = Lists.newArrayList();
ClassDescriptor captureThis = closure.getCaptureThis();
if (captureThis != null) {
Type type = typeMapper.mapType(captureThis);
args.add(FieldInfo.createForHiddenField(ownerType, type, CAPTURED_THIS_FIELD));
}
KotlinType captureReceiverType = closure.getCaptureReceiverType();
if (captureReceiverType != null) {
args.add(FieldInfo.createForHiddenField(ownerType, typeMapper.mapType(captureReceiverType), CAPTURED_RECEIVER_FIELD));
}
for (EnclosedValueDescriptor enclosedValueDescriptor : closure.getCaptureVariables().values()) {
DeclarationDescriptor descriptor = enclosedValueDescriptor.getDescriptor();
if ((descriptor instanceof VariableDescriptor && !(descriptor instanceof PropertyDescriptor)) || ExpressionTypingUtils.isLocalFunction(descriptor)) {
args.add(FieldInfo.createForHiddenField(ownerType, enclosedValueDescriptor.getType(), enclosedValueDescriptor.getFieldName()));
} else if (descriptor instanceof FunctionDescriptor) {
assert captureReceiverType != null;
}
}
return args;
}
Aggregations