use of org.eclipse.titan.designer.AST.TTCN3.types.UnionGenerator.FieldInfo in project titan.EclipsePlug-ins by eclipse.
the class TTCN3_Set_Seq_Choice_BaseType method convertRAWCodingAttributes.
// FIXME comment
protected RawASTStruct convertRAWCodingAttributes(final JavaGenData aData, final StringBuilder source, final boolean hasRaw, final List<FieldInfo> namesList) {
RawASTStruct raw = null;
if (hasRaw) {
RawAST dummy_raw;
if (rawAttribute == null) {
dummy_raw = new RawAST(getDefaultRawFieldLength());
} else {
dummy_raw = rawAttribute;
}
raw = new RawASTStruct(dummy_raw);
// building taglist
final int taglistSize = dummy_raw.taglist == null ? 0 : dummy_raw.taglist.size();
for (int c = 0; c < taglistSize; c++) {
final rawAST_single_tag singleTag = dummy_raw.taglist.get(c);
final rawAST_coding_taglist codingSingleTag = raw.taglist.list.get(c);
if (singleTag.keyList != null) {
codingSingleTag.fields = new ArrayList<RawASTStruct.rawAST_coding_field_list>(singleTag.keyList.size());
}
codingSingleTag.fieldname = singleTag.fieldName.getName();
codingSingleTag.varName = FieldSubReference.getJavaGetterName(codingSingleTag.fieldname);
final Identifier idf = singleTag.fieldName;
codingSingleTag.fieldnum = getComponentIndexByName(idf);
final int keyListSize = singleTag.keyList == null ? 0 : singleTag.keyList.size();
for (int a = 0; a < keyListSize; a++) {
final rawAST_tag_field_value key = singleTag.keyList.get(a);
final RawASTStruct.rawAST_coding_field_list codingKey = new RawASTStruct.rawAST_coding_field_list();
codingSingleTag.fields.add(codingKey);
codingKey.fields = new ArrayList<RawASTStruct.rawAST_coding_fields>(key.keyField.names.size());
// codingKey.value = key.value;
final ExpressionStruct expression = new ExpressionStruct();
key.v_value.generateCodeExpression(aData, expression, true);
codingKey.expression = expression;
codingKey.isOmitValue = key.v_value.getValuetype() == Value_type.OMIT_VALUE;
codingKey.start_pos = 0;
final CompField cf = getComponentByIndex(codingSingleTag.fieldnum);
IType t = cf.getType().getTypeRefdLast(CompilationTimeStamp.getBaseTimestamp());
final RawASTStruct.rawAST_coding_fields tempField = new rawAST_coding_fields();
tempField.nthfield = codingSingleTag.fieldnum;
tempField.nthfieldname = singleTag.fieldName.getName();
tempField.fieldtype = rawAST_coding_field_type.UNION_FIELD;
tempField.type = t.getGenNameValue(aData, source, myScope);
tempField.typedesc = t.getGenNameTypeDescriptor(aData, source, myScope);
if (cf.isOptional()) {
tempField.fieldtype = rawAST_coding_field_type.OPTIONAL_FIELD;
} else {
tempField.fieldtype = rawAST_coding_field_type.MANDATORY_FIELD;
}
codingKey.fields.add(tempField);
for (int b = 0; b < key.keyField.names.size(); b++) {
final RawASTStruct.rawAST_coding_fields newField = new rawAST_coding_fields();
codingKey.fields.add(newField);
final Identifier idf2 = key.keyField.names.get(b);
int comp_index = 0;
CompField cf2;
switch(t.getTypetype()) {
case TYPE_TTCN3_CHOICE:
comp_index = ((TTCN3_Choice_Type) t).getComponentIndexByName(idf2);
cf2 = ((TTCN3_Choice_Type) t).getComponentByIndex(comp_index);
newField.nthfield = comp_index;
newField.nthfieldname = idf2.getName();
newField.fieldtype = rawAST_coding_field_type.UNION_FIELD;
break;
case TYPE_TTCN3_SEQUENCE:
case TYPE_TTCN3_SET:
comp_index = ((TTCN3_Set_Seq_Choice_BaseType) t).getComponentIndexByName(idf2);
cf2 = ((TTCN3_Set_Seq_Choice_BaseType) t).getComponentByIndex(comp_index);
newField.nthfield = comp_index;
newField.nthfieldname = idf2.getName();
if (cf2.isOptional()) {
newField.fieldtype = rawAST_coding_field_type.OPTIONAL_FIELD;
} else {
newField.fieldtype = rawAST_coding_field_type.MANDATORY_FIELD;
}
break;
default:
// internal error
return null;
}
final IType field_type = cf2.getType();
newField.type = field_type.getGenNameValue(aData, source, myScope);
newField.typedesc = field_type.getGenNameTypeDescriptor(aData, source, myScope);
if (field_type.getTypetype() == Type_type.TYPE_TTCN3_SEQUENCE && ((TTCN3_Sequence_Type) field_type).rawAttribute != null && (((TTCN3_Sequence_Type) field_type).rawAttribute.pointerto == null || ((TTCN3_Sequence_Type) field_type).rawAttribute.lengthto != null)) {
codingKey.start_pos = -1;
}
if (t.getTypetype() == Type_type.TYPE_TTCN3_SEQUENCE) {
IType t2;
for (int i = 0; i < comp_index && codingKey.start_pos >= 0; i++) {
t2 = ((TTCN3_Sequence_Type) t).getComponentByIndex(i).getType();
if (t2.getRawLength() >= 0) {
if (((Type) t2).rawAttribute != null) {
codingKey.start_pos += ((Type) t2).rawAttribute.padding;
}
codingKey.start_pos += ((Type) t2).getRawLength();
} else {
codingKey.start_pos = -1;
}
}
}
t = field_type.getTypeRefdLast(CompilationTimeStamp.getBaseTimestamp());
}
}
}
// building presence list
final int presenceListSize = dummy_raw.presence == null || dummy_raw.presence.keyList == null ? 0 : dummy_raw.presence.keyList.size();
for (int a = 0; a < presenceListSize; a++) {
final rawAST_tag_field_value fieldValue = dummy_raw.presence.keyList.get(a);
final rawAST_coding_field_list presences = new rawAST_coding_field_list();
raw.presence.fields.add(presences);
final ExpressionStruct expression = new ExpressionStruct();
fieldValue.v_value.generateCodeExpression(aData, expression, true);
presences.expression = expression;
presences.isOmitValue = fieldValue.v_value.getValuetype() == Value_type.OMIT_VALUE;
final int keySize = fieldValue.keyField == null || fieldValue.keyField.names == null ? 0 : fieldValue.keyField.names.size();
presences.fields = new ArrayList<RawASTStruct.rawAST_coding_fields>(keySize);
IType t = this;
for (int b = 0; b < keySize; b++) {
final RawASTStruct.rawAST_coding_fields newField = new rawAST_coding_fields();
presences.fields.add(newField);
final Identifier idf2 = fieldValue.keyField.names.get(b);
int comp_index = 0;
CompField cf2;
switch(t.getTypetype()) {
case TYPE_TTCN3_CHOICE:
comp_index = ((TTCN3_Choice_Type) t).getComponentIndexByName(idf2);
cf2 = ((TTCN3_Choice_Type) t).getComponentByIndex(comp_index);
newField.nthfield = comp_index;
newField.nthfieldname = idf2.getName();
newField.fieldtype = rawAST_coding_field_type.UNION_FIELD;
break;
case TYPE_TTCN3_SEQUENCE:
case TYPE_TTCN3_SET:
comp_index = ((TTCN3_Set_Seq_Choice_BaseType) t).getComponentIndexByName(idf2);
cf2 = ((TTCN3_Set_Seq_Choice_BaseType) t).getComponentByIndex(comp_index);
newField.nthfield = comp_index;
newField.nthfieldname = idf2.getName();
if (cf2.isOptional()) {
newField.fieldtype = rawAST_coding_field_type.OPTIONAL_FIELD;
} else {
newField.fieldtype = rawAST_coding_field_type.MANDATORY_FIELD;
}
break;
default:
// internal error
return null;
}
final IType field_type = cf2.getType();
newField.type = field_type.getGenNameValue(aData, source, myScope);
newField.typedesc = field_type.getGenNameTypeDescriptor(aData, source, myScope);
t = field_type.getTypeRefdLast(CompilationTimeStamp.getBaseTimestamp());
}
}
final int extBiGroupSize = dummy_raw.ext_bit_groups == null ? 0 : dummy_raw.ext_bit_groups.size();
for (int c = 0; c < extBiGroupSize; c++) {
final rawAST_ext_bit_group tempGroup = dummy_raw.ext_bit_groups.get(c);
final Identifier idf = tempGroup.from;
final Identifier idf2 = tempGroup.to;
final rawAST_coding_ext_group codingGroup = new rawAST_coding_ext_group();
raw.ext_bit_groups.add(codingGroup);
codingGroup.ext_bit = tempGroup.ext_bit;
codingGroup.from = getComponentIndexByName(idf);
codingGroup.to = getComponentIndexByName(idf2);
}
for (int i = 0; i < getNofComponents(); i++) {
final FieldInfo element_i = namesList.get(i);
final CompField cf = getComponentByIndex(i);
final IType t_field = cf.getType();
final IType t_field_last = t_field.getTypeRefdLast(CompilationTimeStamp.getBaseTimestamp());
final RawAST rawpar = t_field.getRawAttribute();
if (rawpar != null) {
element_i.raw = new RawASTStruct(rawpar);
final int lengthtoNum = rawpar.lengthto == null ? 0 : rawpar.lengthto.size();
for (int j = 0; j < lengthtoNum; j++) {
final Identifier idf = rawpar.lengthto.get(j);
element_i.raw.lengthto.add(getComponentIndexByName(idf));
}
if (lengthtoNum > 0 && rawpar.lengthindex != null) {
final Identifier idf = rawpar.lengthindex.names.get(0);
int comp_index = 0;
CompField cf2;
switch(t_field_last.getTypetype()) {
case TYPE_TTCN3_CHOICE:
comp_index = ((TTCN3_Choice_Type) t_field_last).getComponentIndexByName(idf);
cf2 = ((TTCN3_Choice_Type) t_field_last).getComponentByIndex(comp_index);
element_i.raw.lengthindex.nthfield = comp_index;
element_i.raw.lengthindex.nthfieldname = idf.getName();
break;
case TYPE_TTCN3_SEQUENCE:
case TYPE_TTCN3_SET:
comp_index = ((TTCN3_Set_Seq_Choice_BaseType) t_field_last).getComponentIndexByName(idf);
cf2 = ((TTCN3_Set_Seq_Choice_BaseType) t_field_last).getComponentByIndex(comp_index);
element_i.raw.lengthindex.nthfield = comp_index;
element_i.raw.lengthindex.nthfieldname = idf.getName();
break;
default:
// internal error
return null;
}
final Type t_field2 = cf2.getType();
if (t_field2.getTypetype() == Type_type.TYPE_TTCN3_CHOICE) {
element_i.raw.lengthindex.fieldtype = rawAST_coding_field_type.UNION_FIELD;
} else if (cf2.isOptional()) {
element_i.raw.lengthindex.fieldtype = rawAST_coding_field_type.OPTIONAL_FIELD;
} else {
element_i.raw.lengthindex.fieldtype = rawAST_coding_field_type.MANDATORY_FIELD;
}
element_i.raw.lengthindex.type = t_field2.getGenNameValue(aData, source, myScope);
element_i.raw.lengthindex.typedesc = t_field2.getGenNameTypeDescriptor(aData, source, myScope);
}
if (lengthtoNum > 0 && rawpar.lengthindex == null) {
switch(t_field_last.getTypetype()) {
case TYPE_TTCN3_CHOICE:
case TYPE_TTCN3_SEQUENCE:
case TYPE_TTCN3_SET:
final int componentsNumber = ((TTCN3_Set_Seq_Choice_BaseType) t_field_last).getNofComponents();
element_i.raw.union_member_num = componentsNumber;
element_i.raw.member_name = new ArrayList<String>(componentsNumber + 1);
element_i.raw.member_name.add(t_field_last.getGenNameValue(aData, source, myScope));
for (int m = 1; m < componentsNumber + 1; m++) {
final CompField compf = ((TTCN3_Set_Seq_Choice_BaseType) t_field_last).getComponentByIndex(m - 1);
element_i.raw.member_name.add(compf.getIdentifier().getName());
}
break;
default:
break;
}
}
if (rawpar.pointerto != null) {
final Identifier idf = rawpar.pointerto;
element_i.raw.pointerto = getComponentIndexByName(idf);
if (rawpar.ptrbase != null) {
final Identifier idf2 = rawpar.ptrbase;
element_i.raw.pointerbase = getComponentIndexByName(idf2);
} else {
element_i.raw.pointerbase = i;
}
}
// building presence list
final int parPresenceListSize = rawpar.presence == null || rawpar.presence.keyList == null ? 0 : rawpar.presence.keyList.size();
for (int a = 0; a < parPresenceListSize; a++) {
final rawAST_coding_field_list presences = new rawAST_coding_field_list();
element_i.raw.presence.fields.add(presences);
final rawAST_tag_field_value fieldValue = rawpar.presence.keyList.get(a);
final ExpressionStruct expression = new ExpressionStruct();
fieldValue.v_value.generateCodeExpression(aData, expression, true);
presences.expression = expression;
presences.isOmitValue = fieldValue.v_value.getValuetype() == Value_type.OMIT_VALUE;
presences.fields = new ArrayList<RawASTStruct.rawAST_coding_fields>(fieldValue.keyField.names.size());
IType t = this;
for (int b = 0; b < fieldValue.keyField.names.size(); b++) {
final RawASTStruct.rawAST_coding_fields newField = new rawAST_coding_fields();
presences.fields.add(newField);
final Identifier idf2 = fieldValue.keyField.names.get(b);
int comp_index = 0;
CompField cf2;
switch(t.getTypetype()) {
case TYPE_TTCN3_CHOICE:
comp_index = ((TTCN3_Choice_Type) t).getComponentIndexByName(idf2);
cf2 = ((TTCN3_Choice_Type) t).getComponentByIndex(comp_index);
newField.nthfield = comp_index;
newField.nthfieldname = idf2.getName();
newField.fieldtype = rawAST_coding_field_type.UNION_FIELD;
break;
case TYPE_TTCN3_SEQUENCE:
case TYPE_TTCN3_SET:
comp_index = ((TTCN3_Set_Seq_Choice_BaseType) t).getComponentIndexByName(idf2);
cf2 = ((TTCN3_Set_Seq_Choice_BaseType) t).getComponentByIndex(comp_index);
newField.nthfield = comp_index;
newField.nthfieldname = idf2.getName();
if (cf2.isOptional()) {
newField.fieldtype = rawAST_coding_field_type.OPTIONAL_FIELD;
} else {
newField.fieldtype = rawAST_coding_field_type.MANDATORY_FIELD;
}
break;
default:
// internal error
return null;
}
final IType field_type = cf2.getType();
newField.type = field_type.getGenNameValue(aData, source, myScope);
newField.typedesc = field_type.getGenNameTypeDescriptor(aData, source, myScope);
t = field_type.getTypeRefdLast(CompilationTimeStamp.getBaseTimestamp());
}
}
// building crosstaglist
final int crossTaglistSize = rawpar.crosstaglist == null ? 0 : rawpar.crosstaglist.size();
for (int c = 0; c < crossTaglistSize; c++) {
final rawAST_single_tag singleTag = rawpar.crosstaglist.get(c);
final rawAST_coding_taglist codingSingleTag = element_i.raw.crosstaglist.list.get(c);
if (singleTag.keyList != null) {
codingSingleTag.fields = new ArrayList<RawASTStruct.rawAST_coding_field_list>(singleTag.keyList.size());
}
codingSingleTag.fieldname = singleTag.fieldName.getName();
codingSingleTag.varName = FieldSubReference.getJavaGetterName(codingSingleTag.fieldname);
final Identifier idf = singleTag.fieldName;
switch(t_field_last.getTypetype()) {
case TYPE_TTCN3_CHOICE:
case TYPE_TTCN3_SEQUENCE:
case TYPE_TTCN3_SET:
codingSingleTag.fieldnum = ((TTCN3_Set_Seq_Choice_BaseType) t_field_last).getComponentIndexByName(idf);
break;
case TYPE_ASN1_CHOICE:
codingSingleTag.fieldnum = ((ASN1_Set_Seq_Choice_BaseType) t_field_last).getComponentIndexByName(idf);
break;
default:
codingSingleTag.fieldnum = -1;
break;
}
final int keyListSize = singleTag.keyList == null ? 0 : singleTag.keyList.size();
for (int a = 0; a < keyListSize; a++) {
final rawAST_tag_field_value key = singleTag.keyList.get(a);
final RawASTStruct.rawAST_coding_field_list codingKey = new RawASTStruct.rawAST_coding_field_list();
codingSingleTag.fields.add(codingKey);
codingKey.fields = new ArrayList<RawASTStruct.rawAST_coding_fields>(key.keyField.names.size());
final ExpressionStruct expression = new ExpressionStruct();
key.v_value.generateCodeExpression(aData, expression, true);
codingKey.expression = expression;
codingKey.isOmitValue = key.v_value.getValuetype() == Value_type.OMIT_VALUE;
IType t = this;
for (int b = 0; b < key.keyField.names.size(); b++) {
final RawASTStruct.rawAST_coding_fields newField = new rawAST_coding_fields();
codingKey.fields.add(newField);
final Identifier idf2 = key.keyField.names.get(b);
int comp_index = 0;
CompField cf2;
switch(t.getTypetype()) {
case TYPE_TTCN3_CHOICE:
comp_index = ((TTCN3_Choice_Type) t).getComponentIndexByName(idf2);
cf2 = ((TTCN3_Choice_Type) t).getComponentByIndex(comp_index);
newField.nthfield = comp_index;
newField.nthfieldname = idf2.getName();
newField.fieldtype = rawAST_coding_field_type.UNION_FIELD;
break;
case TYPE_TTCN3_SEQUENCE:
case TYPE_TTCN3_SET:
comp_index = ((TTCN3_Set_Seq_Choice_BaseType) t).getComponentIndexByName(idf2);
cf2 = ((TTCN3_Set_Seq_Choice_BaseType) t).getComponentByIndex(comp_index);
newField.nthfield = comp_index;
newField.nthfieldname = idf2.getName();
if (cf2.isOptional()) {
newField.fieldtype = rawAST_coding_field_type.OPTIONAL_FIELD;
} else {
newField.fieldtype = rawAST_coding_field_type.MANDATORY_FIELD;
}
break;
default:
// internal error
return null;
}
final IType field_type = cf2.getType();
newField.type = field_type.getGenNameValue(aData, source, myScope);
newField.typedesc = field_type.getGenNameTypeDescriptor(aData, source, myScope);
t = field_type.getTypeRefdLast(CompilationTimeStamp.getBaseTimestamp());
}
}
}
element_i.raw.length = t_field.getRawLength();
element_i.hasRaw = true;
} else {
element_i.hasRaw = false;
}
}
}
return raw;
}
use of org.eclipse.titan.designer.AST.TTCN3.types.UnionGenerator.FieldInfo in project titan.EclipsePlug-ins by eclipse.
the class RecordSetCodeGenerator method generateValueEncodeDecode.
/**
* Generate encode/decode
*
* @param aData only used to update imports if needed.
* @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 raw the raw coding related settings if applicable.
*/
private static void generateValueEncodeDecode(final JavaGenData aData, final StringBuilder source, final String genName, final String displayName, final List<FieldInfo> fieldInfos, final boolean isSet, final boolean rawNeeded, 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(false, 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 ArrayList<raw_option_struct> raw_options = new ArrayList<RecordSetCodeGenerator.raw_option_struct>(fieldInfos.size());
final AtomicBoolean hasLengthto = new AtomicBoolean();
final AtomicBoolean hasPointer = new AtomicBoolean();
final AtomicBoolean hasCrosstag = new AtomicBoolean();
final AtomicBoolean has_ext_bit = new AtomicBoolean();
set_raw_options(isSet, fieldInfos, raw != null, raw, raw_options, hasLengthto, hasPointer, hasCrosstag, has_ext_bit);
source.append("@Override\n");
source.append("public int RAW_encode(final TTCN_Typedescriptor p_td, final RAW_enc_tree myleaf) {\n");
source.append("if (!isBound()) {\n");
source.append("TTCN_EncDec_ErrorContext.error(error_type.ET_UNBOUND, \"Encoding an unbound value.\", \"\");\n");
source.append("}\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()));
for (int i = 0; i < fieldInfos.size(); i++) {
final FieldInfo fieldInfo = fieldInfos.get(i);
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("if ({0}.isPresent()) '{'\n", fieldInfo.mVarName));
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("} else {\n");
source.append(MessageFormat.format("myleaf.nodes[{0}] = null;\n", i));
source.append("}\n");
} else {
source.append(MessageFormat.format("myleaf.nodes[{0}] = new RAW_enc_tree(true, myleaf, myleaf.curr_pos, {0}, {1}_descr_.raw);\n", i, fieldInfo.mTypeDescriptorName));
}
}
final int ext_bit_group_length = raw == null || raw.ext_bit_groups == null ? 0 : raw.ext_bit_groups.size();
for (int i = 0; i < ext_bit_group_length; i++) {
final rawAST_coding_ext_group tempGroup = raw.ext_bit_groups.get(i);
if (tempGroup.ext_bit != RawASTStruct.XDEFNO) {
source.append("{\n");
source.append(MessageFormat.format("int node_idx = {0};\n", tempGroup.from));
source.append(MessageFormat.format("while (node_idx <= {0} && myleaf.nodes[node_idx] == null) '{'\n", tempGroup.to));
source.append("node_idx++;\n");
source.append("}\n");
source.append("if (myleaf.nodes[node_idx] != null) {\n");
source.append("myleaf.nodes[node_idx].ext_bit_handling = 1;\n");
source.append(MessageFormat.format("myleaf.nodes[node_idx].ext_bit = ext_bit_t.{0};\n", tempGroup.ext_bit == RawASTStruct.XDEFYES ? "EXT_BIT_YES" : "EXT_BIT_REVERSE"));
source.append("}\n");
source.append(MessageFormat.format("node_idx = {0};\n", tempGroup.to));
source.append(MessageFormat.format("while (node_idx >= {0} && myleaf.nodes[node_idx] == null) '{'\n", tempGroup.from));
source.append("node_idx--;\n");
source.append("}\n");
source.append("if (myleaf.nodes[node_idx] != null) {\n");
source.append("myleaf.nodes[node_idx].ext_bit_handling += 2;;\n");
source.append("}\n");
source.append("}\n");
}
}
for (int i = 0; i < fieldInfos.size(); i++) {
final FieldInfo fieldInfo = fieldInfos.get(i);
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("if ({0}.isPresent()) '{'\n", fieldInfo.mVarName));
}
if (raw_options.get(i).lengthto && fieldInfo.raw.lengthindex == null && fieldInfo.raw.union_member_num == 0) {
aData.addBuiltinTypeImport("RAW.calc_type");
aData.addBuiltinTypeImport("RAW.RAW_enc_lengthto");
source.append(MessageFormat.format("encoded_length += {0};\n", fieldInfo.raw.fieldlength));
source.append(MessageFormat.format("myleaf.nodes[{0}].calc = calc_type.CALC_LENGTH;\n", i));
source.append(MessageFormat.format("myleaf.nodes[{0}].coding_descr = {1}_descr_;\n", i, fieldInfo.mTypeDescriptorName));
final int lengthtoSize = fieldInfo.raw.lengthto == null ? 0 : fieldInfo.raw.lengthto.size();
source.append(MessageFormat.format("myleaf.nodes[{0}].length = {1};\n", i, fieldInfo.raw.fieldlength));
source.append(MessageFormat.format("myleaf.nodes[{0}].lengthto = new RAW_enc_lengthto({1}, new RAW_enc_tr_pos[{1}], {2}, {3});\n", i, lengthtoSize, fieldInfo.raw.unit, fieldInfo.raw.lengthto_offset));
for (int a = 0; a < lengthtoSize; a++) {
if (fieldInfos.get(fieldInfo.raw.lengthto.get(a)).isOptional) {
source.append(MessageFormat.format("if ({0}.isPresent()) '{'\n", fieldInfos.get(fieldInfo.raw.lengthto.get(a)).mVarName));
}
source.append(MessageFormat.format("myleaf.nodes[{0}].lengthto.fields[{1}] = new RAW_enc_tr_pos(myleaf.nodes[{2}].curr_pos.level, myleaf.nodes[{2}].curr_pos.pos);\n", i, a, fieldInfo.raw.lengthto.get(a)));
if (fieldInfos.get(fieldInfo.raw.lengthto.get(a)).isOptional) {
source.append("} else {\n");
source.append(MessageFormat.format("myleaf.nodes[{0}].lengthto.fields[{1}] = new RAW_enc_tr_pos(0, null);\n", i, a));
source.append("}\n");
}
}
} else if (raw_options.get(i).pointerto) {
aData.addBuiltinTypeImport("RAW.calc_type");
aData.addBuiltinTypeImport("RAW.RAW_enc_pointer");
if (fieldInfos.get(fieldInfo.raw.pointerto).isOptional) {
source.append(MessageFormat.format("if ({0}.isPresent()) '{'\n", fieldInfos.get(fieldInfo.raw.pointerto).mVarName));
}
source.append(MessageFormat.format("encoded_length += {0};\n", fieldInfo.raw.fieldlength));
source.append(MessageFormat.format("myleaf.nodes[{0}].calc = calc_type.CALC_POINTER;\n", i));
source.append(MessageFormat.format("myleaf.nodes[{0}].coding_descr = {1}_descr_;\n", i, fieldInfo.mTypeDescriptorName));
source.append(MessageFormat.format("myleaf.nodes[{0}].pointerto = new RAW_enc_pointer(new RAW_enc_tr_pos(myleaf.nodes[{1}].curr_pos.level, myleaf.nodes[{1}].curr_pos.pos), {2}, {3}, {4});\n", i, fieldInfo.raw.pointerto, fieldInfo.raw.ptroffset, fieldInfo.raw.unit, fieldInfo.raw.pointerbase));
source.append(MessageFormat.format("myleaf.nodes[{0}].length = {1};\n", i, fieldInfo.raw.fieldlength));
if (fieldInfos.get(fieldInfo.raw.pointerto).isOptional) {
source.append("} else {\n");
source.append("TitanInteger atm = new TitanInteger(0);\n");
source.append(MessageFormat.format("encoded_length += atm.RAW_encode({0}_descr_, myleaf.nodes[{1}]);\n", fieldInfo.mTypeDescriptorName, i));
source.append("}\n");
}
} else {
source.append(MessageFormat.format("encoded_length += {0}{1}.RAW_encode({2}_descr_, myleaf.nodes[{3}]);\n", fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.mTypeDescriptorName, i));
}
if (fieldInfo.isOptional) {
source.append("}\n");
}
}
for (int i = 0; i < fieldInfos.size(); i++) {
final FieldInfo fieldInfo = fieldInfos.get(i);
if (raw_options.get(i).lengthto && fieldInfo.raw.lengthindex != null) {
aData.addBuiltinTypeImport("RAW.calc_type");
aData.addBuiltinTypeImport("RAW.RAW_enc_lengthto");
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("if ({0}.isPresent()) '{'\n", fieldInfo.mVarName));
}
source.append(MessageFormat.format("if (myleaf.nodes[{0}].nodes[{1}] != null) '{'\n", i, fieldInfo.raw.lengthindex.nthfield));
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[{1}].calc = calc_type.CALC_LENGTH;\n", i, fieldInfo.raw.lengthindex.nthfield));
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[{1}].coding_descr = {2}_descr_;\n", i, fieldInfo.raw.lengthindex.nthfield, fieldInfo.mTypeDescriptorName));
final int lengthtoSize = fieldInfo.raw.lengthto == null ? 0 : fieldInfo.raw.lengthto.size();
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[{1}].length = {1};\n", i, fieldInfo.raw.lengthindex.nthfield, fieldInfo.raw.fieldlength));
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[{1}].lengthto = new RAW_enc_lengthto({2}, new RAW_enc_tr_pos[{2}], {3}, {4});\n", i, fieldInfo.raw.lengthindex.nthfield, lengthtoSize, fieldInfo.raw.unit, fieldInfo.raw.lengthto_offset));
for (int a = 0; a < lengthtoSize; a++) {
if (fieldInfos.get(fieldInfo.raw.lengthto.get(a)).isOptional) {
source.append(MessageFormat.format("if ({0}.isPresent()) '{'\n", fieldInfos.get(fieldInfo.raw.lengthto.get(a)).mVarName));
}
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[{1}].lengthto.fields[{2}] = new RAW_enc_tr_pos(myleaf.nodes[{3}].curr_pos.level, myleaf.nodes[{3}].curr_pos.pos);\n", i, fieldInfo.raw.lengthindex.nthfield, a, fieldInfo.raw.lengthto.get(a)));
if (fieldInfos.get(fieldInfo.raw.lengthto.get(a)).isOptional) {
source.append("} else {\n");
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[{1}].lengthto.fields[{2}] = new RAW_enc_tr_pos(0, null);\n", i, fieldInfo.raw.lengthindex.nthfield, a));
source.append("}\n");
}
}
source.append("}\n");
if (fieldInfo.isOptional) {
source.append("}\n");
}
}
if (raw_options.get(i).lengthto && fieldInfo.raw.union_member_num > 0) {
aData.addBuiltinTypeImport("RAW.calc_type");
aData.addBuiltinTypeImport("RAW.RAW_enc_lengthto");
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("if ({0}.isPresent()) ", fieldInfo.mVarName));
}
source.append("{\n");
source.append("int sel_field = 0;\n");
source.append(MessageFormat.format("while (myleaf.nodes[{0}].nodes[sel_field] == null) '{'\n", i));
source.append("sel_field++;\n");
source.append("}\n");
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[sel_field].calc = calc_type.CALC_LENGTH;\n", i));
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[sel_field].coding_descr = {1}_descr_;\n", i, fieldInfo.mTypeDescriptorName));
final int lengthtoSize = fieldInfo.raw.lengthto == null ? 0 : fieldInfo.raw.lengthto.size();
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[sel_field].length = {1};\n", i, fieldInfo.raw.fieldlength));
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[sel_field].lengthto = new RAW_enc_lengthto({1}, new RAW_enc_tr_pos[{1}], {2}, {3});\n", i, lengthtoSize, fieldInfo.raw.unit, fieldInfo.raw.lengthto_offset));
for (int a = 0; a < lengthtoSize; a++) {
if (fieldInfos.get(fieldInfo.raw.lengthto.get(a)).isOptional) {
source.append(MessageFormat.format("if ({0}.isPresent()) '{'\n", fieldInfos.get(fieldInfo.raw.lengthto.get(a)).mVarName));
}
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[sel_field].lengthto.fields[{1}] = new RAW_enc_tr_pos(myleaf.nodes[{2}].curr_pos.level, myleaf.nodes[{2}].curr_pos.pos);\n", i, a, fieldInfo.raw.lengthto.get(a)));
if (fieldInfos.get(fieldInfo.raw.lengthto.get(a)).isOptional) {
source.append("} else {\n");
source.append(MessageFormat.format("myleaf.nodes[{0}].nodes[sel_field].lengthto.fields[{1}] = new RAW_enc_tr_pos(0, null);\n", i, a));
source.append("}\n");
}
}
source.append("}\n");
if (fieldInfo.isOptional) {
source.append("}\n");
}
}
final int tag_type = raw_options.get(i).tag_type;
if (tag_type > 0 && raw.taglist.list.get(tag_type - 1).fields != null && raw.taglist.list.get(tag_type - 1).fields.size() > 0) {
final rawAST_coding_taglist cur_choice = raw.taglist.list.get(tag_type - 1);
source.append("if (");
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("{0}.isPresent() && (", fieldInfo.mVarName));
}
genRawFieldChecker(source, cur_choice, false);
if (fieldInfo.isOptional) {
source.append(")");
}
source.append(") {\n");
genRawTagChecker(source, cur_choice);
source.append("}\n");
}
final int presenceLength = fieldInfo.raw == null || fieldInfo.raw.presence == null || fieldInfo.raw.presence.fields == null ? 0 : fieldInfo.raw.presence.fields.size();
if (fieldInfo.hasRaw && presenceLength > 0) {
source.append("if (");
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("{0}.isPresent() && (", fieldInfo.mVarName));
}
genRawFieldChecker(source, fieldInfo.raw.presence, false);
if (fieldInfo.isOptional) {
source.append(")");
}
source.append(") {\n");
genRawTagChecker(source, fieldInfo.raw.presence);
source.append("}\n");
}
final int crosstagLength = fieldInfo.raw == null || fieldInfo.raw.crosstaglist == null || fieldInfo.raw.crosstaglist.list == null ? 0 : fieldInfo.raw.crosstaglist.list.size();
if (fieldInfo.hasRaw && crosstagLength > 0) {
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("if ({0}.isPresent()) '{'\n", fieldInfo.mVarName));
}
source.append(MessageFormat.format("switch ({0}{1}.get_selection()) '{'\n", fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : ""));
for (int a = 0; a < crosstagLength; a++) {
final rawAST_coding_taglist cur_choice = fieldInfo.raw.crosstaglist.list.get(a);
final int curSize = cur_choice == null || cur_choice.fields == null ? 0 : cur_choice.fields.size();
if (curSize > 0) {
source.append(MessageFormat.format("case ALT_{0}:\n", cur_choice.varName));
source.append("if (");
genRawFieldChecker(source, cur_choice, false);
source.append(") {\n");
if (cur_choice.fields.get(0).isOmitValue) {
if (cur_choice.fields.get(0).fields.size() != 1) {
// FIXME report error "omit value with multiple fields in CROSSTAG"
}
source.append(MessageFormat.format("encoded_length -= myleaf.nodes[{0}].length;\n", cur_choice.fields.get(0).fields.get(0).nthfield));
source.append(MessageFormat.format("myleaf.nodes[{0}] = null;\n", cur_choice.fields.get(0).fields.get(0).nthfield));
} else {
source.append(MessageFormat.format("RAW_enc_tr_pos pr_pos = new RAW_enc_tr_pos(myleaf.curr_pos.level + {0}, new int[] '{'", cur_choice.fields.get(0).fields.size()));
for (int ll = 0; ll < cur_choice.fields.get(0).fields.size(); ll++) {
if (ll > 0) {
source.append(',');
}
source.append(cur_choice.fields.get(0).fields.get(ll).nthfield);
}
source.append("});\n");
source.append("RAW_enc_tree temp_leaf = myleaf.get_node(pr_pos);\n");
source.append("if (temp_leaf != null) {\n");
source.append(MessageFormat.format("{0}.RAW_encode({1}_descr_, temp_leaf);\n", cur_choice.fields.get(0).expression.expression, cur_choice.fields.get(0).fields.get(cur_choice.fields.get(0).fields.size() - 1).typedesc));
source.append("} else {\n");
source.append("TTCN_EncDec_ErrorContext.error(error_type.ET_OMITTED_TAG, \"Encoding a tagged, but omitted value.\", \"\");\n");
source.append("}\n");
}
source.append("}\n");
source.append("break;\n");
}
}
source.append("default:\n");
source.append("break;\n");
source.append("}\n");
if (fieldInfo.isOptional) {
source.append("}\n");
}
}
}
// presence
final int presenceLength = raw == null || raw.presence == null || raw.presence.fields == null ? 0 : raw.presence.fields.size();
if (presenceLength > 0) {
source.append(" if (");
genRawFieldChecker(source, raw.presence, false);
source.append(" ) {\n");
genRawTagChecker(source, raw.presence);
source.append("}\n");
}
source.append("myleaf.length = encoded_length;\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");
if (isSet) {
int mand_num = 0;
for (int i = 0; i < fieldInfos.size(); i++) {
if (!fieldInfos.get(i).isOptional) {
mand_num++;
}
}
source.append("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("int field_map[] = new int[]{");
for (int i = 0; i < fieldInfos.size(); i++) {
if (i != 0) {
source.append(',');
}
source.append('0');
}
source.append("};\n");
if (mand_num > 0) {
source.append("int nof_mand_fields = 0;\n");
}
for (int i = 0; i < fieldInfos.size(); i++) {
final FieldInfo fieldInfo = fieldInfos.get(i);
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("{0}.assign(template_sel.OMIT_VALUE);\n", fieldInfo.mVarName));
}
}
source.append("raw_order_t local_top_order;\n");
source.append("if (p_td.raw.top_bit_order == top_bit_order_t.TOP_BIT_INHERITED) {\n");
source.append("local_top_order = top_bit_ord;\n");
source.append("} else if (p_td.raw.top_bit_order == top_bit_order_t.TOP_BIT_RIGHT) {\n");
source.append("local_top_order = raw_order_t.ORDER_MSB;\n");
source.append("} else {\n");
source.append("local_top_order = raw_order_t.ORDER_LSB;\n");
source.append("}\n");
source.append("while (limit > 0) {\n");
source.append("int fl_start_pos = buff.get_pos_bit();\n");
for (int i = 0; i < fieldInfos.size(); i++) {
// tagged fields
final FieldInfo fieldInfo = fieldInfos.get(i);
final int tag_type = raw_options.get(i).tag_type;
if (tag_type > 0 && raw.taglist.list.get(tag_type - 1).fields.size() > 0) {
final rawAST_coding_taglist cur_choice = raw.taglist.list.get(raw_options.get(i).tag_type - 1);
boolean has_fixed = false;
boolean has_variable = false;
boolean flag_needed = false;
for (int j = 0; j < cur_choice.fields.size(); j++) {
if (cur_choice.fields.get(j).start_pos >= 0) {
if (has_fixed || has_variable) {
flag_needed = true;
}
has_fixed = true;
} else {
if (has_fixed) {
flag_needed = true;
}
has_variable = true;
}
if (has_fixed && has_variable) {
break;
}
}
source.append(MessageFormat.format("if (field_map[{0}] == 0) '{'\n", i));
if (flag_needed) {
source.append("boolean already_failed = true;\n");
}
if (has_fixed) {
boolean first_fixed = true;
source.append("raw_order_t temporal_top_order;\n");
source.append("int temporal_decoded_length;\n");
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) {
continue;
}
if (!first_fixed) {
source.append("if (!already_failed) {\n");
}
for (int k = cur_field_list.fields.size() - 1; k > 0; k--) {
source.append(MessageFormat.format("if ({0}_descr_.raw.top_bit_order == top_bit_order_t.TOP_BIT_RIGHT) '{'\n", cur_field_list.fields.get(k - 1).typedesc));
source.append("temporal_top_order = raw_order_t.ORDER_MSB;\n");
source.append(MessageFormat.format("} else if ({0}_descr_.raw.top_bit_order == top_bit_order_t.TOP_BIT_LEFT) '{'\n", cur_field_list.fields.get(k - 1).typedesc));
source.append("temporal_top_order = raw_order_t.ORDER_LSB;\n");
source.append("} else ");
}
source.append("{\n");
source.append("temporal_top_order = top_bit_ord;\n");
source.append("}\n");
source.append(MessageFormat.format("{0} temporal_{1} = new {0}();\n", cur_field_list.fields.get(cur_field_list.fields.size() - 1).type, j));
source.append(MessageFormat.format("buff.set_pos_bit(fl_start_pos + {0});\n", cur_field_list.start_pos));
source.append(MessageFormat.format("temporal_decoded_length = temporal_{0}.RAW_decode({1}_descr_, buff, limit, temporal_top_order, true, -1, true);\n", j, cur_field_list.fields.get(cur_field_list.fields.size() - 1).typedesc));
source.append("buff.set_pos_bit(fl_start_pos);\n");
source.append(MessageFormat.format("if (temporal_decoded_length > 0 && temporal_{0}.operatorEquals({1})) '{'\n", j, cur_field_list.expression.expression));
source.append(MessageFormat.format("int decoded_field_length = {0}{1}.RAW_decode({2}_descr_, buff, limit, local_top_order, true, -1, true);\n", fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.mTypeDescriptorName));
source.append(MessageFormat.format("if (decoded_field_length {0} 0 && (", fieldInfo.isOptional ? ">" : ">="));
genRawFieldChecker(source, cur_choice, true);
source.append(")) {\n");
source.append("decoded_length += decoded_field_length;\n");
source.append("limit -= decoded_field_length;\n");
if (!fieldInfo.isOptional) {
source.append("nof_mand_fields++;\n");
}
source.append(MessageFormat.format("field_map[{0}] = 1;", i));
source.append("continue;\n");
source.append("} else {\n");
source.append("buff.set_pos_bit(fl_start_pos);\n");
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("{0}.assign(template_sel.OMIT_VALUE);\n", fieldInfo.mVarName));
}
if (flag_needed) {
source.append("already_failed = true;\n");
}
source.append("}\n");
source.append("}\n");
if (first_fixed) {
first_fixed = false;
} else {
source.append("}\n");
}
}
}
if (has_variable) {
if (flag_needed) {
source.append("if (!already_failed) {\n");
}
source.append(MessageFormat.format("int decoded_field_length = {0}{1}.RAW_decode({2}_descr_, buff, limit, local_top_order, true, -1, true);\n", fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.mTypeDescriptorName));
source.append(MessageFormat.format("if (decoded_field_length {0} 0 && (", fieldInfo.isOptional ? ">" : ">="));
genRawFieldChecker(source, cur_choice, true);
source.append(")) {\n");
source.append("decoded_length += decoded_field_length;\n");
source.append("limit -= decoded_field_length;\n");
if (!fieldInfo.isOptional) {
source.append("nof_mand_fields++;\n");
}
source.append(MessageFormat.format("field_map[{0}] = 1;", i));
source.append("continue;\n");
source.append("} else {\n");
source.append("buff.set_pos_bit(fl_start_pos);\n");
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("{0}.assign(template_sel.OMIT_VALUE);\n", fieldInfo.mVarName));
}
source.append("}\n");
if (flag_needed) {
source.append("}\n");
}
}
source.append("}\n");
}
}
for (int i = 0; i < fieldInfos.size(); i++) {
// untagged fields
final FieldInfo fieldInfo = fieldInfos.get(i);
if (raw_options.get(i).tag_type == 0) {
boolean repeatable;
if (fieldInfo.ofType && fieldInfo.raw != null && fieldInfo.raw.repeatable == RawAST.XDEFYES) {
repeatable = true;
} else {
repeatable = false;
source.append(MessageFormat.format("if (field_map[{0}] == 0) ", i));
}
source.append("{\n");
source.append(MessageFormat.format("int decoded_field_length = {0}{1}.RAW_decode({2}_descr_, buff, limit, top_bit_ord, true, -1, ", fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.mTypeDescriptorName));
if (repeatable) {
source.append(MessageFormat.format("field_map[{0}]);\n", i));
} else {
source.append("true);\n");
}
source.append(MessageFormat.format("if (decoded_field_length {0} 0) '{'\n", fieldInfo.isOptional ? ">" : ">="));
source.append("decoded_length += decoded_field_length;\n");
source.append("limit -= decoded_field_length;\n");
if (repeatable) {
if (!fieldInfo.isOptional) {
source.append(MessageFormat.format("if (field_map[{0}] == 0 ) '{'\n", i));
source.append("nof_mand_fields++;\n");
source.append("}\n");
}
source.append(MessageFormat.format("field_map[{0}]++;\n", i));
} else {
if (!fieldInfo.isOptional) {
source.append("nof_mand_fields++;\n");
}
source.append(MessageFormat.format("field_map[{0}] = 1;\n", i));
}
source.append("continue;\n");
source.append("} else {\n");
source.append("buff.set_pos_bit(fl_start_pos);\n");
if (fieldInfo.isOptional) {
if (repeatable) {
source.append(MessageFormat.format("if (field_map[{0}] == 0) ", i));
}
source.append(MessageFormat.format("{0}.assign(template_sel.OMIT_VALUE);\n", fieldInfo.mVarName));
}
source.append("}\n");
source.append("}\n");
}
}
for (int i = 0; i < fieldInfos.size(); i++) {
// tag OTHERWISE
final FieldInfo fieldInfo = fieldInfos.get(i);
final int tag_type = raw_options.get(i).tag_type;
if (tag_type > 0 && raw.taglist.list.get(tag_type - 1).fields.size() == 0) {
source.append(MessageFormat.format("if (field_map[{0}] == 0) ", i));
source.append(MessageFormat.format("int decoded_field_length = {0}{1}.RAW_decode({2}_descr_, buff, limit, top_bit_ord, true, -1, ", fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.mTypeDescriptorName));
source.append(MessageFormat.format("if (decoded_field_length {0} 0) '{'\n", fieldInfo.isOptional ? ">" : ">="));
source.append("decoded_length += decoded_field_length;\n");
source.append("limit -= decoded_field_length;\n");
if (!fieldInfo.isOptional) {
source.append("nof_mand_fields++;\n");
}
source.append(MessageFormat.format("field_map[{0}] = 1;\n", i));
source.append("continue;\n");
source.append("} else {\n");
source.append("buff.set_pos_bit(fl_start_pos);\n");
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("{0}.assign(template_sel.OMIT_VALUE);\n", fieldInfo.mVarName));
}
source.append("}\n");
source.append("}\n");
}
}
source.append("break;\n");
source.append("}\n");
if (mand_num > 0) {
source.append(MessageFormat.format("if (nof_mand_fields != {0}) '{'\n", fieldInfos.size()));
source.append("return limit > 0 ? -1 : TTCN_EncDec.error_type.ET_INCOMPL_MSG.ordinal();\n");
source.append("}\n");
}
source.append("return decoded_length + prepaddlength + buff.increase_pos_padd(p_td.raw.padding);\n");
} else {
source.append("int prepaddlength = buff.increase_pos_padd(p_td.raw.prepadding);\n");
source.append("limit -= prepaddlength;\n");
source.append("int last_decoded_pos = buff.get_pos_bit();\n");
source.append("int decoded_length = 0;\n");
source.append("int decoded_field_length = 0;\n");
source.append("raw_order_t local_top_order;\n");
if (hasCrosstag.get()) {
source.append("int selected_field = -1;\n");
}
if (raw != null && raw.ext_bit_groups != null && raw.ext_bit_groups.size() > 0) {
source.append("int group_limit = 0;\n");
}
source.append("if (p_td.raw.top_bit_order == top_bit_order_t.TOP_BIT_INHERITED) {\n");
source.append("local_top_order = top_bit_ord;\n");
source.append("} else if (p_td.raw.top_bit_order == top_bit_order_t.TOP_BIT_RIGHT) {\n");
source.append("local_top_order = raw_order_t.ORDER_MSB;\n");
source.append("} else {\n");
source.append("local_top_order = raw_order_t.ORDER_LSB;\n");
source.append("}\n");
if (has_ext_bit.get()) {
source.append("{\n");
source.append("char data[] = buff.get_read_data();\n");
source.append("int count = 1;\n");
source.append("int mask = 1 << (local_top_order == raw_order_t.ORDER_LSB ? 0 : 7);\n");
source.append("if (p_td.raw.extension_bit == ext_bit_t.EXT_BIT_YES) {\n");
source.append("while ((data[count - 1] & mask) == 0 && count * 8 < limit) {\n");
source.append("count++;\n");
source.append("}\n");
source.append("} else {\n");
source.append("while ((data[count - 1] & mask) != 0 && count * 8 < limit) {\n");
source.append("count++;\n");
source.append("}\n");
source.append("}\n");
source.append("if (limit > 0) {\n");
source.append("limit = count * 8;\n");
source.append("}\n");
source.append("}\n");
}
if (hasPointer.get()) {
source.append("int end_of_available_data = last_decoded_pos + limit;\n");
}
for (int i = 0; i < fieldInfos.size(); i++) {
if (raw_options.get(i).pointerof > 0) {
source.append(MessageFormat.format("int start_of_field{0} = -1;\n", i));
}
if (raw_options.get(i).ptrbase) {
source.append(MessageFormat.format("int start_pos_of_field{0} = -1;\n", i));
}
if (raw_options.get(i).lengthto) {
source.append(MessageFormat.format("int value_of_length_field{0} = 0;\n", i));
}
}
final AtomicInteger prev_ext_group = new AtomicInteger(0);
for (int i = 0; i < fieldInfos.size(); i++) {
final FieldInfo fieldInfo = fieldInfos.get(i);
final raw_option_struct tempRawOption = raw_options.get(i);
if (tempRawOption.delayedDecode) {
final ExpressionStruct expression = new ExpressionStruct();
genRawFieldDecodeLimit(aData, expression, fieldInfos, i, raw, raw_options);
if (expression.preamble.length() > 0) {
source.append(expression.preamble);
}
source.append("if (");
source.append(expression.expression);
source.append(MessageFormat.format(" < {0}) '{'\n", fieldInfo.raw.length));
source.append("return -1 * TTCN_EncDec.error_type.ET_LEN_ERR.ordinal();\n");
source.append("}\n");
source.append(MessageFormat.format("int start_of_field{0} = buff.get_pos_bit();\n", i));
source.append(MessageFormat.format("buff.set_pos_bit(start_of_field{0} + {1});\n", i, fieldInfo.raw.length));
source.append(MessageFormat.format("decoded_length += {0};\n", fieldInfo.raw.length));
source.append(MessageFormat.format("last_decoded_pos += {0};\n", fieldInfo.raw.length));
source.append(MessageFormat.format("limit -= {0};\n", fieldInfo.raw.length));
for (int j = 0; j < tempRawOption.lengthof; j++) {
source.append(MessageFormat.format("value_of_length_field{0} -= {1};\n", tempRawOption.lengthofField.get(j), fieldInfo.raw.length));
}
} else {
genRawDecodeRecordField(aData, source, fieldInfos, i, raw, raw_options, false, prev_ext_group);
if (tempRawOption.dependentFields != null && tempRawOption.dependentFields.size() > 0) {
for (int j = 0; j < tempRawOption.dependentFields.size(); j++) {
final int dependent_field_index = tempRawOption.dependentFields.get(j);
source.append(MessageFormat.format("buff.set_pos_bit(start_of_field{0});\n", dependent_field_index));
genRawDecodeRecordField(aData, source, fieldInfos, dependent_field_index, raw, raw_options, true, prev_ext_group);
}
if (i < fieldInfos.size() - 1) {
/* seek back if there are more regular fields to decode */
source.append("buff.set_pos_bit(last_decoded_pos);\n");
}
}
}
}
if (raw != null && raw.presence != null && raw.presence.fields != null && raw.presence.fields.size() > 0) {
source.append("if (");
genRawFieldChecker(source, raw.presence, false);
source.append(") {\n");
source.append("return -1;");
source.append("}\n");
}
source.append("buff.set_pos_bit(last_decoded_pos);\n");
source.append("return decoded_length + prepaddlength + buff.increase_pos_padd(p_td.raw.padding);\n");
}
source.append("}\n\n");
}
}
use of org.eclipse.titan.designer.AST.TTCN3.types.UnionGenerator.FieldInfo 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");
}
}
Aggregations