-
Notifications
You must be signed in to change notification settings - Fork 31
Check Type Uses
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.
-
A list tul of translation units.
-
An analysis data structure a representing the results of analysis so far. The use-def map must already be computed.
-
The analysis a with an updated type map, if the check passes; otherwise an error.
-
Visit each translation unit in tul with input a, yielding either a new analysis a' or an error.
Each method accepts an analysis data structure a as input and yields either a new analysis data structure a' or an error as output.
For each definition d that defines a type:
-
If d is not in the type map of a, then
-
Visit each type name appearing in d, threading the analysis through the visits. Let a' be the resulting analysis.
-
Check that d obeys all of its typing rules. Throw an error if not.
-
Use information in the type map of a' to compute the type of d.
-
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.
-
Yield a'' as the result.
-
-
Otherwise we have already visited d; yield a as the result.
For each AST node n that represents a type name:
-
Compute the type T of n:
-
If the type of n is directly available (for example, the type of
U32
isU32
), then use that as T. -
Otherwise
-
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.
-
Visit the definition corresponding to s.
-
Look in the type map of a to get the type T of s. Throw an internal error if it is not there.
-
Use T as the type of n.
-
-
-
Update the type map with the mapping of n to T.
Because array size expressions and default value expressions are unevaluated, the following rules apply to the typing of array, struct, and enum definitions:
-
An array type may have
None
in its size field.-
When comparing two array types,
None
matches any size, includingNone
. -
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.
-
-
Array, struct, and enum types may have a default value of
None
.
A later pass will fill in the missing values.