private AMethodDeclCG consEqualsMethod(String name) {
    AIdentifierVarExpCG paramVar =
        transAssistant
            .getInfo()
            .getExpAssistant()
            .consIdVar(EQUALS_METHOD_PARAM, new AObjectTypeCG());

    AClassTypeCG quoteClass = new AClassTypeCG();
    quoteClass.setName(name);

    AInstanceofExpCG instanceCheck = new AInstanceofExpCG();
    instanceCheck.setType(new ABoolBasicTypeCG());
    instanceCheck.setExp(paramVar);
    instanceCheck.setCheckedType(quoteClass);

    AReturnStmCG checkReturned = new AReturnStmCG();
    checkReturned.setExp(instanceCheck);

    AMethodDeclCG equalsMethod = consEqualMethodSignature(EQUALS_METHOD_PARAM);

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

    equalsMethod.setBody(body);

    return equalsMethod;
  }
  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;
  }
  private AMethodDeclCG consQuoteCtor(String name) {
    AExternalTypeCG fieldType = new AExternalTypeCG();
    fieldType.setName(IJavaConstants.INT);

    AIdentifierVarExpCG hashcodeVar =
        transAssistant.getInfo().getExpAssistant().consIdVar(HASHCODE_FIELD, fieldType);

    AEqualsBinaryExpCG hashcodeCompare = new AEqualsBinaryExpCG();
    hashcodeCompare.setType(new ABoolBasicTypeCG());
    hashcodeCompare.setLeft(hashcodeVar);
    hashcodeCompare.setRight(consZero());

    AIdentifierVarExpCG hashCodeId =
        transAssistant.getInfo().getExpAssistant().consIdVar(HASHCODE_FIELD, consFieldType());

    AMethodTypeCG hashCodeMethodType = new AMethodTypeCG();
    hashCodeMethodType.setResult(consFieldType());

    ASuperVarExpCG superVar = new ASuperVarExpCG();
    superVar.setName(HASH_CODE_METHOD);
    superVar.setType(hashCodeMethodType);
    superVar.setIsLambda(false);
    superVar.setIsLocal(false);

    AApplyExpCG superCall = new AApplyExpCG();
    superCall.setType(consFieldType());
    superCall.setRoot(superVar);

    AAssignToExpStmCG assignHashcode = new AAssignToExpStmCG();
    assignHashcode.setTarget(hashCodeId);
    assignHashcode.setExp(superCall);

    AIfStmCG hashcodeCheck = new AIfStmCG();
    hashcodeCheck.setIfExp(hashcodeCompare);
    hashcodeCheck.setThenStm(assignHashcode);

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

    AClassTypeCG quoteClassType = new AClassTypeCG();
    quoteClassType.setName(name);

    AMethodTypeCG constructorType = new AMethodTypeCG();
    constructorType.setResult(quoteClassType);

    AMethodDeclCG ctor = consDefaultCtorSignature(name);
    ctor.setMethodType(constructorType);
    ctor.setBody(body);

    return ctor;
  }
Example #4
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;
  }
  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;
  }
  private AMethodDeclCG consHashcodeMethod() {
    AIdentifierVarExpCG hashCodeVar =
        transAssistant.getInfo().getExpAssistant().consIdVar(HASHCODE_FIELD, consFieldType());

    AReturnStmCG returnHashCode = new AReturnStmCG();
    returnHashCode.setExp(hashCodeVar);

    AMethodDeclCG hashCodeMethod = consHashcodeMethodSignature();

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

    hashCodeMethod.setBody(body);

    return hashCodeMethod;
  }
Example #7
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;
  }