Java Reference
In-Depth Information
error recognized. Finally, beginning at Marker
79
,themeaningof
fieldName
is retrieved from the structure's symbol table and its type is returned as the
type of this reference. As would be expected, an appropriate error message is
produced if the name is not found.
The LHSSemanticsVisitor
method for structs is shown at Marker
66
.
It first calls the normal semantic visitor to verify that a valid struct and field are
present. If so, it then does two additional checks. First, it visits the
objectName
subtree to verify that the struct is assignable. It then looks up the
fieldName
's
attributes and checks that the field is assignable (to cover the case in which an
individual field may be marked as const or final).
visit
Complex Name Example
Figure 8.39 illustrates the AST that would be used to represent the name
s.a[i+1].f. This name includes a structure name, a field name, which names
an array of structures, and the name of a field of one of the array elements.
Thus Figure 8.39 includes two StructReferencing nodes (nodes 1 and 4), and
one ArrayReferencing node (node 2). When the
method for node 1 in
SemanticsVisitor is invoked to process this name, the type checking process
will proceed through the following steps:
visit
1. Through the call to the
method at Marker
76
in Figure 8.38, the
type checking traversal immediately moves to node 2.
accept
2. The
visit
method for anArrayReferencingnode in Figure 8.37 beginswith
acallto
visit
C
hildren
, which first invokes the
accept
method for node
4. (Node 5 will be visited later as a result of this call to
visit
C
hildren
.)
3. Once again, the StructReferencing
accept
method atMarker
76
to propagate the traversal on to the node referenced
by its
objectName
field, node 6 in this case.
visit
method uses the call to the
4. The meaning of name s is retrieved from the symbol table. Presuming
that s names a data object, its type will be set as the value of the
type
field
on node 6. Control will then return to the
visit
method for node 4.
5. ThecodeatMarker
78
in Figure 8.38 checks that the type just retrieved
for s is a structure. Presuming that it is, the identifier a from the
name
field in node 7 will be looked up in the symbol table associated with
struct s. The type retrieved for a (an array) will be set as the value of
the
type
field of node 4. Control will then return to the
visit
method for
node 2.
Note that the type checking traversal was not propagated to node 7. If
that had been done, then a would have been looked up in the symbol
table for the current scope rather than the one associatedwith structure s.