diff --git a/build.gradle.kts b/build.gradle.kts index e5780179dbc..6e02974c1ed 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -88,6 +88,7 @@ val commonLocalDataDir = "$rootDir/local" val ideaSandboxDir = "$commonLocalDataDir/ideaSandbox" val ideaUltimateSandboxDir = "$commonLocalDataDir/ideaUltimateSandbox" val ideaPluginDir = "$distDir/artifacts/ideaPlugin/Kotlin" +val serialPluginDir = "$distDir/artifacts/Serialization" val ideaUltimatePluginDir = "$distDir/artifacts/ideaUltimatePlugin/Kotlin" val cidrPluginDir = "$distDir/artifacts/cidrPlugin/Kotlin" @@ -98,6 +99,7 @@ extra["commonLocalDataDir"] = project.file(commonLocalDataDir) extra["ideaSandboxDir"] = project.file(ideaSandboxDir) extra["ideaUltimateSandboxDir"] = project.file(ideaUltimateSandboxDir) extra["ideaPluginDir"] = project.file(ideaPluginDir) +extra["serialPluginDir"] = project.file(serialPluginDir) extra["ideaUltimatePluginDir"] = project.file(ideaUltimatePluginDir) extra["cidrPluginDir"] = project.file(cidrPluginDir) extra["isSonatypeRelease"] = false diff --git a/idea-runner/build.gradle.kts b/idea-runner/build.gradle.kts index 2c211fb34d4..ea46a797bb4 100644 --- a/idea-runner/build.gradle.kts +++ b/idea-runner/build.gradle.kts @@ -11,7 +11,7 @@ dependencies { compileOnly(project(":idea:idea-jvm")) compile(intellijDep()) - + runtimeOnly(files(toolsJar())) } @@ -19,5 +19,6 @@ val ideaPluginDir: File by rootProject.extra val ideaSandboxDir: File by rootProject.extra runIdeTask("runIde", ideaPluginDir, ideaSandboxDir) { - dependsOn(":dist", ":ideaPlugin") + // TODO: add serialization plugin to pluginDir + dependsOn(":dist", ":ideaPlugin", ":kotlinx-serialization-compiler-plugin:dist") } diff --git a/libraries/tools/kotlin-maven-serialization/pom.xml b/libraries/tools/kotlin-maven-serialization/pom.xml index a3bbcc61ed8..33098bdf480 100755 --- a/libraries/tools/kotlin-maven-serialization/pom.xml +++ b/libraries/tools/kotlin-maven-serialization/pom.xml @@ -11,14 +11,13 @@ ${basedir}/src/main/resources ${basedir}/target/src/main/kotlin ${basedir}/target/resource - 1.1-SNAPSHOT + 1.2-SNAPSHOT org.jetbrains.kotlinx kotlinx-maven-serialization-plugin jar - 0.1.1 - + 0.2 Serialization plugin for Maven @@ -33,6 +32,7 @@ org.jetbrains.kotlin kotlin-stdlib ${kotlin.version} + provided org.jetbrains.kotlin diff --git a/libraries/tools/kotlin-serialization/build.gradle b/libraries/tools/kotlin-serialization/build.gradle index bcb8b3fbaf6..a356756a6d6 100644 --- a/libraries/tools/kotlin-serialization/build.gradle +++ b/libraries/tools/kotlin-serialization/build.gradle @@ -2,12 +2,16 @@ plugins { id "com.jfrog.bintray" version "1.7.3" } -group = 'org.jetbrains.kotlinx' -version = '0.1.1' - apply plugin: 'kotlin' configureJvmProject(project) +configurePublishing(project) + +group = 'org.jetbrains.kotlinx' +version = '0.2' +if (!project.hasProperty("deploy")) { + version = "$version-SNAPSHOT" +} compileJava { sourceCompatibility = 1.8 @@ -24,55 +28,35 @@ repositories { dependencies { compileOnly project(':kotlin-gradle-plugin-api') - compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - compile "org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlin_version" + compileOnly project(':kotlin-stdlib') + compileOnly project(path: ':kotlin-compiler-embeddable', configuration: 'runtimeJar') + compileOnly project(':kotlin-allopen-compiler-plugin') compileOnly 'org.jetbrains.kotlin:gradle-api:1.6' } -def originalSrc = "$kotlin_root/plugins/kotlin-serialization/kotlin-serialization-compiler/src" -def targetSrc = file("$buildDir/kotlin-serialization-target-src") - -task preprocessSources(type: Copy) { - from originalSrc - into targetSrc - filter { it.replaceAll('(?().archivesBaseName.removePrefix("kotlin-") + ".jar") + +ideaPlugin { + from(jar) + rename("^kotlin-", "") +} diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/gradle/wrapper/gradle-wrapper.jar b/plugins/kotlin-serialization/kotlin-serialization-compiler/gradle/wrapper/gradle-wrapper.jar deleted file mode 100644 index 9553fe277ab..00000000000 Binary files a/plugins/kotlin-serialization/kotlin-serialization-compiler/gradle/wrapper/gradle-wrapper.jar and /dev/null differ diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/gradle/wrapper/gradle-wrapper.properties b/plugins/kotlin-serialization/kotlin-serialization-compiler/gradle/wrapper/gradle-wrapper.properties deleted file mode 100644 index 5c80a8d804b..00000000000 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/gradle/wrapper/gradle-wrapper.properties +++ /dev/null @@ -1,6 +0,0 @@ -#Mon Jul 31 13:14:07 MSK 2017 -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/gradlew b/plugins/kotlin-serialization/kotlin-serialization-compiler/gradlew deleted file mode 100755 index 4453ccea33d..00000000000 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/gradlew +++ /dev/null @@ -1,172 +0,0 @@ -#!/usr/bin/env sh - -############################################################################## -## -## Gradle start up script for UN*X -## -############################################################################## - -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null - -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" - -# Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" - -warn ( ) { - echo "$*" -} - -die ( ) { - echo - echo "$*" - echo - exit 1 -} - -# OS specific support (must be 'true' or 'false'). -cygwin=false -msys=false -darwin=false -nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; -esac - -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar - -# Determine the Java command to use to start the JVM. -if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" - else - JAVACMD="$JAVA_HOME/bin/java" - fi - if [ ! -x "$JAVACMD" ] ; then - die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." - fi -else - JAVACMD="java" - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." -fi - -# Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi -fi - -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi - -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi - # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" - fi - i=$((i+1)) - done - case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac -fi - -# Escape application args -save ( ) { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=$(save "$@") - -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" - -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" -fi - -exec "$JAVACMD" "$@" diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/gradlew.bat b/plugins/kotlin-serialization/kotlin-serialization-compiler/gradlew.bat deleted file mode 100644 index e95643d6a2c..00000000000 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/gradlew.bat +++ /dev/null @@ -1,84 +0,0 @@ -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/kotlin-serialization-compiler.iml b/plugins/kotlin-serialization/kotlin-serialization-compiler/kotlin-serialization-compiler.iml deleted file mode 100644 index 42883528a12..00000000000 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/kotlin-serialization-compiler.iml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/settings.gradle b/plugins/kotlin-serialization/kotlin-serialization-compiler/settings.gradle deleted file mode 100644 index 82367d8bce4..00000000000 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/settings.gradle +++ /dev/null @@ -1 +0,0 @@ -rootProject.name='serialization-plugin' diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/SerializerCodegen.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/SerializerCodegen.kt index 33e7d557212..e217f52e317 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/SerializerCodegen.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/SerializerCodegen.kt @@ -38,10 +38,10 @@ abstract class SerializerCodegen(declaration: KtPureClassOrObject, bindingContex fun generate() { check(properties.isExternallySerializable) { "Class ${serializableDescriptor.name} is not externally serializable" } - generateSerializableClassPropertyIfNeeded() + val prop = generateSerializableClassPropertyIfNeeded() val save = generateSaveIfNeeded() val load = generateLoadIfNeeded() - if (save || load) + if (save || load || prop) generateSerialDesc() } @@ -55,10 +55,11 @@ abstract class SerializerCodegen(declaration: KtPureClassOrObject, bindingContex protected abstract fun generateLoad(function: FunctionDescriptor) - private fun generateSerializableClassPropertyIfNeeded() { + private fun generateSerializableClassPropertyIfNeeded(): Boolean { val property = serialDescPropertyDescriptor - ?: return + ?: return false generateSerializableClassProperty(property) + return true } private fun generateSaveIfNeeded(): Boolean { diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/TypeUtil.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/TypeUtil.kt index 436624024a4..b5414c51ca9 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/TypeUtil.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/TypeUtil.kt @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.typeUtil.containsTypeProjectionsInTopLevelArguments import org.jetbrains.kotlin.types.typeUtil.isBoolean import org.jetbrains.kotlin.types.typeUtil.isPrimitiveNumberType +import org.jetbrains.kotlinx.serialization.compiler.backend.jvm.contextSerializerId import org.jetbrains.kotlinx.serialization.compiler.backend.jvm.enumSerializerId import org.jetbrains.kotlinx.serialization.compiler.backend.jvm.polymorphicSerializerId import org.jetbrains.kotlinx.serialization.compiler.backend.jvm.referenceArraySerializerId @@ -52,12 +53,12 @@ fun getSerialTypeInfo(property: SerializableProperty): SerialTypeInfo { KotlinBuiltIns.isUnit(T) -> SerialTypeInfo(property, "Unit", unit = true) KotlinBuiltIns.isPrimitiveArray(T) -> TODO("primitive arrays are not supported yet") KotlinBuiltIns.isNonPrimitiveArray(T.toClassDescriptor!!) -> { - val serializer = property.serializer?.toClassDescriptor ?: + val serializer = property.serializableWith?.toClassDescriptor ?: property.module.findClassAcrossModuleDependencies(referenceArraySerializerId) SerialTypeInfo(property, if (property.type.isMarkedNullable) "Nullable" else "", serializer) } T.toClassDescriptor?.kind == ClassKind.ENUM_CLASS -> { - val serializer = property.serializer?.toClassDescriptor ?: + val serializer = property.serializableWith?.toClassDescriptor ?: property.module.findClassAcrossModuleDependencies(enumSerializerId) SerialTypeInfo(property, if (property.type.isMarkedNullable) "Nullable" else "", serializer) } @@ -73,6 +74,7 @@ fun findTypeSerializer(module: ModuleDescriptor, kType: KotlinType): ClassDescri else kType.typeSerializer.toClassDescriptor // check for serializer defined on the type ?: findStandardKotlinTypeSerializer(module, kType) // otherwise see if there is a standard serializer ?: findEnumTypeSerializer(module, kType) + ?: module.findClassAcrossModuleDependencies(contextSerializerId) } fun findStandardKotlinTypeSerializer(module: ModuleDescriptor, kType: KotlinType): ClassDescriptor? { @@ -105,7 +107,7 @@ fun findEnumTypeSerializer(module: ModuleDescriptor, kType: KotlinType): ClassDe fun KotlinType.requiresPolymorphism(): Boolean { return this.toClassDescriptor?.getSuperClassNotAny()?.isInternalSerializable == true - || this.toClassDescriptor?.modality == Modality.OPEN + || (this.toClassDescriptor?.modality == Modality.OPEN && this.toClassDescriptor?.unsubstitutedPrimaryConstructor != null) // open not java class || this.containsTypeProjectionsInTopLevelArguments() // List<*> } diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/js/SerializerJsTranslator.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/js/SerializerJsTranslator.kt index 98385cc2370..5bb53d9c74b 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/js/SerializerJsTranslator.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/js/SerializerJsTranslator.kt @@ -35,6 +35,7 @@ import org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializerCod import org.jetbrains.kotlinx.serialization.compiler.backend.common.annotationVarsAndDesc import org.jetbrains.kotlinx.serialization.compiler.backend.common.findTypeSerializer import org.jetbrains.kotlinx.serialization.compiler.backend.common.getSerialTypeInfo +import org.jetbrains.kotlinx.serialization.compiler.backend.jvm.contextSerializerId import org.jetbrains.kotlinx.serialization.compiler.backend.jvm.enumSerializerId import org.jetbrains.kotlinx.serialization.compiler.backend.jvm.referenceArraySerializerId import org.jetbrains.kotlinx.serialization.compiler.resolve.* @@ -158,7 +159,7 @@ class SerializerJsTranslator(declaration: KtPureClassOrObject, return getQualifiedClassReferenceName(serializerClass) } else { - var args = if (serializerClass.classId == enumSerializerId) + var args = if (serializerClass.classId == enumSerializerId || serializerClass.classId == contextSerializerId) listOf(createGetKClassExpression(kType.toClassDescriptor!!)) else kType.arguments.map { val argSer = findTypeSerializer(module, it.type) ?: return null @@ -166,7 +167,7 @@ class SerializerJsTranslator(declaration: KtPureClassOrObject, if (it.type.isMarkedNullable) JsNew(nullableSerClass, listOf(expr)) else expr } if (serializerClass.classId == referenceArraySerializerId) - args = listOf(createGetKClassExpression(kType.arguments[0].type.toClassDescriptor!!)) + args + args = listOf(createGetKClassExpression(kType.arguments[0].type.toClassDescriptor!!)) + args return JsNew(getQualifiedClassReferenceName(serializerClass), args) } } @@ -215,7 +216,7 @@ class SerializerJsTranslator(declaration: KtPureClassOrObject, JsInvocation(JsNameRef(readElementF, inputVar), serialClassDescRef) ).makeStmt() // switch(index) - jsSwitch (indexVar) { + jsSwitch(indexVar) { // -2: readAll = true case(JsIntLiteral(-2)) { +JsAstUtils.assignment( @@ -230,12 +231,15 @@ class SerializerJsTranslator(declaration: KtPureClassOrObject, val sti = getSerialTypeInfo(property) val innerSerial = if (sti.serializer == null) null else serializerInstance(sti.serializer, property.module, property.type) val call = if (innerSerial == null) { + val unknownSer = (sti.elementMethodPrefix.isEmpty()) val readFunc = - inputClass.getFuncDesc("read${sti.elementMethodPrefix}ElementValue").single() + inputClass.getFuncDesc("read${sti.elementMethodPrefix}ElementValue") + // if readElementValue, must have 3 parameters, if readXXXElementValue - 2 + .single { !unknownSer || (it is FunctionDescriptor && it.valueParameters.size == 3) } .let { context.getNameForDescriptor(it) } - JsInvocation(JsNameRef(readFunc, inputVar), - serialClassDescRef, - JsIntLiteral(i)) + val readArgs = mutableListOf(serialClassDescRef, JsIntLiteral(i)) + if (unknownSer) readArgs.add(createGetKClassExpression(property.type.toClassDescriptor!!)) + JsInvocation(JsNameRef(readFunc, inputVar), readArgs) } else { val readFunc = @@ -262,7 +266,7 @@ class SerializerJsTranslator(declaration: KtPureClassOrObject, if (KotlinBuiltIns.isCharOrNullableChar(property.type)) { +JsAstUtils.assignment( localProps[i], - TranslationUtils.coerce(context, localProps[i], TranslationUtils.getReturnTypeForCoercion(property.descriptor.getter!!)) + TranslationUtils.coerce(context, localProps[i], context.currentModule.builtIns.charType) ).makeStmt() } @@ -298,6 +302,7 @@ class SerializerJsTranslator(declaration: KtPureClassOrObject, ).makeStmt() // deserialization constructor call + // todo: external deserialization with primary constructor and setters calls after resolution of KT-11586 val constrDesc = KSerializerDescriptorResolver.createLoadConstructorDescriptor(serializableDescriptor, context.bindingContext()) val constrRef = context.getInnerNameForDescriptor(constrDesc).makeRef() val args: MutableList = mutableListOf(bitMasks[0]) diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/jvm/JVMCodegenUtil.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/jvm/JVMCodegenUtil.kt index 338432f00c9..2e50508b13a 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/jvm/JVMCodegenUtil.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/jvm/JVMCodegenUtil.kt @@ -31,10 +31,7 @@ import org.jetbrains.kotlinx.serialization.compiler.backend.common.SerialTypeInf import org.jetbrains.kotlinx.serialization.compiler.backend.common.findEnumTypeSerializer import org.jetbrains.kotlinx.serialization.compiler.backend.common.findPolymorphicSerializer import org.jetbrains.kotlinx.serialization.compiler.backend.common.requiresPolymorphism -import org.jetbrains.kotlinx.serialization.compiler.resolve.SerializableProperty -import org.jetbrains.kotlinx.serialization.compiler.resolve.internalPackageFqName -import org.jetbrains.kotlinx.serialization.compiler.resolve.toClassDescriptor -import org.jetbrains.kotlinx.serialization.compiler.resolve.typeSerializer +import org.jetbrains.kotlinx.serialization.compiler.resolve.* import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter @@ -113,8 +110,9 @@ internal fun ImplementationBodyCodegen.generateMethod(function: FunctionDescript internal val enumSerializerId = ClassId(internalPackageFqName, Name.identifier("EnumSerializer")) -internal val polymorphicSerializerId = ClassId(internalPackageFqName, Name.identifier("PolymorphicSerializer")) +internal val polymorphicSerializerId = ClassId(packageFqName, Name.identifier("PolymorphicSerializer")) internal val referenceArraySerializerId = ClassId(internalPackageFqName, Name.identifier("ReferenceArraySerializer")) +internal val contextSerializerId = ClassId(packageFqName, Name.identifier("ContextSerializer")) // returns false is property should not use serializer internal fun InstructionAdapter.stackValueSerializerInstance(codegen: ClassBodyCodegen, sti: JVMSerialTypeInfo): Boolean { @@ -150,7 +148,7 @@ internal fun stackValueSerializerInstance(codegen: ClassBodyCodegen, module: Mod // instantiate all arg serializers on stack val signature = StringBuilder("(") when (serializer.classId) { - enumSerializerId -> { + enumSerializerId, contextSerializerId -> { // a special way to instantiate enum -- need a enum KClass reference aconst(codegen.typeMapper.mapType(kType)) AsmUtil.wrapJavaClassIntoKClass(this) @@ -202,7 +200,7 @@ fun getSerialTypeInfo(property: SerializableProperty, type: Type): JVMSerialType } ARRAY -> { // check for explicit serialization annotation on this property - var serializer = property.serializer.toClassDescriptor + var serializer = property.serializableWith.toClassDescriptor if (serializer == null) { // no explicit serializer for this property. Select strategy by element type when (type.elementType.sort) { @@ -224,7 +222,8 @@ fun getSerialTypeInfo(property: SerializableProperty, type: Type): JVMSerialType return JVMSerialTypeInfo(property, Type.getType("Lkotlin/Unit;"), "Unit", unit = true) // todo: more efficient enum support here, but only for enums that don't define custom serializer // otherwise, it is a serializer for some other type - val serializer = findTypeSerializer(property.module, property.type, type) + val serializer = property.serializableWith?.toClassDescriptor + ?: findTypeSerializer(property.module, property.type, type) return JVMSerialTypeInfo(property, Type.getType("Ljava/lang/Object;"), if (property.type.isMarkedNullable) "Nullable" else "", serializer) } @@ -237,6 +236,7 @@ fun findTypeSerializer(module: ModuleDescriptor, kType: KotlinType, asmType: Typ else kType.typeSerializer.toClassDescriptor // check for serializer defined on the type ?: findStandardAsmTypeSerializer(module, asmType) // otherwise see if there is a standard serializer ?: findEnumTypeSerializer(module, kType) + ?: module.findClassAcrossModuleDependencies(contextSerializerId) } fun findStandardAsmTypeSerializer(module: ModuleDescriptor, asmType: Type): ClassDescriptor? { diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/jvm/SerializerCodegenImpl.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/jvm/SerializerCodegenImpl.kt index 82cc23630ac..7d3700fb48e 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/jvm/SerializerCodegenImpl.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/jvm/SerializerCodegenImpl.kt @@ -16,13 +16,11 @@ package org.jetbrains.kotlinx.serialization.compiler.backend.jvm -import org.jetbrains.kotlin.codegen.CompilationException -import org.jetbrains.kotlin.codegen.ExpressionCodegen -import org.jetbrains.kotlin.codegen.ImplementationBodyCodegen -import org.jetbrains.kotlin.codegen.StackValue +import org.jetbrains.kotlin.codegen.* import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.resolve.jvm.AsmTypes import org.jetbrains.kotlin.resolve.jvm.diagnostics.OtherOrigin import org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializerCodegen import org.jetbrains.kotlinx.serialization.compiler.backend.common.annotationVarsAndDesc @@ -238,10 +236,16 @@ class SerializerCodegenImpl( val sti = getSerialTypeInfo(property, propertyType) val useSerializer = stackValueSerializerInstance(codegen, sti) + val unknownSer = (!useSerializer && sti.elementMethodPrefix.isEmpty()) + if (unknownSer) { + aconst(codegen.typeMapper.mapType(property.type)) + AsmUtil.wrapJavaClassIntoKClass(this) + } invokevirtual(kInputType.internalName, "read" + sti.elementMethodPrefix + (if (useSerializer) "Serializable" else "") + "ElementValue", "(" + descType.descriptor + "I" + (if (useSerializer) kSerialLoaderType.descriptor else "") + + (if (unknownSer) AsmTypes.K_CLASS_TYPE.descriptor else "") + ")" + (if (sti.unit) "V" else sti.type.descriptor), false) if (sti.unit) { StackValue.putUnitInstance(this) diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializationUtil.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializationUtil.kt index c50e5bdb747..bf59da60a15 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializationUtil.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializationUtil.kt @@ -128,15 +128,8 @@ internal val ClassDescriptor?.classSerializer: KotlinType? } // serializer that was declared for this specific type or annotation from a class declaration -val KotlinType?.typeSerializer: KotlinType? - get() = this?.let { - // serializer annotation on this type or from a class - return it.annotations.serializableWith ?: (it.toClassDescriptor).classSerializer - } - -// serializer that was declared specifically for this property via its own annotation or via annotation on its type -val PropertyDescriptor.propertySerializer: KotlinType? - get() = annotations.serializableWith ?: type.typeSerializer +val KotlinType.typeSerializer: KotlinType? + get() = this.annotations.serializableWith ?: (this.toClassDescriptor).classSerializer fun getSerializableClassDescriptorBySerializer(serializerDescriptor: ClassDescriptor): ClassDescriptor? { diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/SerializableProperty.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/SerializableProperty.kt index ffad36e7c46..b63fbf12b24 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/SerializableProperty.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/SerializableProperty.kt @@ -23,7 +23,7 @@ class SerializableProperty(val descriptor: PropertyDescriptor, val isConstructor val name = descriptor.annotations.serialNameValue ?: descriptor.name.asString() val type = descriptor.type val module = descriptor.module - val serializer = descriptor.propertySerializer + val serializableWith = descriptor.annotations.serializableWith val optional = descriptor.annotations.serialOptional val transient = descriptor.annotations.serialTransient val annotations = descriptor.annotations diff --git a/settings.gradle b/settings.gradle index 4d8b64cee8a..d23e308696d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -180,7 +180,10 @@ include ":kotlin-build-common", ':kotlin-gradle-plugin:plugin-marker', ':kotlin-allopen:plugin-marker', ':kotlin-noarg:plugin-marker', - ":test-instrumenter" + ":test-instrumenter", + + ":kotlinx-serialization-compiler-plugin", + ":kotlinx-gradle-serialization-plugin" def isTeamcityBuild = hasProperty("teamcity") || System.getenv("TEAMCITY_VERSION") != null def includeUltimate = hasProperty("intellijUltimateEnabled") && intellijUltimateEnabled != 'false' @@ -294,3 +297,6 @@ project(':libraries:kotlin-prepush-hook').projectDir = "$rootDir/libraries/tools project(':kotlin-gradle-plugin:plugin-marker').projectDir = file("$rootDir/libraries/tools/kotlin-gradle-plugin/plugin-marker") project(':kotlin-allopen:plugin-marker').projectDir = file("$rootDir/libraries/tools/kotlin-allopen/plugin-marker") project(':kotlin-noarg:plugin-marker').projectDir = file("$rootDir/libraries/tools/kotlin-noarg/plugin-marker") + +project(':kotlinx-serialization-compiler-plugin').projectDir = file("$rootDir/plugins/kotlin-serialization/kotlin-serialization-compiler") +project(':kotlinx-gradle-serialization-plugin').projectDir = file("$rootDir/libraries/tools/kotlin-serialization")