use of org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template in project titan.EclipsePlug-ins by eclipse.
the class Indexed_Template_List method getReferencedArrayTemplate.
@Override
protected /**
* {@inheritDoc}
*/
ITTCN3Template getReferencedArrayTemplate(final CompilationTimeStamp timestamp, final IValue arrayIndex, final IReferenceChain referenceChain) {
IValue indexValue = arrayIndex.setLoweridToReference(timestamp);
indexValue = indexValue.getValueRefdLast(timestamp, referenceChain);
if (indexValue.getIsErroneous(timestamp)) {
return null;
}
long index = 0;
if (!indexValue.isUnfoldable(timestamp)) {
if (Value_type.INTEGER_VALUE.equals(indexValue.getValuetype())) {
index = ((Integer_Value) indexValue).getValue();
} else {
arrayIndex.getLocation().reportSemanticError("An integer value was expected as index");
return null;
}
} else {
return null;
}
final IType tempType = myGovernor.getTypeRefdLast(timestamp);
if (tempType.getIsErroneous(timestamp)) {
return null;
}
switch(tempType.getTypetype()) {
case TYPE_SEQUENCE_OF:
{
if (index < 0) {
final String message = MessageFormat.format("A non-negative integer value was expected instead of {0} for indexing a template of `sequence of'' type `{1}''", index, tempType.getTypename());
arrayIndex.getLocation().reportSemanticError(message);
return null;
}
break;
}
case TYPE_SET_OF:
{
if (index < 0) {
final String message = MessageFormat.format("A non-negative integer value was expected instead of {0} for indexing a template of `set of'' type `{1}''", index, tempType.getTypename());
arrayIndex.getLocation().reportSemanticError(message);
return null;
}
break;
}
case TYPE_ARRAY:
{
final ArrayDimension dimension = ((Array_Type) tempType).getDimension();
dimension.checkIndex(timestamp, indexValue, Expected_Value_type.EXPECTED_DYNAMIC_VALUE);
if (!dimension.getIsErroneous(timestamp)) {
// re-base the index
index -= dimension.getOffset();
if (index < 0 || index > getNofTemplates()) {
arrayIndex.getLocation().reportSemanticError(MessageFormat.format("The index value {0} is outside the array indexable range", index + dimension.getOffset()));
return null;
}
} else {
return null;
}
break;
}
default:
arrayIndex.getLocation().reportSemanticError(MessageFormat.format("Invalid array element reference: type `{0}'' cannot be indexed", tempType.getTypename()));
return null;
}
for (int i = 0, size = indexedTemplates.getNofTemplates(); i < size; i++) {
final IndexedTemplate template = indexedTemplates.getTemplateByIndex(i);
IValue lastValue = template.getIndex().getValue();
final IReferenceChain chain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
lastValue = lastValue.getValueRefdLast(timestamp, chain);
chain.release();
if (Value_type.INTEGER_VALUE.equals(lastValue.getValuetype())) {
final long tempIndex = ((Integer_Value) lastValue).getValue();
if (index == tempIndex) {
final ITTCN3Template realTemplate = template.getTemplate();
if (Template_type.TEMPLATE_NOTUSED.equals(realTemplate.getTemplatetype())) {
if (baseTemplate != null) {
return baseTemplate.getTemplateReferencedLast(timestamp, referenceChain).getReferencedArrayTemplate(timestamp, indexValue, referenceChain);
}
return null;
}
return realTemplate;
}
}
}
switch(tempType.getTypetype()) {
case TYPE_SEQUENCE_OF:
case TYPE_SET_OF:
case TYPE_ARRAY:
// unfoldable (for now)
break;
default:
// the error was reported earlier
break;
}
return null;
}
use of org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template 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.templates.ITTCN3Template in project titan.EclipsePlug-ins by eclipse.
the class Referenced_Template method getTemplateReferenced.
/**
* Calculates the referenced template, and while doing so checks the
* reference too.
*
* @param timestamp
* the time stamp of the actual semantic check cycle.
* @param referenceChain
* the reference chain used to detect cyclic references.
*
* @return the template referenced
*/
private ITTCN3Template getTemplateReferenced(final CompilationTimeStamp timestamp, final IReferenceChain referenceChain) {
if (reference == null) {
setIsErroneous(true);
return null;
}
final Assignment ass = reference.getRefdAssignment(timestamp, true);
if (ass == null) {
setIsErroneous(true);
return this;
}
ITTCN3Template template = null;
switch(ass.getAssignmentType()) {
case A_TEMPLATE:
template = ((Def_Template) ass).getTemplate(timestamp);
break;
case A_VAR_TEMPLATE:
((Def_Var_Template) ass).check(timestamp);
template = ((Def_Var_Template) ass).getInitialValue();
break;
case A_MODULEPAR_TEMPLATE:
template = ((Def_ModulePar_Template) ass).getDefaultTemplate();
break;
default:
setIsErroneous(true);
return this;
}
if (template != null) {
template = template.getReferencedSubTemplate(timestamp, reference, referenceChain);
}
final List<ISubReference> subreferences = reference.getSubreferences();
if (template != null) {
return template;
} else if (subreferences != null && reference.hasUnfoldableIndexSubReference(timestamp)) {
// some array indices could not be evaluated
} else if (reference.getUsedInIsbound()) {
return this;
} else {
setIsErroneous(true);
}
return this;
}
use of org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template 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.templates.ITTCN3Template 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;
}
Aggregations