]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: overloading of intrinsic binary operators [PR109641]
authorHarald Anlauf <anlauf@gmx.de>
Fri, 5 May 2023 19:22:12 +0000 (21:22 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 5 May 2023 19:22:12 +0000 (21:22 +0200)
Fortran allows overloading of intrinsic operators also for operands of
numeric intrinsic types.  The intrinsic operator versions are used
according to the rules of F2018 table 10.2 and imply type conversion as
long as the operand ranks are conformable.  Otherwise no type conversion
shall be performed to allow the resolution of a matching user-defined
operator.

gcc/fortran/ChangeLog:

PR fortran/109641
* arith.cc (eval_intrinsic): Check conformability of ranks of operands
for intrinsic binary operators before performing type conversions.
* gfortran.h (gfc_op_rank_conformable): Add prototype.
* resolve.cc (resolve_operator): Check conformability of ranks of
operands for intrinsic binary operators before performing type
conversions.
(gfc_op_rank_conformable): New helper function to compare ranks of
operands of binary operator.

gcc/testsuite/ChangeLog:

PR fortran/109641
* gfortran.dg/overload_5.f90: New test.

gcc/fortran/arith.cc
gcc/fortran/gfortran.h
gcc/fortran/resolve.cc
gcc/testsuite/gfortran.dg/overload_5.f90 [new file with mode: 0644]

index d1d814b3ae19ef420671615f4ff3d31543751b9e..86d56406047e1c0c46ae2cdd0af8525e242c8c53 100644 (file)
@@ -1663,6 +1663,12 @@ eval_intrinsic (gfc_intrinsic_op op,
       if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
        goto runtime;
 
+      /* Do not perform conversions if operands are not conformable as
+        required for the binary intrinsic operators (F2018:10.1.5).
+        Defer to a possibly overloading user-defined operator.  */
+      if (!gfc_op_rank_conformable (op1, op2))
+           goto runtime;
+
       /* Insert any necessary type conversions to make the operands
         compatible.  */
 
index a15ff90e22862c5e1825bcd8a67b2d7aa1933ba4..ac21e1813d9492c0a7f47b84a734949723bdaec6 100644 (file)
@@ -3730,6 +3730,7 @@ void gfc_free_association_list (gfc_association_list *);
 
 /* resolve.cc */
 void gfc_expression_rank (gfc_expr *);
+bool gfc_op_rank_conformable (gfc_expr *, gfc_expr *);
 bool gfc_resolve_ref (gfc_expr *);
 bool gfc_resolve_expr (gfc_expr *);
 void gfc_resolve (gfc_namespace *);
index c3d508fb45d68dd9c0358ea5aec768e6c435541b..6f274f71032fb00c6cb7abfd8e8929ee916647d1 100644 (file)
@@ -4200,6 +4200,17 @@ resolve_operator (gfc_expr *e)
     case INTRINSIC_POWER:
       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
        {
+         /* Do not perform conversions if operands are not conformable as
+            required for the binary intrinsic operators (F2018:10.1.5).
+            Defer to a possibly overloading user-defined operator.  */
+         if (!gfc_op_rank_conformable (op1, op2))
+           {
+             dual_locus_error = true;
+             snprintf (msg, sizeof (msg),
+                       _("Inconsistent ranks for operator at %%L and %%L"));
+             goto bad_op;
+           }
+
          gfc_type_convert_binary (e, 1);
          break;
        }
@@ -4372,6 +4383,17 @@ resolve_operator (gfc_expr *e)
 
       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
        {
+         /* Do not perform conversions if operands are not conformable as
+            required for the binary intrinsic operators (F2018:10.1.5).
+            Defer to a possibly overloading user-defined operator.  */
+         if (!gfc_op_rank_conformable (op1, op2))
+           {
+             dual_locus_error = true;
+             snprintf (msg, sizeof (msg),
+                       _("Inconsistent ranks for operator at %%L and %%L"));
+             goto bad_op;
+           }
+
          gfc_type_convert_binary (e, 1);
 
          e->ts.type = BT_LOGICAL;
@@ -5644,6 +5666,21 @@ done:
 }
 
 
+/* Given two expressions, check that their rank is conformable, i.e. either
+   both have the same rank or at least one is a scalar.  */
+
+bool
+gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
+{
+  if (op1->expr_type == EXPR_VARIABLE)
+    gfc_expression_rank (op1);
+  if (op2->expr_type == EXPR_VARIABLE)
+    gfc_expression_rank (op2);
+
+  return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank);
+}
+
+
 static void
 add_caf_get_intrinsic (gfc_expr *e)
 {
diff --git a/gcc/testsuite/gfortran.dg/overload_5.f90 b/gcc/testsuite/gfortran.dg/overload_5.f90
new file mode 100644 (file)
index 0000000..f8c93af
--- /dev/null
@@ -0,0 +1,118 @@
+! { dg-do run }
+! PR fortran/109641
+!
+! Check overloading of intrinsic binary operators for numeric operands
+! Reported by Adelson Oliveira
+
+MODULE TESTEOP
+  IMPLICIT NONE
+  INTERFACE OPERATOR(.MULT.)
+    MODULE PROCEDURE MULTr4
+    MODULE PROCEDURE MULTc4
+  END INTERFACE
+  INTERFACE OPERATOR(*)
+    MODULE PROCEDURE MULTr4
+    MODULE PROCEDURE MULTc4
+  END INTERFACE
+  INTERFACE OPERATOR(==)
+    MODULE PROCEDURE MULTr4
+    MODULE PROCEDURE MULTc4
+    MODULE PROCEDURE MULTr8
+  END INTERFACE
+  INTERFACE OPERATOR(<)
+    MODULE PROCEDURE MULTc4
+    MODULE PROCEDURE MULTi4
+  END INTERFACE
+  INTERFACE OPERATOR(**)
+    MODULE PROCEDURE MULTc4
+    MODULE PROCEDURE MULTi4
+  END INTERFACE
+  interface copy
+     MODULE PROCEDURE copy
+  end interface copy
+CONTAINS
+  elemental function copy (z)
+    complex, intent(in) :: z
+    complex             :: copy
+    copy = z
+  end function copy
+  FUNCTION MULTr4(v,m)
+    REAL,    INTENT(IN) :: v(:)
+    REAL,    INTENT(IN) :: m(:,:)
+    REAL                :: MULTr4(SIZE(m,DIM=1),SIZE(m,DIM=2))
+    INTEGER             :: i
+    FORALL(i=1:SIZE(v)) MULTr4(:,i)=m(:,i)*v(i)
+  END FUNCTION MULTr4
+  FUNCTION MULTr8(v,m)
+    REAL,             INTENT(IN) :: v(:)
+    double precision, INTENT(IN) :: m(:,:)
+    double precision             :: MULTr8(SIZE(m,DIM=1),SIZE(m,DIM=2))
+    INTEGER             :: i
+    FORALL(i=1:SIZE(v)) MULTr8(:,i)=m(:,i)*v(i)
+  END FUNCTION MULTr8
+  FUNCTION MULTc4(v,m)
+    REAL,    INTENT(IN) :: v(:)
+    COMPLEX, INTENT(IN) :: m(:,:)
+    COMPLEX             :: MULTc4(SIZE(m,DIM=1),SIZE(m,DIM=2))
+    INTEGER             :: i
+    FORALL(i=1:SIZE(v)) MULTc4(:,i)=m(:,i)*v(i)
+  END FUNCTION MULTc4
+  FUNCTION MULTi4(v,m)
+    REAL,    INTENT(IN) :: v(:)
+    integer, INTENT(IN) :: m(:,:)
+    REAL                :: MULTi4(SIZE(m,DIM=1),SIZE(m,DIM=2))
+    INTEGER             :: i
+    FORALL(i=1:SIZE(v)) MULTi4(:,i)=m(:,i)*v(i)
+  END FUNCTION MULTi4
+END MODULE TESTEOP
+PROGRAM TESTE
+  USE TESTEOP
+  implicit none
+  type t
+     complex :: c(3,3)
+  end type t
+  real,    parameter   :: vv(3)   = 42.
+  complex, parameter   :: zz(3,3) = (1.0,0.0)
+  integer, parameter   :: kk(3,3) = 2
+  double precision     :: dd(3,3) = 3.d0
+  COMPLEX, ALLOCATABLE :: m(:,:),r(:,:), s(:,:)
+  REAL,    ALLOCATABLE :: v(:)
+  type(t)              :: z(1) = t(zz)
+  ALLOCATE(v(3),m(3,3),r(3,3),s(3,3))
+  v = vv
+  m = zz
+  ! Original bug report
+  r=v.MULT.m ! Reference
+  s=v*m
+  if (any (r /= s)) stop 1
+  if (.not. all (r == s)) stop 2
+  ! Check other binary intrinsics
+  s=v==m
+  if (any (r /= s)) stop 3
+  s=v==copy(m)
+  if (any (r /= s)) stop 4
+  s=v==zz
+  if (any (r /= s)) stop 5
+  s=v==copy(zz)
+  if (any (r /= s)) stop 6
+  s=vv==m
+  if (any (r /= s)) stop 7
+  s=vv==copy(m)
+  if (any (r /= s)) stop 8
+  s=vv==zz
+  if (any (r /= s)) stop 9
+  s=vv==copy(zz)
+  if (any (r /= s)) stop 10
+  ! check if .eq. same operator as == etc.
+  s=v.eq.m
+  if (any (r /= s)) stop 11
+  s=v.lt.z(1)%c
+  if (any (r /= s)) stop 12
+  s=v<((z(1)%c))
+  if (any (r /= s)) stop 13
+  if (.not. all (     1.   < (vv**kk))) stop 14
+  if (.not. all (     1.   < (vv< kk))) stop 15
+  if (.not. all ((42.,0.) == (v < m ))) stop 16
+  if (.not. all ((42.,0.) == (v** m ))) stop 17
+  if (.not. all ( 126.d0  == (vv==dd))) stop 18
+END PROGRAM TESTE