Java Reference
In-Depth Information
their types that define the interface to the method. This visitor enters all of the
names in the symbol table, similar to variables, and constructs a descriptor for
the list of parameter types, which is subsequently incorporated in the Attributes
descriptor for the method. Details of this method are left as an exercise. (See
Exercise 22.) Since the final subtree representing the body of the method con-
sists of a combination of declarations and executable statements, we have to
consider what will happen as we propagate the current TopDeclVisitor to this
subtree. When a declaration is encountered, the appropriate
method
declared within TopDeclVisitorwill be invoked. When a statement is encoun-
tered during traversal of the list, no corresponding method will be found in
TopDeclVisitor. Since the parent class of TopDeclVisitor is SemanticsVisitor,
visit
visit
methods declared within that class for statement AST nodes will be in-
voked when corresponding statement nodes are visited.
methods for
statement nodes are described in Section 8.8 and Chapter 9. Finally, the value
of currentMethod is returned to null , since we are no longer within a method
declaration, and the
visit
method returns the symbol table stack to its original
state before completing its actions for the MethodDeclaringnode.
Java includes one additional significant feature as part of method declara-
tions. A method may include a list of exceptions that might be thrown by exe-
cution of the method. A complete implementation of Javamethod declarations
requires a reference to this list of exceptions as part of theMethodDeclaringAST
node and a
visit
method to process the exception list. In addition the Attributes
descriptor for the method must be extended to include a declaredThrowsList for
use by the
visit
visit
methods that handle throws, as defined in Section 9.1.7 on
page 369.
Constructors aremuch likemethods, except that no return type is specified,
since the type they return is the type of the class in which they are declared.
Certain restrictions apply to constructors, depending on the language being
compiled. A
method for a ConstructorDeclaringnode will look much like
the method defined here for a MethodDeclaringnode.
visit
8.8 An Introduction to Type Checking
Sections 8.6 and 8.7 presented the methods defined in TopDeclVisitor that
process declarations in order to collect information in the symbol table. In
this section, we define the actions performed by SemanticsVisitor that use the
information in symbol table to check types and other semantic requirements
as it visits the AST nodes for the executable parts of a program.
We begin our discussion of semantic checking by defining the type check-
ing requirements for names and expressions in the context of an assignment
statement. After a general discussion of the interpretation of names in this
context, Section 8.8.1 presents the
visit
methods used for semantic analysis of
 
 
Search WWH ::




Custom Search