Blame SOURCES/0016-Allow-calls-to-intrinsics-with-smaller-types-than-sp.patch

840d93
From 109b1eeba24e5091bf3bdb6caedf7101a9dcaa6a Mon Sep 17 00:00:00 2001
840d93
From: Jim MacArthur <jim.macarthur@codethink.co.uk>
840d93
Date: Wed, 18 Nov 2015 11:50:41 +0000
840d93
Subject: [PATCH 16/23] Allow calls to intrinsics with smaller types than
840d93
 specified
840d93
840d93
This feature is enabled by the `-std=extra-legacy` compiler flag.
840d93
---
840d93
840d93
    0016-Allow-calls-to-intrinsics-with-smaller-types-than-sp.patch
840d93
840d93
diff -Nrup a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
840d93
--- a/gcc/fortran/gfortran.h	2018-06-05 11:59:14.269337049 -0600
840d93
+++ b/gcc/fortran/gfortran.h	2018-06-05 11:59:52.830081690 -0600
840d93
@@ -656,6 +656,13 @@ enum gfc_param_spec_type
840d93
   SPEC_DEFERRED
840d93
 };
840d93
 
840d93
+enum match_type
840d93
+{
840d93
+  MATCH_EXACT,
840d93
+  MATCH_PROMOTABLE,
840d93
+  MATCH_INVALID
840d93
+};
840d93
+
840d93
 /************************* Structures *****************************/
840d93
 
840d93
 /* Used for keeping things in balanced binary trees.  */
840d93
@@ -3251,7 +3253,7 @@ bool gfc_add_interface (gfc_symbol *);
840d93
 gfc_interface *gfc_current_interface_head (void);
840d93
 void gfc_set_current_interface_head (gfc_interface *);
840d93
 gfc_symtree* gfc_find_sym_in_symtree (gfc_symbol*);
840d93
-bool gfc_arglist_matches_symbol (gfc_actual_arglist**, gfc_symbol*);
840d93
+bool gfc_arglist_matches_symbol (gfc_actual_arglist**, gfc_symbol*, enum match_type mtype);
840d93
 bool gfc_check_operator_interface (gfc_symbol*, gfc_intrinsic_op, locus);
840d93
 bool gfc_has_vector_subscript (gfc_expr*);
840d93
 gfc_intrinsic_op gfc_equivalent_op (gfc_intrinsic_op);
840d93
diff -Nrup a/gcc/fortran/interface.c b/gcc/fortran/interface.c
840d93
--- a/gcc/fortran/interface.c	2018-03-03 06:51:39.000000000 -0700
840d93
+++ b/gcc/fortran/interface.c	2018-06-05 12:01:11.218559539 -0600
840d93
@@ -682,7 +682,7 @@ gfc_compare_derived_types (gfc_symbol *d
840d93
 /* Compare two typespecs, recursively if necessary.  */
840d93
 
840d93
 bool
840d93
-gfc_compare_types (gfc_typespec *ts1, gfc_typespec *ts2)
840d93
+gfc_compare_types_generic (gfc_typespec *ts1, gfc_typespec *ts2, enum match_type mtype)
840d93
 {
840d93
   /* See if one of the typespecs is a BT_VOID, which is what is being used
840d93
      to allow the funcs like c_f_pointer to accept any pointer type.
840d93
@@ -721,12 +721,23 @@ gfc_compare_types (gfc_typespec *ts1, gf
840d93
     return compare_union_types (ts1->u.derived, ts2->u.derived);
840d93
 
840d93
   if (ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
840d93
-    return (ts1->kind == ts2->kind);
840d93
+    {
840d93
+    if (mtype == MATCH_PROMOTABLE)
840d93
+      return (ts1->kind >= ts2->kind);
840d93
+    else
840d93
+      return (ts1->kind == ts2->kind);
840d93
+    }
840d93
+
840d93
 
840d93
   /* Compare derived types.  */
840d93
   return gfc_type_compatible (ts1, ts2);
840d93
 }
840d93
 
840d93
+bool
840d93
+gfc_compare_types (gfc_typespec *ts1, gfc_typespec *ts2)
840d93
+{
840d93
+  return gfc_compare_types_generic (ts1, ts2, MATCH_EXACT);
840d93
+}
840d93
 
840d93
 static bool
840d93
 compare_type (gfc_symbol *s1, gfc_symbol *s2)
840d93
@@ -743,7 +754,9 @@ compare_type (gfc_symbol *s1, gfc_symbol
840d93
   return compare_type (s1, s2);
840d93
 }
840d93
 
840d93
-
840d93
+/* Given two symbols that are formal arguments, compare their ranks
840d93
+   and types.  Returns nonzero if they have the same rank and type,
840d93
+   zero otherwise.  */
840d93
 static bool
840d93
 compare_rank (gfc_symbol *s1, gfc_symbol *s2)
840d93
 {
840d93
@@ -2150,7 +2163,7 @@ argument_rank_mismatch (const char *name
840d93
 
840d93
 static bool
840d93
 compare_parameter (gfc_symbol *formal, gfc_expr *actual,
840d93
-		   int ranks_must_agree, int is_elemental, locus *where)
840d93
+                   int ranks_must_agree, int is_elemental, locus *where, enum match_type mtype)
840d93
 {
840d93
   gfc_ref *ref;
840d93
   bool rank_check, is_pointer;
840d93
@@ -2242,7 +2255,7 @@ compare_parameter (gfc_symbol *formal, g
840d93
       && actual->ts.type != BT_HOLLERITH
840d93
       && formal->ts.type != BT_ASSUMED
840d93
       && !(formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
840d93
-      && !gfc_compare_types (&formal->ts, &actual->ts)
840d93
+      && !gfc_compare_types_generic (&formal->ts, &actual->ts, mtype)
840d93
       && !(formal->ts.type == BT_DERIVED && actual->ts.type == BT_CLASS
840d93
 	   && gfc_compare_derived_types (formal->ts.u.derived,
840d93
 					 CLASS_DATA (actual)->ts.u.derived)))
840d93
@@ -2792,7 +2805,8 @@ is_procptr_result (gfc_expr *expr)
840d93
 static bool
840d93
 compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
840d93
 	 	       int ranks_must_agree, int is_elemental,
840d93
-		       bool in_statement_function, locus *where)
840d93
+		       bool in_statement_function, locus *where,
840d93
+		       enum match_type mtype)
840d93
 {
840d93
   gfc_actual_arglist **new_arg, *a, *actual;
840d93
   gfc_formal_arglist *f;
840d93
@@ -2918,7 +2932,7 @@ compare_actual_formal (gfc_actual_arglis
840d93
 	}
840d93
 
840d93
       if (!compare_parameter (f->sym, a->expr, ranks_must_agree,
840d93
-			      is_elemental, where))
840d93
+			      is_elemental, where, mtype))
840d93
 	return false;
840d93
 
840d93
       /* TS 29113, 6.3p2.  */
840d93
@@ -3666,7 +3680,7 @@ gfc_procedure_use (gfc_symbol *sym, gfc_
840d93
   /* For a statement function, check that types and type parameters of actual
840d93
      arguments and dummy arguments match.  */
840d93
   if (!compare_actual_formal (ap, dummy_args, 0, sym->attr.elemental,
840d93
-			      sym->attr.proc == PROC_ST_FUNCTION, where))
840d93
+			      sym->attr.proc == PROC_ST_FUNCTION, where, MATCH_PROMOTABLE))
840d93
     return false;
840d93
  
840d93
   if (!check_intents (dummy_args, *ap))
840d93
@@ -3715,7 +3730,7 @@ gfc_ppc_use (gfc_component *comp, gfc_ac
840d93
     }
840d93
 
840d93
   if (!compare_actual_formal (ap, comp->ts.interface->formal, 0,
840d93
-			      comp->attr.elemental, false, where))
840d93
+			      comp->attr.elemental, false, where, MATCH_EXACT))
840d93
     return;
840d93
 
840d93
   check_intents (comp->ts.interface->formal, *ap);
840d93
@@ -3729,7 +3744,7 @@ gfc_ppc_use (gfc_component *comp, gfc_ac
840d93
    GENERIC resolution.  */
840d93
 
840d93
 bool
840d93
-gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym)
840d93
+gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym, enum match_type mtype)
840d93
 {
840d93
   gfc_formal_arglist *dummy_args;
840d93
   bool r;
840d93
@@ -3740,7 +3755,7 @@ gfc_arglist_matches_symbol (gfc_actual_a
840d93
   dummy_args = gfc_sym_get_dummy_args (sym);
840d93
 
840d93
   r = !sym->attr.elemental;
840d93
-  if (compare_actual_formal (args, dummy_args, r, !r, false, NULL))
840d93
+  if (compare_actual_formal (args, dummy_args, r, !r, false, NULL, mtype))
840d93
     {
840d93
       check_intents (dummy_args, *args);
840d93
       if (warn_aliasing)
840d93
@@ -3766,7 +3781,8 @@ gfc_search_interface (gfc_interface *int
840d93
   locus null_expr_loc;
840d93
   gfc_actual_arglist *a;
840d93
   bool has_null_arg = false;
840d93
-
840d93
+  enum match_type mtypes[] = { MATCH_EXACT, MATCH_PROMOTABLE };
840d93
+  int i;
840d93
   for (a = *ap; a; a = a->next)
840d93
     if (a->expr && a->expr->expr_type == EXPR_NULL
840d93
 	&& a->expr->ts.type == BT_UNKNOWN)
840d93
@@ -3776,38 +3792,43 @@ gfc_search_interface (gfc_interface *int
840d93
 	break;
840d93
       }
840d93
 
840d93
-  for (; intr; intr = intr->next)
840d93
+  for (i=0; i<2; i++)
840d93
     {
840d93
+      for (; intr; intr = intr->next)
840d93
+	{
840d93
+	  if (intr->sym->attr.flavor == FL_DERIVED)
840d93
+	    continue;
840d93
       if (gfc_fl_struct (intr->sym->attr.flavor))
840d93
 	continue;
840d93
-      if (sub_flag && intr->sym->attr.function)
840d93
-	continue;
840d93
-      if (!sub_flag && intr->sym->attr.subroutine)
840d93
+	  if (sub_flag && intr->sym->attr.function)
840d93
+	    continue;
840d93
+	  if (!sub_flag && intr->sym->attr.subroutine)
840d93
 	continue;
840d93
 
840d93
-      if (gfc_arglist_matches_symbol (ap, intr->sym))
840d93
-	{
840d93
-	  if (has_null_arg && null_sym)
840d93
-	    {
840d93
-	      gfc_error ("MOLD= required in NULL() argument at %L: Ambiguity "
840d93
-			 "between specific functions %s and %s",
840d93
-			 &null_expr_loc, null_sym->name, intr->sym->name);
840d93
-	      return NULL;
840d93
-	    }
840d93
-	  else if (has_null_arg)
840d93
+	  if (gfc_arglist_matches_symbol (ap, intr->sym, mtypes[i]))
840d93
 	    {
840d93
-	      null_sym = intr->sym;
840d93
-	      continue;
840d93
-	    }
840d93
+	      if (has_null_arg && null_sym)
840d93
+		{
840d93
+		  gfc_error ("MOLD= required in NULL() argument at %L: Ambiguity "
840d93
+			     "between specific functions %s and %s",
840d93
+			     &null_expr_loc, null_sym->name, intr->sym->name);
840d93
+		  return NULL;
840d93
+		}
840d93
+	      else if (has_null_arg)
840d93
+		{
840d93
+		  null_sym = intr->sym;
840d93
+		  continue;
840d93
+		}
840d93
 
840d93
-	  /* Satisfy 12.4.4.1 such that an elemental match has lower
840d93
-	     weight than a non-elemental match.  */
840d93
-	  if (intr->sym->attr.elemental)
840d93
-	    {
840d93
-	      elem_sym = intr->sym;
840d93
-	      continue;
840d93
+	      /* Satisfy 12.4.4.1 such that an elemental match has lower
840d93
+		 weight than a non-elemental match.  */
840d93
+	      if (intr->sym->attr.elemental)
840d93
+		{
840d93
+		  elem_sym = intr->sym;
840d93
+		  continue;
840d93
+		}
840d93
+	      return intr->sym;
840d93
 	    }
840d93
-	  return intr->sym;
840d93
 	}
840d93
     }
840d93
 
840d93
@@ -3942,7 +3963,7 @@ matching_typebound_op (gfc_expr** tb_bas
840d93
 
840d93
 		/* Check if this arglist matches the formal.  */
840d93
 		argcopy = gfc_copy_actual_arglist (args);
840d93
-		matches = gfc_arglist_matches_symbol (&argcopy, target);
840d93
+		matches = gfc_arglist_matches_symbol (&argcopy, target, MATCH_EXACT);
840d93
 		gfc_free_actual_arglist (argcopy);
840d93
 
840d93
 		/* Return if we found a match.  */
840d93
diff -Nrup a/gcc/fortran/intrinsic.c b/gcc/fortran/intrinsic.c
840d93
--- a/gcc/fortran/intrinsic.c	2018-06-05 11:59:14.278336990 -0600
840d93
+++ b/gcc/fortran/intrinsic.c	2018-06-05 11:59:52.831081683 -0600
840d93
@@ -4229,6 +4229,16 @@ check_arglist (gfc_actual_arglist **ap,
840d93
       if (ts.kind == 0)
840d93
 	ts.kind = actual->expr->ts.kind;
840d93
 
840d93
+      /* ts.kind is the argument spec. actual is what was passed. */
840d93
+
840d93
+      if (actual->expr->ts.kind < ts.kind
840d93
+	  && ts.type == BT_INTEGER)
840d93
+	{
840d93
+	  /* If it was OK to overwrite ts.kind in the previous case, it
840d93
+	     should be fine here... */
840d93
+	  ts.kind = actual->expr->ts.kind;
840d93
+	}
840d93
+
840d93
       if (!gfc_compare_types (&ts, &actual->expr->ts))
840d93
 	{
840d93
 	  if (error_flag)
840d93
diff -Nrup a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
840d93
--- a/gcc/fortran/resolve.c	2018-06-05 11:59:14.291336904 -0600
840d93
+++ b/gcc/fortran/resolve.c	2018-06-05 11:59:52.833081670 -0600
840d93
@@ -6055,7 +6055,7 @@ resolve_typebound_generic_call (gfc_expr
840d93
 				  && gfc_sym_get_dummy_args (target) == NULL);
840d93
 
840d93
 	  /* Check if this arglist matches the formal.  */
840d93
-	  matches = gfc_arglist_matches_symbol (&args, target);
840d93
+	  matches = gfc_arglist_matches_symbol (&args, target, MATCH_EXACT);
840d93
 
840d93
 	  /* Clean up and break out of the loop if we've found it.  */
840d93
 	  gfc_free_actual_arglist (args);