Java Reference
In-Depth Information
/
Visitor code for Marker
2
/
procedure
visit
(ClassDeclaring
cd
)
(
cd
.
getClassName())
foreach
superclass
∈
cd
.
getSuperClasses()
do
call
emit
C
lass
N
ame
14
(
superclass
)
foreach
field
∈
cd
.
getFields()
do
call
emit
E
xtends
15
(
field
)
foreach
static
∈
cd
.
getStatics()
do
call
emit
F
ield
D
eclaration
16
call
emit
S
tatic
D
eclaration
(
static
)
17
foreach
node
∈
cd
.
getMethods()
do
node
.
accept(
this
)
18
end
The AST contains a subtree for each class defined in the source program. The
root of each subtree implements the ClassDeclaringinterface, which provides
important information about the class that must be transcribed by the visitor
whengenerating code so that instances of the class canbe properly instantiated.
Such information specifies how the class type fits into the namespace and
inheritance structure of all classes.
ThecodeintheClassDeclaring visitor serves well to document the con-
ventions we follow in writing visitor methods:
•
Generally, methods in our visitors that begin with
access such infor-
mation through the interface provided by the visited node.
get
•
Invocations of
accept
trigger traversal of an AST subtree with the e
ff
ect
of generating code for whatever is found there.
•
Methods that begin with
emit
generate the VM instructions.
Code generation on behalf of a class declaration includes the following:
Name:
code emitted at Marker
14
reflects the
name
of the class represented by
cd
. The name must include any contextual information that is required
to reference instances of
cd
. Such information can also serve to allow
instances of
cd
to access data within their purview. For example, the
package that hosts a Java class appears as a prefix of the class name.
Some classes are meant to be practically invisible across separate com-
pilations. Examples include
inner
and
anonymous classes
. The name
for such classes is typically generated as a distinguishing variation of the
primary class's name. If
cd
's name is Name, then an anonymous inner
class of
cd
might be dubbed Name$001.