SUIF symbol tables provide a number of methods to search for and retrieve particular types, symbols, and variable definitions. Most of these lookup methods will optionally search all the ancestor symbol tables, making it easy to determine if an object is defined in the current scope.
The lookup_type
method is available at all levels in the symbol
table hierarchy to search for SUIF types. Given an existing type, the
method searches for a type that is the same. It uses the is_same
method from the type_node
class to perform these comparisons. If
a matching type is not found within the current symbol table,
lookup_type
will continue searching in the ancestor symbol tables
by default. However, if the optional up
parameter is set to
FALSE
, it will give up after searching the first table.
Several methods are provided to lookup symbols. Each different kind of
symbol (variable, procedure, and label) has its own name space, so the
lookup_sym
method requires that you specify both the name and the
kind of symbol for which to search. This method may be used with all
symbol tables. For convenience, other methods are defined as wrappers
around lookup_sym
. Each of these wrappers searches for a
particular kind of symbol: lookup_var
searches for variables,
lookup_proc
searches for procedures, and lookup_label
searches for labels. Because procedure symbols may only be defined in
global symbol tables, the lookup_proc
method is declared in the
global_symtab
class. Similarly, the lookup_label
method
is declared in the block_symtab
class, because labels may only be
defined within procedures. By default, all of these methods search the
current symbol table and, if unsuccessful, proceed to search the
ancestor symbol tables. The optional up
parameters may be set to
FALSE
to turn off this default behavior and only search the
current symbol table.
A symbol for a global variable is just a declaration of that variable
and does not automatically have any storage allocated. Variable
definitions are required to allocate storage and to specify alignment
requirements and any initial data for the variable. Since the variable
definitions are not directly connected to the variable symbols, the
lookup_var_def
method is provided to search a symbol table for
the definition of a particular variable symbol. This method does not
search the parent symbol table. In general the definition
method
in the var_sym
class is a better way to locate a variable
definition.
Symbols and types are assigned ID numbers (see section Numbering Types and Symbols) that uniquely identify them within a particular context. The
lookup_type_id
method searches the types defined within a symbol
table and its ancestors for a type with the specified ID number. The
lookup_sym_id
does the same thing for symbols.
Besides searching for one of the entries in a symbol table, you can also
search for one of its children in the symbol table hierarchy. The
lookup_child
method searches through the list of children for a
symbol table with a given name. This may not be very useful, but it is
included for completeness.