Search in sources :

Example 6 with Declaration

use of org.yakindu.base.types.Declaration in project statecharts by Yakindu.

the class STextJavaValidator method checkValueReferenedBeforeDefined.

@Check(CheckType.NORMAL)
public void checkValueReferenedBeforeDefined(Scope scope) {
    EList<Declaration> declarations = scope.getDeclarations();
    Set<QualifiedName> defined = Sets.newHashSet();
    for (Declaration declaration : declarations) {
        if (declaration instanceof VariableDefinition) {
            VariableDefinition definition = (VariableDefinition) declaration;
            if (!definition.isConst())
                return;
            Expression initialValue = definition.getInitialValue();
            List<Expression> toCheck = Lists.newArrayList(initialValue);
            TreeIterator<EObject> eAllContents = initialValue.eAllContents();
            while (eAllContents.hasNext()) {
                EObject next = eAllContents.next();
                if (next instanceof Expression)
                    toCheck.add((Expression) next);
            }
            for (Expression expression : toCheck) {
                EObject referencedObject = null;
                if (expression instanceof FeatureCall)
                    referencedObject = ((FeatureCall) expression).getFeature();
                else if (expression instanceof ElementReferenceExpression)
                    referencedObject = ((ElementReferenceExpression) expression).getReference();
                if (referencedObject instanceof VariableDefinition) {
                    if (!defined.contains(nameProvider.getFullyQualifiedName(referencedObject)))
                        error(REFERENCE_CONSTANT_BEFORE_DEFINED, definition, StextPackage.Literals.VARIABLE_DEFINITION__INITIAL_VALUE);
                }
            }
            defined.add(nameProvider.getFullyQualifiedName(definition));
        }
    }
}
Also used : VariableDefinition(org.yakindu.sct.model.stext.stext.VariableDefinition) EventRaisingExpression(org.yakindu.sct.model.stext.stext.EventRaisingExpression) Expression(org.yakindu.base.expressions.expressions.Expression) AssignmentExpression(org.yakindu.base.expressions.expressions.AssignmentExpression) EventValueReferenceExpression(org.yakindu.sct.model.stext.stext.EventValueReferenceExpression) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) PostFixUnaryExpression(org.yakindu.base.expressions.expressions.PostFixUnaryExpression) QualifiedName(org.eclipse.xtext.naming.QualifiedName) EObject(org.eclipse.emf.ecore.EObject) Declaration(org.yakindu.base.types.Declaration) FeatureCall(org.yakindu.base.expressions.expressions.FeatureCall) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) Check(org.eclipse.xtext.validation.Check)

Aggregations

Declaration (org.yakindu.base.types.Declaration)6 Scope (org.yakindu.sct.model.sgraph.Scope)3 Statechart (org.yakindu.sct.model.sgraph.Statechart)3 EventDefinition (org.yakindu.sct.model.stext.stext.EventDefinition)3 InternalScope (org.yakindu.sct.model.stext.stext.InternalScope)3 VariableDefinition (org.yakindu.sct.model.stext.stext.VariableDefinition)3 EObject (org.eclipse.emf.ecore.EObject)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 Check (org.eclipse.xtext.validation.Check)2 Test (org.junit.Test)2 ElementReferenceExpression (org.yakindu.base.expressions.expressions.ElementReferenceExpression)2 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)1 LazyLinkingResource (org.eclipse.xtext.linking.lazy.LazyLinkingResource)1 QualifiedName (org.eclipse.xtext.naming.QualifiedName)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 NamedElement (org.yakindu.base.base.NamedElement)1 AssignmentExpression (org.yakindu.base.expressions.expressions.AssignmentExpression)1 Expression (org.yakindu.base.expressions.expressions.Expression)1 FeatureCall (org.yakindu.base.expressions.expressions.FeatureCall)1 PostFixUnaryExpression (org.yakindu.base.expressions.expressions.PostFixUnaryExpression)1