Home » openjdk-7 » com.sun.tools » javac » comp » [javadoc | source]
com.sun.tools.javac.comp
public class: Flow [javadoc | source]
java.lang.Object
   com.sun.tools.javac.tree.JCTree.Visitor
      com.sun.tools.javac.tree.TreeScanner
         com.sun.tools.javac.comp.Flow
This pass implements dataflow analysis for Java programs. Liveness analysis checks that every statement is reachable. Exception analysis ensures that every checked exception that is thrown is declared or caught. Definite assignment analysis ensures that each variable is assigned when used. Definite unassignment analysis ensures that no final variable is assigned more than once.

The JLS has a number of problems in the specification of these flow analysis problems. This implementation attempts to address those issues.

First, there is no accommodation for a finally clause that cannot complete normally. For liveness analysis, an intervening finally clause can cause a break, continue, or return not to reach its target. For exception analysis, an intervening finally clause can cause any exception to be "caught". For DA/DU analysis, the finally clause can prevent a transfer of control from propagating DA/DU state to the target. In addition, code in the finally clause can affect the DA/DU status of variables.

For try statements, we introduce the idea of a variable being definitely unassigned "everywhere" in a block. A variable V is "unassigned everywhere" in a block iff it is unassigned at the beginning of the block and there is no reachable assignment to V in the block. An assignment V=e is reachable iff V is not DA after e. Then we can say that V is DU at the beginning of the catch block iff V is DU everywhere in the try block. Similarly, V is DU at the beginning of the finally block iff V is DU everywhere in the try block and in every catch block. Specifically, the following bullet is added to 16.2.2

     V is unassigned everywhere in a block if it is
     unassigned before the block and there is no reachable
     assignment to V within the block.
 

In 16.2.15, the third bullet (and all of its sub-bullets) for all try blocks is changed to

     V is definitely unassigned before a catch block iff V is
     definitely unassigned everywhere in the try block.
 

The last bullet (and all of its sub-bullets) for try blocks that have a finally block is changed to

     V is definitely unassigned before the finally block iff
     V is definitely unassigned everywhere in the try block
     and everywhere in each catch block of the try statement.
 

In addition,

     V is definitely assigned at the end of a constructor iff
     V is definitely assigned after the block that is the body
     of the constructor and V is definitely assigned at every
     return that can return from the constructor.
 

In addition, each continue statement with the loop as its target is treated as a jump to the end of the loop body, and "intervening" finally clauses are treated as follows: V is DA "due to the continue" iff V is DA before the continue statement or V is DA at the end of any intervening finally block. V is DU "due to the continue" iff any intervening finally cannot complete normally or V is DU at the end of every intervening finally block. This "due to the continue" concept is then used in the spec for the loops.

Similarly, break statements must consider intervening finally blocks. For liveness analysis, a break statement for which any intervening finally cannot complete normally is not considered to cause the target statement to be able to complete normally. Then we say V is DA "due to the break" iff V is DA before the break or V is DA at the end of any intervening finally block. V is DU "due to the break" iff any intervening finally cannot complete normally or V is DU at the break and at the end of every intervening finally block. (I suspect this latter condition can be simplified.) This "due to the break" is then used in the spec for all statements that can be "broken".

The return statement is treated similarly. V is DA "due to a return statement" iff V is DA before the return statement or V is DA at the end of any intervening finally block. Note that we don't have to worry about the return expression because this concept is only used for construcrors.

There is no spec in the JLS for when a variable is definitely assigned at the end of a constructor, which is needed for final fields (8.3.1.2). We implement the rule that V is DA at the end of the constructor iff it is DA and the end of the body of the constructor and V is DA "due to" every return of the constructor.

Intervening finally blocks similarly affect exception analysis. An intervening finally that cannot complete normally allows us to ignore an otherwise uncaught exception.

To implement the semantics of intervening finally clauses, all nonlocal transfers (break, continue, return, throw, method call that can throw a checked exception, and a constructor invocation that can thrown a checked exception) are recorded in a queue, and removed from the queue when we complete processing the target of the nonlocal transfer. This allows us to modify the queue in accordance with the above rules when we encounter a finally clause. The only exception to this [no pun intended] is that checked exceptions that are known to be caught or declared to be caught in the enclosing method are not recorded in the queue, but instead are recorded in a global variable "Set thrown" that records the type of all exceptions that can be thrown.

Other minor issues the treatment of members of other classes (always considered DA except that within an anonymous class constructor, where DA status from the enclosing scope is preserved), treatment of the case expression (V is DA before the case expression iff V is DA after the switch expression), treatment of variables declared in a switch block (the implied DA/DU status after the switch expression is DU and not DA for variables defined in a switch block), the treatment of boolean ?: expressions (The JLS rules only handle b and c non-boolean; the new rule is that if b and c are boolean valued, then V is (un)assigned after a?b:c when true/false iff V is (un)assigned after b when true/false and V is (un)assigned after c when true/false).

There is the remaining question of what syntactic forms constitute a reference to a variable. It is conventional to allow this.x on the left-hand-side to initialize a final instance field named x, yet this.x isn't considered a "use" when appearing on a right-hand-side in most implementations. Should parentheses affect what is considered a variable reference? The simplest rule would be to allow unqualified forms only, parentheses optional, and phase out support for assigning to a final field via this.x.

This is NOT part of any supported API. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice.
Nested Class Summary:
static class  Flow.PendingExit  A pending exit. These are the statements return, break, and continue. In addition, exception-throwing expressions or statements are put here when not known to be caught. This will typically result in an error unless it is within a try-finally whose finally block cannot complete normally. 
Field Summary
protected static final  Key<Flow> flowKey     
 Bits inits    The set of definitely assigned variables. 
 Bits uninits    The set of definitely unassigned variables. 
 HashMap<Type> preciseRethrowTypes     
 Bits uninitsTry    The set of variables that are definitely unassigned everywhere in current try block. This variable is maintained lazily; it is updated only when something gets removed from uninits, typically by being assigned in reachable code. To obtain the correct set of variables which are definitely unassigned anywhere in current try block, intersect uninitsTry and uninits. 
 Bits initsWhenTrue    When analyzing a condition, inits and uninits are null. Instead we have: 
 Bits initsWhenFalse     
 Bits uninitsWhenTrue     
 Bits uninitsWhenFalse     
 VarSymbol[] vars    A mapping from addresses to variable symbols. 
 JCClassDecl classDef    The current class being defined. 
 int firstadr    The first variable sequence number in this class definition. 
 int nextadr    The next available variable sequence number. 
 List<Type> thrown    The list of possibly thrown declarable exceptions. 
 List<Type> caught    The list of exceptions that are either caught or declared to be thrown. 
 Scope unrefdResources    The list of unreferenced automatic resources. 
 boolean loopPassTwo    Set when processing a loop body the second time for DU analysis. 
 ListBuffer<PendingExit> pendingExits    The currently pending exits that go from current inner blocks to an enclosing block, in source order. 
Constructor:
 protected Flow(Context context) 
Method from com.sun.tools.javac.comp.Flow Summary:
analyzeTree,   checkCaughtType,   checkInit,   errorUncaught,   instance,   letInit,   letInit,   markDead,   markThrown,   merge,   newVar,   recordExit,   referenced,   resolveBreaks,   resolveContinues,   scanCond,   scanDef,   scanExpr,   scanExprs,   scanStat,   scanStats,   split,   trackable,   visitApply,   visitAssert,   visitAssign,   visitAssignop,   visitBinary,   visitBlock,   visitBreak,   visitClassDef,   visitConditional,   visitContinue,   visitDoLoop,   visitForLoop,   visitForeachLoop,   visitIdent,   visitIf,   visitLabelled,   visitMethodDef,   visitNewArray,   visitNewClass,   visitReturn,   visitSwitch,   visitThrow,   visitTopLevel,   visitTry,   visitTypeCast,   visitUnary,   visitVarDef,   visitWhileLoop
Methods from com.sun.tools.javac.tree.TreeScanner:
scan,   scan,   visitAnnotation,   visitApply,   visitAssert,   visitAssign,   visitAssignop,   visitBinary,   visitBlock,   visitBreak,   visitCase,   visitCatch,   visitClassDef,   visitConditional,   visitContinue,   visitDoLoop,   visitErroneous,   visitExec,   visitForLoop,   visitForeachLoop,   visitIdent,   visitIf,   visitImport,   visitIndexed,   visitLabelled,   visitLetExpr,   visitLiteral,   visitMethodDef,   visitModifiers,   visitNewArray,   visitNewClass,   visitParens,   visitReturn,   visitSelect,   visitSkip,   visitSwitch,   visitSynchronized,   visitThrow,   visitTopLevel,   visitTree,   visitTry,   visitTypeApply,   visitTypeArray,   visitTypeBoundKind,   visitTypeCast,   visitTypeIdent,   visitTypeParameter,   visitTypeTest,   visitTypeUnion,   visitUnary,   visitVarDef,   visitWhileLoop,   visitWildcard
Methods from com.sun.tools.javac.tree.JCTree$Visitor:
visitAnnotation,   visitApply,   visitAssert,   visitAssign,   visitAssignop,   visitBinary,   visitBlock,   visitBreak,   visitCase,   visitCatch,   visitClassDef,   visitConditional,   visitContinue,   visitDoLoop,   visitErroneous,   visitExec,   visitForLoop,   visitForeachLoop,   visitIdent,   visitIf,   visitImport,   visitIndexed,   visitLabelled,   visitLetExpr,   visitLiteral,   visitMethodDef,   visitModifiers,   visitNewArray,   visitNewClass,   visitParens,   visitReturn,   visitSelect,   visitSkip,   visitSwitch,   visitSynchronized,   visitThrow,   visitTopLevel,   visitTree,   visitTry,   visitTypeApply,   visitTypeArray,   visitTypeBoundKind,   visitTypeCast,   visitTypeIdent,   visitTypeParameter,   visitTypeTest,   visitTypeUnion,   visitUnary,   visitVarDef,   visitWhileLoop,   visitWildcard
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from com.sun.tools.javac.comp.Flow Detail:
 public  void analyzeTree(Env<AttrContext> env,
    TreeMaker make) 
    Perform definite assignment/unassignment analysis on a tree.
  void checkCaughtType(DiagnosticPosition pos,
    Type exc,
    List<Type> thrownInTry,
    List<Type> caughtInTry) 
  void checkInit(DiagnosticPosition pos,
    VarSymbol sym) 
    Check that trackable variable is initialized.
  void errorUncaught() 
    Complain that pending exceptions are not caught.
 public static Flow instance(Context context) 
  void letInit(JCTree tree) 
    If tree is either a simple name or of the form this.name or C.this.name, and tree represents a trackable variable, record an initialization of the variable.
  void letInit(DiagnosticPosition pos,
    VarSymbol sym) 
    Record an initialization of a trackable variable.
  void markDead() 
    Record that statement is unreachable.
  void markThrown(JCTree tree,
    Type exc) 
    Record that exception is potentially thrown and check that it is caught.
  void merge() 
    Merge (intersect) inits/uninits from WhenTrue/WhenFalse sets.
  void newVar(VarSymbol sym) 
    Initialize new trackable variable by setting its address field to the next available sequence number and entering it under that index into the vars array.
  void recordExit(JCTree tree) 
    Record an outward transfer of control.
  void referenced(Symbol sym) 
 boolean resolveBreaks(JCTree tree,
    ListBuffer<PendingExit> oldPendingExits) 
    Resolve all breaks of this statement.
 boolean resolveContinues(JCTree tree) 
    Resolve all continues of this statement.
  void scanCond(JCTree tree) 
    Analyze a condition. Make sure to set (un)initsWhenTrue(WhenFalse) rather than (un)inits on exit.
  void scanDef(JCTree tree) 
    Analyze a definition.
  void scanExpr(JCTree tree) 
    Analyze an expression. Make sure to set (un)inits rather than (un)initsWhenTrue(WhenFalse) on exit.
  void scanExprs(List<JCExpression> trees) 
    Analyze a list of expressions.
  void scanStat(JCTree tree) 
    Analyze a statement. Check that statement is reachable.
  void scanStats(List<JCStatement> trees) 
    Analyze list of statements.
  void split(boolean setToNull) 
    Split (duplicate) inits/uninits into WhenTrue/WhenFalse sets
 boolean trackable(VarSymbol sym) 
    Do we need to track init/uninit state of this symbol? I.e. is symbol either a local or a blank final variable?
 public  void visitApply(JCMethodInvocation tree) 
 public  void visitAssert(JCAssert tree) 
 public  void visitAssign(JCAssign tree) 
 public  void visitAssignop(JCAssignOp tree) 
 public  void visitBinary(JCBinary tree) 
 public  void visitBlock(JCBlock tree) 
 public  void visitBreak(JCBreak tree) 
 public  void visitClassDef(JCClassDecl tree) 
 public  void visitConditional(JCConditional tree) 
 public  void visitContinue(JCContinue tree) 
 public  void visitDoLoop(JCDoWhileLoop tree) 
 public  void visitForLoop(JCForLoop tree) 
 public  void visitForeachLoop(JCEnhancedForLoop tree) 
 public  void visitIdent(JCIdent tree) 
 public  void visitIf(JCIf tree) 
 public  void visitLabelled(JCLabeledStatement tree) 
 public  void visitMethodDef(JCMethodDecl tree) 
 public  void visitNewArray(JCNewArray tree) 
 public  void visitNewClass(JCNewClass tree) 
 public  void visitReturn(JCReturn tree) 
 public  void visitSwitch(JCSwitch tree) 
 public  void visitThrow(JCThrow tree) 
 public  void visitTopLevel(JCCompilationUnit tree) 
 public  void visitTry(JCTry tree) 
 public  void visitTypeCast(JCTypeCast tree) 
 public  void visitUnary(JCUnary tree) 
 public  void visitVarDef(JCVariableDecl tree) 
 public  void visitWhileLoop(JCWhileLoop tree)