KT-2752: rename FQNPart to SuggestedName and FQNGenerator to NameSuggestion
This commit is contained in:
+11
-11
@@ -33,10 +33,10 @@ import java.util.*
|
||||
* fully-qualified names for static declarations.
|
||||
*
|
||||
* A new instance of this class can be created for each request, however, it's recommended to use shared instance, since
|
||||
* [FQNGenerator] supports caching.
|
||||
* [NameSuggestion] supports caching.
|
||||
*/
|
||||
class FQNGenerator {
|
||||
private val cache: MutableMap<DeclarationDescriptor, FQNPart?> = WeakHashMap()
|
||||
class NameSuggestion {
|
||||
private val cache: MutableMap<DeclarationDescriptor, SuggestedName?> = WeakHashMap()
|
||||
|
||||
/**
|
||||
* Generates names for declarations. Name consist of the following parts:
|
||||
@@ -57,12 +57,12 @@ class FQNGenerator {
|
||||
* list consists of exactly one string for any declaration except for package. Package name lists
|
||||
* have at least one string.
|
||||
*/
|
||||
fun generate(descriptor: DeclarationDescriptor) = cache.getOrPut(descriptor) { generateCacheMiss(descriptor.original) }
|
||||
fun suggest(descriptor: DeclarationDescriptor) = cache.getOrPut(descriptor) { generate(descriptor.original) }
|
||||
|
||||
private fun generateCacheMiss(descriptor: DeclarationDescriptor): FQNPart? {
|
||||
private fun generate(descriptor: DeclarationDescriptor): SuggestedName? {
|
||||
// Members of companion objects of classes are treated as static members of these classes
|
||||
if (isNativeObject(descriptor) && isCompanionObject(descriptor)) {
|
||||
return generate(descriptor.containingDeclaration!!)
|
||||
return suggest(descriptor.containingDeclaration!!)
|
||||
}
|
||||
|
||||
when (descriptor) {
|
||||
@@ -71,7 +71,7 @@ class FQNGenerator {
|
||||
|
||||
is PackageFragmentDescriptor -> {
|
||||
return if (!descriptor.name.isSpecial) {
|
||||
FQNPart(descriptor.fqName.pathSegments().map { it.asString() }, true, descriptor, descriptor.containingDeclaration)
|
||||
SuggestedName(descriptor.fqName.pathSegments().map { it.asString() }, true, descriptor, descriptor.containingDeclaration)
|
||||
}
|
||||
else {
|
||||
// Root packages are similar to modules
|
||||
@@ -80,12 +80,12 @@ class FQNGenerator {
|
||||
}
|
||||
|
||||
// It's a special case when an object has `invoke` operator defined, in this case we simply generate object itself
|
||||
is FakeCallableDescriptorForObject -> return generate(descriptor.getReferencedDescriptor())
|
||||
is FakeCallableDescriptorForObject -> return suggest(descriptor.getReferencedDescriptor())
|
||||
|
||||
// For primary constructors and constructors of native classes we generate references to containing classes
|
||||
is ConstructorDescriptor -> {
|
||||
if (descriptor.isPrimary || isNativeObject(descriptor)) {
|
||||
return generate(descriptor.containingDeclaration)
|
||||
return suggest(descriptor.containingDeclaration)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,12 +93,12 @@ class FQNGenerator {
|
||||
is CallableDescriptor ->
|
||||
if (DescriptorUtils.isDescriptorWithLocalVisibility(descriptor)) {
|
||||
val name = getMangledName(getSuggestedName(descriptor), descriptor)
|
||||
return FQNPart(listOf(name.first), false, descriptor, descriptor.containingDeclaration)
|
||||
return SuggestedName(listOf(name.first), false, descriptor, descriptor.containingDeclaration)
|
||||
}
|
||||
}
|
||||
|
||||
val (localName, shared, parent) = getLocalName(descriptor)
|
||||
return FQNPart(listOf(localName), shared, descriptor, fixParent(parent))
|
||||
return SuggestedName(listOf(localName), shared, descriptor, fixParent(parent))
|
||||
}
|
||||
|
||||
// Getters and setters have generation strategy similar to common declarations, except for they are declared as
|
||||
+1
-1
@@ -18,4 +18,4 @@ package org.jetbrains.kotlin.js.naming
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
|
||||
class FQNPart(val names: List<String>, val shared: Boolean, val descriptor: DeclarationDescriptor, val scope: DeclarationDescriptor)
|
||||
class SuggestedName(val names: List<String>, val stable: Boolean, val descriptor: DeclarationDescriptor, val scope: DeclarationDescriptor)
|
||||
+13
-13
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.js.resolve.diagnostics
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
|
||||
import org.jetbrains.kotlin.js.naming.FQNGenerator
|
||||
import org.jetbrains.kotlin.js.naming.NameSuggestion
|
||||
import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
@@ -29,7 +29,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
|
||||
class JsNameClashChecker : SimpleDeclarationChecker {
|
||||
private val fqnGenerator = FQNGenerator()
|
||||
private val nameSuggestion = NameSuggestion()
|
||||
private val scopes = mutableMapOf<DeclarationDescriptor, MutableMap<String, DeclarationDescriptor>>()
|
||||
private val clashedFakeOverrides = mutableMapOf<DeclarationDescriptor, Pair<DeclarationDescriptor, DeclarationDescriptor>>()
|
||||
private val clashedDescriptors = mutableSetOf<DeclarationDescriptor>()
|
||||
@@ -46,12 +46,12 @@ class JsNameClashChecker : SimpleDeclarationChecker {
|
||||
}
|
||||
|
||||
private fun checkDescriptor(descriptor: DeclarationDescriptor, declaration: KtDeclaration, diagnosticHolder: DiagnosticSink) {
|
||||
val fqn = fqnGenerator.generate(descriptor)!!
|
||||
if (fqn.shared && fqn.scope is ClassOrPackageFragmentDescriptor && isOpaque(fqn.descriptor)) {
|
||||
val scope = getScope(fqn.scope)
|
||||
val name = fqn.names.last()
|
||||
val suggested = nameSuggestion.suggest(descriptor)!!
|
||||
if (suggested.stable && suggested.scope is ClassOrPackageFragmentDescriptor && isOpaque(suggested.descriptor)) {
|
||||
val scope = getScope(suggested.scope)
|
||||
val name = suggested.names.last()
|
||||
val existing = scope[name]
|
||||
if (existing != null && existing != fqn.descriptor) {
|
||||
if (existing != null && existing != suggested.descriptor) {
|
||||
diagnosticHolder.report(ErrorsJs.JS_NAME_CLASH.on(declaration, name, existing))
|
||||
val existingDeclaration = existing.findPsi() ?: declaration
|
||||
if (clashedDescriptors.add(existing) && existingDeclaration is KtDeclaration && existingDeclaration != declaration) {
|
||||
@@ -60,13 +60,13 @@ class JsNameClashChecker : SimpleDeclarationChecker {
|
||||
}
|
||||
}
|
||||
|
||||
val fqnDescriptor = fqn.descriptor
|
||||
val fqnDescriptor = suggested.descriptor
|
||||
if (fqnDescriptor is ClassDescriptor) {
|
||||
val fakeOverrides = fqnDescriptor.defaultType.memberScope.getContributedDescriptors().asSequence()
|
||||
.mapNotNull { it as? CallableMemberDescriptor }
|
||||
.filter { it.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE }
|
||||
for (override in fakeOverrides) {
|
||||
val overrideFqn = fqnGenerator.generate(override)!!
|
||||
val overrideFqn = nameSuggestion.suggest(override)!!
|
||||
val scope = getScope(overrideFqn.scope)
|
||||
val name = overrideFqn.names.last()
|
||||
val existing = scope[name]
|
||||
@@ -114,8 +114,8 @@ class JsNameClashChecker : SimpleDeclarationChecker {
|
||||
}
|
||||
}
|
||||
|
||||
val fqn = fqnGenerator.generate(descriptor) ?: return
|
||||
if (fqn.shared && isOpaque(fqn.descriptor)) {
|
||||
val fqn = nameSuggestion.suggest(descriptor) ?: return
|
||||
if (fqn.stable && isOpaque(fqn.descriptor)) {
|
||||
target[fqn.names.last()] = fqn.descriptor
|
||||
(fqn.descriptor as? CallableMemberDescriptor)?.let { checkOverrideClashes(it, target) }
|
||||
}
|
||||
@@ -125,8 +125,8 @@ class JsNameClashChecker : SimpleDeclarationChecker {
|
||||
var overridden = descriptor.overriddenDescriptors
|
||||
while (overridden.isNotEmpty()) {
|
||||
for (overridenDescriptor in overridden) {
|
||||
val overriddenFqn = fqnGenerator.generate(overridenDescriptor)!!
|
||||
if (overriddenFqn.shared) {
|
||||
val overriddenFqn = nameSuggestion.suggest(overridenDescriptor)!!
|
||||
if (overriddenFqn.stable) {
|
||||
val existing = target[overriddenFqn.names.last()]
|
||||
if (existing != null) {
|
||||
if (existing != descriptor && descriptor.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) {
|
||||
|
||||
Reference in New Issue
Block a user