Java Reference
In-Depth Information
procedure
visit
(Returning
rn
)
call
(
rn
)
currentMethod
← get
visit
C
hildren
C
urrent
M
ethod
(
rn
)
if
rn
.
returnVal
null
then
if
currentMethod
=
null
then
call
error
(”
A value may not be returned f rom a constructor
”)
else
if not
assignable
(
currentMethod
.
returnType
,
rn
.
returnValue
.
type
)
then call
error
(”
Illegal return type
”)
else
if
currentMethod
null and
currentMethod
.
returnType
void
then call
error
(”
A value must be returned
”)
end
Figure 9.18: Semantic Analysis for a Return
Returning
returnVal
Expr
Figure 9.19: Abstract Syntax Tree for a Return Statement
by verifying that the statement list that comprises a function's body has its
terminatesNormally
value set to false.
To determine the validity of a return, we will check the kind of construct
(method or constructor) within which it appears. But AST links all point
downward; hence looking ”upward” is di
cult. To assist our analysis, we will
assume twomethods that returnAttributesstructures,
get
C
urrent
M
ethod
and
get
C
urrent
C
onstructor
. These methods store information gathered during
semantic analysis.
If we are checking an AST node contained within the body of a method,
then
will tell us which one. If we are analyzing an AST
node not within a method, then
get
C
urrent
M
ethod
get
C
urrent
M
ethod
returns null. The same is
true for
get
C
urrent
C
onstructor
. We can determine which kind of construct