Search in sources :

Example 21 with Array_Type

use of org.eclipse.titan.designer.AST.TTCN3.types.Array_Type 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)

Example 22 with Array_Type

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

the class Def_Const method generateCode.

@Override
public /**
 * {@inheritDoc}
 */
void generateCode(final JavaGenData aData, final boolean cleanUp) {
    final String genName = getGenName();
    if (type == null || value == null) {
        return;
    }
    final IReferenceChain referenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
    final IValue last = value.getValueRefdLast(CompilationTimeStamp.getBaseTimestamp(), referenceChain);
    referenceChain.release();
    final StringBuilder sb = aData.getSrc();
    final StringBuilder source = new StringBuilder();
    if (!isLocal()) {
        if (VisibilityModifier.Private.equals(getVisibilityModifier())) {
            source.append("private");
        } else {
            source.append("public");
        }
    }
    final String typeGeneratedName = type.getGenNameValue(aData, source, getMyScope());
    if (type.getTypetype().equals(Type_type.TYPE_ARRAY)) {
        final Array_Type arrayType = (Array_Type) type;
        final StringBuilder temp_sb = aData.getCodeForType(arrayType.getGenNameOwn());
        arrayType.generateCodeValue(aData, temp_sb);
    }
    source.append(MessageFormat.format(" static final {0} {1} = new {0}();\n", typeGeneratedName, genName));
    getLocation().update_location_object(aData, aData.getPreInit());
    last.generateCodeInit(aData, aData.getPreInit(), genName);
    sb.append(source);
}
Also used : IValue(org.eclipse.titan.designer.AST.IValue) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain) Array_Type(org.eclipse.titan.designer.AST.TTCN3.types.Array_Type)

Example 23 with Array_Type

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

the class Def_Const method generateCodeString.

@Override
public /**
 * {@inheritDoc}
 */
void generateCodeString(final JavaGenData aData, final StringBuilder source) {
    final String genName = getGenName();
    if (type == null || value == null) {
        return;
    }
    final IReferenceChain referenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
    final IValue last = value.getValueRefdLast(CompilationTimeStamp.getBaseTimestamp(), referenceChain);
    referenceChain.release();
    final String typeGeneratedName = type.getGenNameValue(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);
    }
    if (last.canGenerateSingleExpression()) {
        if (last.returnsNative() || type.getTypetypeTtcn3() != last.getExpressionReturntype(CompilationTimeStamp.getBaseTimestamp(), Expected_Value_type.EXPECTED_TEMPLATE)) {
            source.append(MessageFormat.format("{0} {1} = new {0}({2});\n", typeGeneratedName, genName, last.generateSingleExpression(aData)));
        } else {
            source.append(MessageFormat.format("{0} {1} = {2};\n", typeGeneratedName, genName, last.generateSingleExpression(aData)));
        }
    } else {
        source.append(MessageFormat.format("{0} {1} = new {0}();\n", typeGeneratedName, genName));
        last.generateCodeInit(aData, source, genName);
    }
}
Also used : IValue(org.eclipse.titan.designer.AST.IValue) IReferenceChain(org.eclipse.titan.designer.AST.IReferenceChain) Array_Type(org.eclipse.titan.designer.AST.TTCN3.types.Array_Type)

Example 24 with Array_Type

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

the class Type method checkThisVariant.

@Override
public /**
 * {@inheritDoc}
 */
void checkThisVariant(final CompilationTimeStamp timestamp, final SingleWithAttribute singleWithAttribute, final boolean global) {
    final IType type = getTypeWithCodingTable(timestamp, false);
    if (type == null) {
    // FIXME as only RAW is supported for now, we can not report this error
    // if (!global) {
    // singleWithAttribute.getLocation().reportSemanticError(MessageFormat.format("No encoding rules defined for type `{0}''", getTypename()));
    // }
    } else {
        final List<String> codingStrings = singleWithAttribute.getAttributeSpecification().getEncodings();
        // gather the built-in codecs referred to by the variant's encoding strings
        final ArrayList<MessageEncoding_type> codings = new ArrayList<IType.MessageEncoding_type>();
        boolean erroneous = false;
        if (codingStrings == null) {
            if (type.getCodingTable().size() > 1) {
                if (!global) {
                    singleWithAttribute.getLocation().reportSemanticError(MessageFormat.format("The encoding reference is mandatory for variant attributes of type  `{0}''", getTypename()));
                }
                erroneous = true;
            } else if (type.getCodingTable().get(0).builtIn) {
                codings.add(type.getCodingTable().get(0).builtInCoding);
            } else {
                // PER or custom encoding
                final MessageEncoding_type coding = "PER".equals(type.getCodingTable().get(0).customCoding.name) ? MessageEncoding_type.PER : MessageEncoding_type.CUSTOM;
                singleWithAttribute.getLocation().reportSemanticWarning(MessageFormat.format("Variant attributes related to `{0}'' encoding are ignored", coding.getEncodingName()));
            }
        } else {
            for (int i = 0; i < codingStrings.size(); i++) {
                final String encodingString = codingStrings.get(i);
                final MessageEncoding_type coding = getEncodingType(encodingString);
                if (!hasEncoding(timestamp, coding, encodingString)) {
                    erroneous = true;
                    // FIXME RAW restriction only exists because that is the only supported encoding right now
                    if (!global && coding == MessageEncoding_type.RAW) {
                        if (coding == MessageEncoding_type.CUSTOM) {
                            singleWithAttribute.getLocation().reportSemanticError(MessageFormat.format("Type `{0}'' does not support {1} encoding", getTypename(), coding.getEncodingName()));
                        } else {
                            singleWithAttribute.getLocation().reportSemanticError(MessageFormat.format("Type `{0}'' does not support custom encoding `{1}''", getTypename(), encodingString));
                        }
                    }
                } else if (coding != MessageEncoding_type.PER && coding != MessageEncoding_type.CUSTOM) {
                    codings.add(coding);
                } else {
                    // PER or custom encoding
                    singleWithAttribute.getLocation().reportSemanticWarning(MessageFormat.format("Variant attributes related to {0} encoding are ignored", coding.getEncodingName()));
                }
            }
        }
        // FIXME implement checks
        // TODO only raw data is extracted
        final VariantAttributeAnalyzer analyzer = new VariantAttributeAnalyzer();
        boolean newRaw = false;
        final AtomicBoolean rawFoud = new AtomicBoolean(false);
        if (rawAttribute == null) {
            IType t_refd = this;
            while (t_refd.getRawAttribute() == null && t_refd instanceof Referenced_Type) {
                final IReferenceChain referenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
                t_refd = ((Referenced_Type) t_refd).getTypeRefd(timestamp, referenceChain);
                referenceChain.release();
            }
            rawAttribute = new RawAST(t_refd.getRawAttribute(), getDefaultRawFieldLength());
            newRaw = true;
        }
        analyzer.parse(rawAttribute, singleWithAttribute.getAttributeSpecification(), getLengthMultiplier(), rawFoud);
        if (!rawFoud.get() && newRaw) {
            rawAttribute = null;
        }
    }
    if (global) {
        // send global variant attributes to field/element types
        switch(getTypetype()) {
            case TYPE_TTCN3_CHOICE:
            case TYPE_TTCN3_SEQUENCE:
            case TYPE_TTCN3_SET:
                for (int i = 0; i < ((TTCN3_Set_Seq_Choice_BaseType) this).getNofComponents(); i++) {
                    ((TTCN3_Set_Seq_Choice_BaseType) this).getComponentByIndex(i).getType().checkThisVariant(timestamp, singleWithAttribute, global);
                }
                break;
            case TYPE_ASN1_CHOICE:
            case TYPE_ASN1_SEQUENCE:
            case TYPE_ASN1_SET:
                for (int i = 0; i < ((ASN1_Set_Seq_Choice_BaseType) this).getNofComponents(timestamp); i++) {
                    ((ASN1_Set_Seq_Choice_BaseType) this).getComponentByIndex(i).getType().checkThisVariant(timestamp, singleWithAttribute, global);
                }
                break;
            case TYPE_ANYTYPE:
                for (int i = 0; i < ((Anytype_Type) this).getNofComponents(); i++) {
                    ((Anytype_Type) this).getComponentByIndex(i).getType().checkThisVariant(timestamp, singleWithAttribute, global);
                }
                break;
            case TYPE_OPENTYPE:
                for (int i = 0; i < ((Open_Type) this).getNofComponents(); i++) {
                    ((Open_Type) this).getComponentByIndex(i).getType().checkThisVariant(timestamp, singleWithAttribute, global);
                }
                break;
            case TYPE_ARRAY:
                ((Array_Type) this).getElementType().checkThisVariant(timestamp, singleWithAttribute, global);
                break;
            case TYPE_SEQUENCE_OF:
                ((SequenceOf_Type) this).getOfType().checkThisVariant(timestamp, singleWithAttribute, global);
                break;
            case TYPE_SET_OF:
                ((SetOf_Type) this).getOfType().checkThisVariant(timestamp, singleWithAttribute, global);
                break;
            default:
                break;
        }
    }
}
Also used : VariantAttributeAnalyzer(org.eclipse.titan.designer.parsers.variantattributeparser.VariantAttributeAnalyzer) Open_Type(org.eclipse.titan.designer.AST.ASN1.types.Open_Type) RawAST(org.eclipse.titan.designer.AST.TTCN3.attributes.RawAST) ASN1_Set_Seq_Choice_BaseType(org.eclipse.titan.designer.AST.ASN1.types.ASN1_Set_Seq_Choice_BaseType) ArrayList(java.util.ArrayList) Anytype_Type(org.eclipse.titan.designer.AST.TTCN3.types.Anytype_Type) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TTCN3_Set_Seq_Choice_BaseType(org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Set_Seq_Choice_BaseType) Referenced_Type(org.eclipse.titan.designer.AST.TTCN3.types.Referenced_Type)

Example 25 with Array_Type

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

the class AbstractOfType method isSubtypeCompatible.

/**
 * Checks that the provided type is sub-type compatible with the actual
 * set of type.
 * <p>
 * In case of sequence/set/array this means that the number of their
 * fields fulfills the length restriction of the set of type.
 *
 * @param timestamp
 *                the timestamp of the actual semantic check cycle
 * @param other
 *                the type to check against.
 *
 * @return true if they are sub-type compatible, false otherwise.
 */
public boolean isSubtypeCompatible(final CompilationTimeStamp timestamp, final IType other) {
    if (subType == null || other == null) {
        return true;
    }
    long nofComponents;
    switch(other.getTypetype()) {
        case TYPE_ASN1_SEQUENCE:
            nofComponents = ((ASN1_Sequence_Type) other).getNofComponents(timestamp);
            break;
        case TYPE_TTCN3_SEQUENCE:
            nofComponents = ((TTCN3_Sequence_Type) other).getNofComponents();
            break;
        case TYPE_ASN1_SET:
            nofComponents = ((ASN1_Set_Type) other).getNofComponents(timestamp);
            break;
        case TYPE_TTCN3_SET:
            nofComponents = ((TTCN3_Set_Type) other).getNofComponents();
            break;
        case TYPE_SEQUENCE_OF:
        case TYPE_SET_OF:
            if (other.getSubtype() == null) {
                return true;
            }
            return subType.isCompatible(timestamp, other.getSubtype());
        case TYPE_ARRAY:
            {
                final ArrayDimension dimension = ((Array_Type) other).getDimension();
                if (dimension.getIsErroneous(timestamp)) {
                    return false;
                }
                nofComponents = dimension.getSize();
                break;
            }
        default:
            return false;
    }
    final List<ParsedSubType> tempRestrictions = new ArrayList<ParsedSubType>(1);
    final Integer_Value length = new Integer_Value(nofComponents);
    tempRestrictions.add(new Length_ParsedSubType(new SingleLenghtRestriction(length)));
    final SubType tempSubtype = new SubType(getSubtypeType(), this, tempRestrictions, null);
    tempSubtype.check(timestamp);
    return subType.isCompatible(timestamp, tempSubtype);
}
Also used : ParsedSubType(org.eclipse.titan.designer.AST.TTCN3.types.subtypes.ParsedSubType) Length_ParsedSubType(org.eclipse.titan.designer.AST.TTCN3.types.subtypes.Length_ParsedSubType) SubType(org.eclipse.titan.designer.AST.TTCN3.types.subtypes.SubType) ArrayList(java.util.ArrayList) Integer_Value(org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value) Length_ParsedSubType(org.eclipse.titan.designer.AST.TTCN3.types.subtypes.Length_ParsedSubType) ParsedSubType(org.eclipse.titan.designer.AST.TTCN3.types.subtypes.ParsedSubType) Length_ParsedSubType(org.eclipse.titan.designer.AST.TTCN3.types.subtypes.Length_ParsedSubType) ArrayDimension(org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension) SingleLenghtRestriction(org.eclipse.titan.designer.AST.TTCN3.templates.SingleLenghtRestriction)

Aggregations

Array_Type (org.eclipse.titan.designer.AST.TTCN3.types.Array_Type)19 IType (org.eclipse.titan.designer.AST.IType)13 IValue (org.eclipse.titan.designer.AST.IValue)10 Assignment (org.eclipse.titan.designer.AST.Assignment)6 IReferenceChain (org.eclipse.titan.designer.AST.IReferenceChain)6 ArrayDimension (org.eclipse.titan.designer.AST.TTCN3.values.ArrayDimension)6 Integer_Value (org.eclipse.titan.designer.AST.TTCN3.values.Integer_Value)6 Reference (org.eclipse.titan.designer.AST.Reference)5 SequenceOf_Type (org.eclipse.titan.designer.AST.TTCN3.types.SequenceOf_Type)5 ArrayList (java.util.ArrayList)4 ISubReference (org.eclipse.titan.designer.AST.ISubReference)4 SetOf_Type (org.eclipse.titan.designer.AST.TTCN3.types.SetOf_Type)4 ExpressionStruct (org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)4 ArraySubReference (org.eclipse.titan.designer.AST.ArraySubReference)3 CompField (org.eclipse.titan.designer.AST.TTCN3.types.CompField)3 ASN1_Choice_Type (org.eclipse.titan.designer.AST.ASN1.types.ASN1_Choice_Type)2 ASN1_Sequence_Type (org.eclipse.titan.designer.AST.ASN1.types.ASN1_Sequence_Type)2 ASN1_Set_Type (org.eclipse.titan.designer.AST.ASN1.types.ASN1_Set_Type)2 FieldSubReference (org.eclipse.titan.designer.AST.FieldSubReference)2 Identifier (org.eclipse.titan.designer.AST.Identifier)2