GabrielDosReis/ipr

How to represent variable templates?

Opened this issue · 6 comments

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 same Var node twice in the tree. Once as the child of the Mapping, and once directly in the scope of the Region.

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:

  1. Create the name and the forall type for the template
  2. Create the template via Region::declare_primary_template
  3. Now I need to create a Var that I can use as the body of the template, and the only way to get it (as far as I understand), is to do Region::declare_var. But if I invoke Region::declare_var, I will have the Var node twice in the tree. Once as a member of the Region, and once, as the body of the template I created via Region::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 calling declare_var() on the region member related to the attendant().
  • attendant: the scope returned by the region member of ipr::impl::Where.

Note: the Where node becomes the result() of the ipr::Template (formerly ipr::Named_map).

Also see #191 .