Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 org.jetbrains.kotlin.resolve.lazyimport org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotationsimport org.jetbrains.kotlin.incremental.components.LookupLocationimport org.jetbrains.kotlin.name.FqNameimport org.jetbrains.kotlin.name.Nameimport org.jetbrains.kotlin.psi.KtFileimport org.jetbrains.kotlin.psi.KtImportDirectiveimport org.jetbrains.kotlin.psi.KtImportsFactoryimport org.jetbrains.kotlin.resolve.BindingTraceimport org.jetbrains.kotlin.resolve.QualifiedExpressionResolverimport org.jetbrains.kotlin.resolve.TemporaryBindingTraceimport org.jetbrains.kotlin.resolve.bindingContextUtil.recordScope
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilterimport org.jetbrains.kotlin.resolve.scopes.ImportingScopeimport org.jetbrains.kotlin.resolve.scopes.LexicalScopeimport org.jetbrains.kotlin.resolve.scopes.SubpackagesImportingScopeimport org.jetbrains.kotlin.resolve.source.KotlinSourceElementimport org.jetbrains.kotlin.storage.StorageManagerimport org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.types.TypeSubstitutorimport org.jetbrains.kotlin.utils.Printerimport org.jetbrains.kotlin.utils.addToStdlib.check
data classFileScopes(val lexicalScope: LexicalScope, val importingScope: ImportingScope, val importResolver: ImportResolver)classFileScopeFactory(
private val topLevelDescriptorProvider: TopLevelDescriptorProvider,
private val storageManager: StorageManager,
private val moduleDescriptor: ModuleDescriptor,
private val qualifiedExpressionResolver: QualifiedExpressionResolver,
private val bindingTrace: BindingTrace,
private val ktImportsFactory: KtImportsFactory) {
privateval defaultImports by storageManager.createLazyValue {
ktImportsFactory.createImportDirectives(moduleDescriptor.defaultImports)
}
fun createScopesForFile(file: KtFile, existingImports: ImportingScope? = null): FileScopes {
val debugName = "LazyFileScope for file " + file.name
val tempTrace = TemporaryBindingTrace.create(bindingTrace, "Transient trace for default imports lazy resolve")
val imports = file.importDirectives
val aliasImportNames = imports.mapNotNull { if (it.aliasName != null) it.importedFqName elsenull }
val packageView = moduleDescriptor.getPackage(file.packageFqName)
val packageFragment = topLevelDescriptorProvider.getPackageFragment(file.packageFqName)
?: error("Could not find fragment ${file.packageFqName} for file ${file.name}")
fun createImportResolver(indexedImports: IndexedImports, trace: BindingTrace)
= LazyImportResolver(storageManager, qualifiedExpressionResolver, moduleDescriptor, indexedImports, aliasImportNames, trace, packageFragment)
val explicitImportResolver = createImportResolver(ExplicitImportsIndexed(imports), bindingTrace)
val allUnderImportResolver = createImportResolver(AllUnderImportsIndexed(imports), bindingTrace)
val defaultImportsFiltered = if (aliasImportNames.isEmpty()) { // optimization
defaultImports
}
else {
defaultImports.filter { it.isAllUnder || it.importedFqName !in aliasImportNames }
}
val defaultExplicitImportResolver = createImportResolver(ExplicitImportsIndexed(defaultImportsFiltered), tempTrace)
val defaultAllUnderImportResolver = createImportResolver(AllUnderImportsIndexed(defaultImportsFiltered), tempTrace)
val dummyContainerDescriptor = DummyContainerDescriptor(file, packageFragment)
var scope: ImportingScope
scope = LazyImportScope(existingImports, defaultAllUnderImportResolver, LazyImportScope.FilteringKind.INVISIBLE_CLASSES,
"Default all under imports in $debugName (invisible classes only)")
scope = LazyImportScope(scope, allUnderImportResolver, LazyImportScope.FilteringKind.INVISIBLE_CLASSES,
"All under imports in $debugName (invisible classes only)")
scope = currentPackageScope(packageView, aliasImportNames, dummyContainerDescriptor, FilteringKind.INVISIBLE_CLASSES, scope)
scope = LazyImportScope(scope, defaultAllUnderImportResolver, LazyImportScope.FilteringKind.VISIBLE_CLASSES,
"Default all under imports in $debugName (visible classes)")
scope = LazyImportScope(scope, allUnderImportResolver, LazyImportScope.FilteringKind.VISIBLE_CLASSES,
"All under imports in $debugName (visible classes)")
scope = LazyImportScope(scope, defaultExplicitImportResolver, LazyImportScope.FilteringKind.ALL,
"Default explicit imports in $debugName")
scope = SubpackagesImportingScope(scope, moduleDescriptor, FqName.ROOT)
scope = currentPackageScope(packageView, aliasImportNames, dummyContainerDescriptor, FilteringKind.VISIBLE_CLASSES, scope)
val importingScope = LazyImportScope(scope, explicitImportResolver, LazyImportScope.FilteringKind.ALL, "Explicit imports in $debugName")
val lexicalScope = LexicalScope.empty(importingScope, packageFragment)
val importResolver = object: ImportResolver {
override fun forceResolveAllImports() {
explicitImportResolver.forceResolveAllImports()
allUnderImportResolver.forceResolveAllImports()
}
override fun forceResolveImport(importDirective: KtImportDirective) {
if (importDirective.isAllUnder) {
allUnderImportResolver.forceResolveImport(importDirective)
}
else {
explicitImportResolver.forceResolveImport(importDirective)
}
}
}
returnFileScopes(lexicalScope, importingScope, importResolver)
}
private enum classFilteringKind{
VISIBLE_CLASSES, INVISIBLE_CLASSES
}
private fun currentPackageScope(
packageView: PackageViewDescriptor,
aliasImportNames: Collection,
fromDescriptor: DummyContainerDescriptor,
filteringKind: FilteringKind,
parentScope: ImportingScope
): ImportingScope {
val scope = packageView.memberScope
val packageName = packageView.fqName
val excludedNames = aliasImportNames.mapNotNull { if (it.parent() == packageName) it.shortName() elsenull }
returnobject: ImportingScope {
overrideval parent: ImportingScope? = parentScope
override fun getContributedPackage(name: Name) = nulloverride fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? {
if (name in excludedNames) returnnullval classifier = scope.getContributedClassifier(name, location) ?: returnnullval visible = Visibilities.isVisibleIgnoringReceiver(classifier as ClassDescriptor, fromDescriptor)
return classifier.check { filteringKind == if (visible) FilteringKind.VISIBLE_CLASSESelseFilteringKind.INVISIBLE_CLASSES }
}
override fun getContributedVariables(name: Name, location: LookupLocation): Collection {
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return listOf()
if (name in excludedNames) return emptyList()
return scope.getContributedVariables(name, location)
}
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection {
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return listOf()
if (name in excludedNames) return emptyList()
return scope.getContributedFunctions(name, location)
}
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection {
// we do not perform any filtering by visibility here because all descriptors from both visible/invisible filter scopes are to be added anywayif (filteringKind == FilteringKind.INVISIBLE_CLASSES) return listOf()
return scope.getContributedDescriptors(
kindFilter.withoutKinds(DescriptorKindFilter.PACKAGES_MASK),
{ name -> name !in excludedNames && nameFilter(name) }
).filter { it !is PackageViewDescriptor } // subpackages of the current package not accessible by the short name
}
override fun toString() = "Scope for current package (${filteringKind.name})"override fun printStructure(p: Printer) {
p.println(this.toString())
}
}
}
// we use this dummy implementation of DeclarationDescriptor to check accessibility of symbols from the current packageprivateclassDummyContainerDescriptor(private val file: KtFile, private val packageFragment: PackageFragmentDescriptor) : DeclarationDescriptorNonRoot {
privateval sourceElement = KotlinSourceElement(file)
override fun getContainingDeclaration() = packageFragment
override fun getSource() = sourceElement
override fun getOriginal() = thisoverride fun getAnnotations() = Annotations.EMPTYoverride fun substitute(substitutor: TypeSubstitutor) = thisoverride fun accept(visitor: DeclarationDescriptorVisitor?, data: D): R {
throwUnsupportedOperationException()
}
override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) {
throwUnsupportedOperationException()
}
override fun getName(): Name {
throwUnsupportedOperationException()
}
}
}