Search in sources :

Example 51 with ISubReference

use of org.eclipse.titan.designer.AST.ISubReference 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(')');
        }
    }
}
Also used : FieldSubReference(org.eclipse.titan.designer.AST.FieldSubReference) Reference(org.eclipse.titan.designer.AST.Reference) ISubReference(org.eclipse.titan.designer.AST.ISubReference) FieldSubReference(org.eclipse.titan.designer.AST.FieldSubReference) ArraySubReference(org.eclipse.titan.designer.AST.ArraySubReference) ParameterisedSubReference(org.eclipse.titan.designer.AST.ParameterisedSubReference) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) Stack(java.util.Stack) ArraySubReference(org.eclipse.titan.designer.AST.ArraySubReference) Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) Identifier(org.eclipse.titan.designer.AST.Identifier) IValue(org.eclipse.titan.designer.AST.IValue) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain)

Example 52 with ISubReference

use of org.eclipse.titan.designer.AST.ISubReference 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;
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) Def_Var_Template(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Var_Template)

Example 53 with ISubReference

use of org.eclipse.titan.designer.AST.ISubReference 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);
    }
}
Also used : FieldSubReference(org.eclipse.titan.designer.AST.FieldSubReference) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) ArraySubReference(org.eclipse.titan.designer.AST.ArraySubReference) Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) ParameterisedSubReference(org.eclipse.titan.designer.AST.ParameterisedSubReference) FormalParameterList(org.eclipse.titan.designer.AST.TTCN3.definitions.FormalParameterList) Def_Template(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Template) Identifier(org.eclipse.titan.designer.AST.Identifier) IValue(org.eclipse.titan.designer.AST.IValue) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain) ActualParameterList(org.eclipse.titan.designer.AST.TTCN3.definitions.ActualParameterList)

Example 54 with ISubReference

use of org.eclipse.titan.designer.AST.ISubReference 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;
}
Also used : ComponentTypeBody(org.eclipse.titan.designer.AST.TTCN3.types.ComponentTypeBody) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) IType(org.eclipse.titan.designer.AST.IType) ArraySubReference(org.eclipse.titan.designer.AST.ArraySubReference) Def_Port(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Port) Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) IValue(org.eclipse.titan.designer.AST.IValue) Identifier(org.eclipse.titan.designer.AST.Identifier) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain) Value(org.eclipse.titan.designer.AST.Value) Expression_Value(org.eclipse.titan.designer.AST.TTCN3.values.Expression_Value) IValue(org.eclipse.titan.designer.AST.IValue) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) PortType_type(org.eclipse.titan.designer.AST.TTCN3.types.PortTypeBody.PortType_type) Type_type(org.eclipse.titan.designer.AST.IType.Type_type) ArrayDimensions(org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimensions) Port_Type(org.eclipse.titan.designer.AST.TTCN3.types.Port_Type) Component_Type(org.eclipse.titan.designer.AST.TTCN3.types.Component_Type) PortTypeBody(org.eclipse.titan.designer.AST.TTCN3.types.PortTypeBody)

Example 55 with ISubReference

use of org.eclipse.titan.designer.AST.ISubReference in project titan.EclipsePlug-ins by eclipse.

the class Start_Component_Statement method generateCode.

@Override
public /**
 * {@inheritDoc}
 */
void generateCode(final JavaGenData aData, final StringBuilder source) {
    final ExpressionStruct expression = new ExpressionStruct();
    final Assignment func = functionInstanceReference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
    final Def_Function function = (Def_Function) func;
    expression.expression.append(MessageFormat.format("{0}(", func.getGenNameFromScope(aData, source, myScope, "start_")));
    componentReference.generateCodeExpression(aData, expression, false);
    FormalParameterList formalParameterList = function.getFormalParameterList();
    if (formalParameterList.getNofParameters() > 0) {
        expression.expression.append(',');
        ISubReference subReference = functionInstanceReference.getSubreferences().get(0);
        ((ParameterisedSubReference) subReference).getActualParameters().generateCodeNoAlias(aData, expression);
    }
    expression.expression.append(')');
    expression.mergeExpression(source);
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) FormalParameterList(org.eclipse.titan.designer.AST.TTCN3.definitions.FormalParameterList) ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct) Def_Function(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Function)

Aggregations

ISubReference (org.eclipse.titan.designer.AST.ISubReference)205 IType (org.eclipse.titan.designer.AST.IType)80 IValue (org.eclipse.titan.designer.AST.IValue)63 Identifier (org.eclipse.titan.designer.AST.Identifier)53 Value (org.eclipse.titan.designer.AST.Value)47 ArraySubReference (org.eclipse.titan.designer.AST.ArraySubReference)46 FieldSubReference (org.eclipse.titan.designer.AST.FieldSubReference)40 Assignment (org.eclipse.titan.designer.AST.Assignment)28 CompField (org.eclipse.titan.designer.AST.TTCN3.types.CompField)25 Reference (org.eclipse.titan.designer.AST.Reference)18 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)15 ArrayList (java.util.ArrayList)14 Template (org.eclipse.jface.text.templates.Template)11 ParameterisedSubReference (org.eclipse.titan.designer.AST.ParameterisedSubReference)11 Integer_Value (org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value)11 Expected_Value_type (org.eclipse.titan.designer.AST.TTCN3.Expected_Value_type)10 BridgingNamedNode (org.eclipse.titan.designer.AST.BridgingNamedNode)8 Module (org.eclipse.titan.designer.AST.Module)8 ITTCN3Template (org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template)7 ActualParameterList (org.eclipse.titan.designer.AST.TTCN3.definitions.ActualParameterList)6