use of org.eclipse.titan.designer.AST.TTCN3.templates.TTCN3Template in project titan.EclipsePlug-ins by eclipse.
the class Template_List 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();
if (asValue != null) {
asValue.generateCodeInit(aData, source, name);
return;
}
if (converted != null) {
converted.generateCodeInit(aData, source, name);
return;
}
if (myGovernor == null) {
return;
}
// TODO special case for empty list
final IType typeLast = myGovernor.getTypeRefdLast(CompilationTimeStamp.getBaseTimestamp());
long indexOffset = 0;
if (typeLast.getTypetype().equals(Type_type.TYPE_ARRAY)) {
indexOffset = ((Array_Type) typeLast).getDimension().getOffset();
}
String ofTypeName;
switch(typeLast.getTypetype()) {
case TYPE_SEQUENCE_OF:
ofTypeName = ((SequenceOf_Type) typeLast).getOfType().getGenNameTemplate(aData, source, myScope);
break;
case TYPE_SET_OF:
ofTypeName = ((SetOf_Type) typeLast).getOfType().getGenNameTemplate(aData, source, myScope);
break;
case TYPE_ARRAY:
ofTypeName = ((Array_Type) typeLast).getElementType().getGenNameTemplate(aData, source, myScope);
break;
default:
ErrorReporter.INTERNAL_ERROR("FATAL ERROR while processing template list `" + getFullName() + "''");
return;
}
if (hasPermutation || hasAllFrom()) {
long fixedPart = 0;
final StringBuilder preamble = new StringBuilder();
final StringBuilder setSize = new StringBuilder();
final StringBuilder body = new StringBuilder();
final String counter = aData.getTemporaryVariableName();
body.append(MessageFormat.format("int {0} = {1};\n", counter, indexOffset));
for (int i = 0, size = templates.getNofTemplates(); i < size; i++) {
final TTCN3Template template = templates.getTemplateByIndex(i);
if (template.getTemplatetype() == Template_type.ALL_FROM) {
final TTCN3Template subTemplate = ((All_From_Template) template).getAllFrom();
final Reference reference = ((SpecificValue_Template) subTemplate).getReference();
final Assignment assignment = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
setSize.append(" + ");
final ExpressionStruct referenceExpression = new ExpressionStruct();
reference.generateCode(aData, referenceExpression);
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())) {
referenceExpression.expression.append(".get()");
}
break;
default:
break;
}
setSize.append(referenceExpression.expression);
if (referenceExpression.preamble.length() > 0) {
preamble.append(referenceExpression.preamble);
}
setSize.append(".n_elem().getInt()");
body.append(MessageFormat.format("for (int i_i = 0, i_lim = {0}.n_elem().getInt(); i_i < i_lim; ++i_i ) '{'\n", referenceExpression.expression));
final String embeddedName = MessageFormat.format("{0}.getAt({1} + i_i)", name, counter);
((All_From_Template) template).generateCodeInitAllFrom(aData, body, embeddedName, referenceExpression.expression);
body.append("}\n");
body.append(MessageFormat.format("{0} += {1}.n_elem().getInt();\n", counter, referenceExpression.expression));
} else if (template.getTemplatetype() == Template_type.PERMUTATION_MATCH) {
final int numPermutations = ((PermutationMatch_Template) template).getNofTemplates();
final String permutationStart = aData.getTemporaryVariableName();
body.append(MessageFormat.format("int {0} = {1};\n", permutationStart, counter));
for (int j = 0; j < numPermutations; j++) {
final TTCN3Template template2 = ((PermutationMatch_Template) template).getTemplateByIndex(j);
if (template2.getTemplatetype() == Template_type.ALL_FROM) {
final TTCN3Template subTemplate = ((All_From_Template) template2).getAllFrom();
final Reference reference = ((SpecificValue_Template) subTemplate).getReference();
final Assignment assignment = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
setSize.append(" + ");
final ExpressionStruct referenceExpression = new ExpressionStruct();
reference.generateCode(aData, referenceExpression);
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())) {
referenceExpression.expression.append(".get()");
}
break;
default:
break;
}
setSize.append(referenceExpression.expression);
if (referenceExpression.preamble.length() > 0) {
preamble.append(referenceExpression.preamble);
}
setSize.append(".n_elem().getInt()");
body.append(MessageFormat.format("for (int i_i = 0, i_lim = {0}.n_elem().getInt(); i_i < i_lim; ++i_i ) '{'\n", referenceExpression.expression));
final String embeddedName = MessageFormat.format("{0}.getAt({1} + i_i)", name, counter);
((All_From_Template) template2).generateCodeInitAllFrom(aData, body, embeddedName, referenceExpression.expression);
body.append("}\n");
body.append(MessageFormat.format("{0} += {1}.n_elem().getInt();\n", counter, referenceExpression.expression));
template2.lastTimeBuilt = aData.buildTimestamp;
} else {
fixedPart++;
template2.generateCodeInitSeofElement(aData, body, name, counter, ofTypeName);
body.append(MessageFormat.format("{0}++;\n", counter));
}
}
// do not consider index_offset in case of permutation indicators
body.append(MessageFormat.format("{0}.add_permutation({1}, {2} - 1);\n", name, permutationStart, counter));
template.lastTimeBuilt = aData.buildTimestamp;
} else {
fixedPart++;
template.generateCodeInitSeofElement(aData, body, name, counter, ofTypeName);
body.append(MessageFormat.format("{0}++;\n", counter));
}
}
source.append(preamble);
source.append(MessageFormat.format("{0}.setSize({1}", name, fixedPart));
source.append(setSize);
source.append(");\n");
source.append(body);
} else {
// source.append(MessageFormat.format("{0}.setSize({1});\n", name, getNofTemplates()));
int index = 0;
for (int i = 0, size = templates.getNofTemplates(); i < size; i++) {
final TTCN3Template template = templates.getTemplateByIndex(i);
switch(template.getTemplatetype()) {
case PERMUTATION_MATCH:
final PermutationMatch_Template actualTemplate = (PermutationMatch_Template) template;
final int nofPermutatedTemplates = actualTemplate.getNofTemplates();
for (int j = 0; j < nofPermutatedTemplates; j++) {
final long ix = indexOffset + index + j;
final TTCN3Template template2 = actualTemplate.getTemplateByIndex(j);
template2.generateCodeInitSeofElement(aData, source, name, Long.toString(ix), ofTypeName);
}
// do not consider index_offset in case of permutation indicators
source.append(MessageFormat.format("{0}.add_permutation({1}, {2});\n", name, index, index + nofPermutatedTemplates - 1));
template.lastTimeBuilt = aData.buildTimestamp;
index += nofPermutatedTemplates;
break;
case ALL_FROM:
case TEMPLATE_NOTUSED:
index++;
break;
default:
template.generateCodeInitSeofElement(aData, source, name, Long.toString(indexOffset + index), ofTypeName);
index++;
break;
}
}
}
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.templates.TTCN3Template in project titan.EclipsePlug-ins by eclipse.
the class Named_Template_List 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();
if (asValue != null) {
asValue.generateCodeInit(aData, source, name);
return;
}
if (myGovernor == null) {
return;
}
// FIXME actually a bit more complex
final IType type = myGovernor.getTypeRefdLast(CompilationTimeStamp.getBaseTimestamp());
if (type == null) {
return;
}
if (namedTemplates.getNofTemplates() == 0) {
aData.addBuiltinTypeImport("TitanNull_Type");
source.append(MessageFormat.format("{0}.assign(TitanNull_Type.NULL_VALUE);\n", name));
}
// else is not needed as the loop will not run
for (int i = 0; i < namedTemplates.getNofTemplates(); i++) {
final NamedTemplate namedTemplate = namedTemplates.getTemplateByIndex(i);
final String fieldName = namedTemplate.getName().getName();
// FIXME handle needs_temp_ref case
final String generatedFieldName = FieldSubReference.getJavaGetterName(fieldName);
final TTCN3Template template = namedTemplate.getTemplate();
if (template.needsTemporaryReference()) {
Type fieldType;
switch(type.getTypetype()) {
case TYPE_SIGNATURE:
fieldType = ((Signature_Type) type).getParameterByName(fieldName).getType();
break;
case TYPE_TTCN3_SEQUENCE:
fieldType = ((TTCN3_Sequence_Type) type).getComponentByName(fieldName).getType();
break;
case TYPE_TTCN3_SET:
fieldType = ((TTCN3_Set_Type) type).getComponentByName(fieldName).getType();
break;
case TYPE_ASN1_SEQUENCE:
fieldType = ((ASN1_Sequence_Type) type).getComponentByName(new Identifier(Identifier_type.ID_NAME, fieldName)).getType();
break;
case TYPE_ASN1_SET:
fieldType = ((ASN1_Set_Type) type).getComponentByName(new Identifier(Identifier_type.ID_NAME, fieldName)).getType();
break;
case TYPE_ASN1_CHOICE:
fieldType = ((ASN1_Choice_Type) type).getComponentByName(new Identifier(Identifier_type.ID_NAME, fieldName)).getType();
break;
case TYPE_TTCN3_CHOICE:
fieldType = ((TTCN3_Choice_Type) type).getComponentByName(fieldName).getType();
break;
case TYPE_OPENTYPE:
fieldType = ((Open_Type) type).getComponentByName(new Identifier(Identifier_type.ID_NAME, fieldName)).getType();
break;
case TYPE_ANYTYPE:
fieldType = ((Anytype_Type) type).getComponentByName(fieldName).getType();
break;
default:
ErrorReporter.INTERNAL_ERROR("FATAL ERROR while processing named template list `" + getFullName() + "''");
return;
}
final String tempId = aData.getTemporaryVariableName();
source.append("{\n");
source.append(MessageFormat.format("{0} {1} = {2}.get{3}();\n", fieldType.getGenNameTemplate(aData, source, myScope), tempId, name, generatedFieldName));
template.generateCodeInit(aData, source, tempId);
source.append("}\n");
} else {
final String embeddedName = MessageFormat.format("{0}.get{1}()", name, generatedFieldName);
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