GabrielDosReis / ipr

Compiler-neutral Internal Program Representation for C++
BSD 3-Clause "New" or "Revised" License
222 stars 23 forks source link

How to represent variable templates? #153

Open Xazax-hun opened 3 years ago

Xazax-hun commented 3 years ago

I wanted to create a mapping from template parameters to Var with a Forall type, but I encountered some difficulties.

GabrielDosReis commented 3 years ago

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.

Xazax-hun commented 3 years ago

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.

GabrielDosReis commented 3 years ago

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?

Xazax-hun commented 3 years ago

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
GabrielDosReis commented 3 years ago

See also conversation in #122

GabrielDosReis commented 2 years ago

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

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

Also see #191 .