Java Reference
In-Depth Information
this convenience is the fact that the structural type equivalence rule makes
it impossible for a programmer to get full benefit from the concept of type
checking. That is, even if a programmer wants the compiler to distinguish
between Type1 and Type2 because they represent di
erent concepts in the
program despite their identical implementations, the compiler is unable to do
so.
ff
Structural equivalence is also much harder to implement. Rather than
being determined by a single pointer comparison, a parallel traversal of two
type descriptor structures is required. The code to do such a traversal requires
special cases for each of the type descriptor alternatives. Another approach
does the comparison work as a type definition is processed by the semantic
checking pass through the tree. The type being defined is compared against
previously defined types so that equivalent types are represented by the same
data structure, even though they are defined separately. This technique allows
the type equivalence test to be implemented by a pointer comparison, but it re-
quires an indexing mechanism that makes it possible to tell during declaration
processing whether each newly defined type is equivalent to any previously
defined type.
Further, the recursion possible within pointer type definitions poses sub-
tle di
culties to the implementation of a structural type equivalence test.
Consider the problem of writing a routine that can determine whether the
following two Ada types are structurally equivalent (access means ”points
to”):
type A is access B;
type B is access A;
Even though such a definition is meaningless semantically, it is syntactically
legal (presuming there is an incomplete type definition to introduce the name
B before the definition of A). Thus a compiler for a language with structural
type equivalence rules must be able to make the appropriate determination—
that A and B are equivalent. If parallel traversals are used to implement the
equivalence test, then the traversal routines must ”remember” which type
descriptors they have visited during the comparison process in order to avoid
an infinite loop. Su
ce it to say that comparing pointers to type descriptors is
much simpler!
8.6.4 Variable Declarations Revisited
Declarations of variables and, more generally, data members of classes can
be more complex than the simple form shown in Section 8.6.1. Types need
not be specified by a name, but can be constructed by a variety of syntactic
forms. To cover the cases, the AST for declarations shown in Figure 8.17
 
 
Search WWH ::




Custom Search