ipr icon indicating copy to clipboard operation
ipr copied to clipboard

Type inequality for forward declarations?

Open Xazax-hun opened this issue 3 years ago • 2 comments

Consider the following code:

struct X;

struct X
{
  void m();
};

It will produce the following IPR:

Namespace 'namespace '
|-Typedecl 'class' X
\-Typedecl 'class' X
  \-Class 'class X'
    \-Fundecl 'void (X *)' m
      \-Parameter 'X *' this

Currently, we are building this representation the following way:

  1. We create a Typedecl node with the name X and the type class (Typedecl(X) : class) for the forward declaration (the init of this Typedecl is null).
  2. We create a new Typedecl node for the definition, which has the same name and type (Typedecl(X) : class) but the init field is the actual class type with all the info from the definiton.

This approach is problematic, for the following reason. We get different types for the forward declaration, and the definition. The type of the forward declaration: AsType(Typedecl(X)) The type of the definition: class X (from the init field of the second Typedecl)

Ideally, we want both the forward declaration and the definition to have the same type. One option would be, to also create an empty class for the forward declaration, but in that case we would either end up having two class types of the same name, or (in case they both refer to the same type that has all its details filled) we cannot tell which declaration is a forward declaration and which one is a definition.

Alternatively, I could also use AsType(Typedecl(X)) for the definition to have the same type, but it would also end up producing different types as we are talking about different Typedecl nodes.

How should we solve this? Do I miss something?

Xazax-hun avatar Sep 30 '21 19:09 Xazax-hun

To my understanding the Type of the declaration (Typedecl) itself is not the Class node but rather the built in 'class' primitive. If you're referring to usages of the Type in other declarations such as Var or Parameter before and after the definition I think we have had a similar discussion in the past. I'm not sure if we came to a final decision on which approach to use but I think there was more leaning towards following the semantics of the language and referring to an incomplete type (AsType(Typedecl(X))) rather than referring to the defined class (or creating an empty class). The user would then use a call a free function to compare the equality of types (which you would have to do for your proposal as well, give different Class nodes are used).

This was a while ago so Gaby or Gor may have a better recollection of the discussion.

d-winsor avatar Sep 30 '21 20:09 d-winsor

If you're referring to usages of the Type in other declarations such as Var or Parameter before and after the definition

Yep, this is exactly what I am referring to. Thanks for the clarification!

Xazax-hun avatar Sep 30 '21 20:09 Xazax-hun