Search in sources :

Example 6 with EnumItem

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

the class ASN1_Enumerated_Type method checkEnumItem.

/**
 * Helper function for checking a single enumeration item. Checks if the
 * name of the item is not a duplicate, and its value is in correct
 * order. Also for items after the ellipsis if the value is missing a
 * new one is assigned.
 *
 * @param timestamp
 *                the timestamp of the actual semantic check cycle.
 * @param item
 *                the enumeration item to work on.
 * @param afterEllipsis
 *                true if the enumeration item is after the ellipsis.
 * @param valueMap
 *                a value map so that the correctness of the item's
 *                value can be checked.
 */
private final void checkEnumItem(final CompilationTimeStamp timestamp, final EnumItem item, final boolean afterEllipsis, final Map<Integer, EnumItem> valueMap) {
    final Identifier itemID = item.getId();
    if (nameMap.containsKey(itemID.getName())) {
        nameMap.get(itemID.getName()).getLocation().reportSingularSemanticError(MessageFormat.format(Assignments.DUPLICATEDEFINITIONFIRST, itemID.getDisplayName()));
        itemID.getLocation().reportSemanticError(MessageFormat.format(DUPLICATEENUMERATEDREPEATED, itemID.getDisplayName()));
    } else {
        nameMap.put(itemID.getName(), item);
    }
    if (!itemID.getHasValid(Identifier_type.ID_TTCN)) {
        itemID.getLocation().reportSemanticWarning(MessageFormat.format(ASN1Assignment.UNREACHABLE, itemID.getDisplayName()));
    }
    final Value value = item.getValue();
    if (!item.isOriginal()) {
        if (afterEllipsis) {
            while (valueMap.containsKey(firstUnused)) {
                firstUnused++;
            }
            valueMap.put(firstUnused, item);
            // again.
            if (null == value || ((Integer_Value) value).getValue() != firstUnused) {
                final Integer_Value tempValue = new Integer_Value(firstUnused.longValue());
                tempValue.setLocation(item.getLocation());
                item.setValue(tempValue);
            }
        }
        return;
    }
    final IReferenceChain referenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
    final IValue last = value.getValueRefdLast(timestamp, referenceChain);
    referenceChain.release();
    if (last.getIsErroneous(timestamp)) {
        return;
    }
    if (!Value_type.INTEGER_VALUE.equals(last.getValuetype())) {
        value.getLocation().reportSemanticError(MessageFormat.format("INTEGER value was expected for enumeration `{0}''", itemID.getDisplayName()));
        value.setIsErroneous(true);
        return;
    }
    final Integer_Value temp = (Integer_Value) last;
    if (!temp.isNative()) {
        value.getLocation().reportSemanticError(MessageFormat.format("The numeric value of enumeration `{0}'' ({1}) is too large for being represented in memory", itemID.getDisplayName(), temp.getValueValue()));
        value.setIsErroneous(true);
        return;
    }
    final Integer enumValue = Integer.valueOf(temp.intValue());
    if (afterEllipsis) {
        if (enumValue >= firstUnused) {
            valueMap.put(enumValue, item);
            while (valueMap.containsKey(firstUnused)) {
                firstUnused++;
            }
        } else {
            value.getLocation().reportSemanticError(MessageFormat.format("ENUMERATED values shall be monotonically growing after the ellipsis: the value of `{0}'' must be at least {1} instead of {2}", itemID.getDisplayName(), firstUnused, enumValue));
            value.setIsErroneous(true);
        }
    } else {
        if (valueMap.containsKey(enumValue)) {
            final Location tempLocation = valueMap.get(enumValue).getLocation();
            tempLocation.reportSingularSemanticError(MessageFormat.format(DUPLICATEDENUMERATIONVALUEFIRST, enumValue, valueMap.get(enumValue).getId().getDisplayName()));
            value.getLocation().reportSemanticError(MessageFormat.format(DUPLICATEDENUMERATIONVALUEREPEATED, enumValue, itemID.getDisplayName()));
            setIsErroneous(true);
        } else {
            valueMap.put(enumValue, item);
        }
    }
}
Also used : Identifier(org.eclipse.titan.designer.AST.Identifier) IValue(org.eclipse.titan.designer.AST.IValue) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain) 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) Enumerated_Value(org.eclipse.titan.designer.AST.TTCN3.values.Enumerated_Value) Undefined_LowerIdentifier_Value(org.eclipse.titan.designer.AST.TTCN3.values.Undefined_LowerIdentifier_Value) Referenced_Value(org.eclipse.titan.designer.AST.TTCN3.values.Referenced_Value) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) Location(org.eclipse.titan.designer.AST.Location)

Example 7 with EnumItem

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

the class ASN1_Enumerated_Type method addDeclaration.

@Override
public final /**
 * {@inheritDoc}
 */
void addDeclaration(final DeclarationCollector declarationCollector, final int i) {
    final List<ISubReference> subreferences = declarationCollector.getReference().getSubreferences();
    if (subreferences.size() <= i) {
        return;
    }
    final ISubReference subreference = subreferences.get(i);
    if (Subreference_type.fieldSubReference.equals(subreference.getReferenceType())) {
        if (subreferences.size() <= i + 1) {
            final String referenceName = subreference.getId().getName();
            if (enumerations.enumItems1 != null) {
                final List<EnumItem> enumItems = enumerations.enumItems1.getItems();
                for (EnumItem item : enumItems) {
                    final Identifier itemID = item.getId();
                    if (itemID.getName().startsWith(referenceName)) {
                        declarationCollector.addDeclaration(itemID.getDisplayName(), itemID.getLocation(), this);
                    }
                }
            }
            if (enumerations.enumItems2 != null) {
                final List<EnumItem> enumItems = enumerations.enumItems2.getItems();
                for (EnumItem item : enumItems) {
                    final Identifier itemID = item.getId();
                    if (itemID.getName().startsWith(referenceName)) {
                        declarationCollector.addDeclaration(itemID.getDisplayName(), itemID.getLocation(), this);
                    }
                }
            }
        }
    }
}
Also used : ISubReference(org.eclipse.titan.designer.AST.ISubReference) Identifier(org.eclipse.titan.designer.AST.Identifier) EnumItem(org.eclipse.titan.designer.AST.TTCN3.types.EnumItem)

Example 8 with EnumItem

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

the class ASN1_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 List<EnumItem> items = new ArrayList<EnumItem>();
    if (enumerations != null) {
        if (enumerations.enumItems1 != null) {
            items.addAll(enumerations.enumItems1.getItems());
        }
        if (enumerations.enumItems2 != null) {
            items.addAll(enumerations.enumItems2.getItems());
        }
    }
    final boolean hasRaw = getGenerateCoderFunctions(MessageEncoding_type.RAW);
    final ArrayList<Enum_field> fields = new ArrayList<EnumeratedGenerator.Enum_field>(items.size());
    for (int i = 0; i < items.size(); i++) {
        final EnumItem tempItem = items.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);
    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) EnumeratedGenerator(org.eclipse.titan.designer.AST.TTCN3.types.EnumeratedGenerator) Enum_Defs(org.eclipse.titan.designer.AST.TTCN3.types.EnumeratedGenerator.Enum_Defs) EnumItem(org.eclipse.titan.designer.AST.TTCN3.types.EnumItem)

Example 9 with EnumItem

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

the class ASN1_Enumerated_Type method check.

@Override
public final /**
 * {@inheritDoc}
 */
void check(final CompilationTimeStamp timestamp) {
    if (null != lastTimeChecked && !lastTimeChecked.isLess(timestamp)) {
        return;
    }
    lastTimeChecked = timestamp;
    if (null != myScope) {
        final Module module = myScope.getModuleScope();
        if (null != module) {
            if (module.getSkippedFromSemanticChecking()) {
                return;
            }
        }
    }
    isErroneous = false;
    if (null == enumerations) {
        parseBlockEnumeration();
    }
    if (isErroneous || null == enumerations) {
        return;
    }
    /* check duplications and set missing values */
    firstUnused = Integer.valueOf(0);
    nameMap = new HashMap<String, EnumItem>();
    final Map<Integer, EnumItem> valueMap = new HashMap<Integer, EnumItem>();
    if (null != enumerations.enumItems1) {
        final List<EnumItem> enumItems = enumerations.enumItems1.getItems();
        for (EnumItem item : enumItems) {
            checkEnumItem(timestamp, item, false, valueMap);
        }
        // set the default values
        while (valueMap.containsKey(firstUnused)) {
            firstUnused++;
        }
        for (EnumItem item : enumItems) {
            if (null == item.getValue() || !item.isOriginal()) {
                final Integer_Value tempValue = new Integer_Value(firstUnused.longValue());
                tempValue.setLocation(item.getLocation());
                item.setValue(tempValue);
                valueMap.put(firstUnused, item);
                while (valueMap.containsKey(firstUnused)) {
                    firstUnused++;
                }
            }
        }
    }
    if (null != enumerations.enumItems2) {
        final List<EnumItem> enumItems = enumerations.enumItems2.getItems();
        for (EnumItem item : enumItems) {
            checkEnumItem(timestamp, item, true, valueMap);
        }
    }
    if (null != constraints) {
        constraints.check(timestamp);
    }
    if (myScope != null) {
        checkEncode(timestamp);
        checkVariants(timestamp);
    }
}
Also used : HashMap(java.util.HashMap) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) Module(org.eclipse.titan.designer.AST.Module) EnumItem(org.eclipse.titan.designer.AST.TTCN3.types.EnumItem)

Example 10 with EnumItem

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

the class LessThanOrEqualExpression 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)

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