This assignment covers the structural checks that need to be performed
on whole method and constructor bodies. It encompasses the two phases
Reachability and
DefiniteAssignment. You must hand in two files
reachability.ml and
definiteassignment.ml that extend the skeleton.
Reachability
The Reachability phase analyzes the code to ensure
that no statements are unreachable and that methods and constructors
always execute a return (Joos 2: or throw)
statement, i.e. that they never run off the end of the body.
The analysis is described in Section
14.20 of the JLS and on slides 13-15 in the lecture on
static analysis.
Transformations
- If the body of a constructor or a method whose return type is
void can complete normally, add an
TAst.VoidReturn stm node at the end of the body.
Checks
- Check that all statements (including empty statements and empty
blocks) are reachable. (error_unreachable_statement)
- If the body of a method whose return type is not
void can complete normally, produce an error
message. (error_missing_return_statement)
DefiniteAssignment
The DefiniteAssignment phase analyzes the code to ensure
that all variables are initialized before they are used.
The analysis is described in Chapter
16 of the JLS and on slides 22-28 in the lecture on
static analysis.
The extra restrictions on local variable initializers imposed on Joos
1 eliminates the need for the definite assignment analysis. Only a
Joos 2 compiler needs to perform the analysis.
Checks (Joos 1)
- A local variable declaration must have an initializer. (check_joos1_omitted_local_initializer)
- A local variable must not occur in its own initializer. (check_joos1_local_variable_in_own_initializer)
Checks (Joos 2)
- Whenever a local variable is used in any context which is not the
direct left-hand side of an assignment, it must be definitely
assigned at that point in the program. (error_variable_might_not_have_been_initialized)
Documentation
You must post an entry in your group blog that explains how your code works and
which problems you have encountered and solved in the process. Furthermore:
- Give an example of a program containing some dead code, i.e. one or more unreachable statements, which is
rejected by the reachability analysis.
- Give an example of a program containing some dead code which is accepted by the reachability analysis.
- Give an example of a method where the reachability analysis requires a void-return to be inserted.
- Give an example of a method where the reachability analysis detects a missing value-return.