LCOV - code coverage report
Current view: top level - gcc/fortran - interface.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 92.8 % 2822 2619
Test Date: 2026-08-01 15:33:25 Functions: 100.0 % 77 77
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Deal with interfaces.
       2              :    Copyright (C) 2000-2026 Free Software Foundation, Inc.
       3              :    Contributed by Andy Vaught
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : 
      22              : /* Deal with interfaces.  An explicit interface is represented as a
      23              :    singly linked list of formal argument structures attached to the
      24              :    relevant symbols.  For an implicit interface, the arguments don't
      25              :    point to symbols.  Explicit interfaces point to namespaces that
      26              :    contain the symbols within that interface.
      27              : 
      28              :    Implicit interfaces are linked together in a singly linked list
      29              :    along the next_if member of symbol nodes.  Since a particular
      30              :    symbol can only have a single explicit interface, the symbol cannot
      31              :    be part of multiple lists and a single next-member suffices.
      32              : 
      33              :    This is not the case for general classes, though.  An operator
      34              :    definition is independent of just about all other uses and has it's
      35              :    own head pointer.
      36              : 
      37              :    Nameless interfaces:
      38              :      Nameless interfaces create symbols with explicit interfaces within
      39              :      the current namespace.  They are otherwise unlinked.
      40              : 
      41              :    Generic interfaces:
      42              :      The generic name points to a linked list of symbols.  Each symbol
      43              :      has an explicit interface.  Each explicit interface has its own
      44              :      namespace containing the arguments.  Module procedures are symbols in
      45              :      which the interface is added later when the module procedure is parsed.
      46              : 
      47              :    User operators:
      48              :      User-defined operators are stored in a their own set of symtrees
      49              :      separate from regular symbols.  The symtrees point to gfc_user_op
      50              :      structures which in turn head up a list of relevant interfaces.
      51              : 
      52              :    Extended intrinsics and assignment:
      53              :      The head of these interface lists are stored in the containing namespace.
      54              : 
      55              :    Implicit interfaces:
      56              :      An implicit interface is represented as a singly linked list of
      57              :      formal argument list structures that don't point to any symbol
      58              :      nodes -- they just contain types.
      59              : 
      60              : 
      61              :    When a subprogram is defined, the program unit's name points to an
      62              :    interface as usual, but the link to the namespace is NULL and the
      63              :    formal argument list points to symbols within the same namespace as
      64              :    the program unit name.  */
      65              : 
      66              : #include "config.h"
      67              : #include "system.h"
      68              : #include "coretypes.h"
      69              : #include "options.h"
      70              : #include "gfortran.h"
      71              : #include "match.h"
      72              : #include "arith.h"
      73              : 
      74              : /* The current_interface structure holds information about the
      75              :    interface currently being parsed.  This structure is saved and
      76              :    restored during recursive interfaces.  */
      77              : 
      78              : gfc_interface_info current_interface;
      79              : 
      80              : 
      81              : /* Free the leading members of the gfc_interface linked list given in INTR
      82              :    up to the END element (exclusive: the END element is not freed).
      83              :    If END is not nullptr, it is assumed that END is in the linked list starting
      84              :    with INTR.  */
      85              : 
      86              : static void
      87     22255371 : free_interface_elements_until (gfc_interface *intr, gfc_interface *end)
      88              : {
      89     22255371 :   gfc_interface *next;
      90              : 
      91     22455157 :   for (; intr != end; intr = next)
      92              :     {
      93       199786 :       next = intr->next;
      94       199786 :       free (intr);
      95              :     }
      96            0 : }
      97              : 
      98              : 
      99              : /* Free a singly linked list of gfc_interface structures.  */
     100              : 
     101              : void
     102     21542292 : gfc_free_interface (gfc_interface *intr)
     103              : {
     104     21542292 :   free_interface_elements_until (intr, nullptr);
     105     21542292 : }
     106              : 
     107              : 
     108              : /* Update the interface pointer given by IFC_PTR to make it point to TAIL.
     109              :    It is expected that TAIL (if non-null) is in the list pointed to by
     110              :    IFC_PTR, hence the tail of it.  The members of the list before TAIL are
     111              :    freed before the pointer reassignment.  */
     112              : 
     113              : void
     114      9144837 : gfc_drop_interface_elements_before (gfc_interface **ifc_ptr,
     115              :                                     gfc_interface *tail)
     116              : {
     117      9144837 :   if (ifc_ptr == nullptr)
     118              :     return;
     119              : 
     120       713079 :   free_interface_elements_until (*ifc_ptr, tail);
     121       713079 :   *ifc_ptr = tail;
     122              : }
     123              : 
     124              : 
     125              : /* Change the operators unary plus and minus into binary plus and
     126              :    minus respectively, leaving the rest unchanged.  */
     127              : 
     128              : static gfc_intrinsic_op
     129         3003 : fold_unary_intrinsic (gfc_intrinsic_op op)
     130              : {
     131            0 :   switch (op)
     132              :     {
     133            0 :     case INTRINSIC_UPLUS:
     134            0 :       op = INTRINSIC_PLUS;
     135            0 :       break;
     136           56 :     case INTRINSIC_UMINUS:
     137           56 :       op = INTRINSIC_MINUS;
     138            0 :       break;
     139              :     default:
     140              :       break;
     141              :     }
     142              : 
     143         2989 :   return op;
     144              : }
     145              : 
     146              : 
     147              : /* Return the operator depending on the DTIO moded string.  Note that
     148              :    these are not operators in the normal sense and so have been placed
     149              :    beyond GFC_INTRINSIC_END in gfortran.h:enum gfc_intrinsic_op.  */
     150              : 
     151              : static gfc_intrinsic_op
     152          410 : dtio_op (char* mode)
     153              : {
     154          410 :   if (strcmp (mode, "formatted") == 0)
     155              :     return INTRINSIC_FORMATTED;
     156           84 :   if (strcmp (mode, "unformatted") == 0)
     157           84 :     return INTRINSIC_UNFORMATTED;
     158              :   return INTRINSIC_NONE;
     159              : }
     160              : 
     161              : 
     162              : /* Match a generic specification.  Depending on which type of
     163              :    interface is found, the 'name' or 'op' pointers may be set.
     164              :    This subroutine doesn't return MATCH_NO.  */
     165              : 
     166              : match
     167        29899 : gfc_match_generic_spec (interface_type *type,
     168              :                         char *name,
     169              :                         gfc_intrinsic_op *op)
     170              : {
     171        29899 :   char buffer[GFC_MAX_SYMBOL_LEN + 1];
     172        29899 :   match m;
     173        29899 :   gfc_intrinsic_op i;
     174              : 
     175        29899 :   if (gfc_match (" assignment ( = )") == MATCH_YES)
     176              :     {
     177          574 :       *type = INTERFACE_INTRINSIC_OP;
     178          574 :       *op = INTRINSIC_ASSIGN;
     179          574 :       return MATCH_YES;
     180              :     }
     181              : 
     182        29325 :   if (gfc_match (" operator ( %o )", &i) == MATCH_YES)
     183              :     {                           /* Operator i/f */
     184          770 :       *type = INTERFACE_INTRINSIC_OP;
     185          770 :       *op = fold_unary_intrinsic (i);
     186          770 :       return MATCH_YES;
     187              :     }
     188              : 
     189        28555 :   *op = INTRINSIC_NONE;
     190        28555 :   if (gfc_match (" operator ( ") == MATCH_YES)
     191              :     {
     192          368 :       m = gfc_match_defined_op_name (buffer, 1);
     193          368 :       if (m == MATCH_NO)
     194            0 :         goto syntax;
     195          368 :       if (m != MATCH_YES)
     196              :         return MATCH_ERROR;
     197              : 
     198          368 :       m = gfc_match_char (')');
     199          368 :       if (m == MATCH_NO)
     200            0 :         goto syntax;
     201          368 :       if (m != MATCH_YES)
     202              :         return MATCH_ERROR;
     203              : 
     204          368 :       strcpy (name, buffer);
     205          368 :       *type = INTERFACE_USER_OP;
     206          368 :       return MATCH_YES;
     207              :     }
     208              : 
     209        28187 :   if (gfc_match (" read ( %n )", buffer) == MATCH_YES)
     210              :     {
     211          166 :       *op = dtio_op (buffer);
     212          166 :       if (*op == INTRINSIC_FORMATTED)
     213              :         {
     214          123 :           if (flag_default_integer)
     215            0 :             goto conflict;
     216          123 :           strcpy (name, gfc_code2string (dtio_procs, DTIO_RF));
     217          123 :           *type = INTERFACE_DTIO;
     218              :         }
     219          166 :       if (*op == INTRINSIC_UNFORMATTED)
     220              :         {
     221           43 :           if (flag_default_integer)
     222            0 :             goto conflict;
     223           43 :           strcpy (name, gfc_code2string (dtio_procs, DTIO_RUF));
     224           43 :           *type = INTERFACE_DTIO;
     225              :         }
     226          166 :       if (*op != INTRINSIC_NONE)
     227              :         return MATCH_YES;
     228              :     }
     229              : 
     230        28021 :   if (gfc_match (" write ( %n )", buffer) == MATCH_YES)
     231              :     {
     232          244 :       *op = dtio_op (buffer);
     233          244 :       if (*op == INTRINSIC_FORMATTED)
     234              :         {
     235          203 :           if (flag_default_integer)
     236            1 :             goto conflict;
     237          202 :           strcpy (name, gfc_code2string (dtio_procs, DTIO_WF));
     238          202 :           *type = INTERFACE_DTIO;
     239              :         }
     240          243 :       if (*op == INTRINSIC_UNFORMATTED)
     241              :         {
     242           41 :           if (flag_default_integer)
     243            0 :             goto conflict;
     244           41 :           strcpy (name, gfc_code2string (dtio_procs, DTIO_WUF));
     245           41 :           *type = INTERFACE_DTIO;
     246              :         }
     247          243 :       if (*op != INTRINSIC_NONE)
     248              :         return MATCH_YES;
     249              :     }
     250              : 
     251        27777 :   if (gfc_match_name (buffer) == MATCH_YES)
     252              :     {
     253        22041 :       strcpy (name, buffer);
     254        22041 :       *type = INTERFACE_GENERIC;
     255        22041 :       return MATCH_YES;
     256              :     }
     257              : 
     258         5736 :   *type = INTERFACE_NAMELESS;
     259         5736 :   return MATCH_YES;
     260              : 
     261            1 : conflict:
     262            1 :   gfc_error ("Sorry: -fdefault-integer-8 option is not supported with "
     263              :              "user-defined input/output at %C");
     264            1 :   return MATCH_ERROR;
     265              : 
     266            0 : syntax:
     267            0 :   gfc_error ("Syntax error in generic specification at %C");
     268            0 :   return MATCH_ERROR;
     269              : }
     270              : 
     271              : 
     272              : /* Match one of the five F95 forms of an interface statement.  The
     273              :    matcher for the abstract interface follows.  */
     274              : 
     275              : match
     276        10859 : gfc_match_interface (void)
     277              : {
     278        10859 :   char name[GFC_MAX_SYMBOL_LEN + 1];
     279        10859 :   interface_type type;
     280        10859 :   gfc_symbol *sym;
     281        10859 :   gfc_intrinsic_op op;
     282        10859 :   match m;
     283              : 
     284        10859 :   m = gfc_match_space ();
     285              : 
     286        10859 :   if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
     287              :     return MATCH_ERROR;
     288              : 
     289              :   /* If we're not looking at the end of the statement now, or if this
     290              :      is not a nameless interface but we did not see a space, punt.  */
     291        10858 :   if (gfc_match_eos () != MATCH_YES
     292        10858 :       || (type != INTERFACE_NAMELESS && m != MATCH_YES))
     293              :     {
     294            0 :       gfc_error ("Syntax error: Trailing garbage in INTERFACE statement "
     295              :                  "at %C");
     296            0 :       return MATCH_ERROR;
     297              :     }
     298              : 
     299        10858 :   current_interface.type = type;
     300              : 
     301        10858 :   switch (type)
     302              :     {
     303         4414 :     case INTERFACE_DTIO:
     304         4414 :     case INTERFACE_GENERIC:
     305         4414 :       if (gfc_get_symbol (name, NULL, &sym))
     306              :         return MATCH_ERROR;
     307              : 
     308         4414 :       if (!sym->attr.generic
     309         4414 :           && !gfc_add_generic (&sym->attr, sym->name, NULL))
     310              :         return MATCH_ERROR;
     311              : 
     312         4413 :       if (sym->attr.dummy)
     313              :         {
     314            0 :           gfc_error ("Dummy procedure %qs at %C cannot have a "
     315              :                      "generic interface", sym->name);
     316            0 :           return MATCH_ERROR;
     317              :         }
     318              : 
     319         4413 :       current_interface.sym = gfc_new_block = sym;
     320         4413 :       break;
     321              : 
     322          156 :     case INTERFACE_USER_OP:
     323          156 :       current_interface.uop = gfc_get_uop (name);
     324          156 :       break;
     325              : 
     326          556 :     case INTERFACE_INTRINSIC_OP:
     327          556 :       current_interface.op = op;
     328          556 :       break;
     329              : 
     330              :     case INTERFACE_NAMELESS:
     331              :     case INTERFACE_ABSTRACT:
     332              :       break;
     333              :     }
     334              : 
     335              :   return MATCH_YES;
     336              : }
     337              : 
     338              : 
     339              : 
     340              : /* Match a F2003 abstract interface.  */
     341              : 
     342              : match
     343          476 : gfc_match_abstract_interface (void)
     344              : {
     345          476 :   match m;
     346              : 
     347          476 :   if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT INTERFACE at %C"))
     348              :     return MATCH_ERROR;
     349              : 
     350          475 :   m = gfc_match_eos ();
     351              : 
     352          475 :   if (m != MATCH_YES)
     353              :     {
     354            1 :       gfc_error ("Syntax error in ABSTRACT INTERFACE statement at %C");
     355            1 :       return MATCH_ERROR;
     356              :     }
     357              : 
     358          474 :   current_interface.type = INTERFACE_ABSTRACT;
     359              : 
     360          474 :   return m;
     361              : }
     362              : 
     363              : 
     364              : /* Match the different sort of generic-specs that can be present after
     365              :    the END INTERFACE itself.  */
     366              : 
     367              : match
     368          696 : gfc_match_end_interface (void)
     369              : {
     370          696 :   char name[GFC_MAX_SYMBOL_LEN + 1];
     371          696 :   interface_type type;
     372          696 :   gfc_intrinsic_op op;
     373          696 :   match m;
     374              : 
     375          696 :   m = gfc_match_space ();
     376              : 
     377          696 :   if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
     378              :     return MATCH_ERROR;
     379              : 
     380              :   /* If we're not looking at the end of the statement now, or if this
     381              :      is not a nameless interface but we did not see a space, punt.  */
     382          696 :   if (gfc_match_eos () != MATCH_YES
     383          696 :       || (type != INTERFACE_NAMELESS && m != MATCH_YES))
     384              :     {
     385            0 :       gfc_error ("Syntax error: Trailing garbage in END INTERFACE "
     386              :                  "statement at %C");
     387            0 :       return MATCH_ERROR;
     388              :     }
     389              : 
     390          696 :   m = MATCH_YES;
     391              : 
     392          696 :   switch (current_interface.type)
     393              :     {
     394            0 :     case INTERFACE_NAMELESS:
     395            0 :     case INTERFACE_ABSTRACT:
     396            0 :       if (type != INTERFACE_NAMELESS)
     397              :         {
     398            0 :           gfc_error ("Expected a nameless interface at %C");
     399            0 :           m = MATCH_ERROR;
     400              :         }
     401              : 
     402              :       break;
     403              : 
     404          157 :     case INTERFACE_INTRINSIC_OP:
     405          157 :       if (type != current_interface.type || op != current_interface.op)
     406              :         {
     407              : 
     408           14 :           if (current_interface.op == INTRINSIC_ASSIGN)
     409              :             {
     410            0 :               m = MATCH_ERROR;
     411            0 :               gfc_error ("Expected %<END INTERFACE ASSIGNMENT (=)%> at %C");
     412              :             }
     413              :           else
     414              :             {
     415           14 :               const char *s1, *s2;
     416           14 :               s1 = gfc_op2string (current_interface.op);
     417           14 :               s2 = gfc_op2string (op);
     418              : 
     419              :               /* The following if-statements are used to enforce C1202
     420              :                  from F2003.  */
     421           14 :               if ((strcmp(s1, "==") == 0 && strcmp (s2, ".eq.") == 0)
     422           13 :                   || (strcmp(s1, ".eq.") == 0 && strcmp (s2, "==") == 0))
     423              :                 break;
     424           12 :               if ((strcmp(s1, "/=") == 0 && strcmp (s2, ".ne.") == 0)
     425           11 :                   || (strcmp(s1, ".ne.") == 0 && strcmp (s2, "/=") == 0))
     426              :                 break;
     427           10 :               if ((strcmp(s1, "<=") == 0 && strcmp (s2, ".le.") == 0)
     428            9 :                   || (strcmp(s1, ".le.") == 0 && strcmp (s2, "<=") == 0))
     429              :                 break;
     430            8 :               if ((strcmp(s1, "<") == 0 && strcmp (s2, ".lt.") == 0)
     431            7 :                   || (strcmp(s1, ".lt.") == 0 && strcmp (s2, "<") == 0))
     432              :                 break;
     433            6 :               if ((strcmp(s1, ">=") == 0 && strcmp (s2, ".ge.") == 0)
     434            5 :                   || (strcmp(s1, ".ge.") == 0 && strcmp (s2, ">=") == 0))
     435              :                 break;
     436            4 :               if ((strcmp(s1, ">") == 0 && strcmp (s2, ".gt.") == 0)
     437            3 :                   || (strcmp(s1, ".gt.") == 0 && strcmp (s2, ">") == 0))
     438              :                 break;
     439              : 
     440            2 :               m = MATCH_ERROR;
     441            2 :               if (strcmp(s2, "none") == 0)
     442            1 :                 gfc_error ("Expecting %<END INTERFACE OPERATOR (%s)%> "
     443              :                            "at %C", s1);
     444              :               else
     445            1 :                 gfc_error ("Expecting %<END INTERFACE OPERATOR (%s)%> at %C, "
     446              :                            "but got %qs", s1, s2);
     447              :             }
     448              : 
     449              :         }
     450              : 
     451              :       break;
     452              : 
     453           15 :     case INTERFACE_USER_OP:
     454              :       /* Comparing the symbol node names is OK because only use-associated
     455              :          symbols can be renamed.  */
     456           15 :       if (type != current_interface.type
     457           15 :           || strcmp (current_interface.uop->name, name) != 0)
     458              :         {
     459            0 :           gfc_error ("Expecting %<END INTERFACE OPERATOR (.%s.)%> at %C",
     460            0 :                      current_interface.uop->name);
     461            0 :           m = MATCH_ERROR;
     462              :         }
     463              : 
     464              :       break;
     465              : 
     466          524 :     case INTERFACE_DTIO:
     467          524 :     case INTERFACE_GENERIC:
     468              :       /* If a use-associated symbol is renamed, check the local_name.   */
     469          524 :       const char *local_name = current_interface.sym->name;
     470              : 
     471          524 :       if (current_interface.sym->attr.use_assoc
     472            4 :           && current_interface.sym->attr.use_rename
     473            2 :           && current_interface.sym->ns->use_stmts->rename
     474            2 :           && (current_interface.sym->ns->use_stmts->rename->local_name[0]
     475              :               != '\0'))
     476            1 :         local_name = current_interface.sym->ns->use_stmts->rename->local_name;
     477              : 
     478          524 :       if (type != current_interface.type
     479          524 :           || strcmp (local_name, name) != 0)
     480              :         {
     481            0 :           gfc_error ("Expecting %<END INTERFACE %s%> at %C", local_name);
     482            0 :           m = MATCH_ERROR;
     483              :         }
     484              : 
     485              :       break;
     486              :     }
     487              : 
     488              :   return m;
     489              : }
     490              : 
     491              : 
     492              : /* Return whether the component was defined anonymously.  */
     493              : 
     494              : static bool
     495         9769 : is_anonymous_component (gfc_component *cmp)
     496              : {
     497              :   /* Only UNION and MAP components are anonymous.  In the case of a MAP,
     498              :      the derived type symbol is FL_STRUCT and the component name looks like mM*.
     499              :      This is the only case in which the second character of a component name is
     500              :      uppercase.  */
     501         9769 :   return cmp->ts.type == BT_UNION
     502         9769 :     || (cmp->ts.type == BT_DERIVED
     503         3640 :         && cmp->ts.u.derived->attr.flavor == FL_STRUCT
     504           72 :         && cmp->name[0] && cmp->name[1] && ISUPPER (cmp->name[1]));
     505              : }
     506              : 
     507              : 
     508              : /* Return whether the derived type was defined anonymously.  */
     509              : 
     510              : static bool
     511       599061 : is_anonymous_dt (gfc_symbol *derived)
     512              : {
     513              :   /* UNION and MAP types are always anonymous. Otherwise, only nested STRUCTURE
     514              :      types can be anonymous.  For anonymous MAP/STRUCTURE, we have FL_STRUCT
     515              :      and the type name looks like XX*.  This is the only case in which the
     516              :      second character of a type name is uppercase.  */
     517       599061 :   return derived->attr.flavor == FL_UNION
     518       599061 :     || (derived->attr.flavor == FL_STRUCT
     519         3345 :         && derived->name[0] && derived->name[1] && ISUPPER (derived->name[1]));
     520              : }
     521              : 
     522              : 
     523              : /* Compare components according to 4.4.2 of the Fortran standard.  */
     524              : 
     525              : static bool
     526         5048 : compare_components (gfc_component *cmp1, gfc_component *cmp2,
     527              :     gfc_symbol *derived1, gfc_symbol *derived2)
     528              : {
     529              :   /* Compare names, but not for anonymous components such as UNION or MAP.  */
     530         4721 :   if (!is_anonymous_component (cmp1) && !is_anonymous_component (cmp2)
     531         9484 :       && strcmp (cmp1->name, cmp2->name) != 0)
     532              :     return false;
     533              : 
     534         4185 :   if (cmp1->attr.access != cmp2->attr.access)
     535              :     return false;
     536              : 
     537         4184 :   if (cmp1->attr.pointer != cmp2->attr.pointer)
     538              :     return false;
     539              : 
     540         4184 :   if (cmp1->attr.dimension != cmp2->attr.dimension)
     541              :     return false;
     542              : 
     543         4050 :   if (cmp1->attr.codimension != cmp2->attr.codimension)
     544              :     return false;
     545              : 
     546         4050 :   if (cmp1->attr.allocatable != cmp2->attr.allocatable)
     547              :     return false;
     548              : 
     549         4050 :   if (cmp1->attr.dimension && gfc_compare_array_spec (cmp1->as, cmp2->as) == 0)
     550              :     return false;
     551              : 
     552         3646 :   if (cmp1->attr.codimension
     553         3646 :       && gfc_compare_array_spec (cmp1->as, cmp2->as) == 0)
     554              :     return false;
     555              : 
     556         3646 :   if (cmp1->ts.type == BT_CHARACTER && cmp2->ts.type == BT_CHARACTER)
     557              :     {
     558           75 :       gfc_charlen *l1 = cmp1->ts.u.cl;
     559           75 :       gfc_charlen *l2 = cmp2->ts.u.cl;
     560           75 :       if (l1 && l2 && l1->length && l2->length
     561           75 :           && l1->length->expr_type == EXPR_CONSTANT
     562           75 :           && l2->length->expr_type == EXPR_CONSTANT
     563          150 :           && gfc_dep_compare_expr (l1->length, l2->length) != 0)
     564              :         return false;
     565              :     }
     566              : 
     567              :   /* Make sure that link lists do not put this function into an
     568              :      endless recursive loop!  */
     569         1421 :   if (!(cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
     570         3494 :       && !(cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived)
     571         7135 :       && !gfc_compare_types (&cmp1->ts, &cmp2->ts))
     572              :     return false;
     573              : 
     574         3223 :   else if ( (cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
     575          147 :         && !(cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived))
     576              :     return false;
     577              : 
     578         3223 :   else if (!(cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
     579         3076 :         &&  (cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived))
     580              :     return false;
     581              : 
     582              :   return true;
     583              : }
     584              : 
     585              : 
     586              : /* Compare two union types by comparing the components of their maps.
     587              :    Because unions and maps are anonymous their types get special internal
     588              :    names; therefore the usual derived type comparison will fail on them.
     589              : 
     590              :    Returns nonzero if equal, as with gfc_compare_derived_types. Also as with
     591              :    gfc_compare_derived_types, 'equal' is closer to meaning 'duplicate
     592              :    definitions' than 'equivalent structure'. */
     593              : 
     594              : static bool
     595          793 : compare_union_types (gfc_symbol *un1, gfc_symbol *un2)
     596              : {
     597          793 :   gfc_component *map1, *map2, *cmp1, *cmp2;
     598          793 :   gfc_symbol *map1_t, *map2_t;
     599              : 
     600          793 :   if (un1->attr.flavor != FL_UNION || un2->attr.flavor != FL_UNION)
     601              :     return false;
     602              : 
     603          148 :   if (un1->attr.zero_comp != un2->attr.zero_comp)
     604              :     return false;
     605              : 
     606          148 :   if (un1->attr.zero_comp)
     607              :     return true;
     608              : 
     609          146 :   map1 = un1->components;
     610          146 :   map2 = un2->components;
     611              : 
     612              :   /* In terms of 'equality' here we are worried about types which are
     613              :      declared the same in two places, not types that represent equivalent
     614              :      structures. (This is common because of FORTRAN's weird scoping rules.)
     615              :      Though two unions with their maps in different orders could be equivalent,
     616              :      we will say they are not equal for the purposes of this test; therefore
     617              :      we compare the maps sequentially. */
     618          229 :   for (;;)
     619              :     {
     620          229 :       map1_t = map1->ts.u.derived;
     621          229 :       map2_t = map2->ts.u.derived;
     622              : 
     623          229 :       cmp1 = map1_t->components;
     624          229 :       cmp2 = map2_t->components;
     625              : 
     626              :       /* Protect against null components.  */
     627          229 :       if (map1_t->attr.zero_comp != map2_t->attr.zero_comp)
     628              :         return false;
     629              : 
     630          229 :       if (map1_t->attr.zero_comp)
     631              :         return true;
     632              : 
     633          609 :       for (;;)
     634              :         {
     635              :           /* No two fields will ever point to the same map type unless they are
     636              :              the same component, because one map field is created with its type
     637              :              declaration. Therefore don't worry about recursion here. */
     638              :           /* TODO: worry about recursion into parent types of the unions? */
     639          609 :           if (!compare_components (cmp1, cmp2, map1_t, map2_t))
     640              :             return false;
     641              : 
     642          603 :           cmp1 = cmp1->next;
     643          603 :           cmp2 = cmp2->next;
     644              : 
     645          603 :           if (cmp1 == NULL && cmp2 == NULL)
     646              :             break;
     647          384 :           if (cmp1 == NULL || cmp2 == NULL)
     648              :             return false;
     649              :         }
     650              : 
     651          219 :       map1 = map1->next;
     652          219 :       map2 = map2->next;
     653              : 
     654          219 :       if (map1 == NULL && map2 == NULL)
     655              :         break;
     656           83 :       if (map1 == NULL || map2 == NULL)
     657              :         return false;
     658              :     }
     659              : 
     660              :   return true;
     661              : }
     662              : 
     663              : 
     664              : 
     665              : /* Compare two derived types using the criteria in 4.4.2 of the standard,
     666              :    recursing through gfc_compare_types for the components.  */
     667              : 
     668              : bool
     669       630138 : gfc_compare_derived_types (gfc_symbol *derived1, gfc_symbol *derived2)
     670              : {
     671       630138 :   gfc_component *cmp1, *cmp2;
     672              : 
     673       630138 :   if (derived1 == derived2)
     674              :     return true;
     675              : 
     676       331025 :   if (!derived1 || !derived2)
     677            0 :     gfc_internal_error ("gfc_compare_derived_types: invalid derived type");
     678              : 
     679       331025 :   if (derived1->attr.unlimited_polymorphic
     680          187 :       && derived2->attr.unlimited_polymorphic)
     681              :     return true;
     682              : 
     683       330852 :   if (derived1->attr.unlimited_polymorphic
     684       330852 :       != derived2->attr.unlimited_polymorphic)
     685              :     return false;
     686              : 
     687              :   /* Compare UNION types specially.  */
     688       330763 :   if (derived1->attr.flavor == FL_UNION || derived2->attr.flavor == FL_UNION)
     689          645 :     return compare_union_types (derived1, derived2);
     690              : 
     691              :   /* Special case for comparing derived types across namespaces.  If the
     692              :      true names and module names are the same and the module name is
     693              :      nonnull, then they are equal.  */
     694       330118 :   if (strcmp (derived1->name, derived2->name) == 0
     695        32903 :       && derived1->module != NULL && derived2->module != NULL
     696        30469 :       && strcmp (derived1->module, derived2->module) == 0)
     697              :     return true;
     698              : 
     699              :   /* Compare type via the rules of the standard.  Both types must have the
     700              :      SEQUENCE or BIND(C) attribute to be equal.  We also compare types
     701              :      recursively if they are class descriptors types or virtual tables types.
     702              :      STRUCTUREs are special because they can be anonymous; therefore two
     703              :      structures with different names may be equal.  */
     704              : 
     705              :   /* Compare names, but not for anonymous types such as UNION or MAP.  */
     706       298966 :   if (!is_anonymous_dt (derived1) && !is_anonymous_dt (derived2)
     707       598706 :       && strcmp (derived1->name, derived2->name) != 0)
     708              :     return false;
     709              : 
     710         4364 :   if (derived1->component_access == ACCESS_PRIVATE
     711         4363 :       || derived2->component_access == ACCESS_PRIVATE)
     712              :     return false;
     713              : 
     714         4363 :   if (!(derived1->attr.sequence && derived2->attr.sequence)
     715         2622 :       && !(derived1->attr.is_bind_c && derived2->attr.is_bind_c)
     716         2609 :       && !(derived1->attr.is_class && derived2->attr.is_class)
     717         1677 :       && !(derived1->attr.vtype && derived2->attr.vtype)
     718         1519 :       && !(derived1->attr.pdt_type && derived2->attr.pdt_type))
     719              :     return false;
     720              : 
     721              :   /* Protect against null components.  */
     722         2844 :   if (derived1->attr.zero_comp != derived2->attr.zero_comp)
     723              :     return false;
     724              : 
     725         2835 :   if (derived1->attr.zero_comp)
     726              :     return true;
     727              : 
     728         2835 :   cmp1 = derived1->components;
     729         2835 :   cmp2 = derived2->components;
     730              : 
     731              :   /* Since subtypes of SEQUENCE types must be SEQUENCE types as well, a
     732              :      simple test can speed things up.  Otherwise, lots of things have to
     733              :      match.  */
     734         4439 :   for (;;)
     735              :     {
     736         4439 :       if (!compare_components (cmp1, cmp2, derived1, derived2))
     737              :         return false;
     738              : 
     739         2620 :       cmp1 = cmp1->next;
     740         2620 :       cmp2 = cmp2->next;
     741              : 
     742         2620 :       if (cmp1 == NULL && cmp2 == NULL)
     743              :         break;
     744         1610 :       if (cmp1 == NULL || cmp2 == NULL)
     745              :         return false;
     746              :     }
     747              : 
     748              :   return true;
     749              : }
     750              : 
     751              : 
     752              : /* Compare two typespecs, recursively if necessary.  */
     753              : 
     754              : bool
     755      7534061 : gfc_compare_types (gfc_typespec *ts1, gfc_typespec *ts2)
     756              : {
     757              :   /* See if one of the typespecs is a BT_VOID, which is what is being used
     758              :      to allow the funcs like c_f_pointer to accept any pointer type.
     759              :      TODO: Possibly should narrow this to just the one typespec coming in
     760              :      that is for the formal arg, but oh well.  */
     761      7534061 :   if (ts1->type == BT_VOID || ts2->type == BT_VOID)
     762              :     return true;
     763              : 
     764              :   /* Special case for our C interop types.  FIXME: There should be a
     765              :      better way of doing this.  When ISO C binding is cleared up,
     766              :      this can probably be removed.  See PR 57048.  */
     767              : 
     768      7534032 :   if ((ts1->type == BT_INTEGER
     769      2006469 :        && ts2->type == BT_DERIVED
     770         5656 :        && ts1->f90_type == BT_VOID
     771           86 :        && ts2->u.derived->from_intmod == INTMOD_ISO_C_BINDING
     772           86 :        && ts1->u.derived
     773           86 :        && strcmp (ts1->u.derived->name, ts2->u.derived->name) == 0)
     774      7533947 :       || (ts2->type == BT_INTEGER
     775      2145876 :           && ts1->type == BT_DERIVED
     776         5212 :           && ts2->f90_type == BT_VOID
     777           84 :           && ts1->u.derived->from_intmod == INTMOD_ISO_C_BINDING
     778           84 :           && ts2->u.derived
     779           84 :           && strcmp (ts1->u.derived->name, ts2->u.derived->name) == 0))
     780              :     return true;
     781              : 
     782              :   /* The _data component is not always present, therefore check for its
     783              :      presence before assuming, that its derived->attr is available.
     784              :      When the _data component is not present, then nevertheless the
     785              :      unlimited_polymorphic flag may be set in the derived type's attr.  */
     786      7533863 :   if (ts1->type == BT_CLASS && ts1->u.derived->components
     787        32265 :       && ((ts1->u.derived->attr.is_class
     788        32258 :            && ts1->u.derived->components->ts.u.derived->attr
     789        32258 :                                                   .unlimited_polymorphic)
     790        26780 :           || ts1->u.derived->attr.unlimited_polymorphic))
     791              :     return true;
     792              : 
     793              :   /* F2003: C717  */
     794      7528378 :   if (ts2->type == BT_CLASS && ts1->type == BT_DERIVED
     795          977 :       && ts2->u.derived->components
     796          976 :       && ((ts2->u.derived->attr.is_class
     797          974 :            && ts2->u.derived->components->ts.u.derived->attr
     798          974 :                                                   .unlimited_polymorphic)
     799          935 :           || ts2->u.derived->attr.unlimited_polymorphic)
     800           41 :       && (ts1->u.derived->attr.sequence || ts1->u.derived->attr.is_bind_c))
     801              :     return true;
     802              : 
     803      7528352 :   if (ts1->type != ts2->type
     804      1061741 :       && ((ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
     805        72303 :           || (ts2->type != BT_DERIVED && ts2->type != BT_CLASS)))
     806              :     return false;
     807              : 
     808      6475584 :   if (ts1->type == BT_UNION)
     809          148 :     return compare_union_types (ts1->u.derived, ts2->u.derived);
     810              : 
     811      6475436 :   if (ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
     812      6189956 :     return (ts1->kind == ts2->kind);
     813              : 
     814              :   /* Compare derived types.  */
     815       285480 :   return gfc_type_compatible (ts1, ts2);
     816              : }
     817              : 
     818              : 
     819              : static bool
     820      5312741 : compare_type (gfc_symbol *s1, gfc_symbol *s2)
     821              : {
     822      5312741 :   if (s2->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
     823              :     return true;
     824              : 
     825      5112293 :   return gfc_compare_types (&s1->ts, &s2->ts) || s2->ts.type == BT_ASSUMED;
     826              : }
     827              : 
     828              : 
     829              : static bool
     830       309611 : compare_type_characteristics (gfc_symbol *s1, gfc_symbol *s2)
     831              : {
     832              :   /* TYPE and CLASS of the same declared type are type compatible,
     833              :      but have different characteristics.  */
     834       309611 :   if ((s1->ts.type == BT_CLASS && s2->ts.type == BT_DERIVED)
     835       309603 :       || (s1->ts.type == BT_DERIVED && s2->ts.type == BT_CLASS))
     836              :     return false;
     837              : 
     838       309602 :   return compare_type (s1, s2);
     839              : }
     840              : 
     841              : 
     842              : static bool
     843       921439 : compare_rank (gfc_symbol *s1, gfc_symbol *s2)
     844              : {
     845       921439 :   gfc_array_spec *as1, *as2;
     846       921439 :   int r1, r2;
     847              : 
     848       921439 :   if (s2->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
     849              :     return true;
     850              : 
     851       716522 :   as1 = (s1->ts.type == BT_CLASS
     852         5169 :          && !s1->ts.u.derived->attr.unlimited_polymorphic)
     853       726856 :         ? CLASS_DATA (s1)->as : s1->as;
     854       716540 :   as2 = (s2->ts.type == BT_CLASS
     855         5151 :          && !s2->ts.u.derived->attr.unlimited_polymorphic)
     856       726838 :         ? CLASS_DATA (s2)->as : s2->as;
     857              : 
     858       721689 :   r1 = as1 ? as1->rank : 0;
     859       721689 :   r2 = as2 ? as2->rank : 0;
     860              : 
     861       721689 :   if (r1 != r2 && (!as2 || as2->type != AS_ASSUMED_RANK))
     862         3862 :     return false;  /* Ranks differ.  */
     863              : 
     864              :   return true;
     865              : }
     866              : 
     867              : 
     868              : /* Given two symbols that are formal arguments, compare their ranks
     869              :    and types.  Returns true if they have the same rank and type,
     870              :    false otherwise.  */
     871              : 
     872              : static bool
     873      4999832 : compare_type_rank (gfc_symbol *s1, gfc_symbol *s2)
     874              : {
     875      4999832 :   return compare_type (s1, s2) && compare_rank (s1, s2);
     876              : }
     877              : 
     878              : 
     879              : /* Given two symbols that are formal arguments, compare their types
     880              :    and rank and their formal interfaces if they are both dummy
     881              :    procedures.  Returns true if the same, false if different.  */
     882              : 
     883              : static bool
     884      4881825 : compare_type_rank_if (gfc_symbol *s1, gfc_symbol *s2)
     885              : {
     886      4881825 :   if (s1 == NULL || s2 == NULL)
     887          120 :     return (s1 == s2);
     888              : 
     889      4881705 :   if (s1 == s2)
     890              :     return true;
     891              : 
     892      4881705 :   if (s1->attr.flavor != FL_PROCEDURE && s2->attr.flavor != FL_PROCEDURE)
     893      4881535 :     return compare_type_rank (s1, s2);
     894              : 
     895          170 :   if (s1->attr.flavor != FL_PROCEDURE || s2->attr.flavor != FL_PROCEDURE)
     896              :     return false;
     897              : 
     898              :   /* At this point, both symbols are procedures.  It can happen that
     899              :      external procedures are compared, where one is identified by usage
     900              :      to be a function or subroutine but the other is not.  Check TKR
     901              :      nonetheless for these cases.  */
     902            6 :   if (s1->attr.function == 0 && s1->attr.subroutine == 0)
     903            2 :     return s1->attr.external ? compare_type_rank (s1, s2) : false;
     904              : 
     905            4 :   if (s2->attr.function == 0 && s2->attr.subroutine == 0)
     906            0 :     return s2->attr.external ? compare_type_rank (s1, s2) : false;
     907              : 
     908              :   /* Now the type of procedure has been identified.  */
     909            4 :   if (s1->attr.function != s2->attr.function
     910            4 :       || s1->attr.subroutine != s2->attr.subroutine)
     911              :     return false;
     912              : 
     913            4 :   if (s1->attr.function && !compare_type_rank (s1, s2))
     914              :     return false;
     915              : 
     916              :   /* Originally, gfortran recursed here to check the interfaces of passed
     917              :      procedures.  This is explicitly not required by the standard.  */
     918              :   return true;
     919              : }
     920              : 
     921              : 
     922              : /* Given a formal argument list and a keyword name, search the list
     923              :    for that keyword.  Returns the correct symbol node if found, NULL
     924              :    if not found.  */
     925              : 
     926              : static gfc_symbol *
     927        34328 : find_keyword_arg (const char *name, gfc_formal_arglist *f)
     928              : {
     929        49586 :   for (; f; f = f->next)
     930        49586 :     if (strcmp (f->sym->name, name) == 0)
     931              :       return f->sym;
     932              : 
     933              :   return NULL;
     934              : }
     935              : 
     936              : 
     937              : /******** Interface checking subroutines **********/
     938              : 
     939              : 
     940              : /* Given an operator interface and the operator, make sure that all
     941              :    interfaces for that operator are legal.  */
     942              : 
     943              : bool
     944         3599 : gfc_check_operator_interface (gfc_symbol *sym, gfc_intrinsic_op op,
     945              :                               locus opwhere)
     946              : {
     947         3599 :   gfc_formal_arglist *formal;
     948         3599 :   sym_intent i1, i2;
     949         3599 :   bt t1, t2;
     950         3599 :   int args, r1, r2, k1, k2;
     951              : 
     952         3599 :   gcc_assert (sym);
     953              : 
     954         3599 :   args = 0;
     955         3599 :   t1 = t2 = BT_UNKNOWN;
     956         3599 :   i1 = i2 = INTENT_UNKNOWN;
     957         3599 :   r1 = r2 = -1;
     958         3599 :   k1 = k2 = -1;
     959              : 
     960        10765 :   for (formal = gfc_sym_get_dummy_args (sym); formal; formal = formal->next)
     961              :     {
     962         7167 :       gfc_symbol *fsym = formal->sym;
     963         7167 :       if (fsym == NULL)
     964              :         {
     965            1 :           gfc_error ("Alternate return cannot appear in operator "
     966              :                      "interface at %L", &sym->declared_at);
     967            1 :           return false;
     968              :         }
     969         7166 :       if (args == 0)
     970              :         {
     971         3599 :           t1 = fsym->ts.type;
     972         3599 :           i1 = fsym->attr.intent;
     973         3599 :           r1 = (fsym->as != NULL) ? fsym->as->rank : 0;
     974         3599 :           k1 = fsym->ts.kind;
     975              :         }
     976         7166 :       if (args == 1)
     977              :         {
     978         3567 :           t2 = fsym->ts.type;
     979         3567 :           i2 = fsym->attr.intent;
     980         3567 :           r2 = (fsym->as != NULL) ? fsym->as->rank : 0;
     981         3567 :           k2 = fsym->ts.kind;
     982              :         }
     983         7166 :       args++;
     984              :     }
     985              : 
     986              :   /* Only +, - and .not. can be unary operators.
     987              :      .not. cannot be a binary operator.  */
     988         3598 :   if (args == 0 || args > 2 || (args == 1 && op != INTRINSIC_PLUS
     989           30 :                                 && op != INTRINSIC_MINUS
     990           30 :                                 && op != INTRINSIC_NOT)
     991         3597 :       || (args == 2 && op == INTRINSIC_NOT))
     992              :     {
     993            1 :       if (op == INTRINSIC_ASSIGN)
     994            0 :         gfc_error ("Assignment operator interface at %L must have "
     995              :                    "two arguments", &sym->declared_at);
     996              :       else
     997            1 :         gfc_error ("Operator interface at %L has the wrong number of arguments",
     998              :                    &sym->declared_at);
     999            1 :       return false;
    1000              :     }
    1001              : 
    1002              :   /* Check that intrinsics are mapped to functions, except
    1003              :      INTRINSIC_ASSIGN which should map to a subroutine.  */
    1004         3597 :   if (op == INTRINSIC_ASSIGN)
    1005              :     {
    1006         1385 :       gfc_formal_arglist *dummy_args;
    1007              : 
    1008         1385 :       if (!sym->attr.subroutine)
    1009              :         {
    1010            1 :           gfc_error ("Assignment operator interface at %L must be "
    1011              :                      "a SUBROUTINE", &sym->declared_at);
    1012            1 :           return false;
    1013              :         }
    1014              : 
    1015              :       /* Allowed are (per F2003, 12.3.2.1.2 Defined assignments):
    1016              :          - First argument an array with different rank than second,
    1017              :          - First argument is a scalar and second an array,
    1018              :          - Types and kinds do not conform, or
    1019              :          - First argument is of derived type.  */
    1020         1384 :       dummy_args = gfc_sym_get_dummy_args (sym);
    1021         1384 :       if (dummy_args->sym->ts.type != BT_DERIVED
    1022         1153 :           && dummy_args->sym->ts.type != BT_CLASS
    1023           94 :           && (r2 == 0 || r1 == r2)
    1024         1473 :           && (dummy_args->sym->ts.type == dummy_args->next->sym->ts.type
    1025           84 :               || (gfc_numeric_ts (&dummy_args->sym->ts)
    1026           50 :                   && gfc_numeric_ts (&dummy_args->next->sym->ts))))
    1027              :         {
    1028            5 :           gfc_error ("Assignment operator interface at %L must not redefine "
    1029              :                      "an INTRINSIC type assignment", &sym->declared_at);
    1030            5 :           return false;
    1031              :         }
    1032              :     }
    1033              :   else
    1034              :     {
    1035         2212 :       if (!sym->attr.function)
    1036              :         {
    1037            1 :           gfc_error ("Intrinsic operator interface at %L must be a FUNCTION",
    1038              :                      &sym->declared_at);
    1039            1 :           return false;
    1040              :         }
    1041              :     }
    1042              : 
    1043              :   /* Check intents on operator interfaces.  */
    1044         3590 :   if (op == INTRINSIC_ASSIGN)
    1045              :     {
    1046         1379 :       if (i1 != INTENT_OUT && i1 != INTENT_INOUT)
    1047              :         {
    1048            0 :           gfc_error ("First argument of defined assignment at %L must be "
    1049              :                      "INTENT(OUT) or INTENT(INOUT)", &sym->declared_at);
    1050            0 :           return false;
    1051              :         }
    1052              : 
    1053         1379 :       if (i2 != INTENT_IN)
    1054              :         {
    1055            0 :           gfc_error ("Second argument of defined assignment at %L must be "
    1056              :                      "INTENT(IN)", &sym->declared_at);
    1057            0 :           return false;
    1058              :         }
    1059              :     }
    1060              :   else
    1061              :     {
    1062         2211 :       if (i1 != INTENT_IN)
    1063              :         {
    1064            0 :           gfc_error ("First argument of operator interface at %L must be "
    1065              :                      "INTENT(IN)", &sym->declared_at);
    1066            0 :           return false;
    1067              :         }
    1068              : 
    1069         2211 :       if (args == 2 && i2 != INTENT_IN)
    1070              :         {
    1071            0 :           gfc_error ("Second argument of operator interface at %L must be "
    1072              :                      "INTENT(IN)", &sym->declared_at);
    1073            0 :           return false;
    1074              :         }
    1075              :     }
    1076              : 
    1077              :   /* From now on, all we have to do is check that the operator definition
    1078              :      doesn't conflict with an intrinsic operator. The rules for this
    1079              :      game are defined in 7.1.2 and 7.1.3 of both F95 and F2003 standards,
    1080              :      as well as 12.3.2.1.1 of Fortran 2003:
    1081              : 
    1082              :      "If the operator is an intrinsic-operator (R310), the number of
    1083              :      function arguments shall be consistent with the intrinsic uses of
    1084              :      that operator, and the types, kind type parameters, or ranks of the
    1085              :      dummy arguments shall differ from those required for the intrinsic
    1086              :      operation (7.1.2)."  */
    1087              : 
    1088              : #define IS_NUMERIC_TYPE(t) \
    1089              :   ((t) == BT_INTEGER || (t) == BT_REAL || (t) == BT_COMPLEX)
    1090              : 
    1091              :   /* Unary ops are easy, do them first.  */
    1092         3590 :   if (op == INTRINSIC_NOT)
    1093              :     {
    1094            5 :       if (t1 == BT_LOGICAL)
    1095            0 :         goto bad_repl;
    1096              :       else
    1097              :         return true;
    1098              :     }
    1099              : 
    1100         3585 :   if (args == 1 && (op == INTRINSIC_PLUS || op == INTRINSIC_MINUS))
    1101              :     {
    1102           25 :       if (IS_NUMERIC_TYPE (t1))
    1103            0 :         goto bad_repl;
    1104              :       else
    1105              :         return true;
    1106              :     }
    1107              : 
    1108              :   /* Character intrinsic operators have same character kind, thus
    1109              :      operator definitions with operands of different character kinds
    1110              :      are always safe.  */
    1111         3560 :   if (t1 == BT_CHARACTER && t2 == BT_CHARACTER && k1 != k2)
    1112              :     return true;
    1113              : 
    1114              :   /* Intrinsic operators always perform on arguments of same rank,
    1115              :      so different ranks is also always safe.  (rank == 0) is an exception
    1116              :      to that, because all intrinsic operators are elemental.  */
    1117         3560 :   if (r1 != r2 && r1 != 0 && r2 != 0)
    1118              :     return true;
    1119              : 
    1120         3494 :   switch (op)
    1121              :   {
    1122         1019 :     case INTRINSIC_EQ:
    1123         1019 :     case INTRINSIC_EQ_OS:
    1124         1019 :     case INTRINSIC_NE:
    1125         1019 :     case INTRINSIC_NE_OS:
    1126         1019 :       if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
    1127            0 :         goto bad_repl;
    1128              :       /* Fall through.  */
    1129              : 
    1130         1748 :     case INTRINSIC_PLUS:
    1131         1748 :     case INTRINSIC_MINUS:
    1132         1748 :     case INTRINSIC_TIMES:
    1133         1748 :     case INTRINSIC_DIVIDE:
    1134         1748 :     case INTRINSIC_POWER:
    1135         1748 :       if (IS_NUMERIC_TYPE (t1) && IS_NUMERIC_TYPE (t2))
    1136            2 :         goto bad_repl;
    1137              :       break;
    1138              : 
    1139          278 :     case INTRINSIC_GT:
    1140          278 :     case INTRINSIC_GT_OS:
    1141          278 :     case INTRINSIC_GE:
    1142          278 :     case INTRINSIC_GE_OS:
    1143          278 :     case INTRINSIC_LT:
    1144          278 :     case INTRINSIC_LT_OS:
    1145          278 :     case INTRINSIC_LE:
    1146          278 :     case INTRINSIC_LE_OS:
    1147          278 :       if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
    1148            1 :         goto bad_repl;
    1149          277 :       if ((t1 == BT_INTEGER || t1 == BT_REAL)
    1150            0 :           && (t2 == BT_INTEGER || t2 == BT_REAL))
    1151            0 :         goto bad_repl;
    1152              :       break;
    1153              : 
    1154           36 :     case INTRINSIC_CONCAT:
    1155           36 :       if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
    1156            0 :         goto bad_repl;
    1157              :       break;
    1158              : 
    1159           56 :     case INTRINSIC_AND:
    1160           56 :     case INTRINSIC_OR:
    1161           56 :     case INTRINSIC_EQV:
    1162           56 :     case INTRINSIC_NEQV:
    1163           56 :       if (t1 == BT_LOGICAL && t2 == BT_LOGICAL)
    1164            0 :         goto bad_repl;
    1165              :       break;
    1166              : 
    1167              :     default:
    1168              :       break;
    1169              :   }
    1170              : 
    1171              :   return true;
    1172              : 
    1173              : #undef IS_NUMERIC_TYPE
    1174              : 
    1175            3 : bad_repl:
    1176            3 :   gfc_error ("Operator interface at %L conflicts with intrinsic interface",
    1177              :              &opwhere);
    1178            3 :   return false;
    1179              : }
    1180              : 
    1181              : 
    1182              : /* Given a pair of formal argument lists, we see if the two lists can
    1183              :    be distinguished by counting the number of nonoptional arguments of
    1184              :    a given type/rank in f1 and seeing if there are less then that
    1185              :    number of those arguments in f2 (including optional arguments).
    1186              :    Since this test is asymmetric, it has to be called twice to make it
    1187              :    symmetric. Returns nonzero if the argument lists are incompatible
    1188              :    by this test. This subroutine implements rule 1 of section F03:16.2.3.
    1189              :    'p1' and 'p2' are the PASS arguments of both procedures (if applicable).  */
    1190              : 
    1191              : static bool
    1192       897732 : count_types_test (gfc_formal_arglist *f1, gfc_formal_arglist *f2,
    1193              :                   const char *p1, const char *p2)
    1194              : {
    1195       897732 :   int ac1, ac2, i, j, k, n1;
    1196       897732 :   gfc_formal_arglist *f;
    1197              : 
    1198       897732 :   typedef struct
    1199              :   {
    1200              :     int flag;
    1201              :     gfc_symbol *sym;
    1202              :   }
    1203              :   arginfo;
    1204              : 
    1205       897732 :   arginfo *arg;
    1206              : 
    1207       897732 :   n1 = 0;
    1208              : 
    1209      2540442 :   for (f = f1; f; f = f->next)
    1210      1642710 :     n1++;
    1211              : 
    1212              :   /* Build an array of integers that gives the same integer to
    1213              :      arguments of the same type/rank.  */
    1214       897732 :   arg = XCNEWVEC (arginfo, n1);
    1215              : 
    1216       897732 :   f = f1;
    1217      3438174 :   for (i = 0; i < n1; i++, f = f->next)
    1218              :     {
    1219      1642710 :       arg[i].flag = -1;
    1220      1642710 :       arg[i].sym = f->sym;
    1221              :     }
    1222              : 
    1223              :   k = 0;
    1224              : 
    1225      2540442 :   for (i = 0; i < n1; i++)
    1226              :     {
    1227      1642710 :       if (arg[i].flag != -1)
    1228       276314 :         continue;
    1229              : 
    1230      1366396 :       if (arg[i].sym && (arg[i].sym->attr.optional
    1231      1366207 :                          || (p1 && strcmp (arg[i].sym->name, p1) == 0)))
    1232          505 :         continue;               /* Skip OPTIONAL and PASS arguments.  */
    1233              : 
    1234      1365891 :       arg[i].flag = k;
    1235              : 
    1236              :       /* Find other non-optional, non-pass arguments of the same type/rank.  */
    1237      2127742 :       for (j = i + 1; j < n1; j++)
    1238       761851 :         if ((arg[j].sym == NULL
    1239       761819 :              || !(arg[j].sym->attr.optional
    1240          188 :                   || (p1 && strcmp (arg[j].sym->name, p1) == 0)))
    1241      1523312 :             && (compare_type_rank_if (arg[i].sym, arg[j].sym)
    1242       575251 :                 || compare_type_rank_if (arg[j].sym, arg[i].sym)))
    1243       276314 :           arg[j].flag = k;
    1244              : 
    1245      1365891 :       k++;
    1246              :     }
    1247              : 
    1248              :   /* Now loop over each distinct type found in f1.  */
    1249              :   k = 0;
    1250      1218583 :   bool rc = false;
    1251              : 
    1252      1218583 :   for (i = 0; i < n1; i++)
    1253              :     {
    1254      1115103 :       if (arg[i].flag != k)
    1255        48262 :         continue;
    1256              : 
    1257      1066841 :       ac1 = 1;
    1258      1828431 :       for (j = i + 1; j < n1; j++)
    1259       761590 :         if (arg[j].flag == k)
    1260       276293 :           ac1++;
    1261              : 
    1262              :       /* Count the number of non-pass arguments in f2 with that type,
    1263              :          including those that are optional.  */
    1264              :       ac2 = 0;
    1265              : 
    1266      3022958 :       for (f = f2; f; f = f->next)
    1267          627 :         if ((!p2 || strcmp (f->sym->name, p2) != 0)
    1268      1956388 :             && (compare_type_rank_if (arg[i].sym, f->sym)
    1269      1589320 :                 || compare_type_rank_if (f->sym, arg[i].sym)))
    1270       441309 :           ac2++;
    1271              : 
    1272      1066841 :       if (ac1 > ac2)
    1273              :         {
    1274              :           rc = true;
    1275              :           break;
    1276              :         }
    1277              : 
    1278       272589 :       k++;
    1279              :     }
    1280              : 
    1281       897732 :   free (arg);
    1282              : 
    1283       897732 :   return rc;
    1284              : }
    1285              : 
    1286              : 
    1287              : /* Returns true if two dummy arguments are distinguishable due to their POINTER
    1288              :    and ALLOCATABLE attributes according to F2018 section 15.4.3.4.5 (3).
    1289              :    The function is asymmetric wrt to the arguments s1 and s2 and should always
    1290              :    be called twice (with flipped arguments in the second call).  */
    1291              : 
    1292              : static bool
    1293        31329 : compare_ptr_alloc(gfc_symbol *s1, gfc_symbol *s2)
    1294              : {
    1295              :   /* Is s1 allocatable?  */
    1296        31329 :   const bool a1 = s1->ts.type == BT_CLASS ?
    1297        31329 :                   CLASS_DATA(s1)->attr.allocatable : s1->attr.allocatable;
    1298              :   /* Is s2 a pointer?  */
    1299        31329 :   const bool p2 = s2->ts.type == BT_CLASS ?
    1300        31329 :                   CLASS_DATA(s2)->attr.class_pointer : s2->attr.pointer;
    1301        31329 :   return a1 && p2 && (s2->attr.intent != INTENT_IN);
    1302              : }
    1303              : 
    1304              : 
    1305              : /* Perform the correspondence test in rule (3) of F08:C1215.
    1306              :    Returns zero if no argument is found that satisfies this rule,
    1307              :    nonzero otherwise. 'p1' and 'p2' are the PASS arguments of both procedures
    1308              :    (if applicable).
    1309              : 
    1310              :    This test is also not symmetric in f1 and f2 and must be called
    1311              :    twice.  This test finds problems caused by sorting the actual
    1312              :    argument list with keywords.  For example:
    1313              : 
    1314              :    INTERFACE FOO
    1315              :      SUBROUTINE F1(A, B)
    1316              :        INTEGER :: A ; REAL :: B
    1317              :      END SUBROUTINE F1
    1318              : 
    1319              :      SUBROUTINE F2(B, A)
    1320              :        INTEGER :: A ; REAL :: B
    1321              :      END SUBROUTINE F1
    1322              :    END INTERFACE FOO
    1323              : 
    1324              :    At this point, 'CALL FOO(A=1, B=1.0)' is ambiguous.  */
    1325              : 
    1326              : static bool
    1327        34378 : generic_correspondence (gfc_formal_arglist *f1, gfc_formal_arglist *f2,
    1328              :                         const char *p1, const char *p2)
    1329              : {
    1330        34378 :   gfc_formal_arglist *f2_save, *g;
    1331        34378 :   gfc_symbol *sym;
    1332              : 
    1333        34378 :   f2_save = f2;
    1334              : 
    1335        49704 :   while (f1)
    1336              :     {
    1337        49654 :       if (!f1->sym || f1->sym->attr.optional)
    1338            4 :         goto next;
    1339              : 
    1340        49650 :       if (p1 && strcmp (f1->sym->name, p1) == 0)
    1341            7 :         f1 = f1->next;
    1342        49650 :       if (f2 && p2 && strcmp (f2->sym->name, p2) == 0)
    1343            5 :         f2 = f2->next;
    1344              : 
    1345        49646 :       if (f2 != NULL && (compare_type_rank (f1->sym, f2->sym)
    1346        34319 :                          || compare_type_rank (f2->sym, f1->sym))
    1347        64980 :           && !((gfc_option.allow_std & GFC_STD_F2008)
    1348        15330 :                && (compare_ptr_alloc(f1->sym, f2->sym)
    1349        15323 :                    || compare_ptr_alloc(f2->sym, f1->sym))))
    1350        15318 :         goto next;
    1351              : 
    1352              :       /* Now search for a disambiguating keyword argument starting at
    1353              :          the current non-match.  */
    1354        34332 :       for (g = f1; g; g = g->next)
    1355              :         {
    1356        34328 :           if (g->sym->attr.optional || (p1 && strcmp (g->sym->name, p1) == 0))
    1357            0 :             continue;
    1358              : 
    1359        34328 :           sym = find_keyword_arg (g->sym->name, f2_save);
    1360        34328 :           if (sym == NULL || !compare_type_rank (g->sym, sym)
    1361        34342 :               || ((gfc_option.allow_std & GFC_STD_F2008)
    1362           14 :                   && (compare_ptr_alloc(sym, g->sym)
    1363            7 :                       || compare_ptr_alloc(g->sym, sym))))
    1364        34328 :             return true;
    1365              :         }
    1366              : 
    1367        15326 :     next:
    1368        15326 :       if (f1 != NULL)
    1369        15322 :         f1 = f1->next;
    1370        15326 :       if (f2 != NULL)
    1371        15322 :         f2 = f2->next;
    1372              :     }
    1373              : 
    1374              :   return false;
    1375              : }
    1376              : 
    1377              : 
    1378              : int
    1379       569729 : gfc_symbol_rank (gfc_symbol *sym)
    1380              : {
    1381       569729 :   gfc_array_spec *as = NULL;
    1382              : 
    1383       569729 :   if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
    1384        17115 :     as = CLASS_DATA (sym)->as;
    1385              :   else
    1386       552614 :     as = sym->as;
    1387              : 
    1388       569729 :   return as ? as->rank : 0;
    1389              : }
    1390              : 
    1391              : 
    1392              : /* Check if the characteristics of two dummy arguments match,
    1393              :    cf. F08:12.3.2.  */
    1394              : 
    1395              : bool
    1396       126473 : gfc_check_dummy_characteristics (gfc_symbol *s1, gfc_symbol *s2,
    1397              :                                  bool type_must_agree, char *errmsg,
    1398              :                                  int err_len)
    1399              : {
    1400       126473 :   if (s1 == NULL || s2 == NULL)
    1401           27 :     return s1 == s2;
    1402              : 
    1403       126446 :   if (s1->attr.proc == PROC_ST_FUNCTION || s2->attr.proc == PROC_ST_FUNCTION)
    1404              :     {
    1405            1 :       strncpy (errmsg, "Statement function", err_len);
    1406            1 :       return false;
    1407              :     }
    1408              : 
    1409              :   /* Check type and rank.  */
    1410       126445 :   if (type_must_agree)
    1411              :     {
    1412       125276 :       if (!compare_type_characteristics (s1, s2)
    1413       125276 :           || !compare_type_characteristics (s2, s1))
    1414              :         {
    1415           24 :           snprintf (errmsg, err_len, "Type mismatch in argument '%s' (%s/%s)",
    1416              :                     s1->name, gfc_dummy_typename (&s1->ts),
    1417              :                     gfc_dummy_typename (&s2->ts));
    1418           24 :           return false;
    1419              :         }
    1420       125252 :       if (!compare_rank (s1, s2))
    1421              :         {
    1422            5 :           snprintf (errmsg, err_len, "Rank mismatch in argument '%s' (%i/%i)",
    1423              :                     s1->name, gfc_symbol_rank (s1), gfc_symbol_rank (s2));
    1424            5 :           return false;
    1425              :         }
    1426              :     }
    1427              : 
    1428              :   /* A lot of information is missing for artificially generated
    1429              :      formal arguments, let's not look into that.  */
    1430              : 
    1431       126416 :   if (!s1->attr.artificial && !s2->attr.artificial)
    1432              :     {
    1433              :       /* Check INTENT.  */
    1434       100498 :       if (s1->attr.intent != s2->attr.intent)
    1435              :         {
    1436            5 :           snprintf (errmsg, err_len, "INTENT mismatch in argument '%s'",
    1437              :                     s1->name);
    1438            5 :           return false;
    1439              :         }
    1440              : 
    1441              :       /* Check OPTIONAL attribute.  */
    1442       100493 :       if (s1->attr.optional != s2->attr.optional)
    1443              :         {
    1444            1 :           snprintf (errmsg, err_len, "OPTIONAL mismatch in argument '%s'",
    1445              :                     s1->name);
    1446            1 :           return false;
    1447              :         }
    1448              : 
    1449              :       /* Check ALLOCATABLE attribute.  */
    1450       100492 :       if (s1->attr.allocatable != s2->attr.allocatable)
    1451              :         {
    1452            0 :           snprintf (errmsg, err_len, "ALLOCATABLE mismatch in argument '%s'",
    1453              :                     s1->name);
    1454            0 :           return false;
    1455              :         }
    1456              : 
    1457              :       /* Check POINTER attribute.  */
    1458       100492 :       if (s1->attr.pointer != s2->attr.pointer)
    1459              :         {
    1460            0 :           snprintf (errmsg, err_len, "POINTER mismatch in argument '%s'",
    1461              :                     s1->name);
    1462            0 :           return false;
    1463              :         }
    1464              : 
    1465              :       /* Check TARGET attribute.  */
    1466       100492 :       if (s1->attr.target != s2->attr.target)
    1467              :         {
    1468            0 :           snprintf (errmsg, err_len, "TARGET mismatch in argument '%s'",
    1469              :                     s1->name);
    1470            0 :           return false;
    1471              :         }
    1472              : 
    1473              :       /* Check ASYNCHRONOUS attribute.  */
    1474       100492 :       if (s1->attr.asynchronous != s2->attr.asynchronous)
    1475              :         {
    1476            1 :           snprintf (errmsg, err_len, "ASYNCHRONOUS mismatch in argument '%s'",
    1477              :                     s1->name);
    1478            1 :           return false;
    1479              :         }
    1480              : 
    1481              :       /* Check CONTIGUOUS attribute.  */
    1482       100491 :       if (s1->attr.contiguous != s2->attr.contiguous)
    1483              :         {
    1484            1 :           snprintf (errmsg, err_len, "CONTIGUOUS mismatch in argument '%s'",
    1485              :                     s1->name);
    1486            1 :           return false;
    1487              :         }
    1488              : 
    1489              :       /* Check VALUE attribute.  */
    1490       100490 :       if (s1->attr.value != s2->attr.value)
    1491              :         {
    1492            1 :           snprintf (errmsg, err_len, "VALUE mismatch in argument '%s'",
    1493              :                     s1->name);
    1494            1 :           return false;
    1495              :         }
    1496              : 
    1497              :       /* Check VOLATILE attribute.  */
    1498       100489 :       if (s1->attr.volatile_ != s2->attr.volatile_)
    1499              :         {
    1500            1 :           snprintf (errmsg, err_len, "VOLATILE mismatch in argument '%s'",
    1501              :                     s1->name);
    1502            1 :           return false;
    1503              :         }
    1504              :     }
    1505              : 
    1506              :   /* Check interface of dummy procedures.  */
    1507       126406 :   if (s1->attr.flavor == FL_PROCEDURE)
    1508              :     {
    1509          129 :       char err[200];
    1510          129 :       if (!gfc_compare_interfaces (s1, s2, s2->name, 0, 1, err, sizeof(err),
    1511              :                                    NULL, NULL))
    1512              :         {
    1513            1 :           snprintf (errmsg, err_len, "Interface mismatch in dummy procedure "
    1514              :                     "'%s': %s", s1->name, err);
    1515            1 :           return false;
    1516              :         }
    1517              :     }
    1518              : 
    1519              :   /* Check string length.  */
    1520       126405 :   if (s1->ts.type == BT_CHARACTER
    1521         2789 :       && s1->ts.u.cl && s1->ts.u.cl->length
    1522          885 :       && s2->ts.u.cl && s2->ts.u.cl->length)
    1523              :     {
    1524          885 :       int compval = gfc_dep_compare_expr (s1->ts.u.cl->length,
    1525              :                                           s2->ts.u.cl->length);
    1526          885 :       switch (compval)
    1527              :       {
    1528            0 :         case -1:
    1529            0 :         case  1:
    1530            0 :         case -3:
    1531            0 :           snprintf (errmsg, err_len, "Character length mismatch "
    1532              :                     "in argument '%s'", s1->name);
    1533            0 :           return false;
    1534              : 
    1535              :         case -2:
    1536              :           /* FIXME: Implement a warning for this case.
    1537              :           gfc_warning (0, "Possible character length mismatch in argument %qs",
    1538              :                        s1->name);*/
    1539              :           break;
    1540              : 
    1541              :         case 0:
    1542              :           break;
    1543              : 
    1544            0 :         default:
    1545            0 :           gfc_internal_error ("check_dummy_characteristics: Unexpected result "
    1546              :                               "%i of gfc_dep_compare_expr", compval);
    1547              :           break;
    1548              :       }
    1549              :     }
    1550              : 
    1551              :   /* Check array shape.  */
    1552       126405 :   if (s1->as && s2->as)
    1553              :     {
    1554        22154 :       int i, compval;
    1555        22154 :       gfc_expr *shape1, *shape2;
    1556              : 
    1557        22154 :       if (s1->as->rank != s2->as->rank)
    1558              :         {
    1559            2 :           snprintf (errmsg, err_len, "Rank mismatch in argument '%s' (%i/%i)",
    1560              :                     s1->name, s1->as->rank, s2->as->rank);
    1561            2 :           return false;
    1562              :         }
    1563              : 
    1564              :       /* Sometimes the ambiguity between deferred shape and assumed shape
    1565              :          does not get resolved in module procedures, where the only explicit
    1566              :          declaration of the dummy is in the interface.  */
    1567        22152 :       if (s1->ns->proc_name && s1->ns->proc_name->attr.module_procedure
    1568          114 :           && s1->as->type == AS_ASSUMED_SHAPE
    1569           67 :           && s2->as->type == AS_DEFERRED)
    1570              :         {
    1571            7 :           s2->as->type = AS_ASSUMED_SHAPE;
    1572           14 :           for (i = 0; i < s2->as->rank; i++)
    1573            7 :             if (s1->as->lower[i] != NULL)
    1574            7 :               s2->as->lower[i] = gfc_copy_expr (s1->as->lower[i]);
    1575              :         }
    1576              : 
    1577        22152 :       if (s1->as->type != s2->as->type
    1578            4 :           && !(s1->as->type == AS_DEFERRED
    1579              :                && s2->as->type == AS_ASSUMED_SHAPE))
    1580              :         {
    1581            2 :           snprintf (errmsg, err_len, "Shape mismatch in argument '%s'",
    1582              :                     s1->name);
    1583            2 :           return false;
    1584              :         }
    1585              : 
    1586        22150 :       if (s1->as->corank != s2->as->corank)
    1587              :         {
    1588            1 :           snprintf (errmsg, err_len, "Corank mismatch in argument '%s' (%i/%i)",
    1589              :                     s1->name, s1->as->corank, s2->as->corank);
    1590            1 :           return false;
    1591              :         }
    1592              : 
    1593        22149 :       if (s1->as->type == AS_EXPLICIT)
    1594         3891 :         for (i = 0; i < s1->as->rank + MAX (0, s1->as->corank-1); i++)
    1595              :           {
    1596         2096 :             shape1 = gfc_subtract (gfc_copy_expr (s1->as->upper[i]),
    1597         2096 :                                   gfc_copy_expr (s1->as->lower[i]));
    1598         2096 :             shape2 = gfc_subtract (gfc_copy_expr (s2->as->upper[i]),
    1599         2096 :                                   gfc_copy_expr (s2->as->lower[i]));
    1600         2096 :             compval = gfc_dep_compare_expr (shape1, shape2);
    1601         2096 :             gfc_free_expr (shape1);
    1602         2096 :             gfc_free_expr (shape2);
    1603         2096 :             switch (compval)
    1604              :             {
    1605            2 :               case -1:
    1606            2 :               case  1:
    1607            2 :               case -3:
    1608            2 :                 if (i < s1->as->rank)
    1609            2 :                   snprintf (errmsg, err_len, "Shape mismatch in dimension %i of"
    1610              :                             " argument '%s'", i + 1, s1->name);
    1611              :                 else
    1612            0 :                   snprintf (errmsg, err_len, "Shape mismatch in codimension %i "
    1613            0 :                             "of argument '%s'", i - s1->as->rank + 1, s1->name);
    1614            2 :                 return false;
    1615              : 
    1616              :               case -2:
    1617              :                 /* FIXME: Implement a warning for this case.
    1618              :                 gfc_warning (0, "Possible shape mismatch in argument %qs",
    1619              :                             s1->name);*/
    1620              :                 break;
    1621              : 
    1622              :               case 0:
    1623              :                 break;
    1624              : 
    1625            0 :               default:
    1626            0 :                 gfc_internal_error ("check_dummy_characteristics: Unexpected "
    1627              :                                     "result %i of gfc_dep_compare_expr",
    1628              :                                     compval);
    1629         2094 :                 break;
    1630              :             }
    1631              :           }
    1632              :     }
    1633              : 
    1634              :   return true;
    1635              : }
    1636              : 
    1637              : 
    1638              : /* Check if the characteristics of two function results match,
    1639              :    cf. F08:12.3.3.  */
    1640              : 
    1641              : bool
    1642        59349 : gfc_check_result_characteristics (gfc_symbol *s1, gfc_symbol *s2,
    1643              :                                   char *errmsg, int err_len)
    1644              : {
    1645        59349 :   gfc_symbol *r1, *r2;
    1646              : 
    1647        59349 :   if (s1->ts.interface && s1->ts.interface->result)
    1648              :     r1 = s1->ts.interface->result;
    1649              :   else
    1650        58878 :     r1 = s1->result ? s1->result : s1;
    1651              : 
    1652        59349 :   if (s2->ts.interface && s2->ts.interface->result)
    1653              :     r2 = s2->ts.interface->result;
    1654              :   else
    1655        58880 :     r2 = s2->result ? s2->result : s2;
    1656              : 
    1657        59349 :   if (r1->ts.type == BT_UNKNOWN)
    1658              :     return true;
    1659              : 
    1660              :   /* Check type and rank.  */
    1661        59081 :   if (!compare_type_characteristics (r1, r2))
    1662              :     {
    1663           21 :       snprintf (errmsg, err_len, "Type mismatch in function result (%s/%s)",
    1664              :                 gfc_typename (&r1->ts), gfc_typename (&r2->ts));
    1665           21 :       return false;
    1666              :     }
    1667        59060 :   if (!compare_rank (r1, r2))
    1668              :     {
    1669            5 :       snprintf (errmsg, err_len, "Rank mismatch in function result (%i/%i)",
    1670              :                 gfc_symbol_rank (r1), gfc_symbol_rank (r2));
    1671            5 :       return false;
    1672              :     }
    1673              : 
    1674              :   /* Check ALLOCATABLE attribute.  */
    1675        59055 :   if (r1->attr.allocatable != r2->attr.allocatable)
    1676              :     {
    1677            2 :       snprintf (errmsg, err_len, "ALLOCATABLE attribute mismatch in "
    1678              :                 "function result");
    1679            2 :       return false;
    1680              :     }
    1681              : 
    1682              :   /* Check POINTER attribute.  */
    1683        59053 :   if (r1->attr.pointer != r2->attr.pointer)
    1684              :     {
    1685            2 :       snprintf (errmsg, err_len, "POINTER attribute mismatch in "
    1686              :                 "function result");
    1687            2 :       return false;
    1688              :     }
    1689              : 
    1690              :   /* Check CONTIGUOUS attribute.  */
    1691        59051 :   if (r1->attr.contiguous != r2->attr.contiguous)
    1692              :     {
    1693            1 :       snprintf (errmsg, err_len, "CONTIGUOUS attribute mismatch in "
    1694              :                 "function result");
    1695            1 :       return false;
    1696              :     }
    1697              : 
    1698              :   /* Check PROCEDURE POINTER attribute.  */
    1699        59050 :   if (r1 != s1 && r1->attr.proc_pointer != r2->attr.proc_pointer)
    1700              :     {
    1701            3 :       snprintf (errmsg, err_len, "PROCEDURE POINTER mismatch in "
    1702              :                 "function result");
    1703            3 :       return false;
    1704              :     }
    1705              : 
    1706              :   /* Check string length.  */
    1707        59047 :   if (r1->ts.type == BT_CHARACTER && r1->ts.u.cl && r2->ts.u.cl)
    1708              :     {
    1709         2211 :       if (r1->ts.deferred != r2->ts.deferred)
    1710              :         {
    1711            0 :           snprintf (errmsg, err_len, "Character length mismatch "
    1712              :                     "in function result");
    1713            0 :           return false;
    1714              :         }
    1715              : 
    1716         2211 :       if (r1->ts.u.cl->length && r2->ts.u.cl->length)
    1717              :         {
    1718         1647 :           int compval = gfc_dep_compare_expr (r1->ts.u.cl->length,
    1719              :                                               r2->ts.u.cl->length);
    1720         1647 :           switch (compval)
    1721              :           {
    1722            3 :             case -1:
    1723            3 :             case  1:
    1724            3 :             case -3:
    1725            3 :               snprintf (errmsg, err_len, "Character length mismatch "
    1726              :                         "in function result");
    1727            3 :               return false;
    1728              : 
    1729           75 :             case -2:
    1730           75 :               if (r1->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    1731              :                 {
    1732            0 :                   snprintf (errmsg, err_len,
    1733              :                             "Function declared with a non-constant character "
    1734              :                             "length referenced with a constant length");
    1735            0 :                   return false;
    1736              :                 }
    1737           75 :               else if (r2->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    1738              :                 {
    1739            3 :                   snprintf (errmsg, err_len,
    1740              :                             "Function declared with a constant character "
    1741              :                             "length referenced with a non-constant length");
    1742            3 :                   return false;
    1743              :                 }
    1744              :               /* Warn if length expression types are different, except for
    1745              :                   possibly false positives where complex expressions might have
    1746              :                   been used.  */
    1747           72 :               else if ((r1->ts.u.cl->length->expr_type
    1748              :                         != r2->ts.u.cl->length->expr_type)
    1749            4 :                        && (r1->ts.u.cl->length->expr_type != EXPR_OP
    1750            2 :                            || r2->ts.u.cl->length->expr_type != EXPR_OP))
    1751            4 :                 gfc_warning (0, "Possible character length mismatch in "
    1752              :                              "function result between %L and %L",
    1753              :                              &r1->declared_at, &r2->declared_at);
    1754              :               break;
    1755              : 
    1756              :             case 0:
    1757              :               break;
    1758              : 
    1759            0 :             default:
    1760            0 :               gfc_internal_error ("check_result_characteristics (1): Unexpected "
    1761              :                                   "result %i of gfc_dep_compare_expr", compval);
    1762              :               break;
    1763              :           }
    1764              :         }
    1765              :     }
    1766              : 
    1767              :   /* Check array shape.  */
    1768        59041 :   if (!r1->attr.allocatable && !r1->attr.pointer && r1->as && r2->as)
    1769              :     {
    1770          989 :       int i, compval;
    1771          989 :       gfc_expr *shape1, *shape2;
    1772              : 
    1773          989 :       if (r1->as->type != r2->as->type)
    1774              :         {
    1775            0 :           snprintf (errmsg, err_len, "Shape mismatch in function result");
    1776            0 :           return false;
    1777              :         }
    1778              : 
    1779          989 :       if (r1->as->type == AS_EXPLICIT)
    1780         2493 :         for (i = 0; i < r1->as->rank + r1->as->corank; i++)
    1781              :           {
    1782         1505 :             shape1 = gfc_subtract (gfc_copy_expr (r1->as->upper[i]),
    1783         1505 :                                    gfc_copy_expr (r1->as->lower[i]));
    1784         1505 :             shape2 = gfc_subtract (gfc_copy_expr (r2->as->upper[i]),
    1785         1505 :                                    gfc_copy_expr (r2->as->lower[i]));
    1786         1505 :             compval = gfc_dep_compare_expr (shape1, shape2);
    1787         1505 :             gfc_free_expr (shape1);
    1788         1505 :             gfc_free_expr (shape2);
    1789         1505 :             switch (compval)
    1790              :             {
    1791            1 :               case -1:
    1792            1 :               case  1:
    1793            1 :               case -3:
    1794            1 :                 snprintf (errmsg, err_len, "Shape mismatch in dimension %i of "
    1795              :                           "function result", i + 1);
    1796            1 :                 return false;
    1797              : 
    1798              :               case -2:
    1799              :                 /* FIXME: Implement a warning for this case.
    1800              :                 gfc_warning (0, "Possible shape mismatch in return value");*/
    1801              :                 break;
    1802              : 
    1803              :               case 0:
    1804              :                 break;
    1805              : 
    1806            0 :               default:
    1807            0 :                 gfc_internal_error ("check_result_characteristics (2): "
    1808              :                                     "Unexpected result %i of "
    1809              :                                     "gfc_dep_compare_expr", compval);
    1810         1504 :                 break;
    1811              :             }
    1812              :           }
    1813              :     }
    1814              : 
    1815              :   return true;
    1816              : }
    1817              : 
    1818              : 
    1819              : /* 'Compare' two formal interfaces associated with a pair of symbols.
    1820              :    We return true if there exists an actual argument list that
    1821              :    would be ambiguous between the two interfaces, zero otherwise.
    1822              :    'strict_flag' specifies whether all the characteristics are
    1823              :    required to match, which is not the case for ambiguity checks.
    1824              :    'p1' and 'p2' are the PASS arguments of both procedures (if applicable).  */
    1825              : 
    1826              : bool
    1827       895074 : gfc_compare_interfaces (gfc_symbol *s1, gfc_symbol *s2, const char *name2,
    1828              :                         int generic_flag, int strict_flag,
    1829              :                         char *errmsg, int err_len,
    1830              :                         const char *p1, const char *p2,
    1831              :                         bool *bad_result_characteristics)
    1832              : {
    1833       895074 :   gfc_formal_arglist *f1, *f2;
    1834              : 
    1835       895074 :   gcc_assert (name2 != NULL);
    1836              : 
    1837       895074 :   if (bad_result_characteristics)
    1838        14947 :     *bad_result_characteristics = false;
    1839              : 
    1840       895074 :   if (s1->attr.function && (s2->attr.subroutine
    1841       797178 :       || (!s2->attr.function && s2->ts.type == BT_UNKNOWN
    1842            5 :           && gfc_get_default_type (name2, s2->ns)->type == BT_UNKNOWN)))
    1843              :     {
    1844            3 :       if (errmsg != NULL)
    1845            3 :         snprintf (errmsg, err_len, "'%s' is not a function", name2);
    1846            3 :       return false;
    1847              :     }
    1848              : 
    1849       895071 :   if (s1->attr.subroutine && s2->attr.function)
    1850              :     {
    1851            6 :       if (errmsg != NULL)
    1852            6 :         snprintf (errmsg, err_len, "'%s' is not a subroutine", name2);
    1853            6 :       return false;
    1854              :     }
    1855              : 
    1856       895065 :   if (s2->attr.subroutine && s1->attr.flavor == FL_VARIABLE)
    1857              :     {
    1858            2 :       if (errmsg != NULL)
    1859            2 :         snprintf (errmsg, err_len, "subroutine proc pointer '%s' passed "
    1860              :                   "to dummy variable '%s'", name2, s1->name);
    1861            2 :       return false;
    1862              :     }
    1863              : 
    1864              :   /* Do strict checks on all characteristics
    1865              :      (for dummy procedures and procedure pointer assignments).  */
    1866       895063 :   if (!generic_flag && strict_flag)
    1867              :     {
    1868        63460 :       if (s1->attr.function && s2->attr.function)
    1869              :         {
    1870              :           /* If both are functions, check result characteristics.  */
    1871        29130 :           if (!gfc_check_result_characteristics (s1, s2, errmsg, err_len)
    1872        29130 :               || !gfc_check_result_characteristics (s2, s1, errmsg, err_len))
    1873              :             {
    1874           30 :               if (bad_result_characteristics)
    1875            6 :                 *bad_result_characteristics = true;
    1876           30 :               return false;
    1877              :             }
    1878              :         }
    1879              : 
    1880        63430 :       if (s1->attr.pure && !s2->attr.pure)
    1881              :         {
    1882            2 :           snprintf (errmsg, err_len, "Mismatch in PURE attribute");
    1883            2 :           return false;
    1884              :         }
    1885        63428 :       if (s1->attr.elemental && !s2->attr.elemental)
    1886              :         {
    1887            0 :           snprintf (errmsg, err_len, "Mismatch in ELEMENTAL attribute");
    1888            0 :           return false;
    1889              :         }
    1890              :     }
    1891              : 
    1892       895031 :   if (s1->attr.if_source == IFSRC_UNKNOWN
    1893       879370 :       || s2->attr.if_source == IFSRC_UNKNOWN)
    1894              :     return true;
    1895              : 
    1896       879294 :   f1 = gfc_sym_get_dummy_args (s1);
    1897       879294 :   f2 = gfc_sym_get_dummy_args (s2);
    1898              : 
    1899              :   /* Special case: No arguments.  */
    1900       879294 :   if (f1 == NULL && f2 == NULL)
    1901              :     return true;
    1902              : 
    1903       877203 :   if (generic_flag)
    1904              :     {
    1905       828608 :       if (count_types_test (f1, f2, p1, p2)
    1906       828608 :           || count_types_test (f2, f1, p2, p1))
    1907       794252 :         return false;
    1908              : 
    1909              :       /* Special case: alternate returns.  If both f1->sym and f2->sym are
    1910              :          NULL, then the leading formal arguments are alternate returns.
    1911              :          The previous conditional should catch argument lists with
    1912              :          different number of argument.  */
    1913        34356 :       if (f1 && f1->sym == NULL && f2 && f2->sym == NULL)
    1914              :         return true;
    1915              : 
    1916        34353 :       if (generic_correspondence (f1, f2, p1, p2)
    1917        34353 :           || generic_correspondence (f2, f1, p2, p1))
    1918        34328 :         return false;
    1919              :     }
    1920              :   else
    1921              :     /* Perform the abbreviated correspondence test for operators (the
    1922              :        arguments cannot be optional and are always ordered correctly).
    1923              :        This is also done when comparing interfaces for dummy procedures and in
    1924              :        procedure pointer assignments.  */
    1925              : 
    1926       172889 :     for (; f1 || f2; f1 = f1->next, f2 = f2->next)
    1927              :       {
    1928              :         /* Check existence.  */
    1929       127325 :         if (f1 == NULL || f2 == NULL)
    1930              :           {
    1931           10 :             if (errmsg != NULL)
    1932            6 :               snprintf (errmsg, err_len, "'%s' has the wrong number of "
    1933              :                         "arguments", name2);
    1934           10 :             return false;
    1935              :           }
    1936              : 
    1937       127315 :         if (strict_flag)
    1938              :           {
    1939              :             /* Check all characteristics.  */
    1940       124008 :             if (!gfc_check_dummy_characteristics (f1->sym, f2->sym, true,
    1941              :                                               errmsg, err_len))
    1942              :               return false;
    1943              :           }
    1944              :         else
    1945              :           {
    1946              :             /* Operators: Only check type and rank of arguments.  */
    1947         3307 :             if (!compare_type (f2->sym, f1->sym))
    1948              :               {
    1949         2975 :                 if (errmsg != NULL)
    1950            0 :                   snprintf (errmsg, err_len, "Type mismatch in argument '%s' "
    1951            0 :                             "(%s/%s)", f1->sym->name,
    1952            0 :                             gfc_typename (&f1->sym->ts),
    1953            0 :                             gfc_typename (&f2->sym->ts));
    1954         2975 :                 return false;
    1955              :               }
    1956          332 :             if (!compare_rank (f2->sym, f1->sym))
    1957              :               {
    1958            4 :                 if (errmsg != NULL)
    1959            0 :                   snprintf (errmsg, err_len, "Rank mismatch in argument "
    1960            0 :                             "'%s' (%i/%i)", f1->sym->name,
    1961            0 :                             gfc_symbol_rank (f1->sym), gfc_symbol_rank (f2->sym));
    1962            4 :                 return false;
    1963              :               }
    1964          328 :             if ((gfc_option.allow_std & GFC_STD_F2008)
    1965          328 :                 && (compare_ptr_alloc(f1->sym, f2->sym)
    1966          327 :                     || compare_ptr_alloc(f2->sym, f1->sym)))
    1967              :               {
    1968            2 :                 if (errmsg != NULL)
    1969            0 :                   snprintf (errmsg, err_len, "Mismatching POINTER/ALLOCATABLE "
    1970              :                             "attribute in argument '%s' ", f1->sym->name);
    1971            2 :                 return false;
    1972              :               }
    1973              :           }
    1974              :       }
    1975              : 
    1976              :   return true;
    1977              : }
    1978              : 
    1979              : 
    1980              : /* Given a pointer to an interface pointer, remove duplicate
    1981              :    interfaces and make sure that all symbols are either functions
    1982              :    or subroutines, and all of the same kind.  Returns true if
    1983              :    something goes wrong.  */
    1984              : 
    1985              : static bool
    1986      9817211 : check_interface0 (gfc_interface *p, const char *interface_name)
    1987              : {
    1988      9817211 :   gfc_interface *psave, *q, *qlast;
    1989              : 
    1990      9817211 :   psave = p;
    1991     10022154 :   for (; p; p = p->next)
    1992              :     {
    1993              :       /* Make sure all symbols in the interface have been defined as
    1994              :          functions or subroutines.  */
    1995       204959 :       if (((!p->sym->attr.function && !p->sym->attr.subroutine)
    1996       168216 :            || !p->sym->attr.if_source)
    1997        36746 :           && !gfc_fl_struct (p->sym->attr.flavor))
    1998              :         {
    1999           12 :           const char *guessed
    2000           12 :             = gfc_lookup_function_fuzzy (p->sym->name, p->sym->ns->sym_root);
    2001              : 
    2002           12 :           if (p->sym->attr.external)
    2003            5 :             if (guessed)
    2004            5 :               gfc_error ("Procedure %qs in %s at %L has no explicit interface"
    2005              :                          "; did you mean %qs?",
    2006              :                          p->sym->name, interface_name, &p->sym->declared_at,
    2007              :                          guessed);
    2008              :             else
    2009            0 :               gfc_error ("Procedure %qs in %s at %L has no explicit interface",
    2010              :                          p->sym->name, interface_name, &p->sym->declared_at);
    2011              :           else
    2012            7 :             if (guessed)
    2013            4 :               gfc_error ("Procedure %qs in %s at %L is neither function nor "
    2014              :                          "subroutine; did you mean %qs?", p->sym->name,
    2015              :                         interface_name, &p->sym->declared_at, guessed);
    2016              :             else
    2017            3 :               gfc_error ("Procedure %qs in %s at %L is neither function nor "
    2018              :                          "subroutine", p->sym->name, interface_name,
    2019              :                         &p->sym->declared_at);
    2020           12 :           return true;
    2021              :         }
    2022              : 
    2023              :       /* Verify that procedures are either all SUBROUTINEs or all FUNCTIONs.  */
    2024       204947 :       if ((psave->sym->attr.function && !p->sym->attr.function
    2025          282 :            && !gfc_fl_struct (p->sym->attr.flavor))
    2026       204945 :           || (psave->sym->attr.subroutine && !p->sym->attr.subroutine))
    2027              :         {
    2028            3 :           if (!gfc_fl_struct (p->sym->attr.flavor))
    2029            3 :             gfc_error ("In %s at %L procedures must be either all SUBROUTINEs"
    2030              :                        " or all FUNCTIONs", interface_name,
    2031              :                        &p->sym->declared_at);
    2032            0 :           else if (p->sym->attr.flavor == FL_DERIVED)
    2033            0 :             gfc_error ("In %s at %L procedures must be all FUNCTIONs as the "
    2034              :                        "generic name is also the name of a derived type",
    2035              :                        interface_name, &p->sym->declared_at);
    2036            3 :           return true;
    2037              :         }
    2038              : 
    2039              :       /* F2003, C1207. F2008, C1207.  */
    2040       204944 :       if (p->sym->attr.proc == PROC_INTERNAL
    2041       204944 :           && !gfc_notify_std (GFC_STD_F2008, "Internal procedure "
    2042              :                               "%qs in %s at %L", p->sym->name,
    2043              :                               interface_name, &p->sym->declared_at))
    2044              :         return true;
    2045              :     }
    2046              :   p = psave;
    2047              : 
    2048              :   /* Remove duplicate interfaces in this interface list.  */
    2049     10017090 :   for (; p; p = p->next)
    2050              :     {
    2051       199895 :       qlast = p;
    2052              : 
    2053       632205 :       for (q = p->next; q;)
    2054              :         {
    2055       432310 :           if (p->sym != q->sym)
    2056              :             {
    2057       427266 :               qlast = q;
    2058       427266 :               q = q->next;
    2059              :             }
    2060              :           else
    2061              :             {
    2062              :               /* Duplicate interface.  */
    2063         5044 :               qlast->next = q->next;
    2064         5044 :               free (q);
    2065         5044 :               q = qlast->next;
    2066              :             }
    2067              :         }
    2068              :     }
    2069              : 
    2070              :   return false;
    2071              : }
    2072              : 
    2073              : 
    2074              : /* Check lists of interfaces to make sure that no two interfaces are
    2075              :    ambiguous.  Duplicate interfaces (from the same symbol) are OK here.  */
    2076              : 
    2077              : static bool
    2078     17708926 : check_interface1 (gfc_interface *p, gfc_interface *q0,
    2079              :                   int generic_flag, const char *interface_name,
    2080              :                   bool referenced)
    2081              : {
    2082     17708926 :   gfc_interface *q;
    2083     17912024 :   for (; p; p = p->next)
    2084      1234578 :     for (q = q0; q; q = q->next)
    2085              :       {
    2086      1031480 :         if (p->sym == q->sym)
    2087       199857 :           continue;             /* Duplicates OK here.  */
    2088              : 
    2089       831623 :         if (p->sym->name == q->sym->name && p->sym->module == q->sym->module)
    2090          128 :           continue;
    2091              : 
    2092       831495 :         if (!gfc_fl_struct (p->sym->attr.flavor)
    2093       831173 :             && !gfc_fl_struct (q->sym->attr.flavor)
    2094       830855 :             && gfc_compare_interfaces (p->sym, q->sym, q->sym->name,
    2095              :                                        generic_flag, 0, NULL, 0, NULL, NULL))
    2096              :           {
    2097           30 :             if (referenced)
    2098           27 :               gfc_error ("Ambiguous interfaces in %s for %qs at %L "
    2099              :                          "and %qs at %L", interface_name,
    2100           27 :                          q->sym->name, &q->sym->declared_at,
    2101           27 :                          p->sym->name, &p->sym->declared_at);
    2102            3 :             else if (!p->sym->attr.use_assoc && q->sym->attr.use_assoc)
    2103            1 :               gfc_warning (0, "Ambiguous interfaces in %s for %qs at %L "
    2104              :                          "and %qs at %L", interface_name,
    2105              :                          q->sym->name, &q->sym->declared_at,
    2106              :                          p->sym->name, &p->sym->declared_at);
    2107              :             else
    2108            2 :               gfc_warning (0, "Although not referenced, %qs has ambiguous "
    2109              :                            "interfaces at %L", interface_name, &p->where);
    2110           30 :             return true;
    2111              :           }
    2112              :       }
    2113              :   return false;
    2114              : }
    2115              : 
    2116              : 
    2117              : /* Check the generic and operator interfaces of symbols to make sure
    2118              :    that none of the interfaces conflict.  The check has to be done
    2119              :    after all of the symbols are actually loaded.  */
    2120              : 
    2121              : static void
    2122      1939251 : check_sym_interfaces (gfc_symbol *sym)
    2123              : {
    2124              :   /* Provide sufficient space to hold "generic interface 'symbol.symbol'".  */
    2125      1939251 :   char interface_name[2*GFC_MAX_SYMBOL_LEN+2 + sizeof("generic interface ''")];
    2126      1939251 :   gfc_interface *p;
    2127              : 
    2128      1939251 :   if (sym->ns != gfc_current_ns)
    2129        61664 :     return;
    2130              : 
    2131      1877605 :   if (sym->generic != NULL)
    2132              :     {
    2133        84369 :       size_t len = strlen (sym->name) + sizeof("generic interface ''");
    2134        84369 :       gcc_assert (len < sizeof (interface_name));
    2135        84369 :       sprintf (interface_name, "generic interface '%s'", sym->name);
    2136        84369 :       if (check_interface0 (sym->generic, interface_name))
    2137              :         return;
    2138              : 
    2139       280269 :       for (p = sym->generic; p; p = p->next)
    2140              :         {
    2141       195918 :           if (p->sym->attr.mod_proc
    2142         1218 :               && !p->sym->attr.module_procedure
    2143         1212 :               && (p->sym->attr.if_source != IFSRC_DECL
    2144         1208 :                   || p->sym->attr.procedure))
    2145              :             {
    2146            4 :               gfc_error ("%qs at %L is not a module procedure",
    2147              :                          p->sym->name, &p->where);
    2148            4 :               return;
    2149              :             }
    2150              :         }
    2151              : 
    2152              :       /* Originally, this test was applied to host interfaces too;
    2153              :          this is incorrect since host associated symbols, from any
    2154              :          source, cannot be ambiguous with local symbols.  */
    2155        84351 :       check_interface1 (sym->generic, sym->generic, 1, interface_name,
    2156        84351 :                         sym->attr.referenced || !sym->attr.use_assoc);
    2157              :     }
    2158              : }
    2159              : 
    2160              : 
    2161              : static void
    2162          403 : check_uop_interfaces (gfc_user_op *uop)
    2163              : {
    2164          403 :   char interface_name[GFC_MAX_SYMBOL_LEN + sizeof("operator interface ''")];
    2165          403 :   gfc_user_op *uop2;
    2166          403 :   gfc_namespace *ns;
    2167              : 
    2168          403 :   sprintf (interface_name, "operator interface '%s'", uop->name);
    2169          403 :   if (check_interface0 (uop->op, interface_name))
    2170            2 :     return;
    2171              : 
    2172          840 :   for (ns = gfc_current_ns; ns; ns = ns->parent)
    2173              :     {
    2174          439 :       uop2 = gfc_find_uop (uop->name, ns);
    2175          439 :       if (uop2 == NULL)
    2176           22 :         continue;
    2177              : 
    2178          417 :       check_interface1 (uop->op, uop2->op, 0,
    2179              :                         interface_name, true);
    2180              :     }
    2181              : }
    2182              : 
    2183              : /* Given an intrinsic op, return an equivalent op if one exists,
    2184              :    or INTRINSIC_NONE otherwise.  */
    2185              : 
    2186              : gfc_intrinsic_op
    2187     12201415 : gfc_equivalent_op (gfc_intrinsic_op op)
    2188              : {
    2189     12201415 :   switch(op)
    2190              :     {
    2191              :     case INTRINSIC_EQ:
    2192              :       return INTRINSIC_EQ_OS;
    2193              : 
    2194              :     case INTRINSIC_EQ_OS:
    2195              :       return INTRINSIC_EQ;
    2196              : 
    2197              :     case INTRINSIC_NE:
    2198              :       return INTRINSIC_NE_OS;
    2199              : 
    2200              :     case INTRINSIC_NE_OS:
    2201              :       return INTRINSIC_NE;
    2202              : 
    2203              :     case INTRINSIC_GT:
    2204              :       return INTRINSIC_GT_OS;
    2205              : 
    2206              :     case INTRINSIC_GT_OS:
    2207              :       return INTRINSIC_GT;
    2208              : 
    2209              :     case INTRINSIC_GE:
    2210              :       return INTRINSIC_GE_OS;
    2211              : 
    2212              :     case INTRINSIC_GE_OS:
    2213              :       return INTRINSIC_GE;
    2214              : 
    2215              :     case INTRINSIC_LT:
    2216              :       return INTRINSIC_LT_OS;
    2217              : 
    2218              :     case INTRINSIC_LT_OS:
    2219              :       return INTRINSIC_LT;
    2220              : 
    2221              :     case INTRINSIC_LE:
    2222              :       return INTRINSIC_LE_OS;
    2223              : 
    2224              :     case INTRINSIC_LE_OS:
    2225              :       return INTRINSIC_LE;
    2226              : 
    2227              :     default:
    2228              :       return INTRINSIC_NONE;
    2229              :     }
    2230              : }
    2231              : 
    2232              : /* For the namespace, check generic, user operator and intrinsic
    2233              :    operator interfaces for consistency and to remove duplicate
    2234              :    interfaces.  We traverse the whole namespace, counting on the fact
    2235              :    that most symbols will not have generic or operator interfaces.  */
    2236              : 
    2237              : void
    2238       360463 : gfc_check_interfaces (gfc_namespace *ns)
    2239              : {
    2240       360463 :   gfc_namespace *old_ns, *ns2;
    2241       360463 :   char interface_name[GFC_MAX_SYMBOL_LEN + sizeof("intrinsic '' operator")];
    2242       360463 :   int i;
    2243              : 
    2244       360463 :   old_ns = gfc_current_ns;
    2245       360463 :   gfc_current_ns = ns;
    2246              : 
    2247       360463 :   gfc_traverse_ns (ns, check_sym_interfaces);
    2248              : 
    2249       360463 :   gfc_traverse_user_op (ns, check_uop_interfaces);
    2250              : 
    2251     10453359 :   for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
    2252              :     {
    2253     10092899 :       if (i == INTRINSIC_USER)
    2254       360460 :         continue;
    2255              : 
    2256      9732439 :       if (i == INTRINSIC_ASSIGN)
    2257       360460 :         strcpy (interface_name, "intrinsic assignment operator");
    2258              :       else
    2259      9371979 :         sprintf (interface_name, "intrinsic '%s' operator",
    2260              :                  gfc_op2string ((gfc_intrinsic_op) i));
    2261              : 
    2262      9732439 :       if (check_interface0 (ns->op[i], interface_name))
    2263            0 :         continue;
    2264              : 
    2265      9732439 :       if (ns->op[i])
    2266         2472 :         gfc_check_operator_interface (ns->op[i]->sym, (gfc_intrinsic_op) i,
    2267              :                                       ns->op[i]->where);
    2268              : 
    2269     21933782 :       for (ns2 = ns; ns2; ns2 = ns2->parent)
    2270              :         {
    2271     12201346 :           gfc_intrinsic_op other_op;
    2272              : 
    2273     12201346 :           if (check_interface1 (ns->op[i], ns2->op[i], 0,
    2274              :                                 interface_name, true))
    2275            3 :             goto done;
    2276              : 
    2277              :           /* i should be gfc_intrinsic_op, but has to be int with this cast
    2278              :              here for stupid C++ compatibility rules.  */
    2279     12201343 :           other_op = gfc_equivalent_op ((gfc_intrinsic_op) i);
    2280     12201343 :           if (other_op != INTRINSIC_NONE
    2281     12201343 :             &&  check_interface1 (ns->op[i], ns2->op[other_op],
    2282              :                                   0, interface_name, true))
    2283            0 :             goto done;
    2284              :         }
    2285              :     }
    2286              : 
    2287       360460 : done:
    2288       360463 :   gfc_current_ns = old_ns;
    2289       360463 : }
    2290              : 
    2291              : 
    2292              : /* Given a symbol of a formal argument list and an expression, if the
    2293              :    formal argument is allocatable, check that the actual argument is
    2294              :    allocatable. Returns true if compatible, zero if not compatible.  */
    2295              : 
    2296              : static bool
    2297       257822 : compare_allocatable (gfc_symbol *formal, gfc_expr *actual)
    2298              : {
    2299       257822 :   if (formal->attr.allocatable
    2300       254712 :       || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)->attr.allocatable))
    2301              :     {
    2302         3996 :       symbol_attribute attr = gfc_expr_attr (actual);
    2303         3996 :       if (actual->ts.type == BT_CLASS && !attr.class_ok)
    2304           23 :         return true;
    2305         3982 :       else if (!attr.allocatable)
    2306              :         return false;
    2307              :     }
    2308              : 
    2309              :   return true;
    2310              : }
    2311              : 
    2312              : 
    2313              : /* Given a symbol of a formal argument list and an expression, if the
    2314              :    formal argument is a pointer, see if the actual argument is a
    2315              :    pointer. Returns nonzero if compatible, zero if not compatible.  */
    2316              : 
    2317              : static int
    2318       257843 : compare_pointer (gfc_symbol *formal, gfc_expr *actual)
    2319              : {
    2320       257843 :   symbol_attribute attr;
    2321              : 
    2322       257843 :   if (formal->attr.pointer
    2323       253040 :       || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)
    2324        14250 :           && CLASS_DATA (formal)->attr.class_pointer))
    2325              :     {
    2326         5743 :       attr = gfc_expr_attr (actual);
    2327              : 
    2328              :       /* Fortran 2008 allows non-pointer actual arguments.  */
    2329         5743 :       if (!attr.pointer && attr.target && formal->attr.intent == INTENT_IN)
    2330              :         return 2;
    2331              : 
    2332         5356 :       if (!attr.pointer)
    2333              :         return 0;
    2334              :     }
    2335              : 
    2336              :   return 1;
    2337              : }
    2338              : 
    2339              : 
    2340              : /* Emit clear error messages for rank mismatch.  */
    2341              : 
    2342              : static void
    2343          153 : argument_rank_mismatch (const char *name, locus *where,
    2344              :                         int rank1, int rank2, locus *where_formal)
    2345              : {
    2346              : 
    2347              :   /* TS 29113, C407b.  */
    2348          153 :   if (where_formal == NULL)
    2349              :     {
    2350          143 :       if (rank2 == -1)
    2351           10 :         gfc_error ("The assumed-rank array at %L requires that the dummy "
    2352              :                    "argument %qs has assumed-rank", where, name);
    2353          133 :       else if (rank1 == 0)
    2354           22 :         gfc_error_opt (0, "Rank mismatch in argument %qs "
    2355              :                        "at %L (scalar and rank-%d)", name, where, rank2);
    2356          111 :       else if (rank2 == 0)
    2357          104 :         gfc_error_opt (0, "Rank mismatch in argument %qs "
    2358              :                        "at %L (rank-%d and scalar)", name, where, rank1);
    2359              :       else
    2360            7 :         gfc_error_opt (0, "Rank mismatch in argument %qs "
    2361              :                        "at %L (rank-%d and rank-%d)", name, where, rank1,
    2362              :                        rank2);
    2363              :     }
    2364              :   else
    2365              :     {
    2366           10 :       if (rank2 == -1)
    2367              :         /* This is an assumed rank-actual passed to a function without
    2368              :            an explicit interface, which is already diagnosed in
    2369              :            gfc_procedure_use.  */
    2370              :         return;
    2371            8 :       if (rank1 == 0)
    2372            6 :         gfc_error_opt (0, "Rank mismatch between actual argument at %L "
    2373              :                        "and actual argument at %L (scalar and rank-%d)",
    2374              :                        where, where_formal, rank2);
    2375            2 :       else if (rank2 == 0)
    2376            2 :         gfc_error_opt (0, "Rank mismatch between actual argument at %L "
    2377              :                        "and actual argument at %L (rank-%d and scalar)",
    2378              :                        where, where_formal, rank1);
    2379              :       else
    2380            0 :         gfc_error_opt (0, "Rank mismatch between actual argument at %L "
    2381              :                        "and actual argument at %L (rank-%d and rank-%d)", where,
    2382              :                        where_formal, rank1, rank2);
    2383              :     }
    2384              : }
    2385              : 
    2386              : 
    2387              : /* Under certain conditions, a scalar actual argument can be passed
    2388              :    to an array dummy argument - see F2018, 15.5.2.4, paragraph 14.
    2389              :    This function returns true for these conditions so that an error
    2390              :    or warning for this can be suppressed later.  Always return false
    2391              :    for expressions with rank > 0.  */
    2392              : 
    2393              : bool
    2394         3069 : maybe_dummy_array_arg (gfc_expr *e)
    2395              : {
    2396         3069 :   gfc_symbol *s;
    2397         3069 :   gfc_ref *ref;
    2398         3069 :   bool array_pointer = false;
    2399         3069 :   bool assumed_shape = false;
    2400         3069 :   bool scalar_ref = true;
    2401              : 
    2402         3069 :   if (e->rank > 0)
    2403              :     return false;
    2404              : 
    2405         3063 :   if (e->ts.type == BT_CHARACTER && e->ts.kind == 1)
    2406              :     return true;
    2407              : 
    2408              :   /* If this comes from a constructor, it has been an array element
    2409              :      originally.  */
    2410              : 
    2411         2914 :   if (e->expr_type == EXPR_CONSTANT)
    2412          687 :     return e->from_constructor;
    2413              : 
    2414         2227 :   if (e->expr_type != EXPR_VARIABLE)
    2415              :     return false;
    2416              : 
    2417         2119 :   s = e->symtree->n.sym;
    2418              : 
    2419         2119 :   if (s->attr.dimension)
    2420              :     {
    2421          235 :       scalar_ref = false;
    2422          235 :       array_pointer = s->attr.pointer;
    2423              :     }
    2424              : 
    2425         2119 :   if (s->as && s->as->type == AS_ASSUMED_SHAPE)
    2426         2119 :     assumed_shape = true;
    2427              : 
    2428         2383 :   for (ref=e->ref; ref; ref=ref->next)
    2429              :     {
    2430          264 :       if (ref->type == REF_COMPONENT)
    2431              :         {
    2432           20 :           symbol_attribute *attr;
    2433           20 :           attr = &ref->u.c.component->attr;
    2434           20 :           if (attr->dimension)
    2435              :             {
    2436            2 :               array_pointer = attr->pointer;
    2437            2 :               assumed_shape = false;
    2438            2 :               scalar_ref = false;
    2439              :             }
    2440              :           else
    2441              :             scalar_ref = true;
    2442              :         }
    2443              :     }
    2444              : 
    2445         2119 :   return !(scalar_ref || array_pointer || assumed_shape);
    2446              : }
    2447              : 
    2448              : /* Given a symbol of a formal argument list and an expression, see if
    2449              :    the two are compatible as arguments.  Returns true if
    2450              :    compatible, false if not compatible.  */
    2451              : 
    2452              : static bool
    2453       365026 : compare_parameter (gfc_symbol *formal, gfc_expr *actual,
    2454              :                    int ranks_must_agree, int is_elemental, locus *where)
    2455              : {
    2456       365026 :   gfc_ref *ref;
    2457       365026 :   bool rank_check, is_pointer;
    2458       365026 :   char err[200];
    2459       365026 :   gfc_component *ppc;
    2460       365026 :   bool codimension = false;
    2461       365026 :   gfc_array_spec *formal_as;
    2462       365026 :   const char *actual_name;
    2463              : 
    2464              :   /* If the formal arg has type BT_VOID, it's to one of the iso_c_binding
    2465              :      procs c_f_pointer or c_f_procpointer, and we need to accept most
    2466              :      pointers the user could give us.  This should allow that.  */
    2467       365026 :   if (formal->ts.type == BT_VOID)
    2468              :     return true;
    2469              : 
    2470       365026 :   if (formal->ts.type == BT_DERIVED
    2471        29932 :       && formal->ts.u.derived && formal->ts.u.derived->ts.is_iso_c
    2472         4414 :       && actual->ts.type == BT_DERIVED
    2473         4404 :       && actual->ts.u.derived && actual->ts.u.derived->ts.is_iso_c)
    2474              :     {
    2475         4404 :       if (formal->ts.u.derived->intmod_sym_id
    2476         4404 :           != actual->ts.u.derived->intmod_sym_id)
    2477              :         return false;
    2478              : 
    2479         4303 :       if (ranks_must_agree
    2480          136 :           && gfc_symbol_rank (formal) != actual->rank
    2481         4363 :           && gfc_symbol_rank (formal) != -1)
    2482              :         {
    2483           42 :           if (where)
    2484            0 :             argument_rank_mismatch (formal->name, &actual->where,
    2485              :                                     gfc_symbol_rank (formal), actual->rank,
    2486              :                                     NULL);
    2487           42 :           return false;
    2488              :         }
    2489         4261 :       return true;
    2490              :     }
    2491              : 
    2492       360622 :   if (formal->ts.type == BT_CLASS && actual->ts.type == BT_DERIVED)
    2493              :     /* Make sure the vtab symbol is present when
    2494              :        the module variables are generated.  */
    2495         7439 :     gfc_find_derived_vtab (actual->ts.u.derived);
    2496              : 
    2497       360622 :   if (actual->ts.type == BT_PROCEDURE)
    2498              :     {
    2499         1991 :       gfc_symbol *act_sym = actual->symtree->n.sym;
    2500              : 
    2501         1991 :       if (formal->attr.flavor != FL_PROCEDURE && !act_sym->ts.interface)
    2502              :         {
    2503            4 :           if (where)
    2504            2 :             gfc_error ("Invalid procedure argument at %L", &actual->where);
    2505            4 :           return false;
    2506              :         }
    2507         1987 :       else if (act_sym->ts.interface
    2508         1987 :                && !gfc_compare_interfaces (formal, act_sym->ts.interface,
    2509              :                                            act_sym->name, 0, 1, err,
    2510              :                                            sizeof(err),NULL, NULL))
    2511              :         {
    2512            1 :           if (where)
    2513              :             {
    2514              :               /* Artificially generated symbol names would only confuse.  */
    2515            1 :               if (formal->attr.artificial)
    2516            0 :                 gfc_error_opt (0, "Interface mismatch in dummy procedure "
    2517              :                                "at %L conflicts with %L: %s", &actual->where,
    2518              :                                &formal->declared_at, err);
    2519              :               else
    2520            1 :                 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs "
    2521              :                                "at %L: %s", formal->name, &actual->where, err);
    2522              :             }
    2523            1 :           return false;
    2524              :         }
    2525              : 
    2526         1986 :       if (!gfc_compare_interfaces (formal, act_sym, act_sym->name, 0, 1, err,
    2527              :                                    sizeof(err), NULL, NULL))
    2528              :         {
    2529           40 :           if (where)
    2530              :             {
    2531           40 :               if (formal->attr.artificial)
    2532            1 :                 gfc_error_opt (0, "Interface mismatch in dummy procedure "
    2533              :                                "at %L conflicts with %L: %s", &actual->where,
    2534              :                                &formal->declared_at, err);
    2535              :               else
    2536           39 :                 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at "
    2537              :                                "%L: %s", formal->name, &actual->where, err);
    2538              : 
    2539              :             }
    2540           40 :           return false;
    2541              :         }
    2542              : 
    2543              :       /* The actual symbol may disagree with a global symbol.  If so, issue an
    2544              :          error, but only if no previous error has been reported on the formal
    2545              :          argument.  */
    2546         1946 :       actual_name = act_sym->name;
    2547         1946 :       if (!formal->error && actual_name)
    2548              :         {
    2549         1946 :           gfc_gsymbol *gsym;
    2550         1946 :           gsym = gfc_find_gsymbol (gfc_gsym_root, actual_name);
    2551         1946 :           if (gsym != NULL)
    2552              :             {
    2553          180 :               if (gsym->type == GSYM_SUBROUTINE && formal->attr.function)
    2554              :                 {
    2555            1 :                   gfc_error ("Passing global subroutine %qs declared at %L "
    2556              :                              "as function at %L", actual_name, &gsym->where,
    2557              :                              &actual->where);
    2558            1 :                   return false;
    2559              :                 }
    2560          179 :               if (gsym->type == GSYM_FUNCTION && formal->attr.subroutine)
    2561              :                 {
    2562            1 :                   gfc_error ("Passing global function %qs declared at %L "
    2563              :                              "as subroutine at %L", actual_name, &gsym->where,
    2564              :                              &actual->where);
    2565            1 :                   return false;
    2566              :                 }
    2567          178 :               if (gsym->type == GSYM_FUNCTION)
    2568              :                 {
    2569           99 :                   gfc_symbol *global_asym;
    2570           99 :                   gfc_find_symbol (actual_name, gsym->ns, 0, &global_asym);
    2571           99 :                   if (global_asym != NULL)
    2572              :                     {
    2573           99 :                       if (formal->attr.subroutine)
    2574              :                         {
    2575            0 :                           gfc_error ("Mismatch between subroutine and "
    2576              :                                      "function at %L", &actual->where);
    2577            1 :                           return false;
    2578              :                         }
    2579           99 :                       else if (formal->attr.function)
    2580              :                         {
    2581           98 :                           gfc_typespec ts;
    2582              : 
    2583           98 :                           if (global_asym->result)
    2584           97 :                             ts = global_asym->result->ts;
    2585              :                           else
    2586            1 :                             ts = global_asym->ts;
    2587              : 
    2588           98 :                           if (!gfc_compare_types (&ts,
    2589              :                                                   &formal->ts))
    2590              :                             {
    2591            2 :                               gfc_error ("Type mismatch at %L passing global "
    2592              :                                          "function %qs declared at %L (%s/%s)",
    2593              :                                          &actual->where, actual_name,
    2594              :                                          &gsym->where,
    2595            1 :                                          gfc_typename (&global_asym->ts),
    2596              :                                          gfc_dummy_typename (&formal->ts));
    2597            1 :                               return false;
    2598              :                             }
    2599              :                         }
    2600              :                       else
    2601              :                         {
    2602              :                           /* The global symbol is a function.  Set the formal
    2603              :                              argument accordingly.  */
    2604            1 :                           formal->attr.function = 1;
    2605            1 :                           formal->ts = global_asym->ts;
    2606              :                         }
    2607              :                     }
    2608              :                 }
    2609              :             }
    2610              :         }
    2611              : 
    2612         1943 :       if (formal->attr.function && !act_sym->attr.function)
    2613              :         {
    2614            5 :           gfc_add_function (&act_sym->attr, act_sym->name,
    2615              :           &act_sym->declared_at);
    2616            5 :           if (act_sym->ts.type == BT_UNKNOWN
    2617            5 :               && !gfc_set_default_type (act_sym, 1, act_sym->ns))
    2618              :             return false;
    2619              :         }
    2620         1938 :       else if (formal->attr.subroutine && !act_sym->attr.subroutine)
    2621           50 :         gfc_add_subroutine (&act_sym->attr, act_sym->name,
    2622              :                             &act_sym->declared_at);
    2623              : 
    2624         1943 :       return true;
    2625              :     }
    2626       358631 :   ppc = gfc_get_proc_ptr_comp (actual);
    2627       358631 :   if (ppc && ppc->ts.interface)
    2628              :     {
    2629          496 :       if (!gfc_compare_interfaces (formal, ppc->ts.interface, ppc->name, 0, 1,
    2630              :                                    err, sizeof(err), NULL, NULL))
    2631              :         {
    2632            2 :           if (where)
    2633            2 :             gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at %L:"
    2634              :                            " %s", formal->name, &actual->where, err);
    2635            2 :           return false;
    2636              :         }
    2637              :     }
    2638              : 
    2639              :   /* F2008, C1241.  */
    2640         5318 :   if (formal->attr.pointer && formal->attr.contiguous
    2641       358664 :       && !gfc_is_simply_contiguous (actual, true, false))
    2642              :     {
    2643            4 :       if (where)
    2644            4 :         gfc_error ("Actual argument to contiguous pointer dummy %qs at %L "
    2645              :                    "must be simply contiguous", formal->name, &actual->where);
    2646            4 :       return false;
    2647              :     }
    2648              : 
    2649       358625 :   symbol_attribute actual_attr = gfc_expr_attr (actual);
    2650       358625 :   if (actual->ts.type == BT_CLASS && !actual_attr.class_ok)
    2651              :     return true;
    2652              : 
    2653          807 :   if ((actual->expr_type != EXPR_NULL || actual->ts.type != BT_UNKNOWN)
    2654       358310 :       && actual->ts.type != BT_HOLLERITH
    2655       358291 :       && formal->ts.type != BT_ASSUMED
    2656       354824 :       && !(formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    2657       354824 :       && !gfc_compare_types (&formal->ts, &actual->ts)
    2658       464326 :       && !(formal->ts.type == BT_DERIVED && actual->ts.type == BT_CLASS
    2659            2 :            && gfc_compare_derived_types (formal->ts.u.derived,
    2660            2 :                                          CLASS_DATA (actual)->ts.u.derived)))
    2661              :     {
    2662       105756 :       if (where)
    2663              :         {
    2664           68 :           if (formal->attr.artificial)
    2665              :             {
    2666           19 :               if (!flag_allow_argument_mismatch || !formal->error)
    2667           14 :                 gfc_error_opt (0, "Type mismatch between actual argument at %L "
    2668              :                                "and actual argument at %L (%s/%s).",
    2669              :                                &actual->where,
    2670              :                                &formal->declared_at,
    2671              :                                gfc_typename (actual),
    2672              :                                gfc_dummy_typename (&formal->ts));
    2673              : 
    2674           19 :               formal->error = 1;
    2675              :             }
    2676              :           else
    2677           49 :             gfc_error_opt (0, "Type mismatch in argument %qs at %L; passed %s "
    2678              :                            "to %s", formal->name, where, gfc_typename (actual),
    2679              :                            gfc_dummy_typename (&formal->ts));
    2680              :         }
    2681       105756 :       return false;
    2682              :     }
    2683              : 
    2684       252812 :   if (actual->ts.type == BT_ASSUMED && formal->ts.type != BT_ASSUMED)
    2685              :     {
    2686            3 :       if (where)
    2687            1 :         gfc_error ("Assumed-type actual argument at %L requires that dummy "
    2688              :                    "argument %qs is of assumed type", &actual->where,
    2689              :                    formal->name);
    2690            3 :       return false;
    2691              :     }
    2692              : 
    2693              :   /* TS29113 C407c; F2018 C711.  */
    2694       252809 :   if (actual->ts.type == BT_ASSUMED
    2695          326 :       && gfc_symbol_rank (formal) == -1
    2696           27 :       && actual->rank != -1
    2697       252816 :       && !(actual->symtree->n.sym->as
    2698            5 :            && actual->symtree->n.sym->as->type == AS_ASSUMED_SHAPE))
    2699              :     {
    2700            4 :       if (where)
    2701            4 :         gfc_error ("Assumed-type actual argument at %L corresponding to "
    2702              :                    "assumed-rank dummy argument %qs must be "
    2703              :                    "assumed-shape or assumed-rank",
    2704              :                    &actual->where, formal->name);
    2705            4 :       return false;
    2706              :     }
    2707              : 
    2708              :   /* F2008, 12.5.2.5; IR F08/0073.  */
    2709       252805 :   if (formal->ts.type == BT_CLASS && formal->attr.class_ok
    2710        14244 :       && actual->expr_type != EXPR_NULL
    2711        14244 :       && ((CLASS_DATA (formal)->attr.class_pointer
    2712          917 :            && formal->attr.intent != INTENT_IN)
    2713        13992 :           || CLASS_DATA (formal)->attr.allocatable))
    2714              :     {
    2715         1126 :       if (actual->ts.type != BT_CLASS)
    2716              :         {
    2717            2 :           if (where)
    2718            2 :             gfc_error ("Actual argument to %qs at %L must be polymorphic",
    2719              :                         formal->name, &actual->where);
    2720            2 :           return false;
    2721              :         }
    2722              : 
    2723         1124 :       if ((!UNLIMITED_POLY (formal) || !UNLIMITED_POLY(actual))
    2724          781 :           && !gfc_compare_derived_types (CLASS_DATA (actual)->ts.u.derived,
    2725          781 :                                          CLASS_DATA (formal)->ts.u.derived))
    2726              :         {
    2727            1 :           if (where)
    2728            1 :             gfc_error ("Actual argument to %qs at %L must have the same "
    2729              :                        "declared type", formal->name, &actual->where);
    2730            1 :           return false;
    2731              :         }
    2732              :     }
    2733              : 
    2734              :   /* F08: 12.5.2.5 Allocatable and pointer dummy variables.  However, this
    2735              :      is necessary also for F03, so retain error for both.
    2736              :      NOTE: Other type/kind errors pre-empt this error.  Since they are F03
    2737              :      compatible, no attempt has been made to channel to this one.  */
    2738       252802 :   if (UNLIMITED_POLY (formal) && !UNLIMITED_POLY (actual)
    2739         1622 :       && (CLASS_DATA (formal)->attr.allocatable
    2740         1622 :           ||CLASS_DATA (formal)->attr.class_pointer))
    2741              :     {
    2742            0 :       if (where)
    2743            0 :         gfc_error ("Actual argument to %qs at %L must be unlimited "
    2744              :                    "polymorphic since the formal argument is a "
    2745              :                    "pointer or allocatable unlimited polymorphic "
    2746              :                    "entity [F2008: 12.5.2.5]", formal->name,
    2747              :                    &actual->where);
    2748            0 :       return false;
    2749              :     }
    2750              : 
    2751       252802 :   if (formal->ts.type == BT_CLASS && formal->attr.class_ok)
    2752        14241 :     codimension = CLASS_DATA (formal)->attr.codimension;
    2753              :   else
    2754       238561 :     codimension = formal->attr.codimension;
    2755              : 
    2756       252802 :   if (codimension && !gfc_is_coarray (actual))
    2757              :     {
    2758            4 :       if (where)
    2759            4 :         gfc_error ("Actual argument to %qs at %L must be a coarray",
    2760              :                        formal->name, &actual->where);
    2761            4 :       return false;
    2762              :     }
    2763              : 
    2764       238558 :   formal_as = (formal->ts.type == BT_CLASS
    2765       252798 :                ? CLASS_DATA (formal)->as : formal->as);
    2766              : 
    2767       252798 :   if (codimension && formal->attr.allocatable)
    2768              :     {
    2769           27 :       gfc_ref *last = NULL;
    2770              : 
    2771           54 :       for (ref = actual->ref; ref; ref = ref->next)
    2772           27 :         if (ref->type == REF_COMPONENT)
    2773            0 :           last = ref;
    2774              : 
    2775              :       /* F2008, 12.5.2.6.  */
    2776           27 :       if ((last && last->u.c.component->as->corank != formal->as->corank)
    2777              :           || (!last
    2778           27 :               && actual->symtree->n.sym->as->corank != formal->as->corank))
    2779              :         {
    2780            1 :           if (where)
    2781            1 :             gfc_error ("Corank mismatch in argument %qs at %L (%d and %d)",
    2782            1 :                    formal->name, &actual->where, formal->as->corank,
    2783            0 :                    last ? last->u.c.component->as->corank
    2784            1 :                         : actual->symtree->n.sym->as->corank);
    2785            1 :           return false;
    2786              :         }
    2787              :     }
    2788              : 
    2789          417 :   if (codimension)
    2790              :     {
    2791              :       /* F2008, 12.5.2.8 + Corrig 2 (IR F08/0048).  */
    2792              :       /* F2018, 12.5.2.8.  */
    2793          417 :       if (formal->attr.dimension
    2794          162 :           && (formal->attr.contiguous || formal->as->type != AS_ASSUMED_SHAPE)
    2795          103 :           && actual_attr.dimension
    2796          519 :           && !gfc_is_simply_contiguous (actual, true, true))
    2797              :         {
    2798            2 :           if (where)
    2799            2 :             gfc_error ("Actual argument to %qs at %L must be simply "
    2800              :                        "contiguous or an element of such an array",
    2801              :                        formal->name, &actual->where);
    2802            2 :           return false;
    2803              :         }
    2804              : 
    2805              :       /* F2008, C1303 and C1304.  */
    2806          415 :       if (formal->attr.intent != INTENT_INOUT
    2807          406 :           && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
    2808          203 :                && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    2809            1 :                && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
    2810          405 :               || formal->attr.lock_comp))
    2811              : 
    2812              :         {
    2813            1 :           if (where)
    2814            1 :             gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
    2815              :                        "which is LOCK_TYPE or has a LOCK_TYPE component",
    2816              :                        formal->name, &actual->where);
    2817            1 :           return false;
    2818              :         }
    2819              : 
    2820              :       /* TS18508, C702/C703.  */
    2821          414 :       if (formal->attr.intent != INTENT_INOUT
    2822          405 :           && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
    2823          202 :                && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    2824            0 :                && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    2825          405 :               || formal->attr.event_comp))
    2826              : 
    2827              :         {
    2828            0 :           if (where)
    2829            0 :             gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
    2830              :                        "which is EVENT_TYPE or has a EVENT_TYPE component",
    2831              :                        formal->name, &actual->where);
    2832            0 :           return false;
    2833              :         }
    2834              :     }
    2835              : 
    2836              :   /* F2008, C1239/C1240.  */
    2837       252794 :   if (actual->expr_type == EXPR_VARIABLE
    2838       104156 :       && (actual->symtree->n.sym->attr.asynchronous
    2839       104119 :          || actual->symtree->n.sym->attr.volatile_)
    2840         3284 :       &&  (formal->attr.asynchronous || formal->attr.volatile_)
    2841           75 :       && actual->rank && formal->as
    2842           70 :       && !gfc_is_simply_contiguous (actual, true, false)
    2843       252842 :       && ((formal->as->type != AS_ASSUMED_SHAPE
    2844           19 :            && formal->as->type != AS_ASSUMED_RANK && !formal->attr.pointer)
    2845           37 :           || formal->attr.contiguous))
    2846              :     {
    2847           22 :       if (where)
    2848           22 :         gfc_error ("Dummy argument %qs has to be a pointer, assumed-shape or "
    2849              :                    "assumed-rank array without CONTIGUOUS attribute - as actual"
    2850              :                    " argument at %L is not simply contiguous and both are "
    2851              :                    "ASYNCHRONOUS or VOLATILE", formal->name, &actual->where);
    2852           22 :       return false;
    2853              :     }
    2854              : 
    2855       252772 :   if (formal->attr.allocatable && !codimension
    2856         3188 :       && actual_attr.codimension)
    2857              :     {
    2858            5 :       if (formal->attr.intent == INTENT_OUT)
    2859              :         {
    2860            1 :           if (where)
    2861            1 :             gfc_error ("Passing coarray at %L to allocatable, noncoarray, "
    2862              :                        "INTENT(OUT) dummy argument %qs", &actual->where,
    2863              :                        formal->name);
    2864            1 :           return false;
    2865              :         }
    2866            4 :       else if (warn_surprising && where && formal->attr.intent != INTENT_IN)
    2867            1 :         gfc_warning (OPT_Wsurprising,
    2868              :                      "Passing coarray at %L to allocatable, noncoarray dummy "
    2869              :                      "argument %qs, which is invalid if the allocation status"
    2870              :                      " is modified",  &actual->where, formal->name);
    2871              :     }
    2872              : 
    2873              :   /* If the rank is the same or the formal argument has assumed-rank.  */
    2874       252771 :   if (gfc_symbol_rank (formal) == actual->rank || gfc_symbol_rank (formal) == -1)
    2875       244664 :     return true;
    2876              : 
    2877         1818 :   rank_check = where != NULL && !is_elemental && formal_as
    2878         1785 :     && (formal_as->type == AS_ASSUMED_SHAPE
    2879         1785 :         || formal_as->type == AS_DEFERRED)
    2880         8258 :     && !(actual->expr_type == EXPR_NULL
    2881           86 :          && actual->ts.type == BT_UNKNOWN);
    2882              : 
    2883              :   /* Skip rank checks for NO_ARG_CHECK.  */
    2884         8107 :   if (formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    2885              :     return true;
    2886              : 
    2887              :   /* Scalar & coindexed, see: F2008, Section 12.5.2.4.  */
    2888         7769 :   if (rank_check || ranks_must_agree
    2889         7611 :       || (formal->attr.pointer && actual->expr_type != EXPR_NULL)
    2890         7611 :       || (actual->rank != 0
    2891         6820 :           && !(is_elemental || formal->attr.dimension
    2892          118 :                || (formal->ts.type == BT_CLASS
    2893           85 :                    && CLASS_DATA (formal)->attr.dimension)))
    2894         7578 :       || (actual->rank == 0
    2895          791 :           && ((formal->ts.type == BT_CLASS
    2896            1 :                && CLASS_DATA (formal)->as->type == AS_ASSUMED_SHAPE)
    2897          791 :               || (formal->ts.type != BT_CLASS
    2898          790 :                    && formal->as->type == AS_ASSUMED_SHAPE))
    2899           13 :           && actual->expr_type != EXPR_NULL)
    2900         7578 :       || (actual->rank == 0
    2901          791 :           && (formal->attr.dimension
    2902            1 :               || (formal->ts.type == BT_CLASS
    2903            1 :                   && CLASS_DATA (formal)->attr.dimension))
    2904          791 :           && gfc_is_coindexed (actual))
    2905              :       /* Assumed-rank actual argument; F2018 C838.  */
    2906        15344 :       || actual->rank == -1)
    2907              :     {
    2908          199 :       if (where
    2909          199 :           && (!formal->attr.artificial || (!formal->maybe_array
    2910            8 :                                            && !maybe_dummy_array_arg (actual))))
    2911              :         {
    2912          104 :           locus *where_formal;
    2913          104 :           if (formal->attr.artificial)
    2914            8 :             where_formal = &formal->declared_at;
    2915              :           else
    2916              :             where_formal = NULL;
    2917              : 
    2918          104 :           argument_rank_mismatch (formal->name, &actual->where,
    2919              :                                   gfc_symbol_rank (formal), actual->rank,
    2920              :                                   where_formal);
    2921              :         }
    2922          199 :       return false;
    2923              :     }
    2924         7570 :   else if (actual->rank != 0
    2925         6782 :            && (is_elemental || formal->attr.dimension
    2926           85 :                || (formal->ts.type == BT_CLASS
    2927           85 :                    && CLASS_DATA (formal)->attr.dimension)))
    2928              :     return true;
    2929              : 
    2930              :   /* At this point, we are considering a scalar passed to an array.   This
    2931              :      is valid (cf. F95 12.4.1.1, F2003 12.4.1.2, and F2008 12.5.2.4),
    2932              :      - if the actual argument is (a substring of) an element of a
    2933              :        non-assumed-shape/non-pointer/non-polymorphic array; or
    2934              :      - (F2003) if the actual argument is of type character of default/c_char
    2935              :        kind.
    2936              :      - (F2018) if the dummy argument is type(*).  */
    2937              : 
    2938         1576 :   is_pointer = actual->expr_type == EXPR_VARIABLE
    2939          788 :                ? actual->symtree->n.sym->attr.pointer : false;
    2940              : 
    2941          811 :   for (ref = actual->ref; ref; ref = ref->next)
    2942              :     {
    2943          439 :       if (ref->type == REF_COMPONENT)
    2944           12 :         is_pointer = ref->u.c.component->attr.pointer;
    2945          427 :       else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
    2946          420 :                && ref->u.ar.dimen > 0
    2947          417 :                && (!ref->next
    2948            9 :                    || (ref->next->type == REF_SUBSTRING && !ref->next->next)))
    2949              :         break;
    2950              :     }
    2951              : 
    2952          788 :   if (actual->ts.type == BT_CLASS && actual->expr_type != EXPR_NULL)
    2953              :     {
    2954            0 :       if (where)
    2955            0 :         gfc_error ("Polymorphic scalar passed to array dummy argument %qs "
    2956              :                    "at %L", formal->name, &actual->where);
    2957            0 :       return false;
    2958              :     }
    2959              : 
    2960          788 :   if (actual->expr_type != EXPR_NULL && ref && actual->ts.type != BT_CHARACTER
    2961          367 :       && (is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
    2962              :     {
    2963           10 :       if (where)
    2964              :         {
    2965           10 :           if (formal->attr.artificial)
    2966            3 :             gfc_error ("Element of assumed-shape or pointer array "
    2967              :                        "as actual argument at %L cannot correspond to "
    2968              :                        "actual argument at %L",
    2969              :                        &actual->where, &formal->declared_at);
    2970              :           else
    2971            7 :             gfc_error ("Element of assumed-shape or pointer "
    2972              :                        "array passed to array dummy argument %qs at %L",
    2973              :                        formal->name, &actual->where);
    2974              :         }
    2975           10 :       return false;
    2976              :     }
    2977              : 
    2978          778 :   if (actual->ts.type == BT_CHARACTER && actual->expr_type != EXPR_NULL
    2979          280 :       && (!ref || is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
    2980              :     {
    2981          263 :       if (formal->ts.kind != 1 && (gfc_option.allow_std & GFC_STD_GNU) == 0)
    2982              :         {
    2983            0 :           if (where)
    2984            0 :             gfc_error ("Extension: Scalar non-default-kind, non-C_CHAR-kind "
    2985              :                        "CHARACTER actual argument with array dummy argument "
    2986              :                        "%qs at %L", formal->name, &actual->where);
    2987            0 :           return false;
    2988              :         }
    2989              : 
    2990          263 :       if (where && (gfc_option.allow_std & GFC_STD_F2003) == 0)
    2991              :         {
    2992           50 :           gfc_error ("Fortran 2003: Scalar CHARACTER actual argument with "
    2993              :                      "array dummy argument %qs at %L",
    2994              :                      formal->name, &actual->where);
    2995           50 :           return false;
    2996              :         }
    2997              :       else
    2998          213 :         return ((gfc_option.allow_std & GFC_STD_F2003) != 0);
    2999              :     }
    3000              : 
    3001          498 :   if (ref == NULL && actual->expr_type != EXPR_NULL)
    3002              :     {
    3003           53 :       if (actual->rank == 0
    3004           53 :           && formal->ts.type == BT_ASSUMED
    3005            3 :           && formal->as
    3006            3 :           && formal->as->type == AS_ASSUMED_SIZE)
    3007              :         /* This is new in F2018, type(*) is new in TS29113, but gfortran does
    3008              :            not differentiate.  Thus, if type(*) exists, it is valid;
    3009              :            otherwise, type(*) is already rejected.  */
    3010              :         return true;
    3011           50 :       if (where
    3012           50 :           && (!formal->attr.artificial || (!formal->maybe_array
    3013            3 :                                            && !maybe_dummy_array_arg (actual))))
    3014              :         {
    3015           49 :           locus *where_formal;
    3016           49 :           if (formal->attr.artificial)
    3017            2 :             where_formal = &formal->declared_at;
    3018              :           else
    3019              :             where_formal = NULL;
    3020              : 
    3021           49 :           argument_rank_mismatch (formal->name, &actual->where,
    3022              :                                   gfc_symbol_rank (formal), actual->rank,
    3023              :                                   where_formal);
    3024              :         }
    3025           50 :       return false;
    3026              :     }
    3027              : 
    3028              :   return true;
    3029              : }
    3030              : 
    3031              : 
    3032              : /* Returns the storage size of a symbol (formal argument) or sets argument
    3033              :    size_known to false if it cannot be determined.  */
    3034              : 
    3035              : static unsigned long
    3036       244182 : get_sym_storage_size (gfc_symbol *sym, bool *size_known)
    3037              : {
    3038       244182 :   int i;
    3039       244182 :   unsigned long strlen, elements;
    3040              : 
    3041       244182 :   *size_known = false;
    3042              : 
    3043       244182 :   if (sym->ts.type == BT_CHARACTER)
    3044              :     {
    3045        33735 :       if (sym->ts.u.cl && sym->ts.u.cl->length
    3046         7170 :           && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
    3047         6183 :           && sym->ts.u.cl->length->ts.type == BT_INTEGER)
    3048         6181 :         strlen = mpz_get_ui (sym->ts.u.cl->length->value.integer);
    3049              :       else
    3050              :         return 0;
    3051              :     }
    3052              :   else
    3053              :     strlen = 1;
    3054              : 
    3055       216628 :   if (gfc_symbol_rank (sym) == 0)
    3056              :     {
    3057       183206 :       *size_known = true;
    3058       183206 :       return strlen;
    3059              :     }
    3060              : 
    3061        33422 :   elements = 1;
    3062        33422 :   if (sym->as->type != AS_EXPLICIT)
    3063              :     return 0;
    3064        14736 :   for (i = 0; i < sym->as->rank; i++)
    3065              :     {
    3066         9708 :       if (sym->as->upper[i]->expr_type != EXPR_CONSTANT
    3067         6530 :           || sym->as->lower[i]->expr_type != EXPR_CONSTANT
    3068         6530 :           || sym->as->upper[i]->ts.type != BT_INTEGER
    3069         6529 :           || sym->as->lower[i]->ts.type != BT_INTEGER)
    3070              :         return 0;
    3071              : 
    3072         6527 :       elements *= mpz_get_si (sym->as->upper[i]->value.integer)
    3073         6527 :                   - mpz_get_si (sym->as->lower[i]->value.integer) + 1L;
    3074              :     }
    3075              : 
    3076         5028 :   *size_known = true;
    3077              : 
    3078         5028 :   return strlen*elements;
    3079              : }
    3080              : 
    3081              : 
    3082              : /* Returns the storage size of an expression (actual argument) or sets argument
    3083              :    size_known to false if it cannot be determined.  For an array element, it
    3084              :    returns the remaining size as the element sequence consists of all storage
    3085              :    units of the actual argument up to the end of the array.  */
    3086              : 
    3087              : static unsigned long
    3088       244182 : get_expr_storage_size (gfc_expr *e, bool *size_known, long int *charlen)
    3089              : {
    3090       244182 :   int i;
    3091       244182 :   long int strlen, elements;
    3092       244182 :   long int substrlen = 0;
    3093       244182 :   bool is_str_storage = false;
    3094       244182 :   gfc_ref *ref;
    3095              : 
    3096       244182 :   *size_known = false;
    3097       244182 :   *charlen = -1;
    3098              : 
    3099       244182 :   if (e == NULL)
    3100              :     return 0;
    3101              : 
    3102       244182 :   if (e->ts.type == BT_CHARACTER)
    3103              :     {
    3104        34128 :       if (e->ts.u.cl && e->ts.u.cl->length
    3105        11592 :           && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
    3106        10783 :           && e->ts.u.cl->length->ts.type == BT_INTEGER)
    3107        10782 :         strlen = mpz_get_si (e->ts.u.cl->length->value.integer);
    3108        23346 :       else if (e->expr_type == EXPR_CONSTANT
    3109        19647 :                && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
    3110        19647 :         strlen = e->value.character.length;
    3111              :       else
    3112              :         return 0;
    3113        30429 :       *charlen = strlen;
    3114              :     }
    3115              :   else
    3116              :     strlen = 1; /* Length per element.  */
    3117              : 
    3118       240483 :   if (e->rank == 0 && !e->ref)
    3119              :     {
    3120       195344 :       *size_known = true;
    3121       195344 :       return strlen;
    3122              :     }
    3123              : 
    3124        45139 :   elements = 1;
    3125        45139 :   if (!e->ref)
    3126              :     {
    3127         6704 :       if (!e->shape)
    3128              :         return 0;
    3129        12279 :       for (i = 0; i < e->rank; i++)
    3130         6687 :         elements *= mpz_get_si (e->shape[i]);
    3131         5592 :       {
    3132         5592 :         *size_known = true;
    3133         5592 :         return elements*strlen;
    3134              :       }
    3135              :     }
    3136              : 
    3137        63092 :   for (ref = e->ref; ref; ref = ref->next)
    3138              :     {
    3139        39929 :       if (ref->type == REF_SUBSTRING && ref->u.ss.start
    3140           64 :           && ref->u.ss.start->expr_type == EXPR_CONSTANT)
    3141              :         {
    3142           58 :           if (is_str_storage)
    3143              :             {
    3144              :               /* The string length is the substring length.
    3145              :                  Set now to full string length.  */
    3146            5 :               if (!ref->u.ss.length || !ref->u.ss.length->length
    3147            4 :                   || ref->u.ss.length->length->expr_type != EXPR_CONSTANT)
    3148              :                 return 0;
    3149              : 
    3150            4 :               strlen = mpz_get_ui (ref->u.ss.length->length->value.integer);
    3151              :             }
    3152           57 :           substrlen = strlen - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
    3153           57 :           continue;
    3154              :         }
    3155              : 
    3156        39871 :       if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
    3157        11542 :         for (i = 0; i < ref->u.ar.dimen; i++)
    3158              :           {
    3159         7104 :             long int start, end, stride;
    3160         7104 :             stride = 1;
    3161              : 
    3162         7104 :             if (ref->u.ar.stride[i])
    3163              :               {
    3164         2784 :                 if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT
    3165         2621 :                     && ref->u.ar.stride[i]->ts.type == BT_INTEGER)
    3166         2621 :                   stride = mpz_get_si (ref->u.ar.stride[i]->value.integer);
    3167              :                 else
    3168              :                   return 0;
    3169              :               }
    3170              : 
    3171         6941 :             if (ref->u.ar.start[i])
    3172              :               {
    3173         3995 :                 if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT
    3174         3600 :                     && ref->u.ar.start[i]->ts.type == BT_INTEGER)
    3175         3600 :                   start = mpz_get_si (ref->u.ar.start[i]->value.integer);
    3176              :                 else
    3177              :                   return 0;
    3178              :               }
    3179         2946 :             else if (ref->u.ar.as->lower[i]
    3180         2650 :                      && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
    3181         2650 :                      && ref->u.ar.as->lower[i]->ts.type == BT_INTEGER)
    3182         2650 :               start = mpz_get_si (ref->u.ar.as->lower[i]->value.integer);
    3183              :             else
    3184              :               return 0;
    3185              : 
    3186         6250 :             if (ref->u.ar.end[i])
    3187              :               {
    3188         4879 :                 if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT
    3189         4760 :                     && ref->u.ar.end[i]->ts.type == BT_INTEGER)
    3190         4760 :                   end = mpz_get_si (ref->u.ar.end[i]->value.integer);
    3191              :                 else
    3192              :                   return 0;
    3193              :               }
    3194         1371 :             else if (ref->u.ar.as->upper[i]
    3195         1117 :                      && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
    3196         1083 :                      && ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
    3197         1082 :               end = mpz_get_si (ref->u.ar.as->upper[i]->value.integer);
    3198              :             else
    3199              :               return 0;
    3200              : 
    3201         5842 :             elements *= (end - start)/stride + 1L;
    3202              :           }
    3203        34171 :       else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL)
    3204        49398 :         for (i = 0; i < ref->u.ar.as->rank; i++)
    3205              :           {
    3206        33278 :             if (ref->u.ar.as->lower[i] && ref->u.ar.as->upper[i]
    3207        23351 :                 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
    3208        23302 :                 && ref->u.ar.as->lower[i]->ts.type == BT_INTEGER
    3209        23302 :                 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
    3210        21672 :                 && ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
    3211        21672 :               elements *= mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
    3212        21672 :                           - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
    3213        21672 :                           + 1L;
    3214              :             else
    3215              :               return 0;
    3216              :           }
    3217         6445 :       else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
    3218         4059 :                && e->expr_type == EXPR_VARIABLE)
    3219              :         {
    3220         4059 :           if (ref->u.ar.as->type == AS_ASSUMED_SHAPE
    3221         3860 :               || e->symtree->n.sym->attr.pointer)
    3222              :             {
    3223          240 :               elements = 1;
    3224          240 :               continue;
    3225              :             }
    3226              : 
    3227              :           /* Determine the number of remaining elements in the element
    3228              :              sequence for array element designators.  */
    3229         3819 :           is_str_storage = true;
    3230         5334 :           for (i = ref->u.ar.dimen - 1; i >= 0; i--)
    3231              :             {
    3232         3917 :               if (ref->u.ar.start[i] == NULL
    3233         3917 :                   || ref->u.ar.start[i]->expr_type != EXPR_CONSTANT
    3234         2113 :                   || ref->u.ar.as->upper[i] == NULL
    3235         1542 :                   || ref->u.ar.as->lower[i] == NULL
    3236         1542 :                   || ref->u.ar.as->upper[i]->expr_type != EXPR_CONSTANT
    3237         1515 :                   || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT
    3238         1515 :                   || ref->u.ar.as->upper[i]->ts.type != BT_INTEGER
    3239         1515 :                   || ref->u.ar.as->lower[i]->ts.type != BT_INTEGER)
    3240              :                 return 0;
    3241              : 
    3242         1515 :               elements
    3243         1515 :                    = elements
    3244         1515 :                      * (mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
    3245         1515 :                         - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
    3246         1515 :                         + 1L)
    3247         1515 :                      - (mpz_get_si (ref->u.ar.start[i]->value.integer)
    3248         1515 :                         - mpz_get_si (ref->u.ar.as->lower[i]->value.integer));
    3249              :             }
    3250              :         }
    3251         2386 :       else if (ref->type == REF_COMPONENT && ref->u.c.component->attr.function
    3252           91 :                && ref->u.c.component->attr.proc_pointer
    3253           91 :                && ref->u.c.component->attr.dimension)
    3254              :         {
    3255              :           /* Array-valued procedure-pointer components.  */
    3256            8 :           gfc_array_spec *as = ref->u.c.component->as;
    3257           15 :           for (i = 0; i < as->rank; i++)
    3258              :             {
    3259            8 :               if (!as->upper[i] || !as->lower[i]
    3260            8 :                   || as->upper[i]->expr_type != EXPR_CONSTANT
    3261            7 :                   || as->lower[i]->expr_type != EXPR_CONSTANT
    3262            7 :                   || as->upper[i]->ts.type != BT_INTEGER
    3263            7 :                   || as->lower[i]->ts.type != BT_INTEGER)
    3264              :                 return 0;
    3265              : 
    3266            7 :               elements = elements
    3267            7 :                          * (mpz_get_si (as->upper[i]->value.integer)
    3268            7 :                             - mpz_get_si (as->lower[i]->value.integer) + 1L);
    3269              :             }
    3270              :         }
    3271              :     }
    3272              : 
    3273        23163 :   *size_known = true;
    3274              : 
    3275        23163 :   if (substrlen)
    3276           51 :     return (is_str_storage) ? substrlen + (elements-1)*strlen
    3277           51 :                             : elements*strlen;
    3278              :   else
    3279        23112 :     return elements*strlen;
    3280              : }
    3281              : 
    3282              : 
    3283              : /* Given an expression, check whether it is an array section
    3284              :    which has a vector subscript.  */
    3285              : 
    3286              : bool
    3287        14159 : gfc_has_vector_subscript (gfc_expr *e)
    3288              : {
    3289        14159 :   int i;
    3290        14159 :   gfc_ref *ref;
    3291              : 
    3292        14159 :   if (e == NULL || e->rank == 0 || e->expr_type != EXPR_VARIABLE)
    3293              :     return false;
    3294              : 
    3295        13503 :   for (ref = e->ref; ref; ref = ref->next)
    3296         7808 :     if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
    3297         1067 :       for (i = 0; i < ref->u.ar.dimen; i++)
    3298          635 :         if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    3299              :           return true;
    3300              : 
    3301              :   return false;
    3302              : }
    3303              : 
    3304              : 
    3305              : static bool
    3306           27 : is_procptr_result (gfc_expr *expr)
    3307              : {
    3308           27 :   gfc_component *c = gfc_get_proc_ptr_comp (expr);
    3309           27 :   if (c)
    3310            2 :     return (c->ts.interface && (c->ts.interface->attr.proc_pointer == 1));
    3311              :   else
    3312           26 :     return ((expr->symtree->n.sym->result != expr->symtree->n.sym)
    3313           28 :             && (expr->symtree->n.sym->result->attr.proc_pointer == 1));
    3314              : }
    3315              : 
    3316              : 
    3317              : /* Recursively append candidate argument ARG to CANDIDATES.  Store the
    3318              :    number of total candidates in CANDIDATES_LEN.  */
    3319              : 
    3320              : static void
    3321            1 : lookup_arg_fuzzy_find_candidates (gfc_formal_arglist *arg,
    3322              :                                   char **&candidates,
    3323              :                                   size_t &candidates_len)
    3324              : {
    3325            2 :   for (gfc_formal_arglist *p = arg; p && p->sym; p = p->next)
    3326            1 :     vec_push (candidates, candidates_len, p->sym->name);
    3327            1 : }
    3328              : 
    3329              : 
    3330              : /* Lookup argument ARG fuzzily, taking names in ARGUMENTS into account.  */
    3331              : 
    3332              : static const char*
    3333            1 : lookup_arg_fuzzy (const char *arg, gfc_formal_arglist *arguments)
    3334              : {
    3335            1 :   char **candidates = NULL;
    3336            1 :   size_t candidates_len = 0;
    3337            1 :   lookup_arg_fuzzy_find_candidates (arguments, candidates, candidates_len);
    3338            1 :   return gfc_closest_fuzzy_match (arg, candidates);
    3339              : }
    3340              : 
    3341              : 
    3342              : static gfc_dummy_arg *
    3343       370621 : get_nonintrinsic_dummy_arg (gfc_formal_arglist *formal)
    3344              : {
    3345            0 :   gfc_dummy_arg * const dummy_arg = gfc_get_dummy_arg ();
    3346              : 
    3347       370621 :   dummy_arg->intrinsicness = GFC_NON_INTRINSIC_DUMMY_ARG;
    3348       370621 :   dummy_arg->u.non_intrinsic = formal;
    3349              : 
    3350       370621 :   return dummy_arg;
    3351              : }
    3352              : 
    3353              : 
    3354              : /* Given formal and actual argument lists, see if they are compatible.
    3355              :    If they are compatible, the actual argument list is sorted to
    3356              :    correspond with the formal list, and elements for missing optional
    3357              :    arguments are inserted. If WHERE pointer is nonnull, then we issue
    3358              :    errors when things don't match instead of just returning the status
    3359              :    code.  */
    3360              : 
    3361              : bool
    3362       196664 : gfc_compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
    3363              :                            int ranks_must_agree, int is_elemental,
    3364              :                            bool in_statement_function, locus *where)
    3365              : {
    3366       196664 :   gfc_actual_arglist **new_arg, *a, *actual;
    3367       196664 :   gfc_formal_arglist *f;
    3368       196664 :   int i, n, na;
    3369       196664 :   unsigned long actual_size, formal_size;
    3370       196664 :   long int charlen;
    3371       196664 :   bool full_array = false;
    3372       196664 :   gfc_array_ref *actual_arr_ref;
    3373       196664 :   gfc_array_spec *fas, *aas;
    3374       196664 :   bool pointer_dummy, pointer_arg, allocatable_arg;
    3375       196664 :   bool procptr_dummy, optional_dummy, allocatable_dummy;
    3376       196664 :   bool actual_size_known = false;
    3377       196664 :   bool formal_size_known = false;
    3378       196664 :   bool ok = true;
    3379              : 
    3380       196664 :   actual = *ap;
    3381              : 
    3382       196664 :   if (actual == NULL && formal == NULL)
    3383              :     return true;
    3384              : 
    3385              :   n = 0;
    3386       549734 :   for (f = formal; f; f = f->next)
    3387       371038 :     n++;
    3388              : 
    3389       178696 :   new_arg = XALLOCAVEC (gfc_actual_arglist *, n);
    3390              : 
    3391       549734 :   for (i = 0; i < n; i++)
    3392       371038 :     new_arg[i] = NULL;
    3393              : 
    3394              :   na = 0;
    3395              :   f = formal;
    3396              :   i = 0;
    3397              : 
    3398       543945 :   for (a = actual; a; a = a->next, f = f->next)
    3399              :     {
    3400       366450 :       if (a->name != NULL && in_statement_function)
    3401              :         {
    3402            1 :           gfc_error ("Keyword argument %qs at %L is invalid in "
    3403            1 :                      "a statement function", a->name, &a->expr->where);
    3404            1 :           return false;
    3405              :         }
    3406              : 
    3407              :       /* Look for keywords but ignore g77 extensions like %VAL.  */
    3408       366449 :       if (a->name != NULL && a->name[0] != '%')
    3409              :         {
    3410              :           i = 0;
    3411        12205 :           for (f = formal; f; f = f->next, i++)
    3412              :             {
    3413        12171 :               if (f->sym == NULL)
    3414            0 :                 continue;
    3415        12171 :               if (strcmp (f->sym->name, a->name) == 0)
    3416              :                 break;
    3417              :             }
    3418              : 
    3419         3518 :           if (f == NULL)
    3420              :             {
    3421           34 :               if (where)
    3422              :                 {
    3423            1 :                   const char *guessed = lookup_arg_fuzzy (a->name, formal);
    3424            1 :                   if (guessed)
    3425            1 :                     gfc_error ("Keyword argument %qs at %L is not in "
    3426              :                                "the procedure; did you mean %qs?",
    3427            1 :                                a->name, &a->expr->where, guessed);
    3428              :                   else
    3429            0 :                     gfc_error ("Keyword argument %qs at %L is not in "
    3430            0 :                                "the procedure", a->name, &a->expr->where);
    3431              :                 }
    3432           34 :               return false;
    3433              :             }
    3434              : 
    3435         3518 :           if (new_arg[i] != NULL)
    3436              :             {
    3437            0 :               if (where)
    3438            0 :                 gfc_error ("Keyword argument %qs at %L is already associated "
    3439              :                            "with another actual argument", a->name,
    3440            0 :                            &a->expr->where);
    3441            0 :               return false;
    3442              :             }
    3443              :         }
    3444              : 
    3445       366415 :       if (f == NULL)
    3446              :         {
    3447         1158 :           if (where)
    3448            8 :             gfc_error ("More actual than formal arguments in procedure "
    3449              :                        "call at %L", where);
    3450         1158 :           return false;
    3451              :         }
    3452              : 
    3453       365257 :       if (f->sym == NULL && a->expr == NULL)
    3454          210 :         goto match;
    3455              : 
    3456       365047 :       if (f->sym == NULL)
    3457              :         {
    3458              :           /* These errors have to be issued, otherwise an ICE can occur.
    3459              :              See PR 78865.  */
    3460            6 :           if (where)
    3461            6 :             gfc_error_now ("Missing alternate return specifier in subroutine "
    3462              :                            "call at %L", where);
    3463            6 :           return false;
    3464              :         }
    3465              :       else
    3466              :         {
    3467       365041 :           if (a->associated_dummy)
    3468       124375 :             free (a->associated_dummy);
    3469       365041 :           a->associated_dummy = get_nonintrinsic_dummy_arg (f);
    3470              :         }
    3471              : 
    3472       365041 :       if (a->expr == NULL)
    3473              :         {
    3474            8 :           if (f->sym->attr.optional)
    3475            6 :             continue;
    3476              :           else
    3477              :             {
    3478            2 :               if (where)
    3479            1 :                 gfc_error_now ("Unexpected alternate return specifier in "
    3480              :                                "subroutine call at %L", where);
    3481            2 :               return false;
    3482              :             }
    3483              :         }
    3484              : 
    3485              :       /* Make sure that intrinsic vtables exist for calls to unlimited
    3486              :          polymorphic formal arguments.  */
    3487       365033 :       if (UNLIMITED_POLY (f->sym)
    3488         2879 :           && a->expr->ts.type != BT_DERIVED
    3489              :           && a->expr->ts.type != BT_CLASS
    3490              :           && a->expr->ts.type != BT_ASSUMED)
    3491          929 :         gfc_find_vtab (&a->expr->ts);
    3492              : 
    3493              :       /* Interp J3/22-146:
    3494              :          "If the context of the reference to NULL is an <actual argument>
    3495              :          corresponding to an <assumed-rank> dummy argument, MOLD shall be
    3496              :          present."  */
    3497       365033 :       if (a->expr->expr_type == EXPR_NULL
    3498          826 :           && a->expr->ts.type == BT_UNKNOWN
    3499          264 :           && f->sym->as
    3500           97 :           && f->sym->as->type == AS_ASSUMED_RANK)
    3501              :         {
    3502            1 :           gfc_error ("Intrinsic %<NULL()%> without %<MOLD%> argument at %L "
    3503              :                      "passed to assumed-rank dummy %qs",
    3504              :                      &a->expr->where, f->sym->name);
    3505            1 :           ok = false;
    3506            1 :           goto match;
    3507              :         }
    3508              : 
    3509       365032 :       if (warn_surprising
    3510         1281 :           && a->expr->expr_type == EXPR_VARIABLE
    3511          620 :           && a->expr->symtree->n.sym->as
    3512          265 :           && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SIZE
    3513          153 :           && f->sym->as
    3514          153 :           && f->sym->as->type == AS_ASSUMED_RANK)
    3515            1 :         gfc_warning (0, "The assumed-size dummy %qs is being passed at %L to "
    3516              :                      "an assumed-rank dummy %qs", a->expr->symtree->name,
    3517              :                      &a->expr->where, f->sym->name);
    3518              : 
    3519       365032 :       if (a->expr->expr_type == EXPR_NULL
    3520          825 :           && a->expr->ts.type == BT_UNKNOWN
    3521          263 :           && f->sym->ts.type == BT_CHARACTER
    3522           83 :           && !f->sym->ts.deferred
    3523           46 :           && f->sym->ts.u.cl
    3524           46 :           && f->sym->ts.u.cl->length == NULL)
    3525              :         {
    3526            1 :           gfc_error ("Intrinsic %<NULL()%> without %<MOLD%> argument at %L "
    3527              :                      "passed to assumed-length dummy %qs",
    3528              :                      &a->expr->where, f->sym->name);
    3529            1 :           ok = false;
    3530            1 :           goto match;
    3531              :         }
    3532              : 
    3533              :       /* Allow passing of NULL() as disassociated pointer, procedure
    3534              :          pointer, or unallocated allocatable (F2008+) to a respective dummy
    3535              :          argument.  */
    3536       730062 :       pointer_dummy = ((f->sym->ts.type != BT_CLASS
    3537       349984 :                         && f->sym->attr.pointer)
    3538       709653 :                        || (f->sym->ts.type == BT_CLASS
    3539        15047 :                            && CLASS_DATA (f->sym)->attr.class_pointer));
    3540              : 
    3541       730062 :       procptr_dummy = ((f->sym->ts.type != BT_CLASS
    3542       349984 :                         && f->sym->attr.proc_pointer)
    3543       714803 :                        || (f->sym->ts.type == BT_CLASS
    3544        15047 :                            && CLASS_DATA (f->sym)->attr.proc_pointer));
    3545              : 
    3546       365031 :       optional_dummy = f->sym->attr.optional;
    3547              : 
    3548       730062 :       allocatable_dummy = ((f->sym->ts.type != BT_CLASS
    3549       349984 :                             && f->sym->attr.allocatable)
    3550       711766 :                            || (f->sym->ts.type == BT_CLASS
    3551        15047 :                                && CLASS_DATA (f->sym)->attr.allocatable));
    3552              : 
    3553       365031 :       if (a->expr->expr_type == EXPR_NULL
    3554              :           && !pointer_dummy
    3555          824 :           && !procptr_dummy
    3556          338 :           && !(optional_dummy
    3557          287 :                && (gfc_option.allow_std & GFC_STD_F2008) != 0)
    3558           54 :           && !(allocatable_dummy
    3559           50 :                && (gfc_option.allow_std & GFC_STD_F2008) != 0))
    3560              :         {
    3561            5 :           if (where
    3562            4 :               && (!f->sym->attr.optional
    3563            2 :                   || (f->sym->ts.type != BT_CLASS && f->sym->attr.allocatable)
    3564            1 :                   || (f->sym->ts.type == BT_CLASS
    3565            0 :                          && CLASS_DATA (f->sym)->attr.allocatable)))
    3566            3 :             gfc_error ("Unexpected NULL() intrinsic at %L to dummy %qs",
    3567              :                        where, f->sym->name);
    3568            1 :           else if (where)
    3569            1 :             gfc_error ("Fortran 2008: Null pointer at %L to non-pointer "
    3570              :                        "dummy %qs", where, f->sym->name);
    3571            5 :           ok = false;
    3572            5 :           goto match;
    3573              :         }
    3574              : 
    3575       365026 :       if (!compare_parameter (f->sym, a->expr, ranks_must_agree,
    3576              :                               is_elemental, where))
    3577              :         {
    3578       106303 :           ok = false;
    3579       106303 :           goto match;
    3580              :         }
    3581              : 
    3582              :       /* TS 29113, 6.3p2; F2018 15.5.2.4.  */
    3583       258723 :       if (f->sym->ts.type == BT_ASSUMED
    3584         3473 :           && (a->expr->ts.type == BT_DERIVED
    3585         3029 :               || (a->expr->ts.type == BT_CLASS && CLASS_DATA (a->expr))))
    3586              :         {
    3587          651 :           gfc_symbol *derived = (a->expr->ts.type == BT_DERIVED
    3588              :                                  ? a->expr->ts.u.derived
    3589          207 :                                  : CLASS_DATA (a->expr)->ts.u.derived);
    3590          651 :           gfc_namespace *f2k_derived = derived->f2k_derived;
    3591          651 :           if (derived->attr.pdt_type
    3592          650 :               || (f2k_derived
    3593          585 :                   && (f2k_derived->finalizers || f2k_derived->tb_sym_root)))
    3594              :             {
    3595            5 :               gfc_error ("Actual argument at %L to assumed-type dummy "
    3596              :                          "has type parameters or is of "
    3597              :                          "derived type with type-bound or FINAL procedures",
    3598              :                          &a->expr->where);
    3599            5 :               ok = false;
    3600            5 :               goto match;
    3601              :             }
    3602              :         }
    3603              : 
    3604       258718 :       if (UNLIMITED_POLY (a->expr)
    3605         1231 :           && !(f->sym->ts.type == BT_ASSUMED || UNLIMITED_POLY (f->sym)))
    3606              :         {
    3607            1 :           gfc_error ("Unlimited polymorphic actual argument at %L is not "
    3608              :                      "matched with either an unlimited polymorphic or "
    3609              :                      "assumed type dummy argument", &a->expr->where);
    3610            1 :           ok = false;
    3611            1 :           goto match;
    3612              :         }
    3613              : 
    3614              :       /* Special case for character arguments.  For allocatable, pointer
    3615              :          and assumed-shape dummies, the string length needs to match
    3616              :          exactly.  */
    3617       258717 :       if (a->expr->ts.type == BT_CHARACTER
    3618        34321 :           && a->expr->ts.u.cl && a->expr->ts.u.cl->length
    3619        11732 :           && a->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
    3620        10923 :           && a->expr->ts.u.cl->length->ts.type == BT_INTEGER
    3621        10922 :           && f->sym->ts.type == BT_CHARACTER && f->sym->ts.u.cl
    3622        10591 :           && f->sym->ts.u.cl->length
    3623         5572 :           && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
    3624         4719 :           && f->sym->ts.u.cl->length->ts.type == BT_INTEGER
    3625         4717 :           && (f->sym->attr.pointer || f->sym->attr.allocatable
    3626         4307 :               || (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
    3627         1020 :           && (mpz_cmp (a->expr->ts.u.cl->length->value.integer,
    3628         1020 :                        f->sym->ts.u.cl->length->value.integer) != 0))
    3629              :         {
    3630           14 :           long actual_len, formal_len;
    3631           14 :           actual_len = mpz_get_si (a->expr->ts.u.cl->length->value.integer);
    3632           14 :           formal_len = mpz_get_si (f->sym->ts.u.cl->length->value.integer);
    3633              : 
    3634           14 :           if (where && (f->sym->attr.pointer || f->sym->attr.allocatable))
    3635              :             {
    3636              :               /* Emit a warning for -std=legacy and an error otherwise. */
    3637            5 :               if (gfc_option.warn_std == 0)
    3638            4 :                 gfc_warning (0, "Character length mismatch (%ld/%ld) between "
    3639              :                              "actual argument and pointer or allocatable "
    3640              :                              "dummy argument %qs at %L", actual_len, formal_len,
    3641              :                              f->sym->name, &a->expr->where);
    3642              :               else
    3643            1 :                 gfc_error ("Character length mismatch (%ld/%ld) between "
    3644              :                            "actual argument and pointer or allocatable "
    3645              :                            "dummy argument %qs at %L", actual_len, formal_len,
    3646              :                            f->sym->name, &a->expr->where);
    3647              :             }
    3648            9 :           else if (where)
    3649              :             {
    3650              :               /* Emit a warning for -std=legacy and an error otherwise. */
    3651            9 :               if (gfc_option.warn_std == 0)
    3652            0 :                 gfc_warning (0, "Character length mismatch (%ld/%ld) between "
    3653              :                              "actual argument and assumed-shape dummy argument "
    3654              :                              "%qs at %L", actual_len, formal_len,
    3655              :                              f->sym->name, &a->expr->where);
    3656              :               else
    3657            9 :                 gfc_error ("Character length mismatch (%ld/%ld) between "
    3658              :                            "actual argument and assumed-shape dummy argument "
    3659              :                            "%qs at %L", actual_len, formal_len,
    3660              :                            f->sym->name, &a->expr->where);
    3661              : 
    3662              :             }
    3663           14 :           ok = false;
    3664           14 :           goto match;
    3665              :         }
    3666              : 
    3667       258703 :       if ((f->sym->attr.pointer || f->sym->attr.allocatable)
    3668         8441 :           && f->sym->ts.deferred != a->expr->ts.deferred
    3669           38 :           && a->expr->ts.type == BT_CHARACTER)
    3670              :         {
    3671            1 :           if (where)
    3672            1 :             gfc_error ("Actual argument at %L to allocatable or "
    3673              :                        "pointer dummy argument %qs must have a deferred "
    3674              :                        "length type parameter if and only if the dummy has one",
    3675              :                        &a->expr->where, f->sym->name);
    3676            1 :           ok = false;
    3677            1 :           goto match;
    3678              :         }
    3679              : 
    3680       258702 :       if (f->sym->ts.type == BT_CLASS)
    3681        14263 :         goto skip_size_check;
    3682              : 
    3683              :       /* Skip size check for NULL() actual without MOLD argument.  */
    3684       244439 :       if (a->expr->expr_type == EXPR_NULL && a->expr->ts.type == BT_UNKNOWN)
    3685          257 :         goto skip_size_check;
    3686              : 
    3687       244182 :       actual_size = get_expr_storage_size (a->expr, &actual_size_known, &charlen);
    3688       244182 :       formal_size = get_sym_storage_size (f->sym, &formal_size_known);
    3689              : 
    3690              :       /* If the formal is a scalar character variable, use the charlen of the
    3691              :          actual.  */
    3692       244182 :       if (actual_size_known && formal_size_known && charlen >= 0
    3693         4264 :           && a->expr->ts.type == BT_CHARACTER
    3694         4264 :           && f->sym->attr.flavor != FL_PROCEDURE
    3695         4264 :           && !f->sym->attr.dimension)
    3696         3717 :         actual_size = charlen;
    3697              : 
    3698       244182 :       if (actual_size_known && formal_size_known
    3699       183581 :           && actual_size != formal_size
    3700         3904 :           && a->expr->ts.type == BT_CHARACTER
    3701          256 :           && f->sym->attr.flavor != FL_PROCEDURE)
    3702              :         {
    3703              :           /* F2018:15.5.2.4:
    3704              :              (3) "The length type parameter values of a present actual argument
    3705              :              shall agree with the corresponding ones of the dummy argument that
    3706              :              are not assumed, except for the case of the character length
    3707              :              parameter of an actual argument of type character with default
    3708              :              kind or C character kind associated with a dummy argument that is
    3709              :              not assumed-shape or assumed-rank."
    3710              : 
    3711              :              (4) "If a present scalar dummy argument is of type character with
    3712              :              default kind or C character kind, the length len of the dummy
    3713              :              argument shall be less than or equal to the length of the actual
    3714              :              argument.  The dummy argument becomes associated with the leftmost
    3715              :              len characters of the actual argument.  If a present array dummy
    3716              :              argument is of type character with default kind or C character
    3717              :              kind and is not assumed-shape or assumed-rank, it becomes
    3718              :              associated with the leftmost characters of the actual argument
    3719              :              element sequence."
    3720              : 
    3721              :              As an extension we treat kind=4 character similarly to kind=1.  */
    3722              : 
    3723          256 :           if (actual_size > formal_size)
    3724              :             {
    3725          175 :               if (a->expr->ts.type == BT_CHARACTER && where
    3726          175 :                   && (!f->sym->as || f->sym->as->type == AS_EXPLICIT))
    3727          175 :                 gfc_warning (OPT_Wcharacter_truncation,
    3728              :                              "Character length of actual argument longer "
    3729              :                              "than of dummy argument %qs (%lu/%lu) at %L",
    3730              :                              f->sym->name, actual_size, formal_size,
    3731              :                              &a->expr->where);
    3732          175 :               goto skip_size_check;
    3733              :             }
    3734              : 
    3735           81 :           if (a->expr->ts.type == BT_CHARACTER && where && !f->sym->as)
    3736              :             {
    3737              :               /* Emit warning for -std=legacy/gnu and an error otherwise. */
    3738           57 :               if (gfc_notification_std (GFC_STD_LEGACY) == ERROR)
    3739              :                 {
    3740           11 :                   gfc_error ("Character length of actual argument shorter "
    3741              :                              "than of dummy argument %qs (%lu/%lu) at %L",
    3742           11 :                              f->sym->name, actual_size, formal_size,
    3743           11 :                              &a->expr->where);
    3744           11 :                   ok = false;
    3745           11 :                   goto match;
    3746              :                 }
    3747              :               else
    3748           46 :                 gfc_warning (0, "Character length of actual argument shorter "
    3749              :                              "than of dummy argument %qs (%lu/%lu) at %L",
    3750           46 :                              f->sym->name, actual_size, formal_size,
    3751           46 :                              &a->expr->where);
    3752           46 :               goto skip_size_check;
    3753              :             }
    3754              :         }
    3755              : 
    3756       243950 :       if (actual_size_known && formal_size_known
    3757       183349 :           && actual_size < formal_size
    3758           54 :           && f->sym->as
    3759           48 :           && a->expr->ts.type != BT_PROCEDURE
    3760           48 :           && f->sym->attr.flavor != FL_PROCEDURE)
    3761              :         {
    3762           48 :           if (where)
    3763              :             {
    3764              :               /* Emit a warning for -std=legacy and an error otherwise. */
    3765           48 :               if (gfc_option.warn_std == 0)
    3766            0 :                 gfc_warning (0, "Actual argument contains too few "
    3767              :                              "elements for dummy argument %qs (%lu/%lu) "
    3768              :                              "at %L", f->sym->name, actual_size,
    3769              :                              formal_size, &a->expr->where);
    3770              :               else
    3771           48 :                 gfc_error_now ("Actual argument contains too few "
    3772              :                                "elements for dummy argument %qs (%lu/%lu) "
    3773              :                                "at %L", f->sym->name, actual_size,
    3774              :                                formal_size, &a->expr->where);
    3775              :             }
    3776           48 :           ok = false;
    3777           48 :           goto match;
    3778              :         }
    3779              : 
    3780       243902 :      skip_size_check:
    3781              : 
    3782              :       /* Satisfy either: F03:12.4.1.3 by ensuring that a procedure pointer
    3783              :          actual argument is provided for a procedure pointer formal argument;
    3784              :          or: F08:12.5.2.9 (F18:15.5.2.10) by ensuring that the effective
    3785              :          argument shall be an external, internal, module, or dummy procedure.
    3786              :          The interfaces are checked elsewhere.  */
    3787       258643 :       if (f->sym->attr.proc_pointer
    3788       258643 :           && !((a->expr->expr_type == EXPR_VARIABLE
    3789          200 :                 && (a->expr->symtree->n.sym->attr.proc_pointer
    3790           31 :                     || gfc_is_proc_ptr_comp (a->expr)))
    3791           16 :                || (a->expr->ts.type == BT_PROCEDURE
    3792           10 :                    && f->sym->ts.interface)
    3793            6 :                || (a->expr->expr_type == EXPR_FUNCTION
    3794            6 :                    && is_procptr_result (a->expr))))
    3795              :         {
    3796            0 :           if (where)
    3797            0 :             gfc_error ("Expected a procedure pointer for argument %qs at %L",
    3798            0 :                        f->sym->name, &a->expr->where);
    3799            0 :           ok = false;
    3800            0 :           goto match;
    3801              :         }
    3802              : 
    3803              :       /* Satisfy F03:12.4.1.3 by ensuring that a procedure actual argument is
    3804              :          provided for a procedure formal argument.  */
    3805       258643 :       if (f->sym->attr.flavor == FL_PROCEDURE
    3806       258643 :           && !((a->expr->expr_type == EXPR_VARIABLE
    3807         1968 :                 && (a->expr->symtree->n.sym->attr.flavor == FL_PROCEDURE
    3808           32 :                     || a->expr->symtree->n.sym->attr.proc_pointer
    3809           32 :                     || gfc_is_proc_ptr_comp (a->expr)))
    3810           30 :                || (a->expr->expr_type == EXPR_FUNCTION
    3811           21 :                    && is_procptr_result (a->expr))))
    3812              :         {
    3813           12 :           if (where)
    3814            6 :             gfc_error ("Expected a procedure for argument %qs at %L",
    3815            6 :                        f->sym->name, &a->expr->where);
    3816           12 :           ok = false;
    3817           12 :           goto match;
    3818              :         }
    3819              : 
    3820              :       /* F23:15.5.2.5, para 2: A procedure pointer actual argument cannot correspond
    3821              :          to a data-object dummy argument (reverse of the two checks above).
    3822              :          Only flag EXPR_VARIABLE to avoid false positives on function calls
    3823              :          through procedure pointer components (e.g. o%f(args)).  */
    3824       258631 :       if (!f->sym->attr.proc_pointer
    3825       258425 :           && f->sym->attr.flavor != FL_PROCEDURE
    3826       256648 :           && a->expr->expr_type == EXPR_VARIABLE
    3827       365093 :           && (a->expr->symtree->n.sym->attr.proc_pointer
    3828       106455 :               || gfc_is_proc_ptr_comp (a->expr)))
    3829              :         {
    3830            8 :           if (where)
    3831            2 :             gfc_error ("Procedure pointer actual argument at %L cannot "
    3832              :                        "be passed to data-object dummy argument %qs",
    3833            2 :                        &a->expr->where, f->sym->name);
    3834            8 :           ok = false;
    3835            8 :           goto match;
    3836              :         }
    3837              : 
    3838              :       /* Class array variables and expressions store array info in a
    3839              :          different place from non-class objects; consolidate the logic
    3840              :          to access it here instead of repeating it below.  Note that
    3841              :          pointer_arg and allocatable_arg are not fully general and are
    3842              :          only used in a specific situation below with an assumed-rank
    3843              :          argument.  */
    3844       258623 :       if (f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym))
    3845              :         {
    3846        14263 :           gfc_component *classdata = CLASS_DATA (f->sym);
    3847        14263 :           fas = classdata->as;
    3848        14263 :           pointer_dummy = classdata->attr.class_pointer;
    3849        14263 :         }
    3850              :       else
    3851              :         {
    3852       244360 :           fas = f->sym->as;
    3853       244360 :           pointer_dummy = f->sym->attr.pointer;
    3854              :         }
    3855              : 
    3856       258623 :       if (a->expr->expr_type != EXPR_VARIABLE
    3857       150204 :           && !(a->expr->expr_type == EXPR_NULL
    3858          758 :                && a->expr->ts.type != BT_UNKNOWN))
    3859              :         {
    3860              :           aas = NULL;
    3861              :           pointer_arg = false;
    3862              :           allocatable_arg = false;
    3863              :         }
    3864       108920 :       else if (a->expr->ts.type == BT_CLASS
    3865         6691 :                && a->expr->symtree->n.sym
    3866         6691 :                && CLASS_DATA (a->expr->symtree->n.sym))
    3867              :         {
    3868         6688 :           gfc_component *classdata = CLASS_DATA (a->expr->symtree->n.sym);
    3869         6688 :           aas = classdata->as;
    3870         6688 :           pointer_arg = classdata->attr.class_pointer;
    3871         6688 :           allocatable_arg = classdata->attr.allocatable;
    3872         6688 :         }
    3873              :       else
    3874              :         {
    3875       102232 :           aas = a->expr->symtree->n.sym->as;
    3876       102232 :           pointer_arg = a->expr->symtree->n.sym->attr.pointer;
    3877       102232 :           allocatable_arg = a->expr->symtree->n.sym->attr.allocatable;
    3878              :         }
    3879              : 
    3880              :       /* F2018:9.5.2(2) permits assumed-size whole array expressions as
    3881              :          actual arguments only if the shape is not required; thus it
    3882              :          cannot be passed to an assumed-shape array dummy.
    3883              :          F2018:15.5.2.(2) permits passing a nonpointer actual to an
    3884              :          intent(in) pointer dummy argument and this is accepted by
    3885              :          the compare_pointer check below, but this also requires shape
    3886              :          information.
    3887              :          There's more discussion of this in PR94110.  */
    3888       258623 :       if (fas
    3889        43555 :           && (fas->type == AS_ASSUMED_SHAPE
    3890        43555 :               || fas->type == AS_DEFERRED
    3891        21974 :               || (fas->type == AS_ASSUMED_RANK && pointer_dummy))
    3892        22643 :           && aas
    3893        17876 :           && aas->type == AS_ASSUMED_SIZE
    3894           14 :           && (a->expr->ref == NULL
    3895           14 :               || (a->expr->ref->type == REF_ARRAY
    3896           14 :                   && a->expr->ref->u.ar.type == AR_FULL)))
    3897              :         {
    3898           10 :           if (where)
    3899           10 :             gfc_error ("Actual argument for %qs cannot be an assumed-size"
    3900              :                        " array at %L", f->sym->name, where);
    3901           10 :           ok = false;
    3902           10 :           goto match;
    3903              :         }
    3904              : 
    3905              :       /* Diagnose F2018 C839 (TS29113 C535c).  Here the problem is
    3906              :          passing an assumed-size array to an INTENT(OUT) assumed-rank
    3907              :          dummy when it doesn't have the size information needed to run
    3908              :          initializers and finalizers.  */
    3909       258613 :       if (f->sym->attr.intent == INTENT_OUT
    3910         6681 :           && fas
    3911         1247 :           && fas->type == AS_ASSUMED_RANK
    3912          276 :           && aas
    3913          223 :           && ((aas->type == AS_ASSUMED_SIZE
    3914           61 :                && (a->expr->ref == NULL
    3915           61 :                    || (a->expr->ref->type == REF_ARRAY
    3916           61 :                        && a->expr->ref->u.ar.type == AR_FULL)))
    3917          173 :               || (aas->type == AS_ASSUMED_RANK
    3918              :                   && !pointer_arg
    3919           34 :                   && !allocatable_arg))
    3920       258681 :           && (a->expr->ts.type == BT_CLASS
    3921           62 :               || (a->expr->ts.type == BT_DERIVED
    3922           16 :                   && (gfc_is_finalizable (a->expr->ts.u.derived, NULL)
    3923           14 :                       || gfc_has_ultimate_allocatable (a->expr)
    3924           12 :                       || gfc_has_default_initializer
    3925           12 :                            (a->expr->ts.u.derived)))))
    3926              :         {
    3927           12 :           if (where)
    3928           12 :             gfc_error ("Actual argument to assumed-rank INTENT(OUT) "
    3929              :                        "dummy %qs at %L cannot be of unknown size",
    3930           12 :                        f->sym->name, where);
    3931           12 :           ok = false;
    3932           12 :           goto match;
    3933              :         }
    3934              : 
    3935       258601 :       if (a->expr->expr_type != EXPR_NULL)
    3936              :         {
    3937       257843 :           int cmp = compare_pointer (f->sym, a->expr);
    3938       257843 :           bool pre2008 = ((gfc_option.allow_std & GFC_STD_F2008) == 0);
    3939              : 
    3940       257843 :           if (pre2008 && cmp == 0)
    3941              :             {
    3942            1 :               if (where)
    3943            1 :                 gfc_error ("Actual argument for %qs at %L must be a pointer",
    3944            1 :                            f->sym->name, &a->expr->where);
    3945            1 :               ok = false;
    3946            1 :               goto match;
    3947              :             }
    3948              : 
    3949       257842 :           if (pre2008 && cmp == 2)
    3950              :             {
    3951            3 :               if (where)
    3952            3 :                 gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
    3953            3 :                            "pointer dummy %qs", &a->expr->where, f->sym->name);
    3954            3 :               ok = false;
    3955            3 :               goto match;
    3956              :             }
    3957              : 
    3958       257839 :           if (!pre2008 && cmp == 0)
    3959              :             {
    3960           11 :               if (where)
    3961            5 :                 gfc_error ("Actual argument for %qs at %L must be a pointer "
    3962              :                            "or a valid target for the dummy pointer in a "
    3963              :                            "pointer assignment statement",
    3964            5 :                            f->sym->name, &a->expr->where);
    3965           11 :               ok = false;
    3966           11 :               goto match;
    3967              :             }
    3968              :         }
    3969              : 
    3970              : 
    3971              :       /* Fortran 2008, C1242.  */
    3972       258586 :       if (f->sym->attr.pointer && gfc_is_coindexed (a->expr))
    3973              :         {
    3974            2 :           if (where)
    3975            2 :             gfc_error ("Coindexed actual argument at %L to pointer "
    3976              :                        "dummy %qs",
    3977            2 :                        &a->expr->where, f->sym->name);
    3978            2 :           ok = false;
    3979            2 :           goto match;
    3980              :         }
    3981              : 
    3982              :       /* Fortran 2008, 12.5.2.5 (no constraint).  */
    3983       258584 :       if (a->expr->expr_type == EXPR_VARIABLE
    3984       108381 :           && f->sym->attr.intent != INTENT_IN
    3985        61989 :           && f->sym->attr.allocatable
    3986       261511 :           && gfc_is_coindexed (a->expr))
    3987              :         {
    3988            1 :           if (where)
    3989            1 :             gfc_error ("Coindexed actual argument at %L to allocatable "
    3990              :                        "dummy %qs requires INTENT(IN)",
    3991            1 :                        &a->expr->where, f->sym->name);
    3992            1 :           ok = false;
    3993            1 :           goto match;
    3994              :         }
    3995              : 
    3996              :       /* Fortran 2008, C1237.  */
    3997       258583 :       if (a->expr->expr_type == EXPR_VARIABLE
    3998       108380 :           && (f->sym->attr.asynchronous || f->sym->attr.volatile_)
    3999           65 :           && gfc_is_coindexed (a->expr)
    4000       258585 :           && (a->expr->symtree->n.sym->attr.volatile_
    4001            1 :               || a->expr->symtree->n.sym->attr.asynchronous))
    4002              :         {
    4003            2 :           if (where)
    4004            2 :             gfc_error ("Coindexed ASYNCHRONOUS or VOLATILE actual argument at "
    4005              :                        "%L requires that dummy %qs has neither "
    4006              :                        "ASYNCHRONOUS nor VOLATILE", &a->expr->where,
    4007            2 :                        f->sym->name);
    4008            2 :           ok = false;
    4009            2 :           goto match;
    4010              :         }
    4011              : 
    4012              :       /* Fortran 2008, 12.5.2.4 (no constraint).  */
    4013       258581 :       if (a->expr->expr_type == EXPR_VARIABLE
    4014       108378 :           && f->sym->attr.intent != INTENT_IN && !f->sym->attr.value
    4015        57498 :           && gfc_is_coindexed (a->expr)
    4016       258592 :           && gfc_has_ultimate_allocatable (a->expr))
    4017              :         {
    4018            1 :           if (where)
    4019            1 :             gfc_error ("Coindexed actual argument at %L with allocatable "
    4020              :                        "ultimate component to dummy %qs requires either VALUE "
    4021            1 :                        "or INTENT(IN)", &a->expr->where, f->sym->name);
    4022            1 :           ok = false;
    4023            1 :           goto match;
    4024              :         }
    4025              : 
    4026       258580 :      if (f->sym->ts.type == BT_CLASS
    4027        14255 :            && CLASS_DATA (f->sym)->attr.allocatable
    4028          886 :            && gfc_is_class_array_ref (a->expr, &full_array)
    4029       259025 :            && !full_array)
    4030              :         {
    4031            0 :           if (where)
    4032            0 :             gfc_error ("Actual CLASS array argument for %qs must be a full "
    4033            0 :                        "array at %L", f->sym->name, &a->expr->where);
    4034            0 :           ok = false;
    4035            0 :           goto match;
    4036              :         }
    4037              : 
    4038              : 
    4039       258580 :       if (a->expr->expr_type != EXPR_NULL
    4040       258580 :           && !compare_allocatable (f->sym, a->expr))
    4041              :         {
    4042            9 :           if (where)
    4043            9 :             gfc_error ("Actual argument for %qs must be ALLOCATABLE at %L",
    4044            9 :                        f->sym->name, &a->expr->where);
    4045            9 :           ok = false;
    4046            9 :           goto match;
    4047              :         }
    4048              : 
    4049       258571 :       if (a->expr->expr_type == EXPR_FUNCTION
    4050        15209 :           && a->expr->value.function.esym
    4051         5033 :           && f->sym->attr.allocatable)
    4052              :         {
    4053            4 :           if (where)
    4054            4 :             gfc_error ("Actual argument for %qs at %L is a function result "
    4055              :                        "and the dummy argument is ALLOCATABLE",
    4056              :                        f->sym->name, &a->expr->where);
    4057            4 :           ok = false;
    4058            4 :           goto match;
    4059              :         }
    4060              : 
    4061              :       /* Check intent = OUT/INOUT for definable actual argument.  */
    4062       258567 :       if (!in_statement_function
    4063       258092 :           && (f->sym->attr.intent == INTENT_OUT
    4064       251425 :               || f->sym->attr.intent == INTENT_INOUT))
    4065              :         {
    4066        10944 :           const char* context = (where
    4067        10944 :                                  ? _("actual argument to INTENT = OUT/INOUT")
    4068              :                                  : NULL);
    4069              : 
    4070         2879 :           if (((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
    4071         2879 :                 && CLASS_DATA (f->sym)->attr.class_pointer)
    4072        10924 :                || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
    4073        11134 :               && !gfc_check_vardef_context (a->expr, true, false, false, context))
    4074              :             {
    4075            6 :               ok = false;
    4076            6 :               goto match;
    4077              :             }
    4078        10938 :           if (!gfc_check_vardef_context (a->expr, false, false, false, context))
    4079              :             {
    4080           21 :               ok = false;
    4081           21 :               goto match;
    4082              :             }
    4083              :         }
    4084              :       /* F2023: 15.5.2.5 Ordinary dummy variables:
    4085              :          "(21) If the procedure is nonelemental, the dummy argument does not
    4086              :          have the VALUE attribute, and the actual argument is an array section
    4087              :          having a vector subscript, the dummy argument is not definable and
    4088              :          shall not have the ASYNCHRONOUS, INTENT (OUT), INTENT (INOUT), or
    4089              :          VOLATILE attributes."
    4090              :        */
    4091       258540 :       if ((f->sym->attr.intent == INTENT_OUT
    4092       251881 :            || f->sym->attr.intent == INTENT_INOUT
    4093       247621 :            || f->sym->attr.volatile_
    4094       247585 :            || f->sym->attr.asynchronous)
    4095        10983 :           && !f->sym->attr.value
    4096        10983 :           && !is_elemental
    4097       265798 :           && gfc_has_vector_subscript (a->expr))
    4098              :         {
    4099            3 :           if (where)
    4100            3 :             gfc_error ("Array-section actual argument with vector "
    4101              :                        "subscripts at %L is incompatible with INTENT(OUT), "
    4102              :                        "INTENT(INOUT), VOLATILE or ASYNCHRONOUS attribute "
    4103              :                        "of the dummy argument %qs",
    4104            3 :                        &a->expr->where, f->sym->name);
    4105            3 :           ok = false;
    4106            3 :           goto match;
    4107              :         }
    4108              : 
    4109              :       /* C1232 (R1221) For an actual argument which is an array section or
    4110              :          an assumed-shape array, the dummy argument shall be an assumed-
    4111              :          shape array, if the dummy argument has the VOLATILE attribute.  */
    4112              : 
    4113       258537 :       if (f->sym->attr.volatile_
    4114           37 :           && a->expr->expr_type == EXPR_VARIABLE
    4115           34 :           && a->expr->symtree->n.sym->as
    4116           29 :           && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SHAPE
    4117            2 :           && !(fas && fas->type == AS_ASSUMED_SHAPE))
    4118              :         {
    4119            1 :           if (where)
    4120            1 :             gfc_error ("Assumed-shape actual argument at %L is "
    4121              :                        "incompatible with the non-assumed-shape "
    4122              :                        "dummy argument %qs due to VOLATILE attribute",
    4123              :                        &a->expr->where,f->sym->name);
    4124            1 :           ok = false;
    4125            1 :           goto match;
    4126              :         }
    4127              : 
    4128              :       /* Find the last array_ref.  */
    4129       258536 :       actual_arr_ref = NULL;
    4130       258536 :       if (a->expr->ref)
    4131        46403 :         actual_arr_ref = gfc_find_array_ref (a->expr, true);
    4132              : 
    4133       258536 :       if (f->sym->attr.volatile_
    4134           36 :           && actual_arr_ref && actual_arr_ref->type == AR_SECTION
    4135            5 :           && !(fas && fas->type == AS_ASSUMED_SHAPE))
    4136              :         {
    4137            1 :           if (where)
    4138            1 :             gfc_error ("Array-section actual argument at %L is "
    4139              :                        "incompatible with the non-assumed-shape "
    4140              :                        "dummy argument %qs due to VOLATILE attribute",
    4141            1 :                        &a->expr->where, f->sym->name);
    4142            1 :           ok = false;
    4143            1 :           goto match;
    4144              :         }
    4145              : 
    4146              :       /* C1233 (R1221) For an actual argument which is a pointer array, the
    4147              :          dummy argument shall be an assumed-shape or pointer array, if the
    4148              :          dummy argument has the VOLATILE attribute.  */
    4149              : 
    4150       258535 :       if (f->sym->attr.volatile_
    4151           35 :           && a->expr->expr_type == EXPR_VARIABLE
    4152           32 :           && a->expr->symtree->n.sym->attr.pointer
    4153           17 :           && a->expr->symtree->n.sym->as
    4154           17 :           && !(fas
    4155           17 :                && (fas->type == AS_ASSUMED_SHAPE
    4156            6 :                    || f->sym->attr.pointer)))
    4157              :         {
    4158            3 :           if (where)
    4159            2 :             gfc_error ("Pointer-array actual argument at %L requires "
    4160              :                        "an assumed-shape or pointer-array dummy "
    4161              :                        "argument %qs due to VOLATILE attribute",
    4162              :                        &a->expr->where,f->sym->name);
    4163            3 :           ok = false;
    4164            3 :           goto match;
    4165              :         }
    4166              : 
    4167              :       /* C_LOC/C_FUNLOC from ISO_C_BINDING as actual argument can only be
    4168              :          passed to a dummy argument of matching type C_PTR/C_FUNPTR.  */
    4169       258532 :       if (a->expr->expr_type == EXPR_FUNCTION
    4170        15202 :           && a->expr->ts.type == BT_VOID
    4171            5 :           && a->expr->symtree->n.sym
    4172            5 :           && a->expr->symtree->n.sym->from_intmod == INTMOD_ISO_C_BINDING
    4173            5 :           && (f->sym->ts.type != BT_DERIVED
    4174            3 :               || f->sym->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
    4175            3 :               || !((a->expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_FUNLOC
    4176            1 :                     && f->sym->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR)
    4177              :                    || (a->expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_LOC
    4178            2 :                        && f->sym->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR))))
    4179              :         {
    4180            3 :           if (where)
    4181            0 :             gfc_error ("ISO_C_BINDING function actual argument at %L "
    4182              :                        "requires dummy argument %qs to have a matching "
    4183              :                        "type from ISO_C_BINDING",
    4184              :                        &a->expr->where,f->sym->name);
    4185            3 :           ok = false;
    4186            3 :           goto match;
    4187              :         }
    4188              : 
    4189       258529 :     match:
    4190       365243 :       if (a == actual)
    4191       177336 :         na = i;
    4192              : 
    4193       365243 :       new_arg[i++] = a;
    4194              :     }
    4195              : 
    4196              :   /* Give up now if we saw any bad argument.  */
    4197       177495 :   if (!ok)
    4198              :     return false;
    4199              : 
    4200              :   /* Make sure missing actual arguments are optional.  */
    4201              :   i = 0;
    4202       359164 :   for (f = formal; f; f = f->next, i++)
    4203              :     {
    4204       247764 :       if (new_arg[i] != NULL)
    4205       242096 :         continue;
    4206         5668 :       if (f->sym == NULL)
    4207              :         {
    4208            1 :           if (where)
    4209            1 :             gfc_error ("Missing alternate return spec in subroutine call "
    4210              :                        "at %L", where);
    4211            1 :           return false;
    4212              :         }
    4213              :       /* For CLASS, the optional attribute might be set at either location. */
    4214         5667 :       if (((f->sym->ts.type != BT_CLASS || !CLASS_DATA (f->sym)->attr.optional)
    4215         5667 :            && !f->sym->attr.optional)
    4216         5581 :           || (in_statement_function
    4217            1 :               && (f->sym->attr.optional
    4218            0 :                   || (f->sym->ts.type == BT_CLASS
    4219            0 :                       && CLASS_DATA (f->sym)->attr.optional))))
    4220              :         {
    4221           87 :           if (where)
    4222            4 :             gfc_error ("Missing actual argument for argument %qs at %L",
    4223              :                        f->sym->name, where);
    4224           87 :           return false;
    4225              :         }
    4226              :     }
    4227              : 
    4228              :   /* We should have handled the cases where the formal arglist is null
    4229              :      already.  */
    4230       111400 :   gcc_assert (n > 0);
    4231              : 
    4232              :   /* The argument lists are compatible.  We now relink a new actual
    4233              :      argument list with null arguments in the right places.  The head
    4234              :      of the list remains the head.  */
    4235       358997 :   for (f = formal, i = 0; f; f = f->next, i++)
    4236       247597 :     if (new_arg[i] == NULL)
    4237              :       {
    4238         5580 :         new_arg[i] = gfc_get_actual_arglist ();
    4239         5580 :         new_arg[i]->associated_dummy = get_nonintrinsic_dummy_arg (f);
    4240              :       }
    4241              : 
    4242       111400 :   if (na != 0)
    4243              :     {
    4244          385 :       std::swap (*new_arg[0], *actual);
    4245          385 :       std::swap (new_arg[0], new_arg[na]);
    4246              :     }
    4247              : 
    4248       247597 :   for (i = 0; i < n - 1; i++)
    4249       136197 :     new_arg[i]->next = new_arg[i + 1];
    4250              : 
    4251       111400 :   new_arg[i]->next = NULL;
    4252              : 
    4253       111400 :   if (*ap == NULL && n > 0)
    4254          796 :     *ap = new_arg[0];
    4255              : 
    4256       111400 :   if (!in_statement_function)
    4257       358238 :     for (f = formal, i = 0; f; f = f->next, i++)
    4258              :       {
    4259       247122 :         if (new_arg[i]->expr)
    4260              :           {
    4261       241333 :             gfc_expr *e = new_arg[i]->expr;
    4262              : 
    4263       241333 :             if (f->sym->attr.value)
    4264              :               {
    4265        21981 :                 gfc_value_used_expr (e, VALUE_VALUE_ARG);
    4266        21981 :                 continue;
    4267              :               }
    4268       219352 :             switch (f->sym->attr.intent)
    4269              :               {
    4270         6532 :               case INTENT_OUT:
    4271         6532 :                 {
    4272         6532 :                   gfc_expr_set_at (e, &e->where, VALUE_INTENT_OUT);
    4273         6532 :                   if (f->sym->attr.allocatable)
    4274          419 :                     gfc_used_in_allocate_expr (e, &e->where, ALLOCATED_ARG);
    4275              : 
    4276              :                 }
    4277              :                 break;
    4278              : 
    4279       115533 :               case INTENT_IN:
    4280       115533 :                 gfc_value_used_expr (e, VALUE_INTENT_IN);
    4281       115533 :                 break;
    4282              : 
    4283        97287 :               case INTENT_INOUT:
    4284        97287 :               case INTENT_UNKNOWN:
    4285        97287 :                 gfc_value_set_and_used (e, &e->where, VALUE_ARG,
    4286              :                                         VALUE_MAYBE_USED);
    4287              : 
    4288        97287 :                 if (f->sym->attr.allocatable)
    4289         2564 :                   gfc_used_in_allocate_expr (e, &e->where, ALLOCATED_ARG);
    4290              :                 break;
    4291              :               }
    4292              :           }
    4293              :     }
    4294              : 
    4295              :   return true;
    4296              : }
    4297              : 
    4298              : 
    4299              : typedef struct
    4300              : {
    4301              :   gfc_formal_arglist *f;
    4302              :   gfc_actual_arglist *a;
    4303              : }
    4304              : argpair;
    4305              : 
    4306              : /* qsort comparison function for argument pairs, with the following
    4307              :    order:
    4308              :     - p->a->expr == NULL
    4309              :     - p->a->expr->expr_type != EXPR_VARIABLE
    4310              :     - by gfc_symbol pointer value (larger first).  */
    4311              : 
    4312              : static int
    4313         2345 : pair_cmp (const void *p1, const void *p2)
    4314              : {
    4315         2345 :   const gfc_actual_arglist *a1, *a2;
    4316              : 
    4317              :   /* *p1 and *p2 are elements of the to-be-sorted array.  */
    4318         2345 :   a1 = ((const argpair *) p1)->a;
    4319         2345 :   a2 = ((const argpair *) p2)->a;
    4320         2345 :   if (!a1->expr)
    4321              :     {
    4322           23 :       if (!a2->expr)
    4323              :         return 0;
    4324           23 :       return -1;
    4325              :     }
    4326         2322 :   if (!a2->expr)
    4327              :     return 1;
    4328         2313 :   if (a1->expr->expr_type != EXPR_VARIABLE)
    4329              :     {
    4330         1658 :       if (a2->expr->expr_type != EXPR_VARIABLE)
    4331              :         return 0;
    4332         1110 :       return -1;
    4333              :     }
    4334          655 :   if (a2->expr->expr_type != EXPR_VARIABLE)
    4335              :     return 1;
    4336          195 :   if (a1->expr->symtree->n.sym > a2->expr->symtree->n.sym)
    4337              :     return -1;
    4338           79 :   return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
    4339              : }
    4340              : 
    4341              : 
    4342              : /* Given two expressions from some actual arguments, test whether they
    4343              :    refer to the same expression. The analysis is conservative.
    4344              :    Returning false will produce no warning.  */
    4345              : 
    4346              : static bool
    4347           43 : compare_actual_expr (gfc_expr *e1, gfc_expr *e2)
    4348              : {
    4349           43 :   const gfc_ref *r1, *r2;
    4350              : 
    4351           43 :   if (!e1 || !e2
    4352           43 :       || e1->expr_type != EXPR_VARIABLE
    4353           43 :       || e2->expr_type != EXPR_VARIABLE
    4354           43 :       || e1->symtree->n.sym != e2->symtree->n.sym)
    4355              :     return false;
    4356              : 
    4357              :   /* TODO: improve comparison, see expr.cc:show_ref().  */
    4358            4 :   for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
    4359              :     {
    4360            1 :       if (r1->type != r2->type)
    4361              :         return false;
    4362            1 :       switch (r1->type)
    4363              :         {
    4364            0 :         case REF_ARRAY:
    4365            0 :           if (r1->u.ar.type != r2->u.ar.type)
    4366              :             return false;
    4367              :           /* TODO: At the moment, consider only full arrays;
    4368              :              we could do better.  */
    4369            0 :           if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
    4370              :             return false;
    4371              :           break;
    4372              : 
    4373            0 :         case REF_COMPONENT:
    4374            0 :           if (r1->u.c.component != r2->u.c.component)
    4375              :             return false;
    4376              :           break;
    4377              : 
    4378              :         case REF_SUBSTRING:
    4379              :           return false;
    4380              : 
    4381            1 :         case REF_INQUIRY:
    4382            1 :           if (e1->symtree->n.sym->ts.type == BT_COMPLEX
    4383            1 :               && e1->ts.type == BT_REAL && e2->ts.type == BT_REAL
    4384            1 :               && r1->u.i != r2->u.i)
    4385              :             return false;
    4386              :           break;
    4387              : 
    4388            0 :         default:
    4389            0 :           gfc_internal_error ("compare_actual_expr(): Bad component code");
    4390              :         }
    4391              :     }
    4392            3 :   if (!r1 && !r2)
    4393              :     return true;
    4394              :   return false;
    4395              : }
    4396              : 
    4397              : 
    4398              : /* Given formal and actual argument lists that correspond to one
    4399              :    another, check that identical actual arguments aren't not
    4400              :    associated with some incompatible INTENTs.  */
    4401              : 
    4402              : static bool
    4403          740 : check_some_aliasing (gfc_formal_arglist *f, gfc_actual_arglist *a)
    4404              : {
    4405          740 :   sym_intent f1_intent, f2_intent;
    4406          740 :   gfc_formal_arglist *f1;
    4407          740 :   gfc_actual_arglist *a1;
    4408          740 :   size_t n, i, j;
    4409          740 :   argpair *p;
    4410          740 :   bool t = true;
    4411              : 
    4412          740 :   n = 0;
    4413          740 :   for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
    4414              :     {
    4415         1939 :       if (f1 == NULL && a1 == NULL)
    4416              :         break;
    4417         1199 :       if (f1 == NULL || a1 == NULL)
    4418            0 :         gfc_internal_error ("check_some_aliasing(): List mismatch");
    4419         1199 :       n++;
    4420              :     }
    4421          740 :   if (n == 0)
    4422              :     return t;
    4423          657 :   p = XALLOCAVEC (argpair, n);
    4424              : 
    4425         1856 :   for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
    4426              :     {
    4427         1199 :       p[i].f = f1;
    4428         1199 :       p[i].a = a1;
    4429              :     }
    4430              : 
    4431          657 :   qsort (p, n, sizeof (argpair), pair_cmp);
    4432              : 
    4433         2513 :   for (i = 0; i < n; i++)
    4434              :     {
    4435         1199 :       if (!p[i].a->expr
    4436         1194 :           || p[i].a->expr->expr_type != EXPR_VARIABLE
    4437          572 :           || p[i].a->expr->ts.type == BT_PROCEDURE)
    4438          628 :         continue;
    4439          571 :       f1_intent = p[i].f->sym->attr.intent;
    4440          574 :       for (j = i + 1; j < n; j++)
    4441              :         {
    4442              :           /* Expected order after the sort.  */
    4443           43 :           if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
    4444            0 :             gfc_internal_error ("check_some_aliasing(): corrupted data");
    4445              : 
    4446              :           /* Are the expression the same?  */
    4447           43 :           if (!compare_actual_expr (p[i].a->expr, p[j].a->expr))
    4448              :             break;
    4449            3 :           f2_intent = p[j].f->sym->attr.intent;
    4450            3 :           if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
    4451            2 :               || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN)
    4452            1 :               || (f1_intent == INTENT_OUT && f2_intent == INTENT_OUT))
    4453              :             {
    4454            3 :               gfc_warning (0, "Same actual argument associated with INTENT(%s) "
    4455              :                            "argument %qs and INTENT(%s) argument %qs at %L",
    4456            3 :                            gfc_intent_string (f1_intent), p[i].f->sym->name,
    4457              :                            gfc_intent_string (f2_intent), p[j].f->sym->name,
    4458              :                            &p[i].a->expr->where);
    4459            3 :               t = false;
    4460              :             }
    4461              :         }
    4462              :     }
    4463              : 
    4464              :   return t;
    4465              : }
    4466              : 
    4467              : 
    4468              : /* Given formal and actual argument lists that correspond to one
    4469              :    another, check that they are compatible in the sense that intents
    4470              :    are not mismatched.  */
    4471              : 
    4472              : static bool
    4473       114931 : check_intents (gfc_formal_arglist *f, gfc_actual_arglist *a)
    4474              : {
    4475       334881 :   sym_intent f_intent;
    4476              : 
    4477       554831 :   for (;; f = f->next, a = a->next)
    4478              :     {
    4479       334881 :       gfc_expr *expr;
    4480              : 
    4481       334881 :       if (f == NULL && a == NULL)
    4482              :         break;
    4483       219954 :       if (f == NULL || a == NULL)
    4484            0 :         gfc_internal_error ("check_intents(): List mismatch");
    4485              : 
    4486       219954 :       if (a->expr && a->expr->expr_type == EXPR_FUNCTION
    4487        12744 :           && a->expr->value.function.isym
    4488         7685 :           && a->expr->value.function.isym->id == GFC_ISYM_CAF_GET)
    4489            0 :         expr = a->expr->value.function.actual->expr;
    4490              :       else
    4491              :         expr = a->expr;
    4492              : 
    4493       219954 :       if (expr == NULL || expr->expr_type != EXPR_VARIABLE)
    4494       127638 :         continue;
    4495              : 
    4496        92316 :       f_intent = f->sym->attr.intent;
    4497              : 
    4498        92316 :       if (gfc_pure (NULL) && gfc_impure_variable (expr->symtree->n.sym))
    4499              :         {
    4500          412 :           if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
    4501           16 :                && CLASS_DATA (f->sym)->attr.class_pointer)
    4502          411 :               || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
    4503              :             {
    4504            2 :               gfc_error ("Procedure argument at %L is local to a PURE "
    4505              :                          "procedure and has the POINTER attribute",
    4506              :                          &expr->where);
    4507            2 :               return false;
    4508              :             }
    4509              :         }
    4510              : 
    4511              :        /* Fortran 2008, C1283.  */
    4512        92314 :        if (gfc_pure (NULL) && gfc_is_coindexed (expr))
    4513              :         {
    4514            1 :           if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
    4515              :             {
    4516            1 :               gfc_error ("Coindexed actual argument at %L in PURE procedure "
    4517              :                          "is passed to an INTENT(%s) argument",
    4518              :                          &expr->where, gfc_intent_string (f_intent));
    4519            1 :               return false;
    4520              :             }
    4521              : 
    4522            0 :           if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
    4523            0 :                && CLASS_DATA (f->sym)->attr.class_pointer)
    4524            0 :               || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
    4525              :             {
    4526            0 :               gfc_error ("Coindexed actual argument at %L in PURE procedure "
    4527              :                          "is passed to a POINTER dummy argument",
    4528              :                          &expr->where);
    4529            0 :               return false;
    4530              :             }
    4531              :         }
    4532              : 
    4533              :        /* F2008, Section 12.5.2.4.  */
    4534         6556 :        if (expr->ts.type == BT_CLASS && f->sym->ts.type == BT_CLASS
    4535        98169 :            && gfc_is_coindexed (expr))
    4536              :          {
    4537            1 :            gfc_error ("Coindexed polymorphic actual argument at %L is passed "
    4538              :                       "polymorphic dummy argument %qs",
    4539            1 :                          &expr->where, f->sym->name);
    4540            1 :            return false;
    4541              :          }
    4542       219950 :     }
    4543              : 
    4544              :   return true;
    4545              : }
    4546              : 
    4547              : 
    4548              : /* Check how a procedure is used against its interface.  If all goes
    4549              :    well, the actual argument list will also end up being properly
    4550              :    sorted.  */
    4551              : 
    4552              : bool
    4553       104896 : gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
    4554              : {
    4555       104896 :   gfc_actual_arglist *a;
    4556       104896 :   gfc_formal_arglist *dummy_args;
    4557       104896 :   bool implicit = false;
    4558              : 
    4559              :   /* Warn about calls with an implicit interface.  Special case
    4560              :      for calling a ISO_C_BINDING because c_loc and c_funloc
    4561              :      are pseudo-unknown.  Additionally, warn about procedures not
    4562              :      explicitly declared at all if requested.  */
    4563       104896 :   if (sym->attr.if_source == IFSRC_UNKNOWN && !sym->attr.is_iso_c)
    4564              :     {
    4565        16458 :       bool has_implicit_none_export = false;
    4566        16458 :       implicit = true;
    4567        16458 :       if (sym->attr.proc == PROC_UNKNOWN)
    4568        23300 :         for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
    4569        11744 :           if (ns->has_implicit_none_export)
    4570              :             {
    4571              :               has_implicit_none_export = true;
    4572              :               break;
    4573              :             }
    4574        11560 :       if (has_implicit_none_export)
    4575              :         {
    4576            4 :           const char *guessed
    4577            4 :             = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
    4578            4 :           if (guessed)
    4579            1 :             gfc_error ("Procedure %qs called at %L is not explicitly declared"
    4580              :                        "; did you mean %qs?",
    4581              :                        sym->name, where, guessed);
    4582              :           else
    4583            3 :             gfc_error ("Procedure %qs called at %L is not explicitly declared",
    4584              :                        sym->name, where);
    4585            4 :           return false;
    4586              :         }
    4587        16454 :       if (warn_implicit_interface)
    4588            0 :         gfc_warning (OPT_Wimplicit_interface,
    4589              :                      "Procedure %qs called with an implicit interface at %L",
    4590              :                      sym->name, where);
    4591        16454 :       else if (warn_implicit_procedure && sym->attr.proc == PROC_UNKNOWN)
    4592            1 :         gfc_warning (OPT_Wimplicit_procedure,
    4593              :                      "Procedure %qs called at %L is not explicitly declared",
    4594              :                      sym->name, where);
    4595        16454 :       gfc_find_proc_namespace (sym->ns)->implicit_interface_calls = 1;
    4596              :     }
    4597              : 
    4598       104892 :   if (sym->attr.if_source == IFSRC_UNKNOWN)
    4599              :     {
    4600        16454 :       if (sym->attr.pointer)
    4601              :         {
    4602            1 :           gfc_error ("The pointer object %qs at %L must have an explicit "
    4603              :                      "function interface or be declared as array",
    4604              :                      sym->name, where);
    4605            1 :           return false;
    4606              :         }
    4607              : 
    4608        16453 :       if (sym->attr.allocatable && !sym->attr.external)
    4609              :         {
    4610            1 :           gfc_error ("The allocatable object %qs at %L must have an explicit "
    4611              :                      "function interface or be declared as array",
    4612              :                      sym->name, where);
    4613            1 :           return false;
    4614              :         }
    4615              : 
    4616        16452 :       if (sym->attr.allocatable)
    4617              :         {
    4618            1 :           gfc_error ("Allocatable function %qs at %L must have an explicit "
    4619              :                      "function interface", sym->name, where);
    4620            1 :           return false;
    4621              :         }
    4622              : 
    4623        46914 :       for (a = *ap; a; a = a->next)
    4624              :         {
    4625        30478 :           if (a->expr && a->expr->error)
    4626              :             return false;
    4627              : 
    4628              :           /* F2018, 15.4.2.2 Explicit interface is required for a
    4629              :              polymorphic dummy argument, so there is no way to
    4630              :              legally have a class appear in an argument with an
    4631              :              implicit interface.  */
    4632              : 
    4633        30478 :           if (implicit && a->expr && a->expr->ts.type == BT_CLASS)
    4634              :             {
    4635            3 :               gfc_error ("Explicit interface required for polymorphic "
    4636              :                          "argument at %L",&a->expr->where);
    4637            3 :               a->expr->error = 1;
    4638            3 :               break;
    4639              :             }
    4640              : 
    4641              :           /* Skip g77 keyword extensions like %VAL, %REF, %LOC.  */
    4642        30475 :           if (a->name != NULL && a->name[0] != '%')
    4643              :             {
    4644            2 :               gfc_error ("Keyword argument requires explicit interface "
    4645              :                          "for procedure %qs at %L", sym->name, &a->expr->where);
    4646            2 :               break;
    4647              :             }
    4648              : 
    4649              :           /* TS 29113, 6.2.  */
    4650        30473 :           if (a->expr && a->expr->ts.type == BT_ASSUMED
    4651            3 :               && sym->intmod_sym_id != ISOCBINDING_LOC)
    4652              :             {
    4653            3 :               gfc_error ("Assumed-type argument %s at %L requires an explicit "
    4654            3 :                          "interface", a->expr->symtree->n.sym->name,
    4655              :                          &a->expr->where);
    4656            3 :               a->expr->error = 1;
    4657            3 :               break;
    4658              :             }
    4659              : 
    4660              :           /* F2008, C1303 and C1304.  */
    4661        30470 :           if (a->expr
    4662        30295 :               && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
    4663           73 :               && a->expr->ts.u.derived
    4664        30541 :               && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    4665            1 :                    && a->expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
    4666           70 :                   || gfc_expr_attr (a->expr).lock_comp))
    4667              :             {
    4668            1 :               gfc_error ("Actual argument of LOCK_TYPE or with LOCK_TYPE "
    4669              :                          "component at %L requires an explicit interface for "
    4670            1 :                          "procedure %qs", &a->expr->where, sym->name);
    4671            1 :               a->expr->error = 1;
    4672            1 :               break;
    4673              :             }
    4674              : 
    4675        30469 :           if (a->expr
    4676        30294 :               && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
    4677           72 :               && a->expr->ts.u.derived
    4678        30539 :               && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    4679            0 :                    && a->expr->ts.u.derived->intmod_sym_id
    4680              :                       == ISOFORTRAN_EVENT_TYPE)
    4681           70 :                   || gfc_expr_attr (a->expr).event_comp))
    4682              :             {
    4683            0 :               gfc_error ("Actual argument of EVENT_TYPE or with EVENT_TYPE "
    4684              :                          "component at %L requires an explicit interface for "
    4685            0 :                          "procedure %qs", &a->expr->where, sym->name);
    4686            0 :               a->expr->error = 1;
    4687            0 :               break;
    4688              :             }
    4689              : 
    4690        30469 :           if (a->expr && a->expr->expr_type == EXPR_NULL
    4691            2 :               && a->expr->ts.type == BT_UNKNOWN)
    4692              :             {
    4693            1 :               gfc_error ("MOLD argument to NULL required at %L",
    4694              :                          &a->expr->where);
    4695            1 :               a->expr->error = 1;
    4696            1 :               return false;
    4697              :             }
    4698              : 
    4699        30468 :           if (a->expr && a->expr->expr_type == EXPR_NULL)
    4700              :             {
    4701            1 :               gfc_error ("Passing intrinsic NULL as actual argument at %L "
    4702              :                          "requires an explicit interface", &a->expr->where);
    4703            1 :               a->expr->error = 1;
    4704            1 :               return false;
    4705              :             }
    4706              : 
    4707              :           /* TS 29113, C407b.  */
    4708        30292 :           if (a->expr && a->expr->expr_type == EXPR_VARIABLE
    4709        43763 :               && gfc_symbol_rank (a->expr->symtree->n.sym) == -1)
    4710              :             {
    4711            4 :               gfc_error ("Assumed-rank argument requires an explicit interface "
    4712            4 :                          "at %L", &a->expr->where);
    4713            4 :               a->expr->error = 1;
    4714            4 :               return false;
    4715              :             }
    4716              :         }
    4717              : 
    4718        16445 :       if (implicit)
    4719        46917 :         for (a = *ap; a; a = a->next)
    4720        30472 :           if (a->expr)
    4721        30297 :             gfc_value_set_and_used (a->expr, &a->expr->where, VALUE_ARG,
    4722              :                                     VALUE_MAYBE_USED);
    4723              : 
    4724        16445 :       return true;
    4725              :     }
    4726              : 
    4727        88438 :   dummy_args = gfc_sym_get_dummy_args (sym);
    4728              : 
    4729              :   /* For a statement function, check that types and type parameters of actual
    4730              :      arguments and dummy arguments match.  */
    4731        88438 :   if (!gfc_compare_actual_formal (ap, dummy_args, 0, sym->attr.elemental,
    4732        88438 :                                   sym->attr.proc == PROC_ST_FUNCTION, where))
    4733              :     return false;
    4734              : 
    4735        87999 :   if (!check_intents (dummy_args, *ap))
    4736              :     return false;
    4737              : 
    4738        87995 :   if (warn_aliasing)
    4739          728 :     check_some_aliasing (dummy_args, *ap);
    4740              : 
    4741              :   return true;
    4742              : }
    4743              : 
    4744              : 
    4745              : /* Check how a procedure pointer component is used against its interface.
    4746              :    If all goes well, the actual argument list will also end up being properly
    4747              :    sorted. Completely analogous to gfc_procedure_use.  */
    4748              : 
    4749              : void
    4750          583 : gfc_ppc_use (gfc_component *comp, gfc_actual_arglist **ap, locus *where)
    4751              : {
    4752              :   /* Warn about calls with an implicit interface.  Special case
    4753              :      for calling a ISO_C_BINDING because c_loc and c_funloc
    4754              :      are pseudo-unknown.  */
    4755          583 :   if (warn_implicit_interface
    4756            0 :       && comp->attr.if_source == IFSRC_UNKNOWN
    4757            0 :       && !comp->attr.is_iso_c)
    4758            0 :     gfc_warning (OPT_Wimplicit_interface,
    4759              :                  "Procedure pointer component %qs called with an implicit "
    4760              :                  "interface at %L", comp->name, where);
    4761              : 
    4762          583 :   if (comp->attr.if_source == IFSRC_UNKNOWN)
    4763              :     {
    4764           60 :       gfc_actual_arglist *a;
    4765          105 :       for (a = *ap; a; a = a->next)
    4766              :         {
    4767              :           /* Skip g77 keyword extensions like %VAL, %REF, %LOC.  */
    4768           45 :           if (a->name != NULL && a->name[0] != '%')
    4769              :             {
    4770            0 :               gfc_error ("Keyword argument requires explicit interface "
    4771              :                          "for procedure pointer component %qs at %L",
    4772            0 :                          comp->name, &a->expr->where);
    4773            0 :               break;
    4774              :             }
    4775              :         }
    4776              : 
    4777           60 :       return;
    4778              :     }
    4779              : 
    4780          523 :   if (!gfc_compare_actual_formal (ap, comp->ts.interface->formal, 0,
    4781          523 :                               comp->attr.elemental, false, where))
    4782              :     return;
    4783              : 
    4784          523 :   check_intents (comp->ts.interface->formal, *ap);
    4785          523 :   if (warn_aliasing)
    4786            0 :     check_some_aliasing (comp->ts.interface->formal, *ap);
    4787              : }
    4788              : 
    4789              : 
    4790              : /* Try if an actual argument list matches the formal list of a symbol,
    4791              :    respecting the symbol's attributes like ELEMENTAL.  This is used for
    4792              :    GENERIC resolution.  */
    4793              : 
    4794              : bool
    4795        93194 : gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym)
    4796              : {
    4797        93194 :   gfc_formal_arglist *dummy_args;
    4798        93194 :   bool r;
    4799              : 
    4800        93194 :   if (sym->attr.flavor != FL_PROCEDURE)
    4801              :     return false;
    4802              : 
    4803        93190 :   dummy_args = gfc_sym_get_dummy_args (sym);
    4804              : 
    4805        93190 :   r = !sym->attr.elemental;
    4806        93190 :   if (gfc_compare_actual_formal (args, dummy_args, r, !r, false, NULL))
    4807              :     {
    4808        26409 :       check_intents (dummy_args, *args);
    4809        26409 :       if (warn_aliasing)
    4810           12 :         check_some_aliasing (dummy_args, *args);
    4811        26409 :       return true;
    4812              :     }
    4813              : 
    4814              :   return false;
    4815              : }
    4816              : 
    4817              : 
    4818              : /* Given an interface pointer and an actual argument list, search for
    4819              :    a formal argument list that matches the actual.  If found, returns
    4820              :    a pointer to the symbol of the correct interface.  Returns NULL if
    4821              :    not found.  */
    4822              : 
    4823              : gfc_symbol *
    4824        46350 : gfc_search_interface (gfc_interface *intr, int sub_flag,
    4825              :                       gfc_actual_arglist **ap)
    4826              : {
    4827        46350 :   gfc_symbol *elem_sym = NULL;
    4828        46350 :   gfc_symbol *null_sym = NULL;
    4829        46350 :   locus null_expr_loc;
    4830        46350 :   gfc_actual_arglist *a;
    4831        46350 :   bool has_null_arg = false;
    4832              : 
    4833       128693 :   for (a = *ap; a; a = a->next)
    4834        82472 :     if (a->expr && a->expr->expr_type == EXPR_NULL
    4835          175 :         && a->expr->ts.type == BT_UNKNOWN)
    4836              :       {
    4837          129 :         has_null_arg = true;
    4838          129 :         null_expr_loc = a->expr->where;
    4839          129 :         break;
    4840              :       }
    4841              : 
    4842       132710 :   for (; intr; intr = intr->next)
    4843              :     {
    4844        97785 :       if (gfc_fl_struct (intr->sym->attr.flavor))
    4845         6658 :         continue;
    4846        91127 :       if (sub_flag && intr->sym->attr.function)
    4847            0 :         continue;
    4848        84105 :       if (!sub_flag && intr->sym->attr.subroutine)
    4849            0 :         continue;
    4850              : 
    4851        91127 :       if (gfc_arglist_matches_symbol (ap, intr->sym))
    4852              :         {
    4853        25148 :           if (has_null_arg && null_sym)
    4854              :             {
    4855            2 :               gfc_error ("MOLD= required in NULL() argument at %L: Ambiguity "
    4856              :                          "between specific functions %s and %s",
    4857            2 :                          &null_expr_loc, null_sym->name, intr->sym->name);
    4858            2 :               return NULL;
    4859              :             }
    4860        25146 :           else if (has_null_arg)
    4861              :             {
    4862            4 :               null_sym = intr->sym;
    4863            4 :               continue;
    4864              :             }
    4865              : 
    4866              :           /* Satisfy 12.4.4.1 such that an elemental match has lower
    4867              :              weight than a non-elemental match.  */
    4868        25142 :           if (intr->sym->attr.elemental)
    4869              :             {
    4870        13719 :               elem_sym = intr->sym;
    4871        13719 :               continue;
    4872              :             }
    4873              :           return intr->sym;
    4874              :         }
    4875              :     }
    4876              : 
    4877        34925 :   if (null_sym)
    4878            2 :     return null_sym;
    4879              : 
    4880              :   return elem_sym ? elem_sym : NULL;
    4881              : }
    4882              : 
    4883              : 
    4884              : /* Do a brute force recursive search for a symbol.  */
    4885              : 
    4886              : static gfc_symtree *
    4887        58860 : find_symtree0 (gfc_symtree *root, gfc_symbol *sym)
    4888              : {
    4889       113804 :   gfc_symtree * st;
    4890              : 
    4891       113804 :   if (root == NULL || root->n.sym == sym)
    4892              :     return root;
    4893              : 
    4894       112763 :   st = NULL;
    4895       112763 :   if (root->left)
    4896        57753 :     st = find_symtree0 (root->left, sym);
    4897       112763 :   if (root->right && ! st)
    4898              :     st = find_symtree0 (root->right, sym);
    4899              :   return st;
    4900              : }
    4901              : 
    4902              : 
    4903              : /* Find a symtree for a symbol.  */
    4904              : 
    4905              : gfc_symtree *
    4906         5693 : gfc_find_sym_in_symtree (gfc_symbol *sym)
    4907              : {
    4908         5693 :   gfc_symtree *st;
    4909         5693 :   gfc_namespace *ns;
    4910              : 
    4911              :   /* First try to find it by name.  */
    4912         5693 :   gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
    4913         5693 :   if (st && st->n.sym == sym)
    4914              :     return st;
    4915              : 
    4916              :   /* If it's been renamed, resort to a brute-force search.  */
    4917              :   /* TODO: avoid having to do this search.  If the symbol doesn't exist
    4918              :      in the symtree for the current namespace, it should probably be added.  */
    4919         1104 :   for (ns = gfc_current_ns; ns; ns = ns->parent)
    4920              :     {
    4921         1104 :       st = find_symtree0 (ns->sym_root, sym);
    4922         1104 :       if (st)
    4923              :         return st;
    4924              : 
    4925              :       /* Search user-defined operators.  */
    4926           66 :       if (ns->uop_root && sym->attr.function)
    4927              :         {
    4928            3 :           st = find_symtree0 (ns->uop_root, sym);
    4929            3 :           if (st)
    4930              :             return st;
    4931              :         }
    4932              :     }
    4933            0 :   gfc_internal_error ("Unable to find symbol %qs", sym->name);
    4934              :   /* Not reached.  */
    4935              : }
    4936              : 
    4937              : 
    4938              : /* See if the arglist to an operator-call contains a derived-type argument
    4939              :    with a matching type-bound operator.  If so, return the matching specific
    4940              :    procedure defined as operator-target as well as the base-object to use
    4941              :    (which is the found derived-type argument with operator).  The generic
    4942              :    name, if any, is transmitted to the final expression via 'gname'.  */
    4943              : 
    4944              : static gfc_typebound_proc*
    4945        13736 : matching_typebound_op (gfc_expr** tb_base,
    4946              :                        gfc_actual_arglist* args,
    4947              :                        gfc_intrinsic_op op, const char* uop,
    4948              :                        const char ** gname)
    4949              : {
    4950        13736 :   gfc_actual_arglist* base;
    4951              : 
    4952        39447 :   for (base = args; base; base = base->next)
    4953        26539 :     if (base->expr->ts.type == BT_DERIVED || base->expr->ts.type == BT_CLASS)
    4954              :       {
    4955              :         gfc_typebound_proc* tb;
    4956              :         gfc_symbol* derived;
    4957              :         bool result;
    4958              : 
    4959        22504 :         while (base->expr->expr_type == EXPR_OP
    4960        22504 :                && base->expr->value.op.op == INTRINSIC_PARENTHESES)
    4961          123 :           base->expr = base->expr->value.op.op1;
    4962              : 
    4963        22381 :         if (base->expr->ts.type == BT_CLASS)
    4964              :           {
    4965         1936 :             if (!base->expr->ts.u.derived || CLASS_DATA (base->expr) == NULL
    4966         3869 :                 || !gfc_expr_attr (base->expr).class_ok)
    4967           87 :               continue;
    4968         1850 :             derived = CLASS_DATA (base->expr)->ts.u.derived;
    4969              :           }
    4970              :         else
    4971        20444 :           derived = base->expr->ts.u.derived;
    4972              : 
    4973              :         /* A use associated derived type is resolvable during parsing.  */
    4974        22294 :         if (derived && derived->attr.use_assoc && !gfc_current_ns->resolved)
    4975         4125 :           gfc_resolve_symbol (derived);
    4976              : 
    4977        22294 :         if (op == INTRINSIC_USER)
    4978              :           {
    4979          228 :             gfc_symtree* tb_uop;
    4980              : 
    4981          228 :             gcc_assert (uop);
    4982          228 :             tb_uop = gfc_find_typebound_user_op (derived, &result, uop,
    4983              :                                                  false, NULL);
    4984              : 
    4985          228 :             if (tb_uop)
    4986           84 :               tb = tb_uop->n.tb;
    4987              :             else
    4988              :               tb = NULL;
    4989              :           }
    4990              :         else
    4991        22066 :           tb = gfc_find_typebound_intrinsic_op (derived, &result, op,
    4992              :                                                 false, NULL);
    4993              : 
    4994              :         /* This means we hit a PRIVATE operator which is use-associated and
    4995              :            should thus not be seen.  */
    4996        22294 :         if (!result)
    4997        21316 :           tb = NULL;
    4998              : 
    4999              :         /* Look through the super-type hierarchy for a matching specific
    5000              :            binding.  */
    5001        22444 :         for (; tb; tb = tb->overridden)
    5002              :           {
    5003          978 :             gfc_tbp_generic* g;
    5004              : 
    5005          978 :             gcc_assert (tb->is_generic);
    5006         1550 :             for (g = tb->u.generic; g; g = g->next)
    5007              :               {
    5008         1400 :                 gfc_symbol* target;
    5009         1400 :                 gfc_actual_arglist* argcopy;
    5010         1400 :                 bool matches;
    5011              : 
    5012              :                 /* If expression matching comes here during parsing, eg. when
    5013              :                    parsing ASSOCIATE, generic TBPs have not yet been resolved
    5014              :                    and g->specific will not have been set. Wait for expression
    5015              :                    resolution by returning NULL.  */
    5016         1400 :                 if (!g->specific && !gfc_current_ns->resolved)
    5017          828 :                   return NULL;
    5018              : 
    5019         1400 :                 gcc_assert (g->specific);
    5020         1400 :                 if (g->specific->error)
    5021            0 :                   continue;
    5022              : 
    5023         1400 :                 target = g->specific->u.specific->n.sym;
    5024              : 
    5025              :                 /* Check if this arglist matches the formal.  */
    5026         1400 :                 argcopy = gfc_copy_actual_arglist (args);
    5027         1400 :                 matches = gfc_arglist_matches_symbol (&argcopy, target);
    5028         1400 :                 gfc_free_actual_arglist (argcopy);
    5029              : 
    5030              :                 /* Return if we found a match.  */
    5031         1400 :                 if (matches)
    5032              :                   {
    5033          828 :                     *tb_base = base->expr;
    5034          828 :                     *gname = g->specific_st->name;
    5035          828 :                     return g->specific;
    5036              :                   }
    5037              :               }
    5038              :           }
    5039              :       }
    5040              : 
    5041              :   return NULL;
    5042              : }
    5043              : 
    5044              : 
    5045              : /* For the 'actual arglist' of an operator call and a specific typebound
    5046              :    procedure that has been found the target of a type-bound operator, build the
    5047              :    appropriate EXPR_COMPCALL and resolve it.  We take this indirection over
    5048              :    type-bound procedures rather than resolving type-bound operators 'directly'
    5049              :    so that we can reuse the existing logic.  */
    5050              : 
    5051              : static void
    5052          828 : build_compcall_for_operator (gfc_expr* e, gfc_actual_arglist* actual,
    5053              :                              gfc_expr* base, gfc_typebound_proc* target,
    5054              :                              const char *gname)
    5055              : {
    5056          828 :   e->expr_type = EXPR_COMPCALL;
    5057          828 :   e->value.compcall.tbp = target;
    5058          828 :   e->value.compcall.name = gname ? gname : "$op";
    5059          828 :   e->value.compcall.actual = actual;
    5060          828 :   e->value.compcall.base_object = base;
    5061          828 :   e->value.compcall.ignore_pass = 1;
    5062          828 :   e->value.compcall.assign = 0;
    5063          828 :   if (e->ts.type == BT_UNKNOWN
    5064          810 :         && target->function)
    5065              :     {
    5066          361 :       if (target->is_generic)
    5067            0 :         e->ts = target->u.generic->specific->u.specific->n.sym->ts;
    5068              :       else
    5069          361 :         e->ts = target->u.specific->n.sym->ts;
    5070              :     }
    5071          828 : }
    5072              : 
    5073              : 
    5074              : /* This subroutine is called when an expression is being resolved.
    5075              :    The expression node in question is either a user defined operator
    5076              :    or an intrinsic operator with arguments that aren't compatible
    5077              :    with the operator.  This subroutine builds an actual argument list
    5078              :    corresponding to the operands, then searches for a compatible
    5079              :    interface.  If one is found, the expression node is replaced with
    5080              :    the appropriate function call. We use the 'match' enum to specify
    5081              :    whether a replacement has been made or not, or if an error occurred.  */
    5082              : 
    5083              : match
    5084         2233 : gfc_extend_expr (gfc_expr *e)
    5085              : {
    5086         2233 :   gfc_actual_arglist *actual;
    5087         2233 :   gfc_symbol *sym;
    5088         2233 :   gfc_namespace *ns;
    5089         2233 :   gfc_user_op *uop;
    5090         2233 :   gfc_intrinsic_op i;
    5091         2233 :   const char *gname;
    5092         2233 :   gfc_typebound_proc* tbo;
    5093         2233 :   gfc_expr* tb_base;
    5094              : 
    5095         2233 :   sym = NULL;
    5096              : 
    5097         2233 :   actual = gfc_get_actual_arglist ();
    5098         2233 :   actual->expr = e->value.op.op1;
    5099              : 
    5100         2233 :   gname = NULL;
    5101              : 
    5102         2233 :   if (e->value.op.op2 != NULL)
    5103              :     {
    5104         2006 :       actual->next = gfc_get_actual_arglist ();
    5105         2006 :       actual->next->expr = e->value.op.op2;
    5106              :     }
    5107              : 
    5108         2233 :   i = fold_unary_intrinsic (e->value.op.op);
    5109              : 
    5110              :   /* See if we find a matching type-bound operator.  */
    5111         2219 :   if (i == INTRINSIC_USER)
    5112          329 :     tbo = matching_typebound_op (&tb_base, actual,
    5113          329 :                                   i, e->value.op.uop->name, &gname);
    5114              :   else
    5115         1904 :     switch (i)
    5116              :       {
    5117              : #define CHECK_OS_COMPARISON(comp) \
    5118              :   case INTRINSIC_##comp: \
    5119              :   case INTRINSIC_##comp##_OS: \
    5120              :     tbo = matching_typebound_op (&tb_base, actual, \
    5121              :                                  INTRINSIC_##comp, NULL, &gname); \
    5122              :     if (!tbo) \
    5123              :       tbo = matching_typebound_op (&tb_base, actual, \
    5124              :                                    INTRINSIC_##comp##_OS, NULL, &gname); \
    5125              :     break;
    5126          193 :         CHECK_OS_COMPARISON(EQ)
    5127          828 :         CHECK_OS_COMPARISON(NE)
    5128           41 :         CHECK_OS_COMPARISON(GT)
    5129           40 :         CHECK_OS_COMPARISON(GE)
    5130           78 :         CHECK_OS_COMPARISON(LT)
    5131           40 :         CHECK_OS_COMPARISON(LE)
    5132              : #undef CHECK_OS_COMPARISON
    5133              : 
    5134          684 :         default:
    5135          684 :           tbo = matching_typebound_op (&tb_base, actual, i, NULL, &gname);
    5136          684 :           break;
    5137              :       }
    5138              : 
    5139              :   /* If there is a matching typebound-operator, replace the expression with
    5140              :       a call to it and succeed.  */
    5141         2229 :   if (tbo)
    5142              :     {
    5143          379 :       gcc_assert (tb_base);
    5144          379 :       build_compcall_for_operator (e, actual, tb_base, tbo, gname);
    5145              : 
    5146          379 :       if (!gfc_resolve_expr (e))
    5147              :         return MATCH_ERROR;
    5148              :       else
    5149              :         return MATCH_YES;
    5150              :     }
    5151              : 
    5152         1854 :   if (i == INTRINSIC_USER)
    5153              :     {
    5154          279 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
    5155              :         {
    5156          269 :           uop = gfc_find_uop (e->value.op.uop->name, ns);
    5157          269 :           if (uop == NULL)
    5158            6 :             continue;
    5159              : 
    5160          263 :           sym = gfc_search_interface (uop->op, 0, &actual);
    5161          263 :           if (sym != NULL)
    5162              :             break;
    5163              :         }
    5164              :     }
    5165              :   else
    5166              :     {
    5167         1923 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
    5168              :         {
    5169              :           /* Due to the distinction between '==' and '.eq.' and friends, one has
    5170              :              to check if either is defined.  */
    5171         1683 :           switch (i)
    5172              :             {
    5173              : #define CHECK_OS_COMPARISON(comp) \
    5174              :   case INTRINSIC_##comp: \
    5175              :   case INTRINSIC_##comp##_OS: \
    5176              :     sym = gfc_search_interface (ns->op[INTRINSIC_##comp], 0, &actual); \
    5177              :     if (!sym) \
    5178              :       sym = gfc_search_interface (ns->op[INTRINSIC_##comp##_OS], 0, &actual); \
    5179              :     break;
    5180          196 :               CHECK_OS_COMPARISON(EQ)
    5181          872 :               CHECK_OS_COMPARISON(NE)
    5182           41 :               CHECK_OS_COMPARISON(GT)
    5183           40 :               CHECK_OS_COMPARISON(GE)
    5184           65 :               CHECK_OS_COMPARISON(LT)
    5185           40 :               CHECK_OS_COMPARISON(LE)
    5186              : #undef CHECK_OS_COMPARISON
    5187              : 
    5188          429 :               default:
    5189          429 :                 sym = gfc_search_interface (ns->op[i], 0, &actual);
    5190              :             }
    5191              : 
    5192         1449 :           if (sym != NULL)
    5193              :             break;
    5194              :         }
    5195              : 
    5196              :       /* F2018(15.4.3.4.2) requires that the use of unlimited polymorphic
    5197              :          formal arguments does not override the intrinsic uses.  */
    5198         1608 :       gfc_push_suppress_errors ();
    5199         1608 :       if (sym
    5200         1368 :           && (UNLIMITED_POLY (sym->formal->sym)
    5201         1358 :               || (sym->formal->next
    5202         1332 :                   && UNLIMITED_POLY (sym->formal->next->sym)))
    5203         1618 :           && !gfc_check_operator_interface (sym, e->value.op.op, e->where))
    5204            0 :         sym = NULL;
    5205         1608 :       gfc_pop_suppress_errors ();
    5206              :     }
    5207              : 
    5208              :   /* TODO: Do an ambiguity-check and error if multiple matching interfaces are
    5209              :      found rather than just taking the first one and not checking further.  */
    5210              : 
    5211         1854 :   if (sym == NULL)
    5212              :     {
    5213              :       /* Don't use gfc_free_actual_arglist().  */
    5214          250 :       free (actual->next);
    5215          250 :       free (actual);
    5216          250 :       return MATCH_NO;
    5217              :     }
    5218              : 
    5219              :   /* Change the expression node to a function call.  */
    5220         1604 :   e->expr_type = EXPR_FUNCTION;
    5221         1604 :   e->symtree = gfc_find_sym_in_symtree (sym);
    5222         1604 :   e->value.function.actual = actual;
    5223         1604 :   e->value.function.esym = NULL;
    5224         1604 :   e->value.function.isym = NULL;
    5225         1604 :   e->value.function.name = NULL;
    5226         1604 :   e->user_operator = 1;
    5227              : 
    5228         1604 :   if (!gfc_resolve_expr (e))
    5229              :     return MATCH_ERROR;
    5230              : 
    5231              :   return MATCH_YES;
    5232              : }
    5233              : 
    5234              : 
    5235              : /* Tries to replace an assignment code node with a subroutine call to the
    5236              :    subroutine associated with the assignment operator. Return true if the node
    5237              :    was replaced. On false, no error is generated.  */
    5238              : 
    5239              : bool
    5240       287703 : gfc_extend_assign (gfc_code *c, gfc_namespace *ns)
    5241              : {
    5242       287703 :   gfc_actual_arglist *actual;
    5243       287703 :   gfc_expr *lhs, *rhs, *tb_base;
    5244       287703 :   gfc_symbol *sym = NULL;
    5245       287703 :   const char *gname = NULL;
    5246       287703 :   gfc_typebound_proc* tbo;
    5247              : 
    5248       287703 :   lhs = c->expr1;
    5249       287703 :   rhs = c->expr2;
    5250              : 
    5251              :   /* Don't allow an intrinsic assignment with a BOZ rhs to be replaced.  */
    5252       287703 :   if (c->op == EXEC_ASSIGN
    5253       287703 :       && c->expr1->expr_type == EXPR_VARIABLE
    5254       287703 :       && c->expr2->expr_type == EXPR_CONSTANT && c->expr2->ts.type == BT_BOZ)
    5255              :     return false;
    5256              : 
    5257              :   /* Don't allow an intrinsic assignment to be replaced.  */
    5258       279650 :   if (lhs->ts.type != BT_DERIVED && lhs->ts.type != BT_CLASS
    5259       278536 :       && (rhs->rank == 0 || rhs->rank == lhs->rank)
    5260       566212 :       && (lhs->ts.type == rhs->ts.type
    5261         6901 :           || (gfc_numeric_ts (&lhs->ts) && gfc_numeric_ts (&rhs->ts))))
    5262       277413 :     return false;
    5263              : 
    5264        10287 :   actual = gfc_get_actual_arglist ();
    5265        10287 :   actual->expr = lhs;
    5266              : 
    5267        10287 :   actual->next = gfc_get_actual_arglist ();
    5268        10287 :   actual->next->expr = rhs;
    5269              : 
    5270              :   /* TODO: Ambiguity-check, see above for gfc_extend_expr.  */
    5271              : 
    5272              :   /* See if we find a matching type-bound assignment.  */
    5273        10287 :   tbo = matching_typebound_op (&tb_base, actual, INTRINSIC_ASSIGN,
    5274              :                                NULL, &gname);
    5275              : 
    5276        10287 :   if (tbo)
    5277              :     {
    5278              :       /* Success: Replace the expression with a type-bound call.  */
    5279          449 :       gcc_assert (tb_base);
    5280          449 :       c->expr1 = gfc_get_expr ();
    5281          449 :       build_compcall_for_operator (c->expr1, actual, tb_base, tbo, gname);
    5282          449 :       c->expr1->value.compcall.assign = 1;
    5283          449 :       c->expr1->where = c->loc;
    5284          449 :       c->expr2 = NULL;
    5285          449 :       c->op = EXEC_COMPCALL;
    5286          449 :       return true;
    5287              :     }
    5288              : 
    5289              :   /* See if we find an 'ordinary' (non-typebound) assignment procedure.  */
    5290        22989 :   for (; ns; ns = ns->parent)
    5291              :     {
    5292        13620 :       sym = gfc_search_interface (ns->op[INTRINSIC_ASSIGN], 1, &actual);
    5293        13620 :       if (sym != NULL)
    5294              :         break;
    5295              :     }
    5296              : 
    5297         9838 :   if (sym)
    5298              :     {
    5299              :       /* Success: Replace the assignment with the call.  */
    5300          469 :       c->op = EXEC_ASSIGN_CALL;
    5301          469 :       c->symtree = gfc_find_sym_in_symtree (sym);
    5302          469 :       c->expr1 = NULL;
    5303          469 :       c->expr2 = NULL;
    5304          469 :       c->ext.actual = actual;
    5305          469 :       return true;
    5306              :     }
    5307              : 
    5308              :   /* Failure: No assignment procedure found.  */
    5309         9369 :   free (actual->next);
    5310         9369 :   free (actual);
    5311         9369 :   return false;
    5312              : }
    5313              : 
    5314              : 
    5315              : /* Make sure that the interface just parsed is not already present in
    5316              :    the given interface list.  Ambiguity isn't checked yet since module
    5317              :    procedures can be present without interfaces.  */
    5318              : 
    5319              : bool
    5320        10383 : gfc_check_new_interface (gfc_interface *base, gfc_symbol *new_sym, locus loc)
    5321              : {
    5322        10383 :   gfc_interface *ip;
    5323              : 
    5324        20222 :   for (ip = base; ip; ip = ip->next)
    5325              :     {
    5326         9846 :       if (ip->sym == new_sym)
    5327              :         {
    5328            7 :           gfc_error ("Entity %qs at %L is already present in the interface",
    5329              :                      new_sym->name, &loc);
    5330            7 :           return false;
    5331              :         }
    5332              :     }
    5333              : 
    5334              :   return true;
    5335              : }
    5336              : 
    5337              : 
    5338              : /* Add a symbol to the current interface.  */
    5339              : 
    5340              : bool
    5341        19036 : gfc_add_interface (gfc_symbol *new_sym)
    5342              : {
    5343        19036 :   gfc_interface **head, *intr;
    5344        19036 :   gfc_namespace *ns;
    5345        19036 :   gfc_symbol *sym;
    5346              : 
    5347        19036 :   switch (current_interface.type)
    5348              :     {
    5349              :     case INTERFACE_NAMELESS:
    5350              :     case INTERFACE_ABSTRACT:
    5351              :       return true;
    5352              : 
    5353          672 :     case INTERFACE_INTRINSIC_OP:
    5354         1347 :       for (ns = current_interface.ns; ns; ns = ns->parent)
    5355          678 :         switch (current_interface.op)
    5356              :           {
    5357           75 :             case INTRINSIC_EQ:
    5358           75 :             case INTRINSIC_EQ_OS:
    5359           75 :               if (!gfc_check_new_interface (ns->op[INTRINSIC_EQ], new_sym,
    5360              :                                             gfc_current_locus)
    5361           75 :                   || !gfc_check_new_interface (ns->op[INTRINSIC_EQ_OS],
    5362              :                                                new_sym, gfc_current_locus))
    5363            2 :                 return false;
    5364              :               break;
    5365              : 
    5366           44 :             case INTRINSIC_NE:
    5367           44 :             case INTRINSIC_NE_OS:
    5368           44 :               if (!gfc_check_new_interface (ns->op[INTRINSIC_NE], new_sym,
    5369              :                                             gfc_current_locus)
    5370           44 :                   || !gfc_check_new_interface (ns->op[INTRINSIC_NE_OS],
    5371              :                                                new_sym, gfc_current_locus))
    5372            0 :                 return false;
    5373              :               break;
    5374              : 
    5375           19 :             case INTRINSIC_GT:
    5376           19 :             case INTRINSIC_GT_OS:
    5377           19 :               if (!gfc_check_new_interface (ns->op[INTRINSIC_GT],
    5378              :                                             new_sym, gfc_current_locus)
    5379           19 :                   || !gfc_check_new_interface (ns->op[INTRINSIC_GT_OS],
    5380              :                                                new_sym, gfc_current_locus))
    5381            0 :                 return false;
    5382              :               break;
    5383              : 
    5384           17 :             case INTRINSIC_GE:
    5385           17 :             case INTRINSIC_GE_OS:
    5386           17 :               if (!gfc_check_new_interface (ns->op[INTRINSIC_GE],
    5387              :                                             new_sym, gfc_current_locus)
    5388           17 :                   || !gfc_check_new_interface (ns->op[INTRINSIC_GE_OS],
    5389              :                                                new_sym, gfc_current_locus))
    5390            0 :                 return false;
    5391              :               break;
    5392              : 
    5393           29 :             case INTRINSIC_LT:
    5394           29 :             case INTRINSIC_LT_OS:
    5395           29 :               if (!gfc_check_new_interface (ns->op[INTRINSIC_LT],
    5396              :                                             new_sym, gfc_current_locus)
    5397           29 :                   || !gfc_check_new_interface (ns->op[INTRINSIC_LT_OS],
    5398              :                                                new_sym, gfc_current_locus))
    5399            0 :                 return false;
    5400              :               break;
    5401              : 
    5402           17 :             case INTRINSIC_LE:
    5403           17 :             case INTRINSIC_LE_OS:
    5404           17 :               if (!gfc_check_new_interface (ns->op[INTRINSIC_LE],
    5405              :                                             new_sym, gfc_current_locus)
    5406           17 :                   || !gfc_check_new_interface (ns->op[INTRINSIC_LE_OS],
    5407              :                                                new_sym, gfc_current_locus))
    5408            0 :                 return false;
    5409              :               break;
    5410              : 
    5411          477 :             default:
    5412          477 :               if (!gfc_check_new_interface (ns->op[current_interface.op],
    5413              :                                             new_sym, gfc_current_locus))
    5414              :                 return false;
    5415              :           }
    5416              : 
    5417          669 :       head = &current_interface.ns->op[current_interface.op];
    5418          669 :       break;
    5419              : 
    5420         8968 :     case INTERFACE_GENERIC:
    5421         8968 :     case INTERFACE_DTIO:
    5422        17945 :       for (ns = current_interface.ns; ns; ns = ns->parent)
    5423              :         {
    5424         8978 :           gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
    5425         8978 :           if (sym == NULL)
    5426           11 :             continue;
    5427              : 
    5428         8967 :           if (!gfc_check_new_interface (sym->generic,
    5429              :                                         new_sym, gfc_current_locus))
    5430              :             return false;
    5431              :         }
    5432              : 
    5433         8967 :       head = &current_interface.sym->generic;
    5434         8967 :       break;
    5435              : 
    5436          169 :     case INTERFACE_USER_OP:
    5437          169 :       if (!gfc_check_new_interface (current_interface.uop->op,
    5438              :                                     new_sym, gfc_current_locus))
    5439              :         return false;
    5440              : 
    5441          168 :       head = &current_interface.uop->op;
    5442          168 :       break;
    5443              : 
    5444            0 :     default:
    5445            0 :       gfc_internal_error ("gfc_add_interface(): Bad interface type");
    5446              :     }
    5447              : 
    5448         9804 :   intr = gfc_get_interface ();
    5449         9804 :   intr->sym = new_sym;
    5450         9804 :   intr->where = gfc_current_locus;
    5451              : 
    5452         9804 :   intr->next = *head;
    5453         9804 :   *head = intr;
    5454              : 
    5455         9804 :   return true;
    5456              : }
    5457              : 
    5458              : 
    5459              : gfc_interface *&
    5460        94951 : gfc_current_interface_head (void)
    5461              : {
    5462        94951 :   switch (current_interface.type)
    5463              :     {
    5464        12183 :       case INTERFACE_INTRINSIC_OP:
    5465        12183 :         return current_interface.ns->op[current_interface.op];
    5466              : 
    5467        79868 :       case INTERFACE_GENERIC:
    5468        79868 :       case INTERFACE_DTIO:
    5469        79868 :         return current_interface.sym->generic;
    5470              : 
    5471         2900 :       case INTERFACE_USER_OP:
    5472         2900 :         return current_interface.uop->op;
    5473              : 
    5474            0 :       default:
    5475            0 :         gcc_unreachable ();
    5476              :     }
    5477              : }
    5478              : 
    5479              : 
    5480              : void
    5481            3 : gfc_set_current_interface_head (gfc_interface *i)
    5482              : {
    5483            3 :   switch (current_interface.type)
    5484              :     {
    5485            0 :       case INTERFACE_INTRINSIC_OP:
    5486            0 :         current_interface.ns->op[current_interface.op] = i;
    5487            0 :         break;
    5488              : 
    5489            3 :       case INTERFACE_GENERIC:
    5490            3 :       case INTERFACE_DTIO:
    5491            3 :         current_interface.sym->generic = i;
    5492            3 :         break;
    5493              : 
    5494            0 :       case INTERFACE_USER_OP:
    5495            0 :         current_interface.uop->op = i;
    5496            0 :         break;
    5497              : 
    5498            0 :       default:
    5499            0 :         gcc_unreachable ();
    5500              :     }
    5501            3 : }
    5502              : 
    5503              : 
    5504              : /* Gets rid of a formal argument list.  We do not free symbols.
    5505              :    Symbols are freed when a namespace is freed.  */
    5506              : 
    5507              : void
    5508      6409414 : gfc_free_formal_arglist (gfc_formal_arglist *p)
    5509              : {
    5510      6409414 :   gfc_formal_arglist *q;
    5511              : 
    5512      7177909 :   for (; p; p = q)
    5513              :     {
    5514       768495 :       q = p->next;
    5515       768495 :       free (p);
    5516              :     }
    5517      6409414 : }
    5518              : 
    5519              : 
    5520              : /* Check that it is ok for the type-bound procedure 'proc' to override the
    5521              :    procedure 'old', cf. F08:4.5.7.3.  */
    5522              : 
    5523              : bool
    5524         1218 : gfc_check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
    5525              : {
    5526         1218 :   locus where;
    5527         1218 :   gfc_symbol *proc_target, *old_target;
    5528         1218 :   unsigned proc_pass_arg, old_pass_arg, argpos;
    5529         1218 :   gfc_formal_arglist *proc_formal, *old_formal;
    5530         1218 :   bool check_type;
    5531         1218 :   char err[200];
    5532              : 
    5533              :   /* This procedure should only be called for non-GENERIC proc.  */
    5534         1218 :   gcc_assert (!proc->n.tb->is_generic);
    5535              : 
    5536              :   /* If the overwritten procedure is GENERIC, this is an error.  */
    5537         1218 :   if (old->n.tb->is_generic)
    5538              :     {
    5539            1 :       gfc_error ("Cannot overwrite GENERIC %qs at %L",
    5540              :                  old->name, &proc->n.tb->where);
    5541            1 :       return false;
    5542              :     }
    5543              : 
    5544         1217 :   where = proc->n.tb->where;
    5545         1217 :   proc_target = proc->n.tb->u.specific->n.sym;
    5546         1217 :   old_target = old->n.tb->u.specific->n.sym;
    5547              : 
    5548              :   /* Check that overridden binding is not NON_OVERRIDABLE.  */
    5549         1217 :   if (old->n.tb->non_overridable)
    5550              :     {
    5551            1 :       gfc_error ("%qs at %L overrides a procedure binding declared"
    5552              :                  " NON_OVERRIDABLE", proc->name, &where);
    5553            1 :       return false;
    5554              :     }
    5555              : 
    5556              :   /* It's an error to override a non-DEFERRED procedure with a DEFERRED one.  */
    5557         1216 :   if (!old->n.tb->deferred && proc->n.tb->deferred)
    5558              :     {
    5559            1 :       gfc_error ("%qs at %L must not be DEFERRED as it overrides a"
    5560              :                  " non-DEFERRED binding", proc->name, &where);
    5561            1 :       return false;
    5562              :     }
    5563              : 
    5564              :   /* If the overridden binding is PURE, the overriding must be, too.  */
    5565         1215 :   if (old_target->attr.pure && !proc_target->attr.pure)
    5566              :     {
    5567            2 :       gfc_error ("%qs at %L overrides a PURE procedure and must also be PURE",
    5568              :                  proc->name, &where);
    5569            2 :       return false;
    5570              :     }
    5571              : 
    5572              :   /* If the overridden binding is ELEMENTAL, the overriding must be, too.  If it
    5573              :      is not, the overriding must not be either.  */
    5574         1213 :   if (old_target->attr.elemental && !proc_target->attr.elemental)
    5575              :     {
    5576            0 :       gfc_error ("%qs at %L overrides an ELEMENTAL procedure and must also be"
    5577              :                  " ELEMENTAL", proc->name, &where);
    5578            0 :       return false;
    5579              :     }
    5580         1213 :   if (!old_target->attr.elemental && proc_target->attr.elemental)
    5581              :     {
    5582            1 :       gfc_error ("%qs at %L overrides a non-ELEMENTAL procedure and must not"
    5583              :                  " be ELEMENTAL, either", proc->name, &where);
    5584            1 :       return false;
    5585              :     }
    5586              : 
    5587              :   /* If the overridden binding is a SUBROUTINE, the overriding must also be a
    5588              :      SUBROUTINE.  */
    5589         1212 :   if (old_target->attr.subroutine && !proc_target->attr.subroutine)
    5590              :     {
    5591            1 :       gfc_error ("%qs at %L overrides a SUBROUTINE and must also be a"
    5592              :                  " SUBROUTINE", proc->name, &where);
    5593            1 :       return false;
    5594              :     }
    5595              : 
    5596              :   /* If the overridden binding is a FUNCTION, the overriding must also be a
    5597              :      FUNCTION and have the same characteristics.  */
    5598         1211 :   if (old_target->attr.function)
    5599              :     {
    5600          661 :       if (!proc_target->attr.function)
    5601              :         {
    5602            1 :           gfc_error ("%qs at %L overrides a FUNCTION and must also be a"
    5603              :                      " FUNCTION", proc->name, &where);
    5604            1 :           return false;
    5605              :         }
    5606              : 
    5607          660 :       if (!gfc_check_result_characteristics (proc_target, old_target,
    5608              :                                              err, sizeof(err)))
    5609              :         {
    5610            6 :           gfc_error ("Result mismatch for the overriding procedure "
    5611              :                      "%qs at %L: %s", proc->name, &where, err);
    5612            6 :           return false;
    5613              :         }
    5614              :     }
    5615              : 
    5616              :   /* If the overridden binding is PUBLIC, the overriding one must not be
    5617              :      PRIVATE.  */
    5618         1204 :   if (old->n.tb->access == ACCESS_PUBLIC
    5619         1179 :       && proc->n.tb->access == ACCESS_PRIVATE)
    5620              :     {
    5621            1 :       gfc_error ("%qs at %L overrides a PUBLIC procedure and must not be"
    5622              :                  " PRIVATE", proc->name, &where);
    5623            1 :       return false;
    5624              :     }
    5625              : 
    5626              :   /* Compare the formal argument lists of both procedures.  This is also abused
    5627              :      to find the position of the passed-object dummy arguments of both
    5628              :      bindings as at least the overridden one might not yet be resolved and we
    5629              :      need those positions in the check below.  */
    5630         1203 :   proc_pass_arg = old_pass_arg = 0;
    5631         1203 :   if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
    5632         1203 :     proc_pass_arg = 1;
    5633         1203 :   if (!old->n.tb->nopass && !old->n.tb->pass_arg)
    5634         1203 :     old_pass_arg = 1;
    5635         1203 :   argpos = 1;
    5636         1203 :   proc_formal = gfc_sym_get_dummy_args (proc_target);
    5637         1203 :   old_formal = gfc_sym_get_dummy_args (old_target);
    5638         4342 :   for ( ; proc_formal && old_formal;
    5639         1936 :        proc_formal = proc_formal->next, old_formal = old_formal->next)
    5640              :     {
    5641         1943 :       if (proc->n.tb->pass_arg
    5642          493 :           && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
    5643         1943 :         proc_pass_arg = argpos;
    5644         1943 :       if (old->n.tb->pass_arg
    5645          495 :           && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
    5646         1943 :         old_pass_arg = argpos;
    5647              : 
    5648              :       /* Check that the names correspond.  */
    5649         1943 :       if (strcmp (proc_formal->sym->name, old_formal->sym->name))
    5650              :         {
    5651            1 :           gfc_error ("Dummy argument %qs of %qs at %L should be named %qs as"
    5652              :                      " to match the corresponding argument of the overridden"
    5653              :                      " procedure", proc_formal->sym->name, proc->name, &where,
    5654              :                      old_formal->sym->name);
    5655            1 :           return false;
    5656              :         }
    5657              : 
    5658         1942 :       check_type = proc_pass_arg != argpos && old_pass_arg != argpos;
    5659         1942 :       if (!gfc_check_dummy_characteristics (proc_formal->sym, old_formal->sym,
    5660              :                                         check_type, err, sizeof(err)))
    5661              :         {
    5662            6 :           gfc_error_opt (0, "Argument mismatch for the overriding procedure "
    5663              :                          "%qs at %L: %s", proc->name, &where, err);
    5664            6 :           return false;
    5665              :         }
    5666              : 
    5667         1936 :       ++argpos;
    5668              :     }
    5669         1196 :   if (proc_formal || old_formal)
    5670              :     {
    5671            1 :       gfc_error ("%qs at %L must have the same number of formal arguments as"
    5672              :                  " the overridden procedure", proc->name, &where);
    5673            1 :       return false;
    5674              :     }
    5675              : 
    5676              :   /* If the overridden binding is NOPASS, the overriding one must also be
    5677              :      NOPASS.  */
    5678         1195 :   if (old->n.tb->nopass && !proc->n.tb->nopass)
    5679              :     {
    5680            1 :       gfc_error ("%qs at %L overrides a NOPASS binding and must also be"
    5681              :                  " NOPASS", proc->name, &where);
    5682            1 :       return false;
    5683              :     }
    5684              : 
    5685              :   /* If the overridden binding is PASS(x), the overriding one must also be
    5686              :      PASS and the passed-object dummy arguments must correspond.  */
    5687         1194 :   if (!old->n.tb->nopass)
    5688              :     {
    5689         1160 :       if (proc->n.tb->nopass)
    5690              :         {
    5691            1 :           gfc_error ("%qs at %L overrides a binding with PASS and must also be"
    5692              :                      " PASS", proc->name, &where);
    5693            1 :           return false;
    5694              :         }
    5695              : 
    5696         1159 :       if (proc_pass_arg != old_pass_arg)
    5697              :         {
    5698            1 :           gfc_error ("Passed-object dummy argument of %qs at %L must be at"
    5699              :                      " the same position as the passed-object dummy argument of"
    5700              :                      " the overridden procedure", proc->name, &where);
    5701            1 :           return false;
    5702              :         }
    5703              :     }
    5704              : 
    5705              :   return true;
    5706              : }
    5707              : 
    5708              : 
    5709              : /* The following three functions check that the formal arguments
    5710              :    of user defined derived type IO procedures are compliant with
    5711              :    the requirements of the standard, see F03:9.5.3.7.2 (F08:9.6.4.8.3).  */
    5712              : 
    5713              : static void
    5714         4572 : check_dtio_arg_TKR_intent (gfc_symbol *fsym, bool typebound, bt type,
    5715              :                            int kind, int rank, sym_intent intent)
    5716              : {
    5717         4572 :   if (fsym->ts.type != type)
    5718              :     {
    5719            3 :       gfc_error ("DTIO dummy argument at %L must be of type %s",
    5720              :                  &fsym->declared_at, gfc_basic_typename (type));
    5721            3 :       return;
    5722              :     }
    5723              : 
    5724         4569 :   if (fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED
    5725         3767 :       && fsym->ts.kind != kind)
    5726            1 :     gfc_error ("DTIO dummy argument at %L must be of KIND = %d",
    5727              :                &fsym->declared_at, kind);
    5728              : 
    5729         4569 :   if (!typebound
    5730         4569 :       && rank == 0
    5731         1148 :       && (((type == BT_CLASS) && CLASS_DATA (fsym)->attr.dimension)
    5732          950 :           || ((type != BT_CLASS) && fsym->attr.dimension)))
    5733            0 :     gfc_error ("DTIO dummy argument at %L must be a scalar",
    5734              :                &fsym->declared_at);
    5735         4569 :   else if (rank == 1
    5736          677 :            && (fsym->as == NULL || fsym->as->type != AS_ASSUMED_SHAPE))
    5737            1 :     gfc_error ("DTIO dummy argument at %L must be an "
    5738              :                "ASSUMED SHAPE ARRAY", &fsym->declared_at);
    5739              : 
    5740         4569 :   if (type == BT_CHARACTER && fsym->ts.u.cl->length != NULL)
    5741            1 :     gfc_error ("DTIO character argument at %L must have assumed length",
    5742              :                &fsym->declared_at);
    5743              : 
    5744         4569 :   if (fsym->attr.intent != intent)
    5745            1 :     gfc_error ("DTIO dummy argument at %L must have INTENT %s",
    5746              :                &fsym->declared_at, gfc_code2string (intents, (int)intent));
    5747              :   return;
    5748              : }
    5749              : 
    5750              : 
    5751              : static void
    5752          889 : check_dtio_interface1 (gfc_symbol *derived, gfc_symtree *tb_io_st,
    5753              :                        bool typebound, bool formatted, int code)
    5754              : {
    5755          889 :   gfc_symbol *dtio_sub, *generic_proc, *fsym;
    5756          889 :   gfc_typebound_proc *tb_io_proc, *specific_proc;
    5757          889 :   gfc_interface *intr;
    5758          889 :   gfc_formal_arglist *formal;
    5759          889 :   int arg_num;
    5760              : 
    5761          889 :   bool read = ((dtio_codes)code == DTIO_RF)
    5762          889 :                || ((dtio_codes)code == DTIO_RUF);
    5763          889 :   bt type;
    5764          889 :   sym_intent intent;
    5765          889 :   int kind;
    5766              : 
    5767          889 :   dtio_sub = NULL;
    5768          889 :   if (typebound)
    5769              :     {
    5770              :       /* Typebound DTIO binding.  */
    5771          559 :       tb_io_proc = tb_io_st->n.tb;
    5772          559 :       if (tb_io_proc == NULL)
    5773              :         return;
    5774              : 
    5775          559 :       gcc_assert (tb_io_proc->is_generic);
    5776              : 
    5777          559 :       specific_proc = tb_io_proc->u.generic->specific;
    5778          559 :       if (specific_proc == NULL || specific_proc->is_generic)
    5779              :         return;
    5780              : 
    5781          559 :       dtio_sub = specific_proc->u.specific->n.sym;
    5782              :     }
    5783              :   else
    5784              :     {
    5785          330 :       generic_proc = tb_io_st->n.sym;
    5786          330 :       if (generic_proc == NULL || generic_proc->generic == NULL)
    5787              :         return;
    5788              : 
    5789          407 :       for (intr = tb_io_st->n.sym->generic; intr; intr = intr->next)
    5790              :         {
    5791          334 :           if (intr->sym && intr->sym->formal && intr->sym->formal->sym
    5792          330 :               && ((intr->sym->formal->sym->ts.type == BT_CLASS
    5793          231 :                    && CLASS_DATA (intr->sym->formal->sym)->ts.u.derived
    5794              :                                                              == derived)
    5795          127 :                   || (intr->sym->formal->sym->ts.type == BT_DERIVED
    5796           99 :                       && intr->sym->formal->sym->ts.u.derived == derived)))
    5797              :             {
    5798              :               dtio_sub = intr->sym;
    5799              :               break;
    5800              :             }
    5801           80 :           else if (intr->sym && intr->sym->formal && !intr->sym->formal->sym)
    5802              :             {
    5803            1 :               gfc_error ("Alternate return at %L is not permitted in a DTIO "
    5804              :                          "procedure", &intr->sym->declared_at);
    5805            1 :               return;
    5806              :             }
    5807              :         }
    5808              : 
    5809          327 :       if (dtio_sub == NULL)
    5810              :         return;
    5811              :     }
    5812              : 
    5813          559 :   gcc_assert (dtio_sub);
    5814          813 :   if (!dtio_sub->attr.subroutine)
    5815            0 :     gfc_error ("DTIO procedure %qs at %L must be a subroutine",
    5816              :                dtio_sub->name, &dtio_sub->declared_at);
    5817              : 
    5818          813 :   if (!dtio_sub->resolve_symbol_called)
    5819            1 :     gfc_resolve_formal_arglist (dtio_sub);
    5820              : 
    5821          813 :   arg_num = 0;
    5822         5416 :   for (formal = dtio_sub->formal; formal; formal = formal->next)
    5823         4603 :     arg_num++;
    5824              : 
    5825          944 :   if (arg_num < (formatted ? 6 : 4))
    5826              :     {
    5827            5 :       gfc_error ("Too few dummy arguments in DTIO procedure %qs at %L",
    5828              :                  dtio_sub->name, &dtio_sub->declared_at);
    5829            5 :       return;
    5830              :     }
    5831              : 
    5832          808 :   if (arg_num > (formatted ? 6 : 4))
    5833              :     {
    5834            3 :       gfc_error ("Too many dummy arguments in DTIO procedure %qs at %L",
    5835              :                  dtio_sub->name, &dtio_sub->declared_at);
    5836            3 :       return;
    5837              :     }
    5838              : 
    5839              :   /* Now go through the formal arglist.  */
    5840              :   arg_num = 1;
    5841         5377 :   for (formal = dtio_sub->formal; formal; formal = formal->next, arg_num++)
    5842              :     {
    5843         4573 :       if (!formatted && arg_num == 3)
    5844          128 :         arg_num = 5;
    5845         4573 :       fsym = formal->sym;
    5846              : 
    5847         4573 :       if (fsym == NULL)
    5848              :         {
    5849            1 :           gfc_error ("Alternate return at %L is not permitted in a DTIO "
    5850              :                      "procedure", &dtio_sub->declared_at);
    5851            1 :           return;
    5852              :         }
    5853              : 
    5854         4572 :       switch (arg_num)
    5855              :         {
    5856          805 :         case(1):                        /* DTV  */
    5857          805 :           type = derived->attr.sequence || derived->attr.is_bind_c ?
    5858              :                  BT_DERIVED : BT_CLASS;
    5859          805 :           kind = 0;
    5860          805 :           intent = read ? INTENT_INOUT : INTENT_IN;
    5861          805 :           check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
    5862              :                                      0, intent);
    5863          805 :           break;
    5864              : 
    5865          805 :         case(2):                        /* UNIT  */
    5866          805 :           type = BT_INTEGER;
    5867          805 :           kind = gfc_default_integer_kind;
    5868          805 :           intent = INTENT_IN;
    5869          805 :           check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
    5870              :                                      0, intent);
    5871          805 :           break;
    5872          677 :         case(3):                        /* IOTYPE  */
    5873          677 :           type = BT_CHARACTER;
    5874          677 :           kind = gfc_default_character_kind;
    5875          677 :           intent = INTENT_IN;
    5876          677 :           check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
    5877              :                                      0, intent);
    5878          677 :           break;
    5879          677 :         case(4):                        /* VLIST  */
    5880          677 :           type = BT_INTEGER;
    5881          677 :           kind = gfc_default_integer_kind;
    5882          677 :           intent = INTENT_IN;
    5883          677 :           check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
    5884              :                                      1, intent);
    5885          677 :           break;
    5886          804 :         case(5):                        /* IOSTAT  */
    5887          804 :           type = BT_INTEGER;
    5888          804 :           kind = gfc_default_integer_kind;
    5889          804 :           intent = INTENT_OUT;
    5890          804 :           check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
    5891              :                                      0, intent);
    5892          804 :           break;
    5893          804 :         case(6):                        /* IOMSG  */
    5894          804 :           type = BT_CHARACTER;
    5895          804 :           kind = gfc_default_character_kind;
    5896          804 :           intent = INTENT_INOUT;
    5897          804 :           check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
    5898              :                                      0, intent);
    5899          804 :           break;
    5900            0 :         default:
    5901            0 :           gcc_unreachable ();
    5902              :         }
    5903              :     }
    5904          804 :   derived->attr.has_dtio_procs = 1;
    5905          804 :   return;
    5906              : }
    5907              : 
    5908              : void
    5909        94832 : gfc_check_dtio_interfaces (gfc_symbol *derived)
    5910              : {
    5911        94832 :   gfc_symtree *tb_io_st;
    5912        94832 :   bool t = false;
    5913        94832 :   int code;
    5914        94832 :   bool formatted;
    5915              : 
    5916        94832 :   if (derived->attr.is_class == 1 || derived->attr.vtype == 1)
    5917        37191 :     return;
    5918              : 
    5919              :   /* Check typebound DTIO bindings.  */
    5920       288205 :   for (code = 0; code < 4; code++)
    5921              :     {
    5922       230564 :       formatted = ((dtio_codes)code == DTIO_RF)
    5923              :                    || ((dtio_codes)code == DTIO_WF);
    5924              : 
    5925       230564 :       tb_io_st = gfc_find_typebound_proc (derived, &t,
    5926              :                                           gfc_code2string (dtio_procs, code),
    5927              :                                           true, &derived->declared_at);
    5928       230564 :       if (tb_io_st != NULL)
    5929          559 :         check_dtio_interface1 (derived, tb_io_st, true, formatted, code);
    5930              :     }
    5931              : 
    5932              :   /* Check generic DTIO interfaces.  */
    5933       288205 :   for (code = 0; code < 4; code++)
    5934              :     {
    5935       230564 :       formatted = ((dtio_codes)code == DTIO_RF)
    5936              :                    || ((dtio_codes)code == DTIO_WF);
    5937              : 
    5938       230564 :       tb_io_st = gfc_find_symtree (derived->ns->sym_root,
    5939              :                                    gfc_code2string (dtio_procs, code));
    5940       230564 :       if (tb_io_st != NULL)
    5941          330 :         check_dtio_interface1 (derived, tb_io_st, false, formatted, code);
    5942              :     }
    5943              : }
    5944              : 
    5945              : 
    5946              : gfc_symtree*
    5947         4367 : gfc_find_typebound_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
    5948              : {
    5949         4367 :   gfc_symtree *tb_io_st = NULL;
    5950         4367 :   bool t = false;
    5951              : 
    5952         4367 :   if (!derived || !derived->resolve_symbol_called
    5953         4367 :       || derived->attr.flavor != FL_DERIVED)
    5954              :     return NULL;
    5955              : 
    5956              :   /* Try to find a typebound DTIO binding.  */
    5957         4361 :   if (formatted == true)
    5958              :     {
    5959         4116 :       if (write == true)
    5960         1929 :         tb_io_st = gfc_find_typebound_proc (derived, &t,
    5961              :                                             gfc_code2string (dtio_procs,
    5962              :                                                              DTIO_WF),
    5963              :                                             true,
    5964              :                                             &derived->declared_at);
    5965              :       else
    5966         2187 :         tb_io_st = gfc_find_typebound_proc (derived, &t,
    5967              :                                             gfc_code2string (dtio_procs,
    5968              :                                                              DTIO_RF),
    5969              :                                             true,
    5970              :                                             &derived->declared_at);
    5971              :     }
    5972              :   else
    5973              :     {
    5974          245 :       if (write == true)
    5975          109 :         tb_io_st = gfc_find_typebound_proc (derived, &t,
    5976              :                                             gfc_code2string (dtio_procs,
    5977              :                                                              DTIO_WUF),
    5978              :                                             true,
    5979              :                                             &derived->declared_at);
    5980              :       else
    5981          136 :         tb_io_st = gfc_find_typebound_proc (derived, &t,
    5982              :                                             gfc_code2string (dtio_procs,
    5983              :                                                              DTIO_RUF),
    5984              :                                             true,
    5985              :                                             &derived->declared_at);
    5986              :     }
    5987              :   return tb_io_st;
    5988              : }
    5989              : 
    5990              : 
    5991              : gfc_symbol *
    5992         2919 : gfc_find_specific_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
    5993              : {
    5994         2919 :   gfc_symtree *tb_io_st = NULL;
    5995         2919 :   gfc_symbol *dtio_sub = NULL;
    5996         2919 :   gfc_symbol *extended;
    5997         2919 :   gfc_typebound_proc *tb_io_proc, *specific_proc;
    5998              : 
    5999         2919 :   tb_io_st = gfc_find_typebound_dtio_proc (derived, write, formatted);
    6000              : 
    6001         2919 :   if (tb_io_st != NULL)
    6002              :     {
    6003          860 :       const char *genname;
    6004          860 :       gfc_symtree *st;
    6005              : 
    6006          860 :       tb_io_proc = tb_io_st->n.tb;
    6007          860 :       gcc_assert (tb_io_proc != NULL);
    6008          860 :       gcc_assert (tb_io_proc->is_generic);
    6009          860 :       gcc_assert (tb_io_proc->u.generic->next == NULL);
    6010              : 
    6011          860 :       specific_proc = tb_io_proc->u.generic->specific;
    6012          860 :       gcc_assert (!specific_proc->is_generic);
    6013              : 
    6014              :       /* Go back and make sure that we have the right specific procedure.
    6015              :          Here we most likely have a procedure from the parent type, which
    6016              :          can be overridden in extensions.  */
    6017          860 :       genname = tb_io_proc->u.generic->specific_st->name;
    6018          860 :       st = gfc_find_typebound_proc (derived, NULL, genname,
    6019              :                                     true, &tb_io_proc->where);
    6020          860 :       if (st)
    6021          860 :         dtio_sub = st->n.tb->u.specific->n.sym;
    6022              :       else
    6023            0 :         dtio_sub = specific_proc->u.specific->n.sym;
    6024              : 
    6025          860 :       goto finish;
    6026              :     }
    6027              : 
    6028              :   /* If there is not a typebound binding, look for a generic
    6029              :      DTIO interface.  */
    6030         4197 :   for (extended = derived; extended;
    6031         2138 :        extended = gfc_get_derived_super_type (extended))
    6032              :     {
    6033         2138 :       if (extended == NULL || extended->ns == NULL
    6034         2138 :           || extended->attr.flavor == FL_UNKNOWN)
    6035              :         return NULL;
    6036              : 
    6037         2138 :       if (formatted == true)
    6038              :         {
    6039         2051 :           if (write == true)
    6040          928 :             tb_io_st = gfc_find_symtree (extended->ns->sym_root,
    6041              :                                          gfc_code2string (dtio_procs,
    6042              :                                                           DTIO_WF));
    6043              :           else
    6044         1123 :             tb_io_st = gfc_find_symtree (extended->ns->sym_root,
    6045              :                                          gfc_code2string (dtio_procs,
    6046              :                                                           DTIO_RF));
    6047              :         }
    6048              :       else
    6049              :         {
    6050           87 :           if (write == true)
    6051           37 :             tb_io_st = gfc_find_symtree (extended->ns->sym_root,
    6052              :                                          gfc_code2string (dtio_procs,
    6053              :                                                           DTIO_WUF));
    6054              :           else
    6055           50 :             tb_io_st = gfc_find_symtree (extended->ns->sym_root,
    6056              :                                          gfc_code2string (dtio_procs,
    6057              :                                                           DTIO_RUF));
    6058              :         }
    6059              : 
    6060         2138 :       if (tb_io_st != NULL
    6061          269 :           && tb_io_st->n.sym
    6062          269 :           && tb_io_st->n.sym->generic)
    6063              :         {
    6064           26 :           for (gfc_interface *intr = tb_io_st->n.sym->generic;
    6065          295 :                intr && intr->sym; intr = intr->next)
    6066              :             {
    6067          273 :               if (intr->sym->formal)
    6068              :                 {
    6069          268 :                   gfc_symbol *fsym = intr->sym->formal->sym;
    6070          268 :                   if ((fsym->ts.type == BT_CLASS
    6071          218 :                       && CLASS_DATA (fsym)->ts.u.derived == extended)
    6072           71 :                       || (fsym->ts.type == BT_DERIVED
    6073           50 :                           && fsym->ts.u.derived == extended))
    6074              :                     {
    6075              :                       dtio_sub = intr->sym;
    6076              :                       break;
    6077              :                     }
    6078              :                 }
    6079              :             }
    6080              :         }
    6081              :     }
    6082              : 
    6083         2059 : finish:
    6084         2919 :   if (dtio_sub
    6085         1107 :       && dtio_sub->formal->sym->ts.type == BT_CLASS
    6086         1057 :       && derived != CLASS_DATA (dtio_sub->formal->sym)->ts.u.derived)
    6087           97 :     gfc_find_derived_vtab (derived);
    6088              : 
    6089              :   return dtio_sub;
    6090              : }
    6091              : 
    6092              : /* Helper function - if we do not find an interface for a procedure,
    6093              :    construct it from the actual arglist.  Luckily, this can only
    6094              :    happen for call by reference, so the information we actually need
    6095              :    to provide (and which would be impossible to guess from the call
    6096              :    itself) is not actually needed.  */
    6097              : 
    6098              : void
    6099         1993 : gfc_get_formal_from_actual_arglist (gfc_symbol *sym,
    6100              :                                     gfc_actual_arglist *actual_args)
    6101              : {
    6102         1993 :   gfc_actual_arglist *a;
    6103         1993 :   gfc_formal_arglist **f;
    6104         1993 :   gfc_symbol *s;
    6105         1993 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    6106         1993 :   static int var_num;
    6107              : 
    6108              :   /* Do not infer the formal from actual arguments if we are dealing with
    6109              :      classes.  */
    6110              : 
    6111         1993 :   if (sym->ts.type == BT_CLASS)
    6112            1 :     return;
    6113              : 
    6114         1992 :   f = &sym->formal;
    6115         5974 :   for (a = actual_args; a != NULL; a = a->next)
    6116              :     {
    6117         3982 :       (*f) = gfc_get_formal_arglist ();
    6118         3982 :       if (a->expr)
    6119              :         {
    6120         3974 :           snprintf (name, GFC_MAX_SYMBOL_LEN, "_formal_%d", var_num ++);
    6121         3974 :           gfc_get_symbol (name, gfc_current_ns, &s);
    6122         3974 :           if (a->expr->ts.type == BT_PROCEDURE)
    6123              :             {
    6124           44 :               gfc_symbol *asym = a->expr->symtree->n.sym;
    6125           44 :               s->attr.flavor = FL_PROCEDURE;
    6126           44 :               if (asym->attr.function)
    6127              :                 {
    6128           24 :                   s->attr.function = 1;
    6129           24 :                   s->ts = asym->ts;
    6130              :                 }
    6131           44 :               s->attr.subroutine = asym->attr.subroutine;
    6132              :             }
    6133              :           else
    6134              :             {
    6135         3930 :               s->ts = a->expr->ts;
    6136              : 
    6137         3930 :               if (s->ts.type == BT_CHARACTER)
    6138          180 :                 s->ts.u.cl = gfc_get_charlen ();
    6139              : 
    6140         3930 :               s->ts.deferred = 0;
    6141         3930 :               s->ts.is_iso_c = 0;
    6142         3930 :               s->ts.is_c_interop = 0;
    6143         3930 :               s->attr.flavor = FL_VARIABLE;
    6144         3930 :               if (a->expr->rank > 0)
    6145              :                 {
    6146          872 :                   s->attr.dimension = 1;
    6147          872 :                   s->as = gfc_get_array_spec ();
    6148          872 :                   s->as->rank = 1;
    6149         1744 :                   s->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
    6150          872 :                                                       &a->expr->where, 1);
    6151          872 :                   s->as->upper[0] = NULL;
    6152          872 :                   s->as->type = AS_ASSUMED_SIZE;
    6153              :                 }
    6154              :               else
    6155         3058 :                 s->maybe_array = maybe_dummy_array_arg (a->expr);
    6156              :             }
    6157         3974 :           s->attr.dummy = 1;
    6158         3974 :           s->attr.artificial = 1;
    6159         3974 :           s->declared_at = a->expr->where;
    6160         3974 :           s->attr.intent = INTENT_UNKNOWN;
    6161         3974 :           (*f)->sym = s;
    6162         3974 :           gfc_commit_symbol (s);
    6163              :         }
    6164              :       else  /* If a->expr is NULL, this is an alternate rerturn.  */
    6165            8 :         (*f)->sym = NULL;
    6166              : 
    6167         3982 :       f = &((*f)->next);
    6168              :     }
    6169              : 
    6170              : }
    6171              : 
    6172              : 
    6173              : const char *
    6174          241 : gfc_dummy_arg_get_name (gfc_dummy_arg & dummy_arg)
    6175              : {
    6176          241 :   switch (dummy_arg.intrinsicness)
    6177              :     {
    6178          241 :     case GFC_INTRINSIC_DUMMY_ARG:
    6179          241 :       return dummy_arg.u.intrinsic->name;
    6180              : 
    6181            0 :     case GFC_NON_INTRINSIC_DUMMY_ARG:
    6182            0 :       return dummy_arg.u.non_intrinsic->sym->name;
    6183              : 
    6184            0 :     default:
    6185            0 :       gcc_unreachable ();
    6186              :     }
    6187              : }
    6188              : 
    6189              : 
    6190              : const gfc_typespec &
    6191         2460 : gfc_dummy_arg_get_typespec (gfc_dummy_arg & dummy_arg)
    6192              : {
    6193         2460 :   switch (dummy_arg.intrinsicness)
    6194              :     {
    6195         1352 :     case GFC_INTRINSIC_DUMMY_ARG:
    6196         1352 :       return dummy_arg.u.intrinsic->ts;
    6197              : 
    6198         1108 :     case GFC_NON_INTRINSIC_DUMMY_ARG:
    6199         1108 :       return dummy_arg.u.non_intrinsic->sym->ts;
    6200              : 
    6201            0 :     default:
    6202            0 :       gcc_unreachable ();
    6203              :     }
    6204              : }
    6205              : 
    6206              : 
    6207              : bool
    6208        26426 : gfc_dummy_arg_is_optional (gfc_dummy_arg & dummy_arg)
    6209              : {
    6210        26426 :   switch (dummy_arg.intrinsicness)
    6211              :     {
    6212        12434 :     case GFC_INTRINSIC_DUMMY_ARG:
    6213        12434 :       return dummy_arg.u.intrinsic->optional;
    6214              : 
    6215        13992 :     case GFC_NON_INTRINSIC_DUMMY_ARG:
    6216        13992 :       return dummy_arg.u.non_intrinsic->sym->attr.optional;
    6217              : 
    6218            0 :     default:
    6219            0 :       gcc_unreachable ();
    6220              :     }
    6221              : }
        

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.