Search in sources :

Example 11 with Array_Type

use of org.eclipse.titan.designer.AST.TTCN3.types.Array_Type in project titan.EclipsePlug-ins by eclipse.

the class Array_Type method generateCodeValue.

/**
 * Generate the value class to represent an array.
 * (Also generates the value classes of the of type if it is an array)
 *
 * @param aData only used to update imports if needed
 * @param source where the source code should be generated
 */
public void generateCodeValue(final JavaGenData aData, final StringBuilder source) {
    final String className = getGenNameValue(aData, source, myScope);
    final IType elementType = getElementType();
    final String ofType = elementType.getGenNameValue(aData, source, getMyScope());
    if (elementType.getTypetype() == Type_type.TYPE_ARRAY) {
        ((Array_Type) elementType).generateCodeValue(aData, source);
    }
    final ArrayDimension dim = getDimension();
    aData.addBuiltinTypeImport("TitanValueArray");
    source.append(MessageFormat.format("public static class {0} extends TitanValueArray<{1}> '{'\n", className, ofType));
    source.append(MessageFormat.format("public {0}() '{'\n", className));
    source.append(MessageFormat.format("super({0}.class, {1} , {2});\n", ofType, dim.getSize(), dim.getOffset()));
    source.append("}\n");
    source.append(MessageFormat.format("public {0}({0} otherValue) '{'\n", className));
    source.append("super(otherValue);\n");
    source.append("}\n");
    source.append(MessageFormat.format("public {0}(final TitanValueArray<{1}> otherValue) '{'\n", className, ofType));
    source.append("super(otherValue);\n");
    source.append("}\n");
    source.append("}\n\n");
}
Also used : ArrayDimension(org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension) IType(org.eclipse.titan.designer.AST.IType)

Example 12 with Array_Type

use of org.eclipse.titan.designer.AST.TTCN3.types.Array_Type in project titan.EclipsePlug-ins by eclipse.

the class SubsetMatch_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");
    String ofTypeName;
    switch(myGovernor.getTypetype()) {
        case TYPE_SEQUENCE_OF:
            ofTypeName = ((SequenceOf_Type) myGovernor).getOfType().getGenNameTemplate(aData, source, myScope);
            break;
        case TYPE_SET_OF:
            ofTypeName = ((SetOf_Type) myGovernor).getOfType().getGenNameTemplate(aData, source, myScope);
            break;
        case TYPE_ARRAY:
            ofTypeName = ((Array_Type) myGovernor).getElementType().getGenNameTemplate(aData, source, myScope);
            break;
        default:
            ErrorReporter.INTERNAL_ERROR("FATAL ERROR while processing subset match template `" + getFullName() + "''");
            return;
    }
    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++;
        }
    }
    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.SUBSET_MATCH, {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())) {
                        expression.expression.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));
                        final String embeddedName = MessageFormat.format("{0}.setItem({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}.setItem({3}{4});\n", ofTypeName, tempId, name, i, shifty));
                        template.generateCodeInit(aData, source, tempId);
                        source.append("}\n");
                    } else {
                        final String embeddedName = MessageFormat.format("{0}.setItem({1}{2})", name, i, shifty);
                        template.generateCodeInit(aData, source, embeddedName);
                    }
                    break;
            }
        }
    } else {
        source.append(MessageFormat.format("{0}.setType(template_sel.SUBSET_MATCH, {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}.setItem({3});\n", ofTypeName, tempId, name, i));
                template.generateCodeInit(aData, source, tempId);
                source.append("}\n");
            } else {
                final String embeddedName = MessageFormat.format("{0}.setItem({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) SetOf_Type(org.eclipse.titan.designer.AST.TTCN3.types.SetOf_Type) ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct) Array_Type(org.eclipse.titan.designer.AST.TTCN3.types.Array_Type) SequenceOf_Type(org.eclipse.titan.designer.AST.TTCN3.types.SequenceOf_Type)

Example 13 with Array_Type

use of org.eclipse.titan.designer.AST.TTCN3.types.Array_Type in project titan.EclipsePlug-ins by eclipse.

the class TTCN3Template method getReferencedArrayTemplate.

/**
 * Checks whether array indexing is allowed for a given template sub
 * reference or not.
 *
 * @param timestamp
 *                the time stamp of the actual semantic check cycle.
 * @param arrayIndex
 *                the index to check.
 * @param referenceChain
 *                the reference chain use to detect circular references.
 */
protected ITTCN3Template getReferencedArrayTemplate(final CompilationTimeStamp timestamp, final IValue arrayIndex, final IReferenceChain referenceChain) {
    switch(getTemplatetype()) {
        case OMIT_VALUE:
        case ANY_VALUE:
        case ANY_OR_OMIT:
        case VALUE_LIST:
        case COMPLEMENTED_LIST:
        case SUPERSET_MATCH:
        case SUBSET_MATCH:
            arrayIndex.getLocation().reportSemanticError(MessageFormat.format("Reference with index to an element of {0} `{1}''", getTemplateTypeName(), getFullName()));
            break;
        default:
            break;
    }
    IValue indexValue = arrayIndex.setLoweridToReference(timestamp);
    indexValue = indexValue.getValueRefdLast(timestamp, referenceChain);
    if (indexValue.getIsErroneous(timestamp)) {
        return null;
    }
    long index = 0;
    if (!indexValue.isUnfoldable(timestamp)) {
        if (Value_type.INTEGER_VALUE.equals(indexValue.getValuetype())) {
            index = ((Integer_Value) indexValue).getValue();
        } else {
            arrayIndex.getLocation().reportSemanticError("An integer value was expected as index");
            return null;
        }
    } else {
        return null;
    }
    final IType tempType = myGovernor.getTypeRefdLast(timestamp);
    if (tempType.getIsErroneous(timestamp)) {
        return null;
    }
    switch(tempType.getTypetype()) {
        case TYPE_SEQUENCE_OF:
            if (index < 0) {
                final String message = MessageFormat.format("A non-negative integer value was expected instead of {0} for indexing a template of `sequence of'' type `{1}''", index, tempType.getTypename());
                arrayIndex.getLocation().reportSemanticError(message);
                return null;
            } else if (!Template_type.TEMPLATE_LIST.equals(getTemplatetype())) {
                return null;
            } else {
                final int nofElements = ((Template_List) this).getNofTemplates();
                if (index > nofElements) {
                    final String message = MessageFormat.format("Index overflow in a template of `sequence of'' type `{0}'': the index is {1}, but the template has only {2} elements", tempType.getTypename(), index, nofElements);
                    arrayIndex.getLocation().reportSemanticError(message);
                    return null;
                }
            }
            break;
        case TYPE_SET_OF:
            if (index < 0) {
                final String message = MessageFormat.format("A non-negative integer value was expected instead of {0} for indexing a template of `set of'' type `{1}''", index, tempType.getTypename());
                arrayIndex.getLocation().reportSemanticError(message);
                return null;
            } else if (!Template_type.TEMPLATE_LIST.equals(getTemplatetype())) {
                return null;
            } else {
                final int nofElements = ((Template_List) this).getNofTemplates();
                if (index > nofElements) {
                    final String message = MessageFormat.format("Index overflow in a template of `set of'' type `{0}'': the index is {1}, but the template has only {2} elements", tempType.getTypename(), index, nofElements);
                    arrayIndex.getLocation().reportSemanticError(message);
                    return null;
                }
            }
            break;
        case TYPE_ARRAY:
            {
                final ArrayDimension dimension = ((Array_Type) tempType).getDimension();
                dimension.checkIndex(timestamp, indexValue, Expected_Value_type.EXPECTED_DYNAMIC_VALUE);
                if (Template_type.TEMPLATE_LIST.equals(getTemplatetype()) && !dimension.getIsErroneous(timestamp)) {
                    // re-base the index
                    index -= dimension.getOffset();
                    if (index < 0 || index > ((Template_List) this).getNofTemplates()) {
                        arrayIndex.getLocation().reportSemanticError(MessageFormat.format("The index value {0} is outside the array indexable range", index + dimension.getOffset()));
                        return null;
                    }
                } else {
                    return null;
                }
                break;
            }
        default:
            {
                final String message = MessageFormat.format("Invalid array element reference: type `{0}'' cannot be indexed", tempType.getTypename());
                arrayIndex.getLocation().reportSemanticError(message);
                return null;
            }
    }
    if (this instanceof Template_List) {
        final TTCN3Template returnValue = ((Template_List) this).getTemplateByIndex((int) index);
        if (Template_type.TEMPLATE_NOTUSED.equals(returnValue.getTemplatetype())) {
            if (baseTemplate != null) {
                return baseTemplate.getTemplateReferencedLast(timestamp, referenceChain).getReferencedArrayTemplate(timestamp, indexValue, referenceChain);
            }
            return null;
        }
        return returnValue;
    } else {
        return null;
    }
}
Also used : IValue(org.eclipse.titan.designer.AST.IValue) ArrayDimension(org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension) IType(org.eclipse.titan.designer.AST.IType)

Example 14 with Array_Type

use of org.eclipse.titan.designer.AST.TTCN3.types.Array_Type in project titan.EclipsePlug-ins by eclipse.

the class Template_List method setGenNameRecursive.

@Override
public /**
 * {@inheritDoc}
 */
void setGenNameRecursive(final String parameterGenName) {
    super.setGenNameRecursive(parameterGenName);
    if (converted != null) {
        converted.setGenNameRecursive(parameterGenName);
        return;
    }
    if (myGovernor == null) {
        return;
    }
    final IType type = myGovernor.getTypeRefdLast(CompilationTimeStamp.getBaseTimestamp());
    long offset = 0;
    if (Type_type.TYPE_ARRAY.equals(type.getTypetype())) {
        offset = ((Array_Type) type).getDimension().getOffset();
    }
    for (int i = 0; i < templates.getNofTemplates(); i++) {
        final StringBuilder embeddedName = new StringBuilder(parameterGenName);
        embeddedName.append('[').append(offset + i).append(']');
        templates.getTemplateByIndex(i).setGenNameRecursive(embeddedName.toString());
    }
}
Also used : Array_Type(org.eclipse.titan.designer.AST.TTCN3.types.Array_Type) IType(org.eclipse.titan.designer.AST.IType)

Example 15 with Array_Type

use of org.eclipse.titan.designer.AST.TTCN3.types.Array_Type in project titan.EclipsePlug-ins by eclipse.

the class Template_List method getReferencedArrayTemplate.

@Override
protected /**
 * {@inheritDoc}
 */
ITTCN3Template getReferencedArrayTemplate(final CompilationTimeStamp timestamp, final IValue arrayIndex, final IReferenceChain referenceChain) {
    IValue indexValue = arrayIndex.setLoweridToReference(timestamp);
    indexValue = indexValue.getValueRefdLast(timestamp, referenceChain);
    if (indexValue.getIsErroneous(timestamp)) {
        return null;
    }
    long index = 0;
    if (!indexValue.isUnfoldable(timestamp)) {
        if (Value_type.INTEGER_VALUE.equals(indexValue.getValuetype())) {
            index = ((Integer_Value) indexValue).getValue();
        } else {
            arrayIndex.getLocation().reportSemanticError("An integer value was expected as index");
            return null;
        }
    } else {
        return null;
    }
    final IType tempType = myGovernor.getTypeRefdLast(timestamp);
    if (tempType.getIsErroneous(timestamp)) {
        return null;
    }
    switch(tempType.getTypetype()) {
        case TYPE_SEQUENCE_OF:
            {
                if (index < 0) {
                    final String message = MessageFormat.format("A non-negative integer value was expected instead of {0} for indexing a template of `sequence of'' type `{1}''", index, tempType.getTypename());
                    arrayIndex.getLocation().reportSemanticError(message);
                    return null;
                }
                final int nofElements = getNofTemplates();
                if (!(index < nofElements)) {
                    final String message = MessageFormat.format("Index overflow in a template of `sequence of'' type `{0}'': the index is {1}, but the template has only {2} elements", tempType.getTypename(), index, nofElements);
                    arrayIndex.getLocation().reportSemanticError(message);
                    return null;
                }
                break;
            }
        case TYPE_SET_OF:
            {
                if (index < 0) {
                    final String message = MessageFormat.format("A non-negative integer value was expected instead of {0} for indexing a template of `set of'' type `{1}''", index, tempType.getTypename());
                    arrayIndex.getLocation().reportSemanticError(message);
                    return null;
                }
                final int nofElements = getNofTemplates();
                if (!(index < nofElements)) {
                    final String message = MessageFormat.format("Index overflow in a template of `set of'' type `{0}'': the index is {1}, but the template has only {2} elements", tempType.getTypename(), index, nofElements);
                    arrayIndex.getLocation().reportSemanticError(message);
                    return null;
                }
                break;
            }
        case TYPE_ARRAY:
            {
                final ArrayDimension dimension = ((Array_Type) tempType).getDimension();
                dimension.checkIndex(timestamp, indexValue, Expected_Value_type.EXPECTED_DYNAMIC_VALUE);
                if (!dimension.getIsErroneous(timestamp)) {
                    // re-base the index
                    index -= dimension.getOffset();
                    if (index < 0 || !(index < getNofTemplates())) {
                        arrayIndex.getLocation().reportSemanticError(MessageFormat.format("The index value {0} is outside the array indexable range", index + dimension.getOffset()));
                        return null;
                    }
                } else {
                    return null;
                }
                break;
            }
        default:
            {
                final String message = MessageFormat.format("Invalid array element reference: type `{0}'' cannot be indexed", tempType.getTypename());
                arrayIndex.getLocation().reportSemanticError(message);
                return null;
            }
    }
    final ITTCN3Template returnValue = getTemplateByIndex((int) index);
    if (Template_type.TEMPLATE_NOTUSED.equals(returnValue.getTemplatetype())) {
        if (baseTemplate != null) {
            return baseTemplate.getTemplateReferencedLast(timestamp, referenceChain).getReferencedArrayTemplate(timestamp, indexValue, referenceChain);
        }
        return null;
    }
    return returnValue;
}
Also used : IValue(org.eclipse.titan.designer.AST.IValue) ArrayDimension(org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension) IType(org.eclipse.titan.designer.AST.IType)

Aggregations

Array_Type (org.eclipse.titan.designer.AST.TTCN3.types.Array_Type)19 IType (org.eclipse.titan.designer.AST.IType)13 IValue (org.eclipse.titan.designer.AST.IValue)10 Assignment (org.eclipse.titan.designer.AST.Assignment)6 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)6 ArrayDimension (org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension)6 Integer_Value (org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value)6 Reference (org.eclipse.titan.designer.AST.Reference)5 SequenceOf_Type (org.eclipse.titan.designer.AST.TTCN3.types.SequenceOf_Type)5 ArrayList (java.util.ArrayList)4 ISubReference (org.eclipse.titan.designer.AST.ISubReference)4 SetOf_Type (org.eclipse.titan.designer.AST.TTCN3.types.SetOf_Type)4 ExpressionStruct (org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)4 ArraySubReference (org.eclipse.titan.designer.AST.ArraySubReference)3 CompField (org.eclipse.titan.designer.AST.TTCN3.types.CompField)3 ASN1_Choice_Type (org.eclipse.titan.designer.AST.ASN1.types.ASN1_Choice_Type)2 ASN1_Sequence_Type (org.eclipse.titan.designer.AST.ASN1.types.ASN1_Sequence_Type)2 ASN1_Set_Type (org.eclipse.titan.designer.AST.ASN1.types.ASN1_Set_Type)2 FieldSubReference (org.eclipse.titan.designer.AST.FieldSubReference)2 Identifier (org.eclipse.titan.designer.AST.Identifier)2