[LL FIR] FirElementBuilder: make getFirForNonBodyElement more abstract

This is required for the next step

^KT-65780
This commit is contained in:
Dmitrii Gridin
2024-02-13 15:42:35 +01:00
committed by Space Team
parent 38a3010ac8
commit 04a2d6b616
@@ -109,20 +109,20 @@ internal class FirElementBuilder(
return mappings.getFir(psi) return mappings.getFir(psi)
} }
private inline fun <T : KtElement> getFirForNonBodyElement( private inline fun <T : KtElement, E : PsiElement> getFirForNonBodyElement(
element: KtElement, element: KtElement,
anchorElementProvider: (KtElement) -> T?, anchorElementProvider: (KtElement) -> T?,
annotatedElementProvider: (T) -> KtAnnotated?, elementOwnerProvider: (T) -> E?,
resolveAndFindFirForAnchor: (FirAnnotationContainer, T) -> FirElement?, resolveAndFindFirForAnchor: (FirElementWithResolveState, T) -> FirElement?,
): FirElement? { ): FirElement? {
val anchorElement = anchorElementProvider(element) ?: return null val anchorElement = anchorElementProvider(element) ?: return null
val annotationElement = annotatedElementProvider(anchorElement) ?: return null val elementOwner = elementOwnerProvider(anchorElement) ?: return null
val firAnnotationContainer = if (annotationElement is KtFile) { val firElementContainer = if (elementOwner is KtFile) {
moduleComponents.firFileBuilder.buildRawFirFileWithCaching(annotationElement) moduleComponents.firFileBuilder.buildRawFirFileWithCaching(elementOwner)
} else { } else {
val nonLocalDeclaration = annotationElement.getNonLocalContainingOrThisDeclaration() val nonLocalDeclaration = elementOwner.getNonLocalContainingOrThisDeclaration()
if (annotationElement != nonLocalDeclaration) return null if (elementOwner != nonLocalDeclaration) return null
nonLocalDeclaration.findSourceNonLocalFirDeclaration( nonLocalDeclaration.findSourceNonLocalFirDeclaration(
firFileBuilder = moduleComponents.firFileBuilder, firFileBuilder = moduleComponents.firFileBuilder,
@@ -130,7 +130,7 @@ internal class FirElementBuilder(
) )
} }
val anchorFir = resolveAndFindFirForAnchor(firAnnotationContainer, anchorElement) ?: return null val anchorFir = resolveAndFindFirForAnchor(firElementContainer, anchorElement) ?: return null
// We use identity comparison here intentionally to check that it is exactly the object we want to find // We use identity comparison here intentionally to check that it is exactly the object we want to find
if (element === anchorElement) return anchorFir if (element === anchorElement) return anchorFir
@@ -148,17 +148,19 @@ internal class FirElementBuilder(
return modifierList?.owner as? KtDeclaration return modifierList?.owner as? KtDeclaration
} }
private fun getFirForElementInsideAnnotations(element: KtElement): FirElement? = getFirForNonBodyElement( private fun getFirForElementInsideAnnotations(
element: KtElement,
): FirElement? = getFirForNonBodyElement<KtAnnotationEntry, KtAnnotated>(
element = element, element = element,
anchorElementProvider = { it.parentOfType<KtAnnotationEntry>(withSelf = true) }, anchorElementProvider = { it.parentOfType<KtAnnotationEntry>(withSelf = true) },
annotatedElementProvider = { it.owner() }, elementOwnerProvider = { it.owner() },
resolveAndFindFirForAnchor = { declaration, anchor -> declaration.resolveAndFindAnnotation(anchor, goDeep = true) }, resolveAndFindFirForAnchor = { declaration, anchor -> declaration.resolveAndFindAnnotation(anchor, goDeep = true) },
) )
private fun getFirForElementInsideTypes(element: KtElement): FirElement? = getFirForNonBodyElement( private fun getFirForElementInsideTypes(element: KtElement): FirElement? = getFirForNonBodyElement<KtTypeReference, KtDeclaration>(
element = element, element = element,
anchorElementProvider = { it.parentsOfType<KtTypeReference>(withSelf = true).lastOrNull() }, anchorElementProvider = { it.parentsOfType<KtTypeReference>(withSelf = true).lastOrNull() },
annotatedElementProvider = { elementOwnerProvider = {
when (val parent = it.parent) { when (val parent = it.parent) {
is KtDeclaration -> parent is KtDeclaration -> parent
is KtSuperTypeListEntry, is KtConstructorCalleeExpression, is KtTypeConstraint -> parent.parentOfType<KtDeclaration>() is KtSuperTypeListEntry, is KtConstructorCalleeExpression, is KtTypeConstraint -> parent.parentOfType<KtDeclaration>()
@@ -190,8 +192,8 @@ internal class FirElementBuilder(
return firElement return firElement
} }
private fun FirAnnotationContainer.resolveAndFindTypeRefAnchor(typeReference: KtTypeReference): FirElement? { private fun FirElementWithResolveState.resolveAndFindTypeRefAnchor(typeReference: KtTypeReference): FirElement? {
requireTypeIntersectionWith<FirElementWithResolveState>() requireTypeIntersectionWith<FirAnnotationContainer>()
lazyResolveToPhase(FirResolvePhase.ANNOTATION_ARGUMENTS) lazyResolveToPhase(FirResolvePhase.ANNOTATION_ARGUMENTS)
@@ -231,11 +233,11 @@ internal class FirElementBuilder(
return null return null
} }
private fun FirAnnotationContainer.resolveAndFindAnnotation( private fun FirElementWithResolveState.resolveAndFindAnnotation(
annotationEntry: KtAnnotationEntry, annotationEntry: KtAnnotationEntry,
goDeep: Boolean = false, goDeep: Boolean = false,
): FirAnnotation? { ): FirAnnotation? {
requireTypeIntersectionWith<FirElementWithResolveState>() requireTypeIntersectionWith<FirAnnotationContainer>()
lazyResolveToPhase(FirResolvePhase.ANNOTATION_ARGUMENTS) lazyResolveToPhase(FirResolvePhase.ANNOTATION_ARGUMENTS)
findAnnotation(annotationEntry)?.let { return it } findAnnotation(annotationEntry)?.let { return it }