use of org.eclipse.titan.designer.AST.TTCN3.statements.Statement 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.statements.Statement in project titan.EclipsePlug-ins by eclipse.
the class Port_Utility method checkConnectionEndpoint.
/**
* Checks a reference to see if it really references a valid component
* type.
*
* @param timestamp
* the timestamp of the actual semantic check cycle.
* @param source
* the statement to report an error to, in case it is in
* a control part.
* @param componentReference
* the reference of the component to be checked.
* @param portReference
* the reference to a port of the component to be
* checked.
* @param allowSystem
* tells if the system component should be allowed or not
* as an endpoint.
*
* @return the referenced component type, or null if there were
* problems.
*/
public static IType checkConnectionEndpoint(final CompilationTimeStamp timestamp, final Statement source, final Value componentReference, final PortReference portReference, final boolean allowSystem) {
final IType componentType = checkComponentReference(timestamp, source, componentReference, true, allowSystem);
if (portReference == null) {
return componentType;
}
if (componentType == null) {
// the component type can not be determined
final List<ISubReference> subreferences = portReference.getSubreferences();
if (subreferences.size() > 1) {
// check array indices
for (int i = 0; i < subreferences.size(); i++) {
final ISubReference subreference = subreferences.get(i);
if (subreference instanceof ArraySubReference) {
final Value value = ((ArraySubReference) subreference).getValue();
value.setLoweridToReference(timestamp);
final Type_type temporalType1 = value.getExpressionReturntype(timestamp, Expected_Value_type.EXPECTED_DYNAMIC_VALUE);
switch(temporalType1) {
case TYPE_INTEGER:
{
final IReferenceChain referenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
final IValue last1 = value.getValueRefdLast(timestamp, Expected_Value_type.EXPECTED_DYNAMIC_VALUE, referenceChain);
referenceChain.release();
if (!last1.isUnfoldable(timestamp) && Value.Value_type.INTEGER_VALUE.equals(last1.getValuetype())) {
if (((Integer_Value) last1).signum() < 0) {
value.getLocation().reportSemanticError(ArraySubReference.NATIVEINTEGEREXPECTED);
value.setIsErroneous(true);
}
}
break;
}
case TYPE_UNDEFINED:
value.setIsErroneous(true);
break;
default:
if (!value.getIsErroneous(timestamp)) {
value.getLocation().reportSemanticError(ArraySubReference.INTEGERINDEXEXPECTED);
value.setIsErroneous(true);
}
break;
}
}
}
}
return null;
}
final ComponentTypeBody componentBody = ((Component_Type) componentType).getComponentBody();
portReference.setBaseScope(componentBody);
portReference.setComponent((Component_Type) componentType);
// for compatibility
portReference.getRefdAssignment(timestamp, false);
final Identifier portIdentifier = portReference.getId();
if (!componentBody.hasLocalAssignmentWithId(portIdentifier)) {
portReference.getLocation().reportSemanticError(MessageFormat.format(NOPORTWITHNAME, componentType.getTypename(), portIdentifier.getDisplayName()));
return null;
}
final Assignment assignment = componentBody.getLocalAssignmentById(portIdentifier);
if (assignment == null) {
return null;
}
if (!Assignment_type.A_PORT.semanticallyEquals(assignment.getAssignmentType())) {
portReference.getLocation().reportSemanticError(MessageFormat.format(DEFINITIONNOTPORT, portIdentifier.getDisplayName(), componentType.getTypename(), assignment.getAssignmentName()));
return null;
}
final ArrayDimensions dimensions = ((Def_Port) assignment).getDimensions();
if (dimensions != null) {
dimensions.checkIndices(timestamp, portReference, "port", false, Expected_Value_type.EXPECTED_DYNAMIC_VALUE);
} else if (portReference.getSubreferences().size() > 1) {
portReference.getLocation().reportSemanticError(MessageFormat.format("Port `{0}'' is not an array. The reference cannot have field or array sub-references", portIdentifier.getDisplayName()));
}
Port_Type portType = ((Def_Port) assignment).getType(timestamp);
if (portType != null) {
final PortTypeBody portBody = portType.getPortBody();
if (PortType_type.PT_USER.equals(portBody.getPortType())) {
final IType providerType = portBody.getProviderType();
if (providerType instanceof Port_Type) {
portType = (Port_Type) providerType;
}
}
}
return portType;
}
use of org.eclipse.titan.designer.AST.TTCN3.statements.Statement in project titan.EclipsePlug-ins by eclipse.
the class Port_Utility method checkFromClause.
/**
* Checks a from clause reference and a sender redirect to see if they
* really references valid component types.
*
* @param timestamp
* the timestamp of the actual semantic check cycle.
* @param source
* the source statement to report errors to.
* @param portType
* the type of the port used in the statement. Used to
* find the address type in effect.
* @param fromClause
* the from clause to check
* @param redirectSender
* the sender redirect to check.
*/
public static void checkFromClause(final CompilationTimeStamp timestamp, final Statement source, final Port_Type portType, final TemplateInstance fromClause, final Reference redirectSender) {
IType addressType = null;
if (portType != null) {
addressType = portType.getPortBody().getAddressType(timestamp);
} else if (source != null && source.getMyStatementBlock() != null) {
final Module module = source.getMyStatementBlock().getModuleScope();
if (module != null && module_type.TTCN3_MODULE.equals(module.getModuletype())) {
addressType = ((TTCN3Module) module).getAddressType(timestamp);
}
}
boolean senderRedirectChecked = false;
IType fromClauseType = null;
if (fromClause != null) {
fromClauseType = fromClause.getExpressionGovernor(timestamp, Expected_Value_type.EXPECTED_TEMPLATE);
ITTCN3Template templateBody = fromClause.getTemplateBody();
if (fromClauseType == null) {
if (addressType != null) {
templateBody = addressType.checkThisTemplateRef(timestamp, templateBody);
} else {
templateBody = templateBody.setLoweridToReference(timestamp);
}
fromClauseType = templateBody.getExpressionGovernor(timestamp, Expected_Value_type.EXPECTED_TEMPLATE);
}
if (fromClauseType == null) {
fromClauseType = checkSenderRedirect(timestamp, addressType, redirectSender);
senderRedirectChecked = true;
}
if (fromClauseType == null) {
// trying to figure out whether the template is
// a component reference or an SUT address
boolean isComponentReference;
if (Type_type.TYPE_COMPONENT.equals(templateBody.getExpressionReturntype(timestamp, Expected_Value_type.EXPECTED_TEMPLATE))) {
isComponentReference = true;
} else {
switch(templateBody.getTemplatetype()) {
case SPECIFIC_VALUE:
// treat 'null' as component
// reference
isComponentReference = Value_type.TTCN3_NULL_VALUE.equals(((SpecificValue_Template) templateBody).getSpecificValue().getValuetype());
break;
case ANY_VALUE:
case ANY_OR_OMIT:
isComponentReference = true;
break;
default:
isComponentReference = false;
break;
}
}
if (isComponentReference) {
// the argument is a component
// reference: get a pool type
fromClauseType = TypeFactory.createType(Type_type.TYPE_COMPONENT);
} else if (addressType != null) {
// the argument is not a component
// reference: try the address type
fromClauseType = addressType;
}
}
if (fromClauseType != null) {
fromClause.check(timestamp, fromClauseType);
if (addressType == null || !addressType.isCompatible(timestamp, fromClauseType, null, null, null)) {
// from_clause_type must be a component
// type
final IType last = fromClauseType.getTypeRefdLast(timestamp);
if (last.getIsErroneous(timestamp)) {
fromClauseType = null;
} else if (Type_type.TYPE_COMPONENT.equals(last.getTypetype())) {
if (Template_type.SPECIFIC_VALUE.equals(templateBody.getTemplatetype())) {
checkComponentReference(timestamp, source, ((SpecificValue_Template) templateBody).getSpecificValue(), true, true);
}
} else {
final String message = MessageFormat.format("The type of the template should be a component type {0} instead of `{1}''", (addressType == null) ? "" : "or the `address' type ", fromClauseType.getTypename());
fromClause.getLocation().reportSemanticError(message);
fromClauseType = null;
}
}
} else {
fromClause.getLocation().reportSemanticError("Cannot determine the type of the template");
}
}
if (!senderRedirectChecked) {
final IType senderRedirectType = checkSenderRedirect(timestamp, addressType, redirectSender);
if (fromClauseType != null && senderRedirectType != null && !fromClauseType.isIdentical(timestamp, senderRedirectType)) {
final String message = MessageFormat.format("The types in `from'' clause and `sender'' redirect are not the same: `{0}'' was expected instead of `{1}''", fromClauseType.getTypename(), senderRedirectType.getTypename());
senderRedirectType.getLocation().reportSemanticError(message);
}
}
}
use of org.eclipse.titan.designer.AST.TTCN3.statements.Statement in project titan.EclipsePlug-ins by eclipse.
the class Port_Utility method checkToClause.
/**
* Checks a to clause reference to see if it really references valid
* component type.
*
* @param timestamp
* the timestamp of the actual semantic check cycle.
* @param source
* the source statement of this check.
* @param portType
* the type of the port used in the statement. Used to
* find the address type in effect.
* @param toClause
* the to clause to check
*/
public static void checkToClause(final CompilationTimeStamp timestamp, final Statement source, final Port_Type portType, final IValue toClause) {
if (toClause == null) {
return;
}
IType addressType = null;
if (portType != null) {
addressType = portType.getPortBody().getAddressType(timestamp);
} else if (source != null) {
final Module module = source.getMyStatementBlock().getModuleScope();
if (module != null && module_type.TTCN3_MODULE.equals(module.getModuletype())) {
addressType = ((TTCN3Module) module).getAddressType(timestamp);
}
}
if (addressType == null) {
checkComponentReference(timestamp, source, toClause, true, true);
} else {
// detect possible enumerated values (address may be an
// enumerated type)
final IValue temp = addressType.checkThisValueRef(timestamp, toClause);
// try to figure out whether the argument is a component
// reference or an SUT address
boolean isAddress;
final IType governor = temp.getExpressionGovernor(timestamp, Expected_Value_type.EXPECTED_DYNAMIC_VALUE);
if (governor == null) {
isAddress = !Type_type.TYPE_COMPONENT.equals(temp.getExpressionReturntype(timestamp, Expected_Value_type.EXPECTED_DYNAMIC_VALUE));
} else {
isAddress = addressType.isCompatible(timestamp, governor, null, null, null);
}
if (isAddress) {
addressType.checkThisValue(timestamp, temp, null, new ValueCheckingOptions(Expected_Value_type.EXPECTED_DYNAMIC_VALUE, false, false, true, false, false));
} else {
checkComponentReference(timestamp, source, temp, true, true);
}
}
}
use of org.eclipse.titan.designer.AST.TTCN3.statements.Statement in project titan.EclipsePlug-ins by eclipse.
the class StatementBlock method updateSyntax.
@Override
public /**
* {@inheritDoc}
*/
void updateSyntax(final TTCN3ReparseUpdater reparser, final boolean isDamaged) throws ReParseException {
if (!isDamaged) {
// handle the simple case quickly
for (int i = 0, size = statements.size(); i < size; i++) {
final Statement statement = statements.get(i);
statement.updateSyntax(reparser, false);
reparser.updateLocation(statement.getLocation());
}
return;
}
returnStatus = null;
lastTimeChecked = null;
boolean enveloped = false;
int nofDamaged = 0;
int leftBoundary = location.getOffset();
int rightBoundary = location.getEndOffset();
final int damageOffset = reparser.getDamageStart();
IAppendableSyntax lastAppendableBeforeChange = null;
IAppendableSyntax lastPrependableBeforeChange = null;
for (int i = 0, size = statements.size(); i < size && !enveloped; i++) {
final Statement statement = statements.get(i);
final Location temporalLocation = statement.getLocation();
Location cumulativeLocation;
if (statement instanceof Definition_Statement) {
cumulativeLocation = ((Definition_Statement) statement).getDefinition().getCumulativeDefinitionLocation();
} else {
cumulativeLocation = temporalLocation;
}
if (temporalLocation.equals(cumulativeLocation) && reparser.envelopsDamage(cumulativeLocation)) {
enveloped = true;
leftBoundary = cumulativeLocation.getOffset();
rightBoundary = cumulativeLocation.getEndOffset();
} else if (reparser.isDamaged(cumulativeLocation)) {
nofDamaged++;
if (reparser.getDamageStart() == cumulativeLocation.getEndOffset()) {
lastAppendableBeforeChange = statement;
} else if (reparser.getDamageEnd() == cumulativeLocation.getOffset()) {
lastPrependableBeforeChange = statement;
}
} else {
if (cumulativeLocation.getEndOffset() < damageOffset && cumulativeLocation.getEndOffset() > leftBoundary) {
leftBoundary = cumulativeLocation.getEndOffset();
lastAppendableBeforeChange = statement;
}
if (cumulativeLocation.getOffset() >= damageOffset && cumulativeLocation.getOffset() < rightBoundary) {
rightBoundary = cumulativeLocation.getOffset();
lastPrependableBeforeChange = statement;
}
}
}
// was not enveloped
if (!enveloped) {
reparser.extendDamagedRegion(leftBoundary, rightBoundary);
}
// extension might be correct
if (lastAppendableBeforeChange != null) {
final boolean isBeingExtended = reparser.startsWithFollow(lastAppendableBeforeChange.getPossibleExtensionStarterTokens());
if (isBeingExtended) {
leftBoundary = lastAppendableBeforeChange.getLocation().getOffset();
nofDamaged++;
enveloped = false;
reparser.extendDamagedRegion(leftBoundary, rightBoundary);
}
}
if (lastPrependableBeforeChange != null) {
final List<Integer> temp = lastPrependableBeforeChange.getPossiblePrefixTokens();
if (temp != null && reparser.endsWithToken(temp)) {
rightBoundary = lastPrependableBeforeChange.getLocation().getEndOffset();
nofDamaged++;
enveloped = false;
reparser.extendDamagedRegion(leftBoundary, rightBoundary);
}
}
if (nofDamaged != 0) {
removeStuffInRange(reparser);
}
for (final Iterator<Statement> iterator = statements.iterator(); iterator.hasNext(); ) {
final Statement statement = iterator.next();
final Location temporalLocation = statement.getLocation();
Location cumulativeLocation;
if (statement instanceof Definition_Statement) {
cumulativeLocation = ((Definition_Statement) statement).getDefinition().getCumulativeDefinitionLocation();
} else {
cumulativeLocation = temporalLocation;
}
if (reparser.isAffectedAppended(cumulativeLocation)) {
try {
statement.updateSyntax(reparser, enveloped && reparser.envelopsDamage(cumulativeLocation));
reparser.updateLocation(statement.getLocation());
} catch (ReParseException e) {
if (e.getDepth() == 1) {
enveloped = false;
iterator.remove();
reparser.extendDamagedRegion(cumulativeLocation);
} else {
e.decreaseDepth();
throw e;
}
}
}
}
if (!enveloped) {
reparser.extendDamagedRegion(leftBoundary, rightBoundary);
final int result = reparse(reparser);
if (result > 1) {
throw new ReParseException(result - 1);
}
}
}
Aggregations