'this' is resolved to the corresponding declaration in labeled function literals

This commit is contained in:
Andrey Breslav
2011-05-30 16:02:51 +04:00
parent 2052683de3
commit a676c960cf
10 changed files with 105 additions and 29 deletions
@@ -72,10 +72,12 @@ public class JetControlFlowProcessor {
}
@Nullable
private JetElement resolveLabel(@NotNull String labelName, @NotNull JetSimpleNameExpression labelExpression) {
private JetElement resolveLabel(@NotNull String labelName, @NotNull JetSimpleNameExpression labelExpression, boolean reportUnresolved) {
Stack<JetElement> stack = labeledElements.get(labelName);
if (stack == null || stack.isEmpty()) {
trace.getErrorHandler().unresolvedReference(labelExpression);
if (reportUnresolved) {
trace.getErrorHandler().unresolvedReference(labelExpression);
}
return null;
}
else if (stack.size() > 1) {
@@ -119,6 +121,12 @@ public class JetControlFlowProcessor {
@Override
public void visitThisExpression(JetThisExpression expression) {
JetSimpleNameExpression targetLabel = expression.getTargetLabel();
if (targetLabel != null) {
String labelName = expression.getLabelName();
assert labelName != null;
resolveLabel(labelName, targetLabel, false);
}
builder.read(expression);
}
@@ -416,7 +424,7 @@ public class JetControlFlowProcessor {
if (labelName != null) {
JetSimpleNameExpression targetLabel = expression.getTargetLabel();
assert targetLabel != null;
loop = resolveLabel(labelName, targetLabel);
loop = resolveLabel(labelName, targetLabel, true);
if (!isLoop(loop)) {
trace.getErrorHandler().genericError(expression.getNode(), "The label '" + targetLabel.getText() + "' does not denote a loop");
loop = null;
@@ -448,7 +456,7 @@ public class JetControlFlowProcessor {
if (labelElement != null) {
String labelName = expression.getLabelName();
assert labelName != null;
subroutine = resolveLabel(labelName, labelElement);
subroutine = resolveLabel(labelName, labelElement, true);
}
else {
subroutine = builder.getCurrentSubroutine();
@@ -27,7 +27,7 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
@Override
@Deprecated
public FunctionDescriptor initialize(@Nullable JetType receiverType, @NotNull List<TypeParameterDescriptor> typeParameters, @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters, @NotNull JetType unsubstitutedReturnType) {
public FunctionDescriptorImpl initialize(@Nullable JetType receiverType, @NotNull List<TypeParameterDescriptor> typeParameters, @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters, @Nullable JetType unsubstitutedReturnType) {
assert receiverType == null;
return super.initialize(null, typeParameters, unsubstitutedValueParameters, unsubstitutedReturnType);
}
@@ -40,7 +40,7 @@ public class FunctionDescriptorImpl extends DeclarationDescriptorImpl implements
this.original = original;
}
public FunctionDescriptor initialize(
public FunctionDescriptorImpl initialize(
@Nullable JetType receiverType,
@NotNull List<TypeParameterDescriptor> typeParameters,
@NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
@@ -52,6 +52,10 @@ public class FunctionDescriptorImpl extends DeclarationDescriptorImpl implements
return this;
}
public void setReturnType(JetType unsubstitutedReturnType) {
this.unsubstitutedReturnType = unsubstitutedReturnType;
}
@Override
public JetType getReceiverType() {
return receiverType;
@@ -248,7 +248,7 @@ public class ClassDescriptorResolver {
}
@NotNull
private MutableValueParameterDescriptor resolveValueParameterDescriptor(DeclarationDescriptor declarationDescriptor, JetParameter valueParameter, int index, JetType type) {
public MutableValueParameterDescriptor resolveValueParameterDescriptor(DeclarationDescriptor declarationDescriptor, JetParameter valueParameter, int index, JetType type) {
MutableValueParameterDescriptor valueParameterDescriptor = new ValueParameterDescriptorImpl(
declarationDescriptor,
index,
@@ -343,7 +343,7 @@ public class ClassDescriptorResolver {
@NotNull
public VariableDescriptor resolveLocalVariableDescriptor(DeclarationDescriptor containingDeclaration, WritableScope scope, JetProperty property) {
JetType type = getType(scope, property, false); // For a local variable the type must not be deferred
JetType type = getVariableType(scope, property, false); // For a local variable the type must not be deferred
VariableDescriptorImpl variableDescriptor = new LocalVariableDescriptor(
containingDeclaration,
@@ -380,7 +380,7 @@ public class ClassDescriptorResolver {
JetModifierList modifierList = property.getModifierList();
boolean isVar = property.isVar();
JetType type = getType(scopeWithTypeParameters, property, true);
JetType type = getVariableType(scopeWithTypeParameters, property, true);
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
containingDeclaration,
@@ -402,7 +402,7 @@ public class ClassDescriptorResolver {
}
@NotNull
private JetType getType(@NotNull final JetScope scope, @NotNull JetProperty property, boolean allowDeferred) {
private JetType getVariableType(@NotNull final JetScope scope, @NotNull JetProperty property, boolean allowDeferred) {
// TODO : receiver?
JetTypeReference propertyTypeRef = property.getPropertyTypeRef();
@@ -3,7 +3,7 @@ package org.jetbrains.jet.lang.resolve;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.lang.types.JetType;
import java.util.Collection;
@@ -1,6 +1,7 @@
package org.jetbrains.jet.lang.types;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.intellij.lang.ASTNode;
@@ -735,10 +736,6 @@ public class JetTypeInferrer {
return;
}
FunctionDescriptorImpl functionDescriptor = new FunctionDescriptorImpl(scope.getContainingDeclaration(), Collections.<Annotation>emptyList(), "<anonymous>");
JetTypeReference returnTypeRef = expression.getReturnTypeRef();
JetTypeReference receiverTypeRef = expression.getReceiverTypeRef();
final JetType receiverType;
if (receiverTypeRef != null) {
@@ -747,31 +744,44 @@ public class JetTypeInferrer {
receiverType = scope.getThisType();
}
List<JetElement> body = expression.getBody();
final Map<String, VariableDescriptor> parameterDescriptors = new HashMap<String, VariableDescriptor>();
FunctionDescriptorImpl functionDescriptor = new FunctionDescriptorImpl(
scope.getContainingDeclaration(), Collections.<Annotation>emptyList(), "<anonymous>");
List<JetType> parameterTypes = new ArrayList<JetType>();
for (JetParameter parameter : expression.getParameters()) {
List<ValueParameterDescriptor> valueParameterDescriptors = Lists.newArrayList();
List<JetParameter> parameters = expression.getParameters();
for (int i = 0, parametersSize = parameters.size(); i < parametersSize; i++) {
JetParameter parameter = parameters.get(i);
JetTypeReference typeReference = parameter.getTypeReference();
if (typeReference == null) {
throw new UnsupportedOperationException("Type inference for parameters is not implemented yet");
}
VariableDescriptor variableDescriptor = classDescriptorResolver.resolveLocalVariableDescriptor(functionDescriptor, scope, parameter);
parameterDescriptors.put(parameter.getName(), variableDescriptor);
parameterTypes.add(variableDescriptor.getOutType());
JetType type = typeResolver.resolveType(scope, typeReference);
ValueParameterDescriptor valueParameterDescriptor = classDescriptorResolver.resolveValueParameterDescriptor(functionDescriptor, parameter, i, type);
parameterTypes.add(valueParameterDescriptor.getOutType());
valueParameterDescriptors.add(valueParameterDescriptor);
}
JetType effectiveReceiverType = receiverTypeRef == null ? null : receiverType;
functionDescriptor.initialize(effectiveReceiverType, Collections.<TypeParameterDescriptor>emptyList(), valueParameterDescriptors, null);
trace.recordDeclarationResolution(expression, functionDescriptor);
JetTypeReference returnTypeRef = expression.getReturnTypeRef();
JetType returnType;
if (returnTypeRef != null) {
returnType = typeResolver.resolveType(scope, returnTypeRef);
} else {
WritableScope writableScope = new WritableScopeImpl(scope, functionDescriptor, trace.getErrorHandler());
for (VariableDescriptor variableDescriptor : parameterDescriptors.values()) {
for (VariableDescriptor variableDescriptor : valueParameterDescriptors) {
writableScope.addVariableDescriptor(variableDescriptor);
}
writableScope.setThisType(receiverType);
returnType = getBlockReturnedType(writableScope, body);
returnType = getBlockReturnedType(writableScope, expression.getBody());
}
JetType effectiveReceiverType = receiverTypeRef == null ? null : receiverType;
JetType safeReturnType = returnType == null ? ErrorUtils.createErrorType("<return type>") : returnType;
functionDescriptor.setReturnType(safeReturnType);
result = JetStandardClasses.getFunctionType(Collections.<Annotation>emptyList(), effectiveReceiverType, parameterTypes, safeReturnType);
}
@@ -928,7 +938,6 @@ public class JetTypeInferrer {
@Override
public void visitThisExpression(JetThisExpression expression) {
// TODO : qualified this, e.g. this@Foo<Bar>
JetType thisType = null;
String labelName = expression.getLabelName();
if (labelName != null) {
@@ -941,15 +950,39 @@ public class JetTypeInferrer {
if (declarationDescriptor instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) declarationDescriptor;
thisType = classDescriptor.getDefaultType();
trace.recordReferenceResolution(targetLabel, classDescriptor);
trace.recordReferenceResolution(expression.getThisReference(), classDescriptor);
}
else if (declarationDescriptor instanceof FunctionDescriptor) {
FunctionDescriptor functionDescriptor = (FunctionDescriptor) declarationDescriptor;
thisType = functionDescriptor.getReceiverType();
}
else {
throw new UnsupportedOperationException(); // TODO
}
trace.recordReferenceResolution(targetLabel, declarationDescriptor);
trace.recordReferenceResolution(expression.getThisReference(), declarationDescriptor);
}
else if (size == 0) {
trace.getErrorHandler().unresolvedReference(targetLabel);
// This uses the info written by the control flow processor
PsiElement psiElement = trace.getBindingContext().resolveToDeclarationPsiElement(targetLabel);
if (psiElement instanceof JetFunctionLiteralExpression) {
DeclarationDescriptor declarationDescriptor = trace.getBindingContext().getDeclarationDescriptor(psiElement);
if (declarationDescriptor instanceof FunctionDescriptor) {
thisType = ((FunctionDescriptor) declarationDescriptor).getReceiverType();
if (thisType == null) {
thisType = JetStandardClasses.getNothingType();
}
else {
trace.recordReferenceResolution(targetLabel, declarationDescriptor);
trace.recordReferenceResolution(expression.getThisReference(), declarationDescriptor);
}
}
else {
trace.getErrorHandler().unresolvedReference(targetLabel);
}
}
else {
trace.getErrorHandler().unresolvedReference(targetLabel);
}
}
else {
trace.getErrorHandler().genericError(targetLabel.getNode(), "Ambiguous label");
@@ -34,6 +34,13 @@ public class JetQuickDocumentationProvider implements DocumentationProvider {
if (declarationDescriptor != null) {
return render(declarationDescriptor);
}
PsiElement psiElement = bindingContext.resolveToDeclarationPsiElement(ref);
if (psiElement != null) {
declarationDescriptor = bindingContext.getDeclarationDescriptor(psiElement);
if (declarationDescriptor != null) {
return render(declarationDescriptor);
}
}
return "Unresolved";
}
+19
View File
@@ -20,3 +20,22 @@ fun foo() : Unit {
<error>this</error>
this<error>@a</error>
}
namespace closures {
class A(val a:Int) {
class B() {
val x = this@B : B
val y = this@A : A
val z = this : B
val Int.xx = this : Int
fun Char.xx() {
this : Char
val a = {Double.() => this : Double + this@xx : Char}
val b = @a{Double.() => this@a : Double + this@xx : Char}
val c = @a{() => <error>this@a</error> + this@xx : Char}
return (@a{Double.() => this@a : Double + this@xx : Char})
}
}
}
}
+2
View File
@@ -8,6 +8,8 @@ namespace qualified_this {
~xx~val Int.xx = `xx`this : Int
~xx()~fun Int.xx() {
`xx()`this : Int
val a = {Int.() => `xx()`this`xx()`@xx + this}
}
}
}
@@ -12,7 +12,10 @@ import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.JetChangeUtil;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.resolve.*;
import org.jetbrains.jet.lang.resolve.ClassDescriptorResolver;
import org.jetbrains.jet.lang.resolve.JetScope;
import org.jetbrains.jet.lang.resolve.JetScopeAdapter;
import org.jetbrains.jet.lang.resolve.TypeResolver;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.parsing.JetParsingTest;