Search in sources :

Example 6 with TemplateRestriction

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

the class Def_Var_Template method generateCodeString.

@Override
public /**
 * {@inheritDoc}
 */
void generateCodeString(final JavaGenData aData, final StringBuilder source) {
    final String genName = getGenName();
    // FIXME temporal code until generate_code_object is supported for templates
    final String typeGeneratedName = type.getGenNameTemplate(aData, source, getMyScope());
    if (type.getTypetype().equals(Type_type.TYPE_ARRAY)) {
        final Array_Type arrayType = (Array_Type) type;
        final StringBuilder sb = aData.getCodeForType(arrayType.getGenNameOwn());
        arrayType.generateCodeValue(aData, sb);
        arrayType.generateCodeTemplate(aData, sb);
    }
    source.append(MessageFormat.format("{0} {1} = new {0}();\n", typeGeneratedName, genName));
    if (initialValue != null) {
        initialValue.generateCodeInit(aData, source, genName);
        if (templateRestriction != Restriction_type.TR_NONE && generateRestrictionCheck) {
            TemplateRestriction.generateRestrictionCheckCode(aData, source, location, genName, templateRestriction);
        }
    }
}
Also used : Array_Type(org.eclipse.titan.designer.AST.TTCN3.types.Array_Type)

Example 7 with TemplateRestriction

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

the class FormalParameter method checkActualParameterTemplate.

private ActualParameter checkActualParameterTemplate(final CompilationTimeStamp timestamp, final TemplateInstance actualParameter) {
    actualParameter.check(timestamp, type);
    final TemplateInstance instance = new TemplateInstance(actualParameter.getType(), actualParameter.getDerivedReference(), actualParameter.getTemplateBody());
    final ActualParameter returnValue = new Template_ActualParameter(instance);
    if (!Restriction_type.TR_NONE.equals(templateRestriction)) {
        instance.checkRestriction(timestamp, this);
    }
    return returnValue;
}
Also used : TemplateInstance(org.eclipse.titan.designer.AST.TTCN3.templates.TemplateInstance)

Example 8 with TemplateRestriction

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

the class FormalParameter method checkActualParameterByReference.

/**
 * Checks if the actual parameter paired with this formal parameter is
 * semantically correct as a reference parameter.
 *
 * @param timestamp
 *                the timestamp of the actual semantic check cycle.
 * @param parameter
 *                the template instance assigned as actual parameter to
 *                this formal parameter
 * @param isTemplate
 *                true if the formal parameter is template, false
 *                otherwise
 * @param expectedValue
 *                the value kind expected from the actual parameter.
 *
 * @return the actual parameter created from the value, or null if there
 *         was an error.
 */
private ActualParameter checkActualParameterByReference(final CompilationTimeStamp timestamp, final TemplateInstance parameter, final boolean isTemplate, final Expected_Value_type expectedValue) {
    final Type parameterType = parameter.getType();
    if (parameterType != null) {
        parameterType.getLocation().reportSemanticWarning(MessageFormat.format(EXPLICITESPECIFICATIONFORREFERENCE, getAssignmentName()));
        parameter.checkType(timestamp, type);
    }
    final Reference derivedReference = parameter.getDerivedReference();
    if (derivedReference != null) {
        derivedReference.getLocation().reportSemanticError(MessageFormat.format(INLINETEMPLATEFORREFERENCE, getAssignmentName()));
        parameter.checkDerivedReference(timestamp, type);
    }
    String expectedString;
    if (isTemplate) {
        expectedString = "template variable or template parameter";
    } else {
        expectedString = "variable or value parameter";
    }
    final ITTCN3Template template = parameter.getTemplateBody();
    if (Template_type.SPECIFIC_VALUE.equals(template.getTemplatetype()) && ((SpecificValue_Template) template).isReference()) {
        final Reference reference = ((SpecificValue_Template) template).getReference();
        reference.setUsedOnLeftHandSide();
        final Assignment assignment = reference.getRefdAssignment(timestamp, true);
        if (assignment == null) {
            final ActualParameter temp = new Value_ActualParameter(null);
            temp.setIsErroneous();
            return temp;
        }
        boolean assignmentTypeIsCorrect;
        switch(assignment.getAssignmentType()) {
            case A_PAR_VAL_IN:
                ((FormalParameter) assignment).useAsLValue(reference);
                assignmentTypeIsCorrect = !isTemplate;
                break;
            case A_PAR_VAL:
            case A_PAR_VAL_OUT:
            case A_PAR_VAL_INOUT:
                ((FormalParameter) assignment).setWritten();
                assignmentTypeIsCorrect = !isTemplate;
                break;
            case A_VAR:
                ((Def_Var) assignment).setWritten();
                assignmentTypeIsCorrect = !isTemplate;
                break;
            case A_PAR_TEMP_IN:
                assignmentTypeIsCorrect = isTemplate;
                ((FormalParameter) assignment).useAsLValue(reference);
                break;
            case A_PAR_TEMP_OUT:
            case A_PAR_TEMP_INOUT:
                ((FormalParameter) assignment).setWritten();
                assignmentTypeIsCorrect = isTemplate;
                break;
            case A_VAR_TEMPLATE:
                ((Def_Var_Template) assignment).setWritten();
                assignmentTypeIsCorrect = isTemplate;
                break;
            default:
                assignmentTypeIsCorrect = false;
                break;
        }
        if (assignmentTypeIsCorrect) {
            final IType fieldType = assignment.getType(timestamp).getFieldType(timestamp, reference, 1, Expected_Value_type.EXPECTED_DYNAMIC_VALUE, false);
            if (fieldType != null) {
                if (type != null && !type.isCompatible(timestamp, fieldType, null, null, null)) {
                    reference.getLocation().reportSemanticError(MessageFormat.format(TYPEMISMATCH2, expectedString, type.getTypename(), fieldType.getTypename()));
                } else if (type != null && type.getSubtype() != null && fieldType.getSubtype() != null && !type.getSubtype().isCompatible(timestamp, fieldType.getSubtype())) {
                    reference.getLocation().reportSemanticError(MessageFormat.format(SUBTYPEMISMATCH, type.getSubtype().toString(), fieldType.getSubtype().toString()));
                }
                if (!reference.getSubreferences().isEmpty() && reference.refersToStringElement()) {
                    reference.getLocation().reportSemanticError(MessageFormat.format(REFERENCEEXPECTED3, fieldType.getTypename()));
                }
            }
        } else {
            reference.getLocation().reportSemanticError(MessageFormat.format(REFERENCEEXPECTED1, expectedString, getAssignmentName(), assignment.getDescription()));
        }
        final ActualParameter returnActualParameter = new Referenced_ActualParameter(reference);
        if (isTemplate && assignmentTypeIsCorrect) {
            TemplateRestriction.Restriction_type refdRestriction;
            switch(assignment.getAssignmentType()) {
                case A_VAR_TEMPLATE:
                    {
                        final Def_Var_Template temp = (Def_Var_Template) assignment;
                        refdRestriction = temp.getTemplateRestriction();
                        break;
                    }
                case A_PAR_TEMP_IN:
                case A_PAR_TEMP_OUT:
                case A_PAR_TEMP_INOUT:
                    {
                        final FormalParameter par = (FormalParameter) assignment;
                        refdRestriction = par.getTemplateRestriction();
                        break;
                    }
                default:
                    return returnActualParameter;
            }
            TemplateRestriction.getSubRestriction(refdRestriction, timestamp, reference);
            if (templateRestriction != refdRestriction) {
                final boolean preCallCheck = TemplateRestriction.isLessRestrictive(templateRestriction, refdRestriction);
                final boolean postCallCheck = TemplateRestriction.isLessRestrictive(refdRestriction, templateRestriction);
                if (preCallCheck || postCallCheck) {
                    final String message = MessageFormat.format("Inadequate restriction on the referenced {0} `{1}'' this may cause a dynamic test case error at runtime", assignment.getAssignmentName(), reference.getDisplayName());
                    reference.getLocation().reportSemanticWarning(message);
                }
            }
            // written C++ code
            if (!Restriction_type.TR_NONE.equals(refdRestriction)) {
                switch(myParameterList.getMyDefinition().getAssignmentType()) {
                    case A_EXT_FUNCTION:
                    case A_EXT_FUNCTION_RVAL:
                    case A_EXT_FUNCTION_RTEMP:
                        // here
                        break;
                    default:
                        break;
                }
            }
        }
        return returnActualParameter;
    }
    parameter.getLocation().reportSemanticError(MessageFormat.format(REFERENCEEXPECTED2, expectedString, getAssignmentName()));
    final ActualParameter temp = new Value_ActualParameter(null);
    temp.setIsErroneous();
    return temp;
}
Also used : ITTCN3Template(org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template) Restriction_type(org.eclipse.titan.designer.AST.TTCN3.TemplateRestriction.Restriction_type) Reference(org.eclipse.titan.designer.AST.Reference) ISubReference(org.eclipse.titan.designer.AST.ISubReference) IType(org.eclipse.titan.designer.AST.IType) Assignment(org.eclipse.titan.designer.AST.Assignment) CodeSectionType(org.eclipse.titan.designer.AST.GovernedSimple.CodeSectionType) Type(org.eclipse.titan.designer.AST.Type) IType(org.eclipse.titan.designer.AST.IType) SpecificValue_Template(org.eclipse.titan.designer.AST.TTCN3.templates.SpecificValue_Template) TemplateRestriction(org.eclipse.titan.designer.AST.TTCN3.TemplateRestriction)

Example 9 with TemplateRestriction

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

the class Def_Template method generateCodeString.

@Override
public /**
 * {@inheritDoc}
 */
void generateCodeString(final JavaGenData aData, final StringBuilder source) {
    final String genName = getGenName();
    if (formalParList == null) {
        final String typeName = type.getGenNameTemplate(aData, source, getMyScope());
        if (baseTemplate == null) {
            if (type.getTypetype().equals(Type_type.TYPE_ARRAY)) {
                final Array_Type arrayType = (Array_Type) type;
                final StringBuilder sb = aData.getCodeForType(arrayType.getGenNameOwn());
                arrayType.generateCodeValue(aData, sb);
                arrayType.generateCodeTemplate(aData, sb);
            }
            source.append(MessageFormat.format("{0} {1} = new {0}();\n", typeName, genName));
            if (body != null) {
                // TODO can optimize for single expressions;
                body.generateCodeInit(aData, source, genName);
            }
        } else {
            source.append(MessageFormat.format("{0} {1} = new {0}({2});\n", typeName, genName, baseTemplate.getGenNameFromScope(aData, source, myScope, "")));
            if (body != null) {
                body.generateCodeInit(aData, source, genName);
            }
        }
    } else {
        source.append(MessageFormat.format("Code generation for parameterized local template `{0}' is not supported", identifier.getDisplayName()));
    }
    if (templateRestriction != Restriction_type.TR_NONE && generateRestrictionCheck) {
        TemplateRestriction.generateRestrictionCheckCode(aData, source, location, genName, templateRestriction);
    }
}
Also used : Array_Type(org.eclipse.titan.designer.AST.TTCN3.types.Array_Type)

Example 10 with TemplateRestriction

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

the class Def_Template method generateCode.

@Override
public /**
 * {@inheritDoc}
 */
void generateCode(final JavaGenData aData, final boolean cleanUp) {
    final String genName = getGenName();
    final StringBuilder sb = aData.getSrc();
    final StringBuilder source = new StringBuilder();
    if (!isLocal()) {
        if (VisibilityModifier.Private.equals(getVisibilityModifier())) {
            source.append("private");
        } else {
            source.append("public");
        }
        source.append(" static ");
    }
    final String typeName = type.getGenNameTemplate(aData, source, getMyScope());
    if (formalParList == null) {
        if (type.getTypetype().equals(Type_type.TYPE_ARRAY)) {
            final Array_Type arrayType = (Array_Type) type;
            final StringBuilder sbforTemp = aData.getCodeForType(arrayType.getGenNameOwn());
            arrayType.generateCodeValue(aData, sbforTemp);
            arrayType.generateCodeTemplate(aData, sbforTemp);
        }
        source.append(MessageFormat.format("{0} {1} = new {0}();\n", typeName, genName));
        if (baseTemplate != null) {
            // modified template
            if (baseTemplate.myScope.getModuleScope() == myScope.getModuleScope()) {
                // if the base template is in the same module its body has to be
                // initialized first
                getLocation().update_location_object(aData, aData.getPostInit());
                baseTemplate.body.generateCodeInit(aData, aData.getPostInit(), body.get_lhs_name());
            }
        }
        if (body != null) {
            getLocation().update_location_object(aData, aData.getPostInit());
            body.generateCodeInit(aData, aData.getPostInit(), body.get_lhs_name());
            if (templateRestriction != Restriction_type.TR_NONE && generateRestrictionCheck) {
                TemplateRestriction.generateRestrictionCheckCode(aData, source, location, genName, templateRestriction);
            }
        }
    } else {
        final StringBuilder formalParameters = formalParList.generateCode(aData);
        source.append(MessageFormat.format("{0} {1}({2}) '{'\n", typeName, genName, formalParameters));
        getLocation().create_location_object(aData, source, getIdentifier().getDisplayName());
        if (baseTemplate == null) {
            if (type.getTypetype().equals(Type_type.TYPE_ARRAY)) {
                final Array_Type arrayType = (Array_Type) type;
                final StringBuilder sbforTemp = aData.getCodeForType(arrayType.getGenNameOwn());
                arrayType.generateCodeValue(aData, sbforTemp);
                arrayType.generateCodeTemplate(aData, sbforTemp);
            }
            source.append(MessageFormat.format("{0} ret_val = new {0}();\n", typeName));
        } else {
            // modified template
            source.append(MessageFormat.format("{0} ret_val = new {0}({1}", typeName, baseTemplate.getGenNameFromScope(aData, source, myScope, "")));
            if (baseTemplate.formalParList != null) {
                // the base is also parameterized
                source.append('(');
                for (int i = 0; i < baseTemplate.formalParList.getNofParameters(); i++) {
                    if (i > 0) {
                        source.append(", ");
                    }
                    source.append(formalParList.getParameterByIndex(i).getIdentifier().getName());
                }
                source.append(')');
            }
            source.append(");\n");
        }
        if (body != null) {
            body.generateCodeInit(aData, source, "ret_val");
            if (templateRestriction != Restriction_type.TR_NONE && generateRestrictionCheck) {
                TemplateRestriction.generateRestrictionCheckCode(aData, source, location, "ret_val", templateRestriction);
            }
        }
        getLocation().release_location_object(aData, source);
        source.append("return ret_val;\n");
        source.append("}\n\n");
    }
    sb.append(source);
}
Also used : Array_Type(org.eclipse.titan.designer.AST.TTCN3.types.Array_Type)

Aggregations

IType (org.eclipse.titan.designer.AST.IType)6 Array_Type (org.eclipse.titan.designer.AST.TTCN3.types.Array_Type)6 Assignment (org.eclipse.titan.designer.AST.Assignment)5 IValue (org.eclipse.titan.designer.AST.IValue)3 Restriction_type (org.eclipse.titan.designer.AST.TTCN3.TemplateRestriction.Restriction_type)3 ISubReference (org.eclipse.titan.designer.AST.ISubReference)2 Reference (org.eclipse.titan.designer.AST.Reference)2 TemplateRestriction (org.eclipse.titan.designer.AST.TTCN3.TemplateRestriction)2 ITTCN3Template (org.eclipse.titan.designer.AST.TTCN3.templates.ITTCN3Template)2 ExpressionStruct (org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)2 CodeSectionType (org.eclipse.titan.designer.AST.GovernedSimple.CodeSectionType)1 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)1 Scope (org.eclipse.titan.designer.AST.Scope)1 Def_Function (org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Function)1 Def_Var (org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Var)1 Def_Var_Template (org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Var_Template)1 Definition (org.eclipse.titan.designer.AST.TTCN3.definitions.Definition)1 FormalParameter (org.eclipse.titan.designer.AST.TTCN3.definitions.FormalParameter)1 RunsOnScope (org.eclipse.titan.designer.AST.TTCN3.definitions.RunsOnScope)1 SpecificValue_Template (org.eclipse.titan.designer.AST.TTCN3.templates.SpecificValue_Template)1