Skip to content

Commit

Permalink
[TableGen] Unify the priority of variables
Browse files Browse the repository at this point in the history
In D148197, we have made `defvar` statement able to refer to class
template arguments. However, the priority of class/multiclass
template argument is higher than variables defined by `defvar`, which
is a little counterintuitive.

In this patch, we unify the priority of variables. Each pair of
braces introduces a new scope, which may contain some additional
variables like template arguments, loop iterators, etc. We can
define local variables inside this scope via `defvar` and these
variables are of higher priority than additional variables. This
means that `defvar` will shadow additional variables with the same
name. The scope can be nested, and we use the innermost variable.

This make variables defined by `defvar` prior to class/multiclass
template arguments, loop iterators, etc. The shadow rules now are:

* `V` in a record body shadows a global `V`.

* `V` in a record body shadows template argument `V`.

* `V` in template arguments shadows a global `V`.

* `V` in a `foreach` statement list shadows any `V` in surrounding record or global scopes.

Reviewed By: tra

Differential Revision: https://reviews.llvm.org/D149016
  • Loading branch information
pcwang-thead committed May 24, 2023
1 parent 0c316f0 commit 45ea4d6
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 144 deletions.
20 changes: 14 additions & 6 deletions llvm/docs/TableGen/ProgRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1375,15 +1375,23 @@ Defvar in a record body

In addition to defining global variables, the ``defvar`` statement can
be used inside the :token:`Body` of a class or record definition to define
local variables. The scope of the variable extends from the ``defvar``
statement to the end of the body. It cannot be set to a different value
within its scope. The ``defvar`` statement can also be used in the statement
local variables. Template arguments of ``class`` or ``multiclass`` can be
used in the value expression. The scope of the variable extends from the
``defvar`` statement to the end of the body. It cannot be set to a different
value within its scope. The ``defvar`` statement can also be used in the statement
list of a ``foreach``, which establishes a scope.

A variable named ``V`` in an inner scope shadows (hides) any variables ``V``
in outer scopes. In particular, ``V`` in a record body shadows a global
``V``, and ``V`` in a ``foreach`` statement list shadows any ``V`` in
surrounding record or global scopes.
in outer scopes. In particular, there are several cases:

* ``V`` in a record body shadows a global ``V``.

* ``V`` in a record body shadows template argument ``V``.

* ``V`` in template arguments shadows a global ``V``.

* ``V`` in a ``foreach`` statement list shadows any ``V`` in surrounding record or
global scopes.

Variables defined in a ``foreach`` go out of scope at the end of
each loop iteration, so their value in one iteration is not available in
Expand Down

0 comments on commit 45ea4d6

Please sign in to comment.