Esempio n. 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;
  }
Esempio n. 2
0
  @Override
  public List<PDefinition> caseAExplicitOperationDefinition(AExplicitOperationDefinition node)
      throws AnalysisException {
    List<PDefinition> defs = new Vector<PDefinition>();
    defs.add(node);

    if (node.getPredef() != null) {
      defs.add(node.getPredef());
    }

    if (node.getPostdef() != null) {
      defs.add(node.getPostdef());
    }

    return defs;
  }
Esempio n. 3
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;
  }