Skip to content

Check Type Uses

Robert L. Bocchino Jr. edited this page Apr 18, 2024 · 3 revisions

This algorithm traverses a source model and computes the types of type definition symbols, enumerated constant symbols, and type names, except that array size expressions and default value expressions are still unevaluated. As the algorithm computes types, it checks for typing errors.

Input

  1. A list tul of translation units.

  2. An analysis data structure a representing the results of analysis so far. The use-def map must already be computed.

Output

  1. The analysis a with an updated type map, if the check passes; otherwise an error.

Procedure

  1. Visit each translation unit in tul with input a, yielding either a new analysis a' or an error.

AST Visitor Methods

Each method accepts an analysis data structure a as input and yields either a new analysis data structure a' or an error as output.

Translation Units

For each translation unit tu, visit each definition appearing in tu.

Definitions

For each definition d that defines a type:

  1. If d is not in the type map of a, then

    1. Visit each type name appearing in d, threading the analysis through the visits. Let a' be the resulting analysis.

    2. Check that d obeys all of its typing rules. Throw an error if not.

    3. Use information in the type map of a' to compute the type of d.

    4. Let a'' be the analysis data structure that results from adding the type of d to a'. In the case of enum definitions, also map each enumerated constant in d to the enum type of d.

    5. Yield a'' as the result.

  2. Otherwise we have already visited d; yield a as the result.

Type Names

For each AST node n that represents a type name:

  1. Compute the type T of n:

    1. If the type of n is directly available (for example, the type of U32 is U32), then use that as T.

    2. Otherwise

      1. Look in the use-def map of n to get the symbol s corresponding to n. Throw an internal error if it is not there.

      2. Visit the definition corresponding to s.

      3. Look in the type map of a to get the type T of s. Throw an internal error if it is not there.

      4. Use T as the type of n.

  2. Update the type map with the mapping of n to T.

Type Definitions

Because array size expressions and default value expressions are unevaluated, the following rules apply to the typing of array, struct, and enum definitions:

  1. An array type may have None in its size field.

    1. When comparing two array types, None matches any size, including None.

    2. When computing the common type of two array types T1 and T2, if either T1 or T2 has None in its size field, then the common type does as well.

  2. Array, struct, and enum types may have a default value of None.

A later pass will fill in the missing values.