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

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