next up previous
Next: Array types Up: Types and type identifiers Previous: Primitive types

Class and interface types

Each class or interface type is declared once and named, and then represented by its name wherever it is used in the program text. No two class or interface types are equivalent, even though they may have identical declarations:

Class and interface types[17]:
SYMBOL TypeIdDef       INHERITS TypeDefDefId   END;
SYMBOL TypeName        INHERITS TypeDefUseId   END;
SYMBOL pTypeName       INHERITS TypeDefUseId   END;

RULE: TypeDeclaration ::=
        Modifiers 'class' TypeIdDef Super Interfaces ClassBody
COMPUTE
  TypeIdDef.Type=TypeDeclaration.Type;
END;

RULE: TypeDeclaration ::=
        Modifiers 'interface' TypeIdDef Interfaces InterfaceBody
COMPUTE
  TypeIdDef.Type=TypeDeclaration.Type;
END;
This macro is defined in definitions 17, 19, 20, 21, 22, 23, and 25.
This macro is invoked in definition 14.

Interface types differ in several ways from class types, and therefore we use a property to distinguish them:

Property definitions[18]:
IsInterfaceType: int;
This macro is defined in definitions 18, 24, 27, and 29.
This macro is invoked in definition 3.

Class and interface types[19]:
RULE: TypeDeclaration ::=
        Modifiers 'interface' TypeIdDef Interfaces InterfaceBody
COMPUTE
  TypeDeclaration.GotType=ResetIsInterfaceType(TypeDeclaration.Type,1);
END;
This macro is defined in definitions 17, 19, 20, 21, 22, 23, and 25.
This macro is invoked in definition 14.

The TypeDenotation role establishes the value of the Type attribute as a new definition table key, but three classes in the java.lang package (Object, String, and Throwable) are represented in this specification by known keys. Therefore it is necessary to override the computation of the Type attribute in a class declaration:

Class and interface types[20]:
SYMBOL TypeDeclaration INHERITS TypeDenotation END;

RULE: TypeDeclaration ::=
        Modifiers 'class' TypeIdDef Super Interfaces ClassBody COMPUTE
  TypeDeclaration.Type=
    IF(NOT(INCLUDING CompilationUnit.IsJavaLang),NewType(),
    IF(EQ(TypeIdDef.Sym,MakeName("Object")),objectType,
    IF(EQ(TypeIdDef.Sym,MakeName("String")),stringType,
    IF(EQ(TypeIdDef.Sym,MakeName("Throwable")),throwableType,
    NewType()))));
END;

ATTR IsJavaLang: int;

RULE: CompilationUnit ::=
        PackageDeclarationOpt
        ImportJavaLang ImportDeclarationsOpt TypeDeclarationsOpt
COMPUTE
  CompilationUnit.IsJavaLang=PackageDeclarationOpt.IsJavaLang;
END;

RULE: PackageDeclarationOpt ::= 'package' PackageName ';' COMPUTE
  PackageDeclarationOpt.IsJavaLang=
    EQ(FullyQualifiedName(PackageName.Ids),MakeName("java.lang"));
END;

RULE: PackageDeclarationOpt ::= COMPUTE
  PackageDeclarationOpt.IsJavaLang=0;
END;
This macro is defined in definitions 17, 19, 20, 21, 22, 23, and 25.
This macro is invoked in definition 14.

Each class and interface type has associated operators, defined by instantiating the OIL class classOps:

Class and interface types[21]:
SYMBOL TypeDeclaration INHERITS OperatorDefs END;

TREE SYMBOL TypeDeclaration COMPUTE
  SYNT.GotOper=
    ORDER(
      InstClass0(classOps,THIS.Type),
      ListOperator(
        FinalType(THIS.Type),
        NoOprName,
        NULLDefTableKeyList,
        THIS.Type));
END;
This macro is defined in definitions 17, 19, 20, 21, 22, 23, and 25.
This macro is invoked in definition 14.

There are also operators associated with inheritance, defined by instantiating classInh:

Class and interface types[22]:
SYMBOL Super INHERITS OperatorDefs END;

RULE: Super ::= 'extends' InhName COMPUTE
  Super.GotOper=
    InstClass1(classInh,INCLUDING TypeDeclaration.Type,InhName.Type);
END;

RULE: Super ::= COMPUTE
  Super.GotOper=
    InstClass1(classInh,INCLUDING TypeDeclaration.Type,objectType);
END;

SYMBOL InterfaceType INHERITS OperatorDefs END;

RULE: InterfaceType ::= InhName COMPUTE
  InterfaceType.GotOper=
    InstClass1(classInh,INCLUDING TypeDeclaration.Type,InhName.Type);
END;
This macro is defined in definitions 17, 19, 20, 21, 22, 23, and 25.
This macro is invoked in definition 14.

We need to keep track of the supertype of each class in order to access it when the keyword super shows up in expressions:

Class and interface types[23]:
TREE SYMBOL TypeDeclaration: SuperType: DefTableKey;

RULE: TypeDeclaration ::=
        Modifiers 'class' TypeIdDef Super Interfaces ClassBody COMPUTE
  TypeDeclaration.SuperType=Super.Type;
END;

RULE: TypeDeclaration ::=
        Modifiers 'interface' TypeIdDef Interfaces InterfaceBody COMPUTE
  TypeDeclaration.SuperType=NoKey;
END;
This macro is defined in definitions 17, 19, 20, 21, 22, 23, and 25.
This macro is invoked in definition 14.

Not all of the contexts in which the supertype is needed are textually enclosed by the type in question. Therefore we need to store the supertype as a property of the type, and ensure that it has been set whenever it is accessed.

Property definitions[24]:
SuperType: DefTableKey;
This macro is defined in definitions 18, 24, 27, and 29.
This macro is invoked in definition 3.

Class and interface types[25]:
TREE SYMBOL Goal COMPUTE
  SYNT.GotSuperTypes=CONSTITUENTS TypeDeclaration.GotSuperType;
END;

TREE SYMBOL TypeDeclaration COMPUTE
  SYNT.GotSuperType=ResetSuperType(THIS.Type,THIS.SuperType);
END;
This macro is defined in definitions 17, 19, 20, 21, 22, 23, and 25.
This macro is invoked in definition 14.


next up previous
Next: Array types Up: Types and type identifiers Previous: Primitive types
2008-09-11