b9880e
From bb76446db10c21860a4e19569ce3e350d8a2b59f Mon Sep 17 00:00:00 2001
b9880e
From: Mark Eggleston <markeggleston@gcc.gnu.org>
b9880e
Date: Fri, 22 Jan 2021 15:00:44 +0000
b9880e
Subject: [PATCH 09/10] Add the SEQUENCE attribute by default if it's not
b9880e
 present.
b9880e
b9880e
Use -fdec-sequence to enable this feature. Also enabled by -fdec.
b9880e
---
b9880e
 gcc/fortran/lang.opt                          |  4 ++
b9880e
 gcc/fortran/options.c                         |  1 +
b9880e
 gcc/fortran/resolve.c                         | 13 ++++-
b9880e
 ...dd_SEQUENCE_to_COMMON_block_by_default_1.f | 57 +++++++++++++++++++
b9880e
 ...dd_SEQUENCE_to_COMMON_block_by_default_2.f | 57 +++++++++++++++++++
b9880e
 ...dd_SEQUENCE_to_COMMON_block_by_default_3.f | 57 +++++++++++++++++++
b9880e
 6 files changed, 186 insertions(+), 3 deletions(-)
b9880e
 create mode 100644 gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_1.f
b9880e
 create mode 100644 gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_2.f
b9880e
 create mode 100644 gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_3.f
b9880e
b9880e
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
b9880e
index 4ca2f93f2df..019c798cf09 100644
b9880e
--- a/gcc/fortran/lang.opt
b9880e
+++ b/gcc/fortran/lang.opt
b9880e
@@ -509,6 +509,10 @@ fdec-promotion
b9880e
 Fortran Var(flag_dec_promotion)
b9880e
 Add support for type promotion in intrinsic arguments.
b9880e
 
b9880e
+fdec-sequence
b9880e
+Fortran Var(flag_dec_sequence)
b9880e
+Add the SEQUENCE attribute by default if it's not present.
b9880e
+
b9880e
 fdec-structure
b9880e
 Fortran Var(flag_dec_structure)
b9880e
 Enable support for DEC STRUCTURE/RECORD.
b9880e
diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c
b9880e
index 15079c7e95a..050f56fdc25 100644
b9880e
--- a/gcc/fortran/options.c
b9880e
+++ b/gcc/fortran/options.c
b9880e
@@ -83,6 +83,7 @@ set_dec_flags (int value)
b9880e
   SET_BITFLAG (flag_dec_override_kind, value, value);
b9880e
   SET_BITFLAG (flag_dec_non_logical_if, value, value);
b9880e
   SET_BITFLAG (flag_dec_promotion, value, value);
b9880e
+  SET_BITFLAG (flag_dec_sequence, value, value);
b9880e
 }
b9880e
 
b9880e
 /* Finalize DEC flags.  */
b9880e
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
b9880e
index 07dd039f3bf..fe7d0cc5944 100644
b9880e
--- a/gcc/fortran/resolve.c
b9880e
+++ b/gcc/fortran/resolve.c
b9880e
@@ -978,9 +978,16 @@ resolve_common_vars (gfc_common_head *common_block, bool named_common)
b9880e
 
b9880e
       if (!(csym->ts.u.derived->attr.sequence
b9880e
 	    || csym->ts.u.derived->attr.is_bind_c))
b9880e
-	gfc_error_now ("Derived type variable %qs in COMMON at %L "
b9880e
-		       "has neither the SEQUENCE nor the BIND(C) "
b9880e
-		       "attribute", csym->name, &csym->declared_at);
b9880e
+	{
b9880e
+	  if (flag_dec_sequence)
b9880e
+	    /* Assume sequence. */
b9880e
+	    csym->ts.u.derived->attr.sequence = 1;
b9880e
+	  else
b9880e
+	    gfc_error_now ("Derived type variable '%s' in COMMON at %L "
b9880e
+			   "has neither the SEQUENCE nor the BIND(C) "
b9880e
+			   "attribute", csym->name, &csym->declared_at);
b9880e
+	}
b9880e
+
b9880e
       if (csym->ts.u.derived->attr.alloc_comp)
b9880e
 	gfc_error_now ("Derived type variable %qs in COMMON at %L "
b9880e
 		       "has an ultimate component that is "
b9880e
diff --git a/gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_1.f b/gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_1.f
b9880e
new file mode 100644
b9880e
index 00000000000..fe7b39625eb
b9880e
--- /dev/null
b9880e
+++ b/gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_1.f
b9880e
@@ -0,0 +1,57 @@
b9880e
+! { dg-do run }
b9880e
+! { dg-options "-fdec" }
b9880e
+!
b9880e
+! Test add default SEQUENCE attribute derived types appearing in
b9880e
+! COMMON blocks and EQUIVALENCE statements.
b9880e
+!
b9880e
+! Contributed by Francisco Redondo Marchena <francisco.marchena@codethink.co.uk>
b9880e
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
b9880e
+!
b9880e
+        MODULE SEQ
b9880e
+          TYPE STRUCT1
b9880e
+            INTEGER*4     ID
b9880e
+            INTEGER*4     TYPE
b9880e
+            INTEGER*8     DEFVAL
b9880e
+            CHARACTER*(4) NAME
b9880e
+            LOGICAL*1     NIL
b9880e
+          END TYPE STRUCT1
b9880e
+        END MODULE
b9880e
+
b9880e
+        SUBROUTINE A
b9880e
+          USE SEQ
b9880e
+          TYPE (STRUCT1) S
b9880e
+          COMMON /BLOCK1/ S
b9880e
+          IF (S%ID.NE.5) STOP 1
b9880e
+          IF (S%TYPE.NE.1000) STOP 2
b9880e
+          IF (S%DEFVAL.NE.-99) STOP 3
b9880e
+          IF (S%NAME.NE."JANE") STOP 4
b9880e
+          IF (S%NIL.NEQV..FALSE.) STOP 5
b9880e
+        END SUBROUTINE
b9880e
+
b9880e
+        PROGRAM sequence_att_common
b9880e
+          USE SEQ
b9880e
+          IMPLICIT NONE
b9880e
+          TYPE (STRUCT1) S1
b9880e
+          TYPE (STRUCT1) S2
b9880e
+          TYPE (STRUCT1) S3
b9880e
+
b9880e
+          EQUIVALENCE (S1,S2)
b9880e
+          COMMON /BLOCK1/ S3
b9880e
+
b9880e
+          S1%ID = 5
b9880e
+          S1%TYPE = 1000
b9880e
+          S1%DEFVAL = -99
b9880e
+          S1%NAME = "JANE"
b9880e
+          S1%NIL = .FALSE.
b9880e
+
b9880e
+          IF (S2%ID.NE.5) STOP 1
b9880e
+          IF (S2%TYPE.NE.1000) STOP 2
b9880e
+          IF (S2%DEFVAL.NE.-99) STOP 3
b9880e
+          IF (S2%NAME.NE."JANE") STOP 4
b9880e
+          IF (S2%NIL.NEQV..FALSE.) STOP 5
b9880e
+
b9880e
+          S3 = S1
b9880e
+
b9880e
+          CALL A
b9880e
+          
b9880e
+        END
b9880e
diff --git a/gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_2.f b/gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_2.f
b9880e
new file mode 100644
b9880e
index 00000000000..83512f0f3a2
b9880e
--- /dev/null
b9880e
+++ b/gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_2.f
b9880e
@@ -0,0 +1,57 @@
b9880e
+! { dg-do run }
b9880e
+! { dg-options "-fdec-sequence" }
b9880e
+!
b9880e
+! Test add default SEQUENCE attribute derived types appearing in
b9880e
+! COMMON blocks and EQUIVALENCE statements.
b9880e
+!
b9880e
+! Contributed by Francisco Redondo Marchena <francisco.marchena@codethink.co.uk>
b9880e
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
b9880e
+!
b9880e
+        MODULE SEQ
b9880e
+          TYPE STRUCT1
b9880e
+            INTEGER*4     ID
b9880e
+            INTEGER*4     TYPE
b9880e
+            INTEGER*8     DEFVAL
b9880e
+            CHARACTER*(4) NAME
b9880e
+            LOGICAL*1     NIL
b9880e
+          END TYPE STRUCT1
b9880e
+        END MODULE
b9880e
+
b9880e
+        SUBROUTINE A
b9880e
+          USE SEQ
b9880e
+          TYPE (STRUCT1) S
b9880e
+          COMMON /BLOCK1/ S
b9880e
+          IF (S%ID.NE.5) STOP 1
b9880e
+          IF (S%TYPE.NE.1000) STOP 2
b9880e
+          IF (S%DEFVAL.NE.-99) STOP 3
b9880e
+          IF (S%NAME.NE."JANE") STOP 4
b9880e
+          IF (S%NIL.NEQV..FALSE.) STOP 5
b9880e
+        END SUBROUTINE
b9880e
+
b9880e
+        PROGRAM sequence_att_common
b9880e
+          USE SEQ
b9880e
+          IMPLICIT NONE
b9880e
+          TYPE (STRUCT1) S1
b9880e
+          TYPE (STRUCT1) S2
b9880e
+          TYPE (STRUCT1) S3
b9880e
+
b9880e
+          EQUIVALENCE (S1,S2)
b9880e
+          COMMON /BLOCK1/ S3
b9880e
+
b9880e
+          S1%ID = 5
b9880e
+          S1%TYPE = 1000
b9880e
+          S1%DEFVAL = -99
b9880e
+          S1%NAME = "JANE"
b9880e
+          S1%NIL = .FALSE.
b9880e
+
b9880e
+          IF (S2%ID.NE.5) STOP 1
b9880e
+          IF (S2%TYPE.NE.1000) STOP 2
b9880e
+          IF (S2%DEFVAL.NE.-99) STOP 3
b9880e
+          IF (S2%NAME.NE."JANE") STOP 4
b9880e
+          IF (S2%NIL.NEQV..FALSE.) STOP 5
b9880e
+
b9880e
+          S3 = S1
b9880e
+
b9880e
+          CALL A
b9880e
+          
b9880e
+        END
b9880e
diff --git a/gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_3.f b/gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_3.f
b9880e
new file mode 100644
b9880e
index 00000000000..26cd59f9090
b9880e
--- /dev/null
b9880e
+++ b/gcc/testsuite/gfortran.dg/dec_add_SEQUENCE_to_COMMON_block_by_default_3.f
b9880e
@@ -0,0 +1,57 @@
b9880e
+! { dg-do compile }
b9880e
+! { dg-options "-fdec -fno-dec-sequence" }
b9880e
+!
b9880e
+! Test add default SEQUENCE attribute derived types appearing in
b9880e
+! COMMON blocks and EQUIVALENCE statements.
b9880e
+!
b9880e
+! Contributed by Francisco Redondo Marchena <francisco.marchena@codethink.co.uk>
b9880e
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
b9880e
+!
b9880e
+        MODULE SEQ
b9880e
+          TYPE STRUCT1
b9880e
+            INTEGER*4     ID
b9880e
+            INTEGER*4     TYPE
b9880e
+            INTEGER*8     DEFVAL
b9880e
+            CHARACTER*(4) NAME
b9880e
+            LOGICAL*1     NIL
b9880e
+          END TYPE STRUCT1
b9880e
+        END MODULE
b9880e
+
b9880e
+        SUBROUTINE A
b9880e
+          USE SEQ
b9880e
+          TYPE (STRUCT1) S ! { dg-error "Derived type variable" }
b9880e
+          COMMON /BLOCK1/ S
b9880e
+          IF (S%ID.NE.5) STOP 1
b9880e
+          IF (S%TYPE.NE.1000) STOP 2
b9880e
+          IF (S%DEFVAL.NE.-99) STOP 3
b9880e
+          IF (S%NAME.NE."JANE") STOP 4
b9880e
+          IF (S%NIL.NEQV..FALSE.) STOP 5
b9880e
+        END SUBROUTINE
b9880e
+
b9880e
+        PROGRAM sequence_att_common
b9880e
+          USE SEQ
b9880e
+          IMPLICIT NONE
b9880e
+          TYPE (STRUCT1) S1
b9880e
+          TYPE (STRUCT1) S2
b9880e
+          TYPE (STRUCT1) S3 ! { dg-error "Derived type variable" }
b9880e
+
b9880e
+          EQUIVALENCE (S1,S2) ! { dg-error "Derived type variable" }
b9880e
+          COMMON /BLOCK1/ S3
b9880e
+
b9880e
+          S1%ID = 5
b9880e
+          S1%TYPE = 1000
b9880e
+          S1%DEFVAL = -99
b9880e
+          S1%NAME = "JANE"
b9880e
+          S1%NIL = .FALSE.
b9880e
+
b9880e
+          IF (S2%ID.NE.5) STOP 1
b9880e
+          IF (S2%TYPE.NE.1000) STOP 2
b9880e
+          IF (S2%DEFVAL.NE.-99) STOP 3
b9880e
+          IF (S2%NAME.NE."JANE") STOP 4
b9880e
+          IF (S2%NIL.NEQV..FALSE.) STOP 5
b9880e
+
b9880e
+          S3 = S1
b9880e
+
b9880e
+          CALL A
b9880e
+          
b9880e
+        END
b9880e
-- 
b9880e
2.27.0
b9880e