Converting JetScope's implementations to Kotlin (in progress)

This commit is contained in:
Valentin Kipyatkov
2014-10-22 17:22:14 +04:00
parent 8a9977de72
commit e2b7b79fa1
14 changed files with 499 additions and 679 deletions
@@ -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<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
return Collections.singletonList(classObjectDescriptor.getThisAsReceiverParameter());
}
}
@@ -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())
}
@@ -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);
}
}
@@ -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<JetScope> imports;
private WritableScope currentIndividualImportScope;
protected final RedeclarationHandler redeclarationHandler;
private List<ReceiverParameterDescriptor> 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<JetScope> getImports() {
if (imports == null) {
imports = new ArrayList<JetScope>();
}
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<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
checkMayRead();
if (implicitReceiverHierarchy == null) {
implicitReceiverHierarchy = computeImplicitReceiversHierarchy();
}
return implicitReceiverHierarchy;
}
protected List<ReceiverParameterDescriptor> computeImplicitReceiversHierarchy() {
List<ReceiverParameterDescriptor> 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<VariableDescriptor> getProperties(@NotNull Name name) {
checkMayRead();
Set<VariableDescriptor> 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<FunctionDescriptor> getFunctions(@NotNull Name name) {
checkMayRead();
if (getImports().isEmpty()) {
return Collections.emptySet();
}
Set<FunctionDescriptor> 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);
}
@@ -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<JetScope>? = null
private var currentIndividualImportScope: WritableScope? = null
private var implicitReceiverHierarchy: List<ReceiverParameterDescriptor>? = 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<JetScope> {
if (imports == null) {
imports = ArrayList<JetScope>()
}
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<ReceiverParameterDescriptor> {
checkMayRead()
if (implicitReceiverHierarchy == null) {
implicitReceiverHierarchy = computeImplicitReceiversHierarchy()
}
return implicitReceiverHierarchy!!
}
protected open fun computeImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> {
val implicitReceiverHierarchy = Lists.newArrayList<ReceiverParameterDescriptor>()
// 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<AbstractScopeAdapter>.getImplicitReceiversHierarchy())
return implicitReceiverHierarchy
}
override fun getProperties(name: Name): Set<VariableDescriptor> {
checkMayRead()
val properties = Sets.newLinkedHashSet<VariableDescriptor>()
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<FunctionDescriptor> {
checkMayRead()
if (getImports().isEmpty()) {
return setOf()
}
val result = Sets.newLinkedHashSet<FunctionDescriptor>()
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)
}
@@ -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<DeclarationDescriptor> getAllDescriptors() {
Collection<FqName> subFqNames = packageView.getModule().getPackageFragmentProvider().getSubPackagesOf(packageView.getFqName());
List<DeclarationDescriptor> result = new ArrayList<DeclarationDescriptor>(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("}");
}
}
@@ -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<DeclarationDescriptor> {
val subFqNames = containingDeclaration.getModule().getPackageFragmentProvider().getSubPackagesOf(containingDeclaration.getFqName())
val result = ArrayList<DeclarationDescriptor>(subFqNames.size())
for (subFqName in subFqNames) {
result.addIfNotNull<DeclarationDescriptor>(getPackage(subFqName.shortName()))
}
return result
}
override fun printScopeStructure(p: Printer) {
p.println(javaClass.getSimpleName(), " {")
p.pushIndent()
p.println("thisDescriptor = ", containingDeclaration)
p.popIndent()
p.println("}")
}
}
@@ -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<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
return getWorkerScope().getImplicitReceiversHierarchy();
}
@NotNull
@Override
public Collection<FunctionDescriptor> 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<VariableDescriptor> 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<DeclarationDescriptor> getDeclarationsByLabel(@NotNull Name labelName) {
return getWorkerScope().getDeclarationsByLabel(labelName);
}
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
return getWorkerScope().getAllDescriptors();
}
@NotNull
@Override
public Collection<DeclarationDescriptor> 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("}");
}
}
@@ -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<ReceiverParameterDescriptor> {
return getWorkerScope().getImplicitReceiversHierarchy()
}
override fun getFunctions(name: Name): Collection<FunctionDescriptor> {
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<VariableDescriptor> {
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<DeclarationDescriptor> {
return getWorkerScope().getDeclarationsByLabel(labelName)
}
override fun getAllDescriptors(): Collection<DeclarationDescriptor> {
return getWorkerScope().getAllDescriptors()
}
override fun getOwnDeclaredDescriptors(): Collection<DeclarationDescriptor> {
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("}")
}
}
@@ -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<DeclarationDescriptor> getDeclarationsByLabel(@NotNull Name labelName) {
return (Collection) KotlinPackage.filterIsInstance(actualScope.getDeclarationsByLabel(labelName), ClassDescriptor.class);
}
@NotNull
@Override
@SuppressWarnings("unchecked")
public Collection<DeclarationDescriptor> getAllDescriptors() {
return (Collection) KotlinPackage.filterIsInstance(actualScope.getAllDescriptors(), ClassDescriptor.class);
}
@NotNull
@Override
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
return Collections.emptyList();
}
@Override
public String toString() {
return "Classes from " + actualScope;
}
}
@@ -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<ClassDescriptor>())
override fun getAllDescriptors() = workerScope.getAllDescriptors().filterIsInstance(javaClass<ClassDescriptor>())
override fun getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> = listOf()
override fun toString() = "Classes from " + workerScope
}
@@ -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<VariableDescriptor> 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<FunctionDescriptor> getFunctions(@NotNull Name name) {
return Collections.emptySet();
}
@NotNull
@Override
public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull Name labelName) {
return Collections.emptyList();
}
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
return Collections.emptyList();
}
@NotNull
@Override
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
return Collections.emptyList();
}
@NotNull
@Override
public Collection<DeclarationDescriptor> 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);
}
@@ -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<VariableDescriptor> = setOf()
override fun getLocalVariable(name: Name): VariableDescriptor? = null
override fun getPackage(name: Name): PackageViewDescriptor? = null
override fun getFunctions(name: Name): Collection<FunctionDescriptor> = setOf()
override fun getDeclarationsByLabel(labelName: Name): Collection<DeclarationDescriptor> = listOf()
override fun getAllDescriptors(): Collection<DeclarationDescriptor> = listOf()
override fun getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> = listOf()
override fun getOwnDeclaredDescriptors(): Collection<DeclarationDescriptor> = listOf()
// This method should not be implemented here by default: every scope class has its unique structure pattern
abstract override fun printScopeStructure(p: Printer)
}
@@ -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<JetScope> scope;
public LazyScopeAdapter(NotNullLazyValue<JetScope> scope) {
this.scope = scope;
}
@NotNull
@Override
protected JetScope getWorkerScope() {
return scope.invoke();
}
public class LazyScopeAdapter(private val scope: NotNullLazyValue<JetScope>) : AbstractScopeAdapter() {
override fun getWorkerScope() = scope()
}