use of com.rockwellcollins.atc.agree.agree.FnDef 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) + "'");
}
}
}
use of com.rockwellcollins.atc.agree.agree.FnDef in project AGREE by loonwerks.
the class AgreeLinkingService method getElm.
private Element getElm(EObject context, EReference reference, String name) {
String[] segments = name.split("::");
if (segments.length >= 2) {
String pkgName = String.join("::", Arrays.copyOf(segments, segments.length - 1));
String statementName = segments[segments.length - 1];
Namespace namespace = AadlUtil.getContainingTopLevelNamespace(context);
PropertySet propSet = AadlUtil.findImportedPropertySet(pkgName, namespace);
if (propSet != null) {
Element elm = propSet.findNamedElement(statementName);
return elm;
}
AadlPackage aadlPackage = AadlUtil.findImportedPackage(pkgName, namespace);
if (aadlPackage == null) {
for (PackageRename rename : EcoreUtil2.getAllContentsOfType(namespace.getElementRoot(), PackageRename.class)) {
if (rename.getName() != null && pkgName.equals(rename.getName())) {
aadlPackage = rename.getRenamedPackage();
}
}
}
if (aadlPackage != null) {
for (AnnexLibrary annex : AnnexUtil.getAllActualAnnexLibraries(aadlPackage, AgreePackage.eINSTANCE.getAgreeContractLibrary())) {
AgreeContract contract = (AgreeContract) ((AgreeContractLibrary) annex).getContract();
for (SpecStatement spec : contract.getSpecs()) {
if (spec instanceof RecordDef) {
if (((RecordDef) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof FnDef) {
if (((FnDef) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof LibraryFnDef) {
if (((LibraryFnDef) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof UninterpretedFnDef) {
if (((UninterpretedFnDef) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof NodeDef) {
if (((NodeDef) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof LinearizationDef) {
if (((LinearizationDef) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof ConstStatement) {
if (((ConstStatement) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof EnumStatement) {
if (((EnumStatement) spec).getName().equals(statementName)) {
return (spec);
}
EList<NamedID> enums = ((EnumStatement) spec).getEnums();
for (NamedID nid : enums) {
if (nid.getName().contentEquals(statementName)) {
return nid;
}
}
}
}
}
}
}
return null;
}
use of com.rockwellcollins.atc.agree.agree.FnDef in project AMASE by loonwerks.
the class SafetyLinkingService method getElm.
private Element getElm(EObject context, EReference reference, String name) {
String[] segments = name.split("::");
if (segments.length >= 2) {
String pkgName = String.join("::", Arrays.copyOf(segments, segments.length - 1));
String statementName = segments[segments.length - 1];
Namespace namespace = AadlUtil.getContainingTopLevelNamespace(context);
PropertySet propSet = AadlUtil.findImportedPropertySet(pkgName, namespace);
if (propSet != null) {
Element elm = propSet.findNamedElement(statementName);
return elm;
}
AadlPackage aadlPackage = AadlUtil.findImportedPackage(pkgName, namespace);
if (aadlPackage == null) {
for (PackageRename rename : EcoreUtil2.getAllContentsOfType(namespace.getElementRoot(), PackageRename.class)) {
if (rename.getName() != null && pkgName.equals(rename.getName())) {
aadlPackage = rename.getRenamedPackage();
}
}
}
if (aadlPackage != null) {
for (AnnexLibrary annex : AnnexUtil.getAllActualAnnexLibraries(aadlPackage, AgreePackage.eINSTANCE.getAgreeContractLibrary())) {
AgreeContract contract = (AgreeContract) ((AgreeContractLibrary) annex).getContract();
for (com.rockwellcollins.atc.agree.agree.SpecStatement spec : contract.getSpecs()) {
if (spec instanceof RecordDef) {
if (((RecordDef) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof FnDef) {
if (((FnDef) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof LibraryFnDef) {
if (((LibraryFnDef) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof NodeDef) {
if (((NodeDef) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof LinearizationDef) {
if (((LinearizationDef) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof ConstStatement) {
if (((ConstStatement) spec).getName().equals(statementName)) {
return (spec);
}
} else if (spec instanceof EnumStatement) {
if (((EnumStatement) spec).getName().equals(statementName)) {
return (spec);
}
EList<NamedID> enums = ((EnumStatement) spec).getEnums();
for (NamedID nid : enums) {
if (nid.getName().contentEquals(statementName)) {
return nid;
}
}
}
}
}
}
}
return null;
}
use of com.rockwellcollins.atc.agree.agree.FnDef in project AGREE by loonwerks.
the class AgreeValidator method checkNodeStmt.
@Check(CheckType.FAST)
public void checkNodeStmt(NodeStmt nodeStmt) {
List<NamedElmExpr> dotIds = EcoreUtil2.getAllContentsOfType(nodeStmt, NamedElmExpr.class);
for (NamedElmExpr dotId : dotIds) {
NamedElement id = dotId.getElm();
// restrict the elements that are single names or the last projection.
boolean restrictedElm = true;
if (dotId.eContainer() instanceof SelectionExpr) {
NamedElement ne = ((SelectionExpr) dotId.eContainer()).getField();
restrictedElm = ne == id && !(dotId.eContainer().eContainer() instanceof SelectionExpr);
}
if (restrictedElm && !(id instanceof Arg) && !(id instanceof ConstStatement) && !(id instanceof NodeDef) && !(id instanceof FnDef) && !(id instanceof UninterpretedFnDef) && !(id instanceof DataSubcomponent) && !(id instanceof DoubleDotRef) && !(id instanceof DataImplementation) && !(id instanceof RecordDef) && !(id instanceof NamedID)) {
error(dotId, "Only arguments, constants, and node calls allowed within a node");
}
}
}
use of com.rockwellcollins.atc.agree.agree.FnDef in project AGREE by loonwerks.
the class AgreeValidator method checkInputsVsActuals.
public void checkInputsVsActuals(CallExpr call) {
DoubleDotRef dotId = call.getRef();
NamedElement namedEl = dotId.getElm();
if (!(namedEl instanceof Abstraction)) {
// this error will be caught elsewhere
return;
}
Abstraction callDef = (Abstraction) namedEl;
List<TypeDef> inDefTypes;
String callName;
// extract in/out arguments
if (callDef instanceof FnDef) {
FnDef fnDef = (FnDef) callDef;
inDefTypes = typeDefsFromArgs(fnDef.getArgs());
callName = fnDef.getName();
} else if (callDef instanceof NodeDef) {
NodeDef nodeDef = (NodeDef) callDef;
inDefTypes = typeDefsFromArgs(nodeDef.getArgs());
callName = nodeDef.getName();
} else if (callDef instanceof LinearizationDef) {
LinearizationDef linDef = (LinearizationDef) callDef;
inDefTypes = typeDefsFromArgs(linDef.getArgs());
callName = linDef.getName();
} else if (callDef instanceof LibraryFnDef) {
LibraryFnDef nativeDef = (LibraryFnDef) callDef;
inDefTypes = typeDefsFromArgs(nativeDef.getArgs());
callName = nativeDef.getName();
} else if (callDef instanceof UninterpretedFnDef) {
UninterpretedFnDef nativeDef = (UninterpretedFnDef) callDef;
inDefTypes = typeDefsFromArgs(nativeDef.getArgs());
callName = nativeDef.getName();
} else {
error(call, "Node, function or linearization definition name expected.");
return;
}
// extract args
List<TypeDef> argCallTypes = new ArrayList<>();
for (Expr expr : call.getArgs()) {
checkTypeExists(expr);
argCallTypes.add(AgreeTypeSystem.infer(expr));
}
if (inDefTypes.size() != argCallTypes.size()) {
error(call, "Function definition '" + callName + "' requires " + inDefTypes.size() + " arguments, but this function call provides " + argCallTypes.size() + " arguments");
return;
}
for (int i = 0; i < inDefTypes.size(); i++) {
TypeDef callType = argCallTypes.get(i);
TypeDef defType = inDefTypes.get(i);
if (!AgreeTypeSystem.typesEqual(defType, callType)) {
error(call, "Argument " + i + " of function call '" + callName + "' is of type '" + callType + "' but must be of type '" + defType + "'");
}
}
}
Aggregations