Every symbol and type is assigned an ID number that uniquely identifies
it within a particular context. These ID numbers should be used to
refer to symbols and types in annotations that will be written to the
output files and in other situations where pointers to the symbol and
type nodes cannot be used. The sym_id
method retrieves the ID
number for a symbol, and the type_id
method gets the number for a
type.
For symbols and types within a procedure, the ID numbers are only unique
within that procedure. Similarly, the ID numbers for symbols and types
in a file symbol table are only unique within that file. Only in the
global symbol table are the ID numbers truly unique. This is
implemented by dividing the ID numbers into three ranges. Each range is
reserved for a particular level in the symbol table hierarchy. To make
it easier to read an ID number, the print_id_number
function
prints it as a character to identify the range (`g' for global,
`f' for file, `p' for procedure) combined with the offset of
the number within that range.
The symbol and type ID numbers cannot be assigned individually, but the
symbol tables provide methods to set them. The number_globals
method is defined in the global_symtab
class to number the
entries in global and file symbol tables, and the number_locals
method is defined in the proc_symtab
class to number all of the
entries in the procedure symbol table and its descendents. These
methods only assign ID numbers to symbols and types that do not already
have numbers. These methods are called automatically before writing
things out to files, but they can also be called whenever you want to
assign numbers to new symbols and types.
The clear_sym_id
symbol method and clear_type_id
method
are provided to reset the ID numbers to zero manually, but as far as
the library itself is concerned, this is never necessary. The library
automatically changes ID numbers when necessary, such as when moving
from one symbol table to another.