Search in sources :

Example 1 with Scope

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

the class DecvalueUnicharExpression method generateCodeExpressionExpression.

@Override
public /**
 * {@inheritDoc}
 */
void generateCodeExpressionExpression(final JavaGenData aData, final ExpressionStruct expression) {
    aData.addBuiltinTypeImport("TitanOctetString");
    aData.addCommonLibraryImport("AdditionalFunctions");
    final ExpressionStruct expression1 = new ExpressionStruct();
    final ExpressionStruct expression2 = new ExpressionStruct();
    reference1.generateCode(aData, expression1);
    reference2.generateCode(aData, expression2);
    final Assignment tempAssignment = reference2.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
    final IType type = tempAssignment.getType(CompilationTimeStamp.getBaseTimestamp());
    final IType fieldType = type.getFieldType(CompilationTimeStamp.getBaseTimestamp(), reference2, 1, Expected_Value_type.EXPECTED_DYNAMIC_VALUE, false);
    if (expression1.preamble.length() > 0) {
        expression.preamble.append(expression1.preamble);
    }
    if (expression2.preamble.length() > 0) {
        expression.preamble.append(expression2.preamble);
    }
    String v3_code;
    if (value3 == null) {
        v3_code = "\"UTF-8\"";
    } else {
        final ExpressionStruct tempExpression = new ExpressionStruct();
        value3.generateCodeExpressionMandatory(aData, tempExpression, true);
        final String tempID = aData.getTemporaryVariableName();
        expression.preamble.append(MessageFormat.format("TitanCharString {0} = {1};\n", tempID, tempExpression.expression));
        expression.preamble.append(MessageFormat.format("if ({0}.operatorNotEquals(\"UTF-8\") && {0}.operatorNotEquals(\"UTF-16\") && {0}.operatorNotEquals(\"UTF-16LE\") && {0}.operatorNotEquals(\"UTF-16BE\") && {0}.operatorNotEquals(\"UTF-32\") && {0}.operatorNotEquals(\"UTF-32LE\") && {0}.operatorNotEquals(\"UTF-32BE\")) '{'\n", tempID));
        expression.preamble.append(MessageFormat.format("throw new TtcnError(MessageFormat.format(\"decvalue_unichar: Invalid encoding parameter: '{'0'}'\", {0}));\n", tempID));
        expression.preamble.append("}\n");
        v3_code = tempID;
    }
    final Scope scope = reference2.getMyScope();
    final boolean isOptional = fieldType.fieldIsOptional(reference2.getSubreferences());
    final ExpressionStruct expression3 = new ExpressionStruct();
    // TODO add support for 4th parameter
    expression3.expression.append(MessageFormat.format("{0}_default_coding", fieldType.getGenNameDefaultCoding(aData, expression.expression, scope)));
    final String bufferID = aData.getTemporaryVariableName();
    final String returnValueID = aData.getTemporaryVariableName();
    // TOOD add handling for non-built-in encoding
    expression.preamble.append("TTCN_EncDec.set_error_behavior(TTCN_EncDec.error_type.ET_ALL, TTCN_EncDec.error_behavior_type.EB_WARNING);\n");
    expression.preamble.append("TTCN_EncDec.clear_error();\n");
    expression.preamble.append(MessageFormat.format("TitanOctetString {0} = new TitanOctetString(AdditionalFunctions.unichar2oct({1}, {2}));\n", bufferID, expression1.expression, v3_code));
    expression.preamble.append(MessageFormat.format("TitanInteger {0} = new TitanInteger({1}_decoder({2}, {3}{4}, {5}));\n", returnValueID, fieldType.getGenNameCoder(aData, expression.expression, scope), bufferID, expression2.expression, isOptional ? ".get()" : "", expression3.expression));
    expression.preamble.append(MessageFormat.format("if ({0}.operatorEquals(0)) '{'\n", returnValueID));
    expression.preamble.append(MessageFormat.format("{0} = AdditionalFunctions.oct2unichar({1}, {2});\n", expression1.expression, bufferID, v3_code));
    expression.preamble.append("}\n");
    // TOOD add handling for non-built-in encoding
    expression.preamble.append("TTCN_EncDec.set_error_behavior(TTCN_EncDec.error_type.ET_ALL, TTCN_EncDec.error_behavior_type.EB_DEFAULT);\n");
    expression.preamble.append("TTCN_EncDec.clear_error();\n");
    expression.expression.append(returnValueID);
    if (expression1.postamble.length() > 0) {
        expression.postamble.append(expression1.postamble);
    }
    if (expression2.postamble.length() > 0) {
        expression.postamble.append(expression2.postamble);
    }
    if (expression3.postamble.length() > 0) {
        expression.postamble.append(expression3.postamble);
    }
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) Scope(org.eclipse.titan.designer.AST.Scope) IType(org.eclipse.titan.designer.AST.IType)

Example 2 with Scope

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

the class EncodeExpression method generateCodeExpressionExpression.

@Override
public /**
 * {@inheritDoc}
 */
void generateCodeExpressionExpression(final JavaGenData aData, final ExpressionStruct expression) {
    aData.addBuiltinTypeImport("TitanOctetString");
    aData.addCommonLibraryImport("AdditionalFunctions");
    final boolean isValue = templateInstance.getTemplateBody().isValue(CompilationTimeStamp.getBaseTimestamp());
    final ExpressionStruct expression2 = new ExpressionStruct();
    if (isValue) {
        templateInstance.getTemplateBody().getValue().generateCodeExpressionMandatory(aData, expression2, true);
    } else {
        templateInstance.generateCode(aData, expression2, Restriction_type.TR_NONE);
    }
    final Scope scope = templateInstance.getTemplateBody().getMyScope();
    final IType governor = templateInstance.getTemplateBody().getMyGovernor();
    if (expression2.preamble.length() > 0) {
        expression.postamble.append(expression2.preamble);
    }
    final ExpressionStruct expression3 = new ExpressionStruct();
    // FIXME handle third parameter
    expression3.expression.append(MessageFormat.format("{0}_default_coding", governor.getGenNameDefaultCoding(aData, expression.expression, scope)));
    final String tempID = aData.getTemporaryVariableName();
    expression.preamble.append(MessageFormat.format("TitanOctetString {0} = new TitanOctetString();\n", tempID));
    expression.preamble.append(MessageFormat.format("{0}_encoder({1}{2}, {3}, {4});\n", governor.getGenNameCoder(aData, expression.expression, scope), expression2.expression, isValue ? "" : ".valueOf()", tempID, expression3.expression));
    expression.expression.append(MessageFormat.format("AdditionalFunctions.oct2bit({0})", tempID));
    if (expression2.postamble.length() > 0) {
        expression.postamble.append(expression2.postamble);
    }
    if (expression3.postamble.length() > 0) {
        expression.postamble.append(expression3.postamble);
    }
}
Also used : Scope(org.eclipse.titan.designer.AST.Scope) IType(org.eclipse.titan.designer.AST.IType)

Example 3 with Scope

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

the class EncvalueUnicharExpression method generateCodeExpressionExpression.

@Override
public /**
 * {@inheritDoc}
 */
void generateCodeExpressionExpression(final JavaGenData aData, final ExpressionStruct expression) {
    aData.addBuiltinTypeImport("TitanOctetString");
    aData.addCommonLibraryImport("AdditionalFunctions");
    final boolean isValue = templateInstance1.getTemplateBody().isValue(CompilationTimeStamp.getBaseTimestamp());
    final ExpressionStruct expression2 = new ExpressionStruct();
    if (isValue) {
        templateInstance1.getTemplateBody().getValue().generateCodeExpressionMandatory(aData, expression2, true);
    } else {
        templateInstance1.generateCode(aData, expression2, Restriction_type.TR_NONE);
    }
    String v2_code;
    if (value2 == null) {
        v2_code = "\"UTF-8\"";
    } else {
        final ExpressionStruct tempExpression = new ExpressionStruct();
        value2.generateCodeExpressionMandatory(aData, tempExpression, true);
        final String tempID = aData.getTemporaryVariableName();
        expression.preamble.append(MessageFormat.format("TitanCharString {0} = {1};\n", tempID, tempExpression.expression));
        expression.preamble.append(MessageFormat.format("if ({0}.operatorNotEquals(\"UTF-8\") && {0}.operatorNotEquals(\"UTF-16\") && {0}.operatorNotEquals(\"UTF-16LE\") && {0}.operatorNotEquals(\"UTF-16BE\") && {0}.operatorNotEquals(\"UTF-32\") && {0}.operatorNotEquals(\"UTF-32LE\") && {0}.operatorNotEquals(\"UTF-32BE\")) '{'\n", tempID));
        expression.preamble.append(MessageFormat.format("throw new TtcnError(MessageFormat.format(\"decvalue_unichar: Invalid encoding parameter: '{'0'}'\", {0}));\n", tempID));
        expression.preamble.append("}\n");
        v2_code = tempID;
    }
    final Scope scope = templateInstance1.getTemplateBody().getMyScope();
    final IType governor = templateInstance1.getTemplateBody().getMyGovernor();
    if (expression2.preamble.length() > 0) {
        expression.postamble.append(expression2.preamble);
    }
    final ExpressionStruct expression3 = new ExpressionStruct();
    // FIXME handle 4th parameter
    expression3.expression.append(MessageFormat.format("{0}_default_coding", governor.getGenNameDefaultCoding(aData, expression.expression, scope)));
    final String tempID = aData.getTemporaryVariableName();
    expression.preamble.append(MessageFormat.format("TitanOctetString {0} = new TitanOctetString();\n", tempID));
    expression.preamble.append(MessageFormat.format("{0}_encoder({1}{2}, {3}, {4});\n", governor.getGenNameCoder(aData, expression.expression, scope), expression2.expression, isValue ? "" : ".valueOf()", tempID, expression3.expression));
    expression.expression.append(MessageFormat.format("AdditionalFunctions.oct2unichar({0}, {1})", tempID, v2_code));
    if (expression2.postamble.length() > 0) {
        expression.postamble.append(expression2.postamble);
    }
    if (expression3.postamble.length() > 0) {
        expression.postamble.append(expression3.postamble);
    }
}
Also used : Scope(org.eclipse.titan.designer.AST.Scope) IType(org.eclipse.titan.designer.AST.IType)

Example 4 with Scope

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

the class OccurencesMarker method findOccurrencesReferenceBased.

/**
 * Finds the occurrences of the element located on the given offset.
 * This solution can be used, when the locations are not correct. (e.g.
 * in case of an ASN.1 file)
 *
 * @param document
 * @param reference
 * @param module
 *                The module to search the occurrences in
 * @param offset
 *                An offset in the module
 * @return The found references. Includes the definition of the element.
 */
protected List<Hit> findOccurrencesReferenceBased(final IDocument document, final Reference reference, final Module module, final int offset) {
    Scope scope = module.getSmallestEnclosingScope(offset);
    if (scope == null) {
        removeOccurences(false);
        error(document, offset, "Can not determine the smallest enclosing scope.");
        return new ArrayList<ReferenceFinder.Hit>();
    }
    reference.setMyScope(scope);
    if (reference.getId() == null) {
        removeOccurences(false);
        error(document, offset, "The identifier of the reference is null.");
        return new ArrayList<ReferenceFinder.Hit>();
    }
    if (reference.getSubreferences().size() > 1) {
        // highlighting the subreferences is not yet supported
        removeOccurences(false);
        return new ArrayList<ReferenceFinder.Hit>();
    }
    ReferenceFinder referenceFinder;
    List<Hit> result = null;
    boolean found = false;
    if (scope.hasAssignmentWithId(CompilationTimeStamp.getBaseTimestamp(), reference.getId()) || (scope.getModuleScope().hasImportedAssignmentWithID(CompilationTimeStamp.getBaseTimestamp(), reference.getId()))) {
        Assignment assignment = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
        if (assignment == null) {
            error(document, offset, "The assignment could not be determined from the reference: " + reference.getDisplayName());
            removeOccurences(false);
            return new ArrayList<ReferenceFinder.Hit>();
        }
        if (!assignment.shouldMarkOccurrences()) {
            removeOccurences(false);
            return new ArrayList<ReferenceFinder.Hit>();
        }
        try {
            referenceFinder = new ReferenceFinder(assignment);
        } catch (final IllegalArgumentException e) {
            removeOccurences(false);
            return new ArrayList<ReferenceFinder.Hit>();
        }
        result = referenceFinder.findReferencesInModule(module);
        // Hack to eliminate false positive results
        if (assignment.getLocation().containsOffset(offset)) {
            found = true;
        } else {
            for (Hit hit : result) {
                if (hit.identifier.getLocation().containsOffset(offset)) {
                    found = true;
                    break;
                }
            }
        }
        if (found && assignment.getMyScope().getModuleScope() == module && assignment.getIdentifier() != null) {
            result.add(new Hit(assignment.getIdentifier()));
        }
    }
    if (!found) {
        // Check if the reference points to a field of a type
        // definition
        referenceFinder = new ReferenceFinder();
        referenceFinder.detectAssignmentDataByOffset(module, offset, editor, false, false);
        Assignment assignment = referenceFinder.assignment;
        if (assignment == null) {
            removeOccurences(false);
            error(document, offset, "Could not detect the assignment.");
            return new ArrayList<ReferenceFinder.Hit>();
        }
        if (assignment.getAssignmentType() != null && assignment.getAssignmentType() != Assignment_type.A_TYPE || referenceFinder.fieldId == null || !assignment.shouldMarkOccurrences()) {
            removeOccurences(false);
            return new ArrayList<ReferenceFinder.Hit>();
        }
        result = referenceFinder.findReferencesInModule(module);
        if (referenceFinder.fieldId != null) {
            result.add(new Hit(referenceFinder.fieldId));
        }
    }
    return result;
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) Hit(org.eclipse.titan.designer.AST.ReferenceFinder.Hit) Scope(org.eclipse.titan.designer.AST.Scope) ReferenceFinder(org.eclipse.titan.designer.AST.ReferenceFinder) ArrayList(java.util.ArrayList)

Example 5 with Scope

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

the class OpenDeclarationHelper method findVisibleDeclarations.

public static DeclarationCollector findVisibleDeclarations(final IEditorPart targetEditor, final IReferenceParser referenceParser, final IDocument document, final int offset, final boolean reportErrors) {
    final IFile file = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class);
    if (file == null) {
        if (reportErrors) {
            ErrorReporter.parallelDisplayInStatusLine(targetEditor, NORECOGNISABLEFILE);
        }
        return null;
    }
    Reference reference = null;
    referenceParser.setErrorReporting(reportErrors);
    reference = referenceParser.findReferenceForOpening(file, offset, document);
    if (reference == null) {
        if (reportErrors) {
            ErrorReporter.parallelDisplayInStatusLine(targetEditor, NORECOGNISABLEREFERENCE);
        }
        return null;
    }
    final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
    if (ResourceExclusionHelper.isExcluded(file)) {
        ErrorReporter.parallelDisplayInStatusLine(targetEditor, MessageFormat.format(EXCLUDEDFROMBUILD, file.getFullPath()));
        return null;
    }
    final Module tempModule = projectSourceParser.containedModule(file);
    if (tempModule == null) {
        if (reportErrors) {
            ErrorReporter.parallelDisplayInStatusLine(targetEditor, MessageFormat.format(NOTFOUNDMODULE, file.getFullPath()));
        }
        return null;
    }
    final Scope scope = tempModule.getSmallestEnclosingScope(offset);
    if (scope == null) {
        if (reportErrors) {
            ErrorReporter.parallelDisplayInStatusLine(targetEditor, NORECOGNISABLESCOPE);
        }
        return null;
    }
    reference.setMyScope(scope);
    reference.detectModid();
    if (reference.getId() == null) {
        if (reportErrors) {
            ErrorReporter.parallelDisplayInStatusLine(targetEditor, NORECOGNISABLEREFERENCE);
        }
        return null;
    }
    final DeclarationCollector declarationCollector = new DeclarationCollector(reference);
    scope.addDeclaration(declarationCollector);
    return declarationCollector;
}
Also used : IFile(org.eclipse.core.resources.IFile) Scope(org.eclipse.titan.designer.AST.Scope) Reference(org.eclipse.titan.designer.AST.Reference) Module(org.eclipse.titan.designer.AST.Module) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser)

Aggregations

Scope (org.eclipse.titan.designer.AST.Scope)24 IType (org.eclipse.titan.designer.AST.IType)13 Module (org.eclipse.titan.designer.AST.Module)9 Assignment (org.eclipse.titan.designer.AST.Assignment)6 Identifier (org.eclipse.titan.designer.AST.Identifier)6 Reference (org.eclipse.titan.designer.AST.Reference)6 RunsOnScope (org.eclipse.titan.designer.AST.TTCN3.definitions.RunsOnScope)5 IValue (org.eclipse.titan.designer.AST.IValue)4 NamedBridgeScope (org.eclipse.titan.designer.AST.NamedBridgeScope)4 ArrayList (java.util.ArrayList)3 IFile (org.eclipse.core.resources.IFile)3 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)3 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)2 IDocument (org.eclipse.jface.text.IDocument)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)2 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