Search in sources :

Example 1 with InterfaceType

use of org.abs_models.frontend.typechecker.InterfaceType 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);
    }
}
Also used : Constraint(com.gzoumix.semisolver.constraint.Constraint) LinkedList(java.util.LinkedList) Type(org.abs_models.frontend.typechecker.Type) InterfaceType(org.abs_models.frontend.typechecker.InterfaceType) InterfaceType(org.abs_models.frontend.typechecker.InterfaceType)

Example 2 with InterfaceType

use of org.abs_models.frontend.typechecker.InterfaceType 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");
}
Also used : BoundedType(org.abs_models.frontend.typechecker.BoundedType) UnionType(org.abs_models.frontend.typechecker.UnionType) Type(org.abs_models.frontend.typechecker.Type) InterfaceType(org.abs_models.frontend.typechecker.InterfaceType) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) BoundedType(org.abs_models.frontend.typechecker.BoundedType) UnionType(org.abs_models.frontend.typechecker.UnionType) InterfaceType(org.abs_models.frontend.typechecker.InterfaceType) TypeParameter(org.abs_models.frontend.typechecker.TypeParameter) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) ABSString(org.abs_models.backend.java.lib.types.ABSString)

Aggregations

InterfaceType (org.abs_models.frontend.typechecker.InterfaceType)2 Type (org.abs_models.frontend.typechecker.Type)2 Constraint (com.gzoumix.semisolver.constraint.Constraint)1 LinkedList (java.util.LinkedList)1 ABSString (org.abs_models.backend.java.lib.types.ABSString)1 BoundedType (org.abs_models.frontend.typechecker.BoundedType)1 DataTypeType (org.abs_models.frontend.typechecker.DataTypeType)1 TypeParameter (org.abs_models.frontend.typechecker.TypeParameter)1 UnionType (org.abs_models.frontend.typechecker.UnionType)1