[Analysis API] implement an API to get a substitution based on an inheritance relationship between classes
^KT-62090 fixed
This commit is contained in:
committed by
Space Team
parent
49cfbb0576
commit
15e4cbc10d
+3
-1
@@ -75,8 +75,10 @@ class KtFe10AnalysisSession(
|
||||
override val metadataCalculatorImpl: KtMetadataCalculator
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
|
||||
@Suppress("AnalysisApiMissingLifetimeCheck")
|
||||
override val substitutorProviderImpl: KtSubstitutorProvider
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
override fun createContextDependentCopy(originalKtFile: KtFile, elementToReanalyze: KtElement): KtAnalysisSession =
|
||||
KtFe10AnalysisSession(originalKtFile.project, elementToReanalyze, token)
|
||||
}
|
||||
|
||||
+2
@@ -133,6 +133,8 @@ private constructor(
|
||||
|
||||
override val metadataCalculatorImpl: KtMetadataCalculator = KtFirMetadataCalculator(this)
|
||||
|
||||
override val substitutorProviderImpl: KtSubstitutorProvider = KtFirSubstitutorProvider(this)
|
||||
|
||||
@Suppress("AnalysisApiMissingLifetimeCheck")
|
||||
override fun createContextDependentCopy(originalKtFile: KtFile, elementToReanalyze: KtElement): KtAnalysisSession {
|
||||
check(mode == AnalysisSessionMode.REGULAR) {
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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.analysis.api.fir.components
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtSubstitutorProvider
|
||||
import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.fir.utils.firSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtSubstitutor
|
||||
import org.jetbrains.kotlin.fir.resolve.substitution.chain
|
||||
import org.jetbrains.kotlin.fir.scopes.substitutorForSuperType
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
|
||||
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
|
||||
import org.jetbrains.kotlin.fir.types.toRegularClassSymbol
|
||||
|
||||
internal class KtFirSubstitutorProvider(
|
||||
override val analysisSession: KtFirAnalysisSession,
|
||||
) : KtSubstitutorProvider(), KtFirAnalysisSessionComponent {
|
||||
override fun createSubstitutor(
|
||||
subClass: KtClassOrObjectSymbol,
|
||||
superClass: KtClassOrObjectSymbol,
|
||||
): KtSubstitutor? {
|
||||
if (subClass == superClass) return KtSubstitutor.Empty(token)
|
||||
|
||||
val baseFirSymbol = subClass.firSymbol
|
||||
val superFirSymbol = superClass.firSymbol
|
||||
val inheritancePath = collectInheritancePath(baseFirSymbol, superFirSymbol) ?: return null
|
||||
val substitutors = inheritancePath.map { (type, symbol) ->
|
||||
type.substitutorForSuperType(rootModuleSession, symbol)
|
||||
}
|
||||
return when (substitutors.size) {
|
||||
0 -> KtSubstitutor.Empty(token)
|
||||
else -> {
|
||||
val chained = substitutors.reduce { left, right -> left.chain(right) }
|
||||
firSymbolBuilder.typeBuilder.buildSubstitutor(chained)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun collectInheritancePath(
|
||||
baseSymbol: FirClassSymbol<*>,
|
||||
superSymbol: FirClassSymbol<*>,
|
||||
): List<Pair<ConeClassLikeType, FirRegularClassSymbol>>? {
|
||||
val stack = mutableListOf<Pair<ConeClassLikeType, FirRegularClassSymbol>>()
|
||||
var result: List<Pair<ConeClassLikeType, FirRegularClassSymbol>>? = null
|
||||
|
||||
fun dfs(symbol: FirClassSymbol<*>) {
|
||||
for (superType in symbol.resolvedSuperTypes) {
|
||||
if (result != null) {
|
||||
return
|
||||
}
|
||||
if (superType !is ConeClassLikeType) continue
|
||||
val superClassSymbol = superType.toRegularClassSymbol(rootModuleSession) ?: continue
|
||||
stack += superType to superClassSymbol
|
||||
if (superClassSymbol == superSymbol) {
|
||||
result = stack.toList()
|
||||
check(stack.removeLast().second == superClassSymbol)
|
||||
break
|
||||
}
|
||||
dfs(superClassSymbol)
|
||||
check(stack.removeLast().second == superClassSymbol)
|
||||
}
|
||||
}
|
||||
|
||||
dfs(baseSymbol)
|
||||
return result?.reversed()
|
||||
}
|
||||
|
||||
}
|
||||
+1
@@ -34,6 +34,7 @@ internal val KtConstructorSymbol.firSymbol: FirConstructorSymbol get() = (this a
|
||||
internal val KtPropertyAccessorSymbol.firSymbol: FirPropertyAccessorSymbol get() = (this as KtFirSymbol<*>).firSymbol as FirPropertyAccessorSymbol
|
||||
internal val KtClassInitializerSymbol.firSymbol: FirAnonymousInitializerSymbol get() = (this as KtFirSymbol<*>).firSymbol as FirAnonymousInitializerSymbol
|
||||
internal val KtClassLikeSymbol.firSymbol: FirClassLikeSymbol<*> get() = (this as KtFirSymbol<*>).firSymbol as FirClassLikeSymbol<*>
|
||||
internal val KtClassOrObjectSymbol.firSymbol: FirClassSymbol<*> get() = (this as KtFirSymbol<*>).firSymbol as FirClassSymbol<*>
|
||||
|
||||
|
||||
fun FirBasedSymbol<*>.getContainingKtModule(firResolveSession: LLFirResolveSession): KtModule {
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.analysis.api.fir.test.cases.generated.cases.components.substitutorProvider;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.test.util.KtTestUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.analysis.api.fir.test.configurators.AnalysisApiFirTestConfiguratorFactory;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfiguratorFactoryData;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.FrontendKind;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisSessionMode;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiMode;
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.substitutorProvider.AbstractCreateInheritanceTypeSubstitutorTest;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
public class FirIdeDependentAnalysisScriptSourceModuleCreateInheritanceTypeSubstitutorTestGenerated extends AbstractCreateInheritanceTypeSubstitutorTest {
|
||||
@NotNull
|
||||
@Override
|
||||
public AnalysisApiTestConfigurator getConfigurator() {
|
||||
return AnalysisApiFirTestConfiguratorFactory.INSTANCE.createConfigurator(
|
||||
new AnalysisApiTestConfiguratorFactoryData(
|
||||
FrontendKind.Fir,
|
||||
TestModuleKind.ScriptSource,
|
||||
AnalysisSessionMode.Dependent,
|
||||
AnalysisApiMode.Ide
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllFilesPresentInCreateInheritanceTypeSubstitutor() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor"), Pattern.compile("^(.+)\\.kts$"), null, true);
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.analysis.api.fir.test.cases.generated.cases.components.substitutorProvider;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.test.util.KtTestUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.analysis.api.fir.test.configurators.AnalysisApiFirTestConfiguratorFactory;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfiguratorFactoryData;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.FrontendKind;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisSessionMode;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiMode;
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.substitutorProvider.AbstractCreateInheritanceTypeSubstitutorTest;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
public class FirIdeDependentAnalysisSourceModuleCreateInheritanceTypeSubstitutorTestGenerated extends AbstractCreateInheritanceTypeSubstitutorTest {
|
||||
@NotNull
|
||||
@Override
|
||||
public AnalysisApiTestConfigurator getConfigurator() {
|
||||
return AnalysisApiFirTestConfiguratorFactory.INSTANCE.createConfigurator(
|
||||
new AnalysisApiTestConfiguratorFactoryData(
|
||||
FrontendKind.Fir,
|
||||
TestModuleKind.Source,
|
||||
AnalysisSessionMode.Dependent,
|
||||
AnalysisApiMode.Ide
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllFilesPresentInCreateInheritanceTypeSubstitutor() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("chain.kt")
|
||||
public void testChain() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/chain.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("cycle.kt")
|
||||
public void testCycle() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/cycle.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("directComplexSubstitution.kt")
|
||||
public void testDirectComplexSubstitution() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/directComplexSubstitution.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("directType.kt")
|
||||
public void testDirectType() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/directType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("directTypeParam.kt")
|
||||
public void testDirectTypeParam() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/directTypeParam.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("errorInUpperBound.kt")
|
||||
public void testErrorInUpperBound() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/errorInUpperBound.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("extraTypeArgument.kt")
|
||||
public void testExtraTypeArgument() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/extraTypeArgument.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("missingTypeArgument.kt")
|
||||
public void testMissingTypeArgument() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/missingTypeArgument.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multipleInheritance.kt")
|
||||
public void testMultipleInheritance() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/multipleInheritance.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("self.kt")
|
||||
public void testSelf() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/self.kt");
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.analysis.api.fir.test.cases.generated.cases.components.substitutorProvider;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.test.util.KtTestUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.analysis.api.fir.test.configurators.AnalysisApiFirTestConfiguratorFactory;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfiguratorFactoryData;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.FrontendKind;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisSessionMode;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiMode;
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.substitutorProvider.AbstractCreateInheritanceTypeSubstitutorTest;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
public class FirIdeNormalAnalysisScriptSourceModuleCreateInheritanceTypeSubstitutorTestGenerated extends AbstractCreateInheritanceTypeSubstitutorTest {
|
||||
@NotNull
|
||||
@Override
|
||||
public AnalysisApiTestConfigurator getConfigurator() {
|
||||
return AnalysisApiFirTestConfiguratorFactory.INSTANCE.createConfigurator(
|
||||
new AnalysisApiTestConfiguratorFactoryData(
|
||||
FrontendKind.Fir,
|
||||
TestModuleKind.ScriptSource,
|
||||
AnalysisSessionMode.Normal,
|
||||
AnalysisApiMode.Ide
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllFilesPresentInCreateInheritanceTypeSubstitutor() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor"), Pattern.compile("^(.+)\\.kts$"), null, true);
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.analysis.api.fir.test.cases.generated.cases.components.substitutorProvider;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.test.util.KtTestUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.analysis.api.fir.test.configurators.AnalysisApiFirTestConfiguratorFactory;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfiguratorFactoryData;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.FrontendKind;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisSessionMode;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiMode;
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.substitutorProvider.AbstractCreateInheritanceTypeSubstitutorTest;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
public class FirIdeNormalAnalysisSourceModuleCreateInheritanceTypeSubstitutorTestGenerated extends AbstractCreateInheritanceTypeSubstitutorTest {
|
||||
@NotNull
|
||||
@Override
|
||||
public AnalysisApiTestConfigurator getConfigurator() {
|
||||
return AnalysisApiFirTestConfiguratorFactory.INSTANCE.createConfigurator(
|
||||
new AnalysisApiTestConfiguratorFactoryData(
|
||||
FrontendKind.Fir,
|
||||
TestModuleKind.Source,
|
||||
AnalysisSessionMode.Normal,
|
||||
AnalysisApiMode.Ide
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllFilesPresentInCreateInheritanceTypeSubstitutor() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("chain.kt")
|
||||
public void testChain() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/chain.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("cycle.kt")
|
||||
public void testCycle() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/cycle.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("directComplexSubstitution.kt")
|
||||
public void testDirectComplexSubstitution() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/directComplexSubstitution.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("directType.kt")
|
||||
public void testDirectType() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/directType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("directTypeParam.kt")
|
||||
public void testDirectTypeParam() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/directTypeParam.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("errorInUpperBound.kt")
|
||||
public void testErrorInUpperBound() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/errorInUpperBound.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("extraTypeArgument.kt")
|
||||
public void testExtraTypeArgument() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/extraTypeArgument.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("missingTypeArgument.kt")
|
||||
public void testMissingTypeArgument() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/missingTypeArgument.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multipleInheritance.kt")
|
||||
public void testMultipleInheritance() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/multipleInheritance.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("self.kt")
|
||||
public void testSelf() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/self.kt");
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.analysis.api.impl.base.test.cases.components.substitutorProvider
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.stringRepresentation
|
||||
import org.jetbrains.kotlin.analysis.api.renderer.types.impl.KtTypeRendererForSource
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtConstructorSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtFunctionSymbol
|
||||
import org.jetbrains.kotlin.analysis.test.framework.base.AbstractAnalysisApiBasedTest
|
||||
import org.jetbrains.kotlin.analysis.test.framework.services.expressionMarkerProvider
|
||||
import org.jetbrains.kotlin.analysis.utils.printer.prettyPrint
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.test.services.TestModuleStructure
|
||||
import org.jetbrains.kotlin.test.services.TestServices
|
||||
import org.jetbrains.kotlin.test.services.assertions
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import kotlin.collections.single
|
||||
|
||||
abstract class AbstractCreateInheritanceTypeSubstitutorTest : AbstractAnalysisApiBasedTest() {
|
||||
override fun doTestByModuleStructure(moduleStructure: TestModuleStructure, testServices: TestServices) {
|
||||
val baseClass = testServices.expressionMarkerProvider
|
||||
.getElementsOfTypeAtCarets<KtClassOrObject>(moduleStructure, testServices, "base")
|
||||
.single().first
|
||||
val superClass = testServices.expressionMarkerProvider
|
||||
.getElementsOfTypeAtCarets<KtClassOrObject>(moduleStructure, testServices, "super")
|
||||
.single().first
|
||||
|
||||
val substitutorRendered = analyseForTest(baseClass) {
|
||||
val superClassSymbol = superClass.getClassOrObjectSymbol()!!
|
||||
val substitutor = createInheritanceTypeSubstitutor(baseClass.getClassOrObjectSymbol()!!, superClassSymbol)
|
||||
prettyPrint {
|
||||
appendLine("Substitutor: ${stringRepresentation(substitutor)}")
|
||||
if (substitutor != null) {
|
||||
val functions = superClassSymbol.getDeclaredMemberScope().getAllSymbols()
|
||||
.filterIsInstance<KtFunctionSymbol>()
|
||||
.toList()
|
||||
if (functions.isNotEmpty()) {
|
||||
appendLine("Substituted callables:")
|
||||
withIndent {
|
||||
for (function in functions) {
|
||||
val signature = function.substitute(substitutor)
|
||||
append(signature.callableIdIfNonLocal!!.callableName.asString())
|
||||
printCollection(signature.valueParameters, prefix = "(", postfix = ")") {
|
||||
append(it.returnType.render(typeRenderer, position = Variance.IN_VARIANCE))
|
||||
}
|
||||
append(": ${signature.returnType.render(typeRenderer, position = Variance.OUT_VARIANCE)}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
testServices.assertions.assertEqualsToTestDataFileSibling(substitutorRendered, extension = ".result.txt")
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val typeRenderer = KtTypeRendererForSource.WITH_SHORT_NAMES
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.analysis.api.standalone.fir.test.cases.generated.cases.components.substitutorProvider;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.test.util.KtTestUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.analysis.api.standalone.fir.test.AnalysisApiFirStandaloneModeTestConfiguratorFactory;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfiguratorFactoryData;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.FrontendKind;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisSessionMode;
|
||||
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiMode;
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.substitutorProvider.AbstractCreateInheritanceTypeSubstitutorTest;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
public class FirStandaloneNormalAnalysisSourceModuleCreateInheritanceTypeSubstitutorTestGenerated extends AbstractCreateInheritanceTypeSubstitutorTest {
|
||||
@NotNull
|
||||
@Override
|
||||
public AnalysisApiTestConfigurator getConfigurator() {
|
||||
return AnalysisApiFirStandaloneModeTestConfiguratorFactory.INSTANCE.createConfigurator(
|
||||
new AnalysisApiTestConfiguratorFactoryData(
|
||||
FrontendKind.Fir,
|
||||
TestModuleKind.Source,
|
||||
AnalysisSessionMode.Normal,
|
||||
AnalysisApiMode.Standalone
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllFilesPresentInCreateInheritanceTypeSubstitutor() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("chain.kt")
|
||||
public void testChain() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/chain.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("cycle.kt")
|
||||
public void testCycle() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/cycle.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("directComplexSubstitution.kt")
|
||||
public void testDirectComplexSubstitution() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/directComplexSubstitution.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("directType.kt")
|
||||
public void testDirectType() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/directType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("directTypeParam.kt")
|
||||
public void testDirectTypeParam() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/directTypeParam.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("errorInUpperBound.kt")
|
||||
public void testErrorInUpperBound() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/errorInUpperBound.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("extraTypeArgument.kt")
|
||||
public void testExtraTypeArgument() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/extraTypeArgument.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("missingTypeArgument.kt")
|
||||
public void testMissingTypeArgument() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/missingTypeArgument.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multipleInheritance.kt")
|
||||
public void testMultipleInheritance() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/multipleInheritance.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("self.kt")
|
||||
public void testSelf() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/substitutorProvider/createInheritanceTypeSubstitutor/self.kt");
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,8 @@ public abstract class KtAnalysisSession(final override val token: KtLifetimeToke
|
||||
KtSymbolProviderByJavaPsiMixIn,
|
||||
KtResolveExtensionInfoProviderMixIn,
|
||||
KtCompilerFacilityMixIn,
|
||||
KtMetadataCalculatorMixIn {
|
||||
KtMetadataCalculatorMixIn,
|
||||
KtSubstitutorProviderMixIn {
|
||||
|
||||
public abstract val useSiteModule: KtModule
|
||||
|
||||
@@ -194,6 +195,9 @@ public abstract class KtAnalysisSession(final override val token: KtLifetimeToke
|
||||
internal val typesCreator: KtTypeCreator
|
||||
get() = typesCreatorImpl
|
||||
protected abstract val typesCreatorImpl: KtTypeCreator
|
||||
|
||||
internal val substitutorProvider: KtSubstitutorProvider get() = substitutorProviderImpl
|
||||
protected abstract val substitutorProviderImpl: KtSubstitutorProvider
|
||||
}
|
||||
|
||||
public fun KtAnalysisSession.getModule(element: PsiElement): KtModule {
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.analysis.api.components
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtSubstitutor
|
||||
|
||||
public abstract class KtSubstitutorProvider : KtAnalysisSessionComponent() {
|
||||
public abstract fun createSubstitutor(
|
||||
subClass: KtClassOrObjectSymbol,
|
||||
superClass: KtClassOrObjectSymbol,
|
||||
): KtSubstitutor?
|
||||
}
|
||||
|
||||
public interface KtSubstitutorProviderMixIn : KtAnalysisSessionMixIn {
|
||||
/**
|
||||
* Creates a [KtSubstitutor] based on the inheritance relationship between [subClass] and [superClass].
|
||||
*
|
||||
* The semantic of resulted [KtSubstitutor] is the substitutor that should be applied to a member of [superClass],
|
||||
* so it can be called on an instance of [subClass].
|
||||
*
|
||||
* Basically, it's a composition of inheritance-based substitutions for all the inheritance chain.
|
||||
*
|
||||
* On the following code:
|
||||
* ```
|
||||
* class A : B<String>
|
||||
* class B<T> : C<T, Int>
|
||||
* class C<X, Y>
|
||||
* ```
|
||||
*
|
||||
* * `createInheritanceTypeSubstitutor(A, B)` returns `KtSubstitutor {T -> String}`
|
||||
* * `createInheritanceTypeSubstitutor(B, C)` returns `KtSubstitutor {X -> T, Y -> Int}`
|
||||
* * `createInheritanceTypeSubstitutor(A, C)` returns `KtSubstitutor {X -> T, Y -> Int} andThen KtSubstitutor {T -> String}`
|
||||
*
|
||||
* @param subClass the subClass or object symbol.
|
||||
* @param superClass the super class symbol.
|
||||
* @return [KtSubstitutor] if [subClass] inherits [superClass] and there are no error types in the inheritance path. Returns `null` otherwise.
|
||||
*/
|
||||
public fun createInheritanceTypeSubstitutor(
|
||||
subClass: KtClassOrObjectSymbol,
|
||||
superClass: KtClassOrObjectSymbol,
|
||||
): KtSubstitutor? = withValidityAssertion {
|
||||
analysisSession.substitutorProvider.createSubstitutor(subClass, superClass)
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
class A<caret_base>A : BB<Int, String>
|
||||
|
||||
class BB<S, T> : CC<S, T, List<T>>
|
||||
|
||||
class C<caret_super>C<X, Y, Z> {
|
||||
fun foo(): kotlin.Triple<X, Y, Z>
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Substitutor: <map substitutor: {X = S, Y = T, Z = kotlin/collections/List<T>}> then <map substitutor: {S = kotlin/Int, T = kotlin/String}>
|
||||
Substituted callables:
|
||||
foo(): Triple<Int, String, List<String>>
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class A<caret_base>A : BB<Int, String>
|
||||
|
||||
class BB<S, T> : CC<S, T, List<T>>
|
||||
|
||||
class C<caret_super>CC<X, Y, Z> : AA
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
Substitutor: null
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
class A<caret_base>A<T, S> : BB<List<T>, Map<S, Int>, S>
|
||||
|
||||
class B<caret_super>B<Q, R, T> {
|
||||
fun foo(): kotlin.Triple<Q, R, T>
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Substitutor: <map substitutor: {Q = kotlin/collections/List<T>, R = kotlin/collections/Map<S, kotlin/Int>, T = S}>
|
||||
Substituted callables:
|
||||
foo(): Triple<List<T>, Map<S, Int>, S>
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
class A<caret_base>A : BB<Int>
|
||||
|
||||
class B<caret_super>B<S> {
|
||||
fun foo(): List<S>
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Substitutor: <map substitutor: {S = kotlin/Int}>
|
||||
Substituted callables:
|
||||
foo(): List<Int>
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class A<caret_base>A<T> : BB<T>
|
||||
|
||||
class B<caret_super>B<S> {
|
||||
fun foo(): S
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Substitutor: <map substitutor: {S = T}>
|
||||
Substituted callables:
|
||||
foo(): T
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class A<caret_base>A : BB<Int>
|
||||
|
||||
class B<caret_super>B<S : String> {
|
||||
fun foo(): S
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Substitutor: <map substitutor: {S = kotlin/Int}>
|
||||
Substituted callables:
|
||||
foo(): Int
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class A<caret_base>A : BB<Int, String>
|
||||
|
||||
class B<caret_super>B<S> {
|
||||
fun foo(): S
|
||||
}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
Substitutor: null
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// WITH_STDLIB
|
||||
class A<caret_base>A : BB<Int, String>
|
||||
|
||||
class B<caret_super>B<S, Q, U> {
|
||||
fun foo(): Triple<S, Q, U>
|
||||
}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
Substitutor: null
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class A<caret_base>A : BB<Int>, BB<String>
|
||||
|
||||
class B<caret_super>B<S> {
|
||||
fun foo(): Triple<S>
|
||||
}
|
||||
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Substitutor: <map substitutor: {S = kotlin/Int}>
|
||||
Substituted callables:
|
||||
foo(): Triple<S>
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
class A<caret_base>A<caret_super>A : BB<Int, String>
|
||||
|
||||
class BB<S, T> : CC<S, T, List<T>> {
|
||||
fun foo(): Triple<S, T>
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
Substitutor: <empty substitutor>
|
||||
+7
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.signatu
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.signatureSubstitution.AbstractAnalysisApiSymbolAsSignatureTest
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.signatureSubstitution.AbstractAnalysisApiSymbolSubstitutionTest
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.smartCastProvider.AbstractHLSmartCastInfoTest
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.substitutorProvider.AbstractCreateInheritanceTypeSubstitutorTest
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.substututorFactory.AbstractSubstitutorBuilderTest
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.symbolDeclarationOverridesProvider.AbstractIsSubclassOfTest
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.symbolDeclarationOverridesProvider.AbstractOverriddenDeclarationProviderTest
|
||||
@@ -438,6 +439,12 @@ private fun AnalysisApiTestGroup.generateAnalysisApiComponentsTests() {
|
||||
}
|
||||
}
|
||||
|
||||
component("substitutorProvider", filter = frontendIs(FrontendKind.Fir)) {
|
||||
test(AbstractCreateInheritanceTypeSubstitutorTest::class) {
|
||||
model(it, "createInheritanceTypeSubstitutor")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
component("referenceResolveProvider") {
|
||||
test(AbstractIsImplicitCompanionReferenceTest::class) {
|
||||
|
||||
Reference in New Issue
Block a user