kotlin.reflect.jvm.internal.impl.resolve.scopes.GivenFunctionsMemberScope.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-reflect Show documentation
Show all versions of kotlin-reflect Show documentation
Kotlin Full Reflection Library
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.reflect.jvm.internal.impl.resolve.scopes
import kotlin.reflect.jvm.internal.impl.descriptors.*
import kotlin.reflect.jvm.internal.impl.incremental.components.LookupLocation
import kotlin.reflect.jvm.internal.impl.name.Name
import kotlin.reflect.jvm.internal.impl.resolve.NonReportingOverrideStrategy
import kotlin.reflect.jvm.internal.impl.resolve.OverridingUtil
import kotlin.reflect.jvm.internal.impl.storage.StorageManager
import kotlin.reflect.jvm.internal.impl.storage.getValue
import kotlin.reflect.jvm.internal.impl.utils.Printer
import kotlin.reflect.jvm.internal.impl.utils.compactIfPossible
import java.util.*
/**
* A scope that may contain some declared functions + fake-overridden functions/properties from supertypes.
*/
abstract class GivenFunctionsMemberScope(
storageManager: StorageManager,
protected val containingClass: ClassDescriptor
) : MemberScopeImpl() {
private val allDescriptors by storageManager.createLazyValue {
val fromCurrent = computeDeclaredFunctions()
fromCurrent + createFakeOverrides(fromCurrent)
}
protected abstract fun computeDeclaredFunctions(): List
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection {
if (!kindFilter.acceptsKinds(DescriptorKindFilter.CALLABLES.kindMask)) return listOf()
return allDescriptors
}
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection {
return allDescriptors.filterIsInstance().filter { it.name == name }
}
override fun getContributedVariables(name: Name, location: LookupLocation): Collection {
return allDescriptors.filterIsInstance().filter { it.name == name }
}
private fun createFakeOverrides(functionsFromCurrent: List): List {
val result = ArrayList(3)
val allSuperDescriptors = containingClass.typeConstructor.supertypes
.flatMap { it.memberScope.getContributedDescriptors() }
.filterIsInstance()
for ((name, group) in allSuperDescriptors.groupBy { it.name }) {
for ((isFunction, descriptors) in group.groupBy { it is FunctionDescriptor }) {
OverridingUtil.generateOverridesInFunctionGroup(
name,
/* membersFromSupertypes = */ descriptors,
/* membersFromCurrent = */ if (isFunction) functionsFromCurrent.filter { it.name == name } else listOf(),
containingClass,
object : NonReportingOverrideStrategy() {
override fun addFakeOverride(fakeOverride: CallableMemberDescriptor) {
OverridingUtil.resolveUnknownVisibilityForMember(fakeOverride, null)
result.add(fakeOverride)
}
override fun conflict(fromSuper: CallableMemberDescriptor, fromCurrent: CallableMemberDescriptor) {
error("Conflict in scope of $containingClass: $fromSuper vs $fromCurrent")
}
}
)
}
}
return result.compactIfPossible()
}
override fun printScopeStructure(p: Printer) {
p.println("Scope of class: $containingClass")
}
}