diff mbox series

[1/1] PR100281 C++: Fix SImode pointer handling

Message ID 20210430063232.12903-1-krebbel@linux.ibm.com
State New
Headers show
Series [1/1] PR100281 C++: Fix SImode pointer handling | expand

Commit Message

Andreas Krebbel April 30, 2021, 6:32 a.m. UTC
The problem appears to be triggered by two locations in the front-end
where non-POINTER_SIZE pointers aren't handled right now.

1. An assertion in strip_typedefs is triggered because the alignment
of the types don't match. This in turn is caused by creating the new
type with build_pointer_type instead of taking the type of the
original pointer into account.

2. An assertion in cp_convert_to_pointer is triggered which expects
the target type to always have POINTER_SIZE.

Bootstrapped and regression tested on x86_64 and s390x.

Ok for mainline?

gcc/cp/ChangeLog:

	PR c++/100281
	* cvt.c (cp_convert_to_pointer): Use the size of the target
	pointer type.
	* tree.c (cp_build_reference_type): Call
	cp_build_reference_type_for_mode with VOIDmode.
	(cp_build_reference_type_for_mode): Rename from
	cp_build_reference_type.  Add MODE argument and invoke
	build_reference_type_for_mode if MODE isn't VOIDmode.
	(strip_typedefs): Use build_pointer_type_for_mode and
	cp_build_reference_type_for_mode for pointers and references.

gcc/testsuite/ChangeLog:

	PR c++/100281
	* g++.target/s390/pr100281-1.C: New test.
	* g++.target/s390/pr100281-2.C: New test.
---
 gcc/cp/cvt.c                               |  2 +-
 gcc/cp/tree.c                              | 28 +++++++++++++++++-----
 gcc/testsuite/g++.target/s390/pr100281-1.C | 10 ++++++++
 gcc/testsuite/g++.target/s390/pr100281-2.C |  9 +++++++
 4 files changed, 42 insertions(+), 7 deletions(-)
 create mode 100644 gcc/testsuite/g++.target/s390/pr100281-1.C
 create mode 100644 gcc/testsuite/g++.target/s390/pr100281-2.C

Comments

Andreas Krebbel May 12, 2021, 8:35 a.m. UTC | #1
Ping

On 4/30/21 8:32 AM, Andreas Krebbel via Gcc-patches wrote:
> The problem appears to be triggered by two locations in the front-end
> where non-POINTER_SIZE pointers aren't handled right now.
> 
> 1. An assertion in strip_typedefs is triggered because the alignment
> of the types don't match. This in turn is caused by creating the new
> type with build_pointer_type instead of taking the type of the
> original pointer into account.
> 
> 2. An assertion in cp_convert_to_pointer is triggered which expects
> the target type to always have POINTER_SIZE.
> 
> Bootstrapped and regression tested on x86_64 and s390x.
> 
> Ok for mainline?
> 
> gcc/cp/ChangeLog:
> 
> 	PR c++/100281
> 	* cvt.c (cp_convert_to_pointer): Use the size of the target
> 	pointer type.
> 	* tree.c (cp_build_reference_type): Call
> 	cp_build_reference_type_for_mode with VOIDmode.
> 	(cp_build_reference_type_for_mode): Rename from
> 	cp_build_reference_type.  Add MODE argument and invoke
> 	build_reference_type_for_mode if MODE isn't VOIDmode.
> 	(strip_typedefs): Use build_pointer_type_for_mode and
> 	cp_build_reference_type_for_mode for pointers and references.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR c++/100281
> 	* g++.target/s390/pr100281-1.C: New test.
> 	* g++.target/s390/pr100281-2.C: New test.
> ---
>  gcc/cp/cvt.c                               |  2 +-
>  gcc/cp/tree.c                              | 28 +++++++++++++++++-----
>  gcc/testsuite/g++.target/s390/pr100281-1.C | 10 ++++++++
>  gcc/testsuite/g++.target/s390/pr100281-2.C |  9 +++++++
>  4 files changed, 42 insertions(+), 7 deletions(-)
>  create mode 100644 gcc/testsuite/g++.target/s390/pr100281-1.C
>  create mode 100644 gcc/testsuite/g++.target/s390/pr100281-2.C
> 
> diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
> index f1687e804d1..7fa6e8df52b 100644
> --- a/gcc/cp/cvt.c
> +++ b/gcc/cp/cvt.c
> @@ -232,7 +232,7 @@ cp_convert_to_pointer (tree type, tree expr, bool dofold,
>      {
>        if (TYPE_PRECISION (intype) == POINTER_SIZE)
>  	return build1 (CONVERT_EXPR, type, expr);
> -      expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr,
> +      expr = cp_convert (c_common_type_for_size (TYPE_PRECISION (type), 0), expr,
>  			 complain);
>        /* Modes may be different but sizes should be the same.  There
>  	 is supposed to be some integral type that is the same width
> diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
> index a8bfd5fc053..3817b499e46 100644
> --- a/gcc/cp/tree.c
> +++ b/gcc/cp/tree.c
> @@ -1201,12 +1201,14 @@ vla_type_p (tree t)
>    return false;
>  }
>  
> -/* Return a reference type node referring to TO_TYPE.  If RVAL is
> +
> +/* Return a reference type node of MODE referring to TO_TYPE.  If MODE
> +   is VOIDmode the standard pointer mode will be picked.  If RVAL is
>     true, return an rvalue reference type, otherwise return an lvalue
>     reference type.  If a type node exists, reuse it, otherwise create
>     a new one.  */
>  tree
> -cp_build_reference_type (tree to_type, bool rval)
> +cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
>  {
>    tree lvalue_ref, t;
>  
> @@ -1219,7 +1221,11 @@ cp_build_reference_type (tree to_type, bool rval)
>        to_type = TREE_TYPE (to_type);
>      }
>  
> -  lvalue_ref = build_reference_type (to_type);
> +  if (mode == VOIDmode)
> +    lvalue_ref = build_reference_type (to_type);
> +  else
> +    lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
> +
>    if (!rval)
>      return lvalue_ref;
>  
> @@ -1245,7 +1251,7 @@ cp_build_reference_type (tree to_type, bool rval)
>      SET_TYPE_STRUCTURAL_EQUALITY (t);
>    else if (TYPE_CANONICAL (to_type) != to_type)
>      TYPE_CANONICAL (t) 
> -      = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
> +      = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
>    else
>      TYPE_CANONICAL (t) = t;
>  
> @@ -1255,6 +1261,16 @@ cp_build_reference_type (tree to_type, bool rval)
>  
>  }
>  
> +/* Return a reference type node referring to TO_TYPE.  If RVAL is
> +   true, return an rvalue reference type, otherwise return an lvalue
> +   reference type.  If a type node exists, reuse it, otherwise create
> +   a new one.  */
> +tree
> +cp_build_reference_type (tree to_type, bool rval)
> +{
> +  return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
> +}
> +
>  /* Returns EXPR cast to rvalue reference type, like std::move.  */
>  
>  tree
> @@ -1556,11 +1572,11 @@ strip_typedefs (tree t, bool *remove_attributes, unsigned int flags)
>      {
>      case POINTER_TYPE:
>        type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
> -      result = build_pointer_type (type);
> +      result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
>        break;
>      case REFERENCE_TYPE:
>        type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
> -      result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
> +      result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
>        break;
>      case OFFSET_TYPE:
>        t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
> diff --git a/gcc/testsuite/g++.target/s390/pr100281-1.C b/gcc/testsuite/g++.target/s390/pr100281-1.C
> new file mode 100644
> index 00000000000..b82e27b64e9
> --- /dev/null
> +++ b/gcc/testsuite/g++.target/s390/pr100281-1.C
> @@ -0,0 +1,10 @@
> +// PR C++/100281
> +// { dg-do compile }
> +
> +typedef void * __attribute__((mode (SI))) __ptr32_t;
> +
> +void foo () {
> +  unsigned int b = 100;
> +  __ptr32_t a;
> +  a = b; /* { dg-error "invalid conversion from 'unsigned int' to '__ptr32_t'.*" } */
> +}
> diff --git a/gcc/testsuite/g++.target/s390/pr100281-2.C b/gcc/testsuite/g++.target/s390/pr100281-2.C
> new file mode 100644
> index 00000000000..58552becd7c
> --- /dev/null
> +++ b/gcc/testsuite/g++.target/s390/pr100281-2.C
> @@ -0,0 +1,9 @@
> +// PR C++/100281
> +// { dg-do compile }
> +
> +typedef int & __attribute__((mode (SI))) __ref32_t;
> +
> +void foo () {
> +  unsigned int b = 100;
> +  __ref32_t a = b; /* { dg-error "cannot bind non-const lvalue reference of type '__ref32_t'.*" } */
> +}
>
Jason Merrill May 12, 2021, 2:43 p.m. UTC | #2
On 4/30/21 2:32 AM, Andreas Krebbel via Gcc-patches wrote:
> The problem appears to be triggered by two locations in the front-end
> where non-POINTER_SIZE pointers aren't handled right now.
> 
> 1. An assertion in strip_typedefs is triggered because the alignment
> of the types don't match. This in turn is caused by creating the new
> type with build_pointer_type instead of taking the type of the
> original pointer into account.
> 
> 2. An assertion in cp_convert_to_pointer is triggered which expects
> the target type to always have POINTER_SIZE.
> 
> Bootstrapped and regression tested on x86_64 and s390x.
> 
> Ok for mainline?
> 
> gcc/cp/ChangeLog:
> 
> 	PR c++/100281
> 	* cvt.c (cp_convert_to_pointer): Use the size of the target
> 	pointer type.
> 	* tree.c (cp_build_reference_type): Call
> 	cp_build_reference_type_for_mode with VOIDmode.
> 	(cp_build_reference_type_for_mode): Rename from
> 	cp_build_reference_type.  Add MODE argument and invoke
> 	build_reference_type_for_mode if MODE isn't VOIDmode.

How about putting the VOIDmode special case in the generic 
build_reference_type_for_mode instead of the C++ wrapper?

> 	(strip_typedefs): Use build_pointer_type_for_mode and
> 	cp_build_reference_type_for_mode for pointers and references.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR c++/100281
> 	* g++.target/s390/pr100281-1.C: New test.
> 	* g++.target/s390/pr100281-2.C: New test.
> ---
>   gcc/cp/cvt.c                               |  2 +-
>   gcc/cp/tree.c                              | 28 +++++++++++++++++-----
>   gcc/testsuite/g++.target/s390/pr100281-1.C | 10 ++++++++
>   gcc/testsuite/g++.target/s390/pr100281-2.C |  9 +++++++
>   4 files changed, 42 insertions(+), 7 deletions(-)
>   create mode 100644 gcc/testsuite/g++.target/s390/pr100281-1.C
>   create mode 100644 gcc/testsuite/g++.target/s390/pr100281-2.C
> 
> diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
> index f1687e804d1..7fa6e8df52b 100644
> --- a/gcc/cp/cvt.c
> +++ b/gcc/cp/cvt.c
> @@ -232,7 +232,7 @@ cp_convert_to_pointer (tree type, tree expr, bool dofold,
>       {
>         if (TYPE_PRECISION (intype) == POINTER_SIZE)
>   	return build1 (CONVERT_EXPR, type, expr);
> -      expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr,
> +      expr = cp_convert (c_common_type_for_size (TYPE_PRECISION (type), 0), expr,
>   			 complain);
>         /* Modes may be different but sizes should be the same.  There
>   	 is supposed to be some integral type that is the same width
> diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
> index a8bfd5fc053..3817b499e46 100644
> --- a/gcc/cp/tree.c
> +++ b/gcc/cp/tree.c
> @@ -1201,12 +1201,14 @@ vla_type_p (tree t)
>     return false;
>   }
>   
> -/* Return a reference type node referring to TO_TYPE.  If RVAL is
> +
> +/* Return a reference type node of MODE referring to TO_TYPE.  If MODE
> +   is VOIDmode the standard pointer mode will be picked.  If RVAL is
>      true, return an rvalue reference type, otherwise return an lvalue
>      reference type.  If a type node exists, reuse it, otherwise create
>      a new one.  */
>   tree
> -cp_build_reference_type (tree to_type, bool rval)
> +cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
>   {
>     tree lvalue_ref, t;
>   
> @@ -1219,7 +1221,11 @@ cp_build_reference_type (tree to_type, bool rval)
>         to_type = TREE_TYPE (to_type);
>       }
>   
> -  lvalue_ref = build_reference_type (to_type);
> +  if (mode == VOIDmode)
> +    lvalue_ref = build_reference_type (to_type);
> +  else
> +    lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
> +
>     if (!rval)
>       return lvalue_ref;
>   
> @@ -1245,7 +1251,7 @@ cp_build_reference_type (tree to_type, bool rval)
>       SET_TYPE_STRUCTURAL_EQUALITY (t);
>     else if (TYPE_CANONICAL (to_type) != to_type)
>       TYPE_CANONICAL (t)
> -      = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
> +      = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
>     else
>       TYPE_CANONICAL (t) = t;
>   
> @@ -1255,6 +1261,16 @@ cp_build_reference_type (tree to_type, bool rval)
>   
>   }
>   
> +/* Return a reference type node referring to TO_TYPE.  If RVAL is
> +   true, return an rvalue reference type, otherwise return an lvalue
> +   reference type.  If a type node exists, reuse it, otherwise create
> +   a new one.  */
> +tree
> +cp_build_reference_type (tree to_type, bool rval)
> +{
> +  return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
> +}
> +
>   /* Returns EXPR cast to rvalue reference type, like std::move.  */
>   
>   tree
> @@ -1556,11 +1572,11 @@ strip_typedefs (tree t, bool *remove_attributes, unsigned int flags)
>       {
>       case POINTER_TYPE:
>         type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
> -      result = build_pointer_type (type);
> +      result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
>         break;
>       case REFERENCE_TYPE:
>         type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
> -      result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
> +      result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
>         break;
>       case OFFSET_TYPE:
>         t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
> diff --git a/gcc/testsuite/g++.target/s390/pr100281-1.C b/gcc/testsuite/g++.target/s390/pr100281-1.C
> new file mode 100644
> index 00000000000..b82e27b64e9
> --- /dev/null
> +++ b/gcc/testsuite/g++.target/s390/pr100281-1.C
> @@ -0,0 +1,10 @@
> +// PR C++/100281
> +// { dg-do compile }
> +
> +typedef void * __attribute__((mode (SI))) __ptr32_t;
> +
> +void foo () {
> +  unsigned int b = 100;
> +  __ptr32_t a;
> +  a = b; /* { dg-error "invalid conversion from 'unsigned int' to '__ptr32_t'.*" } */
> +}
> diff --git a/gcc/testsuite/g++.target/s390/pr100281-2.C b/gcc/testsuite/g++.target/s390/pr100281-2.C
> new file mode 100644
> index 00000000000..58552becd7c
> --- /dev/null
> +++ b/gcc/testsuite/g++.target/s390/pr100281-2.C
> @@ -0,0 +1,9 @@
> +// PR C++/100281
> +// { dg-do compile }
> +
> +typedef int & __attribute__((mode (SI))) __ref32_t;
> +
> +void foo () {
> +  unsigned int b = 100;
> +  __ref32_t a = b; /* { dg-error "cannot bind non-const lvalue reference of type '__ref32_t'.*" } */
> +}
>
Andreas Krebbel May 13, 2021, 6:02 a.m. UTC | #3
v1 -> v2: build_reference_type_for_mode and build_pointer_type_for_mode now pick pointer mode if
MODE argument is VOIDmode.

Bootstrapped and regression tested on x86_64 and s390x.

Ok for mainline and GCC 11?

Andreas


gcc/cp/ChangeLog:

        PR c++/100281
	* cvt.c (cp_convert_to_pointer): Use the size of the target
	pointer type.
	* tree.c (cp_build_reference_type): Call
	cp_build_reference_type_for_mode with VOIDmode.
	(cp_build_reference_type_for_mode): Rename from
	cp_build_reference_type.  Add MODE argument and invoke
	build_reference_type_for_mode.
	(strip_typedefs): Use build_pointer_type_for_mode and
	cp_build_reference_type_for_mode for pointers and references.

gcc/ChangeLog:

        PR c++/100281
	* tree.c (build_reference_type_for_mode)
	(build_pointer_type_for_mode): Pick pointer mode if MODE argument
	is VOIDmode.
	(build_reference_type, build_pointer_type): Invoke
	build_*_type_for_mode with VOIDmode.

gcc/testsuite/ChangeLog:

        PR c++/100281
	* g++.target/s390/pr100281-1.C: New test.
	* g++.target/s390/pr100281-2.C: New test.
---
 gcc/cp/cvt.c                               |  2 +-
 gcc/cp/tree.c                              | 25 ++++++++++++++-----
 gcc/testsuite/g++.target/s390/pr100281-1.C | 10 ++++++++
 gcc/testsuite/g++.target/s390/pr100281-2.C |  9 +++++++
 gcc/tree.c                                 | 29 ++++++++++++++--------
 5 files changed, 57 insertions(+), 18 deletions(-)
 create mode 100644 gcc/testsuite/g++.target/s390/pr100281-1.C
 create mode 100644 gcc/testsuite/g++.target/s390/pr100281-2.C

diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index f1687e804d1..7fa6e8df52b 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -232,7 +232,7 @@ cp_convert_to_pointer (tree type, tree expr, bool dofold,
     {
       if (TYPE_PRECISION (intype) == POINTER_SIZE)
 	return build1 (CONVERT_EXPR, type, expr);
-      expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr,
+      expr = cp_convert (c_common_type_for_size (TYPE_PRECISION (type), 0), expr,
 			 complain);
       /* Modes may be different but sizes should be the same.  There
 	 is supposed to be some integral type that is the same width
diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index 7f148b4b158..35faeff065a 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -1206,12 +1206,14 @@ vla_type_p (tree t)
   return false;
 }

-/* Return a reference type node referring to TO_TYPE.  If RVAL is
+
+/* Return a reference type node of MODE referring to TO_TYPE.  If MODE
+   is VOIDmode the standard pointer mode will be picked.  If RVAL is
    true, return an rvalue reference type, otherwise return an lvalue
    reference type.  If a type node exists, reuse it, otherwise create
    a new one.  */
 tree
-cp_build_reference_type (tree to_type, bool rval)
+cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
 {
   tree lvalue_ref, t;

@@ -1224,7 +1226,8 @@ cp_build_reference_type (tree to_type, bool rval)
       to_type = TREE_TYPE (to_type);
     }

-  lvalue_ref = build_reference_type (to_type);
+  lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
+
   if (!rval)
     return lvalue_ref;

@@ -1250,7 +1253,7 @@ cp_build_reference_type (tree to_type, bool rval)
     SET_TYPE_STRUCTURAL_EQUALITY (t);
   else if (TYPE_CANONICAL (to_type) != to_type)
     TYPE_CANONICAL (t)
-      = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
+      = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
   else
     TYPE_CANONICAL (t) = t;

@@ -1260,6 +1263,16 @@ cp_build_reference_type (tree to_type, bool rval)

 }

+/* Return a reference type node referring to TO_TYPE.  If RVAL is
+   true, return an rvalue reference type, otherwise return an lvalue
+   reference type.  If a type node exists, reuse it, otherwise create
+   a new one.  */
+tree
+cp_build_reference_type (tree to_type, bool rval)
+{
+  return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
+}
+
 /* Returns EXPR cast to rvalue reference type, like std::move.  */

 tree
@@ -1561,11 +1574,11 @@ strip_typedefs (tree t, bool *remove_attributes, unsigned int flags)
     {
     case POINTER_TYPE:
       type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
-      result = build_pointer_type (type);
+      result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
       break;
     case REFERENCE_TYPE:
       type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
-      result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
+      result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
       break;
     case OFFSET_TYPE:
       t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
diff --git a/gcc/testsuite/g++.target/s390/pr100281-1.C b/gcc/testsuite/g++.target/s390/pr100281-1.C
new file mode 100644
index 00000000000..b82e27b64e9
--- /dev/null
+++ b/gcc/testsuite/g++.target/s390/pr100281-1.C
@@ -0,0 +1,10 @@
+// PR C++/100281
+// { dg-do compile }
+
+typedef void * __attribute__((mode (SI))) __ptr32_t;
+
+void foo () {
+  unsigned int b = 100;
+  __ptr32_t a;
+  a = b; /* { dg-error "invalid conversion from 'unsigned int' to '__ptr32_t'.*" } */
+}
diff --git a/gcc/testsuite/g++.target/s390/pr100281-2.C b/gcc/testsuite/g++.target/s390/pr100281-2.C
new file mode 100644
index 00000000000..58552becd7c
--- /dev/null
+++ b/gcc/testsuite/g++.target/s390/pr100281-2.C
@@ -0,0 +1,9 @@
+// PR C++/100281
+// { dg-do compile }
+
+typedef int & __attribute__((mode (SI))) __ref32_t;
+
+void foo () {
+  unsigned int b = 100;
+  __ref32_t a = b; /* { dg-error "cannot bind non-const lvalue reference of type '__ref32_t'.*" } */
+}
diff --git a/gcc/tree.c b/gcc/tree.c
index 01eda553a65..e92cae286d4 100644
--- a/gcc/tree.c
+++ b/gcc/tree.c
@@ -6792,9 +6792,10 @@ operation_no_trapping_overflow (tree type, enum tree_code code)
    constructed by language-dependent code, not here.)  */

 /* Construct, lay out and return the type of pointers to TO_TYPE with
-   mode MODE.  If CAN_ALIAS_ALL is TRUE, indicate this type can
-   reference all of memory. If such a type has already been
-   constructed, reuse it.  */
+   mode MODE.  If MODE is VOIDmode, a pointer mode for the address
+   space of TO_TYPE will be picked.  If CAN_ALIAS_ALL is TRUE,
+   indicate this type can reference all of memory. If such a type has
+   already been constructed, reuse it.  */

 tree
 build_pointer_type_for_mode (tree to_type, machine_mode mode,
@@ -6806,6 +6807,12 @@ build_pointer_type_for_mode (tree to_type, machine_mode mode,
   if (to_type == error_mark_node)
     return error_mark_node;

+  if (mode == VOIDmode)
+    {
+      addr_space_t as = TYPE_ADDR_SPACE (to_type);
+      mode = targetm.addr_space.pointer_mode (as);
+    }
+
   /* If the pointed-to type has the may_alias attribute set, force
      a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
   if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
@@ -6857,10 +6864,7 @@ build_pointer_type_for_mode (tree to_type, machine_mode mode,
 tree
 build_pointer_type (tree to_type)
 {
-  addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
-					      : TYPE_ADDR_SPACE (to_type);
-  machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
-  return build_pointer_type_for_mode (to_type, pointer_mode, false);
+  return build_pointer_type_for_mode (to_type, VOIDmode, false);
 }

 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE.  */
@@ -6875,6 +6879,12 @@ build_reference_type_for_mode (tree to_type, machine_mode mode,
   if (to_type == error_mark_node)
     return error_mark_node;

+  if (mode == VOIDmode)
+    {
+      addr_space_t as = TYPE_ADDR_SPACE (to_type);
+      mode = targetm.addr_space.pointer_mode (as);
+    }
+
   /* If the pointed-to type has the may_alias attribute set, force
      a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
   if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
@@ -6926,10 +6936,7 @@ build_reference_type_for_mode (tree to_type, machine_mode mode,
 tree
 build_reference_type (tree to_type)
 {
-  addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
-					      : TYPE_ADDR_SPACE (to_type);
-  machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
-  return build_reference_type_for_mode (to_type, pointer_mode, false);
+  return build_reference_type_for_mode (to_type, VOIDmode, false);
 }

 #define MAX_INT_CACHED_PREC \
Richard Biener May 17, 2021, 8:48 a.m. UTC | #4
On Thu, May 13, 2021 at 8:28 AM Andreas Krebbel via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> v1 -> v2: build_reference_type_for_mode and build_pointer_type_for_mode now pick pointer mode if
> MODE argument is VOIDmode.
>
> Bootstrapped and regression tested on x86_64 and s390x.
>
> Ok for mainline and GCC 11?

The middle-end parts are fine with me.

Richard.

> Andreas
>
>
> gcc/cp/ChangeLog:
>
>         PR c++/100281
>         * cvt.c (cp_convert_to_pointer): Use the size of the target
>         pointer type.
>         * tree.c (cp_build_reference_type): Call
>         cp_build_reference_type_for_mode with VOIDmode.
>         (cp_build_reference_type_for_mode): Rename from
>         cp_build_reference_type.  Add MODE argument and invoke
>         build_reference_type_for_mode.
>         (strip_typedefs): Use build_pointer_type_for_mode and
>         cp_build_reference_type_for_mode for pointers and references.
>
> gcc/ChangeLog:
>
>         PR c++/100281
>         * tree.c (build_reference_type_for_mode)
>         (build_pointer_type_for_mode): Pick pointer mode if MODE argument
>         is VOIDmode.
>         (build_reference_type, build_pointer_type): Invoke
>         build_*_type_for_mode with VOIDmode.
>
> gcc/testsuite/ChangeLog:
>
>         PR c++/100281
>         * g++.target/s390/pr100281-1.C: New test.
>         * g++.target/s390/pr100281-2.C: New test.
> ---
>  gcc/cp/cvt.c                               |  2 +-
>  gcc/cp/tree.c                              | 25 ++++++++++++++-----
>  gcc/testsuite/g++.target/s390/pr100281-1.C | 10 ++++++++
>  gcc/testsuite/g++.target/s390/pr100281-2.C |  9 +++++++
>  gcc/tree.c                                 | 29 ++++++++++++++--------
>  5 files changed, 57 insertions(+), 18 deletions(-)
>  create mode 100644 gcc/testsuite/g++.target/s390/pr100281-1.C
>  create mode 100644 gcc/testsuite/g++.target/s390/pr100281-2.C
>
> diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
> index f1687e804d1..7fa6e8df52b 100644
> --- a/gcc/cp/cvt.c
> +++ b/gcc/cp/cvt.c
> @@ -232,7 +232,7 @@ cp_convert_to_pointer (tree type, tree expr, bool dofold,
>      {
>        if (TYPE_PRECISION (intype) == POINTER_SIZE)
>         return build1 (CONVERT_EXPR, type, expr);
> -      expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr,
> +      expr = cp_convert (c_common_type_for_size (TYPE_PRECISION (type), 0), expr,
>                          complain);
>        /* Modes may be different but sizes should be the same.  There
>          is supposed to be some integral type that is the same width
> diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
> index 7f148b4b158..35faeff065a 100644
> --- a/gcc/cp/tree.c
> +++ b/gcc/cp/tree.c
> @@ -1206,12 +1206,14 @@ vla_type_p (tree t)
>    return false;
>  }
>
> -/* Return a reference type node referring to TO_TYPE.  If RVAL is
> +
> +/* Return a reference type node of MODE referring to TO_TYPE.  If MODE
> +   is VOIDmode the standard pointer mode will be picked.  If RVAL is
>     true, return an rvalue reference type, otherwise return an lvalue
>     reference type.  If a type node exists, reuse it, otherwise create
>     a new one.  */
>  tree
> -cp_build_reference_type (tree to_type, bool rval)
> +cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
>  {
>    tree lvalue_ref, t;
>
> @@ -1224,7 +1226,8 @@ cp_build_reference_type (tree to_type, bool rval)
>        to_type = TREE_TYPE (to_type);
>      }
>
> -  lvalue_ref = build_reference_type (to_type);
> +  lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
> +
>    if (!rval)
>      return lvalue_ref;
>
> @@ -1250,7 +1253,7 @@ cp_build_reference_type (tree to_type, bool rval)
>      SET_TYPE_STRUCTURAL_EQUALITY (t);
>    else if (TYPE_CANONICAL (to_type) != to_type)
>      TYPE_CANONICAL (t)
> -      = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
> +      = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
>    else
>      TYPE_CANONICAL (t) = t;
>
> @@ -1260,6 +1263,16 @@ cp_build_reference_type (tree to_type, bool rval)
>
>  }
>
> +/* Return a reference type node referring to TO_TYPE.  If RVAL is
> +   true, return an rvalue reference type, otherwise return an lvalue
> +   reference type.  If a type node exists, reuse it, otherwise create
> +   a new one.  */
> +tree
> +cp_build_reference_type (tree to_type, bool rval)
> +{
> +  return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
> +}
> +
>  /* Returns EXPR cast to rvalue reference type, like std::move.  */
>
>  tree
> @@ -1561,11 +1574,11 @@ strip_typedefs (tree t, bool *remove_attributes, unsigned int flags)
>      {
>      case POINTER_TYPE:
>        type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
> -      result = build_pointer_type (type);
> +      result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
>        break;
>      case REFERENCE_TYPE:
>        type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
> -      result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
> +      result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
>        break;
>      case OFFSET_TYPE:
>        t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
> diff --git a/gcc/testsuite/g++.target/s390/pr100281-1.C b/gcc/testsuite/g++.target/s390/pr100281-1.C
> new file mode 100644
> index 00000000000..b82e27b64e9
> --- /dev/null
> +++ b/gcc/testsuite/g++.target/s390/pr100281-1.C
> @@ -0,0 +1,10 @@
> +// PR C++/100281
> +// { dg-do compile }
> +
> +typedef void * __attribute__((mode (SI))) __ptr32_t;
> +
> +void foo () {
> +  unsigned int b = 100;
> +  __ptr32_t a;
> +  a = b; /* { dg-error "invalid conversion from 'unsigned int' to '__ptr32_t'.*" } */
> +}
> diff --git a/gcc/testsuite/g++.target/s390/pr100281-2.C b/gcc/testsuite/g++.target/s390/pr100281-2.C
> new file mode 100644
> index 00000000000..58552becd7c
> --- /dev/null
> +++ b/gcc/testsuite/g++.target/s390/pr100281-2.C
> @@ -0,0 +1,9 @@
> +// PR C++/100281
> +// { dg-do compile }
> +
> +typedef int & __attribute__((mode (SI))) __ref32_t;
> +
> +void foo () {
> +  unsigned int b = 100;
> +  __ref32_t a = b; /* { dg-error "cannot bind non-const lvalue reference of type '__ref32_t'.*" } */
> +}
> diff --git a/gcc/tree.c b/gcc/tree.c
> index 01eda553a65..e92cae286d4 100644
> --- a/gcc/tree.c
> +++ b/gcc/tree.c
> @@ -6792,9 +6792,10 @@ operation_no_trapping_overflow (tree type, enum tree_code code)
>     constructed by language-dependent code, not here.)  */
>
>  /* Construct, lay out and return the type of pointers to TO_TYPE with
> -   mode MODE.  If CAN_ALIAS_ALL is TRUE, indicate this type can
> -   reference all of memory. If such a type has already been
> -   constructed, reuse it.  */
> +   mode MODE.  If MODE is VOIDmode, a pointer mode for the address
> +   space of TO_TYPE will be picked.  If CAN_ALIAS_ALL is TRUE,
> +   indicate this type can reference all of memory. If such a type has
> +   already been constructed, reuse it.  */
>
>  tree
>  build_pointer_type_for_mode (tree to_type, machine_mode mode,
> @@ -6806,6 +6807,12 @@ build_pointer_type_for_mode (tree to_type, machine_mode mode,
>    if (to_type == error_mark_node)
>      return error_mark_node;
>
> +  if (mode == VOIDmode)
> +    {
> +      addr_space_t as = TYPE_ADDR_SPACE (to_type);
> +      mode = targetm.addr_space.pointer_mode (as);
> +    }
> +
>    /* If the pointed-to type has the may_alias attribute set, force
>       a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
>    if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
> @@ -6857,10 +6864,7 @@ build_pointer_type_for_mode (tree to_type, machine_mode mode,
>  tree
>  build_pointer_type (tree to_type)
>  {
> -  addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
> -                                             : TYPE_ADDR_SPACE (to_type);
> -  machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
> -  return build_pointer_type_for_mode (to_type, pointer_mode, false);
> +  return build_pointer_type_for_mode (to_type, VOIDmode, false);
>  }
>
>  /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE.  */
> @@ -6875,6 +6879,12 @@ build_reference_type_for_mode (tree to_type, machine_mode mode,
>    if (to_type == error_mark_node)
>      return error_mark_node;
>
> +  if (mode == VOIDmode)
> +    {
> +      addr_space_t as = TYPE_ADDR_SPACE (to_type);
> +      mode = targetm.addr_space.pointer_mode (as);
> +    }
> +
>    /* If the pointed-to type has the may_alias attribute set, force
>       a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
>    if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
> @@ -6926,10 +6936,7 @@ build_reference_type_for_mode (tree to_type, machine_mode mode,
>  tree
>  build_reference_type (tree to_type)
>  {
> -  addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
> -                                             : TYPE_ADDR_SPACE (to_type);
> -  machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
> -  return build_reference_type_for_mode (to_type, pointer_mode, false);
> +  return build_reference_type_for_mode (to_type, VOIDmode, false);
>  }
>
>  #define MAX_INT_CACHED_PREC \
> --
> 2.30.2
>
>
Jason Merrill May 17, 2021, 6:01 p.m. UTC | #5
On 5/17/21 4:48 AM, Richard Biener wrote:
> On Thu, May 13, 2021 at 8:28 AM Andreas Krebbel via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
>>
>> v1 -> v2: build_reference_type_for_mode and build_pointer_type_for_mode now pick pointer mode if
>> MODE argument is VOIDmode.
>>
>> Bootstrapped and regression tested on x86_64 and s390x.
>>
>> Ok for mainline and GCC 11?
> 
> The middle-end parts are fine with me.

The C++ parts are OK as well.

> Richard.
> 
>> Andreas
>>
>>
>> gcc/cp/ChangeLog:
>>
>>          PR c++/100281
>>          * cvt.c (cp_convert_to_pointer): Use the size of the target
>>          pointer type.
>>          * tree.c (cp_build_reference_type): Call
>>          cp_build_reference_type_for_mode with VOIDmode.
>>          (cp_build_reference_type_for_mode): Rename from
>>          cp_build_reference_type.  Add MODE argument and invoke
>>          build_reference_type_for_mode.
>>          (strip_typedefs): Use build_pointer_type_for_mode and
>>          cp_build_reference_type_for_mode for pointers and references.
>>
>> gcc/ChangeLog:
>>
>>          PR c++/100281
>>          * tree.c (build_reference_type_for_mode)
>>          (build_pointer_type_for_mode): Pick pointer mode if MODE argument
>>          is VOIDmode.
>>          (build_reference_type, build_pointer_type): Invoke
>>          build_*_type_for_mode with VOIDmode.
>>
>> gcc/testsuite/ChangeLog:
>>
>>          PR c++/100281
>>          * g++.target/s390/pr100281-1.C: New test.
>>          * g++.target/s390/pr100281-2.C: New test.
>> ---
>>   gcc/cp/cvt.c                               |  2 +-
>>   gcc/cp/tree.c                              | 25 ++++++++++++++-----
>>   gcc/testsuite/g++.target/s390/pr100281-1.C | 10 ++++++++
>>   gcc/testsuite/g++.target/s390/pr100281-2.C |  9 +++++++
>>   gcc/tree.c                                 | 29 ++++++++++++++--------
>>   5 files changed, 57 insertions(+), 18 deletions(-)
>>   create mode 100644 gcc/testsuite/g++.target/s390/pr100281-1.C
>>   create mode 100644 gcc/testsuite/g++.target/s390/pr100281-2.C
>>
>> diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
>> index f1687e804d1..7fa6e8df52b 100644
>> --- a/gcc/cp/cvt.c
>> +++ b/gcc/cp/cvt.c
>> @@ -232,7 +232,7 @@ cp_convert_to_pointer (tree type, tree expr, bool dofold,
>>       {
>>         if (TYPE_PRECISION (intype) == POINTER_SIZE)
>>          return build1 (CONVERT_EXPR, type, expr);
>> -      expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr,
>> +      expr = cp_convert (c_common_type_for_size (TYPE_PRECISION (type), 0), expr,
>>                           complain);
>>         /* Modes may be different but sizes should be the same.  There
>>           is supposed to be some integral type that is the same width
>> diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
>> index 7f148b4b158..35faeff065a 100644
>> --- a/gcc/cp/tree.c
>> +++ b/gcc/cp/tree.c
>> @@ -1206,12 +1206,14 @@ vla_type_p (tree t)
>>     return false;
>>   }
>>
>> -/* Return a reference type node referring to TO_TYPE.  If RVAL is
>> +
>> +/* Return a reference type node of MODE referring to TO_TYPE.  If MODE
>> +   is VOIDmode the standard pointer mode will be picked.  If RVAL is
>>      true, return an rvalue reference type, otherwise return an lvalue
>>      reference type.  If a type node exists, reuse it, otherwise create
>>      a new one.  */
>>   tree
>> -cp_build_reference_type (tree to_type, bool rval)
>> +cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
>>   {
>>     tree lvalue_ref, t;
>>
>> @@ -1224,7 +1226,8 @@ cp_build_reference_type (tree to_type, bool rval)
>>         to_type = TREE_TYPE (to_type);
>>       }
>>
>> -  lvalue_ref = build_reference_type (to_type);
>> +  lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
>> +
>>     if (!rval)
>>       return lvalue_ref;
>>
>> @@ -1250,7 +1253,7 @@ cp_build_reference_type (tree to_type, bool rval)
>>       SET_TYPE_STRUCTURAL_EQUALITY (t);
>>     else if (TYPE_CANONICAL (to_type) != to_type)
>>       TYPE_CANONICAL (t)
>> -      = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
>> +      = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
>>     else
>>       TYPE_CANONICAL (t) = t;
>>
>> @@ -1260,6 +1263,16 @@ cp_build_reference_type (tree to_type, bool rval)
>>
>>   }
>>
>> +/* Return a reference type node referring to TO_TYPE.  If RVAL is
>> +   true, return an rvalue reference type, otherwise return an lvalue
>> +   reference type.  If a type node exists, reuse it, otherwise create
>> +   a new one.  */
>> +tree
>> +cp_build_reference_type (tree to_type, bool rval)
>> +{
>> +  return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
>> +}
>> +
>>   /* Returns EXPR cast to rvalue reference type, like std::move.  */
>>
>>   tree
>> @@ -1561,11 +1574,11 @@ strip_typedefs (tree t, bool *remove_attributes, unsigned int flags)
>>       {
>>       case POINTER_TYPE:
>>         type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
>> -      result = build_pointer_type (type);
>> +      result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
>>         break;
>>       case REFERENCE_TYPE:
>>         type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
>> -      result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
>> +      result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
>>         break;
>>       case OFFSET_TYPE:
>>         t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
>> diff --git a/gcc/testsuite/g++.target/s390/pr100281-1.C b/gcc/testsuite/g++.target/s390/pr100281-1.C
>> new file mode 100644
>> index 00000000000..b82e27b64e9
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.target/s390/pr100281-1.C
>> @@ -0,0 +1,10 @@
>> +// PR C++/100281
>> +// { dg-do compile }
>> +
>> +typedef void * __attribute__((mode (SI))) __ptr32_t;
>> +
>> +void foo () {
>> +  unsigned int b = 100;
>> +  __ptr32_t a;
>> +  a = b; /* { dg-error "invalid conversion from 'unsigned int' to '__ptr32_t'.*" } */
>> +}
>> diff --git a/gcc/testsuite/g++.target/s390/pr100281-2.C b/gcc/testsuite/g++.target/s390/pr100281-2.C
>> new file mode 100644
>> index 00000000000..58552becd7c
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.target/s390/pr100281-2.C
>> @@ -0,0 +1,9 @@
>> +// PR C++/100281
>> +// { dg-do compile }
>> +
>> +typedef int & __attribute__((mode (SI))) __ref32_t;
>> +
>> +void foo () {
>> +  unsigned int b = 100;
>> +  __ref32_t a = b; /* { dg-error "cannot bind non-const lvalue reference of type '__ref32_t'.*" } */
>> +}
>> diff --git a/gcc/tree.c b/gcc/tree.c
>> index 01eda553a65..e92cae286d4 100644
>> --- a/gcc/tree.c
>> +++ b/gcc/tree.c
>> @@ -6792,9 +6792,10 @@ operation_no_trapping_overflow (tree type, enum tree_code code)
>>      constructed by language-dependent code, not here.)  */
>>
>>   /* Construct, lay out and return the type of pointers to TO_TYPE with
>> -   mode MODE.  If CAN_ALIAS_ALL is TRUE, indicate this type can
>> -   reference all of memory. If such a type has already been
>> -   constructed, reuse it.  */
>> +   mode MODE.  If MODE is VOIDmode, a pointer mode for the address
>> +   space of TO_TYPE will be picked.  If CAN_ALIAS_ALL is TRUE,
>> +   indicate this type can reference all of memory. If such a type has
>> +   already been constructed, reuse it.  */
>>
>>   tree
>>   build_pointer_type_for_mode (tree to_type, machine_mode mode,
>> @@ -6806,6 +6807,12 @@ build_pointer_type_for_mode (tree to_type, machine_mode mode,
>>     if (to_type == error_mark_node)
>>       return error_mark_node;
>>
>> +  if (mode == VOIDmode)
>> +    {
>> +      addr_space_t as = TYPE_ADDR_SPACE (to_type);
>> +      mode = targetm.addr_space.pointer_mode (as);
>> +    }
>> +
>>     /* If the pointed-to type has the may_alias attribute set, force
>>        a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
>>     if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
>> @@ -6857,10 +6864,7 @@ build_pointer_type_for_mode (tree to_type, machine_mode mode,
>>   tree
>>   build_pointer_type (tree to_type)
>>   {
>> -  addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
>> -                                             : TYPE_ADDR_SPACE (to_type);
>> -  machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
>> -  return build_pointer_type_for_mode (to_type, pointer_mode, false);
>> +  return build_pointer_type_for_mode (to_type, VOIDmode, false);
>>   }
>>
>>   /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE.  */
>> @@ -6875,6 +6879,12 @@ build_reference_type_for_mode (tree to_type, machine_mode mode,
>>     if (to_type == error_mark_node)
>>       return error_mark_node;
>>
>> +  if (mode == VOIDmode)
>> +    {
>> +      addr_space_t as = TYPE_ADDR_SPACE (to_type);
>> +      mode = targetm.addr_space.pointer_mode (as);
>> +    }
>> +
>>     /* If the pointed-to type has the may_alias attribute set, force
>>        a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
>>     if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
>> @@ -6926,10 +6936,7 @@ build_reference_type_for_mode (tree to_type, machine_mode mode,
>>   tree
>>   build_reference_type (tree to_type)
>>   {
>> -  addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
>> -                                             : TYPE_ADDR_SPACE (to_type);
>> -  machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
>> -  return build_reference_type_for_mode (to_type, pointer_mode, false);
>> +  return build_reference_type_for_mode (to_type, VOIDmode, false);
>>   }
>>
>>   #define MAX_INT_CACHED_PREC \
>> --
>> 2.30.2
>>
>>
>
diff mbox series

Patch

diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index f1687e804d1..7fa6e8df52b 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -232,7 +232,7 @@  cp_convert_to_pointer (tree type, tree expr, bool dofold,
     {
       if (TYPE_PRECISION (intype) == POINTER_SIZE)
 	return build1 (CONVERT_EXPR, type, expr);
-      expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr,
+      expr = cp_convert (c_common_type_for_size (TYPE_PRECISION (type), 0), expr,
 			 complain);
       /* Modes may be different but sizes should be the same.  There
 	 is supposed to be some integral type that is the same width
diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index a8bfd5fc053..3817b499e46 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -1201,12 +1201,14 @@  vla_type_p (tree t)
   return false;
 }
 
-/* Return a reference type node referring to TO_TYPE.  If RVAL is
+
+/* Return a reference type node of MODE referring to TO_TYPE.  If MODE
+   is VOIDmode the standard pointer mode will be picked.  If RVAL is
    true, return an rvalue reference type, otherwise return an lvalue
    reference type.  If a type node exists, reuse it, otherwise create
    a new one.  */
 tree
-cp_build_reference_type (tree to_type, bool rval)
+cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
 {
   tree lvalue_ref, t;
 
@@ -1219,7 +1221,11 @@  cp_build_reference_type (tree to_type, bool rval)
       to_type = TREE_TYPE (to_type);
     }
 
-  lvalue_ref = build_reference_type (to_type);
+  if (mode == VOIDmode)
+    lvalue_ref = build_reference_type (to_type);
+  else
+    lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
+
   if (!rval)
     return lvalue_ref;
 
@@ -1245,7 +1251,7 @@  cp_build_reference_type (tree to_type, bool rval)
     SET_TYPE_STRUCTURAL_EQUALITY (t);
   else if (TYPE_CANONICAL (to_type) != to_type)
     TYPE_CANONICAL (t) 
-      = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
+      = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
   else
     TYPE_CANONICAL (t) = t;
 
@@ -1255,6 +1261,16 @@  cp_build_reference_type (tree to_type, bool rval)
 
 }
 
+/* Return a reference type node referring to TO_TYPE.  If RVAL is
+   true, return an rvalue reference type, otherwise return an lvalue
+   reference type.  If a type node exists, reuse it, otherwise create
+   a new one.  */
+tree
+cp_build_reference_type (tree to_type, bool rval)
+{
+  return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
+}
+
 /* Returns EXPR cast to rvalue reference type, like std::move.  */
 
 tree
@@ -1556,11 +1572,11 @@  strip_typedefs (tree t, bool *remove_attributes, unsigned int flags)
     {
     case POINTER_TYPE:
       type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
-      result = build_pointer_type (type);
+      result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
       break;
     case REFERENCE_TYPE:
       type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
-      result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
+      result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
       break;
     case OFFSET_TYPE:
       t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
diff --git a/gcc/testsuite/g++.target/s390/pr100281-1.C b/gcc/testsuite/g++.target/s390/pr100281-1.C
new file mode 100644
index 00000000000..b82e27b64e9
--- /dev/null
+++ b/gcc/testsuite/g++.target/s390/pr100281-1.C
@@ -0,0 +1,10 @@ 
+// PR C++/100281
+// { dg-do compile }
+
+typedef void * __attribute__((mode (SI))) __ptr32_t;
+
+void foo () {
+  unsigned int b = 100;
+  __ptr32_t a;
+  a = b; /* { dg-error "invalid conversion from 'unsigned int' to '__ptr32_t'.*" } */
+}
diff --git a/gcc/testsuite/g++.target/s390/pr100281-2.C b/gcc/testsuite/g++.target/s390/pr100281-2.C
new file mode 100644
index 00000000000..58552becd7c
--- /dev/null
+++ b/gcc/testsuite/g++.target/s390/pr100281-2.C
@@ -0,0 +1,9 @@ 
+// PR C++/100281
+// { dg-do compile }
+
+typedef int & __attribute__((mode (SI))) __ref32_t;
+
+void foo () {
+  unsigned int b = 100;
+  __ref32_t a = b; /* { dg-error "cannot bind non-const lvalue reference of type '__ref32_t'.*" } */
+}