Support for local objects

This commit is contained in:
Andrey Breslav
2011-06-03 16:02:26 +04:00
parent 36470388c4
commit f0b8ef6946
11 changed files with 132 additions and 30 deletions
@@ -595,22 +595,6 @@ public class JetControlFlowProcessor {
builder.read(expression);
}
@Override
public void visitObjectLiteralExpression(JetObjectLiteralExpression expression) {
List<JetDelegationSpecifier> delegationSpecifiers = expression.getObjectDeclaration().getDelegationSpecifiers();
for (JetDelegationSpecifier delegationSpecifier : delegationSpecifiers) {
if (delegationSpecifier instanceof JetDelegatorByExpressionSpecifier) {
JetDelegatorByExpressionSpecifier specifier = (JetDelegatorByExpressionSpecifier) delegationSpecifier;
JetExpression delegateExpression = specifier.getDelegateExpression();
if (delegateExpression != null) {
value(delegateExpression, false, false);
}
}
}
builder.read(expression);
}
@Override
public void visitIsExpression(JetIsExpression expression) {
value(expression.getLeftHandSide(), false, inCondition);
@@ -720,6 +704,32 @@ public class JetControlFlowProcessor {
builder.bindLabel(doneLabel);
}
@Override
public void visitObjectLiteralExpression(JetObjectLiteralExpression expression) {
// List<JetDelegationSpecifier> delegationSpecifiers = expression.getObjectDeclaration().getDelegationSpecifiers();
// for (JetDelegationSpecifier delegationSpecifier : delegationSpecifiers) {
// if (delegationSpecifier instanceof JetDelegatorByExpressionSpecifier) {
// JetDelegatorByExpressionSpecifier specifier = (JetDelegatorByExpressionSpecifier) delegationSpecifier;
// JetExpression delegateExpression = specifier.getDelegateExpression();
// if (delegateExpression != null) {
// value(delegateExpression, false, false);
// }
// }
// }
value(expression.getObjectDeclaration(), false, inCondition);
builder.read(expression);
}
@Override
public void visitObjectDeclaration(JetObjectDeclaration declaration) {
for (JetDelegationSpecifier delegationSpecifier : declaration.getDelegationSpecifiers()) {
value(delegationSpecifier, false, inCondition);
}
for (JetDeclaration jetDeclaration : declaration.getDeclarations()) {
FOR_LOCAL_CLASSES.value(jetDeclaration, false, false);
}
}
@Override
public void visitTypeProjection(JetTypeProjection typeProjection) {
// TODO : Support Type Arguments. Class object may be initialized at this point");
@@ -730,4 +740,11 @@ public class JetControlFlowProcessor {
builder.unsupported(element);
}
}
private final CFPVisitor FOR_LOCAL_CLASSES = new CFPVisitor(false, false) {
@Override
public void visitFunction(JetFunction function) {
// Nothing
}
};
}
@@ -44,6 +44,6 @@ public abstract class DeclarationDescriptorImpl extends AnnotatedImpl implements
@Override
public String toString() {
return DescriptorRenderer.TEXT.render(this) + "[" + getClass().getCanonicalName() + "@" + System.identityHashCode(this) + "]";
return DescriptorRenderer.TEXT_FOR_DEBUG.render(this) + "[" + getClass().getCanonicalName() + "@" + System.identityHashCode(this) + "]";
}
}
@@ -25,6 +25,7 @@ public interface BindingContext {
VariableDescriptor getVariableDescriptor(JetParameter declaration);
PropertyDescriptor getPropertyDescriptor(JetParameter primaryConstructorParameter);
PropertyDescriptor getPropertyDescriptor(JetObjectDeclarationName objectDeclarationName);
JetType getExpressionType(JetExpression expression);
@@ -194,6 +194,11 @@ public class BindingTraceContext implements BindingContext, BindingTrace {
return primaryConstructorParameterDeclarationsToPropertyDescriptors.get(primaryConstructorParameter);
}
@Override
public PropertyDescriptor getPropertyDescriptor(JetObjectDeclarationName objectDeclarationName) {
return (PropertyDescriptor) declarationsToDescriptors.get(objectDeclarationName);
}
@Nullable
@Override
public ConstructorDescriptor getConstructorDescriptor(@NotNull JetElement declaration) {
@@ -16,6 +16,10 @@ public class DeferredType implements JetType {
this.lazyValue = lazyValue;
}
public boolean isComputed() {
return lazyValue.isComputed();
}
public JetType getActualType() {
return lazyValue.get();
}
@@ -1870,6 +1870,17 @@ public class JetTypeInferrer {
this.scope = scope;
}
@Override
public void visitObjectDeclaration(JetObjectDeclaration declaration) {
TopDownAnalyzer topDownAnalyzer = new TopDownAnalyzer(semanticServices, trace);
topDownAnalyzer.processObject(scope, scope.getContainingDeclaration(), declaration);
ClassDescriptor classDescriptor = trace.getBindingContext().getClassDescriptor(declaration);
if (classDescriptor != null) {
PropertyDescriptor propertyDescriptor = classDescriptorResolver.resolveObjectDeclarationAsPropertyDescriptor(scope.getContainingDeclaration(), declaration, classDescriptor);
scope.addVariableDescriptor(propertyDescriptor);
}
}
@Override
public void visitProperty(JetProperty property) {
@@ -1,7 +1,5 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
/**
* @author abreslav
*/
@@ -19,8 +17,8 @@ public abstract class LazyValue<T> {
protected abstract T compute();
public State getState() {
return state;
public boolean isComputed() {
return state == State.ERROR || state == State.COMPUTED;
}
public final T get() {
@@ -26,6 +26,16 @@ public class DescriptorRenderer {
}
public static final DescriptorRenderer TEXT = new DescriptorRenderer();
public static final DescriptorRenderer TEXT_FOR_DEBUG = new DescriptorRenderer() {
@Override
public String renderType(JetType type) {
if (type instanceof DeferredType) {
DeferredType deferredType = (DeferredType) type;
if (!deferredType.isComputed()) return "<Deferred>";
}
return "" + type;
}
};
public static final DescriptorRenderer HTML = new DescriptorRenderer() {
@Override
@@ -68,6 +78,10 @@ public class DescriptorRenderer {
return keyword;
}
public String renderType(JetType type) {
return "" + type;
}
protected String escape(String s) {
return s;
}
@@ -139,25 +153,25 @@ public class DescriptorRenderer {
if (inType != null && outType != null) {
builder.append(renderKeyword("var")).append(" ");
if (inType.equals(outType)) {
typeString = outType.toString();
typeString = renderType(outType);
}
else {
typeString = "<" + renderKeyword("in") + ": " + inType + " " + renderKeyword("out") + ": " + outType + ">";
typeString = "<" + renderKeyword("in") + ": " + renderType(inType) + " " + renderKeyword("out") + ": " + renderType(outType) + ">";
}
}
else if (outType != null) {
builder.append(renderKeyword("val")).append(" ");
typeString = outType.toString();
typeString = renderType(outType);
}
else if (inType != null) {
builder.append(lt()).append("write-only> ");
typeString = inType.toString();
typeString = renderType(inType);
}
renderTypeParameters(typeParameters, builder);
if (receiverType != null) {
builder.append(escape(receiverType.toString())).append(".");
builder.append(escape(renderType(receiverType))).append(".");
}
return typeString;
@@ -183,7 +197,7 @@ public class DescriptorRenderer {
JetType receiverType = descriptor.getReceiverType();
if (receiverType != null) {
builder.append(escape(receiverType.toString())).append(".");
builder.append(escape(renderType(receiverType))).append(".");
}
renderName(descriptor, builder);
@@ -195,7 +209,7 @@ public class DescriptorRenderer {
builder.append(", ");
}
}
builder.append(") : ").append(escape(descriptor.getUnsubstitutedReturnType().toString()));
builder.append(") : ").append(escape(renderType(descriptor.getUnsubstitutedReturnType())));
return super.visitFunctionDescriptor(descriptor, builder);
}
@@ -244,7 +258,7 @@ public class DescriptorRenderer {
builder.append(" : ");
for (Iterator<? extends JetType> iterator = supertypes.iterator(); iterator.hasNext(); ) {
JetType supertype = iterator.next();
builder.append(supertype);
builder.append(renderType(supertype));
if (iterator.hasNext()) {
builder.append(", ");
}
@@ -261,7 +275,7 @@ public class DescriptorRenderer {
if (!descriptor.getUpperBounds().isEmpty()) {
JetType bound = descriptor.getUpperBounds().iterator().next();
if (bound != JetStandardClasses.getAnyType()) {
builder.append(" : ").append(bound);
builder.append(" : ").append(renderType(bound));
if (descriptor.getUpperBounds().size() > 1) {
builder.append(" (...)");
}
+24
View File
@@ -57,3 +57,27 @@ namespace nestedObejcts {
val d = A.B.A
val e = B.<error>A</error>.B
}
namespace localObjects {
object A {
val x : Int
}
class Foo {
fun foo() : Int
}
fun test() {
A.x
val b = object : Foo {
}
b.foo()
object B {
fun foo() {}
}
B.foo()
}
val bb = <error>B</error>.foo()
}
+23
View File
@@ -0,0 +1,23 @@
namespace localObjects {
object ~A~A {
~x~val x : Int
}
class Foo {
~foo()~fun foo() : Int
}
fun test() {
`A`A.`x`x
val b = object : Foo {
}
b.`foo()`foo()
object ~B~B {
~B.foo()~fun foo() {}
}
`B`B.`B.foo()`foo()
}
val bb = `!`B.foo()
}
@@ -128,6 +128,11 @@ public class JetTestUtils {
throw new UnsupportedOperationException(); // TODO
}
@Override
public PropertyDescriptor getPropertyDescriptor(JetObjectDeclarationName objectDeclarationName) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public JetType getExpressionType(JetExpression expression) {
throw new UnsupportedOperationException(); // TODO