use of org.eclipse.titan.designer.AST.TTCN3.definitions.Definition in project titan.EclipsePlug-ins by eclipse.
the class Referenced_Template method generateRearrangeInitCodeReferenced.
private void generateRearrangeInitCodeReferenced(final JavaGenData aData, final StringBuilder source, final ExpressionStruct expression) {
/**
* Initially we can assume that:
* - this is a referenced template and a part of a non-parameterized template
* - u.ref.ref points to (a field of) a non-parameterized template within the same module as this.
* - this ensures that the do-while loop will run at least twice (i.e. the first continue statement will be reached in the first iteration)
*/
final Stack<ISubReference> referenceStack = new Stack<ISubReference>();
ITTCN3Template template = this;
for (; ; ) {
if (template.getTemplatetype() == Template_type.TEMPLATE_REFD) {
final Reference reference = ((Referenced_Template) template).getReference();
final Assignment assignment = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
/**
* Don't follow the reference if:
* - the referenced definition is not a template
* - the referenced template is parameterized or
* - the referenced template is in different module
*/
if (assignment.getAssignmentType() == Assignment_type.A_TEMPLATE && ((Def_Template) assignment).getFormalParameterList() == null && assignment.getMyScope().getModuleScope() == myScope.getModuleScope()) {
// accumulate the sub-references of the referred reference
final List<ISubReference> subReferences = reference.getSubreferences();
if (subReferences != null && subReferences.size() > 1) {
for (int i = subReferences.size(); i > 1; i--) {
referenceStack.push(subReferences.get(i - 1));
}
}
// jump to the referred top-level template
template = ((Def_Template) assignment).getTemplate(CompilationTimeStamp.getBaseTimestamp());
// start the iteration from the beginning
continue;
} else {
// the reference cannot be followed
break;
}
}
// stop if there are no sub-references
if (referenceStack.isEmpty()) {
break;
}
// take the topmost sub-reference
final ISubReference subReference = referenceStack.peek();
if (subReference instanceof FieldSubReference) {
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 {
// trying to follow an array reference
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);
}
// the topmost sub-reference was processed
// it can be erased from the stack
referenceStack.pop();
}
// the smallest dependent template is now in t
// generate the initializer sequence for t
template.generateCodeInit(aData, source, template.get_lhs_name());
// the equivalent Java code of the referenced template is composed of the
// genname of t and the remained sub-references in refstack
expression.expression.append(template.getGenNameOwn(myScope));
while (!referenceStack.isEmpty()) {
final ISubReference subReference = referenceStack.pop();
if (subReference instanceof FieldSubReference) {
expression.expression.append(MessageFormat.format(".get{0}()", FieldSubReference.getJavaGetterName(((FieldSubReference) subReference).getId().getName())));
} else {
expression.expression.append(".getAt(");
((ArraySubReference) subReference).getValue().generateCodeExpression(aData, expression, false);
expression.expression.append(')');
}
}
}
use of org.eclipse.titan.designer.AST.TTCN3.definitions.Definition 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.TTCN3.definitions.Definition in project titan.EclipsePlug-ins by eclipse.
the class Referenced_Template method checkPresentRestriction.
@Override
public /**
* {@inheritDoc}
*/
boolean checkPresentRestriction(final CompilationTimeStamp timestamp, final String definitionName, final Location usageLocation) {
checkRestrictionCommon(timestamp, definitionName, TemplateRestriction.Restriction_type.TR_PRESENT, usageLocation);
if (reference != null) {
final Assignment ass = reference.getRefdAssignment(timestamp, true);
switch(ass.getAssignmentType()) {
case A_TEMPLATE:
final ITTCN3Template templateLast = getTemplateReferencedLast(timestamp);
return templateLast.checkPresentRestriction(timestamp, definitionName, usageLocation);
case A_VAR_TEMPLATE:
case A_EXT_FUNCTION_RTEMP:
case A_FUNCTION_RTEMP:
case A_PAR_TEMP_IN:
case A_PAR_TEMP_OUT:
case A_PAR_TEMP_INOUT:
if (ass instanceof Definition) {
TemplateRestriction.Restriction_type refdTemplateRestriction = ((Definition) ass).getTemplateRestriction();
refdTemplateRestriction = TemplateRestriction.getSubRestriction(refdTemplateRestriction, timestamp, reference);
// if restriction not satisfied issue warning
if (TemplateRestriction.isLessRestrictive(TemplateRestriction.Restriction_type.TR_PRESENT, refdTemplateRestriction)) {
getLocation().reportSemanticWarning(MessageFormat.format(INADEQUATETEMPLATERESTRICTION, ass.getAssignmentName(), reference.getDisplayName()));
return true;
}
}
return false;
default:
return false;
}
}
return false;
}
use of org.eclipse.titan.designer.AST.TTCN3.definitions.Definition in project titan.EclipsePlug-ins by eclipse.
the class Start_Referenced_Component_Statement method generateCode.
@Override
public /**
* {@inheritDoc}
*/
void generateCode(final JavaGenData aData, final StringBuilder source) {
final ExpressionStruct expression = new ExpressionStruct();
final IReferenceChain referenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
final IValue last = dereferredValue.getValueRefdLast(CompilationTimeStamp.getBaseTimestamp(), referenceChain);
referenceChain.release();
if (last.getValuetype() == Value_type.FUNCTION_REFERENCE_VALUE) {
Definition definition = ((Function_Reference_Value) last).getReferredFunction();
expression.expression.append(MessageFormat.format("{0}(", definition.getGenNameFromScope(aData, source, myScope, "start_")));
} else {
dereferredValue.generateCodeExpressionMandatory(aData, expression, true);
expression.expression.append(".start(");
}
componentReference.generateCodeExpression(aData, expression, false);
if (actualParameterList2.getNofParameters() > 0) {
expression.expression.append(", ");
actualParameterList2.generateCodeNoAlias(aData, expression);
}
expression.expression.append(')');
expression.mergeExpression(source);
}
use of org.eclipse.titan.designer.AST.TTCN3.definitions.Definition in project titan.EclipsePlug-ins by eclipse.
the class Stop_Component_Statement method generateCode.
@Override
public /**
* {@inheritDoc}
*/
void generateCode(final JavaGenData aData, final StringBuilder source) {
aData.addCommonLibraryImport("TTCN_Runtime");
final ExpressionStruct expression = new ExpressionStruct();
if (componentReference == null) {
aData.addBuiltinTypeImport("TitanComponent");
expression.expression.append("TTCN_Runtime.stop_component(TitanComponent.ALL_COMPREF)");
} else {
final IReferenceChain referenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
final IValue last = componentReference.getValueRefdLast(CompilationTimeStamp.getBaseTimestamp(), referenceChain);
referenceChain.release();
if (last.getValuetype() == Value_type.REFERENCED_VALUE) {
// the argument is a simple component reference
last.generateCodeExpressionMandatory(aData, expression, true);
expression.expression.append(".stop()");
} else {
boolean refers_to_self = false;
if (last.getValuetype() == Value_type.EXPRESSION_VALUE) {
// the argument is a special component reference (mtc, self, etc.)
switch(((Expression_Value) last).getOperationType()) {
case MTC_COMPONENT_OPERATION:
{
Definition myDefinition = myStatementBlock.getMyDefinition();
if (myDefinition != null && myDefinition.getAssignmentType() == Assignment_type.A_TESTCASE) {
refers_to_self = true;
}
break;
}
case SELF_COMPONENT_OPERATION:
refers_to_self = true;
default:
break;
}
}
if (refers_to_self) {
expression.expression.append("TTCN_Runtime.stop_execution()");
} else {
expression.expression.append("TTCN_Runtime.stop_component(");
last.generateCodeExpression(aData, expression, false);
expression.expression.append(')');
}
}
}
expression.mergeExpression(source);
}
Aggregations