Example #1
0
  @Override
  public SStmCG caseAForPatternBindStm(AForPatternBindStm node, IRInfo question)
      throws AnalysisException {
    // Example for mk_(a,b) in [mk_(1,2), mk_(3,4)] do skip;
    PPattern pattern = node.getPatternBind().getPattern();
    PExp exp = node.getExp();
    PStm stm = node.getStatement();
    Boolean reverse = node.getReverse();

    SPatternCG patternCg = pattern.apply(question.getPatternVisitor(), question);
    SExpCG seqExpCg = exp.apply(question.getExpVisitor(), question);
    SStmCG stmCg = stm.apply(question.getStmVisitor(), question);

    AForAllStmCG forAll = new AForAllStmCG();
    forAll.setPattern(patternCg);
    forAll.setBody(stmCg);

    if (reverse != null && reverse) {
      AReverseUnaryExpCG reversedExp = new AReverseUnaryExpCG();
      reversedExp.setType(seqExpCg.getType().clone());
      reversedExp.setExp(seqExpCg);
      forAll.setExp(reversedExp);
    } else {
      forAll.setExp(seqExpCg);
    }

    return forAll;
  }
Example #2
0
  @Override
  public SStmCG caseACaseAlternativeStm(ACaseAlternativeStm node, IRInfo question)
      throws AnalysisException {
    PPattern pattern = node.getPattern();
    PStm result = node.getResult();

    SPatternCG patternCg = pattern.apply(question.getPatternVisitor(), question);
    SStmCG resultCg = result.apply(question.getStmVisitor(), question);

    ACaseAltStmStmCG caseCg = new ACaseAltStmStmCG();
    caseCg.setPattern(patternCg);
    caseCg.setResult(resultCg);

    return caseCg;
  }
Example #3
0
  @Override
  public SStmCG caseAForAllStm(AForAllStm node, IRInfo question) throws AnalysisException {
    // Example: for all x in set {1,2,3} do skip;
    PPattern pattern = node.getPattern();
    PExp set = node.getSet();
    PStm body = node.getStatement();

    SPatternCG patternCg = pattern.apply(question.getPatternVisitor(), question);
    SExpCG setExpCg = set.apply(question.getExpVisitor(), question);
    SStmCG bodyCg = body.apply(question.getStmVisitor(), question);

    AForAllStmCG forAll = new AForAllStmCG();
    forAll.setPattern(patternCg);
    forAll.setExp(setExpCg);
    forAll.setBody(bodyCg);

    return forAll;
  }
  PExp buildPredicate(AImplicitOperationDefinition op, PDefinition stateDefinition)
      throws AnalysisException {
    List<PExp> arglist = new Vector<PExp>();

    for (APatternListTypePair pltp : op.getParameterPatterns()) {
      for (PPattern pattern : pltp.getPatterns()) {
        arglist.add(patternToExp(pattern.clone()));
      }
    }

    if (stateDefinition != null) {
      stateInPre(arglist, stateDefinition);
    }
    AApplyExp preApply = null;

    if (op.getPredef() != null) {
      preApply = getApplyExp(getVarExp(op.getPredef().getName().clone(), op.getPredef()), arglist);
      preApply.getRoot().setType(op.getPredef().getType().clone());
      preApply.setType(new ABooleanBasicType());
    }

    PExp mainExp;

    // Operation Has a Result. Add it in the post condition.
    if (op.getResult() != null) {

      AExistsExp existsExp = new AExistsExp();
      existsExp.setType(new ABooleanBasicType());
      List<PExp> postArglist = new Vector<PExp>(arglist);

      if (op.getResult().getPattern() instanceof AIdentifierPattern) {
        AIdentifierPattern ip = (AIdentifierPattern) op.getResult().getPattern();
        postArglist.add(patternToExp(op.getResult().getPattern().clone()));

        if (stateDefinition != null) {

          if (stateDefinition instanceof AStateDefinition) {
            AVariableExp varExp = getVarExp(OLD_STATE_ARG);
            varExp.setType(((AStateDefinition) stateDefinition).getRecordType().clone());
            postArglist.add(varExp);
            AVariableExp varExp2 = getVarExp(NEW_STATE_ARG);
            varExp2.setType(((AStateDefinition) stateDefinition).getRecordType().clone());
            postArglist.add(varExp2);
          } else {
            AVariableExp varExp = getVarExp(OLD_SELF_ARG);
            postArglist.add(varExp);
            varExp.setType(stateDefinition.getType().clone());
            AVariableExp varExp2 = getVarExp(NEW_SELF_ARG);
            postArglist.add(varExp2);
            varExp2.setType(stateDefinition.getType().clone());
          }
        }

        existsExp.setBindList(
            getMultipleTypeBindList(op.getResult().getType().clone(), ip.getName().clone()));
      } else {
        throw new RuntimeException("Expecting single identifier pattern in operation result");
      }

      AApplyExp postApply = getApplyExp(getVarExp(op.getPostdef().getName()), postArglist);
      postApply.getRoot().setType(op.getPostdef().getType().clone());
      postApply.setType(new ABooleanBasicType());
      existsExp.setPredicate(postApply);
      mainExp = existsExp;
    }

    // No Result. Just add new state to post condition
    else {

      AExistsExp exists_exp = new AExistsExp();
      exists_exp.setType(new ABooleanBasicType());
      List<PExp> postArglist = new Vector<PExp>(arglist);

      List<PMultipleBind> exists_binds = new LinkedList<PMultipleBind>();
      if (stateDefinition != null) {
        stateInPost(exists_binds, postArglist, stateDefinition);
      }
      exists_exp.setBindList(exists_binds);
      AApplyExp postApply =
          getApplyExp(getVarExp(op.getPostdef().getName()), new Vector<PExp>(postArglist));
      postApply.setType(new ABooleanBasicType());
      postApply.getRoot().setType(op.getPostdef().getType().clone());
      exists_exp.setPredicate(postApply);
      mainExp = exists_exp;
    }

    if (preApply != null) {
      return AstExpressionFactory.newAImpliesBooleanBinaryExp(preApply, mainExp);
    } else {
      return mainExp;
    }
  }