Loading standard classes from a piece of Jet code. Lazy resolution facilities started

This commit is contained in:
Andrey Breslav
2011-02-24 18:54:10 +03:00
parent bfe22843ce
commit 7b71e7a2f2
10 changed files with 394 additions and 107 deletions
+1
View File
@@ -14,6 +14,7 @@
<entry name="?*.dtd" />
<entry name="?*.tld" />
<entry name="?*.ftl" />
<entry name="?*.jet" />
</wildcardResourcePatterns>
<annotationProcessing enabled="false" useClasspath="true" />
</component>
+62
View File
@@ -0,0 +1,62 @@
namespace jet.lang
virtual class Comparable<T> {
fun compareTo(other : T) : Int
}
class Boolean {
}
class String {
fun get(index : Int) : Char
val length : Int
fun plus(other : Any?) : String
}
class Double {
fun plus(other : Double) : Double
}
class Float {
fun plus(other : Double) : Double
fun plus(other : Float) : Float
}
class Long {
fun plus(other : Double) : Double
fun plus(other : Float) : Float
fun plus(other : Long) : Long
}
class Int {
fun plus(other : Double) : Double
fun plus(other : Float) : Float
fun plus(other : Long) : Long
fun plus(other : Int) : Int
}
class Char {
fun plus(other : Double) : Double
fun plus(other : Float) : Float
fun plus(other : Long) : Long
fun plus(other : Int) : Int
fun plus(other : Char) : Char
}
class Short {
fun plus(other : Double) : Double
fun plus(other : Float) : Float
fun plus(other : Long) : Long
fun plus(other : Int) : Int
fun plus(other : Short) : Short
}
class Byte {
fun plus(other : Double) : Double
fun plus(other : Float) : Float
fun plus(other : Long) : Long
fun plus(other : Int) : Int
fun plus(other : Short) : Short
fun plus(other : Byte) : Byte
}
@@ -18,7 +18,7 @@ public class ClassDescriptorResolver {
@Nullable
public ClassDescriptor resolveClassDescriptor(@NotNull JetScope scope, @NotNull JetClass classElement) {
ParameterExtensibleScope parameterScope = new ParameterExtensibleScope(scope);
WritableScope parameterScope = new WritableScope(scope);
// This call has side-effects on the parameterScope (fills it in)
List<TypeParameterDescriptor> typeParameters
@@ -32,7 +32,7 @@ public class ClassDescriptorResolver {
boolean open = classElement.hasModifier(JetTokens.OPEN_KEYWORD);
WritableScope members = resolveMembers(classElement, typeParameters, scope, parameterScope, superclasses);
return new ClassDescriptor(
return new ClassDescriptorImpl(
AttributeResolver.INSTANCE.resolveAttributes(classElement.getModifierList()),
!open,
classElement.getName(),
@@ -49,7 +49,7 @@ public class ClassDescriptorResolver {
final JetScope typeParameterScope,
final Collection<? extends Type> supertypes) {
final WritableScope memberDeclarations = new WritableScope();
final WritableScope memberDeclarations = new WritableScope(typeParameterScope);
List<JetDeclaration> declarations = classElement.getDeclarations();
for (JetDeclaration declaration : declarations) {
@@ -86,7 +86,7 @@ public class ClassDescriptorResolver {
@NotNull
public FunctionDescriptor resolveFunctionDescriptor(JetScope scope, JetFunction function) {
ParameterExtensibleScope parameterScope = new ParameterExtensibleScope(scope);
WritableScope parameterScope = new WritableScope(scope);
// The two calls below have side-effects on parameterScope
List<TypeParameterDescriptor> typeParameterDescriptors = resolveTypeParameters(parameterScope, function.getTypeParameters());
List<ValueParameterDescriptor> valueParameterDescriptors = resolveValueParameters(parameterScope, function.getValueParameters());
@@ -112,7 +112,7 @@ public class ClassDescriptorResolver {
);
}
private List<ValueParameterDescriptor> resolveValueParameters(ParameterExtensibleScope parameterScope, List<JetParameter> valueParameters) {
private List<ValueParameterDescriptor> resolveValueParameters(WritableScope parameterScope, List<JetParameter> valueParameters) {
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
for (JetParameter valueParameter : valueParameters) {
JetTypeReference typeReference = valueParameter.getTypeReference();
@@ -135,7 +135,7 @@ public class ClassDescriptorResolver {
return result;
}
private static List<TypeParameterDescriptor> resolveTypeParameters(ParameterExtensibleScope extensibleScope, List<JetTypeParameter> typeParameters) {
public List<TypeParameterDescriptor> resolveTypeParameters(WritableScope extensibleScope, List<JetTypeParameter> typeParameters) {
// TODO : When-clause
List<TypeParameterDescriptor> result = new ArrayList<TypeParameterDescriptor>();
for (JetTypeParameter typeParameter : typeParameters) {
@@ -144,7 +144,7 @@ public class ClassDescriptorResolver {
return result;
}
private static TypeParameterDescriptor resolveTypeParameter(ParameterExtensibleScope extensibleScope, JetTypeParameter typeParameter) {
private static TypeParameterDescriptor resolveTypeParameter(WritableScope extensibleScope, JetTypeParameter typeParameter) {
JetTypeReference extendsBound = typeParameter.getExtendsBound();
TypeParameterDescriptor typeParameterDescriptor = new TypeParameterDescriptor(
AttributeResolver.INSTANCE.resolveAttributes(typeParameter.getModifierList()),
@@ -158,7 +158,7 @@ public class ClassDescriptorResolver {
return typeParameterDescriptor;
}
private static Collection<? extends Type> resolveTypes(ParameterExtensibleScope extensibleScope, List<JetDelegationSpecifier> delegationSpecifiers) {
public static Collection<? extends Type> resolveTypes(WritableScope extensibleScope, List<JetDelegationSpecifier> delegationSpecifiers) {
if (delegationSpecifiers.isEmpty()) {
return Collections.emptyList();
}
@@ -201,49 +201,4 @@ public class ClassDescriptorResolver {
property.getName(),
type);
}
private static final class ParameterExtensibleScope extends JetScopeAdapter {
private final Map<String, TypeParameterDescriptor> typeParameterDescriptors = new HashMap<String, TypeParameterDescriptor>();
private final Map<String, PropertyDescriptor> propertyDescriptors = new HashMap<String, PropertyDescriptor>();
private ParameterExtensibleScope(JetScope scope) {
super(scope);
}
public void addTypeParameterDescriptor(TypeParameterDescriptor typeParameterDescriptor) {
String name = typeParameterDescriptor.getName();
if (typeParameterDescriptors.containsKey(name)) {
throw new UnsupportedOperationException(); // TODO
}
typeParameterDescriptors.put(name, typeParameterDescriptor);
}
@Override
public TypeParameterDescriptor getTypeParameter(String name) {
TypeParameterDescriptor typeParameterDescriptor = typeParameterDescriptors.get(name);
if (typeParameterDescriptor != null) {
return typeParameterDescriptor;
}
return super.getTypeParameter(name);
}
public void addPropertyDescriptor(PropertyDescriptor descriptor) {
String name = descriptor.getName();
if (propertyDescriptors.containsKey(name)) {
throw new UnsupportedOperationException(); // TODO
}
propertyDescriptors.put(name, descriptor);
}
@Override
public PropertyDescriptor getProperty(String name) {
PropertyDescriptor PropertyDescriptor = propertyDescriptors.get(name);
if (PropertyDescriptor != null) {
return PropertyDescriptor;
}
return super.getProperty(name);
}
}
}
@@ -0,0 +1,20 @@
package org.jetbrains.jet.lang.resolve;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
/**
* @author abreslav
*/
public class FileContentsResolver {
public static final FileContentsResolver INSTANCE = new FileContentsResolver();
private FileContentsResolver() {}
@NotNull
public JetScope resolveFileContents(@NotNull JetScope scope, @NotNull JetFile file) {
return new LazyScope(scope, file.getRootNamespace().getDeclarations());
}
}
@@ -0,0 +1,84 @@
package org.jetbrains.jet.lang.resolve;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.psi.JetDelegationSpecifier;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.lexer.JetTokens;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @author abreslav
*/
public class LazyClassDescriptor implements ClassDescriptor {
private final JetScope outerScope;
private JetClass declaration;
private TypeConstructor typeConstructor;
private LazyScope unsubstitutedMemberScope;
private List<Attribute> attributes;
private WritableScope parameterScope;
private List<TypeParameterDescriptor> typeParameterDescriptors;
public LazyClassDescriptor(@NotNull JetScope scope, @NotNull JetClass klass) {
this.declaration = klass;
this.outerScope = scope;
}
@Override
public List<Attribute> getAttributes() {
if (attributes == null) {
attributes = AttributeResolver.INSTANCE.resolveAttributes(declaration.getModifierList());
}
return attributes;
}
@Override
public String getName() {
return declaration.getName();
}
@NotNull
@Override
public TypeConstructor getTypeConstructor() {
if (typeConstructor == null) {
List<TypeParameterDescriptor> typeParameters = getTypeParameters();
List<JetDelegationSpecifier> delegationSpecifiers = declaration.getDelegationSpecifiers();
// TODO : assuming that the hierarchy is acyclic
// TODO : cannot inherit from an inner class
Collection<? extends Type> superclasses = delegationSpecifiers.isEmpty()
? Collections.singleton(JetStandardClasses.getAnyType())
: ClassDescriptorResolver.resolveTypes(getTypeParameterScope(), delegationSpecifiers);
boolean open = declaration.hasModifier(JetTokens.OPEN_KEYWORD);
typeConstructor = new TypeConstructor(getAttributes(), !open, getName(), typeParameters, superclasses);
}
return typeConstructor;
}
public WritableScope getTypeParameterScope() {
getTypeParameters();
return parameterScope;
}
private List<TypeParameterDescriptor> getTypeParameters() {
if (parameterScope == null) {
parameterScope = new WritableScope(outerScope);
typeParameterDescriptors = ClassDescriptorResolver.INSTANCE.resolveTypeParameters(parameterScope, declaration.getTypeParameters());
}
return typeParameterDescriptors;
}
@Override
public JetScope getMemberScope(List<TypeProjection> typeArguments) {
if (unsubstitutedMemberScope == null) {
unsubstitutedMemberScope = new LazyScope(getTypeParameterScope(), declaration.getDeclarations());
}
List<TypeParameterDescriptor> parameters = getTypeConstructor().getParameters();
Map<TypeConstructor,TypeProjection> substitutionContext = TypeSubstitutor.INSTANCE.buildSubstitutionContext(parameters, typeArguments);
return new SubstitutingScope(unsubstitutedMemberScope, substitutionContext);
}
}
@@ -0,0 +1,81 @@
package org.jetbrains.jet.lang.resolve;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetVisitor;
import org.jetbrains.jet.lang.types.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author abreslav
*/
public class LazyScope extends JetScopeAdapter {
private final List<JetDeclaration> declarations;
private Map<String, ClassDescriptor> classDescriptors;
public LazyScope(JetScope scope, List<JetDeclaration> declarations) {
super(scope);
this.declarations = declarations;
}
public Map<String, ClassDescriptor> getClassDescriptors() {
if (classDescriptors == null) {
classDescriptors = new HashMap<String, ClassDescriptor>();
for (JetDeclaration declaration : declarations) {
declaration.accept(new JetVisitor() {
@Override
public void visitClass(JetClass klass) {
classDescriptors.put(klass.getName(), new LazyClassDescriptor(LazyScope.this, klass));
}
});
}
}
return classDescriptors;
}
@Override
public ClassDescriptor getClass(String name) {
ClassDescriptor classDescriptor = getClassDescriptors().get(name);
if (classDescriptor != null) {
return classDescriptor;
}
return super.getClass(name);
}
@Override
public PropertyDescriptor getProperty(String name) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public ExtensionDescriptor getExtension(String name) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public NamespaceDescriptor getNamespace(String name) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public TypeParameterDescriptor getTypeParameter(String name) {
throw new UnsupportedOperationException(); // TODO
}
@NotNull
@Override
public Type getThisType() {
throw new UnsupportedOperationException(); // TODO
}
@NotNull
@Override
public FunctionGroup getFunctionGroup(@NotNull String name) {
throw new UnsupportedOperationException(); // TODO
}
}
@@ -9,11 +9,21 @@ import java.util.*;
/**
* @author abreslav
*/
public class WritableScope implements JetScope {
public class WritableScope extends JetScopeAdapter {
@Nullable
private Map<String, PropertyDescriptor> propertyDescriptors;
@Nullable
private Map<String, WritableFunctionGroup> functionGroups;
@Nullable
private Map<String, TypeParameterDescriptor> typeParameterDescriptors;
public WritableScope(JetScope scope) {
super(scope);
}
public WritableScope() {
super(JetScope.EMPTY);
}
@NotNull
private Map<String, PropertyDescriptor> getPropertyDescriptors() {
@@ -25,7 +35,9 @@ public class WritableScope implements JetScope {
public void addPropertyDescriptor(PropertyDescriptor propertyDescriptor) {
Map<String, PropertyDescriptor> propertyDescriptors = getPropertyDescriptors();
assert !propertyDescriptors.containsKey(propertyDescriptor.getName()) : "Property redeclared";
if (propertyDescriptors.containsKey(propertyDescriptor.getName())) {
throw new UnsupportedOperationException("Property redeclared: " + propertyDescriptor.getName());
}
propertyDescriptors.put(propertyDescriptor.getName(), propertyDescriptor);
}
@@ -67,9 +79,35 @@ public class WritableScope implements JetScope {
return functionGroup;
}
@NotNull
private Map<String, TypeParameterDescriptor> getTypeParameterDescriptors() {
if (typeParameterDescriptors == null) {
typeParameterDescriptors = new HashMap<String, TypeParameterDescriptor>();
}
return typeParameterDescriptors;
}
public void addTypeParameterDescriptor(TypeParameterDescriptor typeParameterDescriptor) {
String name = typeParameterDescriptor.getName();
Map<String, TypeParameterDescriptor> typeParameterDescriptors = getTypeParameterDescriptors();
if (typeParameterDescriptors.containsKey(name)) {
throw new UnsupportedOperationException("Type parameter redeclared"); // TODO
}
typeParameterDescriptors.put(name, typeParameterDescriptor);
}
@Override
public TypeParameterDescriptor getTypeParameter(String name) {
TypeParameterDescriptor typeParameterDescriptor = getTypeParameterDescriptors().get(name);
if (typeParameterDescriptor != null) {
return typeParameterDescriptor;
}
return super.getTypeParameter(name);
}
@Override
public ClassDescriptor getClass(String name) {
throw new UnsupportedOperationException(); // TODO
return super.getClass(name); // TODO
}
@Override
@@ -82,11 +120,6 @@ public class WritableScope implements JetScope {
throw new UnsupportedOperationException(); // TODO
}
@Override
public TypeParameterDescriptor getTypeParameter(String name) {
throw new UnsupportedOperationException(); // TODO
}
@NotNull
@Override
public Type getThisType() {
@@ -2,42 +2,15 @@ package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.JetScope;
import org.jetbrains.jet.lang.resolve.SubstitutingScope;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @author abreslav
*/
public class ClassDescriptor extends MemberDescriptorImpl {
private final TypeConstructor typeConstructor;
private final JetScope memberDeclarations;
public ClassDescriptor(
List<Attribute> attributes, boolean sealed,
String name, List<TypeParameterDescriptor> typeParameters,
Collection<? extends Type> superclasses, JetScope memberDeclarations) {
super(attributes, name);
this.typeConstructor = new TypeConstructor(attributes, sealed, name, typeParameters, superclasses);
this.memberDeclarations = memberDeclarations;
}
public ClassDescriptor(String name, JetScope memberDeclarations) {
this(Collections.<Attribute>emptyList(), true,
name, Collections.<TypeParameterDescriptor>emptyList(),
Collections.<Type>singleton(JetStandardClasses.getAnyType()), memberDeclarations);
}
public interface ClassDescriptor extends Annotated, Named {
@NotNull
public TypeConstructor getTypeConstructor() {
return typeConstructor;
}
TypeConstructor getTypeConstructor();
public JetScope getMemberScope(List<TypeProjection> typeArguments) {
Map<TypeConstructor,TypeProjection> substitutionContext = TypeSubstitutor.INSTANCE.buildSubstitutionContext(typeConstructor.getParameters(), typeArguments);
return new SubstitutingScope(memberDeclarations, substitutionContext);
}
}
JetScope getMemberScope(List<TypeProjection> typeArguments);
}
@@ -0,0 +1,45 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.JetScope;
import org.jetbrains.jet.lang.resolve.SubstitutingScope;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @author abreslav
*/
public class ClassDescriptorImpl extends MemberDescriptorImpl implements ClassDescriptor {
private final TypeConstructor typeConstructor;
private final JetScope memberDeclarations;
public ClassDescriptorImpl(
List<Attribute> attributes, boolean sealed,
String name, List<TypeParameterDescriptor> typeParameters,
Collection<? extends Type> superclasses, JetScope memberDeclarations) {
super(attributes, name);
this.typeConstructor = new TypeConstructor(attributes, sealed, name, typeParameters, superclasses);
this.memberDeclarations = memberDeclarations;
}
public ClassDescriptorImpl(String name, JetScope memberDeclarations) {
this(Collections.<Attribute>emptyList(), true,
name, Collections.<TypeParameterDescriptor>emptyList(),
Collections.<Type>singleton(JetStandardClasses.getAnyType()), memberDeclarations);
}
@Override
@NotNull
public TypeConstructor getTypeConstructor() {
return typeConstructor;
}
@Override
public JetScope getMemberScope(List<TypeProjection> typeArguments) {
Map<TypeConstructor,TypeProjection> substitutionContext = TypeSubstitutor.INSTANCE.buildSubstitutionContext(typeConstructor.getParameters(), typeArguments);
return new SubstitutingScope(memberDeclarations, substitutionContext);
}
}
@@ -1,10 +1,20 @@
package org.jetbrains.jet.lang.types;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiFileFactory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.JetFileType;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.FileContentsResolver;
import org.jetbrains.jet.lang.resolve.JetScope;
import org.jetbrains.jet.lang.resolve.JetScopeImpl;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
@@ -14,7 +24,7 @@ import java.util.*;
*/
public class JetStandardClasses {
private static ClassDescriptor NOTHING_CLASS = new ClassDescriptor(
private static ClassDescriptor NOTHING_CLASS = new ClassDescriptorImpl(
Collections.<Attribute>emptyList(),
true,
"Nothing",
@@ -37,7 +47,7 @@ public class JetStandardClasses {
}, JetScope.EMPTY
);
private static final ClassDescriptor ANY = new ClassDescriptor(
private static final ClassDescriptor ANY = new ClassDescriptorImpl(
Collections.<Attribute>emptyList(),
false,
"Any",
@@ -50,17 +60,40 @@ public class JetStandardClasses {
private static final Type ANY_TYPE = new TypeImpl(ANY.getTypeConstructor(), JetScope.EMPTY);
private static final Type NULLABLE_ANY_TYPE = TypeUtils.makeNullable(ANY_TYPE);
private static final ClassDescriptor BYTE = new ClassDescriptor("Byte", STUB);
private static final ClassDescriptor CHAR = new ClassDescriptor("Char", STUB);
private static final ClassDescriptor SHORT = new ClassDescriptor("Short", STUB);
private static final ClassDescriptor INT = new ClassDescriptor("Int", STUB);
private static final ClassDescriptor LONG = new ClassDescriptor("Long", STUB);
private static final ClassDescriptor FLOAT = new ClassDescriptor("Float", STUB);
private static final ClassDescriptor DOUBLE = new ClassDescriptor("Double", STUB);
private static final ClassDescriptor BOOLEAN = new ClassDescriptor("Boolean", STUB);
private static final JetScope LIBRARY_SCOPE;
static {
// TODO : review
Project project = ProjectManager.getInstance().getDefaultProject();
InputStream stream = JetStandardClasses.class.getClassLoader().getResourceAsStream("jet/lang/Library.jet");
try {
//noinspection IOResourceOpenedButNotSafelyClosed
JetFile file = (JetFile) PsiFileFactory.getInstance(project).createFileFromText("Library.jet", JetFileType.INSTANCE, FileUtil.loadTextAndClose(new InputStreamReader(stream)));
private static final ClassDescriptor STRING = new ClassDescriptor("String", STUB);
LIBRARY_SCOPE = FileContentsResolver.INSTANCE.resolveFileContents(JetScope.EMPTY, file);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
private static final Type NULLABLE_ANY_TYPE = TypeUtils.makeNullable(ANY_TYPE);
@NotNull
private static final ClassDescriptor BYTE = LIBRARY_SCOPE.getClass("Byte");
@NotNull
private static final ClassDescriptor CHAR = LIBRARY_SCOPE.getClass("Char");
@NotNull
private static final ClassDescriptor SHORT = LIBRARY_SCOPE.getClass("Short");
@NotNull
private static final ClassDescriptor INT = LIBRARY_SCOPE.getClass("Int");
@NotNull
private static final ClassDescriptor LONG = LIBRARY_SCOPE.getClass("Long");
@NotNull
private static final ClassDescriptor FLOAT = LIBRARY_SCOPE.getClass("Float");
@NotNull
private static final ClassDescriptor DOUBLE = LIBRARY_SCOPE.getClass("Double");
@NotNull
private static final ClassDescriptor BOOLEAN = LIBRARY_SCOPE.getClass("Boolean");
@NotNull
private static final ClassDescriptor STRING = LIBRARY_SCOPE.getClass("String");
public static final int TUPLE_COUNT = 22;
private static final ClassDescriptor[] TUPLE = new ClassDescriptor[TUPLE_COUNT];
@@ -74,7 +107,7 @@ public class JetStandardClasses {
Variance.OUT_VARIANCE, "T" + j,
Collections.singleton(getNullableAnyType())));
}
TUPLE[i] = new ClassDescriptor(
TUPLE[i] = new ClassDescriptorImpl(
Collections.<Attribute>emptyList(),
true,
"Tuple" + i,
@@ -100,7 +133,7 @@ public class JetStandardClasses {
Collections.<Attribute>emptyList(),
Variance.OUT_VARIANCE, "R",
Collections.singleton(getNullableAnyType())));
FUNCTION[i] = new ClassDescriptor(
FUNCTION[i] = new ClassDescriptorImpl(
Collections.<Attribute>emptyList(),
false,
"Function" + i,
@@ -110,7 +143,7 @@ public class JetStandardClasses {
Collections.<Attribute>emptyList(),
Variance.IN_VARIANCE, "T",
Collections.singleton(getNullableAnyType())));
RECEIVER_FUNCTION[i] = new ClassDescriptor(
RECEIVER_FUNCTION[i] = new ClassDescriptorImpl(
Collections.<Attribute>emptyList(),
false,
"ReceiverFunction" + i,