-
Notifications
You must be signed in to change notification settings - Fork 67
Finalize Constant Expressions
This algorithm traverses the source model and recomputes the types and values of constant symbols and expressions, using the finalized type definitions. Doing this fills in any missing anonymous array sizes and updates any type or value that depends on a finalized type definition.
The rules below are those of Evaluate Constant Expressions, except that this algorithm also updates the type map, and that it visits each expression again even if the expression already has a value. The type and the value of any expression not mentioned below are the ones computed by Evaluate Constant Expressions.
-
A list tul of translation units.
-
An analysis data structure a representing the results of analysis so far. Finalize Type Definitions must have already been run.
-
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 with value expression e, visit e and update the type map and the value map with the mappings of d to the type and the value of e.
For each template expansion specifier n, for each constant parameter symbol of n, apply the rule of Evaluate Constant Expressions, except that the type of the parameter type name is already finalized.
For each AST node n that represents a use of a constant:
-
Look in the use-def map of a to get the symbol s corresponding to n.
-
If s is a constant symbol, then visit the constant definition corresponding to s.
-
Otherwise if s is an enumerated constant symbol, then do nothing.
-
Otherwise if s is a constant parameter symbol, then apply the rule stated above for the constant parameters of a template expansion specifier.
-
Otherwise report an error: s is not a constant symbol.
-
Update the type map and the value map with the mappings of n to the type and the value of s.
For each AST node n that represents an array expression:
-
For each element e of n, visit e, so that it has a type and a value.
-
Use the type map to compute the common type E of the elements of n. If n has no elements, then report an error for an empty array.
-
If E is an anonymous array type with no size, then report a type mismatch error.
-
Let k be the number of elements of n, and let v be the anonymous array value whose elements are the values of the elements of n, each converted to E.
-
Update the type map and the value map with the mappings of n to the anonymous array type with size k and element type E, and to v.
For each AST node n that represents an array subscript expression
e1 [ e2 ]:
-
Visit e1 and e2, so that each has a type and a value.
-
Use the value map to get the list L of elements of the array value of e1, and the integer index i of the value of e2.
-
Check that i is greater than or equal to zero and less than the length of L. Evaluate Constant Expressions checks the upper bound only when the size of the array is known; the size is known here, so check it in every case. If i is out of bounds, then report an error for an invalid integer value.
-
Let T be the element type of the underlying type of e1.
-
Update the type map and the value map with the mappings of n to T and to the element of L at index i.
For each AST node n that represents a dot expression e . m:
-
Visit e, so that it has a type and a value.
-
If n appears in the use-def map of a, then n is a use of a symbol, and visiting it has already updated its type and its value. Yield a as the result.
-
Otherwise n selects the member m of a struct value. Let T and v be the type and the value of m in the underlying type and the value of e, or the existing type and value of n if m is not there.
-
Update the type map and the value map with the mappings of n to T and v.
For each AST node n that represents a struct expression:
-
For each member m of n, visit the value expression of m, so that it has a type and a value.
-
Let T and v be the anonymous struct type and the anonymous struct value that map the name of each member of n to the type and the value of its value expression.
-
Update the type map and the value map with the mappings of n to T and v.