Store resolved calls for loop range's iterator(), next(), hasNext()

+ code duplication removed from expression typing visitor
This commit is contained in:
Andrey Breslav
2012-08-23 16:32:59 +04:00
parent b1ba3e1260
commit 784bf31a3e
5 changed files with 81 additions and 66 deletions
@@ -20,10 +20,10 @@ import com.google.dart.compiler.backend.js.ast.*;
import com.intellij.openapi.util.Pair;
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.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetForExpression;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.general.Translation;
import org.jetbrains.k2js.translate.reference.CallBuilder;
@@ -61,24 +61,24 @@ public final class IteratorForTranslator extends ForTranslator {
@NotNull
private JsExpression hasNextMethodInvocation() {
CallableDescriptor hasNextFunction = getHasNextCallable(bindingContext(), getLoopRange(expression));
return translateMethodInvocation(iterator.second, hasNextFunction);
ResolvedCall<FunctionDescriptor> resolvedCall = getHasNextCallable(bindingContext(), getLoopRange(expression));
return translateMethodInvocation(iterator.second, resolvedCall);
}
@NotNull
private JsExpression iteratorMethodInvocation() {
JetExpression rangeExpression = getLoopRange(expression);
JsExpression range = Translation.translateAsExpression(rangeExpression, context());
FunctionDescriptor iteratorFunction = getIteratorFunction(bindingContext(), rangeExpression);
return translateMethodInvocation(range, iteratorFunction);
ResolvedCall<FunctionDescriptor> resolvedCall = getIteratorFunction(bindingContext(), rangeExpression);
return translateMethodInvocation(range, resolvedCall);
}
@NotNull
private JsExpression translateMethodInvocation(@Nullable JsExpression receiver,
@NotNull CallableDescriptor descriptor) {
@NotNull ResolvedCall<FunctionDescriptor> resolvedCall) {
return CallBuilder.build(context())
.resolvedCall(resolvedCall)
.receiver(receiver)
.descriptor(descriptor)
.translate();
}
}
@@ -227,35 +227,30 @@ public final class BindingUtils {
}
@NotNull
public static FunctionDescriptor getIteratorFunction(@NotNull BindingContext context,
public static ResolvedCall<FunctionDescriptor> getIteratorFunction(@NotNull BindingContext context,
@NotNull JetExpression rangeExpression) {
FunctionDescriptor functionDescriptor = context.get(BindingContext.LOOP_RANGE_ITERATOR, rangeExpression);
assert functionDescriptor != null :
ResolvedCall<FunctionDescriptor> resolvedCall = context.get(BindingContext.LOOP_RANGE_ITERATOR_RESOLVED_CALL, rangeExpression);
assert resolvedCall != null :
message(rangeExpression, "Range expression must have a descriptor for iterator function");
return functionDescriptor;
return resolvedCall;
}
@NotNull
public static FunctionDescriptor getNextFunction(@NotNull BindingContext context,
public static ResolvedCall<FunctionDescriptor> getNextFunction(@NotNull BindingContext context,
@NotNull JetExpression rangeExpression) {
FunctionDescriptor functionDescriptor = context.get(BindingContext.LOOP_RANGE_NEXT, rangeExpression);
assert functionDescriptor != null : ErrorReportingUtils
ResolvedCall<FunctionDescriptor> resolvedCall = context.get(BindingContext.LOOP_RANGE_NEXT_RESOLVED_CALL, rangeExpression);
assert resolvedCall != null : ErrorReportingUtils
.message(rangeExpression, "Range expression must have a descriptor for next function");
return functionDescriptor;
return resolvedCall;
}
@NotNull
public static CallableDescriptor getHasNextCallable(@NotNull BindingContext context,
public static ResolvedCall<FunctionDescriptor> getHasNextCallable(@NotNull BindingContext context,
@NotNull JetExpression rangeExpression) {
CallableDescriptor hasNextDescriptor = context.get(BindingContext.LOOP_RANGE_HAS_NEXT, rangeExpression);
assert hasNextDescriptor != null
ResolvedCall<FunctionDescriptor> resolvedCall = context.get(BindingContext.LOOP_RANGE_HAS_NEXT_RESOLVED_CALL, rangeExpression);
assert resolvedCall != null
: message(rangeExpression, "Range expression must have a descriptor for hasNext function or property");
if (hasNextDescriptor instanceof PropertyDescriptor) {
PropertyGetterDescriptor getter = ((PropertyDescriptor) hasNextDescriptor).getGetter();
assert getter != null : "Loop range hasNext val should have a getter.";
return getter;
}
return hasNextDescriptor;
return resolvedCall;
}
@NotNull