use of org.abs_models.frontend.typechecker.Type in project abstools by abstools.
the class StringBasedABSTestRunnerGenerator method generateDataPoints.
/**
* Generates data points for test class {@code clazz}
*
* @param inf
* @param clazz
* @param paramNames
* @param main
* @return The data type the set return type is parametric on, null if
* {@code inf} does not define a data point method or {@code clazz}
* does not implement such method.
*/
private Type generateDataPoints(InterfaceDecl inf, ClassDecl clazz, Set<Type> paramNames, TestRunnerScriptBuilder main) {
MethodSig dataPoint = findDataPoints(inf);
if (dataPoint == null) {
return null;
}
/*
* It must be a Set of data
*/
Type data = ((ParametricDataTypeUse) dataPoint.getReturnType()).getParams().iterator().next().getType();
/*
* make sure the return type can be resolved TODO this needs to be
* resolved recursively
*/
paramNames.add(data);
/*
* create an object in the same cog as main for retrieving the data
* points
*/
String objName = uncap(clazz.getName()) + "dataPoint";
String dataSet = dataPointSetName(clazz);
newObj(main, inf, clazz, objName, false);
main.append(dataPoint.getReturnType()).append(" ").append(dataSet).append(" = ").append(objName).append(".").append(dataPoint.getName()).append("();").newLine();
return data;
}
use of org.abs_models.frontend.typechecker.Type in project abstools by abstools.
the class StringBasedABSTestRunnerGenerator method generateTestClassImpl.
private Set<Type> generateTestClassImpl(InterfaceDecl inf, ClassDecl clazz, TestRunnerScriptBuilder main) {
Set<Type> paramNames = new HashSet<>();
Type dataType = generateDataPoints(inf, clazz, paramNames, main);
String namePrefix = clazz.getName();
int instance = 0;
for (MethodSig method : getTestMethods(inf)) {
boolean needdata = method.getParamList().iterator().hasNext();
if (needdata) {
if (dataType == null) {
throw new IllegalStateException("Test method requires arguments but test class defines no data point");
}
/*
* a while loop over all data points
*/
String dataPointSet = dataPointSetName(clazz);
// begin while
main.append("while (hasNext(").append(dataPointSet).append(")) {").newLine().increaseIndent();
main.append("Pair<Set<").append(dataType).append(">,").append(dataType).append("> nt = next(").append(dataPointSet).append(");").newLine();
main.append(dataType).append(" ").append(dataValue).append(" = snd(nt);").newLine();
main.append(dataPointSet).append(" = fst(nt);").newLine();
}
/*
* Add those methods that are not ignored
*/
if (!isIgnored(clazz, method)) {
main.append("//Test cases for method ").append(method.getName()).newLine();
String objectRef = uncap(namePrefix) + instance;
main = newCog(main, inf, clazz, objectRef);
generateAsyncTestCall(main, objectRef, method);
}
if (needdata) {
// end while
main.decreaseIndent().append("}").newLine();
}
instance++;
}
return paramNames;
}
use of org.abs_models.frontend.typechecker.Type in project abstools by abstools.
the class MaudeCompilerHelper method emitParameterValueList.
public static void emitParameterValueList(PrintStream stream, List<PureExp> params, java.util.List<Type> paramTypes) {
if (params.getNumChild() == 0) {
stream.print("emp ");
} else {
boolean inlist = false;
stream.print("(");
for (int i = 0; i < params.getNumChild(); i++) {
PureExp param = params.getChild(i);
Type t = paramTypes.get(i);
boolean needConversion = t.isIntType() && param.getType().isRatType();
if (inlist)
stream.print(":: ");
else
inlist = true;
if (needConversion)
stream.print("\"ABS.StdLib.truncate\"(");
param.generateMaude(stream);
if (needConversion)
stream.print(")");
}
stream.print(") ");
}
}
use of org.abs_models.frontend.typechecker.Type in project abstools by abstools.
the class ContractInference method typeInference.
public ResultInferenceEffExp typeInference(Call call) {
_log.logDebug("Contract Inference for a " + ((call instanceof SyncCall) ? "Synchronous" : "Asynchronous") + " method Call");
_log.beginIndent();
ITypingEnvironmentVariableType r;
GroupName aprime = _df.newGroupName();
// 1. Get the method interface
Type t = call.getCallee().getType();
ClassDecl cl;
if (t.isInterfaceType()) {
cl = _intertoclass.get(((InterfaceType) t).getDecl());
} else {
cl = _cd;
}
String nameOfClass;
String nameOfMethod;
LinkedList<IRecord> s = new LinkedList<>();
if (cl == _emptyDecl) {
// we are in presence of an non implemented interface
// in that case, we don't know how the method would behave, and
// simply put a null contract.
_log.endIndent();
_log.logError("Class retrival failed!!!");
nameOfClass = _dummyClass;
nameOfMethod = _dummyMethod;
// return new ResultInferenceEffExp( _df.newRecordVariable(),
// _df.newContractEmpty(), _df.newConstraint(), _env);
} else {
nameOfClass = cl.getName();
nameOfMethod = call.getMethod();
ResultInferencePureExp resParam;
for (PureExp p : call.getParams()) {
resParam = typeInferenceAsPure(p);
s.add(_env.getRecord(resParam.getVariableType()));
}
}
// else
{
// 2. Collect contracts and
// deadlock.constraints.constraint.Constraints from the call
ResultInferencePureExp resCallee = typeInferenceAsPure(call.getCallee());
Contract contract = _df.newContractEmpty();
Constraint c = _df.newConstraint();
// cast to IRecord as the callee cannot be a future
IRecord callee = (IRecord) resCallee.getVariableType();
// 3. Construct the record for the return value
IRecord Y = _df.newRecordVariable();
// 4. pack up the result
MethodInterface mi = _df.newMethodInterface(callee, s, Y);
c.addSemiEquation(new ASTNodeInformation(call), _env.getMethod(nameOfClass, nameOfMethod), mi);
if (call instanceof SyncCall) {
r = Y;
contract.add(_df.newContractSyncInvk(call, nameOfClass, nameOfMethod, mi));
} else if (call instanceof AsyncCall) {
IRecord calleeShape = createInstance(cl, aprime);
c.addEquation(new ASTNodeInformation(call), callee, calleeShape);
r = new TypingEnvironmentVariableTypeFuture();
_env.putFuture((TypingEnvironmentVariableTypeFuture) r, new TypingEnvironmentFutureTypeUntick(_df.newRecordFuture(aprime, Y), new ContractElementInvk(call, nameOfClass, nameOfMethod, mi)));
} else if (call instanceof AwaitAsyncCall) {
// same stuff as for async calls
// AwaitAsyncCall are actually rewritten in the AST if the flag
// public static boolean Model.doAACrewrite is set to true;
// objects of this kind are rewritten at the ast level as
// AsyncCall + await (+ get)
IRecord calleeShape = createInstance(cl, aprime);
c.addEquation(new ASTNodeInformation(call), callee, calleeShape);
r = new TypingEnvironmentVariableTypeFuture();
_env.putFuture((TypingEnvironmentVariableTypeFuture) r, new TypingEnvironmentFutureTypeUntick(_df.newRecordFuture(aprime, Y), new ContractElementInvk(call, nameOfClass, nameOfMethod, mi)));
} else {
// should not happen
r = null;
_log.logError("Unexpected call type");
}
_log.endIndent();
_log.logDebug("End Contract Inference for the Call");
return new ResultInferenceEffExp(r, contract, c, _env);
}
}
use of org.abs_models.frontend.typechecker.Type in project abstools by abstools.
the class JavaBackend method getQualifiedString.
public static String getQualifiedString(Type absType) {
String res = null;
if (absType.isDataType()) {
DataTypeType dt = (DataTypeType) absType;
res = dataTypeMap.get(dt.getDecl().getName());
if (res != null)
return res;
StringBuilder sb = new StringBuilder();
if (dt.hasTypeArgs() && !containsUnboundedType(dt.getTypeArgs())) {
sb.append("<");
boolean first = true;
for (Type t : dt.getTypeArgs()) {
if (first)
first = false;
else
sb.append(',');
sb.append(getQualifiedString(t));
}
sb.append(">");
}
return getQualifiedString(dt.getDecl()) + sb.toString();
/*
* if (dt.hasTypeArgs() && !containsUnboundedType(dt.getTypeArgs()))
* {
*
* sb.append("<"); boolean first = true; for (Type t :
* dt.getTypeArgs()) { if (first) first = false; else
* sb.append(','); sb.append(getQualifiedString(t)); }
* sb.append(">"); }
*/
} else if (absType.isInterfaceType()) {
InterfaceType it = (InterfaceType) absType;
return getQualifiedString(it.getDecl());
} else if (absType.isTypeParameter()) {
TypeParameter tp = (TypeParameter) absType;
return tp.getDecl().getName();
} else if (absType.isBoundedType()) {
BoundedType bt = (BoundedType) absType;
if (bt.hasBoundType())
return getQualifiedString(bt.getBoundType());
return "?";
} else if (absType.isAnyType()) {
return "java.lang.Object";
} else if (absType.isUnionType()) {
return getQualifiedString(((UnionType) absType).getOriginatingClass());
}
throw new RuntimeException("Type " + absType.getClass().getName() + " not yet supported by Java backend");
}
Aggregations