Search in sources :

Example 1 with Parameter

use of org.yakindu.base.types.Parameter in project statecharts by Yakindu.

the class ExpressionsTypeInferrer method validateParameters.

/**
 * Takes the operation parameter type and performs a lookup for all contained
 * type parameters by using the given type parameter inference map.<br>
 * The parameter types are validated against the operation call's argument
 * types.
 *
 * @throws TypeParameterInferrenceException
 */
public Map<TypeParameter, InferenceResult> validateParameters(Map<TypeParameter, InferenceResult> typeParameterMapping, Operation operation, List<Expression> args, IValidationIssueAcceptor acceptor) {
    List<Parameter> parameters = operation.getParameters();
    for (int i = 0; i < parameters.size(); i++) {
        if (args.size() > i) {
            Parameter parameter = parameters.get(i);
            Expression argument = args.get(i);
            InferenceResult parameterType = inferTypeDispatch(parameter);
            InferenceResult argumentType = inferTypeDispatch(argument);
            parameterType = typeParameterInferrer.buildInferenceResult(parameterType, typeParameterMapping, acceptor);
            assertAssignable(parameterType, argumentType, String.format(INCOMPATIBLE_TYPES, argumentType, parameterType));
        }
    }
    if (operation.isVariadic() && args.size() - 1 >= operation.getVarArgIndex()) {
        Parameter parameter = operation.getParameters().get(operation.getVarArgIndex());
        List<Expression> varArgs = args.subList(operation.getVarArgIndex(), args.size() - 1);
        InferenceResult parameterType = inferTypeDispatch(parameter);
        for (Expression expression : varArgs) {
            parameterType = typeParameterInferrer.buildInferenceResult(parameterType, typeParameterMapping, acceptor);
            InferenceResult argumentType = inferTypeDispatch(expression);
            assertAssignable(parameterType, argumentType, String.format(INCOMPATIBLE_TYPES, argumentType, parameterType));
        }
    }
    return typeParameterMapping;
}
Also used : ConditionalExpression(org.yakindu.base.expressions.expressions.ConditionalExpression) LogicalRelationExpression(org.yakindu.base.expressions.expressions.LogicalRelationExpression) LogicalAndExpression(org.yakindu.base.expressions.expressions.LogicalAndExpression) AssignmentExpression(org.yakindu.base.expressions.expressions.AssignmentExpression) ArgumentExpression(org.yakindu.base.expressions.expressions.ArgumentExpression) LogicalOrExpression(org.yakindu.base.expressions.expressions.LogicalOrExpression) NumericalMultiplyDivideExpression(org.yakindu.base.expressions.expressions.NumericalMultiplyDivideExpression) ShiftExpression(org.yakindu.base.expressions.expressions.ShiftExpression) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) PostFixUnaryExpression(org.yakindu.base.expressions.expressions.PostFixUnaryExpression) BitwiseAndExpression(org.yakindu.base.expressions.expressions.BitwiseAndExpression) BitwiseXorExpression(org.yakindu.base.expressions.expressions.BitwiseXorExpression) TypeCastExpression(org.yakindu.base.expressions.expressions.TypeCastExpression) Expression(org.yakindu.base.expressions.expressions.Expression) ParenthesizedExpression(org.yakindu.base.expressions.expressions.ParenthesizedExpression) NumericalAddSubtractExpression(org.yakindu.base.expressions.expressions.NumericalAddSubtractExpression) LogicalNotExpression(org.yakindu.base.expressions.expressions.LogicalNotExpression) NumericalUnaryExpression(org.yakindu.base.expressions.expressions.NumericalUnaryExpression) PrimitiveValueExpression(org.yakindu.base.expressions.expressions.PrimitiveValueExpression) BitwiseOrExpression(org.yakindu.base.expressions.expressions.BitwiseOrExpression) Parameter(org.yakindu.base.types.Parameter) TypeParameter(org.yakindu.base.types.TypeParameter)

Example 2 with Parameter

use of org.yakindu.base.types.Parameter in project statecharts by Yakindu.

the class ArgumentImpl method setParameter.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public void setParameter(Parameter newParameter) {
    Parameter oldParameter = parameter;
    parameter = newParameter;
    if (eNotificationRequired())
        eNotify(new ENotificationImpl(this, Notification.SET, ExpressionsPackage.ARGUMENT__PARAMETER, oldParameter, parameter));
}
Also used : ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl) Parameter(org.yakindu.base.types.Parameter)

Example 3 with Parameter

use of org.yakindu.base.types.Parameter in project statecharts by Yakindu.

the class ExpressionsJavaValidator method checkDuplicateParameterAssignment.

@Check
public void checkDuplicateParameterAssignment(ArgumentExpression exp) {
    Set<Parameter> assignedParameters = new HashSet<>();
    EList<Argument> arguments = exp.getArguments();
    for (Argument argument : arguments) {
        if (argument.getParameter() != null) {
            if (assignedParameters.contains(argument.getParameter())) {
                error(String.format(ERROR_DUPLICATE_PARAMETER_ASSIGNMENT_MSG, argument.getParameter().getName()), argument, null, ERROR_DUPLICATE_PARAMETER_ASSIGNMENT_CODE);
                break;
            }
            assignedParameters.add(argument.getParameter());
        }
    }
}
Also used : Argument(org.yakindu.base.expressions.expressions.Argument) Parameter(org.yakindu.base.types.Parameter) TypeParameter(org.yakindu.base.types.TypeParameter) HashSet(java.util.HashSet) Check(org.eclipse.xtext.validation.Check)

Example 4 with Parameter

use of org.yakindu.base.types.Parameter in project statecharts by Yakindu.

the class OperationItemProvider method getText.

/**
 * @generated NOT
 */
@Override
public String getText(Object object) {
    Operation operation = (Operation) object;
    if (operation.getName() == null) {
        return "null";
    }
    StringBuilder builder = new StringBuilder(operation.getName());
    builder.append("(");
    EList<Parameter> parameters = operation.getParameters();
    String sep = "";
    for (Parameter parameter : parameters) {
        builder.append(sep);
        builder.append(parameter.getName());
        builder.append(" : ");
        String typeName = parameter.getType().getName();
        builder.append(typeName);
        sep = ", ";
    }
    builder.append(")");
    if (operation.getType() != null) {
        builder.append(" : ");
        String name = operation.getType().getName();
        builder.append(name == null ? "void" : name);
    }
    return builder.toString();
}
Also used : Parameter(org.yakindu.base.types.Parameter) Operation(org.yakindu.base.types.Operation)

Example 5 with Parameter

use of org.yakindu.base.types.Parameter in project statecharts by Yakindu.

the class TypeInferrerTest method testOperationCallWithOptionalParameter.

@Test
public void testOperationCallWithOptionalParameter() {
    OperationDefinition opDef = StextTestFactory._createOperation("opWithOptionals", StextFactory.eINSTANCE.createInternalScope());
    Parameter pReq = typesFactory.createParameter("pReq", ITypeSystem.INTEGER, false);
    Parameter pOpt = typesFactory.createParameter("pOpt", ITypeSystem.INTEGER, true);
    opDef.getParameters().add(pReq);
    opDef.getParameters().add(pOpt);
    Argument boolArg = (Argument) parseExpression("true", Argument.class.getSimpleName());
    Argument intArg = (Argument) parseExpression("17", Argument.class.getSimpleName());
    // opWithOptionals(17, 17) => valid
    ElementReferenceExpression opCall1 = StextTestFactory._createOperationCall(opDef, intArg, intArg);
    expectNoErrors(opCall1);
    // opWithOptionals(17) => valid, because of optional parameter
    ElementReferenceExpression opCall2 = StextTestFactory._createOperationCall(opDef, intArg);
    expectNoErrors(opCall2);
    // opWithOptionals(true) => invalid
    ElementReferenceExpression opCall3 = StextTestFactory._createOperationCall(opDef, boolArg);
    expectError(opCall3, ITypeSystemInferrer.NOT_COMPATIBLE_CODE);
}
Also used : Argument(org.yakindu.base.expressions.expressions.Argument) Parameter(org.yakindu.base.types.Parameter) OperationDefinition(org.yakindu.sct.model.stext.stext.OperationDefinition) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) Test(org.junit.Test) AbstractTypeInferrerTest(org.yakindu.sct.model.stext.test.util.AbstractTypeInferrerTest)

Aggregations

Parameter (org.yakindu.base.types.Parameter)7 Argument (org.yakindu.base.expressions.expressions.Argument)3 TypeParameter (org.yakindu.base.types.TypeParameter)3 ElementReferenceExpression (org.yakindu.base.expressions.expressions.ElementReferenceExpression)2 Expression (org.yakindu.base.expressions.expressions.Expression)2 HashSet (java.util.HashSet)1 BasicEList (org.eclipse.emf.common.util.BasicEList)1 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)1 INode (org.eclipse.xtext.nodemodel.INode)1 Check (org.eclipse.xtext.validation.Check)1 Test (org.junit.Test)1 ArgumentExpression (org.yakindu.base.expressions.expressions.ArgumentExpression)1 AssignmentExpression (org.yakindu.base.expressions.expressions.AssignmentExpression)1 BitwiseAndExpression (org.yakindu.base.expressions.expressions.BitwiseAndExpression)1 BitwiseOrExpression (org.yakindu.base.expressions.expressions.BitwiseOrExpression)1 BitwiseXorExpression (org.yakindu.base.expressions.expressions.BitwiseXorExpression)1 ConditionalExpression (org.yakindu.base.expressions.expressions.ConditionalExpression)1 LogicalAndExpression (org.yakindu.base.expressions.expressions.LogicalAndExpression)1 LogicalNotExpression (org.yakindu.base.expressions.expressions.LogicalNotExpression)1 LogicalOrExpression (org.yakindu.base.expressions.expressions.LogicalOrExpression)1