LCOV - code coverage report
Current view: top level - gcc/fortran - interface.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 92.7 % 2617 2425
Test Date: 2024-04-13 14:00:49 Functions: 96.1 % 77 74
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

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

Generated by: LCOV version 2.1-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.