Next: , Previous: Predefined, Up: BasicType


1.2 Establishing the type of an entity

Variables of integer type are are allowed, and procedures can be declared with integer parameters and integer results. Thus Mystery defines a keyword INTEGER to denote the type in declarations. The following rule associates the known key with that keyword:

     RULE: Type ::= 'INTEGER'
     COMPUTE Type.Type=intType;
     END;

Mystery does not allow the user to declare Boolean variables or parameters, and thus there is no such rule relating the known key boolType with a keyword.

Language-defined types could also be denoted by pre-defined identifiers (as in Pascal). This approach increases the complexity of the specification by introducing type identifiers (see Type identifiers). It also allows a user to re-define the names of language-defined types as names of variables or procedure parameters, making programs very hard to understand. We recommend using keywords to denote language-defined types.

TypedDefId is a defining occurrence of an identifier representing a typed entity. The value of the TypedDefId.Type attribute could be set to the definition table key of the type by a user computation in the upper context of TypedDefId. A default computation is provided, however, for the common case where the defining occurrence is a component of a definition construct.

Information characterizing a typed entity is often stored as properties of the definition table key that is the value of TypedDefId.|KEY|Key. The post-condition TypedDefId.GotProp establishes the fact that such properties have been set, but it can only be used for computations that are themselves independent of any aspect of type analysis (see Dependences for typed entities).

TypedDefinition is a context where one or more typed identifiers are defined with the same type. The value of the TypedDefinition.Type attribute must be set by a user computation to the definition table key of the type. Default computations in descendent TypedDefId constructs will access that attribute.

Consider the declaration of an integer variable in Mystery:

     VAR x: INTEGER;

Here is the type analysis computation for the Mystery constructs making up this declaration:

     SYMBOL VrblDecl INHERITS TypedDefinition END;
     SYMBOL VarIdDef INHERITS TypedDefId      END;
     
     RULE: VrblDecl ::= 'VAR' VarIdDef ':' Type
     COMPUTE VrblDecl.Type=Type.Type;
     END;

VrblDecl is a definition context for typed entities, and therefore inherits the TypedDefinition role. Its VarIdDef component is a defining occurrence of a variable identifier, playing the TypedDefId role. The user computation that sets VrblDecl.Type propagates the known key value intType up the tree from the instance of the keyword INTEGER (see Establishing language-defined types).

The same inheritance and computation could be used if the variable declaration in Mystery were extended to allow any number of variables to be declared with the same type:

     RULE: VrblDecl ::= 'VAR' VarIds ':' Type
     COMPUTE VrblDecl.Type=Type.Type;
     END;
     
     RULE: VarIds LISTOF VarIdDef END;