From d70271b6aaac130918a7b190b8b9aecf5badd2a8 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Sun, 15 Mar 2020 14:21:42 +0100 Subject: [PATCH] Move RequireKotlinNames to 'descriptors' Also move version string regex there and rename the class to RequireKotlinConstants. This allows to get rid of dependency of 'serialization' on 'frontend'. --- .../kotlin/resolve/RequireKotlinNames.kt | 30 ------------------- ...tlinVersionStringAnnotationValueChecker.kt | 10 ++----- compiler/serialization/build.gradle.kts | 6 +--- .../serialization/DescriptorSerializer.kt | 17 +++++------ .../kotlin/resolve/RequireKotlinConstants.kt | 21 +++++++++++++ 5 files changed, 33 insertions(+), 51 deletions(-) delete mode 100644 compiler/frontend/src/org/jetbrains/kotlin/resolve/RequireKotlinNames.kt create mode 100644 core/descriptors/src/org/jetbrains/kotlin/resolve/RequireKotlinConstants.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/RequireKotlinNames.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/RequireKotlinNames.kt deleted file mode 100644 index 5493360cb43..00000000000 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/RequireKotlinNames.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.resolve - -import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name - -object RequireKotlinNames { - val FQ_NAME = FqName("kotlin.internal.RequireKotlin") - - val VERSION = Name.identifier("version") - val MESSAGE = Name.identifier("message") - val LEVEL = Name.identifier("level") - val VERSION_KIND = Name.identifier("versionKind") - val ERROR_CODE = Name.identifier("errorCode") -} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/KotlinVersionStringAnnotationValueChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/KotlinVersionStringAnnotationValueChecker.kt index 2c431038030..dfb3cbee401 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/KotlinVersionStringAnnotationValueChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/KotlinVersionStringAnnotationValueChecker.kt @@ -24,7 +24,7 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticSink import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.KtDeclaration -import org.jetbrains.kotlin.resolve.RequireKotlinNames +import org.jetbrains.kotlin.resolve.RequireKotlinConstants import org.jetbrains.kotlin.resolve.SINCE_KOTLIN_FQ_NAME import org.jetbrains.kotlin.resolve.source.getPsi @@ -34,7 +34,7 @@ abstract class KotlinVersionStringAnnotationValueChecker( override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) { val annotation = descriptor.annotations.findAnnotation(annotationFqName) ?: return val version = annotation.allValueArguments.values.singleOrNull()?.value as? String ?: return - if (!version.matches(VERSION_REGEX)) { + if (!version.matches(RequireKotlinConstants.VERSION_REGEX)) { context.trace.report(Errors.ILLEGAL_KOTLIN_VERSION_STRING_VALUE.on(annotation.source.getPsi() ?: declaration, annotationFqName)) return } @@ -50,10 +50,6 @@ abstract class KotlinVersionStringAnnotationValueChecker( languageVersionSettings: LanguageVersionSettings ) { } - - companion object { - val VERSION_REGEX: Regex = "(0|[1-9][0-9]*)".let { number -> Regex("$number\\.$number(\\.$number)?") } - } } object SinceKotlinAnnotationValueChecker : KotlinVersionStringAnnotationValueChecker(SINCE_KOTLIN_FQ_NAME) { @@ -77,4 +73,4 @@ object SinceKotlinAnnotationValueChecker : KotlinVersionStringAnnotationValueChe } } -object RequireKotlinAnnotationValueChecker : KotlinVersionStringAnnotationValueChecker(RequireKotlinNames.FQ_NAME) +object RequireKotlinAnnotationValueChecker : KotlinVersionStringAnnotationValueChecker(RequireKotlinConstants.FQ_NAME) diff --git a/compiler/serialization/build.gradle.kts b/compiler/serialization/build.gradle.kts index 74af454cb0d..f3abf619e15 100644 --- a/compiler/serialization/build.gradle.kts +++ b/compiler/serialization/build.gradle.kts @@ -1,14 +1,10 @@ - plugins { kotlin("jvm") id("jps-compatible") } dependencies { - compile(project(":compiler:util")) - compile(project(":compiler:frontend")) - compile(project(":core:descriptors")) - compile(project(":core:deserialization")) + compile(project(":compiler:resolution")) } sourceSets { diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt b/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt index 31a7857c210..b02c89638aa 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt @@ -25,9 +25,8 @@ import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumEntry import org.jetbrains.kotlin.resolve.MemberComparator -import org.jetbrains.kotlin.resolve.RequireKotlinNames +import org.jetbrains.kotlin.resolve.RequireKotlinConstants import org.jetbrains.kotlin.resolve.calls.components.isActualParameterWithAnyExpectedDefault -import org.jetbrains.kotlin.resolve.checkers.KotlinVersionStringAnnotationValueChecker import org.jetbrains.kotlin.resolve.constants.EnumValue import org.jetbrains.kotlin.resolve.constants.IntValue import org.jetbrains.kotlin.resolve.constants.NullValue @@ -672,15 +671,15 @@ class DescriptorSerializer private constructor( // Returns a list of indices into versionRequirementTable, or empty list if there's no @RequireKotlin on the descriptor private fun MutableVersionRequirementTable.serializeVersionRequirements(descriptor: DeclarationDescriptor): List = descriptor.annotations - .filter { it.fqName == RequireKotlinNames.FQ_NAME } + .filter { it.fqName == RequireKotlinConstants.FQ_NAME } .mapNotNull(::serializeVersionRequirementFromRequireKotlin) .map(::get) private fun serializeVersionRequirementFromRequireKotlin(annotation: AnnotationDescriptor): ProtoBuf.VersionRequirement.Builder? { val args = annotation.allValueArguments - val versionString = (args[RequireKotlinNames.VERSION] as? StringValue)?.value ?: return null - val matchResult = KotlinVersionStringAnnotationValueChecker.VERSION_REGEX.matchEntire(versionString) ?: return null + val versionString = (args[RequireKotlinConstants.VERSION] as? StringValue)?.value ?: return null + val matchResult = RequireKotlinConstants.VERSION_REGEX.matchEntire(versionString) ?: return null val major = matchResult.groupValues.getOrNull(1)?.toIntOrNull() ?: return null val minor = matchResult.groupValues.getOrNull(2)?.toIntOrNull() ?: 0 @@ -692,12 +691,12 @@ class DescriptorSerializer private constructor( writeVersionFull = { proto.versionFull = it } ) - val message = (args[RequireKotlinNames.MESSAGE] as? StringValue)?.value + val message = (args[RequireKotlinConstants.MESSAGE] as? StringValue)?.value if (message != null) { proto.message = stringTable.getStringIndex(message) } - when ((args[RequireKotlinNames.LEVEL] as? EnumValue)?.enumEntryName?.asString()) { + when ((args[RequireKotlinConstants.LEVEL] as? EnumValue)?.enumEntryName?.asString()) { DeprecationLevel.ERROR.name -> { // ERROR is the default level } @@ -705,7 +704,7 @@ class DescriptorSerializer private constructor( DeprecationLevel.HIDDEN.name -> proto.level = ProtoBuf.VersionRequirement.Level.HIDDEN } - when ((args[RequireKotlinNames.VERSION_KIND] as? EnumValue)?.enumEntryName?.asString()) { + when ((args[RequireKotlinConstants.VERSION_KIND] as? EnumValue)?.enumEntryName?.asString()) { ProtoBuf.VersionRequirement.VersionKind.LANGUAGE_VERSION.name -> { // LANGUAGE_VERSION is the default kind } @@ -715,7 +714,7 @@ class DescriptorSerializer private constructor( proto.versionKind = ProtoBuf.VersionRequirement.VersionKind.API_VERSION } - val errorCode = (args[RequireKotlinNames.ERROR_CODE] as? IntValue)?.value + val errorCode = (args[RequireKotlinConstants.ERROR_CODE] as? IntValue)?.value if (errorCode != null && errorCode != -1) { proto.errorCode = errorCode } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/RequireKotlinConstants.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/RequireKotlinConstants.kt new file mode 100644 index 00000000000..1915e595f83 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/RequireKotlinConstants.kt @@ -0,0 +1,21 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * 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.resolve + +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name + +object RequireKotlinConstants { + val FQ_NAME = FqName("kotlin.internal.RequireKotlin") + + val VERSION = Name.identifier("version") + val MESSAGE = Name.identifier("message") + val LEVEL = Name.identifier("level") + val VERSION_KIND = Name.identifier("versionKind") + val ERROR_CODE = Name.identifier("errorCode") + + val VERSION_REGEX: Regex = "(0|[1-9][0-9]*)".let { number -> Regex("$number\\.$number(\\.$number)?") } +}