Search in sources :

Example 16 with Reference

use of org.eclipse.titan.designer.AST.Reference in project titan.EclipsePlug-ins by eclipse.

the class FormalParameterList method check.

public final void check(final CompilationTimeStamp timestamp, final Assignment_type definitionType) {
    if (lastTimeChecked != null && !lastTimeChecked.isLess(timestamp)) {
        return;
    }
    minimumNofParameters = 0;
    isStartable = true;
    for (int i = 0, size = parameters.size(); i < size; i++) {
        FormalParameter parameter = parameters.get(i);
        final Identifier identifier = parameter.getIdentifier();
        if (parentScope != null) {
            if (parentScope.hasAssignmentWithId(timestamp, identifier)) {
                parameter.getLocation().reportSemanticError(MessageFormat.format(HIDINGSCOPEELEMENT, identifier.getDisplayName()));
                parentScope.hasAssignmentWithId(timestamp, identifier);
                final List<ISubReference> subReferences = new ArrayList<ISubReference>();
                subReferences.add(new FieldSubReference(identifier));
                final Reference reference = new Reference(null, subReferences);
                final Assignment assignment = parentScope.getAssBySRef(timestamp, reference);
                if (assignment != null && assignment.getLocation() != null) {
                    assignment.getLocation().reportSingularSemanticWarning(MessageFormat.format(HIDDENSCOPEELEMENT, identifier.getDisplayName()));
                }
            } else if (parentScope.isValidModuleId(identifier)) {
                parameter.getLocation().reportSemanticWarning(MessageFormat.format(HIDINGMODULEIDENTIFIER, identifier.getDisplayName()));
            }
        }
        parameter.check(timestamp);
        if (parameter.getAssignmentType() != parameter.getRealAssignmentType()) {
            parameter = parameter.setParameterType(parameter.getRealAssignmentType());
            parameters.set(i, parameter);
        }
        if (Assignment_type.A_TEMPLATE.semanticallyEquals(definitionType)) {
            if (!Assignment_type.A_PAR_VAL_IN.semanticallyEquals(parameter.getAssignmentType()) && !Assignment_type.A_PAR_TEMP_IN.semanticallyEquals(parameter.getAssignmentType())) {
                parameter.getLocation().reportSemanticError("A template cannot have " + parameter.getAssignmentName());
            }
        } else if (Assignment_type.A_TESTCASE.semanticallyEquals(definitionType)) {
            if (Assignment_type.A_PAR_TIMER.semanticallyEquals(parameter.getAssignmentType()) && Assignment_type.A_PAR_PORT.semanticallyEquals(parameter.getAssignmentType())) {
                parameter.getLocation().reportSemanticError("A testcase cannot have " + parameter.getAssignmentName());
            }
        } else {
        // everything is allowed for functions and altsteps
        }
        // startability check
        switch(parameter.getAssignmentType()) {
            case A_PAR_VAL:
            case A_PAR_VAL_IN:
            case A_PAR_VAL_OUT:
            case A_PAR_VAL_INOUT:
            case A_PAR_TEMP_IN:
            case A_PAR_TEMP_OUT:
            case A_PAR_TEMP_INOUT:
                {
                    final IType tempType = parameter.getType(timestamp);
                    if (isStartable && tempType != null && tempType.isComponentInternal(timestamp)) {
                        isStartable = false;
                    }
                    break;
                }
            default:
                isStartable = false;
        }
        if (!parameter.hasDefaultValue()) {
            minimumNofParameters = i + 1;
        }
    }
    if (parameters.size() > reportTooManyParametersSize) {
        getLocation().reportConfigurableSemanticProblem(reportTooManyParameters, MessageFormat.format(TOOMANYPARAMETERS, reportTooManyParametersSize));
    }
    checkUniqueness(timestamp);
    lastTimeChecked = timestamp;
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) Identifier(org.eclipse.titan.designer.AST.Identifier) FieldSubReference(org.eclipse.titan.designer.AST.FieldSubReference) ISubReference(org.eclipse.titan.designer.AST.ISubReference) FieldSubReference(org.eclipse.titan.designer.AST.FieldSubReference) Reference(org.eclipse.titan.designer.AST.Reference) ArrayList(java.util.ArrayList) IType(org.eclipse.titan.designer.AST.IType)

Example 17 with Reference

use of org.eclipse.titan.designer.AST.Reference in project titan.EclipsePlug-ins by eclipse.

the class AltGuards method generateCodeAlt.

/**
 * Generate code for an alt
 *
 * @param aData the structure to put imports into and get temporal variable names from.
 * @param source the source code generated
 */
public void generateCodeAlt(final JavaGenData aData, final StringBuilder source) {
    aData.addBuiltinTypeImport("TitanAlt_Status");
    boolean labelNeeded = hasRepeat;
    boolean hasElseBranch = false;
    for (int i = 0; i < altGuards.size(); i++) {
        final AltGuard altGuard = altGuards.get(i);
        switch(altGuard.getType()) {
            case AG_OP:
                if (((Operation_Altguard) altGuard).getGuardStatement().canRepeat()) {
                    labelNeeded = true;
                }
                break;
            case AG_REF:
            case AG_INVOKE:
                labelNeeded = true;
                break;
            case AG_ELSE:
                hasElseBranch = true;
                break;
            default:
                ErrorReporter.INTERNAL_ERROR("unknown altguard type encountered `" + getFullName() + "''");
                return;
        }
        if (hasElseBranch) {
            break;
        }
    }
    // if there is no [else] branch the defaults may return ALT_REPEAT
    if (!hasElseBranch) {
        labelNeeded = true;
    }
    // opening bracket of the statement block
    label = aData.getTemporaryVariableName();
    if (labelNeeded) {
        source.append(label).append(":\n");
    }
    source.append("for ( ; ; ) {\n");
    // temporary variables used for caching of status codes
    for (int i = 0; i < altGuards.size(); i++) {
        final AltGuard altGuard = altGuards.get(i);
        if (altGuard.getType().equals(altguard_type.AG_ELSE)) {
            break;
        }
        source.append(MessageFormat.format("TitanAlt_Status {0}_alt_flag_{1} = ", label, i));
        if (altGuard.getGuardExpression() == null) {
            source.append("TitanAlt_Status.ALT_MAYBE");
        } else {
            source.append("TitanAlt_Status.ALT_UNCHECKED");
        }
        source.append(";\n");
    }
    if (!hasElseBranch) {
        source.append(MessageFormat.format("TitanAlt_Status {0}_default_flag = TitanAlt_Status.ALT_MAYBE;\n", label));
    }
    // the first snapshot is taken in non-blocking mode
    aData.addCommonLibraryImport("TTCN_Snapshot");
    source.append("TTCN_Snapshot.takeNew(false);\n");
    // and opening infinite for() loop
    source.append("for ( ; ; ) {\n");
    for (int i = 0; i < altGuards.size(); i++) {
        final AltGuard altGuard = altGuards.get(i);
        final altguard_type altGuardType = altGuard.getType();
        if (altGuardType.equals(altguard_type.AG_ELSE)) {
            source.append("TTCN_Snapshot.elseBranchReached();\n");
            StatementBlock block = altGuard.getStatementBlock();
            if (block.getSize() > 0) {
                source.append("{\n");
                // FIXME handle debugger
                block.generateCode(aData, source);
                source.append("}\n");
            }
            if (block.hasReturn(CompilationTimeStamp.getBaseTimestamp()) != ReturnStatus_type.RS_YES) {
                source.append("break;\n");
            }
            // do not generate code for further branches
            break;
        } else {
            final IValue guardExpression = altGuard.getGuardExpression();
            if (guardExpression != null) {
                source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_UNCHECKED) '{'\n", label, i));
                guardExpression.getLocation().update_location_object(aData, source);
                final ExpressionStruct expression = new ExpressionStruct();
                guardExpression.generateCodeExpression(aData, expression, true);
                source.append(expression.preamble);
                source.append(MessageFormat.format("if(TitanBoolean.getNative({0})) '{'\n", expression.expression));
                source.append(MessageFormat.format("{0}_alt_flag_{1} = TitanAlt_Status.ALT_MAYBE;\n", label, i));
                source.append("} else {\n");
                source.append(MessageFormat.format("{0}_alt_flag_{1} = TitanAlt_Status.ALT_NO;\n", label, i));
                source.append("}\n");
                source.append("}\n");
            }
            source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_MAYBE) '{'\n", label, i));
            boolean canRepeat = false;
            final ExpressionStruct expression = new ExpressionStruct();
            expression.expression.append(MessageFormat.format("{0}_alt_flag_{1} = ", label, i));
            switch(altGuardType) {
                case AG_OP:
                    {
                        // the guard operation is a receiving statement
                        final Statement statement = ((Operation_Altguard) altGuard).getGuardStatement();
                        altGuard.getLocation().update_location_object(aData, source);
                        statement.generateCodeExpression(aData, expression);
                        canRepeat = statement.canRepeat();
                    }
                    break;
                case AG_REF:
                    {
                        // the guard operation is an altstep instance
                        final Reference reference = ((Referenced_Altguard) altGuard).getGuardReference();
                        altGuard.getLocation().update_location_object(aData, source);
                        final Assignment altstep = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
                        expression.expression.append(MessageFormat.format("{0}_instance(", altstep.getGenNameFromScope(aData, source, myScope, "")));
                        final ISubReference subreference = reference.getSubreferences().get(0);
                        ((ParameterisedSubReference) subreference).getActualParameters().generateCodeAlias(aData, expression);
                        source.append(')');
                        canRepeat = true;
                    }
                    break;
                case AG_INVOKE:
                    {
                        // the guard operation is an altstep invocation
                        altGuard.getLocation().update_location_object(aData, source);
                        ((Invoke_Altguard) altGuard).generateCodeInvokeInstance(aData, expression);
                        canRepeat = true;
                    }
                    break;
                default:
                    ErrorReporter.INTERNAL_ERROR("unknown altguard type encountered `" + getFullName() + "''");
                    return;
            }
            expression.mergeExpression(source);
            if (canRepeat) {
                source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_REPEAT) continue {2};\n", label, i, label));
            }
            if (altGuardType.equals(altguard_type.AG_REF) || altGuardType.equals(altguard_type.AG_INVOKE)) {
                source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_BREAK) break;\n", label, i));
            }
            // execution of statement block if the guard was successful
            source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_YES) ", label, i));
            final StatementBlock block = altGuard.getStatementBlock();
            if (block != null && block.getSize() > 0) {
                source.append("{\n");
                // TODO handle debugger
                block.generateCode(aData, source);
                if (!ReturnStatus_type.RS_YES.equals(block.hasReturn(CompilationTimeStamp.getBaseTimestamp()))) {
                    source.append("break;\n");
                }
                source.append("}\n");
            } else {
                source.append("break;\n");
            }
            source.append("}\n");
        }
    }
    if (!hasElseBranch) {
        aData.addCommonLibraryImport("TTCN_Default");
        source.append(MessageFormat.format("if ({0}_default_flag == TitanAlt_Status.ALT_MAYBE) '{'\n", label));
        source.append(MessageFormat.format("{0}_default_flag = TTCN_Default.tryAltsteps();\n", label));
        source.append(MessageFormat.format("if ({0}_default_flag == TitanAlt_Status.ALT_YES || {0}_default_flag == TitanAlt_Status.ALT_BREAK) '{'\n", label));
        source.append("break;\n");
        source.append(MessageFormat.format("} else if({0}_default_flag == TitanAlt_Status.ALT_REPEAT) '{'\n", label));
        source.append(MessageFormat.format("continue {0};\n", label));
        source.append("}\n");
        source.append("}\n");
        getLocation().update_location_object(aData, source);
        // error handling and taking the next snapshot in blocking mode
        source.append("if ( ");
        for (int i = 0; i < altGuards.size(); i++) {
            source.append(MessageFormat.format("{0}_alt_flag_{1} == TitanAlt_Status.ALT_NO &&", label, i));
        }
        source.append(MessageFormat.format("{0}_default_flag == TitanAlt_Status.ALT_NO) '{'\n", label));
        source.append("throw new TtcnError(\"None of the branches can be chosen in the alt statement");
        // TODO translate_string
        if (location != null && location.getFile() != null) {
            source.append(MessageFormat.format("in file {0} at line {1}", location.getFile().getName(), location.getLine()));
        }
        source.append("\");\n");
        source.append("}\n");
        source.append("TTCN_Snapshot.takeNew(true);\n");
    }
    source.append("}\n");
    source.append("break;\n");
    source.append("}\n");
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) ParameterisedSubReference(org.eclipse.titan.designer.AST.ParameterisedSubReference) IValue(org.eclipse.titan.designer.AST.IValue) AltGuard.altguard_type(org.eclipse.titan.designer.AST.TTCN3.statements.AltGuard.altguard_type) Reference(org.eclipse.titan.designer.AST.Reference) ISubReference(org.eclipse.titan.designer.AST.ISubReference) ParameterisedSubReference(org.eclipse.titan.designer.AST.ParameterisedSubReference) ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)

Example 18 with Reference

use of org.eclipse.titan.designer.AST.Reference in project titan.EclipsePlug-ins by eclipse.

the class AltGuards method generateCodeAltstep.

/**
 * Generate code for an altstep
 *
 * @param aData the structure to put imports into and get temporal variable names from.
 * @param source the source code generated
 */
public void generateCodeAltstep(final JavaGenData aData, final StringBuilder source) {
    final boolean hasElse = hasElse();
    if (!hasElse) {
        source.append("TitanAlt_Status returnValue = TitanAlt_Status.ALT_NO;\n");
    }
    for (int i = 0; i < altGuards.size(); i++) {
        final AltGuard altGuard = altGuards.get(i);
        final altguard_type altGuardType = altGuard.getType();
        if (altGuardType.equals(altguard_type.AG_ELSE)) {
            source.append("TTCN_Snapshot.elseBranchReached();\n");
            final StatementBlock block = altGuard.getStatementBlock();
            if (block.getSize() > 0) {
                source.append("{\n");
                // TODO debugger
                block.generateCode(aData, source);
                source.append("}\n");
            }
            if (block.hasReturn(CompilationTimeStamp.getBaseTimestamp()) != ReturnStatus_type.RS_YES) {
                source.append("return TitanAlt_Status.ALT_YES;\n");
            }
            break;
        } else {
            final AtomicInteger blockCount = new AtomicInteger(0);
            final IValue guardExpression = altGuard.getGuardExpression();
            if (guardExpression != null) {
                guardExpression.getLocation().update_location_object(aData, source);
                guardExpression.generateCodeTmp(aData, source, "if (", blockCount);
                source.append(") {\n");
                blockCount.incrementAndGet();
            }
            boolean canRepeat = false;
            final ExpressionStruct expression = new ExpressionStruct();
            switch(altGuardType) {
                case AG_OP:
                    {
                        final Statement statement = ((Operation_Altguard) altGuard).getGuardStatement();
                        altGuard.getLocation().update_location_object(aData, source);
                        statement.generateCodeExpression(aData, expression);
                        canRepeat = statement.canRepeat();
                    }
                    break;
                case AG_REF:
                    {
                        // the guard operation is an altstep instance
                        final Reference reference = ((Referenced_Altguard) altGuard).getGuardReference();
                        altGuard.getLocation().update_location_object(aData, source);
                        final Assignment altstep = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
                        expression.expression.append(MessageFormat.format("{0}_instance(", altstep.getGenNameFromScope(aData, source, myScope, "")));
                        final ISubReference subreference = reference.getSubreferences().get(0);
                        ((ParameterisedSubReference) subreference).getActualParameters().generateCodeAlias(aData, expression);
                        source.append(')');
                        canRepeat = true;
                    }
                    break;
                case AG_INVOKE:
                    {
                        // the guard operation is an altstep invocation
                        altGuard.getLocation().update_location_object(aData, source);
                        ((Invoke_Altguard) altGuard).generateCodeInvokeInstance(aData, expression);
                        canRepeat = true;
                    }
                    break;
                default:
                    ErrorReporter.INTERNAL_ERROR("unknown altguard type encountered `" + getFullName() + "''");
                    return;
            }
            if (expression.preamble.length() > 0 || expression.postamble.length() > 0) {
                if (blockCount.get() == 0) {
                    source.append("{\n");
                    blockCount.set(blockCount.get() + 1);
                }
                final String tempId = aData.getTemporaryVariableName();
                source.append(MessageFormat.format("TitanAlt_Status {0};\n", tempId));
                source.append("{\n");
                source.append(expression.preamble);
                source.append(MessageFormat.format("{0}.assign({1});\n", tempId, expression.expression));
                source.append(expression.postamble);
                source.append("}\n");
                source.append(MessageFormat.format("switch ({0}) '{'\n", tempId));
            } else {
                source.append(MessageFormat.format("switch ({0}) '{'\n", expression.expression));
            }
            source.append("case ALT_YES:\n");
            final StatementBlock block = altGuard.getStatementBlock();
            if (block != null && block.getSize() > 0) {
                source.append("{\n");
                // TODO handle debugger
                block.generateCode(aData, source);
                source.append("}\n");
            }
            if (block == null || block.hasReturn(CompilationTimeStamp.getBaseTimestamp()) != ReturnStatus_type.RS_YES) {
                source.append("return TitanAlt_Status.ALT_YES;\n");
            }
            if (canRepeat) {
                source.append("case ALT_REPEAT:\n");
                source.append("return TitanAlt_Status.ALT_REPEAT;\n");
            }
            if (altGuardType == altguard_type.AG_REF || altGuardType == altguard_type.AG_INVOKE) {
                source.append("case ALT_BREAK:\n");
                source.append("return TitanAlt_Status.ALT_BREAK;\n");
            }
            if (!hasElse) {
                source.append("case ALT_MAYBE:\n");
                source.append("returnValue = TitanAlt_Status.ALT_MAYBE;\n");
            }
            source.append("default:\n");
            source.append("break;\n");
            source.append("}\n");
            // closing statement blocks
            for (int j = 0; j < blockCount.get(); j++) {
                source.append("}\n");
            }
        }
    }
    if (!hasElse) {
        source.append("return returnValue;\n");
    }
}
Also used : AltGuard.altguard_type(org.eclipse.titan.designer.AST.TTCN3.statements.AltGuard.altguard_type) Reference(org.eclipse.titan.designer.AST.Reference) ISubReference(org.eclipse.titan.designer.AST.ISubReference) ParameterisedSubReference(org.eclipse.titan.designer.AST.ParameterisedSubReference) Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) ParameterisedSubReference(org.eclipse.titan.designer.AST.ParameterisedSubReference) IValue(org.eclipse.titan.designer.AST.IValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)

Example 19 with Reference

use of org.eclipse.titan.designer.AST.Reference in project titan.EclipsePlug-ins by eclipse.

the class Assignment_Statement method checkVarAssignment.

private void checkVarAssignment(final CompilationTimeStamp timestamp, final Assignment assignment, final IValue value) {
    final IType varType = getType(timestamp, assignment);
    if (varType == null || value == null) {
        isErroneous = true;
        return;
    }
    final IType type = varType.getFieldType(timestamp, reference, 1, Expected_Value_type.EXPECTED_DYNAMIC_VALUE, false);
    if (type == null) {
        isErroneous = true;
        return;
    }
    value.setMyGovernor(type);
    IValue lastValue = type.checkThisValueRef(timestamp, value);
    final IReferenceChain referenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
    lastValue = lastValue.getValueRefdLast(timestamp, referenceChain);
    referenceChain.release();
    if (Value_type.OMIT_VALUE.equals(lastValue.getValuetype())) {
        final ISubReference lastReference = reference.removeLastSubReference();
        if (lastReference == null || lastReference.getId() == null) {
            value.getLocation().reportSemanticError(OMITTOMANDATORYASSIGNMENT1);
            isErroneous = true;
            reference.addSubReference(lastReference);
            return;
        }
        final Identifier lastField = lastReference.getId();
        final List<ISubReference> baseReference = reference.getSubreferences(0, reference.getSubreferences().size() - 1);
        reference.addSubReference(lastReference);
        final Reference newReference = new TemporalReference(null, baseReference);
        newReference.clearStringElementReferencing();
        IType baseType = varType.getFieldType(timestamp, newReference, 1, Expected_Value_type.EXPECTED_DYNAMIC_VALUE, false);
        if (baseType == null) {
            isErroneous = true;
            return;
        }
        baseType = baseType.getTypeRefdLast(timestamp);
        if (baseType.getIsErroneous(timestamp)) {
            isErroneous = true;
            return;
        }
        CompField componentField;
        switch(baseType.getTypetype()) {
            case TYPE_TTCN3_SEQUENCE:
                componentField = ((TTCN3_Sequence_Type) baseType).getComponentByName(lastField.getName());
                if (componentField != null && !componentField.isOptional()) {
                    value.getLocation().reportSemanticError(MessageFormat.format(OMITTOMANDATORYASSIGNMENT2, lastField.getDisplayName(), baseType.getTypename()));
                    value.setIsErroneous(true);
                }
                break;
            case TYPE_ASN1_SEQUENCE:
                componentField = ((ASN1_Sequence_Type) baseType).getComponentByName(lastField);
                if (componentField != null && !componentField.isOptional()) {
                    value.getLocation().reportSemanticError(MessageFormat.format(OMITTOMANDATORYASSIGNMENT2, lastField.getDisplayName(), baseType.getTypename()));
                    value.setIsErroneous(true);
                }
                break;
            case TYPE_TTCN3_SET:
                componentField = ((TTCN3_Set_Type) baseType).getComponentByName(lastField.getName());
                if (componentField != null && !componentField.isOptional()) {
                    value.getLocation().reportSemanticError(MessageFormat.format(OMITTOMANDATORYASSIGNMENT2, lastField.getDisplayName(), baseType.getTypename()));
                    value.setIsErroneous(true);
                }
                break;
            case TYPE_ASN1_SET:
                componentField = ((ASN1_Set_Type) baseType).getComponentByName(lastField);
                if (componentField != null && !componentField.isOptional()) {
                    value.getLocation().reportSemanticError(MessageFormat.format(OMITTOMANDATORYASSIGNMENT2, lastField.getDisplayName(), baseType.getTypename()));
                    value.setIsErroneous(true);
                }
                break;
            default:
                // TODO:check this!!!
                value.getLocation().reportSemanticError(OMITTOMANDATORYASSIGNMENT1);
                value.setIsErroneous(true);
                isErroneous = true;
                break;
        }
    } else {
        final boolean isStringElement = reference.refersToStringElement();
        selfReference = type.checkThisValue(timestamp, value, assignment, new ValueCheckingOptions(Expected_Value_type.EXPECTED_DYNAMIC_VALUE, true, false, !isStringElement, false, isStringElement));
        if (isStringElement) {
            // The length of the right hand side should be 1
            final IType lastType = type.getTypeRefdLast(timestamp);
            int stringLength = 1;
            switch(lastType.getTypetype()) {
                case TYPE_BITSTRING:
                case TYPE_BITSTRING_A:
                    if (!Value_type.BITSTRING_VALUE.equals(lastValue.getValuetype())) {
                        return;
                    }
                    stringLength = ((Bitstring_Value) lastValue).getValueLength();
                    break;
                case TYPE_HEXSTRING:
                    if (!Value_type.HEXSTRING_VALUE.equals(lastValue.getValuetype())) {
                        lastValue = null;
                    } else {
                        stringLength = ((Hexstring_Value) lastValue).getValueLength();
                    }
                    break;
                case TYPE_OCTETSTRING:
                    if (!Value_type.OCTETSTRING_VALUE.equals(lastValue.getValuetype())) {
                        return;
                    }
                    stringLength = ((Octetstring_Value) lastValue).getValueLength();
                    break;
                case TYPE_CHARSTRING:
                case TYPE_NUMERICSTRING:
                case TYPE_PRINTABLESTRING:
                case TYPE_IA5STRING:
                case TYPE_VISIBLESTRING:
                case TYPE_UTCTIME:
                case TYPE_GENERALIZEDTIME:
                    if (!Value_type.CHARSTRING_VALUE.equals(lastValue.getValuetype())) {
                        return;
                    }
                    stringLength = ((Charstring_Value) lastValue).getValueLength();
                    break;
                case TYPE_UCHARSTRING:
                case TYPE_UTF8STRING:
                case TYPE_TELETEXSTRING:
                case TYPE_VIDEOTEXSTRING:
                case TYPE_GRAPHICSTRING:
                case TYPE_GENERALSTRING:
                case TYPE_UNIVERSALSTRING:
                case TYPE_BMPSTRING:
                case TYPE_OBJECTDESCRIPTOR:
                    if (Value_type.UNIVERSALCHARSTRING_VALUE.equals(lastValue.getValuetype())) {
                        stringLength = ((UniversalCharstring_Value) lastValue).getValueLength();
                    } else if (Value_type.CHARSTRING_VALUE.equals(lastValue.getValuetype())) {
                        stringLength = ((Charstring_Value) lastValue).getValueLength();
                    } else {
                        return;
                    }
                    break;
                default:
                    lastValue = null;
                    return;
            }
            if (stringLength != 1) {
                final String message = MessageFormat.format("The length of the string to be assigned to a string element of type `{0}'' should be 1 instead of {1}", type.getTypename(), stringLength);
                value.getLocation().reportSemanticError(message);
                value.setIsErroneous(true);
            }
        }
    }
}
Also used : ISubReference(org.eclipse.titan.designer.AST.ISubReference) Reference(org.eclipse.titan.designer.AST.Reference) TemporalReference(org.eclipse.titan.designer.AST.TemporalReference) IType(org.eclipse.titan.designer.AST.IType) ISubReference(org.eclipse.titan.designer.AST.ISubReference) IValue(org.eclipse.titan.designer.AST.IValue) Identifier(org.eclipse.titan.designer.AST.Identifier) TemporalReference(org.eclipse.titan.designer.AST.TemporalReference) CompField(org.eclipse.titan.designer.AST.TTCN3.types.CompField) Charstring_Value(org.eclipse.titan.designer.AST.TTCN3.values.Charstring_Value) UniversalCharstring_Value(org.eclipse.titan.designer.AST.TTCN3.values.UniversalCharstring_Value) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain) ValueCheckingOptions(org.eclipse.titan.designer.AST.IType.ValueCheckingOptions)

Example 20 with Reference

use of org.eclipse.titan.designer.AST.Reference in project titan.EclipsePlug-ins by eclipse.

the class Assignment_Statement method generateCode.

@Override
public /**
 * {@inheritDoc}
 */
void generateCode(final JavaGenData aData, final StringBuilder source) {
    if (reference == null || template == null) {
        return;
    }
    final boolean rhsCopied = selfReference;
    // TODO this is actually much more complicated
    final Assignment assignment = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), true);
    if (assignment == null) {
        // TODO: handle null
        return;
    }
    boolean isValue;
    switch(assignment.getAssignmentType()) {
        case A_PAR_VAL_IN:
        case A_PAR_VAL_OUT:
        case A_PAR_VAL_INOUT:
        case A_PAR_VAL:
        case A_VAR:
            isValue = true;
            break;
        default:
            isValue = false;
            break;
    }
    boolean isOptional = false;
    if (assignment.getType(CompilationTimeStamp.getBaseTimestamp()).fieldIsOptional(reference.getSubreferences())) {
        isOptional = true;
    }
    if (isValue) {
        final String rhsCopy = aData.getTemporaryVariableName();
        String rhsRef = rhsCopy;
        if (rhsCopied) {
            source.append("{\n");
            if (isOptional) {
                source.append(MessageFormat.format("Optional<{0}> {1} = new Optional<{0}>({0}.class);\n", template.getMyGovernor().getGenNameValue(aData, source, myScope), rhsCopy));
                rhsRef += ".get()";
            } else {
                source.append(MessageFormat.format("{0} {1} = new {0}();\n", template.getMyGovernor().getGenNameValue(aData, source, myScope), rhsCopy));
            }
        }
        final IValue value = template.getValue();
        // TODO handle needs_conv
        if (reference.getSubreferences().size() > 1) {
            if (value.canGenerateSingleExpression()) {
                final ExpressionStruct expression = new ExpressionStruct();
                reference.generateCode(aData, expression);
                source.append(expression.preamble);
                String temp;
                IType type = getType(CompilationTimeStamp.getBaseTimestamp(), assignment);
                type = type.getFieldType(CompilationTimeStamp.getBaseTimestamp(), reference, 1, Expected_Value_type.EXPECTED_TEMPLATE, false);
                // if (value.getValuetype() != Value_type.OMIT_VALUE && (isOptional || type.getTypetypeTtcn3() != value.getExpressionReturntype(CompilationTimeStamp.getBaseTimestamp(), Expected_Value_type.EXPECTED_TEMPLATE))) {
                if (value.getValuetype() != Value_type.OMIT_VALUE && value.getValuetype() != Value_type.REFERENCED_VALUE && (isOptional || type.getTypetypeTtcn3() != value.getExpressionReturntype(CompilationTimeStamp.getBaseTimestamp(), Expected_Value_type.EXPECTED_TEMPLATE))) {
                    temp = MessageFormat.format("new {0}({1})", value.getMyGovernor().getGenNameValue(aData, source, myScope), value.generateSingleExpression(aData));
                } else {
                    temp = value.generateSingleExpression(aData).toString();
                }
                if (rhsCopied) {
                    source.append(MessageFormat.format("{0}.assign({1});\n", rhsCopy, temp));
                    expression.expression.append(MessageFormat.format(".assign({0});\n", rhsCopy));
                } else {
                    expression.expression.append(MessageFormat.format(".assign({0});\n", temp));
                }
                source.append(expression.expression);
                source.append(expression.postamble);
            } else {
                final String tempID = aData.getTemporaryVariableName();
                final String typeGenname = value.getMyGovernor().getGenNameValue(aData, source, myScope);
                final ExpressionStruct leftExpression = new ExpressionStruct();
                reference.generateCode(aData, leftExpression);
                if (rhsCopied) {
                    // TODO handle needs conversion case
                    value.generateCodeInit(aData, source, rhsRef);
                } else if (isOptional) {
                    leftExpression.expression.append(".get()");
                }
                source.append("{\n");
                source.append(leftExpression.preamble);
                if (reference.refersToStringElement()) {
                    // LHS is a string element
                    aData.addBuiltinTypeImport(typeGenname + "_Element");
                    source.append(MessageFormat.format("{0}_Element {1} = {2};\n", typeGenname, tempID, leftExpression.expression));
                } else {
                    source.append(MessageFormat.format("{0} {1} = {2};\n", typeGenname, tempID, leftExpression.expression));
                }
                source.append(leftExpression.postamble);
                if (rhsCopied) {
                    source.append(MessageFormat.format("{0}.assign({1});\n", tempID, rhsCopy));
                } else {
                    // TODO handle needs conversion
                    value.generateCodeInit(aData, source, tempID);
                }
                source.append("}\n");
            }
        } else {
            // left hand side is a single assignment
            final String name = assignment.getGenNameFromScope(aData, source, myScope, null);
            if (!isOptional && value.getValuetype() == Value_type.REFERENCED_VALUE) {
                final Reference rightReference = ((Referenced_Value) value).getReference();
                final Assignment rightAssignment = rightReference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), true);
                if (rightAssignment.getType(CompilationTimeStamp.getBaseTimestamp()).fieldIsOptional(rightReference.getSubreferences())) {
                    value.generateCodeInitMandatory(aData, source, rhsCopied ? rhsCopy : name);
                } else {
                    value.generateCodeInit(aData, source, rhsCopied ? rhsCopy : name);
                }
            } else {
                value.generateCodeInit(aData, source, rhsCopied ? rhsCopy : name);
            }
            if (rhsCopied) {
                source.append(MessageFormat.format("{0}.assign({1});\n", name, rhsCopy));
            }
        }
        if (rhsCopied) {
            source.append("}\n");
        }
    } else {
        final String rhsCopy = aData.getTemporaryVariableName();
        if (rhsCopied) {
            source.append("{\n");
            source.append(MessageFormat.format("{0} {1} = new {0}();\n", template.getMyGovernor().getGenNameTemplate(aData, source, myScope), rhsCopy));
        }
        // TODO handle needs_conv
        if (reference.getSubreferences().size() > 1) {
            if ((templateRestriction != Restriction_type.TR_NONE || !generateRestrictionCheck) && template.hasSingleExpression()) {
                final ExpressionStruct expression = new ExpressionStruct();
                reference.generateCode(aData, expression);
                source.append(expression.preamble);
                if (rhsCopied) {
                    source.append(MessageFormat.format("{0}.assign({1});\n", rhsCopy, template.getSingleExpression(aData, false)));
                    expression.expression.append(MessageFormat.format(".assign({0});\n", rhsCopy));
                } else {
                    expression.expression.append(MessageFormat.format(".assign({0});\n", template.getSingleExpression(aData, false)));
                }
                expression.mergeExpression(source);
            } else {
                final String tempID = aData.getTemporaryVariableName();
                final ExpressionStruct expression = new ExpressionStruct();
                reference.generateCode(aData, expression);
                if (rhsCopied) {
                    // TODO handle needs conversion case
                    template.generateCodeInit(aData, source, rhsCopy);
                }
                source.append("{\n");
                source.append(expression.preamble);
                final IType governor = template.getMyGovernor();
                source.append(MessageFormat.format("{0} {1} = {2};\n", governor.getGenNameTemplate(aData, source, template.getMyScope()), tempID, expression.expression));
                source.append(expression.postamble);
                if (rhsCopied) {
                    source.append(MessageFormat.format("{0}.assign({1});\n", tempID, rhsCopy));
                } else {
                    // TODO handle needs conversion case
                    if (Type_type.TYPE_SEQUENCE_OF.equals(governor.getTypetype()) || Type_type.TYPE_ARRAY.equals(governor.getTypetype())) {
                        source.append(MessageFormat.format("{0}.removeAllPermutations();\n", tempID));
                    }
                    template.generateCodeInit(aData, source, tempID);
                }
                if (templateRestriction != Restriction_type.TR_NONE && generateRestrictionCheck) {
                    TemplateRestriction.generateRestrictionCheckCode(aData, source, location, tempID, templateRestriction);
                }
                source.append("}\n");
            }
        } else {
            // left hand side is a single assignment
            final String rhsName = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false).getGenNameFromScope(aData, source, myScope, "");
            final IType governor = template.getMyGovernor();
            if (Type_type.TYPE_SEQUENCE_OF.equals(governor.getTypetype()) || Type_type.TYPE_ARRAY.equals(governor.getTypetype())) {
                source.append(MessageFormat.format("{0}.removeAllPermutations();\n", rhsCopied ? rhsCopy : rhsName));
            }
            template.generateCodeInit(aData, source, rhsCopied ? rhsCopy : rhsName);
            if (rhsCopied) {
                source.append(MessageFormat.format("{0}.assign({1});\n", rhsName, rhsCopy));
            }
            if (templateRestriction != Restriction_type.TR_NONE && generateRestrictionCheck) {
                TemplateRestriction.generateRestrictionCheckCode(aData, source, location, rhsName, templateRestriction);
            }
        }
        if (rhsCopied) {
            source.append("}\n");
        }
    }
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) IValue(org.eclipse.titan.designer.AST.IValue) ISubReference(org.eclipse.titan.designer.AST.ISubReference) Reference(org.eclipse.titan.designer.AST.Reference) TemporalReference(org.eclipse.titan.designer.AST.TemporalReference) ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct) Referenced_Value(org.eclipse.titan.designer.AST.TTCN3.values.Referenced_Value) IType(org.eclipse.titan.designer.AST.IType)

Aggregations

Reference (org.eclipse.titan.designer.AST.Reference)88 Assignment (org.eclipse.titan.designer.AST.Assignment)48 ISubReference (org.eclipse.titan.designer.AST.ISubReference)37 IValue (org.eclipse.titan.designer.AST.IValue)26 FieldSubReference (org.eclipse.titan.designer.AST.FieldSubReference)23 IType (org.eclipse.titan.designer.AST.IType)23 Identifier (org.eclipse.titan.designer.AST.Identifier)22 ArrayList (java.util.ArrayList)19 Module (org.eclipse.titan.designer.AST.Module)16 ParameterisedSubReference (org.eclipse.titan.designer.AST.ParameterisedSubReference)15 Referenced_Value (org.eclipse.titan.designer.AST.TTCN3.values.Referenced_Value)13 IFile (org.eclipse.core.resources.IFile)11 Location (org.eclipse.titan.designer.AST.Location)10 ExpressionStruct (org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)10 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)9 ITTCN3Template (org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template)9 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)9 ArraySubReference (org.eclipse.titan.designer.AST.ArraySubReference)8 Type (org.eclipse.titan.designer.AST.Type)8 BadLocationException (org.eclipse.jface.text.BadLocationException)7