factory method added

not to create empty Collections on invocation every time
This commit is contained in:
Svetlana Isakova
2012-06-07 17:09:19 +04:00
parent 6c6c1a1cd1
commit 3d2021a132
4 changed files with 36 additions and 35 deletions
@@ -17,11 +17,9 @@
package org.jetbrains.jet.lang.resolve;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.psi.JetPattern;
import org.jetbrains.jet.lang.psi.JetScript;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.name.FqName;
@@ -37,7 +35,6 @@ import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.lang.types.expressions.CoercionStrategy;
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingContext;
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
import org.jetbrains.jet.lang.types.expressions.LabelResolver;
import org.jetbrains.jet.lang.types.ref.JetTypeName;
import javax.inject.Inject;
@@ -170,9 +167,6 @@ public class ScriptResolver {
ExpressionTypingContext context = ExpressionTypingContext.newContext(
expressionTypingServices,
Maps.<JetPattern, DataFlowInfo>newHashMap(),
Maps.<JetPattern, List<VariableDescriptor>>newHashMap(),
new LabelResolver(),
trace,
scope,
DataFlowInfo.EMPTY,
@@ -19,13 +19,10 @@ package org.jetbrains.jet.lang.types.expressions;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.TemporaryBindingTrace;
import org.jetbrains.jet.lang.resolve.calls.BasicResolutionContext;
import org.jetbrains.jet.lang.resolve.calls.CallMaker;
import org.jetbrains.jet.lang.resolve.calls.OverloadResolutionResults;
@@ -37,7 +34,7 @@ import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeUtils;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -46,6 +43,18 @@ import java.util.Map;
*/
public class ExpressionTypingContext {
@NotNull
public static ExpressionTypingContext newContext(
@NotNull ExpressionTypingServices expressionTypingServices,
@NotNull BindingTrace trace,
@NotNull JetScope scope,
@NotNull DataFlowInfo dataFlowInfo,
@NotNull JetType expectedType,
boolean namespacesAllowed) {
return newContext(expressionTypingServices, new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(),
new LabelResolver(), trace, scope, dataFlowInfo, expectedType, namespacesAllowed);
}
@NotNull
public static ExpressionTypingContext newContext(
@NotNull ExpressionTypingServices expressionTypingServices,
@@ -101,30 +110,37 @@ public class ExpressionTypingContext {
@NotNull
public ExpressionTypingContext replaceNamespacesAllowed(boolean namespacesAllowed) {
if (namespacesAllowed == this.namespacesAllowed) return this;
return newContext(expressionTypingServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, expectedType, namespacesAllowed);
return newContext(expressionTypingServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver,
trace, scope, dataFlowInfo, expectedType, namespacesAllowed);
}
@NotNull
public ExpressionTypingContext replaceDataFlowInfo(DataFlowInfo newDataFlowInfo) {
if (newDataFlowInfo == dataFlowInfo) return this;
return newContext(expressionTypingServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, newDataFlowInfo, expectedType, namespacesAllowed);
return newContext(expressionTypingServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver,
trace, scope, newDataFlowInfo, expectedType, namespacesAllowed);
}
@NotNull
public ExpressionTypingContext replaceExpectedType(@Nullable JetType newExpectedType) {
if (newExpectedType == null) return replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE);
if (expectedType == newExpectedType) return this;
return newContext(expressionTypingServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, newExpectedType, namespacesAllowed);
return newContext(expressionTypingServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver,
trace, scope, dataFlowInfo, newExpectedType, namespacesAllowed);
}
@NotNull
public ExpressionTypingContext replaceBindingTrace(@NotNull BindingTrace newTrace) {
if (newTrace == trace) return this;
return newContext(expressionTypingServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, newTrace, scope, dataFlowInfo, expectedType, namespacesAllowed);
return newContext(expressionTypingServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver,
newTrace, scope, dataFlowInfo, expectedType, namespacesAllowed);
}
@NotNull
public ExpressionTypingContext replaceScope(@NotNull JetScope newScope) {
if (newScope == scope) return this;
return newContext(expressionTypingServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, newScope, dataFlowInfo, expectedType, namespacesAllowed);
return newContext(expressionTypingServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver,
trace, newScope, dataFlowInfo, expectedType, namespacesAllowed);
}
///////////// LAZY ACCESSORS
@@ -26,7 +26,6 @@ import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptorUtil;
import org.jetbrains.jet.lang.descriptors.ScriptDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.*;
@@ -116,31 +115,27 @@ public class ExpressionTypingServices {
}
@Nullable
public JetType getType(@NotNull final JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType, BindingTrace trace) {
public JetType getType(@NotNull final JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType, @NotNull BindingTrace trace) {
return getType(scope, expression, expectedType, DataFlowInfo.EMPTY, trace);
}
@Nullable
public JetType getType(@NotNull final JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType, @NotNull DataFlowInfo dataFlowInfo, BindingTrace trace) {
public JetType getType(@NotNull final JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType, @NotNull DataFlowInfo dataFlowInfo, @NotNull BindingTrace trace) {
ExpressionTypingContext context = ExpressionTypingContext.newContext(
this,
new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
trace, scope, dataFlowInfo, expectedType, false
this, trace, scope, dataFlowInfo, expectedType, false
);
return expressionTypingFacade.getType(expression, context);
}
public JetType getTypeWithNamespaces(@NotNull final JetScope scope, @NotNull JetExpression expression, BindingTrace trace) {
public JetType getTypeWithNamespaces(@NotNull final JetScope scope, @NotNull JetExpression expression, @NotNull BindingTrace trace) {
ExpressionTypingContext context = ExpressionTypingContext.newContext(
this,
new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
trace, scope, DataFlowInfo.EMPTY, NO_EXPECTED_TYPE, true);
this, trace, scope, DataFlowInfo.EMPTY, NO_EXPECTED_TYPE, true);
return expressionTypingFacade.getType(expression, context);
// return ((ExpressionTypingContext) ExpressionTyperVisitorWithNamespaces).INSTANCE.getType(expression, ExpressionTypingContext.newRootContext(semanticServices, trace, scope, DataFlowInfo.getEmpty(), TypeUtils.NO_EXPECTED_TYPE, TypeUtils.NO_EXPECTED_TYPE));
}
@NotNull
public JetType inferFunctionReturnType(@NotNull JetScope outerScope, JetDeclarationWithBody function, FunctionDescriptor functionDescriptor, BindingTrace trace) {
public JetType inferFunctionReturnType(@NotNull JetScope outerScope, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, @NotNull BindingTrace trace) {
Map<JetExpression, JetType> typeMap = collectReturnedExpressionsWithTypes(trace, outerScope, function, functionDescriptor);
Collection<JetType> types = typeMap.values();
return types.isEmpty()
@@ -159,9 +154,7 @@ public class ExpressionTypingServices {
}
}
checkFunctionReturnType(function, ExpressionTypingContext.newContext(
this,
new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
trace, functionInnerScope, dataFlowInfo, expectedReturnType != null ? expectedReturnType : NO_EXPECTED_TYPE, false
this, trace, functionInnerScope, dataFlowInfo, expectedReturnType != null ? expectedReturnType : NO_EXPECTED_TYPE, false
), trace);
}
@@ -217,7 +210,6 @@ public class ExpressionTypingServices {
JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(outerScope, functionDescriptor, trace);
expressionTypingFacade.getType(bodyExpression, ExpressionTypingContext.newContext(
this,
new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
trace, functionInnerScope, DataFlowInfo.EMPTY, NO_EXPECTED_TYPE, false), !function.hasBlockBody());
//todo function literals
final Collection<JetExpression> returnedExpressions = Lists.newArrayList();
@@ -26,7 +26,10 @@ import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetImportDirective;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
import org.jetbrains.jet.lang.resolve.*;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.calls.inference.*;
@@ -47,7 +50,6 @@ import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import static org.jetbrains.jet.lang.diagnostics.Errors.RESULT_TYPE_MISMATCH;
@@ -159,9 +161,6 @@ public class ExpressionTypingUtils {
ExpressionReceiver expressionReceiver = new ExpressionReceiver(expression, variableDescriptor.getType());
ExpressionTypingContext context = ExpressionTypingContext.newContext(
expressionTypingServices,
new HashMap<JetPattern, DataFlowInfo>(),
new HashMap<JetPattern, List<VariableDescriptor>>(),
new LabelResolver(),
new BindingTraceContext(),
scope,
DataFlowInfo.EMPTY,