Java Reference
In-Depth Information
that each definition has a unique type signature . The type signature of a
method includes the number and types of its parameters and its return type.
With overloading, a program can contain the method print(int) as well as
print(String).
When the type signatures are included, the compiler comes to view a
method definition not only in terms of its name but also in terms of its type
signature. The symbol table must be capable of entering and retrieving the
appropriate symbol for a method. In one approach to method overloading,
the type signature of a method is encoded along with its name. For example,
the method M that accepts an integer and returns void is encoded as M ( int ):
void . It then becomes necessary to include a method's type signature each
time a method name is retrieved from the symbol table. Alternatively, the
symbol table could simply record a method along with a list of its overloaded
definitions. In the AST, method invocations point to the entire list of method
definitions. Subsequently, semantic processing scans the list to make certain
that a valid definition of themethod occurs for each invocation. (See Section 9.2
on page 376.)
Some languages, such as C
and Ada, allow operator symbols to be
overloaded. For example, the meaning of + would change if its arguments
are strings instead of numbers. The symbol table for such languages must be
capable of determining the definition of an operator symbol in every scope.
Ada allows literals to be overloaded. For example, the symbol diamond
could participate simultaneously in two di
++
ff
erent enumeration types: as a
playing card suit and as a gem.
Pascal and Fortran employ a small degree of overloading in that the same
symbol can represent the invocation of a method as well as the value of the
method's result. For such languages, the symbol table contains two entries.
One represents the method while the other represents a name for the value
returned by themethod. It is clear in context whether the namemeans the value
returned by the method or the method itself. As demonstrated in Figure 8.1,
semantic processing makes an explicit connection between a name and its
symbol.
C also has overloading to a certain degree. A program can use the same
name as a local variable, a struct name, and a label. Although it is unwise to
write such confusing programs, C allows this because it is clear in each context
which definition of a name is intended. (See Exercise 16.)
Languages such as Java andC
er type extension through subclassing.
The symbol table could contain a method resize(Shape), while the program
invokes the method resize(Rectangle).IfRectangle is a subclass of Shape,
then the invocation should resolve to the method resize(Shape). However,
if the program contains a resize method for both Rectangle and Shape types,
then resolution should choose the method whose formal parameters most
++
o
ff
 
Search WWH ::




Custom Search