JS: minor, simplify code locating default argument values

At the only call site of getDefaultArgument it is checked that the
parameter actually _declares_ default value, so it's not necessary to
try to load that value from supertypes
This commit is contained in:
Alexander Udalov
2018-01-12 18:17:56 +01:00
parent ae2d67a5b1
commit 8b3d439d5b
2 changed files with 2 additions and 18 deletions
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt;
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant;
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.TypeUtils;
@@ -178,26 +177,12 @@ public final class BindingUtils {
@NotNull
public static KtExpression getDefaultArgument(@NotNull ValueParameterDescriptor parameterDescriptor) {
ValueParameterDescriptor descriptorWhichDeclaresDefaultValue =
getOriginalDescriptorWhichDeclaresDefaultValue(parameterDescriptor);
KtParameter psiParameter = getParameterForDescriptor(descriptorWhichDeclaresDefaultValue);
KtParameter psiParameter = getParameterForDescriptor(parameterDescriptor);
KtExpression defaultValue = psiParameter.getDefaultValue();
assert defaultValue != null : message(parameterDescriptor, "No default value found in PSI");
return defaultValue;
}
private static ValueParameterDescriptor getOriginalDescriptorWhichDeclaresDefaultValue(
@NotNull ValueParameterDescriptor parameterDescriptor
) {
ValueParameterDescriptor result = parameterDescriptor;
assert DescriptorUtilsKt.hasDefaultValue(result) : message(parameterDescriptor, "Unsupplied parameter must have default value");
// TODO: this seems incorrect, as the default value may come from _not the first_ overridden parameter
while (!result.declaresDefaultValue()) {
result = result.getOverriddenDescriptors().iterator().next();
}
return result;
}
@NotNull
public static ResolvedCall<FunctionDescriptor> getIteratorFunction(@NotNull BindingContext context,
@NotNull KtExpression rangeExpression) {
@@ -43,7 +43,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.jetbrains.kotlin.js.translate.utils.BindingUtils.getDefaultArgument;
import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.*;
import static org.jetbrains.kotlin.js.translate.utils.mutator.LastExpressionMutator.mutateLastExpression;
@@ -84,7 +83,7 @@ public final class FunctionBodyTranslator extends AbstractTranslator {
if (!valueParameter.declaresDefaultValue()) continue;
JsExpression jsNameRef = ReferenceTranslator.translateAsValueReference(valueParameter, functionBodyContext);
KtExpression defaultArgument = getDefaultArgument(valueParameter);
KtExpression defaultArgument = BindingUtils.getDefaultArgument(valueParameter);
JsBlock defaultArgBlock = new JsBlock();
JsExpression defaultValue = Translation.translateAsExpression(defaultArgument, functionBodyContext, defaultArgBlock);
PsiElement psi = KotlinSourceElementKt.getPsi(valueParameter.getSource());