ipr
ipr copied to clipboard
How to represent variable templates?
I wanted to create a mapping from template parameters to Var with a Forall
type, but I encountered some difficulties.
- What should be the target type for Forall?
- What should be the type for the Var node under the mapping?
- How should that Var node be created?
Region::declare_var()
will add the var to the scope in the Region. As a result, I will end up having the sameVar
node twice in the tree. Once as the child of theMapping
, and once directly in the scope of theRegion
.
What should be the target type for Forall?
The target()
of the Forall
type node should be the declared type of the current instantiation of the variable template declaration. That is, for example, in
template<typename T>
constexpr T* nil = nullptr;
the target()
node should designate the representation of the type const T*
.
What should be the type for the Var node under the mapping?
In the example above, the type node should be the representation of const T*
-- that is the type of the current instantiation.
How should that Var node be created? Region::declare_var() will add the var to the scope in the Region. As a result, I will end up having the same Var node twice in the tree. Once as the child of the Mapping, and once directly in the scope of the Region.
Well, there are two declarations in that "single template declaration": (a) the declaration of the template (represented by a Named_map
node, with name nil
), and the declaration of the current instantiation (represented by a Var
node, with name nil<T>
). They should be distinct names and of distinct node types. You shouldn't invoke declare_var()
on both.
You shouldn't invoke declare_var() on both.
I did not invoke it on both, but this is not the problem. I would expect to have IPR like:
\- Named_map
\- Var
So only Named_map
is part of the parent Region
, but Var
is not. But the only way to create a Var
node is to invoke declare_var
on a Region
, which will also add the Var
to the region. Ending up in a tree like:
|- Named_map
| \- Var
\- Var
So I only called declare_var
once, but as a result, I have the same Var
at two places in the tree.
So I only called declare_var once, but as a result, I have the same Var at two places in the tree.
On which node?
There is a Region
, where I want to put the variable template.
I do the following:
- Create the name and the forall type for the template
- Create the template via
Region::declare_primary_template
- Now I need to create a
Var
that I can use as thebody
of the template, and the only way to get it (as far as I understand), is to doRegion::declare_var
. But if I invokeRegion::declare_var
, I will have theVar
node twice in the tree. Once as a member of theRegion
, and once, as thebody
of the template I created viaRegion::declare_primary_template
See also conversation in #122
The proposed resolution for this (and many other declarative templates): use a Where
node (whose parent region is the template parameter scope region of the Template
) with
- main: the current instantiation, e.g.
Var<T>
. That current instantiation can be constructed by callingdeclare_var()
on the region member related to theattendant()
. - attendant: the scope returned by the
region
member ofipr::impl::Where
.
Note: the Where
node becomes the result()
of the ipr::Template
(formerly ipr::Named_map
).
Also see #191 .