Java Reference
In-Depth Information
class LHSSemanticsVisitor extends SemanticsVisitor
procedure visit
( Identifier id )
64
visitor
new SemanticsVisitor()
call id . accept( visitor )
if not
is
A
ssignable
( id . attributeRe f )
then
call
error
( id . name ,
is not assignable .
”)
id . type errorType
id . attributesRe f
null
end
procedure
(ArrayReferencing ar )
call ar . arrayName . accept( this )
visitor new SemanticsVisitor()
call ar . accept( visitor )
visit
65
end
procedure
visit
(StructReferencing sr )
66
visitor
new SemanticsVisitor()
call sr . accept( visitor )
if sr . type errorType
then
call sr . ob jectName . accept( this )
st sr . ob jectName . type . fields
attributeRe f st . retrieveSymbol( fieldName . name )
if not
is
A
ssignable
( id . attributeRe f )
then call error
( fieldName . name ,
is not an assignable f ield ”)
end
end
Figure 8.34: Type checking visitors (Part 2)
lyze constructs expected to produce an R-value. This includes simple iden-
tifiers, literals, operators, and function calls. To analyze constructs expected
to produce an L-value, we shall introduce a new specialized visitor class,
LHSSemanticsVisitor. This visitor class will be defined for fewer AST nodes,
since only a few constructs produce a memory location (a variable, an arry ele-
ment or field reference). In most cases a
visit
method in LHSSemanticsVisitor
will do the same analysis as a corresponding
method in SemanticsVisitor
plus a”bitmore.”Theextracheckingitwill do is to verify that an assignable
name (an L-value) is produced. Thus, in checking an Identifier as an L-value,
we must check that it is properly declared and produces a valid type plus that
it names a variable (or it can somehow be the target of an assignment).
Figures 8.33 and 8.34 outline the semantic processing visitors that handle
each of the constructs discussed in this section. We only need
visit
methods
to be defined in LHSSemanticsVisitor for a few constructs because the parser
visit
 
Search WWH ::




Custom Search