예제 #1
0
  @Override
  public SStmCG caseAIfStm(AIfStm node, IRInfo question) throws AnalysisException {
    SExpCG ifExp = node.getIfExp().apply(question.getExpVisitor(), question);
    SStmCG thenStm = node.getThenStm().apply(question.getStmVisitor(), question);

    AIfStmCG ifStm = new AIfStmCG();

    ifStm.setIfExp(ifExp);
    ifStm.setThenStm(thenStm);

    LinkedList<AElseIfStm> elseIfs = node.getElseIf();

    for (AElseIfStm stm : elseIfs) {
      ifExp = stm.getElseIf().apply(question.getExpVisitor(), question);
      thenStm = stm.getThenStm().apply(question.getStmVisitor(), question);

      AElseIfStmCG elseIfStm = new AElseIfStmCG();
      elseIfStm.setElseIf(ifExp);
      elseIfStm.setThenStm(thenStm);

      ifStm.getElseIf().add(elseIfStm);
    }

    if (node.getElseStm() != null) {
      SStmCG elseStm = node.getElseStm().apply(question.getStmVisitor(), question);
      ifStm.setElseStm(elseStm);
    }

    return ifStm;
  }
예제 #2
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;
  }
예제 #3
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;
  }
예제 #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;
    }
  }
예제 #5
0
  @Override
  public SStmCG caseAClassInvariantStm(AClassInvariantStm node, IRInfo question)
      throws AnalysisException {
    List<PExp> exps = new LinkedList<PExp>();

    for (PDefinition d : node.getInvDefs()) {
      if (!(d instanceof AClassInvariantDefinition)) {
        Logger.getLog()
            .printErrorln(
                "Expected class invariant definition in '"
                    + this.getClass().getName()
                    + "'. Got: "
                    + d);
        return null;
      }

      AClassInvariantDefinition invDef = (AClassInvariantDefinition) d;
      exps.add(invDef.getExpression());
    }

    AReturnStmCG returnStmCg = new AReturnStmCG();

    if (exps.isEmpty()) {
      // Should not really be necessary
      returnStmCg.setExp(question.getExpAssistant().consBoolLiteral(true));
    } else if (exps.size() == 1) {
      SExpCG expCg = exps.get(0).apply(question.getExpVisitor(), question);
      returnStmCg.setExp(expCg);
    } else {
      // We have more than one expressions from which we will build an 'and chain'
      AAndBoolBinaryExpCG andExpTopCg = new AAndBoolBinaryExpCG();
      andExpTopCg.setType(new ABoolBasicTypeCG());
      andExpTopCg.setLeft(exps.get(0).apply(question.getExpVisitor(), question));

      AAndBoolBinaryExpCG previousAndExpCg = andExpTopCg;

      // The remaining ones except the last
      for (int i = 1; i < exps.size() - 1; i++) {
        SExpCG nextExpCg = exps.get(i).apply(question.getExpVisitor(), question);

        AAndBoolBinaryExpCG nextAndExpCg = new AAndBoolBinaryExpCG();
        nextAndExpCg.setType(new ABoolBasicTypeCG());
        nextAndExpCg.setLeft(nextExpCg);

        previousAndExpCg.setRight(nextAndExpCg);
        previousAndExpCg = nextAndExpCg;
      }

      previousAndExpCg.setRight(
          exps.get(exps.size() - 1).apply(question.getExpVisitor(), question));

      returnStmCg.setExp(andExpTopCg);
    }

    return returnStmCg;
  }
예제 #6
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;
  }
예제 #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;
  }
예제 #8
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;
  }
예제 #9
0
  @Override
  public SStmCG caseALetStm(ALetStm node, IRInfo question) throws AnalysisException {
    ABlockStmCG block = new ABlockStmCG();
    block.setScoped(question.getStmAssistant().isScoped(node));

    question.getDeclAssistant().setLocalDefs(node.getLocalDefs(), block.getLocalDefs(), question);

    SStmCG stm = node.getStatement().apply(question.getStmVisitor(), question);

    if (stm != null) {
      block.getStatements().add(stm);
    }

    return block;
  }
예제 #10
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;
  }
예제 #11
0
  private AMethodDeclCG consGetInstanceMethod(String name) {
    AClassTypeCG quoteClassType = new AClassTypeCG();
    quoteClassType.setName(name);

    AIdentifierVarExpCG instanceVar =
        transAssistant.getInfo().getExpAssistant().consIdVar(INSTANCE_FIELD, quoteClassType);

    AEqualsBinaryExpCG nullCompare = new AEqualsBinaryExpCG();
    nullCompare.setType(new ABoolBasicTypeCG());
    nullCompare.setLeft(instanceVar);
    nullCompare.setRight(info.getExpAssistant().consNullExp());

    AIdentifierVarExpCG instanceId =
        transAssistant
            .getInfo()
            .getExpAssistant()
            .consIdVar(INSTANCE_FIELD, quoteClassType.clone());

    ATypeNameCG typeName = new ATypeNameCG();
    typeName.setDefiningClass(null);
    typeName.setName(name);

    ANewExpCG newQuote = new ANewExpCG();
    newQuote.setName(typeName);
    newQuote.setType(quoteClassType);

    AAssignToExpStmCG assignInstance = new AAssignToExpStmCG();
    assignInstance.setTarget(instanceId);
    assignInstance.setExp(newQuote);

    AIfStmCG ensureInstance = new AIfStmCG();
    ensureInstance.setIfExp(nullCompare);
    ensureInstance.setThenStm(assignInstance);

    AReturnStmCG returnInstance = new AReturnStmCG();
    returnInstance.setExp(instanceVar.clone());

    ABlockStmCG body = new ABlockStmCG();
    body.getStatements().add(ensureInstance);
    body.getStatements().add(returnInstance);

    AMethodTypeCG methodType = new AMethodTypeCG();
    methodType.setResult(quoteClassType.clone());

    AMethodDeclCG getInstanceMethod = new AMethodDeclCG();

    getInstanceMethod.setImplicit(false);
    getInstanceMethod.setAbstract(false);
    getInstanceMethod.setAccess(IJavaConstants.PUBLIC);
    getInstanceMethod.setIsConstructor(false);
    getInstanceMethod.setName(GET_INSTANCE_METHOD);
    getInstanceMethod.setStatic(true);

    getInstanceMethod.setMethodType(methodType);
    getInstanceMethod.setBody(body);

    return getInstanceMethod;
  }
예제 #12
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;
  }
예제 #13
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;
  }
예제 #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;
  }
예제 #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;
  }
예제 #16
0
  private AFieldDeclCG consInstanceField(String name) {
    AClassTypeCG quoteClassType = new AClassTypeCG();
    quoteClassType.setName(name);

    AFieldDeclCG field = new AFieldDeclCG();
    field.setAccess(IJavaConstants.PRIVATE);
    field.setVolatile(false);
    field.setFinal(false);
    field.setStatic(true);
    field.setName(INSTANCE_FIELD);
    field.setType(quoteClassType);
    field.setInitial(info.getExpAssistant().consNullExp());

    return field;
  }
예제 #17
0
  private AMethodDeclCG consToStringMethod(String name) {
    SExpCG stringLit = info.getExpAssistant().consStringLiteral("<" + name + ">", false);

    AReturnStmCG returnStr = new AReturnStmCG();
    returnStr.setExp(stringLit);

    AMethodDeclCG toStringMethod = consToStringSignature();

    ABlockStmCG body = new ABlockStmCG();
    body.getStatements().add(returnStr);

    toStringMethod.setBody(body);

    return toStringMethod;
  }
예제 #18
0
  @Override
  public SStmCG caseAAtomicStm(AAtomicStm node, IRInfo question) throws AnalysisException {
    AAtomicStmCG atomicBlock = new AAtomicStmCG();

    for (AAssignmentStm assignment : node.getAssignments()) {
      SStmCG stmCg = assignment.apply(question.getStmVisitor(), question);

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

    return atomicBlock;
  }
예제 #19
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;
  }
예제 #20
0
  public STypeCG toIrType(IRInfo info) {
    try {
      STypeCG irType = type.apply(info.getTypeVisitor(), info);

      if (irType != null) {
        irType.setOptional(optional);
      }

      return irType;
    } catch (AnalysisException e) {
      Logger.getLog()
          .printErrorln(
              "Problems encountered while attempting "
                  + "to construct the IR type from a VDM type: "
                  + e.getMessage()
                  + " in '"
                  + this.getClass().getSimpleName()
                  + "'");
      e.printStackTrace();
    }

    return null;
  }
예제 #21
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;
  }
예제 #22
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;
  }