Previous Next |
Type-checking for a polymorphic language is not as straightforward as for a monomorphic language. Before embarking on an implementation, we must be clear about what the typing rules are. The types used in the basic MiniJava compiler could all be represented as atomic strings: int, boolean, int[], and class identifiers such as IntList. In GJ we have three kinds of type expressions:
Primitive types such as int and boolean;
Type apps of the form c 〈t_{1}, t_{2},… t_{n}〉, where c is a type constructor - a polymorphic class such as List in our example - and t_{1} through t_{n} are type expressions; and Type variables such as the X in the type expression ListX.
All class identifiers will be considered polymorphic, but those with no type arguments can be considered as types of the form C<>. In this chapter we will omit discussion of array types, though in a "MiniGJ" compiler int[] could be treated as a primitive type. Syntactic conventions The type-checking algorithm will have several kinds of variables:
T, T_{i}, U, V stand for type expressions;
We will often abbreviate the keyword extends with the symbol ◃. In discussing the type-checking algorithm we will require that every TyParams have an explicit bound, so instead of writing class List<X> we would write class List<X◃Object>. Method types It's convenient to separate the type of a method from the name of the method. For example, consider the declaration
<X extends Printable> GPair<X> firstTwo(List<X> x) { ... }
For any type X that's a subclass of Printable, the firstTwo method takes a list of X's, and returns a pair of X's. The type of the firstTwo method is 〈X ◃ Printable〉 List 〈X〉 → GPair 〈X〉. (Notice that the binding occurrence of X requires a bounding clause 〈X ◃ Printable〉, but the applied occurrences of X do not use the ◃ symbol.) In general, the form of a method type is
meaning that, with type parameters TyParams and value parameters whose types are TyList, the method returns a value of type Ty. Substitution Suppose we want to know the type of the firstTwo method when it's applied to a list of PrintableInt. We take List 〈X〉 → GPair 〈X〉 and substitute PrintableInt for X, yielding the method type
List 〈PrintableInt〉 → GPair 〈PrintableInt〉
We can write a substitution function in the compiler's type-checker to do this; in this chapter we'll write [V_{1},…, V_{k}/X_{1},…, X_{k}]U to mean the substitution of type V_{i} for every occurrence of type variable X_{i} in type expression (or function prototype) U. Because class parameterization is not nested in GJ - that is, U cannot contain class declarations - we don't need to worry about internal redeclarations of the type variables X_{i}; the problem of avoiding variable capture (described in ) does not occur. Class table, type table, and var table explained that, because classes can mutually refer to each other, type-checking must proceed in two phases: First, build a symbol table (the class table) mapping class names to class declarations; second, type-check the interior of the class declarations. The same approach works for GJ. The first phase is quite straightforward; the rest of this section explains the algorithm for the second phase. This phase will use the class table, and maintain a type table mapping formal type parameters to their bounds, and a var table mapping ordinary (value) variables to their types. For example, in processing the firstTwo method (described above), the type table would map X to Printable, and the var table would map x to List<X>. Well-formed types A type is well formed if every type variable has a bound in the appropriate type table. Thus, the type List<X> is well formed when processing the declaration of firstTwo because X ◃ Printable is in the type table. In general, we must also account for substitutions. Algorithm 16.1 shows the procedure checktype for checking wellformedness of types.
ALGORITHM 16.1: Checking wellformedness of types and subtyping.checkType(T) =
if T is primitive (int, boolean, etc.) then OK
else if T is Object then OK
else if (T ↦ ...) is in the type table then OK
else if T is C 〈T_{1},..., T_{n}〉
look up C in the class table, yielding class C 〈X_{1} ◃ N_{1},..., X_{n} ◃ N_{n}〉 ◃ N{...}
for each T_{i} do checkType(T_{i}
for each T_{i} do checkSubtype(T_{i}, [T_{1},..., T^{n})X_{1},..., X^{n}]N_{i}
checkSubtype(T, U) =
if T = U then OK
else if (T ↦ T′) is in the type table then subtype(T′, U
else if T is C 〈T_{1},..., T_{n}〉
look up C in the class table, yielding class C 〈X_{1} ◃ N_{1},..., X_{n} ◃ N_{n}〉 ◃ N{...}
subtype([T_{1},..., T_{n}/X_{1},..., X_{n}]N, U)
else error
Subtyping In type-checking Java (and GJ) it is often necessary to test whether one type is a subtype of another: A is a subtype of B if, whenever a value of type B is required, a value of type A will suffice. A subtype is not exactly the same as a subclass, but still, in class-based languages like Java, if class A extends B, then there is also a subtype relationship. Checking subtyping in GJ is made more complex by the need to perform type substitution and the need to look up type variables in the type table; Algorithm 16.1 shows how to do it. Finding the bound of a type The formal type parameters of a polymorphic class or method are of the form hX ◃ N_{i}. Inside that class or method, suppose there's a local variable declaration X x, where X is the type and x is the variable. When type-checking expressions such as the field access x. f, we need to know what class x is an instance of. We don't know exactly what class x belongs to - it belongs to some unknown class X - but we do know that X is a subclass of N. Therefore, we take the type X and look up its bound, N, in the type table. The function getBound(T) looks up type bounds. Looking up field and method types When type-checking a field access e: f or a method call e.m 〈T 〉 (), one must look up the field f or method m in e's class. This may involve a search through superclasses, and in the process, types must be substituted for type variables. These searches are handled by fieldType and methodType in Algorithm 16.2. Algorithm 16.3 shows how to type-check expressions using all the auxiliary procedures defined thus far.
ALGORITHM 16.2: Field and method search.getBound(T) =
if T is a type variable X
then if (X ↦ N) is in the type table then N else error
else if T is a nonvariable type N then N
else error
fieldType(f;) =
if T is C 〈T_{1},..., T_{n}〉
look in the class table for class C 〈X_{1} ◃ N_{1},..., X_{n} ◃ N_{n}〉 ◃ N{fields, ...}
if field f with type S is in fields
return [T_{1},..., T_{n}/X_{1},..., X_{n}]S
else return fieldType(f, [T_{1},..., T_{n}/X_{1},..., X^{n}]N)
else error
methodType(m, T ) =
if T is C 〈T_{1},..., T_{n}〉
look in the class table for class C 〈X_{1} ◃ N_{1},..., X_{n} ◃ N_{n}〉 ◃ N{..., methods}
if method m is in methods; with the declaration
〈Y_{1} ◃ P_{1},..., Y_{k} ◃ P_{k}〉 U m(U_{1} x_{1} ... U_{l} x_{l}){return e}
then return [T_{1},..., T_{n}/X_{1},..., X_{n}](〈Y_{1} ◃ P_{1},..., Y_{k} ◃ P_{k}〉 (U_{1} x_{1},..., U_{l} x_{l}) → U)
else return methodType(m, [T_{1},..., T_{n}/X_{1},..., X_{n}]N)
checkExp(e) =
if e is a variable x
look up (x ↦ T ) in the var table; return T
else if e is a field-selection e_{0}. f
return fieldType(f; getBound(checkExp(e_{0})))
else if e is a method-call e_{0}m 〈V_{1},..., V_{n}〉 (e_{1},..., e^{l})
call methodType(m, getBound(checkExp(e_{0}))), yielding
〈Y_{1} ◃ P_{1},..., Y_{k} ◃ P_{k}〉 (U_{1} x_{1},..., U^{l} x_{l}) → U)
for each V_{i} do checkType(V_{i})
for each V_{i} do checkSubtype(V_{i}, [V_{1},..., V^{k}/Y_{1},..., Y_{k}]P^{i})
for i ∈ 1 ... l do checkSubtype(checkExp(e_{i}), [V_{1},..., V_{k}/Y_{1},..., Y_{k}]U_{i})
return [V_{1},..., V_{k}/Y_{1},..., Y_{k}]U
else if e is new N)
checktype(N/)
return N
Algorithm 16.4 shows how to check class and method declarations. The formal type parameters of the class are added to the type table, then all the methods are checked. For each method, the formal type parameters and the (ordinary) value parameters of the method are added to the type table and value table, then the body of the method is type-checked. The four lines ending at the last call to checkSubtype are to make sure that the method over-riding is valid; in GJ as in Java, one can only override a method at the same argument and result type as in the superclass, but for GJ there are more substitutions to perform in order to check this.
ALGORITHM 16.4: Type-checking class declarations.checkClass(cdecl) =
suppose cdecl is class C 〈X_{1} ◃ N_{1},..., X_{n} ◃ N_{n}〉 ◃ N{fields, methods}
add (X_{1} ↦ N_{1},... X_{k} ↦ N_{k}) to the type table
checkType(N)
for each N_{i} do checkType(N_{i})
for each mdecl in methods
suppose mdecl is 〈Y_{1} ◃ P_{1},..., Y_{j}〉 ◃ P_{j}〉 T m(T_{1} x_{1} ... T_{l} x_{l}) {return e}
add (Y_{1} ↦ P_{1},... Y_{j} ↦ P_{j}) to the type table
checkType(T)
for each T_{i} do checkType(T_{i})
for each P_{i} do checkType(P_{i})
add (this ↦ C 〈X_{1},..., X_{n}〉) to the var table
add (x_{1} ↦ T_{1},... x_{l} ↦ T_{l}) to the var table
checkSubtype(checkType(e), T )
suppose methodType(m, N) is 〈Z_{1} ◃ Q_{1},..., Z_{j} ◃ Q_{j} 〉 (U_{1} x_{1},..., U_{l} x_{l}) → U
for each T_{i} check that T_{i} = ([Y_{1},..., Y_{j}/Z_{1},..., Z_{j}]U^{i})
for each P_{i} check that P_{i} = ([Y_{1},..., Y_{j}/Z_{1},..., Z_{j}]Q_{i})
checkSubtype(T, [Y_{1},..., Y_{j}/Z_{1},..., Z_{j}]U)
pop j most recent bindings from the type table
pop l + 1 most recent bindings from the var table
pop k most recent bindings from the type table
JaVa | Previous Next |