Skip to content

Expand Templates

Andrei Tumbar edited this page Sep 24, 2026 · 3 revisions

This algorithm traverses a source model and expands each template expansion specifier by copying the members of the module template definition that it refers to. It also checks that templates are not expanded or defined in disallowed positions.

Input

  1. A list tul of translation units.

  2. An analysis data structure a representing the results of analysis so far. The use-def map of a must contain an entry for the template name used in each template expansion specifier. Check template uses establishes this condition.

Output

  1. A list tul' of translation units in which each template expansion specifier carries a copy of the members of the template it expands.

Each template expansion specifier in tul' has the same identity as the corresponding specifier in tul.

Copying

To copy an AST node is to produce a new node with the same content but a fresh, distinct identity, recursively copying its descendants. The location of each copied node records the template expansion specifier that produced it, called the expansion point. Recording the expansion point ensures that (a) two expansions of the same template yield distinct definitions and (b) diagnostics for the copied definitions can refer back to the point of expansion.

Copying occurs if and only if the current template member of a is present; elsewhere the traversal leaves each node, and so each node identity, unchanged.

Procedure

  1. Translation unit lists: Visit each translation unit tu in tul in order.

  2. Translation units: Visit a translation unit tu by visiting its members tum.

  3. Translation unit members: Visit a translation unit member tum as follows:

    1. Module definitions: If tum is a module definition d, then visit each member of d.

    2. Template expansion specifiers: If tum is a template expansion specifier te, then

      1. If the current template definition member of a is present, then report an error for a template expansion inside a template, citing the location of the enclosing template definition.

      2. Otherwise

        1. Look up the module template definition t that the template name of te refers to using the use-def map of a. Report an error if the name does not resolve to a template symbol.

        2. Set the current template member of a to te and the current template definition member of a to t.

        3. Copy each member of t.

        4. Restore the current template and current template definition members of a.

        5. Replace te with a specifier that has the same identity as te and that carries the copied members.

    3. Module template definitions: If tum is a module template definition t, then

      1. If the current template definition member of a is present, then t is enclosed in another module template definition. Report an error for a nested template definition, citing the location of the enclosing definition.

      2. Otherwise set the current template definition member of a to t, visit the parameters and the members of t, and restore the current template definition member of a.

    4. Other translation unit members: For any other translation unit member m, visit m.

Clone this wiki locally