From e2b7b79fa121335112137b440f7dbfbedc836470 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 22 Oct 2014 17:22:14 +0400 Subject: [PATCH] Converting JetScope's implementations to Kotlin (in progress) --- .../resolve/scopes/ClassObjectMixinScope.java | 48 --- .../resolve/scopes/ClassObjectMixinScope.kt | 33 +++ .../resolve/scopes/WritableScopeImpl.java | 4 +- .../scopes/WritableScopeWithImports.java | 277 ------------------ .../scopes/WritableScopeWithImports.kt | 240 +++++++++++++++ .../descriptors/impl/SubpackagesScope.java | 74 ----- .../lang/descriptors/impl/SubpackagesScope.kt | 56 ++++ .../resolve/scopes/AbstractScopeAdapter.java | 102 ------- .../resolve/scopes/AbstractScopeAdapter.kt | 79 +++++ .../scopes/InnerClassesScopeWrapper.java | 74 ----- .../scopes/InnerClassesScopeWrapper.kt | 39 +++ .../jet/lang/resolve/scopes/JetScopeImpl.java | 85 ------ .../jet/lang/resolve/scopes/JetScopeImpl.kt | 45 +++ ...yScopeAdapter.java => LazyScopeAdapter.kt} | 22 +- 14 files changed, 499 insertions(+), 679 deletions(-) delete mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ClassObjectMixinScope.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ClassObjectMixinScope.kt delete mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.kt delete mode 100644 core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SubpackagesScope.java create mode 100644 core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SubpackagesScope.kt delete mode 100644 core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/AbstractScopeAdapter.java create mode 100644 core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/AbstractScopeAdapter.kt delete mode 100644 core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/InnerClassesScopeWrapper.java create mode 100644 core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/InnerClassesScopeWrapper.kt delete mode 100644 core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.java create mode 100644 core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.kt rename core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/{LazyScopeAdapter.java => LazyScopeAdapter.kt} (53%) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ClassObjectMixinScope.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ClassObjectMixinScope.java deleted file mode 100644 index ea75f986945..00000000000 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ClassObjectMixinScope.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.lang.resolve.scopes; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.descriptors.ClassDescriptor; -import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor; - -import java.util.Collections; -import java.util.List; - -/** - * Members of the class object are accessible from the class. - * Scope lazily delegates requests to class object scope. - */ -public class ClassObjectMixinScope extends AbstractScopeAdapter { - private final ClassDescriptor classObjectDescriptor; - - public ClassObjectMixinScope(ClassDescriptor classObjectDescriptor) { - this.classObjectDescriptor = classObjectDescriptor; - } - - @NotNull - @Override - protected JetScope getWorkerScope() { - return classObjectDescriptor.getDefaultType().getMemberScope(); - } - - @NotNull - @Override - public List getImplicitReceiversHierarchy() { - return Collections.singletonList(classObjectDescriptor.getThisAsReceiverParameter()); - } -} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ClassObjectMixinScope.kt b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ClassObjectMixinScope.kt new file mode 100644 index 00000000000..ae6efb92e8e --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ClassObjectMixinScope.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2010-2014 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.jet.lang.resolve.scopes + +import org.jetbrains.jet.lang.descriptors.ClassDescriptor +import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor + +import java.util.Collections + +/** + * Members of the class object are accessible from the class. + * Scope lazily delegates requests to class object scope. + */ +public class ClassObjectMixinScope(private val classObjectDescriptor: ClassDescriptor) : AbstractScopeAdapter() { + + override fun getWorkerScope() = classObjectDescriptor.getDefaultType().getMemberScope() + + override fun getImplicitReceiversHierarchy() = listOf(classObjectDescriptor.getThisAsReceiverParameter()) +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeImpl.java index 86cbc3f93a0..921acce81d0 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeImpl.java @@ -352,7 +352,7 @@ public class WritableScopeImpl extends WritableScopeWithImports { ReceiverParameterDescriptor receiverParameterForOldVariable = oldProperty.getExtensionReceiverParameter(); if (((receiverParameter != null && receiverParameterForOldVariable != null) && (JetTypeChecker.DEFAULT.equalTypes(receiverParameter.getType(), receiverParameterForOldVariable.getType())))) { - redeclarationHandler.handleRedeclaration(oldProperty, variableDescriptor); + getRedeclarationHandler().handleRedeclaration(oldProperty, variableDescriptor); } } } @@ -360,7 +360,7 @@ public class WritableScopeImpl extends WritableScopeWithImports { private void checkForRedeclaration(@NotNull Name name, DeclarationDescriptor classifierDescriptor) { DeclarationDescriptor originalDescriptor = getVariableOrClassDescriptors().get(name); if (originalDescriptor != null) { - redeclarationHandler.handleRedeclaration(originalDescriptor, classifierDescriptor); + getRedeclarationHandler().handleRedeclaration(originalDescriptor, classifierDescriptor); } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.java deleted file mode 100644 index 822b5d93917..00000000000 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.java +++ /dev/null @@ -1,277 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.lang.resolve.scopes; - -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.descriptors.*; -import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.utils.Printer; - -import java.util.*; - -public abstract class WritableScopeWithImports extends AbstractScopeAdapter implements WritableScope { - @NotNull - private final JetScope workerScope; - - @NotNull - private final String debugName; - - @Nullable - private List imports; - private WritableScope currentIndividualImportScope; - protected final RedeclarationHandler redeclarationHandler; - private List implicitReceiverHierarchy; - - public WritableScopeWithImports(@NotNull JetScope scope, @NotNull RedeclarationHandler redeclarationHandler, @NotNull String debugName) { - this.workerScope = scope; - this.redeclarationHandler = redeclarationHandler; - this.debugName = debugName; - } - - - - private LockLevel lockLevel = LockLevel.WRITING; - - @Override - public WritableScope changeLockLevel(LockLevel lockLevel) { - if (lockLevel.ordinal() < this.lockLevel.ordinal()) { - throw new IllegalStateException("cannot lower lock level from " + this.lockLevel + " to " + lockLevel + " at " + toString()); - } - this.lockLevel = lockLevel; - return this; - } - - protected void checkMayRead() { - if (lockLevel != LockLevel.READING && lockLevel != LockLevel.BOTH) { - throw new IllegalStateException("cannot read with lock level " + lockLevel + " at " + toString()); - } - } - - protected void checkMayWrite() { - if (lockLevel != LockLevel.WRITING && lockLevel != LockLevel.BOTH) { - throw new IllegalStateException("cannot write with lock level " + lockLevel + " at " + toString()); - } - } - - protected void checkMayNotWrite() { - if (lockLevel == LockLevel.WRITING || lockLevel == LockLevel.BOTH) { - throw new IllegalStateException("cannot write with lock level " + lockLevel + " at " + toString()); - } - } - - - - - @NotNull - @Override - protected final JetScope getWorkerScope() { - return workerScope; - } - - @NotNull - protected final List getImports() { - if (imports == null) { - imports = new ArrayList(); - } - return imports; - } - - @Override - public void importScope(@NotNull JetScope imported) { - if (imported == this) { - throw new IllegalStateException("cannot import scope into self"); - } - - checkMayWrite(); - - getImports().add(0, imported); - currentIndividualImportScope = null; - } - - @NotNull - @Override - public List getImplicitReceiversHierarchy() { - checkMayRead(); - - if (implicitReceiverHierarchy == null) { - implicitReceiverHierarchy = computeImplicitReceiversHierarchy(); - } - return implicitReceiverHierarchy; - } - - protected List computeImplicitReceiversHierarchy() { - List implicitReceiverHierarchy = Lists.newArrayList(); - // Imported scopes come with their receivers - // Example: class member resolution scope imports a scope of it's class object - // members of the class object must be able to find it as an implicit receiver - for (JetScope scope : getImports()) { - implicitReceiverHierarchy.addAll(scope.getImplicitReceiversHierarchy()); - } - implicitReceiverHierarchy.addAll(super.getImplicitReceiversHierarchy()); - return implicitReceiverHierarchy; - } - - @NotNull - @Override - public Set getProperties(@NotNull Name name) { - checkMayRead(); - - Set properties = Sets.newLinkedHashSet(); - for (JetScope imported : getImports()) { - properties.addAll(imported.getProperties(name)); - } - return properties; - } - - @Override - public VariableDescriptor getLocalVariable(@NotNull Name name) { - checkMayRead(); - - // Meaningful lookup goes here - for (JetScope imported : getImports()) { - VariableDescriptor importedDescriptor = imported.getLocalVariable(name); - if (importedDescriptor != null) { - return importedDescriptor; - } - } - return null; - } - - @NotNull - @Override - public Collection getFunctions(@NotNull Name name) { - checkMayRead(); - - if (getImports().isEmpty()) { - return Collections.emptySet(); - } - Set result = Sets.newLinkedHashSet(); - for (JetScope imported : getImports()) { - result.addAll(imported.getFunctions(name)); - } - return result; - } - - @Override - public ClassifierDescriptor getClassifier(@NotNull Name name) { - checkMayRead(); - - for (JetScope imported : getImports()) { - ClassifierDescriptor importedClassifier = imported.getClassifier(name); - if (importedClassifier != null) { - return importedClassifier; - } - } - return null; - } - - @Override - public PackageViewDescriptor getPackage(@NotNull Name name) { - checkMayRead(); - - for (JetScope imported : getImports()) { - PackageViewDescriptor importedDescriptor = imported.getPackage(name); - if (importedDescriptor != null) { - return importedDescriptor; - } - } - return null; - } - - private WritableScope getCurrentIndividualImportScope() { - if (currentIndividualImportScope == null) { - WritableScopeImpl writableScope = new WritableScopeImpl(EMPTY, getContainingDeclaration(), RedeclarationHandler.DO_NOTHING, "Individual import scope"); - writableScope.changeLockLevel(LockLevel.BOTH); - importScope(writableScope); - currentIndividualImportScope = writableScope; - } - return currentIndividualImportScope; - } - - @Override - public void importClassifierAlias(@NotNull Name importedClassifierName, @NotNull ClassifierDescriptor classifierDescriptor) { - checkMayWrite(); - - getCurrentIndividualImportScope().addClassifierAlias(importedClassifierName, classifierDescriptor); - } - - @Override - public void importPackageAlias(@NotNull Name aliasName, @NotNull PackageViewDescriptor packageView) { - checkMayWrite(); - - getCurrentIndividualImportScope().addPackageAlias(aliasName, packageView); - } - - @Override - public void importFunctionAlias(@NotNull Name aliasName, @NotNull FunctionDescriptor functionDescriptor) { - checkMayWrite(); - - getCurrentIndividualImportScope().addFunctionAlias(aliasName, functionDescriptor); - } - - @Override - public void importVariableAlias(@NotNull Name aliasName, @NotNull VariableDescriptor variableDescriptor) { - checkMayWrite(); - - getCurrentIndividualImportScope().addVariableAlias(aliasName, variableDescriptor); - } - - @Override - public void clearImports() { - currentIndividualImportScope = null; - getImports().clear(); - } - - @Override - public String toString() { - return getClass().getSimpleName() + "@" + Integer.toHexString(System.identityHashCode(this)) + " " + debugName + " for " + getContainingDeclaration(); - } - - @Override - public void printScopeStructure(@NotNull Printer p) { - p.println(getClass().getSimpleName(), ": ", debugName, " for ", getContainingDeclaration(), " {"); - p.pushIndent(); - - p.println("lockLevel = ", lockLevel); - - printAdditionalScopeStructure(p); - - p.print("worker = "); - getWorkerScope().printScopeStructure(p.withholdIndentOnce()); - - if (getImports().isEmpty()) { - p.println("imports = {}"); - } - else { - p.println("imports = {"); - p.pushIndent(); - for (JetScope anImport : getImports()) { - anImport.printScopeStructure(p); - } - p.popIndent(); - p.println("}"); - } - - p.popIndent(); - p.println("}"); - } - - protected abstract void printAdditionalScopeStructure(@NotNull Printer p); -} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.kt b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.kt new file mode 100644 index 00000000000..7f8543f9d62 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.kt @@ -0,0 +1,240 @@ +/* + * Copyright 2010-2014 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.jet.lang.resolve.scopes + +import com.google.common.collect.Lists +import com.google.common.collect.Sets +import org.jetbrains.jet.lang.descriptors.* +import org.jetbrains.jet.lang.resolve.name.Name +import org.jetbrains.jet.utils.Printer + +import java.util.* +import kotlin.List +import kotlin.Set +import kotlin.Collection + +public abstract class WritableScopeWithImports(private val workerScope: JetScope, + protected val redeclarationHandler: RedeclarationHandler, + private val debugName: String) : AbstractScopeAdapter(), WritableScope { + + private var imports: MutableList? = null + private var currentIndividualImportScope: WritableScope? = null + private var implicitReceiverHierarchy: List? = null + + + private var lockLevel: WritableScope.LockLevel = WritableScope.LockLevel.WRITING + + override fun getWorkerScope() = workerScope + + override fun changeLockLevel(lockLevel: WritableScope.LockLevel): WritableScope { + if (lockLevel.ordinal() < this.lockLevel.ordinal()) { + throw IllegalStateException("cannot lower lock level from " + this.lockLevel + " to " + lockLevel + " at " + toString()) + } + this.lockLevel = lockLevel + return this + } + + protected fun checkMayRead() { + if (lockLevel != WritableScope.LockLevel.READING && lockLevel != WritableScope.LockLevel.BOTH) { + throw IllegalStateException("cannot read with lock level " + lockLevel + " at " + toString()) + } + } + + protected fun checkMayWrite() { + if (lockLevel != WritableScope.LockLevel.WRITING && lockLevel != WritableScope.LockLevel.BOTH) { + throw IllegalStateException("cannot write with lock level " + lockLevel + " at " + toString()) + } + } + + protected fun checkMayNotWrite() { + if (lockLevel == WritableScope.LockLevel.WRITING || lockLevel == WritableScope.LockLevel.BOTH) { + throw IllegalStateException("cannot write with lock level " + lockLevel + " at " + toString()) + } + } + + protected fun getImports(): MutableList { + if (imports == null) { + imports = ArrayList() + } + return imports!! + } + + override fun importScope(imported: JetScope) { + if (imported == this) { + throw IllegalStateException("cannot import scope into self") + } + + checkMayWrite() + + getImports().add(0, imported) + currentIndividualImportScope = null + } + + override fun getImplicitReceiversHierarchy(): List { + checkMayRead() + + if (implicitReceiverHierarchy == null) { + implicitReceiverHierarchy = computeImplicitReceiversHierarchy() + } + return implicitReceiverHierarchy!! + } + + protected open fun computeImplicitReceiversHierarchy(): List { + val implicitReceiverHierarchy = Lists.newArrayList() + // Imported scopes come with their receivers + // Example: class member resolution scope imports a scope of it's class object + // members of the class object must be able to find it as an implicit receiver + for (scope in getImports()) { + implicitReceiverHierarchy.addAll(scope.getImplicitReceiversHierarchy()) + } + implicitReceiverHierarchy.addAll(super.getImplicitReceiversHierarchy()) + return implicitReceiverHierarchy + } + + override fun getProperties(name: Name): Set { + checkMayRead() + + val properties = Sets.newLinkedHashSet() + for (imported in getImports()) { + properties.addAll(imported.getProperties(name)) + } + return properties + } + + override fun getLocalVariable(name: Name): VariableDescriptor? { + checkMayRead() + + // Meaningful lookup goes here + for (imported in getImports()) { + val importedDescriptor = imported.getLocalVariable(name) + if (importedDescriptor != null) { + return importedDescriptor + } + } + return null + } + + override fun getFunctions(name: Name): Collection { + checkMayRead() + + if (getImports().isEmpty()) { + return setOf() + } + val result = Sets.newLinkedHashSet() + for (imported in getImports()) { + result.addAll(imported.getFunctions(name)) + } + return result + } + + override fun getClassifier(name: Name): ClassifierDescriptor? { + checkMayRead() + + for (imported in getImports()) { + val importedClassifier = imported.getClassifier(name) + if (importedClassifier != null) { + return importedClassifier + } + } + return null + } + + override fun getPackage(name: Name): PackageViewDescriptor? { + checkMayRead() + + for (imported in getImports()) { + val importedDescriptor = imported.getPackage(name) + if (importedDescriptor != null) { + return importedDescriptor + } + } + return null + } + + private fun getCurrentIndividualImportScope(): WritableScope { + if (currentIndividualImportScope == null) { + val writableScope = WritableScopeImpl(JetScope.EMPTY, getContainingDeclaration(), RedeclarationHandler.DO_NOTHING, "Individual import scope") + writableScope.changeLockLevel(WritableScope.LockLevel.BOTH) + importScope(writableScope) + currentIndividualImportScope = writableScope + } + return currentIndividualImportScope!! + } + + override fun importClassifierAlias(importedClassifierName: Name, classifierDescriptor: ClassifierDescriptor) { + checkMayWrite() + + getCurrentIndividualImportScope().addClassifierAlias(importedClassifierName, classifierDescriptor) + } + + override fun importPackageAlias(aliasName: Name, packageView: PackageViewDescriptor) { + checkMayWrite() + + getCurrentIndividualImportScope().addPackageAlias(aliasName, packageView) + } + + override fun importFunctionAlias(aliasName: Name, functionDescriptor: FunctionDescriptor) { + checkMayWrite() + + getCurrentIndividualImportScope().addFunctionAlias(aliasName, functionDescriptor) + } + + override fun importVariableAlias(aliasName: Name, variableDescriptor: VariableDescriptor) { + checkMayWrite() + + getCurrentIndividualImportScope().addVariableAlias(aliasName, variableDescriptor) + } + + override fun clearImports() { + currentIndividualImportScope = null + getImports().clear() + } + + override fun toString(): String { + return javaClass.getSimpleName() + "@" + Integer.toHexString(System.identityHashCode(this)) + " " + debugName + " for " + getContainingDeclaration() + } + + override fun printScopeStructure(p: Printer) { + p.println(javaClass.getSimpleName(), ": ", debugName, " for ", getContainingDeclaration(), " {") + p.pushIndent() + + p.println("lockLevel = ", lockLevel) + + printAdditionalScopeStructure(p) + + p.print("worker = ") + getWorkerScope().printScopeStructure(p.withholdIndentOnce()) + + if (getImports().isEmpty()) { + p.println("imports = {}") + } + else { + p.println("imports = {") + p.pushIndent() + for (anImport in getImports()) { + anImport.printScopeStructure(p) + } + p.popIndent() + p.println("}") + } + + p.popIndent() + p.println("}") + } + + protected abstract fun printAdditionalScopeStructure(p: Printer) +} diff --git a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SubpackagesScope.java b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SubpackagesScope.java deleted file mode 100644 index ccd4dc5359d..00000000000 --- a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SubpackagesScope.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2010-2014 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.jet.lang.descriptors.impl; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor; -import org.jetbrains.jet.lang.resolve.name.FqName; -import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.lang.resolve.scopes.JetScopeImpl; -import org.jetbrains.jet.utils.Printer; -import org.jetbrains.jet.utils.UtilsPackage; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -public class SubpackagesScope extends JetScopeImpl { - private final PackageViewDescriptor packageView; - - public SubpackagesScope(PackageViewDescriptor packageView) { - this.packageView = packageView; - - } - - @NotNull - @Override - public DeclarationDescriptor getContainingDeclaration() { - return packageView; - } - - @Nullable - @Override - public PackageViewDescriptor getPackage(@NotNull Name name) { - return name.isSpecial() ? null : packageView.getModule().getPackage(packageView.getFqName().child(name)); - } - - @NotNull - @Override - public Collection getAllDescriptors() { - Collection subFqNames = packageView.getModule().getPackageFragmentProvider().getSubPackagesOf(packageView.getFqName()); - List result = new ArrayList(subFqNames.size()); - for (FqName subFqName : subFqNames) { - UtilsPackage.addIfNotNull(result, getPackage(subFqName.shortName())); - } - return result; - } - - @Override - public void printScopeStructure(@NotNull Printer p) { - p.println(getClass().getSimpleName(), " {"); - p.pushIndent(); - - p.println("thisDescriptor = ", packageView); - - p.popIndent(); - p.println("}"); - } -} diff --git a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SubpackagesScope.kt b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SubpackagesScope.kt new file mode 100644 index 00000000000..00240eaf6bd --- /dev/null +++ b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SubpackagesScope.kt @@ -0,0 +1,56 @@ +/* + * Copyright 2010-2014 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.jet.lang.descriptors.impl + +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor +import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor +import org.jetbrains.jet.lang.resolve.name.FqName +import org.jetbrains.jet.lang.resolve.name.Name +import org.jetbrains.jet.lang.resolve.scopes.JetScopeImpl +import org.jetbrains.jet.utils.Printer +import org.jetbrains.jet.utils.* + +import java.util.ArrayList + +public class SubpackagesScope(private val containingDeclaration: PackageViewDescriptor) : JetScopeImpl() { + override fun getContainingDeclaration(): DeclarationDescriptor { + return containingDeclaration + } + + override fun getPackage(name: Name): PackageViewDescriptor? { + return if (name.isSpecial()) null else containingDeclaration.getModule().getPackage(containingDeclaration.getFqName().child(name)) + } + + override fun getAllDescriptors(): Collection { + val subFqNames = containingDeclaration.getModule().getPackageFragmentProvider().getSubPackagesOf(containingDeclaration.getFqName()) + val result = ArrayList(subFqNames.size()) + for (subFqName in subFqNames) { + result.addIfNotNull(getPackage(subFqName.shortName())) + } + return result + } + + override fun printScopeStructure(p: Printer) { + p.println(javaClass.getSimpleName(), " {") + p.pushIndent() + + p.println("thisDescriptor = ", containingDeclaration) + + p.popIndent() + p.println("}") + } +} diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/AbstractScopeAdapter.java b/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/AbstractScopeAdapter.java deleted file mode 100644 index 6727ed7f1f4..00000000000 --- a/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/AbstractScopeAdapter.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.lang.resolve.scopes; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.descriptors.*; -import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.utils.Printer; - -import java.util.Collection; -import java.util.List; - -/** - * Introduces a simple wrapper for internal scope. - */ -public abstract class AbstractScopeAdapter implements JetScope { - @NotNull - protected abstract JetScope getWorkerScope(); - - @NotNull - @Override - public List getImplicitReceiversHierarchy() { - return getWorkerScope().getImplicitReceiversHierarchy(); - } - - @NotNull - @Override - public Collection getFunctions(@NotNull Name name) { - return getWorkerScope().getFunctions(name); - } - - @Override - public PackageViewDescriptor getPackage(@NotNull Name name) { - return getWorkerScope().getPackage(name); - } - - @Override - public ClassifierDescriptor getClassifier(@NotNull Name name) { - return getWorkerScope().getClassifier(name); - } - - @NotNull - @Override - public Collection getProperties(@NotNull Name name) { - return getWorkerScope().getProperties(name); - } - - @Override - public VariableDescriptor getLocalVariable(@NotNull Name name) { - return getWorkerScope().getLocalVariable(name); - } - - @NotNull - @Override - public DeclarationDescriptor getContainingDeclaration() { - return getWorkerScope().getContainingDeclaration(); - } - - @NotNull - @Override - public Collection getDeclarationsByLabel(@NotNull Name labelName) { - return getWorkerScope().getDeclarationsByLabel(labelName); - } - - @NotNull - @Override - public Collection getAllDescriptors() { - return getWorkerScope().getAllDescriptors(); - } - - @NotNull - @Override - public Collection getOwnDeclaredDescriptors() { - return getWorkerScope().getOwnDeclaredDescriptors(); - } - - @Override - public void printScopeStructure(@NotNull Printer p) { - p.println(getClass().getSimpleName(), " {"); - p.pushIndent(); - - p.print("worker ="); - getWorkerScope().printScopeStructure(p.withholdIndentOnce()); - - p.popIndent(); - p.println("}"); - } -} diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/AbstractScopeAdapter.kt b/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/AbstractScopeAdapter.kt new file mode 100644 index 00000000000..da5d2400cb8 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/AbstractScopeAdapter.kt @@ -0,0 +1,79 @@ +/* + * Copyright 2010-2014 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.jet.lang.resolve.scopes + +import org.jetbrains.jet.lang.descriptors.* +import org.jetbrains.jet.lang.resolve.name.Name +import org.jetbrains.jet.utils.Printer + +/** + * Introduces a simple wrapper for internal scope. + */ +public abstract class AbstractScopeAdapter : JetScope { + protected abstract fun getWorkerScope(): JetScope + + override fun getImplicitReceiversHierarchy(): List { + return getWorkerScope().getImplicitReceiversHierarchy() + } + + override fun getFunctions(name: Name): Collection { + return getWorkerScope().getFunctions(name) + } + + override fun getPackage(name: Name): PackageViewDescriptor? { + return getWorkerScope().getPackage(name) + } + + override fun getClassifier(name: Name): ClassifierDescriptor? { + return getWorkerScope().getClassifier(name) + } + + override fun getProperties(name: Name): Collection { + return getWorkerScope().getProperties(name) + } + + override fun getLocalVariable(name: Name): VariableDescriptor? { + return getWorkerScope().getLocalVariable(name) + } + + override fun getContainingDeclaration(): DeclarationDescriptor { + return getWorkerScope().getContainingDeclaration() + } + + override fun getDeclarationsByLabel(labelName: Name): Collection { + return getWorkerScope().getDeclarationsByLabel(labelName) + } + + override fun getAllDescriptors(): Collection { + return getWorkerScope().getAllDescriptors() + } + + override fun getOwnDeclaredDescriptors(): Collection { + return getWorkerScope().getOwnDeclaredDescriptors() + } + + override fun printScopeStructure(p: Printer) { + p.println(javaClass.getSimpleName(), " {") + p.pushIndent() + + p.print("worker =") + getWorkerScope().printScopeStructure(p.withholdIndentOnce()) + + p.popIndent() + p.println("}") + } +} diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/InnerClassesScopeWrapper.java b/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/InnerClassesScopeWrapper.java deleted file mode 100644 index 90feff3b6a4..00000000000 --- a/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/InnerClassesScopeWrapper.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.lang.resolve.scopes; - -import kotlin.KotlinPackage; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.descriptors.ClassDescriptor; -import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor; -import org.jetbrains.jet.lang.resolve.name.Name; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -public class InnerClassesScopeWrapper extends AbstractScopeAdapter { - private final JetScope actualScope; - - public InnerClassesScopeWrapper(@NotNull JetScope actualScope) { - this.actualScope = actualScope; - } - - @NotNull - @Override - protected JetScope getWorkerScope() { - return actualScope; - } - - @Override - public ClassifierDescriptor getClassifier(@NotNull Name name) { - ClassifierDescriptor classifier = actualScope.getClassifier(name); - return classifier instanceof ClassDescriptor ? classifier : null; - } - - @NotNull - @Override - @SuppressWarnings("unchecked") - public Collection getDeclarationsByLabel(@NotNull Name labelName) { - return (Collection) KotlinPackage.filterIsInstance(actualScope.getDeclarationsByLabel(labelName), ClassDescriptor.class); - } - - @NotNull - @Override - @SuppressWarnings("unchecked") - public Collection getAllDescriptors() { - return (Collection) KotlinPackage.filterIsInstance(actualScope.getAllDescriptors(), ClassDescriptor.class); - } - - @NotNull - @Override - public List getImplicitReceiversHierarchy() { - return Collections.emptyList(); - } - - @Override - public String toString() { - return "Classes from " + actualScope; - } -} diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/InnerClassesScopeWrapper.kt b/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/InnerClassesScopeWrapper.kt new file mode 100644 index 00000000000..842a7f855e8 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/InnerClassesScopeWrapper.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2010-2014 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.jet.lang.resolve.scopes + +import org.jetbrains.jet.lang.descriptors.ClassDescriptor +import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor +import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor +import org.jetbrains.jet.lang.resolve.name.Name +import java.util.Collections + +public class InnerClassesScopeWrapper(private val workerScope: JetScope) : AbstractScopeAdapter() { + + override fun getWorkerScope() = workerScope + + override fun getClassifier(name: Name) = workerScope.getClassifier(name) as? ClassDescriptor + + override fun getDeclarationsByLabel(labelName: Name) = workerScope.getDeclarationsByLabel(labelName).filterIsInstance(javaClass()) + + override fun getAllDescriptors() = workerScope.getAllDescriptors().filterIsInstance(javaClass()) + + override fun getImplicitReceiversHierarchy(): List = listOf() + + override fun toString() = "Classes from " + workerScope +} diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.java b/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.java deleted file mode 100644 index fe1d05c647a..00000000000 --- a/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.lang.resolve.scopes; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.descriptors.*; -import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.utils.Printer; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -public abstract class JetScopeImpl implements JetScope { - @Override - public ClassifierDescriptor getClassifier(@NotNull Name name) { - return null; - } - - @NotNull - @Override - public Collection getProperties(@NotNull Name name) { - return Collections.emptySet(); - } - - @Override - public VariableDescriptor getLocalVariable(@NotNull Name name) { - return null; - } - - @Nullable - @Override - public PackageViewDescriptor getPackage(@NotNull Name name) { - return null; - } - - @NotNull - @Override - public Collection getFunctions(@NotNull Name name) { - return Collections.emptySet(); - } - - @NotNull - @Override - public Collection getDeclarationsByLabel(@NotNull Name labelName) { - return Collections.emptyList(); - } - - @NotNull - @Override - public Collection getAllDescriptors() { - return Collections.emptyList(); - } - - @NotNull - @Override - public List getImplicitReceiversHierarchy() { - return Collections.emptyList(); - } - - @NotNull - @Override - public Collection getOwnDeclaredDescriptors() { - return Collections.emptyList(); - } - - // This method should not be implemented here by default: every scope class has its unique structure pattern - @Override - public abstract void printScopeStructure(@NotNull Printer p); -} diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.kt b/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.kt new file mode 100644 index 00000000000..f4d27f41df5 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2014 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.jet.lang.resolve.scopes + +import org.jetbrains.jet.lang.descriptors.* +import org.jetbrains.jet.lang.resolve.name.Name +import org.jetbrains.jet.utils.Printer +import java.util.Collections + +public abstract class JetScopeImpl : JetScope { + override fun getClassifier(name: Name): ClassifierDescriptor? = null + + override fun getProperties(name: Name): Collection = setOf() + + override fun getLocalVariable(name: Name): VariableDescriptor? = null + + override fun getPackage(name: Name): PackageViewDescriptor? = null + + override fun getFunctions(name: Name): Collection = setOf() + + override fun getDeclarationsByLabel(labelName: Name): Collection = listOf() + + override fun getAllDescriptors(): Collection = listOf() + + override fun getImplicitReceiversHierarchy(): List = listOf() + + override fun getOwnDeclaredDescriptors(): Collection = listOf() + + // This method should not be implemented here by default: every scope class has its unique structure pattern + abstract override fun printScopeStructure(p: Printer) +} diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/LazyScopeAdapter.java b/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/LazyScopeAdapter.kt similarity index 53% rename from core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/LazyScopeAdapter.java rename to core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/LazyScopeAdapter.kt index 3971ade2ee2..fbcdb500a11 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/LazyScopeAdapter.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/resolve/scopes/LazyScopeAdapter.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * Copyright 2010-2014 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. @@ -14,22 +14,10 @@ * limitations under the License. */ -package org.jetbrains.jet.lang.resolve.scopes; +package org.jetbrains.jet.lang.resolve.scopes -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.storage.NotNullLazyValue; +import org.jetbrains.jet.storage.NotNullLazyValue -public class LazyScopeAdapter extends AbstractScopeAdapter { - - private final NotNullLazyValue scope; - - public LazyScopeAdapter(NotNullLazyValue scope) { - this.scope = scope; - } - - @NotNull - @Override - protected JetScope getWorkerScope() { - return scope.invoke(); - } +public class LazyScopeAdapter(private val scope: NotNullLazyValue) : AbstractScopeAdapter() { + override fun getWorkerScope() = scope() }