use of org.eclipse.titan.designer.AST.TTCN3.templates.TemplateInstances in project titan.EclipsePlug-ins by eclipse.
the class FormalParameterList method collateLazyAndNonLazyActualParameters.
/**
* Read the parsed actual parameters, and collate the lazy and non-lazy actual parameters
* according to their associated formal parameters.
*
* @param timestamp
* the timestamp of the actual semantic check cycle.
* @param parsedParameters
* the parsed actual parameters (may contain named, and unnamed parts too).
* @param actualLazyParameters
* the list of actual lazy parameters returned for later usage.
* @param actualNonLazyParameters
* the list of actual non lazy parameters returned for later usage.
*/
public final void collateLazyAndNonLazyActualParameters(final CompilationTimeStamp timestamp, final ParsedActualParameters parsedParameters, final ActualParameterList actualLazyParameters, final ActualParameterList actualNonLazyParameters) {
final TemplateInstances unnamed = parsedParameters.getInstances();
final NamedParameters named = parsedParameters.getNamedParameters();
int nofLocated = unnamed.getNofTis();
final Map<FormalParameter, Integer> formalParameterMap = new HashMap<FormalParameter, Integer>();
for (int i = 0, size = parameters.size(); i < size; i++) {
formalParameterMap.put(parameters.get(i), Integer.valueOf(i));
}
final TemplateInstances finalUnnamed = new TemplateInstances(unnamed);
for (int i = 0, size = named.getNofParams(); i < size; i++) {
final NamedParameter namedParameter = named.getParamByIndex(i);
final FormalParameter formalParameter = parameterMap.get(namedParameter.getName().getName());
final int isAt = formalParameterMap.get(formalParameter);
for (; nofLocated < isAt; nofLocated++) {
final NotUsed_Template temp = new NotUsed_Template();
if (!parameters.get(nofLocated).hasDefaultValue()) {
temp.setIsErroneous(true);
}
final TemplateInstance instance = new TemplateInstance(null, null, temp);
instance.setLocation(parsedParameters.getLocation());
finalUnnamed.addTemplateInstance(instance);
}
finalUnnamed.addTemplateInstance(namedParameter.getInstance());
nofLocated++;
}
finalUnnamed.setLocation(parsedParameters.getLocation());
final int upperLimit = (finalUnnamed.getNofTis() < parameters.size()) ? finalUnnamed.getNofTis() : parameters.size();
for (int i = 0; i < upperLimit; i++) {
final TemplateInstance instance = finalUnnamed.getInstanceByIndex(i);
final FormalParameter formalParameter = parameters.get(i);
if (instance.getType() == null && instance.getDerivedReference() == null && Template_type.TEMPLATE_NOTUSED.equals(instance.getTemplateBody().getTemplatetype())) {
final ActualParameter defaultValue = formalParameter.getDefaultValue();
final Default_ActualParameter temp = new Default_ActualParameter(defaultValue);
if (defaultValue != null && !defaultValue.getIsErroneous()) {
temp.setLocation(defaultValue.getLocation());
}
if (formalParameter.getIsLazy()) {
actualLazyParameters.addParameter(temp);
} else {
actualNonLazyParameters.addParameter(temp);
}
} else {
final ActualParameter actualParameter = formalParameter.checkActualParameter(timestamp, instance, Expected_Value_type.EXPECTED_DYNAMIC_VALUE);
actualParameter.setLocation(instance.getLocation());
if (formalParameter.getIsLazy()) {
actualLazyParameters.addParameter(actualParameter);
} else {
actualNonLazyParameters.addParameter(actualParameter);
}
}
}
for (int i = upperLimit; i < parameters.size(); i++) {
final FormalParameter formalParameter = parameters.get(i);
final ActualParameter defaultValue = formalParameter.getDefaultValue();
final Default_ActualParameter temp = new Default_ActualParameter(defaultValue);
if (defaultValue != null && !defaultValue.getIsErroneous()) {
temp.setLocation(defaultValue.getLocation());
}
if (formalParameter.getIsLazy()) {
actualLazyParameters.addParameter(temp);
} else {
actualNonLazyParameters.addParameter(temp);
}
}
}
use of org.eclipse.titan.designer.AST.TTCN3.templates.TemplateInstances in project titan.EclipsePlug-ins by eclipse.
the class FormalParameterList method checkActualParameterList.
/**
* Check if a list of parsed actual parameters is semantically correct
* according to a list of formal parameters (the called entity).
*
* @param timestamp
* the timestamp of the actual semantic check cycle.
* @param parsedParameters
* the parsed actual parameters (may contain named, and
* unnamed parts too).
* @param actualParameters
* the list of actual parameters returned for later
* usage.
*
* @return true if a semantic error was found, false otherwise
*/
public final boolean checkActualParameterList(final CompilationTimeStamp timestamp, final ParsedActualParameters parsedParameters, final ActualParameterList actualParameters) {
parsedParameters.setFormalParList(this);
checkUniqueness(timestamp);
boolean isErroneous = false;
final TemplateInstances unnamed = parsedParameters.getInstances();
final NamedParameters named = parsedParameters.getNamedParameters();
int nofLocated = unnamed.getNofTis();
final Map<FormalParameter, Integer> formalParameterMap = new HashMap<FormalParameter, Integer>();
for (int i = 0, size = parameters.size(); i < size; i++) {
formalParameterMap.put(parameters.get(i), Integer.valueOf(i));
}
final TemplateInstances finalUnnamed = new TemplateInstances(unnamed);
for (int i = 0, size = named.getNofParams(); i < size; i++) {
final NamedParameter namedParameter = named.getParamByIndex(i);
if (parameterMap != null && parameterMap.containsKey(namedParameter.getName().getName())) {
final FormalParameter formalParameter = parameterMap.get(namedParameter.getName().getName());
final int isAt = formalParameterMap.get(formalParameter);
if (isAt >= nofLocated) {
for (; nofLocated < isAt; nofLocated++) {
final NotUsed_Template temp = new NotUsed_Template();
if (!parameters.get(nofLocated).hasDefaultValue()) {
temp.setIsErroneous(true);
}
final TemplateInstance instance = new TemplateInstance(null, null, temp);
instance.setLocation(parsedParameters.getLocation());
finalUnnamed.addTemplateInstance(instance);
}
finalUnnamed.addTemplateInstance(namedParameter.getInstance());
nofLocated++;
} else {
isErroneous = true;
if (isAt >= unnamed.getNofTis()) {
namedParameter.getLocation().reportSemanticError(MessageFormat.format("Named parameter `{0}'' is out of order", namedParameter.getName().getDisplayName()));
} else {
namedParameter.getLocation().reportSemanticError(MessageFormat.format("Named parameter `{0}'' is assigned more than once", namedParameter.getName().getDisplayName()));
}
}
} else {
String name;
switch(myDefinition.getAssignmentType()) {
case A_TYPE:
switch(((Def_Type) myDefinition).getType(timestamp).getTypetype()) {
case TYPE_FUNCTION:
name = "Function reference";
break;
case TYPE_ALTSTEP:
name = "Altstep reference";
break;
case TYPE_TESTCASE:
name = "Testcase reference";
break;
default:
name = "";
break;
}
break;
default:
name = myDefinition.getAssignmentName();
break;
}
isErroneous = true;
namedParameter.getLocation().reportSemanticError(MessageFormat.format("{0} `{1}'' has no formal parameter with name `{2}''", name, myDefinition.getFullName(), namedParameter.getName().getDisplayName()));
}
}
if (isErroneous) {
return false;
}
finalUnnamed.setLocation(parsedParameters.getLocation());
return checkActualParameterList(timestamp, finalUnnamed, actualParameters);
}
use of org.eclipse.titan.designer.AST.TTCN3.templates.TemplateInstances in project titan.EclipsePlug-ins by eclipse.
the class SelectCoverage method process.
@Override
protected void process(final IVisitableNode node, final Problems problems) {
if (!(node instanceof SelectCase_Statement)) {
return;
}
final SelectCase_Statement s = (SelectCase_Statement) node;
final Value v = s.getExpression();
if (v == null || v.getIsErroneous(timestamp)) {
return;
}
// if there is an else branch, no smell will be reported
final SelectCases scs = s.getSelectCases();
if (scs == null || scs.getSelectCaseArray() == null) {
return;
}
for (final SelectCase sc : scs.getSelectCaseArray()) {
if (sc.hasElse()) {
return;
}
}
IType itype = v.getExpressionGovernor(timestamp, Expected_Value_type.EXPECTED_TEMPLATE);
if (itype instanceof Referenced_Type) {
itype = itype.getTypeRefdLast(timestamp);
}
if (itype == null || !(itype instanceof TTCN3_Enumerated_Type)) {
return;
}
final TTCN3_Enumerated_Type enumType = (TTCN3_Enumerated_Type) itype;
// count number of items in enum, get all items from enum
final EnumItemVisitor enumVisitor = new EnumItemVisitor();
enumType.accept(enumVisitor);
// count number of TemplateInstances in select, get used enum items
final CaseVisitor caseVisitor = new CaseVisitor();
scs.accept(caseVisitor);
if (caseVisitor.isContainsUnfoldable()) {
return;
}
final int casesSize = caseVisitor.getCount();
final int enumSize = enumVisitor.getCount();
if (enumSize > casesSize) {
final List<Identifier> allEnumItems = enumVisitor.getItemsFound();
final List<Identifier> usedEnumItems = caseVisitor.getItemsUsed();
final String enumName = itype.getTypename();
final String itemsNotCovered = getItemsNotCovered(allEnumItems, usedEnumItems);
problems.report(v.getLocation(), MessageFormat.format(ERR_MSG, enumName, enumSize, casesSize, itemsNotCovered));
}
}
use of org.eclipse.titan.designer.AST.TTCN3.templates.TemplateInstances in project titan.EclipsePlug-ins by eclipse.
the class SelectCase method generateCode.
/**
* Add generated java code for a single select case.
*
* @param aData the structure to put imports into and get temporal variable names from.
* @param source the source code generated
* @param name the name to compare the branch variables to (expression or temporary name)
* @param unReachable tells whether this branch is already unreachable because of previous conditions
*/
public void generateCode(final JavaGenData aData, final StringBuilder source, final String name, final AtomicBoolean unreach) {
ExpressionStruct expression = new ExpressionStruct();
StringBuilder condition = new StringBuilder();
if (templateInstances != null) {
for (int i = 0; i < templateInstances.getNofTis(); i++) {
final String tmp = aData.getTemporaryVariableName();
final TemplateInstance templateInstance = templateInstances.getInstanceByIndex(i);
final TTCN3Template templateBody = templateInstance.getTemplateBody();
final boolean isValue = templateInstance.getDerivedReference() == null && templateBody.isValue(CompilationTimeStamp.getBaseTimestamp());
final IType last = templateInstance.getExpressionGovernor(CompilationTimeStamp.getBaseTimestamp(), Expected_Value_type.EXPECTED_DYNAMIC_VALUE);
if (i > 0) {
condition.append(" || ");
}
if (isValue) {
final String genName = last.getGenNameValue(aData, expression.expression, last.getMyScope());
expression.expression.append(MessageFormat.format("{0} {1} = new {0} (", genName, tmp));
IValue value = templateBody.getValue();
value.getValueRefdLast(CompilationTimeStamp.getBaseTimestamp(), null);
value.generateCodeExpressionMandatory(aData, expression, true);
expression.expression.append(");\n");
condition.append(MessageFormat.format("{0}.operatorEquals({1})", tmp, name));
} else {
final String genName = last.getGenNameTemplate(aData, expression.expression, last.getMyScope());
expression.expression.append(MessageFormat.format("{0} {1} = new {0} (", genName, tmp));
templateInstance.generateCode(aData, expression, Restriction_type.TR_NONE);
expression.expression.append(");\n");
condition.append(MessageFormat.format("{0}.match({1})", tmp, name));
}
}
source.append(expression.preamble);
source.append(expression.expression);
source.append(expression.postamble);
getLocation().update_location_object(aData, source);
source.append("if (").append(condition).append(") {\n");
statementBlock.generateCode(aData, source);
source.append("}\n");
} else {
statementBlock.generateCode(aData, source);
unreach.set(true);
}
}
use of org.eclipse.titan.designer.AST.TTCN3.templates.TemplateInstances in project titan.EclipsePlug-ins by eclipse.
the class FormalParameterList method checkActualParameterList.
/**
* Check if a list of parsed actual parameters is semantically correct
* according to a list of formal parameters (the called entity).
*
* @param timestamp
* the timestamp of the actual semantic check cycle.
* @param instances
* the list of actual parameters containing both the
* named and the unnamed part converted into an unnamed
* list, that is full and in correct order.
* @param actualParameters
* the list of actual parameters returned for later
* usage.
*
* @return true if a semantic error was found, false otherwise
*/
private boolean checkActualParameterList(final CompilationTimeStamp timestamp, final TemplateInstances instances, final ActualParameterList actualParameters) {
boolean isErroneous = false;
if (minimumNofParameters == parameters.size()) {
if (instances.getNofTis() != parameters.size()) {
instances.getLocation().reportSemanticError(MessageFormat.format("Too {0} parameters: {1} was expected instead of {2}", (instances.getNofTis() < parameters.size()) ? "few" : "many", parameters.size(), instances.getNofTis()));
isErroneous = true;
}
} else {
if (instances.getNofTis() < minimumNofParameters) {
instances.getLocation().reportSemanticError(MessageFormat.format("Too few parameters: at least {0} was expected instaed of {1}", minimumNofParameters, instances.getNofTis()));
isErroneous = true;
} else if (instances.getNofTis() > parameters.size()) {
instances.getLocation().reportSemanticError(MessageFormat.format("Too many parameters: at most {0} was expected instead of {1}", parameters.size(), instances.getNofTis()));
isErroneous = true;
}
}
final int upperLimit = (instances.getNofTis() < parameters.size()) ? instances.getNofTis() : parameters.size();
for (int i = 0; i < upperLimit; i++) {
final TemplateInstance instance = instances.getInstanceByIndex(i);
final FormalParameter formalParameter = parameters.get(i);
if (instance.getType() == null && instance.getDerivedReference() == null && Template_type.TEMPLATE_NOTUSED.equals(instance.getTemplateBody().getTemplatetype())) {
if (formalParameter.hasDefaultValue()) {
final ActualParameter defaultValue = formalParameter.getDefaultValue();
final Default_ActualParameter temp = new Default_ActualParameter(defaultValue);
actualParameters.addParameter(temp);
if (defaultValue == null || defaultValue.getIsErroneous()) {
isErroneous = true;
} else {
temp.setLocation(defaultValue.getLocation());
}
} else if (instance.getTemplateBody().getIsErroneous(timestamp)) {
instances.getLocation().reportSemanticError(MessageFormat.format(MISSINGPARAMETER, formalParameter.getIdentifier().getDisplayName()));
isErroneous = true;
} else {
instance.getLocation().reportSemanticError("Not used symbol (`-'') cannot be used for parameter that does not have a default value");
final ActualParameter temp = new Value_ActualParameter(null);
temp.setLocation(instances.getLocation());
temp.setIsErroneous();
actualParameters.addParameter(temp);
isErroneous = true;
}
} else {
final ActualParameter actualParameter = formalParameter.checkActualParameter(timestamp, instance, Expected_Value_type.EXPECTED_DYNAMIC_VALUE);
actualParameter.setLocation(instance.getLocation());
actualParameters.addParameter(actualParameter);
if (actualParameter.getIsErroneous()) {
isErroneous = true;
}
}
}
for (int i = upperLimit; i < parameters.size(); i++) {
final FormalParameter formalParameter = parameters.get(i);
if (formalParameter.hasDefaultValue()) {
final ActualParameter defaultValue = formalParameter.getDefaultValue();
final Default_ActualParameter temp = new Default_ActualParameter(defaultValue);
actualParameters.addParameter(temp);
if (defaultValue == null || defaultValue.getIsErroneous()) {
isErroneous = true;
} else {
temp.setLocation(defaultValue.getLocation());
}
} else {
final ActualParameter temp = new Value_ActualParameter(null);
temp.setLocation(instances.getLocation());
temp.setIsErroneous();
actualParameters.addParameter(temp);
isErroneous = true;
}
}
return isErroneous;
}
Aggregations