Search in sources :

Example 1 with IndexedTemplate

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

the class Indexed_Template_List 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();
    if (asValue != null) {
        asValue.generateCodeInit(aData, source, name);
        return;
    }
    if (myGovernor == null) {
        return;
    }
    // FIXME actually a bit more complex
    final IType type = myGovernor.getTypeRefdLast(CompilationTimeStamp.getBaseTimestamp());
    String ofTypeName;
    switch(type.getTypetype()) {
        case TYPE_SEQUENCE_OF:
            ofTypeName = ((SequenceOf_Type) type).getOfType().getGenNameTemplate(aData, source, myScope);
            break;
        case TYPE_SET_OF:
            ofTypeName = ((SetOf_Type) type).getOfType().getGenNameTemplate(aData, source, myScope);
            break;
        case TYPE_ARRAY:
            ofTypeName = ((Array_Type) type).getElementType().getGenNameTemplate(aData, source, myScope);
            break;
        default:
            ErrorReporter.INTERNAL_ERROR("FATAL ERROR while processing indexed template `" + getFullName() + "''");
            return;
    }
    if (indexedTemplates.getNofTemplates() == 0) {
        aData.addBuiltinTypeImport("TitanNull_Type");
        source.append(MessageFormat.format("{0}.assign(TitanNull_Type.NULL_VALUE);\n", name));
    }
    // else is not needed as the loop will not run
    for (int i = 0; i < indexedTemplates.getNofTemplates(); i++) {
        final IndexedTemplate indexedTemplate = indexedTemplates.getTemplateByIndex(i);
        final String tempId = aData.getTemporaryVariableName();
        source.append("{\n");
        final Value index = indexedTemplate.getIndex().getValue();
        if (Value_type.INTEGER_VALUE.equals(index.getValuetype())) {
            source.append(MessageFormat.format("{0} {1} = {2}.getAt({3});\n", ofTypeName, tempId, name, ((Integer_Value) index).getValue()));
        } else {
            final String tempId2 = aData.getTemporaryVariableName();
            source.append(MessageFormat.format("TitanInteger {0} = new TitanInteger();\n", tempId2));
            index.generateCodeInit(aData, source, tempId2);
            source.append(MessageFormat.format("{0} {1} = {2}.getAt({3});\n", ofTypeName, tempId, name, tempId2));
        }
        indexedTemplate.getTemplate().generateCodeInit(aData, source, tempId);
        source.append("}\n");
    }
    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 : SetOf_Type(org.eclipse.titan.designer.AST.TTCN3.types.SetOf_Type) Value(org.eclipse.titan.designer.AST.Value) SequenceOf_Value(org.eclipse.titan.designer.AST.TTCN3.values.SequenceOf_Value) IValue(org.eclipse.titan.designer.AST.IValue) IndexedValue(org.eclipse.titan.designer.AST.TTCN3.values.IndexedValue) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) Array_Type(org.eclipse.titan.designer.AST.TTCN3.types.Array_Type) IType(org.eclipse.titan.designer.AST.IType) SequenceOf_Type(org.eclipse.titan.designer.AST.TTCN3.types.SequenceOf_Type)

Example 2 with IndexedTemplate

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

the class Indexed_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;
                }
                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;
                }
                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:
            arrayIndex.getLocation().reportSemanticError(MessageFormat.format("Invalid array element reference: type `{0}'' cannot be indexed", tempType.getTypename()));
            return null;
    }
    for (int i = 0, size = indexedTemplates.getNofTemplates(); i < size; i++) {
        final IndexedTemplate template = indexedTemplates.getTemplateByIndex(i);
        IValue lastValue = template.getIndex().getValue();
        final IReferenceChain chain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
        lastValue = lastValue.getValueRefdLast(timestamp, chain);
        chain.release();
        if (Value_type.INTEGER_VALUE.equals(lastValue.getValuetype())) {
            final long tempIndex = ((Integer_Value) lastValue).getValue();
            if (index == tempIndex) {
                final ITTCN3Template realTemplate = template.getTemplate();
                if (Template_type.TEMPLATE_NOTUSED.equals(realTemplate.getTemplatetype())) {
                    if (baseTemplate != null) {
                        return baseTemplate.getTemplateReferencedLast(timestamp, referenceChain).getReferencedArrayTemplate(timestamp, indexValue, referenceChain);
                    }
                    return null;
                }
                return realTemplate;
            }
        }
    }
    switch(tempType.getTypetype()) {
        case TYPE_SEQUENCE_OF:
        case TYPE_SET_OF:
        case TYPE_ARRAY:
            // unfoldable (for now)
            break;
        default:
            // the error was reported earlier
            break;
    }
    return null;
}
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) ArrayDimension(org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension) IType(org.eclipse.titan.designer.AST.IType)

Example 3 with IndexedTemplate

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

the class Indexed_Template_List method getValue.

@Override
public /**
 * {@inheritDoc}
 */
IValue getValue() {
    if (asValue != null) {
        return asValue;
    }
    final Values values = new Values(true);
    for (int i = 0, size = getNofTemplates(); i < size; i++) {
        final IndexedTemplate indexedTemplate = indexedTemplates.getTemplateByIndex(i);
        final IndexedValue indexedValue = new IndexedValue(indexedTemplate.getIndex(), indexedTemplate.getTemplate().getValue());
        indexedValue.setLocation(indexedTemplate.getLocation());
        values.addIndexedValue(indexedValue);
    }
    asValue = new SequenceOf_Value(values);
    asValue.setLocation(getLocation());
    asValue.setMyScope(getMyScope());
    asValue.setFullNameParent(getNameParent());
    asValue.setMyGovernor(getMyGovernor());
    return asValue;
}
Also used : Values(org.eclipse.titan.designer.AST.TTCN3.values.Values) SequenceOf_Value(org.eclipse.titan.designer.AST.TTCN3.values.SequenceOf_Value) IndexedValue(org.eclipse.titan.designer.AST.TTCN3.values.IndexedValue)

Aggregations

IType (org.eclipse.titan.designer.AST.IType)2 IValue (org.eclipse.titan.designer.AST.IValue)2 IndexedValue (org.eclipse.titan.designer.AST.TTCN3.values.IndexedValue)2 Integer_Value (org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value)2 SequenceOf_Value (org.eclipse.titan.designer.AST.TTCN3.values.SequenceOf_Value)2 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)1 Array_Type (org.eclipse.titan.designer.AST.TTCN3.types.Array_Type)1 SequenceOf_Type (org.eclipse.titan.designer.AST.TTCN3.types.SequenceOf_Type)1 SetOf_Type (org.eclipse.titan.designer.AST.TTCN3.types.SetOf_Type)1 ArrayDimension (org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension)1 Values (org.eclipse.titan.designer.AST.TTCN3.values.Values)1 Value (org.eclipse.titan.designer.AST.Value)1