Ejemplo n.º 1
0
  /** Flatten complex expressions within the AST */
  public Node leave(Node old, Node n, NodeVisitor v) {
    if (n == noFlatten) {
      noFlatten = null;
      return n;
    }

    if (n instanceof Block) {
      List l = (List) stack.removeFirst();
      return ((Block) n).statements(l);
    } else if (n instanceof Stmt && !(n instanceof LocalDecl)) {
      List l = (List) stack.getFirst();
      l.add(n);
      return n;
    } else if (n instanceof Expr
        && !(n instanceof Lit)
        && !(n instanceof Special)
        && !(n instanceof Local)) {

      Expr e = (Expr) n;

      if (e instanceof Assign) {
        return n;
      }

      // create a local temp, initialized to the value of the complex
      // expression

      String name = newID();
      LocalDecl def =
          nf.LocalDecl(
              e.position(), Flags.FINAL, nf.CanonicalTypeNode(e.position(), e.type()), name, e);
      def = def.localInstance(ts.localInstance(e.position(), Flags.FINAL, e.type(), name));

      List l = (List) stack.getFirst();
      l.add(def);

      // return the local temp instead of the complex expression
      Local use = nf.Local(e.position(), name);
      use = (Local) use.type(e.type());
      use = use.localInstance(ts.localInstance(e.position(), Flags.FINAL, e.type(), name));
      return use;
    }

    return n;
  }
Ejemplo n.º 2
0
  /** Perform the appropriate flow operations for assignment to a local variable */
  protected Map flowLocalAssign(
      DataFlowItem inItem, FlowGraph graph, LocalAssign a, Set succEdgeKeys) {
    Local l = (Local) a.left();
    Map m = new HashMap(inItem.initStatus);
    MinMaxInitCount initCount = (MinMaxInitCount) m.get(l.localInstance());

    // initcount could be null if the local is defined in the outer
    // class, or if we have not yet seen its declaration (i.e. the
    // local is used in its own initialization)
    if (initCount == null) {
      initCount = new MinMaxInitCount(InitCount.ZERO, InitCount.ZERO);
    }

    initCount = new MinMaxInitCount(initCount.getMin().increment(), initCount.getMax().increment());

    m.put(l.localInstance(), initCount);
    return itemToMap(new DataFlowItem(m), succEdgeKeys);
  }
Ejemplo n.º 3
0
 /** Check that the local variable <code>l</code> is used correctly. */
 protected void checkLocal(FlowGraph graph, Local l, DataFlowItem dfIn, DataFlowItem dfOut)
     throws SemanticException {
   if (!currCBI.localDeclarations.contains(l.localInstance())) {
     // it's a local variable that has not been declared within
     // this scope. The only way this can arise is from an
     // inner class that is not a member of a class (typically
     // a local class, or an anonymous class declared in a method,
     // constructor or initializer).
     // We need to check that it is a final local, and also
     // keep track of it, to ensure that it has been definitely
     // assigned at this point.
     currCBI.outerLocalsUsed.add(l.localInstance());
   } else {
     MinMaxInitCount initCount = (MinMaxInitCount) dfIn.initStatus.get(l.localInstance());
     if (initCount != null && InitCount.ZERO.equals(initCount.getMin())) {
       // the local variable may not have been initialized.
       // However, we only want to complain if the local is reachable
       if (l.reachable()) {
         throw new SemanticException(
             "Local variable \"" + l.name() + "\" may not have been initialized", l.position());
       }
     }
   }
 }
Ejemplo n.º 4
0
  @Override
  public Node labelCheckLHS(LabelChecker lc) throws SemanticException {
    final Assign assign = (Assign) node();
    Local lve = (Local) assign.left();

    JifTypeSystem ts = lc.jifTypeSystem();
    JifContext A = lc.jifContext();
    A = (JifContext) lve.del().enterScope(A);

    List throwTypes = new ArrayList(assign.del().throwTypes(ts));

    final LocalInstance li = lve.localInstance();

    Label L = ts.labelOfLocal(li, A.pc());

    Expr rhs = (Expr) lc.context(A).labelCheck(assign.right());
    PathMap Xr = rhsPathMap(lc.context(A), rhs, throwTypes);
    PathMap X;

    if (assign.operator() != Assign.ASSIGN) {
      PathMap Xv = ts.pathMap();
      Xv = Xv.N(A.pc());
      Xv = Xv.NV(lc.upperBound(L, A.pc()));

      if (assign.throwsArithmeticException()) {
        Type arithExc = ts.ArithmeticException();
        checkAndRemoveThrowType(throwTypes, arithExc);
        X = Xv.join(Xr).exc(Xr.NV(), arithExc);
      } else {
        X = Xv.join(Xr);
      }
    } else {
      X = Xr;
    }

    lc.constrain(
        new NamedLabel(
            "rhs.nv", "label of successful evaluation of right hand of assignment", X.NV()),
        LabelConstraint.LEQ,
        new NamedLabel("label of var " + li.name(), L),
        A.labelEnv(),
        lve.position(),
        new LabelConstraintMessage() {
          @Override
          public String msg() {
            return "Label of right hand side not less "
                + "restrictive than the label for local variable "
                + li.name();
          }

          @Override
          public String detailMsg() {
            return "More information is revealed by the successful "
                + "evaluation of the right hand side of the "
                + "assignment than is allowed to flow to "
                + "the local variable "
                + li.name()
                + ".";
          }

          @Override
          public String technicalMsg() {
            return "Invalid assignment: path NV of rhs is "
                + "not less restrictive than the declared label "
                + "of the local variable <"
                + li.name()
                + ">.";
          }
        });

    if (JLiftOptions.getInstance().safeMutateConstraintSet) {
      // also constrain PC label
      lc.constrain(
          new NamedLabel("pc", "program counter label for assignment", A.pc()),
          LabelConstraint.LEQ,
          new NamedLabel("label of var " + li.name(), L),
          A.labelEnv(),
          lve.position(),
          new LabelConstraintMessage() {
            @Override
            public String msg() {
              return "Program counter not less "
                  + "restrictive than the label for local variable "
                  + li.name();
            }
          });
    }

    Expr lhs = (Expr) updatePathMap(lve, X);
    checkThrowTypes(throwTypes);
    return updatePathMap(assign.right(rhs).left(lhs), X);
  }
Ejemplo n.º 5
0
  private Node translateExtForArray(ExtendedFor n, List<String> labels) throws SemanticException {
    Position pos = Position.compilerGenerated();
    Type iteratedType = n.decl().type().type();
    // translate "L1,...,Ln: for (C x: e) b" to
    // "{ C[] arr = e; int iter = 0;  L1,...,Ln: while (iter < arr.length)  { C x = arr[iter]; b ;
    // iter = iter + 1; }"
    List<Stmt> stmts = new ArrayList<Stmt>();

    // add the declaration of arr: "C[] arr = e"
    Id arrID = freshName("arr");
    LocalInstance arrLI = ts.localInstance(pos, Flags.NONE, n.expr().type(), arrID.id());
    {
      LocalDecl ld =
          nodeFactory()
              .LocalDecl(
                  pos, Flags.NONE, nodeFactory().CanonicalTypeNode(pos, arrLI.type()), arrID);
      ld = ld.localInstance(arrLI);
      ld = ld.init(n.expr());
      stmts.add(ld);
    }

    // add the declaration of iterator: "int iter = 0"
    Id iterID = freshName("iter");
    LocalInstance iterLI = ts.localInstance(pos, Flags.NONE, ts.Int(), iterID.id());
    {
      LocalDecl ld =
          nodeFactory()
              .LocalDecl(
                  pos, Flags.NONE, nodeFactory().CanonicalTypeNode(pos, iterLI.type()), iterID);
      ld = ld.localInstance(iterLI);
      ld = ld.init(nodeFactory().IntLit(pos, IntLit.INT, 0).type(ts.Int()));
      stmts.add(ld);
    }

    // build the conditional "iter < arr.length"
    Expr cond;
    {
      Local iterLocal =
          (Local) nodeFactory().Local(pos, iterID).localInstance(iterLI).type(ts.Int());
      Local arrLocal =
          (Local) nodeFactory().Local(pos, arrID).localInstance(arrLI).type(arrLI.type());
      Id id = nodeFactory().Id(pos, "length");
      Field field = (Field) nodeFactory().Field(pos, arrLocal, id).type(ts.Int());
      field = field.fieldInstance(ts.findField(arrLI.type().toReference(), "length"));

      cond = nodeFactory().Binary(pos, iterLocal, Binary.LT, field).type(ts.Boolean());
    }

    // build the initlizer for the local decl: arr[iter]
    Expr init;
    {
      Local iterLocal =
          (Local) nodeFactory().Local(pos, iterID).localInstance(iterLI).type(ts.Int());
      Local arrLocal =
          (Local) nodeFactory().Local(pos, arrID).localInstance(arrLI).type(arrLI.type());
      init = nodeFactory().ArrayAccess(pos, arrLocal, iterLocal);
      init = init.type(iteratedType);
    }

    // build the increment for iter (iter = iter + 1;)
    Stmt inc;
    {
      Local iterLocal =
          (Local) nodeFactory().Local(pos, iterID).localInstance(iterLI).type(ts.Int());
      Expr incExpr =
          nodeFactory()
              .Binary(
                  pos,
                  iterLocal.type(ts.Int()),
                  Binary.ADD,
                  nodeFactory().IntLit(pos, IntLit.INT, 1).type(ts.Int()))
              .type(ts.Int());
      Assign incStore =
          (Assign) nodeFactory().Assign(pos, iterLocal, Assign.ASSIGN, incExpr).type(ts.Int());
      inc = nodeFactory().Eval(pos, incStore);
    }

    // build the while loop
    {
      // Create a new loop body from the old body followed by the increment
      Block loopBody = nodeFactory().Block(pos, n.decl().init(init), n.body(), inc);
      While loop = nodeFactory().While(pos, cond, loopBody);
      stmts.add(labelStmt(loop, labels));
    }
    return nodeFactory().Block(pos, stmts);
  }