Blame SOURCES/0009-Convert-LOGICAL-to-INTEGER-for-arithmetic-ops-and-vi.patch

3db796
From 00f13a60974cb4145799593398cc61894326c222 Mon Sep 17 00:00:00 2001
3db796
From: Jim MacArthur <jim.macarthur@codethink.co.uk>
3db796
Date: Wed, 7 Oct 2015 16:31:18 -0400
3db796
Subject: [PATCH 09/23] Convert LOGICAL to INTEGER for arithmetic ops, and vice
3db796
 versa
3db796
3db796
We allow converting LOGICAL types to INTEGER when doing arithmetic
3db796
operations, and converting INTEGER types to LOGICAL for use in
3db796
boolean operations.
3db796
3db796
This feature is enabled with the `-std=extra-legacy` compiler flag.
3db796
6068c7
commit f40dbd54915de8155aad94bfa19c22f11b8a8eae
6068c7
Author: Jim MacArthur <jim.macarthur@codethink.co.uk>
6068c7
Date:   Wed Oct 7 16:31:18 2015 -0400
6068c7
6068c7
    Convert LOGICAL to INTEGER for arithmetic ops, and vice versa
6068c7
    
6068c7
    We allow converting LOGICAL types to INTEGER when doing arithmetic
6068c7
    operations, and converting INTEGER types to LOGICAL for use in
6068c7
    boolean operations.
6068c7
    
6068c7
    This feature is enabled with the `-std=extra-legacy` compiler flag.
6068c7
    
6068c7
    Test written by: Francisco Redondo Marchena <francisco.marchena@codethink.co.uk>
3db796
3db796
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
6068c7
index 667cc5073e3..33b441aa1bc 100644
3db796
--- a/gcc/fortran/resolve.c
3db796
+++ b/gcc/fortran/resolve.c
6068c7
@@ -3623,6 +3623,22 @@ is_character_based (bt type)
6068c7
   return type == BT_CHARACTER || type == BT_HOLLERITH;
6068c7
 }
3db796
 
6068c7
+/* If E is a logical, convert it to an integer and issue a warning
6068c7
+   for the conversion.  */
6068c7
+
6068c7
+static void
3db796
+convert_integer_to_logical (gfc_expr *e)
3db796
+{
3db796
+  if (e->ts.type == BT_INTEGER)
3db796
+    {
3db796
+      /* Convert to LOGICAL */
3db796
+      gfc_typespec t;
3db796
+      t.type = BT_LOGICAL;
3db796
+      t.kind = 1;
3db796
+      gfc_convert_type_warn (e, &t, 2, 1);
3db796
+    }
3db796
+}
3db796
+
6068c7
 /* If E is a logical, convert it to an integer and issue a warning
6068c7
    for the conversion.  */
6068c7
 
3db796
@@ -3733,6 +3749,12 @@ resolve_operator (gfc_expr *e)
3db796
     case INTRINSIC_OR:
3db796
     case INTRINSIC_EQV:
3db796
     case INTRINSIC_NEQV:
3db796
+      if (gfc_option.allow_std & GFC_STD_EXTRA_LEGACY)
3db796
+	{
3db796
+	  convert_integer_to_logical (op1);
3db796
+	  convert_integer_to_logical (op2);
3db796
+	}
3db796
+
3db796
       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3db796
 	{
3db796
 	  e->ts.type = BT_LOGICAL;
3db796
@@ -3774,6 +3796,11 @@ resolve_operator (gfc_expr *e)
3db796
 	  return resolve_function (e);
3db796
 	}
3db796
 
3db796
+      if (gfc_option.allow_std & GFC_STD_EXTRA_LEGACY)
3db796
+	{
3db796
+	  convert_integer_to_logical (op1);
3db796
+	}
3db796
+
3db796
       if (op1->ts.type == BT_LOGICAL)
3db796
 	{
3db796
 	  e->ts.type = BT_LOGICAL;
6068c7
diff --git a/gcc/testsuite/gfortran.dg/dec_logical_to_integer_and_vice_versa.f b/gcc/testsuite/gfortran.dg/dec_logical_to_integer_and_vice_versa.f
6068c7
new file mode 100644
6068c7
index 00000000000..7b9ec0d0cd2
6068c7
--- /dev/null
6068c7
+++ b/gcc/testsuite/gfortran.dg/dec_logical_to_integer_and_vice_versa.f
6068c7
@@ -0,0 +1,27 @@
6068c7
+! { dg-do compile }
6068c7
+! { dg-options "-std=extra-legacy" }
6068c7
+!
6068c7
+! Test convertion between logical and integer for logical operators
6068c7
+!
6068c7
+        PROGRAM logical_integer_conversion
6068c7
+          LOGICAL lpos /.true./
6068c7
+          INTEGER ineg/0/
6068c7
+          INTEGER ires
6068c7
+          LOGICAL lres
6068c7
+
6068c7
+          ! Test Logicals converted to Integers
6068c7
+          if ((lpos.AND.ineg).EQ.1) STOP 3
6068c7
+          if ((ineg.AND.lpos).NE.0) STOP 4
6068c7
+          ires = (.true..AND.0)
6068c7
+          if (ires.NE.0) STOP 5
6068c7
+          ires = (1.AND..false.)
6068c7
+          if (ires.EQ.1) STOP 6
6068c7
+
6068c7
+          ! Test Integers converted to Logicals
6068c7
+          if (lpos.EQ.ineg) STOP 7
6068c7
+          if (ineg.EQ.lpos) STOP 8
6068c7
+          lres = (.true..EQ.0)
6068c7
+          if (lres) STOP 9
6068c7
+          lres = (1.EQ..false.)
6068c7
+          if (lres) STOP 10
6068c7
+        END