use of org.eclipse.titan.designer.AST.TTCN3.templates.LengthRestriction in project titan.EclipsePlug-ins by eclipse.
the class SizeOfExpression method evaluateTemplate.
/**
* Evaluates a checked template.
*
* @param template
* The template to evaluate
* @param timestamp
* The compilation timestamp
* @return The folded value or -1 if the template is unfoldable.
*/
private long evaluateTemplate(final ITTCN3Template template, final CompilationTimeStamp timestamp) {
switch(template.getTemplatetype()) {
case TEMPLATE_LIST:
{
final Template_List temp = (Template_List) template;
if (temp.templateContainsAnyornone()) {
final LengthRestriction lengthRestriction = temp.getLengthRestriction();
if (lengthRestriction == null) {
templateInstance.getLocation().reportSemanticError("`sizeof' operation is not applicable for templates containing `*' without length restriction");
setIsErroneous(true);
return -1;
}
if (lengthRestriction instanceof RangeLenghtRestriction) {
final IValue upper = ((RangeLenghtRestriction) lengthRestriction).getUpperValue(timestamp);
if (Value_type.REAL_VALUE.equals(upper.getValuetype()) && ((Real_Value) upper).isPositiveInfinity()) {
templateInstance.getLocation().reportSemanticError("`sizeof' operation is not applicable for templates containing `*' without upper boundary in the length restriction");
setIsErroneous(true);
return -1;
}
if (Value_type.INTEGER_VALUE.equals(upper.getValuetype())) {
final int nofComponents = temp.getNofTemplatesNotAnyornone(timestamp);
if (nofComponents == ((Integer_Value) upper).intValue()) {
return nofComponents;
}
final IValue lower = ((RangeLenghtRestriction) lengthRestriction).getLowerValue(timestamp);
if (lower != null && Value_type.INTEGER_VALUE.equals(lower.getValuetype()) && ((Integer_Value) upper).intValue() == ((Integer_Value) lower).intValue()) {
return ((Integer_Value) upper).intValue();
}
templateInstance.getLocation().reportSemanticError("`sizeof' operation is not applicable for templates without exact size");
setIsErroneous(true);
return -1;
}
} else {
final IValue restriction = ((SingleLenghtRestriction) lengthRestriction).getRestriction(timestamp);
if (Value_type.INTEGER_VALUE.equals(restriction.getValuetype())) {
return ((Integer_Value) restriction).intValue();
}
}
} else {
int result = 0;
for (int i = 0, size = temp.getNofTemplates(); i < size; i++) {
final ITTCN3Template tmp = temp.getTemplateByIndex(i);
switch(tmp.getTemplatetype()) {
case SPECIFIC_VALUE:
if (tmp.getValue().getValuetype() != Value_type.OMIT_VALUE) {
++result;
}
break;
default:
++result;
}
}
return result;
}
break;
}
case NAMED_TEMPLATE_LIST:
{
int result = 0;
final Named_Template_List temp = (Named_Template_List) template;
for (int i = 0, size = temp.getNofTemplates(); i < size; i++) {
final ITTCN3Template tmp = temp.getTemplateByIndex(i).getTemplate();
switch(tmp.getTemplatetype()) {
case SPECIFIC_VALUE:
if (tmp.getValue().getValuetype() != Value_type.OMIT_VALUE) {
++result;
}
break;
default:
++result;
}
}
return result;
}
case SUBSET_MATCH:
{
final LengthRestriction restriction = template.getLengthRestriction();
if (restriction instanceof SingleLenghtRestriction) {
final IValue value = ((SingleLenghtRestriction) restriction).getRestriction(timestamp);
if (value.getValuetype() == Value_type.INTEGER_VALUE && !value.isUnfoldable(timestamp)) {
return ((Integer_Value) value).getValue();
} else {
return -1;
}
} else if (restriction instanceof RangeLenghtRestriction) {
final IValue minValue = ((RangeLenghtRestriction) restriction).getLowerValue(timestamp);
if (minValue.getValuetype() != Value_type.INTEGER_VALUE || minValue.isUnfoldable(timestamp)) {
return -1;
}
final SubsetMatch_Template temp = (SubsetMatch_Template) template;
if (temp.getNofTemplates() != ((Integer_Value) minValue).getValue()) {
return -1;
}
for (int i = 0, size = temp.getNofTemplates(); i < size; i++) {
final ITTCN3Template tmp = temp.getTemplateByIndex(i);
switch(tmp.getTemplatetype()) {
case SPECIFIC_VALUE:
break;
default:
return -1;
}
}
return temp.getNofTemplates();
}
return -1;
}
case SUPERSET_MATCH:
{
final LengthRestriction restriction = template.getLengthRestriction();
if (restriction instanceof SingleLenghtRestriction) {
final IValue value = ((SingleLenghtRestriction) restriction).getRestriction(timestamp);
if (value.getValuetype() == Value_type.INTEGER_VALUE && !value.isUnfoldable(timestamp)) {
return ((Integer_Value) value).getValue();
} else {
return -1;
}
} else if (restriction instanceof RangeLenghtRestriction) {
final IValue maxValue = ((RangeLenghtRestriction) restriction).getUpperValue(timestamp);
if (maxValue.getValuetype() != Value_type.INTEGER_VALUE || maxValue.isUnfoldable(timestamp)) {
return -1;
}
final SupersetMatch_Template temp = (SupersetMatch_Template) template;
if (temp.getNofTemplates() != ((Integer_Value) maxValue).getValue()) {
return -1;
}
for (int i = 0, size = temp.getNofTemplates(); i < size; i++) {
final ITTCN3Template tmp = temp.getTemplateByIndex(i);
switch(tmp.getTemplatetype()) {
case SPECIFIC_VALUE:
break;
default:
return -1;
}
}
return temp.getNofTemplates();
}
return -1;
}
default:
return -1;
}
return -1;
}
use of org.eclipse.titan.designer.AST.TTCN3.templates.LengthRestriction in project titan.EclipsePlug-ins by eclipse.
the class Indexed_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());
String ofTypeName;
switch(type.getTypetype()) {
case TYPE_SEQUENCE_OF:
ofTypeName = ((SequenceOf_Type) type).getOfType().getGenNameTemplate(aData, source, myScope);
break;
case TYPE_SET_OF:
ofTypeName = ((SetOf_Type) type).getOfType().getGenNameTemplate(aData, source, myScope);
break;
case TYPE_ARRAY:
ofTypeName = ((Array_Type) type).getElementType().getGenNameTemplate(aData, source, myScope);
break;
default:
ErrorReporter.INTERNAL_ERROR("FATAL ERROR while processing indexed template `" + getFullName() + "''");
return;
}
if (indexedTemplates.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 < indexedTemplates.getNofTemplates(); i++) {
final IndexedTemplate indexedTemplate = indexedTemplates.getTemplateByIndex(i);
final String tempId = aData.getTemporaryVariableName();
source.append("{\n");
final Value index = indexedTemplate.getIndex().getValue();
if (Value_type.INTEGER_VALUE.equals(index.getValuetype())) {
source.append(MessageFormat.format("{0} {1} = {2}.getAt({3});\n", ofTypeName, tempId, name, ((Integer_Value) index).getValue()));
} else {
final String tempId2 = aData.getTemporaryVariableName();
source.append(MessageFormat.format("TitanInteger {0} = new TitanInteger();\n", tempId2));
index.generateCodeInit(aData, source, tempId2);
source.append(MessageFormat.format("{0} {1} = {2}.getAt({3});\n", ofTypeName, tempId, name, tempId2));
}
indexedTemplate.getTemplate().generateCodeInit(aData, source, tempId);
source.append("}\n");
}
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.LengthRestriction in project titan.EclipsePlug-ins by eclipse.
the class Invoke_Template method generateCodeExpression.
@Override
public /**
* {@inheritDoc}
*/
void generateCodeExpression(final JavaGenData aData, final ExpressionStruct expression, final TemplateRestriction.Restriction_type templateRestriction) {
IType governor = myGovernor;
if (governor == null) {
governor = getExpressionGovernor(CompilationTimeStamp.getBaseTimestamp(), Expected_Value_type.EXPECTED_TEMPLATE);
}
if (governor == null) {
return;
}
if (lengthRestriction == null && !isIfpresent && templateRestriction == Restriction_type.TR_NONE) {
// The single expression must be tried first because this rule might cover some referenced templates.
if (hasSingleExpression()) {
final String genName = governor.getGenNameTemplate(aData, expression.expression, myScope);
expression.expression.append(MessageFormat.format("new {0}(", genName));
if (governor.getTypetype() == Type_type.TYPE_ARRAY) {
final Array_Type array_type = (Array_Type) governor;
expression.expression.append(MessageFormat.format(" {0}.class, ", array_type.getElementType().getGenNameTemplate(aData, expression.expression, myScope)));
}
expression.expression.append(getSingleExpression(aData, true));
expression.expression.append(')');
return;
}
generateCodeExpressionInvoke(aData, expression);
return;
}
final String tempId = aData.getTemporaryVariableName();
expression.preamble.append(MessageFormat.format("{0} {1} = new {0}();\n", governor.getGenNameTemplate(aData, expression.expression, myScope), tempId));
generateCodeInit(aData, expression.preamble, tempId);
if (templateRestriction != Restriction_type.TR_NONE) {
TemplateRestriction.generateRestrictionCheckCode(aData, expression.expression, location, tempId, templateRestriction);
}
expression.expression.append(tempId);
}
use of org.eclipse.titan.designer.AST.TTCN3.templates.LengthRestriction in project titan.EclipsePlug-ins by eclipse.
the class Invoke_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();
if (getCodeSection() == CodeSectionType.CS_POST_INIT) {
reArrangeInitCode(aData, source, myScope.getModuleScope());
}
final ExpressionStruct expression = new ExpressionStruct();
expression.expression.append(MessageFormat.format("{0}.assign(", name));
generateCodeExpressionInvoke(aData, expression);
expression.expression.append(')');
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.LengthRestriction in project titan.EclipsePlug-ins by eclipse.
the class Invoke_Template method updateSyntax.
@Override
public /**
* {@inheritDoc}
*/
void updateSyntax(final TTCN3ReparseUpdater reparser, final boolean isDamaged) throws ReParseException {
if (isDamaged) {
throw new ReParseException();
}
if (lengthRestriction != null) {
lengthRestriction.updateSyntax(reparser, false);
reparser.updateLocation(lengthRestriction.getLocation());
}
if (baseTemplate instanceof IIncrementallyUpdateable) {
((IIncrementallyUpdateable) baseTemplate).updateSyntax(reparser, false);
reparser.updateLocation(baseTemplate.getLocation());
} else if (baseTemplate != null) {
throw new ReParseException();
}
if (value != null) {
value.updateSyntax(reparser, false);
reparser.updateLocation(value.getLocation());
}
if (actualParameterList != null) {
actualParameterList.updateSyntax(reparser, false);
reparser.updateLocation(actualParameterList.getLocation());
}
}
Aggregations