Java Reference
In-Depth Information
ReachabilityVisitor
is a specialized visitor used to analyze control structures
for reachability and proper termination. These visitors set two flags,
isReachable
and
terminatesNormally
, used for error analysis and optional
code optimization.
ThrowsVisitor
is a specializedvisitor used to collect informationabout throws
that may ”escape” from a given construct. These visitors compute the
throwsSet
field, which records exceptions that may be thrown.
An important issue in analyzing Java control structures is
reachability
.Javare-
quires that unreachable statements be detected during semantic analysis with
suitable error messages generated. For example, in the statement sequence:
...; return; a=a+1; ...
the assignment statement must be marked as unreachable during semantic
analysis.
Reachability analysis is
conservative
. In general, determining whether a
given statement can be reached is very di
cult. In fact, it is impossible!
Computer science theorists have proven that it is undecidable whether a given
statement is ever executed, even when we know in advance all of the data that
a programwill access (reachability is a variant of the famous
halting problem
first discussed in [Tur36]).
Because our analyses will be conservative, we will not detect all occur-
rences of unreachable statements. However, the statements we recognize as
unreachable will definitely be erroneous, so our analysis will certainly be use-
ful. In fact, even in languages like CandC
, whichdo not require reachability
analysis, we can still produce useful warnings about unreachable statements
that may well be erroneous.
To detect unreachable statements during semantic analysis, we will add
two Boolean-valued fields to the ASTs that represent statements and statement
lists. The first,
isReachable
, marks whether a statement or statement list is con-
sidered reachable. We will issue an error message for any non-null statement
or statement list for which
isReachable
is false.
The second field,
terminatesNormally
, marks whether a given statement or
statement list is expected to terminate normally. A statement that terminates
normally will continue execution ”normally” with the next statement that
follows. Some statements (such as a break, continue, or return) may force
execution to proceed to a statement other than the normal successor statement.
These statements are marked with
terminatesNormally
set to false. Similarly, a
loop may never terminate its iteration (e.g., for(;;)
++
{
a=a+1;
}
). Loops that do