Big refactoring. Migrating to package views and fragments.

This commit is contained in:
Evgeny Gerashchenko
2013-10-11 21:43:40 +04:00
parent 369824cd2e
commit 7abe6a5b4a
141 changed files with 1728 additions and 1465 deletions
@@ -32,8 +32,11 @@ public interface ModuleDescriptor extends DeclarationDescriptor, NamespaceDescri
@Nullable
DeclarationDescriptor getContainingDeclaration();
@NotNull
PackageFragmentProvider getPackageFragmentProvider();
@Nullable
NamespaceDescriptor getNamespace(@NotNull FqName fqName);
PackageViewDescriptor getPackage(@NotNull FqName fqName);
@NotNull
ModuleConfiguration getModuleConfiguration();
@@ -16,13 +16,15 @@
package org.jetbrains.jet.lang.descriptors;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.ModuleConfiguration;
import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.CompositePackageFragmentProvider;
import org.jetbrains.jet.lang.descriptors.impl.DeclarationDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.NamespaceDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.PackageViewDescriptorImpl;
import org.jetbrains.jet.lang.resolve.ImportPath;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -32,7 +34,8 @@ import java.util.Collections;
import java.util.List;
public class ModuleDescriptorImpl extends DeclarationDescriptorImpl implements ModuleDescriptor {
private NamespaceDescriptor rootNamepsace;
private final List<PackageFragmentProvider> fragmentProviders = Lists.newArrayList();
private final CompositePackageFragmentProvider packageFragmentProvider = new CompositePackageFragmentProvider(fragmentProviders);
private ModuleConfiguration moduleConfiguration;
private final List<ImportPath> defaultImports;
private final PlatformToKotlinClassMap platformToKotlinClassMap;
@@ -50,11 +53,8 @@ public class ModuleDescriptorImpl extends DeclarationDescriptorImpl implements M
this.platformToKotlinClassMap = platformToKotlinClassMap;
}
public void setRootNamespace(@NotNull NamespaceDescriptor rootNs) {
if (this.rootNamepsace != null) {
throw new IllegalStateException("setRootNamespace() is called twice");
}
this.rootNamepsace = rootNs;
public void addFragmentProvider(@NotNull PackageFragmentProvider provider) {
fragmentProviders.add(provider);
}
@Override
@@ -63,16 +63,19 @@ public class ModuleDescriptorImpl extends DeclarationDescriptorImpl implements M
return null;
}
@NotNull
@Override
public PackageFragmentProvider getPackageFragmentProvider() {
return packageFragmentProvider;
}
@Nullable
@Override
public NamespaceDescriptor getNamespace(@NotNull FqName fqName) {
if (fqName.isRoot()) return rootNamepsace;
NamespaceDescriptor current = rootNamepsace;
for (Name simpleName : fqName.pathSegments()) {
current = current.getMemberScope().getNamespace(simpleName);
if (current == null) return null;
}
return current;
public PackageViewDescriptor getPackage(@NotNull FqName fqName) {
List<PackageFragmentDescriptor> fragments = packageFragmentProvider.getPackageFragments(fqName);
return !fragments.isEmpty()
? new PackageViewDescriptorImpl(this, fqName, fragments)
: null;
}
@NotNull
@@ -100,10 +103,6 @@ public class ModuleDescriptorImpl extends DeclarationDescriptorImpl implements M
return platformToKotlinClassMap;
}
public NamespaceDescriptorImpl getRootNamespaceDescriptorImpl() {
return (NamespaceDescriptorImpl) rootNamepsace;
}
@NotNull
@Override
public ModuleDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
@@ -114,14 +113,4 @@ public class ModuleDescriptorImpl extends DeclarationDescriptorImpl implements M
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitModuleDeclaration(this, data);
}
@Override
public void addNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
if (namespaceDescriptor.getContainingDeclaration() != this) {
throw new IllegalStateException();
}
setRootNamespace(namespaceDescriptor);
}
}
@@ -0,0 +1,34 @@
/*
* 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.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.name.FqName;
import java.util.Collection;
import java.util.List;
public interface PackageFragmentProvider {
@NotNull
List<PackageFragmentDescriptor> getPackageFragments(@NotNull FqName fqName);
/**
* @return declared subpackages of {@code fqName}
*/
@NotNull
Collection<FqName> getSubPackagesOf(@NotNull FqName fqName);
}
@@ -31,4 +31,7 @@ public interface PackageViewDescriptor extends DeclarationDescriptor {
@NotNull
JetScope getMemberScope();
@NotNull
ModuleDescriptor getModule();
}
@@ -33,15 +33,22 @@ public class Visibilities {
while (parent != null) {
parent = parent.getContainingDeclaration();
if ((parent instanceof ClassDescriptor && !DescriptorUtils.isClassObject(parent)) ||
parent instanceof NamespaceDescriptor) {
parent instanceof PackageFragmentDescriptor) {
break;
}
}
if (parent == null) {
return false;
}
DeclarationDescriptor fromParent = from;
while (fromParent != null) {
if (parent == fromParent) {
return true;
}
if (fromParent instanceof PackageFragmentDescriptor) {
return parent instanceof PackageFragmentDescriptor && ((PackageFragmentDescriptor) parent).getFqName()
.isAncestorOf(((PackageFragmentDescriptor) fromParent).getFqName());
}
fromParent = fromParent.getContainingDeclaration();
}
return false;
@@ -112,6 +119,9 @@ public class Visibilities {
@Nullable DeclarationDescriptorWithVisibility what,
@NotNull DeclarationDescriptor from
) {
if (from instanceof PackageViewDescriptor) {
return null; // TODO 1 review: everything is visible from package view
}
DeclarationDescriptorWithVisibility parent = what;
while (parent != null && parent.getVisibility() != LOCAL) {
if (!parent.getVisibility().isVisible(parent, from)) {
@@ -46,11 +46,6 @@ public abstract class AbstractNamespaceDescriptorImpl extends DeclarationDescrip
return (NamespaceDescriptorParent) super.getContainingDeclaration();
}
@Override
public void addNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
throw new IllegalStateException("immutable");
}
@NotNull
@Override
public NamespaceDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
@@ -0,0 +1,57 @@
/*
* 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.descriptors.impl;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor;
import org.jetbrains.jet.lang.descriptors.PackageFragmentProvider;
import org.jetbrains.jet.lang.resolve.name.FqName;
import java.util.Collection;
import java.util.List;
import java.util.Set;
public class CompositePackageFragmentProvider implements PackageFragmentProvider {
// can be modified from outside
private final List<PackageFragmentProvider> providers;
public CompositePackageFragmentProvider(@NotNull List<PackageFragmentProvider> providers) {
this.providers = providers;
}
@NotNull
@Override
public List<PackageFragmentDescriptor> getPackageFragments(@NotNull FqName fqName) {
List<PackageFragmentDescriptor> result = Lists.newArrayList();
for (PackageFragmentProvider provider : providers) {
result.addAll(provider.getPackageFragments(fqName));
}
return result;
}
@NotNull
@Override
public Collection<FqName> getSubPackagesOf(@NotNull FqName fqName) {
Set<FqName> result = Sets.newHashSet();
for (PackageFragmentProvider provider : providers) {
result.addAll(provider.getSubPackagesOf(fqName));
}
return result;
}
}
@@ -0,0 +1,96 @@
/*
* 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.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.DeclarationDescriptorVisitor;
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
import java.util.Collections;
public class MutablePackageFragmentDescriptor extends DeclarationDescriptorImpl implements PackageFragmentDescriptor {
private final ModuleDescriptor module;
private final FqName fqName;
private final WritableScope scope;
private final NamespaceLikeBuilder builder;
public MutablePackageFragmentDescriptor(
@NotNull ModuleDescriptor module,
@NotNull FqName fqName
) {
super(Collections.<AnnotationDescriptor>emptyList(), fqName.shortNameOrSpecial());
this.module = module;
this.fqName = fqName;
scope = new WritableScopeImpl(JetScope.EMPTY, this, RedeclarationHandler.DO_NOTHING,
"Members of " + fqName + " in " + module);
// TODO 1 make read-only after
scope.changeLockLevel(WritableScope.LockLevel.BOTH);
builder = new ScopeBasedNamespaceLikeBuilder(this, scope);
}
@NotNull
@Override
public ModuleDescriptor getContainingDeclaration() {
return module;
}
@NotNull
@Override
public FqName getFqName() {
return fqName;
}
@NotNull
@Override
public WritableScope getMemberScope() {
return scope;
}
@Nullable
@Override
public DeclarationDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
return this;
}
@Override
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitPackageFragmentDescriptor(this, data);
}
@NotNull
public NamespaceLikeBuilder getBuilder() {
return builder;
}
@Override
public String toString() {
return "fragment of " + fqName + " in " + module;
}
}
@@ -25,6 +25,7 @@ import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import java.util.List;
@Deprecated
public class NamespaceDescriptorImpl extends AbstractNamespaceDescriptorImpl {
private WritableScope memberScope;
@@ -16,12 +16,10 @@
package org.jetbrains.jet.lang.descriptors.impl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
// TODO 2 remove
@Deprecated
public interface NamespaceDescriptorParent extends DeclarationDescriptor {
void addNamespace(@NotNull NamespaceDescriptor namespaceDescriptor);
}
@@ -0,0 +1,155 @@
/*
* 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.descriptors.impl;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.ChainedScope;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.JetScopeImpl;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
import org.jetbrains.jet.utils.Printer;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class PackageViewDescriptorImpl extends DeclarationDescriptorImpl implements PackageViewDescriptor {
private final ModuleDescriptor module;
private final FqName fqName;
private final JetScope memberScope;
public PackageViewDescriptorImpl(
@NotNull ModuleDescriptor module,
@NotNull FqName fqName,
@NotNull List<PackageFragmentDescriptor> fragments
) {
super(Collections.<AnnotationDescriptor>emptyList(), fqName.shortNameOrSpecial());
this.module = module;
this.fqName = fqName;
List<JetScope> scopes = Lists.newArrayList();
assert !fragments.isEmpty() : fqName + " in " + module;
for (PackageFragmentDescriptor fragment : fragments) {
scopes.add(fragment.getMemberScope());
}
scopes.add(new SubpackagesScope());
memberScope = new ChainedScope(this, "package view scope for " + fqName + " in " + module.getName(),
scopes.toArray(new JetScope[scopes.size()]));
}
@Nullable
@Override
public PackageViewDescriptor getContainingDeclaration() {
return fqName.isRoot() ? null : module.getPackage(fqName.parent());
}
@Nullable
@Override
public DeclarationDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
return this;
}
@Override
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitPackageViewDescriptor(this, data);
}
@NotNull
@Override
public FqName getFqName() {
return fqName;
}
@NotNull
@Override
public JetScope getMemberScope() {
return memberScope;
}
@Override
@NotNull
public ModuleDescriptor getModule() {
return module;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PackageViewDescriptorImpl that = (PackageViewDescriptorImpl) o;
if (!fqName.equals(that.fqName)) return false;
if (!module.equals(that.module)) return false;
return true;
}
@Override
public int hashCode() {
int result = module.hashCode();
result = 31 * result + fqName.hashCode();
return result;
}
@Override
public String toString() {
return DescriptorRenderer.DEBUG_TEXT.render(this);
}
private class SubpackagesScope extends JetScopeImpl {
@NotNull
@Override
public DeclarationDescriptor getContainingDeclaration() {
return PackageViewDescriptorImpl.this;
}
@Nullable
@Override
public PackageViewDescriptor getPackage(@NotNull Name name) {
return name.isSpecial() ? null : module.getPackage(fqName.child(name));
}
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
List<DeclarationDescriptor> result = Lists.newArrayList();
for (FqName subFqName : module.getPackageFragmentProvider().getSubPackagesOf(fqName)) {
result.add(getPackage(subFqName.shortName()));
}
return result;
}
@Override
public void printScopeStructure(@NotNull Printer p) {
p.println(getClass().getSimpleName(), " {");
p.pushIndent();
p.println("thisDescriptor = ", PackageViewDescriptorImpl.this);
p.popIndent();
p.println("}");
}
}
}
@@ -111,21 +111,15 @@ public class DescriptorUtils {
public static FqNameUnsafe getFQName(@NotNull DeclarationDescriptor descriptor) {
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
if (descriptor instanceof ModuleDescriptor || containingDeclaration instanceof ModuleDescriptor) {
if (descriptor instanceof ModuleDescriptor || ErrorUtils.isError(descriptor)) {
return FqName.ROOT.toUnsafe();
}
if (containingDeclaration == null) {
if (descriptor instanceof NamespaceDescriptor) {
// TODO: namespace must always have parent
if (descriptor.getName().equals(Name.identifier("jet"))) {
return FqNameUnsafe.topLevel(Name.identifier("jet"));
}
if (descriptor.getName().equals(Name.special("<java_root>"))) {
return FqName.ROOT.toUnsafe();
}
}
throw new IllegalStateException("descriptor is not module descriptor and has null containingDeclaration: " + descriptor);
if (descriptor instanceof PackageViewDescriptor) {
return ((PackageViewDescriptor) descriptor).getFqName().toUnsafe();
}
else if (descriptor instanceof PackageFragmentDescriptor) {
return ((PackageFragmentDescriptor) descriptor).getFqName().toUnsafe();
}
if (containingDeclaration instanceof ClassDescriptor && ((ClassDescriptor) containingDeclaration).getKind() == ClassKind.CLASS_OBJECT) {
@@ -138,7 +132,7 @@ public class DescriptorUtils {
}
public static boolean isTopLevelDeclaration(@NotNull DeclarationDescriptor descriptor) {
return descriptor.getContainingDeclaration() instanceof NamespaceDescriptor;
return descriptor.getContainingDeclaration() instanceof PackageFragmentDescriptor;
}
public static boolean isInSameModule(@NotNull DeclarationDescriptor first, @NotNull DeclarationDescriptor second) {
@@ -303,7 +297,7 @@ public class DescriptorUtils {
public static boolean inStaticContext(@NotNull DeclarationDescriptor descriptor) {
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
if (containingDeclaration instanceof NamespaceDescriptor) {
if (containingDeclaration instanceof PackageFragmentDescriptor) {
return true;
}
if (containingDeclaration instanceof ClassDescriptor) {
@@ -141,6 +141,11 @@ public final class FqName extends FqNameBase {
return fqName.lastSegmentIs(segment);
}
public boolean isAncestorOf(@NotNull FqName other) {
String thisString = this.asString();
String otherString = other.asString();
return otherString.equals(thisString) || otherString.startsWith(thisString + ".");
}
@NotNull
public static FqName topLevel(@NotNull Name shortName) {
@@ -46,8 +46,8 @@ public abstract class AbstractScopeAdapter implements JetScope {
}
@Override
public NamespaceDescriptor getNamespace(@NotNull Name name) {
return getWorkerScope().getNamespace(name);
public PackageViewDescriptor getPackage(@NotNull Name name) {
return getWorkerScope().getPackage(name);
}
@Override
@@ -58,11 +58,11 @@ public class ChainedScope implements JetScope {
}
@Override
public NamespaceDescriptor getNamespace(@NotNull Name name) {
public PackageViewDescriptor getPackage(@NotNull Name name) {
for (JetScope jetScope : scopeChain) {
NamespaceDescriptor namespace = jetScope.getNamespace(name);
if (namespace != null) {
return namespace;
PackageViewDescriptor aPackage = jetScope.getPackage(name);
if (aPackage != null) {
return aPackage;
}
}
return null;
@@ -55,9 +55,10 @@ public class FilteringScope implements JetScope {
return descriptor != null && predicate.apply(descriptor) ? descriptor : null;
}
@Nullable
@Override
public NamespaceDescriptor getNamespace(@NotNull Name name) {
return filterDescriptor(workerScope.getNamespace(name));
public PackageViewDescriptor getPackage(@NotNull Name name) {
return filterDescriptor(workerScope.getPackage(name));
}
@Override
@@ -54,7 +54,7 @@ public interface JetScope {
ClassifierDescriptor getClassifier(@NotNull Name name);
@Nullable
NamespaceDescriptor getNamespace(@NotNull Name name);
PackageViewDescriptor getPackage(@NotNull Name name);
@NotNull
@ReadOnly
@@ -17,6 +17,7 @@
package org.jetbrains.jet.lang.resolve.scopes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.name.LabelName;
@@ -44,8 +45,9 @@ public abstract class JetScopeImpl implements JetScope {
return null;
}
@Nullable
@Override
public NamespaceDescriptor getNamespace(@NotNull Name name) {
public PackageViewDescriptor getPackage(@NotNull Name name) {
return null;
}
@@ -105,8 +105,8 @@ public class SubstitutingScope implements JetScope {
}
@Override
public NamespaceDescriptor getNamespace(@NotNull Name name) {
return workerScope.getNamespace(name); // TODO
public PackageViewDescriptor getPackage(@NotNull Name name) {
return workerScope.getPackage(name);
}
@NotNull
@@ -45,7 +45,7 @@ public interface WritableScope extends JetScope {
void addClassifierAlias(@NotNull Name name, @NotNull ClassifierDescriptor classifierDescriptor);
void addNamespaceAlias(@NotNull Name name, @NotNull NamespaceDescriptor namespaceDescriptor);
void addPackageAlias(@NotNull Name name, @NotNull PackageViewDescriptor packageView);
void addFunctionAlias(@NotNull Name name, @NotNull FunctionDescriptor functionDescriptor);
@@ -53,6 +53,7 @@ public interface WritableScope extends JetScope {
void addNamespace(@NotNull NamespaceDescriptor namespaceDescriptor);
@Deprecated
@Nullable
NamespaceDescriptor getDeclaredNamespace(@NotNull Name name);
@@ -64,7 +65,7 @@ public interface WritableScope extends JetScope {
void importClassifierAlias(@NotNull Name importedClassifierName, @NotNull ClassifierDescriptor classifierDescriptor);
void importNamespaceAlias(@NotNull Name aliasName, @NotNull NamespaceDescriptor namespaceDescriptor);
void importPackageAlias(@NotNull Name aliasName, @NotNull PackageViewDescriptor packageView);
void importFunctionAlias(@NotNull Name aliasName, @NotNull FunctionDescriptor functionDescriptor);
@@ -29,6 +29,13 @@ import org.jetbrains.jet.utils.Printer;
import java.util.*;
// Reads from:
// 1. Maps
// 2. Worker
// 3. Imports
// Writes to: maps
public class WritableScopeImpl extends WritableScopeWithImports {
private final Collection<DeclarationDescriptor> allDescriptors = Lists.newArrayList();
@@ -48,7 +55,7 @@ public class WritableScopeImpl extends WritableScopeWithImports {
private SetMultimap<Name, VariableDescriptor> propertyGroups;
@Nullable
private Map<Name, NamespaceDescriptor> namespaceAliases;
private Map<Name, PackageViewDescriptor> packageAliases;
@Nullable
private Map<LabelName, List<DeclarationDescriptor>> labelsToDescriptors;
@@ -83,11 +90,11 @@ public class WritableScopeImpl extends WritableScopeWithImports {
}
@Override
public void importNamespaceAlias(@NotNull Name aliasName, @NotNull NamespaceDescriptor namespaceDescriptor) {
public void importPackageAlias(@NotNull Name aliasName, @NotNull PackageViewDescriptor packageView) {
checkMayWrite();
allDescriptors.add(namespaceDescriptor);
super.importNamespaceAlias(aliasName, namespaceDescriptor);
allDescriptors.add(packageView);
super.importPackageAlias(aliasName, packageView);
}
@Override
@@ -181,11 +188,11 @@ public class WritableScopeImpl extends WritableScopeWithImports {
}
@NotNull
private Map<Name, NamespaceDescriptor> getNamespaceAliases() {
if (namespaceAliases == null) {
namespaceAliases = Maps.newHashMap();
private Map<Name, PackageViewDescriptor> getPackageAliases() {
if (packageAliases == null) {
packageAliases = Maps.newHashMap();
}
return namespaceAliases;
return packageAliases;
}
@Override
@@ -310,13 +317,13 @@ public class WritableScopeImpl extends WritableScopeWithImports {
}
@Override
public void addNamespaceAlias(@NotNull Name name, @NotNull NamespaceDescriptor namespaceDescriptor) {
public void addPackageAlias(@NotNull Name name, @NotNull PackageViewDescriptor packageView) {
checkMayWrite();
checkForRedeclaration(name, namespaceDescriptor);
getNamespaceAliases().put(name, namespaceDescriptor);
allDescriptors.add(namespaceDescriptor);
addToDeclared(namespaceDescriptor);
checkForRedeclaration(name, packageView);
getPackageAliases().put(name, packageView);
allDescriptors.add(packageView);
addToDeclared(packageView);
}
@Override
@@ -395,18 +402,19 @@ public class WritableScopeImpl extends WritableScopeWithImports {
}
@Override
public NamespaceDescriptor getNamespace(@NotNull Name name) {
public PackageViewDescriptor getPackage(@NotNull Name name) {
checkMayRead();
NamespaceDescriptor declaredNamespace = getDeclaredNamespace(name);
if (declaredNamespace != null) return declaredNamespace;
// TODO 1
//NamespaceDescriptor declaredNamespace = getDeclaredNamespace(name);
//if (declaredNamespace != null) return declaredNamespace;
NamespaceDescriptor aliased = getNamespaceAliases().get(name);
PackageViewDescriptor aliased = getPackageAliases().get(name);
if (aliased != null) return aliased;
NamespaceDescriptor namespace = getWorkerScope().getNamespace(name);
if (namespace != null) return namespace;
return super.getNamespace(name);
PackageViewDescriptor packageView = getWorkerScope().getPackage(name);
if (packageView != null) return packageView;
return super.getPackage(name);
}
@Override
@@ -176,11 +176,11 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
}
@Override
public NamespaceDescriptor getNamespace(@NotNull Name name) {
public PackageViewDescriptor getPackage(@NotNull Name name) {
checkMayRead();
for (JetScope imported : getImports()) {
NamespaceDescriptor importedDescriptor = imported.getNamespace(name);
PackageViewDescriptor importedDescriptor = imported.getPackage(name);
if (importedDescriptor != null) {
return importedDescriptor;
}
@@ -206,10 +206,10 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
}
@Override
public void importNamespaceAlias(@NotNull Name aliasName, @NotNull NamespaceDescriptor namespaceDescriptor) {
public void importPackageAlias(@NotNull Name aliasName, @NotNull PackageViewDescriptor packageView) {
checkMayWrite();
getCurrentIndividualImportScope().addNamespaceAlias(aliasName, namespaceDescriptor);
getCurrentIndividualImportScope().addPackageAlias(aliasName, packageView);
}
@Override
@@ -100,8 +100,8 @@ public class ErrorUtils {
}
@Override
public NamespaceDescriptor getNamespace(@NotNull Name name) {
return null; // TODO : review
public PackageViewDescriptor getPackage(@NotNull Name name) {
return null;
}
@NotNull
@@ -166,7 +166,7 @@ public class ErrorUtils {
@Nullable
@Override
public NamespaceDescriptor getNamespace(@NotNull Name name) {
public PackageViewDescriptor getPackage(@NotNull Name name) {
throw new IllegalStateException();
}
@@ -18,8 +18,7 @@ package org.jetbrains.jet.lang.types.lang;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.descriptors.serialization.ClassId;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -51,30 +50,30 @@ public class BuiltInsSerializationUtil {
@NotNull
public static String getClassMetadataPath(@NotNull ClassId classId) {
return packageFqNameToPath(classId.getPackageFqName().toUnsafe())
return packageFqNameToPath(classId.getPackageFqName())
+ "/" + relativeClassNameToFilePath(classId.getRelativeClassName())
+ "." + CLASS_METADATA_FILE_EXTENSION;
}
@NotNull
public static String getPackageFilePath(@NotNull NamespaceDescriptor packageDescriptor) {
FqNameUnsafe fqName = DescriptorUtils.getFQName(packageDescriptor);
public static String getPackageFilePath(@NotNull PackageFragmentDescriptor packageFragment) {
FqName fqName = packageFragment.getFqName();
return packageFqNameToPath(fqName) + "/" + PACKAGE_FILE_NAME;
}
@NotNull
public static String getNameTableFilePath(@NotNull NamespaceDescriptor packageDescriptor) {
FqNameUnsafe fqName = DescriptorUtils.getFQName(packageDescriptor);
public static String getNameTableFilePath(@NotNull PackageFragmentDescriptor packageFragment) {
FqName fqName = packageFragment.getFqName();
return packageFqNameToPath(fqName) + "/" + NAME_TABLE_FILE_NAME;
}
@NotNull
public static String getClassNamesFilePath(@NotNull NamespaceDescriptor packageDescriptor) {
FqNameUnsafe fqName = DescriptorUtils.getFQName(packageDescriptor);
public static String getClassNamesFilePath(@NotNull PackageFragmentDescriptor packageFragmentDescriptor) {
FqName fqName = packageFragmentDescriptor.getFqName();
return packageFqNameToPath(fqName) + "/" + CLASS_NAMES_FILE_NAME;
}
private static String packageFqNameToPath(FqNameUnsafe fqName) {
private static String packageFqNameToPath(FqName fqName) {
return fqName.asString().replace('.', '/');
}
}
@@ -5,12 +5,13 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.descriptors.serialization.*;
import org.jetbrains.jet.descriptors.serialization.descriptors.DeserializedPackageMemberScope;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.AbstractNamespaceDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.DeclarationDescriptorImpl;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
import org.jetbrains.jet.storage.NotNullLazyValue;
import org.jetbrains.jet.storage.StorageManager;
@@ -24,13 +25,14 @@ import java.util.List;
import static org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationDeserializer.UNSUPPORTED;
class BuiltinsNamespaceDescriptorImpl extends AbstractNamespaceDescriptorImpl {
class BuiltinsPackageFragment extends DeclarationDescriptorImpl implements PackageFragmentDescriptor {
private final DeserializedPackageMemberScope members;
private final NameResolver nameResolver;
private final ModuleDescriptor module;
public BuiltinsNamespaceDescriptorImpl(@NotNull StorageManager storageManager, @NotNull NamespaceDescriptor containingDeclaration) {
super(containingDeclaration, Collections.<AnnotationDescriptor>emptyList(), KotlinBuiltIns.BUILT_INS_PACKAGE_NAME);
public BuiltinsPackageFragment(@NotNull StorageManager storageManager, @NotNull ModuleDescriptor module) {
super(Collections.<AnnotationDescriptor>emptyList(), KotlinBuiltIns.BUILT_INS_PACKAGE_NAME);
this.module = module;
nameResolver = NameSerializationUtil.deserializeNameResolver(getStream(BuiltInsSerializationUtil.getNameTableFilePath(this)));
members = new DeserializedPackageMemberScope(storageManager, this, UNSUPPORTED, new BuiltInsDescriptorFinder(storageManager),
@@ -55,6 +57,23 @@ class BuiltinsNamespaceDescriptorImpl extends AbstractNamespaceDescriptorImpl {
return members;
}
@NotNull
@Override
public ModuleDescriptor getContainingDeclaration() {
return module;
}
@Nullable
@Override
public DeclarationDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
return this;
}
@Override
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitPackageFragmentDescriptor(this, data);
}
@NotNull
@Override
public FqName getFqName() {
@@ -85,7 +104,7 @@ class BuiltinsNamespaceDescriptorImpl extends AbstractNamespaceDescriptorImpl {
@Override
@NotNull
public Collection<Name> invoke() {
InputStream in = getStream(BuiltInsSerializationUtil.getClassNamesFilePath(BuiltinsNamespaceDescriptorImpl.this));
InputStream in = getStream(BuiltInsSerializationUtil.getClassNamesFilePath(BuiltinsPackageFragment.this));
try {
DataInputStream data = new DataInputStream(in);
@@ -136,8 +155,8 @@ class BuiltinsNamespaceDescriptorImpl extends AbstractNamespaceDescriptorImpl {
@Nullable
@Override
public NamespaceDescriptor findPackage(@NotNull FqName fqName) {
return fqName.equals(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) ? BuiltinsNamespaceDescriptorImpl.this : null;
public PackageFragmentDescriptor findPackage(@NotNull FqName fqName) {
return fqName.equals(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) ? BuiltinsPackageFragment.this : null;
}
@NotNull
@@ -25,16 +25,13 @@ import org.jetbrains.jet.lang.ModuleConfiguration;
import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.NamespaceDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.MutablePackageFragmentDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.ValueParameterDescriptorImpl;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.name.SpecialNames;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.storage.LockBasedStorageManager;
@@ -163,15 +160,31 @@ public class KotlinBuiltIns {
}
private static void loadBuiltIns(@NotNull ModuleDescriptorImpl module) throws IOException {
NamespaceDescriptorImpl rootNamespace =
new NamespaceDescriptorImpl(module, Collections.<AnnotationDescriptor>emptyList(), SpecialNames.ROOT_NAMESPACE);
rootNamespace.initialize(
new WritableScopeImpl(JetScope.EMPTY, rootNamespace, RedeclarationHandler.DO_NOTHING, "members of root namespace"));
final PackageFragmentDescriptor rootPackage = new MutablePackageFragmentDescriptor(module, FqName.ROOT);
final PackageFragmentDescriptor builtinsPackage = new BuiltinsPackageFragment(new LockBasedStorageManager(), module);
module.setRootNamespace(rootNamespace);
module.addFragmentProvider(new PackageFragmentProvider() {
@NotNull
@Override
public List<PackageFragmentDescriptor> getPackageFragments(@NotNull FqName fqName) {
if (fqName.isRoot()) {
return Collections.singletonList(rootPackage);
}
else if (BUILT_INS_PACKAGE_FQ_NAME.equals(fqName)) {
return Collections.singletonList(builtinsPackage);
}
return Collections.emptyList();
}
rootNamespace.getMemberScope().addNamespace(new BuiltinsNamespaceDescriptorImpl(new LockBasedStorageManager(), rootNamespace));
rootNamespace.getMemberScope().changeLockLevel(WritableScope.LockLevel.READING);
@NotNull
@Override
public Collection<FqName> getSubPackagesOf(@NotNull FqName fqName) {
if (fqName.isRoot()) {
return Collections.singleton(BUILT_INS_PACKAGE_FQ_NAME);
}
return Collections.emptyList();
}
});
}
private void doInitialize() {
@@ -213,20 +226,20 @@ public class KotlinBuiltIns {
}
@NotNull
public NamespaceDescriptor getBuiltInsPackage() {
NamespaceDescriptor namespace = getBuiltInsModule().getNamespace(BUILT_INS_PACKAGE_FQ_NAME);
assert namespace != null : "Built ins namespace not found: " + BUILT_INS_PACKAGE_FQ_NAME;
return namespace;
public PackageFragmentDescriptor getBuiltInsPackageFragment() {
return builtInsModule.getPackageFragmentProvider().getPackageFragments(BUILT_INS_PACKAGE_FQ_NAME).get(0);
}
// TODO 1 inline
@NotNull
public FqName getBuiltInsPackageFqName() {
return getBuiltInsPackage().getFqName();
return BUILT_INS_PACKAGE_FQ_NAME;
}
@Deprecated
@NotNull
public JetScope getBuiltInsScope() {
return getBuiltInsPackage().getMemberScope();
public JetScope getBuiltInsScope() { // TODO 1 scope?
return getBuiltInsPackageFragment().getMemberScope();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -690,13 +690,19 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
private void renderPackageView(@NotNull PackageViewDescriptor packageView, @NotNull StringBuilder builder) {
builder.append(renderKeyword("package")).append(" ");
renderName(packageView, builder);
if (debugMode) {
builder.append(" in context of ");
renderName(packageView.getModule(), builder);
}
}
private void renderPackageFragment(@NotNull PackageFragmentDescriptor fragment, @NotNull StringBuilder builder) {
builder.append(renderKeyword("package-fragment")).append(" ");
renderName(fragment, builder);
builder.append(" in context of ");
renderName(fragment.getContainingDeclaration(), builder);
if (debugMode) {
builder.append(" in context of ");
renderName(fragment.getContainingDeclaration(), builder);
}
}