Java Reference
In-Depth Information
/
Visitor code for Marker
15
on page 302
/
procedure
visit
(MethodDeclaring
md
)
typeVisitor
←
new
TypeVisitor()
call
md
.
returnType
.
accept(
typeVisitor
)
attr
←
56
new
Attributes(
MethodAttributes
)
attr
.
returnType
←
md
.
returnType
.
type
attr
.
modi f iers
←
md
.
modi f iers
attr
.
isDe f inedIn
← get
C
urrent
C
lass
()
attr
.
locals
←
new
SymbolTable()
call
currentSymbolTable
.
enterSymbol(
name
.
name
,
attr
)
md
.
name
.
attributeRe f
←
attr
call
(
attr
.
locals
)
oldCurrentMethod
← get
open
S
cope
C
urrent
M
ethod
()
call
set
C
urrent
M
ethod
(
attr
)
call
md
.
parameters
.
accept(
this
)
attr
.
signature
←
parameters
.
signature
.
addReturn(
attr
.
returntype
)
57
call
md
.
body
.
accept(
this
)
call
58
set
C
urrent
M
ethod
(
oldCurrentMethod
)
call
close
S
cope
()
end
Figure 8.31:
visit
method in TopDeclVisitor for MethodDeclaring.
if the name given for the return type did not designate a valid type. Next, an
Attributes descriptor for the method is created, with the return type, a newly
created symbol table, and the value of
currentClass
being used to provide
values for three of its fields. (See Figure 8.28 for the details of an Attributes
descriptor for a method.) This symbol table is set as the current symbol scope
and the Attributesdescriptor for the method is set as the
currentMethod
before
processing of the rest of the subtrees of the MethodDeclaring node begins.
Note that the old value of
currentMethod
issavedsothatitcanberestoredat
the end of this method. If nested methods are allowed by the language, this
step will make sure that the surrounding method is referenced again after this
method declaration is finished. If nested methods are not allowed, then the
restore step should return the
currentMethod
reference to a null value.
The
modi f iers
field of the AST node was simply copied to the Attributes
descriptor for the method rather than interpreting the values of the modifier
set, as we did in the
method for a ClassDeclaringnode. Because of the
many modifiers that can be specified for a method, it is simpler just to carry
along the set of modifiers that were part of the declaration and let other visitors
look for values in this set when needed.
At Marker
57
,thecurrentTopDeclVisitor is propagated to the list of pa-
rameter declarations. The
visit
method for the ParameterDeclaringnode refer-
enced by
parameters
will encounter a sequence of formal parameter names and
visit