Java Reference
In-Depth Information
class NodeVisitor
procedure
( n )
foreach c n . getChildren() do
visit
C
hildren
call c . accept( this )
1
end
end
class TopVisitor extends NodeVisitor
procedure
visit
(ClassDeclaring cd )
2
/
Section 11.2.1 on page 422
/
end
procedure
visit
(MethodDeclaring md )
3
/
Section 11.2.2 on page 424
/
end
end
/
Continued in Figure 11.2
/
Figure 11.1: Structure of the code-generation visitors, with references
to sections addressing specific constructs.
child to accept the current visitor. Such a visitor is an instance of the type
NodeVisitor,suchasaTopVisitoror a MethodVisitor.
Within a visitor, access to a particular node's contents is specified using
accessor methods, which typically include the word
get
. For example, the
name of a class is retrieved using the method
get
C
lass
N
ame
at Marker 14 .
11.2 Class and Method Declarations
The outermost portions of an AST contain class and method declarations.
The TopVisitor shown in Figure 11.1 is responsible for processing each class
and method declarations. Section 11.2.1 explains how classes are processed,
including their field and static declarations. Section 11.2.2 covers the initial
processing of a method declaration.
The superclass NodeVisitor provides a useful method for visiting a node
n 's children, passing the current visitor to each in turn. To be consistent with
the semantics of most programming languages, the visitor is passed to the
children in left-to-right order of their appearance in the AST. The code shown
at Marker 1 is typical of visitors that call for recursive processing of AST
subtrees. By calling for each child c of n to accept this visitor, the code at
Marker 1 causes the current visitor to process c recursively.
 
 
Search WWH ::




Custom Search