Java Reference
In-Depth Information
StructDefining
type
fieldList
FieldDeclaring
FieldDeclaring
. . .
idList
fieldType
idList
fieldType
IdentifierList
IdentifierList
ids
Type
Subtree
. . .
Type
. . .
ids
Subtree
Figure 8.21: Abstract Syntax Tree for a Struct Definition
8.6.6 Struct and Record Types
Programming languages typically include a type constructor for a heteroge-
neous collection of named data items, commonly known as records or structs.
The data items are named individually, using a syntax similar to variable dec-
larations. Because the names and types of all of the fields of a record or struct
must be individually specified, both the AST and semantic processing for this
kind of type constructor is much more complicated than that for arrays.
The AST in Figure 8.21 illustrates the representation necessary for a struct
definition. The StructDefiningnode provides access to a list of FieldDeclaring
nodes. Each of the FieldDeclaringnodes looks much like VariableListDeclaring
node from Section 8.6.4 and will be processed similarly. A struct defines a new
name scope in which all of the fields will be declared, but this scope can only be
accessed by naming an instance of the struct type. (See Section 8.8.4.) Thus the
symbol table for the scope defined by the struct will not be pushed on the stack
of currently open scopes. Rather, it will become part of the TypeDescriptorfor
the struct type. The specialization of TypeDescriptor needed for structs was
presented in Figure 8.10 of Section 8.5.1.
The
method for a StructDefining node is found in Figure 8.22. It
begins at Marker 39 by building a TypeDescriptor for a struct type. It then
creates a new symbol table to hold all of the field declarations.
Beginning at Marker 40 two nested loops process the list of FieldDeclaring
nodes and the individual field declarations they contain. At Marker 41 in the
visit
 
Search WWH ::




Custom Search