Search in sources :

Example 1 with Named_Template_List

use of org.eclipse.titan.designer.AST.TTCN3.templates.Named_Template_List 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 Named_Template_List

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

the class SizeOfExpression method checkExpressionOperands.

/**
 * Checks the parameters of the expression and if they are valid in
 * their position in the expression or not.
 *
 * @param timestamp
 *                the timestamp of the actual semantic check cycle.
 * @param expectedValue
 *                the kind of value expected.
 * @param referenceChain
 *                a reference chain to detect cyclic references.
 *
 * @return the size of the expression, or -1 in case of error
 */
private long checkExpressionOperands(final CompilationTimeStamp timestamp, final Expected_Value_type expectedValue, final IReferenceChain referenceChain) {
    Expected_Value_type internalExpectedValue;
    if (Expected_Value_type.EXPECTED_DYNAMIC_VALUE.equals(expectedValue)) {
        internalExpectedValue = Expected_Value_type.EXPECTED_TEMPLATE;
    } else {
        internalExpectedValue = expectedValue;
    }
    ITTCN3Template template = templateInstance.getTemplateBody();
    template.setLoweridToReference(timestamp);
    template = template.getTemplateReferencedLast(timestamp, referenceChain);
    if (template.getIsErroneous(timestamp)) {
        setIsErroneous(true);
        return -1;
    }
    // Timer and port arrays are handled separately
    if (template.getTemplatetype() == Template_type.SPECIFIC_VALUE) {
        final SpecificValue_Template specValTempl = (SpecificValue_Template) template;
        IValue val = specValTempl.getSpecificValue();
        val.setMyGovernor(specValTempl.getMyGovernor());
        if (val.getValuetype() == Value_type.UNDEFINED_LOWERIDENTIFIER_VALUE) {
            val = val.setLoweridToReference(timestamp);
        }
        if (val != null && val.getValuetype() == Value_type.REFERENCED_VALUE) {
            final Referenced_Value referencedValue = (Referenced_Value) val;
            final Reference ref = referencedValue.getReference();
            final Assignment temporalAss = ref.getRefdAssignment(timestamp, true);
            if (temporalAss != null) {
                final Assignment_type asstype = temporalAss.getAssignmentType();
                ArrayDimensions dimensions;
                if (asstype == Assignment_type.A_PORT) {
                    dimensions = ((Def_Port) temporalAss).getDimensions();
                    return checkTimerPort(timestamp, ref, dimensions, temporalAss);
                } else if (asstype == Assignment_type.A_TIMER) {
                    dimensions = ((Def_Timer) temporalAss).getDimensions();
                    return checkTimerPort(timestamp, ref, dimensions, temporalAss);
                }
            }
        }
    }
    IType governor = templateInstance.getExpressionGovernor(timestamp, internalExpectedValue);
    if (governor == null) {
        final ITTCN3Template templ = template.setLoweridToReference(timestamp);
        governor = templ.getExpressionGovernor(timestamp, internalExpectedValue);
    }
    if (governor == null) {
        if (!template.getIsErroneous(timestamp)) {
            templateInstance.getLocation().reportSemanticError("Cannot determine the type of the argument in the `sizeof' operation. If type is known, use valueof(<type>: ...) as argument.");
        }
        setIsErroneous(true);
        return -1;
    }
    IsValueExpression.checkExpressionTemplateInstance(timestamp, this, templateInstance, governor, referenceChain, internalExpectedValue);
    if (isErroneous) {
        return -1;
    }
    IType type = governor.getTypeRefdLast(timestamp);
    switch(type.getTypetype()) {
        case TYPE_SEQUENCE_OF:
        case TYPE_SET_OF:
        case TYPE_TTCN3_SEQUENCE:
        case TYPE_TTCN3_SET:
        case TYPE_ASN1_SEQUENCE:
        case TYPE_ASN1_SET:
        case TYPE_ARRAY:
        case TYPE_OBJECTID:
        case TYPE_ROID:
        case TYPE_UNDEFINED:
            break;
        default:
            templateInstance.getLocation().reportSemanticError("Reference to a value or template of type record, record of, set, set of, objid or array was expected");
            setIsErroneous(true);
            return -1;
    }
    IValue value = null;
    Reference reference = null;
    Assignment assignment = null;
    List<ISubReference> subreferences = null;
    switch(template.getTemplatetype()) {
        case INDEXED_TEMPLATE_LIST:
            return -1;
        case TEMPLATE_REFD:
            reference = ((Referenced_Template) template).getReference();
            assignment = reference.getRefdAssignment(timestamp, false);
            subreferences = reference.getSubreferences();
            break;
        case TEMPLATE_LIST:
        case NAMED_TEMPLATE_LIST:
        case SUBSET_MATCH:
        case SUPERSET_MATCH:
            // compute later
            break;
        case SPECIFIC_VALUE:
            value = ((SpecificValue_Template) template).getSpecificValue().getValueRefdLast(timestamp, referenceChain);
            if (value != null) {
                switch(value.getValuetype()) {
                    case SEQUENCEOF_VALUE:
                    case SETOF_VALUE:
                    case ARRAY_VALUE:
                    case RELATIVEOBJECTIDENTIFIER_VALUE:
                    case OBJECTID_VALUE:
                    case SEQUENCE_VALUE:
                    case SET_VALUE:
                        break;
                    case REFERENCED_VALUE:
                        {
                            reference = ((Referenced_Value) value).getReference();
                            assignment = reference.getRefdAssignment(timestamp, false);
                            subreferences = reference.getSubreferences();
                            break;
                        }
                    default:
                        templateInstance.getLocation().reportSemanticError(MessageFormat.format("`sizeof'' operation is not applicable to `{0}''", value.createStringRepresentation()));
                        setIsErroneous(true);
                        return -1;
                }
            }
            break;
        default:
            templateInstance.getLocation().reportSemanticError(MessageFormat.format("`sizeof'' operation is not applicable to {0}", template.getTemplateTypeName()));
            setIsErroneous(true);
            return -1;
    }
    if (assignment != null) {
        if (assignment.getIsErroneous()) {
            setIsErroneous(true);
            return -1;
        }
        switch(assignment.getAssignmentType()) {
            case A_CONST:
                value = ((Def_Const) assignment).getValue();
                break;
            case A_EXT_CONST:
            case A_MODULEPAR:
            case A_MODULEPAR_TEMPLATE:
                if (Expected_Value_type.EXPECTED_CONSTANT.equals(internalExpectedValue)) {
                    templateInstance.getLocation().reportSemanticError(MessageFormat.format("Reference to an (evaluable) constant value was expected instead of {0}", assignment.getDescription()));
                    setIsErroneous(true);
                    return -1;
                }
                break;
            case A_VAR:
            case A_PAR_VAL:
            case A_PAR_VAL_IN:
            case A_PAR_VAL_OUT:
            case A_PAR_VAL_INOUT:
                switch(internalExpectedValue) {
                    case EXPECTED_CONSTANT:
                        templateInstance.getLocation().reportSemanticError(MessageFormat.format("Reference to a constant value was expected instead of {0}", assignment.getDescription()));
                        setIsErroneous(true);
                        return -1;
                    case EXPECTED_STATIC_VALUE:
                        templateInstance.getLocation().reportSemanticError(MessageFormat.format("Reference to a static value was expected instead of {0}", assignment.getDescription()));
                        setIsErroneous(true);
                        return -1;
                    default:
                        break;
                }
                break;
            case A_TEMPLATE:
                template = ((Def_Template) assignment).getTemplate(timestamp);
                if (!Expected_Value_type.EXPECTED_TEMPLATE.equals(internalExpectedValue)) {
                    templateInstance.getLocation().reportSemanticError(MessageFormat.format("Reference to a value was expected instead of {0}", assignment.getDescription()));
                    setIsErroneous(true);
                    return -1;
                }
                break;
            case A_VAR_TEMPLATE:
            case A_PAR_TEMP_IN:
            case A_PAR_TEMP_OUT:
            case A_PAR_TEMP_INOUT:
                if (!Expected_Value_type.EXPECTED_TEMPLATE.equals(internalExpectedValue)) {
                    templateInstance.getLocation().reportSemanticError(MessageFormat.format("Reference to a value was expected instead of {0}", assignment.getDescription()));
                    setIsErroneous(true);
                    return -1;
                }
                break;
            case A_FUNCTION_RVAL:
            case A_EXT_FUNCTION_RVAL:
                switch(internalExpectedValue) {
                    case EXPECTED_CONSTANT:
                        templateInstance.getLocation().reportSemanticError(MessageFormat.format("Reference to a constant value was expected instead of the return value of {0}", assignment.getDescription()));
                        setIsErroneous(true);
                        return -1;
                    case EXPECTED_STATIC_VALUE:
                        templateInstance.getLocation().reportSemanticError(MessageFormat.format("Reference to a static value was expected instead of the return value of {0}", assignment.getDescription()));
                        setIsErroneous(true);
                        return -1;
                    default:
                        break;
                }
                break;
            case A_FUNCTION_RTEMP:
            case A_EXT_FUNCTION_RTEMP:
                if (!Expected_Value_type.EXPECTED_TEMPLATE.equals(internalExpectedValue)) {
                    templateInstance.getLocation().reportSemanticError(MessageFormat.format("Reference to a value was expected instead of a call of {0}, which returns a template", assignment.getDescription()));
                    setIsErroneous(true);
                    return -1;
                }
                break;
            case A_TIMER:
            case A_PORT:
                // were already checked separately.
                break;
            default:
                templateInstance.getLocation().reportSemanticError(MessageFormat.format("Reference to a {0} was expected instead of {1}", Expected_Value_type.EXPECTED_TEMPLATE.equals(internalExpectedValue) ? "value or template" : "value", assignment.getDescription()));
                setIsErroneous(true);
                return -1;
        }
        type = assignment.getType(timestamp).getFieldType(timestamp, reference, 1, Expected_Value_type.EXPECTED_DYNAMIC_VALUE, false);
        if (type == null || type.getIsErroneous(timestamp)) {
            setIsErroneous(true);
            return -1;
        }
        type = type.getTypeRefdLast(timestamp);
        switch(type.getTypetype()) {
            case TYPE_SEQUENCE_OF:
            case TYPE_SET_OF:
            case TYPE_TTCN3_SEQUENCE:
            case TYPE_TTCN3_SET:
            case TYPE_ASN1_SEQUENCE:
            case TYPE_ASN1_SET:
            case TYPE_ARRAY:
            case TYPE_OBJECTID:
            case TYPE_ROID:
            case TYPE_UNDEFINED:
                break;
            default:
                templateInstance.getLocation().reportSemanticError("Reference to a value or template of type record, record of, set, set of, objid or array was expected");
                setIsErroneous(true);
                return -1;
        }
    }
    // check for index overflows in subrefs if possible
    if (value != null) {
        switch(value.getValuetype()) {
            case SEQUENCEOF_VALUE:
                if (((SequenceOf_Value) value).isIndexed()) {
                    return -1;
                }
                break;
            case SETOF_VALUE:
                if (((SetOf_Value) value).isIndexed()) {
                    return -1;
                }
                break;
            case ARRAY_VALUE:
                if (((Array_Value) value).isIndexed()) {
                    return -1;
                }
                break;
            default:
                break;
        }
        /* The reference points to a constant.  */
        if (subreferences != null && !reference.hasUnfoldableIndexSubReference(timestamp)) {
            value = value.getReferencedSubValue(timestamp, reference, 1, referenceChain);
            if (value == null) {
                setIsErroneous(true);
                return -1;
            }
            value = value.getValueRefdLast(timestamp, referenceChain);
        } else {
            // stop processing
            value = null;
        }
    } else if (template != null) {
        /* The size of INDEXED_TEMPLATE_LIST nodes is unknown at compile
		         time.  Don't try to evaluate it at compile time.  */
        if (reference != null && reference.hasUnfoldableIndexSubReference(timestamp)) {
            return -1;
        }
        if (reference != null && subreferences != null) {
            template = template.getReferencedSubTemplate(timestamp, reference, referenceChain);
            if (template == null) {
                setIsErroneous(true);
                return -1;
            }
            template = template.getTemplateReferencedLast(timestamp);
        }
    }
    if (template != null) {
        if (template.getIsErroneous(timestamp)) {
            setIsErroneous(true);
            return -1;
        }
        switch(template.getTemplatetype()) {
            case TEMPLATE_REFD:
                template = null;
                break;
            case SPECIFIC_VALUE:
                value = ((SpecificValue_Template) template).getSpecificValue().getValueRefdLast(timestamp, referenceChain);
                template = null;
                break;
            case TEMPLATE_LIST:
            case NAMED_TEMPLATE_LIST:
            case SUBSET_MATCH:
            case SUPERSET_MATCH:
                break;
            default:
                // FIXME this can not happen
                templateInstance.getLocation().reportSemanticError(MessageFormat.format("`sizeof'' operation is not applicable to {0}", template.getTemplateTypeName()));
                setIsErroneous(true);
                return -1;
        }
    }
    if (value != null) {
        switch(value.getValuetype()) {
            case SEQUENCEOF_VALUE:
            case SETOF_VALUE:
            case ARRAY_VALUE:
            case RELATIVEOBJECTIDENTIFIER_VALUE:
            case OBJECTID_VALUE:
            case SEQUENCE_VALUE:
            case SET_VALUE:
                break;
            default:
                value = null;
                return -1;
        }
    }
    /* evaluation */
    if (Type_type.TYPE_ARRAY.equals(type.getTypetype())) {
        return ((Array_Type) type).getDimension().getSize();
    } else if (template != null) {
        return evaluateTemplate(template, timestamp);
    } else if (value != null) {
        return evaluateValue(value);
    } else {
        return -1;
    }
}
Also used : ITTCN3Template(org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template) Assignment_type(org.eclipse.titan.designer.AST.Assignment.Assignment_type) Array_Value(org.eclipse.titan.designer.AST.TTCN3.values.Array_Value) ISubReference(org.eclipse.titan.designer.AST.ISubReference) Reference(org.eclipse.titan.designer.AST.Reference) Def_Timer(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Timer) SequenceOf_Value(org.eclipse.titan.designer.AST.TTCN3.values.SequenceOf_Value) Referenced_Value(org.eclipse.titan.designer.AST.TTCN3.values.Referenced_Value) SetOf_Value(org.eclipse.titan.designer.AST.TTCN3.values.SetOf_Value) IType(org.eclipse.titan.designer.AST.IType) Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) SpecificValue_Template(org.eclipse.titan.designer.AST.TTCN3.templates.SpecificValue_Template) IValue(org.eclipse.titan.designer.AST.IValue) ArrayDimensions(org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimensions) Expected_Value_type(org.eclipse.titan.designer.AST.TTCN3.Expected_Value_type)

Example 3 with Named_Template_List

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

the class ASN1_Sequence_Type method checkThisNamedTemplateList.

private boolean checkThisNamedTemplateList(final CompilationTimeStamp timestamp, final Named_Template_List templateList, final boolean isModified, final boolean implicitOmit, final Assignment lhs) {
    templateList.removeGeneratedValues();
    boolean selfReference = false;
    final Map<String, NamedTemplate> componentMap = new HashMap<String, NamedTemplate>();
    final int nofTypeComponents = getNofComponents(timestamp);
    final int nofTemplateComponents = templateList.getNofTemplates();
    boolean inSync = true;
    CompField lastComponentField = null;
    int nextIndex = 0;
    for (int i = 0; i < nofTemplateComponents; i++) {
        final NamedTemplate namedTemplate = templateList.getTemplateByIndex(i);
        final Identifier identifier = namedTemplate.getName();
        final String templateName = identifier.getName();
        if (hasComponentWithName(identifier)) {
            if (componentMap.containsKey(templateName)) {
                namedTemplate.getLocation().reportSemanticError(MessageFormat.format(DUPLICATETEMPLATEFIELDFIRST, identifier.getDisplayName()));
                final Location tempLocation = componentMap.get(templateName).getLocation();
                tempLocation.reportSemanticError(MessageFormat.format(DUPLICATETEMPLATEFIELDAGAIN, identifier.getDisplayName()));
                inSync = false;
            } else {
                componentMap.put(templateName, namedTemplate);
            }
            final CompField componentField = getComponentByName(identifier);
            if (inSync) {
                if (isModified) {
                    boolean found = false;
                    for (int j = nextIndex; j < nofTypeComponents && !found; j++) {
                        final CompField componentField2 = getComponentByIndex(j);
                        if (templateName.equals(componentField2.getIdentifier().getName())) {
                            lastComponentField = componentField2;
                            nextIndex = j + 1;
                            found = true;
                        }
                    }
                    if (!found && lastComponentField != null) {
                        namedTemplate.getLocation().reportSemanticError(MessageFormat.format(INCORRECTTEMPLATEFIELDORDER, identifier.getDisplayName(), lastComponentField.getIdentifier().getDisplayName(), getFullName()));
                        inSync = false;
                    }
                } else if (strictConstantCheckingSeverity) {
                    final CompField componentField2 = getComponentByIndex(i);
                    if (componentField2 != componentField) {
                        if (!componentField2.isOptional() || (componentField2.isOptional() && !implicitOmit)) {
                            namedTemplate.getLocation().reportSemanticError(MessageFormat.format(UNEXPECTEDTEMPLATEFIELD, identifier.getDisplayName(), componentField2.getIdentifier().getDisplayName()));
                            inSync = false;
                        }
                    }
                }
            }
            final Type type = componentField.getType();
            if (type != null && !type.getIsErroneous(timestamp)) {
                ITTCN3Template componentTemplate = namedTemplate.getTemplate();
                componentTemplate.setMyGovernor(type);
                componentTemplate = type.checkThisTemplateRef(timestamp, componentTemplate);
                boolean isOptional = componentField.isOptional();
                if (!isOptional && componentField.hasDefault() && defaultAsOptional) {
                    isOptional = true;
                }
                selfReference |= componentTemplate.checkThisTemplateGeneric(timestamp, type, isModified, isOptional, isOptional, true, implicitOmit, lhs);
            }
        } else {
            namedTemplate.getLocation().reportSemanticError(MessageFormat.format(NONEXISTENTTEMPLATEFIELDREFERENCE, identifier.getDisplayName(), getTypename()));
            inSync = false;
        }
    }
    if (!isModified && strictConstantCheckingSeverity) {
        // check missing fields
        for (int i = 0; i < nofTypeComponents; i++) {
            final Identifier identifier = getComponentIdentifierByIndex(i);
            if (!componentMap.containsKey(identifier.getName())) {
                if (getComponentByIndex(i).isOptional() && implicitOmit) {
                    templateList.addNamedValue(new NamedTemplate(new Identifier(Identifier_type.ID_TTCN, identifier.getDisplayName()), new OmitValue_Template(), false));
                } else {
                    templateList.getLocation().reportSemanticError(MessageFormat.format(MISSINGTEMPLATEFIELD, identifier.getDisplayName(), getTypename()));
                }
            }
        }
    }
    return selfReference;
}
Also used : ITTCN3Template(org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template) SequenceOf_Type(org.eclipse.titan.designer.AST.TTCN3.types.SequenceOf_Type) IASN1Type(org.eclipse.titan.designer.AST.ASN1.IASN1Type) Array_Type(org.eclipse.titan.designer.AST.TTCN3.types.Array_Type) TTCN3_Sequence_Type(org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Sequence_Type) Type(org.eclipse.titan.designer.AST.Type) IType(org.eclipse.titan.designer.AST.IType) Identifier(org.eclipse.titan.designer.AST.Identifier) HashMap(java.util.HashMap) NamedTemplate(org.eclipse.titan.designer.AST.TTCN3.templates.NamedTemplate) CompField(org.eclipse.titan.designer.AST.TTCN3.types.CompField) OmitValue_Template(org.eclipse.titan.designer.AST.TTCN3.templates.OmitValue_Template) Location(org.eclipse.titan.designer.AST.Location)

Example 4 with Named_Template_List

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

the class ASN1_Choice_Type method checkThisTemplate.

@Override
public /**
 * {@inheritDoc}
 */
boolean checkThisTemplate(final CompilationTimeStamp timestamp, final ITTCN3Template template, final boolean isModified, final boolean implicitOmit, final Assignment lhs) {
    registerUsage(template);
    template.setMyGovernor(this);
    if (getIsErroneous(timestamp)) {
        return false;
    }
    boolean selfReference = false;
    if (Template_type.NAMED_TEMPLATE_LIST.equals(template.getTemplatetype())) {
        final Named_Template_List namedTemplateList = (Named_Template_List) template;
        final int nofTemplates = namedTemplateList.getNofTemplates();
        if (nofTemplates != 1) {
            template.getLocation().reportSemanticError(ONEFIELDEXPECTED);
            template.setIsErroneous(true);
        }
        for (int i = 0; i < nofTemplates; i++) {
            final NamedTemplate namedTemplate = namedTemplateList.getTemplateByIndex(i);
            final Identifier name = namedTemplate.getName();
            final CompField field = components.getCompByName(name);
            if (field == null) {
                namedTemplate.getLocation().reportSemanticError(MessageFormat.format(REFERENCETONONEXISTENTFIELD, name.getDisplayName(), getFullName()));
            } else {
                final Type fieldType = field.getType();
                if (fieldType != null && !fieldType.getIsErroneous(timestamp)) {
                    ITTCN3Template namedTemplateTemplate = namedTemplate.getTemplate();
                    namedTemplateTemplate.setMyGovernor(fieldType);
                    namedTemplateTemplate = fieldType.checkThisTemplateRef(timestamp, namedTemplateTemplate);
                    final Completeness_type completeness = namedTemplateList.getCompletenessConditionChoice(timestamp, isModified, name);
                    selfReference |= namedTemplateTemplate.checkThisTemplateGeneric(timestamp, fieldType, Completeness_type.MAY_INCOMPLETE.equals(completeness), false, false, true, implicitOmit, lhs);
                }
            }
        }
    } else {
        template.getLocation().reportSemanticError(MessageFormat.format(TEMPLATENOTALLOWED, template.getTemplateTypeName(), getTypename()));
        template.setIsErroneous(true);
    }
    if (template.getLengthRestriction() != null) {
        template.getLocation().reportSemanticError(MessageFormat.format(LENGTHRESTRICTIONNOTALLOWED, getTypename()));
        template.setIsErroneous(true);
    }
    return selfReference;
}
Also used : ITTCN3Template(org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template) Completeness_type(org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template.Completeness_type) IASN1Type(org.eclipse.titan.designer.AST.ASN1.IASN1Type) TTCN3_Choice_Type(org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Choice_Type) Type(org.eclipse.titan.designer.AST.Type) IType(org.eclipse.titan.designer.AST.IType) Identifier(org.eclipse.titan.designer.AST.Identifier) NamedTemplate(org.eclipse.titan.designer.AST.TTCN3.templates.NamedTemplate) CompField(org.eclipse.titan.designer.AST.TTCN3.types.CompField) Named_Template_List(org.eclipse.titan.designer.AST.TTCN3.templates.Named_Template_List)

Example 5 with Named_Template_List

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

the class Referenced_Template method generateRearrangeInitCodeReferenced.

private void generateRearrangeInitCodeReferenced(final JavaGenData aData, final StringBuilder source, final ExpressionStruct expression) {
    /**
     * Initially we can assume that:
     * - this is a referenced template and a part of a non-parameterized template
     * - u.ref.ref points to (a field of) a non-parameterized template within the same module as this.
     * - this ensures that the do-while loop will run at least twice (i.e. the first continue statement will be reached in the first iteration)
     */
    final Stack<ISubReference> referenceStack = new Stack<ISubReference>();
    ITTCN3Template template = this;
    for (; ; ) {
        if (template.getTemplatetype() == Template_type.TEMPLATE_REFD) {
            final Reference reference = ((Referenced_Template) template).getReference();
            final Assignment assignment = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
            /**
             * Don't follow the reference if:
             *  - the referenced definition is not a template
             *  - the referenced template is parameterized or
             *  - the referenced template is in different module
             */
            if (assignment.getAssignmentType() == Assignment_type.A_TEMPLATE && ((Def_Template) assignment).getFormalParameterList() == null && assignment.getMyScope().getModuleScope() == myScope.getModuleScope()) {
                // accumulate the sub-references of the referred reference
                final List<ISubReference> subReferences = reference.getSubreferences();
                if (subReferences != null && subReferences.size() > 1) {
                    for (int i = subReferences.size(); i > 1; i--) {
                        referenceStack.push(subReferences.get(i - 1));
                    }
                }
                // jump to the referred top-level template
                template = ((Def_Template) assignment).getTemplate(CompilationTimeStamp.getBaseTimestamp());
                // start the iteration from the beginning
                continue;
            } else {
                // the reference cannot be followed
                break;
            }
        }
        // stop if there are no sub-references
        if (referenceStack.isEmpty()) {
            break;
        }
        // take the topmost sub-reference
        final ISubReference subReference = referenceStack.peek();
        if (subReference instanceof FieldSubReference) {
            if (template.getTemplatetype() != Template_type.NAMED_TEMPLATE_LIST) {
                break;
            }
            // the field reference can be followed
            final Identifier fieldId = ((FieldSubReference) subReference).getId();
            template = ((Named_Template_List) template).getNamedTemplate(fieldId).getTemplate();
        } else {
            // trying to follow an array reference
            if (template.getTemplatetype() != Template_type.TEMPLATE_LIST) {
                break;
            }
            IValue arrayIndex = ((ArraySubReference) subReference).getValue();
            final IReferenceChain referenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
            arrayIndex = arrayIndex.getValueRefdLast(CompilationTimeStamp.getBaseTimestamp(), referenceChain);
            referenceChain.release();
            if (arrayIndex.getValuetype() != Value_type.INTEGER_VALUE) {
                break;
            }
            // the index is available at compilation time
            long index = ((Integer_Value) arrayIndex).getValue();
            // index transformation in case of arrays
            if (template.getMyGovernor().getTypetype() == Type_type.TYPE_ARRAY) {
                index = index - ((Array_Type) template.getMyGovernor()).getDimension().getOffset();
            }
            template = ((Template_List) template).getTemplateByIndex((int) index);
        }
        // the topmost sub-reference was processed
        // it can be erased from the stack
        referenceStack.pop();
    }
    // the smallest dependent template is now in t
    // generate the initializer sequence for t
    template.generateCodeInit(aData, source, template.get_lhs_name());
    // the equivalent Java code of the referenced template is composed of the
    // genname of t and the remained sub-references in refstack
    expression.expression.append(template.getGenNameOwn(myScope));
    while (!referenceStack.isEmpty()) {
        final ISubReference subReference = referenceStack.pop();
        if (subReference instanceof FieldSubReference) {
            expression.expression.append(MessageFormat.format(".get{0}()", FieldSubReference.getJavaGetterName(((FieldSubReference) subReference).getId().getName())));
        } else {
            expression.expression.append(".getAt(");
            ((ArraySubReference) subReference).getValue().generateCodeExpression(aData, expression, false);
            expression.expression.append(')');
        }
    }
}
Also used : FieldSubReference(org.eclipse.titan.designer.AST.FieldSubReference) Reference(org.eclipse.titan.designer.AST.Reference) ISubReference(org.eclipse.titan.designer.AST.ISubReference) FieldSubReference(org.eclipse.titan.designer.AST.FieldSubReference) ArraySubReference(org.eclipse.titan.designer.AST.ArraySubReference) ParameterisedSubReference(org.eclipse.titan.designer.AST.ParameterisedSubReference) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) Stack(java.util.Stack) ArraySubReference(org.eclipse.titan.designer.AST.ArraySubReference) Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) Identifier(org.eclipse.titan.designer.AST.Identifier) IValue(org.eclipse.titan.designer.AST.IValue) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain)

Aggregations

ITTCN3Template (org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template)16 Identifier (org.eclipse.titan.designer.AST.Identifier)11 IType (org.eclipse.titan.designer.AST.IType)10 Type (org.eclipse.titan.designer.AST.Type)10 NamedTemplate (org.eclipse.titan.designer.AST.TTCN3.templates.NamedTemplate)9 Named_Template_List (org.eclipse.titan.designer.AST.TTCN3.templates.Named_Template_List)9 HashMap (java.util.HashMap)5 IValue (org.eclipse.titan.designer.AST.IValue)5 IASN1Type (org.eclipse.titan.designer.AST.ASN1.IASN1Type)4 Completeness_type (org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template.Completeness_type)4 OmitValue_Template (org.eclipse.titan.designer.AST.TTCN3.templates.OmitValue_Template)4 CompField (org.eclipse.titan.designer.AST.TTCN3.types.CompField)4 Integer_Value (org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value)4 Assignment (org.eclipse.titan.designer.AST.Assignment)3 ISubReference (org.eclipse.titan.designer.AST.ISubReference)3 Template_List (org.eclipse.titan.designer.AST.TTCN3.templates.Template_List)3 SubType (org.eclipse.titan.designer.AST.TTCN3.types.subtypes.SubType)3 ArraySubReference (org.eclipse.titan.designer.AST.ArraySubReference)2 FieldSubReference (org.eclipse.titan.designer.AST.FieldSubReference)2 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)2