-
Notifications
You must be signed in to change notification settings - Fork 32
Evaluate Constant Expressions
This algorithm traverses the source model and computes the values of constant symbols and expressions.
-
A list tul of translation units.
-
An analysis data structure a representing the results of analysis so far. Evaluate Implied Enum Constants must have already been run.
-
The analysis a with an updated value 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 constant definition d:
-
If d is not in the value map of a, then
-
Visit each expression appearing in d, threading the analysis through the visits. Let a' be the resulting analysis.
-
Use the value map of a' to compute the value v of d.
-
Let a'' be the analysis data structure that results from adding the mapping from d to v to the value map of a'.
-
Yield a'' as the result.
-
-
Otherwise we have already visited d; yield a as the result.
For each enum definition d, check that the enumerated constants of d all have different values.
For each AST node n that represents an expression:
-
Compute the value v of n:
-
If the value of n is directly available (for example, the value of the integer literal
1
is 1), then use that as v. -
Otherwise if n represents a use, then
-
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 value map of a to get the value v of s. Throw an internal error if it is not there.
-
Use v as the value of n.
-
-
Otherwise
-
Visit the child expressions of v.
-
Use the child values to compute the value v of n.
-
-
-
Update the value map with the mapping of n to v.