Search in sources :

Example 6 with ValueParameterDescriptor

use of org.jetbrains.kotlin.descriptors.ValueParameterDescriptor in project kotlin by JetBrains.

the class FunctionBodyTranslator method setDefaultValueForArguments.

@NotNull
public static List<JsStatement> setDefaultValueForArguments(@NotNull FunctionDescriptor descriptor, @NotNull TranslationContext functionBodyContext) {
    List<ValueParameterDescriptor> valueParameters = descriptor.getValueParameters();
    List<JsStatement> result = new ArrayList<JsStatement>(valueParameters.size());
    for (ValueParameterDescriptor valueParameter : valueParameters) {
        if (!valueParameter.declaresDefaultValue())
            continue;
        JsNameRef jsNameRef = functionBodyContext.getNameForDescriptor(valueParameter).makeRef();
        KtExpression defaultArgument = getDefaultArgument(valueParameter);
        JsBlock defaultArgBlock = new JsBlock();
        JsExpression defaultValue = Translation.translateAsExpression(defaultArgument, functionBodyContext, defaultArgBlock);
        JsStatement assignStatement = assignment(jsNameRef, defaultValue).makeStmt();
        JsStatement thenStatement = JsAstUtils.mergeStatementInBlockIfNeeded(assignStatement, defaultArgBlock);
        JsBinaryOperation checkArgIsUndefined = equality(jsNameRef, Namer.getUndefinedExpression());
        JsIf jsIf = JsAstUtils.newJsIf(checkArgIsUndefined, thenStatement);
        result.add(jsIf);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) KtExpression(org.jetbrains.kotlin.psi.KtExpression) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with ValueParameterDescriptor

use of org.jetbrains.kotlin.descriptors.ValueParameterDescriptor in project kotlin by JetBrains.

the class CallBasedArgumentGenerator method generateExpression.

@Override
protected void generateExpression(int i, @NotNull ExpressionValueArgument argument) {
    ValueParameterDescriptor parameter = valueParameters.get(i);
    Type type = valueParameterTypes.get(i);
    ValueArgument valueArgument = argument.getValueArgument();
    assert valueArgument != null;
    KtExpression argumentExpression = valueArgument.getArgumentExpression();
    assert argumentExpression != null : valueArgument.asElement().getText();
    callGenerator.genValueAndPut(parameter, argumentExpression, type, i);
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) ValueArgument(org.jetbrains.kotlin.psi.ValueArgument) KtExpression(org.jetbrains.kotlin.psi.KtExpression) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)

Example 8 with ValueParameterDescriptor

use of org.jetbrains.kotlin.descriptors.ValueParameterDescriptor in project kotlin by JetBrains.

the class MemberMatching method valueParametersTypesMatch.

static boolean valueParametersTypesMatch(@NotNull KtNamedDeclaration declaration, @NotNull CallableDescriptor descriptor) {
    List<KtParameter> declarationParameters = getValueParameters(declaration);
    List<ValueParameterDescriptor> descriptorParameters = descriptor.getValueParameters();
    if (descriptorParameters.size() != declarationParameters.size()) {
        return false;
    }
    for (int i = 0; i < descriptorParameters.size(); i++) {
        ValueParameterDescriptor descriptorParameter = descriptorParameters.get(i);
        KtParameter declarationParameter = declarationParameters.get(i);
        KtTypeReference typeReference = declarationParameter.getTypeReference();
        if (typeReference == null) {
            return false;
        }
        KtModifierList modifierList = declarationParameter.getModifierList();
        boolean varargInDeclaration = modifierList != null && modifierList.hasModifier(KtTokens.VARARG_KEYWORD);
        boolean varargInDescriptor = descriptorParameter.getVarargElementType() != null;
        if (varargInDeclaration != varargInDescriptor) {
            return false;
        }
        String declarationTypeText = typeReference.getText();
        KotlinType typeToRender = varargInDeclaration ? descriptorParameter.getVarargElementType() : descriptorParameter.getType();
        assert typeToRender != null;
        String descriptorParameterText = DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(typeToRender);
        if (!declarationTypeText.equals(descriptorParameterText)) {
            return false;
        }
    }
    return true;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)

Aggregations

ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)8 Type (org.jetbrains.org.objectweb.asm.Type)4 KtExpression (org.jetbrains.kotlin.psi.KtExpression)3 ValueArgument (org.jetbrains.kotlin.psi.ValueArgument)3 SmartList (com.intellij.util.SmartList)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)1 ConstructorDescriptor (org.jetbrains.kotlin.descriptors.ConstructorDescriptor)1 ScriptDescriptor (org.jetbrains.kotlin.descriptors.ScriptDescriptor)1 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)1 KtSimpleNameExpression (org.jetbrains.kotlin.psi.KtSimpleNameExpression)1 JvmMethodSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1 TypeProjection (org.jetbrains.kotlin.types.TypeProjection)1 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)1 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)1