[Analysis API] remove old renderer code

This commit is contained in:
Ilya Kirillov
2022-10-28 10:59:21 +02:00
parent 10b593ba8c
commit b47675916f
13 changed files with 33 additions and 1753 deletions
@@ -56,7 +56,7 @@ internal class KtFe10DescConstructorSymbol(
val callableId = descriptor.callableIdIfNotLocal
if (callableId != null) {
val signature = descriptor.getSymbolPointerSignature(analysisContext)
val signature = descriptor.getSymbolPointerSignature()
return KtFe10DescFunctionLikeSymbolPointer(callableId, signature)
}
@@ -95,7 +95,7 @@ internal class KtFe10DescFunctionSymbol private constructor(
val callableId = descriptor.callableIdIfNotLocal
if (callableId != null) {
val signature = descriptor.getSymbolPointerSignature(analysisContext)
val signature = descriptor.getSymbolPointerSignature()
return KtFe10DescFunctionLikeSymbolPointer(callableId, signature)
}
@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.analysis.api.*
import org.jetbrains.kotlin.analysis.api.annotations.*
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
import org.jetbrains.kotlin.analysis.api.base.KtContextReceiver
import org.jetbrains.kotlin.analysis.api.components.KtDeclarationRendererOptions
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisContext
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.KtFe10FileSymbol
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.KtFe10PackageSymbol
@@ -21,14 +20,12 @@ import org.jetbrains.kotlin.analysis.api.descriptors.symbols.psiBased.KtFe10PsiD
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.psiBased.KtFe10PsiDefaultSetterParameterSymbol
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.psiBased.base.KtFe10PsiSymbol
import org.jetbrains.kotlin.analysis.api.descriptors.types.*
import org.jetbrains.kotlin.analysis.api.descriptors.utils.KtFe10Renderer
import org.jetbrains.kotlin.analysis.api.impl.base.KtContextReceiverImpl
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolKind
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.api.types.KtTypeNullability
import org.jetbrains.kotlin.analysis.utils.errors.unexpectedElementError
import org.jetbrains.kotlin.analysis.utils.printer.prettyPrint
import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
@@ -43,6 +40,7 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtCallElement
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.inference.CapturedType
import org.jetbrains.kotlin.resolve.constants.*
@@ -537,13 +535,9 @@ internal fun ClassDescriptor.getSupertypesWithAny(): Collection<KotlinType> {
return if (hasClassSupertype) supertypes else listOf(builtIns.anyType) + supertypes
}
internal fun DeclarationDescriptor.render(analysisContext: Fe10AnalysisContext, options: KtDeclarationRendererOptions): String {
val renderer = KtFe10Renderer(analysisContext, options)
return prettyPrint { renderer.render(this@render, this) }.trim()
}
internal fun CallableMemberDescriptor.getSymbolPointerSignature(analysisContext: Fe10AnalysisContext): String {
return render(analysisContext, KtDeclarationRendererOptions.DEFAULT)
internal fun CallableMemberDescriptor.getSymbolPointerSignature(): String {
return DescriptorRenderer.FQ_NAMES_IN_TYPES.render(this)
}
internal fun createKtInitializerValue(
@@ -41,7 +41,7 @@ class KtFe10DescFunctionLikeSymbolPointer<T : KtFunctionLikeSymbol>(
return memberScope
.getContributedDescriptors(DescriptorKindFilter.CALLABLES) { it == callableId.callableName }
.filterIsInstance<CallableMemberDescriptor>()
.firstOrNull { it.getSymbolPointerSignature(analysisContext) == signature }
.firstOrNull { it.getSymbolPointerSignature() == signature }
?.toKtCallableSymbol(analysisContext) as? T
}
}
@@ -5,13 +5,12 @@
package org.jetbrains.kotlin.analysis.api.descriptors.types.base
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeOwner
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotated
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationsList
import org.jetbrains.kotlin.analysis.api.components.KtTypeRendererOptions
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisContext
import org.jetbrains.kotlin.analysis.api.descriptors.annotations.KtFe10AnnotationsList
import org.jetbrains.kotlin.analysis.api.descriptors.utils.KtFe10TypeRenderer
import org.jetbrains.kotlin.analysis.api.descriptors.utils.KtFe10DebugTypeRenderer
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeOwner
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.utils.printer.prettyPrint
@@ -33,6 +32,6 @@ interface KtFe10Type : KtLifetimeOwner, KtAnnotated {
}
internal fun KotlinType.asStringForDebugging(): String {
val renderer = KtFe10TypeRenderer(KtTypeRendererOptions.DEFAULT, isDebugText = true)
val renderer = KtFe10DebugTypeRenderer()
return prettyPrint { renderer.render(this@asStringForDebugging, this) }
}
@@ -6,14 +6,12 @@
package org.jetbrains.kotlin.analysis.api.descriptors.utils
import org.jetbrains.kotlin.analysis.api.annotations.*
import org.jetbrains.kotlin.analysis.api.components.KtTypeRendererOptions
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.classId
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.getKtNamedAnnotationArguments
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.maybeLocalClassId
import org.jetbrains.kotlin.analysis.utils.printer.PrettyPrinter
import org.jetbrains.kotlin.analysis.utils.printer.prettyPrint
import org.jetbrains.kotlin.builtins.*
import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.descriptors.PossiblyInnerType
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
@@ -32,7 +30,7 @@ import org.jetbrains.kotlin.types.checker.NewTypeVariableConstructor
import org.jetbrains.kotlin.types.error.ErrorType
import org.jetbrains.kotlin.types.typeUtil.builtIns
internal class KtFe10TypeRenderer(private val options: KtTypeRendererOptions, private val isDebugText: Boolean = false) {
internal class KtFe10DebugTypeRenderer {
private companion object {
const val ERROR_TYPE_TEXT = "ERROR_TYPE"
}
@@ -41,18 +39,8 @@ internal class KtFe10TypeRenderer(private val options: KtTypeRendererOptions, pr
consumer.renderType(type)
}
private fun KtFe10RendererConsumer.renderType(type: KotlinType) {
if (isDebugText) {
renderTypeAnnotationsDebug(type)
} else {
renderFe10Annotations(
type.annotations,
isSingleLineAnnotations = true,
renderAnnotationWithShortNames = options.shortQualifiedNames
) { classId ->
classId != StandardClassIds.Annotations.ExtensionFunctionType
}
}
private fun PrettyPrinter.renderType(type: KotlinType) {
renderTypeAnnotationsDebug(type)
when (val unwrappedType = type.unwrap()) {
is DynamicType -> append("dynamic")
is FlexibleType -> renderFlexibleType(unwrappedType)
@@ -67,9 +55,7 @@ internal class KtFe10TypeRenderer(private val options: KtTypeRendererOptions, pr
is IntersectionTypeConstructor -> renderIntersectionType(typeConstructor)
else -> {
val descriptor = unwrappedType.constructor.declarationDescriptor
if (!isDebugText && options.renderFunctionType && descriptor is FunctionClassDescriptor) {
renderFunctionType(unwrappedType)
} else if (descriptor is TypeParameterDescriptor) {
if (descriptor is TypeParameterDescriptor) {
renderTypeParameterType(descriptor)
} else if (descriptor is ClassifierDescriptorWithTypeParameters) {
renderOrdinaryType(unwrappedType)
@@ -86,19 +72,19 @@ internal class KtFe10TypeRenderer(private val options: KtTypeRendererOptions, pr
}
}
private fun KtFe10RendererConsumer.renderTypeAnnotationsDebug(type: KotlinType) {
private fun PrettyPrinter.renderTypeAnnotationsDebug(type: KotlinType) {
val annotations = type.annotations
.filter { it.annotationClass?.classId != StandardClassIds.Annotations.ExtensionFunctionType }
printCollectionIfNotEmpty(annotations, separator = " ", postfix = " ") { renderTypeAnnotationDebug(it) }
}
private fun KtFe10RendererConsumer.renderTypeAnnotationDebug(annotation: AnnotationDescriptor) {
private fun PrettyPrinter.renderTypeAnnotationDebug(annotation: AnnotationDescriptor) {
val namedValues = annotation.getKtNamedAnnotationArguments()
renderAnnotationDebug(annotation.annotationClass?.classId, namedValues)
}
private fun KtFe10RendererConsumer.renderAnnotationDebug(classId: ClassId?, namedValues: List<KtNamedAnnotationValue>) {
private fun PrettyPrinter.renderAnnotationDebug(classId: ClassId?, namedValues: List<KtNamedAnnotationValue>) {
append("@")
if (classId != null) {
@@ -116,7 +102,7 @@ internal class KtFe10TypeRenderer(private val options: KtTypeRendererOptions, pr
}
}
private fun KtFe10RendererConsumer.renderConstantValueDebug(value: KtAnnotationValue) {
private fun PrettyPrinter.renderConstantValueDebug(value: KtAnnotationValue) {
when (value) {
is KtAnnotationApplicationValue -> renderAnnotationDebug(value.annotationValue.classId, value.annotationValue.arguments)
is KtArrayAnnotationValue ->
@@ -128,46 +114,44 @@ internal class KtFe10TypeRenderer(private val options: KtTypeRendererOptions, pr
}
}
private fun KtFe10RendererConsumer.renderFlexibleType(type: FlexibleType) {
private fun PrettyPrinter.renderFlexibleType(type: FlexibleType) {
val lowerBoundText = prettyPrint { renderType(type.lowerBound) }
val upperBoundText = prettyPrint { renderType(type.upperBound) }
append(DescriptorRenderer.COMPACT.renderFlexibleType(lowerBoundText, upperBoundText, type.builtIns))
}
private fun KtFe10RendererConsumer.renderDefinitelyNotNullType(type: DefinitelyNotNullType) {
private fun PrettyPrinter.renderDefinitelyNotNullType(type: DefinitelyNotNullType) {
renderType(type.original)
append(" & Any")
}
private fun KtFe10RendererConsumer.renderErrorType() {
private fun PrettyPrinter.renderErrorType() {
append(ERROR_TYPE_TEXT)
}
private fun KtFe10RendererConsumer.renderCapturedType(type: CapturedType) {
private fun PrettyPrinter.renderCapturedType(type: CapturedType) {
append("CapturedType(")
renderTypeProjection(type.typeProjection)
append(")")
}
private fun KtFe10RendererConsumer.renderCapturedType(type: NewCapturedType) {
private fun PrettyPrinter.renderCapturedType(type: NewCapturedType) {
append("CapturedType(")
renderTypeProjection(type.constructor.projection)
append(")")
}
private fun KtFe10RendererConsumer.renderTypeVariableType(typeConstructor: NewTypeVariableConstructor) {
private fun PrettyPrinter.renderTypeVariableType(typeConstructor: NewTypeVariableConstructor) {
val name = typeConstructor.originalTypeParameter?.name ?: SpecialNames.NO_NAME_PROVIDED
append("TypeVariable(").append(name.asString()).append(")")
}
private fun KtFe10RendererConsumer.renderIntersectionType(typeConstructor: IntersectionTypeConstructor) {
if (isDebugText) {
append("it")
}
private fun PrettyPrinter.renderIntersectionType(typeConstructor: IntersectionTypeConstructor) {
append("it")
printCollection(typeConstructor.supertypes, separator = " & ", prefix = "(", postfix = ")") { renderType(it) }
}
private fun KtFe10RendererConsumer.renderFunctionType(type: SimpleType) {
private fun PrettyPrinter.renderFunctionType(type: SimpleType) {
if (type.isSuspendFunctionType || type.isKSuspendFunctionType) {
append("suspend ")
}
@@ -193,41 +177,30 @@ internal class KtFe10TypeRenderer(private val options: KtTypeRendererOptions, pr
renderType(returnType)
}
private fun KtFe10RendererConsumer.renderTypeParameterType(descriptor: TypeParameterDescriptor) {
private fun PrettyPrinter.renderTypeParameterType(descriptor: TypeParameterDescriptor) {
append(descriptor.name.render())
}
private fun KtFe10RendererConsumer.renderOrdinaryType(type: SimpleType) {
private fun PrettyPrinter.renderOrdinaryType(type: SimpleType) {
val nestedType = KtFe10JvmTypeMapperContext.getNestedType(type)
renderTypeSegment(nestedType.root, isRoot = true)
renderTypeSegment(nestedType.root)
printCollectionIfNotEmpty(nestedType.nested, separator = ".", prefix = ".", postfix = "") { renderTypeSegment(it) }
}
private fun KtFe10RendererConsumer.renderTypeSegment(typeSegment: PossiblyInnerType, isRoot: Boolean = false) {
private fun PrettyPrinter.renderTypeSegment(typeSegment: PossiblyInnerType) {
val classifier = typeSegment.classifierDescriptor
if (isDebugText) {
append(classifier.maybeLocalClassId.asString())
} else if (isRoot) {
val classId = classifier.maybeLocalClassId
if (!options.shortQualifiedNames && !classId.packageFqName.isRoot) {
renderFqName(classId.packageFqName)
append('.')
}
renderFqName(classId.relativeClassName)
} else {
append(classifier.name.render())
}
append(classifier.maybeLocalClassId.asString())
val arguments = typeSegment.arguments
printCollectionIfNotEmpty(arguments, separator = ", ", prefix = "<", postfix = ">") { renderTypeProjection(it) }
}
private fun KtFe10RendererConsumer.renderFqName(fqName: FqName) {
private fun PrettyPrinter.renderFqName(fqName: FqName) {
printCollection(fqName.pathSegments(), separator = ".") { append(it.render()) }
}
private fun KtFe10RendererConsumer.renderTypeProjection(projection: TypeProjection) {
private fun PrettyPrinter.renderTypeProjection(projection: TypeProjection) {
if (projection.isStarProjection) {
append("*")
} else {
@@ -1,575 +0,0 @@
/*
* Copyright 2010-2021 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.descriptors.utils
import org.jetbrains.kotlin.analysis.api.components.KtDeclarationRendererOptions
import org.jetbrains.kotlin.analysis.api.components.RendererModifier
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisContext
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.classId
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.isExplicitOverride
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.ktModality
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.ktVisibility
import org.jetbrains.kotlin.analysis.utils.printer.PrettyPrinter
import org.jetbrains.kotlin.analysis.utils.printer.prettyPrint
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor
import org.jetbrains.kotlin.builtins.isFunctionType
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotated
import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor
import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue
import org.jetbrains.kotlin.resolve.calls.components.isVararg
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassNotAny
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperInterfaces
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeApproximator
import org.jetbrains.kotlin.types.typeUtil.isNullableAny
import org.jetbrains.kotlin.types.typeUtil.isUnit
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
internal typealias KtFe10RendererConsumer = PrettyPrinter
internal class KtFe10Renderer(
private val analysisContext: Fe10AnalysisContext,
private val options: KtDeclarationRendererOptions,
isDebugText: Boolean = false
) {
private companion object {
val IGNORED_VISIBILITIES: Set<Visibility> = setOf(
Visibilities.Local,
Visibilities.PrivateToThis,
Visibilities.InvisibleFake,
Visibilities.Inherited,
Visibilities.Unknown,
Visibilities.DEFAULT_VISIBILITY
)
val IGNORED_SUPERTYPES: Set<ClassId> = setOf(
StandardClassIds.Enum,
StandardClassIds.Annotation
)
}
private val typeRenderer = KtFe10TypeRenderer(options.typeRendererOptions, isDebugText)
private val typeApproximator = TypeApproximator(
analysisContext.builtIns,
analysisContext.resolveSession.languageVersionSettings
)
fun render(descriptor: DeclarationDescriptor, consumer: KtFe10RendererConsumer) {
consumer.renderDeclaration(descriptor)
}
private fun KtFe10RendererConsumer.renderType(type: KotlinType, shouldApproximate: Boolean = false) {
if (shouldApproximate) {
val approximatedType = typeApproximator.approximateToSuperType(type.unwrap(), PublicApproximatorConfiguration(localTypes = true))
?: type.takeIf { it.constructor.declarationDescriptor?.name != SpecialNames.NO_NAME_PROVIDED }
?: analysisContext.builtIns.anyType
renderType(approximatedType, shouldApproximate = false)
return
}
typeRenderer.render(type, this)
}
private fun needsParenthesis(type: KotlinType): Boolean {
if (!type.annotations.isEmpty()) {
return true
}
if (options.typeRendererOptions.renderFunctionType && type.isFunctionType) {
return true
}
val descriptor = type.constructor.declarationDescriptor
if (descriptor is FunctionClassDescriptor && descriptor.functionKind.isSuspendType) {
return true
}
return false
}
private fun KtFe10RendererConsumer.renderDeclaration(descriptor: DeclarationDescriptor) {
when (descriptor) {
is ClassifierDescriptor -> renderClassifier(descriptor)
is CallableDescriptor -> renderCallable(descriptor)
else -> error("Unexpected descriptor kind: $descriptor")
}
}
private fun KtFe10RendererConsumer.renderClassifier(descriptor: ClassifierDescriptor) {
when (descriptor) {
is TypeAliasDescriptor -> renderTypeAlias(descriptor)
is TypeParameterDescriptor -> renderTypeParameter(descriptor)
is ClassDescriptor -> {
if (descriptor.kind == ClassKind.ENUM_ENTRY) {
renderEnumEntry(descriptor)
} else {
renderClass(descriptor)
}
}
else -> error("Unexpected descriptor kind: $descriptor")
}
}
private fun KtFe10RendererConsumer.renderTypeAlias(descriptor: TypeAliasDescriptor) {
if (options.renderDeclarationHeader) {
renderAnnotations(descriptor)
renderModifiers(descriptor)
append("typealias ")
}
renderName(descriptor)
renderTypeParameters(descriptor.declaredTypeParameters)
append(" = ")
renderType(descriptor.expandedType)
}
private fun KtFe10RendererConsumer.renderTypeParameters(typeParameters: List<TypeParameterDescriptor>) {
if (typeParameters.isNotEmpty()) append('<')
printCollection(typeParameters, separator = ", ") { renderTypeParameter(it) }
if (typeParameters.isNotEmpty()) append('>')
}
private fun KtFe10RendererConsumer.renderTypeParameter(descriptor: TypeParameterDescriptor) {
renderModifier("reified", descriptor.isReified)
val variance = descriptor.variance.label
renderModifier(variance, variance.isNotEmpty())
renderAnnotations(descriptor)
renderName(descriptor)
val upperBounds = descriptor.upperBounds.filterNot { it.isNullableAny() }
if (upperBounds.isNotEmpty()) {
append(" : ")
renderType(upperBounds.first())
}
}
private fun KtFe10RendererConsumer.renderClass(descriptor: ClassDescriptor) {
if (options.renderDeclarationHeader) {
renderAnnotations(descriptor)
renderModifiers(descriptor)
if (DescriptorUtils.isAnonymousObject(descriptor)) {
append("object")
} else {
val classKeyword = when (descriptor.kind) {
ClassKind.CLASS -> "class"
ClassKind.INTERFACE -> "interface"
ClassKind.ENUM_CLASS -> "enum class"
ClassKind.ENUM_ENTRY -> error("enum entries should not be rendered via renderClass")
ClassKind.ANNOTATION_CLASS -> "annotation class"
ClassKind.OBJECT -> "object"
}
append(classKeyword)
}
}
val shouldRenderName = !descriptor.name.isSpecial
&& (!descriptor.isCompanionObject || descriptor.name != SpecialNames.DEFAULT_NAME_FOR_COMPANION_OBJECT)
if (shouldRenderName) {
append(' ')
renderName(descriptor)
}
renderTypeParameters(descriptor.declaredTypeParameters)
renderSupertypes(descriptor)
renderWhereSuffix(descriptor.declaredTypeParameters)
if (options.renderClassMembers) {
val (enumEntries, otherDeclarations) = descriptor.unsubstitutedMemberScope.getContributedDescriptors()
.filter { shouldRenderNestedDeclaration(descriptor, it) }
.partition { it is ClassDescriptor && it.kind == ClassKind.ENUM_ENTRY }
val constructors = descriptor.constructors
.filter { shouldRenderNestedDeclaration(descriptor, it) }
if (enumEntries.isNotEmpty() || otherDeclarations.isNotEmpty() || constructors.isNotEmpty()) {
append(' ')
withIndentInBraces {
printCollection(sortDeclarations(enumEntries), separator = ",\n") {
renderEnumEntry(it as ClassDescriptor)
}
if (enumEntries.isNotEmpty() && (constructors.isNotEmpty() || otherDeclarations.isNotEmpty())) {
appendLine(";\n")
}
printCollection(sortDeclarations(constructors + otherDeclarations), separator = "\n\n") {
renderDeclaration(it)
}
}
}
}
}
private fun shouldRenderNestedDeclaration(owner: ClassDescriptor, declaration: DeclarationDescriptor): Boolean {
if (declaration is CallableMemberDescriptor && declaration.kind != CallableMemberDescriptor.Kind.DECLARATION) {
return false
}
if (
declaration is ConstructorDescriptor && !DescriptorUtils.isAnonymousObject(declaration.constructedClass)
&& declaration.isPrimary && declaration.valueParameters.isEmpty() && declaration.annotations.isEmpty()
) {
if (declaration.visibility == DescriptorVisibilities.DEFAULT_VISIBILITY) {
return false
} else if (owner.kind == ClassKind.OBJECT || owner.kind == ClassKind.ENUM_CLASS) {
return false
}
}
if (declaration is FunctionDescriptor && owner.kind == ClassKind.ENUM_CLASS) {
if (declaration.name.asString() == "valueOf" && KotlinBuiltIns.isString(declaration.valueParameters.singleOrNull()?.type)) {
return false
} else if (declaration.name.asString() == "values" && declaration.valueParameters.isEmpty()) {
return false
}
}
return true
}
private fun <T : DeclarationDescriptor> sortDeclarations(declarations: List<T>): List<T> {
if (!options.sortNestedDeclarations) {
return declarations
}
fun getDeclarationKind(declaration: DeclarationDescriptor): Int = when (declaration) {
is ConstructorDescriptor -> if (declaration.isPrimary) 1 else 2
is PropertyDescriptor -> 3
is FunctionDescriptor -> 4
else -> 5
}
return declarations.sortedWith(Comparator { left, right ->
val kindResult = getDeclarationKind(left) - getDeclarationKind(right)
if (kindResult != 0) {
return@Comparator kindResult
}
val nameResult = left.name.asString().compareTo(right.name.asString())
if (nameResult != 0) {
return@Comparator nameResult
}
val leftString = prettyPrint { renderDeclaration(left) }
val rightString = prettyPrint { renderDeclaration(right) }
return@Comparator leftString.compareTo(rightString)
})
}
private fun KtFe10RendererConsumer.renderSupertypes(descriptor: ClassDescriptor) {
val allowedSuperClasses = (listOfNotNull(descriptor.getSuperClassNotAny()) + descriptor.getSuperInterfaces())
.filterTo(HashSet()) { it.classId !in IGNORED_SUPERTYPES }
val supertypes = descriptor.typeConstructor.supertypes.filter { it.constructor.declarationDescriptor in allowedSuperClasses }
printCollectionIfNotEmpty(supertypes, separator = ", ", prefix = " : ") { renderType(it) }
}
private fun KtFe10RendererConsumer.renderCallable(descriptor: CallableDescriptor) {
when (descriptor) {
is SyntheticFieldDescriptor -> renderSyntheticFieldDescriptor()
is PropertyGetterDescriptor -> renderPropertyAccessor(descriptor)
is PropertySetterDescriptor -> renderPropertyAccessor(descriptor)
is PropertyDescriptor -> renderProperty(descriptor)
is ConstructorDescriptor -> renderConstructor(descriptor)
is FunctionDescriptor -> renderFunction(descriptor)
is ValueParameterDescriptor -> renderValueParameter(descriptor)
is LocalVariableDescriptor -> renderLocalVariable(descriptor)
else -> error("Unexpected descriptor kind: $descriptor")
}
}
private fun KtFe10RendererConsumer.renderSyntheticFieldDescriptor() {
append("field")
}
private fun KtFe10RendererConsumer.renderPropertyAccessor(descriptor: PropertyAccessorDescriptor) {
if (options.renderDeclarationHeader) {
renderAnnotations(descriptor)
renderModifiers(descriptor)
}
when (descriptor) {
is PropertyGetterDescriptor -> {
append("get()")
}
is PropertySetterDescriptor -> {
append("set(")
val valueParameter = descriptor.valueParameters.singleOrNull()
if (valueParameter != null) {
val name = valueParameter.name.takeIf { !it.isSpecial } ?: Name.identifier("value")
renderValueParameter(valueParameter, name)
}
append(")")
}
}
}
private fun KtFe10RendererConsumer.renderEnumEntry(descriptor: ClassDescriptor) {
assert(descriptor.kind == ClassKind.ENUM_ENTRY)
renderName(descriptor)
}
private fun KtFe10RendererConsumer.renderLocalVariable(descriptor: LocalVariableDescriptor) {
if (options.renderDeclarationHeader) {
renderAnnotations(descriptor)
append(if (descriptor.isVar) "var" else "val").append(' ')
}
renderName(descriptor)
append(": ")
renderType(descriptor.type, shouldApproximate = options.approximateTypes)
}
private fun KtFe10RendererConsumer.renderProperty(descriptor: PropertyDescriptor) {
if (options.renderDeclarationHeader) {
renderAnnotations(descriptor)
renderModifiers(descriptor)
append(if (descriptor.isVar) "var" else "val").append(' ')
renderTypeParameters(descriptor.typeParameters)
if (descriptor.typeParameters.isNotEmpty()) {
append(' ')
}
}
renderReceiver(descriptor)
renderName(descriptor)
append(": ")
renderType(descriptor.type, shouldApproximate = options.approximateTypes)
renderWhereSuffix(descriptor.typeParameters)
if (options.renderClassMembers) {
fun shouldRenderAccessor(accessor: PropertyAccessorDescriptor): Boolean {
return !accessor.annotations.isEmpty()
|| accessor.visibility != descriptor.visibility
}
val getter = descriptor.getter
val setter = descriptor.setter
val shouldRenderAccessors = (getter != null && shouldRenderAccessor(getter)) || (setter != null && shouldRenderAccessor(setter))
if (shouldRenderAccessors) {
withIndent {
if (getter != null) {
appendLine()
renderPropertyAccessor(getter)
}
if (setter != null) {
appendLine()
renderPropertyAccessor(setter)
}
}
}
}
}
private fun KtFe10RendererConsumer.renderReceiver(descriptor: CallableDescriptor) {
val extensionReceiver = descriptor.extensionReceiverParameter ?: return
val needsParentheses = !extensionReceiver.annotations.isEmpty() || needsParenthesis(extensionReceiver.type)
if (needsParentheses) {
append('(')
}
renderType(extensionReceiver.type)
if (needsParentheses) {
append(')')
}
append('.')
}
private fun KtFe10RendererConsumer.renderConstructor(descriptor: ConstructorDescriptor) {
if (options.renderDeclarationHeader) {
renderAnnotations(descriptor)
}
append("constructor")
renderValueParameters(descriptor.valueParameters)
}
private fun KtFe10RendererConsumer.renderFunction(descriptor: FunctionDescriptor) {
if (options.renderDeclarationHeader) {
renderAnnotations(descriptor)
renderModifiers(descriptor)
append("fun ")
renderTypeParameters(descriptor.typeParameters)
if (descriptor.typeParameters.isNotEmpty()) {
append(" ")
}
}
renderReceiver(descriptor)
renderName(descriptor)
renderValueParameters(descriptor.valueParameters)
val returnType = descriptor.returnType
if (returnType != null && !returnType.isUnit()) {
append(": ")
renderType(returnType, shouldApproximate = options.approximateTypes)
}
renderWhereSuffix(descriptor.typeParameters)
}
private fun KtFe10RendererConsumer.renderWhereSuffix(descriptors: List<TypeParameterDescriptor>) {
val upperBoundStrings = ArrayList<String>(0)
for (typeParameter in descriptors) {
typeParameter.upperBounds
.drop(1) // first parameter is rendered by renderTypeParameter
.mapTo(upperBoundStrings) { typeParameter.name.render() + " : " + prettyPrint { renderType(it) } }
}
if (upperBoundStrings.isNotEmpty()) {
append(" where ")
upperBoundStrings.joinTo(this, ", ")
append(' ')
}
}
private fun KtFe10RendererConsumer.renderValueParameters(valueParameters: List<ValueParameterDescriptor>) {
printCollection(valueParameters, separator = ", ", prefix = "(", postfix = ")") { renderValueParameter(it) }
}
private fun KtFe10RendererConsumer.renderValueParameter(descriptor: ValueParameterDescriptor, name: Name = descriptor.name) {
if (options.renderDeclarationHeader) {
renderAnnotations(descriptor)
}
renderModifiers(descriptor)
append(name.render())
append(": ")
renderType(descriptor.varargElementType ?: descriptor.type)
if (options.renderDefaultParameterValue && descriptor.hasDefaultValue()) {
append(" = ...")
}
}
private fun KtFe10RendererConsumer.renderName(descriptor: DeclarationDescriptor) {
append(descriptor.name.render())
}
private fun KtFe10RendererConsumer.renderAnnotations(declaration: Annotated, predicate: (ClassId) -> Boolean = { true }) {
if (RendererModifier.ANNOTATIONS !in options.modifiers) {
return
}
val isSingleLineAnnotations = declaration is ValueParameterDescriptor || declaration is TypeParameterDescriptor
renderFe10Annotations(
declaration.annotations,
isSingleLineAnnotations,
renderAnnotationWithShortNames = options.typeRendererOptions.shortQualifiedNames,
predicate
)
}
private fun KtFe10RendererConsumer.renderModifiers(descriptor: DeclarationDescriptor) {
if (descriptor is MemberDescriptor) {
renderVisibility(descriptor)
}
if (descriptor is PropertyDescriptor) {
renderModifier("const", descriptor.isConst, RendererModifier.CONST)
renderModifier("lateinit", descriptor.isLateInit, RendererModifier.LATEINIT)
}
if (descriptor is ValueParameterDescriptor) {
renderModifier("crossinline", descriptor.isCrossinline)
renderModifier("noinline", descriptor.isNoinline)
}
if (descriptor is MemberDescriptor) {
renderModifier("external", descriptor.isExternal)
renderModifier("expect", descriptor.isExpect, RendererModifier.EXPECT)
renderModifier("actual", descriptor.isActual, RendererModifier.ACTUAL)
renderModality(descriptor)
}
if (descriptor is CallableMemberDescriptor) {
renderModifier("override", descriptor !is DeserializedDescriptor && descriptor.isExplicitOverride, RendererModifier.OVERRIDE)
}
if (descriptor is ValueParameterDescriptor) {
renderModifier("vararg", descriptor.isVararg)
}
if (descriptor is FunctionDescriptor) {
renderModifier("tailrec", descriptor.isTailrec)
renderModifier("suspend", descriptor.isSuspend)
renderModifier("inline", descriptor.isInline, RendererModifier.INLINE)
renderModifier("infix", descriptor.isInfix)
renderModifier("operator", descriptor.isOperator, RendererModifier.OPERATOR)
}
if (descriptor is ClassDescriptor) {
renderModifier("inner", descriptor.isInner, RendererModifier.INNER)
renderModifier("data", descriptor.isData, RendererModifier.DATA)
renderModifier("inline", descriptor.isInline, RendererModifier.INNER)
renderModifier("fun", descriptor.isFun, RendererModifier.FUN)
renderModifier("companion", descriptor.isCompanionObject)
}
}
private fun KtFe10RendererConsumer.renderVisibility(descriptor: DeclarationDescriptorWithVisibility) {
val visibility = descriptor.ktVisibility
renderModifier(visibility.internalDisplayName, visibility !in IGNORED_VISIBILITIES, RendererModifier.VISIBILITY)
}
private fun KtFe10RendererConsumer.renderModality(descriptor: MemberDescriptor) {
if (descriptor is PropertyAccessorDescriptor || (descriptor is CallableMemberDescriptor && descriptor.isExplicitOverride)) {
return
}
val modality = descriptor.ktModality
renderModifier(
modality.name.toLowerCaseAsciiOnly(),
modality != getDefaultModality(descriptor),
RendererModifier.MODALITY
)
}
private fun getDefaultModality(descriptor: MemberDescriptor): Modality {
when (descriptor) {
is ClassDescriptor -> return if (descriptor.kind == ClassKind.INTERFACE) Modality.ABSTRACT else Modality.FINAL
is CallableMemberDescriptor -> {
val containingDeclaration = descriptor.containingDeclaration
if (containingDeclaration !is ClassDescriptor) {
return Modality.FINAL
}
if (descriptor.isExplicitOverride && containingDeclaration.ktModality != Modality.FINAL) {
return Modality.OPEN
} else if (containingDeclaration.kind == ClassKind.INTERFACE && descriptor.visibility != DescriptorVisibilities.PRIVATE) {
return if (descriptor.ktModality == Modality.ABSTRACT || descriptor.kind == CallableMemberDescriptor.Kind.DELEGATION)
Modality.ABSTRACT
else
Modality.OPEN
}
return Modality.FINAL
}
else -> return Modality.FINAL
}
}
private fun KtFe10RendererConsumer.renderModifier(text: String, state: Boolean, modifier: RendererModifier? = null) {
if (state && (modifier == null || modifier in options.modifiers)) {
append(text).append(' ')
}
}
}