Search in sources :

Example 11 with EnumItem

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

the class GreaterThanExpression method evaluateValue.

@Override
public /**
 * {@inheritDoc}
 */
IValue evaluateValue(final CompilationTimeStamp timestamp, final Expected_Value_type expectedValue, final IReferenceChain referenceChain) {
    if (lastTimeChecked != null && !lastTimeChecked.isLess(timestamp)) {
        return lastValue;
    }
    isErroneous = false;
    lastTimeChecked = timestamp;
    lastValue = this;
    if (value1 == null || value2 == null) {
        return lastValue;
    }
    checkExpressionOperands(timestamp, expectedValue, referenceChain);
    if (getIsErroneous(timestamp) || isUnfoldable(timestamp, referenceChain)) {
        return lastValue;
    }
    final IValue last1 = value1.getValueRefdLast(timestamp, referenceChain);
    final IValue last2 = value2.getValueRefdLast(timestamp, referenceChain);
    if (last1.getIsErroneous(timestamp) || last2.getIsErroneous(timestamp)) {
        setIsErroneous(true);
        return lastValue;
    }
    switch(last1.getValuetype()) {
        case INTEGER_VALUE:
            {
                lastValue = new Boolean_Value(((Integer_Value) last1).compareTo((Integer_Value) last2) > 0);
                lastValue.copyGeneralProperties(this);
                break;
            }
        case REAL_VALUE:
            {
                final double float1 = ((Real_Value) last1).getValue();
                final double float2 = ((Real_Value) last2).getValue();
                lastValue = new Boolean_Value(Double.compare(float1, float2) > 0);
                lastValue.copyGeneralProperties(this);
                break;
            }
        case ENUMERATED_VALUE:
            {
                IType governor1 = last1.getExpressionGovernor(timestamp, expectedValue);
                governor1 = governor1.getTypeRefdLast(timestamp);
                IType governor2 = last2.getExpressionGovernor(timestamp, expectedValue);
                governor2 = governor2.getTypeRefdLast(timestamp);
                if (governor1 instanceof TTCN3_Enumerated_Type) {
                    final EnumItem item1 = ((TTCN3_Enumerated_Type) governor1).getEnumItemWithName(((Enumerated_Value) last1).getValue());
                    final EnumItem item2 = ((TTCN3_Enumerated_Type) governor2).getEnumItemWithName(((Enumerated_Value) last2).getValue());
                    lastValue = new Boolean_Value(((Integer_Value) item1.getValue()).intValue() > ((Integer_Value) item2.getValue()).intValue());
                    lastValue.copyGeneralProperties(this);
                } else {
                    final EnumItem item1 = ((ASN1_Enumerated_Type) governor1).getEnumItemWithName(((Enumerated_Value) last1).getValue());
                    final EnumItem item2 = ((ASN1_Enumerated_Type) governor2).getEnumItemWithName(((Enumerated_Value) last2).getValue());
                    lastValue = new Boolean_Value(((Integer_Value) item1.getValue()).intValue() > ((Integer_Value) item2.getValue()).intValue());
                    lastValue.copyGeneralProperties(this);
                }
                break;
            }
        default:
            setIsErroneous(true);
    }
    return lastValue;
}
Also used : Boolean_Value(org.eclipse.titan.designer.AST.TTCN3.values.Boolean_Value) IValue(org.eclipse.titan.designer.AST.IValue) TTCN3_Enumerated_Type(org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Enumerated_Type) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) EnumItem(org.eclipse.titan.designer.AST.TTCN3.types.EnumItem) Enumerated_Value(org.eclipse.titan.designer.AST.TTCN3.values.Enumerated_Value) IType(org.eclipse.titan.designer.AST.IType)

Example 12 with EnumItem

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

the class TTCN3_Enumerated_Type method generateCode.

/**
 * Add generated java code on this level.
 * @param aData only used to update imports if needed
 * @param source the source code generated
 */
@Override
public /**
 * {@inheritDoc}
 */
void generateCode(final JavaGenData aData, final StringBuilder source) {
    final String ownName = getGenNameOwn();
    final String displayName = getFullName();
    generateCodeTypedescriptor(aData, source);
    final boolean hasRaw = getGenerateCoderFunctions(MessageEncoding_type.RAW);
    final ArrayList<Enum_field> fields = new ArrayList<EnumeratedGenerator.Enum_field>(items.getItems().size());
    for (int i = 0; i < items.getItems().size(); i++) {
        final EnumItem tempItem = items.getItems().get(i);
        fields.add(new Enum_field(tempItem.getId().getName(), tempItem.getId().getDisplayName(), ((Integer_Value) tempItem.getValue()).getValue()));
    }
    final Enum_Defs e_defs = new Enum_Defs(fields, ownName, displayName, getGenNameTemplate(aData, source, myScope), hasRaw);
    EnumeratedGenerator.generateValueClass(aData, source, e_defs);
    EnumeratedGenerator.generateTemplateClass(aData, source, e_defs);
    if (hasDoneAttribute()) {
        generateCodeDone(aData, source);
    }
    if (subType != null) {
        subType.generateCode(aData, source);
    }
    generateCodeForCodingHandlers(aData, source);
}
Also used : Enum_field(org.eclipse.titan.designer.AST.TTCN3.types.EnumeratedGenerator.Enum_field) ArrayList(java.util.ArrayList) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) Enum_Defs(org.eclipse.titan.designer.AST.TTCN3.types.EnumeratedGenerator.Enum_Defs)

Example 13 with EnumItem

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

the class TTCN3_Enumerated_Type method check.

/**
 * Does the semantic checking of the enumerations.
 *
 * @param timestamp the timestamp of the actual semantic check cycle.
 */
@Override
public /**
 * {@inheritDoc}
 */
void check(final CompilationTimeStamp timestamp) {
    if (lastTimeChecked != null && !lastTimeChecked.isLess(timestamp)) {
        return;
    }
    parseAttributes(timestamp);
    nameMap = new HashMap<String, EnumItem>(items.getItems().size());
    final Map<Long, EnumItem> valueMap = new HashMap<Long, EnumItem>(items.getItems().size());
    final List<EnumItem> enumItems = items.getItems();
    // check duplicated names and values
    for (int i = 0, size = enumItems.size(); i < size; i++) {
        final EnumItem item = enumItems.get(i);
        final Identifier id = item.getId();
        final String fieldName = id.getName();
        if (nameMap.containsKey(fieldName)) {
            nameMap.get(fieldName).getId().getLocation().reportSingularSemanticError(MessageFormat.format(DUPLICATEENUMERATIONIDENTIFIERFIRST, id.getDisplayName()));
            id.getLocation().reportSemanticError(MessageFormat.format(DUPLICATEENUMERATIONIDENTIFIERREPEATED, id.getDisplayName()));
        } else {
            nameMap.put(fieldName, item);
        }
        final Value value = item.getValue();
        if (value != null && item.isOriginal()) {
            if (value.getIsErroneous(timestamp) || !Value_type.INTEGER_VALUE.equals(value.getValuetype())) {
                value.getLocation().reportSemanticError(MessageFormat.format("INTEGER value was expected for enumeration `{0}''", id.getDisplayName()));
                setIsErroneous(true);
            } else {
                final Integer_Value enumValue = (Integer_Value) value;
                if (!enumValue.isNative()) {
                    enumValue.getLocation().reportSemanticError(MessageFormat.format(LARGEINTEGERERROR, enumValue.getValueValue()));
                    setIsErroneous(true);
                } else {
                    final Long enumLong = enumValue.getValue();
                    if (valueMap.containsKey(enumLong)) {
                        valueMap.get(enumLong).getLocation().reportSingularSemanticError(MessageFormat.format(DUPLICATEDENUMERATIONVALUEFIRST, enumLong, valueMap.get(enumLong).getId().getDisplayName()));
                        value.getLocation().reportSemanticError(MessageFormat.format(DUPLICATEDENUMERATIONVALUEREPEATED, enumLong, id.getDisplayName()));
                        setIsErroneous(true);
                    } else {
                        valueMap.put(enumLong, item);
                    }
                }
            }
        }
    }
    // Assign default values
    if (!getIsErroneous(timestamp) && lastTimeChecked == null) {
        Long firstUnused = Long.valueOf(0);
        while (valueMap.containsKey(firstUnused)) {
            firstUnused++;
        }
        for (int i = 0, size = enumItems.size(); i < size; i++) {
            final EnumItem item = enumItems.get(i);
            if (!item.isOriginal()) {
                // optimization: if the same value was already assigned, there is no need to create it again.
                final IValue value = item.getValue();
                if (value == null || ((Integer_Value) value).getValue() != firstUnused) {
                    final Integer_Value tempValue = new Integer_Value(firstUnused.longValue());
                    tempValue.setLocation(item.getLocation());
                    item.setValue(tempValue);
                }
                valueMap.put(firstUnused, item);
                firstUnused = Long.valueOf(firstUnused.longValue() + 1);
                while (valueMap.containsKey(firstUnused)) {
                    firstUnused++;
                }
            }
        }
    }
    valueMap.clear();
    lastTimeChecked = timestamp;
    checkSubtypeRestrictions(timestamp);
    if (myScope != null) {
        checkEncode(timestamp);
        checkVariants(timestamp);
    }
}
Also used : Identifier(org.eclipse.titan.designer.AST.Identifier) IValue(org.eclipse.titan.designer.AST.IValue) HashMap(java.util.HashMap) Value(org.eclipse.titan.designer.AST.Value) IValue(org.eclipse.titan.designer.AST.IValue) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) Undefined_LowerIdentifier_Value(org.eclipse.titan.designer.AST.TTCN3.values.Undefined_LowerIdentifier_Value) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value)

Aggregations

EnumItem (org.eclipse.titan.designer.AST.TTCN3.types.EnumItem)10 Integer_Value (org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value)10 IValue (org.eclipse.titan.designer.AST.IValue)7 Enumerated_Value (org.eclipse.titan.designer.AST.TTCN3.values.Enumerated_Value)6 IType (org.eclipse.titan.designer.AST.IType)5 Identifier (org.eclipse.titan.designer.AST.Identifier)4 TTCN3_Enumerated_Type (org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Enumerated_Type)4 Boolean_Value (org.eclipse.titan.designer.AST.TTCN3.values.Boolean_Value)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ISubReference (org.eclipse.titan.designer.AST.ISubReference)2 Enum_Defs (org.eclipse.titan.designer.AST.TTCN3.types.EnumeratedGenerator.Enum_Defs)2 Enum_field (org.eclipse.titan.designer.AST.TTCN3.types.EnumeratedGenerator.Enum_field)2 Undefined_LowerIdentifier_Value (org.eclipse.titan.designer.AST.TTCN3.values.Undefined_LowerIdentifier_Value)2 Value (org.eclipse.titan.designer.AST.Value)2 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)1 Location (org.eclipse.titan.designer.AST.Location)1 Module (org.eclipse.titan.designer.AST.Module)1 EnumeratedGenerator (org.eclipse.titan.designer.AST.TTCN3.types.EnumeratedGenerator)1 Referenced_Value (org.eclipse.titan.designer.AST.TTCN3.values.Referenced_Value)1