Search in sources :

Example 1 with ExprCycleVisitor

use of com.rockwellcollins.atc.agree.visitors.ExprCycleVisitor in project AGREE by loonwerks.

the class AgreeValidator method checkMultiAssignEq.

private void checkMultiAssignEq(EObject src, List<Arg> lhsArgs, Expr rhsExpr) {
    if (rhsExpr == null) {
        return;
    }
    if (lhsArgs.size() == 1) {
        // we should only need to check for cycles for single equations
        String name = lhsArgs.get(0).getName();
        ExprCycleVisitor cycleVisitor = new ExprCycleVisitor(name);
        Set<EObject> cycleObjects = cycleVisitor.doSwitch(rhsExpr);
        if (cycleObjects == null) {
            throw new IllegalArgumentException("something went wrong with the cycle checker");
        }
        for (EObject obj : cycleObjects) {
            error(obj, "Cyclic reference to variable '" + name + "'");
        }
    }
    if (argsContainRangeValue(lhsArgs)) {
        error(src, "Equation statements cannot contain a ranged value and a right hand side expression");
    }
    List<TypeDef> agreeLhsTypes = new ArrayList<>();
    for (Arg arg : lhsArgs) {
        agreeLhsTypes.add(AgreeTypeSystem.typeDefFromType(arg.getType()));
    }
    List<TypeDef> agreeRhsTypes = new ArrayList<>();
    if (rhsExpr instanceof CallExpr) {
        NamedElement namedEl = ((CallExpr) rhsExpr).getRef().getElm();
        if (namedEl instanceof NodeDef) {
            NodeDef nodeDef = (NodeDef) namedEl;
            for (Arg var : nodeDef.getRets()) {
                agreeRhsTypes.add(AgreeTypeSystem.typeDefFromType(var.getType()));
            }
        } else if (namedEl instanceof FnDef) {
            FnDef fnDef = (FnDef) namedEl;
            agreeRhsTypes.add(AgreeTypeSystem.typeDefFromType(fnDef.getType()));
        } else if (namedEl instanceof UninterpretedFnDef) {
            UninterpretedFnDef uninterpretedFnDef = (UninterpretedFnDef) namedEl;
            agreeRhsTypes.add(AgreeTypeSystem.typeDefFromType(uninterpretedFnDef.getType()));
        } else {
            // parse error
            return;
        }
    } else {
        checkTypeExists(rhsExpr);
        TypeDef rhsType = AgreeTypeSystem.infer(rhsExpr);
        agreeRhsTypes.add(rhsType);
    }
    if (agreeLhsTypes.size() != agreeRhsTypes.size()) {
        error(src, "Equation assigns " + agreeLhsTypes.size() + " variables, but right side returns " + agreeRhsTypes.size() + " values");
        return;
    }
    for (int i = 0; i < agreeLhsTypes.size(); i++) {
        TypeDef lhsType = agreeLhsTypes.get(i);
        TypeDef rhsType = agreeRhsTypes.get(i);
        if (!AgreeTypeSystem.typesEqual(rhsType, lhsType)) {
            error(src, "The variable '" + lhsArgs.get(i).getName() + "' on the left side of equation is of type '" + nameOfTypeDef(lhsType) + "' but must be of type '" + nameOfTypeDef(rhsType) + "'");
        }
    }
}
Also used : NodeDef(com.rockwellcollins.atc.agree.agree.NodeDef) ArrayList(java.util.ArrayList) UninterpretedFnDef(com.rockwellcollins.atc.agree.agree.UninterpretedFnDef) FnDef(com.rockwellcollins.atc.agree.agree.FnDef) LibraryFnDef(com.rockwellcollins.atc.agree.agree.LibraryFnDef) UninterpretedFnDef(com.rockwellcollins.atc.agree.agree.UninterpretedFnDef) ExprCycleVisitor(com.rockwellcollins.atc.agree.visitors.ExprCycleVisitor) AgreeTypeSystem.nameOfTypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.nameOfTypeDef) ArrayTypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.ArrayTypeDef) RecordTypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.RecordTypeDef) TypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.TypeDef) EObject(org.eclipse.emf.ecore.EObject) Arg(com.rockwellcollins.atc.agree.agree.Arg) CallExpr(com.rockwellcollins.atc.agree.agree.CallExpr) NamedElement(org.osate.aadl2.NamedElement)

Aggregations

ArrayTypeDef (com.rockwellcollins.atc.agree.AgreeTypeSystem.ArrayTypeDef)1 RecordTypeDef (com.rockwellcollins.atc.agree.AgreeTypeSystem.RecordTypeDef)1 TypeDef (com.rockwellcollins.atc.agree.AgreeTypeSystem.TypeDef)1 AgreeTypeSystem.nameOfTypeDef (com.rockwellcollins.atc.agree.AgreeTypeSystem.nameOfTypeDef)1 Arg (com.rockwellcollins.atc.agree.agree.Arg)1 CallExpr (com.rockwellcollins.atc.agree.agree.CallExpr)1 FnDef (com.rockwellcollins.atc.agree.agree.FnDef)1 LibraryFnDef (com.rockwellcollins.atc.agree.agree.LibraryFnDef)1 NodeDef (com.rockwellcollins.atc.agree.agree.NodeDef)1 UninterpretedFnDef (com.rockwellcollins.atc.agree.agree.UninterpretedFnDef)1 ExprCycleVisitor (com.rockwellcollins.atc.agree.visitors.ExprCycleVisitor)1 ArrayList (java.util.ArrayList)1 EObject (org.eclipse.emf.ecore.EObject)1 NamedElement (org.osate.aadl2.NamedElement)1