Search in sources :

Example 46 with ExpressionStruct

use of org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct in project titan.EclipsePlug-ins by eclipse.

the class ComplementedList_Template method generateCodeInit.

@Override
public /**
 * {@inheritDoc}
 */
void generateCodeInit(final JavaGenData aData, final StringBuilder source, final String name) {
    if (lastTimeBuilt != null && !lastTimeBuilt.isLess(aData.getBuildTimstamp())) {
        return;
    }
    lastTimeBuilt = aData.getBuildTimstamp();
    aData.addBuiltinTypeImport("Base_Template.template_sel");
    final ArrayList<Integer> variables = new ArrayList<Integer>();
    long fixedPart = 0;
    for (int i = 0; i < templates.getNofTemplates(); i++) {
        final TTCN3Template templateListItem = templates.getTemplateByIndex(i);
        if (templateListItem.getTemplatetype() == Template_type.ALL_FROM) {
            variables.add(i);
        } else {
            fixedPart++;
        }
    }
    final String typeName = myGovernor.getGenNameTemplate(aData, source, myScope);
    if (variables.size() > 0) {
        final StringBuilder preamble = new StringBuilder();
        final StringBuilder setType = new StringBuilder();
        final StringBuilder[] variableReferences = new StringBuilder[templates.getNofTemplates()];
        setType.append(MessageFormat.format("{0}.setType(template_sel.COMPLEMENTED_LIST, {1}", name, fixedPart));
        for (int v = 0; v < variables.size(); v++) {
            TTCN3Template template = templates.getTemplateByIndex(variables.get(v));
            // the template must be all from
            if (template instanceof All_From_Template) {
                template = ((All_From_Template) template).getAllFrom();
            }
            final Reference reference = ((SpecificValue_Template) template).getReference();
            final Assignment assignment = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
            setType.append(" + ");
            final ExpressionStruct expression = new ExpressionStruct();
            reference.generateCode(aData, expression);
            if (expression.preamble.length() > 0) {
                preamble.append(expression.preamble);
            }
            switch(assignment.getAssignmentType()) {
                case A_CONST:
                case A_EXT_CONST:
                case A_MODULEPAR:
                case A_VAR:
                case A_PAR_VAL:
                case A_PAR_VAL_IN:
                case A_PAR_VAL_OUT:
                case A_PAR_VAL_INOUT:
                case A_FUNCTION_RVAL:
                case A_EXT_FUNCTION_RVAL:
                    if (assignment.getType(CompilationTimeStamp.getBaseTimestamp()).fieldIsOptional(reference.getSubreferences())) {
                        setType.append(".get()");
                    }
                    break;
                default:
                    break;
            }
            variableReferences[variables.get(v)] = expression.expression;
            setType.append(expression.expression);
            setType.append(".n_elem().getInt()");
        }
        source.append(preamble);
        source.append(setType);
        source.append(");\n");
        final StringBuilder shifty = new StringBuilder();
        for (int i = 0; i < templates.getNofTemplates(); i++) {
            final TTCN3Template template = templates.getTemplateByIndex(i);
            switch(template.getTemplatetype()) {
                case ALL_FROM:
                    {
                        // the template must be all from
                        final StringBuilder storedExpression = variableReferences[i];
                        source.append(MessageFormat.format("for (int i_i = 0, i_lim = {0}.n_elem().getInt(); i_i < i_lim; ++i_i ) '{'\n", storedExpression));
                        String embeddedName = MessageFormat.format("{0}.listItem({1}{2} + i_i)", name, i, shifty);
                        ((All_From_Template) template).generateCodeInitAllFrom(aData, source, embeddedName, storedExpression);
                        source.append("}\n");
                        shifty.append(MessageFormat.format("-1 + {0}.n_elem().getInt()", storedExpression));
                        break;
                    }
                default:
                    if (template.needsTemporaryReference()) {
                        final String tempId = aData.getTemporaryVariableName();
                        source.append("{\n");
                        source.append(MessageFormat.format("{0} {1} = {2}.listItem({3}{4});\n", typeName, tempId, name, i, shifty));
                        template.generateCodeInit(aData, source, tempId);
                        source.append("}\n");
                    } else {
                        final String embeddedName = MessageFormat.format("{0}.listItem({1}{2})", name, i, shifty);
                        template.generateCodeInit(aData, source, embeddedName);
                    }
                    break;
            }
        }
    } else {
        source.append(MessageFormat.format("{0}.setType(template_sel.COMPLEMENTED_LIST, {1});\n", name, templates.getNofTemplates()));
        for (int i = 0; i < templates.getNofTemplates(); i++) {
            final TTCN3Template template = templates.getTemplateByIndex(i);
            if (template.needsTemporaryReference()) {
                final String tempId = aData.getTemporaryVariableName();
                source.append("{\n");
                source.append(MessageFormat.format("{0} {1} = {2}.listItem({3});\n", typeName, tempId, name, i));
                template.generateCodeInit(aData, source, tempId);
                source.append("}\n");
            } else {
                final String embeddedName = MessageFormat.format("{0}.listItem({1})", name, i);
                template.generateCodeInit(aData, source, embeddedName);
            }
        }
    }
    if (lengthRestriction != null) {
        if (getCodeSection() == CodeSectionType.CS_POST_INIT) {
            lengthRestriction.reArrangeInitCode(aData, source, myScope.getModuleScope());
        }
        lengthRestriction.generateCodeInit(aData, source, name);
    }
    if (isIfpresent) {
        source.append(name);
        source.append(".set_ifPresent();\n");
    }
}
Also used : Reference(org.eclipse.titan.designer.AST.Reference) ArrayList(java.util.ArrayList) Assignment(org.eclipse.titan.designer.AST.Assignment) ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)

Example 47 with ExpressionStruct

use of org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct in project titan.EclipsePlug-ins by eclipse.

the class Timeout_Statement method generateCode.

@Override
public /**
 * {@inheritDoc}
 */
void generateCode(final JavaGenData aData, final StringBuilder source) {
    final ExpressionStruct expression = new ExpressionStruct();
    generateCodeExpression(aData, expression);
    PortGenerator.generateCodeStandalone(aData, source, expression.expression.toString(), getLocation());
}
Also used : ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)

Example 48 with ExpressionStruct

use of org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct in project titan.EclipsePlug-ins by eclipse.

the class Open_Type method generateCodeIsPresentBoundChosen.

@Override
public /**
 * {@inheritDoc}
 */
void generateCodeIsPresentBoundChosen(final JavaGenData aData, final ExpressionStruct expression, final List<ISubReference> subreferences, final int subReferenceIndex, final String globalId, final String externalId, final boolean isTemplate, final Operation_type optype, String field) {
    if (subreferences == null || getIsErroneous(CompilationTimeStamp.getBaseTimestamp())) {
        return;
    }
    if (subReferenceIndex >= subreferences.size()) {
        return;
    }
    final StringBuilder closingBrackets = new StringBuilder();
    if (isTemplate) {
        boolean anyvalueReturnValue = true;
        if (optype == Operation_type.ISPRESENT_OPERATION) {
            anyvalueReturnValue = isPresentAnyvalueEmbeddedField(expression, subreferences, subReferenceIndex);
        } else if (optype == Operation_type.ISCHOOSEN_OPERATION) {
            anyvalueReturnValue = false;
        }
        expression.expression.append(MessageFormat.format("if({0}) '{'\n", globalId));
        expression.expression.append(MessageFormat.format("switch({0}.getSelection()) '{'\n", externalId));
        expression.expression.append("case UNINITIALIZED_TEMPLATE:\n");
        expression.expression.append(MessageFormat.format("{0} = false;\n", globalId));
        expression.expression.append("break;\n");
        expression.expression.append("case ANY_VALUE:\n");
        expression.expression.append(MessageFormat.format("{0} = {1};\n", globalId, anyvalueReturnValue ? "true" : "false"));
        expression.expression.append("break;\n");
        expression.expression.append("case SPECIFIC_VALUE:{\n");
        closingBrackets.append("break;}\n");
        closingBrackets.append("default:\n");
        closingBrackets.append(MessageFormat.format("{0} = false;\n", globalId));
        closingBrackets.append("break;\n");
        closingBrackets.append("}\n");
        closingBrackets.append("}\n");
    }
    final ISubReference subReference = subreferences.get(subReferenceIndex);
    if (!(subReference instanceof FieldSubReference)) {
        ErrorReporter.INTERNAL_ERROR("Code generator reached erroneous type reference `" + getFullName() + "''");
        expression.expression.append("FATAL_ERROR encountered");
        return;
    }
    final Identifier fieldId = ((FieldSubReference) subReference).getId();
    expression.expression.append(MessageFormat.format("if({0}) '{'\n", globalId));
    expression.expression.append(MessageFormat.format("{0} = {1}.isChosen({2}.union_selection_type.ALT_{3});\n", globalId, externalId, getGenNameValue(aData, expression.expression, myScope), FieldSubReference.getJavaGetterName(fieldId.getName())));
    expression.expression.append("}\n");
    final CompField compField = getComponentByName(fieldId);
    final Type nextType = compField.getType();
    expression.expression.append(MessageFormat.format("if({0}) '{'\n", globalId));
    closingBrackets.insert(0, "}\n");
    final String temporalId = aData.getTemporaryVariableName();
    final String temporalId2 = aData.getTemporaryVariableName();
    expression.expression.append(MessageFormat.format("{0}{1} {2} = new {0}{1}({3});\n", getGenNameValue(aData, expression.expression, myScope), isTemplate ? "_template" : "", temporalId, externalId));
    expression.expression.append(MessageFormat.format("{0}{1} {2} = {3}.get{4}();\n", nextType.getGenNameValue(aData, expression.expression, myScope), isTemplate ? "_template" : "", temporalId2, temporalId, FieldSubReference.getJavaGetterName(fieldId.getName())));
    if (optype == Operation_type.ISBOUND_OPERATION) {
        expression.expression.append(MessageFormat.format("{0} = {1}.isBound();\n", globalId, temporalId2));
    } else if (optype == Operation_type.ISPRESENT_OPERATION) {
        expression.expression.append(MessageFormat.format("{0} = {1}.isPresent({2});\n", globalId, temporalId2, isTemplate && aData.getAllowOmitInValueList() ? "true" : ""));
    } else if (optype == Operation_type.ISCHOOSEN_OPERATION) {
        expression.expression.append(MessageFormat.format("{0} = {1}.isBound();\n", globalId, temporalId2));
        if (subReferenceIndex == subreferences.size() - 1) {
            expression.expression.append(MessageFormat.format("if ({0}) '{'\n", globalId));
            expression.expression.append(MessageFormat.format("{0} = {1}.isChosen({2});\n", globalId, temporalId2, field));
            expression.expression.append("}\n");
        }
    }
    nextType.generateCodeIsPresentBoundChosen(aData, expression, subreferences, subReferenceIndex + 1, globalId, temporalId2, isTemplate, optype, field);
    expression.expression.append(closingBrackets);
}
Also used : ISubReference(org.eclipse.titan.designer.AST.ISubReference) IASN1Type(org.eclipse.titan.designer.AST.ASN1.IASN1Type) Type(org.eclipse.titan.designer.AST.Type) ASN1Type(org.eclipse.titan.designer.AST.ASN1.ASN1Type) IType(org.eclipse.titan.designer.AST.IType) Identifier(org.eclipse.titan.designer.AST.Identifier) FieldSubReference(org.eclipse.titan.designer.AST.FieldSubReference) CompField(org.eclipse.titan.designer.AST.TTCN3.types.CompField)

Example 49 with ExpressionStruct

use of org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct in project titan.EclipsePlug-ins by eclipse.

the class Reference method generateCodeIsPresentBoundChosen.

/**
 * Generates code for checking if the reference
 * and the referred objects are bound or not.
 *
 * generate_code_ispresentbound in the compiler
 *
 * @param aData only used to update imports if needed
 * @param expression the expression for code generated
 * @param isTemplate if the reference is pointing to a template
 * @param optype true to generate code for isbound, false otherwise
 */
public void generateCodeIsPresentBoundChosen(final JavaGenData aData, final ExpressionStruct expression, final boolean isTemplate, final Operation_type optype, final String field) {
    final Assignment assignment = getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
    final String ass_id = assignment.getGenNameFromScope(aData, expression.expression, myScope, null);
    String ass_id2 = ass_id;
    FormalParameterList formalParameterList;
    switch(assignment.getAssignmentType()) {
        case A_FUNCTION:
        case A_FUNCTION_RVAL:
        case A_FUNCTION_RTEMP:
            formalParameterList = ((Def_Function) assignment).getFormalParameterList();
            break;
        case A_EXT_FUNCTION:
        case A_EXT_FUNCTION_RVAL:
        case A_EXT_FUNCTION_RTEMP:
            formalParameterList = ((Def_Extfunction) assignment).getFormalParameterList();
            break;
        case A_TEMPLATE:
            formalParameterList = ((Def_Template) assignment).getFormalParameterList();
            break;
        default:
            formalParameterList = null;
            break;
    }
    if (subReferences.size() > 0 && subReferences.get(0) instanceof ParameterisedSubReference) {
        final ParameterisedSubReference subReference = (ParameterisedSubReference) subReferences.get(0);
        final ExpressionStruct tempExpression = new ExpressionStruct();
        // FIXME should need the formal parameters and other
        // options
        subReference.getActualParameters().generateCodeAlias(aData, tempExpression);
        if (tempExpression.preamble.length() > 0) {
            expression.preamble.append(tempExpression.preamble);
        }
        ass_id2 = MessageFormat.format("{0}({1})", ass_id, tempExpression.expression);
    } else if (formalParameterList != null) {
        // the reference does not have an actual parameter list,
        // but the assignment has
        final ExpressionStruct tempExpression = new ExpressionStruct();
        final StringBuilder newId = new StringBuilder();
        newId.append(assignment.getGenNameFromScope(aData, tempExpression.expression, getMyScope(), null));
        newId.append("( ");
        // ((FieldSubReference)subReferences.get(0));
        for (int i = 0; i < formalParameterList.getNofParameters(); i++) {
            if (i > 0) {
                tempExpression.expression.append(", ");
            }
            formalParameterList.getParameterByIndex(i).getDefaultValue().generateCode(aData, tempExpression);
        }
        // expression);
        if (tempExpression.preamble.length() > 0) {
            expression.preamble.append(tempExpression.preamble);
        }
        newId.append(tempExpression.expression);
        newId.append(" )");
        ass_id2 = newId.toString();
    }
    if (subReferences.size() > 1) {
        final String tempGeneralId = aData.getTemporaryVariableName();
        final ExpressionStruct isboundExpression = new ExpressionStruct();
        isboundExpression.preamble.append(MessageFormat.format("boolean {0} = {1}.isBound();\n", tempGeneralId, ass_id2));
        final IType type = assignment.getType(CompilationTimeStamp.getBaseTimestamp());
        type.generateCodeIsPresentBoundChosen(aData, isboundExpression, subReferences, 1, tempGeneralId, ass_id2, isTemplate, optype, field);
        expression.preamble.append(isboundExpression.preamble);
        expression.preamble.append(isboundExpression.expression);
        expression.expression.append(tempGeneralId);
    } else {
        switch(optype) {
            case ISBOUND_OPERATION:
                expression.expression.append(MessageFormat.format("{0}.isBound()", ass_id2));
                break;
            case ISPRESENT_OPERATION:
                expression.expression.append(MessageFormat.format("{0}.isPresent({1})", ass_id2, isTemplate && aData.getAllowOmitInValueList() ? "true" : ""));
                break;
            case ISCHOOSEN_OPERATION:
                expression.expression.append(MessageFormat.format("{0}.isChosen({1})", ass_id2, field));
                break;
            default:
                ErrorReporter.INTERNAL_ERROR("FATAL ERROR while generating code for reference `" + getFullName() + "''");
                break;
        }
    }
}
Also used : IParameterisedAssignment(org.eclipse.titan.designer.AST.TTCN3.definitions.IParameterisedAssignment) ASN1Assignment(org.eclipse.titan.designer.AST.ASN1.ASN1Assignment) FormalParameterList(org.eclipse.titan.designer.AST.TTCN3.definitions.FormalParameterList) ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)

Example 50 with ExpressionStruct

use of org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct in project titan.EclipsePlug-ins by eclipse.

the class Reference method generateConstRef.

public void generateConstRef(final JavaGenData aData, final ExpressionStruct expression) {
    if (referredAssignment == null) {
        // TODO: handle null
        return;
    }
    boolean isTemplate;
    switch(referredAssignment.getAssignmentType()) {
        case A_CONST:
        case A_EXT_CONST:
        case A_MODULEPAR:
        case A_VAR:
        case A_FUNCTION_RVAL:
        case A_EXT_FUNCTION_RVAL:
        case A_PAR_VAL:
        case A_PAR_VAL_IN:
        case A_PAR_VAL_OUT:
        case A_PAR_VAL_INOUT:
            isTemplate = false;
            break;
        case A_MODULEPAR_TEMPLATE:
        case A_TEMPLATE:
        case A_VAR_TEMPLATE:
        case A_PAR_TEMP_IN:
        case A_PAR_TEMP_OUT:
        case A_PAR_TEMP_INOUT:
            isTemplate = true;
            break;
        default:
            isTemplate = false;
            break;
    }
    FormalParameterList formalParameterList;
    switch(referredAssignment.getAssignmentType()) {
        case A_FUNCTION:
        case A_FUNCTION_RVAL:
        case A_FUNCTION_RTEMP:
            formalParameterList = ((Def_Function) referredAssignment).getFormalParameterList();
            break;
        case A_EXT_FUNCTION:
        case A_EXT_FUNCTION_RVAL:
        case A_EXT_FUNCTION_RTEMP:
            formalParameterList = ((Def_Extfunction) referredAssignment).getFormalParameterList();
            break;
        case A_TEMPLATE:
            formalParameterList = ((Def_Template) referredAssignment).getFormalParameterList();
            break;
        default:
            formalParameterList = null;
            break;
    }
    final IType referedGovernor = referredAssignment.getType(CompilationTimeStamp.getBaseTimestamp());
    // ha parameterezett
    if (subReferences.get(0) instanceof ParameterisedSubReference) {
        expression.expression.append(referredAssignment.getGenNameFromScope(aData, expression.expression, getMyScope(), null));
        expression.expression.append("( ");
        final ParameterisedSubReference temp = ((ParameterisedSubReference) subReferences.get(0));
        final ActualParameterList actualParameterList = temp.getActualParameters();
        if (actualParameterList != null) {
            actualParameterList.generateCodeAlias(aData, expression);
        }
        expression.expression.append(" )");
    } else if (formalParameterList != null) {
        // the reference does not have an actual parameter list, but the assignment has
        expression.expression.append(referredAssignment.getGenNameFromScope(aData, expression.expression, getMyScope(), null));
        expression.expression.append("( ");
        // FieldSubReference temp = ((FieldSubReference)subReferences.get(0));
        for (int i = 0; i < formalParameterList.getNofParameters(); i++) {
            if (i > 0) {
                expression.expression.append(", ");
            }
            formalParameterList.getParameterByIndex(i).getDefaultValue().generateCode(aData, expression);
        }
        // temp.getActualParameters().generateCodeAlias(aData, expression);
        expression.expression.append(" )");
    } else {
        // TODO add fuzzy handling
        expression.expression.append(referredAssignment.getGenNameFromScope(aData, expression.expression, getMyScope(), null));
    }
    generateCode(aData, expression, isTemplate, true, referedGovernor);
}
Also used : FormalParameterList(org.eclipse.titan.designer.AST.TTCN3.definitions.FormalParameterList) ActualParameterList(org.eclipse.titan.designer.AST.TTCN3.definitions.ActualParameterList)

Aggregations

ExpressionStruct (org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)79 IValue (org.eclipse.titan.designer.AST.IValue)38 IType (org.eclipse.titan.designer.AST.IType)22 Assignment (org.eclipse.titan.designer.AST.Assignment)21 ISubReference (org.eclipse.titan.designer.AST.ISubReference)19 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)17 Reference (org.eclipse.titan.designer.AST.Reference)15 Identifier (org.eclipse.titan.designer.AST.Identifier)10 TTCN3Template (org.eclipse.titan.designer.AST.TTCN3.templates.TTCN3Template)9 ArrayList (java.util.ArrayList)8 FieldSubReference (org.eclipse.titan.designer.AST.FieldSubReference)8 SpecificValue_Template (org.eclipse.titan.designer.AST.TTCN3.templates.SpecificValue_Template)8 ParameterisedSubReference (org.eclipse.titan.designer.AST.ParameterisedSubReference)7 Array_Type (org.eclipse.titan.designer.AST.TTCN3.types.Array_Type)7 Integer_Value (org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value)7 Type (org.eclipse.titan.designer.AST.Type)7 ITTCN3Template (org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template)6 CompField (org.eclipse.titan.designer.AST.TTCN3.types.CompField)6 Referenced_Value (org.eclipse.titan.designer.AST.TTCN3.values.Referenced_Value)6 ASN1_Choice_Type (org.eclipse.titan.designer.AST.ASN1.types.ASN1_Choice_Type)4