Search in sources :

Example 31 with Values

use of org.eclipse.titan.designer.AST.TTCN3.values.Values in project titan.EclipsePlug-ins by eclipse.

the class UnionGenerator method generateValueEncodeDecode.

/**
 * Generate encode/decode
 *
 * @param source where the source code is to be generated.
 * @param genName the name of the generated class representing the union/choice type.
 * @param displayName the user readable name of the type to be generated.
 * @param fieldInfos the list of information about the fields.
 * @param rawNeeded true if encoding/decoding for RAW is to be generated.
 * @param hasRaw true if the union has raw attributes.
 * @param raw the raw attributes or null.
 */
private static void generateValueEncodeDecode(final StringBuilder source, final String genName, final String displayName, final List<FieldInfo> fieldInfos, final boolean rawNeeded, final boolean hasRaw, final RawASTStruct raw) {
    source.append("@Override\n");
    source.append("public void encode(final TTCN_Typedescriptor p_td, final TTCN_Buffer p_buf, final coding_type p_coding, final int flavour) {\n");
    source.append("switch (p_coding) {\n");
    source.append("case CT_RAW: {\n");
    source.append("final TTCN_EncDec_ErrorContext errorContext = new TTCN_EncDec_ErrorContext(\"While RAW-encoding type '%s': \", p_td.name);\n");
    source.append("if (p_td.raw == null) {\n");
    source.append("TTCN_EncDec_ErrorContext.error_internal(\"No RAW descriptor available for type '%s'.\", p_td.name);\n");
    source.append("}\n");
    source.append("final RAW_enc_tr_pos rp = new RAW_enc_tr_pos(0, null);\n");
    source.append("final RAW_enc_tree root = new RAW_enc_tree(true, null, rp, 1, p_td.raw);\n");
    source.append("RAW_encode(p_td, root);\n");
    source.append("root.put_to_buf(p_buf);\n");
    source.append("errorContext.leaveContext();\n");
    source.append("break;\n");
    source.append("}\n");
    source.append("default:\n");
    source.append("throw new TtcnError(MessageFormat.format(\"Unknown coding method requested to encode type '{0}''\", p_td.name));\n");
    source.append("}\n");
    source.append("}\n\n");
    source.append("@Override\n");
    source.append("public void decode(final TTCN_Typedescriptor p_td, final TTCN_Buffer p_buf, final coding_type p_coding, final int flavour) {\n");
    source.append("switch (p_coding) {\n");
    source.append("case CT_RAW: {\n");
    source.append("final TTCN_EncDec_ErrorContext errorContext = new TTCN_EncDec_ErrorContext(\"While RAW-decoding type '%s': \", p_td.name);\n");
    source.append("if (p_td.raw == null) {\n");
    source.append("TTCN_EncDec_ErrorContext.error_internal(\"No RAW descriptor available for type '%s'.\", p_td.name);\n");
    source.append("}\n");
    source.append("raw_order_t order;\n");
    source.append("switch (p_td.raw.top_bit_order) {\n");
    source.append("case TOP_BIT_LEFT:\n");
    source.append("order = raw_order_t.ORDER_LSB;\n");
    source.append("break;\n");
    source.append("case TOP_BIT_RIGHT:\n");
    source.append("default:\n");
    source.append("order = raw_order_t.ORDER_MSB;\n");
    source.append("break;\n");
    source.append("}\n");
    source.append("int rawr = RAW_decode(p_td, p_buf, p_buf.get_len() * 8, order);\n");
    source.append("if (rawr < 0) {\n");
    source.append("error_type temp = error_type.values()[-rawr];\n");
    source.append("switch(temp) {\n");
    source.append("case ET_INCOMPL_MSG:\n");
    source.append("case ET_LEN_ERR:\n");
    source.append("TTCN_EncDec_ErrorContext.error(temp, \"Can not decode type '%s', because invalid or incomplete message was received\", p_td.name);\n");
    source.append("break;\n");
    source.append("case ET_UNBOUND:\n");
    source.append("default:\n");
    source.append("TTCN_EncDec_ErrorContext.error(error_type.ET_INVAL_MSG, \"Can not decode type '%s', because invalid or incomplete message was received\", p_td.name);\n");
    source.append("break;\n");
    source.append("}\n");
    source.append("}\n");
    source.append("errorContext.leaveContext();\n");
    source.append("break;\n");
    source.append("}\n");
    source.append("default:\n");
    source.append("throw new TtcnError(MessageFormat.format(\"Unknown coding method requested to decode type '{0}''\", p_td.name));\n");
    source.append("}\n");
    source.append("}\n\n");
    if (rawNeeded) {
        final int[] tag_type = new int[fieldInfos.size()];
        Arrays.fill(tag_type, 0);
        if (hasRaw && raw != null && raw.taglist != null && raw.taglist.list != null) {
            // fill tag_type. 0-No tag, >0 index of the tag + 1
            for (int i = 0; i < raw.taglist.list.size(); i++) {
                final rawAST_coding_taglist tempTaglist = raw.taglist.list.get(i);
                if (tempTaglist.fields != null && tempTaglist.fields.size() > 0) {
                    boolean found = false;
                    for (int v = 0; v < tempTaglist.fields.size(); v++) {
                        if (tempTaglist.fields.get(v).start_pos >= 0) {
                            found = true;
                            break;
                        }
                    }
                    if (found) {
                        tag_type[tempTaglist.fieldnum] = i + 1;
                    } else {
                        tag_type[tempTaglist.fieldnum] = -i + 1;
                    }
                }
            }
        }
        source.append("@Override\n");
        source.append("public int RAW_encode(final TTCN_Typedescriptor p_td, final RAW_enc_tree myleaf) {\n");
        source.append("int encoded_length = 0;\n");
        source.append("myleaf.isleaf = false;\n");
        source.append(MessageFormat.format("myleaf.num_of_nodes = {0};\n", fieldInfos.size()));
        source.append(MessageFormat.format("myleaf.nodes = new RAW_enc_tree[{0}];\n", fieldInfos.size()));
        source.append("switch (union_selection) {\n");
        for (int i = 0; i < fieldInfos.size(); i++) {
            final FieldInfo fieldInfo = fieldInfos.get(i);
            source.append(MessageFormat.format("case ALT_{0}:\n", fieldInfo.mJavaVarName));
            source.append(MessageFormat.format("myleaf.nodes[{0}] = new RAW_enc_tree(true, myleaf, myleaf.curr_pos, {0}, {1}_descr_.raw);\n", i, fieldInfo.mTypeDescriptorName));
            source.append(MessageFormat.format("encoded_length = field.RAW_encode({0}_descr_, myleaf.nodes[{1}]);\n", fieldInfo.mTypeDescriptorName, i));
            source.append(MessageFormat.format("myleaf.nodes[{0}].coding_descr = {1}_descr_;\n", i, fieldInfo.mTypeDescriptorName));
            final int t_type = tag_type[i] > 0 ? tag_type[i] : -tag_type[i];
            if (t_type > 0 && raw.taglist.list.get(t_type - 1).fields.size() > 0) {
                final rawAST_coding_taglist cur_choice = raw.taglist.list.get(t_type - 1);
                source.append(" if (");
                genRawFieldChecker(source, cur_choice, false);
                source.append(" ) {\n");
                genRawTagChecker(source, cur_choice);
                source.append("}\n");
            }
            source.append("break;\n");
        }
        source.append("default:\n");
        source.append("TTCN_EncDec_ErrorContext.error(error_type.ET_UNBOUND, \"Encoding an unbound value.\", \"\");\n");
        source.append("}\n");
        source.append("return encoded_length;\n");
        source.append("}\n\n");
        source.append("@Override\n");
        source.append("public int RAW_decode(final TTCN_Typedescriptor p_td, final TTCN_Buffer buff, int limit, final raw_order_t top_bit_ord, final boolean no_err, final int sel_field, final boolean first_call) {\n");
        source.append("final int prepaddlength = buff.increase_pos_padd(p_td.raw.prepadding);\n");
        source.append("limit -= prepaddlength;\n");
        source.append("int decoded_length = 0;\n");
        source.append("final int starting_pos = buff.get_pos_bit();\n");
        source.append("if (sel_field != -1) {\n");
        source.append("switch (sel_field) {\n");
        for (int i = 0; i < fieldInfos.size(); i++) {
            final FieldInfo fieldInfo = fieldInfos.get(i);
            source.append(MessageFormat.format("case {0}:\n", i));
            source.append(MessageFormat.format("decoded_length = get{0}().RAW_decode({1}_descr_, buff, limit, top_bit_ord, no_err, -1, true);\n", fieldInfo.mJavaVarName, fieldInfo.mTypeDescriptorName));
            source.append("break;\n");
        }
        source.append("default:\n");
        source.append("break;\n");
        source.append("}\n");
        source.append("return decoded_length + buff.increase_pos_padd(p_td.raw.padding) + prepaddlength;\n");
        source.append("} else {\n");
        for (int i = 0; i < fieldInfos.size(); i++) {
            if (tag_type[i] > 0 && raw.taglist.list.get(tag_type[i] - 1).fields.size() > 0) {
                source.append("boolean already_failed = false;\n");
                break;
            }
        }
        /* pre-calculate what we know about the temporal variables*/
        final ArrayList<TemporalVariable> tempVariableList = new ArrayList<UnionGenerator.TemporalVariable>();
        for (int i = 0; i < fieldInfos.size(); i++) {
            if (tag_type[i] > 0 && raw.taglist.list.get(tag_type[i] - 1).fields.size() > 0) {
                final rawAST_coding_taglist cur_choice = raw.taglist.list.get(tag_type[i] - 1);
                for (int j = 0; j < cur_choice.fields.size(); j++) {
                    final rawAST_coding_field_list fieldlist = cur_choice.fields.get(j);
                    if (fieldlist.start_pos >= 0) {
                        final rawAST_coding_fields lastCodingFields = fieldlist.fields.get(fieldlist.fields.size() - 1);
                        boolean found = false;
                        for (int k = 0; k < tempVariableList.size(); k++) {
                            final TemporalVariable temporalVariable = tempVariableList.get(k);
                            if (temporalVariable.start_pos == fieldlist.start_pos && temporalVariable.typedescriptor.equals(lastCodingFields.typedesc)) {
                                temporalVariable.use_counter++;
                                fieldlist.temporal_variable_index = k;
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            final TemporalVariable temp = new TemporalVariable();
                            temp.type = lastCodingFields.type;
                            temp.typedescriptor = lastCodingFields.typedesc;
                            temp.start_pos = fieldlist.start_pos;
                            temp.use_counter = 1;
                            temp.decoded_for_element = -1;
                            fieldlist.temporal_variable_index = tempVariableList.size();
                            tempVariableList.add(temp);
                        }
                    }
                }
            }
        }
        for (int i = 0; i < tempVariableList.size(); i++) {
            final TemporalVariable tempVariable = tempVariableList.get(i);
            if (tempVariable.use_counter > 1) {
                source.append(MessageFormat.format("{0} temporal_{1} = new {0}();\n", tempVariable.type, i));
                source.append(MessageFormat.format("int decoded_{0}_length = 0;\n", i));
            }
        }
        for (int i = 0; i < fieldInfos.size(); i++) {
            /* fields with tag */
            if (tag_type[i] > 0 && raw.taglist.list.get(tag_type[i] - 1).fields.size() > 0) {
                final FieldInfo fieldInfo = fieldInfos.get(i);
                final rawAST_coding_taglist cur_choice = raw.taglist.list.get(tag_type[i] - 1);
                // TODO already_failed handling could be optimized!
                source.append("already_failed = false;\n");
                /* try to decode those key variables whose position we know
					 * this way we might be able to step over bad values faster
					 */
                for (int j = 0; j < cur_choice.fields.size(); j++) {
                    final rawAST_coding_field_list cur_field_list = cur_choice.fields.get(j);
                    if (cur_field_list.start_pos >= 0) {
                        final int variableIndex = cur_field_list.temporal_variable_index;
                        final TemporalVariable tempVariable = tempVariableList.get(variableIndex);
                        if (tempVariable.decoded_for_element == i) {
                            continue;
                        }
                        source.append("if (!already_failed) {\n");
                        if (tempVariable.use_counter == 1) {
                            source.append(MessageFormat.format("{0} temporal_{1} = new {0}();\n", tempVariable.type, variableIndex));
                            source.append(MessageFormat.format("int decoded_{0}_length;\n", variableIndex));
                        }
                        if (tempVariable.decoded_for_element == -1) {
                            source.append(MessageFormat.format("buff.set_pos_bit(starting_pos + {0});\n", cur_field_list.start_pos));
                            source.append(MessageFormat.format("decoded_{0}_length = temporal_{0}.RAW_decode({1}_descr_, buff, limit, top_bit_ord, true, -1, true);\n", variableIndex, tempVariable.typedescriptor));
                        }
                        tempVariable.decoded_for_element = i;
                        source.append(MessageFormat.format("if (decoded_{0}_length > 0) '{'\n", variableIndex));
                        source.append(MessageFormat.format("if (temporal_{0}.operatorEquals({1})", variableIndex, cur_field_list.expression.expression));
                        for (int k = j + 1; k < cur_choice.fields.size(); k++) {
                            final rawAST_coding_field_list tempFieldList = cur_choice.fields.get(k);
                            if (tempFieldList.temporal_variable_index == variableIndex) {
                                source.append(MessageFormat.format(" || temporal_{0}.operatorEquals({1})", variableIndex, tempFieldList.expression.expression));
                            }
                        }
                        source.append(") {\n");
                        source.append("buff.set_pos_bit(starting_pos);\n");
                        source.append(MessageFormat.format("decoded_length = get{0}().RAW_decode({1}_descr_, buff, limit, top_bit_ord, true, -1, true);\n", fieldInfo.mJavaVarName, fieldInfo.mTypeDescriptorName));
                        source.append("if (decoded_length > 0) {\n");
                        source.append("if (");
                        genRawFieldChecker(source, cur_choice, true);
                        source.append(") {\n");
                        source.append("return decoded_length + buff.increase_pos_padd(p_td.raw.padding) + prepaddlength;\n");
                        source.append("} else {\n");
                        source.append("already_failed = true;\n");
                        source.append("}\n");
                        source.append("}\n");
                        source.append("}\n");
                        source.append("}\n");
                        source.append("}\n");
                    }
                }
                /* if there is one tag key whose position we don't know
					 * and we couldn't decide yet if the element can be decoded or not
					 * than we have to decode it.
					 * note that this is not actually a cycle because of the break
					 */
                for (int j = 0; j < cur_choice.fields.size(); j++) {
                    if (cur_choice.fields.get(j).start_pos < 0) {
                        source.append("if (already_failed) {\n");
                        source.append("buff.set_pos_bit(starting_pos);\n");
                        source.append(MessageFormat.format("decoded_length = get{0}().RAW_decode({1}_descr_, buff, limit, top_bit_ord, true, -1, true);\n", fieldInfo.mJavaVarName, fieldInfo.mTypeDescriptorName));
                        source.append("if (decoded_length > 0) {\n");
                        source.append("if (");
                        genRawFieldChecker(source, cur_choice, true);
                        source.append(") {\n");
                        source.append("return decoded_length + buff.increase_pos_padd(p_td.raw.padding) + prepaddlength;\n");
                        source.append("}\n");
                        source.append("}\n");
                        source.append("}\n");
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < fieldInfos.size(); i++) {
            if (tag_type[i] < 0 && raw.taglist.list.get(-1 * tag_type[i] - 1).fields.size() > 0) {
                final FieldInfo fieldInfo = fieldInfos.get(i);
                final rawAST_coding_taglist cur_choice = raw.taglist.list.get(-1 * tag_type[i] - 1);
                source.append("buff.set_pos_bit(starting_pos);\n");
                source.append(MessageFormat.format("decoded_length = get{0}().RAW_decode({1}_descr_, buff, limit, top_bit_ord, true, -1, true);\n", fieldInfo.mJavaVarName, fieldInfo.mTypeDescriptorName));
                source.append("if (decoded_length >= 0) {\n");
                source.append("if (");
                genRawFieldChecker(source, cur_choice, true);
                source.append(") {\n");
                source.append("return decoded_length + buff.increase_pos_padd(p_td.raw.padding) + prepaddlength;\n");
                source.append("}\n");
                source.append("}\n");
            }
        }
        for (int i = 0; i < fieldInfos.size(); i++) {
            if (tag_type[i] == 0) {
                final FieldInfo fieldInfo = fieldInfos.get(i);
                source.append("buff.set_pos_bit(starting_pos);\n");
                source.append(MessageFormat.format("decoded_length = get{0}().RAW_decode({1}_descr_, buff, limit, top_bit_ord, true, -1, true);\n", fieldInfo.mJavaVarName, fieldInfo.mTypeDescriptorName));
                source.append("if (decoded_length >= 0) {\n");
                source.append("return decoded_length + buff.increase_pos_padd(p_td.raw.padding) + prepaddlength;\n");
                source.append("}\n");
            }
        }
        source.append("}\n");
        source.append("cleanUp();\n");
        source.append("return -1;\n");
        source.append("}\n\n");
    }
}
Also used : RawASTStruct.rawAST_coding_field_list(org.eclipse.titan.designer.AST.TTCN3.attributes.RawASTStruct.rawAST_coding_field_list) RawASTStruct.rawAST_coding_taglist(org.eclipse.titan.designer.AST.TTCN3.attributes.RawASTStruct.rawAST_coding_taglist) ArrayList(java.util.ArrayList) RawASTStruct.rawAST_coding_fields(org.eclipse.titan.designer.AST.TTCN3.attributes.RawASTStruct.rawAST_coding_fields)

Example 32 with Values

use of org.eclipse.titan.designer.AST.TTCN3.values.Values in project titan.EclipsePlug-ins by eclipse.

the class Integer_Type method checkThisValueLimit.

/**
 * Checks if a given value is a valid integer limit.
 * <p>
 * The special float values infinity and -infinity are valid integer range limits too.
 *
 * @param timestamp the time stamp of the actual semantic check cycle.
 * @param value the value to be checked
 * @param expectedValue the kind of the value to be expected
 * @param incompleteAllowed true if an incomplete value can be accepted at the given location, false otherwise
 * @param omitAllowed true if the omit value can be accepted at the given location, false otherwise
 * @param subCheck true if the subtypes should also be checked.
 * @param implicitOmit true if the implicit omit optional attribute was set for the value, false otherwise
 */
public void checkThisValueLimit(final CompilationTimeStamp timestamp, final IValue value, final Expected_Value_type expectedValue, final boolean incompleteAllowed, final boolean omitAllowed, final boolean subCheck, final boolean implicitOmit) {
    super.checkThisValue(timestamp, value, null, new ValueCheckingOptions(expectedValue, incompleteAllowed, omitAllowed, subCheck, implicitOmit, false));
    final IValue last = value.getValueRefdLast(timestamp, expectedValue, null);
    if (last == null || last.getIsErroneous(timestamp)) {
        return;
    }
    // already handled ones
    switch(value.getValuetype()) {
        case OMIT_VALUE:
        case REFERENCED_VALUE:
            return;
        case UNDEFINED_LOWERIDENTIFIER_VALUE:
            if (Value_type.REFERENCED_VALUE.equals(last.getValuetype())) {
                return;
            }
            break;
        default:
            break;
    }
    switch(last.getValuetype()) {
        case INTEGER_VALUE:
            break;
        case REAL_VALUE:
            {
                final Real_Value real = (Real_Value) last;
                if (!real.isNegativeInfinity() && !real.isPositiveInfinity()) {
                    value.getLocation().reportSemanticError(INTEGERVALUEEXPECTED);
                    value.setIsErroneous(true);
                }
                break;
            }
        case EXPRESSION_VALUE:
        case MACRO_VALUE:
            // already checked
            break;
        default:
            value.getLocation().reportSemanticError(INTEGERVALUEEXPECTED);
            value.setIsErroneous(true);
    }
    if (subCheck) {
        // there is no parent type to check
        if (subType != null) {
            subType.checkThisValue(timestamp, last);
        }
    }
}
Also used : IValue(org.eclipse.titan.designer.AST.IValue) Real_Value(org.eclipse.titan.designer.AST.TTCN3.values.Real_Value)

Example 33 with Values

use of org.eclipse.titan.designer.AST.TTCN3.values.Values in project titan.EclipsePlug-ins by eclipse.

the class Named_Template_List method getValue.

@Override
public /**
 * {@inheritDoc}
 */
IValue getValue() {
    if (asValue != null) {
        return asValue;
    }
    final NamedValues values = new NamedValues();
    for (int i = 0, size = getNofTemplates(); i < size; i++) {
        final NamedTemplate namedTemplate = namedTemplates.getTemplateByIndex(i);
        final NamedValue namedValue = new NamedValue(namedTemplate.getName(), namedTemplate.getTemplate().getValue());
        namedValue.setLocation(namedTemplate.getLocation());
        values.addNamedValue(namedValue);
    }
    asValue = new Sequence_Value(values);
    asValue.setLocation(getLocation());
    asValue.setMyScope(getMyScope());
    asValue.setFullNameParent(getNameParent());
    asValue.setMyGovernor(getMyGovernor());
    return asValue;
}
Also used : Sequence_Value(org.eclipse.titan.designer.AST.TTCN3.values.Sequence_Value) NamedValue(org.eclipse.titan.designer.AST.TTCN3.values.NamedValue) NamedValues(org.eclipse.titan.designer.AST.TTCN3.values.NamedValues)

Aggregations

IValue (org.eclipse.titan.designer.AST.IValue)18 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)11 Identifier (org.eclipse.titan.designer.AST.Identifier)9 Integer_Value (org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value)9 HashMap (java.util.HashMap)8 IType (org.eclipse.titan.designer.AST.IType)6 SequenceOf_Value (org.eclipse.titan.designer.AST.TTCN3.values.SequenceOf_Value)5 Value (org.eclipse.titan.designer.AST.Value)5 BigInteger (java.math.BigInteger)4 ArrayList (java.util.ArrayList)3 ISubReference (org.eclipse.titan.designer.AST.ISubReference)3 Module (org.eclipse.titan.designer.AST.Module)3 ParameterisedSubReference (org.eclipse.titan.designer.AST.ParameterisedSubReference)3 ReferenceChain (org.eclipse.titan.designer.AST.ReferenceChain)3 NamedValue (org.eclipse.titan.designer.AST.TTCN3.values.NamedValue)3 Referenced_Value (org.eclipse.titan.designer.AST.TTCN3.values.Referenced_Value)3 Undefined_LowerIdentifier_Value (org.eclipse.titan.designer.AST.TTCN3.values.Undefined_LowerIdentifier_Value)3 List (java.util.List)2 Named_Integer_Value (org.eclipse.titan.designer.AST.ASN1.values.Named_Integer_Value)2 ArraySubReference (org.eclipse.titan.designer.AST.ArraySubReference)2