A variable reference that occurs in a Java program can fall under the following categories: local variable, and field variable. If it doesn't name none of the above, the identifier must be a type name in a qualified access expression. Checking the meaning of an identifier follows two goals: setting the type of the identifier in order to do type checking on the expression in which the identifier occurs, and second, detecting invalid references to undeclared or unaccessible variables.
In the case of local variable reference, it is required that the declaration of the variable occurs before its use. Therefore, a local variable will be in the locals hash table in the moment it is used. The parser searches the local variable symbol in the hash table and sets the type field in the syntax tree node of the variable to point to the type descriptor node associated with the local variable symbol. At the stage of link checking, the front-end first tests if the type field of the identifier's syntax tree node is set, and then decides if the identifier refers a local variable.
In the case the identifier was not found to be a local variable reference, it might be a field reference. At this point the this and belong fields from the identifier syntax tree node are used to determine the field variable. The belong field indicates the class or interface in which to look for the field. The this field which points to the type descriptor node of the class in which the identifier occurs, is used to check if the field is accessible. Let us consider the following example:
class A {
...
private int x;
...
}
class B {
...
int method()
{
A.x = 1; // illegal access to field `x' from class B to class A
}
}
In the code above, the field x from class A can be accessed using a
qualified access expression, but this field is not accessible. The this
field of the identifier syntax tree node x points to the class B
type descriptor node, and the belong field points to the type descriptor
node of the class A. The link checker finds out in this case that there is
no access from class B to field x declared in class A.
In the case there is no field named by the identifier in the class pointed by the belong field, the link checker assumes that the identifier is names a type as in a qualified access expression to a static field. In this case the identifier syntax tree node is labeled as a type name using the name_code field from the syntax tree node structure. The type named by the identifier is searched in the types hash table. If it is not found an error is signaled.
In case the identifier names a valid reference to an existent symbol the link field in the identifier structure is set to point to that symbol. This reference will be used at the later stage of code generation.