Example #1
0
  @Override
  public SStmCG caseAReturnStm(AReturnStm node, IRInfo question) throws AnalysisException {
    PExp exp = node.getExpression();

    AExplicitOperationDefinition operation = node.getAncestor(AExplicitOperationDefinition.class);

    if (operation != null && operation.getIsConstructor()) {
      if (exp instanceof ASelfExp) {
        // The expression of the return statement points to 'null' since the OO AST
        // does not allow constructors to return references to explicitly
        // created types. Simply 'returning' in a constructor means returning
        // a reference for the object currently being created.
        return new AReturnStmCG();
      } else {
        question.addUnsupportedNode(
            operation,
            "Unexpected expression returned by constructor: Values expliclty returned by constructors must be 'self'.");
        return null;
      }
    }

    AReturnStmCG returnStm = new AReturnStmCG();

    if (exp != null) {
      SExpCG expCg = exp.apply(question.getExpVisitor(), question);
      returnStm.setExp(expCg);
    }

    return returnStm;
  }
Example #2
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;
  }
  @Test
  public void test() throws IOException, AnalysisException {
    // Assume.assumeTrue(false);// comment this when the tests parse
    PExp exp = null;

    try {
      exp = parse(expression);
    } catch (AnalysisException e) {
      System.err.println(
          "Line: " + lineIndex + " Remove this expression it is invalid: " + expression);
      return;
      // Assert.fail("Invalid expression: " + expression);
    }

    VdmToBConverter translator = new VdmToBConverter();
    try {
      exp.apply(translator);

      if (!translator.unsupportedConstructs.isEmpty()) {
        Assert.fail("Missing implementation for: " + translator.unsupportedConstructs);
      }
    } catch (Exception e) {
      Assert.fail("translation error");
    }
  }
Example #4
0
  @Override
  public SStmCG caseAStartStm(AStartStm node, IRInfo question) throws AnalysisException {
    PType type = node.getType();
    PExp exp = node.getObj();

    if (exp.getType() instanceof ASetType) {

      STypeCG typeCG = type.apply(question.getTypeVisitor(), question);
      SExpCG expCG = exp.apply(question.getExpVisitor(), question);

      AStartlistStmCG s = new AStartlistStmCG();
      s.setType(typeCG);
      s.setExp(expCG);

      return s;
    } else {
      STypeCG typeCG = type.apply(question.getTypeVisitor(), question);
      SExpCG expCG = exp.apply(question.getExpVisitor(), question);

      AStartStmCG thread = new AStartStmCG();
      thread.setType(typeCG);
      thread.setExp(expCG);

      return thread;
    }
  }
Example #5
0
  @Override
  public SStmCG caseACallObjectStm(ACallObjectStm node, IRInfo question) throws AnalysisException {
    PType type = node.getType();
    PObjectDesignator objectDesignator = node.getDesignator();
    ILexNameToken field = node.getField();
    LinkedList<PExp> args = node.getArgs();

    STypeCG typeCg = type.apply(question.getTypeVisitor(), question);
    SObjectDesignatorCG objectDesignatorCg =
        objectDesignator.apply(question.getObjectDesignatorVisitor(), question);

    if (node.getExplicit()) {
      SClassDefinition enclosingClass = node.getAncestor(SClassDefinition.class);

      if (enclosingClass != null) {
        if (!field.getModule().equals(enclosingClass.getName().getName())) {
          // A quoted method call is only supported if the explicit
          // module name is equal to that of the enclosing class. Say A
          // is a sub class of S and 'a' is an instance of A then a.A`op();
          //  is allowed (although it is the same as a.op()). However,
          // a.S`op(); is not allowed.
          question.addUnsupportedNode(
              node,
              "A quoted object call statement is only supported if the explicit module name is equal to that of the enclosing class");
        }
      } else {
        Logger.getLog()
            .printErrorln(
                "Could not find enclosing the statement of call a call object statement.");
      }
    }

    String fieldNameCg = field.getName();

    ACallObjectStmCG callObject = new ACallObjectStmCG();
    callObject.setType(typeCg);
    callObject.setDesignator(objectDesignatorCg);
    callObject.setFieldName(fieldNameCg);

    for (PExp arg : args) {
      SExpCG argCg = arg.apply(question.getExpVisitor(), question);

      if (argCg != null) {
        callObject.getArgs().add(argCg);
      } else {
        return null;
      }
    }

    return callObject;
  }
Example #6
0
  @Override
  public SStmCG caseAWhileStm(AWhileStm node, IRInfo question) throws AnalysisException {
    PStm stm = node.getStatement();
    PExp exp = node.getExp();

    SStmCG bodyCg = stm.apply(question.getStmVisitor(), question);
    SExpCG expCg = exp.apply(question.getExpVisitor(), question);

    AWhileStmCG whileStm = new AWhileStmCG();
    whileStm.setExp(expCg);
    whileStm.setBody(bodyCg);

    return whileStm;
  }
Example #7
0
  @Override
  public SStmCG caseAAssignmentStm(AAssignmentStm node, IRInfo question) throws AnalysisException {
    PStateDesignator target = node.getTarget();
    PExp exp = node.getExp();

    SStateDesignatorCG targetCg = target.apply(question.getStateDesignatorVisitor(), question);
    SExpCG expCg = exp.apply(question.getExpVisitor(), question);

    AAssignmentStmCG assignment = new AAssignmentStmCG();
    assignment.setTarget(targetCg);
    assignment.setExp(expCg);

    return assignment;
  }
Example #8
0
  @Override
  public PExp getContextNode(PExp stitch) {

    PExp r = null;
    try {
      if (first) {
        r = stitch.apply(visitor, subLast);
        first = false;
      } else {
        r = stitch.apply(visitor, sub);
      }
      return r;
    } catch (AnalysisException e) {
      e.printStackTrace();
      return null;
    }
  }
  public TupleSelectObligation(PExp exp, PType type, IPOContextStack ctxt, IPogAssistantFactory af)
      throws AnalysisException {
    // not is_(exp, type)
    super(exp, POType.TUPLE_SELECT, ctxt, exp.getLocation(), af);

    ANotUnaryExp notExp = new ANotUnaryExp();
    AIsExp isExp = new AIsExp();

    isExp.setTest(exp.clone());
    isExp.setBasicType(
        type.clone()); // Do we need the type definition instead? If so, the visitor must provide
    // it.

    notExp.setExp(isExp);

    stitch = notExp;
    valuetree.setPredicate(ctxt.getPredWithContext(stitch));
  }
Example #10
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;
  }
Example #11
0
  @Override
  public SStmCG caseABlockSimpleBlockStm(ABlockSimpleBlockStm node, IRInfo question)
      throws AnalysisException {
    ABlockStmCG blockStm = new ABlockStmCG();
    blockStm.setScoped(question.getStmAssistant().isScoped(node));

    LinkedList<AAssignmentDefinition> assignmentDefs = node.getAssignmentDefs();

    for (AAssignmentDefinition def : assignmentDefs) {
      PType type = def.getType();
      String name = def.getName().getName();
      PExp exp = def.getExpression();

      STypeCG typeCg = type.apply(question.getTypeVisitor(), question);
      AIdentifierPatternCG idPattern = new AIdentifierPatternCG();
      idPattern.setName(name);
      SExpCG expCg = exp.apply(question.getExpVisitor(), question);

      AVarDeclCG localDecl =
          question.getDeclAssistant().consLocalVarDecl(def, typeCg, idPattern, expCg);

      if (expCg instanceof AUndefinedExpCG) {
        question.getDeclAssistant().setDefaultValue(localDecl, typeCg);
      } else {
        localDecl.setExp(expCg);
      }

      blockStm.getLocalDefs().add(localDecl);
    }

    LinkedList<PStm> stms = node.getStatements();

    for (PStm pStm : stms) {
      SStmCG stmCg = pStm.apply(question.getStmVisitor(), question);

      if (stmCg != null) {
        blockStm.getStatements().add(stmCg);
      } else {
        return null;
      }
    }

    return blockStm;
  }
Example #12
0
  @Override
  public SStmCG caseACasesStm(ACasesStm node, IRInfo question) throws AnalysisException {
    PExp exp = node.getExp();
    PStm others = node.getOthers();
    LinkedList<ACaseAlternativeStm> cases = node.getCases();

    SExpCG expCg = exp.apply(question.getExpVisitor(), question);
    SStmCG othersCg = others != null ? others.apply(question.getStmVisitor(), question) : null;

    ACasesStmCG casesStmCg = new ACasesStmCG();
    casesStmCg.setExp(expCg);
    casesStmCg.setOthers(othersCg);

    question
        .getStmAssistant()
        .handleAlternativesCasesStm(question, exp, cases, casesStmCg.getCases());

    return casesStmCg;
  }
Example #13
0
  @Override
  public SStmCG caseAPeriodicStm(APeriodicStm node, IRInfo question) throws AnalysisException {
    String opName = node.getOpname().getName();

    APeriodicStmCG periodicStmCg = new APeriodicStmCG();
    periodicStmCg.setOpname(opName);

    for (PExp exp : node.getArgs()) {
      SExpCG expCg = exp.apply(question.getExpVisitor(), question);

      if (expCg != null) {
        periodicStmCg.getArgs().add(expCg);
      } else {
        return null;
      }
    }

    return periodicStmCg;
  }
Example #14
0
  @Override
  public SStmCG caseALetBeStStm(ALetBeStStm node, IRInfo question) throws AnalysisException {
    PMultipleBind multipleBind = node.getBind();

    if (!(multipleBind instanceof ASetMultipleBind)) {
      question.addUnsupportedNode(
          node,
          "Generation of the let be st statement is only supported for a multiple set bind. Got: "
              + multipleBind);
      return null;
    }

    ASetMultipleBind multipleSetBind = (ASetMultipleBind) multipleBind;

    SMultipleBindCG multipleBindCg =
        multipleSetBind.apply(question.getMultipleBindVisitor(), question);

    if (!(multipleBindCg instanceof ASetMultipleBindCG)) {
      return null;
    }

    ASetMultipleBindCG multipleSetBindCg = (ASetMultipleBindCG) multipleBindCg;

    PExp suchThat = node.getSuchThat();
    PStm stm = node.getStatement();

    SExpCG suchThatCg =
        suchThat != null ? suchThat.apply(question.getExpVisitor(), question) : null;
    SStmCG stmCg = stm.apply(question.getStmVisitor(), question);

    ALetBeStStmCG letBeSt = new ALetBeStStmCG();

    AHeaderLetBeStCG header = question.getExpAssistant().consHeader(multipleSetBindCg, suchThatCg);

    letBeSt.setHeader(header);
    letBeSt.setStatement(stmCg);

    return letBeSt;
  }
Example #15
0
  @Override
  public SStmCG caseAForIndexStm(AForIndexStm node, IRInfo question) throws AnalysisException {
    ILexNameToken var = node.getVar();
    PExp from = node.getFrom();
    PExp to = node.getTo();
    PExp by = node.getBy();
    PStm stm = node.getStatement();

    String varCg = var.getName();
    SExpCG fromCg = from.apply(question.getExpVisitor(), question);
    SExpCG toCg = to.apply(question.getExpVisitor(), question);
    SExpCG byCg = by != null ? by.apply(question.getExpVisitor(), question) : null;
    SStmCG bodyCg = stm.apply(question.getStmVisitor(), question);

    AForIndexStmCG forStm = new AForIndexStmCG();
    forStm.setVar(varCg);
    forStm.setFrom(fromCg);
    forStm.setTo(toCg);
    forStm.setBy(byCg);
    forStm.setBody(bodyCg);

    return forStm;
  }
Example #16
0
  @Override
  public SStmCG caseACallStm(ACallStm node, IRInfo question) throws AnalysisException {
    PDefinition rootdef = node.getRootdef();
    LinkedList<PExp> args = node.getArgs();

    List<SExpCG> argsCg = new LinkedList<SExpCG>();

    for (PExp arg : args) {
      SExpCG argCg = arg.apply(question.getExpVisitor(), question);

      if (argCg != null) {
        argsCg.add(argCg);
      } else {
        return null;
      }
    }

    boolean isStaticOrSl =
        Settings.dialect == Dialect.VDM_SL
            || question.getTcFactory().createPDefinitionAssistant().isStatic(rootdef);

    while (rootdef instanceof AInheritedDefinition) {
      rootdef = ((AInheritedDefinition) rootdef).getSuperdef();
    }

    if (rootdef instanceof AExplicitOperationDefinition) {
      AExplicitOperationDefinition op = (AExplicitOperationDefinition) rootdef;

      if (op.getIsConstructor()) {
        String initName = question.getObjectInitializerCall(op);

        APlainCallStmCG callStm = new APlainCallStmCG();
        callStm.setType(new AVoidTypeCG());
        callStm.setClassType(null);
        callStm.setName(initName);
        callStm.setArgs(argsCg);

        return callStm;
      }
    }

    PType type = node.getType();
    ILexNameToken nameToken = node.getName();
    String name = nameToken.getName();

    AClassTypeCG classType = null;

    STypeCG typeCg = type.apply(question.getTypeVisitor(), question);

    if (!isStaticOrSl) {
      ILexNameToken rootDefClassName = node.getRootdef().getClassDefinition().getName();
      ILexNameToken enclosingClassName = node.getAncestor(SClassDefinition.class).getName();

      if (!rootDefClassName.equals(enclosingClassName)) {

        ASuperCallStmCG superCall = new ASuperCallStmCG();
        superCall.setIsStatic(isStaticOrSl);
        superCall.setType(typeCg);
        superCall.setName(name);
        superCall.setArgs(argsCg);

        return superCall;
      }
    } else if (nameToken != null && nameToken.getExplicit() && isStaticOrSl) {
      String className = nameToken.getModule();
      classType = new AClassTypeCG();
      classType.setName(className);
    }

    APlainCallStmCG callStm = new APlainCallStmCG();

    callStm.setType(typeCg);
    callStm.setIsStatic(isStaticOrSl);
    callStm.setName(name);
    callStm.setClassType(classType);
    callStm.setArgs(argsCg);

    return callStm;
  }
 @Override
 public CmlProofObligationList defaultPExp(PExp node, IPOContextStack question)
     throws AnalysisException {
   return node.apply(this.expressionVisitor, question);
 }