From 8acdb39bdd1e7df8558cb11f6b6b8c6c711e897c Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 29 May 2020 10:25:34 +0300 Subject: [PATCH] [FIR-PLUGIN] Add dummy implementation for member generator extension --- .../jetbrains/kotlin/fir/plugin/WithClass.kt | 8 --- .../fir/plugin/{AllOpen.kt => annotations.kt} | 5 +- .../fir/plugin/AllOpenClassGenerator.kt | 50 ---------------- .../fir/plugin/AllOpenMemberGenerator.kt | 59 +++++++++++++++++++ .../plugin/FirAllOpenComponentRegistrar.kt | 2 +- .../testData/classGen/simple.kt | 7 --- .../testData/classGen/simple.txt | 7 --- .../testData/memberGen/functionForProperty.kt | 22 +++++++ .../memberGen/functionForProperty.txt | 27 +++++++++ .../FirAllOpenDiagnosticTestGenerated.java | 14 ++--- 10 files changed, 120 insertions(+), 81 deletions(-) delete mode 100644 plugins/fir/fir-plugin-prototype/plugin-annotations/src/org/jetbrains/kotlin/fir/plugin/WithClass.kt rename plugins/fir/fir-plugin-prototype/plugin-annotations/src/org/jetbrains/kotlin/fir/plugin/{AllOpen.kt => annotations.kt} (68%) delete mode 100644 plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AllOpenClassGenerator.kt create mode 100644 plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AllOpenMemberGenerator.kt delete mode 100644 plugins/fir/fir-plugin-prototype/testData/classGen/simple.kt delete mode 100644 plugins/fir/fir-plugin-prototype/testData/classGen/simple.txt create mode 100644 plugins/fir/fir-plugin-prototype/testData/memberGen/functionForProperty.kt create mode 100644 plugins/fir/fir-plugin-prototype/testData/memberGen/functionForProperty.txt diff --git a/plugins/fir/fir-plugin-prototype/plugin-annotations/src/org/jetbrains/kotlin/fir/plugin/WithClass.kt b/plugins/fir/fir-plugin-prototype/plugin-annotations/src/org/jetbrains/kotlin/fir/plugin/WithClass.kt deleted file mode 100644 index 7e4ea6a44bc..00000000000 --- a/plugins/fir/fir-plugin-prototype/plugin-annotations/src/org/jetbrains/kotlin/fir/plugin/WithClass.kt +++ /dev/null @@ -1,8 +0,0 @@ -/* - * 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.fir.plugin - -annotation class WithClass \ No newline at end of file diff --git a/plugins/fir/fir-plugin-prototype/plugin-annotations/src/org/jetbrains/kotlin/fir/plugin/AllOpen.kt b/plugins/fir/fir-plugin-prototype/plugin-annotations/src/org/jetbrains/kotlin/fir/plugin/annotations.kt similarity index 68% rename from plugins/fir/fir-plugin-prototype/plugin-annotations/src/org/jetbrains/kotlin/fir/plugin/AllOpen.kt rename to plugins/fir/fir-plugin-prototype/plugin-annotations/src/org/jetbrains/kotlin/fir/plugin/annotations.kt index ea32d487382..aa204f7eb0c 100644 --- a/plugins/fir/fir-plugin-prototype/plugin-annotations/src/org/jetbrains/kotlin/fir/plugin/AllOpen.kt +++ b/plugins/fir/fir-plugin-prototype/plugin-annotations/src/org/jetbrains/kotlin/fir/plugin/annotations.kt @@ -5,4 +5,7 @@ package org.jetbrains.kotlin.fir.plugin -annotation class AllOpen \ No newline at end of file +annotation class AllOpen +annotation class WithGenerated +annotation class WithHello +annotation class WithNestedFoo \ No newline at end of file diff --git a/plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AllOpenClassGenerator.kt b/plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AllOpenClassGenerator.kt deleted file mode 100644 index e1fcabe3cd7..00000000000 --- a/plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AllOpenClassGenerator.kt +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.fir.plugin - -import org.jetbrains.kotlin.descriptors.ClassKind -import org.jetbrains.kotlin.descriptors.Modality -import org.jetbrains.kotlin.descriptors.Visibilities -import org.jetbrains.kotlin.fir.FirAnnotationContainer -import org.jetbrains.kotlin.fir.FirEffectiveVisibilityImpl -import org.jetbrains.kotlin.fir.FirSession -import org.jetbrains.kotlin.fir.declarations.* -import org.jetbrains.kotlin.fir.declarations.builder.buildRegularClass -import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl -import org.jetbrains.kotlin.fir.extensions.FirClassGenerationExtension -import org.jetbrains.kotlin.fir.extensions.predicate.DeclarationPredicate -import org.jetbrains.kotlin.fir.extensions.predicate.has -import org.jetbrains.kotlin.fir.resolve.firProvider -import org.jetbrains.kotlin.fir.resolve.providers.impl.FirProviderImpl -import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol -import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name - -class AllOpenClassGenerator(session: FirSession) : FirClassGenerationExtension(session) { - override fun generateClass( - containingFile: FirFile, - annotatedDeclaration: T - ): List where T : FirDeclaration, T : FirAnnotationContainer { - if (annotatedDeclaration !is FirRegularClass) return emptyList() - val klass = buildRegularClass { - session = this@AllOpenClassGenerator.session - origin = FirDeclarationOrigin.Plugin(AllOpenPluginKey) - status = FirResolvedDeclarationStatusImpl(Visibilities.PUBLIC, FirEffectiveVisibilityImpl.Public, Modality.FINAL) - classKind = ClassKind.OBJECT - scopeProvider = (session.firProvider as FirProviderImpl).kotlinScopeProvider - name = Name.identifier("Foo${annotatedDeclaration.name.identifier}") - symbol = FirRegularClassSymbol(ClassId(containingFile.packageFqName, name)) - superTypeRefs += session.builtinTypes.anyType - } - return listOf(GeneratedClass(klass, containingFile)) - } - - override val predicate: DeclarationPredicate = has(FqName("org.jetbrains.kotlin.fir.plugin.WithClass")) - - override val key: FirPluginKey - get() = AllOpenPluginKey -} \ No newline at end of file diff --git a/plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AllOpenMemberGenerator.kt b/plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AllOpenMemberGenerator.kt new file mode 100644 index 00000000000..ff5a579b9a0 --- /dev/null +++ b/plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AllOpenMemberGenerator.kt @@ -0,0 +1,59 @@ +/* + * 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.fir.plugin + +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.declarations.* +import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunction +import org.jetbrains.kotlin.fir.extensions.FirExistingClassModificationExtension +import org.jetbrains.kotlin.fir.extensions.predicate.DeclarationPredicate +import org.jetbrains.kotlin.fir.extensions.predicate.and +import org.jetbrains.kotlin.fir.extensions.predicate.has +import org.jetbrains.kotlin.fir.extensions.predicate.under +import org.jetbrains.kotlin.fir.resolve.transformers.plugin.GeneratedNestedClass +import org.jetbrains.kotlin.fir.symbols.CallableId +import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name + +class AllOpenMemberGenerator(session: FirSession) : FirExistingClassModificationExtension(session) { + override fun generateNestedClasses( + annotatedDeclaration: FirDeclaration, + owners: List + ): List> { + return emptyList() + } + + override fun generateMembersForNestedClasses(generatedNestedClass: GeneratedNestedClass): List { + return emptyList() + } + + override fun generateMembers( + annotatedDeclaration: FirDeclaration, + owners: List + ): List> { + if (annotatedDeclaration !is FirProperty) return emptyList() + val owner = owners.last() as? FirRegularClass ?: return emptyList() + val propertyName = annotatedDeclaration.name.identifier + val function = buildSimpleFunction { + session = this@AllOpenMemberGenerator.session + resolvePhase = FirResolvePhase.ANALYZED_DEPENDENCIES + origin = FirDeclarationOrigin.Plugin(key) + returnTypeRef = annotatedDeclaration.returnTypeRef + status = annotatedDeclaration.status + name = Name.identifier("hello${propertyName.capitalize()}") + symbol = FirNamedFunctionSymbol(CallableId(owner.classId, name), isFakeOverride = false) + } + return listOf(GeneratedDeclaration(function, owner)) + } + + override val predicate: DeclarationPredicate = under("WithGenerated".fqn()) and has("WithHello".fqn()) + + override val key: FirPluginKey + get() = AllOpenPluginKey +} + +private fun String.fqn(): FqName = FqName("org.jetbrains.kotlin.fir.plugin.$this") \ No newline at end of file diff --git a/plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/FirAllOpenComponentRegistrar.kt b/plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/FirAllOpenComponentRegistrar.kt index b50fdccbe1f..89ebb9d4d6e 100644 --- a/plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/FirAllOpenComponentRegistrar.kt +++ b/plugins/fir/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/FirAllOpenComponentRegistrar.kt @@ -10,7 +10,7 @@ import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar class FirAllOpenComponentRegistrar : FirExtensionRegistrar() { override fun ExtensionRegistrarContext.configurePlugin() { +::AllOpenStatusTransformer - +::AllOpenClassGenerator + +::AllOpenMemberGenerator +::AllOpenAdditionalCheckers } } \ No newline at end of file diff --git a/plugins/fir/fir-plugin-prototype/testData/classGen/simple.kt b/plugins/fir/fir-plugin-prototype/testData/classGen/simple.kt deleted file mode 100644 index b3b71467dc0..00000000000 --- a/plugins/fir/fir-plugin-prototype/testData/classGen/simple.kt +++ /dev/null @@ -1,7 +0,0 @@ -import org.jetbrains.kotlin.fir.plugin.WithClass -import org.jetbrains.kotlin.fir.plugin.AllOpen - -@WithClass -class A { - -} \ No newline at end of file diff --git a/plugins/fir/fir-plugin-prototype/testData/classGen/simple.txt b/plugins/fir/fir-plugin-prototype/testData/classGen/simple.txt deleted file mode 100644 index a14b32ba8b1..00000000000 --- a/plugins/fir/fir-plugin-prototype/testData/classGen/simple.txt +++ /dev/null @@ -1,7 +0,0 @@ -FILE: simple.kt - @R|org/jetbrains/kotlin/fir/plugin/WithClass|() public final class A : R|kotlin/Any| { - public constructor(): R|A| { - super() - } - - } diff --git a/plugins/fir/fir-plugin-prototype/testData/memberGen/functionForProperty.kt b/plugins/fir/fir-plugin-prototype/testData/memberGen/functionForProperty.kt new file mode 100644 index 00000000000..79e0d1c755d --- /dev/null +++ b/plugins/fir/fir-plugin-prototype/testData/memberGen/functionForProperty.kt @@ -0,0 +1,22 @@ +import org.jetbrains.kotlin.fir.plugin.WithHello +import org.jetbrains.kotlin.fir.plugin.WithGenerated +import org.jetbrains.kotlin.fir.plugin.AllOpen + +@WithGenerated +class A { + @WithHello + val x: Int = 1 +} + +class B { + @WithHello + val x: Int = 1 +} + +fun test_1(a: A) { + a.helloX() // should be OK +} + +fun test_2(b: B) { + b.helloX() // should be an error +} \ No newline at end of file diff --git a/plugins/fir/fir-plugin-prototype/testData/memberGen/functionForProperty.txt b/plugins/fir/fir-plugin-prototype/testData/memberGen/functionForProperty.txt new file mode 100644 index 00000000000..7dd2283511e --- /dev/null +++ b/plugins/fir/fir-plugin-prototype/testData/memberGen/functionForProperty.txt @@ -0,0 +1,27 @@ +FILE: functionForProperty.kt + @R|org/jetbrains/kotlin/fir/plugin/WithGenerated|() public final class A : R|kotlin/Any| { + public constructor(): R|A| { + super() + } + + @R|org/jetbrains/kotlin/fir/plugin/WithHello|() public final val x: R|kotlin/Int| = Int(1) + public get(): R|kotlin/Int| + + public final fun helloX(): R|kotlin/Int| + + } + public final class B : R|kotlin/Any| { + public constructor(): R|B| { + super() + } + + @R|org/jetbrains/kotlin/fir/plugin/WithHello|() public final val x: R|kotlin/Int| = Int(1) + public get(): R|kotlin/Int| + + } + public final fun test_1(a: R|A|): R|kotlin/Unit| { + R|/a|.R|/A.helloX|() + } + public final fun test_2(b: R|B|): R|kotlin/Unit| { + R|/b|.#() + } diff --git a/plugins/fir/fir-plugin-prototype/tests/org/jetbrains/kotlin/fir/plugin/FirAllOpenDiagnosticTestGenerated.java b/plugins/fir/fir-plugin-prototype/tests/org/jetbrains/kotlin/fir/plugin/FirAllOpenDiagnosticTestGenerated.java index 836dcc5e4a1..b13cf59d09c 100644 --- a/plugins/fir/fir-plugin-prototype/tests/org/jetbrains/kotlin/fir/plugin/FirAllOpenDiagnosticTestGenerated.java +++ b/plugins/fir/fir-plugin-prototype/tests/org/jetbrains/kotlin/fir/plugin/FirAllOpenDiagnosticTestGenerated.java @@ -46,21 +46,21 @@ public class FirAllOpenDiagnosticTestGenerated extends AbstractFirAllOpenDiagnos } } - @TestMetadata("plugins/fir/fir-plugin-prototype/testData/classGen") + @TestMetadata("plugins/fir/fir-plugin-prototype/testData/memberGen") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class ClassGen extends AbstractFirAllOpenDiagnosticTest { + public static class MemberGen extends AbstractFirAllOpenDiagnosticTest { private void runTest(String testDataFilePath) throws Exception { KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); } - public void testAllFilesPresentInClassGen() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("plugins/fir/fir-plugin-prototype/testData/classGen"), Pattern.compile("^(.+)\\.kt$"), null, true); + public void testAllFilesPresentInMemberGen() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("plugins/fir/fir-plugin-prototype/testData/memberGen"), Pattern.compile("^(.+)\\.kt$"), null, true); } - @TestMetadata("simple.kt") - public void testSimple() throws Exception { - runTest("plugins/fir/fir-plugin-prototype/testData/classGen/simple.kt"); + @TestMetadata("functionForProperty.kt") + public void testFunctionForProperty() throws Exception { + runTest("plugins/fir/fir-plugin-prototype/testData/memberGen/functionForProperty.kt"); } }