[LL FIR] rewrite compiler required annotations transformer to avoid lazy resolve under locks

^KT-56551
This commit is contained in:
Dmitrii Gridin
2023-05-18 13:59:31 +02:00
committed by Space Team
parent 4b1dc4eee6
commit 8f82649529
8 changed files with 344 additions and 99 deletions
@@ -220,11 +220,13 @@ object LowLevelFirApiFacadeForResolveOnAir {
fileAnnotation = annotationEntry,
replacement = replacement
)
val fileAnnotationsContainer = buildFileAnnotationsContainer {
moduleData = firFile.moduleData
containingFileSymbol = firFile.symbol
annotations += annotationCall
}
val llFirResolvableSession = firFile.llFirResolvableSession
?: buildErrorWithAttachment("FirFile session expected to be a resolvable session but was ${firFile.llFirSession::class.java}") {
withEntry("firSession", firFile.llFirSession) { it.toString() }
@@ -239,7 +241,8 @@ object LowLevelFirApiFacadeForResolveOnAir {
collector?.addFileContext(firFile, firFile.createTowerDataContext(firResolveSession.getScopeSessionFor(llFirResolvableSession)))
return annotationCall
// We should return annotation from fileAnnotationsContainer because the original annotation can be replaced
return fileAnnotationsContainer.annotations.single()
}
private fun runBodyResolveOnAir(
@@ -26,7 +26,6 @@ import org.jetbrains.kotlin.fir.visitors.FirTransformer
import org.jetbrains.kotlin.fir.visitors.transformSingle
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.name.StandardClassIds.Annotations.SinceKotlin
import org.jetbrains.kotlin.fir.types.customAnnotations
import org.jetbrains.kotlin.fir.types.forEachType
@@ -57,6 +56,18 @@ internal object FirLazyBodiesCalculator {
FirLazyAnnotationTransformerData(firElement.moduleData.session, FirLazyAnnotationTransformerScope.COMPILER_ONLY)
)
}
fun calculateLazyArgumentsForAnnotation(annotationCall: FirAnnotationCall, session: FirSession): FirArgumentList {
require(needCalculatingAnnotationCall(annotationCall))
val builder = RawFirBuilder(session, baseScopeProvider = session.kotlinScopeProvider)
val ktAnnotationEntry = annotationCall.psi as KtAnnotationEntry
builder.context.packageFqName = ktAnnotationEntry.containingKtFile.packageFqName
val newAnnotationCall = builder.buildAnnotationCall(ktAnnotationEntry)
return newAnnotationCall.argumentList
}
fun needCalculatingAnnotationCall(firAnnotationCall: FirAnnotationCall): Boolean =
firAnnotationCall.argumentList.arguments.any { it is FirLazyExpression }
}
private fun replaceValueParameterDefaultValues(valueParameters: List<FirValueParameter>, newValueParameters: List<FirValueParameter>) {
@@ -68,15 +79,6 @@ private fun replaceValueParameterDefaultValues(valueParameters: List<FirValuePar
}
}
private fun calculateLazyArgumentsForAnnotation(annotationCall: FirAnnotationCall, session: FirSession) {
require(needCalculatingAnnotationCall(annotationCall))
val builder = RawFirBuilder(session, baseScopeProvider = session.kotlinScopeProvider)
val ktAnnotationEntry = annotationCall.psi as KtAnnotationEntry
builder.context.packageFqName = ktAnnotationEntry.containingKtFile.packageFqName
val newAnnotationCall = builder.buildAnnotationCall(ktAnnotationEntry)
annotationCall.replaceArgumentList(newAnnotationCall.argumentList)
}
private fun calculateLazyBodiesForFunction(designation: FirDesignation) {
val simpleFunction = designation.target as FirSimpleFunction
require(needCalculatingLazyBodyForFunction(simpleFunction))
@@ -214,9 +216,6 @@ private fun needCalculatingLazyBodyForProperty(firProperty: FirProperty): Boolea
|| (firProperty.delegate as? FirWrappedDelegateExpression)?.expression is FirLazyExpression
|| firProperty.getExplicitBackingField()?.initializer is FirLazyExpression
private fun needCalculatingAnnotationCall(firAnnotationCall: FirAnnotationCall): Boolean =
firAnnotationCall.argumentList.arguments.any { it is FirLazyExpression }
private enum class FirLazyAnnotationTransformerScope {
ALL_ANNOTATIONS,
COMPILER_ONLY;
@@ -287,8 +286,9 @@ private abstract class FirLazyAnnotationTransformer : FirTransformer<FirLazyAnno
override fun transformAnnotationCall(annotationCall: FirAnnotationCall, data: FirLazyAnnotationTransformerData): FirStatement {
val shouldCalculate = data.compilerAnnotationsOnly == FirLazyAnnotationTransformerScope.ALL_ANNOTATIONS ||
canBeCompilerAnnotation(annotationCall, data.session)
if (shouldCalculate && needCalculatingAnnotationCall(annotationCall)) {
calculateLazyArgumentsForAnnotation(annotationCall, data.session)
if (shouldCalculate && FirLazyBodiesCalculator.needCalculatingAnnotationCall(annotationCall)) {
val newArgumentList = FirLazyBodiesCalculator.calculateLazyArgumentsForAnnotation(annotationCall, data.session)
annotationCall.replaceArgumentList(newArgumentList)
}
super.transformAnnotationCall(annotationCall, data)
@@ -13,17 +13,26 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.file.builder.LLFirLockPro
import org.jetbrains.kotlin.analysis.low.level.api.fir.lazy.resolve.FirLazyBodiesCalculator
import org.jetbrains.kotlin.analysis.low.level.api.fir.lazy.resolve.LLFirPhaseUpdater
import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.llFirSession
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.checkDeprecationProviderIsResolved
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.checkPhase
import org.jetbrains.kotlin.analysis.utils.errors.requireIsInstance
import org.jetbrains.kotlin.fir.FirAnnotationContainer
import org.jetbrains.kotlin.fir.FirElementWithResolveState
import org.jetbrains.kotlin.fir.FirFileAnnotationsContainer
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.PrivateForInline
import org.jetbrains.kotlin.fir.caches.firCachesFactory
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall
import org.jetbrains.kotlin.fir.expressions.builder.buildAnnotationCallCopy
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirTowerDataContextCollector
import org.jetbrains.kotlin.fir.resolve.transformers.plugin.CompilerRequiredAnnotationsComputationSession
import org.jetbrains.kotlin.fir.resolve.transformers.plugin.FirCompilerRequiredAnnotationsResolveTransformer
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase
import org.jetbrains.kotlin.fir.visitors.transformSingle
import org.jetbrains.kotlin.fir.types.FirUserTypeRef
internal object LLFirCompilerAnnotationsLazyResolver : LLFirLazyResolver(FirResolvePhase.COMPILER_REQUIRED_ANNOTATIONS) {
override fun resolve(
@@ -43,8 +52,12 @@ internal object LLFirCompilerAnnotationsLazyResolver : LLFirLazyResolver(FirReso
override fun checkIsResolved(target: FirElementWithResolveState) {
target.checkPhase(resolverPhase)
when (target) {
is FirClassLikeDeclaration -> checkDeprecationProviderIsResolved(target, target.deprecationsProvider)
is FirCallableDeclaration -> checkDeprecationProviderIsResolved(target, target.deprecationsProvider)
}
checkNestedDeclarationsAreResolved(target)
// TODO add proper check that COMPILER_REQUIRED_ANNOTATIONS are resolved
}
}
@@ -54,7 +67,7 @@ private class LLFirCompilerRequiredAnnotationsTargetResolver(
session: FirSession,
scopeSession: ScopeSession,
computationSession: LLFirCompilerRequiredAnnotationsComputationSession? = null,
) : LLFirTargetResolver(target, lockProvider, FirResolvePhase.COMPILER_REQUIRED_ANNOTATIONS, isJumpingPhase = true) {
) : LLFirTargetResolver(target, lockProvider, FirResolvePhase.COMPILER_REQUIRED_ANNOTATIONS, isJumpingPhase = false) {
inner class LLFirCompilerRequiredAnnotationsComputationSession : CompilerRequiredAnnotationsComputationSession() {
override fun resolveAnnotationSymbol(symbol: FirRegularClassSymbol, scopeSession: ScopeSession) {
val regularClass = symbol.fir
@@ -80,6 +93,10 @@ private class LLFirCompilerRequiredAnnotationsTargetResolver(
computationSession ?: LLFirCompilerRequiredAnnotationsComputationSession(),
)
@OptIn(PrivateForInline::class)
private val llFirComputationSession: LLFirCompilerRequiredAnnotationsComputationSession
get() = transformer.annotationTransformer.computationSession as LLFirCompilerRequiredAnnotationsComputationSession
override fun withFile(firFile: FirFile, action: () -> Unit) {
transformer.annotationTransformer.withFileAndFileScopes(firFile) {
action()
@@ -93,25 +110,211 @@ private class LLFirCompilerRequiredAnnotationsTargetResolver(
}
}
override fun doLazyResolveUnderLock(target: FirElementWithResolveState) {
FirLazyBodiesCalculator.calculateCompilerAnnotations(target)
override fun doResolveWithoutLock(target: FirElementWithResolveState): Boolean {
if (target !is FirRegularClass && !target.isRegularDeclarationWithAnnotation) {
throwUnexpectedFirElementError(target)
}
when {
target is FirRegularClass -> {
transformer.annotationTransformer.resolveRegularClass(
target,
transformChildren = {},
afterChildrenTransform = {
transformer.annotationTransformer.calculateDeprecations(target)
}
)
requireIsInstance<FirAnnotationContainer>(target)
resolveTargetDeclaration(target)
return true
}
private fun <T> resolveTargetDeclaration(target: T) where T : FirAnnotationContainer, T : FirElementWithResolveState {
// 1. Check that we should process this target
if (llFirComputationSession.annotationsAreResolved(target, treatNonSourceDeclarationsAsResolved = false)) return
// 2. Mark this target as resolved to avoid cycle
llFirComputationSession.recordThatAnnotationsAreResolved(target)
// 3. Create annotation transformer for targets we want to transform (under read lock)
// Exit if another thread is already resolved this target
val annotationTransformer = target.createAnnotationTransformer() ?: return
// 4. Exit if there are no applicable annotations, so we can just update the phase
if (annotationTransformer.isNothingToResolve()) {
return performCustomResolveUnderLock(target) {
// just update deprecations
annotationTransformer.publishResult(target)
}
}
target.isRegularDeclarationWithAnnotation -> {
target.transformSingle(transformer.annotationTransformer, null)
}
// 5. Transform annotations in the air
annotationTransformer.transformAnnotations()
else -> throwUnexpectedFirElementError(target)
// 6. Move some annotations to the proper positions
annotationTransformer.balanceAnnotations(target)
// 7. Calculate deprecations in the air
annotationTransformer.calculateDeprecations(target)
// 8. Publish result
performCustomResolveUnderLock(target) {
annotationTransformer.publishResult(target)
}
}
private fun <T> T.createAnnotationTransformer(): AnnotationTransformer? where T : FirAnnotationContainer, T : FirElementWithResolveState {
if (!hasAnnotationsToResolve()) {
return (AnnotationTransformer(mutableMapOf()))
}
val map = hashMapOf<FirElementWithResolveState, List<FirAnnotation>>()
var isUnresolved = false
withReadLock(this) {
isUnresolved = true
annotationsForTransformationTo(map)
}
return map.takeIf { isUnresolved }?.let(::AnnotationTransformer)
}
private inner class AnnotationTransformer(
private val annotationMap: MutableMap<FirElementWithResolveState, List<FirAnnotation>>,
) {
private val deprecations: MutableMap<FirElementWithResolveState, DeprecationsProvider> = hashMapOf()
fun isNothingToResolve(): Boolean = annotationMap.isEmpty()
fun transformAnnotations() {
for (annotations in annotationMap.values) {
for (annotation in annotations) {
if (annotation !is FirAnnotationCall) continue
val typeRef = annotation.annotationTypeRef as? FirUserTypeRef ?: continue
if (!transformer.annotationTransformer.shouldRunAnnotationResolve(typeRef)) continue
transformer.annotationTransformer.transformAnnotationCall(annotation, typeRef)
}
}
}
fun balanceAnnotations(target: FirElementWithResolveState) {
if (target !is FirProperty) return
val backingField = target.backingField ?: return
val updatedAnnotations = transformer.annotationTransformer.extractBackingFieldAnnotationsFromProperty(
target,
annotationMap[target].orEmpty(),
annotationMap[backingField].orEmpty(),
) ?: return
annotationMap[target] = updatedAnnotations.propertyAnnotations
annotationMap[backingField] = updatedAnnotations.backingFieldAnnotations
}
fun calculateDeprecations(target: FirElementWithResolveState) {
val session = target.llFirSession
val cacheFactory = session.firCachesFactory
if (target is FirProperty) {
deprecations[target] = target.extractDeprecationInfoPerUseSite(
session = session,
customAnnotations = annotationMap[target].orEmpty(),
getterAnnotations = target.getter?.let(annotationMap::get).orEmpty(),
setterAnnotations = target.setter?.let(annotationMap::get).orEmpty(),
).toDeprecationsProvider(cacheFactory)
}
for ((declaration, annotations) in annotationMap) {
if (declaration is FirProperty || declaration is FirFileAnnotationsContainer) continue
requireIsInstance<FirAnnotationContainer>(declaration)
deprecations[declaration] = declaration.extractDeprecationInfoPerUseSite(
session = session,
customAnnotations = annotations,
).toDeprecationsProvider(cacheFactory)
}
}
fun <T> publishResult(target: T) where T : FirElementWithResolveState, T : FirAnnotationContainer {
val newAnnotations = annotationMap[target]
if (newAnnotations != null) {
target.replaceAnnotations(newAnnotations)
}
val deprecationProvider = deprecations[target] ?: EmptyDeprecationsProvider
when (target) {
is FirClassLikeDeclaration -> target.replaceDeprecationsProvider(deprecationProvider)
is FirCallableDeclaration -> target.replaceDeprecationsProvider(deprecationProvider)
}
when (target) {
is FirFunction -> target.valueParameters.forEach(::publishResult)
is FirProperty -> {
target.getter?.let(::publishResult)
target.setter?.let(::publishResult)
target.backingField?.let(::publishResult)
}
}
}
}
/**
* @return true if at least one applicable annotation is present
*/
private fun <T> T.annotationsForTransformationTo(
map: MutableMap<FirElementWithResolveState, List<FirAnnotation>>,
) where T : FirAnnotationContainer, T : FirElementWithResolveState {
when (this) {
is FirFunction -> {
valueParameters.forEach {
it.annotationsForTransformationTo(map)
}
}
is FirProperty -> {
getter?.annotationsForTransformationTo(map)
setter?.annotationsForTransformationTo(map)
backingField?.annotationsForTransformationTo(map)
}
}
if (annotations.isEmpty()) return
var hasApplicableAnnotation = false
val containerForAnnotations = ArrayList<FirAnnotation>(annotations.size)
for (annotation in annotations) {
val userTypeRef = (annotation as? FirAnnotationCall)?.annotationTypeRef as? FirUserTypeRef
containerForAnnotations += if (userTypeRef != null && transformer.annotationTransformer.shouldRunAnnotationResolve(userTypeRef)) {
hasApplicableAnnotation = true
buildAnnotationCallCopy(annotation) {
/**
* CRA transformer won't modify this type reference,
* but we create a deep copy
* to be sure that no one another thread can't modify this reference during another phase,
* while we do deep copy during
* [org.jetbrains.kotlin.fir.resolve.transformers.plugin.AbstractFirSpecificAnnotationResolveTransformer.transformAnnotationCall]
* without lock
*/
annotationTypeRef = transformer.annotationTransformer.createDeepCopyOfTypeRef(userTypeRef)
// We assume that non-empty argument must be a lazy expression, or it is a copied declaration inside an on-air session
if (FirLazyBodiesCalculator.needCalculatingAnnotationCall(annotation)) {
argumentList = FirLazyBodiesCalculator.calculateLazyArgumentsForAnnotation(annotation, llFirSession)
}
}
} else {
annotation
}
}
if (hasApplicableAnnotation) {
map[this] = containerForAnnotations
}
}
override fun doLazyResolveUnderLock(target: FirElementWithResolveState) {
throwUnexpectedFirElementError(target)
}
}
private fun FirAnnotationContainer.hasAnnotationsToResolve(): Boolean {
if (annotations.isNotEmpty()) return true
return when (this) {
is FirFunction -> valueParameters.any(FirAnnotationContainer::hasAnnotationsToResolve)
is FirProperty -> this.getter?.hasAnnotationsToResolve() == true ||
this.setter?.hasAnnotationsToResolve() == true ||
this.backingField?.hasAnnotationsToResolve() == true
else -> false
}
}
@@ -105,6 +105,15 @@ internal fun checkDefaultValueIsResolved(parameter: FirValueParameter) {
}
}
internal fun checkDeprecationProviderIsResolved(declaration: FirDeclaration, provider: DeprecationsProvider) {
checkWithAttachmentBuilder(
condition = provider !is UnresolvedDeprecationProvider,
message = { "Unresolved deprecation provider found for ${declaration::class.simpleName}" }
) {
withFirEntry("declaration", declaration)
}
}
internal fun checkReturnTypeRefIsResolved(declaration: FirCallableDeclaration, acceptImplicitTypeRef: Boolean = false) {
checkTypeRefIsResolved(declaration.returnTypeRef, typeRefName = "return type", declaration, acceptImplicitTypeRef)
}
@@ -30,7 +30,7 @@ FILE: [ResolvedTo(IMPORTS)] fileAnnotations.kt
COMPILER_REQUIRED_ANNOTATIONS:
FILE: [ResolvedTo(IMPORTS)] fileAnnotations.kt
@FILE:Deprecated[Unresolved](String())
@FILE:R|kotlin/Deprecated|[CompilerRequiredAnnotations](String())
@FILE:Anno[Unresolved](LAZY_EXPRESSION)
[ResolvedTo(COMPILER_REQUIRED_ANNOTATIONS)] annotations container
@Target[Unresolved](LAZY_EXPRESSION) public? final? [ResolvedTo(RAW_FIR)] annotation class Anno : R|kotlin/Annotation| {
@@ -45,7 +45,7 @@ FILE: [ResolvedTo(IMPORTS)] fileAnnotations.kt
COMPANION_GENERATION:
FILE: [ResolvedTo(IMPORTS)] fileAnnotations.kt
@FILE:Deprecated[Unresolved](String())
@FILE:R|kotlin/Deprecated|[CompilerRequiredAnnotations](String())
@FILE:Anno[Unresolved](LAZY_EXPRESSION)
[ResolvedTo(COMPANION_GENERATION)] annotations container
@Target[Unresolved](LAZY_EXPRESSION) public? final? [ResolvedTo(RAW_FIR)] annotation class Anno : R|kotlin/Annotation| {
@@ -60,7 +60,7 @@ FILE: [ResolvedTo(IMPORTS)] fileAnnotations.kt
SUPER_TYPES:
FILE: [ResolvedTo(IMPORTS)] fileAnnotations.kt
@FILE:Deprecated[Unresolved](String())
@FILE:R|kotlin/Deprecated|[CompilerRequiredAnnotations](String())
@FILE:Anno[Unresolved](LAZY_EXPRESSION)
[ResolvedTo(SUPER_TYPES)] annotations container
@Target[Unresolved](LAZY_EXPRESSION) public? final? [ResolvedTo(RAW_FIR)] annotation class Anno : R|kotlin/Annotation| {