From 5e4b39f10f27f1c916ae5bdb5e5076facdd1fcc0 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Mon, 29 Oct 2018 12:47:08 +0300 Subject: [PATCH] New J2K: Add calculating type for JKExpression --- .../org/jetbrains/kotlin/j2k/tree/impl/jk.kt | 17 +++++++++ .../org/jetbrains/kotlin/j2k/tree/types.kt | 35 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 j2k/newSrc/org/jetbrains/kotlin/j2k/tree/types.kt diff --git a/j2k/newSrc/org/jetbrains/kotlin/j2k/tree/impl/jk.kt b/j2k/newSrc/org/jetbrains/kotlin/j2k/tree/impl/jk.kt index fa958e2223b..1be530428cf 100644 --- a/j2k/newSrc/org/jetbrains/kotlin/j2k/tree/impl/jk.kt +++ b/j2k/newSrc/org/jetbrains/kotlin/j2k/tree/impl/jk.kt @@ -17,6 +17,8 @@ package org.jetbrains.kotlin.j2k.tree.impl import com.intellij.psi.PsiClass +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.j2k.JKSymbolProvider import org.jetbrains.kotlin.j2k.ast.Mutability import org.jetbrains.kotlin.j2k.ast.Nullability import org.jetbrains.kotlin.j2k.tree.* @@ -24,6 +26,7 @@ import org.jetbrains.kotlin.j2k.tree.JKLiteralExpression.LiteralType import org.jetbrains.kotlin.j2k.tree.JKLiteralExpression.LiteralType.BOOLEAN import org.jetbrains.kotlin.j2k.tree.JKLiteralExpression.LiteralType.NULL import org.jetbrains.kotlin.j2k.tree.visitors.JKVisitor +import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.psi.KtClass class JKFileImpl : JKFile, JKBranchElementBase() { @@ -233,6 +236,20 @@ class JKBooleanLiteral(val value: Boolean) : JKLiteralExpression, JKElementBase( override fun accept(visitor: JKVisitor, data: D): R = visitor.visitLiteralExpression(this, data) } +fun JKLiteralExpression.LiteralType.toJkType(symbolProvider: JKSymbolProvider): JKType = + when (this) { + JKLiteralExpression.LiteralType.CHAR -> JKJavaPrimitiveTypeImpl.CHAR + JKLiteralExpression.LiteralType.BOOLEAN -> JKJavaPrimitiveTypeImpl.BOOLEAN + JKLiteralExpression.LiteralType.INT -> JKJavaPrimitiveTypeImpl.INT + JKLiteralExpression.LiteralType.LONG -> JKJavaPrimitiveTypeImpl.LONG + JKLiteralExpression.LiteralType.FLOAT -> JKJavaPrimitiveTypeImpl.FLOAT + JKLiteralExpression.LiteralType.DOUBLE -> JKJavaPrimitiveTypeImpl.DOUBLE + JKLiteralExpression.LiteralType.NULL -> + ClassId.topLevel(KotlinBuiltIns.FQ_NAMES.unit.toSafe()).toKtClassType(symbolProvider) + JKLiteralExpression.LiteralType.STRING -> + ClassId.topLevel(KotlinBuiltIns.FQ_NAMES.string.toSafe()).toKtClassType(symbolProvider) + } + class JKLocalVariableImpl(modifierList: JKModifierList, type: JKTypeElement, name: JKNameIdentifier, initializer: JKExpression) : JKLocalVariable, JKBranchElementBase() { override var modifierList by child(modifierList) diff --git a/j2k/newSrc/org/jetbrains/kotlin/j2k/tree/types.kt b/j2k/newSrc/org/jetbrains/kotlin/j2k/tree/types.kt new file mode 100644 index 00000000000..76b78dce044 --- /dev/null +++ b/j2k/newSrc/org/jetbrains/kotlin/j2k/tree/types.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.j2k.tree + +import com.intellij.psi.PsiCatchSection +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.j2k.ConversionContext +import org.jetbrains.kotlin.j2k.JKSymbolProvider +import org.jetbrains.kotlin.j2k.ast.Nullability +import org.jetbrains.kotlin.j2k.conversions.resolveFqName +import org.jetbrains.kotlin.j2k.tree.impl.JKClassSymbol +import org.jetbrains.kotlin.j2k.tree.impl.JKClassTypeImpl +import org.jetbrains.kotlin.j2k.tree.impl.toJkType +import org.jetbrains.kotlin.name.ClassId + + +fun JKExpression.type(context: ConversionContext): JKType = + when (this) { + is JKLiteralExpression -> this.type.toJkType(context.symbolProvider) + else -> TODO(this.toString()) + } + +fun ClassId.toKtClassType( + symbolProvider: JKSymbolProvider, + nullability: Nullability = Nullability.Default, + context: PsiElement = symbolProvider.symbolsByPsi.keys.first() +): JKType { + val typeSymbol = symbolProvider.provideDirectSymbol(resolveFqName(this, context)!!) as JKClassSymbol + return JKClassTypeImpl(typeSymbol, emptyList(), nullability) +} +