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;
  }
示例#2
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;
  }
  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;
  }
  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;
  }
示例#5
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;
  }