Skip to content

Check Expression Types

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

This algorithm traverses the source model and computes the types of constant symbols and expressions, except that array size expressions and default value expressions in type definitions 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. Check Type Uses must have already been run.

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 constant definition d:

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

    1. Visit each expression 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'.

    5. Yield a'' as the result.

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

Type Names

For each string type name that has a size expression, check that the size expression is a numeric type.

Expressions

For each AST node n that represents an expression:

  1. Compute the type T of n:

    1. If the type of n is directly available (for example, the type of an integer literal expression is Integer), then use that as T.

    2. Otherwise if n represents a use, then

      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.

    3. Otherwise

      1. Visit the child expressions of n.

      2. Look in the type map to get the types of the children.

      3. Check that n obeys all of its typing rules. Throw an error if not.

      4. Compute the type T of n.

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