Search in sources :

Example 6 with ArrayDimension

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

the class SingleLenghtRestriction method checkArraySize.

@Override
public /**
 * {@inheritDoc}
 */
void checkArraySize(final CompilationTimeStamp timestamp, final ArrayDimension dimension) {
    if (lastTimeChecked == null || dimension.getIsErroneous(timestamp) || value == null) {
        return;
    }
    boolean errorFlag = false;
    final long arraySize = dimension.getSize();
    final IReferenceChain chain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
    final IValue last = value.getValueRefdLast(timestamp, chain);
    chain.release();
    if (Value_type.INTEGER_VALUE.equals(last.getValuetype()) && !last.getIsErroneous(timestamp)) {
        final BigInteger length = ((Integer_Value) last).getValueValue();
        final int compareResult = length.compareTo(BigInteger.valueOf(arraySize));
        if (compareResult != 0) {
            final String message = MessageFormat.format("There number of elements allowed by the length restriction ({0}) contradicts the array size ({1})", length, arraySize);
            value.getLocation().reportSemanticError(message);
            errorFlag = true;
        }
    }
    if (!errorFlag) {
        getLocation().reportSemanticWarning("Length restriction is useless for an array template");
    }
}
Also used : IValue(org.eclipse.titan.designer.AST.IValue) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) BigInteger(java.math.BigInteger)

Example 7 with ArrayDimension

use of org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension 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 8 with ArrayDimension

use of org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension 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 9 with ArrayDimension

use of org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension 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)

Example 10 with ArrayDimension

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

the class Def_Timer method generateCodeArrayDuration.

private void generateCodeArrayDuration(final JavaGenData aData, final StringBuilder source, final String genName, final ArrayList<String> classNames, final Value defaultDuration2, final int startDim) {
    final ArrayDimension dim = dimensions.get(startDim);
    final int dim_size = (int) dim.getSize();
    final IReferenceChain referenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
    final Value v = (Value) defaultDuration2.getValueRefdLast(CompilationTimeStamp.getBaseTimestamp(), referenceChain);
    referenceChain.release();
    if (v.getValuetype() != Value_type.SEQUENCEOF_VALUE) {
        // ErrorReporter.INTERNAL_ERROR()
        return;
    }
    final SequenceOf_Value value = (SequenceOf_Value) v;
    if (value.getNofComponents() != dim_size && !value.isIndexed()) {
        ErrorReporter.INTERNAL_ERROR("Code generator reached erroneous definition `" + getFullName() + "''");
        return;
    }
    // Value-list notation.
    if (!value.isIndexed()) {
        if (startDim + 1 < dimensions.size()) {
            // boolean temp_ref_needed = dimensions.get(startDim + 1).getSize() > 1;
            for (int i = 0; i < dim_size; i++) {
                // get_comp_byIndex(i);
                final IValue v_elem = value.getValueByIndex(i);
                if (v_elem.getValuetype() == Value_type.NOTUSED_VALUE) {
                    continue;
                }
                final String embeddedName = MessageFormat.format("{0}.getAt({1})", genName, i + dim.getOffset());
                generateCodeArrayDuration(aData, source, embeddedName, classNames, (Value) v_elem, startDim + 1);
            }
        } else {
            // We are in the last dimension, the elements of "value" are floats.
            for (int i = 0; i < dim_size; i++) {
                final IValue v_elem = value.getValueByIndex(i);
                if (v_elem.getValuetype() == Value_type.NOTUSED_VALUE) {
                    continue;
                }
                final ExpressionStruct expression = new ExpressionStruct();
                expression.expression.append(genName);
                expression.expression.append(".getAt(").append(i + dim.getOffset()).append(")");
                // originally set_default_duration(obj_name, i)
                expression.expression.append(".assign(");
                v_elem.generateCodeExpression(aData, expression, true);
                expression.expression.append(')');
                expression.mergeExpression(source);
            }
        }
    // Indexed-list notation.
    } else {
        if (startDim + 1 < dimensions.size()) {
            // boolean temp_ref_needed = dimensions.get(startDim + 1).getSize() > 1;
            for (int i = 0; i < value.getNofComponents(); ++i) {
                final IValue v_elem = value.getValueByIndex(i);
                final IValue index = value.getIndexByIndex(i);
                if (v_elem.getValuetype() == Value_type.NOTUSED_VALUE) {
                    continue;
                }
                final String tempId1 = aData.getTemporaryVariableName();
                final String tempIdX = aData.getTemporaryVariableName();
                source.append("{\n");
                source.append("TitanInteger " + tempIdX + " = new TitanInteger();\n");
                index.generateCodeInit(aData, source, tempIdX);
                source.append(MessageFormat.format("{0} {1} = {2}.getAt({3});\n", classNames.get(classNames.size() - startDim - 2), tempId1, genName, tempIdX));
                generateCodeArrayDuration(aData, source, tempId1, classNames, (Value) v_elem, startDim + 1);
                source.append("}\n");
            }
        } else {
            for (int i = 0; i < value.getNofComponents(); ++i) {
                final IValue v_elem = value.getValueByIndex(i);
                final IValue v_elemIndex = value.getIndexByIndex(i);
                if (v_elem.getValuetype() == Value_type.NOTUSED_VALUE) {
                    continue;
                }
                final ExpressionStruct expression = new ExpressionStruct();
                final String tempIdX = aData.getTemporaryVariableName();
                source.append("{\n");
                source.append("TitanInteger " + tempIdX + " = new TitanInteger();\n");
                v_elemIndex.generateCodeInit(aData, source, tempIdX);
                final String embeddedName = MessageFormat.format("{0}.getAt(", genName);
                expression.expression.append(embeddedName).append(tempIdX).append(")");
                // originally set_default_duration(obj_name, i)
                expression.expression.append(".assign(");
                v_elem.generateCodeExpression(aData, expression, true);
                expression.expression.append(')');
                expression.mergeExpression(source);
                source.append("}\n");
            }
        }
    }
    return;
}
Also used : IValue(org.eclipse.titan.designer.AST.IValue) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain) Value(org.eclipse.titan.designer.AST.Value) SequenceOf_Value(org.eclipse.titan.designer.AST.TTCN3.values.SequenceOf_Value) Real_Value(org.eclipse.titan.designer.AST.TTCN3.values.Real_Value) IValue(org.eclipse.titan.designer.AST.IValue) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) SequenceOf_Value(org.eclipse.titan.designer.AST.TTCN3.values.SequenceOf_Value) ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct) ArrayDimension(org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension)

Aggregations

ArrayDimension (org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension)10 IValue (org.eclipse.titan.designer.AST.IValue)8 IType (org.eclipse.titan.designer.AST.IType)6 Integer_Value (org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value)6 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)5 BigInteger (java.math.BigInteger)3 Value (org.eclipse.titan.designer.AST.Value)3 Real_Value (org.eclipse.titan.designer.AST.TTCN3.values.Real_Value)2 SequenceOf_Value (org.eclipse.titan.designer.AST.TTCN3.values.SequenceOf_Value)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ArraySubReference (org.eclipse.titan.designer.AST.ArraySubReference)1 ISubReference (org.eclipse.titan.designer.AST.ISubReference)1 SingleLenghtRestriction (org.eclipse.titan.designer.AST.TTCN3.templates.SingleLenghtRestriction)1 Array_Type (org.eclipse.titan.designer.AST.TTCN3.types.Array_Type)1 Length_ParsedSubType (org.eclipse.titan.designer.AST.TTCN3.types.subtypes.Length_ParsedSubType)1 ParsedSubType (org.eclipse.titan.designer.AST.TTCN3.types.subtypes.ParsedSubType)1 SubType (org.eclipse.titan.designer.AST.TTCN3.types.subtypes.SubType)1 ExpressionStruct (org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)1