use of com.ge.research.osate.verdict.dsl.type.VerdictField in project VERDICT by ge-high-assurance.
the class ThreatModelUtil method getVarType.
/**
* Get the type of a variable/field.
*
* First finds the type of the variable by looking it up in the
* scope. Then finds the type of each variable by looking it up
* in the fields of the previous type.
*
* If at any point something is out of scope or does not type-check,
* then the returned type will be empty.
*
* @param varField the Var AST object to type
* @param indexProvider the index provider, may be obtained from Guice
* @return see FieldTypeResult
*/
public static FieldTypeResult getVarType(Var varField, ResourceDescriptionsProvider indexProvider) {
FieldTypeResult result = new FieldTypeResult();
// Get correct parent for scoping
EObject scopeParent = getContainerForClasses(varField, VAR_FIELD_SCOPE_PARENT_CLASSES);
// ID is always present
result.varName = varField.getId();
// Get all variables in scope
List<VerdictVariable> vars = ThreatModelUtil.getScope(scopeParent, indexProvider);
// Get the variable corresponding to the ID
// Empty if that ID is not in scope
result.var = vars.stream().filter(var -> var.getId().equals(result.varName)).findFirst();
if (!result.var.isPresent()) {
return result;
} else {
if (!result.var.get().getType().isPresent()) {
// yet have a type because the user is still editing the var/field
return result;
} else {
// Check type iteratively for all fields and their children
// Invariant: "type" holds the type of the rightmost var/field that has been processed
VerdictType type = result.var.get().getType().get();
if (varField.getIds() != null) {
for (String fieldName : varField.getIds()) {
// Find the field of the current type for the next field name
Optional<VerdictField> field = type.getFields().stream().filter(f -> f.getName().equals(fieldName)).findFirst();
if (field.isPresent()) {
// Well-typed, advance to the next field
type = field.get().getType();
result.fieldIndex++;
} else {
// Not well-typed, crash and burn
result.lastField = fieldName;
return result;
}
}
}
// All fields type-check and "type" holds the rightmost type
result.type = Optional.of(type);
return result;
}
}
}
Aggregations