Search in sources :

Example 6 with Constraint

use of com.gzoumix.semisolver.constraint.Constraint in project abstools by abstools.

the class ContractInference method typeInference.

// ////////////////////////////////////////////////////////////////////////////
// 2.3. Statments
public ResultInferenceStmt typeInference(AssignStmt astmt) {
    _log.logDebug("Contract Inference for the AssignStmt");
    _log.beginIndent();
    String name = astmt.getVar().getName();
    Contract contract;
    Constraint c;
    ITypingEnvironmentVariableType X;
    Exp exp = astmt.getValue();
    if (exp instanceof PureExp) {
        ResultInferencePureExp resValue = typeInferenceAsPure((PureExp) exp);
        contract = _df.newContractEmpty();
        c = _df.newConstraint();
        X = resValue.getVariableType();
    } else {
        ResultInferenceEffExp resValue = typeInference((EffExp) exp);
        contract = resValue.getContract();
        c = resValue.getConstraint();
        X = resValue.getRecord();
    }
    _log.endIndent();
    _log.logDebug("AssignStmt Sub-Expression Finished");
    if (_env.isField(name)) {
        c.addEquation(new ASTNodeInformation(astmt), _env.getVariableRecord(name), (IRecord) X);
    } else {
        _env.putVariable(name, X);
    }
    return new ResultInferenceStmt(contract, c, _env);
}
Also used : Constraint(com.gzoumix.semisolver.constraint.Constraint)

Example 7 with Constraint

use of com.gzoumix.semisolver.constraint.Constraint 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 8 with Constraint

use of com.gzoumix.semisolver.constraint.Constraint in project abstools by abstools.

the class ContractInference method typeInference.

public ResultInference typeInference(ClassDecl cd) {
    ResultInference res = new ResultInference();
    if (cd == _emptyDecl)
        return res;
    _log.logDebug("Contract Inference for the class \"" + cd.getName() + "\"");
    _log.beginIndent();
    _cd = cd;
    // 1. Methods
    for (MethodImpl m : cd.getMethods()) {
        res.add(typeInference(m));
    }
    // 2. Init
    MethodInterface mi = _env.getMethod(cd.getName(), _initName);
    IRecord thisRecord = mi.getThis();
    _a = ((RecordPresent) thisRecord).getRoot();
    Constraint c = _df.newConstraint();
    Contract cp, cf;
    _env.newScope();
    _env.putVariable(_this, thisRecord);
    // 2.1. Field assignments
    for (FieldDecl f : cd.getFields()) {
        if (f.hasInitExp()) {
            ResultInferencePureExp tmp = typeInferenceAsPure(f.getInitExp());
            c.add(tmp.getConstraint());
            c.addEquation(new ASTNodeInformation(f), ((RecordPresent) thisRecord).getField(f.getName()), _env.getRecord(tmp.getVariableType()));
        }
    }
    // 2.2. Init block
    if (cd.hasInitBlock()) {
        ResultInferenceStmt resInitBlock = typeInference(cd.getInitBlock());
        cp = resInitBlock.getContract();
        c.add(resInitBlock.getConstraint());
        cf = _df.newContractEmpty();
        for (TypingEnvironment te : resInitBlock.getEnvironment()) cf = _df.newContractUnion(cd.getInitBlock(), cf, _df.newContract(te.unsync(cd.getInitBlock())));
    } else {
        cp = _df.newContractEmpty();
        cf = _df.newContractEmpty();
    }
    // 2.3. add the call to run if the method exists.
    if (_env.getMethod(cd.getName(), _runName) != null) {
        ASTNode node = (cd.hasInitBlock() ? cd.getInitBlock() : cd);
        MethodInterface mirun = _df.newMethodInterface(thisRecord, new LinkedList<>(), _df.newRecordVariable());
        c.addSemiEquation(new ASTNodeInformation(node), _env.getMethod(cd.getName(), _runName), mirun);
        List<Contract> tmp = new LinkedList<>();
        tmp.add(cf);
        tmp.add(_df.newContractInvk(node, cd.getName(), _runName, mirun));
        cf = _df.newContractParallel(node, tmp);
    }
    res.add(c);
    res.add(cd.getName(), _initName, _df.newMethodContract(mi, cp, cf));
    _env.clearFutures();
    _env.clearVariables();
    _log.endIndent();
    _log.logDebug("Inference for the class \"" + cd.getName() + "\" Finished");
    return res;
}
Also used : Constraint(com.gzoumix.semisolver.constraint.Constraint) LinkedList(java.util.LinkedList)

Example 9 with Constraint

use of com.gzoumix.semisolver.constraint.Constraint in project abstools by abstools.

the class ContractInference method typeInference.

/**
 ******************************************************
 */
// TODO:
// CaseExp : PureExp ::= Expr:PureExp Branch:CaseBranch* ;
// CaseBranch ::= Left:Pattern Right:PureExp ;
// abstract Pattern ;
// PatternVarUse: Pattern ::= <Name>;
// PatternVar: Pattern ::= Var:PatternVarDecl;
// ConstructorPattern: Pattern ::= <Constructor> Param:Pattern*;
// LiteralPattern: Pattern ::= Literal:LiteralExp;
// UnderscorePattern: Pattern;
// 2.4.2. Expressions with side effects.
public ResultInferenceEffExp typeInference(NewExp newExp) {
    _log.logDebug("Contract Inference for the NewExp");
    _log.beginIndent();
    // 1. Create the record for the new object, and collect contract and
    // constraints from arguments
    ClassDecl cl = (ClassDecl) (newExp.lookup(new KindedName(Kind.CLASS, newExp.getClassName())));
    LinkedList<RecordField> fields = new LinkedList<>();
    int i = 0;
    Contract contract = _df.newContractEmpty();
    Constraint c = _df.newConstraint();
    // 1.1. Params
    _env.newScope();
    ResultInferencePureExp resParam;
    for (PureExp p : newExp.getParams()) {
        resParam = typeInferenceAsPure(p);
        ITypingEnvironmentVariableType x = _env.getRecord(resParam.getVariableType());
        IRecord r = _env.getRecord(x);
        fields.add(_df.newRecordField(cl.getParam(i).getName(), r));
        _env.putVariable(cl.getParam(i).getName(), r);
        c.add(resParam.getConstraint());
        i++;
    }
    // 1.2. Fields
    for (FieldDecl f : cl.getFields()) {
        if (f.hasInitExp()) {
            resParam = typeInferenceAsPure(f.getInitExp());
            RecordField newRecordField = _df.newRecordField(f.getName(), (IRecord) resParam.getVariableType());
            fields.add(newRecordField);
            _env.putVariable(f.getName(), (IRecord) resParam.getVariableType());
            c.add(resParam.getConstraint());
        } else {
            fields.add(_df.newRecordField(f.getName(), _df.newRecordVariable()));
        }
    }
    // 1.3. Group Name
    // depends if the new object is in the same cog or not
    GroupName aprime;
    if (!newExp.hasLocal()) {
        aprime = _df.newGroupName();
    } else {
        aprime = _a;
    }
    IRecord r = _df.newRecordPresent(aprime, fields);
    // 1.4. Calling the init of r
    MethodInterface miinit = _df.newMethodInterface(r, new LinkedList<>(), _df.newRecordVariable());
    c.addSemiEquation(new ASTNodeInformation(newExp), _env.getMethod(cl.getName(), _initName), miinit);
    contract.add(_df.newContractInvk(newExp, cl.getName(), _initName, miinit));
    _env.popScope();
    _log.endIndent();
    _log.logDebug("End Contract Inference for the NewExp");
    return new ResultInferenceEffExp(r, contract, c, _env);
}
Also used : Constraint(com.gzoumix.semisolver.constraint.Constraint) LinkedList(java.util.LinkedList) Constraint(com.gzoumix.semisolver.constraint.Constraint) KindedName(org.abs_models.frontend.typechecker.KindedName)

Example 10 with Constraint

use of com.gzoumix.semisolver.constraint.Constraint in project abstools by abstools.

the class ContractInference method typeInference.

public ResultInferenceStmt typeInference(AwaitStmt astmt) {
    _log.logDebug("Contract Inference for the AwaitStmt");
    _log.beginIndent();
    Contract contract;
    Constraint c = _df.newConstraint();
    GroupName aprime = _df.newGroupName();
    // 1. First look if there is some annotation defined
    Iterator<Annotation> it = astmt.getAnnotations().iterator();
    if (it.hasNext()) {
        PureExp dep = it.next().getValue();
        ResultInferencePureExp resAnn = typeInferenceAsPure(dep);
        contract = _df.newContractAwait(astmt, aprime, _a);
        _log.endIndent();
        _log.logDebug("AwaitStmt Annotation Finished");
        c.addEquation(new ASTNodeInformation(astmt), (IRecord) resAnn.getVariableType(), createInstance(dep.getType(), _cd, aprime));
        return new ResultInferenceStmt(contract, c, _env);
    } else {
        ResultInferencePureExp resGuard = typeInference(astmt.getGuard());
        _log.endIndent();
        _log.logDebug("AwaitStmt Sub-Expression Finished");
        if (resGuard.getVariableType() instanceof TypingEnvironmentVariableTypeFuture) {
            ITypingEnvironmentFutureType z = _env.getFuture((TypingEnvironmentVariableTypeFuture) resGuard.getVariableType());
            if (z instanceof TypingEnvironmentFutureTypeUntick) {
                _env.putFuture((TypingEnvironmentVariableTypeFuture) resGuard.getVariableType(), new TypingEnvironmentFutureTypeTick(z.getRecord()));
                c.addEquation(new ASTNodeInformation(astmt), z.getRecord(), _df.newRecordFuture(aprime, _df.newRecordVariable()));
                contract = _df.newContract(_df.newContractElementParallel(_df.newContractInvkA(astmt, ((TypingEnvironmentFutureTypeUntick) z).getContract(), new ContractElementAwait(astmt, _a, aprime)), _env.unsync(astmt)));
            } else {
                try {
                    c.addEquation(new ASTNodeInformation(astmt), z.getRecord(), _df.newRecordFuture(aprime, _df.newRecordVariable()));
                } catch (Exception e) {
                    e.getMessage();
                }
                contract = _df.newContractEmpty();
            }
        } else {
            // the guard in the await is not a future.
            // maybe a boolean or something else, in any case, we cannot
            // manage it for now
            _log.logWarning("WARNING: the guard of the await statement is not a future. Assumed an empty contract");
            contract = _df.newContractEmpty();
        }
        return new ResultInferenceStmt(contract, c, _env);
    }
}
Also used : Constraint(com.gzoumix.semisolver.constraint.Constraint) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Constraint (com.gzoumix.semisolver.constraint.Constraint)10 LinkedList (java.util.LinkedList)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 InterfaceType (org.abs_models.frontend.typechecker.InterfaceType)1 KindedName (org.abs_models.frontend.typechecker.KindedName)1 Type (org.abs_models.frontend.typechecker.Type)1