Skip to content

Finalize Type Definitions

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

This algorithm traverses the source model and finalizes the definitions of types.

Input

  1. A list tul of translation units.

  2. An analysis data structure a representing the results of analysis so far. Evaluate Constant Expressions must have already been run.

Output

  1. The analysis a with an updated type map.

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.

Enum Definitions

For each enum definition d that has not yet been visited, use the value map to compute the default value of d. Put the default value into the type.

Array Definitions

For each array definition d that has not yet been visited:

  1. Use the type map to look up the array type A associated with d.

  2. Visit the anonymous array type A' in the array type A to update the members of A'.

  3. Use the value map to fill in the size in A'.

  4. Check that the size is greater than or equal to zero and less than or equal to the maximum allowed value.

  5. If there is a default expression e

    1. Ensure that its type matches the updated array type A.

    2. Compute the value v of e at type A and let v be the default value.

  6. Otherwise use the default value for A' to construct the default value.

  7. Update the default value in the array type A.

  8. Update the format specifier in the array type A and check the specifier for errors.

Struct Definitions

For each struct definition d that has not yet been visited:

  1. Use the type map to look up the struct type S associated with d.

  2. Visit the anonymous struct type S' in S to update the members of S'.

  3. If there is a default expression e

    1. Ensure that its type matches the updated struct type S.

    2. Compute the value v of e at type S and let v be the default value.

  4. Otherwise use the default value for S' to construct the default value.

  5. Update the default value in the struct type S.

  6. Update the format specifiers in the struct type S and check the specifiers for errors.

Types

For each type t:

  1. If t is a string type, then check that the size is greater than or equal to zero.

  2. If t refers to a struct, array, or enum definition d, then

    1. Visit d

    2. Use the updated type of d to update the array sizes, default values, and format specifiers appearing in t.