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) + "'");
}
}
}
Aggregations