Basic resolve to Java elements. One can resolve System.out.println(1), but not println("1") yet (only exact signature match works, no best fit overload is looked for).

This commit is contained in:
Andrey Breslav
2011-03-03 23:18:59 +03:00
parent a327254187
commit da7f44b8f2
36 changed files with 727 additions and 308 deletions
-1
View File
@@ -6,7 +6,6 @@
<option name="LINE_SEPARATOR" value="&#10;" />
<option name="ELSE_ON_NEW_LINE" value="true" />
<option name="ALIGN_MULTILINE_BINARY_OPERATION" value="true" />
<option name="ALIGN_GROUP_FIELD_DECLARATIONS" value="true" />
<option name="IF_BRACE_FORCE" value="1" />
<ADDITIONAL_INDENT_OPTIONS fileType="groovy">
<option name="INDENT_SIZE" value="2" />
@@ -1,12 +1,15 @@
package org.jetbrains.jet.codegen;
import com.intellij.openapi.project.Project;
import org.jetbrains.jet.lang.ErrorHandler;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetFunction;
import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.TopDownAnalyzer;
import org.jetbrains.jet.lang.resolve.*;
import org.jetbrains.jet.lang.resolve.java.JavaLangScope;
import org.jetbrains.jet.lang.resolve.java.JavaSemanticServices;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;
@@ -19,9 +22,15 @@ public class NamespaceCodegen {
public NamespaceCodegen() {
}
public void generate(JetNamespace namespace, ClassVisitor v, JetSemanticServices semanticServices) {
public void generate(JetNamespace namespace, ClassVisitor v, Project project) {
List<JetDeclaration> declarations = namespace.getDeclarations();
BindingContext bindingContext = new TopDownAnalyzer(semanticServices).process(semanticServices.getStandardLibrary().getLibraryScope(), declarations);
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(project, ErrorHandler.THROW_EXCEPTION);
BindingTraceContext bindingTraceContext = new BindingTraceContext();
ScopeWithImports scope = new ScopeWithImports(semanticServices.getStandardLibrary().getLibraryScope());
scope.addImport(new JavaLangScope(new JavaSemanticServices(project, semanticServices, bindingTraceContext)));
new TopDownAnalyzer(semanticServices, bindingTraceContext).process(scope, declarations);
BindingContext bindingContext = bindingTraceContext;
final PropertyCodegen propertyCodegen = new PropertyCodegen(v);
final FunctionCodegen functionCodegen = new FunctionCodegen(v, semanticServices.getStandardLibrary(), bindingContext);
@@ -2,6 +2,7 @@ package org.jetbrains.jet.lang.annotations;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
@@ -9,7 +10,11 @@ import org.jetbrains.jet.lang.ErrorHandler;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.lang.resolve.ScopeWithImports;
import org.jetbrains.jet.lang.resolve.TopDownAnalyzer;
import org.jetbrains.jet.lang.resolve.java.JavaLangScope;
import org.jetbrains.jet.lang.resolve.java.JavaSemanticServices;
import org.jetbrains.jet.lang.types.Type;
/**
@@ -20,6 +25,8 @@ public class JetPsiChecker implements Annotator {
@Override
public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolder holder) {
if (element instanceof JetFile) {
Project project = element.getProject();
JetFile file = (JetFile) element;
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(element.getProject(), new ErrorHandler() {
@Override
@@ -28,7 +35,13 @@ public class JetPsiChecker implements Annotator {
}
});
try {
final BindingContext bindingContext = new TopDownAnalyzer(semanticServices).process(semanticServices.getStandardLibrary().getLibraryScope(), file.getRootNamespace().getDeclarations());
ScopeWithImports scope = new ScopeWithImports(semanticServices.getStandardLibrary().getLibraryScope());
BindingTraceContext bindingTraceContext = new BindingTraceContext();
scope.addImport(new JavaLangScope(new JavaSemanticServices(project, semanticServices, bindingTraceContext)));
new TopDownAnalyzer(semanticServices, bindingTraceContext).process(
scope,
file.getRootNamespace().getDeclarations());
final BindingContext bindingContext = bindingTraceContext;
file.getRootNamespace().accept(new JetVisitor() {
@Override
public void visitClass(JetClass klass) {
@@ -1,10 +0,0 @@
package org.jetbrains.jet.lang.modules;
import java.util.Collection;
/**
* @author abreslav
*/
public interface JetModule extends NamespaceDomain {
Collection<JetModule> getImports();
}
@@ -1,12 +0,0 @@
package org.jetbrains.jet.lang.modules;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.types.NamespaceDescriptor;
/**
* @author abreslav
*/
public interface NamespaceDomain {
@Nullable
NamespaceDescriptor getNamespace(String name);
}
@@ -11,7 +11,11 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.ErrorHandler;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.lang.resolve.ScopeWithImports;
import org.jetbrains.jet.lang.resolve.TopDownAnalyzer;
import org.jetbrains.jet.lang.resolve.java.JavaLangScope;
import org.jetbrains.jet.lang.resolve.java.JavaSemanticServices;
import org.jetbrains.jet.lexer.JetTokens;
/**
@@ -68,7 +72,14 @@ public class JetReferenceExpression extends JetExpression {
}
JetFile file = (JetFile) element;
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(element.getProject(), ErrorHandler.DO_NOTHING);
BindingContext bindingContext = new TopDownAnalyzer(semanticServices).process(semanticServices.getStandardLibrary().getLibraryScope(), file.getRootNamespace().getDeclarations());
ScopeWithImports scope = new ScopeWithImports(semanticServices.getStandardLibrary().getLibraryScope());
BindingTraceContext bindingTraceContext = new BindingTraceContext();
scope.addImport(new JavaLangScope(new JavaSemanticServices(element.getProject(), semanticServices, bindingTraceContext)));
new TopDownAnalyzer(semanticServices, bindingTraceContext).process(scope, file.getRootNamespace().getDeclarations());
BindingContext bindingContext = bindingTraceContext;
return bindingContext.resolveToDeclarationPsiElement(JetReferenceExpression.this);
}
@@ -7,44 +7,19 @@ import org.jetbrains.jet.lang.types.*;
/**
* @author abreslav
*/
public class BindingContext {
public NamespaceDescriptor getNamespaceDescriptor(JetNamespace declaration) {
return null;
}
public interface BindingContext {
NamespaceDescriptor getNamespaceDescriptor(JetNamespace declaration);
ClassDescriptor getClassDescriptor(JetClass declaration);
FunctionDescriptor getFunctionDescriptor(JetFunction declaration);
PropertyDescriptor getPropertyDescriptor(JetProperty declaration);
public ClassDescriptor getClassDescriptor(JetClass declaration) {
return null;
}
Type getExpressionType(JetExpression expression);
public FunctionDescriptor getFunctionDescriptor(JetFunction declaration) {
return null;
}
public PropertyDescriptor getPropertyDescriptor(JetProperty declaration) {
return null;
}
JetScope getTopLevelScope();
public Type getExpressionType(JetExpression expression) {
return null;
}
public JetScope getTopLevelScope() {
return null;
}
public DeclarationDescriptor resolveReferenceExpression(JetReferenceExpression referenceExpression) {
return null;
}
public Type resolveTypeReference(JetTypeReference typeReference) {
return null;
}
public PsiElement resolveToDeclarationPsiElement(JetReferenceExpression referenceExpression) {
return null;
}
DeclarationDescriptor resolveReferenceExpression(JetReferenceExpression referenceExpression);
Type resolveTypeReference(JetTypeReference typeReference);
PsiElement resolveToDeclarationPsiElement(JetReferenceExpression referenceExpression);
}
@@ -0,0 +1,95 @@
package org.jetbrains.jet.lang.resolve;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.types.*;
import java.util.HashMap;
import java.util.Map;
/**
* @author abreslav
*/
public class BindingTraceContext extends BindingTrace implements BindingContext {
private final Map<JetExpression, Type> expressionTypes = new HashMap<JetExpression, Type>();
private final Map<JetReferenceExpression, DeclarationDescriptor> resolutionResults = new HashMap<JetReferenceExpression, DeclarationDescriptor>();
private final Map<JetTypeReference, Type> types = new HashMap<JetTypeReference, Type>();
private final Map<DeclarationDescriptor, PsiElement> descriptorToDeclarations = new HashMap<DeclarationDescriptor, PsiElement>();
private final Map<PsiElement, DeclarationDescriptor> declarationsToDescriptors = new HashMap<PsiElement, DeclarationDescriptor>();
private JetScope toplevelScope;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void recordExpressionType(@NotNull JetExpression expression, @NotNull Type type) {
expressionTypes.put(expression, type);
}
@Override
public void recordReferenceResolution(@NotNull JetReferenceExpression expression, @NotNull DeclarationDescriptor descriptor) {
resolutionResults.put(expression, descriptor);
}
@Override
public void recordTypeResolution(@NotNull JetTypeReference typeReference, @NotNull Type type) {
types.put(typeReference, type);
}
@Override
public void recordDeclarationResolution(@NotNull PsiElement declaration, @NotNull DeclarationDescriptor descriptor) {
descriptorToDeclarations.put(descriptor, declaration);
declarationsToDescriptors.put(declaration, descriptor);
}
public void setToplevelScope(JetScope toplevelScope) {
this.toplevelScope = toplevelScope;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public NamespaceDescriptor getNamespaceDescriptor(JetNamespace declaration) {
return (NamespaceDescriptor) declarationsToDescriptors.get(declaration);
}
@Override
public ClassDescriptor getClassDescriptor(JetClass declaration) {
return (ClassDescriptor) declarationsToDescriptors.get(declaration);
}
@Override
public FunctionDescriptor getFunctionDescriptor(JetFunction declaration) {
return (FunctionDescriptor) declarationsToDescriptors.get(declaration);
}
@Override
public PropertyDescriptor getPropertyDescriptor(JetProperty declaration) {
return (PropertyDescriptor) declarationsToDescriptors.get(declaration);
}
@Override
public Type resolveTypeReference(JetTypeReference typeReference) {
return types.get(typeReference);
}
@Override
public Type getExpressionType(JetExpression expression) {
return expressionTypes.get(expression);
}
@Override
public DeclarationDescriptor resolveReferenceExpression(JetReferenceExpression referenceExpression) {
return resolutionResults.get(referenceExpression);
}
@Override
public JetScope getTopLevelScope() {
return toplevelScope;
}
@Override
public PsiElement resolveToDeclarationPsiElement(JetReferenceExpression referenceExpression) {
return descriptorToDeclarations.get(resolveReferenceExpression(referenceExpression));
}
}
@@ -41,7 +41,6 @@ public class ClassDescriptorResolver {
WritableScope members = resolveMembers(classElement, typeParameters, scope, parameterScope, superclasses);
return new ClassDescriptorImpl(
classElement,
AttributeResolver.INSTANCE.resolveAttributes(classElement.getModifierList()),
!open,
classElement.getName(),
@@ -140,7 +139,6 @@ public class ClassDescriptorResolver {
}
FunctionDescriptorImpl functionDescriptor = new FunctionDescriptorImpl(
function,
null,
AttributeResolver.INSTANCE.resolveAttributes(function.getModifierList()),
function.getName(),
@@ -155,13 +153,14 @@ public class ClassDescriptorResolver {
private List<ValueParameterDescriptor> resolveValueParameters(WritableScope parameterScope, List<JetParameter> valueParameters) {
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
for (JetParameter valueParameter : valueParameters) {
for (int i = 0, valueParametersSize = valueParameters.size(); i < valueParametersSize; i++) {
JetParameter valueParameter = valueParameters.get(i);
JetTypeReference typeReference = valueParameter.getTypeReference();
assert typeReference != null : "Parameters without type annotations are not supported"; // TODO
ValueParameterDescriptor valueParameterDescriptor = new ValueParameterDescriptorImpl(
valueParameter,
i,
AttributeResolver.INSTANCE.resolveAttributes(valueParameter.getModifierList()),
valueParameter.getName(),
typeResolver.resolveType(parameterScope, typeReference),
@@ -190,7 +189,6 @@ public class ClassDescriptorResolver {
private TypeParameterDescriptor resolveTypeParameter(WritableScope extensibleScope, JetTypeParameter typeParameter) {
JetTypeReference extendsBound = typeParameter.getExtendsBound();
TypeParameterDescriptor typeParameterDescriptor = new TypeParameterDescriptor(
typeParameter,
AttributeResolver.INSTANCE.resolveAttributes(typeParameter.getModifierList()),
typeParameter.getVariance(),
typeParameter.getName(),
@@ -221,7 +219,6 @@ public class ClassDescriptorResolver {
@NotNull
public PropertyDescriptor resolvePropertyDescriptor(@NotNull JetScope scope, @NotNull JetParameter parameter) {
return new PropertyDescriptorImpl(
parameter,
AttributeResolver.INSTANCE.resolveAttributes(parameter.getModifierList()),
parameter.getName(),
typeResolver.resolveType(scope, parameter.getTypeReference()));
@@ -242,7 +239,6 @@ public class ClassDescriptorResolver {
}
return new PropertyDescriptorImpl(
property,
AttributeResolver.INSTANCE.resolveAttributes(property.getModifierList()),
property.getName(),
type);
@@ -1,43 +0,0 @@
package org.jetbrains.jet.lang.resolve;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetFunction;
import org.jetbrains.jet.lang.psi.JetParameter;
import org.jetbrains.jet.lang.psi.JetTypeParameter;
import org.jetbrains.jet.lang.types.*;
/**
* @author abreslav
*/
public class ResolveUtil {
@Nullable
private static <T extends JetElement> T getDeclarationElement(Object o) {
if (o instanceof DeclarationDescriptorImpl) {
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
T psiElement = ((DeclarationDescriptorImpl<T>) o).getPsiElement();
return psiElement;
}
return null;
}
@Nullable
private static <T extends JetElement> T getDeclarationElement(DeclarationDescriptorImpl<T> descriptor) {
return descriptor.getPsiElement();
}
@Nullable
public static JetTypeParameter getJetTypeParameter(TypeParameterDescriptor parameterDescriptor) {
return getDeclarationElement(parameterDescriptor);
}
@Nullable
public static JetParameter getJetParameter(ValueParameterDescriptor parameterDescriptor) {
return (JetParameter) getDeclarationElement(parameterDescriptor);
}
@Nullable
public static JetFunction getJetFunction(FunctionDescriptor functionDescriptor) {
return getDeclarationElement(functionDescriptor);
}
}
@@ -0,0 +1,85 @@
package org.jetbrains.jet.lang.resolve;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.types.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author abreslav
*/
public class ScopeWithImports implements JetScope {
private final List<JetScope> imports = new ArrayList<JetScope>();
private final JetScope scope;
public ScopeWithImports(JetScope scope) {
this.scope = scope;
}
public void addImport(JetScope imported) {
imports.add(0, imported);
}
@Override
public ClassDescriptor getClass(@NotNull String name) {
ClassDescriptor descriptor = scope.getClass(name);
if (descriptor != null) return descriptor;
for (JetScope imported : imports) {
ClassDescriptor importedClass = imported.getClass(name);
if (importedClass != null) {
return importedClass;
}
}
return null;
}
@Override
public PropertyDescriptor getProperty(@NotNull String name) {
PropertyDescriptor descriptor = scope.getProperty(name);
if (descriptor != null) return descriptor;
for (JetScope imported : imports) {
PropertyDescriptor importedDescriptor = imported.getProperty(name);
if (importedDescriptor != null) {
return importedDescriptor;
}
}
return null;
}
@Override
public ExtensionDescriptor getExtension(@NotNull String name) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public NamespaceDescriptor getNamespace(@NotNull String name) {
NamespaceDescriptor descriptor = scope.getNamespace(name);
if (descriptor != null) return descriptor;
for (JetScope imported : imports) {
NamespaceDescriptor importedDescriptor = imported.getNamespace(name);
if (importedDescriptor != null) {
return importedDescriptor;
}
}
return null;
}
@Override
public TypeParameterDescriptor getTypeParameter(@NotNull 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
}
}
@@ -1,12 +1,14 @@
package org.jetbrains.jet.lang.resolve;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.types.*;
import java.util.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @author abreslav
@@ -16,94 +18,24 @@ public class TopDownAnalyzer {
private final Map<JetClass, MutableClassDescriptor> classes = new LinkedHashMap<JetClass, MutableClassDescriptor>();
private final Map<JetFunction, FunctionDescriptor> functions = new HashMap<JetFunction, FunctionDescriptor>();
private final Map<JetDeclaration, WritableScope> declaringScopes = new HashMap<JetDeclaration, WritableScope>();
private final Map<JetProperty, PropertyDescriptor> properties = new HashMap<JetProperty, PropertyDescriptor>();
private final Map<JetExpression, Type> expressionTypes = new HashMap<JetExpression, Type>();
private final Map<JetReferenceExpression, DeclarationDescriptor> resolutionResults = new HashMap<JetReferenceExpression, DeclarationDescriptor>();
private final Map<JetTypeReference, Type> types = new HashMap<JetTypeReference, Type>();
private final Map<DeclarationDescriptor, JetDeclaration> descriptorToDeclarations = new HashMap<DeclarationDescriptor, JetDeclaration>();
private final JetSemanticServices semanticServices;
private final ClassDescriptorResolver classDescriptorResolver;
private final BindingTrace trace;
public TopDownAnalyzer(JetSemanticServices semanticServices) {
public TopDownAnalyzer(JetSemanticServices semanticServices, @NotNull BindingTrace bindingTrace) {
this.semanticServices = semanticServices;
this.classDescriptorResolver = new ClassDescriptorResolver(semanticServices, new BindingTrace() {
@Override
public void recordExpressionType(@NotNull JetExpression expression, @NotNull Type type) {
expressionTypes.put(expression, type);
}
@Override
public void recordReferenceResolution(@NotNull JetReferenceExpression expression, @NotNull DeclarationDescriptor descriptor) {
resolutionResults.put(expression, descriptor);
}
@Override
public void recordTypeResolution(@NotNull JetTypeReference typeReference, @NotNull Type type) {
types.put(typeReference, type);
}
@Override
public void recordDeclarationResolution(@NotNull JetDeclaration declaration, @NotNull DeclarationDescriptor descriptor) {
descriptorToDeclarations.put(descriptor, declaration);
}
});
this.classDescriptorResolver = new ClassDescriptorResolver(semanticServices, bindingTrace);
this.trace = bindingTrace;
}
public BindingContext process(@NotNull JetScope outerScope, @NotNull List<JetDeclaration> declarations) {
public void process(@NotNull JetScope outerScope, @NotNull List<JetDeclaration> declarations) {
final WritableScope toplevelScope = new WritableScope(outerScope);
trace.setToplevelScope(toplevelScope); // TODO : this is a hack
collectTypeDeclarators(toplevelScope, declarations);
resolveTypeDeclarations();
collectBehaviorDeclarators(toplevelScope, declarations);
resolveBehaviorDeclarationBodies();
return new BindingContext() {
@Override
public NamespaceDescriptor getNamespaceDescriptor(JetNamespace declaration) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public ClassDescriptor getClassDescriptor(JetClass declaration) {
return classes.get(declaration);
}
@Override
public FunctionDescriptor getFunctionDescriptor(JetFunction declaration) {
return functions.get(declaration);
}
@Override
public PropertyDescriptor getPropertyDescriptor(JetProperty declaration) {
return properties.get(declaration);
}
@Override
public Type resolveTypeReference(JetTypeReference typeReference) {
return types.get(typeReference);
}
@Override
public Type getExpressionType(JetExpression expression) {
return expressionTypes.get(expression);
}
@Override
public DeclarationDescriptor resolveReferenceExpression(JetReferenceExpression referenceExpression) {
return resolutionResults.get(referenceExpression);
}
@Override
public JetScope getTopLevelScope() {
return toplevelScope;
}
@Override
public PsiElement resolveToDeclarationPsiElement(JetReferenceExpression referenceExpression) {
return descriptorToDeclarations.get(resolveReferenceExpression(referenceExpression));
}
};
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -137,6 +69,7 @@ public class TopDownAnalyzer {
mutableClassDescriptor.setName(klass.getName());
declaringScope.addClassDescriptor(mutableClassDescriptor);
classes.put(klass, mutableClassDescriptor);
trace.recordDeclarationResolution(klass, mutableClassDescriptor);
declaringScopes.put(klass, declaringScope);
return mutableClassDescriptor.getUnsubstitutedMemberScope();
}
@@ -204,13 +137,14 @@ public class TopDownAnalyzer {
FunctionDescriptor descriptor = classDescriptorResolver.resolveFunctionDescriptor(declaringScope, function);
declaringScope.addFunctionDescriptor(descriptor);
functions.put(function, descriptor);
trace.recordDeclarationResolution(function, descriptor);
}
private void processProperty(WritableScope declaringScope, JetProperty property) {
declaringScopes.put(property, declaringScope);
PropertyDescriptor descriptor = classDescriptorResolver.resolvePropertyDescriptor(declaringScope, property);
declaringScope.addPropertyDescriptor(descriptor);
properties.put(property, descriptor);
trace.recordDeclarationResolution(property, descriptor);
}
private void processClassObject(JetClassObject classObject) {
@@ -243,27 +177,7 @@ public class TopDownAnalyzer {
}
private void resolveExpression(@NotNull JetScope scope, JetExpression expression, boolean preferBlock) {
semanticServices.getTypeInferrer(new BindingTrace() {
@Override
public void recordExpressionType(@NotNull JetExpression expression, @NotNull Type type) {
expressionTypes.put(expression, type);
}
@Override
public void recordReferenceResolution(@NotNull JetReferenceExpression expression, @NotNull DeclarationDescriptor descriptor) {
resolutionResults.put(expression, descriptor);
}
@Override
public void recordTypeResolution(@NotNull JetTypeReference typeReference, @NotNull Type type) {
types.put(typeReference, type);
}
@Override
public void recordDeclarationResolution(@NotNull JetDeclaration declaration, @NotNull DeclarationDescriptor descriptor) {
throw new IllegalStateException();
}
}).getType(scope, expression, preferBlock);
semanticServices.getTypeInferrer(trace).getType(scope, expression, preferBlock);
}
}
@@ -205,7 +205,7 @@ public class TypeResolver {
if (domain == null) {
return null;
}
return domain.getNamespace(expression.getReferencedName());
return domain.getMemberScope().getNamespace(expression.getReferencedName());
}
assert qualifier == null;
@@ -0,0 +1,93 @@
package org.jetbrains.jet.lang.resolve.java;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.JetScope;
import org.jetbrains.jet.lang.resolve.WritableFunctionGroup;
import org.jetbrains.jet.lang.types.*;
import java.util.Collections;
/**
* @author abreslav
*/
public class JavaClassMembersScope implements JetScope {
private final PsiClass psiClass;
private final JavaSemanticServices semanticServices;
private final boolean staticMembers;
public JavaClassMembersScope(PsiClass psiClass, JavaSemanticServices semanticServices, boolean staticMembers) {
this.psiClass = psiClass;
this.semanticServices = semanticServices;
this.staticMembers = staticMembers;
}
@Override
public ClassDescriptor getClass(@NotNull String name) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public PropertyDescriptor getProperty(@NotNull String name) {
PsiField field = psiClass.findFieldByName(name, true);
if (field == null) return null;
if (field.hasModifierProperty(PsiModifier.STATIC) != staticMembers) {
return null;
}
PropertyDescriptorImpl propertyDescriptor = new PropertyDescriptorImpl(
Collections.<Attribute>emptyList(),
field.getName(),
semanticServices.getTypeTransformer().transform(field.getType()));
semanticServices.getTrace().recordDeclarationResolution(field, propertyDescriptor);
return propertyDescriptor;
}
@NotNull
@Override
public FunctionGroup getFunctionGroup(@NotNull String name) {
WritableFunctionGroup writableFunctionGroup = new WritableFunctionGroup(name);
PsiMethod[] allMethods = psiClass.getAllMethods();
for (PsiMethod method : allMethods) {
if (method.hasModifierProperty(PsiModifier.STATIC) != staticMembers) {
continue;
}
if (!name.equals(method.getName())) {
continue;
}
PsiParameter[] parameters = method.getParameterList().getParameters();
FunctionDescriptorImpl functionDescriptor = new FunctionDescriptorImpl(
null,
Collections.<Attribute>emptyList(), // TODO
name,
Collections.<TypeParameterDescriptor>emptyList(), // TODO
semanticServices.getDescriptorResolver().resolveParameterDescriptors(parameters),
semanticServices.getTypeTransformer().transform(method.getReturnType())
);
semanticServices.getTrace().recordDeclarationResolution(method, functionDescriptor);
writableFunctionGroup.addFunction(functionDescriptor);
}
return writableFunctionGroup;
}
@Override
public ExtensionDescriptor getExtension(@NotNull String name) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public NamespaceDescriptor getNamespace(@NotNull String name) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public TypeParameterDescriptor getTypeParameter(@NotNull String name) {
throw new UnsupportedOperationException(); // TODO
}
@NotNull
@Override
public Type getThisType() {
throw new UnsupportedOperationException(); // TODO
}
}
@@ -0,0 +1,109 @@
package org.jetbrains.jet.lang.resolve.java;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.types.*;
import java.util.*;
/**
* @author abreslav
*/
public class JavaDescriptorResolver {
protected final Map<String, ClassDescriptor> classDescriptorCache = new HashMap<String, ClassDescriptor>();
protected final Map<String, NamespaceDescriptor> namespaceDescriptorCache = new HashMap<String, NamespaceDescriptor>();
protected final JavaPsiFacade javaFacade;
protected final GlobalSearchScope javaSearchScope;
protected final JavaSemanticServices semanticServices;
public JavaDescriptorResolver(Project project, JavaSemanticServices semanticServices) {
this.javaFacade = JavaPsiFacade.getInstance(project);
this.javaSearchScope = GlobalSearchScope.allScope(project);
this.semanticServices = semanticServices;
}
@NotNull
public ClassDescriptor resolveClass(@NotNull PsiClass psiClass) {
String qualifiedName = psiClass.getQualifiedName();
ClassDescriptor classDescriptor = classDescriptorCache.get(qualifiedName);
if (classDescriptor == null) {
classDescriptor = createJavaClassDescriptor(psiClass);
classDescriptorCache.put(qualifiedName, classDescriptor);
}
return classDescriptor;
}
@Nullable
public ClassDescriptor resolveClass(@NotNull String qualifiedName) {
ClassDescriptor classDescriptor = classDescriptorCache.get(qualifiedName);
if (classDescriptor == null) {
PsiClass psiClass = javaFacade.findClass(qualifiedName, javaSearchScope);
if (psiClass == null) {
return null;
}
classDescriptor = createJavaClassDescriptor(psiClass);
classDescriptorCache.put(qualifiedName, classDescriptor);
}
return classDescriptor;
}
private ClassDescriptor createJavaClassDescriptor(@NotNull PsiClass psiClass) {
String name = psiClass.getName();
PsiModifierList modifierList = psiClass.getModifierList();
ClassDescriptorImpl classDescriptor = new ClassDescriptorImpl(
Collections.<Attribute>emptyList(), // TODO
modifierList == null ? false : modifierList.hasModifierProperty(PsiModifier.FINAL),
name,
Collections.<TypeParameterDescriptor>emptyList(),
Collections.<Type>emptyList(),
new JavaClassMembersScope(psiClass, semanticServices, false)
);
semanticServices.getTrace().recordDeclarationResolution(psiClass, classDescriptor);
return classDescriptor;
}
public NamespaceDescriptor resolveNamespace(String qualifiedName) {
NamespaceDescriptor namespaceDescriptor = namespaceDescriptorCache.get(qualifiedName);
if (namespaceDescriptor == null) {
// TODO : packages
PsiClass psiClass = javaFacade.findClass(qualifiedName, javaSearchScope);
if (psiClass == null) {
return null;
}
namespaceDescriptor = createJavaNamespaceDescriptor(psiClass);
namespaceDescriptorCache.put(qualifiedName, namespaceDescriptor);
}
return namespaceDescriptor;
}
private NamespaceDescriptor createJavaNamespaceDescriptor(@NotNull PsiClass psiClass) {
NamespaceDescriptor namespaceDescriptor = new NamespaceDescriptor(
Collections.<Attribute>emptyList(), // TODO
psiClass.getName(),
new JavaClassMembersScope(psiClass, semanticServices, true)
);
semanticServices.getTrace().recordDeclarationResolution(psiClass, namespaceDescriptor);
return namespaceDescriptor;
}
public List<ValueParameterDescriptor> resolveParameterDescriptors(PsiParameter[] parameters) {
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) {
PsiParameter parameter = parameters[i];
result.add(new ValueParameterDescriptorImpl(
i,
Collections.<Attribute>emptyList(), // TODO
parameter.getName(),
semanticServices.getTypeTransformer().transform(parameter.getType()),
false,
parameter.isVarArgs()
));
}
return result;
}
}
@@ -0,0 +1,32 @@
package org.jetbrains.jet.lang.resolve.java;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.JetScopeImpl;
import org.jetbrains.jet.lang.types.ClassDescriptor;
import org.jetbrains.jet.lang.types.NamespaceDescriptor;
/**
* @author abreslav
*/
public class JavaLangScope extends JetScopeImpl {
private final JavaSemanticServices semanticServices;
public JavaLangScope(JavaSemanticServices semanticServices) {
this.semanticServices = semanticServices;
}
@Override
public ClassDescriptor getClass(@NotNull String name) {
return semanticServices.getDescriptorResolver().resolveClass(getQualifiedName(name));
}
@Override
public NamespaceDescriptor getNamespace(@NotNull String name) {
return semanticServices.getDescriptorResolver().resolveNamespace(getQualifiedName(name));
}
private String getQualifiedName(String name) {
return "java.lang." + name;
}
}
@@ -0,0 +1,34 @@
package org.jetbrains.jet.lang.resolve.java;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.types.BindingTrace;
/**
* @author abreslav
*/
public class JavaSemanticServices {
private final JavaTypeTransformer typeTransformer;
private final JavaDescriptorResolver descriptorResolver;
private final BindingTrace trace;
public JavaSemanticServices(Project project, JetSemanticServices jetSemanticServices, BindingTrace trace) {
this.trace = trace;
this.descriptorResolver = new JavaDescriptorResolver(project, this);
this.typeTransformer = new JavaTypeTransformer(jetSemanticServices.getStandardLibrary(), descriptorResolver);
}
@NotNull
public JavaTypeTransformer getTypeTransformer() {
return typeTransformer;
}
public JavaDescriptorResolver getDescriptorResolver() {
return descriptorResolver;
}
public BindingTrace getTrace() {
return trace;
}
}
@@ -0,0 +1,66 @@
package org.jetbrains.jet.lang.resolve.java;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.types.*;
import java.util.HashMap;
import java.util.Map;
/**
* @author abreslav
*/
public class JavaTypeTransformer {
private final JavaDescriptorResolver resolver;
private final JetStandardLibrary standardLibrary;
private Map<String, Type> primitiveTypesMap;
public JavaTypeTransformer(JetStandardLibrary standardLibrary, JavaDescriptorResolver resolver) {
this.resolver = resolver;
this.standardLibrary = standardLibrary;
}
@NotNull
public Type transform(PsiType javaType) {
Type result = javaType.accept(new PsiTypeVisitor<Type>() {
@Override
public Type visitClassType(PsiClassType classType) {
PsiClass psiClass = classType.resolveGenerics().getElement();
ClassDescriptor descriptor = resolver.resolveClass(psiClass);
// TODO : parameters
return new TypeImpl(descriptor);
}
@Override
public Type visitPrimitiveType(PsiPrimitiveType primitiveType) {
String canonicalText = primitiveType.getCanonicalText();
Type type = getPrimitiveTypesMap().get(canonicalText);
assert type != null : canonicalText;
return type;
}
@Override
public Type visitArrayType(PsiArrayType arrayType) {
return JetStandardClasses.getUnitType(); // TODO!!!
}
});
return result;
}
public Map<String, Type> getPrimitiveTypesMap() {
if (primitiveTypesMap == null) {
primitiveTypesMap = new HashMap<String, Type>();
primitiveTypesMap.put("byte", standardLibrary.getByteType());
primitiveTypesMap.put("short", standardLibrary.getShortType());
primitiveTypesMap.put("char", standardLibrary.getCharType());
primitiveTypesMap.put("int", standardLibrary.getIntType());
primitiveTypesMap.put("long", standardLibrary.getLongType());
primitiveTypesMap.put("float", standardLibrary.getFloatType());
primitiveTypesMap.put("double", standardLibrary.getDoubleType());
primitiveTypesMap.put("boolean", standardLibrary.getBooleanType());
primitiveTypesMap.put("void", JetStandardClasses.getUnitType());
}
return primitiveTypesMap;
}
}
@@ -1,10 +1,9 @@
package org.jetbrains.jet.lang.types;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetReferenceExpression;
import org.jetbrains.jet.lang.psi.JetTypeReference;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.JetScope;
/**
* @author abreslav
@@ -19,11 +18,15 @@ public class BindingTrace {
}
public void recordDeclarationResolution(@NotNull JetDeclaration declaration, @NotNull DeclarationDescriptor descriptor) {
public void recordDeclarationResolution(@NotNull PsiElement declaration, @NotNull DeclarationDescriptor descriptor) {
}
public void recordTypeResolution(@NotNull JetTypeReference typeReference, @NotNull Type type) {
}
public void setToplevelScope(JetScope toplevelScope) {
}
}
@@ -1,7 +1,6 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.resolve.JetScope;
import org.jetbrains.jet.lang.resolve.SubstitutingScope;
@@ -13,22 +12,21 @@ import java.util.Map;
/**
* @author abreslav
*/
public class ClassDescriptorImpl extends DeclarationDescriptorImpl<JetClass> implements ClassDescriptor {
public class ClassDescriptorImpl extends DeclarationDescriptorImpl implements ClassDescriptor {
private final TypeConstructor typeConstructor;
private final JetScope memberDeclarations;
public ClassDescriptorImpl(
JetClass psiElement,
List<Attribute> attributes, boolean sealed,
String name, List<TypeParameterDescriptor> typeParameters,
Collection<? extends Type> superclasses, JetScope memberDeclarations) {
super(psiElement, attributes, name);
super(attributes, name);
this.typeConstructor = new TypeConstructor(attributes, sealed, name, typeParameters, superclasses);
this.memberDeclarations = memberDeclarations;
}
public ClassDescriptorImpl(String name, JetScope memberDeclarations) {
this(null, Collections.<Attribute>emptyList(), true,
this(Collections.<Attribute>emptyList(), true,
name, Collections.<TypeParameterDescriptor>emptyList(),
Collections.<Type>singleton(JetStandardClasses.getAnyType()), memberDeclarations);
}
@@ -1,29 +1,21 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.jet.lang.psi.JetElement;
import java.util.List;
/**
* @author abreslav
*/
public abstract class DeclarationDescriptorImpl<T extends JetElement> extends AnnotatedImpl implements Named, DeclarationDescriptor {
public abstract class DeclarationDescriptorImpl extends AnnotatedImpl implements Named, DeclarationDescriptor {
private final String name;
private final T psiElement;
public DeclarationDescriptorImpl(T psiElement, List<Attribute> attributes, String name) {
public DeclarationDescriptorImpl(List<Attribute> attributes, String name) {
super(attributes);
this.name = name;
this.psiElement = psiElement;
}
@Override
public String getName() {
return name;
}
public T getPsiElement() {
return psiElement;
}
}
@@ -9,7 +9,7 @@ import java.util.*;
/**
* @author abreslav
*/
public class FunctionDescriptorImpl extends DeclarationDescriptorImpl<JetFunction> implements FunctionDescriptor {
public class FunctionDescriptorImpl extends DeclarationDescriptorImpl implements FunctionDescriptor {
@NotNull
private final List<TypeParameterDescriptor> typeParameters;
@NotNull
@@ -20,14 +20,13 @@ public class FunctionDescriptorImpl extends DeclarationDescriptorImpl<JetFunctio
private final FunctionDescriptor original;
public FunctionDescriptorImpl(
JetFunction psiElement,
@Nullable FunctionDescriptor original,
@NotNull List<Attribute> attributes,
String name,
@NotNull List<TypeParameterDescriptor> typeParameters,
@NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
@NotNull Type unsubstitutedReturnType) {
super(psiElement, attributes, name);
super(attributes, name);
this.original = original;
this.typeParameters = typeParameters;
this.unsubstitutedValueParameters = unsubstitutedValueParameters;
@@ -1,7 +1,6 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.ResolveUtil;
import java.util.*;
@@ -42,10 +41,12 @@ public class FunctionDescriptorUtil {
public static List<ValueParameterDescriptor> getSubstitutedValueParameters(@NotNull FunctionDescriptor functionDescriptor, @NotNull List<Type> typeArguments) {
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
Map<TypeConstructor, TypeProjection> context = createSubstitutionContext(functionDescriptor, typeArguments);
for (ValueParameterDescriptor unsubstitutedValueParameter : functionDescriptor.getUnsubstitutedValueParameters()) {
List<ValueParameterDescriptor> unsubstitutedValueParameters = functionDescriptor.getUnsubstitutedValueParameters();
for (int i = 0, unsubstitutedValueParametersSize = unsubstitutedValueParameters.size(); i < unsubstitutedValueParametersSize; i++) {
ValueParameterDescriptor unsubstitutedValueParameter = unsubstitutedValueParameters.get(i);
// TODO : Lazy?
result.add(new ValueParameterDescriptorImpl(
ResolveUtil.getJetParameter(unsubstitutedValueParameter),
i,
unsubstitutedValueParameter.getAttributes(),
unsubstitutedValueParameter.getName(),
TypeSubstitutor.INSTANCE.substitute(context, unsubstitutedValueParameter.getType(), Variance.IN_VARIANCE),
@@ -81,7 +82,6 @@ public class FunctionDescriptorUtil {
return functionDescriptor;
}
return new FunctionDescriptorImpl(
ResolveUtil.getJetFunction(functionDescriptor),
functionDescriptor,
// TODO : substitute
functionDescriptor.getAttributes(),
@@ -20,7 +20,6 @@ public class JetStandardClasses {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private static ClassDescriptor NOTHING_CLASS = new ClassDescriptorImpl(
null,
Collections.<Attribute>emptyList(),
true,
"Nothing",
@@ -54,7 +53,6 @@ public class JetStandardClasses {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private static final ClassDescriptor ANY = new ClassDescriptorImpl(
null,
Collections.<Attribute>emptyList(),
false,
"Any",
@@ -80,13 +78,11 @@ public class JetStandardClasses {
List<TypeParameterDescriptor> parameters = new ArrayList<TypeParameterDescriptor>();
for (int j = 0; j < i; j++) {
parameters.add(new TypeParameterDescriptor(
null,
Collections.<Attribute>emptyList(),
Variance.OUT_VARIANCE, "T" + j,
Collections.singleton(getNullableAnyType())));
}
TUPLE[i] = new ClassDescriptorImpl(
null,
Collections.<Attribute>emptyList(),
true,
"Tuple" + i,
@@ -107,30 +103,25 @@ public class JetStandardClasses {
List<TypeParameterDescriptor> parameters = new ArrayList<TypeParameterDescriptor>();
for (int j = 0; j < i; j++) {
parameters.add(new TypeParameterDescriptor(
null,
Collections.<Attribute>emptyList(),
Variance.IN_VARIANCE, "P" + j,
Collections.singleton(getNullableAnyType())));
}
parameters.add(new TypeParameterDescriptor(
null,
Collections.<Attribute>emptyList(),
Collections.<Attribute>emptyList(),
Variance.OUT_VARIANCE, "R",
Collections.singleton(getNullableAnyType())));
FUNCTION[i] = new ClassDescriptorImpl(
null,
Collections.<Attribute>emptyList(),
false,
"Function" + i,
parameters,
Collections.singleton(getAnyType()), STUB);
parameters.add(0, new TypeParameterDescriptor(
null,
Collections.<Attribute>emptyList(),
Collections.<Attribute>emptyList(),
Variance.IN_VARIANCE, "T",
Collections.singleton(getNullableAnyType())));
RECEIVER_FUNCTION[i] = new ClassDescriptorImpl(
null,
Collections.<Attribute>emptyList(),
false,
"ReceiverFunction" + i,
@@ -9,6 +9,7 @@ import org.jetbrains.jet.lang.JetFileType;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.lang.resolve.JetScope;
import org.jetbrains.jet.lang.resolve.TopDownAnalyzer;
@@ -52,8 +53,11 @@ public class JetStandardLibrary {
JetFileType.INSTANCE, FileUtil.loadTextAndClose(new InputStreamReader(stream)));
JetSemanticServices bootstrappingSemanticServices = JetSemanticServices.createSemanticServices(this, ErrorHandler.DO_NOTHING);
TopDownAnalyzer bootstrappingTDA = new TopDownAnalyzer(bootstrappingSemanticServices);
BindingContext bindingContext = bootstrappingTDA.process(JetStandardClasses.STANDARD_CLASSES, file.getRootNamespace().getDeclarations());
BindingTraceContext bindingTraceContext = new BindingTraceContext();
TopDownAnalyzer bootstrappingTDA = new TopDownAnalyzer(bootstrappingSemanticServices, bindingTraceContext);
bootstrappingTDA.process(JetStandardClasses.STANDARD_CLASSES, file.getRootNamespace().getDeclarations());
BindingContext bindingContext = bindingTraceContext;
this.libraryScope = bindingContext.getTopLevelScope();
this.byteClass = libraryScope.getClass("Byte");
@@ -49,13 +49,21 @@ public class JetTypeInferrer {
public void visitReferenceExpression(JetReferenceExpression expression) {
// TODO : other members
// TODO : type substitutions???
PropertyDescriptor property = scope.getProperty(expression.getReferencedName());
String referencedName = expression.getReferencedName();
PropertyDescriptor property = scope.getProperty(referencedName);
if (property != null) {
trace.recordReferenceResolution(expression, property);
result[0] = property.getType();
return;
} else {
semanticServices.getErrorHandler().unresolvedReference(expression);
NamespaceDescriptor namespace = scope.getNamespace(referencedName);
if (namespace != null) {
trace.recordReferenceResolution(expression, namespace);
result[0] = namespace.getNamespaceType();
return;
}
}
semanticServices.getErrorHandler().unresolvedReference(expression);
}
@Override
@@ -275,8 +283,10 @@ public class JetTypeInferrer {
JetExpression receiverExpression = expression.getReceiverExpression();
JetExpression selectorExpression = expression.getSelectorExpression();
Type receiverType = getType(scope, receiverExpression, false);
JetScope compositeScope = new ScopeWithReceiver(scope, receiverType);
result[0] = getType(compositeScope, selectorExpression, false);
if (receiverType != null) { // TODO : review
JetScope compositeScope = new ScopeWithReceiver(scope, receiverType);
result[0] = getType(compositeScope, selectorExpression, false);
}
}
@Override
@@ -439,7 +449,7 @@ public class JetTypeInferrer {
return getType(scope, expression, true);
}
// TODO: functions, classes, etc.
throw new IllegalArgumentException("Last item in the block must be an expression");
throw new IllegalArgumentException("Last item in the block must be an expression, but was " + lastElement.getClass().getCanonicalName());
}
}
@@ -1,7 +1,6 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.ResolveUtil;
import java.util.ArrayList;
import java.util.List;
@@ -26,7 +25,6 @@ public class LazySubstitutingFunctionDescriptor implements FunctionDescriptor {
for (TypeParameterDescriptor parameterDescriptor : functionDescriptor.getTypeParameters()) {
// TODO : lazy?
result.add(new TypeParameterDescriptor(
ResolveUtil.getJetTypeParameter(parameterDescriptor),
parameterDescriptor.getAttributes(),
parameterDescriptor.getVariance(),
parameterDescriptor.getName(),
@@ -39,9 +37,11 @@ public class LazySubstitutingFunctionDescriptor implements FunctionDescriptor {
@Override
public List<ValueParameterDescriptor> getUnsubstitutedValueParameters() {
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
for (ValueParameterDescriptor parameterDescriptor : functionDescriptor.getUnsubstitutedValueParameters()) {
List<ValueParameterDescriptor> unsubstitutedValueParameters = functionDescriptor.getUnsubstitutedValueParameters();
for (int i = 0, unsubstitutedValueParametersSize = unsubstitutedValueParameters.size(); i < unsubstitutedValueParametersSize; i++) {
ValueParameterDescriptor parameterDescriptor = unsubstitutedValueParameters.get(i);
result.add(new ValueParameterDescriptorImpl(
ResolveUtil.getJetParameter(parameterDescriptor),
i,
parameterDescriptor.getAttributes(),
parameterDescriptor.getName(),
TypeSubstitutor.INSTANCE.substitute(substitutionContext, parameterDescriptor.getType(), Variance.IN_VARIANCE),
@@ -1,31 +1,29 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.jet.lang.modules.NamespaceDomain;
import org.jetbrains.jet.lang.resolve.JetScope;
import java.util.Collection;
import java.util.List;
/**
* @author abreslav
*/
public class NamespaceDescriptor implements NamespaceDomain {
public ClassDescriptor getClass(String name) {
throw new UnsupportedOperationException(); // TODO
public class NamespaceDescriptor extends DeclarationDescriptorImpl {
private NamespaceType namespaceType;
private final JetScope memberScope;
public NamespaceDescriptor(List<Attribute> attributes, String name, JetScope memberScope) {
super(attributes, name);
this.memberScope = memberScope;
}
public Collection<FunctionDescriptor> getMethods(String name) {
throw new UnsupportedOperationException(); // TODO
public JetScope getMemberScope() {
return memberScope;
}
public PropertyDescriptor getProperty(String name) {
throw new UnsupportedOperationException(); // TODO
}
public ExtensionDescriptor getExtension(String name) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public NamespaceDescriptor getNamespace(String namespaceName) {
throw new UnsupportedOperationException(); // TODO
public NamespaceType getNamespaceType() {
if (namespaceType == null) {
namespaceType = new NamespaceType(getName(), memberScope);
}
return namespaceType;
}
}
@@ -0,0 +1,57 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.JetScope;
import java.util.List;
/**
* This is a fake type assigned to namespace expressions. Only member lookup is
* supposed to be done on these types.
*
* @author abreslav
*/
public class NamespaceType implements Type {
private final String name;
private final JetScope memberScope;
public NamespaceType(String name, JetScope memberScope) {
this.name = name;
this.memberScope = memberScope;
}
@NotNull
@Override
public JetScope getMemberScope() {
return memberScope;
}
@NotNull
@Override
public TypeConstructor getConstructor() {
return throwException();
}
private TypeConstructor throwException() {
throw new UnsupportedOperationException("Only member lookup is allowed on a namespace type " + name);
}
@NotNull
@Override
public List<TypeProjection> getArguments() {
throwException();
return null;
}
@Override
public boolean isNullable() {
throwException();
return false;
}
@Override
public List<Attribute> getAttributes() {
throwException();
return null;
}
}
@@ -1,18 +1,15 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetProperty;
import java.util.List;
/**
* @author abreslav
*/
public class PropertyDescriptorImpl extends DeclarationDescriptorImpl<JetDeclaration> implements PropertyDescriptor {
public class PropertyDescriptorImpl extends DeclarationDescriptorImpl implements PropertyDescriptor {
private Type type;
public PropertyDescriptorImpl(JetDeclaration psiElement, List<Attribute> attributes, String name, Type type) {
super(psiElement, attributes, name);
public PropertyDescriptorImpl(List<Attribute> attributes, String name, Type type) {
super(attributes, name);
this.type = type;
}
@@ -37,4 +37,5 @@ public class TypeConstructor extends AnnotatedImpl {
public boolean isSealed() {
return sealed;
}
}
@@ -29,7 +29,7 @@ public final class TypeImpl extends AnnotatedImpl implements Type {
this(Collections.<Attribute>emptyList(), constructor, false, Collections.<TypeProjection>emptyList(), memberScope);
}
public TypeImpl(ClassDescriptor classDescriptor) {
public TypeImpl(@NotNull ClassDescriptor classDescriptor) {
this(Collections.<Attribute>emptyList(),
classDescriptor.getTypeConstructor(),
false,
@@ -1,7 +1,5 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.jet.lang.psi.JetTypeParameter;
import java.util.Collections;
import java.util.List;
import java.util.Set;
@@ -9,13 +7,13 @@ import java.util.Set;
/**
* @author abreslav
*/
public class TypeParameterDescriptor extends DeclarationDescriptorImpl<JetTypeParameter> {
public class TypeParameterDescriptor extends DeclarationDescriptorImpl {
private final Variance variance;
private final Set<Type> upperBounds;
private final TypeConstructor typeConstructor;
public TypeParameterDescriptor(JetTypeParameter psiElement, List<Attribute> attributes, Variance variance, String name, Set<Type> upperBounds) {
super(psiElement, attributes, name);
public TypeParameterDescriptor(List<Attribute> attributes, Variance variance, String name, Set<Type> upperBounds) {
super(attributes, name);
this.variance = variance;
this.upperBounds = upperBounds;
// TODO: Should we actually pass the attributes on to the type constructor?
@@ -28,7 +26,7 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl<JetTypePa
}
public TypeParameterDescriptor(List<Attribute> attributes, Variance variance, String name) {
this(null, attributes, variance, name, Collections.singleton(JetStandardClasses.getNullableAnyType()));
this(attributes, variance, name, Collections.singleton(JetStandardClasses.getNullableAnyType()));
}
public Variance getVariance() {
@@ -2,7 +2,6 @@ package org.jetbrains.jet.lang.types;
import com.intellij.psi.PsiElement;
import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetParameter;
import org.jetbrains.jet.lang.psi.JetParameterList;
import java.util.List;
@@ -13,21 +12,24 @@ import java.util.List;
public class ValueParameterDescriptorImpl extends PropertyDescriptorImpl implements ValueParameterDescriptor {
private final boolean hasDefaultValue;
private final boolean isVararg;
private final int index;
public ValueParameterDescriptorImpl(JetParameter psiElement, List<Attribute> attributes, String name, Type type, boolean hasDefaultValue, boolean isVararg) {
super(psiElement, attributes, name, type);
public ValueParameterDescriptorImpl(int index, List<Attribute> attributes, String name, Type type, boolean hasDefaultValue, boolean isVararg) {
super(attributes, name, type);
this.index = index;
this.hasDefaultValue = hasDefaultValue;
this.isVararg = isVararg;
}
@Override
public int getIndex() {
final JetDeclaration element = getPsiElement();
final PsiElement parent = element.getParent();
if (parent instanceof JetParameterList) {
return ((JetParameterList) parent).getParameters().indexOf(element);
}
throw new IllegalStateException("couldn't find index for parameter");
return index;
// final JetDeclaration element = getPsiElement();
// final PsiElement parent = element.getParent();
// if (parent instanceof JetParameterList) {
// return ((JetParameterList) parent).getParameters().indexOf(element);
// }
// throw new IllegalStateException("couldn't find index for parameter");
}
@Override
@@ -62,7 +62,7 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
StringWriter writer = new StringWriter();
JetFile jetFile = (JetFile) myFixture.getFile();
JetNamespace namespace = jetFile.getRootNamespace();
codegen.generate(namespace, new TraceClassVisitor(new PrintWriter(writer)), JetSemanticServices.createSemanticServices(getProject(), ErrorHandler.THROW_EXCEPTION));
codegen.generate(namespace, new TraceClassVisitor(new PrintWriter(writer)), getProject());
return writer.toString();
}
@@ -70,7 +70,7 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
JetFile jetFile = (JetFile) myFixture.getFile();
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
final JetNamespace namespace = jetFile.getRootNamespace();
codegen.generate(namespace, writer, JetSemanticServices.createSemanticServices(getProject(), ErrorHandler.THROW_EXCEPTION));
codegen.generate(namespace, writer, getProject());
final byte[] data = writer.toByteArray();
MyClassLoader classLoader = new MyClassLoader(NamespaceGenTest.class.getClassLoader());
final Class aClass = classLoader.doDefineClass(NamespaceCodegen.getJVMClassName(namespace).replace("/", "."), data);
@@ -7,6 +7,7 @@ import org.jetbrains.jet.lang.ErrorHandler;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.lang.resolve.TopDownAnalyzer;
import org.jetbrains.jet.lang.types.*;
@@ -76,8 +77,10 @@ public class ExpectedResolveData {
Map<String, DeclarationDescriptor> nameToDescriptor = new HashMap<String, DeclarationDescriptor>();
nameToDescriptor.put("std::Int.plus(Int)", standardFunction(lib.getInt(), "plus", lib.getIntType()));
TopDownAnalyzer topDownAnalyzer = new TopDownAnalyzer(semanticServices);
BindingContext bindingContext = topDownAnalyzer.process(lib.getLibraryScope(), file.getRootNamespace().getDeclarations());
BindingTraceContext bindingTraceContext = new BindingTraceContext();
TopDownAnalyzer topDownAnalyzer = new TopDownAnalyzer(semanticServices, bindingTraceContext);
topDownAnalyzer.process(lib.getLibraryScope(), file.getRootNamespace().getDeclarations());
BindingContext bindingContext = bindingTraceContext;
Map<String, JetDeclaration> nameToDeclaration = new HashMap<String, JetDeclaration>();