next up previous contents
Next: The New Instance Up: The Link Checking Previous: The Identifiers Link

The Method Invocation Link Checking

Resolving a method name at compile time is more complicated than resolving a field name because of the possibility of method overloading. Invoking a method at run time is also more complicated than accessing a field because of the possibility of instance method overriding.

Determining the method that will be invoked by a method invocation expression involves several steps. The first step in processing a method invocation at compile time is to figure out the name of the method to be invoked and which class or interface to check for definitions of methods of that name. There are several cases to consider, depending on the form that precedes the left parenthesis, as follows:

The second step searches the class or interface determined in the previous step for method declarations. This step uses the name of the method and the types of the argument expressions to locate method declarations that are both applicable and accessible, that is, declarations that can be correctly invoked on the given arguments. There may be more than one such method declaration, in which case the most specific one is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.

The precise definition of the most specific property is as follows. Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and that the types of the parameters are T, . . . , T; suppose moreover that the other declaration appears within a class or interface U and that the types of the parameters are U, . . . , U. Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true:

A method is said to be maximally specific for a method invocation if it is applicable and accessible and there is no other applicable and accessible method that is more specific.

If there is exactly one maximally specific method, then it is in fact the most specific method; it is necessarily more specific than any other method that is applicable and accessible.

It is possible that no method is the most specific, because there are two or more maximally specific method declarations. In this case, we say that the method invocation is ambiguous, and a compile-time error occurs.

The implementation of the method lookup mechanism builds first a list of method descriptors. The structure of a method descriptor is shown below:

typedef struct descr_tag {
  btype_t * proto;          /* The function's prototype */
  type_t * where;           /* The type where the function 
                               declaration occurs */
  int index;                /* The function's declaration index */
  struct descr_tag * next;
} descr_t;
The first field of the structure points to the function's type descriptor node. It is the function's prototype, that keeps information on the method arguments types. The second field is a pointer to the type descriptor node of the class or interface where the method declaration occurs. The index field is the index of the method in the class method list. It is used to return the corresponding method symbol. The next field is the pointer to the next method descriptor node in the list.

After building the method descriptor list the most specific method's descriptor is chosen from all the elements of the list using the rules described above.



next up previous contents
Next: The New Instance Up: The Link Checking Previous: The Identifiers Link



Radu Iosif
Thu Apr 9 15:10:10 MET DST 1998