Search in sources :

Example 1 with RangeLenghtRestriction

use of org.eclipse.titan.designer.AST.TTCN3.templates.RangeLenghtRestriction in project titan.EclipsePlug-ins by eclipse.

the class SizeOfExpression method evaluateTemplate.

/**
 * Evaluates a checked template.
 *
 * @param template
 *                The template to evaluate
 * @param timestamp
 *                The compilation timestamp
 * @return The folded value or -1 if the template is unfoldable.
 */
private long evaluateTemplate(final ITTCN3Template template, final CompilationTimeStamp timestamp) {
    switch(template.getTemplatetype()) {
        case TEMPLATE_LIST:
            {
                final Template_List temp = (Template_List) template;
                if (temp.templateContainsAnyornone()) {
                    final LengthRestriction lengthRestriction = temp.getLengthRestriction();
                    if (lengthRestriction == null) {
                        templateInstance.getLocation().reportSemanticError("`sizeof' operation is not applicable for templates containing `*' without length restriction");
                        setIsErroneous(true);
                        return -1;
                    }
                    if (lengthRestriction instanceof RangeLenghtRestriction) {
                        final IValue upper = ((RangeLenghtRestriction) lengthRestriction).getUpperValue(timestamp);
                        if (Value_type.REAL_VALUE.equals(upper.getValuetype()) && ((Real_Value) upper).isPositiveInfinity()) {
                            templateInstance.getLocation().reportSemanticError("`sizeof' operation is not applicable for templates containing `*' without upper boundary in the length restriction");
                            setIsErroneous(true);
                            return -1;
                        }
                        if (Value_type.INTEGER_VALUE.equals(upper.getValuetype())) {
                            final int nofComponents = temp.getNofTemplatesNotAnyornone(timestamp);
                            if (nofComponents == ((Integer_Value) upper).intValue()) {
                                return nofComponents;
                            }
                            final IValue lower = ((RangeLenghtRestriction) lengthRestriction).getLowerValue(timestamp);
                            if (lower != null && Value_type.INTEGER_VALUE.equals(lower.getValuetype()) && ((Integer_Value) upper).intValue() == ((Integer_Value) lower).intValue()) {
                                return ((Integer_Value) upper).intValue();
                            }
                            templateInstance.getLocation().reportSemanticError("`sizeof' operation is not applicable for templates without exact size");
                            setIsErroneous(true);
                            return -1;
                        }
                    } else {
                        final IValue restriction = ((SingleLenghtRestriction) lengthRestriction).getRestriction(timestamp);
                        if (Value_type.INTEGER_VALUE.equals(restriction.getValuetype())) {
                            return ((Integer_Value) restriction).intValue();
                        }
                    }
                } else {
                    int result = 0;
                    for (int i = 0, size = temp.getNofTemplates(); i < size; i++) {
                        final ITTCN3Template tmp = temp.getTemplateByIndex(i);
                        switch(tmp.getTemplatetype()) {
                            case SPECIFIC_VALUE:
                                if (tmp.getValue().getValuetype() != Value_type.OMIT_VALUE) {
                                    ++result;
                                }
                                break;
                            default:
                                ++result;
                        }
                    }
                    return result;
                }
                break;
            }
        case NAMED_TEMPLATE_LIST:
            {
                int result = 0;
                final Named_Template_List temp = (Named_Template_List) template;
                for (int i = 0, size = temp.getNofTemplates(); i < size; i++) {
                    final ITTCN3Template tmp = temp.getTemplateByIndex(i).getTemplate();
                    switch(tmp.getTemplatetype()) {
                        case SPECIFIC_VALUE:
                            if (tmp.getValue().getValuetype() != Value_type.OMIT_VALUE) {
                                ++result;
                            }
                            break;
                        default:
                            ++result;
                    }
                }
                return result;
            }
        case SUBSET_MATCH:
            {
                final LengthRestriction restriction = template.getLengthRestriction();
                if (restriction instanceof SingleLenghtRestriction) {
                    final IValue value = ((SingleLenghtRestriction) restriction).getRestriction(timestamp);
                    if (value.getValuetype() == Value_type.INTEGER_VALUE && !value.isUnfoldable(timestamp)) {
                        return ((Integer_Value) value).getValue();
                    } else {
                        return -1;
                    }
                } else if (restriction instanceof RangeLenghtRestriction) {
                    final IValue minValue = ((RangeLenghtRestriction) restriction).getLowerValue(timestamp);
                    if (minValue.getValuetype() != Value_type.INTEGER_VALUE || minValue.isUnfoldable(timestamp)) {
                        return -1;
                    }
                    final SubsetMatch_Template temp = (SubsetMatch_Template) template;
                    if (temp.getNofTemplates() != ((Integer_Value) minValue).getValue()) {
                        return -1;
                    }
                    for (int i = 0, size = temp.getNofTemplates(); i < size; i++) {
                        final ITTCN3Template tmp = temp.getTemplateByIndex(i);
                        switch(tmp.getTemplatetype()) {
                            case SPECIFIC_VALUE:
                                break;
                            default:
                                return -1;
                        }
                    }
                    return temp.getNofTemplates();
                }
                return -1;
            }
        case SUPERSET_MATCH:
            {
                final LengthRestriction restriction = template.getLengthRestriction();
                if (restriction instanceof SingleLenghtRestriction) {
                    final IValue value = ((SingleLenghtRestriction) restriction).getRestriction(timestamp);
                    if (value.getValuetype() == Value_type.INTEGER_VALUE && !value.isUnfoldable(timestamp)) {
                        return ((Integer_Value) value).getValue();
                    } else {
                        return -1;
                    }
                } else if (restriction instanceof RangeLenghtRestriction) {
                    final IValue maxValue = ((RangeLenghtRestriction) restriction).getUpperValue(timestamp);
                    if (maxValue.getValuetype() != Value_type.INTEGER_VALUE || maxValue.isUnfoldable(timestamp)) {
                        return -1;
                    }
                    final SupersetMatch_Template temp = (SupersetMatch_Template) template;
                    if (temp.getNofTemplates() != ((Integer_Value) maxValue).getValue()) {
                        return -1;
                    }
                    for (int i = 0, size = temp.getNofTemplates(); i < size; i++) {
                        final ITTCN3Template tmp = temp.getTemplateByIndex(i);
                        switch(tmp.getTemplatetype()) {
                            case SPECIFIC_VALUE:
                                break;
                            default:
                                return -1;
                        }
                    }
                    return temp.getNofTemplates();
                }
                return -1;
            }
        default:
            return -1;
    }
    return -1;
}
Also used : LengthRestriction(org.eclipse.titan.designer.AST.TTCN3.templates.LengthRestriction) ITTCN3Template(org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template) Template_List(org.eclipse.titan.designer.AST.TTCN3.templates.Template_List) Named_Template_List(org.eclipse.titan.designer.AST.TTCN3.templates.Named_Template_List) IValue(org.eclipse.titan.designer.AST.IValue) RangeLenghtRestriction(org.eclipse.titan.designer.AST.TTCN3.templates.RangeLenghtRestriction) SubsetMatch_Template(org.eclipse.titan.designer.AST.TTCN3.templates.SubsetMatch_Template) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) Named_Template_List(org.eclipse.titan.designer.AST.TTCN3.templates.Named_Template_List) SupersetMatch_Template(org.eclipse.titan.designer.AST.TTCN3.templates.SupersetMatch_Template) Real_Value(org.eclipse.titan.designer.AST.TTCN3.values.Real_Value) SingleLenghtRestriction(org.eclipse.titan.designer.AST.TTCN3.templates.SingleLenghtRestriction)

Example 2 with RangeLenghtRestriction

use of org.eclipse.titan.designer.AST.TTCN3.templates.RangeLenghtRestriction in project titan.EclipsePlug-ins by eclipse.

the class SubType method checkThisTemplateLengthRestriction.

private void checkThisTemplateLengthRestriction(final CompilationTimeStamp timestamp, final TTCN3Template template) {
    final LengthRestriction lengthRestriction = template.getLengthRestriction();
    if ((lengthRestriction == null) || (subtypeConstraint == null)) {
        return;
    }
    // if there is a length restriction on the template then check
    // if
    // the intersection of the two restrictions is not empty
    SizeLimit tmplMinLen, tmplMaxLen;
    lengthRestriction.check(timestamp, Expected_Value_type.EXPECTED_DYNAMIC_VALUE);
    if (lengthRestriction instanceof SingleLenghtRestriction) {
        final SingleLenghtRestriction realRestriction = (SingleLenghtRestriction) lengthRestriction;
        final IValue lower = realRestriction.getRestriction(timestamp);
        if (lower.getIsErroneous(timestamp)) {
            return;
        }
        final IReferenceChain chain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
        final IValue last = lower.getValueRefdLast(timestamp, Expected_Value_type.EXPECTED_DYNAMIC_VALUE, chain);
        chain.release();
        if (!Value_type.INTEGER_VALUE.equals(last.getValuetype())) {
            return;
        }
        final BigInteger length = ((Integer_Value) last).getValueValue();
        tmplMinLen = new SizeLimit(length);
        tmplMaxLen = tmplMinLen;
    } else {
        final RangeLenghtRestriction realRestriction = (RangeLenghtRestriction) lengthRestriction;
        final IValue lower = realRestriction.getLowerValue(timestamp);
        IReferenceChain chain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
        final IValue lastLower = lower.getValueRefdLast(timestamp, Expected_Value_type.EXPECTED_DYNAMIC_VALUE, chain);
        chain.release();
        if (lastLower.getIsErroneous(timestamp) || !Value_type.INTEGER_VALUE.equals(lastLower.getValuetype())) {
            return;
        }
        final IValue upper = realRestriction.getUpperValue(timestamp);
        if (upper == null) {
            return;
        }
        chain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
        final IValue lastUpper = upper.getValueRefdLast(timestamp, Expected_Value_type.EXPECTED_DYNAMIC_VALUE, chain);
        chain.release();
        if (lastUpper.getIsErroneous(timestamp) || !Value_type.INTEGER_VALUE.equals(lastUpper.getValuetype())) {
            return;
        }
        tmplMinLen = new SizeLimit(((Integer_Value) lastLower).getValueValue());
        tmplMaxLen = new SizeLimit(((Integer_Value) lastUpper).getValueValue());
    }
    SubtypeConstraint tmplConstraint;
    switch(subtypeType) {
        case ST_BITSTRING:
            tmplConstraint = new StringSizeAndValueListConstraint(StringSizeAndValueListConstraint.Type.BITSTRING, tmplMinLen, tmplMaxLen);
            break;
        case ST_HEXSTRING:
            tmplConstraint = new StringSizeAndValueListConstraint(StringSizeAndValueListConstraint.Type.HEXSTRING, tmplMinLen, tmplMaxLen);
            break;
        case ST_OCTETSTRING:
            tmplConstraint = new StringSizeAndValueListConstraint(StringSizeAndValueListConstraint.Type.OCTETSTRING, tmplMinLen, tmplMaxLen);
            break;
        case ST_CHARSTRING:
            tmplConstraint = new StringSetConstraint(StringSubtypeTreeElement.StringType.CHARSTRING, StringSetConstraint.ConstraintType.SIZE_CONSTRAINT, new RangeListConstraint(tmplMinLen, tmplMaxLen));
            break;
        case ST_UNIVERSAL_CHARSTRING:
            tmplConstraint = new StringSetConstraint(StringSubtypeTreeElement.StringType.UNIVERSAL_CHARSTRING, StringSetConstraint.ConstraintType.SIZE_CONSTRAINT, new RangeListConstraint(tmplMinLen, tmplMaxLen));
            break;
        case ST_RECORDOF:
        case ST_SETOF:
            tmplConstraint = new ValueListAndSizeConstraint(tmplMinLen, tmplMaxLen);
            break;
        default:
            return;
    }
    if (subtypeConstraint.intersection(tmplConstraint).isEmpty() == TernaryBool.TTRUE) {
        template.getLocation().reportSemanticWarning(MessageFormat.format("Template's length restriction {0} is outside of the type's subtype constraint {1}", (new RangeListConstraint(tmplMinLen, tmplMaxLen)).toString(), subtypeConstraint.toString()));
    }
}
Also used : LengthRestriction(org.eclipse.titan.designer.AST.TTCN3.templates.LengthRestriction) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) SingleLenghtRestriction(org.eclipse.titan.designer.AST.TTCN3.templates.SingleLenghtRestriction) IValue(org.eclipse.titan.designer.AST.IValue) RangeLenghtRestriction(org.eclipse.titan.designer.AST.TTCN3.templates.RangeLenghtRestriction) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain) BigInteger(java.math.BigInteger)

Example 3 with RangeLenghtRestriction

use of org.eclipse.titan.designer.AST.TTCN3.templates.RangeLenghtRestriction in project titan.EclipsePlug-ins by eclipse.

the class SubType method addTtcnLength.

private boolean addTtcnLength(final CompilationTimeStamp timestamp, final LengthRestriction lengthRestriction, final int restrictionIndex) {
    lengthRestriction.setMyScope(myOwner.getMyScope());
    final BridgingNamedNode bridge = new BridgingNamedNode(myOwner, myOwner.getFullName() + ".<length_restriction_" + restrictionIndex + ">");
    lengthRestriction.setFullNameParent(bridge);
    lengthRestriction.check(timestamp, Expected_Value_type.EXPECTED_CONSTANT);
    IValue lower = null, upper = null;
    if (lengthRestriction instanceof SingleLenghtRestriction) {
        lower = ((SingleLenghtRestriction) lengthRestriction).getRestriction(timestamp);
        if (lower == null || lower.getIsErroneous(timestamp) || !Value_type.INTEGER_VALUE.equals(lower.getValuetype()) || lower.isUnfoldable(timestamp)) {
            return false;
        }
        if (!checkBoundaryValid(lower, "length restriction value")) {
            return false;
        }
        final SizeLimit boundaryLimit = new SizeLimit(((Integer_Value) lower).getValueValue());
        return setTtcnLength(boundaryLimit, boundaryLimit);
    }
    lower = ((RangeLenghtRestriction) lengthRestriction).getLowerValue(timestamp);
    if (lower == null || lower.getIsErroneous(timestamp) || !Value_type.INTEGER_VALUE.equals(lower.getValuetype()) || lower.isUnfoldable(timestamp)) {
        return false;
    }
    if (!checkBoundaryValid(lower, "lower boundary")) {
        return false;
    }
    upper = ((RangeLenghtRestriction) lengthRestriction).getUpperValue(timestamp);
    if (upper != null) {
        if (upper.getMyScope() == null) {
            upper.setMyScope(myOwner.getMyScope());
        }
        if (upper.getIsErroneous(timestamp) || !Value_type.INTEGER_VALUE.equals(upper.getValuetype()) || upper.isUnfoldable(timestamp)) {
            return false;
        }
        if (!checkBoundaryValid(upper, "upper boundary")) {
            return false;
        }
        return setTtcnLength(new SizeLimit(((Integer_Value) lower).getValueValue()), new SizeLimit(((Integer_Value) upper).getValueValue()));
    }
    // upper is infinity
    return setTtcnLength(new SizeLimit(((Integer_Value) lower).getValueValue()), SizeLimit.MAXIMUM);
}
Also used : IValue(org.eclipse.titan.designer.AST.IValue) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) BridgingNamedNode(org.eclipse.titan.designer.AST.BridgingNamedNode) SingleLenghtRestriction(org.eclipse.titan.designer.AST.TTCN3.templates.SingleLenghtRestriction)

Aggregations

IValue (org.eclipse.titan.designer.AST.IValue)3 SingleLenghtRestriction (org.eclipse.titan.designer.AST.TTCN3.templates.SingleLenghtRestriction)3 Integer_Value (org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value)3 LengthRestriction (org.eclipse.titan.designer.AST.TTCN3.templates.LengthRestriction)2 RangeLenghtRestriction (org.eclipse.titan.designer.AST.TTCN3.templates.RangeLenghtRestriction)2 BigInteger (java.math.BigInteger)1 BridgingNamedNode (org.eclipse.titan.designer.AST.BridgingNamedNode)1 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)1 ITTCN3Template (org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template)1 Named_Template_List (org.eclipse.titan.designer.AST.TTCN3.templates.Named_Template_List)1 SubsetMatch_Template (org.eclipse.titan.designer.AST.TTCN3.templates.SubsetMatch_Template)1 SupersetMatch_Template (org.eclipse.titan.designer.AST.TTCN3.templates.SupersetMatch_Template)1 Template_List (org.eclipse.titan.designer.AST.TTCN3.templates.Template_List)1 Real_Value (org.eclipse.titan.designer.AST.TTCN3.values.Real_Value)1