Merge remote branch 'origin/master'

This commit is contained in:
Andrey Breslav
2011-12-02 14:39:39 +03:00
13 changed files with 90 additions and 79 deletions
@@ -10,11 +10,7 @@ import org.jetbrains.jet.lang.resolve.AbstractScopeAdapter;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.TraceBasedRedeclarationHandler;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler;
import org.jetbrains.jet.lang.resolve.scopes.SubstitutingScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
import org.jetbrains.jet.lang.resolve.scopes.*;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ClassReceiver;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.*;
@@ -37,15 +33,11 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme
private Modality modality;
private Visibility visibility;
private TypeConstructor typeConstructor;
private final RedeclarationHandler redeclarationHandler;
private final WritableScope scopeForMemberResolution;
private final WritableScope scopeForMemberLookup;
// This scope contains type parameters but does not contain inner classes
private final WritableScope scopeForSupertypeResolution;
private final WritableScope scopeForAnyConstructor;
private final Map<ConstructorDescriptor, WritableScope> scopesForConstructors;
// for primary constructor and property initializers
private final WritableScope scopeForPrimaryConstructor;
private final WritableScope scopeForInitializers; //contains members + primary constructor value parameters + map for backing fields
private MutableClassDescriptor classObjectDescriptor;
private JetType classObjectType;
@@ -57,18 +49,10 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme
public MutableClassDescriptor(@NotNull BindingTrace trace, @NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope outerScope, ClassKind kind) {
super(containingDeclaration);
TraceBasedRedeclarationHandler redeclarationHandler = new TraceBasedRedeclarationHandler(trace);
this.redeclarationHandler = redeclarationHandler;
this.scopeForMemberLookup = new WritableScopeImpl(JetScope.EMPTY, this, redeclarationHandler).setDebugName("MemberLookup");
this.scopeForMemberLookup.changeLockLevel(WritableScope.LockLevel.BOTH);
this.scopeForSupertypeResolution = new WritableScopeImpl(outerScope, this, redeclarationHandler).setDebugName("SupertypeResolution");
this.scopeForSupertypeResolution.changeLockLevel(WritableScope.LockLevel.BOTH);
this.scopeForMemberResolution = new WritableScopeImpl(scopeForSupertypeResolution, this, redeclarationHandler).setDebugName("MemberResolution");
this.scopeForMemberResolution.changeLockLevel(WritableScope.LockLevel.BOTH);
this.scopeForAnyConstructor = new WritableScopeImpl(scopeForMemberResolution, this, redeclarationHandler).setDebugName("AnyConstrutor");
this.scopeForAnyConstructor.changeLockLevel(WritableScope.LockLevel.BOTH);
this.scopeForPrimaryConstructor = new WritableScopeImpl(scopeForAnyConstructor, this, redeclarationHandler).setDebugName("PrimaryConstructor");
this.scopeForPrimaryConstructor.changeLockLevel(WritableScope.LockLevel.BOTH);
this.scopesForConstructors = new HashMap<ConstructorDescriptor, WritableScope>();
this.scopeForMemberLookup = new WritableScopeImpl(JetScope.EMPTY, this, redeclarationHandler).setDebugName("MemberLookup").changeLockLevel(WritableScope.LockLevel.BOTH);
this.scopeForSupertypeResolution = new WritableScopeImpl(outerScope, this, redeclarationHandler).setDebugName("SupertypeResolution").changeLockLevel(WritableScope.LockLevel.BOTH);
this.scopeForMemberResolution = new WritableScopeImpl(scopeForSupertypeResolution, this, redeclarationHandler).setDebugName("MemberResolution").changeLockLevel(WritableScope.LockLevel.BOTH);
this.scopeForInitializers = new WritableScopeImpl(scopeForMemberResolution, this, redeclarationHandler).setDebugName("PropertyInitializers").changeLockLevel(WritableScope.LockLevel.BOTH);
this.kind = kind;
}
@@ -132,32 +116,19 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme
public void addConstructor(@NotNull ConstructorDescriptor constructorDescriptor, BindingTrace trace) {
assert constructorDescriptor.getContainingDeclaration() == this;
// assert constructorDescriptor.getTypeParameters().size() == getTypeConstructor().getValueParameters().size();
constructors.add(constructorDescriptor);
if (defaultType != null) {
// constructorDescriptor.getTypeParameters().addAll(typeParameters);
((ConstructorDescriptorImpl) constructorDescriptor).setReturnType(getDefaultType());
}
WritableScope scope = constructorDescriptor.isPrimary() ?
scopeForPrimaryConstructor :
new WritableScopeImpl(scopeForAnyConstructor, this, redeclarationHandler).setDebugName("SecondaryConstructor");
WritableScope old = scopesForConstructors.put(constructorDescriptor, scope);
if (old != null) {
throw new IllegalStateException();
}
for (ValueParameterDescriptor valueParameterDescriptor : constructorDescriptor.getValueParameters()) {
JetParameter parameter = (JetParameter) trace.getBindingContext().get(BindingContext.DESCRIPTOR_TO_DECLARATION, valueParameterDescriptor);
if (parameter.getValOrVarNode() == null || !constructorDescriptor.isPrimary()) {
scope.addVariableDescriptor(valueParameterDescriptor);
if (constructorDescriptor.isPrimary()) {
for (ValueParameterDescriptor valueParameterDescriptor : constructorDescriptor.getValueParameters()) {
JetParameter parameter = (JetParameter) trace.getBindingContext().get(BindingContext.DESCRIPTOR_TO_DECLARATION, valueParameterDescriptor);
assert parameter != null;
if (parameter.getValOrVarNode() == null || !constructorDescriptor.isPrimary()) {
scopeForInitializers.addVariableDescriptor(valueParameterDescriptor);
}
}
}
scope.addLabeledDeclaration(constructorDescriptor); // TODO : Labels for constructors?!
scope.changeLockLevel(WritableScope.LockLevel.READING);
}
@Override
@@ -166,7 +137,7 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme
callableMembers.add(propertyDescriptor);
scopeForMemberLookup.addVariableDescriptor(propertyDescriptor);
scopeForMemberResolution.addVariableDescriptor(propertyDescriptor);
scopeForAnyConstructor.addPropertyDescriptorByFieldName("$" + propertyDescriptor.getName(), propertyDescriptor);
scopeForInitializers.addPropertyDescriptorByFieldName("$" + propertyDescriptor.getName(), propertyDescriptor);
}
@Override
@@ -299,13 +270,8 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme
}
@NotNull
public JetScope getScopeForConstructor(@NotNull ConstructorDescriptor constructorDescriptor) {
return scopesForConstructors.get(constructorDescriptor);
}
@NotNull
public JetScope getScopeForPropertyIntiailizer() {
return scopeForPrimaryConstructor;
public JetScope getScopeForInitializers() {
return scopeForInitializers;
}
@NotNull
@@ -399,12 +365,7 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme
scopeForSupertypeResolution.changeLockLevel(WritableScope.LockLevel.READING);
scopeForMemberLookup.changeLockLevel(WritableScope.LockLevel.READING);
scopeForMemberResolution.changeLockLevel(WritableScope.LockLevel.READING);
scopeForAnyConstructor.changeLockLevel(WritableScope.LockLevel.READING);
scopeForPrimaryConstructor.changeLockLevel(WritableScope.LockLevel.READING);
for (WritableScope scope : scopesForConstructors.values()) {
scope.changeLockLevel(WritableScope.LockLevel.READING);
}
scopeForInitializers.changeLockLevel(WritableScope.LockLevel.READING);
if (classObjectDescriptor != null) {
classObjectDescriptor.lockScopes();
}
@@ -27,7 +27,6 @@ public class PropertyDescriptor extends VariableDescriptorImpl implements Callab
private final boolean isVar;
private final ReceiverDescriptor expectedThisObject;
private final Set<PropertyDescriptor> overriddenProperties = Sets.newLinkedHashSet();
private final Set<PropertyDescriptor> overridingProperties = Sets.newLinkedHashSet();
private final PropertyDescriptor original;
private ReceiverDescriptor receiver;
@@ -100,7 +100,7 @@ public class BodyResolver {
final ConstructorDescriptor primaryConstructor = descriptor.getUnsubstitutedPrimaryConstructor();
final JetScope scopeForConstructor = primaryConstructor == null
? null
: getInnerScopeForConstructor(primaryConstructor);
: FunctionDescriptorUtil.getFunctionInnerScope(descriptor.getScopeForSupertypeResolution(), primaryConstructor, traceForConstructors);
final ExpressionTypingServices typeInferrer = context.getSemanticServices().getTypeInferrerServices(traceForConstructors); // TODO : flow
final Map<JetTypeReference, JetType> supertypes = Maps.newLinkedHashMap();
@@ -267,10 +267,10 @@ public class BodyResolver {
if (jetClassOrObject.hasPrimaryConstructor()) {
ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor();
assert primaryConstructor != null;
final JetScope scopeForConstructor = getInnerScopeForConstructor(primaryConstructor);
final JetScope scopeForInitializers = classDescriptor.getScopeForInitializers();
ExpressionTypingServices typeInferrer = context.getSemanticServices().getTypeInferrerServices(traceForConstructors);
for (JetClassInitializer anonymousInitializer : anonymousInitializers) {
typeInferrer.getType(scopeForConstructor, anonymousInitializer.getBody(), NO_EXPECTED_TYPE);
typeInferrer.getType(scopeForInitializers, anonymousInitializer.getBody(), NO_EXPECTED_TYPE);
}
}
else {
@@ -285,15 +285,19 @@ public class BodyResolver {
JetSecondaryConstructor constructor = entry.getKey();
ConstructorDescriptor descriptor = entry.getValue();
resolveSecondaryConstructorBody(constructor, descriptor, ((MutableClassDescriptor) descriptor.getContainingDeclaration()).getScopeForMemberResolution());
resolveSecondaryConstructorBody(constructor, descriptor);
assert descriptor.getReturnType() != null;
}
}
private void resolveSecondaryConstructorBody(JetSecondaryConstructor declaration, final ConstructorDescriptor descriptor, final JetScope declaringScope) {
private void resolveSecondaryConstructorBody(JetSecondaryConstructor declaration, final ConstructorDescriptor descriptor) {
if (!context.completeAnalysisNeeded(declaration)) return;
final JetScope functionInnerScope = getInnerScopeForConstructor(descriptor);
MutableClassDescriptor classDescriptor = (MutableClassDescriptor) descriptor.getContainingDeclaration();
final JetScope scopeForSupertypeInitializers = FunctionDescriptorUtil.getFunctionInnerScope(classDescriptor.getScopeForSupertypeResolution(), descriptor, traceForConstructors);
//contains only constructor parameters
final JetScope scopeForConstructorBody = FunctionDescriptorUtil.getFunctionInnerScope(classDescriptor.getScopeForInitializers(), descriptor, traceForConstructors);
//contains members & backing fields
final CallResolver callResolver = new CallResolver(context.getSemanticServices(), DataFlowInfo.EMPTY); // TODO: dataFlowInfo
@@ -313,8 +317,7 @@ public class BodyResolver {
public void visitDelegationToSuperCallSpecifier(JetDelegatorToSuperCall call) {
JetTypeReference typeReference = call.getTypeReference();
if (typeReference != null) {
callResolver.resolveCall(context.getTrace(), functionInnerScope,
CallMaker.makeCall(ReceiverDescriptor.NO_RECEIVER, null, call), NO_EXPECTED_TYPE);
callResolver.resolveCall(context.getTrace(), scopeForSupertypeInitializers, CallMaker.makeCall(ReceiverDescriptor.NO_RECEIVER, null, call), NO_EXPECTED_TYPE);
}
}
@@ -325,7 +328,7 @@ public class BodyResolver {
ClassDescriptor classDescriptor = descriptor.getContainingDeclaration();
callResolver.resolveCall(context.getTrace(),
functionInnerScope,
scopeForSupertypeInitializers,
CallMaker.makeCall(ReceiverDescriptor.NO_RECEIVER, null, call), NO_EXPECTED_TYPE);
// call.getThisReference(),
// classDescriptor,
@@ -357,20 +360,12 @@ public class BodyResolver {
}
JetExpression bodyExpression = declaration.getBodyExpression();
if (bodyExpression != null) {
//context.getDescriptorResolver().computeFlowData(declaration, bodyExpression);
//JetFlowInformationProvider flowInformationProvider = context.getDescriptorResolver().computeFlowData(declaration, bodyExpression);
ExpressionTypingServices typeInferrer = context.getSemanticServices().getTypeInferrerServices(traceForConstructors);
typeInferrer.checkFunctionReturnType(functionInnerScope, declaration, JetStandardClasses.getUnitType());
typeInferrer.checkFunctionReturnType(scopeForConstructorBody, declaration, JetStandardClasses.getUnitType());
}
}
@NotNull
private JetScope getInnerScopeForConstructor(@NotNull ConstructorDescriptor descriptor) {
MutableClassDescriptor mutableClassDescriptor = (MutableClassDescriptor) descriptor.getContainingDeclaration();
return mutableClassDescriptor.getScopeForConstructor(descriptor);
}
private void resolvePropertyDeclarationBodies() {
// Member properties
@@ -74,7 +74,7 @@ public class DeclarationResolver {
MutableClassDescriptor classDescriptor = entry.getValue();
resolveFunctionAndPropertyHeaders(jetClass.getDeclarations(), classDescriptor.getScopeForMemberResolution(),
classDescriptor.getScopeForPropertyIntiailizer(), classDescriptor.getScopeForMemberResolution(),
classDescriptor.getScopeForInitializers(), classDescriptor.getScopeForMemberResolution(),
classDescriptor);
// processPrimaryConstructor(classDescriptor, jetClass);
// for (JetSecondaryConstructor jetConstructor : jetClass.getSecondaryConstructors()) {
@@ -86,7 +86,7 @@ public class DeclarationResolver {
MutableClassDescriptor classDescriptor = entry.getValue();
resolveFunctionAndPropertyHeaders(object.getDeclarations(), classDescriptor.getScopeForMemberResolution(),
classDescriptor.getScopeForPropertyIntiailizer(), classDescriptor.getScopeForMemberResolution(),
classDescriptor.getScopeForInitializers(), classDescriptor.getScopeForMemberResolution(),
classDescriptor);
}
@@ -161,6 +161,9 @@ public class CallResolver {
else if (calleeExpression instanceof JetThisReferenceExpression) {
functionReference = (JetThisReferenceExpression) calleeExpression;
DeclarationDescriptor containingDeclaration = scope.getContainingDeclaration();
if (containingDeclaration instanceof ConstructorDescriptor) {
containingDeclaration = containingDeclaration.getContainingDeclaration();
}
assert containingDeclaration instanceof ClassDescriptor;
ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
@@ -15,7 +15,7 @@ public interface WritableScope extends JetScope {
READING,
}
void changeLockLevel(LockLevel lockLevel);
WritableScope changeLockLevel(LockLevel lockLevel);
void addLabeledDeclaration(@NotNull DeclarationDescriptor descriptor);
@@ -36,11 +36,12 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
private LockLevel lockLevel = LockLevel.WRITING;
@Override
public void changeLockLevel(LockLevel lockLevel) {
public WritableScope changeLockLevel(LockLevel lockLevel) {
if (lockLevel.ordinal() < this.lockLevel.ordinal()) {
throw new IllegalStateException("cannot lower lock level from " + this.lockLevel + " to " + lockLevel);
}
this.lockLevel = lockLevel;
return this;
}
protected void checkMayRead() {
@@ -0,0 +1,52 @@
// +JDK
namespace kt_250_617_10
import java.util.ArrayList
import java.util.HashMap
//KT-250 Incorrect variable resolve in constructor arguments of superclass
open class A(val x: Int)
class B(y: Int) : A(<!UNRESOLVED_REFERENCE!>x<!>) //x is resolved as a property in a, so no error is generated
//KT-617 Prohibit dollars in call to superclass constructors
open class M(p: Int)
class N(val p: Int) : A(<!UNRESOLVED_REFERENCE!>$p<!>)
//KT-10 Don't allow to use properties in supertype initializers
open class Element()
class TextElement(name: String) : Element()
abstract class Tag(val name : String) {
val children = ArrayList<Element>()
val attributes = HashMap<String, String>()
}
abstract class TagWithText(name : String) : Tag(name) {
fun String.plus() {
children.add(TextElement(this))
}
}
open class BodyTag(name : String) : TagWithText(name) {
}
class Body() : BodyTag(<!UNRESOLVED_REFERENCE!>name<!>) { // Must be an error!
}
class Body1() : BodyTag(<!NO_THIS!>this<!>.name) { // Must be an error!
}
//more tests
open class X(p: Int, r: Int) {
val s = "s"
}
class Y(i: Int) : X(i, <!UNRESOLVED_REFERENCE!>rrr<!>) {
val rrr = 3
this(s: Int, r: Int) : this(s, <!UNRESOLVED_REFERENCE!>rrr<!>)
}
class Z(val i: Int) : X(<!UNRESOLVED_REFERENCE!>s<!>, <!UNRESOLVED_REFERENCE!>x<!>) {
val x = 2
}