use of org.eclipse.titan.designer.AST.ParameterisedSubReference in project titan.EclipsePlug-ins by eclipse.
the class AltGuards method generateCodeAlt.
/**
* Generate code for an alt
*
* @param aData the structure to put imports into and get temporal variable names from.
* @param source the source code generated
*/
public void generateCodeAlt(final JavaGenData aData, final StringBuilder source) {
aData.addBuiltinTypeImport("TitanAlt_Status");
boolean labelNeeded = hasRepeat;
boolean hasElseBranch = false;
for (int i = 0; i < altGuards.size(); i++) {
final AltGuard altGuard = altGuards.get(i);
switch(altGuard.getType()) {
case AG_OP:
if (((Operation_Altguard) altGuard).getGuardStatement().canRepeat()) {
labelNeeded = true;
}
break;
case AG_REF:
case AG_INVOKE:
labelNeeded = true;
break;
case AG_ELSE:
hasElseBranch = true;
break;
default:
ErrorReporter.INTERNAL_ERROR("unknown altguard type encountered `" + getFullName() + "''");
return;
}
if (hasElseBranch) {
break;
}
}
// if there is no [else] branch the defaults may return ALT_REPEAT
if (!hasElseBranch) {
labelNeeded = true;
}
// opening bracket of the statement block
label = aData.getTemporaryVariableName();
if (labelNeeded) {
source.append(label).append(":\n");
}
source.append("for ( ; ; ) {\n");
// temporary variables used for caching of status codes
for (int i = 0; i < altGuards.size(); i++) {
final AltGuard altGuard = altGuards.get(i);
if (altGuard.getType().equals(altguard_type.AG_ELSE)) {
break;
}
source.append(MessageFormat.format("TitanAlt_Status {0}_alt_flag_{1} = ", label, i));
if (altGuard.getGuardExpression() == null) {
source.append("TitanAlt_Status.ALT_MAYBE");
} else {
source.append("TitanAlt_Status.ALT_UNCHECKED");
}
source.append(";\n");
}
if (!hasElseBranch) {
source.append(MessageFormat.format("TitanAlt_Status {0}_default_flag = TitanAlt_Status.ALT_MAYBE;\n", label));
}
// the first snapshot is taken in non-blocking mode
aData.addCommonLibraryImport("TTCN_Snapshot");
source.append("TTCN_Snapshot.takeNew(false);\n");
// and opening infinite for() loop
source.append("for ( ; ; ) {\n");
for (int i = 0; i < altGuards.size(); i++) {
final AltGuard altGuard = altGuards.get(i);
final altguard_type altGuardType = altGuard.getType();
if (altGuardType.equals(altguard_type.AG_ELSE)) {
source.append("TTCN_Snapshot.elseBranchReached();\n");
StatementBlock block = altGuard.getStatementBlock();
if (block.getSize() > 0) {
source.append("{\n");
// FIXME handle debugger
block.generateCode(aData, source);
source.append("}\n");
}
if (block.hasReturn(CompilationTimeStamp.getBaseTimestamp()) != ReturnStatus_type.RS_YES) {
source.append("break;\n");
}
// do not generate code for further branches
break;
} else {
final IValue guardExpression = altGuard.getGuardExpression();
if (guardExpression != null) {
source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_UNCHECKED) '{'\n", label, i));
guardExpression.getLocation().update_location_object(aData, source);
final ExpressionStruct expression = new ExpressionStruct();
guardExpression.generateCodeExpression(aData, expression, true);
source.append(expression.preamble);
source.append(MessageFormat.format("if(TitanBoolean.getNative({0})) '{'\n", expression.expression));
source.append(MessageFormat.format("{0}_alt_flag_{1} = TitanAlt_Status.ALT_MAYBE;\n", label, i));
source.append("} else {\n");
source.append(MessageFormat.format("{0}_alt_flag_{1} = TitanAlt_Status.ALT_NO;\n", label, i));
source.append("}\n");
source.append("}\n");
}
source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_MAYBE) '{'\n", label, i));
boolean canRepeat = false;
final ExpressionStruct expression = new ExpressionStruct();
expression.expression.append(MessageFormat.format("{0}_alt_flag_{1} = ", label, i));
switch(altGuardType) {
case AG_OP:
{
// the guard operation is a receiving statement
final Statement statement = ((Operation_Altguard) altGuard).getGuardStatement();
altGuard.getLocation().update_location_object(aData, source);
statement.generateCodeExpression(aData, expression);
canRepeat = statement.canRepeat();
}
break;
case AG_REF:
{
// the guard operation is an altstep instance
final Reference reference = ((Referenced_Altguard) altGuard).getGuardReference();
altGuard.getLocation().update_location_object(aData, source);
final Assignment altstep = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
expression.expression.append(MessageFormat.format("{0}_instance(", altstep.getGenNameFromScope(aData, source, myScope, "")));
final ISubReference subreference = reference.getSubreferences().get(0);
((ParameterisedSubReference) subreference).getActualParameters().generateCodeAlias(aData, expression);
source.append(')');
canRepeat = true;
}
break;
case AG_INVOKE:
{
// the guard operation is an altstep invocation
altGuard.getLocation().update_location_object(aData, source);
((Invoke_Altguard) altGuard).generateCodeInvokeInstance(aData, expression);
canRepeat = true;
}
break;
default:
ErrorReporter.INTERNAL_ERROR("unknown altguard type encountered `" + getFullName() + "''");
return;
}
expression.mergeExpression(source);
if (canRepeat) {
source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_REPEAT) continue {2};\n", label, i, label));
}
if (altGuardType.equals(altguard_type.AG_REF) || altGuardType.equals(altguard_type.AG_INVOKE)) {
source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_BREAK) break;\n", label, i));
}
// execution of statement block if the guard was successful
source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_YES) ", label, i));
final StatementBlock block = altGuard.getStatementBlock();
if (block != null && block.getSize() > 0) {
source.append("{\n");
// TODO handle debugger
block.generateCode(aData, source);
if (!ReturnStatus_type.RS_YES.equals(block.hasReturn(CompilationTimeStamp.getBaseTimestamp()))) {
source.append("break;\n");
}
source.append("}\n");
} else {
source.append("break;\n");
}
source.append("}\n");
}
}
if (!hasElseBranch) {
aData.addCommonLibraryImport("TTCN_Default");
source.append(MessageFormat.format("if ({0}_default_flag == TitanAlt_Status.ALT_MAYBE) '{'\n", label));
source.append(MessageFormat.format("{0}_default_flag = TTCN_Default.tryAltsteps();\n", label));
source.append(MessageFormat.format("if ({0}_default_flag == TitanAlt_Status.ALT_YES || {0}_default_flag == TitanAlt_Status.ALT_BREAK) '{'\n", label));
source.append("break;\n");
source.append(MessageFormat.format("} else if({0}_default_flag == TitanAlt_Status.ALT_REPEAT) '{'\n", label));
source.append(MessageFormat.format("continue {0};\n", label));
source.append("}\n");
source.append("}\n");
getLocation().update_location_object(aData, source);
// error handling and taking the next snapshot in blocking mode
source.append("if ( ");
for (int i = 0; i < altGuards.size(); i++) {
source.append(MessageFormat.format("{0}_alt_flag_{1} == TitanAlt_Status.ALT_NO &&", label, i));
}
source.append(MessageFormat.format("{0}_default_flag == TitanAlt_Status.ALT_NO) '{'\n", label));
source.append("throw new TtcnError(\"None of the branches can be chosen in the alt statement");
// TODO translate_string
if (location != null && location.getFile() != null) {
source.append(MessageFormat.format("in file {0} at line {1}", location.getFile().getName(), location.getLine()));
}
source.append("\");\n");
source.append("}\n");
source.append("TTCN_Snapshot.takeNew(true);\n");
}
source.append("}\n");
source.append("break;\n");
source.append("}\n");
}
use of org.eclipse.titan.designer.AST.ParameterisedSubReference in project titan.EclipsePlug-ins by eclipse.
the class AltGuards method generateCodeAltstep.
/**
* Generate code for an altstep
*
* @param aData the structure to put imports into and get temporal variable names from.
* @param source the source code generated
*/
public void generateCodeAltstep(final JavaGenData aData, final StringBuilder source) {
final boolean hasElse = hasElse();
if (!hasElse) {
source.append("TitanAlt_Status returnValue = TitanAlt_Status.ALT_NO;\n");
}
for (int i = 0; i < altGuards.size(); i++) {
final AltGuard altGuard = altGuards.get(i);
final altguard_type altGuardType = altGuard.getType();
if (altGuardType.equals(altguard_type.AG_ELSE)) {
source.append("TTCN_Snapshot.elseBranchReached();\n");
final StatementBlock block = altGuard.getStatementBlock();
if (block.getSize() > 0) {
source.append("{\n");
// TODO debugger
block.generateCode(aData, source);
source.append("}\n");
}
if (block.hasReturn(CompilationTimeStamp.getBaseTimestamp()) != ReturnStatus_type.RS_YES) {
source.append("return TitanAlt_Status.ALT_YES;\n");
}
break;
} else {
final AtomicInteger blockCount = new AtomicInteger(0);
final IValue guardExpression = altGuard.getGuardExpression();
if (guardExpression != null) {
guardExpression.getLocation().update_location_object(aData, source);
guardExpression.generateCodeTmp(aData, source, "if (", blockCount);
source.append(") {\n");
blockCount.incrementAndGet();
}
boolean canRepeat = false;
final ExpressionStruct expression = new ExpressionStruct();
switch(altGuardType) {
case AG_OP:
{
final Statement statement = ((Operation_Altguard) altGuard).getGuardStatement();
altGuard.getLocation().update_location_object(aData, source);
statement.generateCodeExpression(aData, expression);
canRepeat = statement.canRepeat();
}
break;
case AG_REF:
{
// the guard operation is an altstep instance
final Reference reference = ((Referenced_Altguard) altGuard).getGuardReference();
altGuard.getLocation().update_location_object(aData, source);
final Assignment altstep = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
expression.expression.append(MessageFormat.format("{0}_instance(", altstep.getGenNameFromScope(aData, source, myScope, "")));
final ISubReference subreference = reference.getSubreferences().get(0);
((ParameterisedSubReference) subreference).getActualParameters().generateCodeAlias(aData, expression);
source.append(')');
canRepeat = true;
}
break;
case AG_INVOKE:
{
// the guard operation is an altstep invocation
altGuard.getLocation().update_location_object(aData, source);
((Invoke_Altguard) altGuard).generateCodeInvokeInstance(aData, expression);
canRepeat = true;
}
break;
default:
ErrorReporter.INTERNAL_ERROR("unknown altguard type encountered `" + getFullName() + "''");
return;
}
if (expression.preamble.length() > 0 || expression.postamble.length() > 0) {
if (blockCount.get() == 0) {
source.append("{\n");
blockCount.set(blockCount.get() + 1);
}
final String tempId = aData.getTemporaryVariableName();
source.append(MessageFormat.format("TitanAlt_Status {0};\n", tempId));
source.append("{\n");
source.append(expression.preamble);
source.append(MessageFormat.format("{0}.assign({1});\n", tempId, expression.expression));
source.append(expression.postamble);
source.append("}\n");
source.append(MessageFormat.format("switch ({0}) '{'\n", tempId));
} else {
source.append(MessageFormat.format("switch ({0}) '{'\n", expression.expression));
}
source.append("case ALT_YES:\n");
final StatementBlock block = altGuard.getStatementBlock();
if (block != null && block.getSize() > 0) {
source.append("{\n");
// TODO handle debugger
block.generateCode(aData, source);
source.append("}\n");
}
if (block == null || block.hasReturn(CompilationTimeStamp.getBaseTimestamp()) != ReturnStatus_type.RS_YES) {
source.append("return TitanAlt_Status.ALT_YES;\n");
}
if (canRepeat) {
source.append("case ALT_REPEAT:\n");
source.append("return TitanAlt_Status.ALT_REPEAT;\n");
}
if (altGuardType == altguard_type.AG_REF || altGuardType == altguard_type.AG_INVOKE) {
source.append("case ALT_BREAK:\n");
source.append("return TitanAlt_Status.ALT_BREAK;\n");
}
if (!hasElse) {
source.append("case ALT_MAYBE:\n");
source.append("returnValue = TitanAlt_Status.ALT_MAYBE;\n");
}
source.append("default:\n");
source.append("break;\n");
source.append("}\n");
// closing statement blocks
for (int j = 0; j < blockCount.get(); j++) {
source.append("}\n");
}
}
}
if (!hasElse) {
source.append("return returnValue;\n");
}
}
use of org.eclipse.titan.designer.AST.ParameterisedSubReference in project titan.EclipsePlug-ins by eclipse.
the class Referenced_Template method reArrangeInitCode.
@Override
public /**
* {@inheritDoc}
*/
void reArrangeInitCode(final JavaGenData aData, final StringBuilder source, final Module usageModule) {
final ISubReference tempSubreference = reference.getSubreferences().get(0);
if (tempSubreference instanceof ParameterisedSubReference) {
// generate code for the templates that are used in the actual parameter
// list of the reference
final ActualParameterList actualParameterList = ((ParameterisedSubReference) tempSubreference).getActualParameters();
if (actualParameterList != null) {
actualParameterList.reArrangeInitCode(aData, source, usageModule);
}
}
final Assignment assignment = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
if (assignment.getAssignmentType() != Assignment_type.A_TEMPLATE) {
return;
}
ITTCN3Template template = ((Def_Template) assignment).getTemplate(CompilationTimeStamp.getBaseTimestamp());
final FormalParameterList formalParameterList = ((Def_Template) assignment).getFormalParameterList();
if (formalParameterList != null) {
// the reference points to a parameterized template
// we must perform the rearrangement for all non-parameterized templates
// that are referred by the parameterized template regardless of the
// sub-references of reference
template.reArrangeInitCode(aData, source, usageModule);
// be generated when the template's definition is reached)
if (assignment.getMyScope().getModuleScope() == usageModule) {
formalParameterList.generateCodeDefaultValues(aData, source);
}
} else {
// the reference points to a non-parameterized template
final List<ISubReference> subReferences = reference.getSubreferences();
if (subReferences != null && subReferences.size() > 1) {
// and perform the rearrangement for the referred field only
for (int i = 1; i < subReferences.size(); i++) {
final ISubReference subReference = subReferences.get(i);
if (subReference instanceof FieldSubReference) {
// stop if the body does not have fields
if (template.getTemplatetype() != Template_type.NAMED_TEMPLATE_LIST) {
break;
}
// the field reference can be followed
final Identifier fieldId = ((FieldSubReference) subReference).getId();
template = ((Named_Template_List) template).getNamedTemplate(fieldId).getTemplate();
} else {
// stop if the body is not a list
if (template.getTemplatetype() != Template_type.TEMPLATE_LIST) {
break;
}
IValue arrayIndex = ((ArraySubReference) subReference).getValue();
final IReferenceChain referenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
arrayIndex = arrayIndex.getValueRefdLast(CompilationTimeStamp.getBaseTimestamp(), referenceChain);
referenceChain.release();
if (arrayIndex.getValuetype() != Value_type.INTEGER_VALUE) {
break;
}
// the index is available at compilation time
long index = ((Integer_Value) arrayIndex).getValue();
// index transformation in case of arrays
if (template.getMyGovernor().getTypetype() == Type_type.TYPE_ARRAY) {
index = index - ((Array_Type) template.getMyGovernor()).getDimension().getOffset();
}
template = ((Template_List) template).getTemplateByIndex((int) index);
}
}
}
// we should initialize its entire body
if (assignment.getMyScope().getModuleScope() == usageModule) {
template.generateCodeInit(aData, source, template.get_lhs_name());
}
}
if (lengthRestriction != null) {
lengthRestriction.reArrangeInitCode(aData, source, usageModule);
}
}
use of org.eclipse.titan.designer.AST.ParameterisedSubReference in project titan.EclipsePlug-ins by eclipse.
the class TemplateInstance method reArrangeInitCode.
/**
* Walks through the templateinstance recursively and appends the java
* initialization sequence of all (directly or indirectly) referenced
* non-parameterized templates and the default values of all
* parameterized templates to source and returns the resulting string.
* Only objects belonging to module usageModule are initialized.
*
* @param aData the structure to put imports into and get temporal variable names from.
* @param source the source for code generated
* @param usageModule the module where the template is to be used.
*/
public void reArrangeInitCode(final JavaGenData aData, final StringBuilder source, final Module usageModule) {
if (derivedReference != null) {
final List<ISubReference> subreferences = derivedReference.getSubreferences();
if (subreferences != null && !subreferences.isEmpty() && subreferences.get(0) instanceof ParameterisedSubReference) {
final ParameterisedSubReference subreference = (ParameterisedSubReference) subreferences.get(0);
final ActualParameterList actualParameterList = subreference.getActualParameters();
if (actualParameterList != null) {
actualParameterList.reArrangeInitCode(aData, source, usageModule);
}
}
final Assignment assignment = derivedReference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
if (assignment == null) {
return;
}
if (assignment.getAssignmentType() == Assignment_type.A_TEMPLATE) {
final ITTCN3Template template = ((Def_Template) assignment).getTemplate(CompilationTimeStamp.getBaseTimestamp());
final FormalParameterList formalParameterList = ((Def_Template) assignment).getFormalParameterList();
if (formalParameterList != null) {
// the referred template is parameterized
// the embedded referenced templates shall be visited
template.reArrangeInitCode(aData, source, usageModule);
// FIXME implement
} else {
// its entire body has to be initialized now
if (assignment.getMyScope().getModuleScope() == usageModule) {
template.generateCodeInit(aData, source, template.get_lhs_name());
}
}
}
}
if (templateBody != null) {
templateBody.reArrangeInitCode(aData, source, usageModule);
}
}
use of org.eclipse.titan.designer.AST.ParameterisedSubReference in project titan.EclipsePlug-ins by eclipse.
the class All_From_Template method checkThisTemplateParameterizedReference.
private boolean checkThisTemplateParameterizedReference(final Reference reference, final Assignment lhs) {
final List<ISubReference> subreferences = reference.getSubreferences();
if (subreferences.isEmpty() || !(subreferences.get(0) instanceof ParameterisedSubReference)) {
return false;
}
final ParameterisedSubReference subReference = (ParameterisedSubReference) subreferences.get(0);
final ActualParameterList actualParameterList = subReference.getActualParameters();
if (actualParameterList == null) {
return false;
}
final int nofParameters = actualParameterList.getNofParameters();
for (int i = 0; i < nofParameters; i++) {
Reference parameterReference = null;
final ActualParameter actualParameter = actualParameterList.getParameter(i);
if (actualParameter instanceof Template_ActualParameter) {
TemplateInstance templateInstance = ((Template_ActualParameter) actualParameter).getTemplateInstance();
ITTCN3Template template = templateInstance.getTemplateBody();
template = template.setLoweridToReference(CompilationTimeStamp.getBaseTimestamp());
if (template.getTemplatetype() == Template_type.TEMPLATE_REFD) {
parameterReference = ((Referenced_Template) template).getReference();
}
} else if (actualParameter instanceof Referenced_ActualParameter) {
parameterReference = ((Referenced_ActualParameter) actualParameter).getReference();
}
if (parameterReference != null) {
final Assignment assignment = parameterReference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
if (assignment == lhs) {
return true;
}
// check their parameters as well
switch(assignment.getAssignmentType()) {
case A_TEMPLATE:
case A_FUNCTION_RVAL:
case A_FUNCTION_RTEMP:
case A_EXT_FUNCTION_RVAL:
case A_EXT_FUNCTION_RTEMP:
if (checkThisTemplateParameterizedReference(parameterReference, lhs)) {
return true;
}
break;
default:
break;
}
}
}
return false;
}
Aggregations