Search in sources :

Example 41 with Assignment

use of org.eclipse.titan.designer.AST.Assignment 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 42 with Assignment

use of org.eclipse.titan.designer.AST.Assignment 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 43 with Assignment

use of org.eclipse.titan.designer.AST.Assignment 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 44 with Assignment

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

the class Referenced_Template method getTemplateReferencedLast.

@Override
public /**
 * {@inheritDoc}
 */
TTCN3Template getTemplateReferencedLast(final CompilationTimeStamp timestamp, final IReferenceChain referenceChain) {
    if (reference == null) {
        setIsErroneous(true);
        return this;
    }
    final boolean newChain = null == referenceChain;
    IReferenceChain tempReferenceChain;
    if (newChain) {
        tempReferenceChain = ReferenceChain.getInstance(CIRCULARTEMPLATEREFERENCE, true);
    } else {
        tempReferenceChain = referenceChain;
    }
    TTCN3Template template = this;
    final Assignment ass = reference.getRefdAssignment(timestamp, true);
    if (ass != null) {
        switch(ass.getAssignmentType()) {
            case A_TEMPLATE:
                tempReferenceChain.markState();
                if (tempReferenceChain.add(this)) {
                    final ITTCN3Template refd = getTemplateReferenced(timestamp, tempReferenceChain);
                    if (refd != this) {
                        template = refd.getTemplateReferencedLast(timestamp, referenceChain);
                    }
                } else {
                    setIsErroneous(true);
                }
                tempReferenceChain.previousState();
                break;
            case A_VAR_TEMPLATE:
            case A_FUNCTION_RTEMP:
            case A_MODULEPAR_TEMPLATE:
            case A_PAR_TEMP_IN:
            case A_PAR_TEMP_OUT:
            case A_PAR_TEMP_INOUT:
                return this;
            default:
                setIsErroneous(true);
        }
    } else {
        setIsErroneous(true);
    }
    if (newChain) {
        tempReferenceChain.release();
    }
    return template;
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain)

Example 45 with Assignment

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

Aggregations

Assignment (org.eclipse.titan.designer.AST.Assignment)141 Reference (org.eclipse.titan.designer.AST.Reference)48 IType (org.eclipse.titan.designer.AST.IType)41 ISubReference (org.eclipse.titan.designer.AST.ISubReference)39 IValue (org.eclipse.titan.designer.AST.IValue)31 Identifier (org.eclipse.titan.designer.AST.Identifier)25 ArrayList (java.util.ArrayList)24 Module (org.eclipse.titan.designer.AST.Module)22 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)16 FieldSubReference (org.eclipse.titan.designer.AST.FieldSubReference)14 Referenced_Value (org.eclipse.titan.designer.AST.TTCN3.values.Referenced_Value)14 ExpressionStruct (org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)14 Assignments (org.eclipse.titan.designer.AST.Assignments)13 ParameterisedSubReference (org.eclipse.titan.designer.AST.ParameterisedSubReference)13 ITTCN3Template (org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template)10 Location (org.eclipse.titan.designer.AST.Location)9 ArrayDimensions (org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimensions)8 Type_type (org.eclipse.titan.designer.AST.IType.Type_type)7 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)7 SpecificValue_Template (org.eclipse.titan.designer.AST.TTCN3.templates.SpecificValue_Template)6