LabelName stores label name

TODO: uncomment assertions in LabelName constructor
This commit is contained in:
Stepan Koltsov
2012-05-23 02:52:31 +04:00
parent 65a6b7f726
commit 3c64fbb2a0
13 changed files with 108 additions and 28 deletions
@@ -25,6 +25,7 @@ import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import java.util.Collection;
@@ -56,7 +57,7 @@ public class JavaClassMembersScope extends JavaClassOrPackageScope {
@NotNull
@Override
public Collection<DeclarationDescriptor> getDeclarationsByLabel(String labelName) {
public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
throw new UnsupportedOperationException(); // TODO
}
@@ -18,6 +18,7 @@ package org.jetbrains.jet.lang.resolve;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
@@ -91,7 +92,7 @@ public abstract class AbstractScopeAdapter implements JetScope {
@NotNull
@Override
public Collection<DeclarationDescriptor> getDeclarationsByLabel(String labelName) {
public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
return getWorkerScope().getDeclarationsByLabel(labelName);
}
@@ -0,0 +1,68 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.name;
import org.jetbrains.annotations.NotNull;
/**
* @author Stepan Koltsov
*/
public class LabelName {
@NotNull
private final String name;
public LabelName(@NotNull String name) {
if (name.startsWith("@")) {
// label may contain @, and may not contain
//throw new IllegalArgumentException("@ must be chopped: " + name);
}
if (name.length() == 0) {
// label can be empty
//throw new IllegalStateException("label cannot be empty");
}
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LabelName name1 = (LabelName) o;
if (!name.equals(name1.name)) return false;
return true;
}
@Override
public int hashCode() {
return name.hashCode();
}
}
@@ -19,6 +19,7 @@ package org.jetbrains.jet.lang.resolve.scopes;
import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import java.util.Collection;
@@ -134,7 +135,7 @@ public class ChainedScope implements JetScope {
@NotNull
@Override
public Collection<DeclarationDescriptor> getDeclarationsByLabel(String labelName) {
public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
for (JetScope jetScope : scopeChain) {
Collection<DeclarationDescriptor> declarationsByLabel = jetScope.getDeclarationsByLabel(labelName);
if (!declarationsByLabel.isEmpty()) return declarationsByLabel; // TODO : merge?
@@ -24,6 +24,7 @@ import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassKind;
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import java.util.Collection;
@@ -58,7 +59,7 @@ public class InnerClassesScopeWrapper extends JetScopeImpl {
@NotNull
@Override
public Collection<DeclarationDescriptor> getDeclarationsByLabel(String labelName) {
public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
Collection<DeclarationDescriptor> declarationsByLabel = actualScope.getDeclarationsByLabel(labelName);
return Collections2.filter(declarationsByLabel, new Predicate<DeclarationDescriptor>() {
@Override
@@ -19,6 +19,7 @@ package org.jetbrains.jet.lang.resolve.scopes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import java.util.Collection;
@@ -67,7 +68,7 @@ public interface JetScope {
DeclarationDescriptor getContainingDeclaration();
@NotNull
Collection<DeclarationDescriptor> getDeclarationsByLabel(String labelName);
Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName);
/**
* @param fieldName includes the "$"
@@ -18,6 +18,7 @@ package org.jetbrains.jet.lang.resolve.scopes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import java.util.Collection;
@@ -75,7 +76,7 @@ public abstract class JetScopeImpl implements JetScope {
@NotNull
@Override
public Collection<DeclarationDescriptor> getDeclarationsByLabel(String labelName) {
public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
return Collections.emptyList();
}
@@ -21,6 +21,7 @@ import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
@@ -136,7 +137,7 @@ public class SubstitutingScope implements JetScope {
@NotNull
@Override
public Collection<DeclarationDescriptor> getDeclarationsByLabel(String labelName) {
public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
throw new UnsupportedOperationException(); // TODO
}
@@ -21,6 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
import org.jetbrains.jet.util.CommonSuppliers;
@@ -56,7 +57,7 @@ public class WritableScopeImpl extends WritableScopeWithImports {
private Map<String, NamespaceDescriptor> namespaceAliases;
@Nullable
private Map<String, List<DeclarationDescriptor>> labelsToDescriptors;
private Map<LabelName, List<DeclarationDescriptor>> labelsToDescriptors;
@Nullable
private Map<String, ClassDescriptor> objectDescriptors;
@@ -137,9 +138,9 @@ public class WritableScopeImpl extends WritableScopeWithImports {
}
@NotNull
private Map<String, List<DeclarationDescriptor>> getLabelsToDescriptors() {
private Map<LabelName, List<DeclarationDescriptor>> getLabelsToDescriptors() {
if (labelsToDescriptors == null) {
labelsToDescriptors = new HashMap<String, List<DeclarationDescriptor>>();
labelsToDescriptors = new HashMap<LabelName, List<DeclarationDescriptor>>();
}
return labelsToDescriptors;
}
@@ -154,11 +155,11 @@ public class WritableScopeImpl extends WritableScopeWithImports {
@NotNull
@Override
public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull String labelName) {
public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull LabelName labelName) {
checkMayRead();
Collection<DeclarationDescriptor> superResult = super.getDeclarationsByLabel(labelName);
Map<String, List<DeclarationDescriptor>> labelsToDescriptors = getLabelsToDescriptors();
Map<LabelName, List<DeclarationDescriptor>> labelsToDescriptors = getLabelsToDescriptors();
List<DeclarationDescriptor> declarationDescriptors = labelsToDescriptors.get(labelName);
if (declarationDescriptors == null) {
return superResult;
@@ -173,9 +174,8 @@ public class WritableScopeImpl extends WritableScopeWithImports {
public void addLabeledDeclaration(@NotNull DeclarationDescriptor descriptor) {
checkMayWrite();
Map<String, List<DeclarationDescriptor>> labelsToDescriptors = getLabelsToDescriptors();
String name = descriptor.getName();
assert name != null;
Map<LabelName, List<DeclarationDescriptor>> labelsToDescriptors = getLabelsToDescriptors();
LabelName name = new LabelName(descriptor.getName());
List<DeclarationDescriptor> declarationDescriptors = labelsToDescriptors.get(name);
if (declarationDescriptors == null) {
declarationDescriptors = new ArrayList<DeclarationDescriptor>();
@@ -22,6 +22,7 @@ import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import java.util.Collection;
@@ -49,7 +50,7 @@ public class WriteThroughScope extends WritableScopeWithImports {
@Override
@NotNull
public Collection<DeclarationDescriptor> getDeclarationsByLabel(String labelName) {
public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
checkMayRead();
return writableWorker.getDeclarationsByLabel(labelName);
@@ -20,6 +20,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.error.ErrorSimpleFunctionDescriptorImpl;
@@ -99,7 +100,7 @@ public class ErrorUtils {
@NotNull
@Override
public Collection<DeclarationDescriptor> getDeclarationsByLabel(String labelName) {
public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
return Collections.emptyList();
}
@@ -36,6 +36,7 @@ import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowValueFactory;
import org.jetbrains.jet.lang.resolve.constants.*;
import org.jetbrains.jet.lang.resolve.constants.StringValue;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ClassReceiver;
@@ -486,7 +487,8 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
ReceiverDescriptor thisReceiver = null;
String labelName = expression.getLabelName();
if (labelName != null) {
thisReceiver = context.labelResolver.resolveThisLabel(expression.getInstanceReference(), expression.getTargetLabel(), context, thisReceiver, labelName);
thisReceiver = context.labelResolver.resolveThisLabel(
expression.getInstanceReference(), expression.getTargetLabel(), context, thisReceiver, new LabelName(labelName));
}
else {
if (onlyClassReceivers) {
@@ -682,7 +684,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
if (JetTokens.LABELS.contains(operationSign.getReferencedNameElementType())) {
String referencedName = operationSign.getReferencedName();
referencedName = referencedName == null ? " <?>" : referencedName;
context.labelResolver.enterLabeledElement(referencedName.substring(1), baseExpression);
context.labelResolver.enterLabeledElement(new LabelName(referencedName.substring(1)), baseExpression);
// TODO : Some processing for the label?
JetType type = facade.getType(baseExpression, context, isStatement);
context.labelResolver.exitLabeledElement(baseExpression);
@@ -25,6 +25,7 @@ import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import java.util.*;
@@ -37,11 +38,11 @@ import static org.jetbrains.jet.lang.resolve.BindingContext.*;
*/
public class LabelResolver {
private final Map<String, Stack<JetElement>> labeledElements = new HashMap<String, Stack<JetElement>>();
private final Map<LabelName, Stack<JetElement>> labeledElements = new HashMap<LabelName, Stack<JetElement>>();
public LabelResolver() {}
public void enterLabeledElement(@NotNull String labelName, @NotNull JetExpression labeledExpression) {
public void enterLabeledElement(@NotNull LabelName labelName, @NotNull JetExpression labeledExpression) {
JetExpression deparenthesized = JetPsiUtil.deparenthesize(labeledExpression);
if (deparenthesized != null) {
Stack<JetElement> stack = labeledElements.get(labelName);
@@ -56,8 +57,8 @@ public class LabelResolver {
public void exitLabeledElement(@NotNull JetExpression expression) {
JetExpression deparenthesized = JetPsiUtil.deparenthesize(expression);
// TODO : really suboptimal
for (Iterator<Map.Entry<String, Stack<JetElement>>> mapIter = labeledElements.entrySet().iterator(); mapIter.hasNext(); ) {
Map.Entry<String, Stack<JetElement>> entry = mapIter.next();
for (Iterator<Map.Entry<LabelName,Stack<JetElement>>> mapIter = labeledElements.entrySet().iterator(); mapIter.hasNext(); ) {
Map.Entry<LabelName, Stack<JetElement>> entry = mapIter.next();
Stack<JetElement> stack = entry.getValue();
for (Iterator<JetElement> stackIter = stack.iterator(); stackIter.hasNext(); ) {
JetElement recorded = stackIter.next();
@@ -72,7 +73,7 @@ public class LabelResolver {
}
@Nullable
private JetElement resolveControlLabel(@NotNull String labelName, @NotNull JetSimpleNameExpression labelExpression, boolean reportUnresolved, ExpressionTypingContext context) {
private JetElement resolveControlLabel(@NotNull LabelName labelName, @NotNull JetSimpleNameExpression labelExpression, boolean reportUnresolved, ExpressionTypingContext context) {
Collection<DeclarationDescriptor> declarationsByLabel = context.scope.getDeclarationsByLabel(labelName);
int size = declarationsByLabel.size();
@@ -99,14 +100,13 @@ public class LabelResolver {
public JetElement resolveLabel(JetLabelQualifiedExpression expression, ExpressionTypingContext context) {
JetSimpleNameExpression labelElement = expression.getTargetLabel();
if (labelElement != null) {
String labelName = expression.getLabelName();
assert labelName != null;
LabelName labelName = new LabelName(expression.getLabelName());
return resolveControlLabel(labelName, labelElement, true, context);
}
return null;
}
private JetElement resolveNamedLabel(@NotNull String labelName, @NotNull JetSimpleNameExpression labelExpression, boolean reportUnresolved, ExpressionTypingContext context) {
private JetElement resolveNamedLabel(@NotNull LabelName labelName, @NotNull JetSimpleNameExpression labelExpression, boolean reportUnresolved, ExpressionTypingContext context) {
Stack<JetElement> stack = labeledElements.get(labelName);
if (stack == null || stack.isEmpty()) {
if (reportUnresolved) {
@@ -123,7 +123,8 @@ public class LabelResolver {
return result;
}
public ReceiverDescriptor resolveThisLabel(JetReferenceExpression thisReference, JetSimpleNameExpression targetLabel, ExpressionTypingContext context, ReceiverDescriptor thisReceiver, String labelName) {
public ReceiverDescriptor resolveThisLabel(JetReferenceExpression thisReference, JetSimpleNameExpression targetLabel,
ExpressionTypingContext context, ReceiverDescriptor thisReceiver, LabelName labelName) {
Collection<DeclarationDescriptor> declarationsByLabel = context.scope.getDeclarationsByLabel(labelName);
int size = declarationsByLabel.size();
assert targetLabel != null;