next up previous contents
Next: Pre-translation Up: Type-Checker Previous: Type Analysis   Contents


Reachability and Definite (Un)assignment Analysis

The second phase is reachability analysis, carrying out a conservative flow analysis to make sure all statements are reachable. The idea of Reachability is that there must be some possible execution path from the beginning of the constructor, method, instance initializer or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements. Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis. The third phase is definite (un)assignment analysis. It consists of two parts, DefAssign and DefUnassign. Each local variable and every blank final must have a definitely assigned value when any access of its value occurs. Similarly, every blank final variable must be assigned at most once; it must be definitely unassigned when an assignment to it occurs. The HPJava compiler must carry out a specific conservative flow analysis to make sure that, for every access of a local variable or blank final field f, f is definitely assigned before the access, and, for every assignment to a blank final variable, the variable is definitely unassigned before the assignment. DefUnassign checks the definite unassignment rules for Java, implementing the flow analysis of JLS. This applies three checks that are essentially ``static'' (i.e. independent of the dataflow analysis):
  1. A final field or final local variable cannot appear as the left-hand operand of an assignment operator, or as an operand of ++ or $ --$, unless the variable is blank final.
  2. Moreover, a blank final field can only appear as the left-hand operand of an assignment operator, or as an operand of ++ or $ --$, if it is referenced through its simple name, or has unqualified `this' as prefix (this is a slightly ad hoc restriction, but we follow JDK).
  3. A blank final field that appears as the left-hand operand of an assignment operator, or as an operand of ++ or $ --$, must not be declared as a member of a superclass (of ``the current class''). Superclass fields are considered to be previously assigned.
These are prerequisite to the main business: DefAssign checks the definite assignment rules for Java, implementing the flow analysis of JLS. Main checks implemented here are:
  1. All class variables (static fields) in a class are definitely assigned after the static initialization code in the class (initializers for static fields and static initializer blocks) has been executed.
  2. All instance variables (non-static fields) in the class are definitely assigned after the instance initialization code (initializers of non-static fields and instance initializer blocks, followed by the body of any single constructor in the class) has been executed.
  3. A blank final field declared in a class is definitely assigned before every use of its value anywhere in the class, provided those uses takes the form of a simple, unqualified name. (If the use has a more complicated form, the unassigned, default value may be visible. The restriction to simple names is slightly ad hoc, but we follow JDK).
  4. A local variable that is not initialized in its declaration is definitely assigned before every use of its value.

next up previous contents
Next: Pre-translation Up: Type-Checker Previous: Type Analysis   Contents
Bryan Carpenter 2004-06-09