FIR: add declaration attributes checking to rawFirBuilder/lightTree tests
This commit is contained in:
@@ -19,17 +19,16 @@ import org.jetbrains.kotlin.fir.references.*
|
||||
import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
|
||||
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.*
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.fir.utils.isNotEmpty
|
||||
import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid
|
||||
import org.jetbrains.kotlin.name.SpecialNames
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
fun FirElement.renderWithType(mode: FirRenderer.RenderMode = FirRenderer.RenderMode.Normal): String = buildString {
|
||||
append(this@renderWithType)
|
||||
@@ -56,6 +55,7 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
|
||||
val renderAnnotation: Boolean,
|
||||
val renderBodies: Boolean = true,
|
||||
val renderPropertyAccessors: Boolean = true,
|
||||
val renderDeclarationAttributes: Boolean = false,
|
||||
) {
|
||||
object Normal : RenderMode(
|
||||
renderLambdaBodies = true,
|
||||
@@ -98,6 +98,15 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
|
||||
renderBodies = false,
|
||||
renderPropertyAccessors = false,
|
||||
)
|
||||
|
||||
object WithDeclarationAttributes : RenderMode(
|
||||
renderLambdaBodies = true,
|
||||
renderCallArguments = true,
|
||||
renderCallableFqNames = false,
|
||||
renderDeclarationResolvePhase = false,
|
||||
renderAnnotation = true,
|
||||
renderDeclarationAttributes = true,
|
||||
)
|
||||
}
|
||||
|
||||
private val printer = Printer(builder)
|
||||
@@ -366,6 +375,7 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
|
||||
}
|
||||
|
||||
override fun visitDeclaration(declaration: FirDeclaration) {
|
||||
declaration.renderDeclarationAttributesIfNeeded()
|
||||
declaration.renderPhaseIfNeeded()
|
||||
print(
|
||||
when (declaration) {
|
||||
@@ -389,6 +399,27 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
|
||||
}
|
||||
}
|
||||
|
||||
private fun FirDeclaration.renderDeclarationAttributesIfNeeded() {
|
||||
if (mode.renderDeclarationAttributes && attributes.arrayMap.isNotEmpty()) {
|
||||
val attributes = getAttributesWithValues().mapNotNull { (klass, value) ->
|
||||
value?.let { klass.simpleName to value.renderAsDeclarationAttributeValue() }
|
||||
}.joinToString { (name, value) -> "$name=$value" }
|
||||
print("[$attributes] ")
|
||||
}
|
||||
}
|
||||
|
||||
private fun FirDeclaration.getAttributesWithValues(): List<Pair<KClass<out FirDeclarationDataKey>, Any?>> {
|
||||
val attributesMap = FirDeclarationDataRegistry.allValuesThreadUnsafeForRendering()
|
||||
return attributesMap.entries.sortedBy { it.key.simpleName }.map { (klass, index) -> klass to attributes.arrayMap[index] }
|
||||
}
|
||||
|
||||
private fun Any.renderAsDeclarationAttributeValue() = when (this) {
|
||||
is FirCallableSymbol<*> -> callableId.toString()
|
||||
is FirClassLikeSymbol<*> -> classId.asString()
|
||||
else -> toString()
|
||||
}
|
||||
|
||||
|
||||
private fun List<FirDeclaration>.renderDeclarations() {
|
||||
renderInBraces {
|
||||
for (declaration in this) {
|
||||
@@ -490,6 +521,7 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
|
||||
print("actual ")
|
||||
}
|
||||
constructor.renderPhaseIfNeeded()
|
||||
constructor.renderDeclarationAttributesIfNeeded()
|
||||
print("constructor")
|
||||
constructor.typeParameters.renderTypeParameters()
|
||||
constructor.valueParameters.renderParameters()
|
||||
@@ -512,6 +544,7 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
|
||||
|
||||
override fun visitPropertyAccessor(propertyAccessor: FirPropertyAccessor) {
|
||||
propertyAccessor.renderPhaseIfNeeded()
|
||||
propertyAccessor.renderDeclarationAttributesIfNeeded()
|
||||
propertyAccessor.annotations.renderAnnotations()
|
||||
print(propertyAccessor.visibility.asString() + " ")
|
||||
print(if (propertyAccessor.isInline) "inline " else "")
|
||||
@@ -526,6 +559,7 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
|
||||
|
||||
override fun visitAnonymousFunction(anonymousFunction: FirAnonymousFunction) {
|
||||
anonymousFunction.renderPhaseIfNeeded()
|
||||
anonymousFunction.renderDeclarationAttributesIfNeeded()
|
||||
anonymousFunction.annotations.renderAnnotations()
|
||||
val label = anonymousFunction.label
|
||||
if (label != null) {
|
||||
@@ -625,6 +659,7 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
|
||||
|
||||
override fun visitValueParameter(valueParameter: FirValueParameter) {
|
||||
valueParameter.renderPhaseIfNeeded()
|
||||
valueParameter.renderDeclarationAttributesIfNeeded()
|
||||
valueParameter.annotations.renderAnnotations()
|
||||
if (valueParameter.isCrossinline) {
|
||||
print("crossinline ")
|
||||
|
||||
Reference in New Issue
Block a user