public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Uecker <uecker@tugraz.at>
To: gcc-patches@gcc.gnu.org
Cc: Joseph Myers <josmyers@redhat.com>,
	Richard Biener <rguenther@suse.de>,
	 Jakub Jelinek <jakub@redhat.com>,
	Ian Lance Taylor <iant@golang.org>
Subject: [C PATCH]: allow aliasing of compatible types derived from enumeral types [PR115157]
Date: Tue, 21 May 2024 14:18:48 +0200	[thread overview]
Message-ID: <e367dd44ee5634f2ae3653b322339f8d61aa039a.camel@tugraz.at> (raw)
In-Reply-To: <45067a3258e0ec1677ed95f62ac21c1353d66355.camel@tugraz.at>


For enum and integer we allow aliasing by specifically returning
via a langhook the aliasing set of the underlying type.
But this is not sufficient for derived types, i.e. pointers to
enums and pointers to compatible integers also need to have the
same aliasing set.

We also allow forward declarations of enums which is a GNU 
extension, but I think this has to work consistently too, so
we here have the same issue as in C23 with other tagged types.

The solution in this patch is similar to what we do in C23, i.e. 
we start out with structural equality and then set TYPE_CANONICAL 
to the underlying type. The only way to make the TYPE_CANONICAL 
system work with the C rules for type compatility seems to set 
TYPE_CANONICAL to the same type for all types in a compatibility
equivalence class (as compatibility is not transitive this puts
together similar types that are not compatible). This is the
underlying type in this case.  As all types in such an equivalence
class have the same representation, so this should always work 
in my opinion (but maybe there is some middle end aspects I am
still missing).


When testing, I so far only found two minor issues, i.e. when
computing the 'aka' type in diagnostics and an issue with
godump.cc (not sure I fixed this correctly).


Beyond this patch, we need also some change for function types 
in general and there are problably also some other issues related
to incomplete arrays as well  (I added some checking to 'comptypes'
to check that all types ruled compatible by the C FE also have 
either structural equality, or have the same TYPE_CANONICAL, and
this brings up some more inconsistencies).

Thoughts?


Bootstrapped and regression tested on x86_64 (only C, C++ so far).




    C: allow aliasing of compatible types derived from enumeral types [PR115157]
    
    Aliasing of enumeral types with the underlying integer is now allowed
    by setting the aliasing set to zero.  But this does not allow aliasing
    of derived types which are compatible as required by ISO C.  Instead,
    initially set structural equality.  Then set TYPE_CANONICAL and update
    pointers and main variants when the type is completed (as done for
    structures and unions in C23).
    
    PR 115157
    
    gcc/c/
            * c-decl.cc (shadow_tag-warned,parse_xref_tag,start_enum,
            finish_enum): Set SET_TYPE_STRUCTURAL_EQUALITY / TYPE_CANONICAL.
            * c-obj-common.cc (get_alias_set): Remove special case.
            (get_aka_type): Add special case.
    
    gcc/
            * godump.cc (go_output_typedef): use TYPE_MAIN_VARIANT instead
            of TYPE_CANONICAL.
    
    gcc/testsuite/
            * gcc.dg/enum-alias-1.c: New test.
            * gcc.dg/enum-alias-2.c: New test.
            * gcc.dg/enum-alias-3.c: New test.

diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index b691b91b3db..6e6606c9570 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -5051,7 +5051,7 @@ shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
 	      if (t == NULL_TREE)
 		{
 		  t = make_node (code);
-		  if (flag_isoc23 && code != ENUMERAL_TYPE)
+		  if (flag_isoc23 || code == ENUMERAL_TYPE)
 		    SET_TYPE_STRUCTURAL_EQUALITY (t);
 		  pushtag (input_location, name, t);
 		}
@@ -8828,7 +8828,7 @@ parser_xref_tag (location_t loc, enum tree_code code, tree name,
      the forward-reference will be altered into a real type.  */
 
   ref = make_node (code);
-  if (flag_isoc23 && code != ENUMERAL_TYPE)
+  if (flag_isoc23 || code == ENUMERAL_TYPE)
     SET_TYPE_STRUCTURAL_EQUALITY (ref);
   if (code == ENUMERAL_TYPE)
     {
@@ -9919,6 +9919,7 @@ start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
     {
       enumtype = make_node (ENUMERAL_TYPE);
       TYPE_SIZE (enumtype) = NULL_TREE;
+      SET_TYPE_STRUCTURAL_EQUALITY (enumtype);
       pushtag (loc, name, enumtype);
       if (fixed_underlying_type != NULL_TREE)
 	{
@@ -9935,6 +9936,8 @@ start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
 	  TYPE_SIZE (enumtype) = NULL_TREE;
 	  TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
 	  ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
+	  TYPE_CANONICAL (enumtype) = TYPE_CANONICAL (fixed_underlying_type);
+	  c_update_type_canonical (enumtype);
 	  layout_type (enumtype);
 	}
     }
@@ -10094,6 +10097,10 @@ finish_enum (tree enumtype, tree values, tree attributes)
       ENUM_UNDERLYING_TYPE (enumtype) =
 	c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
 
+      TYPE_CANONICAL (enumtype) =
+	TYPE_CANONICAL (ENUM_UNDERLYING_TYPE (enumtype));
+      c_update_type_canonical (enumtype);
+
       layout_type (enumtype);
     }
 
diff --git a/gcc/c/c-objc-common.cc b/gcc/c/c-objc-common.cc
index b7c72d2609c..551ec6f4b65 100644
--- a/gcc/c/c-objc-common.cc
+++ b/gcc/c/c-objc-common.cc
@@ -130,6 +130,8 @@ get_aka_type (tree type)
 
       result = get_aka_type (orig_type);
     }
+  else if (TREE_CODE (type) == ENUMERAL_TYPE)
+    return type;
   else
     {
       tree canonical = TYPE_CANONICAL (type);
@@ -418,11 +420,6 @@ c_var_mod_p (tree x, tree fn ATTRIBUTE_UNUSED)
 alias_set_type
 c_get_alias_set (tree t)
 {
-  /* Allow aliasing between enumeral types and the underlying
-     integer type.  This is required since those are compatible types.  */
-  if (TREE_CODE (t) == ENUMERAL_TYPE)
-    return get_alias_set (ENUM_UNDERLYING_TYPE (t));
-
   /* Structs with variable size can alias different incompatible
      structs.  Let them alias anything.   */
   if (RECORD_OR_UNION_TYPE_P (t) && C_TYPE_VARIABLE_SIZE (t))
diff --git a/gcc/godump.cc b/gcc/godump.cc
index 66e73ade7df..372871a59ec 100644
--- a/gcc/godump.cc
+++ b/gcc/godump.cc
@@ -1121,7 +1121,7 @@ go_output_typedef (class godump_container *container, tree decl)
       && !container->decls_seen.contains (TREE_TYPE (decl))
       && (TYPE_CANONICAL (TREE_TYPE (decl)) == NULL_TREE
 	  || !container->decls_seen.contains
-				    (TYPE_CANONICAL (TREE_TYPE (decl)))))
+				    (TYPE_MAIN_VARIANT (TREE_TYPE (decl)))))
     {
       tree element;
 
diff --git a/gcc/testsuite/gcc.dg/enum-alias-1.c b/gcc/testsuite/gcc.dg/enum-alias-1.c
new file mode 100644
index 00000000000..8fa30eb7897
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/enum-alias-1.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+enum E { E1 = -1, E2 = 0, E3 = 1 };
+
+typedef int A;
+typedef enum E B;
+
+_Static_assert(_Generic((A){ 0 }, B: 1), "");
+
+void* foo(void* a, void *b, A *c, B *d)
+{
+	*(A**)a = c;
+	*(B**)b = d;
+	return *(A**)a;
+}
+
+int main()
+{
+	A *a, b, c;
+	if (&c != (A*)foo(&a, &a, &b, &c))
+		__builtin_abort();
+}
+
diff --git a/gcc/testsuite/gcc.dg/enum-alias-2.c b/gcc/testsuite/gcc.dg/enum-alias-2.c
new file mode 100644
index 00000000000..7ca3f3b2db8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/enum-alias-2.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+typedef int A;
+
+void* foo(void* a, void *b, void *c, void *d)
+{
+	*(A**)a = c;
+
+	{
+		typedef enum E B;
+		enum E { E1 = -1, E2 = 0, E3 = 1 };
+		*(B**)b = d;
+	}
+
+	return *(A**)a;
+}
+
+int main()
+{
+	A *a, b, c;
+	if (&c != (A*)foo(&a, &a, &b, &c))
+		__builtin_abort();
+}
+
diff --git a/gcc/testsuite/gcc.dg/enum-alias-3.c b/gcc/testsuite/gcc.dg/enum-alias-3.c
new file mode 100644
index 00000000000..36a4f02a455
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/enum-alias-3.c
@@ -0,0 +1,26 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -flto" } */
+
+typedef int *A;
+
+void* foo(void* a, void *b, void *c, void *d)
+{
+	*(A**)a = c;
+
+	typedef enum E *B;
+	enum E { E1 = -1, E2 = 0, E3 = 1 };
+	{
+		*(B**)b = d;
+	}
+
+	return *(A**)a;
+}
+
+int main()
+{
+	A *a, b, c;
+	if (&c != (A*)foo(&a, &a, &b, &c))
+		__builtin_abort();
+}
+
+





  parent reply	other threads:[~2024-05-21 12:18 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-16 21:36 c23 type compatibility rules, v3 Martin Uecker
2023-11-16 21:38 ` [PATCH 1/4] c23: tag compatibility rules for struct and unions Martin Uecker
2023-11-23 23:17   ` Joseph Myers
2023-11-16 21:38 ` [PATCH 2/4] c23: tag compatibility rules for enums Martin Uecker
2023-11-23 23:26   ` Joseph Myers
2023-11-16 21:39 ` [PATCH 3/4] c23: aliasing of compatible tagged types Martin Uecker
2023-11-23 23:47   ` Joseph Myers
2023-11-26 22:48     ` Martin Uecker
2023-11-27  7:46       ` Richard Biener
2023-11-28  1:00       ` Joseph Myers
2023-11-28  6:49         ` Martin Uecker
2023-11-28 10:47         ` Richard Biener
2023-11-28 11:51           ` Martin Uecker
2023-11-16 21:40 ` [PATCH 4/4] c23: construct composite type for " Martin Uecker
2023-11-27 13:16 ` [V4] [C PATCH 1/4] c23: tag compatibility rules for struct and unions Martin Uecker
2023-12-14 20:53   ` Joseph Myers
2023-11-27 13:16 ` [V4] [PATCH 2/4] c23: tag compatibility rules for enums Martin Uecker
2023-12-14 20:58   ` Joseph Myers
2023-11-27 13:16 ` [V4] [PATCH 3/4] c23: aliasing of compatible tagged types Martin Uecker
2023-12-14 21:10   ` Joseph Myers
2023-11-27 13:16 ` [V4] [PATCH 4/4] c23: construct composite type for " Martin Uecker
2023-12-17 17:41 ` [V5] [C PATCH 1/4] c23: tag compatibility rules for struct and unions Martin Uecker
2023-12-19 21:48   ` Joseph Myers
2023-12-17 17:42 ` [V5] [C PATCH 2/4] c23: tag compatibility rules for enums Martin Uecker
2023-12-19 21:50   ` Joseph Myers
2023-12-17 17:42 ` [V5] [C PATCH 3/4] c23: aliasing of compatible tagged types Martin Uecker
2023-12-19 22:02   ` Joseph Myers
2023-12-17 17:42 ` [V5] [C PATCH 4/4] c23: construct composite type for " Martin Uecker
2023-12-19 22:25   ` Joseph Myers
2023-12-21 21:47 ` [V6] " Martin Uecker
2023-12-22 16:27   ` Joseph Myers
2023-12-27 19:23 ` [C PATCH] C: Fix type compatibility for structs with variable sized fields Martin Uecker
2023-12-29 15:57   ` Joseph Myers
2024-01-27 16:10 ` Fix ICE with -g and -std=c23 when forming composite types [PR113438] Martin Uecker
2024-01-29 20:27   ` Joseph Myers
2024-04-02 18:54 ` [C PATCH] Fix ICE with -g and -std=c23 related to incomplete types [PR114361] Martin Uecker
2024-04-02 20:31   ` Joseph Myers
2024-04-02 19:02 ` [C PATCH] fix aliasing for structures/unions with incomplete types Martin Uecker
2024-04-02 20:42   ` Joseph Myers
2024-04-02 21:22     ` Martin Uecker
2024-04-03 15:33       ` Joseph Myers
2024-04-14 12:30 ` [C PATCH, v2] Fix ICE with -g and -std=c23 related to incomplete types [PR114361] Martin Uecker
2024-04-14 12:38   ` Martin Uecker
2024-04-15  6:55   ` Richard Biener
2024-04-15  7:38     ` Jakub Jelinek
2024-04-15  7:59       ` Jakub Jelinek
2024-04-15  8:02         ` Richard Biener
2024-04-15  8:05           ` Jakub Jelinek
2024-04-15 10:48             ` [PATCH] c, v3: " Jakub Jelinek
2024-04-15 11:33               ` Richard Biener
2024-04-16  7:20                 ` Jakub Jelinek
2024-04-19 20:39               ` Joseph Myers
2024-04-15  7:03   ` [C PATCH, v2] " Jakub Jelinek
2024-05-18 13:27 ` [C PATCH] Fix for some variably modified types not being recognized [PR114831] Martin Uecker
2024-05-18 13:29   ` Martin Uecker
2024-05-20 21:18   ` Joseph Myers
2024-05-18 20:18 ` [C PATCH] Fix for redeclared enumerator initialized with different type [PR115109] Martin Uecker
2024-05-19 10:24   ` [C PATCH, v2] " Martin Uecker
2024-05-20 21:30     ` Joseph Myers
2024-05-21  5:40       ` Martin Uecker
2024-05-23 20:51         ` Joseph Myers
2024-05-21 12:18   ` Martin Uecker [this message]
2024-05-23 20:59     ` [C PATCH]: allow aliasing of compatible types derived from enumeral types [PR115157] Joseph Myers
2024-05-23 21:30       ` Ian Lance Taylor
2024-05-23 21:47         ` Martin Uecker
2024-05-23 21:53           ` Ian Lance Taylor
2024-05-24  5:56             ` Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e367dd44ee5634f2ae3653b322339f8d61aa039a.camel@tugraz.at \
    --to=uecker@tugraz.at \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=iant@golang.org \
    --cc=jakub@redhat.com \
    --cc=josmyers@redhat.com \
    --cc=rguenther@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).