use of org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct in project titan.EclipsePlug-ins by eclipse.
the class Reference method generateCode.
// originally fieldOrArrayRefs
private void generateCode(final JavaGenData aData, final ExpressionStruct expression, final boolean isTemplate, final boolean isConst, IType type) {
for (int i = 1; i < subReferences.size(); i++) {
if (type != null) {
type = type.getTypeRefdLast(CompilationTimeStamp.getBaseTimestamp());
}
final ISubReference subreference = subReferences.get(i);
if (Subreference_type.fieldSubReference.equals(subreference.getReferenceType())) {
final Identifier id = ((FieldSubReference) subreference).getId();
expression.expression.append(".");
if (isConst) {
expression.expression.append("constGet");
} else {
expression.expression.append("get");
}
expression.expression.append(FieldSubReference.getJavaGetterName(id.getName()));
expression.expression.append("()");
if (type != null) {
CompField compField = null;
switch(type.getTypetype()) {
case TYPE_TTCN3_CHOICE:
case TYPE_TTCN3_SEQUENCE:
case TYPE_TTCN3_SET:
compField = ((TTCN3_Set_Seq_Choice_BaseType) type).getComponentByName(id.getName());
break;
case TYPE_ANYTYPE:
compField = ((Anytype_Type) type).getComponentByName(id.getName());
break;
case TYPE_OPENTYPE:
compField = ((Open_Type) type).getComponentByName(id);
break;
case TYPE_ASN1_SEQUENCE:
((ASN1_Sequence_Type) type).parseBlockSequence();
compField = ((ASN1_Sequence_Type) type).getComponentByName(id);
break;
case TYPE_ASN1_SET:
((ASN1_Set_Type) type).parseBlockSet();
compField = ((ASN1_Set_Type) type).getComponentByName(id);
break;
case TYPE_ASN1_CHOICE:
((ASN1_Choice_Type) type).parseBlockChoice();
compField = ((ASN1_Choice_Type) type).getComponentByName(id);
break;
default:
ErrorReporter.INTERNAL_ERROR("FATAL ERROR while generating code for reference `" + getFullName() + "''");
return;
}
if (i < subReferences.size() - 1 && compField != null && compField.isOptional() && !isTemplate) {
if (isConst) {
expression.expression.append(".constGet()");
} else {
expression.expression.append(".get()");
}
type = compField.getType();
}
}
} else if (Subreference_type.arraySubReference.equals(subreference.getReferenceType())) {
final Value value = ((ArraySubReference) subreference).getValue();
// TODO actually should get the last governor
final IType pt = value.getExpressionGovernor(CompilationTimeStamp.getBaseTimestamp(), Expected_Value_type.EXPECTED_TEMPLATE);
// generate "getAt" functions instead of operator[]
if (isConst) {
expression.expression.append(".constGetAt(");
} else {
expression.expression.append(".getAt(");
}
value.generateCodeExpression(aData, expression, false);
expression.expression.append(")");
if (type != null) {
switch(type.getTypetype()) {
case TYPE_SEQUENCE_OF:
case TYPE_SET_OF:
type = ((AbstractOfType) type).getOfType();
break;
case TYPE_ARRAY:
type = ((Array_Type) type).getElementType();
break;
default:
type = null;
}
}
}
}
}
use of org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct in project titan.EclipsePlug-ins by eclipse.
the class Reference method generateCode.
/**
* Add generated java code on this level.
* @param aData only used to update imports if needed
* @param expression the expression for code generated
*/
public void generateCode(final JavaGenData aData, final ExpressionStruct expression) {
if (referredAssignment == null) {
return;
}
boolean isTemplate;
IType referedGovernor;
switch(referredAssignment.getAssignmentType()) {
case A_CONST:
case A_EXT_CONST:
case A_MODULEPAR:
case A_VAR:
case A_FUNCTION_RVAL:
case A_EXT_FUNCTION_RVAL:
case A_PAR_VAL:
case A_PAR_VAL_IN:
case A_PAR_VAL_OUT:
case A_PAR_VAL_INOUT:
isTemplate = false;
referedGovernor = referredAssignment.getType(CompilationTimeStamp.getBaseTimestamp());
break;
case A_MODULEPAR_TEMPLATE:
case A_TEMPLATE:
case A_VAR_TEMPLATE:
case A_PAR_TEMP_IN:
case A_PAR_TEMP_OUT:
case A_PAR_TEMP_INOUT:
isTemplate = true;
referedGovernor = referredAssignment.getType(CompilationTimeStamp.getBaseTimestamp());
break;
default:
isTemplate = false;
referedGovernor = null;
break;
}
FormalParameterList formalParameterList;
switch(referredAssignment.getAssignmentType()) {
case A_FUNCTION:
case A_FUNCTION_RVAL:
case A_FUNCTION_RTEMP:
formalParameterList = ((Def_Function) referredAssignment).getFormalParameterList();
break;
case A_EXT_FUNCTION:
case A_EXT_FUNCTION_RVAL:
case A_EXT_FUNCTION_RTEMP:
formalParameterList = ((Def_Extfunction) referredAssignment).getFormalParameterList();
break;
case A_TEMPLATE:
formalParameterList = ((Def_Template) referredAssignment).getFormalParameterList();
break;
default:
formalParameterList = null;
break;
}
if (subReferences.get(0) instanceof ParameterisedSubReference) {
expression.expression.append(referredAssignment.getGenNameFromScope(aData, expression.expression, getMyScope(), null));
expression.expression.append("( ");
final ParameterisedSubReference temp = ((ParameterisedSubReference) subReferences.get(0));
temp.getActualParameters().generateCodeAlias(aData, expression);
expression.expression.append(" )");
} else if (formalParameterList != null) {
// the reference does not have an actual parameter list, but the assignment has
expression.expression.append(referredAssignment.getGenNameFromScope(aData, expression.expression, getMyScope(), null));
expression.expression.append("( ");
// FieldSubReference temp = ((FieldSubReference)subReferences.get(0));
for (int i = 0; i < formalParameterList.getNofParameters(); i++) {
if (i > 0) {
expression.expression.append(", ");
}
formalParameterList.getParameterByIndex(i).getDefaultValue().generateCode(aData, expression);
}
// temp.getActualParameters().generateCodeAlias(aData, expression);
expression.expression.append(" )");
} else {
// TODO add fuzzy handling
expression.expression.append(referredAssignment.getGenNameFromScope(aData, expression.expression, getMyScope(), null));
}
generateCode(aData, expression, isTemplate, false, referedGovernor);
}
use of org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct in project titan.EclipsePlug-ins by eclipse.
the class RecordSetCodeGenerator method genRawDecodeRecordField.
/**
* This function generates the code for decoding a record field.
*
* @param aData only used to update imports if needed.
* @param source where the source code is to be generated.
* @param fieldInfos the list of field informations.
* @param i the index of the field to generate for.
* @param raw the raw attribute of the record/set.
* @param raw_options the pre-calculated raw options.
* @param delayed_decode true to generated for delayed decoding.
* @param prev_ext_group the index of the previous extension group.
*/
private static void genRawDecodeRecordField(final JavaGenData aData, final StringBuilder source, final List<FieldInfo> fieldInfos, final int i, final RawASTStruct raw, final ArrayList<raw_option_struct> raw_options, final boolean delayed_decode, final AtomicInteger prev_ext_group) {
final raw_option_struct tempRawOption = raw_options.get(i);
if (tempRawOption.ptrbase) {
source.append(MessageFormat.format("start_pos_of_field{0} = buff.get_pos_bit();\n", i));
}
if (prev_ext_group.get() != tempRawOption.extbitgroup) {
prev_ext_group.set(tempRawOption.extbitgroup);
if (prev_ext_group.get() > 0 && raw.ext_bit_groups.get(tempRawOption.extbitgroup - 1).ext_bit != RawAST.XDEFNO) {
if (tempRawOption.pointerof > 0) {
final FieldInfo pointedField = fieldInfos.get(tempRawOption.pointerof - 1);
source.append("{\n");
source.append("int old_pos = buff.get_pos_bit();\n");
source.append(MessageFormat.format("if (start_of_field{0} != -1 && start_pos_of_field{1} != -1) '{'\n", i, pointedField.raw.pointerbase));
source.append(MessageFormat.format("start_of_field{0} = start_pos_of_field{1} + get{2}(){3}.getInt() * {4} + {5};\n", i, pointedField.raw.pointerbase, pointedField.mVarName, pointedField.isOptional ? ".get()" : "", pointedField.raw.unit, pointedField.raw.ptroffset));
source.append(MessageFormat.format("buff.set_pos_bit(start_of_field{0});\n", i));
source.append(MessageFormat.format("limit = end_of_available_data - start_of_field{0};\n", i));
}
source.append("{\n");
source.append("char[] data = buff.get_read_data();\n");
source.append("int count = 1;\n");
source.append("int rot = local_top_order == raw_order_t.ORDER_LSB ? 0: 7;\n");
source.append(MessageFormat.format("while (((data[count - 1] >> rot) & 0x01) == {0} && count * 8 < limit) '{'\n", raw.ext_bit_groups.get(tempRawOption.extbitgroup - 1).ext_bit == RawAST.XDEFYES ? 0 : 1));
source.append("count++;\n");
source.append("}\n");
source.append(" if (limit > 0) {\n");
source.append("group_limit = count * 8;\n");
source.append("}\n");
source.append("}\n");
if (tempRawOption.pointerof > 0) {
source.append(" } else {\n");
source.append("group_limit = 0;\n");
source.append("}\n");
source.append("buff.set_pos_bit(old_pos);\n");
source.append("limit = end_of_available_data - old_pos;\n");
source.append("}\n");
}
}
}
final FieldInfo fieldInfo = fieldInfos.get(i);
final int crosstagsize = fieldInfo.raw == null || fieldInfo.raw.crosstaglist == null || fieldInfo.raw.crosstaglist.list == null ? 0 : fieldInfo.raw.crosstaglist.list.size();
if (crosstagsize > 0) {
int other = -1;
boolean first_value = true;
for (int j = 0; j < crosstagsize; j++) {
final rawAST_coding_taglist cur_choice = fieldInfo.raw.crosstaglist.list.get(j);
if (cur_choice.fields != null && cur_choice.fields.size() > 0) {
if (first_value) {
source.append("if (");
first_value = false;
} else {
source.append(" else if (");
}
genRawFieldChecker(source, cur_choice, true);
source.append(") {\n");
source.append(MessageFormat.format("selected_field = {0};\n", cur_choice.fieldnum));
source.append("}");
} else {
other = cur_choice.fieldnum;
}
}
source.append(" else {\n");
source.append(MessageFormat.format("selected_field = {0};\n", other));
source.append("}\n");
}
/* check the presence of optional field*/
if (fieldInfo.isOptional) {
/* check if enough bits to decode the field*/
source.append("if ( limit > 0");
for (int a = 0; a < tempRawOption.lengthof; a++) {
final int field_index = tempRawOption.lengthofField.get(a);
if (i > field_index) {
source.append(MessageFormat.format(" && value_of_length_field{0} > 0", field_index));
}
}
if (tempRawOption.extbitgroup > 0 && raw.ext_bit_groups.get(tempRawOption.extbitgroup - 1).ext_bit != RawAST.XDEFNO) {
source.append(" && group_limit > 0");
}
if (tempRawOption.pointerof > 0) {
source.append(MessageFormat.format(" && start_of_field{0} != -1 && start_pos_of_field{1} != -1", i, fieldInfos.get(tempRawOption.pointerof - 1).raw.pointerbase));
}
final int presenceSize = fieldInfo.raw == null || fieldInfo.raw.presence == null || fieldInfo.raw.presence.fields == null ? 0 : fieldInfo.raw.presence.fields.size();
if (presenceSize > 0) {
source.append(" && ");
if (presenceSize > 1) {
source.append('(');
}
genRawFieldChecker(source, fieldInfo.raw.presence, true);
if (presenceSize > 1) {
source.append(')');
}
}
if (crosstagsize > 0) {
source.append("&& selected_field != -1");
}
source.append(") {\n");
}
if (tempRawOption.pointerof > 0) {
final FieldInfo tempPointed = fieldInfos.get(tempRawOption.pointerof - 1);
source.append(MessageFormat.format("start_of_field{0} = start_pos_of_field{1} + get{2}(){3}.getInt() * {4} + {5};\n", i, tempPointed.raw.pointerbase, tempPointed.mJavaVarName, tempPointed.isOptional ? ".get()" : "", tempPointed.raw.unit, tempPointed.raw.ptroffset));
source.append(MessageFormat.format("buff.set_pos_bit(start_of_field{0});\n", i));
source.append(MessageFormat.format("limit = end_of_available_data - start_of_field{0};\n", i));
}
if (fieldInfo.isOptional) {
source.append("int fl_start_pos = buff.get_pos_bit();\n");
}
final ExpressionStruct expression = new ExpressionStruct();
if (delayed_decode) {
/* the fixed field length is used as limit in case of delayed decoding */
expression.expression.append(fieldInfo.raw.length);
} else {
genRawFieldDecodeLimit(aData, expression, fieldInfos, i, raw, raw_options);
}
if (expression.preamble.length() > 0) {
source.append(expression.preamble);
}
source.append(MessageFormat.format("decoded_field_length = get{0}(){1}.RAW_decode({2}_descr_, buff, ", fieldInfo.mJavaVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.mTypeDescriptorName));
source.append(expression.expression);
source.append(MessageFormat.format(", local_top_order, {0}", fieldInfo.isOptional ? "true" : "no_err"));
if (crosstagsize > 0) {
source.append(", selected_field");
} else {
source.append(", -1");
}
boolean found = false;
for (int a = 0; a < tempRawOption.lengthof && !found; a++) {
final int field_index = tempRawOption.lengthofField.get(a);
if (fieldInfos.get(field_index).raw.unit == -1) {
source.append(MessageFormat.format(", value_of_length_field{0}", field_index));
found = true;
}
}
if (!found) {
source.append(", true");
}
source.append(");\n");
if (delayed_decode) {
source.append(MessageFormat.format("if ( decoded_field_length != {0}) '{'\n", fieldInfo.raw.length));
source.append("return -1;\n");
source.append("}\n");
} else if (fieldInfo.isOptional) {
source.append("if (decoded_field_length < 1) {\n");
source.append(MessageFormat.format("{0}.assign(template_sel.OMIT_VALUE);\n", fieldInfo.mVarName));
source.append("buff.set_pos_bit(fl_start_pos);\n");
source.append(" } else {\n");
} else {
source.append("if (decoded_field_length < 0) {\n");
source.append("return decoded_field_length;\n");
source.append("}\n");
}
if (tempRawOption.tag_type > 0 && raw.taglist.list.get(tempRawOption.tag_type - 1).fields.size() > 0) {
final rawAST_coding_taglist cur_choice = raw.taglist.list.get(tempRawOption.tag_type - 1);
source.append("if (");
genRawFieldChecker(source, cur_choice, false);
source.append(") {\n");
if (fieldInfo.isOptional) {
source.append(MessageFormat.format("{0}.assign(template_sel.OMIT_VALUE);\n", fieldInfo.mVarName));
source.append("buff.set_pos_bit(fl_start_pos);\n");
source.append(" } else {");
} else {
source.append("return -1;\n");
source.append("}\n");
}
}
if (!delayed_decode) {
source.append("decoded_length += decoded_field_length;\n");
source.append("limit -= decoded_field_length;\n");
source.append("last_decoded_pos = Math.max(last_decoded_pos, buff.get_pos_bit());\n");
}
if (tempRawOption.extbitgroup > 0 && raw.ext_bit_groups.get(tempRawOption.extbitgroup - 1).ext_bit != RawAST.XDEFNO) {
source.append("group_limit -= decoded_field_length;\n");
}
if (tempRawOption.lengthto) {
if (fieldInfo.raw.lengthindex != null) {
if (fieldInfo.raw.lengthindex.fieldtype == rawAST_coding_field_type.OPTIONAL_FIELD) {
source.append(MessageFormat.format("if ({0}{1}.get{2}().isPresent()) '{'\n", fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.raw.lengthindex.nthfieldname));
}
if (fieldInfo.raw.lengthto_offset != 0) {
source.append(MessageFormat.format("{0}{1}.get{2}(){3}.assign({0}{1}.get{2}(){3} - {4});\n", fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.raw.lengthindex.nthfieldname, fieldInfo.raw.lengthindex.fieldtype == rawAST_coding_field_type.OPTIONAL_FIELD ? ".get()" : "", fieldInfo.raw.lengthto_offset));
}
source.append(MessageFormat.format("value_of_length_field{0} += {1}{2}.get{3}(){4}.getInt() * {5};\n", i, fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.raw.lengthindex.nthfieldname, fieldInfo.raw.lengthindex.fieldtype == rawAST_coding_field_type.OPTIONAL_FIELD ? ".get()" : "", fieldInfo.raw.unit == -1 ? 1 : fieldInfo.raw.unit));
if (fieldInfo.raw.lengthindex.fieldtype == rawAST_coding_field_type.OPTIONAL_FIELD) {
source.append("}\n");
}
} else if (fieldInfo.raw.union_member_num > 0) {
source.append(MessageFormat.format("switch ({0}{1}.get_selection()) '{'\n", fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : ""));
for (int m = 0; m < fieldInfo.raw.member_name.size(); m++) {
source.append(MessageFormat.format("case {0}.{1}", fieldInfo.raw.member_name.get(0), fieldInfo.raw.member_name.get(m)));
if (fieldInfo.raw.lengthto_offset != 0) {
source.append(MessageFormat.format("{0}{1}.get{2}().assign({0}{1}.get{2}() - {3});\n", fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.raw.member_name.get(m), fieldInfo.raw.lengthto_offset));
}
source.append(MessageFormat.format("value_of_length_field{0} += {1}{2}.get{3}().getInt() * {4};\n", i, fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.raw.member_name.get(m), fieldInfo.raw.unit == -1 ? 1 : fieldInfo.raw.unit));
source.append("break;\n");
}
source.append("default:\n");
source.append(MessageFormat.format("value_of_length_field{0} = 0;\n", i));
source.append("}\n");
} else {
if (fieldInfo.raw.lengthto_offset != 0) {
source.append(MessageFormat.format("{0}{1}.assign({0}{1}.getInt() - {2});\n", fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.raw.lengthto_offset));
}
source.append(MessageFormat.format("value_of_length_field{0} += {1}{2}.getInt() * {3};\n", i, fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.raw.unit == -1 ? 1 : fieldInfo.raw.unit));
}
}
if (tempRawOption.pointerto) {
source.append(MessageFormat.format("start_of_field{0} = {1}{2}.getInt() {3};\n ", fieldInfo.raw.pointerto, fieldInfo.mVarName, fieldInfo.isOptional ? ".get()" : "", fieldInfo.raw.ptroffset > 0 ? " + 1" : "- 1"));
}
if (!delayed_decode) {
/* mark the used bits in length area*/
for (int a = 0; a < tempRawOption.lengthof; a++) {
final int field_index = tempRawOption.lengthofField.get(a);
source.append(MessageFormat.format("value_of_length_field{0} -= decoded_field_length;\n", field_index));
if (i == field_index) {
source.append(MessageFormat.format("if (value_of_length_field{0} < 0) '{'\n", field_index));
source.append("return -1;\n");
source.append("}\n");
}
}
}
if (fieldInfo.isOptional) {
source.append("}\n");
source.append("}");
if (tempRawOption.tag_type > 0) {
source.append("\n}");
}
source.append(" else {\n");
source.append(MessageFormat.format("{0}.assign(template_sel.OMIT_VALUE);\n", fieldInfo.mVarName));
source.append("}\n");
}
}
use of org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct in project titan.EclipsePlug-ins by eclipse.
the class SubsetMatch_Template method generateCodeInit.
@Override
public /**
* {@inheritDoc}
*/
void generateCodeInit(final JavaGenData aData, final StringBuilder source, final String name) {
if (lastTimeBuilt != null && !lastTimeBuilt.isLess(aData.getBuildTimstamp())) {
return;
}
lastTimeBuilt = aData.getBuildTimstamp();
aData.addBuiltinTypeImport("Base_Template.template_sel");
String ofTypeName;
switch(myGovernor.getTypetype()) {
case TYPE_SEQUENCE_OF:
ofTypeName = ((SequenceOf_Type) myGovernor).getOfType().getGenNameTemplate(aData, source, myScope);
break;
case TYPE_SET_OF:
ofTypeName = ((SetOf_Type) myGovernor).getOfType().getGenNameTemplate(aData, source, myScope);
break;
case TYPE_ARRAY:
ofTypeName = ((Array_Type) myGovernor).getElementType().getGenNameTemplate(aData, source, myScope);
break;
default:
ErrorReporter.INTERNAL_ERROR("FATAL ERROR while processing subset match template `" + getFullName() + "''");
return;
}
final ArrayList<Integer> variables = new ArrayList<Integer>();
long fixedPart = 0;
for (int i = 0; i < templates.getNofTemplates(); i++) {
final TTCN3Template templateListItem = templates.getTemplateByIndex(i);
if (templateListItem.getTemplatetype() == Template_type.ALL_FROM) {
variables.add(i);
} else {
fixedPart++;
}
}
if (variables.size() > 0) {
final StringBuilder preamble = new StringBuilder();
final StringBuilder setType = new StringBuilder();
final StringBuilder[] variableReferences = new StringBuilder[templates.getNofTemplates()];
setType.append(MessageFormat.format("{0}.setType(template_sel.SUBSET_MATCH, {1}", name, fixedPart));
for (int v = 0; v < variables.size(); v++) {
TTCN3Template template = templates.getTemplateByIndex(variables.get(v));
// the template must be all from
if (template instanceof All_From_Template) {
template = ((All_From_Template) template).getAllFrom();
}
final Reference reference = ((SpecificValue_Template) template).getReference();
final Assignment assignment = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
setType.append(" + ");
final ExpressionStruct expression = new ExpressionStruct();
reference.generateCode(aData, expression);
if (expression.preamble.length() > 0) {
preamble.append(expression.preamble);
}
switch(assignment.getAssignmentType()) {
case A_CONST:
case A_EXT_CONST:
case A_MODULEPAR:
case A_VAR:
case A_PAR_VAL:
case A_PAR_VAL_IN:
case A_PAR_VAL_OUT:
case A_PAR_VAL_INOUT:
case A_FUNCTION_RVAL:
case A_EXT_FUNCTION_RVAL:
if (assignment.getType(CompilationTimeStamp.getBaseTimestamp()).fieldIsOptional(reference.getSubreferences())) {
expression.expression.append(".get()");
}
break;
default:
break;
}
variableReferences[variables.get(v)] = expression.expression;
setType.append(expression.expression);
setType.append(".n_elem().getInt()");
}
source.append(preamble);
source.append(setType);
source.append(");\n");
final StringBuilder shifty = new StringBuilder();
for (int i = 0; i < templates.getNofTemplates(); i++) {
final TTCN3Template template = templates.getTemplateByIndex(i);
switch(template.getTemplatetype()) {
case ALL_FROM:
{
// the template must be all from
final StringBuilder storedExpression = variableReferences[i];
source.append(MessageFormat.format("for (int i_i = 0, i_lim = {0}.n_elem().getInt(); i_i < i_lim; ++i_i ) '{'\n", storedExpression));
final String embeddedName = MessageFormat.format("{0}.setItem({1}{2} + i_i)", name, i, shifty);
((All_From_Template) template).generateCodeInitAllFrom(aData, source, embeddedName, storedExpression);
source.append("}\n");
shifty.append(MessageFormat.format("-1 + {0}.n_elem().getInt()", storedExpression));
break;
}
default:
if (template.needsTemporaryReference()) {
final String tempId = aData.getTemporaryVariableName();
source.append("{\n");
source.append(MessageFormat.format("{0} {1} = {2}.setItem({3}{4});\n", ofTypeName, tempId, name, i, shifty));
template.generateCodeInit(aData, source, tempId);
source.append("}\n");
} else {
final String embeddedName = MessageFormat.format("{0}.setItem({1}{2})", name, i, shifty);
template.generateCodeInit(aData, source, embeddedName);
}
break;
}
}
} else {
source.append(MessageFormat.format("{0}.setType(template_sel.SUBSET_MATCH, {1});\n", name, templates.getNofTemplates()));
for (int i = 0; i < templates.getNofTemplates(); i++) {
final TTCN3Template template = templates.getTemplateByIndex(i);
if (template.needsTemporaryReference()) {
final String tempId = aData.getTemporaryVariableName();
source.append("{\n");
source.append(MessageFormat.format("{0} {1} = {2}.setItem({3});\n", ofTypeName, tempId, name, i));
template.generateCodeInit(aData, source, tempId);
source.append("}\n");
} else {
final String embeddedName = MessageFormat.format("{0}.setItem({1})", name, i);
template.generateCodeInit(aData, source, embeddedName);
}
}
}
if (lengthRestriction != null) {
if (getCodeSection() == CodeSectionType.CS_POST_INIT) {
lengthRestriction.reArrangeInitCode(aData, source, myScope.getModuleScope());
}
lengthRestriction.generateCodeInit(aData, source, name);
}
if (isIfpresent) {
source.append(name);
source.append(".set_ifPresent();\n");
}
}
use of org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct in project titan.EclipsePlug-ins by eclipse.
the class ValueList_Template method generateCodeInit.
@Override
public /**
* {@inheritDoc}
*/
void generateCodeInit(final JavaGenData aData, final StringBuilder source, final String name) {
if (lastTimeBuilt != null && !lastTimeBuilt.isLess(aData.getBuildTimstamp())) {
return;
}
lastTimeBuilt = aData.getBuildTimstamp();
aData.addBuiltinTypeImport("Base_Template.template_sel");
final ArrayList<Integer> variables = new ArrayList<Integer>();
long fixedPart = 0;
for (int i = 0; i < templates.getNofTemplates(); i++) {
final TTCN3Template templateListItem = templates.getTemplateByIndex(i);
if (templateListItem.getTemplatetype() == Template_type.ALL_FROM) {
variables.add(i);
} else {
fixedPart++;
}
}
final String typeName = myGovernor.getGenNameTemplate(aData, source, myScope);
if (variables.size() > 0) {
final StringBuilder preamble = new StringBuilder();
final StringBuilder setType = new StringBuilder();
final StringBuilder[] variableReferences = new StringBuilder[templates.getNofTemplates()];
setType.append(MessageFormat.format("{0}.setType(template_sel.VALUE_LIST, {1}", name, fixedPart));
for (int v = 0; v < variables.size(); v++) {
TTCN3Template template = templates.getTemplateByIndex(variables.get(v));
// the template must be all from
if (template instanceof All_From_Template) {
template = ((All_From_Template) template).getAllFrom();
}
final Reference reference = ((SpecificValue_Template) template).getReference();
final Assignment assignment = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
setType.append(" + ");
final ExpressionStruct expression = new ExpressionStruct();
reference.generateCode(aData, expression);
if (expression.preamble.length() > 0) {
preamble.append(expression.preamble);
}
switch(assignment.getAssignmentType()) {
case A_CONST:
case A_EXT_CONST:
case A_MODULEPAR:
case A_VAR:
case A_PAR_VAL:
case A_PAR_VAL_IN:
case A_PAR_VAL_OUT:
case A_PAR_VAL_INOUT:
case A_FUNCTION_RVAL:
case A_EXT_FUNCTION_RVAL:
if (assignment.getType(CompilationTimeStamp.getBaseTimestamp()).fieldIsOptional(reference.getSubreferences())) {
expression.expression.append(".get()");
}
break;
default:
break;
}
variableReferences[variables.get(v)] = expression.expression;
setType.append(expression.expression);
setType.append(".n_elem().getInt()");
}
source.append(preamble);
source.append(setType);
source.append(");\n");
final StringBuilder shifty = new StringBuilder();
for (int i = 0; i < templates.getNofTemplates(); i++) {
final TTCN3Template template = templates.getTemplateByIndex(i);
switch(template.getTemplatetype()) {
case ALL_FROM:
{
// the template must be all from
final StringBuilder storedExpression = variableReferences[i];
source.append(MessageFormat.format("for (int i_i = 0, i_lim = {0}.n_elem().getInt(); i_i < i_lim; ++i_i ) '{'\n", storedExpression));
final String embeddedName = MessageFormat.format("{0}.listItem({1}{2} + i_i)", name, i, shifty);
((All_From_Template) template).generateCodeInitAllFrom(aData, source, embeddedName, storedExpression);
source.append("}\n");
shifty.append(MessageFormat.format("-1 + {0}.n_elem().getInt()", storedExpression));
break;
}
default:
if (template.needsTemporaryReference()) {
final String tempId = aData.getTemporaryVariableName();
source.append("{\n");
source.append(MessageFormat.format("{0} {1} = {2}.listItem({3}{4});\n", typeName, tempId, name, i, shifty));
template.generateCodeInit(aData, source, tempId);
source.append("}\n");
} else {
final String embeddedName = MessageFormat.format("{0}.listItem({1}{2})", name, i, shifty);
template.generateCodeInit(aData, source, embeddedName);
}
break;
}
}
} else {
source.append(MessageFormat.format("{0}.setType(template_sel.VALUE_LIST, {1});\n", name, templates.getNofTemplates()));
for (int i = 0; i < templates.getNofTemplates(); i++) {
final TTCN3Template template = templates.getTemplateByIndex(i);
if (template.needsTemporaryReference()) {
final String tempId = aData.getTemporaryVariableName();
source.append("{\n");
source.append(MessageFormat.format("{0} {1} = {2}.listItem({3});\n", typeName, tempId, name, i));
template.generateCodeInit(aData, source, tempId);
source.append("}\n");
} else {
final String embeddedName = MessageFormat.format("{0}.listItem({1})", name, i);
template.generateCodeInit(aData, source, embeddedName);
}
}
}
if (lengthRestriction != null) {
if (getCodeSection() == CodeSectionType.CS_POST_INIT) {
lengthRestriction.reArrangeInitCode(aData, source, myScope.getModuleScope());
}
lengthRestriction.generateCodeInit(aData, source, name);
}
if (isIfpresent) {
source.append(name);
source.append(".set_ifPresent();\n");
}
}
Aggregations